ppdev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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/major.h>
  67. #include <linux/ppdev.h>
  68. #include <linux/smp_lock.h>
  69. #include <linux/uaccess.h>
  70. #define PP_VERSION "ppdev: user-space parallel port driver"
  71. #define CHRDEV "ppdev"
  72. struct pp_struct {
  73. struct pardevice * pdev;
  74. wait_queue_head_t irq_wait;
  75. atomic_t irqc;
  76. unsigned int flags;
  77. int irqresponse;
  78. unsigned char irqctl;
  79. struct ieee1284_info state;
  80. struct ieee1284_info saved_state;
  81. long default_inactivity;
  82. };
  83. /* pp_struct.flags bitfields */
  84. #define PP_CLAIMED (1<<0)
  85. #define PP_EXCL (1<<1)
  86. /* Other constants */
  87. #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
  88. #define PP_BUFFER_SIZE 1024
  89. #define PARDEVICE_MAX 8
  90. /* ROUND_UP macro from fs/select.c */
  91. #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
  92. static inline void pp_enable_irq (struct pp_struct *pp)
  93. {
  94. struct parport *port = pp->pdev->port;
  95. port->ops->enable_irq (port);
  96. }
  97. static ssize_t pp_read (struct file * file, char __user * buf, size_t count,
  98. loff_t * ppos)
  99. {
  100. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  101. struct pp_struct *pp = file->private_data;
  102. char * kbuffer;
  103. ssize_t bytes_read = 0;
  104. struct parport *pport;
  105. int mode;
  106. if (!(pp->flags & PP_CLAIMED)) {
  107. /* Don't have the port claimed */
  108. printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
  109. minor);
  110. return -EINVAL;
  111. }
  112. /* Trivial case. */
  113. if (count == 0)
  114. return 0;
  115. kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
  116. if (!kbuffer) {
  117. return -ENOMEM;
  118. }
  119. pport = pp->pdev->port;
  120. mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  121. parport_set_timeout (pp->pdev,
  122. (file->f_flags & O_NONBLOCK) ?
  123. PARPORT_INACTIVITY_O_NONBLOCK :
  124. pp->default_inactivity);
  125. while (bytes_read == 0) {
  126. ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
  127. if (mode == IEEE1284_MODE_EPP) {
  128. /* various specials for EPP mode */
  129. int flags = 0;
  130. size_t (*fn)(struct parport *, void *, size_t, int);
  131. if (pp->flags & PP_W91284PIC) {
  132. flags |= PARPORT_W91284PIC;
  133. }
  134. if (pp->flags & PP_FASTREAD) {
  135. flags |= PARPORT_EPP_FAST;
  136. }
  137. if (pport->ieee1284.mode & IEEE1284_ADDR) {
  138. fn = pport->ops->epp_read_addr;
  139. } else {
  140. fn = pport->ops->epp_read_data;
  141. }
  142. bytes_read = (*fn)(pport, kbuffer, need, flags);
  143. } else {
  144. bytes_read = parport_read (pport, kbuffer, need);
  145. }
  146. if (bytes_read != 0)
  147. break;
  148. if (file->f_flags & O_NONBLOCK) {
  149. bytes_read = -EAGAIN;
  150. break;
  151. }
  152. if (signal_pending (current)) {
  153. bytes_read = -ERESTARTSYS;
  154. break;
  155. }
  156. cond_resched();
  157. }
  158. parport_set_timeout (pp->pdev, pp->default_inactivity);
  159. if (bytes_read > 0 && copy_to_user (buf, kbuffer, bytes_read))
  160. bytes_read = -EFAULT;
  161. kfree (kbuffer);
  162. pp_enable_irq (pp);
  163. return bytes_read;
  164. }
  165. static ssize_t pp_write (struct file * file, const char __user * buf,
  166. size_t count, loff_t * ppos)
  167. {
  168. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  169. struct pp_struct *pp = file->private_data;
  170. char * kbuffer;
  171. ssize_t bytes_written = 0;
  172. ssize_t wrote;
  173. int mode;
  174. struct parport *pport;
  175. if (!(pp->flags & PP_CLAIMED)) {
  176. /* Don't have the port claimed */
  177. printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
  178. 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 = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
  251. if (name == NULL)
  252. return -ENOMEM;
  253. sprintf (name, CHRDEV "%x", minor);
  254. port = parport_find_number (minor);
  255. if (!port) {
  256. printk (KERN_WARNING "%s: no associated port!\n", name);
  257. kfree (name);
  258. return -ENXIO;
  259. }
  260. fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
  261. pdev = parport_register_device (port, name, NULL,
  262. NULL, pp_irq, fl, pp);
  263. parport_put_port (port);
  264. if (!pdev) {
  265. printk (KERN_WARNING "%s: failed to register device!\n", name);
  266. kfree (name);
  267. return -ENXIO;
  268. }
  269. pp->pdev = pdev;
  270. printk (KERN_DEBUG "%s: registered pardevice\n", name);
  271. return 0;
  272. }
  273. static enum ieee1284_phase init_phase (int mode)
  274. {
  275. switch (mode & ~(IEEE1284_DEVICEID
  276. | IEEE1284_ADDR)) {
  277. case IEEE1284_MODE_NIBBLE:
  278. case IEEE1284_MODE_BYTE:
  279. return IEEE1284_PH_REV_IDLE;
  280. }
  281. return IEEE1284_PH_FWD_IDLE;
  282. }
  283. static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  284. {
  285. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  286. struct pp_struct *pp = file->private_data;
  287. struct parport * port;
  288. void __user *argp = (void __user *)arg;
  289. /* First handle the cases that don't take arguments. */
  290. switch (cmd) {
  291. case PPCLAIM:
  292. {
  293. struct ieee1284_info *info;
  294. int ret;
  295. if (pp->flags & PP_CLAIMED) {
  296. printk (KERN_DEBUG CHRDEV
  297. "%x: you've already got it!\n", minor);
  298. return -EINVAL;
  299. }
  300. /* Deferred device registration. */
  301. if (!pp->pdev) {
  302. int err = register_device (minor, pp);
  303. if (err) {
  304. return err;
  305. }
  306. }
  307. ret = parport_claim_or_block (pp->pdev);
  308. if (ret < 0)
  309. return ret;
  310. pp->flags |= PP_CLAIMED;
  311. /* For interrupt-reporting to work, we need to be
  312. * informed of each interrupt. */
  313. pp_enable_irq (pp);
  314. /* We may need to fix up the state machine. */
  315. info = &pp->pdev->port->ieee1284;
  316. pp->saved_state.mode = info->mode;
  317. pp->saved_state.phase = info->phase;
  318. info->mode = pp->state.mode;
  319. info->phase = pp->state.phase;
  320. pp->default_inactivity = parport_set_timeout (pp->pdev, 0);
  321. parport_set_timeout (pp->pdev, pp->default_inactivity);
  322. return 0;
  323. }
  324. case PPEXCL:
  325. if (pp->pdev) {
  326. printk (KERN_DEBUG CHRDEV "%x: too late for PPEXCL; "
  327. "already registered\n", minor);
  328. if (pp->flags & PP_EXCL)
  329. /* But it's not really an error. */
  330. return 0;
  331. /* There's no chance of making the driver happy. */
  332. return -EINVAL;
  333. }
  334. /* Just remember to register the device exclusively
  335. * when we finally do the registration. */
  336. pp->flags |= PP_EXCL;
  337. return 0;
  338. case PPSETMODE:
  339. {
  340. int mode;
  341. if (copy_from_user (&mode, argp, sizeof (mode)))
  342. return -EFAULT;
  343. /* FIXME: validate mode */
  344. pp->state.mode = mode;
  345. pp->state.phase = init_phase (mode);
  346. if (pp->flags & PP_CLAIMED) {
  347. pp->pdev->port->ieee1284.mode = mode;
  348. pp->pdev->port->ieee1284.phase = pp->state.phase;
  349. }
  350. return 0;
  351. }
  352. case PPGETMODE:
  353. {
  354. int mode;
  355. if (pp->flags & PP_CLAIMED) {
  356. mode = pp->pdev->port->ieee1284.mode;
  357. } else {
  358. mode = pp->state.mode;
  359. }
  360. if (copy_to_user (argp, &mode, sizeof (mode))) {
  361. return -EFAULT;
  362. }
  363. return 0;
  364. }
  365. case PPSETPHASE:
  366. {
  367. int phase;
  368. if (copy_from_user (&phase, argp, sizeof (phase))) {
  369. return -EFAULT;
  370. }
  371. /* FIXME: validate phase */
  372. pp->state.phase = phase;
  373. if (pp->flags & PP_CLAIMED) {
  374. pp->pdev->port->ieee1284.phase = phase;
  375. }
  376. return 0;
  377. }
  378. case PPGETPHASE:
  379. {
  380. int phase;
  381. if (pp->flags & PP_CLAIMED) {
  382. phase = pp->pdev->port->ieee1284.phase;
  383. } else {
  384. phase = pp->state.phase;
  385. }
  386. if (copy_to_user (argp, &phase, sizeof (phase))) {
  387. return -EFAULT;
  388. }
  389. return 0;
  390. }
  391. case PPGETMODES:
  392. {
  393. unsigned int modes;
  394. port = parport_find_number (minor);
  395. if (!port)
  396. return -ENODEV;
  397. modes = port->modes;
  398. if (copy_to_user (argp, &modes, sizeof (modes))) {
  399. return -EFAULT;
  400. }
  401. return 0;
  402. }
  403. case PPSETFLAGS:
  404. {
  405. int uflags;
  406. if (copy_from_user (&uflags, argp, sizeof (uflags))) {
  407. return -EFAULT;
  408. }
  409. pp->flags &= ~PP_FLAGMASK;
  410. pp->flags |= (uflags & PP_FLAGMASK);
  411. return 0;
  412. }
  413. case PPGETFLAGS:
  414. {
  415. int uflags;
  416. uflags = pp->flags & PP_FLAGMASK;
  417. if (copy_to_user (argp, &uflags, sizeof (uflags))) {
  418. return -EFAULT;
  419. }
  420. return 0;
  421. }
  422. } /* end switch() */
  423. /* Everything else requires the port to be claimed, so check
  424. * that now. */
  425. if ((pp->flags & PP_CLAIMED) == 0) {
  426. printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
  427. minor);
  428. return -EINVAL;
  429. }
  430. port = pp->pdev->port;
  431. switch (cmd) {
  432. struct ieee1284_info *info;
  433. unsigned char reg;
  434. unsigned char mask;
  435. int mode;
  436. int ret;
  437. struct timeval par_timeout;
  438. long to_jiffies;
  439. case PPRSTATUS:
  440. reg = parport_read_status (port);
  441. if (copy_to_user (argp, &reg, sizeof (reg)))
  442. return -EFAULT;
  443. return 0;
  444. case PPRDATA:
  445. reg = parport_read_data (port);
  446. if (copy_to_user (argp, &reg, sizeof (reg)))
  447. return -EFAULT;
  448. return 0;
  449. case PPRCONTROL:
  450. reg = parport_read_control (port);
  451. if (copy_to_user (argp, &reg, sizeof (reg)))
  452. return -EFAULT;
  453. return 0;
  454. case PPYIELD:
  455. parport_yield_blocking (pp->pdev);
  456. return 0;
  457. case PPRELEASE:
  458. /* Save the state machine's state. */
  459. info = &pp->pdev->port->ieee1284;
  460. pp->state.mode = info->mode;
  461. pp->state.phase = info->phase;
  462. info->mode = pp->saved_state.mode;
  463. info->phase = pp->saved_state.phase;
  464. parport_release (pp->pdev);
  465. pp->flags &= ~PP_CLAIMED;
  466. return 0;
  467. case PPWCONTROL:
  468. if (copy_from_user (&reg, argp, sizeof (reg)))
  469. return -EFAULT;
  470. parport_write_control (port, reg);
  471. return 0;
  472. case PPWDATA:
  473. if (copy_from_user (&reg, argp, sizeof (reg)))
  474. return -EFAULT;
  475. parport_write_data (port, reg);
  476. return 0;
  477. case PPFCONTROL:
  478. if (copy_from_user (&mask, argp,
  479. sizeof (mask)))
  480. return -EFAULT;
  481. if (copy_from_user (&reg, 1 + (unsigned char __user *) arg,
  482. sizeof (reg)))
  483. return -EFAULT;
  484. parport_frob_control (port, mask, reg);
  485. return 0;
  486. case PPDATADIR:
  487. if (copy_from_user (&mode, argp, sizeof (mode)))
  488. return -EFAULT;
  489. if (mode)
  490. port->ops->data_reverse (port);
  491. else
  492. port->ops->data_forward (port);
  493. return 0;
  494. case PPNEGOT:
  495. if (copy_from_user (&mode, argp, sizeof (mode)))
  496. return -EFAULT;
  497. switch ((ret = parport_negotiate (port, mode))) {
  498. case 0: break;
  499. case -1: /* handshake failed, peripheral not IEEE 1284 */
  500. ret = -EIO;
  501. break;
  502. case 1: /* handshake succeeded, peripheral rejected mode */
  503. ret = -ENXIO;
  504. break;
  505. }
  506. pp_enable_irq (pp);
  507. return ret;
  508. case PPWCTLONIRQ:
  509. if (copy_from_user (&reg, argp, sizeof (reg)))
  510. return -EFAULT;
  511. /* Remember what to set the control lines to, for next
  512. * time we get an interrupt. */
  513. pp->irqctl = reg;
  514. pp->irqresponse = 1;
  515. return 0;
  516. case PPCLRIRQ:
  517. ret = atomic_read (&pp->irqc);
  518. if (copy_to_user (argp, &ret, sizeof (ret)))
  519. return -EFAULT;
  520. atomic_sub (ret, &pp->irqc);
  521. return 0;
  522. case PPSETTIME:
  523. if (copy_from_user (&par_timeout, argp, sizeof(struct timeval))) {
  524. return -EFAULT;
  525. }
  526. /* Convert to jiffies, place in pp->pdev->timeout */
  527. if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
  528. return -EINVAL;
  529. }
  530. to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
  531. to_jiffies += par_timeout.tv_sec * (long)HZ;
  532. if (to_jiffies <= 0) {
  533. return -EINVAL;
  534. }
  535. pp->pdev->timeout = to_jiffies;
  536. return 0;
  537. case PPGETTIME:
  538. to_jiffies = pp->pdev->timeout;
  539. par_timeout.tv_sec = to_jiffies / HZ;
  540. par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
  541. if (copy_to_user (argp, &par_timeout, sizeof(struct timeval)))
  542. return -EFAULT;
  543. return 0;
  544. default:
  545. printk (KERN_DEBUG CHRDEV "%x: What? (cmd=0x%x)\n", minor,
  546. cmd);
  547. return -EINVAL;
  548. }
  549. /* Keep the compiler happy */
  550. return 0;
  551. }
  552. static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  553. {
  554. long ret;
  555. lock_kernel();
  556. ret = pp_do_ioctl(file, cmd, arg);
  557. unlock_kernel();
  558. return ret;
  559. }
  560. static int pp_open (struct inode * inode, struct file * file)
  561. {
  562. unsigned int minor = iminor(inode);
  563. struct pp_struct *pp;
  564. cycle_kernel_lock();
  565. if (minor >= PARPORT_MAX)
  566. return -ENXIO;
  567. pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
  568. if (!pp)
  569. return -ENOMEM;
  570. pp->state.mode = IEEE1284_MODE_COMPAT;
  571. pp->state.phase = init_phase (pp->state.mode);
  572. pp->flags = 0;
  573. pp->irqresponse = 0;
  574. atomic_set (&pp->irqc, 0);
  575. init_waitqueue_head (&pp->irq_wait);
  576. /* Defer the actual device registration until the first claim.
  577. * That way, we know whether or not the driver wants to have
  578. * exclusive access to the port (PPEXCL).
  579. */
  580. pp->pdev = NULL;
  581. file->private_data = pp;
  582. return 0;
  583. }
  584. static int pp_release (struct inode * inode, struct file * file)
  585. {
  586. unsigned int minor = iminor(inode);
  587. struct pp_struct *pp = file->private_data;
  588. int compat_negot;
  589. compat_negot = 0;
  590. if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
  591. (pp->state.mode != IEEE1284_MODE_COMPAT)) {
  592. struct ieee1284_info *info;
  593. /* parport released, but not in compatibility mode */
  594. parport_claim_or_block (pp->pdev);
  595. pp->flags |= PP_CLAIMED;
  596. info = &pp->pdev->port->ieee1284;
  597. pp->saved_state.mode = info->mode;
  598. pp->saved_state.phase = info->phase;
  599. info->mode = pp->state.mode;
  600. info->phase = pp->state.phase;
  601. compat_negot = 1;
  602. } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
  603. (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
  604. compat_negot = 2;
  605. }
  606. if (compat_negot) {
  607. parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
  608. printk (KERN_DEBUG CHRDEV
  609. "%x: negotiated back to compatibility mode because "
  610. "user-space forgot\n", minor);
  611. }
  612. if (pp->flags & PP_CLAIMED) {
  613. struct ieee1284_info *info;
  614. info = &pp->pdev->port->ieee1284;
  615. pp->state.mode = info->mode;
  616. pp->state.phase = info->phase;
  617. info->mode = pp->saved_state.mode;
  618. info->phase = pp->saved_state.phase;
  619. parport_release (pp->pdev);
  620. if (compat_negot != 1) {
  621. printk (KERN_DEBUG CHRDEV "%x: released pardevice "
  622. "because user-space forgot\n", minor);
  623. }
  624. }
  625. if (pp->pdev) {
  626. const char *name = pp->pdev->name;
  627. parport_unregister_device (pp->pdev);
  628. kfree (name);
  629. pp->pdev = NULL;
  630. printk (KERN_DEBUG CHRDEV "%x: unregistered pardevice\n",
  631. minor);
  632. }
  633. kfree (pp);
  634. return 0;
  635. }
  636. /* No kernel lock held - fine */
  637. static unsigned int pp_poll (struct file * file, poll_table * wait)
  638. {
  639. struct pp_struct *pp = file->private_data;
  640. unsigned int mask = 0;
  641. poll_wait (file, &pp->irq_wait, wait);
  642. if (atomic_read (&pp->irqc))
  643. mask |= POLLIN | POLLRDNORM;
  644. return mask;
  645. }
  646. static struct class *ppdev_class;
  647. static const struct file_operations pp_fops = {
  648. .owner = THIS_MODULE,
  649. .llseek = no_llseek,
  650. .read = pp_read,
  651. .write = pp_write,
  652. .poll = pp_poll,
  653. .unlocked_ioctl = pp_ioctl,
  654. .open = pp_open,
  655. .release = pp_release,
  656. };
  657. static void pp_attach(struct parport *port)
  658. {
  659. device_create_drvdata(ppdev_class, port->dev,
  660. MKDEV(PP_MAJOR, port->number),
  661. NULL, "parport%d", port->number);
  662. }
  663. static void pp_detach(struct parport *port)
  664. {
  665. device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
  666. }
  667. static struct parport_driver pp_driver = {
  668. .name = CHRDEV,
  669. .attach = pp_attach,
  670. .detach = pp_detach,
  671. };
  672. static int __init ppdev_init (void)
  673. {
  674. int err = 0;
  675. if (register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
  676. printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
  677. PP_MAJOR);
  678. return -EIO;
  679. }
  680. ppdev_class = class_create(THIS_MODULE, CHRDEV);
  681. if (IS_ERR(ppdev_class)) {
  682. err = PTR_ERR(ppdev_class);
  683. goto out_chrdev;
  684. }
  685. if (parport_register_driver(&pp_driver)) {
  686. printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
  687. goto out_class;
  688. }
  689. printk (KERN_INFO PP_VERSION "\n");
  690. goto out;
  691. out_class:
  692. class_destroy(ppdev_class);
  693. out_chrdev:
  694. unregister_chrdev(PP_MAJOR, CHRDEV);
  695. out:
  696. return err;
  697. }
  698. static void __exit ppdev_cleanup (void)
  699. {
  700. /* Clean up all parport stuff */
  701. parport_unregister_driver(&pp_driver);
  702. class_destroy(ppdev_class);
  703. unregister_chrdev (PP_MAJOR, CHRDEV);
  704. }
  705. module_init(ppdev_init);
  706. module_exit(ppdev_cleanup);
  707. MODULE_LICENSE("GPL");
  708. MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);