lp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 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. break;
  547. case LPGETIRQ:
  548. if (copy_to_user(argp, &LP_IRQ(minor),
  549. sizeof(int)))
  550. return -EFAULT;
  551. break;
  552. case LPGETSTATUS:
  553. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  554. return -EINTR;
  555. lp_claim_parport_or_block(&lp_table[minor]);
  556. status = r_str(minor);
  557. lp_release_parport(&lp_table[minor]);
  558. mutex_unlock(&lp_table[minor].port_mutex);
  559. if (copy_to_user(argp, &status, sizeof(int)))
  560. return -EFAULT;
  561. break;
  562. case LPRESET:
  563. lp_reset(minor);
  564. break;
  565. #ifdef LP_STATS
  566. case LPGETSTATS:
  567. if (copy_to_user(argp, &LP_STAT(minor),
  568. sizeof(struct lp_stats)))
  569. return -EFAULT;
  570. if (capable(CAP_SYS_ADMIN))
  571. memset(&LP_STAT(minor), 0,
  572. sizeof(struct lp_stats));
  573. break;
  574. #endif
  575. case LPGETFLAGS:
  576. status = LP_F(minor);
  577. if (copy_to_user(argp, &status, sizeof(int)))
  578. return -EFAULT;
  579. break;
  580. default:
  581. retval = -EINVAL;
  582. }
  583. return retval;
  584. }
  585. static int lp_set_timeout(unsigned int minor, s64 tv_sec, long tv_usec)
  586. {
  587. long to_jiffies;
  588. /* Convert to jiffies, place in lp_table */
  589. if (tv_sec < 0 || tv_usec < 0)
  590. return -EINVAL;
  591. /*
  592. * we used to not check, so let's not make this fatal,
  593. * but deal with user space passing a 32-bit tv_nsec in
  594. * a 64-bit field, capping the timeout to 1 second
  595. * worth of microseconds, and capping the total at
  596. * MAX_JIFFY_OFFSET.
  597. */
  598. if (tv_usec > 999999)
  599. tv_usec = 999999;
  600. if (tv_sec >= MAX_SEC_IN_JIFFIES - 1) {
  601. to_jiffies = MAX_JIFFY_OFFSET;
  602. } else {
  603. to_jiffies = DIV_ROUND_UP(tv_usec, 1000000/HZ);
  604. to_jiffies += tv_sec * (long) HZ;
  605. }
  606. if (to_jiffies <= 0) {
  607. return -EINVAL;
  608. }
  609. lp_table[minor].timeout = to_jiffies;
  610. return 0;
  611. }
  612. static int lp_set_timeout32(unsigned int minor, void __user *arg)
  613. {
  614. s32 karg[2];
  615. if (copy_from_user(karg, arg, sizeof(karg)))
  616. return -EFAULT;
  617. return lp_set_timeout(minor, karg[0], karg[1]);
  618. }
  619. static int lp_set_timeout64(unsigned int minor, void __user *arg)
  620. {
  621. s64 karg[2];
  622. if (copy_from_user(karg, arg, sizeof(karg)))
  623. return -EFAULT;
  624. /* sparc64 suseconds_t is 32-bit only */
  625. if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
  626. karg[1] >>= 32;
  627. return lp_set_timeout(minor, karg[0], karg[1]);
  628. }
  629. static long lp_ioctl(struct file *file, unsigned int cmd,
  630. unsigned long arg)
  631. {
  632. unsigned int minor;
  633. int ret;
  634. minor = iminor(file_inode(file));
  635. mutex_lock(&lp_mutex);
  636. switch (cmd) {
  637. case LPSETTIMEOUT_OLD:
  638. if (BITS_PER_LONG == 32) {
  639. ret = lp_set_timeout32(minor, (void __user *)arg);
  640. break;
  641. }
  642. fallthrough; /* for 64-bit */
  643. case LPSETTIMEOUT_NEW:
  644. ret = lp_set_timeout64(minor, (void __user *)arg);
  645. break;
  646. default:
  647. ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
  648. break;
  649. }
  650. mutex_unlock(&lp_mutex);
  651. return ret;
  652. }
  653. #ifdef CONFIG_COMPAT
  654. static long lp_compat_ioctl(struct file *file, unsigned int cmd,
  655. unsigned long arg)
  656. {
  657. unsigned int minor;
  658. int ret;
  659. minor = iminor(file_inode(file));
  660. mutex_lock(&lp_mutex);
  661. switch (cmd) {
  662. case LPSETTIMEOUT_OLD:
  663. if (!COMPAT_USE_64BIT_TIME) {
  664. ret = lp_set_timeout32(minor, (void __user *)arg);
  665. break;
  666. }
  667. fallthrough; /* for x32 mode */
  668. case LPSETTIMEOUT_NEW:
  669. ret = lp_set_timeout64(minor, (void __user *)arg);
  670. break;
  671. #ifdef LP_STATS
  672. case LPGETSTATS:
  673. /* FIXME: add an implementation if you set LP_STATS */
  674. ret = -EINVAL;
  675. break;
  676. #endif
  677. default:
  678. ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
  679. break;
  680. }
  681. mutex_unlock(&lp_mutex);
  682. return ret;
  683. }
  684. #endif
  685. static const struct file_operations lp_fops = {
  686. .owner = THIS_MODULE,
  687. .write = lp_write,
  688. .unlocked_ioctl = lp_ioctl,
  689. #ifdef CONFIG_COMPAT
  690. .compat_ioctl = lp_compat_ioctl,
  691. #endif
  692. .open = lp_open,
  693. .release = lp_release,
  694. #ifdef CONFIG_PARPORT_1284
  695. .read = lp_read,
  696. #endif
  697. .llseek = noop_llseek,
  698. };
  699. /* --- support for console on the line printer ----------------- */
  700. #ifdef CONFIG_LP_CONSOLE
  701. #define CONSOLE_LP 0
  702. /* If the printer is out of paper, we can either lose the messages or
  703. * stall until the printer is happy again. Define CONSOLE_LP_STRICT
  704. * non-zero to get the latter behaviour. */
  705. #define CONSOLE_LP_STRICT 1
  706. /* The console must be locked when we get here. */
  707. static void lp_console_write(struct console *co, const char *s,
  708. unsigned count)
  709. {
  710. struct pardevice *dev = lp_table[CONSOLE_LP].dev;
  711. struct parport *port = dev->port;
  712. ssize_t written;
  713. if (parport_claim(dev))
  714. /* Nothing we can do. */
  715. return;
  716. parport_set_timeout(dev, 0);
  717. /* Go to compatibility mode. */
  718. parport_negotiate(port, IEEE1284_MODE_COMPAT);
  719. do {
  720. /* Write the data, converting LF->CRLF as we go. */
  721. ssize_t canwrite = count;
  722. char *lf = memchr(s, '\n', count);
  723. if (lf)
  724. canwrite = lf - s;
  725. if (canwrite > 0) {
  726. written = parport_write(port, s, canwrite);
  727. if (written <= 0)
  728. continue;
  729. s += written;
  730. count -= written;
  731. canwrite -= written;
  732. }
  733. if (lf && canwrite <= 0) {
  734. const char *crlf = "\r\n";
  735. int i = 2;
  736. /* Dodge the original '\n', and put '\r\n' instead. */
  737. s++;
  738. count--;
  739. do {
  740. written = parport_write(port, crlf, i);
  741. if (written > 0) {
  742. i -= written;
  743. crlf += written;
  744. }
  745. } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
  746. }
  747. } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
  748. parport_release(dev);
  749. }
  750. static struct console lpcons = {
  751. .name = "lp",
  752. .write = lp_console_write,
  753. .flags = CON_PRINTBUFFER,
  754. };
  755. #endif /* console on line printer */
  756. /* --- initialisation code ------------------------------------- */
  757. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  758. static char *parport[LP_NO];
  759. static bool reset;
  760. module_param_array(parport, charp, NULL, 0);
  761. module_param(reset, bool, 0);
  762. #ifndef MODULE
  763. static int __init lp_setup(char *str)
  764. {
  765. static int parport_ptr;
  766. int x;
  767. if (get_option(&str, &x)) {
  768. if (x == 0) {
  769. /* disable driver on "lp=" or "lp=0" */
  770. parport_nr[0] = LP_PARPORT_OFF;
  771. } else {
  772. printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
  773. return 0;
  774. }
  775. } else if (!strncmp(str, "parport", 7)) {
  776. int n = simple_strtoul(str+7, NULL, 10);
  777. if (parport_ptr < LP_NO)
  778. parport_nr[parport_ptr++] = n;
  779. else
  780. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  781. str);
  782. } else if (!strcmp(str, "auto")) {
  783. parport_nr[0] = LP_PARPORT_AUTO;
  784. } else if (!strcmp(str, "none")) {
  785. if (parport_ptr < LP_NO)
  786. parport_nr[parport_ptr++] = LP_PARPORT_NONE;
  787. else
  788. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  789. str);
  790. } else if (!strcmp(str, "reset")) {
  791. reset = true;
  792. }
  793. return 1;
  794. }
  795. #endif
  796. static int lp_register(int nr, struct parport *port)
  797. {
  798. struct pardev_cb ppdev_cb;
  799. memset(&ppdev_cb, 0, sizeof(ppdev_cb));
  800. ppdev_cb.preempt = lp_preempt;
  801. ppdev_cb.private = &lp_table[nr];
  802. lp_table[nr].dev = parport_register_dev_model(port, "lp",
  803. &ppdev_cb, nr);
  804. if (lp_table[nr].dev == NULL)
  805. return 1;
  806. lp_table[nr].flags |= LP_EXIST;
  807. if (reset)
  808. lp_reset(nr);
  809. device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
  810. "lp%d", nr);
  811. printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
  812. (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
  813. #ifdef CONFIG_LP_CONSOLE
  814. if (!nr) {
  815. if (port->modes & PARPORT_MODE_SAFEININT) {
  816. register_console(&lpcons);
  817. console_registered = port;
  818. printk(KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
  819. } else
  820. printk(KERN_ERR "lp%d: cannot run console on %s\n",
  821. CONSOLE_LP, port->name);
  822. }
  823. #endif
  824. port_num[nr] = port->number;
  825. return 0;
  826. }
  827. static void lp_attach(struct parport *port)
  828. {
  829. unsigned int i;
  830. switch (parport_nr[0]) {
  831. case LP_PARPORT_UNSPEC:
  832. case LP_PARPORT_AUTO:
  833. if (parport_nr[0] == LP_PARPORT_AUTO &&
  834. port->probe_info[0].class != PARPORT_CLASS_PRINTER)
  835. return;
  836. if (lp_count == LP_NO) {
  837. printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
  838. return;
  839. }
  840. for (i = 0; i < LP_NO; i++)
  841. if (port_num[i] == -1)
  842. break;
  843. if (!lp_register(i, port))
  844. lp_count++;
  845. break;
  846. default:
  847. for (i = 0; i < LP_NO; i++) {
  848. if (port->number == parport_nr[i]) {
  849. if (!lp_register(i, port))
  850. lp_count++;
  851. break;
  852. }
  853. }
  854. break;
  855. }
  856. }
  857. static void lp_detach(struct parport *port)
  858. {
  859. int n;
  860. /* Write this some day. */
  861. #ifdef CONFIG_LP_CONSOLE
  862. if (console_registered == port) {
  863. unregister_console(&lpcons);
  864. console_registered = NULL;
  865. }
  866. #endif /* CONFIG_LP_CONSOLE */
  867. for (n = 0; n < LP_NO; n++) {
  868. if (port_num[n] == port->number) {
  869. port_num[n] = -1;
  870. lp_count--;
  871. device_destroy(lp_class, MKDEV(LP_MAJOR, n));
  872. parport_unregister_device(lp_table[n].dev);
  873. }
  874. }
  875. }
  876. static struct parport_driver lp_driver = {
  877. .name = "lp",
  878. .match_port = lp_attach,
  879. .detach = lp_detach,
  880. .devmodel = true,
  881. };
  882. static int __init lp_init(void)
  883. {
  884. int i, err = 0;
  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. lp_class = class_create(THIS_MODULE, "printer");
  911. if (IS_ERR(lp_class)) {
  912. err = PTR_ERR(lp_class);
  913. goto out_reg;
  914. }
  915. if (parport_register_driver(&lp_driver)) {
  916. printk(KERN_ERR "lp: unable to register with parport\n");
  917. err = -EIO;
  918. goto out_class;
  919. }
  920. if (!lp_count) {
  921. printk(KERN_INFO "lp: driver loaded but no devices found\n");
  922. #ifndef CONFIG_PARPORT_1284
  923. if (parport_nr[0] == LP_PARPORT_AUTO)
  924. printk(KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
  925. #endif
  926. }
  927. return 0;
  928. out_class:
  929. class_destroy(lp_class);
  930. out_reg:
  931. unregister_chrdev(LP_MAJOR, "lp");
  932. return err;
  933. }
  934. static int __init lp_init_module(void)
  935. {
  936. if (parport[0]) {
  937. /* The user gave some parameters. Let's see what they were. */
  938. if (!strncmp(parport[0], "auto", 4))
  939. parport_nr[0] = LP_PARPORT_AUTO;
  940. else {
  941. int n;
  942. for (n = 0; n < LP_NO && parport[n]; n++) {
  943. if (!strncmp(parport[n], "none", 4))
  944. parport_nr[n] = LP_PARPORT_NONE;
  945. else {
  946. char *ep;
  947. unsigned long r = simple_strtoul(parport[n], &ep, 0);
  948. if (ep != parport[n])
  949. parport_nr[n] = r;
  950. else {
  951. printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
  952. return -ENODEV;
  953. }
  954. }
  955. }
  956. }
  957. }
  958. return lp_init();
  959. }
  960. static void lp_cleanup_module(void)
  961. {
  962. parport_unregister_driver(&lp_driver);
  963. #ifdef CONFIG_LP_CONSOLE
  964. unregister_console(&lpcons);
  965. #endif
  966. unregister_chrdev(LP_MAJOR, "lp");
  967. class_destroy(lp_class);
  968. }
  969. __setup("lp=", lp_setup);
  970. module_init(lp_init_module);
  971. module_exit(lp_cleanup_module);
  972. MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
  973. MODULE_LICENSE("GPL");