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. #include <asm/system.h>
  132. /* if you have more than 8 printers, remember to increase LP_NO */
  133. #define LP_NO 8
  134. static DEFINE_MUTEX(lp_mutex);
  135. static struct lp_struct lp_table[LP_NO];
  136. static unsigned int lp_count = 0;
  137. static struct class *lp_class;
  138. #ifdef CONFIG_LP_CONSOLE
  139. static struct parport *console_registered;
  140. #endif /* CONFIG_LP_CONSOLE */
  141. #undef LP_DEBUG
  142. /* Bits used to manage claiming the parport device */
  143. #define LP_PREEMPT_REQUEST 1
  144. #define LP_PARPORT_CLAIMED 2
  145. /* --- low-level port access ----------------------------------- */
  146. #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
  147. #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
  148. #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
  149. #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
  150. /* Claim the parport or block trying unless we've already claimed it */
  151. static void lp_claim_parport_or_block(struct lp_struct *this_lp)
  152. {
  153. if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
  154. parport_claim_or_block (this_lp->dev);
  155. }
  156. }
  157. /* Claim the parport or block trying unless we've already claimed it */
  158. static void lp_release_parport(struct lp_struct *this_lp)
  159. {
  160. if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
  161. parport_release (this_lp->dev);
  162. }
  163. }
  164. static int lp_preempt(void *handle)
  165. {
  166. struct lp_struct *this_lp = (struct lp_struct *)handle;
  167. set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
  168. return (1);
  169. }
  170. /*
  171. * Try to negotiate to a new mode; if unsuccessful negotiate to
  172. * compatibility mode. Return the mode we ended up in.
  173. */
  174. static int lp_negotiate(struct parport * port, int mode)
  175. {
  176. if (parport_negotiate (port, mode) != 0) {
  177. mode = IEEE1284_MODE_COMPAT;
  178. parport_negotiate (port, mode);
  179. }
  180. return (mode);
  181. }
  182. static int lp_reset(int minor)
  183. {
  184. int retval;
  185. lp_claim_parport_or_block (&lp_table[minor]);
  186. w_ctr(minor, LP_PSELECP);
  187. udelay (LP_DELAY);
  188. w_ctr(minor, LP_PSELECP | LP_PINITP);
  189. retval = r_str(minor);
  190. lp_release_parport (&lp_table[minor]);
  191. return retval;
  192. }
  193. static void lp_error (int minor)
  194. {
  195. DEFINE_WAIT(wait);
  196. int polling;
  197. if (LP_F(minor) & LP_ABORT)
  198. return;
  199. polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
  200. if (polling) lp_release_parport (&lp_table[minor]);
  201. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  202. schedule_timeout(LP_TIMEOUT_POLLED);
  203. finish_wait(&lp_table[minor].waitq, &wait);
  204. if (polling) lp_claim_parport_or_block (&lp_table[minor]);
  205. else parport_yield_blocking (lp_table[minor].dev);
  206. }
  207. static int lp_check_status(int minor)
  208. {
  209. int error = 0;
  210. unsigned int last = lp_table[minor].last_error;
  211. unsigned char status = r_str(minor);
  212. if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
  213. /* No error. */
  214. last = 0;
  215. else if ((status & LP_POUTPA)) {
  216. if (last != LP_POUTPA) {
  217. last = LP_POUTPA;
  218. printk(KERN_INFO "lp%d out of paper\n", minor);
  219. }
  220. error = -ENOSPC;
  221. } else if (!(status & LP_PSELECD)) {
  222. if (last != LP_PSELECD) {
  223. last = LP_PSELECD;
  224. printk(KERN_INFO "lp%d off-line\n", minor);
  225. }
  226. error = -EIO;
  227. } else if (!(status & LP_PERRORP)) {
  228. if (last != LP_PERRORP) {
  229. last = LP_PERRORP;
  230. printk(KERN_INFO "lp%d on fire\n", minor);
  231. }
  232. error = -EIO;
  233. } else {
  234. last = 0; /* Come here if LP_CAREFUL is set and no
  235. errors are reported. */
  236. }
  237. lp_table[minor].last_error = last;
  238. if (last != 0)
  239. lp_error(minor);
  240. return error;
  241. }
  242. static int lp_wait_ready(int minor, int nonblock)
  243. {
  244. int error = 0;
  245. /* If we're not in compatibility mode, we're ready now! */
  246. if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
  247. return (0);
  248. }
  249. do {
  250. error = lp_check_status (minor);
  251. if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
  252. break;
  253. if (signal_pending (current)) {
  254. error = -EINTR;
  255. break;
  256. }
  257. } while (error);
  258. return error;
  259. }
  260. static ssize_t lp_write(struct file * file, const char __user * buf,
  261. size_t count, loff_t *ppos)
  262. {
  263. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  264. struct parport *port = lp_table[minor].dev->port;
  265. char *kbuf = lp_table[minor].lp_buffer;
  266. ssize_t retv = 0;
  267. ssize_t written;
  268. size_t copy_size = count;
  269. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  270. (LP_F(minor) & LP_ABORT));
  271. #ifdef LP_STATS
  272. if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
  273. lp_table[minor].runchars = 0;
  274. lp_table[minor].lastcall = jiffies;
  275. #endif
  276. /* Need to copy the data from user-space. */
  277. if (copy_size > LP_BUFFER_SIZE)
  278. copy_size = LP_BUFFER_SIZE;
  279. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  280. return -EINTR;
  281. if (copy_from_user (kbuf, buf, copy_size)) {
  282. retv = -EFAULT;
  283. goto out_unlock;
  284. }
  285. /* Claim Parport or sleep until it becomes available
  286. */
  287. lp_claim_parport_or_block (&lp_table[minor]);
  288. /* Go to the proper mode. */
  289. lp_table[minor].current_mode = lp_negotiate (port,
  290. lp_table[minor].best_mode);
  291. parport_set_timeout (lp_table[minor].dev,
  292. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  293. : lp_table[minor].timeout));
  294. if ((retv = lp_wait_ready (minor, nonblock)) == 0)
  295. do {
  296. /* Write the data. */
  297. written = parport_write (port, kbuf, copy_size);
  298. if (written > 0) {
  299. copy_size -= written;
  300. count -= written;
  301. buf += written;
  302. retv += written;
  303. }
  304. if (signal_pending (current)) {
  305. if (retv == 0)
  306. retv = -EINTR;
  307. break;
  308. }
  309. if (copy_size > 0) {
  310. /* incomplete write -> check error ! */
  311. int error;
  312. parport_negotiate (lp_table[minor].dev->port,
  313. IEEE1284_MODE_COMPAT);
  314. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  315. error = lp_wait_ready (minor, nonblock);
  316. if (error) {
  317. if (retv == 0)
  318. retv = error;
  319. break;
  320. } else if (nonblock) {
  321. if (retv == 0)
  322. retv = -EAGAIN;
  323. break;
  324. }
  325. parport_yield_blocking (lp_table[minor].dev);
  326. lp_table[minor].current_mode
  327. = lp_negotiate (port,
  328. lp_table[minor].best_mode);
  329. } else if (need_resched())
  330. schedule ();
  331. if (count) {
  332. copy_size = count;
  333. if (copy_size > LP_BUFFER_SIZE)
  334. copy_size = LP_BUFFER_SIZE;
  335. if (copy_from_user(kbuf, buf, copy_size)) {
  336. if (retv == 0)
  337. retv = -EFAULT;
  338. break;
  339. }
  340. }
  341. } while (count > 0);
  342. if (test_and_clear_bit(LP_PREEMPT_REQUEST,
  343. &lp_table[minor].bits)) {
  344. printk(KERN_INFO "lp%d releasing parport\n", minor);
  345. parport_negotiate (lp_table[minor].dev->port,
  346. IEEE1284_MODE_COMPAT);
  347. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  348. lp_release_parport (&lp_table[minor]);
  349. }
  350. out_unlock:
  351. mutex_unlock(&lp_table[minor].port_mutex);
  352. return retv;
  353. }
  354. #ifdef CONFIG_PARPORT_1284
  355. /* Status readback conforming to ieee1284 */
  356. static ssize_t lp_read(struct file * file, char __user * buf,
  357. size_t count, loff_t *ppos)
  358. {
  359. DEFINE_WAIT(wait);
  360. unsigned int minor=iminor(file->f_path.dentry->d_inode);
  361. struct parport *port = lp_table[minor].dev->port;
  362. ssize_t retval = 0;
  363. char *kbuf = lp_table[minor].lp_buffer;
  364. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  365. (LP_F(minor) & LP_ABORT));
  366. if (count > LP_BUFFER_SIZE)
  367. count = LP_BUFFER_SIZE;
  368. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  369. return -EINTR;
  370. lp_claim_parport_or_block (&lp_table[minor]);
  371. parport_set_timeout (lp_table[minor].dev,
  372. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  373. : lp_table[minor].timeout));
  374. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  375. if (parport_negotiate (lp_table[minor].dev->port,
  376. IEEE1284_MODE_NIBBLE)) {
  377. retval = -EIO;
  378. goto out;
  379. }
  380. while (retval == 0) {
  381. retval = parport_read (port, kbuf, count);
  382. if (retval > 0)
  383. break;
  384. if (nonblock) {
  385. retval = -EAGAIN;
  386. break;
  387. }
  388. /* Wait for data. */
  389. if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
  390. parport_negotiate (lp_table[minor].dev->port,
  391. IEEE1284_MODE_COMPAT);
  392. lp_error (minor);
  393. if (parport_negotiate (lp_table[minor].dev->port,
  394. IEEE1284_MODE_NIBBLE)) {
  395. retval = -EIO;
  396. goto out;
  397. }
  398. } else {
  399. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  400. schedule_timeout(LP_TIMEOUT_POLLED);
  401. finish_wait(&lp_table[minor].waitq, &wait);
  402. }
  403. if (signal_pending (current)) {
  404. retval = -ERESTARTSYS;
  405. break;
  406. }
  407. cond_resched ();
  408. }
  409. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  410. out:
  411. lp_release_parport (&lp_table[minor]);
  412. if (retval > 0 && copy_to_user (buf, kbuf, retval))
  413. retval = -EFAULT;
  414. mutex_unlock(&lp_table[minor].port_mutex);
  415. return retval;
  416. }
  417. #endif /* IEEE 1284 support */
  418. static int lp_open(struct inode * inode, struct file * file)
  419. {
  420. unsigned int minor = iminor(inode);
  421. int ret = 0;
  422. mutex_lock(&lp_mutex);
  423. if (minor >= LP_NO) {
  424. ret = -ENXIO;
  425. goto out;
  426. }
  427. if ((LP_F(minor) & LP_EXIST) == 0) {
  428. ret = -ENXIO;
  429. goto out;
  430. }
  431. if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
  432. ret = -EBUSY;
  433. goto out;
  434. }
  435. /* If ABORTOPEN is set and the printer is offline or out of paper,
  436. we may still want to open it to perform ioctl()s. Therefore we
  437. have commandeered O_NONBLOCK, even though it is being used in
  438. a non-standard manner. This is strictly a Linux hack, and
  439. should most likely only ever be used by the tunelp application. */
  440. if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
  441. int status;
  442. lp_claim_parport_or_block (&lp_table[minor]);
  443. status = r_str(minor);
  444. lp_release_parport (&lp_table[minor]);
  445. if (status & LP_POUTPA) {
  446. printk(KERN_INFO "lp%d out of paper\n", minor);
  447. LP_F(minor) &= ~LP_BUSY;
  448. ret = -ENOSPC;
  449. goto out;
  450. } else if (!(status & LP_PSELECD)) {
  451. printk(KERN_INFO "lp%d off-line\n", minor);
  452. LP_F(minor) &= ~LP_BUSY;
  453. ret = -EIO;
  454. goto out;
  455. } else if (!(status & LP_PERRORP)) {
  456. printk(KERN_ERR "lp%d printer error\n", minor);
  457. LP_F(minor) &= ~LP_BUSY;
  458. ret = -EIO;
  459. goto out;
  460. }
  461. }
  462. lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
  463. if (!lp_table[minor].lp_buffer) {
  464. LP_F(minor) &= ~LP_BUSY;
  465. ret = -ENOMEM;
  466. goto out;
  467. }
  468. /* Determine if the peripheral supports ECP mode */
  469. lp_claim_parport_or_block (&lp_table[minor]);
  470. if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
  471. !parport_negotiate (lp_table[minor].dev->port,
  472. IEEE1284_MODE_ECP)) {
  473. printk (KERN_INFO "lp%d: ECP mode\n", minor);
  474. lp_table[minor].best_mode = IEEE1284_MODE_ECP;
  475. } else {
  476. lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
  477. }
  478. /* Leave peripheral in compatibility mode */
  479. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  480. lp_release_parport (&lp_table[minor]);
  481. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  482. out:
  483. mutex_unlock(&lp_mutex);
  484. return ret;
  485. }
  486. static int lp_release(struct inode * inode, struct file * file)
  487. {
  488. unsigned int minor = iminor(inode);
  489. lp_claim_parport_or_block (&lp_table[minor]);
  490. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  491. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  492. lp_release_parport (&lp_table[minor]);
  493. kfree(lp_table[minor].lp_buffer);
  494. lp_table[minor].lp_buffer = NULL;
  495. LP_F(minor) &= ~LP_BUSY;
  496. return 0;
  497. }
  498. static int lp_do_ioctl(unsigned int minor, unsigned int cmd,
  499. unsigned long arg, void __user *argp)
  500. {
  501. int status;
  502. int retval = 0;
  503. #ifdef LP_DEBUG
  504. printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
  505. #endif
  506. if (minor >= LP_NO)
  507. return -ENODEV;
  508. if ((LP_F(minor) & LP_EXIST) == 0)
  509. return -ENODEV;
  510. switch ( cmd ) {
  511. case LPTIME:
  512. LP_TIME(minor) = arg * HZ/100;
  513. break;
  514. case LPCHAR:
  515. LP_CHAR(minor) = arg;
  516. break;
  517. case LPABORT:
  518. if (arg)
  519. LP_F(minor) |= LP_ABORT;
  520. else
  521. LP_F(minor) &= ~LP_ABORT;
  522. break;
  523. case LPABORTOPEN:
  524. if (arg)
  525. LP_F(minor) |= LP_ABORTOPEN;
  526. else
  527. LP_F(minor) &= ~LP_ABORTOPEN;
  528. break;
  529. case LPCAREFUL:
  530. if (arg)
  531. LP_F(minor) |= LP_CAREFUL;
  532. else
  533. LP_F(minor) &= ~LP_CAREFUL;
  534. break;
  535. case LPWAIT:
  536. LP_WAIT(minor) = arg;
  537. break;
  538. case LPSETIRQ:
  539. return -EINVAL;
  540. break;
  541. case LPGETIRQ:
  542. if (copy_to_user(argp, &LP_IRQ(minor),
  543. sizeof(int)))
  544. return -EFAULT;
  545. break;
  546. case LPGETSTATUS:
  547. lp_claim_parport_or_block (&lp_table[minor]);
  548. status = r_str(minor);
  549. lp_release_parport (&lp_table[minor]);
  550. if (copy_to_user(argp, &status, sizeof(int)))
  551. return -EFAULT;
  552. break;
  553. case LPRESET:
  554. lp_reset(minor);
  555. break;
  556. #ifdef LP_STATS
  557. case LPGETSTATS:
  558. if (copy_to_user(argp, &LP_STAT(minor),
  559. sizeof(struct lp_stats)))
  560. return -EFAULT;
  561. if (capable(CAP_SYS_ADMIN))
  562. memset(&LP_STAT(minor), 0,
  563. sizeof(struct lp_stats));
  564. break;
  565. #endif
  566. case LPGETFLAGS:
  567. status = LP_F(minor);
  568. if (copy_to_user(argp, &status, sizeof(int)))
  569. return -EFAULT;
  570. break;
  571. default:
  572. retval = -EINVAL;
  573. }
  574. return retval;
  575. }
  576. static int lp_set_timeout(unsigned int minor, struct timeval *par_timeout)
  577. {
  578. long to_jiffies;
  579. /* Convert to jiffies, place in lp_table */
  580. if ((par_timeout->tv_sec < 0) ||
  581. (par_timeout->tv_usec < 0)) {
  582. return -EINVAL;
  583. }
  584. to_jiffies = DIV_ROUND_UP(par_timeout->tv_usec, 1000000/HZ);
  585. to_jiffies += par_timeout->tv_sec * (long) HZ;
  586. if (to_jiffies <= 0) {
  587. return -EINVAL;
  588. }
  589. lp_table[minor].timeout = to_jiffies;
  590. return 0;
  591. }
  592. static long lp_ioctl(struct file *file, unsigned int cmd,
  593. unsigned long arg)
  594. {
  595. unsigned int minor;
  596. struct timeval par_timeout;
  597. int ret;
  598. minor = iminor(file->f_path.dentry->d_inode);
  599. mutex_lock(&lp_mutex);
  600. switch (cmd) {
  601. case LPSETTIMEOUT:
  602. if (copy_from_user(&par_timeout, (void __user *)arg,
  603. sizeof (struct timeval))) {
  604. ret = -EFAULT;
  605. break;
  606. }
  607. ret = lp_set_timeout(minor, &par_timeout);
  608. break;
  609. default:
  610. ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
  611. break;
  612. }
  613. mutex_unlock(&lp_mutex);
  614. return ret;
  615. }
  616. #ifdef CONFIG_COMPAT
  617. static long lp_compat_ioctl(struct file *file, unsigned int cmd,
  618. unsigned long arg)
  619. {
  620. unsigned int minor;
  621. struct timeval par_timeout;
  622. struct compat_timeval __user *tc;
  623. int ret;
  624. minor = iminor(file->f_path.dentry->d_inode);
  625. mutex_lock(&lp_mutex);
  626. switch (cmd) {
  627. case LPSETTIMEOUT:
  628. tc = compat_ptr(arg);
  629. if (get_user(par_timeout.tv_sec, &tc->tv_sec) ||
  630. get_user(par_timeout.tv_usec, &tc->tv_usec)) {
  631. ret = -EFAULT;
  632. break;
  633. }
  634. ret = lp_set_timeout(minor, &par_timeout);
  635. break;
  636. #ifdef LP_STATS
  637. case LPGETSTATS:
  638. /* FIXME: add an implementation if you set LP_STATS */
  639. ret = -EINVAL;
  640. break;
  641. #endif
  642. default:
  643. ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
  644. break;
  645. }
  646. mutex_unlock(&lp_mutex);
  647. return ret;
  648. }
  649. #endif
  650. static const struct file_operations lp_fops = {
  651. .owner = THIS_MODULE,
  652. .write = lp_write,
  653. .unlocked_ioctl = lp_ioctl,
  654. #ifdef CONFIG_COMPAT
  655. .compat_ioctl = lp_compat_ioctl,
  656. #endif
  657. .open = lp_open,
  658. .release = lp_release,
  659. #ifdef CONFIG_PARPORT_1284
  660. .read = lp_read,
  661. #endif
  662. .llseek = noop_llseek,
  663. };
  664. /* --- support for console on the line printer ----------------- */
  665. #ifdef CONFIG_LP_CONSOLE
  666. #define CONSOLE_LP 0
  667. /* If the printer is out of paper, we can either lose the messages or
  668. * stall until the printer is happy again. Define CONSOLE_LP_STRICT
  669. * non-zero to get the latter behaviour. */
  670. #define CONSOLE_LP_STRICT 1
  671. /* The console must be locked when we get here. */
  672. static void lp_console_write (struct console *co, const char *s,
  673. unsigned count)
  674. {
  675. struct pardevice *dev = lp_table[CONSOLE_LP].dev;
  676. struct parport *port = dev->port;
  677. ssize_t written;
  678. if (parport_claim (dev))
  679. /* Nothing we can do. */
  680. return;
  681. parport_set_timeout (dev, 0);
  682. /* Go to compatibility mode. */
  683. parport_negotiate (port, IEEE1284_MODE_COMPAT);
  684. do {
  685. /* Write the data, converting LF->CRLF as we go. */
  686. ssize_t canwrite = count;
  687. char *lf = memchr (s, '\n', count);
  688. if (lf)
  689. canwrite = lf - s;
  690. if (canwrite > 0) {
  691. written = parport_write (port, s, canwrite);
  692. if (written <= 0)
  693. continue;
  694. s += written;
  695. count -= written;
  696. canwrite -= written;
  697. }
  698. if (lf && canwrite <= 0) {
  699. const char *crlf = "\r\n";
  700. int i = 2;
  701. /* Dodge the original '\n', and put '\r\n' instead. */
  702. s++;
  703. count--;
  704. do {
  705. written = parport_write (port, crlf, i);
  706. if (written > 0)
  707. i -= written, crlf += written;
  708. } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
  709. }
  710. } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
  711. parport_release (dev);
  712. }
  713. static struct console lpcons = {
  714. .name = "lp",
  715. .write = lp_console_write,
  716. .flags = CON_PRINTBUFFER,
  717. };
  718. #endif /* console on line printer */
  719. /* --- initialisation code ------------------------------------- */
  720. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  721. static char *parport[LP_NO];
  722. static int reset;
  723. module_param_array(parport, charp, NULL, 0);
  724. module_param(reset, bool, 0);
  725. #ifndef MODULE
  726. static int __init lp_setup (char *str)
  727. {
  728. static int parport_ptr;
  729. int x;
  730. if (get_option(&str, &x)) {
  731. if (x == 0) {
  732. /* disable driver on "lp=" or "lp=0" */
  733. parport_nr[0] = LP_PARPORT_OFF;
  734. } else {
  735. printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
  736. return 0;
  737. }
  738. } else if (!strncmp(str, "parport", 7)) {
  739. int n = simple_strtoul(str+7, NULL, 10);
  740. if (parport_ptr < LP_NO)
  741. parport_nr[parport_ptr++] = n;
  742. else
  743. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  744. str);
  745. } else if (!strcmp(str, "auto")) {
  746. parport_nr[0] = LP_PARPORT_AUTO;
  747. } else if (!strcmp(str, "none")) {
  748. parport_nr[parport_ptr++] = LP_PARPORT_NONE;
  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");