lp.c 25 KB

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