lp.c 25 KB

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