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