1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/char/ppdev.c
4 *
5 * This is the code behind /dev/parport* -- it allows a user-space
6 * application to use the parport subsystem.
7 *
8 * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
9 *
10 * A /dev/parportx device node represents an arbitrary device
11 * on port 'x'. The following operations are possible:
12 *
13 * open do nothing, set up default IEEE 1284 protocol to be COMPAT
14 * close release port and unregister device (if necessary)
15 * ioctl
16 * EXCL register device exclusively (may fail)
17 * CLAIM (register device first time) parport_claim_or_block
18 * RELEASE parport_release
19 * SETMODE set the IEEE 1284 protocol to use for read/write
20 * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be
21 * confused with ioctl(fd, SETPHASER, &stun). ;-)
22 * DATADIR data_forward / data_reverse
23 * WDATA write_data
24 * RDATA read_data
25 * WCONTROL write_control
26 * RCONTROL read_control
27 * FCONTROL frob_control
28 * RSTATUS read_status
29 * NEGOT parport_negotiate
30 * YIELD parport_yield_blocking
31 * WCTLONIRQ on interrupt, set control lines
32 * CLRIRQ clear (and return) interrupt count
33 * SETTIME sets device timeout (struct timeval)
34 * GETTIME gets device timeout (struct timeval)
35 * GETMODES gets hardware supported modes (unsigned int)
36 * GETMODE gets the current IEEE1284 mode
37 * GETPHASE gets the current IEEE1284 phase
38 * GETFLAGS gets current (user-visible) flags
39 * SETFLAGS sets current (user-visible) flags
40 * read/write read or write in current IEEE 1284 protocol
41 * select wait for interrupt (in readfds)
42 *
43 * Changes:
44 * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
45 *
46 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
47 * - On error, copy_from_user and copy_to_user do not return -EFAULT,
48 * They return the positive number of bytes *not* copied due to address
49 * space errors.
50 *
51 * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
52 * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
53 */
54
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/sched/signal.h>
58 #include <linux/device.h>
59 #include <linux/ioctl.h>
60 #include <linux/parport.h>
61 #include <linux/ctype.h>
62 #include <linux/poll.h>
63 #include <linux/slab.h>
64 #include <linux/major.h>
65 #include <linux/ppdev.h>
66 #include <linux/mutex.h>
67 #include <linux/uaccess.h>
68 #include <linux/compat.h>
69
70 #define PP_VERSION "ppdev: user-space parallel port driver"
71 #define CHRDEV "ppdev"
72
73 struct pp_struct {
74 struct pardevice *pdev;
75 wait_queue_head_t irq_wait;
76 atomic_t irqc;
77 unsigned int flags;
78 int irqresponse;
79 unsigned char irqctl;
80 struct ieee1284_info state;
81 struct ieee1284_info saved_state;
82 long default_inactivity;
83 int index;
84 };
85
86 /* should we use PARDEVICE_MAX here? */
87 static struct device *devices[PARPORT_MAX];
88
89 static DEFINE_IDA(ida_index);
90
91 /* pp_struct.flags bitfields */
92 #define PP_CLAIMED (1<<0)
93 #define PP_EXCL (1<<1)
94
95 /* Other constants */
96 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
97 #define PP_BUFFER_SIZE 1024
98 #define PARDEVICE_MAX 8
99
100 static DEFINE_MUTEX(pp_do_mutex);
101
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
pp_enable_irq(struct pp_struct * pp)108 static inline void pp_enable_irq(struct pp_struct *pp)
109 {
110 struct parport *port = pp->pdev->port;
111
112 port->ops->enable_irq(port);
113 }
114
pp_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)115 static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
116 loff_t *ppos)
117 {
118 unsigned int minor = iminor(file_inode(file));
119 struct pp_struct *pp = file->private_data;
120 char *kbuffer;
121 ssize_t bytes_read = 0;
122 struct parport *pport;
123 int mode;
124
125 if (!(pp->flags & PP_CLAIMED)) {
126 /* Don't have the port claimed */
127 pr_debug(CHRDEV "%x: claim the port first\n", minor);
128 return -EINVAL;
129 }
130
131 /* Trivial case. */
132 if (count == 0)
133 return 0;
134
135 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
136 if (!kbuffer)
137 return -ENOMEM;
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 if (pp->flags & PP_FASTREAD)
157 flags |= PARPORT_EPP_FAST;
158 if (pport->ieee1284.mode & IEEE1284_ADDR)
159 fn = pport->ops->epp_read_addr;
160 else
161 fn = pport->ops->epp_read_data;
162 bytes_read = (*fn)(pport, kbuffer, need, flags);
163 } else {
164 bytes_read = parport_read(pport, kbuffer, need);
165 }
166
167 if (bytes_read != 0)
168 break;
169
170 if (file->f_flags & O_NONBLOCK) {
171 bytes_read = -EAGAIN;
172 break;
173 }
174
175 if (signal_pending(current)) {
176 bytes_read = -ERESTARTSYS;
177 break;
178 }
179
180 cond_resched();
181 }
182
183 parport_set_timeout(pp->pdev, pp->default_inactivity);
184
185 if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
186 bytes_read = -EFAULT;
187
188 kfree(kbuffer);
189 pp_enable_irq(pp);
190 return bytes_read;
191 }
192
pp_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)193 static ssize_t pp_write(struct file *file, const char __user *buf,
194 size_t count, loff_t *ppos)
195 {
196 unsigned int minor = iminor(file_inode(file));
197 struct pp_struct *pp = file->private_data;
198 char *kbuffer;
199 ssize_t bytes_written = 0;
200 ssize_t wrote;
201 int mode;
202 struct parport *pport;
203
204 if (!(pp->flags & PP_CLAIMED)) {
205 /* Don't have the port claimed */
206 pr_debug(CHRDEV "%x: claim the port first\n", minor);
207 return -EINVAL;
208 }
209
210 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
211 if (!kbuffer)
212 return -ENOMEM;
213
214 pport = pp->pdev->port;
215 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
216
217 parport_set_timeout(pp->pdev,
218 (file->f_flags & O_NONBLOCK) ?
219 PARPORT_INACTIVITY_O_NONBLOCK :
220 pp->default_inactivity);
221
222 while (bytes_written < count) {
223 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
224
225 if (copy_from_user(kbuffer, buf + bytes_written, n)) {
226 bytes_written = -EFAULT;
227 break;
228 }
229
230 if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
231 /* do a fast EPP write */
232 if (pport->ieee1284.mode & IEEE1284_ADDR) {
233 wrote = pport->ops->epp_write_addr(pport,
234 kbuffer, n, PARPORT_EPP_FAST);
235 } else {
236 wrote = pport->ops->epp_write_data(pport,
237 kbuffer, n, PARPORT_EPP_FAST);
238 }
239 } else {
240 wrote = parport_write(pp->pdev->port, kbuffer, n);
241 }
242
243 if (wrote <= 0) {
244 if (!bytes_written)
245 bytes_written = wrote;
246 break;
247 }
248
249 bytes_written += wrote;
250
251 if (file->f_flags & O_NONBLOCK) {
252 if (!bytes_written)
253 bytes_written = -EAGAIN;
254 break;
255 }
256
257 if (signal_pending(current))
258 break;
259
260 cond_resched();
261 }
262
263 parport_set_timeout(pp->pdev, pp->default_inactivity);
264
265 kfree(kbuffer);
266 pp_enable_irq(pp);
267 return bytes_written;
268 }
269
pp_irq(void * private)270 static void pp_irq(void *private)
271 {
272 struct pp_struct *pp = private;
273
274 if (pp->irqresponse) {
275 parport_write_control(pp->pdev->port, pp->irqctl);
276 pp->irqresponse = 0;
277 }
278
279 atomic_inc(&pp->irqc);
280 wake_up_interruptible(&pp->irq_wait);
281 }
282
register_device(int minor,struct pp_struct * pp)283 static int register_device(int minor, struct pp_struct *pp)
284 {
285 struct parport *port;
286 struct pardevice *pdev = NULL;
287 char *name;
288 struct pardev_cb ppdev_cb;
289 int rc = 0, index;
290
291 name = kasprintf(GFP_KERNEL, CHRDEV "%x", minor);
292 if (name == NULL)
293 return -ENOMEM;
294
295 port = parport_find_number(minor);
296 if (!port) {
297 pr_warn("%s: no associated port!\n", name);
298 rc = -ENXIO;
299 goto err_free_name;
300 }
301
302 index = ida_alloc(&ida_index, GFP_KERNEL);
303 if (index < 0) {
304 pr_warn("%s: failed to get index!\n", name);
305 rc = index;
306 goto err_put_port;
307 }
308
309 memset(&ppdev_cb, 0, sizeof(ppdev_cb));
310 ppdev_cb.irq_func = pp_irq;
311 ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
312 ppdev_cb.private = pp;
313 pdev = parport_register_dev_model(port, name, &ppdev_cb, index);
314
315 if (!pdev) {
316 pr_warn("%s: failed to register device!\n", name);
317 rc = -ENXIO;
318 ida_free(&ida_index, index);
319 goto err_put_port;
320 }
321
322 pp->pdev = pdev;
323 pp->index = index;
324 dev_dbg(&pdev->dev, "registered pardevice\n");
325 err_put_port:
326 parport_put_port(port);
327 err_free_name:
328 kfree(name);
329 return rc;
330 }
331
init_phase(int mode)332 static enum ieee1284_phase init_phase(int mode)
333 {
334 switch (mode & ~(IEEE1284_DEVICEID
335 | IEEE1284_ADDR)) {
336 case IEEE1284_MODE_NIBBLE:
337 case IEEE1284_MODE_BYTE:
338 return IEEE1284_PH_REV_IDLE;
339 }
340 return IEEE1284_PH_FWD_IDLE;
341 }
342
pp_set_timeout(struct pardevice * pdev,long tv_sec,int tv_usec)343 static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
344 {
345 long to_jiffies;
346
347 if ((tv_sec < 0) || (tv_usec < 0))
348 return -EINVAL;
349
350 to_jiffies = usecs_to_jiffies(tv_usec);
351 to_jiffies += tv_sec * HZ;
352 if (to_jiffies <= 0)
353 return -EINVAL;
354
355 pdev->timeout = to_jiffies;
356 return 0;
357 }
358
pp_do_ioctl(struct file * file,unsigned int cmd,unsigned long arg)359 static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
360 {
361 unsigned int minor = iminor(file_inode(file));
362 struct pp_struct *pp = file->private_data;
363 struct parport *port;
364 void __user *argp = (void __user *)arg;
365 struct ieee1284_info *info;
366 unsigned char reg;
367 unsigned char mask;
368 int mode;
369 s32 time32[2];
370 s64 time64[2];
371 struct timespec64 ts;
372 int ret;
373
374 /* First handle the cases that don't take arguments. */
375 switch (cmd) {
376 case PPCLAIM:
377 {
378 if (pp->flags & PP_CLAIMED) {
379 dev_dbg(&pp->pdev->dev, "you've already got it!\n");
380 return -EINVAL;
381 }
382
383 /* Deferred device registration. */
384 if (!pp->pdev) {
385 int err = register_device(minor, pp);
386
387 if (err)
388 return err;
389 }
390
391 ret = parport_claim_or_block(pp->pdev);
392 if (ret < 0)
393 return ret;
394
395 pp->flags |= PP_CLAIMED;
396
397 /* For interrupt-reporting to work, we need to be
398 * informed of each interrupt. */
399 pp_enable_irq(pp);
400
401 /* We may need to fix up the state machine. */
402 info = &pp->pdev->port->ieee1284;
403 pp->saved_state.mode = info->mode;
404 pp->saved_state.phase = info->phase;
405 info->mode = pp->state.mode;
406 info->phase = pp->state.phase;
407 pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
408 parport_set_timeout(pp->pdev, pp->default_inactivity);
409
410 return 0;
411 }
412 case PPEXCL:
413 if (pp->pdev) {
414 dev_dbg(&pp->pdev->dev,
415 "too late for PPEXCL; already registered\n");
416 if (pp->flags & PP_EXCL)
417 /* But it's not really an error. */
418 return 0;
419 /* There's no chance of making the driver happy. */
420 return -EINVAL;
421 }
422
423 /* Just remember to register the device exclusively
424 * when we finally do the registration. */
425 pp->flags |= PP_EXCL;
426 return 0;
427 case PPSETMODE:
428 {
429 int mode;
430
431 if (copy_from_user(&mode, argp, sizeof(mode)))
432 return -EFAULT;
433 /* FIXME: validate mode */
434 pp->state.mode = mode;
435 pp->state.phase = init_phase(mode);
436
437 if (pp->flags & PP_CLAIMED) {
438 pp->pdev->port->ieee1284.mode = mode;
439 pp->pdev->port->ieee1284.phase = pp->state.phase;
440 }
441
442 return 0;
443 }
444 case PPGETMODE:
445 {
446 int mode;
447
448 if (pp->flags & PP_CLAIMED)
449 mode = pp->pdev->port->ieee1284.mode;
450 else
451 mode = pp->state.mode;
452
453 if (copy_to_user(argp, &mode, sizeof(mode)))
454 return -EFAULT;
455 return 0;
456 }
457 case PPSETPHASE:
458 {
459 int phase;
460
461 if (copy_from_user(&phase, argp, sizeof(phase)))
462 return -EFAULT;
463
464 /* FIXME: validate phase */
465 pp->state.phase = phase;
466
467 if (pp->flags & PP_CLAIMED)
468 pp->pdev->port->ieee1284.phase = phase;
469
470 return 0;
471 }
472 case PPGETPHASE:
473 {
474 int phase;
475
476 if (pp->flags & PP_CLAIMED)
477 phase = pp->pdev->port->ieee1284.phase;
478 else
479 phase = pp->state.phase;
480 if (copy_to_user(argp, &phase, sizeof(phase)))
481 return -EFAULT;
482 return 0;
483 }
484 case PPGETMODES:
485 {
486 unsigned int modes;
487
488 port = parport_find_number(minor);
489 if (!port)
490 return -ENODEV;
491
492 modes = port->modes;
493 parport_put_port(port);
494 if (copy_to_user(argp, &modes, sizeof(modes)))
495 return -EFAULT;
496 return 0;
497 }
498 case PPSETFLAGS:
499 {
500 int uflags;
501
502 if (copy_from_user(&uflags, argp, sizeof(uflags)))
503 return -EFAULT;
504 pp->flags &= ~PP_FLAGMASK;
505 pp->flags |= (uflags & PP_FLAGMASK);
506 return 0;
507 }
508 case PPGETFLAGS:
509 {
510 int uflags;
511
512 uflags = pp->flags & PP_FLAGMASK;
513 if (copy_to_user(argp, &uflags, sizeof(uflags)))
514 return -EFAULT;
515 return 0;
516 }
517 } /* end switch() */
518
519 /* Everything else requires the port to be claimed, so check
520 * that now. */
521 if ((pp->flags & PP_CLAIMED) == 0) {
522 pr_debug(CHRDEV "%x: claim the port first\n", minor);
523 return -EINVAL;
524 }
525
526 port = pp->pdev->port;
527 switch (cmd) {
528 case PPRSTATUS:
529 reg = parport_read_status(port);
530 if (copy_to_user(argp, ®, sizeof(reg)))
531 return -EFAULT;
532 return 0;
533 case PPRDATA:
534 reg = parport_read_data(port);
535 if (copy_to_user(argp, ®, sizeof(reg)))
536 return -EFAULT;
537 return 0;
538 case PPRCONTROL:
539 reg = parport_read_control(port);
540 if (copy_to_user(argp, ®, sizeof(reg)))
541 return -EFAULT;
542 return 0;
543 case PPYIELD:
544 parport_yield_blocking(pp->pdev);
545 return 0;
546
547 case PPRELEASE:
548 /* Save the state machine's state. */
549 info = &pp->pdev->port->ieee1284;
550 pp->state.mode = info->mode;
551 pp->state.phase = info->phase;
552 info->mode = pp->saved_state.mode;
553 info->phase = pp->saved_state.phase;
554 parport_release(pp->pdev);
555 pp->flags &= ~PP_CLAIMED;
556 return 0;
557
558 case PPWCONTROL:
559 if (copy_from_user(®, argp, sizeof(reg)))
560 return -EFAULT;
561 parport_write_control(port, reg);
562 return 0;
563
564 case PPWDATA:
565 if (copy_from_user(®, argp, sizeof(reg)))
566 return -EFAULT;
567 parport_write_data(port, reg);
568 return 0;
569
570 case PPFCONTROL:
571 if (copy_from_user(&mask, argp,
572 sizeof(mask)))
573 return -EFAULT;
574 if (copy_from_user(®, 1 + (unsigned char __user *) arg,
575 sizeof(reg)))
576 return -EFAULT;
577 parport_frob_control(port, mask, reg);
578 return 0;
579
580 case PPDATADIR:
581 if (copy_from_user(&mode, argp, sizeof(mode)))
582 return -EFAULT;
583 if (mode)
584 port->ops->data_reverse(port);
585 else
586 port->ops->data_forward(port);
587 return 0;
588
589 case PPNEGOT:
590 if (copy_from_user(&mode, argp, sizeof(mode)))
591 return -EFAULT;
592 switch ((ret = parport_negotiate(port, mode))) {
593 case 0: break;
594 case -1: /* handshake failed, peripheral not IEEE 1284 */
595 ret = -EIO;
596 break;
597 case 1: /* handshake succeeded, peripheral rejected mode */
598 ret = -ENXIO;
599 break;
600 }
601 pp_enable_irq(pp);
602 return ret;
603
604 case PPWCTLONIRQ:
605 if (copy_from_user(®, argp, sizeof(reg)))
606 return -EFAULT;
607
608 /* Remember what to set the control lines to, for next
609 * time we get an interrupt. */
610 pp->irqctl = reg;
611 pp->irqresponse = 1;
612 return 0;
613
614 case PPCLRIRQ:
615 ret = atomic_read(&pp->irqc);
616 if (copy_to_user(argp, &ret, sizeof(ret)))
617 return -EFAULT;
618 atomic_sub(ret, &pp->irqc);
619 return 0;
620
621 case PPSETTIME32:
622 if (copy_from_user(time32, argp, sizeof(time32)))
623 return -EFAULT;
624
625 if ((time32[0] < 0) || (time32[1] < 0))
626 return -EINVAL;
627
628 return pp_set_timeout(pp->pdev, time32[0], time32[1]);
629
630 case PPSETTIME64:
631 if (copy_from_user(time64, argp, sizeof(time64)))
632 return -EFAULT;
633
634 if ((time64[0] < 0) || (time64[1] < 0))
635 return -EINVAL;
636
637 if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
638 time64[1] >>= 32;
639
640 return pp_set_timeout(pp->pdev, time64[0], time64[1]);
641
642 case PPGETTIME32:
643 jiffies_to_timespec64(pp->pdev->timeout, &ts);
644 time32[0] = ts.tv_sec;
645 time32[1] = ts.tv_nsec / NSEC_PER_USEC;
646
647 if (copy_to_user(argp, time32, sizeof(time32)))
648 return -EFAULT;
649
650 return 0;
651
652 case PPGETTIME64:
653 jiffies_to_timespec64(pp->pdev->timeout, &ts);
654 time64[0] = ts.tv_sec;
655 time64[1] = ts.tv_nsec / NSEC_PER_USEC;
656
657 if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
658 time64[1] <<= 32;
659
660 if (copy_to_user(argp, time64, sizeof(time64)))
661 return -EFAULT;
662
663 return 0;
664
665 default:
666 dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
667 return -EINVAL;
668 }
669
670 /* Keep the compiler happy */
671 return 0;
672 }
673
pp_ioctl(struct file * file,unsigned int cmd,unsigned long arg)674 static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
675 {
676 long ret;
677
678 mutex_lock(&pp_do_mutex);
679 ret = pp_do_ioctl(file, cmd, arg);
680 mutex_unlock(&pp_do_mutex);
681 return ret;
682 }
683
pp_open(struct inode * inode,struct file * file)684 static int pp_open(struct inode *inode, struct file *file)
685 {
686 unsigned int minor = iminor(inode);
687 struct pp_struct *pp;
688
689 if (minor >= PARPORT_MAX)
690 return -ENXIO;
691
692 pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
693 if (!pp)
694 return -ENOMEM;
695
696 pp->state.mode = IEEE1284_MODE_COMPAT;
697 pp->state.phase = init_phase(pp->state.mode);
698 pp->flags = 0;
699 pp->irqresponse = 0;
700 atomic_set(&pp->irqc, 0);
701 init_waitqueue_head(&pp->irq_wait);
702
703 /* Defer the actual device registration until the first claim.
704 * That way, we know whether or not the driver wants to have
705 * exclusive access to the port (PPEXCL).
706 */
707 pp->pdev = NULL;
708 file->private_data = pp;
709
710 return 0;
711 }
712
pp_release(struct inode * inode,struct file * file)713 static int pp_release(struct inode *inode, struct file *file)
714 {
715 unsigned int minor = iminor(inode);
716 struct pp_struct *pp = file->private_data;
717 int compat_negot;
718
719 compat_negot = 0;
720 if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
721 (pp->state.mode != IEEE1284_MODE_COMPAT)) {
722 struct ieee1284_info *info;
723
724 /* parport released, but not in compatibility mode */
725 parport_claim_or_block(pp->pdev);
726 pp->flags |= PP_CLAIMED;
727 info = &pp->pdev->port->ieee1284;
728 pp->saved_state.mode = info->mode;
729 pp->saved_state.phase = info->phase;
730 info->mode = pp->state.mode;
731 info->phase = pp->state.phase;
732 compat_negot = 1;
733 } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
734 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
735 compat_negot = 2;
736 }
737 if (compat_negot) {
738 parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
739 dev_dbg(&pp->pdev->dev,
740 "negotiated back to compatibility mode because user-space forgot\n");
741 }
742
743 if ((pp->flags & PP_CLAIMED) && pp->pdev) {
744 struct ieee1284_info *info;
745
746 info = &pp->pdev->port->ieee1284;
747 pp->state.mode = info->mode;
748 pp->state.phase = info->phase;
749 info->mode = pp->saved_state.mode;
750 info->phase = pp->saved_state.phase;
751 parport_release(pp->pdev);
752 if (compat_negot != 1) {
753 pr_debug(CHRDEV "%x: released pardevice "
754 "because user-space forgot\n", minor);
755 }
756 }
757
758 if (pp->pdev) {
759 parport_unregister_device(pp->pdev);
760 ida_free(&ida_index, pp->index);
761 pp->pdev = NULL;
762 pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
763 }
764
765 kfree(pp);
766
767 return 0;
768 }
769
770 /* No kernel lock held - fine */
pp_poll(struct file * file,poll_table * wait)771 static __poll_t pp_poll(struct file *file, poll_table *wait)
772 {
773 struct pp_struct *pp = file->private_data;
774 __poll_t mask = 0;
775
776 poll_wait(file, &pp->irq_wait, wait);
777 if (atomic_read(&pp->irqc))
778 mask |= EPOLLIN | EPOLLRDNORM;
779
780 return mask;
781 }
782
783 static const struct class ppdev_class = {
784 .name = CHRDEV,
785 };
786
787 static const struct file_operations pp_fops = {
788 .owner = THIS_MODULE,
789 .read = pp_read,
790 .write = pp_write,
791 .poll = pp_poll,
792 .unlocked_ioctl = pp_ioctl,
793 .compat_ioctl = compat_ptr_ioctl,
794 .open = pp_open,
795 .release = pp_release,
796 };
797
pp_attach(struct parport * port)798 static void pp_attach(struct parport *port)
799 {
800 struct device *ret;
801
802 if (devices[port->number])
803 return;
804
805 ret = device_create(&ppdev_class, port->dev,
806 MKDEV(PP_MAJOR, port->number), NULL,
807 "parport%d", port->number);
808 if (IS_ERR(ret)) {
809 pr_err("Failed to create device parport%d\n",
810 port->number);
811 return;
812 }
813 devices[port->number] = ret;
814 }
815
pp_detach(struct parport * port)816 static void pp_detach(struct parport *port)
817 {
818 if (!devices[port->number])
819 return;
820
821 device_destroy(&ppdev_class, MKDEV(PP_MAJOR, port->number));
822 devices[port->number] = NULL;
823 }
824
pp_probe(struct pardevice * par_dev)825 static int pp_probe(struct pardevice *par_dev)
826 {
827 struct device_driver *drv = par_dev->dev.driver;
828 int len = strlen(drv->name);
829
830 if (strncmp(par_dev->name, drv->name, len))
831 return -ENODEV;
832
833 return 0;
834 }
835
836 static struct parport_driver pp_driver = {
837 .name = CHRDEV,
838 .probe = pp_probe,
839 .match_port = pp_attach,
840 .detach = pp_detach,
841 };
842
ppdev_init(void)843 static int __init ppdev_init(void)
844 {
845 int err = 0;
846
847 if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
848 pr_warn(CHRDEV ": unable to get major %d\n", PP_MAJOR);
849 return -EIO;
850 }
851 err = class_register(&ppdev_class);
852 if (err)
853 goto out_chrdev;
854
855 err = parport_register_driver(&pp_driver);
856 if (err < 0) {
857 pr_warn(CHRDEV ": unable to register with parport\n");
858 goto out_class;
859 }
860
861 pr_info(PP_VERSION "\n");
862 goto out;
863
864 out_class:
865 class_unregister(&ppdev_class);
866 out_chrdev:
867 unregister_chrdev(PP_MAJOR, CHRDEV);
868 out:
869 return err;
870 }
871
ppdev_cleanup(void)872 static void __exit ppdev_cleanup(void)
873 {
874 /* Clean up all parport stuff */
875 parport_unregister_driver(&pp_driver);
876 class_unregister(&ppdev_class);
877 unregister_chrdev(PP_MAJOR, CHRDEV);
878 }
879
880 module_init(ppdev_init);
881 module_exit(ppdev_cleanup);
882
883 MODULE_DESCRIPTION("Support for user-space parallel port device drivers");
884 MODULE_LICENSE("GPL");
885 MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);
886