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.
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 <linux/device.h>
17 #include <asm/ptrace.h>
18 #include <uapi/linux/parport.h>
20 /* Define this later. */
24 struct pc_parport_state {
29 struct ax_parport_state {
35 /* used by both parport_amiga and parport_mfc3 */
36 struct amiga_parport_state {
37 unsigned char data; /* ciaa.prb */
38 unsigned char datadir; /* ciaa.ddrb */
39 unsigned char status; /* ciab.pra & 7 */
40 unsigned char statusdir;/* ciab.ddrb & 7 */
43 struct ax88796_parport_state {
47 struct ip32_parport_state {
52 struct parport_state {
54 struct pc_parport_state pc;
55 /* ARC has no state. */
56 struct ax_parport_state ax;
57 struct amiga_parport_state amiga;
58 struct ax88796_parport_state ax88796;
59 /* Atari has not state. */
60 struct ip32_parport_state ip32;
65 struct parport_operations {
66 /* IBM PC-style virtual registers. */
67 void (*write_data)(struct parport *, unsigned char);
68 unsigned char (*read_data)(struct parport *);
70 void (*write_control)(struct parport *, unsigned char);
71 unsigned char (*read_control)(struct parport *);
72 unsigned char (*frob_control)(struct parport *, unsigned char mask,
75 unsigned char (*read_status)(struct parport *);
78 void (*enable_irq)(struct parport *);
79 void (*disable_irq)(struct parport *);
82 void (*data_forward) (struct parport *);
83 void (*data_reverse) (struct parport *);
85 /* For core parport code. */
86 void (*init_state)(struct pardevice *, struct parport_state *);
87 void (*save_state)(struct parport *, struct parport_state *);
88 void (*restore_state)(struct parport *, struct parport_state *);
90 /* Block read/write */
91 size_t (*epp_write_data) (struct parport *port, const void *buf,
92 size_t len, int flags);
93 size_t (*epp_read_data) (struct parport *port, void *buf, size_t len,
95 size_t (*epp_write_addr) (struct parport *port, const void *buf,
96 size_t len, int flags);
97 size_t (*epp_read_addr) (struct parport *port, void *buf, size_t len,
100 size_t (*ecp_write_data) (struct parport *port, const void *buf,
101 size_t len, int flags);
102 size_t (*ecp_read_data) (struct parport *port, void *buf, size_t len,
104 size_t (*ecp_write_addr) (struct parport *port, const void *buf,
105 size_t len, int flags);
107 size_t (*compat_write_data) (struct parport *port, const void *buf,
108 size_t len, int flags);
109 size_t (*nibble_read_data) (struct parport *port, void *buf,
110 size_t len, int flags);
111 size_t (*byte_read_data) (struct parport *port, void *buf,
112 size_t len, int flags);
113 struct module *owner;
116 struct parport_device_info {
117 parport_device_class class;
118 const char *class_name;
122 const char *description;
125 /* Each device can have two callback functions:
126 * 1) a preemption function, called by the resource manager to request
127 * that the driver relinquish control of the port. The driver should
128 * return zero if it agrees to release the port, and nonzero if it
129 * refuses. Do not call parport_release() - the kernel will do this
132 * 2) a wake-up function, called by the resource manager to tell drivers
133 * that the port is available to be claimed. If a driver wants to use
134 * the port, it should call parport_claim() here.
137 /* A parallel port device */
140 struct parport *port;
142 int (*preempt)(void *);
143 void (*wakeup)(void *);
145 void (*irq_func)(void *);
147 struct pardevice *next;
148 struct pardevice *prev;
151 struct parport_state *state; /* saved status over preemption */
152 wait_queue_head_t wait_q;
153 unsigned long int time;
154 unsigned long int timeslice;
155 volatile long int timeout;
156 unsigned long waiting; /* long req'd for set_bit --RR */
157 struct pardevice *waitprev;
158 struct pardevice *waitnext;
162 #define to_pardevice(n) container_of(n, struct pardevice, dev)
164 /* IEEE1284 information */
166 /* IEEE1284 phases. These are exposed to userland through ppdev IOCTL
167 * PP[GS]ETPHASE, so do not change existing values. */
168 enum ieee1284_phase {
169 IEEE1284_PH_FWD_DATA,
170 IEEE1284_PH_FWD_IDLE,
171 IEEE1284_PH_TERMINATE,
172 IEEE1284_PH_NEGOTIATION,
173 IEEE1284_PH_HBUSY_DNA,
174 IEEE1284_PH_REV_IDLE,
175 IEEE1284_PH_HBUSY_DAVAIL,
176 IEEE1284_PH_REV_DATA,
177 IEEE1284_PH_ECP_SETUP,
178 IEEE1284_PH_ECP_FWD_TO_REV,
179 IEEE1284_PH_ECP_REV_TO_FWD,
180 IEEE1284_PH_ECP_DIR_UNKNOWN,
182 struct ieee1284_info {
184 volatile enum ieee1284_phase phase;
185 struct semaphore irq;
188 /* A parallel port */
190 unsigned long base; /* base address */
191 unsigned long base_hi; /* base address (hi - ECR) */
192 unsigned int size; /* IO extent */
195 int irq; /* interrupt (or -1 for none) */
197 int muxport; /* which muxport (if any) this is */
198 int portnum; /* which physical parallel port (not mux) */
199 struct device *dev; /* Physical device associated with IO/DMA.
200 * This may unfortulately be null if the
201 * port has a legacy driver.
203 struct device bus_dev; /* to link with the bus */
204 struct parport *physport;
205 /* If this is a non-default mux
206 parport, i.e. we're a clone of a real
207 physical port, this is a pointer to that
208 port. The locking is only done in the
209 real port. For a clone port, the
210 following structure members are
211 meaningless: devices, cad, muxsel,
212 waithead, waittail, flags, pdir,
213 dev, ieee1284, *_lock.
215 It this is a default mux parport, or
216 there is no mux involved, this points to
219 struct pardevice *devices;
220 struct pardevice *cad; /* port owner */
221 int daisy; /* currently selected daisy addr */
222 int muxsel; /* currently selected mux port */
224 struct pardevice *waithead;
225 struct pardevice *waittail;
227 struct list_head list;
228 struct timer_list timer;
232 struct parport_device_info probe_info[5]; /* 0-3 + non-IEEE1284.3 */
233 struct ieee1284_info ieee1284;
235 struct parport_operations *ops;
236 void *private_data; /* for lowlevel driver */
238 int number; /* port index - the `n' in `parportn' */
239 spinlock_t pardevice_lock;
240 spinlock_t waitlist_lock;
246 unsigned long devflags;
247 #define PARPORT_DEVPROC_REGISTERED 0
248 struct pardevice *proc_device; /* Currently register proc device */
250 struct list_head full_list;
251 struct parport *slaves[3];
254 #define to_parport_dev(n) container_of(n, struct parport, bus_dev)
256 #define DEFAULT_SPIN_TIME 500 /* us */
258 struct parport_driver {
260 void (*attach) (struct parport *);
261 void (*detach) (struct parport *);
262 void (*match_port)(struct parport *);
263 int (*probe)(struct pardevice *);
264 struct device_driver driver;
266 struct list_head list;
269 #define to_parport_driver(n) container_of(n, struct parport_driver, driver)
271 int parport_bus_init(void);
272 void parport_bus_exit(void);
274 /* parport_register_port registers a new parallel port at the given
275 address (if one does not already exist) and returns a pointer to it.
276 This entails claiming the I/O region, IRQ and DMA. NULL is returned
277 if initialisation fails. */
278 struct parport *parport_register_port(unsigned long base, int irq, int dma,
279 struct parport_operations *ops);
281 /* Once a registered port is ready for high-level drivers to use, the
282 low-level driver that registered it should announce it. This will
283 call the high-level drivers' attach() functions (after things like
284 determining the IEEE 1284.3 topology of the port and collecting
286 void parport_announce_port (struct parport *port);
288 /* Unregister a port. */
289 extern void parport_remove_port(struct parport *port);
291 /* Register a new high-level driver. */
293 int __must_check __parport_register_driver(struct parport_driver *,
295 const char *mod_name);
297 * parport_register_driver must be a macro so that KBUILD_MODNAME can
302 * parport_register_driver - register a parallel port device driver
303 * @driver: structure describing the driver
305 * This can be called by a parallel port device driver in order
306 * to receive notifications about ports being found in the
307 * system, as well as ports no longer available.
309 * If devmodel is true then the new device model is used
312 * The @driver structure is allocated by the caller and must not be
313 * deallocated until after calling parport_unregister_driver().
315 * If using the non device model:
316 * The driver's attach() function may block. The port that
317 * attach() is given will be valid for the duration of the
318 * callback, but if the driver wants to take a copy of the
319 * pointer it must call parport_get_port() to do so. Calling
320 * parport_register_device() on that port will do this for you.
322 * The driver's detach() function may block. The port that
323 * detach() is given will be valid for the duration of the
324 * callback, but if the driver wants to take a copy of the
325 * pointer it must call parport_get_port() to do so.
328 * Returns 0 on success. The non device model will always succeeds.
329 * but the new device model can fail and will return the error code.
331 #define parport_register_driver(driver) \
332 __parport_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
334 /* Unregister a high-level driver. */
335 void parport_unregister_driver(struct parport_driver *);
338 * module_parport_driver() - Helper macro for registering a modular parport driver
339 * @__parport_driver: struct parport_driver to be used
341 * Helper macro for parport drivers which do not do anything special in module
342 * init and exit. This eliminates a lot of boilerplate. Each module may only
343 * use this macro once, and calling it replaces module_init() and module_exit().
345 #define module_parport_driver(__parport_driver) \
346 module_driver(__parport_driver, parport_register_driver, parport_unregister_driver)
348 /* If parport_register_driver doesn't fit your needs, perhaps
349 * parport_find_xxx does. */
350 extern struct parport *parport_find_number (int);
351 extern struct parport *parport_find_base (unsigned long);
353 /* generic irq handler, if it suits your needs */
354 extern irqreturn_t parport_irq_handler(int irq, void *dev_id);
356 /* Reference counting for ports. */
357 extern struct parport *parport_get_port (struct parport *);
358 extern void parport_put_port (struct parport *);
359 void parport_del_port(struct parport *);
362 int (*preempt)(void *);
363 void (*wakeup)(void *);
365 void (*irq_func)(void *);
370 * parport_register_dev_model declares that a device is connected to a
371 * port, and tells the kernel all it needs to know.
374 parport_register_dev_model(struct parport *port, const char *name,
375 const struct pardev_cb *par_dev_cb, int cnt);
377 /* parport_unregister unlinks a device from the chain. */
378 extern void parport_unregister_device(struct pardevice *dev);
380 /* parport_claim tries to gain ownership of the port for a particular
381 driver. This may fail (return non-zero) if another driver is busy.
382 If this driver has registered an interrupt handler, it will be
384 extern int parport_claim(struct pardevice *dev);
386 /* parport_claim_or_block is the same, but sleeps if the port cannot
387 be claimed. Return value is 1 if it slept, 0 normally and -errno
389 extern int parport_claim_or_block(struct pardevice *dev);
391 /* parport_release reverses a previous parport_claim. This can never
392 fail, though the effects are undefined (except that they are bad)
393 if you didn't previously own the port. Once you have released the
394 port you should make sure that neither your code nor the hardware
395 on the port tries to initiate any communication without first
396 re-claiming the port. If you mess with the port state (enabling
397 ECP for example) you should clean up before releasing the port. */
399 extern void parport_release(struct pardevice *dev);
402 * parport_yield - relinquish a parallel port temporarily
403 * @dev: a device on the parallel port
405 * This function relinquishes the port if it would be helpful to other
406 * drivers to do so. Afterwards it tries to reclaim the port using
407 * parport_claim(), and the return value is the same as for
408 * parport_claim(). If it fails, the port is left unclaimed and it is
409 * the driver's responsibility to reclaim the port.
411 * The parport_yield() and parport_yield_blocking() functions are for
412 * marking points in the driver at which other drivers may claim the
413 * port and use their devices. Yielding the port is similar to
414 * releasing it and reclaiming it, but is more efficient because no
415 * action is taken if there are no other devices needing the port. In
416 * fact, nothing is done even if there are other devices waiting but
417 * the current device is still within its "timeslice". The default
418 * timeslice is half a second, but it can be adjusted via the /proc
421 static __inline__ int parport_yield(struct pardevice *dev)
423 unsigned long int timeslip = (jiffies - dev->time);
424 if ((dev->port->waithead == NULL) || (timeslip < dev->timeslice))
426 parport_release(dev);
427 return parport_claim(dev);
431 * parport_yield_blocking - relinquish a parallel port temporarily
432 * @dev: a device on the parallel port
434 * This function relinquishes the port if it would be helpful to other
435 * drivers to do so. Afterwards it tries to reclaim the port using
436 * parport_claim_or_block(), and the return value is the same as for
437 * parport_claim_or_block().
439 static __inline__ int parport_yield_blocking(struct pardevice *dev)
441 unsigned long int timeslip = (jiffies - dev->time);
442 if ((dev->port->waithead == NULL) || (timeslip < dev->timeslice))
444 parport_release(dev);
445 return parport_claim_or_block(dev);
448 /* Flags used to identify what a device does. */
449 #define PARPORT_DEV_TRAN 0 /* WARNING !! DEPRECATED !! */
450 #define PARPORT_DEV_LURK (1<<0) /* WARNING !! DEPRECATED !! */
451 #define PARPORT_DEV_EXCL (1<<1) /* Need exclusive access. */
453 #define PARPORT_FLAG_EXCL (1<<1) /* EXCL driver registered. */
455 /* IEEE1284 functions */
456 extern void parport_ieee1284_interrupt (void *);
457 extern int parport_negotiate (struct parport *, int mode);
458 extern ssize_t parport_write (struct parport *, const void *buf, size_t len);
459 extern ssize_t parport_read (struct parport *, void *buf, size_t len);
461 #define PARPORT_INACTIVITY_O_NONBLOCK 1
462 extern long parport_set_timeout (struct pardevice *, long inactivity);
464 extern int parport_wait_event (struct parport *, long timeout);
465 extern int parport_wait_peripheral (struct parport *port,
468 extern int parport_poll_peripheral (struct parport *port,
473 /* For architectural drivers */
474 extern size_t parport_ieee1284_write_compat (struct parport *,
475 const void *, size_t, int);
476 extern size_t parport_ieee1284_read_nibble (struct parport *,
477 void *, size_t, int);
478 extern size_t parport_ieee1284_read_byte (struct parport *,
479 void *, size_t, int);
480 extern size_t parport_ieee1284_ecp_read_data (struct parport *,
481 void *, size_t, int);
482 extern size_t parport_ieee1284_ecp_write_data (struct parport *,
483 const void *, size_t, int);
484 extern size_t parport_ieee1284_ecp_write_addr (struct parport *,
485 const void *, size_t, int);
486 extern size_t parport_ieee1284_epp_write_data (struct parport *,
487 const void *, size_t, int);
488 extern size_t parport_ieee1284_epp_read_data (struct parport *,
489 void *, size_t, int);
490 extern size_t parport_ieee1284_epp_write_addr (struct parport *,
491 const void *, size_t, int);
492 extern size_t parport_ieee1284_epp_read_addr (struct parport *,
493 void *, size_t, int);
495 /* IEEE1284.3 functions */
496 #define daisy_dev_name "Device ID probe"
497 extern int parport_daisy_init (struct parport *port);
498 extern void parport_daisy_fini (struct parport *port);
499 extern struct pardevice *parport_open (int devnum, const char *name);
500 extern void parport_close (struct pardevice *dev);
501 extern ssize_t parport_device_id (int devnum, char *buffer, size_t len);
502 extern void parport_daisy_deselect_all (struct parport *port);
503 extern int parport_daisy_select (struct parport *port, int daisy, int mode);
505 /* Lowlevel drivers _can_ call this support function to handle irqs. */
506 static inline void parport_generic_irq(struct parport *port)
508 parport_ieee1284_interrupt (port);
509 read_lock(&port->cad_lock);
510 if (port->cad && port->cad->irq_func)
511 port->cad->irq_func(port->cad->private);
512 read_unlock(&port->cad_lock);
515 /* Prototypes from parport_procfs */
516 extern int parport_proc_register(struct parport *pp);
517 extern int parport_proc_unregister(struct parport *pp);
518 extern int parport_device_proc_register(struct pardevice *device);
519 extern int parport_device_proc_unregister(struct pardevice *device);
521 /* If PC hardware is the only type supported, we can optimise a bit. */
522 #if !defined(CONFIG_PARPORT_NOT_PC)
524 #include <linux/parport_pc.h>
525 #define parport_write_data(p,x) parport_pc_write_data(p,x)
526 #define parport_read_data(p) parport_pc_read_data(p)
527 #define parport_write_control(p,x) parport_pc_write_control(p,x)
528 #define parport_read_control(p) parport_pc_read_control(p)
529 #define parport_frob_control(p,m,v) parport_pc_frob_control(p,m,v)
530 #define parport_read_status(p) parport_pc_read_status(p)
531 #define parport_enable_irq(p) parport_pc_enable_irq(p)
532 #define parport_disable_irq(p) parport_pc_disable_irq(p)
533 #define parport_data_forward(p) parport_pc_data_forward(p)
534 #define parport_data_reverse(p) parport_pc_data_reverse(p)
536 #else /* !CONFIG_PARPORT_NOT_PC */
538 /* Generic operations vector through the dispatch table. */
539 #define parport_write_data(p,x) (p)->ops->write_data(p,x)
540 #define parport_read_data(p) (p)->ops->read_data(p)
541 #define parport_write_control(p,x) (p)->ops->write_control(p,x)
542 #define parport_read_control(p) (p)->ops->read_control(p)
543 #define parport_frob_control(p,m,v) (p)->ops->frob_control(p,m,v)
544 #define parport_read_status(p) (p)->ops->read_status(p)
545 #define parport_enable_irq(p) (p)->ops->enable_irq(p)
546 #define parport_disable_irq(p) (p)->ops->disable_irq(p)
547 #define parport_data_forward(p) (p)->ops->data_forward(p)
548 #define parport_data_reverse(p) (p)->ops->data_reverse(p)
550 #endif /* !CONFIG_PARPORT_NOT_PC */
552 extern unsigned long parport_default_timeslice;
553 extern int parport_default_spintime;
555 #endif /* _PARPORT_H_ */