lp.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. LP_TIME(minor) = arg * HZ/100;
  512. break;
  513. case LPCHAR:
  514. LP_CHAR(minor) = arg;
  515. break;
  516. case LPABORT:
  517. if (arg)
  518. LP_F(minor) |= LP_ABORT;
  519. else
  520. LP_F(minor) &= ~LP_ABORT;
  521. break;
  522. case LPABORTOPEN:
  523. if (arg)
  524. LP_F(minor) |= LP_ABORTOPEN;
  525. else
  526. LP_F(minor) &= ~LP_ABORTOPEN;
  527. break;
  528. case LPCAREFUL:
  529. if (arg)
  530. LP_F(minor) |= LP_CAREFUL;
  531. else
  532. LP_F(minor) &= ~LP_CAREFUL;
  533. break;
  534. case LPWAIT:
  535. LP_WAIT(minor) = arg;
  536. break;
  537. case LPSETIRQ:
  538. return -EINVAL;
  539. break;
  540. case LPGETIRQ:
  541. if (copy_to_user(argp, &LP_IRQ(minor),
  542. sizeof(int)))
  543. return -EFAULT;
  544. break;
  545. case LPGETSTATUS:
  546. if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
  547. return -EINTR;
  548. lp_claim_parport_or_block (&lp_table[minor]);
  549. status = r_str(minor);
  550. lp_release_parport (&lp_table[minor]);
  551. mutex_unlock(&lp_table[minor].port_mutex);
  552. if (copy_to_user(argp, &status, sizeof(int)))
  553. return -EFAULT;
  554. break;
  555. case LPRESET:
  556. lp_reset(minor);
  557. break;
  558. #ifdef LP_STATS
  559. case LPGETSTATS:
  560. if (copy_to_user(argp, &LP_STAT(minor),
  561. sizeof(struct lp_stats)))
  562. return -EFAULT;
  563. if (capable(CAP_SYS_ADMIN))
  564. memset(&LP_STAT(minor), 0,
  565. sizeof(struct lp_stats));
  566. break;
  567. #endif
  568. case LPGETFLAGS:
  569. status = LP_F(minor);
  570. if (copy_to_user(argp, &status, sizeof(int)))
  571. return -EFAULT;
  572. break;
  573. default:
  574. retval = -EINVAL;
  575. }
  576. return retval;
  577. }
  578. static int lp_set_timeout(unsigned int minor, struct timeval *par_timeout)
  579. {
  580. long to_jiffies;
  581. /* Convert to jiffies, place in lp_table */
  582. if ((par_timeout->tv_sec < 0) ||
  583. (par_timeout->tv_usec < 0)) {
  584. return -EINVAL;
  585. }
  586. to_jiffies = DIV_ROUND_UP(par_timeout->tv_usec, 1000000/HZ);
  587. to_jiffies += par_timeout->tv_sec * (long) HZ;
  588. if (to_jiffies <= 0) {
  589. return -EINVAL;
  590. }
  591. lp_table[minor].timeout = to_jiffies;
  592. return 0;
  593. }
  594. static long lp_ioctl(struct file *file, unsigned int cmd,
  595. unsigned long arg)
  596. {
  597. unsigned int minor;
  598. struct timeval par_timeout;
  599. int ret;
  600. minor = iminor(file_inode(file));
  601. mutex_lock(&lp_mutex);
  602. switch (cmd) {
  603. case LPSETTIMEOUT:
  604. if (copy_from_user(&par_timeout, (void __user *)arg,
  605. sizeof (struct timeval))) {
  606. ret = -EFAULT;
  607. break;
  608. }
  609. ret = lp_set_timeout(minor, &par_timeout);
  610. break;
  611. default:
  612. ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
  613. break;
  614. }
  615. mutex_unlock(&lp_mutex);
  616. return ret;
  617. }
  618. #ifdef CONFIG_COMPAT
  619. static long lp_compat_ioctl(struct file *file, unsigned int cmd,
  620. unsigned long arg)
  621. {
  622. unsigned int minor;
  623. struct timeval par_timeout;
  624. int ret;
  625. minor = iminor(file_inode(file));
  626. mutex_lock(&lp_mutex);
  627. switch (cmd) {
  628. case LPSETTIMEOUT:
  629. if (compat_get_timeval(&par_timeout, compat_ptr(arg))) {
  630. ret = -EFAULT;
  631. break;
  632. }
  633. ret = lp_set_timeout(minor, &par_timeout);
  634. break;
  635. #ifdef LP_STATS
  636. case LPGETSTATS:
  637. /* FIXME: add an implementation if you set LP_STATS */
  638. ret = -EINVAL;
  639. break;
  640. #endif
  641. default:
  642. ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
  643. break;
  644. }
  645. mutex_unlock(&lp_mutex);
  646. return ret;
  647. }
  648. #endif
  649. static const struct file_operations lp_fops = {
  650. .owner = THIS_MODULE,
  651. .write = lp_write,
  652. .unlocked_ioctl = lp_ioctl,
  653. #ifdef CONFIG_COMPAT
  654. .compat_ioctl = lp_compat_ioctl,
  655. #endif
  656. .open = lp_open,
  657. .release = lp_release,
  658. #ifdef CONFIG_PARPORT_1284
  659. .read = lp_read,
  660. #endif
  661. .llseek = noop_llseek,
  662. };
  663. /* --- support for console on the line printer ----------------- */
  664. #ifdef CONFIG_LP_CONSOLE
  665. #define CONSOLE_LP 0
  666. /* If the printer is out of paper, we can either lose the messages or
  667. * stall until the printer is happy again. Define CONSOLE_LP_STRICT
  668. * non-zero to get the latter behaviour. */
  669. #define CONSOLE_LP_STRICT 1
  670. /* The console must be locked when we get here. */
  671. static void lp_console_write (struct console *co, const char *s,
  672. unsigned count)
  673. {
  674. struct pardevice *dev = lp_table[CONSOLE_LP].dev;
  675. struct parport *port = dev->port;
  676. ssize_t written;
  677. if (parport_claim (dev))
  678. /* Nothing we can do. */
  679. return;
  680. parport_set_timeout (dev, 0);
  681. /* Go to compatibility mode. */
  682. parport_negotiate (port, IEEE1284_MODE_COMPAT);
  683. do {
  684. /* Write the data, converting LF->CRLF as we go. */
  685. ssize_t canwrite = count;
  686. char *lf = memchr (s, '\n', count);
  687. if (lf)
  688. canwrite = lf - s;
  689. if (canwrite > 0) {
  690. written = parport_write (port, s, canwrite);
  691. if (written <= 0)
  692. continue;
  693. s += written;
  694. count -= written;
  695. canwrite -= written;
  696. }
  697. if (lf && canwrite <= 0) {
  698. const char *crlf = "\r\n";
  699. int i = 2;
  700. /* Dodge the original '\n', and put '\r\n' instead. */
  701. s++;
  702. count--;
  703. do {
  704. written = parport_write (port, crlf, i);
  705. if (written > 0)
  706. i -= written, crlf += written;
  707. } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
  708. }
  709. } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
  710. parport_release (dev);
  711. }
  712. static struct console lpcons = {
  713. .name = "lp",
  714. .write = lp_console_write,
  715. .flags = CON_PRINTBUFFER,
  716. };
  717. #endif /* console on line printer */
  718. /* --- initialisation code ------------------------------------- */
  719. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  720. static char *parport[LP_NO];
  721. static bool reset;
  722. module_param_array(parport, charp, NULL, 0);
  723. module_param(reset, bool, 0);
  724. #ifndef MODULE
  725. static int __init lp_setup (char *str)
  726. {
  727. static int parport_ptr;
  728. int x;
  729. if (get_option(&str, &x)) {
  730. if (x == 0) {
  731. /* disable driver on "lp=" or "lp=0" */
  732. parport_nr[0] = LP_PARPORT_OFF;
  733. } else {
  734. printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
  735. return 0;
  736. }
  737. } else if (!strncmp(str, "parport", 7)) {
  738. int n = simple_strtoul(str+7, NULL, 10);
  739. if (parport_ptr < LP_NO)
  740. parport_nr[parport_ptr++] = n;
  741. else
  742. printk(KERN_INFO "lp: too many ports, %s ignored.\n",
  743. str);
  744. } else if (!strcmp(str, "auto")) {
  745. parport_nr[0] = LP_PARPORT_AUTO;
  746. } else if (!strcmp(str, "none")) {
  747. parport_nr[parport_ptr++] = LP_PARPORT_NONE;
  748. } else if (!strcmp(str, "reset")) {
  749. reset = 1;
  750. }
  751. return 1;
  752. }
  753. #endif
  754. static int lp_register(int nr, struct parport *port)
  755. {
  756. lp_table[nr].dev = parport_register_device(port, "lp",
  757. lp_preempt, NULL, NULL, 0,
  758. (void *) &lp_table[nr]);
  759. if (lp_table[nr].dev == NULL)
  760. return 1;
  761. lp_table[nr].flags |= LP_EXIST;
  762. if (reset)
  763. lp_reset(nr);
  764. device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
  765. "lp%d", nr);
  766. printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
  767. (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
  768. #ifdef CONFIG_LP_CONSOLE
  769. if (!nr) {
  770. if (port->modes & PARPORT_MODE_SAFEININT) {
  771. register_console(&lpcons);
  772. console_registered = port;
  773. printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
  774. } else
  775. printk (KERN_ERR "lp%d: cannot run console on %s\n",
  776. CONSOLE_LP, port->name);
  777. }
  778. #endif
  779. return 0;
  780. }
  781. static void lp_attach (struct parport *port)
  782. {
  783. unsigned int i;
  784. switch (parport_nr[0]) {
  785. case LP_PARPORT_UNSPEC:
  786. case LP_PARPORT_AUTO:
  787. if (parport_nr[0] == LP_PARPORT_AUTO &&
  788. port->probe_info[0].class != PARPORT_CLASS_PRINTER)
  789. return;
  790. if (lp_count == LP_NO) {
  791. printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
  792. return;
  793. }
  794. if (!lp_register(lp_count, port))
  795. lp_count++;
  796. break;
  797. default:
  798. for (i = 0; i < LP_NO; i++) {
  799. if (port->number == parport_nr[i]) {
  800. if (!lp_register(i, port))
  801. lp_count++;
  802. break;
  803. }
  804. }
  805. break;
  806. }
  807. }
  808. static void lp_detach (struct parport *port)
  809. {
  810. /* Write this some day. */
  811. #ifdef CONFIG_LP_CONSOLE
  812. if (console_registered == port) {
  813. unregister_console(&lpcons);
  814. console_registered = NULL;
  815. }
  816. #endif /* CONFIG_LP_CONSOLE */
  817. }
  818. static struct parport_driver lp_driver = {
  819. .name = "lp",
  820. .attach = lp_attach,
  821. .detach = lp_detach,
  822. };
  823. static int __init lp_init (void)
  824. {
  825. int i, err = 0;
  826. if (parport_nr[0] == LP_PARPORT_OFF)
  827. return 0;
  828. for (i = 0; i < LP_NO; i++) {
  829. lp_table[i].dev = NULL;
  830. lp_table[i].flags = 0;
  831. lp_table[i].chars = LP_INIT_CHAR;
  832. lp_table[i].time = LP_INIT_TIME;
  833. lp_table[i].wait = LP_INIT_WAIT;
  834. lp_table[i].lp_buffer = NULL;
  835. #ifdef LP_STATS
  836. lp_table[i].lastcall = 0;
  837. lp_table[i].runchars = 0;
  838. memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
  839. #endif
  840. lp_table[i].last_error = 0;
  841. init_waitqueue_head (&lp_table[i].waitq);
  842. init_waitqueue_head (&lp_table[i].dataq);
  843. mutex_init(&lp_table[i].port_mutex);
  844. lp_table[i].timeout = 10 * HZ;
  845. }
  846. if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
  847. printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
  848. return -EIO;
  849. }
  850. lp_class = class_create(THIS_MODULE, "printer");
  851. if (IS_ERR(lp_class)) {
  852. err = PTR_ERR(lp_class);
  853. goto out_reg;
  854. }
  855. if (parport_register_driver (&lp_driver)) {
  856. printk (KERN_ERR "lp: unable to register with parport\n");
  857. err = -EIO;
  858. goto out_class;
  859. }
  860. if (!lp_count) {
  861. printk (KERN_INFO "lp: driver loaded but no devices found\n");
  862. #ifndef CONFIG_PARPORT_1284
  863. if (parport_nr[0] == LP_PARPORT_AUTO)
  864. printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
  865. #endif
  866. }
  867. return 0;
  868. out_class:
  869. class_destroy(lp_class);
  870. out_reg:
  871. unregister_chrdev(LP_MAJOR, "lp");
  872. return err;
  873. }
  874. static int __init lp_init_module (void)
  875. {
  876. if (parport[0]) {
  877. /* The user gave some parameters. Let's see what they were. */
  878. if (!strncmp(parport[0], "auto", 4))
  879. parport_nr[0] = LP_PARPORT_AUTO;
  880. else {
  881. int n;
  882. for (n = 0; n < LP_NO && parport[n]; n++) {
  883. if (!strncmp(parport[n], "none", 4))
  884. parport_nr[n] = LP_PARPORT_NONE;
  885. else {
  886. char *ep;
  887. unsigned long r = simple_strtoul(parport[n], &ep, 0);
  888. if (ep != parport[n])
  889. parport_nr[n] = r;
  890. else {
  891. printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
  892. return -ENODEV;
  893. }
  894. }
  895. }
  896. }
  897. }
  898. return lp_init();
  899. }
  900. static void lp_cleanup_module (void)
  901. {
  902. unsigned int offset;
  903. parport_unregister_driver (&lp_driver);
  904. #ifdef CONFIG_LP_CONSOLE
  905. unregister_console (&lpcons);
  906. #endif
  907. unregister_chrdev(LP_MAJOR, "lp");
  908. for (offset = 0; offset < LP_NO; offset++) {
  909. if (lp_table[offset].dev == NULL)
  910. continue;
  911. parport_unregister_device(lp_table[offset].dev);
  912. device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
  913. }
  914. class_destroy(lp_class);
  915. }
  916. __setup("lp=", lp_setup);
  917. module_init(lp_init_module);
  918. module_exit(lp_cleanup_module);
  919. MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
  920. MODULE_LICENSE("GPL");