lp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * Generic parallel printer driver
  3. *
  4. * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
  5. * Copyright (C) 1992,1993 by Michael K. Johnson
  6. * - Thanks much to Gunter Windau for pointing out to me where the error
  7. * checking ought to be.
  8. * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
  9. * Copyright (C) 1994 by Alan Cox (Modularised it)
  10. * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
  11. * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
  12. * "lp=" command line parameters added by Grant Guenther, grant@torque.net
  13. * lp_read (Status readback) support added by Carsten Gross,
  14. * carsten@sol.wohnheim.uni-ulm.de
  15. * Support for parport by Philip Blundell <philb@gnu.org>
  16. * Parport sharing hacking by Andrea Arcangeli
  17. * Fixed kernel_(to/from)_user memory copy to check for errors
  18. * by Riccardo Facchetti <fizban@tin.it>
  19. * 22-JAN-1998 Added support for devfs Richard Gooch <rgooch@atnf.csiro.au>
  20. * Redesigned interrupt handling for handle printers with buggy handshake
  21. * by Andrea Arcangeli, 11 May 1998
  22. * Full efficient handling of printer with buggy irq handshake (now I have
  23. * understood the meaning of the strange handshake). This is done sending new
  24. * characters if the interrupt is just happened, even if the printer say to
  25. * be still BUSY. This is needed at least with Epson Stylus Color. To enable
  26. * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below...
  27. * Fixed the irq on the rising edge of the strobe case.
  28. * Obsoleted the CAREFUL flag since a printer that doesn' t work with
  29. * CAREFUL will block a bit after in lp_check_status().
  30. * Andrea Arcangeli, 15 Oct 1998
  31. * Obsoleted and removed all the lowlevel stuff implemented in the last
  32. * month to use the IEEE1284 functions (that handle the _new_ compatibilty
  33. * mode fine).
  34. */
  35. /* This driver should, in theory, work with any parallel port that has an
  36. * appropriate low-level driver; all I/O is done through the parport
  37. * abstraction layer.
  38. *
  39. * If this driver is built into the kernel, you can configure it using the
  40. * kernel command-line. For example:
  41. *
  42. * lp=parport1,none,parport2 (bind lp0 to parport1, disable lp1 and
  43. * bind lp2 to parport2)
  44. *
  45. * lp=auto (assign lp devices to all ports that
  46. * have printers attached, as determined
  47. * by the IEEE-1284 autoprobe)
  48. *
  49. * lp=reset (reset the printer during
  50. * initialisation)
  51. *
  52. * lp=off (disable the printer driver entirely)
  53. *
  54. * If the driver is loaded as a module, similar functionality is available
  55. * using module parameters. The equivalent of the above commands would be:
  56. *
  57. * # insmod lp.o parport=1,none,2
  58. *
  59. * # insmod lp.o parport=auto
  60. *
  61. * # insmod lp.o reset=1
  62. */
  63. /* COMPATIBILITY WITH OLD KERNELS
  64. *
  65. * Under Linux 2.0 and previous versions, lp devices were bound to ports at
  66. * particular I/O addresses, as follows:
  67. *
  68. * lp0 0x3bc
  69. * lp1 0x378
  70. * lp2 0x278
  71. *
  72. * The new driver, by default, binds lp devices to parport devices as it
  73. * finds them. This means that if you only have one port, it will be bound
  74. * to lp0 regardless of its I/O address. If you need the old behaviour, you
  75. * can force it using the parameters described above.
  76. */
  77. /*
  78. * The new interrupt handling code take care of the buggy handshake
  79. * of some HP and Epson printer:
  80. * ___
  81. * ACK _______________ ___________
  82. * |__|
  83. * ____
  84. * BUSY _________ _______
  85. * |____________|
  86. *
  87. * I discovered this using the printer scanner that you can find at:
  88. *
  89. * ftp://e-mind.com/pub/linux/pscan/
  90. *
  91. * 11 May 98, Andrea Arcangeli
  92. *
  93. * My printer scanner run on an Epson Stylus Color show that such printer
  94. * generates the irq on the _rising_ edge of the STROBE. Now lp handle
  95. * this case fine too.
  96. *
  97. * 15 Oct 1998, Andrea Arcangeli
  98. *
  99. * The so called `buggy' handshake is really the well documented
  100. * compatibility mode IEEE1284 handshake. They changed the well known
  101. * Centronics handshake acking in the middle of busy expecting to not
  102. * break drivers or legacy application, while they broken linux lp
  103. * until I fixed it reverse engineering the protocol by hand some
  104. * month ago...
  105. *
  106. * 14 Dec 1998, Andrea Arcangeli
  107. *
  108. * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl)
  109. */
  110. #include <linux/module.h>
  111. #include <linux/init.h>
  112. #include <linux/errno.h>
  113. #include <linux/kernel.h>
  114. #include <linux/major.h>
  115. #include <linux/sched.h>
  116. #include <linux/slab.h>
  117. #include <linux/fcntl.h>
  118. #include <linux/delay.h>
  119. #include <linux/poll.h>
  120. #include <linux/console.h>
  121. #include <linux/device.h>
  122. #include <linux/wait.h>
  123. #include <linux/jiffies.h>
  124. #include <linux/mutex.h>
  125. #include <linux/compat.h>
  126. #include <linux/parport.h>
  127. #undef LP_STATS
  128. #include <linux/lp.h>
  129. #include <asm/irq.h>
  130. #include <asm/uaccess.h>
  131. /* if you have more than 8 printers, remember to increase LP_NO */
  132. #define LP_NO 8
  133. static DEFINE_MUTEX(lp_mutex);
  134. static struct lp_struct lp_table[LP_NO];
  135. static unsigned int lp_count = 0;
  136. static struct class *lp_class;
  137. #ifdef CONFIG_LP_CONSOLE
  138. static struct parport *console_registered;
  139. #endif /* CONFIG_LP_CONSOLE */
  140. #undef LP_DEBUG
  141. /* Bits used to manage claiming the parport device */
  142. #define LP_PREEMPT_REQUEST 1
  143. #define LP_PARPORT_CLAIMED 2
  144. /* --- low-level port access ----------------------------------- */
  145. #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
  146. #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
  147. #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
  148. #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
  149. /* Claim the parport or block trying unless we've already claimed it */
  150. static void lp_claim_parport_or_block(struct lp_struct *this_lp)
  151. {
  152. if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
  153. parport_claim_or_block (this_lp->dev);
  154. }
  155. }
  156. /* Claim the parport or block trying unless we've already claimed it */
  157. static void lp_release_parport(struct lp_struct *this_lp)
  158. {
  159. if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
  160. parport_release (this_lp->dev);
  161. }
  162. }
  163. static int lp_preempt(void *handle)
  164. {
  165. struct lp_struct *this_lp = (struct lp_struct *)handle;
  166. set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
  167. return (1);
  168. }
  169. /*
  170. * Try to negotiate to a new mode; if unsuccessful negotiate to
  171. * compatibility mode. Return the mode we ended up in.
  172. */
  173. static int lp_negotiate(struct parport * port, int mode)
  174. {
  175. if (parport_negotiate (port, mode) != 0) {
  176. mode = IEEE1284_MODE_COMPAT;
  177. parport_negotiate (port, mode);
  178. }
  179. return (mode);
  180. }
  181. static int lp_reset(int minor)
  182. {
  183. int retval;
  184. lp_claim_parport_or_block (&lp_table[minor]);
  185. w_ctr(minor, LP_PSELECP);
  186. udelay (LP_DELAY);
  187. w_ctr(minor, LP_PSELECP | LP_PINITP);
  188. retval = r_str(minor);
  189. lp_release_parport (&lp_table[minor]);
  190. return retval;
  191. }
  192. static void lp_error (int minor)
  193. {
  194. DEFINE_WAIT(wait);
  195. int polling;
  196. if (LP_F(minor) & LP_ABORT)
  197. return;
  198. polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
  199. if (polling) lp_release_parport (&lp_table[minor]);
  200. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  201. schedule_timeout(LP_TIMEOUT_POLLED);
  202. finish_wait(&lp_table[minor].waitq, &wait);
  203. if (polling) lp_claim_parport_or_block (&lp_table[minor]);
  204. else parport_yield_blocking (lp_table[minor].dev);
  205. }
  206. static int lp_check_status(int minor)
  207. {
  208. int error = 0;
  209. unsigned int last = lp_table[minor].last_error;
  210. unsigned char status = r_str(minor);
  211. if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
  212. /* No error. */
  213. last = 0;
  214. else if ((status & LP_POUTPA)) {
  215. if (last != LP_POUTPA) {
  216. last = LP_POUTPA;
  217. printk(KERN_INFO "lp%d out of paper\n", minor);
  218. }
  219. error = -ENOSPC;
  220. } else if (!(status & LP_PSELECD)) {
  221. if (last != LP_PSELECD) {
  222. last = LP_PSELECD;
  223. printk(KERN_INFO "lp%d off-line\n", minor);
  224. }
  225. error = -EIO;
  226. } else if (!(status & LP_PERRORP)) {
  227. if (last != LP_PERRORP) {
  228. last = LP_PERRORP;
  229. printk(KERN_INFO "lp%d on fire\n", minor);
  230. }
  231. error = -EIO;
  232. } else {
  233. last = 0; /* Come here if LP_CAREFUL is set and no
  234. errors are reported. */
  235. }
  236. lp_table[minor].last_error = last;
  237. if (last != 0)
  238. lp_error(minor);
  239. return error;
  240. }
  241. static int lp_wait_ready(int minor, int nonblock)
  242. {
  243. int error = 0;
  244. /* If we're not in compatibility mode, we're ready now! */
  245. if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
  246. return (0);
  247. }
  248. do {
  249. error = lp_check_status (minor);
  250. if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
  251. break;
  252. if (signal_pending (current)) {
  253. error = -EINTR;
  254. break;
  255. }
  256. } while (error);
  257. return error;
  258. }
  259. static ssize_t lp_write(struct file * file, const char __user * buf,
  260. size_t count, loff_t *ppos)
  261. {
  262. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  263. struct parport *port = lp_table[minor].dev->port;
  264. char *kbuf = lp_table[minor].lp_buffer;
  265. ssize_t retv = 0;
  266. ssize_t written;
  267. size_t copy_size = count;
  268. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  269. (LP_F(minor) & LP_ABORT));
  270. #ifdef LP_STATS
  271. if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
  272. lp_table[minor].runchars = 0;
  273. lp_table[minor].lastcall = jiffies;
  274. #endif
  275. /* Need to copy the data from user-space. */
  276. if (copy_size > LP_BUFFER_SIZE)
  277. copy_size = LP_BUFFER_SIZE;
  278. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  279. return -EINTR;
  280. if (copy_from_user (kbuf, buf, copy_size)) {
  281. retv = -EFAULT;
  282. goto out_unlock;
  283. }
  284. /* Claim Parport or sleep until it becomes available
  285. */
  286. lp_claim_parport_or_block (&lp_table[minor]);
  287. /* Go to the proper mode. */
  288. lp_table[minor].current_mode = lp_negotiate (port,
  289. lp_table[minor].best_mode);
  290. parport_set_timeout (lp_table[minor].dev,
  291. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  292. : lp_table[minor].timeout));
  293. if ((retv = lp_wait_ready (minor, nonblock)) == 0)
  294. do {
  295. /* Write the data. */
  296. written = parport_write (port, kbuf, copy_size);
  297. if (written > 0) {
  298. copy_size -= written;
  299. count -= written;
  300. buf += written;
  301. retv += written;
  302. }
  303. if (signal_pending (current)) {
  304. if (retv == 0)
  305. retv = -EINTR;
  306. break;
  307. }
  308. if (copy_size > 0) {
  309. /* incomplete write -> check error ! */
  310. int error;
  311. parport_negotiate (lp_table[minor].dev->port,
  312. IEEE1284_MODE_COMPAT);
  313. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  314. error = lp_wait_ready (minor, nonblock);
  315. if (error) {
  316. if (retv == 0)
  317. retv = error;
  318. break;
  319. } else if (nonblock) {
  320. if (retv == 0)
  321. retv = -EAGAIN;
  322. break;
  323. }
  324. parport_yield_blocking (lp_table[minor].dev);
  325. lp_table[minor].current_mode
  326. = lp_negotiate (port,
  327. lp_table[minor].best_mode);
  328. } else if (need_resched())
  329. schedule ();
  330. if (count) {
  331. copy_size = count;
  332. if (copy_size > LP_BUFFER_SIZE)
  333. copy_size = LP_BUFFER_SIZE;
  334. if (copy_from_user(kbuf, buf, copy_size)) {
  335. if (retv == 0)
  336. retv = -EFAULT;
  337. break;
  338. }
  339. }
  340. } while (count > 0);
  341. if (test_and_clear_bit(LP_PREEMPT_REQUEST,
  342. &lp_table[minor].bits)) {
  343. printk(KERN_INFO "lp%d releasing parport\n", minor);
  344. parport_negotiate (lp_table[minor].dev->port,
  345. IEEE1284_MODE_COMPAT);
  346. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  347. lp_release_parport (&lp_table[minor]);
  348. }
  349. out_unlock:
  350. mutex_unlock(&lp_table[minor].port_mutex);
  351. return retv;
  352. }
  353. #ifdef CONFIG_PARPORT_1284
  354. /* Status readback conforming to ieee1284 */
  355. static ssize_t lp_read(struct file * file, char __user * buf,
  356. size_t count, loff_t *ppos)
  357. {
  358. DEFINE_WAIT(wait);
  359. unsigned int minor=iminor(file->f_path.dentry->d_inode);
  360. struct parport *port = lp_table[minor].dev->port;
  361. ssize_t retval = 0;
  362. char *kbuf = lp_table[minor].lp_buffer;
  363. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  364. (LP_F(minor) & LP_ABORT));
  365. if (count > LP_BUFFER_SIZE)
  366. count = LP_BUFFER_SIZE;
  367. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  368. return -EINTR;
  369. lp_claim_parport_or_block (&lp_table[minor]);
  370. parport_set_timeout (lp_table[minor].dev,
  371. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  372. : lp_table[minor].timeout));
  373. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  374. if (parport_negotiate (lp_table[minor].dev->port,
  375. IEEE1284_MODE_NIBBLE)) {
  376. retval = -EIO;
  377. goto out;
  378. }
  379. while (retval == 0) {
  380. retval = parport_read (port, kbuf, count);
  381. if (retval > 0)
  382. break;
  383. if (nonblock) {
  384. retval = -EAGAIN;
  385. break;
  386. }
  387. /* Wait for data. */
  388. if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
  389. parport_negotiate (lp_table[minor].dev->port,
  390. IEEE1284_MODE_COMPAT);
  391. lp_error (minor);
  392. if (parport_negotiate (lp_table[minor].dev->port,
  393. IEEE1284_MODE_NIBBLE)) {
  394. retval = -EIO;
  395. goto out;
  396. }
  397. } else {
  398. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  399. schedule_timeout(LP_TIMEOUT_POLLED);
  400. finish_wait(&lp_table[minor].waitq, &wait);
  401. }
  402. if (signal_pending (current)) {
  403. retval = -ERESTARTSYS;
  404. break;
  405. }
  406. cond_resched ();
  407. }
  408. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  409. out:
  410. lp_release_parport (&lp_table[minor]);
  411. if (retval > 0 && copy_to_user (buf, kbuf, retval))
  412. retval = -EFAULT;
  413. mutex_unlock(&lp_table[minor].port_mutex);
  414. return retval;
  415. }
  416. #endif /* IEEE 1284 support */
  417. static int lp_open(struct inode * inode, struct file * file)
  418. {
  419. unsigned int minor = iminor(inode);
  420. int ret = 0;
  421. mutex_lock(&lp_mutex);
  422. if (minor >= LP_NO) {
  423. ret = -ENXIO;
  424. goto out;
  425. }
  426. if ((LP_F(minor) & LP_EXIST) == 0) {
  427. ret = -ENXIO;
  428. goto out;
  429. }
  430. if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
  431. ret = -EBUSY;
  432. goto out;
  433. }
  434. /* If ABORTOPEN is set and the printer is offline or out of paper,
  435. we may still want to open it to perform ioctl()s. Therefore we
  436. have commandeered O_NONBLOCK, even though it is being used in
  437. a non-standard manner. This is strictly a Linux hack, and
  438. should most likely only ever be used by the tunelp application. */
  439. if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
  440. int status;
  441. lp_claim_parport_or_block (&lp_table[minor]);
  442. status = r_str(minor);
  443. lp_release_parport (&lp_table[minor]);
  444. if (status & LP_POUTPA) {
  445. printk(KERN_INFO "lp%d out of paper\n", minor);
  446. LP_F(minor) &= ~LP_BUSY;
  447. ret = -ENOSPC;
  448. goto out;
  449. } else if (!(status & LP_PSELECD)) {
  450. printk(KERN_INFO "lp%d off-line\n", minor);
  451. LP_F(minor) &= ~LP_BUSY;
  452. ret = -EIO;
  453. goto out;
  454. } else if (!(status & LP_PERRORP)) {
  455. printk(KERN_ERR "lp%d printer error\n", minor);
  456. LP_F(minor) &= ~LP_BUSY;
  457. ret = -EIO;
  458. goto out;
  459. }
  460. }
  461. lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
  462. if (!lp_table[minor].lp_buffer) {
  463. LP_F(minor) &= ~LP_BUSY;
  464. ret = -ENOMEM;
  465. goto out;
  466. }
  467. /* Determine if the peripheral supports ECP mode */
  468. lp_claim_parport_or_block (&lp_table[minor]);
  469. if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
  470. !parport_negotiate (lp_table[minor].dev->port,
  471. IEEE1284_MODE_ECP)) {
  472. printk (KERN_INFO "lp%d: ECP mode\n", minor);
  473. lp_table[minor].best_mode = IEEE1284_MODE_ECP;
  474. } else {
  475. lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
  476. }
  477. /* Leave peripheral in compatibility mode */
  478. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  479. lp_release_parport (&lp_table[minor]);
  480. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  481. out:
  482. mutex_unlock(&lp_mutex);
  483. return ret;
  484. }
  485. static int lp_release(struct inode * inode, struct file * file)
  486. {
  487. unsigned int minor = iminor(inode);
  488. lp_claim_parport_or_block (&lp_table[minor]);
  489. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  490. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  491. lp_release_parport (&lp_table[minor]);
  492. kfree(lp_table[minor].lp_buffer);
  493. lp_table[minor].lp_buffer = NULL;
  494. LP_F(minor) &= ~LP_BUSY;
  495. return 0;
  496. }
  497. static int lp_do_ioctl(unsigned int minor, unsigned int cmd,
  498. unsigned long arg, void __user *argp)
  499. {
  500. int status;
  501. int retval = 0;
  502. #ifdef LP_DEBUG
  503. printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
  504. #endif
  505. if (minor >= LP_NO)
  506. return -ENODEV;
  507. if ((LP_F(minor) & LP_EXIST) == 0)
  508. return -ENODEV;
  509. switch ( cmd ) {
  510. case LPTIME:
  511. LP_TIME(minor) = arg * HZ/100;
  512. break;
  513. case LPCHAR:
  514. LP_CHAR(minor) = arg;
  515. break;
  516. case LPABORT:
  517. if (arg)
  518. LP_F(minor) |= LP_ABORT;
  519. else
  520. LP_F(minor) &= ~LP_ABORT;
  521. break;
  522. case LPABORTOPEN:
  523. if (arg)
  524. LP_F(minor) |= LP_ABORTOPEN;
  525. else
  526. LP_F(minor) &= ~LP_ABORTOPEN;
  527. break;
  528. case LPCAREFUL:
  529. if (arg)
  530. LP_F(minor) |= LP_CAREFUL;
  531. else
  532. LP_F(minor) &= ~LP_CAREFUL;
  533. break;
  534. case LPWAIT:
  535. LP_WAIT(minor) = arg;
  536. break;
  537. case LPSETIRQ:
  538. return -EINVAL;
  539. break;
  540. case LPGETIRQ:
  541. if (copy_to_user(argp, &LP_IRQ(minor),
  542. sizeof(int)))
  543. return -EFAULT;
  544. break;
  545. case LPGETSTATUS:
  546. lp_claim_parport_or_block (&lp_table[minor]);
  547. status = r_str(minor);
  548. lp_release_parport (&lp_table[minor]);
  549. if (copy_to_user(argp, &status, sizeof(int)))
  550. return -EFAULT;
  551. break;
  552. case LPRESET:
  553. lp_reset(minor);
  554. break;
  555. #ifdef LP_STATS
  556. case LPGETSTATS:
  557. if (copy_to_user(argp, &LP_STAT(minor),
  558. sizeof(struct lp_stats)))
  559. return -EFAULT;
  560. if (capable(CAP_SYS_ADMIN))
  561. memset(&LP_STAT(minor), 0,
  562. sizeof(struct lp_stats));
  563. break;
  564. #endif
  565. case LPGETFLAGS:
  566. status = LP_F(minor);
  567. if (copy_to_user(argp, &status, sizeof(int)))
  568. return -EFAULT;
  569. break;
  570. default:
  571. retval = -EINVAL;
  572. }
  573. return retval;
  574. }
  575. static int lp_set_timeout(unsigned int minor, struct timeval *par_timeout)
  576. {
  577. long to_jiffies;
  578. /* Convert to jiffies, place in lp_table */
  579. if ((par_timeout->tv_sec < 0) ||
  580. (par_timeout->tv_usec < 0)) {
  581. return -EINVAL;
  582. }
  583. to_jiffies = DIV_ROUND_UP(par_timeout->tv_usec, 1000000/HZ);
  584. to_jiffies += par_timeout->tv_sec * (long) HZ;
  585. if (to_jiffies <= 0) {
  586. return -EINVAL;
  587. }
  588. lp_table[minor].timeout = to_jiffies;
  589. return 0;
  590. }
  591. static long lp_ioctl(struct file *file, unsigned int cmd,
  592. unsigned long arg)
  593. {
  594. unsigned int minor;
  595. struct timeval par_timeout;
  596. int ret;
  597. minor = iminor(file->f_path.dentry->d_inode);
  598. mutex_lock(&lp_mutex);
  599. switch (cmd) {
  600. case LPSETTIMEOUT:
  601. if (copy_from_user(&par_timeout, (void __user *)arg,
  602. sizeof (struct timeval))) {
  603. ret = -EFAULT;
  604. break;
  605. }
  606. ret = lp_set_timeout(minor, &par_timeout);
  607. break;
  608. default:
  609. ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
  610. break;
  611. }
  612. mutex_unlock(&lp_mutex);
  613. return ret;
  614. }
  615. #ifdef CONFIG_COMPAT
  616. static long lp_compat_ioctl(struct file *file, unsigned int cmd,
  617. unsigned long arg)
  618. {
  619. unsigned int minor;
  620. struct timeval par_timeout;
  621. int ret;
  622. minor = iminor(file->f_path.dentry->d_inode);
  623. mutex_lock(&lp_mutex);
  624. switch (cmd) {
  625. case LPSETTIMEOUT:
  626. if (compat_get_timeval(&par_timeout, compat_ptr(arg))) {
  627. ret = -EFAULT;
  628. break;
  629. }
  630. ret = lp_set_timeout(minor, &par_timeout);
  631. break;
  632. #ifdef LP_STATS
  633. case LPGETSTATS:
  634. /* FIXME: add an implementation if you set LP_STATS */
  635. ret = -EINVAL;
  636. break;
  637. #endif
  638. default:
  639. ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
  640. break;
  641. }
  642. mutex_unlock(&lp_mutex);
  643. return ret;
  644. }
  645. #endif
  646. static const struct file_operations lp_fops = {
  647. .owner = THIS_MODULE,
  648. .write = lp_write,
  649. .unlocked_ioctl = lp_ioctl,
  650. #ifdef CONFIG_COMPAT
  651. .compat_ioctl = lp_compat_ioctl,
  652. #endif
  653. .open = lp_open,
  654. .release = lp_release,
  655. #ifdef CONFIG_PARPORT_1284
  656. .read = lp_read,
  657. #endif
  658. .llseek = noop_llseek,
  659. };
  660. /* --- support for console on the line printer ----------------- */
  661. #ifdef CONFIG_LP_CONSOLE
  662. #define CONSOLE_LP 0
  663. /* If the printer is out of paper, we can either lose the messages or
  664. * stall until the printer is happy again. Define CONSOLE_LP_STRICT
  665. * non-zero to get the latter behaviour. */
  666. #define CONSOLE_LP_STRICT 1
  667. /* The console must be locked when we get here. */
  668. static void lp_console_write (struct console *co, const char *s,
  669. unsigned count)
  670. {
  671. struct pardevice *dev = lp_table[CONSOLE_LP].dev;
  672. struct parport *port = dev->port;
  673. ssize_t written;
  674. if (parport_claim (dev))
  675. /* Nothing we can do. */
  676. return;
  677. parport_set_timeout (dev, 0);
  678. /* Go to compatibility mode. */
  679. parport_negotiate (port, IEEE1284_MODE_COMPAT);
  680. do {
  681. /* Write the data, converting LF->CRLF as we go. */
  682. ssize_t canwrite = count;
  683. char *lf = memchr (s, '\n', count);
  684. if (lf)
  685. canwrite = lf - s;
  686. if (canwrite > 0) {
  687. written = parport_write (port, s, canwrite);
  688. if (written <= 0)
  689. continue;
  690. s += written;
  691. count -= written;
  692. canwrite -= written;
  693. }
  694. if (lf && canwrite <= 0) {
  695. const char *crlf = "\r\n";
  696. int i = 2;
  697. /* Dodge the original '\n', and put '\r\n' instead. */
  698. s++;
  699. count--;
  700. do {
  701. written = parport_write (port, crlf, i);
  702. if (written > 0)
  703. i -= written, crlf += written;
  704. } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
  705. }
  706. } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
  707. parport_release (dev);
  708. }
  709. static struct console lpcons = {
  710. .name = "lp",
  711. .write = lp_console_write,
  712. .flags = CON_PRINTBUFFER,
  713. };
  714. #endif /* console on line printer */
  715. /* --- initialisation code ------------------------------------- */
  716. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  717. static char *parport[LP_NO];
  718. static bool reset;
  719. module_param_array(parport, charp, NULL, 0);
  720. module_param(reset, bool, 0);
  721. #ifndef MODULE
  722. static int __init lp_setup (char *str)
  723. {
  724. static int parport_ptr;
  725. int x;
  726. if (get_option(&str, &x)) {
  727. if (x == 0) {
  728. /* disable driver on "lp=" or "lp=0" */
  729. parport_nr[0] = LP_PARPORT_OFF;
  730. } else {
  731. printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
  732. return 0;
  733. }
  734. } else if (!strncmp(str, "parport", 7)) {
  735. int n = simple_strtoul(str+7, NULL, 10);
  736. if (parport_ptr < LP_NO)
  737. parport_nr[parport_ptr++] = n;
  738. else
  739. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  740. str);
  741. } else if (!strcmp(str, "auto")) {
  742. parport_nr[0] = LP_PARPORT_AUTO;
  743. } else if (!strcmp(str, "none")) {
  744. if (parport_ptr < LP_NO)
  745. parport_nr[parport_ptr++] = LP_PARPORT_NONE;
  746. else
  747. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  748. str);
  749. } else if (!strcmp(str, "reset")) {
  750. reset = 1;
  751. }
  752. return 1;
  753. }
  754. #endif
  755. static int lp_register(int nr, struct parport *port)
  756. {
  757. lp_table[nr].dev = parport_register_device(port, "lp",
  758. lp_preempt, NULL, NULL, 0,
  759. (void *) &lp_table[nr]);
  760. if (lp_table[nr].dev == NULL)
  761. return 1;
  762. lp_table[nr].flags |= LP_EXIST;
  763. if (reset)
  764. lp_reset(nr);
  765. device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
  766. "lp%d", nr);
  767. printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
  768. (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
  769. #ifdef CONFIG_LP_CONSOLE
  770. if (!nr) {
  771. if (port->modes & PARPORT_MODE_SAFEININT) {
  772. register_console(&lpcons);
  773. console_registered = port;
  774. printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
  775. } else
  776. printk (KERN_ERR "lp%d: cannot run console on %s\n",
  777. CONSOLE_LP, port->name);
  778. }
  779. #endif
  780. return 0;
  781. }
  782. static void lp_attach (struct parport *port)
  783. {
  784. unsigned int i;
  785. switch (parport_nr[0]) {
  786. case LP_PARPORT_UNSPEC:
  787. case LP_PARPORT_AUTO:
  788. if (parport_nr[0] == LP_PARPORT_AUTO &&
  789. port->probe_info[0].class != PARPORT_CLASS_PRINTER)
  790. return;
  791. if (lp_count == LP_NO) {
  792. printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
  793. return;
  794. }
  795. if (!lp_register(lp_count, port))
  796. lp_count++;
  797. break;
  798. default:
  799. for (i = 0; i < LP_NO; i++) {
  800. if (port->number == parport_nr[i]) {
  801. if (!lp_register(i, port))
  802. lp_count++;
  803. break;
  804. }
  805. }
  806. break;
  807. }
  808. }
  809. static void lp_detach (struct parport *port)
  810. {
  811. /* Write this some day. */
  812. #ifdef CONFIG_LP_CONSOLE
  813. if (console_registered == port) {
  814. unregister_console(&lpcons);
  815. console_registered = NULL;
  816. }
  817. #endif /* CONFIG_LP_CONSOLE */
  818. }
  819. static struct parport_driver lp_driver = {
  820. .name = "lp",
  821. .attach = lp_attach,
  822. .detach = lp_detach,
  823. };
  824. static int __init lp_init (void)
  825. {
  826. int i, err = 0;
  827. if (parport_nr[0] == LP_PARPORT_OFF)
  828. return 0;
  829. for (i = 0; i < LP_NO; i++) {
  830. lp_table[i].dev = NULL;
  831. lp_table[i].flags = 0;
  832. lp_table[i].chars = LP_INIT_CHAR;
  833. lp_table[i].time = LP_INIT_TIME;
  834. lp_table[i].wait = LP_INIT_WAIT;
  835. lp_table[i].lp_buffer = NULL;
  836. #ifdef LP_STATS
  837. lp_table[i].lastcall = 0;
  838. lp_table[i].runchars = 0;
  839. memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
  840. #endif
  841. lp_table[i].last_error = 0;
  842. init_waitqueue_head (&lp_table[i].waitq);
  843. init_waitqueue_head (&lp_table[i].dataq);
  844. mutex_init(&lp_table[i].port_mutex);
  845. lp_table[i].timeout = 10 * HZ;
  846. }
  847. if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
  848. printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
  849. return -EIO;
  850. }
  851. lp_class = class_create(THIS_MODULE, "printer");
  852. if (IS_ERR(lp_class)) {
  853. err = PTR_ERR(lp_class);
  854. goto out_reg;
  855. }
  856. if (parport_register_driver (&lp_driver)) {
  857. printk (KERN_ERR "lp: unable to register with parport\n");
  858. err = -EIO;
  859. goto out_class;
  860. }
  861. if (!lp_count) {
  862. printk (KERN_INFO "lp: driver loaded but no devices found\n");
  863. #ifndef CONFIG_PARPORT_1284
  864. if (parport_nr[0] == LP_PARPORT_AUTO)
  865. printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
  866. #endif
  867. }
  868. return 0;
  869. out_class:
  870. class_destroy(lp_class);
  871. out_reg:
  872. unregister_chrdev(LP_MAJOR, "lp");
  873. return err;
  874. }
  875. static int __init lp_init_module (void)
  876. {
  877. if (parport[0]) {
  878. /* The user gave some parameters. Let's see what they were. */
  879. if (!strncmp(parport[0], "auto", 4))
  880. parport_nr[0] = LP_PARPORT_AUTO;
  881. else {
  882. int n;
  883. for (n = 0; n < LP_NO && parport[n]; n++) {
  884. if (!strncmp(parport[n], "none", 4))
  885. parport_nr[n] = LP_PARPORT_NONE;
  886. else {
  887. char *ep;
  888. unsigned long r = simple_strtoul(parport[n], &ep, 0);
  889. if (ep != parport[n])
  890. parport_nr[n] = r;
  891. else {
  892. printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
  893. return -ENODEV;
  894. }
  895. }
  896. }
  897. }
  898. }
  899. return lp_init();
  900. }
  901. static void lp_cleanup_module (void)
  902. {
  903. unsigned int offset;
  904. parport_unregister_driver (&lp_driver);
  905. #ifdef CONFIG_LP_CONSOLE
  906. unregister_console (&lpcons);
  907. #endif
  908. unregister_chrdev(LP_MAJOR, "lp");
  909. for (offset = 0; offset < LP_NO; offset++) {
  910. if (lp_table[offset].dev == NULL)
  911. continue;
  912. parport_unregister_device(lp_table[offset].dev);
  913. device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
  914. }
  915. class_destroy(lp_class);
  916. }
  917. __setup("lp=", lp_setup);
  918. module_init(lp_init_module);
  919. module_exit(lp_cleanup_module);
  920. MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
  921. MODULE_LICENSE("GPL");