lp.c 26 KB

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