lp.c 27 KB

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