PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-2.6.26/drivers/char/lp.c

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