ppdev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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->f_path.dentry->d_inode);
  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->f_path.dentry->d_inode);
  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. if (!bytes_written) {
  223. bytes_written = -EINTR;
  224. }
  225. break;
  226. }
  227. cond_resched();
  228. }
  229. parport_set_timeout (pp->pdev, pp->default_inactivity);
  230. kfree (kbuffer);
  231. pp_enable_irq (pp);
  232. return bytes_written;
  233. }
  234. static void pp_irq (void *private)
  235. {
  236. struct pp_struct *pp = private;
  237. if (pp->irqresponse) {
  238. parport_write_control (pp->pdev->port, pp->irqctl);
  239. pp->irqresponse = 0;
  240. }
  241. atomic_inc (&pp->irqc);
  242. wake_up_interruptible (&pp->irq_wait);
  243. }
  244. static int register_device (int minor, struct pp_struct *pp)
  245. {
  246. struct parport *port;
  247. struct pardevice * pdev = NULL;
  248. char *name;
  249. int fl;
  250. name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
  251. if (name == NULL)
  252. return -ENOMEM;
  253. port = parport_find_number (minor);
  254. if (!port) {
  255. printk (KERN_WARNING "%s: no associated port!\n", name);
  256. kfree (name);
  257. return -ENXIO;
  258. }
  259. fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
  260. pdev = parport_register_device (port, name, NULL,
  261. NULL, pp_irq, fl, pp);
  262. parport_put_port (port);
  263. if (!pdev) {
  264. printk (KERN_WARNING "%s: failed to register device!\n", name);
  265. kfree (name);
  266. return -ENXIO;
  267. }
  268. pp->pdev = pdev;
  269. pr_debug("%s: registered pardevice\n", name);
  270. return 0;
  271. }
  272. static enum ieee1284_phase init_phase (int mode)
  273. {
  274. switch (mode & ~(IEEE1284_DEVICEID
  275. | IEEE1284_ADDR)) {
  276. case IEEE1284_MODE_NIBBLE:
  277. case IEEE1284_MODE_BYTE:
  278. return IEEE1284_PH_REV_IDLE;
  279. }
  280. return IEEE1284_PH_FWD_IDLE;
  281. }
  282. static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  283. {
  284. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  285. struct pp_struct *pp = file->private_data;
  286. struct parport * port;
  287. void __user *argp = (void __user *)arg;
  288. /* First handle the cases that don't take arguments. */
  289. switch (cmd) {
  290. case PPCLAIM:
  291. {
  292. struct ieee1284_info *info;
  293. int ret;
  294. if (pp->flags & PP_CLAIMED) {
  295. pr_debug(CHRDEV "%x: you've already got it!\n", minor);
  296. return -EINVAL;
  297. }
  298. /* Deferred device registration. */
  299. if (!pp->pdev) {
  300. int err = register_device (minor, pp);
  301. if (err) {
  302. return err;
  303. }
  304. }
  305. ret = parport_claim_or_block (pp->pdev);
  306. if (ret < 0)
  307. return ret;
  308. pp->flags |= PP_CLAIMED;
  309. /* For interrupt-reporting to work, we need to be
  310. * informed of each interrupt. */
  311. pp_enable_irq (pp);
  312. /* We may need to fix up the state machine. */
  313. info = &pp->pdev->port->ieee1284;
  314. pp->saved_state.mode = info->mode;
  315. pp->saved_state.phase = info->phase;
  316. info->mode = pp->state.mode;
  317. info->phase = pp->state.phase;
  318. pp->default_inactivity = parport_set_timeout (pp->pdev, 0);
  319. parport_set_timeout (pp->pdev, pp->default_inactivity);
  320. return 0;
  321. }
  322. case PPEXCL:
  323. if (pp->pdev) {
  324. pr_debug(CHRDEV "%x: too late for PPEXCL; "
  325. "already registered\n", minor);
  326. if (pp->flags & PP_EXCL)
  327. /* But it's not really an error. */
  328. return 0;
  329. /* There's no chance of making the driver happy. */
  330. return -EINVAL;
  331. }
  332. /* Just remember to register the device exclusively
  333. * when we finally do the registration. */
  334. pp->flags |= PP_EXCL;
  335. return 0;
  336. case PPSETMODE:
  337. {
  338. int mode;
  339. if (copy_from_user (&mode, argp, sizeof (mode)))
  340. return -EFAULT;
  341. /* FIXME: validate mode */
  342. pp->state.mode = mode;
  343. pp->state.phase = init_phase (mode);
  344. if (pp->flags & PP_CLAIMED) {
  345. pp->pdev->port->ieee1284.mode = mode;
  346. pp->pdev->port->ieee1284.phase = pp->state.phase;
  347. }
  348. return 0;
  349. }
  350. case PPGETMODE:
  351. {
  352. int mode;
  353. if (pp->flags & PP_CLAIMED) {
  354. mode = pp->pdev->port->ieee1284.mode;
  355. } else {
  356. mode = pp->state.mode;
  357. }
  358. if (copy_to_user (argp, &mode, sizeof (mode))) {
  359. return -EFAULT;
  360. }
  361. return 0;
  362. }
  363. case PPSETPHASE:
  364. {
  365. int phase;
  366. if (copy_from_user (&phase, argp, sizeof (phase))) {
  367. return -EFAULT;
  368. }
  369. /* FIXME: validate phase */
  370. pp->state.phase = phase;
  371. if (pp->flags & PP_CLAIMED) {
  372. pp->pdev->port->ieee1284.phase = phase;
  373. }
  374. return 0;
  375. }
  376. case PPGETPHASE:
  377. {
  378. int phase;
  379. if (pp->flags & PP_CLAIMED) {
  380. phase = pp->pdev->port->ieee1284.phase;
  381. } else {
  382. phase = pp->state.phase;
  383. }
  384. if (copy_to_user (argp, &phase, sizeof (phase))) {
  385. return -EFAULT;
  386. }
  387. return 0;
  388. }
  389. case PPGETMODES:
  390. {
  391. unsigned int modes;
  392. port = parport_find_number (minor);
  393. if (!port)
  394. return -ENODEV;
  395. modes = port->modes;
  396. parport_put_port(port);
  397. if (copy_to_user (argp, &modes, sizeof (modes))) {
  398. return -EFAULT;
  399. }
  400. return 0;
  401. }
  402. case PPSETFLAGS:
  403. {
  404. int uflags;
  405. if (copy_from_user (&uflags, argp, sizeof (uflags))) {
  406. return -EFAULT;
  407. }
  408. pp->flags &= ~PP_FLAGMASK;
  409. pp->flags |= (uflags & PP_FLAGMASK);
  410. return 0;
  411. }
  412. case PPGETFLAGS:
  413. {
  414. int uflags;
  415. uflags = pp->flags & PP_FLAGMASK;
  416. if (copy_to_user (argp, &uflags, sizeof (uflags))) {
  417. return -EFAULT;
  418. }
  419. return 0;
  420. }
  421. } /* end switch() */
  422. /* Everything else requires the port to be claimed, so check
  423. * that now. */
  424. if ((pp->flags & PP_CLAIMED) == 0) {
  425. pr_debug(CHRDEV "%x: claim the port first\n", minor);
  426. return -EINVAL;
  427. }
  428. port = pp->pdev->port;
  429. switch (cmd) {
  430. struct ieee1284_info *info;
  431. unsigned char reg;
  432. unsigned char mask;
  433. int mode;
  434. int ret;
  435. struct timeval par_timeout;
  436. long to_jiffies;
  437. case PPRSTATUS:
  438. reg = parport_read_status (port);
  439. if (copy_to_user (argp, &reg, sizeof (reg)))
  440. return -EFAULT;
  441. return 0;
  442. case PPRDATA:
  443. reg = parport_read_data (port);
  444. if (copy_to_user (argp, &reg, sizeof (reg)))
  445. return -EFAULT;
  446. return 0;
  447. case PPRCONTROL:
  448. reg = parport_read_control (port);
  449. if (copy_to_user (argp, &reg, sizeof (reg)))
  450. return -EFAULT;
  451. return 0;
  452. case PPYIELD:
  453. parport_yield_blocking (pp->pdev);
  454. return 0;
  455. case PPRELEASE:
  456. /* Save the state machine's state. */
  457. info = &pp->pdev->port->ieee1284;
  458. pp->state.mode = info->mode;
  459. pp->state.phase = info->phase;
  460. info->mode = pp->saved_state.mode;
  461. info->phase = pp->saved_state.phase;
  462. parport_release (pp->pdev);
  463. pp->flags &= ~PP_CLAIMED;
  464. return 0;
  465. case PPWCONTROL:
  466. if (copy_from_user (&reg, argp, sizeof (reg)))
  467. return -EFAULT;
  468. parport_write_control (port, reg);
  469. return 0;
  470. case PPWDATA:
  471. if (copy_from_user (&reg, argp, sizeof (reg)))
  472. return -EFAULT;
  473. parport_write_data (port, reg);
  474. return 0;
  475. case PPFCONTROL:
  476. if (copy_from_user (&mask, argp,
  477. sizeof (mask)))
  478. return -EFAULT;
  479. if (copy_from_user (&reg, 1 + (unsigned char __user *) arg,
  480. sizeof (reg)))
  481. return -EFAULT;
  482. parport_frob_control (port, mask, reg);
  483. return 0;
  484. case PPDATADIR:
  485. if (copy_from_user (&mode, argp, sizeof (mode)))
  486. return -EFAULT;
  487. if (mode)
  488. port->ops->data_reverse (port);
  489. else
  490. port->ops->data_forward (port);
  491. return 0;
  492. case PPNEGOT:
  493. if (copy_from_user (&mode, argp, sizeof (mode)))
  494. return -EFAULT;
  495. switch ((ret = parport_negotiate (port, mode))) {
  496. case 0: break;
  497. case -1: /* handshake failed, peripheral not IEEE 1284 */
  498. ret = -EIO;
  499. break;
  500. case 1: /* handshake succeeded, peripheral rejected mode */
  501. ret = -ENXIO;
  502. break;
  503. }
  504. pp_enable_irq (pp);
  505. return ret;
  506. case PPWCTLONIRQ:
  507. if (copy_from_user (&reg, argp, sizeof (reg)))
  508. return -EFAULT;
  509. /* Remember what to set the control lines to, for next
  510. * time we get an interrupt. */
  511. pp->irqctl = reg;
  512. pp->irqresponse = 1;
  513. return 0;
  514. case PPCLRIRQ:
  515. ret = atomic_read (&pp->irqc);
  516. if (copy_to_user (argp, &ret, sizeof (ret)))
  517. return -EFAULT;
  518. atomic_sub (ret, &pp->irqc);
  519. return 0;
  520. case PPSETTIME:
  521. if (copy_from_user (&par_timeout, argp, sizeof(struct timeval))) {
  522. return -EFAULT;
  523. }
  524. /* Convert to jiffies, place in pp->pdev->timeout */
  525. if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
  526. return -EINVAL;
  527. }
  528. to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
  529. to_jiffies += par_timeout.tv_sec * (long)HZ;
  530. if (to_jiffies <= 0) {
  531. return -EINVAL;
  532. }
  533. pp->pdev->timeout = to_jiffies;
  534. return 0;
  535. case PPGETTIME:
  536. to_jiffies = pp->pdev->timeout;
  537. memset(&par_timeout, 0, sizeof(par_timeout));
  538. par_timeout.tv_sec = to_jiffies / HZ;
  539. par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
  540. if (copy_to_user (argp, &par_timeout, sizeof(struct timeval)))
  541. return -EFAULT;
  542. return 0;
  543. default:
  544. pr_debug(CHRDEV "%x: What? (cmd=0x%x)\n", minor, cmd);
  545. return -EINVAL;
  546. }
  547. /* Keep the compiler happy */
  548. return 0;
  549. }
  550. static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  551. {
  552. long ret;
  553. mutex_lock(&pp_do_mutex);
  554. ret = pp_do_ioctl(file, cmd, arg);
  555. mutex_unlock(&pp_do_mutex);
  556. return ret;
  557. }
  558. static int pp_open (struct inode * inode, struct file * file)
  559. {
  560. unsigned int minor = iminor(inode);
  561. struct pp_struct *pp;
  562. if (minor >= PARPORT_MAX)
  563. return -ENXIO;
  564. pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
  565. if (!pp)
  566. return -ENOMEM;
  567. pp->state.mode = IEEE1284_MODE_COMPAT;
  568. pp->state.phase = init_phase (pp->state.mode);
  569. pp->flags = 0;
  570. pp->irqresponse = 0;
  571. atomic_set (&pp->irqc, 0);
  572. init_waitqueue_head (&pp->irq_wait);
  573. /* Defer the actual device registration until the first claim.
  574. * That way, we know whether or not the driver wants to have
  575. * exclusive access to the port (PPEXCL).
  576. */
  577. pp->pdev = NULL;
  578. file->private_data = pp;
  579. return 0;
  580. }
  581. static int pp_release (struct inode * inode, struct file * file)
  582. {
  583. unsigned int minor = iminor(inode);
  584. struct pp_struct *pp = file->private_data;
  585. int compat_negot;
  586. compat_negot = 0;
  587. if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
  588. (pp->state.mode != IEEE1284_MODE_COMPAT)) {
  589. struct ieee1284_info *info;
  590. /* parport released, but not in compatibility mode */
  591. parport_claim_or_block (pp->pdev);
  592. pp->flags |= PP_CLAIMED;
  593. info = &pp->pdev->port->ieee1284;
  594. pp->saved_state.mode = info->mode;
  595. pp->saved_state.phase = info->phase;
  596. info->mode = pp->state.mode;
  597. info->phase = pp->state.phase;
  598. compat_negot = 1;
  599. } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
  600. (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
  601. compat_negot = 2;
  602. }
  603. if (compat_negot) {
  604. parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
  605. pr_debug(CHRDEV "%x: negotiated back to compatibility "
  606. "mode because user-space forgot\n", minor);
  607. }
  608. if (pp->flags & PP_CLAIMED) {
  609. struct ieee1284_info *info;
  610. info = &pp->pdev->port->ieee1284;
  611. pp->state.mode = info->mode;
  612. pp->state.phase = info->phase;
  613. info->mode = pp->saved_state.mode;
  614. info->phase = pp->saved_state.phase;
  615. parport_release (pp->pdev);
  616. if (compat_negot != 1) {
  617. pr_debug(CHRDEV "%x: released pardevice "
  618. "because user-space forgot\n", minor);
  619. }
  620. }
  621. if (pp->pdev) {
  622. const char *name = pp->pdev->name;
  623. parport_unregister_device (pp->pdev);
  624. kfree (name);
  625. pp->pdev = NULL;
  626. pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
  627. }
  628. kfree (pp);
  629. return 0;
  630. }
  631. /* No kernel lock held - fine */
  632. static unsigned int pp_poll (struct file * file, poll_table * wait)
  633. {
  634. struct pp_struct *pp = file->private_data;
  635. unsigned int mask = 0;
  636. poll_wait (file, &pp->irq_wait, wait);
  637. if (atomic_read (&pp->irqc))
  638. mask |= POLLIN | POLLRDNORM;
  639. return mask;
  640. }
  641. static struct class *ppdev_class;
  642. static const struct file_operations pp_fops = {
  643. .owner = THIS_MODULE,
  644. .llseek = no_llseek,
  645. .read = pp_read,
  646. .write = pp_write,
  647. .poll = pp_poll,
  648. .unlocked_ioctl = pp_ioctl,
  649. .open = pp_open,
  650. .release = pp_release,
  651. };
  652. static void pp_attach(struct parport *port)
  653. {
  654. device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
  655. NULL, "parport%d", port->number);
  656. }
  657. static void pp_detach(struct parport *port)
  658. {
  659. device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
  660. }
  661. static struct parport_driver pp_driver = {
  662. .name = CHRDEV,
  663. .attach = pp_attach,
  664. .detach = pp_detach,
  665. };
  666. static int __init ppdev_init (void)
  667. {
  668. int err = 0;
  669. if (register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
  670. printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
  671. PP_MAJOR);
  672. return -EIO;
  673. }
  674. ppdev_class = class_create(THIS_MODULE, CHRDEV);
  675. if (IS_ERR(ppdev_class)) {
  676. err = PTR_ERR(ppdev_class);
  677. goto out_chrdev;
  678. }
  679. if (parport_register_driver(&pp_driver)) {
  680. printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
  681. goto out_class;
  682. }
  683. printk (KERN_INFO PP_VERSION "\n");
  684. goto out;
  685. out_class:
  686. class_destroy(ppdev_class);
  687. out_chrdev:
  688. unregister_chrdev(PP_MAJOR, CHRDEV);
  689. out:
  690. return err;
  691. }
  692. static void __exit ppdev_cleanup (void)
  693. {
  694. /* Clean up all parport stuff */
  695. parport_unregister_driver(&pp_driver);
  696. class_destroy(ppdev_class);
  697. unregister_chrdev (PP_MAJOR, CHRDEV);
  698. }
  699. module_init(ppdev_init);
  700. module_exit(ppdev_cleanup);
  701. MODULE_LICENSE("GPL");
  702. MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);