lp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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/config.h>
  113. #include <linux/errno.h>
  114. #include <linux/kernel.h>
  115. #include <linux/major.h>
  116. #include <linux/sched.h>
  117. #include <linux/smp_lock.h>
  118. #include <linux/devfs_fs_kernel.h>
  119. #include <linux/slab.h>
  120. #include <linux/fcntl.h>
  121. #include <linux/delay.h>
  122. #include <linux/poll.h>
  123. #include <linux/console.h>
  124. #include <linux/device.h>
  125. #include <linux/wait.h>
  126. #include <linux/parport.h>
  127. #undef LP_STATS
  128. #include <linux/lp.h>
  129. #include <asm/irq.h>
  130. #include <asm/uaccess.h>
  131. #include <asm/system.h>
  132. /* if you have more than 8 printers, remember to increase LP_NO */
  133. #define LP_NO 8
  134. /* ROUND_UP macro from fs/select.c */
  135. #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
  136. static struct lp_struct lp_table[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; // initially NULL
  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) lp_release_parport (&lp_table[minor]);
  202. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  203. schedule_timeout(LP_TIMEOUT_POLLED);
  204. finish_wait(&lp_table[minor].waitq, &wait);
  205. if (polling) lp_claim_parport_or_block (&lp_table[minor]);
  206. else parport_yield_blocking (lp_table[minor].dev);
  207. }
  208. static int lp_check_status(int minor)
  209. {
  210. int error = 0;
  211. unsigned int last = lp_table[minor].last_error;
  212. unsigned char status = r_str(minor);
  213. if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
  214. /* No error. */
  215. last = 0;
  216. else if ((status & LP_POUTPA)) {
  217. if (last != LP_POUTPA) {
  218. last = LP_POUTPA;
  219. printk(KERN_INFO "lp%d out of paper\n", minor);
  220. }
  221. error = -ENOSPC;
  222. } else if (!(status & LP_PSELECD)) {
  223. if (last != LP_PSELECD) {
  224. last = LP_PSELECD;
  225. printk(KERN_INFO "lp%d off-line\n", minor);
  226. }
  227. error = -EIO;
  228. } else if (!(status & LP_PERRORP)) {
  229. if (last != LP_PERRORP) {
  230. last = LP_PERRORP;
  231. printk(KERN_INFO "lp%d on fire\n", minor);
  232. }
  233. error = -EIO;
  234. } else {
  235. last = 0; /* Come here if LP_CAREFUL is set and no
  236. errors are reported. */
  237. }
  238. lp_table[minor].last_error = last;
  239. if (last != 0)
  240. lp_error(minor);
  241. return error;
  242. }
  243. static int lp_wait_ready(int minor, int nonblock)
  244. {
  245. int error = 0;
  246. /* If we're not in compatibility mode, we're ready now! */
  247. if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
  248. return (0);
  249. }
  250. do {
  251. error = lp_check_status (minor);
  252. if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
  253. break;
  254. if (signal_pending (current)) {
  255. error = -EINTR;
  256. break;
  257. }
  258. } while (error);
  259. return error;
  260. }
  261. static ssize_t lp_write(struct file * file, const char __user * buf,
  262. size_t count, loff_t *ppos)
  263. {
  264. unsigned int minor = iminor(file->f_dentry->d_inode);
  265. struct parport *port = lp_table[minor].dev->port;
  266. char *kbuf = lp_table[minor].lp_buffer;
  267. ssize_t retv = 0;
  268. ssize_t written;
  269. size_t copy_size = count;
  270. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  271. (LP_F(minor) & LP_ABORT));
  272. #ifdef LP_STATS
  273. if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
  274. lp_table[minor].runchars = 0;
  275. lp_table[minor].lastcall = jiffies;
  276. #endif
  277. /* Need to copy the data from user-space. */
  278. if (copy_size > LP_BUFFER_SIZE)
  279. copy_size = LP_BUFFER_SIZE;
  280. if (down_interruptible (&lp_table[minor].port_mutex))
  281. return -EINTR;
  282. if (copy_from_user (kbuf, buf, copy_size)) {
  283. retv = -EFAULT;
  284. goto out_unlock;
  285. }
  286. /* Claim Parport or sleep until it becomes available
  287. */
  288. lp_claim_parport_or_block (&lp_table[minor]);
  289. /* Go to the proper mode. */
  290. lp_table[minor].current_mode = lp_negotiate (port,
  291. lp_table[minor].best_mode);
  292. parport_set_timeout (lp_table[minor].dev,
  293. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  294. : lp_table[minor].timeout));
  295. if ((retv = lp_wait_ready (minor, nonblock)) == 0)
  296. do {
  297. /* Write the data. */
  298. written = parport_write (port, kbuf, copy_size);
  299. if (written > 0) {
  300. copy_size -= written;
  301. count -= written;
  302. buf += written;
  303. retv += written;
  304. }
  305. if (signal_pending (current)) {
  306. if (retv == 0)
  307. retv = -EINTR;
  308. break;
  309. }
  310. if (copy_size > 0) {
  311. /* incomplete write -> check error ! */
  312. int error;
  313. parport_negotiate (lp_table[minor].dev->port,
  314. IEEE1284_MODE_COMPAT);
  315. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  316. error = lp_wait_ready (minor, nonblock);
  317. if (error) {
  318. if (retv == 0)
  319. retv = error;
  320. break;
  321. } else if (nonblock) {
  322. if (retv == 0)
  323. retv = -EAGAIN;
  324. break;
  325. }
  326. parport_yield_blocking (lp_table[minor].dev);
  327. lp_table[minor].current_mode
  328. = lp_negotiate (port,
  329. lp_table[minor].best_mode);
  330. } else if (need_resched())
  331. schedule ();
  332. if (count) {
  333. copy_size = count;
  334. if (copy_size > LP_BUFFER_SIZE)
  335. copy_size = LP_BUFFER_SIZE;
  336. if (copy_from_user(kbuf, buf, copy_size)) {
  337. if (retv == 0)
  338. retv = -EFAULT;
  339. break;
  340. }
  341. }
  342. } while (count > 0);
  343. if (test_and_clear_bit(LP_PREEMPT_REQUEST,
  344. &lp_table[minor].bits)) {
  345. printk(KERN_INFO "lp%d releasing parport\n", minor);
  346. parport_negotiate (lp_table[minor].dev->port,
  347. IEEE1284_MODE_COMPAT);
  348. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  349. lp_release_parport (&lp_table[minor]);
  350. }
  351. out_unlock:
  352. up (&lp_table[minor].port_mutex);
  353. return retv;
  354. }
  355. #ifdef CONFIG_PARPORT_1284
  356. /* Status readback conforming to ieee1284 */
  357. static ssize_t lp_read(struct file * file, char __user * buf,
  358. size_t count, loff_t *ppos)
  359. {
  360. DEFINE_WAIT(wait);
  361. unsigned int minor=iminor(file->f_dentry->d_inode);
  362. struct parport *port = lp_table[minor].dev->port;
  363. ssize_t retval = 0;
  364. char *kbuf = lp_table[minor].lp_buffer;
  365. int nonblock = ((file->f_flags & O_NONBLOCK) ||
  366. (LP_F(minor) & LP_ABORT));
  367. if (count > LP_BUFFER_SIZE)
  368. count = LP_BUFFER_SIZE;
  369. if (down_interruptible (&lp_table[minor].port_mutex))
  370. return -EINTR;
  371. lp_claim_parport_or_block (&lp_table[minor]);
  372. parport_set_timeout (lp_table[minor].dev,
  373. (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
  374. : lp_table[minor].timeout));
  375. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  376. if (parport_negotiate (lp_table[minor].dev->port,
  377. IEEE1284_MODE_NIBBLE)) {
  378. retval = -EIO;
  379. goto out;
  380. }
  381. while (retval == 0) {
  382. retval = parport_read (port, kbuf, count);
  383. if (retval > 0)
  384. break;
  385. if (nonblock) {
  386. retval = -EAGAIN;
  387. break;
  388. }
  389. /* Wait for data. */
  390. if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
  391. parport_negotiate (lp_table[minor].dev->port,
  392. IEEE1284_MODE_COMPAT);
  393. lp_error (minor);
  394. if (parport_negotiate (lp_table[minor].dev->port,
  395. IEEE1284_MODE_NIBBLE)) {
  396. retval = -EIO;
  397. goto out;
  398. }
  399. } else {
  400. prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
  401. schedule_timeout(LP_TIMEOUT_POLLED);
  402. finish_wait(&lp_table[minor].waitq, &wait);
  403. }
  404. if (signal_pending (current)) {
  405. retval = -ERESTARTSYS;
  406. break;
  407. }
  408. cond_resched ();
  409. }
  410. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  411. out:
  412. lp_release_parport (&lp_table[minor]);
  413. if (retval > 0 && copy_to_user (buf, kbuf, retval))
  414. retval = -EFAULT;
  415. up (&lp_table[minor].port_mutex);
  416. return retval;
  417. }
  418. #endif /* IEEE 1284 support */
  419. static int lp_open(struct inode * inode, struct file * file)
  420. {
  421. unsigned int minor = iminor(inode);
  422. if (minor >= LP_NO)
  423. return -ENXIO;
  424. if ((LP_F(minor) & LP_EXIST) == 0)
  425. return -ENXIO;
  426. if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
  427. return -EBUSY;
  428. /* If ABORTOPEN is set and the printer is offline or out of paper,
  429. we may still want to open it to perform ioctl()s. Therefore we
  430. have commandeered O_NONBLOCK, even though it is being used in
  431. a non-standard manner. This is strictly a Linux hack, and
  432. should most likely only ever be used by the tunelp application. */
  433. if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
  434. int status;
  435. lp_claim_parport_or_block (&lp_table[minor]);
  436. status = r_str(minor);
  437. lp_release_parport (&lp_table[minor]);
  438. if (status & LP_POUTPA) {
  439. printk(KERN_INFO "lp%d out of paper\n", minor);
  440. LP_F(minor) &= ~LP_BUSY;
  441. return -ENOSPC;
  442. } else if (!(status & LP_PSELECD)) {
  443. printk(KERN_INFO "lp%d off-line\n", minor);
  444. LP_F(minor) &= ~LP_BUSY;
  445. return -EIO;
  446. } else if (!(status & LP_PERRORP)) {
  447. printk(KERN_ERR "lp%d printer error\n", minor);
  448. LP_F(minor) &= ~LP_BUSY;
  449. return -EIO;
  450. }
  451. }
  452. lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
  453. if (!lp_table[minor].lp_buffer) {
  454. LP_F(minor) &= ~LP_BUSY;
  455. return -ENOMEM;
  456. }
  457. /* Determine if the peripheral supports ECP mode */
  458. lp_claim_parport_or_block (&lp_table[minor]);
  459. if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
  460. !parport_negotiate (lp_table[minor].dev->port,
  461. IEEE1284_MODE_ECP)) {
  462. printk (KERN_INFO "lp%d: ECP mode\n", minor);
  463. lp_table[minor].best_mode = IEEE1284_MODE_ECP;
  464. } else {
  465. lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
  466. }
  467. /* Leave peripheral in compatibility mode */
  468. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  469. lp_release_parport (&lp_table[minor]);
  470. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  471. return 0;
  472. }
  473. static int lp_release(struct inode * inode, struct file * file)
  474. {
  475. unsigned int minor = iminor(inode);
  476. lp_claim_parport_or_block (&lp_table[minor]);
  477. parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
  478. lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
  479. lp_release_parport (&lp_table[minor]);
  480. kfree(lp_table[minor].lp_buffer);
  481. lp_table[minor].lp_buffer = NULL;
  482. LP_F(minor) &= ~LP_BUSY;
  483. return 0;
  484. }
  485. static int lp_ioctl(struct inode *inode, struct file *file,
  486. unsigned int cmd, unsigned long arg)
  487. {
  488. unsigned int minor = iminor(inode);
  489. int status;
  490. int retval = 0;
  491. void __user *argp = (void __user *)arg;
  492. #ifdef LP_DEBUG
  493. printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
  494. #endif
  495. if (minor >= LP_NO)
  496. return -ENODEV;
  497. if ((LP_F(minor) & LP_EXIST) == 0)
  498. return -ENODEV;
  499. switch ( cmd ) {
  500. struct timeval par_timeout;
  501. long to_jiffies;
  502. case LPTIME:
  503. LP_TIME(minor) = arg * HZ/100;
  504. break;
  505. case LPCHAR:
  506. LP_CHAR(minor) = arg;
  507. break;
  508. case LPABORT:
  509. if (arg)
  510. LP_F(minor) |= LP_ABORT;
  511. else
  512. LP_F(minor) &= ~LP_ABORT;
  513. break;
  514. case LPABORTOPEN:
  515. if (arg)
  516. LP_F(minor) |= LP_ABORTOPEN;
  517. else
  518. LP_F(minor) &= ~LP_ABORTOPEN;
  519. break;
  520. case LPCAREFUL:
  521. if (arg)
  522. LP_F(minor) |= LP_CAREFUL;
  523. else
  524. LP_F(minor) &= ~LP_CAREFUL;
  525. break;
  526. case LPWAIT:
  527. LP_WAIT(minor) = arg;
  528. break;
  529. case LPSETIRQ:
  530. return -EINVAL;
  531. break;
  532. case LPGETIRQ:
  533. if (copy_to_user(argp, &LP_IRQ(minor),
  534. sizeof(int)))
  535. return -EFAULT;
  536. break;
  537. case LPGETSTATUS:
  538. lp_claim_parport_or_block (&lp_table[minor]);
  539. status = r_str(minor);
  540. lp_release_parport (&lp_table[minor]);
  541. if (copy_to_user(argp, &status, sizeof(int)))
  542. return -EFAULT;
  543. break;
  544. case LPRESET:
  545. lp_reset(minor);
  546. break;
  547. #ifdef LP_STATS
  548. case LPGETSTATS:
  549. if (copy_to_user(argp, &LP_STAT(minor),
  550. sizeof(struct lp_stats)))
  551. return -EFAULT;
  552. if (capable(CAP_SYS_ADMIN))
  553. memset(&LP_STAT(minor), 0,
  554. sizeof(struct lp_stats));
  555. break;
  556. #endif
  557. case LPGETFLAGS:
  558. status = LP_F(minor);
  559. if (copy_to_user(argp, &status, sizeof(int)))
  560. return -EFAULT;
  561. break;
  562. case LPSETTIMEOUT:
  563. if (copy_from_user (&par_timeout, argp,
  564. sizeof (struct timeval))) {
  565. return -EFAULT;
  566. }
  567. /* Convert to jiffies, place in lp_table */
  568. if ((par_timeout.tv_sec < 0) ||
  569. (par_timeout.tv_usec < 0)) {
  570. return -EINVAL;
  571. }
  572. to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
  573. to_jiffies += par_timeout.tv_sec * (long) HZ;
  574. if (to_jiffies <= 0) {
  575. return -EINVAL;
  576. }
  577. lp_table[minor].timeout = to_jiffies;
  578. break;
  579. default:
  580. retval = -EINVAL;
  581. }
  582. return retval;
  583. }
  584. static struct file_operations lp_fops = {
  585. .owner = THIS_MODULE,
  586. .write = lp_write,
  587. .ioctl = lp_ioctl,
  588. .open = lp_open,
  589. .release = lp_release,
  590. #ifdef CONFIG_PARPORT_1284
  591. .read = lp_read,
  592. #endif
  593. };
  594. /* --- support for console on the line printer ----------------- */
  595. #ifdef CONFIG_LP_CONSOLE
  596. #define CONSOLE_LP 0
  597. /* If the printer is out of paper, we can either lose the messages or
  598. * stall until the printer is happy again. Define CONSOLE_LP_STRICT
  599. * non-zero to get the latter behaviour. */
  600. #define CONSOLE_LP_STRICT 1
  601. /* The console must be locked when we get here. */
  602. static void lp_console_write (struct console *co, const char *s,
  603. unsigned count)
  604. {
  605. struct pardevice *dev = lp_table[CONSOLE_LP].dev;
  606. struct parport *port = dev->port;
  607. ssize_t written;
  608. if (parport_claim (dev))
  609. /* Nothing we can do. */
  610. return;
  611. parport_set_timeout (dev, 0);
  612. /* Go to compatibility mode. */
  613. parport_negotiate (port, IEEE1284_MODE_COMPAT);
  614. do {
  615. /* Write the data, converting LF->CRLF as we go. */
  616. ssize_t canwrite = count;
  617. char *lf = memchr (s, '\n', count);
  618. if (lf)
  619. canwrite = lf - s;
  620. if (canwrite > 0) {
  621. written = parport_write (port, s, canwrite);
  622. if (written <= 0)
  623. continue;
  624. s += written;
  625. count -= written;
  626. canwrite -= written;
  627. }
  628. if (lf && canwrite <= 0) {
  629. const char *crlf = "\r\n";
  630. int i = 2;
  631. /* Dodge the original '\n', and put '\r\n' instead. */
  632. s++;
  633. count--;
  634. do {
  635. written = parport_write (port, crlf, i);
  636. if (written > 0)
  637. i -= written, crlf += written;
  638. } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
  639. }
  640. } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
  641. parport_release (dev);
  642. }
  643. static struct console lpcons = {
  644. .name = "lp",
  645. .write = lp_console_write,
  646. .flags = CON_PRINTBUFFER,
  647. };
  648. #endif /* console on line printer */
  649. /* --- initialisation code ------------------------------------- */
  650. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  651. static char *parport[LP_NO] = { NULL, };
  652. static int reset = 0;
  653. module_param_array(parport, charp, NULL, 0);
  654. module_param(reset, bool, 0);
  655. #ifndef MODULE
  656. static int __init lp_setup (char *str)
  657. {
  658. static int parport_ptr; // initially zero
  659. int x;
  660. if (get_option (&str, &x)) {
  661. if (x == 0) {
  662. /* disable driver on "lp=" or "lp=0" */
  663. parport_nr[0] = LP_PARPORT_OFF;
  664. } else {
  665. printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
  666. return 0;
  667. }
  668. } else if (!strncmp(str, "parport", 7)) {
  669. int n = simple_strtoul(str+7, NULL, 10);
  670. if (parport_ptr < LP_NO)
  671. parport_nr[parport_ptr++] = n;
  672. else
  673. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  674. str);
  675. } else if (!strcmp(str, "auto")) {
  676. parport_nr[0] = LP_PARPORT_AUTO;
  677. } else if (!strcmp(str, "none")) {
  678. parport_nr[parport_ptr++] = LP_PARPORT_NONE;
  679. } else if (!strcmp(str, "reset")) {
  680. reset = 1;
  681. }
  682. return 1;
  683. }
  684. #endif
  685. static int lp_register(int nr, struct parport *port)
  686. {
  687. lp_table[nr].dev = parport_register_device(port, "lp",
  688. lp_preempt, NULL, NULL, 0,
  689. (void *) &lp_table[nr]);
  690. if (lp_table[nr].dev == NULL)
  691. return 1;
  692. lp_table[nr].flags |= LP_EXIST;
  693. if (reset)
  694. lp_reset(nr);
  695. class_device_create(lp_class, MKDEV(LP_MAJOR, nr), NULL,
  696. "lp%d", nr);
  697. devfs_mk_cdev(MKDEV(LP_MAJOR, nr), S_IFCHR | S_IRUGO | S_IWUGO,
  698. "printers/%d", nr);
  699. printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
  700. (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
  701. #ifdef CONFIG_LP_CONSOLE
  702. if (!nr) {
  703. if (port->modes & PARPORT_MODE_SAFEININT) {
  704. register_console (&lpcons);
  705. console_registered = port;
  706. printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
  707. } else
  708. printk (KERN_ERR "lp%d: cannot run console on %s\n",
  709. CONSOLE_LP, port->name);
  710. }
  711. #endif
  712. return 0;
  713. }
  714. static void lp_attach (struct parport *port)
  715. {
  716. unsigned int i;
  717. switch (parport_nr[0])
  718. {
  719. case LP_PARPORT_UNSPEC:
  720. case LP_PARPORT_AUTO:
  721. if (parport_nr[0] == LP_PARPORT_AUTO &&
  722. port->probe_info[0].class != PARPORT_CLASS_PRINTER)
  723. return;
  724. if (lp_count == LP_NO) {
  725. printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
  726. return;
  727. }
  728. if (!lp_register(lp_count, port))
  729. lp_count++;
  730. break;
  731. default:
  732. for (i = 0; i < LP_NO; i++) {
  733. if (port->number == parport_nr[i]) {
  734. if (!lp_register(i, port))
  735. lp_count++;
  736. break;
  737. }
  738. }
  739. break;
  740. }
  741. }
  742. static void lp_detach (struct parport *port)
  743. {
  744. /* Write this some day. */
  745. #ifdef CONFIG_LP_CONSOLE
  746. if (console_registered == port) {
  747. unregister_console (&lpcons);
  748. console_registered = NULL;
  749. }
  750. #endif /* CONFIG_LP_CONSOLE */
  751. }
  752. static struct parport_driver lp_driver = {
  753. .name = "lp",
  754. .attach = lp_attach,
  755. .detach = lp_detach,
  756. };
  757. static int __init lp_init (void)
  758. {
  759. int i, err = 0;
  760. if (parport_nr[0] == LP_PARPORT_OFF)
  761. return 0;
  762. for (i = 0; i < LP_NO; i++) {
  763. lp_table[i].dev = NULL;
  764. lp_table[i].flags = 0;
  765. lp_table[i].chars = LP_INIT_CHAR;
  766. lp_table[i].time = LP_INIT_TIME;
  767. lp_table[i].wait = LP_INIT_WAIT;
  768. lp_table[i].lp_buffer = NULL;
  769. #ifdef LP_STATS
  770. lp_table[i].lastcall = 0;
  771. lp_table[i].runchars = 0;
  772. memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
  773. #endif
  774. lp_table[i].last_error = 0;
  775. init_waitqueue_head (&lp_table[i].waitq);
  776. init_waitqueue_head (&lp_table[i].dataq);
  777. init_MUTEX (&lp_table[i].port_mutex);
  778. lp_table[i].timeout = 10 * HZ;
  779. }
  780. if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
  781. printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
  782. return -EIO;
  783. }
  784. devfs_mk_dir("printers");
  785. lp_class = class_create(THIS_MODULE, "printer");
  786. if (IS_ERR(lp_class)) {
  787. err = PTR_ERR(lp_class);
  788. goto out_devfs;
  789. }
  790. if (parport_register_driver (&lp_driver)) {
  791. printk (KERN_ERR "lp: unable to register with parport\n");
  792. err = -EIO;
  793. goto out_class;
  794. }
  795. if (!lp_count) {
  796. printk (KERN_INFO "lp: driver loaded but no devices found\n");
  797. #ifndef CONFIG_PARPORT_1284
  798. if (parport_nr[0] == LP_PARPORT_AUTO)
  799. printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
  800. #endif
  801. }
  802. return 0;
  803. out_class:
  804. class_destroy(lp_class);
  805. out_devfs:
  806. devfs_remove("printers");
  807. unregister_chrdev(LP_MAJOR, "lp");
  808. return err;
  809. }
  810. static int __init lp_init_module (void)
  811. {
  812. if (parport[0]) {
  813. /* The user gave some parameters. Let's see what they were. */
  814. if (!strncmp(parport[0], "auto", 4))
  815. parport_nr[0] = LP_PARPORT_AUTO;
  816. else {
  817. int n;
  818. for (n = 0; n < LP_NO && parport[n]; n++) {
  819. if (!strncmp(parport[n], "none", 4))
  820. parport_nr[n] = LP_PARPORT_NONE;
  821. else {
  822. char *ep;
  823. unsigned long r = simple_strtoul(parport[n], &ep, 0);
  824. if (ep != parport[n])
  825. parport_nr[n] = r;
  826. else {
  827. printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
  828. return -ENODEV;
  829. }
  830. }
  831. }
  832. }
  833. }
  834. return lp_init();
  835. }
  836. static void lp_cleanup_module (void)
  837. {
  838. unsigned int offset;
  839. parport_unregister_driver (&lp_driver);
  840. #ifdef CONFIG_LP_CONSOLE
  841. unregister_console (&lpcons);
  842. #endif
  843. unregister_chrdev(LP_MAJOR, "lp");
  844. for (offset = 0; offset < LP_NO; offset++) {
  845. if (lp_table[offset].dev == NULL)
  846. continue;
  847. parport_unregister_device(lp_table[offset].dev);
  848. devfs_remove("printers/%d", offset);
  849. class_device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
  850. }
  851. devfs_remove("printers");
  852. class_destroy(lp_class);
  853. }
  854. __setup("lp=", lp_setup);
  855. module_init(lp_init_module);
  856. module_exit(lp_cleanup_module);
  857. MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
  858. MODULE_LICENSE("GPL");