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