ppdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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/signal.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. static DEFINE_MUTEX(pp_do_mutex);
  97. /* define fixed sized ioctl cmd for y2038 migration */
  98. #define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
  99. #define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
  100. #define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
  101. #define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
  102. static inline void pp_enable_irq(struct pp_struct *pp)
  103. {
  104. struct parport *port = pp->pdev->port;
  105. port->ops->enable_irq(port);
  106. }
  107. static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
  108. loff_t *ppos)
  109. {
  110. unsigned int minor = iminor(file_inode(file));
  111. struct pp_struct *pp = file->private_data;
  112. char *kbuffer;
  113. ssize_t bytes_read = 0;
  114. struct parport *pport;
  115. int mode;
  116. if (!(pp->flags & PP_CLAIMED)) {
  117. /* Don't have the port claimed */
  118. pr_debug(CHRDEV "%x: claim the port first\n", minor);
  119. return -EINVAL;
  120. }
  121. /* Trivial case. */
  122. if (count == 0)
  123. return 0;
  124. kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
  125. if (!kbuffer)
  126. return -ENOMEM;
  127. pport = pp->pdev->port;
  128. mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  129. parport_set_timeout(pp->pdev,
  130. (file->f_flags & O_NONBLOCK) ?
  131. PARPORT_INACTIVITY_O_NONBLOCK :
  132. pp->default_inactivity);
  133. while (bytes_read == 0) {
  134. ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
  135. if (mode == IEEE1284_MODE_EPP) {
  136. /* various specials for EPP mode */
  137. int flags = 0;
  138. size_t (*fn)(struct parport *, void *, size_t, int);
  139. if (pp->flags & PP_W91284PIC)
  140. flags |= PARPORT_W91284PIC;
  141. if (pp->flags & PP_FASTREAD)
  142. flags |= PARPORT_EPP_FAST;
  143. if (pport->ieee1284.mode & IEEE1284_ADDR)
  144. fn = pport->ops->epp_read_addr;
  145. else
  146. fn = pport->ops->epp_read_data;
  147. bytes_read = (*fn)(pport, kbuffer, need, flags);
  148. } else {
  149. bytes_read = parport_read(pport, kbuffer, need);
  150. }
  151. if (bytes_read != 0)
  152. break;
  153. if (file->f_flags & O_NONBLOCK) {
  154. bytes_read = -EAGAIN;
  155. break;
  156. }
  157. if (signal_pending(current)) {
  158. bytes_read = -ERESTARTSYS;
  159. break;
  160. }
  161. cond_resched();
  162. }
  163. parport_set_timeout(pp->pdev, pp->default_inactivity);
  164. if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
  165. bytes_read = -EFAULT;
  166. kfree(kbuffer);
  167. pp_enable_irq(pp);
  168. return bytes_read;
  169. }
  170. static ssize_t pp_write(struct file *file, const char __user *buf,
  171. size_t count, loff_t *ppos)
  172. {
  173. unsigned int minor = iminor(file_inode(file));
  174. struct pp_struct *pp = file->private_data;
  175. char *kbuffer;
  176. ssize_t bytes_written = 0;
  177. ssize_t wrote;
  178. int mode;
  179. struct parport *pport;
  180. if (!(pp->flags & PP_CLAIMED)) {
  181. /* Don't have the port claimed */
  182. pr_debug(CHRDEV "%x: claim the port first\n", minor);
  183. return -EINVAL;
  184. }
  185. kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
  186. if (!kbuffer)
  187. return -ENOMEM;
  188. pport = pp->pdev->port;
  189. mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
  190. parport_set_timeout(pp->pdev,
  191. (file->f_flags & O_NONBLOCK) ?
  192. PARPORT_INACTIVITY_O_NONBLOCK :
  193. pp->default_inactivity);
  194. while (bytes_written < count) {
  195. ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
  196. if (copy_from_user(kbuffer, buf + bytes_written, n)) {
  197. bytes_written = -EFAULT;
  198. break;
  199. }
  200. if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
  201. /* do a fast EPP write */
  202. if (pport->ieee1284.mode & IEEE1284_ADDR) {
  203. wrote = pport->ops->epp_write_addr(pport,
  204. kbuffer, n, PARPORT_EPP_FAST);
  205. } else {
  206. wrote = pport->ops->epp_write_data(pport,
  207. kbuffer, n, PARPORT_EPP_FAST);
  208. }
  209. } else {
  210. wrote = parport_write(pp->pdev->port, kbuffer, n);
  211. }
  212. if (wrote <= 0) {
  213. if (!bytes_written)
  214. bytes_written = wrote;
  215. break;
  216. }
  217. bytes_written += wrote;
  218. if (file->f_flags & O_NONBLOCK) {
  219. if (!bytes_written)
  220. bytes_written = -EAGAIN;
  221. break;
  222. }
  223. if (signal_pending(current))
  224. break;
  225. cond_resched();
  226. }
  227. parport_set_timeout(pp->pdev, pp->default_inactivity);
  228. kfree(kbuffer);
  229. pp_enable_irq(pp);
  230. return bytes_written;
  231. }
  232. static void pp_irq(void *private)
  233. {
  234. struct pp_struct *pp = private;
  235. if (pp->irqresponse) {
  236. parport_write_control(pp->pdev->port, pp->irqctl);
  237. pp->irqresponse = 0;
  238. }
  239. atomic_inc(&pp->irqc);
  240. wake_up_interruptible(&pp->irq_wait);
  241. }
  242. static int register_device(int minor, struct pp_struct *pp)
  243. {
  244. struct parport *port;
  245. struct pardevice *pdev = NULL;
  246. char *name;
  247. struct pardev_cb ppdev_cb;
  248. int rc = 0, index;
  249. name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
  250. if (name == NULL)
  251. return -ENOMEM;
  252. port = parport_find_number(minor);
  253. if (!port) {
  254. pr_warn("%s: no associated port!\n", name);
  255. rc = -ENXIO;
  256. goto err;
  257. }
  258. index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
  259. memset(&ppdev_cb, 0, sizeof(ppdev_cb));
  260. ppdev_cb.irq_func = pp_irq;
  261. ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
  262. ppdev_cb.private = pp;
  263. pdev = parport_register_dev_model(port, name, &ppdev_cb, index);
  264. parport_put_port(port);
  265. if (!pdev) {
  266. pr_warn("%s: failed to register device!\n", name);
  267. rc = -ENXIO;
  268. ida_simple_remove(&ida_index, index);
  269. goto err;
  270. }
  271. pp->pdev = pdev;
  272. pp->index = index;
  273. dev_dbg(&pdev->dev, "registered pardevice\n");
  274. err:
  275. kfree(name);
  276. return rc;
  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. if ((time32[0] < 0) || (time32[1] < 0))
  533. return -EINVAL;
  534. return pp_set_timeout(pp->pdev, time32[0], time32[1]);
  535. case PPSETTIME64:
  536. if (copy_from_user(time64, argp, sizeof(time64)))
  537. return -EFAULT;
  538. if ((time64[0] < 0) || (time64[1] < 0))
  539. return -EINVAL;
  540. if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
  541. time64[1] >>= 32;
  542. return pp_set_timeout(pp->pdev, time64[0], time64[1]);
  543. case PPGETTIME32:
  544. jiffies_to_timespec64(pp->pdev->timeout, &ts);
  545. time32[0] = ts.tv_sec;
  546. time32[1] = ts.tv_nsec / NSEC_PER_USEC;
  547. if (copy_to_user(argp, time32, sizeof(time32)))
  548. return -EFAULT;
  549. return 0;
  550. case PPGETTIME64:
  551. jiffies_to_timespec64(pp->pdev->timeout, &ts);
  552. time64[0] = ts.tv_sec;
  553. time64[1] = ts.tv_nsec / NSEC_PER_USEC;
  554. if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
  555. time64[1] <<= 32;
  556. if (copy_to_user(argp, time64, sizeof(time64)))
  557. return -EFAULT;
  558. return 0;
  559. default:
  560. dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
  561. return -EINVAL;
  562. }
  563. /* Keep the compiler happy */
  564. return 0;
  565. }
  566. static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  567. {
  568. long ret;
  569. mutex_lock(&pp_do_mutex);
  570. ret = pp_do_ioctl(file, cmd, arg);
  571. mutex_unlock(&pp_do_mutex);
  572. return ret;
  573. }
  574. #ifdef CONFIG_COMPAT
  575. static long pp_compat_ioctl(struct file *file, unsigned int cmd,
  576. unsigned long arg)
  577. {
  578. return pp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  579. }
  580. #endif
  581. static int pp_open(struct inode *inode, struct file *file)
  582. {
  583. unsigned int minor = iminor(inode);
  584. struct pp_struct *pp;
  585. if (minor >= PARPORT_MAX)
  586. return -ENXIO;
  587. pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
  588. if (!pp)
  589. return -ENOMEM;
  590. pp->state.mode = IEEE1284_MODE_COMPAT;
  591. pp->state.phase = init_phase(pp->state.mode);
  592. pp->flags = 0;
  593. pp->irqresponse = 0;
  594. atomic_set(&pp->irqc, 0);
  595. init_waitqueue_head(&pp->irq_wait);
  596. /* Defer the actual device registration until the first claim.
  597. * That way, we know whether or not the driver wants to have
  598. * exclusive access to the port (PPEXCL).
  599. */
  600. pp->pdev = NULL;
  601. file->private_data = pp;
  602. return 0;
  603. }
  604. static int pp_release(struct inode *inode, struct file *file)
  605. {
  606. unsigned int minor = iminor(inode);
  607. struct pp_struct *pp = file->private_data;
  608. int compat_negot;
  609. compat_negot = 0;
  610. if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
  611. (pp->state.mode != IEEE1284_MODE_COMPAT)) {
  612. struct ieee1284_info *info;
  613. /* parport released, but not in compatibility mode */
  614. parport_claim_or_block(pp->pdev);
  615. pp->flags |= PP_CLAIMED;
  616. info = &pp->pdev->port->ieee1284;
  617. pp->saved_state.mode = info->mode;
  618. pp->saved_state.phase = info->phase;
  619. info->mode = pp->state.mode;
  620. info->phase = pp->state.phase;
  621. compat_negot = 1;
  622. } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
  623. (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
  624. compat_negot = 2;
  625. }
  626. if (compat_negot) {
  627. parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
  628. dev_dbg(&pp->pdev->dev,
  629. "negotiated back to compatibility mode because user-space forgot\n");
  630. }
  631. if (pp->flags & PP_CLAIMED) {
  632. struct ieee1284_info *info;
  633. info = &pp->pdev->port->ieee1284;
  634. pp->state.mode = info->mode;
  635. pp->state.phase = info->phase;
  636. info->mode = pp->saved_state.mode;
  637. info->phase = pp->saved_state.phase;
  638. parport_release(pp->pdev);
  639. if (compat_negot != 1) {
  640. pr_debug(CHRDEV "%x: released pardevice "
  641. "because user-space forgot\n", minor);
  642. }
  643. }
  644. if (pp->pdev) {
  645. parport_unregister_device(pp->pdev);
  646. ida_simple_remove(&ida_index, pp->index);
  647. pp->pdev = NULL;
  648. pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
  649. }
  650. kfree(pp);
  651. return 0;
  652. }
  653. /* No kernel lock held - fine */
  654. static __poll_t pp_poll(struct file *file, poll_table *wait)
  655. {
  656. struct pp_struct *pp = file->private_data;
  657. __poll_t mask = 0;
  658. poll_wait(file, &pp->irq_wait, wait);
  659. if (atomic_read(&pp->irqc))
  660. mask |= EPOLLIN | EPOLLRDNORM;
  661. return mask;
  662. }
  663. static struct class *ppdev_class;
  664. static const struct file_operations pp_fops = {
  665. .owner = THIS_MODULE,
  666. .llseek = no_llseek,
  667. .read = pp_read,
  668. .write = pp_write,
  669. .poll = pp_poll,
  670. .unlocked_ioctl = pp_ioctl,
  671. #ifdef CONFIG_COMPAT
  672. .compat_ioctl = pp_compat_ioctl,
  673. #endif
  674. .open = pp_open,
  675. .release = pp_release,
  676. };
  677. static void pp_attach(struct parport *port)
  678. {
  679. struct device *ret;
  680. if (devices[port->number])
  681. return;
  682. ret = device_create(ppdev_class, port->dev,
  683. MKDEV(PP_MAJOR, port->number), NULL,
  684. "parport%d", port->number);
  685. if (IS_ERR(ret)) {
  686. pr_err("Failed to create device parport%d\n",
  687. port->number);
  688. return;
  689. }
  690. devices[port->number] = ret;
  691. }
  692. static void pp_detach(struct parport *port)
  693. {
  694. if (!devices[port->number])
  695. return;
  696. device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
  697. devices[port->number] = NULL;
  698. }
  699. static int pp_probe(struct pardevice *par_dev)
  700. {
  701. struct device_driver *drv = par_dev->dev.driver;
  702. int len = strlen(drv->name);
  703. if (strncmp(par_dev->name, drv->name, len))
  704. return -ENODEV;
  705. return 0;
  706. }
  707. static struct parport_driver pp_driver = {
  708. .name = CHRDEV,
  709. .probe = pp_probe,
  710. .match_port = pp_attach,
  711. .detach = pp_detach,
  712. .devmodel = true,
  713. };
  714. static int __init ppdev_init(void)
  715. {
  716. int err = 0;
  717. if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
  718. pr_warn(CHRDEV ": unable to get major %d\n", PP_MAJOR);
  719. return -EIO;
  720. }
  721. ppdev_class = class_create(THIS_MODULE, CHRDEV);
  722. if (IS_ERR(ppdev_class)) {
  723. err = PTR_ERR(ppdev_class);
  724. goto out_chrdev;
  725. }
  726. err = parport_register_driver(&pp_driver);
  727. if (err < 0) {
  728. pr_warn(CHRDEV ": unable to register with parport\n");
  729. goto out_class;
  730. }
  731. pr_info(PP_VERSION "\n");
  732. goto out;
  733. out_class:
  734. class_destroy(ppdev_class);
  735. out_chrdev:
  736. unregister_chrdev(PP_MAJOR, CHRDEV);
  737. out:
  738. return err;
  739. }
  740. static void __exit ppdev_cleanup(void)
  741. {
  742. /* Clean up all parport stuff */
  743. parport_unregister_driver(&pp_driver);
  744. class_destroy(ppdev_class);
  745. unregister_chrdev(PP_MAJOR, CHRDEV);
  746. }
  747. module_init(ppdev_init);
  748. module_exit(ppdev_cleanup);
  749. MODULE_LICENSE("GPL");
  750. MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);