1/*
2 * Any part of this program may be used in documents licensed under
3 * the GNU Free Documentation License, Version 1.1 or any later version
4 * published by the Free Software Foundation.
5 */
6#ifndef _PARPORT_H_
7#define _PARPORT_H_
8
9
10#include <linux/jiffies.h>
11#include <linux/proc_fs.h>
12#include <linux/spinlock.h>
13#include <linux/wait.h>
14#include <linux/irqreturn.h>
15#include <linux/semaphore.h>
16#include <asm/ptrace.h>
17#include <uapi/linux/parport.h>
18
19/* Define this later. */
20struct parport;
21struct pardevice;
22
23struct pc_parport_state {
24	unsigned int ctr;
25	unsigned int ecr;
26};
27
28struct ax_parport_state {
29	unsigned int ctr;
30	unsigned int ecr;
31	unsigned int dcsr;
32};
33
34/* used by both parport_amiga and parport_mfc3 */
35struct amiga_parport_state {
36       unsigned char data;     /* ciaa.prb */
37       unsigned char datadir;  /* ciaa.ddrb */
38       unsigned char status;   /* ciab.pra & 7 */
39       unsigned char statusdir;/* ciab.ddrb & 7 */
40};
41
42struct ax88796_parport_state {
43	unsigned char cpr;
44};
45
46struct ip32_parport_state {
47	unsigned int dcr;
48	unsigned int ecr;
49};
50
51struct parport_state {
52	union {
53		struct pc_parport_state pc;
54		/* ARC has no state. */
55		struct ax_parport_state ax;
56		struct amiga_parport_state amiga;
57		struct ax88796_parport_state ax88796;
58		/* Atari has not state. */
59		struct ip32_parport_state ip32;
60		void *misc;
61	} u;
62};
63
64struct parport_operations {
65	/* IBM PC-style virtual registers. */
66	void (*write_data)(struct parport *, unsigned char);
67	unsigned char (*read_data)(struct parport *);
68
69	void (*write_control)(struct parport *, unsigned char);
70	unsigned char (*read_control)(struct parport *);
71	unsigned char (*frob_control)(struct parport *, unsigned char mask,
72				      unsigned char val);
73
74	unsigned char (*read_status)(struct parport *);
75
76	/* IRQs. */
77	void (*enable_irq)(struct parport *);
78	void (*disable_irq)(struct parport *);
79
80	/* Data direction. */
81	void (*data_forward) (struct parport *);
82	void (*data_reverse) (struct parport *);
83
84	/* For core parport code. */
85	void (*init_state)(struct pardevice *, struct parport_state *);
86	void (*save_state)(struct parport *, struct parport_state *);
87	void (*restore_state)(struct parport *, struct parport_state *);
88
89	/* Block read/write */
90	size_t (*epp_write_data) (struct parport *port, const void *buf,
91				  size_t len, int flags);
92	size_t (*epp_read_data) (struct parport *port, void *buf, size_t len,
93				 int flags);
94	size_t (*epp_write_addr) (struct parport *port, const void *buf,
95				  size_t len, int flags);
96	size_t (*epp_read_addr) (struct parport *port, void *buf, size_t len,
97				 int flags);
98
99	size_t (*ecp_write_data) (struct parport *port, const void *buf,
100				  size_t len, int flags);
101	size_t (*ecp_read_data) (struct parport *port, void *buf, size_t len,
102				 int flags);
103	size_t (*ecp_write_addr) (struct parport *port, const void *buf,
104				  size_t len, int flags);
105
106	size_t (*compat_write_data) (struct parport *port, const void *buf,
107				     size_t len, int flags);
108	size_t (*nibble_read_data) (struct parport *port, void *buf,
109				    size_t len, int flags);
110	size_t (*byte_read_data) (struct parport *port, void *buf,
111				  size_t len, int flags);
112	struct module *owner;
113};
114
115struct parport_device_info {
116	parport_device_class class;
117	const char *class_name;
118	const char *mfr;
119	const char *model;
120	const char *cmdset;
121	const char *description;
122};
123
124/* Each device can have two callback functions:
125 *  1) a preemption function, called by the resource manager to request
126 *     that the driver relinquish control of the port.  The driver should
127 *     return zero if it agrees to release the port, and nonzero if it
128 *     refuses.  Do not call parport_release() - the kernel will do this
129 *     implicitly.
130 *
131 *  2) a wake-up function, called by the resource manager to tell drivers
132 *     that the port is available to be claimed.  If a driver wants to use
133 *     the port, it should call parport_claim() here.
134 */
135
136/* A parallel port device */
137struct pardevice {
138	const char *name;
139	struct parport *port;
140	int daisy;
141	int (*preempt)(void *);
142	void (*wakeup)(void *);
143	void *private;
144	void (*irq_func)(void *);
145	unsigned int flags;
146	struct pardevice *next;
147	struct pardevice *prev;
148	struct parport_state *state;     /* saved status over preemption */
149	wait_queue_head_t wait_q;
150	unsigned long int time;
151	unsigned long int timeslice;
152	volatile long int timeout;
153	unsigned long waiting;		 /* long req'd for set_bit --RR */
154	struct pardevice *waitprev;
155	struct pardevice *waitnext;
156	void * sysctl_table;
157};
158
159/* IEEE1284 information */
160
161/* IEEE1284 phases. These are exposed to userland through ppdev IOCTL
162 * PP[GS]ETPHASE, so do not change existing values. */
163enum ieee1284_phase {
164	IEEE1284_PH_FWD_DATA,
165	IEEE1284_PH_FWD_IDLE,
166	IEEE1284_PH_TERMINATE,
167	IEEE1284_PH_NEGOTIATION,
168	IEEE1284_PH_HBUSY_DNA,
169	IEEE1284_PH_REV_IDLE,
170	IEEE1284_PH_HBUSY_DAVAIL,
171	IEEE1284_PH_REV_DATA,
172	IEEE1284_PH_ECP_SETUP,
173	IEEE1284_PH_ECP_FWD_TO_REV,
174	IEEE1284_PH_ECP_REV_TO_FWD,
175	IEEE1284_PH_ECP_DIR_UNKNOWN,
176};
177struct ieee1284_info {
178	int mode;
179	volatile enum ieee1284_phase phase;
180	struct semaphore irq;
181};
182
183/* A parallel port */
184struct parport {
185	unsigned long base;	/* base address */
186	unsigned long base_hi;  /* base address (hi - ECR) */
187	unsigned int size;	/* IO extent */
188	const char *name;
189	unsigned int modes;
190	int irq;		/* interrupt (or -1 for none) */
191	int dma;
192	int muxport;		/* which muxport (if any) this is */
193	int portnum;		/* which physical parallel port (not mux) */
194	struct device *dev;	/* Physical device associated with IO/DMA.
195				 * This may unfortulately be null if the
196				 * port has a legacy driver.
197				 */
198
199	struct parport *physport;
200				/* If this is a non-default mux
201				   parport, i.e. we're a clone of a real
202				   physical port, this is a pointer to that
203				   port. The locking is only done in the
204				   real port.  For a clone port, the
205				   following structure members are
206				   meaningless: devices, cad, muxsel,
207				   waithead, waittail, flags, pdir,
208				   dev, ieee1284, *_lock.
209
210				   It this is a default mux parport, or
211				   there is no mux involved, this points to
212				   ourself. */
213
214	struct pardevice *devices;
215	struct pardevice *cad;	/* port owner */
216	int daisy;		/* currently selected daisy addr */
217	int muxsel;		/* currently selected mux port */
218
219	struct pardevice *waithead;
220	struct pardevice *waittail;
221
222	struct list_head list;
223	unsigned int flags;
224
225	void *sysctl_table;
226	struct parport_device_info probe_info[5]; /* 0-3 + non-IEEE1284.3 */
227	struct ieee1284_info ieee1284;
228
229	struct parport_operations *ops;
230	void *private_data;     /* for lowlevel driver */
231
232	int number;		/* port index - the `n' in `parportn' */
233	spinlock_t pardevice_lock;
234	spinlock_t waitlist_lock;
235	rwlock_t cad_lock;
236
237	int spintime;
238	atomic_t ref_count;
239
240	unsigned long devflags;
241#define PARPORT_DEVPROC_REGISTERED	0
242	struct pardevice *proc_device;	/* Currently register proc device */
243
244	struct list_head full_list;
245	struct parport *slaves[3];
246};
247
248#define DEFAULT_SPIN_TIME 500 /* us */
249
250struct parport_driver {
251	const char *name;
252	void (*attach) (struct parport *);
253	void (*detach) (struct parport *);
254	struct list_head list;
255};
256
257/* parport_register_port registers a new parallel port at the given
258   address (if one does not already exist) and returns a pointer to it.
259   This entails claiming the I/O region, IRQ and DMA.  NULL is returned
260   if initialisation fails. */
261struct parport *parport_register_port(unsigned long base, int irq, int dma,
262				      struct parport_operations *ops);
263
264/* Once a registered port is ready for high-level drivers to use, the
265   low-level driver that registered it should announce it.  This will
266   call the high-level drivers' attach() functions (after things like
267   determining the IEEE 1284.3 topology of the port and collecting
268   DeviceIDs). */
269void parport_announce_port (struct parport *port);
270
271/* Unregister a port. */
272extern void parport_remove_port(struct parport *port);
273
274/* Register a new high-level driver. */
275extern int parport_register_driver (struct parport_driver *);
276
277/* Unregister a high-level driver. */
278extern void parport_unregister_driver (struct parport_driver *);
279
280/* If parport_register_driver doesn't fit your needs, perhaps
281 * parport_find_xxx does. */
282extern struct parport *parport_find_number (int);
283extern struct parport *parport_find_base (unsigned long);
284
285/* generic irq handler, if it suits your needs */
286extern irqreturn_t parport_irq_handler(int irq, void *dev_id);
287
288/* Reference counting for ports. */
289extern struct parport *parport_get_port (struct parport *);
290extern void parport_put_port (struct parport *);
291
292/* parport_register_device declares that a device is connected to a
293   port, and tells the kernel all it needs to know.
294   - pf is the preemption function (may be NULL for no callback)
295   - kf is the wake-up function (may be NULL for no callback)
296   - irq_func is the interrupt handler (may be NULL for no interrupts)
297   - handle is a user pointer that gets handed to callback functions.  */
298struct pardevice *parport_register_device(struct parport *port,
299			  const char *name,
300			  int (*pf)(void *), void (*kf)(void *),
301			  void (*irq_func)(void *),
302			  int flags, void *handle);
303
304/* parport_unregister unlinks a device from the chain. */
305extern void parport_unregister_device(struct pardevice *dev);
306
307/* parport_claim tries to gain ownership of the port for a particular
308   driver.  This may fail (return non-zero) if another driver is busy.
309   If this driver has registered an interrupt handler, it will be
310   enabled.  */
311extern int parport_claim(struct pardevice *dev);
312
313/* parport_claim_or_block is the same, but sleeps if the port cannot
314   be claimed.  Return value is 1 if it slept, 0 normally and -errno
315   on error.  */
316extern int parport_claim_or_block(struct pardevice *dev);
317
318/* parport_release reverses a previous parport_claim.  This can never
319   fail, though the effects are undefined (except that they are bad)
320   if you didn't previously own the port.  Once you have released the
321   port you should make sure that neither your code nor the hardware
322   on the port tries to initiate any communication without first
323   re-claiming the port.  If you mess with the port state (enabling
324   ECP for example) you should clean up before releasing the port. */
325
326extern void parport_release(struct pardevice *dev);
327
328/**
329 * parport_yield - relinquish a parallel port temporarily
330 * @dev: a device on the parallel port
331 *
332 * This function relinquishes the port if it would be helpful to other
333 * drivers to do so.  Afterwards it tries to reclaim the port using
334 * parport_claim(), and the return value is the same as for
335 * parport_claim().  If it fails, the port is left unclaimed and it is
336 * the driver's responsibility to reclaim the port.
337 *
338 * The parport_yield() and parport_yield_blocking() functions are for
339 * marking points in the driver at which other drivers may claim the
340 * port and use their devices.  Yielding the port is similar to
341 * releasing it and reclaiming it, but is more efficient because no
342 * action is taken if there are no other devices needing the port.  In
343 * fact, nothing is done even if there are other devices waiting but
344 * the current device is still within its "timeslice".  The default
345 * timeslice is half a second, but it can be adjusted via the /proc
346 * interface.
347 **/
348static __inline__ int parport_yield(struct pardevice *dev)
349{
350	unsigned long int timeslip = (jiffies - dev->time);
351	if ((dev->port->waithead == NULL) || (timeslip < dev->timeslice))
352		return 0;
353	parport_release(dev);
354	return parport_claim(dev);
355}
356
357/**
358 * parport_yield_blocking - relinquish a parallel port temporarily
359 * @dev: a device on the parallel port
360 *
361 * This function relinquishes the port if it would be helpful to other
362 * drivers to do so.  Afterwards it tries to reclaim the port using
363 * parport_claim_or_block(), and the return value is the same as for
364 * parport_claim_or_block().
365 **/
366static __inline__ int parport_yield_blocking(struct pardevice *dev)
367{
368	unsigned long int timeslip = (jiffies - dev->time);
369	if ((dev->port->waithead == NULL) || (timeslip < dev->timeslice))
370		return 0;
371	parport_release(dev);
372	return parport_claim_or_block(dev);
373}
374
375/* Flags used to identify what a device does. */
376#define PARPORT_DEV_TRAN		0	/* WARNING !! DEPRECATED !! */
377#define PARPORT_DEV_LURK		(1<<0)	/* WARNING !! DEPRECATED !! */
378#define PARPORT_DEV_EXCL		(1<<1)	/* Need exclusive access. */
379
380#define PARPORT_FLAG_EXCL		(1<<1)	/* EXCL driver registered. */
381
382/* IEEE1284 functions */
383extern void parport_ieee1284_interrupt (void *);
384extern int parport_negotiate (struct parport *, int mode);
385extern ssize_t parport_write (struct parport *, const void *buf, size_t len);
386extern ssize_t parport_read (struct parport *, void *buf, size_t len);
387
388#define PARPORT_INACTIVITY_O_NONBLOCK 1
389extern long parport_set_timeout (struct pardevice *, long inactivity);
390
391extern int parport_wait_event (struct parport *, long timeout);
392extern int parport_wait_peripheral (struct parport *port,
393				    unsigned char mask,
394				    unsigned char val);
395extern int parport_poll_peripheral (struct parport *port,
396				    unsigned char mask,
397				    unsigned char val,
398				    int usec);
399
400/* For architectural drivers */
401extern size_t parport_ieee1284_write_compat (struct parport *,
402					     const void *, size_t, int);
403extern size_t parport_ieee1284_read_nibble (struct parport *,
404					    void *, size_t, int);
405extern size_t parport_ieee1284_read_byte (struct parport *,
406					  void *, size_t, int);
407extern size_t parport_ieee1284_ecp_read_data (struct parport *,
408					      void *, size_t, int);
409extern size_t parport_ieee1284_ecp_write_data (struct parport *,
410					       const void *, size_t, int);
411extern size_t parport_ieee1284_ecp_write_addr (struct parport *,
412					       const void *, size_t, int);
413extern size_t parport_ieee1284_epp_write_data (struct parport *,
414					       const void *, size_t, int);
415extern size_t parport_ieee1284_epp_read_data (struct parport *,
416					      void *, size_t, int);
417extern size_t parport_ieee1284_epp_write_addr (struct parport *,
418					       const void *, size_t, int);
419extern size_t parport_ieee1284_epp_read_addr (struct parport *,
420					      void *, size_t, int);
421
422/* IEEE1284.3 functions */
423extern int parport_daisy_init (struct parport *port);
424extern void parport_daisy_fini (struct parport *port);
425extern struct pardevice *parport_open (int devnum, const char *name);
426extern void parport_close (struct pardevice *dev);
427extern ssize_t parport_device_id (int devnum, char *buffer, size_t len);
428extern void parport_daisy_deselect_all (struct parport *port);
429extern int parport_daisy_select (struct parport *port, int daisy, int mode);
430
431/* Lowlevel drivers _can_ call this support function to handle irqs.  */
432static inline void parport_generic_irq(struct parport *port)
433{
434	parport_ieee1284_interrupt (port);
435	read_lock(&port->cad_lock);
436	if (port->cad && port->cad->irq_func)
437		port->cad->irq_func(port->cad->private);
438	read_unlock(&port->cad_lock);
439}
440
441/* Prototypes from parport_procfs */
442extern int parport_proc_register(struct parport *pp);
443extern int parport_proc_unregister(struct parport *pp);
444extern int parport_device_proc_register(struct pardevice *device);
445extern int parport_device_proc_unregister(struct pardevice *device);
446
447/* If PC hardware is the only type supported, we can optimise a bit.  */
448#if !defined(CONFIG_PARPORT_NOT_PC)
449
450#include <linux/parport_pc.h>
451#define parport_write_data(p,x)            parport_pc_write_data(p,x)
452#define parport_read_data(p)               parport_pc_read_data(p)
453#define parport_write_control(p,x)         parport_pc_write_control(p,x)
454#define parport_read_control(p)            parport_pc_read_control(p)
455#define parport_frob_control(p,m,v)        parport_pc_frob_control(p,m,v)
456#define parport_read_status(p)             parport_pc_read_status(p)
457#define parport_enable_irq(p)              parport_pc_enable_irq(p)
458#define parport_disable_irq(p)             parport_pc_disable_irq(p)
459#define parport_data_forward(p)            parport_pc_data_forward(p)
460#define parport_data_reverse(p)            parport_pc_data_reverse(p)
461
462#else  /*  !CONFIG_PARPORT_NOT_PC  */
463
464/* Generic operations vector through the dispatch table. */
465#define parport_write_data(p,x)            (p)->ops->write_data(p,x)
466#define parport_read_data(p)               (p)->ops->read_data(p)
467#define parport_write_control(p,x)         (p)->ops->write_control(p,x)
468#define parport_read_control(p)            (p)->ops->read_control(p)
469#define parport_frob_control(p,m,v)        (p)->ops->frob_control(p,m,v)
470#define parport_read_status(p)             (p)->ops->read_status(p)
471#define parport_enable_irq(p)              (p)->ops->enable_irq(p)
472#define parport_disable_irq(p)             (p)->ops->disable_irq(p)
473#define parport_data_forward(p)            (p)->ops->data_forward(p)
474#define parport_data_reverse(p)            (p)->ops->data_reverse(p)
475
476#endif /*  !CONFIG_PARPORT_NOT_PC  */
477
478extern unsigned long parport_default_timeslice;
479extern int parport_default_spintime;
480
481#endif /* _PARPORT_H_ */
482