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, metcalf@lcs.mit.edu
  12. * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
  13. * "lp=" command line parameters added by Grant Guenther, grant@torque.net
  14. * lp_read (Status readback) support added by Carsten Gross,
  15. * carsten@sol.wohnheim.uni-ulm.de
  16. * Support for parport by Philip Blundell <philb@gnu.org>
  17. * Parport sharing hacking by Andrea Arcangeli
  18. * Fixed kernel_(to/from)_user memory copy to check for errors
  19. * by Riccardo Facchetti <fizban@tin.it>
  20. * 22-JAN-1998 Added support for devfs Richard Gooch <rgooch@atnf.csiro.au>
  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 const struct class lp_class = {
  139. .name = "printer",
  140. };
  141. #ifdef CONFIG_LP_CONSOLE
  142. static struct parport *console_registered;
  143. #endif /* CONFIG_LP_CONSOLE */
  144. #undef LP_DEBUG
  145. /* Bits used to manage claiming the parport device */
  146. #define LP_PREEMPT_REQUEST 1
  147. #define LP_PARPORT_CLAIMED 2
  148. /* --- low-level port access ----------------------------------- */
  149. #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
  150. #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
  151. #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
  152. #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
  153. /* Claim the parport or block trying unless we've already claimed it */
  154. static void lp_claim_parport_or_block(struct lp_struct *this_lp)
  155. {
  156. if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
  157. parport_claim_or_block(this_lp->dev);
  158. }
  159. }
  160. /* Claim the parport or block trying unless we've already claimed it */
  161. static void lp_release_parport(struct lp_struct *this_lp)
  162. {
  163. if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
  164. parport_release(this_lp->dev);
  165. }
  166. }
  167. static int lp_preempt(void *handle)
  168. {
  169. struct lp_struct *this_lp = (struct lp_struct *)handle;
  170. set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
  171. return 1;
  172. }
  173. /*
  174. * Try to negotiate to a new mode; if unsuccessful negotiate to
  175. * compatibility mode. Return the mode we ended up in.
  176. */
  177. static int lp_negotiate(struct parport *port, int mode)
  178. {
  179. if (parport_negotiate(port, mode) != 0) {
  180. mode = IEEE1284_MODE_COMPAT;
  181. parport_negotiate(port, mode);
  182. }
  183. return mode;
  184. }
  185. static int lp_reset(int minor)
  186. {
  187. int retval;
  188. lp_claim_parport_or_block(&lp_table[minor]);
  189. w_ctr(minor, LP_PSELECP);
  190. udelay(LP_DELAY);
  191. w_ctr(minor, LP_PSELECP | LP_PINITP);
  192. retval = r_str(minor);
  193. lp_release_parport(&lp_table[minor]);
  194. return retval;
  195. }
  196. static void lp_error(int minor)
  197. {
  198. DEFINE_WAIT(wait);
  199. int polling;
  200. if (LP_F(minor) & LP_ABORT)
  201. return;
  202. polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
  203. if (polling)
  204. lp_release_parport(&lp_table[minor]);
  205. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  206. schedule_timeout(LP_TIMEOUT_POLLED);
  207. finish_wait(&lp_table[minor].waitq, &wait);
  208. if (polling)
  209. lp_claim_parport_or_block(&lp_table[minor]);
  210. else
  211. parport_yield_blocking(lp_table[minor].dev);
  212. }
  213. static int lp_check_status(int minor)
  214. {
  215. int error = 0;
  216. unsigned int last = lp_table[minor].last_error;
  217. unsigned char status = r_str(minor);
  218. if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
  219. /* No error. */
  220. last = 0;
  221. else if ((status & LP_POUTPA)) {
  222. if (last != LP_POUTPA) {
  223. last = LP_POUTPA;
  224. printk(KERN_INFO "lp%d out of paper\n", minor);
  225. }
  226. error = -ENOSPC;
  227. } else if (!(status & LP_PSELECD)) {
  228. if (last != LP_PSELECD) {
  229. last = LP_PSELECD;
  230. printk(KERN_INFO "lp%d off-line\n", minor);
  231. }
  232. error = -EIO;
  233. } else if (!(status & LP_PERRORP)) {
  234. if (last != LP_PERRORP) {
  235. last = LP_PERRORP;
  236. printk(KERN_INFO "lp%d on fire\n", minor);
  237. }
  238. error = -EIO;
  239. } else {
  240. last = 0; /* Come here if LP_CAREFUL is set and no
  241. errors are reported. */
  242. }
  243. lp_table[minor].last_error = last;
  244. if (last != 0)
  245. lp_error(minor);
  246. return error;
  247. }
  248. static int lp_wait_ready(int minor, int nonblock)
  249. {
  250. int error = 0;
  251. /* If we're not in compatibility mode, we're ready now! */
  252. if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
  253. return 0;
  254. }
  255. do {
  256. error = lp_check_status(minor);
  257. if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
  258. break;
  259. if (signal_pending(current)) {
  260. error = -EINTR;
  261. break;
  262. }
  263. } while (error);
  264. return error;
  265. }
  266. static ssize_t lp_write(struct file *file, const char __user *buf,
  267. size_t count, loff_t *ppos)
  268. {
  269. unsigned int minor = iminor(file_inode(file));
  270. struct parport *port = lp_table[minor].dev->port;
  271. char *kbuf = lp_table[minor].lp_buffer;
  272. ssize_t retv = 0;
  273. ssize_t written;
  274. size_t copy_size = count;
  275. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  276. (LP_F(minor) & LP_ABORT));
  277. #ifdef LP_STATS
  278. if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
  279. lp_table[minor].runchars = 0;
  280. lp_table[minor].lastcall = jiffies;
  281. #endif
  282. /* Need to copy the data from user-space. */
  283. if (copy_size > LP_BUFFER_SIZE)
  284. copy_size = LP_BUFFER_SIZE;
  285. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  286. return -EINTR;
  287. if (copy_from_user(kbuf, buf, copy_size)) {
  288. retv = -EFAULT;
  289. goto out_unlock;
  290. }
  291. /* Claim Parport or sleep until it becomes available
  292. */
  293. lp_claim_parport_or_block(&lp_table[minor]);
  294. /* Go to the proper mode. */
  295. lp_table[minor].current_mode = lp_negotiate(port,
  296. lp_table[minor].best_mode);
  297. parport_set_timeout(lp_table[minor].dev,
  298. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  299. : lp_table[minor].timeout));
  300. if ((retv = lp_wait_ready(minor, nonblock)) == 0)
  301. do {
  302. /* Write the data. */
  303. written = parport_write(port, kbuf, copy_size);
  304. if (written > 0) {
  305. copy_size -= written;
  306. count -= written;
  307. buf += written;
  308. retv += written;
  309. }
  310. if (signal_pending(current)) {
  311. if (retv == 0)
  312. retv = -EINTR;
  313. break;
  314. }
  315. if (copy_size > 0) {
  316. /* incomplete write -> check error ! */
  317. int error;
  318. parport_negotiate(lp_table[minor].dev->port,
  319. IEEE1284_MODE_COMPAT);
  320. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  321. error = lp_wait_ready(minor, nonblock);
  322. if (error) {
  323. if (retv == 0)
  324. retv = error;
  325. break;
  326. } else if (nonblock) {
  327. if (retv == 0)
  328. retv = -EAGAIN;
  329. break;
  330. }
  331. parport_yield_blocking(lp_table[minor].dev);
  332. lp_table[minor].current_mode
  333. = lp_negotiate(port,
  334. lp_table[minor].best_mode);
  335. } else if (need_resched())
  336. schedule();
  337. if (count) {
  338. copy_size = count;
  339. if (copy_size > LP_BUFFER_SIZE)
  340. copy_size = LP_BUFFER_SIZE;
  341. if (copy_from_user(kbuf, buf, copy_size)) {
  342. if (retv == 0)
  343. retv = -EFAULT;
  344. break;
  345. }
  346. }
  347. } while (count > 0);
  348. if (test_and_clear_bit(LP_PREEMPT_REQUEST,
  349. &lp_table[minor].bits)) {
  350. printk(KERN_INFO "lp%d releasing parport\n", minor);
  351. parport_negotiate(lp_table[minor].dev->port,
  352. IEEE1284_MODE_COMPAT);
  353. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  354. lp_release_parport(&lp_table[minor]);
  355. }
  356. out_unlock:
  357. mutex_unlock(&lp_table[minor].port_mutex);
  358. return retv;
  359. }
  360. #ifdef CONFIG_PARPORT_1284
  361. /* Status readback conforming to ieee1284 */
  362. static ssize_t lp_read(struct file *file, char __user *buf,
  363. size_t count, loff_t *ppos)
  364. {
  365. DEFINE_WAIT(wait);
  366. unsigned int minor=iminor(file_inode(file));
  367. struct parport *port = lp_table[minor].dev->port;
  368. ssize_t retval = 0;
  369. char *kbuf = lp_table[minor].lp_buffer;
  370. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  371. (LP_F(minor) & LP_ABORT));
  372. if (count > LP_BUFFER_SIZE)
  373. count = LP_BUFFER_SIZE;
  374. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  375. return -EINTR;
  376. lp_claim_parport_or_block(&lp_table[minor]);
  377. parport_set_timeout(lp_table[minor].dev,
  378. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  379. : lp_table[minor].timeout));
  380. parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  381. if (parport_negotiate(lp_table[minor].dev->port,
  382. IEEE1284_MODE_NIBBLE)) {
  383. retval = -EIO;
  384. goto out;
  385. }
  386. while (retval == 0) {
  387. retval = parport_read(port, kbuf, count);
  388. if (retval > 0)
  389. break;
  390. if (nonblock) {
  391. retval = -EAGAIN;
  392. break;
  393. }
  394. /* Wait for data. */
  395. if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
  396. parport_negotiate(lp_table[minor].dev->port,
  397. IEEE1284_MODE_COMPAT);
  398. lp_error(minor);
  399. if (parport_negotiate(lp_table[minor].dev->port,
  400. IEEE1284_MODE_NIBBLE)) {
  401. retval = -EIO;
  402. goto out;
  403. }
  404. } else {
  405. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  406. schedule_timeout(LP_TIMEOUT_POLLED);
  407. finish_wait(&lp_table[minor].waitq, &wait);
  408. }
  409. if (signal_pending(current)) {
  410. retval = -ERESTARTSYS;
  411. break;
  412. }
  413. cond_resched();
  414. }
  415. parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  416. out:
  417. lp_release_parport(&lp_table[minor]);
  418. if (retval > 0 && copy_to_user(buf, kbuf, retval))
  419. retval = -EFAULT;
  420. mutex_unlock(&lp_table[minor].port_mutex);
  421. return retval;
  422. }
  423. #endif /* IEEE 1284 support */
  424. static int lp_open(struct inode *inode, struct file *file)
  425. {
  426. unsigned int minor = iminor(inode);
  427. int ret = 0;
  428. mutex_lock(&lp_mutex);
  429. if (minor >= LP_NO) {
  430. ret = -ENXIO;
  431. goto out;
  432. }
  433. if ((LP_F(minor) & LP_EXIST) == 0) {
  434. ret = -ENXIO;
  435. goto out;
  436. }
  437. if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
  438. ret = -EBUSY;
  439. goto out;
  440. }
  441. /* If ABORTOPEN is set and the printer is offline or out of paper,
  442. we may still want to open it to perform ioctl()s. Therefore we
  443. have commandeered O_NONBLOCK, even though it is being used in
  444. a non-standard manner. This is strictly a Linux hack, and
  445. should most likely only ever be used by the tunelp application. */
  446. if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
  447. int status;
  448. lp_claim_parport_or_block(&lp_table[minor]);
  449. status = r_str(minor);
  450. lp_release_parport(&lp_table[minor]);
  451. if (status & LP_POUTPA) {
  452. printk(KERN_INFO "lp%d out of paper\n", minor);
  453. LP_F(minor) &= ~LP_BUSY;
  454. ret = -ENOSPC;
  455. goto out;
  456. } else if (!(status & LP_PSELECD)) {
  457. printk(KERN_INFO "lp%d off-line\n", minor);
  458. LP_F(minor) &= ~LP_BUSY;
  459. ret = -EIO;
  460. goto out;
  461. } else if (!(status & LP_PERRORP)) {
  462. printk(KERN_ERR "lp%d printer error\n", minor);
  463. LP_F(minor) &= ~LP_BUSY;
  464. ret = -EIO;
  465. goto out;
  466. }
  467. }
  468. lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
  469. if (!lp_table[minor].lp_buffer) {
  470. LP_F(minor) &= ~LP_BUSY;
  471. ret = -ENOMEM;
  472. goto out;
  473. }
  474. /* Determine if the peripheral supports ECP mode */
  475. lp_claim_parport_or_block(&lp_table[minor]);
  476. if ((lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
  477. !parport_negotiate(lp_table[minor].dev->port,
  478. IEEE1284_MODE_ECP)) {
  479. printk(KERN_INFO "lp%d: ECP mode\n", minor);
  480. lp_table[minor].best_mode = IEEE1284_MODE_ECP;
  481. } else {
  482. lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
  483. }
  484. /* Leave peripheral in compatibility mode */
  485. parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  486. lp_release_parport(&lp_table[minor]);
  487. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  488. out:
  489. mutex_unlock(&lp_mutex);
  490. return ret;
  491. }
  492. static int lp_release(struct inode *inode, struct file *file)
  493. {
  494. unsigned int minor = iminor(inode);
  495. lp_claim_parport_or_block(&lp_table[minor]);
  496. parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  497. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  498. lp_release_parport(&lp_table[minor]);
  499. kfree(lp_table[minor].lp_buffer);
  500. lp_table[minor].lp_buffer = NULL;
  501. LP_F(minor) &= ~LP_BUSY;
  502. return 0;
  503. }
  504. static int lp_do_ioctl(unsigned int minor, unsigned int cmd,
  505. unsigned long arg, void __user *argp)
  506. {
  507. int status;
  508. int retval = 0;
  509. #ifdef LP_DEBUG
  510. printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
  511. #endif
  512. if (minor >= LP_NO)
  513. return -ENODEV;
  514. if ((LP_F(minor) & LP_EXIST) == 0)
  515. return -ENODEV;
  516. switch (cmd) {
  517. case LPTIME:
  518. if (arg > UINT_MAX / HZ)
  519. return -EINVAL;
  520. LP_TIME(minor) = arg * HZ/100;
  521. break;
  522. case LPCHAR:
  523. LP_CHAR(minor) = arg;
  524. break;
  525. case LPABORT:
  526. if (arg)
  527. LP_F(minor) |= LP_ABORT;
  528. else
  529. LP_F(minor) &= ~LP_ABORT;
  530. break;
  531. case LPABORTOPEN:
  532. if (arg)
  533. LP_F(minor) |= LP_ABORTOPEN;
  534. else
  535. LP_F(minor) &= ~LP_ABORTOPEN;
  536. break;
  537. case LPCAREFUL:
  538. if (arg)
  539. LP_F(minor) |= LP_CAREFUL;
  540. else
  541. LP_F(minor) &= ~LP_CAREFUL;
  542. break;
  543. case LPWAIT:
  544. LP_WAIT(minor) = arg;
  545. break;
  546. case LPSETIRQ:
  547. return -EINVAL;
  548. case LPGETIRQ:
  549. if (copy_to_user(argp, &LP_IRQ(minor),
  550. sizeof(int)))
  551. return -EFAULT;
  552. break;
  553. case LPGETSTATUS:
  554. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  555. return -EINTR;
  556. lp_claim_parport_or_block(&lp_table[minor]);
  557. status = r_str(minor);
  558. lp_release_parport(&lp_table[minor]);
  559. mutex_unlock(&lp_table[minor].port_mutex);
  560. if (copy_to_user(argp, &status, sizeof(int)))
  561. return -EFAULT;
  562. break;
  563. case LPRESET:
  564. lp_reset(minor);
  565. break;
  566. #ifdef LP_STATS
  567. case LPGETSTATS:
  568. if (copy_to_user(argp, &LP_STAT(minor),
  569. sizeof(struct lp_stats)))
  570. return -EFAULT;
  571. if (capable(CAP_SYS_ADMIN))
  572. memset(&LP_STAT(minor), 0,
  573. sizeof(struct lp_stats));
  574. break;
  575. #endif
  576. case LPGETFLAGS:
  577. status = LP_F(minor);
  578. if (copy_to_user(argp, &status, sizeof(int)))
  579. return -EFAULT;
  580. break;
  581. default:
  582. retval = -EINVAL;
  583. }
  584. return retval;
  585. }
  586. static int lp_set_timeout(unsigned int minor, s64 tv_sec, long tv_usec)
  587. {
  588. long to_jiffies;
  589. /* Convert to jiffies, place in lp_table */
  590. if (tv_sec < 0 || tv_usec < 0)
  591. return -EINVAL;
  592. /*
  593. * we used to not check, so let's not make this fatal,
  594. * but deal with user space passing a 32-bit tv_nsec in
  595. * a 64-bit field, capping the timeout to 1 second
  596. * worth of microseconds, and capping the total at
  597. * MAX_JIFFY_OFFSET.
  598. */
  599. if (tv_usec > 999999)
  600. tv_usec = 999999;
  601. if (tv_sec >= MAX_SEC_IN_JIFFIES - 1) {
  602. to_jiffies = MAX_JIFFY_OFFSET;
  603. } else {
  604. to_jiffies = DIV_ROUND_UP(tv_usec, 1000000/HZ);
  605. to_jiffies += tv_sec * (long) HZ;
  606. }
  607. if (to_jiffies <= 0) {
  608. return -EINVAL;
  609. }
  610. lp_table[minor].timeout = to_jiffies;
  611. return 0;
  612. }
  613. static int lp_set_timeout32(unsigned int minor, void __user *arg)
  614. {
  615. s32 karg[2];
  616. if (copy_from_user(karg, arg, sizeof(karg)))
  617. return -EFAULT;
  618. return lp_set_timeout(minor, karg[0], karg[1]);
  619. }
  620. static int lp_set_timeout64(unsigned int minor, void __user *arg)
  621. {
  622. s64 karg[2];
  623. if (copy_from_user(karg, arg, sizeof(karg)))
  624. return -EFAULT;
  625. /* sparc64 suseconds_t is 32-bit only */
  626. if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
  627. karg[1] >>= 32;
  628. return lp_set_timeout(minor, karg[0], karg[1]);
  629. }
  630. static long lp_ioctl(struct file *file, unsigned int cmd,
  631. unsigned long arg)
  632. {
  633. unsigned int minor;
  634. int ret;
  635. minor = iminor(file_inode(file));
  636. mutex_lock(&lp_mutex);
  637. switch (cmd) {
  638. case LPSETTIMEOUT_OLD:
  639. if (BITS_PER_LONG == 32) {
  640. ret = lp_set_timeout32(minor, (void __user *)arg);
  641. break;
  642. }
  643. fallthrough; /* for 64-bit */
  644. case LPSETTIMEOUT_NEW:
  645. ret = lp_set_timeout64(minor, (void __user *)arg);
  646. break;
  647. default:
  648. ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
  649. break;
  650. }
  651. mutex_unlock(&lp_mutex);
  652. return ret;
  653. }
  654. #ifdef CONFIG_COMPAT
  655. static long lp_compat_ioctl(struct file *file, unsigned int cmd,
  656. unsigned long arg)
  657. {
  658. unsigned int minor;
  659. int ret;
  660. minor = iminor(file_inode(file));
  661. mutex_lock(&lp_mutex);
  662. switch (cmd) {
  663. case LPSETTIMEOUT_OLD:
  664. if (!COMPAT_USE_64BIT_TIME) {
  665. ret = lp_set_timeout32(minor, (void __user *)arg);
  666. break;
  667. }
  668. fallthrough; /* for x32 mode */
  669. case LPSETTIMEOUT_NEW:
  670. ret = lp_set_timeout64(minor, (void __user *)arg);
  671. break;
  672. #ifdef LP_STATS
  673. case LPGETSTATS:
  674. /* FIXME: add an implementation if you set LP_STATS */
  675. ret = -EINVAL;
  676. break;
  677. #endif
  678. default:
  679. ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
  680. break;
  681. }
  682. mutex_unlock(&lp_mutex);
  683. return ret;
  684. }
  685. #endif
  686. static const struct file_operations lp_fops = {
  687. .owner = THIS_MODULE,
  688. .write = lp_write,
  689. .unlocked_ioctl = lp_ioctl,
  690. #ifdef CONFIG_COMPAT
  691. .compat_ioctl = lp_compat_ioctl,
  692. #endif
  693. .open = lp_open,
  694. .release = lp_release,
  695. #ifdef CONFIG_PARPORT_1284
  696. .read = lp_read,
  697. #endif
  698. .llseek = noop_llseek,
  699. };
  700. /* --- support for console on the line printer ----------------- */
  701. #ifdef CONFIG_LP_CONSOLE
  702. #define CONSOLE_LP 0
  703. /* If the printer is out of paper, we can either lose the messages or
  704. * stall until the printer is happy again. Define CONSOLE_LP_STRICT
  705. * non-zero to get the latter behaviour. */
  706. #define CONSOLE_LP_STRICT 1
  707. /* The console must be locked when we get here. */
  708. static void lp_console_write(struct console *co, const char *s,
  709. unsigned count)
  710. {
  711. struct pardevice *dev = lp_table[CONSOLE_LP].dev;
  712. struct parport *port = dev->port;
  713. ssize_t written;
  714. if (parport_claim(dev))
  715. /* Nothing we can do. */
  716. return;
  717. parport_set_timeout(dev, 0);
  718. /* Go to compatibility mode. */
  719. parport_negotiate(port, IEEE1284_MODE_COMPAT);
  720. do {
  721. /* Write the data, converting LF->CRLF as we go. */
  722. ssize_t canwrite = count;
  723. char *lf = memchr(s, '\n', count);
  724. if (lf)
  725. canwrite = lf - s;
  726. if (canwrite > 0) {
  727. written = parport_write(port, s, canwrite);
  728. if (written <= 0)
  729. continue;
  730. s += written;
  731. count -= written;
  732. canwrite -= written;
  733. }
  734. if (lf && canwrite <= 0) {
  735. const char *crlf = "\r\n";
  736. int i = 2;
  737. /* Dodge the original '\n', and put '\r\n' instead. */
  738. s++;
  739. count--;
  740. do {
  741. written = parport_write(port, crlf, i);
  742. if (written > 0) {
  743. i -= written;
  744. crlf += written;
  745. }
  746. } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
  747. }
  748. } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
  749. parport_release(dev);
  750. }
  751. static struct console lpcons = {
  752. .name = "lp",
  753. .write = lp_console_write,
  754. .flags = CON_PRINTBUFFER,
  755. };
  756. #endif /* console on line printer */
  757. /* --- initialisation code ------------------------------------- */
  758. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  759. static char *parport[LP_NO];
  760. static bool reset;
  761. module_param_array(parport, charp, NULL, 0);
  762. module_param(reset, bool, 0);
  763. #ifndef MODULE
  764. static int __init lp_setup(char *str)
  765. {
  766. static int parport_ptr;
  767. int x;
  768. if (get_option(&str, &x)) {
  769. if (x == 0) {
  770. /* disable driver on "lp=" or "lp=0" */
  771. parport_nr[0] = LP_PARPORT_OFF;
  772. } else {
  773. printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
  774. return 0;
  775. }
  776. } else if (!strncmp(str, "parport", 7)) {
  777. int n = simple_strtoul(str+7, NULL, 10);
  778. if (parport_ptr < LP_NO)
  779. parport_nr[parport_ptr++] = n;
  780. else
  781. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  782. str);
  783. } else if (!strcmp(str, "auto")) {
  784. parport_nr[0] = LP_PARPORT_AUTO;
  785. } else if (!strcmp(str, "none")) {
  786. if (parport_ptr < LP_NO)
  787. parport_nr[parport_ptr++] = LP_PARPORT_NONE;
  788. else
  789. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  790. str);
  791. } else if (!strcmp(str, "reset")) {
  792. reset = true;
  793. }
  794. return 1;
  795. }
  796. #endif
  797. static int lp_register(int nr, struct parport *port)
  798. {
  799. struct pardev_cb ppdev_cb;
  800. memset(&ppdev_cb, 0, sizeof(ppdev_cb));
  801. ppdev_cb.preempt = lp_preempt;
  802. ppdev_cb.private = &lp_table[nr];
  803. lp_table[nr].dev = parport_register_dev_model(port, "lp",
  804. &ppdev_cb, nr);
  805. if (lp_table[nr].dev == NULL)
  806. return 1;
  807. lp_table[nr].flags |= LP_EXIST;
  808. if (reset)
  809. lp_reset(nr);
  810. device_create(&lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
  811. "lp%d", nr);
  812. printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
  813. (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
  814. #ifdef CONFIG_LP_CONSOLE
  815. if (!nr) {
  816. if (port->modes & PARPORT_MODE_SAFEININT) {
  817. register_console(&lpcons);
  818. console_registered = port;
  819. printk(KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
  820. } else
  821. printk(KERN_ERR "lp%d: cannot run console on %s\n",
  822. CONSOLE_LP, port->name);
  823. }
  824. #endif
  825. port_num[nr] = port->number;
  826. return 0;
  827. }
  828. static void lp_attach(struct parport *port)
  829. {
  830. unsigned int i;
  831. switch (parport_nr[0]) {
  832. case LP_PARPORT_UNSPEC:
  833. case LP_PARPORT_AUTO:
  834. if (parport_nr[0] == LP_PARPORT_AUTO &&
  835. port->probe_info[0].class != PARPORT_CLASS_PRINTER)
  836. return;
  837. if (lp_count == LP_NO) {
  838. printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
  839. return;
  840. }
  841. for (i = 0; i < LP_NO; i++)
  842. if (port_num[i] == -1)
  843. break;
  844. if (!lp_register(i, port))
  845. lp_count++;
  846. break;
  847. default:
  848. for (i = 0; i < LP_NO; i++) {
  849. if (port->number == parport_nr[i]) {
  850. if (!lp_register(i, port))
  851. lp_count++;
  852. break;
  853. }
  854. }
  855. break;
  856. }
  857. }
  858. static void lp_detach(struct parport *port)
  859. {
  860. int n;
  861. /* Write this some day. */
  862. #ifdef CONFIG_LP_CONSOLE
  863. if (console_registered == port) {
  864. unregister_console(&lpcons);
  865. console_registered = NULL;
  866. }
  867. #endif /* CONFIG_LP_CONSOLE */
  868. for (n = 0; n < LP_NO; n++) {
  869. if (port_num[n] == port->number) {
  870. port_num[n] = -1;
  871. lp_count--;
  872. device_destroy(&lp_class, MKDEV(LP_MAJOR, n));
  873. parport_unregister_device(lp_table[n].dev);
  874. }
  875. }
  876. }
  877. static struct parport_driver lp_driver = {
  878. .name = "lp",
  879. .match_port = lp_attach,
  880. .detach = lp_detach,
  881. };
  882. static int __init lp_init(void)
  883. {
  884. int i, err;
  885. if (parport_nr[0] == LP_PARPORT_OFF)
  886. return 0;
  887. for (i = 0; i < LP_NO; i++) {
  888. lp_table[i].dev = NULL;
  889. lp_table[i].flags = 0;
  890. lp_table[i].chars = LP_INIT_CHAR;
  891. lp_table[i].time = LP_INIT_TIME;
  892. lp_table[i].wait = LP_INIT_WAIT;
  893. lp_table[i].lp_buffer = NULL;
  894. #ifdef LP_STATS
  895. lp_table[i].lastcall = 0;
  896. lp_table[i].runchars = 0;
  897. memset(&lp_table[i].stats, 0, sizeof(struct lp_stats));
  898. #endif
  899. lp_table[i].last_error = 0;
  900. init_waitqueue_head(&lp_table[i].waitq);
  901. init_waitqueue_head(&lp_table[i].dataq);
  902. mutex_init(&lp_table[i].port_mutex);
  903. lp_table[i].timeout = 10 * HZ;
  904. port_num[i] = -1;
  905. }
  906. if (register_chrdev(LP_MAJOR, "lp", &lp_fops)) {
  907. printk(KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
  908. return -EIO;
  909. }
  910. err = class_register(&lp_class);
  911. if (err)
  912. goto out_reg;
  913. if (parport_register_driver(&lp_driver)) {
  914. printk(KERN_ERR "lp: unable to register with parport\n");
  915. err = -EIO;
  916. goto out_class;
  917. }
  918. if (!lp_count) {
  919. printk(KERN_INFO "lp: driver loaded but no devices found\n");
  920. #ifndef CONFIG_PARPORT_1284
  921. if (parport_nr[0] == LP_PARPORT_AUTO)
  922. printk(KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
  923. #endif
  924. }
  925. return 0;
  926. out_class:
  927. class_unregister(&lp_class);
  928. out_reg:
  929. unregister_chrdev(LP_MAJOR, "lp");
  930. return err;
  931. }
  932. static int __init lp_init_module(void)
  933. {
  934. if (parport[0]) {
  935. /* The user gave some parameters. Let's see what they were. */
  936. if (!strncmp(parport[0], "auto", 4))
  937. parport_nr[0] = LP_PARPORT_AUTO;
  938. else {
  939. int n;
  940. for (n = 0; n < LP_NO && parport[n]; n++) {
  941. if (!strncmp(parport[n], "none", 4))
  942. parport_nr[n] = LP_PARPORT_NONE;
  943. else {
  944. char *ep;
  945. unsigned long r = simple_strtoul(parport[n], &ep, 0);
  946. if (ep != parport[n])
  947. parport_nr[n] = r;
  948. else {
  949. printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
  950. return -ENODEV;
  951. }
  952. }
  953. }
  954. }
  955. }
  956. return lp_init();
  957. }
  958. static void lp_cleanup_module(void)
  959. {
  960. parport_unregister_driver(&lp_driver);
  961. #ifdef CONFIG_LP_CONSOLE
  962. unregister_console(&lpcons);
  963. #endif
  964. unregister_chrdev(LP_MAJOR, "lp");
  965. class_unregister(&lp_class);
  966. }
  967. __setup("lp=", lp_setup);
  968. module_init(lp_init_module);
  969. module_exit(lp_cleanup_module);
  970. MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
  971. MODULE_DESCRIPTION("Generic parallel printer driver");
  972. MODULE_LICENSE("GPL");