ppdev.c 20 KB

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