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