parport.h 18 KB

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