lp.c 25 KB

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