blob: c03d998731ea1bb6e26694a615d74b8e78b9d5f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
59#include <linux/module.h>
60#include <linux/init.h>
61#include <linux/sched.h>
62#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/ioctl.h>
64#include <linux/parport.h>
65#include <linux/ctype.h>
66#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Rene Hermane6a67842006-03-25 03:07:13 -080068#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/ppdev.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020070#include <linux/mutex.h>
Alan Cox6d535d32008-07-25 01:48:16 -070071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define PP_VERSION "ppdev: user-space parallel port driver"
74#define CHRDEV "ppdev"
75
76struct pp_struct {
77 struct pardevice * pdev;
78 wait_queue_head_t irq_wait;
79 atomic_t irqc;
80 unsigned int flags;
81 int irqresponse;
82 unsigned char irqctl;
83 struct ieee1284_info state;
84 struct ieee1284_info saved_state;
85 long default_inactivity;
86};
87
88/* pp_struct.flags bitfields */
89#define PP_CLAIMED (1<<0)
90#define PP_EXCL (1<<1)
91
92/* Other constants */
93#define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
94#define PP_BUFFER_SIZE 1024
95#define PARDEVICE_MAX 8
96
97/* ROUND_UP macro from fs/select.c */
98#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
99
Arnd Bergmann613655f2010-06-02 14:28:52 +0200100static DEFINE_MUTEX(pp_do_mutex);
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800101
102/* define fixed sized ioctl cmd for y2038 migration */
103#define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
104#define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
105#define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
106#define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static inline void pp_enable_irq (struct pp_struct *pp)
109{
110 struct parport *port = pp->pdev->port;
111 port->ops->enable_irq (port);
112}
113
114static ssize_t pp_read (struct file * file, char __user * buf, size_t count,
115 loff_t * ppos)
116{
Al Viro496ad9a2013-01-23 17:07:38 -0500117 unsigned int minor = iminor(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct pp_struct *pp = file->private_data;
119 char * kbuffer;
120 ssize_t bytes_read = 0;
121 struct parport *pport;
122 int mode;
123
124 if (!(pp->flags & PP_CLAIMED)) {
125 /* Don't have the port claimed */
Michael Buesch81fc4012009-06-17 16:27:49 -0700126 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -EINVAL;
128 }
129
130 /* Trivial case. */
131 if (count == 0)
132 return 0;
133
134 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
135 if (!kbuffer) {
136 return -ENOMEM;
137 }
138 pport = pp->pdev->port;
139 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
140
141 parport_set_timeout (pp->pdev,
142 (file->f_flags & O_NONBLOCK) ?
143 PARPORT_INACTIVITY_O_NONBLOCK :
144 pp->default_inactivity);
145
146 while (bytes_read == 0) {
147 ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
148
149 if (mode == IEEE1284_MODE_EPP) {
150 /* various specials for EPP mode */
151 int flags = 0;
152 size_t (*fn)(struct parport *, void *, size_t, int);
153
154 if (pp->flags & PP_W91284PIC) {
155 flags |= PARPORT_W91284PIC;
156 }
157 if (pp->flags & PP_FASTREAD) {
158 flags |= PARPORT_EPP_FAST;
159 }
160 if (pport->ieee1284.mode & IEEE1284_ADDR) {
161 fn = pport->ops->epp_read_addr;
162 } else {
163 fn = pport->ops->epp_read_data;
164 }
165 bytes_read = (*fn)(pport, kbuffer, need, flags);
166 } else {
167 bytes_read = parport_read (pport, kbuffer, need);
168 }
169
170 if (bytes_read != 0)
171 break;
172
173 if (file->f_flags & O_NONBLOCK) {
174 bytes_read = -EAGAIN;
175 break;
176 }
177
178 if (signal_pending (current)) {
179 bytes_read = -ERESTARTSYS;
180 break;
181 }
182
183 cond_resched();
184 }
185
186 parport_set_timeout (pp->pdev, pp->default_inactivity);
187
188 if (bytes_read > 0 && copy_to_user (buf, kbuffer, bytes_read))
189 bytes_read = -EFAULT;
190
191 kfree (kbuffer);
192 pp_enable_irq (pp);
193 return bytes_read;
194}
195
196static ssize_t pp_write (struct file * file, const char __user * buf,
197 size_t count, loff_t * ppos)
198{
Al Viro496ad9a2013-01-23 17:07:38 -0500199 unsigned int minor = iminor(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct pp_struct *pp = file->private_data;
201 char * kbuffer;
202 ssize_t bytes_written = 0;
203 ssize_t wrote;
204 int mode;
205 struct parport *pport;
206
207 if (!(pp->flags & PP_CLAIMED)) {
208 /* Don't have the port claimed */
Michael Buesch81fc4012009-06-17 16:27:49 -0700209 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EINVAL;
211 }
212
213 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
214 if (!kbuffer) {
215 return -ENOMEM;
216 }
217 pport = pp->pdev->port;
218 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
219
220 parport_set_timeout (pp->pdev,
221 (file->f_flags & O_NONBLOCK) ?
222 PARPORT_INACTIVITY_O_NONBLOCK :
223 pp->default_inactivity);
224
225 while (bytes_written < count) {
226 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
227
228 if (copy_from_user (kbuffer, buf + bytes_written, n)) {
229 bytes_written = -EFAULT;
230 break;
231 }
232
233 if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
234 /* do a fast EPP write */
235 if (pport->ieee1284.mode & IEEE1284_ADDR) {
236 wrote = pport->ops->epp_write_addr (pport,
237 kbuffer, n, PARPORT_EPP_FAST);
238 } else {
239 wrote = pport->ops->epp_write_data (pport,
240 kbuffer, n, PARPORT_EPP_FAST);
241 }
242 } else {
243 wrote = parport_write (pp->pdev->port, kbuffer, n);
244 }
245
246 if (wrote <= 0) {
247 if (!bytes_written) {
248 bytes_written = wrote;
249 }
250 break;
251 }
252
253 bytes_written += wrote;
254
255 if (file->f_flags & O_NONBLOCK) {
256 if (!bytes_written)
257 bytes_written = -EAGAIN;
258 break;
259 }
260
Alan Cox5d0c3d42012-10-01 16:24:17 +0100261 if (signal_pending (current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 cond_resched();
265 }
266
267 parport_set_timeout (pp->pdev, pp->default_inactivity);
268
269 kfree (kbuffer);
270 pp_enable_irq (pp);
271 return bytes_written;
272}
273
Jeff Garzik5712cb32007-10-19 02:54:26 -0400274static void pp_irq (void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Jeff Garzik5712cb32007-10-19 02:54:26 -0400276 struct pp_struct *pp = private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (pp->irqresponse) {
279 parport_write_control (pp->pdev->port, pp->irqctl);
280 pp->irqresponse = 0;
281 }
282
283 atomic_inc (&pp->irqc);
284 wake_up_interruptible (&pp->irq_wait);
285}
286
287static int register_device (int minor, struct pp_struct *pp)
288{
289 struct parport *port;
290 struct pardevice * pdev = NULL;
291 char *name;
292 int fl;
293
Julia Lawall87575432010-05-26 14:43:55 -0700294 name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (name == NULL)
296 return -ENOMEM;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 port = parport_find_number (minor);
299 if (!port) {
300 printk (KERN_WARNING "%s: no associated port!\n", name);
301 kfree (name);
302 return -ENXIO;
303 }
304
305 fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
306 pdev = parport_register_device (port, name, NULL,
307 NULL, pp_irq, fl, pp);
308 parport_put_port (port);
309
310 if (!pdev) {
311 printk (KERN_WARNING "%s: failed to register device!\n", name);
312 kfree (name);
313 return -ENXIO;
314 }
315
316 pp->pdev = pdev;
Michael Buesch81fc4012009-06-17 16:27:49 -0700317 pr_debug("%s: registered pardevice\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321static enum ieee1284_phase init_phase (int mode)
322{
323 switch (mode & ~(IEEE1284_DEVICEID
324 | IEEE1284_ADDR)) {
325 case IEEE1284_MODE_NIBBLE:
326 case IEEE1284_MODE_BYTE:
327 return IEEE1284_PH_REV_IDLE;
328 }
329 return IEEE1284_PH_FWD_IDLE;
330}
331
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800332static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
333{
334 long to_jiffies;
335
336 if ((tv_sec < 0) || (tv_usec < 0))
337 return -EINVAL;
338
339 to_jiffies = usecs_to_jiffies(tv_usec);
340 to_jiffies += tv_sec * HZ;
341 if (to_jiffies <= 0)
342 return -EINVAL;
343
344 pdev->timeout = to_jiffies;
345 return 0;
346}
347
Alan Cox6d535d32008-07-25 01:48:16 -0700348static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Al Viro496ad9a2013-01-23 17:07:38 -0500350 unsigned int minor = iminor(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct pp_struct *pp = file->private_data;
352 struct parport * port;
353 void __user *argp = (void __user *)arg;
354
355 /* First handle the cases that don't take arguments. */
356 switch (cmd) {
357 case PPCLAIM:
358 {
359 struct ieee1284_info *info;
360 int ret;
361
362 if (pp->flags & PP_CLAIMED) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700363 pr_debug(CHRDEV "%x: you've already got it!\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EINVAL;
365 }
366
367 /* Deferred device registration. */
368 if (!pp->pdev) {
369 int err = register_device (minor, pp);
370 if (err) {
371 return err;
372 }
373 }
374
375 ret = parport_claim_or_block (pp->pdev);
376 if (ret < 0)
377 return ret;
378
379 pp->flags |= PP_CLAIMED;
380
381 /* For interrupt-reporting to work, we need to be
382 * informed of each interrupt. */
383 pp_enable_irq (pp);
384
385 /* We may need to fix up the state machine. */
386 info = &pp->pdev->port->ieee1284;
387 pp->saved_state.mode = info->mode;
388 pp->saved_state.phase = info->phase;
389 info->mode = pp->state.mode;
390 info->phase = pp->state.phase;
391 pp->default_inactivity = parport_set_timeout (pp->pdev, 0);
392 parport_set_timeout (pp->pdev, pp->default_inactivity);
393
394 return 0;
395 }
396 case PPEXCL:
397 if (pp->pdev) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700398 pr_debug(CHRDEV "%x: too late for PPEXCL; "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 "already registered\n", minor);
400 if (pp->flags & PP_EXCL)
401 /* But it's not really an error. */
402 return 0;
403 /* There's no chance of making the driver happy. */
404 return -EINVAL;
405 }
406
407 /* Just remember to register the device exclusively
408 * when we finally do the registration. */
409 pp->flags |= PP_EXCL;
410 return 0;
411 case PPSETMODE:
412 {
413 int mode;
414 if (copy_from_user (&mode, argp, sizeof (mode)))
415 return -EFAULT;
416 /* FIXME: validate mode */
417 pp->state.mode = mode;
418 pp->state.phase = init_phase (mode);
419
420 if (pp->flags & PP_CLAIMED) {
421 pp->pdev->port->ieee1284.mode = mode;
422 pp->pdev->port->ieee1284.phase = pp->state.phase;
423 }
424
425 return 0;
426 }
427 case PPGETMODE:
428 {
429 int mode;
430
431 if (pp->flags & PP_CLAIMED) {
432 mode = pp->pdev->port->ieee1284.mode;
433 } else {
434 mode = pp->state.mode;
435 }
436 if (copy_to_user (argp, &mode, sizeof (mode))) {
437 return -EFAULT;
438 }
439 return 0;
440 }
441 case PPSETPHASE:
442 {
443 int phase;
444 if (copy_from_user (&phase, argp, sizeof (phase))) {
445 return -EFAULT;
446 }
447 /* FIXME: validate phase */
448 pp->state.phase = phase;
449
450 if (pp->flags & PP_CLAIMED) {
451 pp->pdev->port->ieee1284.phase = phase;
452 }
453
454 return 0;
455 }
456 case PPGETPHASE:
457 {
458 int phase;
459
460 if (pp->flags & PP_CLAIMED) {
461 phase = pp->pdev->port->ieee1284.phase;
462 } else {
463 phase = pp->state.phase;
464 }
465 if (copy_to_user (argp, &phase, sizeof (phase))) {
466 return -EFAULT;
467 }
468 return 0;
469 }
470 case PPGETMODES:
471 {
472 unsigned int modes;
473
474 port = parport_find_number (minor);
475 if (!port)
476 return -ENODEV;
477
478 modes = port->modes;
Julia Lawalld98808a2011-05-26 16:25:59 -0700479 parport_put_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (copy_to_user (argp, &modes, sizeof (modes))) {
481 return -EFAULT;
482 }
483 return 0;
484 }
485 case PPSETFLAGS:
486 {
487 int uflags;
488
489 if (copy_from_user (&uflags, argp, sizeof (uflags))) {
490 return -EFAULT;
491 }
492 pp->flags &= ~PP_FLAGMASK;
493 pp->flags |= (uflags & PP_FLAGMASK);
494 return 0;
495 }
496 case PPGETFLAGS:
497 {
498 int uflags;
499
500 uflags = pp->flags & PP_FLAGMASK;
501 if (copy_to_user (argp, &uflags, sizeof (uflags))) {
502 return -EFAULT;
503 }
504 return 0;
505 }
506 } /* end switch() */
507
508 /* Everything else requires the port to be claimed, so check
509 * that now. */
510 if ((pp->flags & PP_CLAIMED) == 0) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700511 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EINVAL;
513 }
514
515 port = pp->pdev->port;
516 switch (cmd) {
517 struct ieee1284_info *info;
518 unsigned char reg;
519 unsigned char mask;
520 int mode;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800521 s32 time32[2];
522 s64 time64[2];
523 struct timespec64 ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 case PPRSTATUS:
527 reg = parport_read_status (port);
528 if (copy_to_user (argp, &reg, sizeof (reg)))
529 return -EFAULT;
530 return 0;
531 case PPRDATA:
532 reg = parport_read_data (port);
533 if (copy_to_user (argp, &reg, sizeof (reg)))
534 return -EFAULT;
535 return 0;
536 case PPRCONTROL:
537 reg = parport_read_control (port);
538 if (copy_to_user (argp, &reg, sizeof (reg)))
539 return -EFAULT;
540 return 0;
541 case PPYIELD:
542 parport_yield_blocking (pp->pdev);
543 return 0;
544
545 case PPRELEASE:
546 /* Save the state machine's state. */
547 info = &pp->pdev->port->ieee1284;
548 pp->state.mode = info->mode;
549 pp->state.phase = info->phase;
550 info->mode = pp->saved_state.mode;
551 info->phase = pp->saved_state.phase;
552 parport_release (pp->pdev);
553 pp->flags &= ~PP_CLAIMED;
554 return 0;
555
556 case PPWCONTROL:
557 if (copy_from_user (&reg, argp, sizeof (reg)))
558 return -EFAULT;
559 parport_write_control (port, reg);
560 return 0;
561
562 case PPWDATA:
563 if (copy_from_user (&reg, argp, sizeof (reg)))
564 return -EFAULT;
565 parport_write_data (port, reg);
566 return 0;
567
568 case PPFCONTROL:
569 if (copy_from_user (&mask, argp,
570 sizeof (mask)))
571 return -EFAULT;
572 if (copy_from_user (&reg, 1 + (unsigned char __user *) arg,
573 sizeof (reg)))
574 return -EFAULT;
575 parport_frob_control (port, mask, reg);
576 return 0;
577
578 case PPDATADIR:
579 if (copy_from_user (&mode, argp, sizeof (mode)))
580 return -EFAULT;
581 if (mode)
582 port->ops->data_reverse (port);
583 else
584 port->ops->data_forward (port);
585 return 0;
586
587 case PPNEGOT:
588 if (copy_from_user (&mode, argp, sizeof (mode)))
589 return -EFAULT;
590 switch ((ret = parport_negotiate (port, mode))) {
591 case 0: break;
592 case -1: /* handshake failed, peripheral not IEEE 1284 */
593 ret = -EIO;
594 break;
595 case 1: /* handshake succeeded, peripheral rejected mode */
596 ret = -ENXIO;
597 break;
598 }
599 pp_enable_irq (pp);
600 return ret;
601
602 case PPWCTLONIRQ:
603 if (copy_from_user (&reg, argp, sizeof (reg)))
604 return -EFAULT;
605
606 /* Remember what to set the control lines to, for next
607 * time we get an interrupt. */
608 pp->irqctl = reg;
609 pp->irqresponse = 1;
610 return 0;
611
612 case PPCLRIRQ:
613 ret = atomic_read (&pp->irqc);
614 if (copy_to_user (argp, &ret, sizeof (ret)))
615 return -EFAULT;
616 atomic_sub (ret, &pp->irqc);
617 return 0;
618
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800619 case PPSETTIME32:
620 if (copy_from_user(time32, argp, sizeof(time32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -EFAULT;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800622
623 return pp_set_timeout(pp->pdev, time32[0], time32[1]);
624
625 case PPSETTIME64:
626 if (copy_from_user(time64, argp, sizeof(time64)))
627 return -EFAULT;
628
629 return pp_set_timeout(pp->pdev, time64[0], time64[1]);
630
631 case PPGETTIME32:
632 jiffies_to_timespec64(pp->pdev->timeout, &ts);
633 time32[0] = ts.tv_sec;
634 time32[1] = ts.tv_nsec / NSEC_PER_USEC;
635 if ((time32[0] < 0) || (time32[1] < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800637
638 if (copy_to_user(argp, time32, sizeof(time32)))
639 return -EFAULT;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800643 case PPGETTIME64:
644 jiffies_to_timespec64(pp->pdev->timeout, &ts);
645 time64[0] = ts.tv_sec;
646 time64[1] = ts.tv_nsec / NSEC_PER_USEC;
647 if ((time64[0] < 0) || (time64[1] < 0))
648 return -EINVAL;
649
650 if (copy_to_user(argp, time64, sizeof(time64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -EFAULT;
Bamvor Jian Zhang3b9ab372016-01-08 15:50:48 +0800652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654
655 default:
Michael Buesch81fc4012009-06-17 16:27:49 -0700656 pr_debug(CHRDEV "%x: What? (cmd=0x%x)\n", minor, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -EINVAL;
658 }
659
660 /* Keep the compiler happy */
661 return 0;
662}
663
Alan Cox6d535d32008-07-25 01:48:16 -0700664static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
665{
666 long ret;
Arnd Bergmann613655f2010-06-02 14:28:52 +0200667 mutex_lock(&pp_do_mutex);
Alan Cox6d535d32008-07-25 01:48:16 -0700668 ret = pp_do_ioctl(file, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200669 mutex_unlock(&pp_do_mutex);
Alan Cox6d535d32008-07-25 01:48:16 -0700670 return ret;
671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static int pp_open (struct inode * inode, struct file * file)
674{
675 unsigned int minor = iminor(inode);
676 struct pp_struct *pp;
677
678 if (minor >= PARPORT_MAX)
679 return -ENXIO;
680
681 pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
682 if (!pp)
683 return -ENOMEM;
684
685 pp->state.mode = IEEE1284_MODE_COMPAT;
686 pp->state.phase = init_phase (pp->state.mode);
687 pp->flags = 0;
688 pp->irqresponse = 0;
689 atomic_set (&pp->irqc, 0);
690 init_waitqueue_head (&pp->irq_wait);
691
692 /* Defer the actual device registration until the first claim.
693 * That way, we know whether or not the driver wants to have
694 * exclusive access to the port (PPEXCL).
695 */
696 pp->pdev = NULL;
697 file->private_data = pp;
698
699 return 0;
700}
701
702static int pp_release (struct inode * inode, struct file * file)
703{
704 unsigned int minor = iminor(inode);
705 struct pp_struct *pp = file->private_data;
706 int compat_negot;
707
708 compat_negot = 0;
709 if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
710 (pp->state.mode != IEEE1284_MODE_COMPAT)) {
711 struct ieee1284_info *info;
712
713 /* parport released, but not in compatibility mode */
714 parport_claim_or_block (pp->pdev);
715 pp->flags |= PP_CLAIMED;
716 info = &pp->pdev->port->ieee1284;
717 pp->saved_state.mode = info->mode;
718 pp->saved_state.phase = info->phase;
719 info->mode = pp->state.mode;
720 info->phase = pp->state.phase;
721 compat_negot = 1;
722 } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
723 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
724 compat_negot = 2;
725 }
726 if (compat_negot) {
727 parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
Michael Buesch81fc4012009-06-17 16:27:49 -0700728 pr_debug(CHRDEV "%x: negotiated back to compatibility "
729 "mode because user-space forgot\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 if (pp->flags & PP_CLAIMED) {
733 struct ieee1284_info *info;
734
735 info = &pp->pdev->port->ieee1284;
736 pp->state.mode = info->mode;
737 pp->state.phase = info->phase;
738 info->mode = pp->saved_state.mode;
739 info->phase = pp->saved_state.phase;
740 parport_release (pp->pdev);
741 if (compat_negot != 1) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700742 pr_debug(CHRDEV "%x: released pardevice "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 "because user-space forgot\n", minor);
744 }
745 }
746
747 if (pp->pdev) {
748 const char *name = pp->pdev->name;
749 parport_unregister_device (pp->pdev);
750 kfree (name);
751 pp->pdev = NULL;
Michael Buesch81fc4012009-06-17 16:27:49 -0700752 pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 kfree (pp);
756
757 return 0;
758}
759
760/* No kernel lock held - fine */
761static unsigned int pp_poll (struct file * file, poll_table * wait)
762{
763 struct pp_struct *pp = file->private_data;
764 unsigned int mask = 0;
765
766 poll_wait (file, &pp->irq_wait, wait);
767 if (atomic_read (&pp->irqc))
768 mask |= POLLIN | POLLRDNORM;
769
770 return mask;
771}
772
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800773static struct class *ppdev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Arjan van de Ven62322d22006-07-03 00:24:21 -0700775static const struct file_operations pp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .owner = THIS_MODULE,
777 .llseek = no_llseek,
778 .read = pp_read,
779 .write = pp_write,
780 .poll = pp_poll,
Alan Cox6d535d32008-07-25 01:48:16 -0700781 .unlocked_ioctl = pp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 .open = pp_open,
783 .release = pp_release,
784};
785
786static void pp_attach(struct parport *port)
787{
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700788 device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
789 NULL, "parport%d", port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792static void pp_detach(struct parport *port)
793{
Greg Kroah-Hartman04880ed2006-09-12 17:00:10 +0200794 device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797static struct parport_driver pp_driver = {
798 .name = CHRDEV,
799 .attach = pp_attach,
800 .detach = pp_detach,
801};
802
803static int __init ppdev_init (void)
804{
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -0700805 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
808 printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
809 PP_MAJOR);
810 return -EIO;
811 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800812 ppdev_class = class_create(THIS_MODULE, CHRDEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (IS_ERR(ppdev_class)) {
814 err = PTR_ERR(ppdev_class);
815 goto out_chrdev;
816 }
Alexey Khoroshilov9a32bb32012-09-02 00:31:58 +0400817 err = parport_register_driver(&pp_driver);
818 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
820 goto out_class;
821 }
822
823 printk (KERN_INFO PP_VERSION "\n");
824 goto out;
825
826out_class:
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800827 class_destroy(ppdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828out_chrdev:
829 unregister_chrdev(PP_MAJOR, CHRDEV);
830out:
831 return err;
832}
833
834static void __exit ppdev_cleanup (void)
835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Clean up all parport stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 parport_unregister_driver(&pp_driver);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800838 class_destroy(ppdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 unregister_chrdev (PP_MAJOR, CHRDEV);
840}
841
842module_init(ppdev_init);
843module_exit(ppdev_cleanup);
844
845MODULE_LICENSE("GPL");
846MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);