lp.c 27 KB

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