ppdev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * linux/drivers/char/ppdev.c
  3. *
  4. * This is the code behind /dev/parport* -- it allows a user-space
  5. * application to use the parport subsystem.
  6. *
  7. * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * A /dev/parportx device node represents an arbitrary device
  15. * on port 'x'. The following operations are possible:
  16. *
  17. * open do nothing, set up default IEEE 1284 protocol to be COMPAT
  18. * close release port and unregister device (if necessary)
  19. * ioctl
  20. * EXCL register device exclusively (may fail)
  21. * CLAIM (register device first time) parport_claim_or_block
  22. * RELEASE parport_release
  23. * SETMODE set the IEEE 1284 protocol to use for read/write
  24. * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be
  25. * confused with ioctl(fd, SETPHASER, &stun). ;-)
  26. * DATADIR data_forward / data_reverse
  27. * WDATA write_data
  28. * RDATA read_data
  29. * WCONTROL write_control
  30. * RCONTROL read_control
  31. * FCONTROL frob_control
  32. * RSTATUS read_status
  33. * NEGOT parport_negotiate
  34. * YIELD parport_yield_blocking
  35. * WCTLONIRQ on interrupt, set control lines
  36. * CLRIRQ clear (and return) interrupt count
  37. * SETTIME sets device timeout (struct timeval)
  38. * GETTIME gets device timeout (struct timeval)
  39. * GETMODES gets hardware supported modes (unsigned int)
  40. * GETMODE gets the current IEEE1284 mode
  41. * GETPHASE gets the current IEEE1284 phase
  42. * GETFLAGS gets current (user-visible) flags
  43. * SETFLAGS sets current (user-visible) flags
  44. * read/write read or write in current IEEE 1284 protocol
  45. * select wait for interrupt (in readfds)
  46. *
  47. * Changes:
  48. * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
  49. *
  50. * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
  51. * - On error, copy_from_user and copy_to_user do not return -EFAULT,
  52. * They return the positive number of bytes *not* copied due to address
  53. * space errors.
  54. *
  55. * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
  56. * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
  57. */
  58. #include <linux/module.h>
  59. #include <linux/init.h>
  60. #include <linux/sched.h>
  61. #include <linux/device.h>
  62. #include <linux/ioctl.h>
  63. #include <linux/parport.h>
  64. #include <linux/ctype.h>
  65. #include <linux/poll.h>
  66. #include <linux/slab.h>
  67. #include <linux/major.h>
  68. #include <linux/ppdev.h>
  69. #include <linux/mutex.h>
  70. #include <linux/uaccess.h>
  71. #define PP_VERSION "ppdev: user-space parallel port driver"
  72. #define CHRDEV "ppdev"
  73. struct pp_struct {
  74. struct pardevice * pdev;
  75. wait_queue_head_t irq_wait;
  76. atomic_t irqc;
  77. unsigned int flags;
  78. int irqresponse;
  79. unsigned char irqctl;
  80. struct ieee1284_info state;
  81. struct ieee1284_info saved_state;
  82. long default_inactivity;
  83. };
  84. /* pp_struct.flags bitfields */
  85. #define PP_CLAIMED (1<<0)
  86. #define PP_EXCL (1<<1)
  87. /* Other constants */
  88. #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
  89. #define PP_BUFFER_SIZE 1024
  90. #define PARDEVICE_MAX 8
  91. /* ROUND_UP macro from fs/select.c */
  92. #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
  93. static DEFINE_MUTEX(pp_do_mutex);
  94. static inline void pp_enable_irq (struct pp_struct *pp)
  95. {
  96. struct parport *port = pp->pdev->port;
  97. port->ops->enable_irq (port);
  98. }
  99. static ssize_t pp_read (struct file * file, char __user * buf, size_t count,
  100. loff_t * ppos)
  101. {
  102. unsigned int minor = iminor(file_inode(file));
  103. struct pp_struct *pp = file->private_data;
  104. char * kbuffer;
  105. ssize_t bytes_read = 0;
  106. struct parport *pport;
  107. int mode;
  108. if (!(pp->flags & PP_CLAIMED)) {
  109. /* Don't have the port claimed */
  110. pr_debug(CHRDEV "%x: claim the port first\n", minor);
  111. return -EINVAL;
  112. }
  113. /* Trivial case. */
  114. if (count == 0)
  115. return 0;
  116. kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
  117. if (!kbuffer) {
  118. return -ENOMEM;
  119. }
  120. pport = pp->pdev->port;
  121. mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  122. parport_set_timeout (pp->pdev,
  123. (file->f_flags & O_NONBLOCK) ?
  124. PARPORT_INACTIVITY_O_NONBLOCK :
  125. pp->default_inactivity);
  126. while (bytes_read == 0) {
  127. ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
  128. if (mode == IEEE1284_MODE_EPP) {
  129. /* various specials for EPP mode */
  130. int flags = 0;
  131. size_t (*fn)(struct parport *, void *, size_t, int);
  132. if (pp->flags & PP_W91284PIC) {
  133. flags |= PARPORT_W91284PIC;
  134. }
  135. if (pp->flags & PP_FASTREAD) {
  136. flags |= PARPORT_EPP_FAST;
  137. }
  138. if (pport->ieee1284.mode & IEEE1284_ADDR) {
  139. fn = pport->ops->epp_read_addr;
  140. } else {
  141. fn = pport->ops->epp_read_data;
  142. }
  143. bytes_read = (*fn)(pport, kbuffer, need, flags);
  144. } else {
  145. bytes_read = parport_read (pport, kbuffer, need);
  146. }
  147. if (bytes_read != 0)
  148. break;
  149. if (file->f_flags & O_NONBLOCK) {
  150. bytes_read = -EAGAIN;
  151. break;
  152. }
  153. if (signal_pending (current)) {
  154. bytes_read = -ERESTARTSYS;
  155. break;
  156. }
  157. cond_resched();
  158. }
  159. parport_set_timeout (pp->pdev, pp->default_inactivity);
  160. if (bytes_read > 0 && copy_to_user (buf, kbuffer, bytes_read))
  161. bytes_read = -EFAULT;
  162. kfree (kbuffer);
  163. pp_enable_irq (pp);
  164. return bytes_read;
  165. }
  166. static ssize_t pp_write (struct file * file, const char __user * buf,
  167. size_t count, loff_t * ppos)
  168. {
  169. unsigned int minor = iminor(file_inode(file));
  170. struct pp_struct *pp = file->private_data;
  171. char * kbuffer;
  172. ssize_t bytes_written = 0;
  173. ssize_t wrote;
  174. int mode;
  175. struct parport *pport;
  176. if (!(pp->flags & PP_CLAIMED)) {
  177. /* Don't have the port claimed */
  178. pr_debug(CHRDEV "%x: claim the port first\n", minor);
  179. return -EINVAL;
  180. }
  181. kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
  182. if (!kbuffer) {
  183. return -ENOMEM;
  184. }
  185. pport = pp->pdev->port;
  186. mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  187. parport_set_timeout (pp->pdev,
  188. (file->f_flags & O_NONBLOCK) ?
  189. PARPORT_INACTIVITY_O_NONBLOCK :
  190. pp->default_inactivity);
  191. while (bytes_written < count) {
  192. ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
  193. if (copy_from_user (kbuffer, buf + bytes_written, n)) {
  194. bytes_written = -EFAULT;
  195. break;
  196. }
  197. if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
  198. /* do a fast EPP write */
  199. if (pport->ieee1284.mode & IEEE1284_ADDR) {
  200. wrote = pport->ops->epp_write_addr (pport,
  201. kbuffer, n, PARPORT_EPP_FAST);
  202. } else {
  203. wrote = pport->ops->epp_write_data (pport,
  204. kbuffer, n, PARPORT_EPP_FAST);
  205. }
  206. } else {
  207. wrote = parport_write (pp->pdev->port, kbuffer, n);
  208. }
  209. if (wrote <= 0) {
  210. if (!bytes_written) {
  211. bytes_written = wrote;
  212. }
  213. break;
  214. }
  215. bytes_written += wrote;
  216. if (file->f_flags & O_NONBLOCK) {
  217. if (!bytes_written)
  218. bytes_written = -EAGAIN;
  219. break;
  220. }
  221. if (signal_pending (current))
  222. break;
  223. cond_resched();
  224. }
  225. parport_set_timeout (pp->pdev, pp->default_inactivity);
  226. kfree (kbuffer);
  227. pp_enable_irq (pp);
  228. return bytes_written;
  229. }
  230. static void pp_irq (void *private)
  231. {
  232. struct pp_struct *pp = private;
  233. if (pp->irqresponse) {
  234. parport_write_control (pp->pdev->port, pp->irqctl);
  235. pp->irqresponse = 0;
  236. }
  237. atomic_inc (&pp->irqc);
  238. wake_up_interruptible (&pp->irq_wait);
  239. }
  240. static int register_device (int minor, struct pp_struct *pp)
  241. {
  242. struct parport *port;
  243. struct pardevice * pdev = NULL;
  244. char *name;
  245. int fl;
  246. name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
  247. if (name == NULL)
  248. return -ENOMEM;
  249. port = parport_find_number (minor);
  250. if (!port) {
  251. printk (KERN_WARNING "%s: no associated port!\n", name);
  252. kfree (name);
  253. return -ENXIO;
  254. }
  255. fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
  256. pdev = parport_register_device (port, name, NULL,
  257. NULL, pp_irq, fl, pp);
  258. parport_put_port (port);
  259. if (!pdev) {
  260. printk (KERN_WARNING "%s: failed to register device!\n", name);
  261. kfree (name);
  262. return -ENXIO;
  263. }
  264. pp->pdev = pdev;
  265. pr_debug("%s: registered pardevice\n", name);
  266. return 0;
  267. }
  268. static enum ieee1284_phase init_phase (int mode)
  269. {
  270. switch (mode & ~(IEEE1284_DEVICEID
  271. | IEEE1284_ADDR)) {
  272. case IEEE1284_MODE_NIBBLE:
  273. case IEEE1284_MODE_BYTE:
  274. return IEEE1284_PH_REV_IDLE;
  275. }
  276. return IEEE1284_PH_FWD_IDLE;
  277. }
  278. static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  279. {
  280. unsigned int minor = iminor(file_inode(file));
  281. struct pp_struct *pp = file->private_data;
  282. struct parport * port;
  283. void __user *argp = (void __user *)arg;
  284. /* First handle the cases that don't take arguments. */
  285. switch (cmd) {
  286. case PPCLAIM:
  287. {
  288. struct ieee1284_info *info;
  289. int ret;
  290. if (pp->flags & PP_CLAIMED) {
  291. pr_debug(CHRDEV "%x: you've already got it!\n", minor);
  292. return -EINVAL;
  293. }
  294. /* Deferred device registration. */
  295. if (!pp->pdev) {
  296. int err = register_device (minor, pp);
  297. if (err) {
  298. return err;
  299. }
  300. }
  301. ret = parport_claim_or_block (pp->pdev);
  302. if (ret < 0)
  303. return ret;
  304. pp->flags |= PP_CLAIMED;
  305. /* For interrupt-reporting to work, we need to be
  306. * informed of each interrupt. */
  307. pp_enable_irq (pp);
  308. /* We may need to fix up the state machine. */
  309. info = &pp->pdev->port->ieee1284;
  310. pp->saved_state.mode = info->mode;
  311. pp->saved_state.phase = info->phase;
  312. info->mode = pp->state.mode;
  313. info->phase = pp->state.phase;
  314. pp->default_inactivity = parport_set_timeout (pp->pdev, 0);
  315. parport_set_timeout (pp->pdev, pp->default_inactivity);
  316. return 0;
  317. }
  318. case PPEXCL:
  319. if (pp->pdev) {
  320. pr_debug(CHRDEV "%x: too late for PPEXCL; "
  321. "already registered\n", minor);
  322. if (pp->flags & PP_EXCL)
  323. /* But it's not really an error. */
  324. return 0;
  325. /* There's no chance of making the driver happy. */
  326. return -EINVAL;
  327. }
  328. /* Just remember to register the device exclusively
  329. * when we finally do the registration. */
  330. pp->flags |= PP_EXCL;
  331. return 0;
  332. case PPSETMODE:
  333. {
  334. int mode;
  335. if (copy_from_user (&mode, argp, sizeof (mode)))
  336. return -EFAULT;
  337. /* FIXME: validate mode */
  338. pp->state.mode = mode;
  339. pp->state.phase = init_phase (mode);
  340. if (pp->flags & PP_CLAIMED) {
  341. pp->pdev->port->ieee1284.mode = mode;
  342. pp->pdev->port->ieee1284.phase = pp->state.phase;
  343. }
  344. return 0;
  345. }
  346. case PPGETMODE:
  347. {
  348. int mode;
  349. if (pp->flags & PP_CLAIMED) {
  350. mode = pp->pdev->port->ieee1284.mode;
  351. } else {
  352. mode = pp->state.mode;
  353. }
  354. if (copy_to_user (argp, &mode, sizeof (mode))) {
  355. return -EFAULT;
  356. }
  357. return 0;
  358. }
  359. case PPSETPHASE:
  360. {
  361. int phase;
  362. if (copy_from_user (&phase, argp, sizeof (phase))) {
  363. return -EFAULT;
  364. }
  365. /* FIXME: validate phase */
  366. pp->state.phase = phase;
  367. if (pp->flags & PP_CLAIMED) {
  368. pp->pdev->port->ieee1284.phase = phase;
  369. }
  370. return 0;
  371. }
  372. case PPGETPHASE:
  373. {
  374. int phase;
  375. if (pp->flags & PP_CLAIMED) {
  376. phase = pp->pdev->port->ieee1284.phase;
  377. } else {
  378. phase = pp->state.phase;
  379. }
  380. if (copy_to_user (argp, &phase, sizeof (phase))) {
  381. return -EFAULT;
  382. }
  383. return 0;
  384. }
  385. case PPGETMODES:
  386. {
  387. unsigned int modes;
  388. port = parport_find_number (minor);
  389. if (!port)
  390. return -ENODEV;
  391. modes = port->modes;
  392. parport_put_port(port);
  393. if (copy_to_user (argp, &modes, sizeof (modes))) {
  394. return -EFAULT;
  395. }
  396. return 0;
  397. }
  398. case PPSETFLAGS:
  399. {
  400. int uflags;
  401. if (copy_from_user (&uflags, argp, sizeof (uflags))) {
  402. return -EFAULT;
  403. }
  404. pp->flags &= ~PP_FLAGMASK;
  405. pp->flags |= (uflags & PP_FLAGMASK);
  406. return 0;
  407. }
  408. case PPGETFLAGS:
  409. {
  410. int uflags;
  411. uflags = pp->flags & PP_FLAGMASK;
  412. if (copy_to_user (argp, &uflags, sizeof (uflags))) {
  413. return -EFAULT;
  414. }
  415. return 0;
  416. }
  417. } /* end switch() */
  418. /* Everything else requires the port to be claimed, so check
  419. * that now. */
  420. if ((pp->flags & PP_CLAIMED) == 0) {
  421. pr_debug(CHRDEV "%x: claim the port first\n", minor);
  422. return -EINVAL;
  423. }
  424. port = pp->pdev->port;
  425. switch (cmd) {
  426. struct ieee1284_info *info;
  427. unsigned char reg;
  428. unsigned char mask;
  429. int mode;
  430. int ret;
  431. struct timeval par_timeout;
  432. long to_jiffies;
  433. case PPRSTATUS:
  434. reg = parport_read_status (port);
  435. if (copy_to_user (argp, &reg, sizeof (reg)))
  436. return -EFAULT;
  437. return 0;
  438. case PPRDATA:
  439. reg = parport_read_data (port);
  440. if (copy_to_user (argp, &reg, sizeof (reg)))
  441. return -EFAULT;
  442. return 0;
  443. case PPRCONTROL:
  444. reg = parport_read_control (port);
  445. if (copy_to_user (argp, &reg, sizeof (reg)))
  446. return -EFAULT;
  447. return 0;
  448. case PPYIELD:
  449. parport_yield_blocking (pp->pdev);
  450. return 0;
  451. case PPRELEASE:
  452. /* Save the state machine's state. */
  453. info = &pp->pdev->port->ieee1284;
  454. pp->state.mode = info->mode;
  455. pp->state.phase = info->phase;
  456. info->mode = pp->saved_state.mode;
  457. info->phase = pp->saved_state.phase;
  458. parport_release (pp->pdev);
  459. pp->flags &= ~PP_CLAIMED;
  460. return 0;
  461. case PPWCONTROL:
  462. if (copy_from_user (&reg, argp, sizeof (reg)))
  463. return -EFAULT;
  464. parport_write_control (port, reg);
  465. return 0;
  466. case PPWDATA:
  467. if (copy_from_user (&reg, argp, sizeof (reg)))
  468. return -EFAULT;
  469. parport_write_data (port, reg);
  470. return 0;
  471. case PPFCONTROL:
  472. if (copy_from_user (&mask, argp,
  473. sizeof (mask)))
  474. return -EFAULT;
  475. if (copy_from_user (&reg, 1 + (unsigned char __user *) arg,
  476. sizeof (reg)))
  477. return -EFAULT;
  478. parport_frob_control (port, mask, reg);
  479. return 0;
  480. case PPDATADIR:
  481. if (copy_from_user (&mode, argp, sizeof (mode)))
  482. return -EFAULT;
  483. if (mode)
  484. port->ops->data_reverse (port);
  485. else
  486. port->ops->data_forward (port);
  487. return 0;
  488. case PPNEGOT:
  489. if (copy_from_user (&mode, argp, sizeof (mode)))
  490. return -EFAULT;
  491. switch ((ret = parport_negotiate (port, mode))) {
  492. case 0: break;
  493. case -1: /* handshake failed, peripheral not IEEE 1284 */
  494. ret = -EIO;
  495. break;
  496. case 1: /* handshake succeeded, peripheral rejected mode */
  497. ret = -ENXIO;
  498. break;
  499. }
  500. pp_enable_irq (pp);
  501. return ret;
  502. case PPWCTLONIRQ:
  503. if (copy_from_user (&reg, argp, sizeof (reg)))
  504. return -EFAULT;
  505. /* Remember what to set the control lines to, for next
  506. * time we get an interrupt. */
  507. pp->irqctl = reg;
  508. pp->irqresponse = 1;
  509. return 0;
  510. case PPCLRIRQ:
  511. ret = atomic_read (&pp->irqc);
  512. if (copy_to_user (argp, &ret, sizeof (ret)))
  513. return -EFAULT;
  514. atomic_sub (ret, &pp->irqc);
  515. return 0;
  516. case PPSETTIME:
  517. if (copy_from_user (&par_timeout, argp, sizeof(struct timeval))) {
  518. return -EFAULT;
  519. }
  520. /* Convert to jiffies, place in pp->pdev->timeout */
  521. if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
  522. return -EINVAL;
  523. }
  524. to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
  525. to_jiffies += par_timeout.tv_sec * (long)HZ;
  526. if (to_jiffies <= 0) {
  527. return -EINVAL;
  528. }
  529. pp->pdev->timeout = to_jiffies;
  530. return 0;
  531. case PPGETTIME:
  532. to_jiffies = pp->pdev->timeout;
  533. memset(&par_timeout, 0, sizeof(par_timeout));
  534. par_timeout.tv_sec = to_jiffies / HZ;
  535. par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
  536. if (copy_to_user (argp, &par_timeout, sizeof(struct timeval)))
  537. return -EFAULT;
  538. return 0;
  539. default:
  540. pr_debug(CHRDEV "%x: What? (cmd=0x%x)\n", minor, cmd);
  541. return -EINVAL;
  542. }
  543. /* Keep the compiler happy */
  544. return 0;
  545. }
  546. static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  547. {
  548. long ret;
  549. mutex_lock(&pp_do_mutex);
  550. ret = pp_do_ioctl(file, cmd, arg);
  551. mutex_unlock(&pp_do_mutex);
  552. return ret;
  553. }
  554. static int pp_open (struct inode * inode, struct file * file)
  555. {
  556. unsigned int minor = iminor(inode);
  557. struct pp_struct *pp;
  558. if (minor >= PARPORT_MAX)
  559. return -ENXIO;
  560. pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
  561. if (!pp)
  562. return -ENOMEM;
  563. pp->state.mode = IEEE1284_MODE_COMPAT;
  564. pp->state.phase = init_phase (pp->state.mode);
  565. pp->flags = 0;
  566. pp->irqresponse = 0;
  567. atomic_set (&pp->irqc, 0);
  568. init_waitqueue_head (&pp->irq_wait);
  569. /* Defer the actual device registration until the first claim.
  570. * That way, we know whether or not the driver wants to have
  571. * exclusive access to the port (PPEXCL).
  572. */
  573. pp->pdev = NULL;
  574. file->private_data = pp;
  575. return 0;
  576. }
  577. static int pp_release (struct inode * inode, struct file * file)
  578. {
  579. unsigned int minor = iminor(inode);
  580. struct pp_struct *pp = file->private_data;
  581. int compat_negot;
  582. compat_negot = 0;
  583. if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
  584. (pp->state.mode != IEEE1284_MODE_COMPAT)) {
  585. struct ieee1284_info *info;
  586. /* parport released, but not in compatibility mode */
  587. parport_claim_or_block (pp->pdev);
  588. pp->flags |= PP_CLAIMED;
  589. info = &pp->pdev->port->ieee1284;
  590. pp->saved_state.mode = info->mode;
  591. pp->saved_state.phase = info->phase;
  592. info->mode = pp->state.mode;
  593. info->phase = pp->state.phase;
  594. compat_negot = 1;
  595. } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
  596. (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
  597. compat_negot = 2;
  598. }
  599. if (compat_negot) {
  600. parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
  601. pr_debug(CHRDEV "%x: negotiated back to compatibility "
  602. "mode because user-space forgot\n", minor);
  603. }
  604. if (pp->flags & PP_CLAIMED) {
  605. struct ieee1284_info *info;
  606. info = &pp->pdev->port->ieee1284;
  607. pp->state.mode = info->mode;
  608. pp->state.phase = info->phase;
  609. info->mode = pp->saved_state.mode;
  610. info->phase = pp->saved_state.phase;
  611. parport_release (pp->pdev);
  612. if (compat_negot != 1) {
  613. pr_debug(CHRDEV "%x: released pardevice "
  614. "because user-space forgot\n", minor);
  615. }
  616. }
  617. if (pp->pdev) {
  618. const char *name = pp->pdev->name;
  619. parport_unregister_device (pp->pdev);
  620. kfree (name);
  621. pp->pdev = NULL;
  622. pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
  623. }
  624. kfree (pp);
  625. return 0;
  626. }
  627. /* No kernel lock held - fine */
  628. static unsigned int pp_poll (struct file * file, poll_table * wait)
  629. {
  630. struct pp_struct *pp = file->private_data;
  631. unsigned int mask = 0;
  632. poll_wait (file, &pp->irq_wait, wait);
  633. if (atomic_read (&pp->irqc))
  634. mask |= POLLIN | POLLRDNORM;
  635. return mask;
  636. }
  637. static struct class *ppdev_class;
  638. static const struct file_operations pp_fops = {
  639. .owner = THIS_MODULE,
  640. .llseek = no_llseek,
  641. .read = pp_read,
  642. .write = pp_write,
  643. .poll = pp_poll,
  644. .unlocked_ioctl = pp_ioctl,
  645. .open = pp_open,
  646. .release = pp_release,
  647. };
  648. static void pp_attach(struct parport *port)
  649. {
  650. device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
  651. NULL, "parport%d", port->number);
  652. }
  653. static void pp_detach(struct parport *port)
  654. {
  655. device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
  656. }
  657. static struct parport_driver pp_driver = {
  658. .name = CHRDEV,
  659. .attach = pp_attach,
  660. .detach = pp_detach,
  661. };
  662. static int __init ppdev_init (void)
  663. {
  664. int err = 0;
  665. if (register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
  666. printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
  667. PP_MAJOR);
  668. return -EIO;
  669. }
  670. ppdev_class = class_create(THIS_MODULE, CHRDEV);
  671. if (IS_ERR(ppdev_class)) {
  672. err = PTR_ERR(ppdev_class);
  673. goto out_chrdev;
  674. }
  675. err = parport_register_driver(&pp_driver);
  676. if (err < 0) {
  677. printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
  678. goto out_class;
  679. }
  680. printk (KERN_INFO PP_VERSION "\n");
  681. goto out;
  682. out_class:
  683. class_destroy(ppdev_class);
  684. out_chrdev:
  685. unregister_chrdev(PP_MAJOR, CHRDEV);
  686. out:
  687. return err;
  688. }
  689. static void __exit ppdev_cleanup (void)
  690. {
  691. /* Clean up all parport stuff */
  692. parport_unregister_driver(&pp_driver);
  693. class_destroy(ppdev_class);
  694. unregister_chrdev (PP_MAJOR, CHRDEV);
  695. }
  696. module_init(ppdev_init);
  697. module_exit(ppdev_cleanup);
  698. MODULE_LICENSE("GPL");
  699. MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);