ppdev.c 21 KB

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