ppdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. struct pardev_cb ppdev_cb;
  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. memset(&ppdev_cb, 0, sizeof(ppdev_cb));
  256. ppdev_cb.irq_func = pp_irq;
  257. ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
  258. ppdev_cb.private = pp;
  259. pdev = parport_register_dev_model(port, name, &ppdev_cb, minor);
  260. parport_put_port(port);
  261. if (!pdev) {
  262. printk(KERN_WARNING "%s: failed to register device!\n", name);
  263. kfree(name);
  264. return -ENXIO;
  265. }
  266. pp->pdev = pdev;
  267. dev_dbg(&pdev->dev, "registered pardevice\n");
  268. return 0;
  269. }
  270. static enum ieee1284_phase init_phase(int mode)
  271. {
  272. switch (mode & ~(IEEE1284_DEVICEID
  273. | IEEE1284_ADDR)) {
  274. case IEEE1284_MODE_NIBBLE:
  275. case IEEE1284_MODE_BYTE:
  276. return IEEE1284_PH_REV_IDLE;
  277. }
  278. return IEEE1284_PH_FWD_IDLE;
  279. }
  280. static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
  281. {
  282. long to_jiffies;
  283. if ((tv_sec < 0) || (tv_usec < 0))
  284. return -EINVAL;
  285. to_jiffies = usecs_to_jiffies(tv_usec);
  286. to_jiffies += tv_sec * HZ;
  287. if (to_jiffies <= 0)
  288. return -EINVAL;
  289. pdev->timeout = to_jiffies;
  290. return 0;
  291. }
  292. static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  293. {
  294. unsigned int minor = iminor(file_inode(file));
  295. struct pp_struct *pp = file->private_data;
  296. struct parport *port;
  297. void __user *argp = (void __user *)arg;
  298. /* First handle the cases that don't take arguments. */
  299. switch (cmd) {
  300. case PPCLAIM:
  301. {
  302. struct ieee1284_info *info;
  303. int ret;
  304. if (pp->flags & PP_CLAIMED) {
  305. dev_dbg(&pp->pdev->dev, "you've already got it!\n");
  306. return -EINVAL;
  307. }
  308. /* Deferred device registration. */
  309. if (!pp->pdev) {
  310. int err = register_device(minor, pp);
  311. if (err)
  312. return err;
  313. }
  314. ret = parport_claim_or_block(pp->pdev);
  315. if (ret < 0)
  316. return ret;
  317. pp->flags |= PP_CLAIMED;
  318. /* For interrupt-reporting to work, we need to be
  319. * informed of each interrupt. */
  320. pp_enable_irq(pp);
  321. /* We may need to fix up the state machine. */
  322. info = &pp->pdev->port->ieee1284;
  323. pp->saved_state.mode = info->mode;
  324. pp->saved_state.phase = info->phase;
  325. info->mode = pp->state.mode;
  326. info->phase = pp->state.phase;
  327. pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
  328. parport_set_timeout(pp->pdev, pp->default_inactivity);
  329. return 0;
  330. }
  331. case PPEXCL:
  332. if (pp->pdev) {
  333. dev_dbg(&pp->pdev->dev,
  334. "too late for PPEXCL; already registered\n");
  335. if (pp->flags & PP_EXCL)
  336. /* But it's not really an error. */
  337. return 0;
  338. /* There's no chance of making the driver happy. */
  339. return -EINVAL;
  340. }
  341. /* Just remember to register the device exclusively
  342. * when we finally do the registration. */
  343. pp->flags |= PP_EXCL;
  344. return 0;
  345. case PPSETMODE:
  346. {
  347. int mode;
  348. if (copy_from_user(&mode, argp, sizeof(mode)))
  349. return -EFAULT;
  350. /* FIXME: validate mode */
  351. pp->state.mode = mode;
  352. pp->state.phase = init_phase(mode);
  353. if (pp->flags & PP_CLAIMED) {
  354. pp->pdev->port->ieee1284.mode = mode;
  355. pp->pdev->port->ieee1284.phase = pp->state.phase;
  356. }
  357. return 0;
  358. }
  359. case PPGETMODE:
  360. {
  361. int mode;
  362. if (pp->flags & PP_CLAIMED)
  363. mode = pp->pdev->port->ieee1284.mode;
  364. else
  365. mode = pp->state.mode;
  366. if (copy_to_user(argp, &mode, sizeof(mode)))
  367. return -EFAULT;
  368. return 0;
  369. }
  370. case PPSETPHASE:
  371. {
  372. int phase;
  373. if (copy_from_user(&phase, argp, sizeof(phase)))
  374. return -EFAULT;
  375. /* FIXME: validate phase */
  376. pp->state.phase = phase;
  377. if (pp->flags & PP_CLAIMED)
  378. pp->pdev->port->ieee1284.phase = phase;
  379. return 0;
  380. }
  381. case PPGETPHASE:
  382. {
  383. int phase;
  384. if (pp->flags & PP_CLAIMED)
  385. phase = pp->pdev->port->ieee1284.phase;
  386. else
  387. phase = pp->state.phase;
  388. if (copy_to_user(argp, &phase, sizeof(phase)))
  389. return -EFAULT;
  390. return 0;
  391. }
  392. case PPGETMODES:
  393. {
  394. unsigned int modes;
  395. port = parport_find_number(minor);
  396. if (!port)
  397. return -ENODEV;
  398. modes = port->modes;
  399. parport_put_port(port);
  400. if (copy_to_user(argp, &modes, sizeof(modes)))
  401. return -EFAULT;
  402. return 0;
  403. }
  404. case PPSETFLAGS:
  405. {
  406. int uflags;
  407. if (copy_from_user(&uflags, argp, sizeof(uflags)))
  408. return -EFAULT;
  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. 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. s32 time32[2];
  435. s64 time64[2];
  436. struct timespec64 ts;
  437. int ret;
  438. case PPRSTATUS:
  439. reg = parport_read_status(port);
  440. if (copy_to_user(argp, &reg, sizeof(reg)))
  441. return -EFAULT;
  442. return 0;
  443. case PPRDATA:
  444. reg = parport_read_data(port);
  445. if (copy_to_user(argp, &reg, sizeof(reg)))
  446. return -EFAULT;
  447. return 0;
  448. case PPRCONTROL:
  449. reg = parport_read_control(port);
  450. if (copy_to_user(argp, &reg, sizeof(reg)))
  451. return -EFAULT;
  452. return 0;
  453. case PPYIELD:
  454. parport_yield_blocking(pp->pdev);
  455. return 0;
  456. case PPRELEASE:
  457. /* Save the state machine's state. */
  458. info = &pp->pdev->port->ieee1284;
  459. pp->state.mode = info->mode;
  460. pp->state.phase = info->phase;
  461. info->mode = pp->saved_state.mode;
  462. info->phase = pp->saved_state.phase;
  463. parport_release(pp->pdev);
  464. pp->flags &= ~PP_CLAIMED;
  465. return 0;
  466. case PPWCONTROL:
  467. if (copy_from_user(&reg, argp, sizeof(reg)))
  468. return -EFAULT;
  469. parport_write_control(port, reg);
  470. return 0;
  471. case PPWDATA:
  472. if (copy_from_user(&reg, argp, sizeof(reg)))
  473. return -EFAULT;
  474. parport_write_data(port, reg);
  475. return 0;
  476. case PPFCONTROL:
  477. if (copy_from_user(&mask, argp,
  478. sizeof(mask)))
  479. return -EFAULT;
  480. if (copy_from_user(&reg, 1 + (unsigned char __user *) arg,
  481. sizeof(reg)))
  482. return -EFAULT;
  483. parport_frob_control(port, mask, reg);
  484. return 0;
  485. case PPDATADIR:
  486. if (copy_from_user(&mode, argp, sizeof(mode)))
  487. return -EFAULT;
  488. if (mode)
  489. port->ops->data_reverse(port);
  490. else
  491. port->ops->data_forward(port);
  492. return 0;
  493. case PPNEGOT:
  494. if (copy_from_user(&mode, argp, sizeof(mode)))
  495. return -EFAULT;
  496. switch ((ret = parport_negotiate(port, mode))) {
  497. case 0: break;
  498. case -1: /* handshake failed, peripheral not IEEE 1284 */
  499. ret = -EIO;
  500. break;
  501. case 1: /* handshake succeeded, peripheral rejected mode */
  502. ret = -ENXIO;
  503. break;
  504. }
  505. pp_enable_irq(pp);
  506. return ret;
  507. case PPWCTLONIRQ:
  508. if (copy_from_user(&reg, argp, sizeof(reg)))
  509. return -EFAULT;
  510. /* Remember what to set the control lines to, for next
  511. * time we get an interrupt. */
  512. pp->irqctl = reg;
  513. pp->irqresponse = 1;
  514. return 0;
  515. case PPCLRIRQ:
  516. ret = atomic_read(&pp->irqc);
  517. if (copy_to_user(argp, &ret, sizeof(ret)))
  518. return -EFAULT;
  519. atomic_sub(ret, &pp->irqc);
  520. return 0;
  521. case PPSETTIME32:
  522. if (copy_from_user(time32, argp, sizeof(time32)))
  523. return -EFAULT;
  524. return pp_set_timeout(pp->pdev, time32[0], time32[1]);
  525. case PPSETTIME64:
  526. if (copy_from_user(time64, argp, sizeof(time64)))
  527. return -EFAULT;
  528. return pp_set_timeout(pp->pdev, time64[0], time64[1]);
  529. case PPGETTIME32:
  530. jiffies_to_timespec64(pp->pdev->timeout, &ts);
  531. time32[0] = ts.tv_sec;
  532. time32[1] = ts.tv_nsec / NSEC_PER_USEC;
  533. if ((time32[0] < 0) || (time32[1] < 0))
  534. return -EINVAL;
  535. if (copy_to_user(argp, time32, sizeof(time32)))
  536. return -EFAULT;
  537. return 0;
  538. case PPGETTIME64:
  539. jiffies_to_timespec64(pp->pdev->timeout, &ts);
  540. time64[0] = ts.tv_sec;
  541. time64[1] = ts.tv_nsec / NSEC_PER_USEC;
  542. if ((time64[0] < 0) || (time64[1] < 0))
  543. return -EINVAL;
  544. if (copy_to_user(argp, time64, sizeof(time64)))
  545. return -EFAULT;
  546. return 0;
  547. default:
  548. dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
  549. return -EINVAL;
  550. }
  551. /* Keep the compiler happy */
  552. return 0;
  553. }
  554. static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  555. {
  556. long ret;
  557. mutex_lock(&pp_do_mutex);
  558. ret = pp_do_ioctl(file, cmd, arg);
  559. mutex_unlock(&pp_do_mutex);
  560. return ret;
  561. }
  562. #ifdef CONFIG_COMPAT
  563. static long pp_compat_ioctl(struct file *file, unsigned int cmd,
  564. unsigned long arg)
  565. {
  566. return pp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  567. }
  568. #endif
  569. static int pp_open(struct inode *inode, struct file *file)
  570. {
  571. unsigned int minor = iminor(inode);
  572. struct pp_struct *pp;
  573. if (minor >= PARPORT_MAX)
  574. return -ENXIO;
  575. pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
  576. if (!pp)
  577. return -ENOMEM;
  578. pp->state.mode = IEEE1284_MODE_COMPAT;
  579. pp->state.phase = init_phase(pp->state.mode);
  580. pp->flags = 0;
  581. pp->irqresponse = 0;
  582. atomic_set(&pp->irqc, 0);
  583. init_waitqueue_head(&pp->irq_wait);
  584. /* Defer the actual device registration until the first claim.
  585. * That way, we know whether or not the driver wants to have
  586. * exclusive access to the port (PPEXCL).
  587. */
  588. pp->pdev = NULL;
  589. file->private_data = pp;
  590. return 0;
  591. }
  592. static int pp_release(struct inode *inode, struct file *file)
  593. {
  594. unsigned int minor = iminor(inode);
  595. struct pp_struct *pp = file->private_data;
  596. int compat_negot;
  597. compat_negot = 0;
  598. if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
  599. (pp->state.mode != IEEE1284_MODE_COMPAT)) {
  600. struct ieee1284_info *info;
  601. /* parport released, but not in compatibility mode */
  602. parport_claim_or_block(pp->pdev);
  603. pp->flags |= PP_CLAIMED;
  604. info = &pp->pdev->port->ieee1284;
  605. pp->saved_state.mode = info->mode;
  606. pp->saved_state.phase = info->phase;
  607. info->mode = pp->state.mode;
  608. info->phase = pp->state.phase;
  609. compat_negot = 1;
  610. } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
  611. (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
  612. compat_negot = 2;
  613. }
  614. if (compat_negot) {
  615. parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
  616. dev_dbg(&pp->pdev->dev,
  617. "negotiated back to compatibility mode because user-space forgot\n");
  618. }
  619. if (pp->flags & PP_CLAIMED) {
  620. struct ieee1284_info *info;
  621. info = &pp->pdev->port->ieee1284;
  622. pp->state.mode = info->mode;
  623. pp->state.phase = info->phase;
  624. info->mode = pp->saved_state.mode;
  625. info->phase = pp->saved_state.phase;
  626. parport_release(pp->pdev);
  627. if (compat_negot != 1) {
  628. pr_debug(CHRDEV "%x: released pardevice "
  629. "because user-space forgot\n", minor);
  630. }
  631. }
  632. if (pp->pdev) {
  633. parport_unregister_device(pp->pdev);
  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 int pp_probe(struct pardevice *par_dev)
  674. {
  675. struct device_driver *drv = par_dev->dev.driver;
  676. int len = strlen(drv->name);
  677. if (strncmp(par_dev->name, drv->name, len))
  678. return -ENODEV;
  679. return 0;
  680. }
  681. static struct parport_driver pp_driver = {
  682. .name = CHRDEV,
  683. .probe = pp_probe,
  684. .match_port = pp_attach,
  685. .detach = pp_detach,
  686. .devmodel = true,
  687. };
  688. static int __init ppdev_init(void)
  689. {
  690. int err = 0;
  691. if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
  692. printk(KERN_WARNING CHRDEV ": unable to get major %d\n",
  693. PP_MAJOR);
  694. return -EIO;
  695. }
  696. ppdev_class = class_create(THIS_MODULE, CHRDEV);
  697. if (IS_ERR(ppdev_class)) {
  698. err = PTR_ERR(ppdev_class);
  699. goto out_chrdev;
  700. }
  701. err = parport_register_driver(&pp_driver);
  702. if (err < 0) {
  703. printk(KERN_WARNING CHRDEV ": unable to register with parport\n");
  704. goto out_class;
  705. }
  706. printk(KERN_INFO PP_VERSION "\n");
  707. goto out;
  708. out_class:
  709. class_destroy(ppdev_class);
  710. out_chrdev:
  711. unregister_chrdev(PP_MAJOR, CHRDEV);
  712. out:
  713. return err;
  714. }
  715. static void __exit ppdev_cleanup(void)
  716. {
  717. /* Clean up all parport stuff */
  718. parport_unregister_driver(&pp_driver);
  719. class_destroy(ppdev_class);
  720. unregister_chrdev(PP_MAJOR, CHRDEV);
  721. }
  722. module_init(ppdev_init);
  723. module_exit(ppdev_cleanup);
  724. MODULE_LICENSE("GPL");
  725. MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);