]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/char/ppdev.c
Merge remote-tracking branches 'asoc/topic/inntel', 'asoc/topic/input', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / drivers / char / ppdev.c
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
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/sched.h>
62 #include <linux/device.h>
63 #include <linux/ioctl.h>
64 #include <linux/parport.h>
65 #include <linux/ctype.h>
66 #include <linux/poll.h>
67 #include <linux/slab.h>
68 #include <linux/major.h>
69 #include <linux/ppdev.h>
70 #include <linux/mutex.h>
71 #include <linux/uaccess.h>
72 #include <linux/compat.h>
73
74 #define PP_VERSION "ppdev: user-space parallel port driver"
75 #define CHRDEV "ppdev"
76
77 struct pp_struct {
78 struct pardevice *pdev;
79 wait_queue_head_t irq_wait;
80 atomic_t irqc;
81 unsigned int flags;
82 int irqresponse;
83 unsigned char irqctl;
84 struct ieee1284_info state;
85 struct ieee1284_info saved_state;
86 long default_inactivity;
87 };
88
89 /* pp_struct.flags bitfields */
90 #define PP_CLAIMED (1<<0)
91 #define PP_EXCL (1<<1)
92
93 /* Other constants */
94 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
95 #define PP_BUFFER_SIZE 1024
96 #define PARDEVICE_MAX 8
97
98 /* ROUND_UP macro from fs/select.c */
99 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
100
101 static DEFINE_MUTEX(pp_do_mutex);
102
103 /* define fixed sized ioctl cmd for y2038 migration */
104 #define PPGETTIME32 _IOR(PP_IOCTL, 0x95, s32[2])
105 #define PPSETTIME32 _IOW(PP_IOCTL, 0x96, s32[2])
106 #define PPGETTIME64 _IOR(PP_IOCTL, 0x95, s64[2])
107 #define PPSETTIME64 _IOW(PP_IOCTL, 0x96, s64[2])
108
109 static inline void pp_enable_irq(struct pp_struct *pp)
110 {
111 struct parport *port = pp->pdev->port;
112
113 port->ops->enable_irq(port);
114 }
115
116 static ssize_t pp_read(struct file *file, char __user *buf, size_t count,
117 loff_t *ppos)
118 {
119 unsigned int minor = iminor(file_inode(file));
120 struct pp_struct *pp = file->private_data;
121 char *kbuffer;
122 ssize_t bytes_read = 0;
123 struct parport *pport;
124 int mode;
125
126 if (!(pp->flags & PP_CLAIMED)) {
127 /* Don't have the port claimed */
128 pr_debug(CHRDEV "%x: claim the port first\n", minor);
129 return -EINVAL;
130 }
131
132 /* Trivial case. */
133 if (count == 0)
134 return 0;
135
136 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
137 if (!kbuffer)
138 return -ENOMEM;
139 pport = pp->pdev->port;
140 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
141
142 parport_set_timeout(pp->pdev,
143 (file->f_flags & O_NONBLOCK) ?
144 PARPORT_INACTIVITY_O_NONBLOCK :
145 pp->default_inactivity);
146
147 while (bytes_read == 0) {
148 ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
149
150 if (mode == IEEE1284_MODE_EPP) {
151 /* various specials for EPP mode */
152 int flags = 0;
153 size_t (*fn)(struct parport *, void *, size_t, int);
154
155 if (pp->flags & PP_W91284PIC)
156 flags |= PARPORT_W91284PIC;
157 if (pp->flags & PP_FASTREAD)
158 flags |= PARPORT_EPP_FAST;
159 if (pport->ieee1284.mode & IEEE1284_ADDR)
160 fn = pport->ops->epp_read_addr;
161 else
162 fn = pport->ops->epp_read_data;
163 bytes_read = (*fn)(pport, kbuffer, need, flags);
164 } else {
165 bytes_read = parport_read(pport, kbuffer, need);
166 }
167
168 if (bytes_read != 0)
169 break;
170
171 if (file->f_flags & O_NONBLOCK) {
172 bytes_read = -EAGAIN;
173 break;
174 }
175
176 if (signal_pending(current)) {
177 bytes_read = -ERESTARTSYS;
178 break;
179 }
180
181 cond_resched();
182 }
183
184 parport_set_timeout(pp->pdev, pp->default_inactivity);
185
186 if (bytes_read > 0 && copy_to_user(buf, kbuffer, bytes_read))
187 bytes_read = -EFAULT;
188
189 kfree(kbuffer);
190 pp_enable_irq(pp);
191 return bytes_read;
192 }
193
194 static ssize_t pp_write(struct file *file, const char __user *buf,
195 size_t count, loff_t *ppos)
196 {
197 unsigned int minor = iminor(file_inode(file));
198 struct pp_struct *pp = file->private_data;
199 char *kbuffer;
200 ssize_t bytes_written = 0;
201 ssize_t wrote;
202 int mode;
203 struct parport *pport;
204
205 if (!(pp->flags & PP_CLAIMED)) {
206 /* Don't have the port claimed */
207 pr_debug(CHRDEV "%x: claim the port first\n", minor);
208 return -EINVAL;
209 }
210
211 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
212 if (!kbuffer)
213 return -ENOMEM;
214
215 pport = pp->pdev->port;
216 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
217
218 parport_set_timeout(pp->pdev,
219 (file->f_flags & O_NONBLOCK) ?
220 PARPORT_INACTIVITY_O_NONBLOCK :
221 pp->default_inactivity);
222
223 while (bytes_written < count) {
224 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
225
226 if (copy_from_user(kbuffer, buf + bytes_written, n)) {
227 bytes_written = -EFAULT;
228 break;
229 }
230
231 if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
232 /* do a fast EPP write */
233 if (pport->ieee1284.mode & IEEE1284_ADDR) {
234 wrote = pport->ops->epp_write_addr(pport,
235 kbuffer, n, PARPORT_EPP_FAST);
236 } else {
237 wrote = pport->ops->epp_write_data(pport,
238 kbuffer, n, PARPORT_EPP_FAST);
239 }
240 } else {
241 wrote = parport_write(pp->pdev->port, kbuffer, n);
242 }
243
244 if (wrote <= 0) {
245 if (!bytes_written)
246 bytes_written = wrote;
247 break;
248 }
249
250 bytes_written += wrote;
251
252 if (file->f_flags & O_NONBLOCK) {
253 if (!bytes_written)
254 bytes_written = -EAGAIN;
255 break;
256 }
257
258 if (signal_pending(current))
259 break;
260
261 cond_resched();
262 }
263
264 parport_set_timeout(pp->pdev, pp->default_inactivity);
265
266 kfree(kbuffer);
267 pp_enable_irq(pp);
268 return bytes_written;
269 }
270
271 static void pp_irq(void *private)
272 {
273 struct pp_struct *pp = private;
274
275 if (pp->irqresponse) {
276 parport_write_control(pp->pdev->port, pp->irqctl);
277 pp->irqresponse = 0;
278 }
279
280 atomic_inc(&pp->irqc);
281 wake_up_interruptible(&pp->irq_wait);
282 }
283
284 static int register_device(int minor, struct pp_struct *pp)
285 {
286 struct parport *port;
287 struct pardevice *pdev = NULL;
288 char *name;
289 struct pardev_cb ppdev_cb;
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 printk(KERN_WARNING "%s: no associated port!\n", name);
298 kfree(name);
299 return -ENXIO;
300 }
301
302 memset(&ppdev_cb, 0, sizeof(ppdev_cb));
303 ppdev_cb.irq_func = pp_irq;
304 ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
305 ppdev_cb.private = pp;
306 pdev = parport_register_dev_model(port, name, &ppdev_cb, minor);
307 parport_put_port(port);
308
309 if (!pdev) {
310 printk(KERN_WARNING "%s: failed to register device!\n", name);
311 kfree(name);
312 return -ENXIO;
313 }
314
315 pp->pdev = pdev;
316 dev_dbg(&pdev->dev, "registered pardevice\n");
317 return 0;
318 }
319
320 static enum ieee1284_phase init_phase(int mode)
321 {
322 switch (mode & ~(IEEE1284_DEVICEID
323 | IEEE1284_ADDR)) {
324 case IEEE1284_MODE_NIBBLE:
325 case IEEE1284_MODE_BYTE:
326 return IEEE1284_PH_REV_IDLE;
327 }
328 return IEEE1284_PH_FWD_IDLE;
329 }
330
331 static int pp_set_timeout(struct pardevice *pdev, long tv_sec, int tv_usec)
332 {
333 long to_jiffies;
334
335 if ((tv_sec < 0) || (tv_usec < 0))
336 return -EINVAL;
337
338 to_jiffies = usecs_to_jiffies(tv_usec);
339 to_jiffies += tv_sec * HZ;
340 if (to_jiffies <= 0)
341 return -EINVAL;
342
343 pdev->timeout = to_jiffies;
344 return 0;
345 }
346
347 static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
348 {
349 unsigned int minor = iminor(file_inode(file));
350 struct pp_struct *pp = file->private_data;
351 struct parport *port;
352 void __user *argp = (void __user *)arg;
353
354 /* First handle the cases that don't take arguments. */
355 switch (cmd) {
356 case PPCLAIM:
357 {
358 struct ieee1284_info *info;
359 int ret;
360
361 if (pp->flags & PP_CLAIMED) {
362 dev_dbg(&pp->pdev->dev, "you've already got it!\n");
363 return -EINVAL;
364 }
365
366 /* Deferred device registration. */
367 if (!pp->pdev) {
368 int err = register_device(minor, pp);
369
370 if (err)
371 return err;
372 }
373
374 ret = parport_claim_or_block(pp->pdev);
375 if (ret < 0)
376 return ret;
377
378 pp->flags |= PP_CLAIMED;
379
380 /* For interrupt-reporting to work, we need to be
381 * informed of each interrupt. */
382 pp_enable_irq(pp);
383
384 /* We may need to fix up the state machine. */
385 info = &pp->pdev->port->ieee1284;
386 pp->saved_state.mode = info->mode;
387 pp->saved_state.phase = info->phase;
388 info->mode = pp->state.mode;
389 info->phase = pp->state.phase;
390 pp->default_inactivity = parport_set_timeout(pp->pdev, 0);
391 parport_set_timeout(pp->pdev, pp->default_inactivity);
392
393 return 0;
394 }
395 case PPEXCL:
396 if (pp->pdev) {
397 dev_dbg(&pp->pdev->dev,
398 "too late for PPEXCL; already registered\n");
399 if (pp->flags & PP_EXCL)
400 /* But it's not really an error. */
401 return 0;
402 /* There's no chance of making the driver happy. */
403 return -EINVAL;
404 }
405
406 /* Just remember to register the device exclusively
407 * when we finally do the registration. */
408 pp->flags |= PP_EXCL;
409 return 0;
410 case PPSETMODE:
411 {
412 int mode;
413
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 return 0;
439 }
440 case PPSETPHASE:
441 {
442 int phase;
443
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 return 0;
454 }
455 case PPGETPHASE:
456 {
457 int phase;
458
459 if (pp->flags & PP_CLAIMED)
460 phase = pp->pdev->port->ieee1284.phase;
461 else
462 phase = pp->state.phase;
463 if (copy_to_user(argp, &phase, sizeof(phase)))
464 return -EFAULT;
465 return 0;
466 }
467 case PPGETMODES:
468 {
469 unsigned int modes;
470
471 port = parport_find_number(minor);
472 if (!port)
473 return -ENODEV;
474
475 modes = port->modes;
476 parport_put_port(port);
477 if (copy_to_user(argp, &modes, sizeof(modes)))
478 return -EFAULT;
479 return 0;
480 }
481 case PPSETFLAGS:
482 {
483 int uflags;
484
485 if (copy_from_user(&uflags, argp, sizeof(uflags)))
486 return -EFAULT;
487 pp->flags &= ~PP_FLAGMASK;
488 pp->flags |= (uflags & PP_FLAGMASK);
489 return 0;
490 }
491 case PPGETFLAGS:
492 {
493 int uflags;
494
495 uflags = pp->flags & PP_FLAGMASK;
496 if (copy_to_user(argp, &uflags, sizeof(uflags)))
497 return -EFAULT;
498 return 0;
499 }
500 } /* end switch() */
501
502 /* Everything else requires the port to be claimed, so check
503 * that now. */
504 if ((pp->flags & PP_CLAIMED) == 0) {
505 pr_debug(CHRDEV "%x: claim the port first\n", minor);
506 return -EINVAL;
507 }
508
509 port = pp->pdev->port;
510 switch (cmd) {
511 struct ieee1284_info *info;
512 unsigned char reg;
513 unsigned char mask;
514 int mode;
515 s32 time32[2];
516 s64 time64[2];
517 struct timespec64 ts;
518 int ret;
519
520 case PPRSTATUS:
521 reg = parport_read_status(port);
522 if (copy_to_user(argp, &reg, sizeof(reg)))
523 return -EFAULT;
524 return 0;
525 case PPRDATA:
526 reg = parport_read_data(port);
527 if (copy_to_user(argp, &reg, sizeof(reg)))
528 return -EFAULT;
529 return 0;
530 case PPRCONTROL:
531 reg = parport_read_control(port);
532 if (copy_to_user(argp, &reg, sizeof(reg)))
533 return -EFAULT;
534 return 0;
535 case PPYIELD:
536 parport_yield_blocking(pp->pdev);
537 return 0;
538
539 case PPRELEASE:
540 /* Save the state machine's state. */
541 info = &pp->pdev->port->ieee1284;
542 pp->state.mode = info->mode;
543 pp->state.phase = info->phase;
544 info->mode = pp->saved_state.mode;
545 info->phase = pp->saved_state.phase;
546 parport_release(pp->pdev);
547 pp->flags &= ~PP_CLAIMED;
548 return 0;
549
550 case PPWCONTROL:
551 if (copy_from_user(&reg, argp, sizeof(reg)))
552 return -EFAULT;
553 parport_write_control(port, reg);
554 return 0;
555
556 case PPWDATA:
557 if (copy_from_user(&reg, argp, sizeof(reg)))
558 return -EFAULT;
559 parport_write_data(port, reg);
560 return 0;
561
562 case PPFCONTROL:
563 if (copy_from_user(&mask, argp,
564 sizeof(mask)))
565 return -EFAULT;
566 if (copy_from_user(&reg, 1 + (unsigned char __user *) arg,
567 sizeof(reg)))
568 return -EFAULT;
569 parport_frob_control(port, mask, reg);
570 return 0;
571
572 case PPDATADIR:
573 if (copy_from_user(&mode, argp, sizeof(mode)))
574 return -EFAULT;
575 if (mode)
576 port->ops->data_reverse(port);
577 else
578 port->ops->data_forward(port);
579 return 0;
580
581 case PPNEGOT:
582 if (copy_from_user(&mode, argp, sizeof(mode)))
583 return -EFAULT;
584 switch ((ret = parport_negotiate(port, mode))) {
585 case 0: break;
586 case -1: /* handshake failed, peripheral not IEEE 1284 */
587 ret = -EIO;
588 break;
589 case 1: /* handshake succeeded, peripheral rejected mode */
590 ret = -ENXIO;
591 break;
592 }
593 pp_enable_irq(pp);
594 return ret;
595
596 case PPWCTLONIRQ:
597 if (copy_from_user(&reg, argp, sizeof(reg)))
598 return -EFAULT;
599
600 /* Remember what to set the control lines to, for next
601 * time we get an interrupt. */
602 pp->irqctl = reg;
603 pp->irqresponse = 1;
604 return 0;
605
606 case PPCLRIRQ:
607 ret = atomic_read(&pp->irqc);
608 if (copy_to_user(argp, &ret, sizeof(ret)))
609 return -EFAULT;
610 atomic_sub(ret, &pp->irqc);
611 return 0;
612
613 case PPSETTIME32:
614 if (copy_from_user(time32, argp, sizeof(time32)))
615 return -EFAULT;
616
617 return pp_set_timeout(pp->pdev, time32[0], time32[1]);
618
619 case PPSETTIME64:
620 if (copy_from_user(time64, argp, sizeof(time64)))
621 return -EFAULT;
622
623 return pp_set_timeout(pp->pdev, time64[0], time64[1]);
624
625 case PPGETTIME32:
626 jiffies_to_timespec64(pp->pdev->timeout, &ts);
627 time32[0] = ts.tv_sec;
628 time32[1] = ts.tv_nsec / NSEC_PER_USEC;
629 if ((time32[0] < 0) || (time32[1] < 0))
630 return -EINVAL;
631
632 if (copy_to_user(argp, time32, sizeof(time32)))
633 return -EFAULT;
634
635 return 0;
636
637 case PPGETTIME64:
638 jiffies_to_timespec64(pp->pdev->timeout, &ts);
639 time64[0] = ts.tv_sec;
640 time64[1] = ts.tv_nsec / NSEC_PER_USEC;
641 if ((time64[0] < 0) || (time64[1] < 0))
642 return -EINVAL;
643
644 if (copy_to_user(argp, time64, sizeof(time64)))
645 return -EFAULT;
646
647 return 0;
648
649 default:
650 dev_dbg(&pp->pdev->dev, "What? (cmd=0x%x)\n", cmd);
651 return -EINVAL;
652 }
653
654 /* Keep the compiler happy */
655 return 0;
656 }
657
658 static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
659 {
660 long ret;
661
662 mutex_lock(&pp_do_mutex);
663 ret = pp_do_ioctl(file, cmd, arg);
664 mutex_unlock(&pp_do_mutex);
665 return ret;
666 }
667
668 #ifdef CONFIG_COMPAT
669 static long pp_compat_ioctl(struct file *file, unsigned int cmd,
670 unsigned long arg)
671 {
672 return pp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
673 }
674 #endif
675
676 static int pp_open(struct inode *inode, struct file *file)
677 {
678 unsigned int minor = iminor(inode);
679 struct pp_struct *pp;
680
681 if (minor >= PARPORT_MAX)
682 return -ENXIO;
683
684 pp = kmalloc(sizeof(struct pp_struct), GFP_KERNEL);
685 if (!pp)
686 return -ENOMEM;
687
688 pp->state.mode = IEEE1284_MODE_COMPAT;
689 pp->state.phase = init_phase(pp->state.mode);
690 pp->flags = 0;
691 pp->irqresponse = 0;
692 atomic_set(&pp->irqc, 0);
693 init_waitqueue_head(&pp->irq_wait);
694
695 /* Defer the actual device registration until the first claim.
696 * That way, we know whether or not the driver wants to have
697 * exclusive access to the port (PPEXCL).
698 */
699 pp->pdev = NULL;
700 file->private_data = pp;
701
702 return 0;
703 }
704
705 static int pp_release(struct inode *inode, struct file *file)
706 {
707 unsigned int minor = iminor(inode);
708 struct pp_struct *pp = file->private_data;
709 int compat_negot;
710
711 compat_negot = 0;
712 if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
713 (pp->state.mode != IEEE1284_MODE_COMPAT)) {
714 struct ieee1284_info *info;
715
716 /* parport released, but not in compatibility mode */
717 parport_claim_or_block(pp->pdev);
718 pp->flags |= PP_CLAIMED;
719 info = &pp->pdev->port->ieee1284;
720 pp->saved_state.mode = info->mode;
721 pp->saved_state.phase = info->phase;
722 info->mode = pp->state.mode;
723 info->phase = pp->state.phase;
724 compat_negot = 1;
725 } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
726 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
727 compat_negot = 2;
728 }
729 if (compat_negot) {
730 parport_negotiate(pp->pdev->port, IEEE1284_MODE_COMPAT);
731 dev_dbg(&pp->pdev->dev,
732 "negotiated back to compatibility mode because user-space forgot\n");
733 }
734
735 if (pp->flags & PP_CLAIMED) {
736 struct ieee1284_info *info;
737
738 info = &pp->pdev->port->ieee1284;
739 pp->state.mode = info->mode;
740 pp->state.phase = info->phase;
741 info->mode = pp->saved_state.mode;
742 info->phase = pp->saved_state.phase;
743 parport_release(pp->pdev);
744 if (compat_negot != 1) {
745 pr_debug(CHRDEV "%x: released pardevice "
746 "because user-space forgot\n", minor);
747 }
748 }
749
750 if (pp->pdev) {
751 parport_unregister_device(pp->pdev);
752 pp->pdev = NULL;
753 pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
754 }
755
756 kfree(pp);
757
758 return 0;
759 }
760
761 /* No kernel lock held - fine */
762 static unsigned int pp_poll(struct file *file, poll_table *wait)
763 {
764 struct pp_struct *pp = file->private_data;
765 unsigned int mask = 0;
766
767 poll_wait(file, &pp->irq_wait, wait);
768 if (atomic_read(&pp->irqc))
769 mask |= POLLIN | POLLRDNORM;
770
771 return mask;
772 }
773
774 static struct class *ppdev_class;
775
776 static const struct file_operations pp_fops = {
777 .owner = THIS_MODULE,
778 .llseek = no_llseek,
779 .read = pp_read,
780 .write = pp_write,
781 .poll = pp_poll,
782 .unlocked_ioctl = pp_ioctl,
783 #ifdef CONFIG_COMPAT
784 .compat_ioctl = pp_compat_ioctl,
785 #endif
786 .open = pp_open,
787 .release = pp_release,
788 };
789
790 static void pp_attach(struct parport *port)
791 {
792 device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
793 NULL, "parport%d", port->number);
794 }
795
796 static void pp_detach(struct parport *port)
797 {
798 device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
799 }
800
801 static int pp_probe(struct pardevice *par_dev)
802 {
803 struct device_driver *drv = par_dev->dev.driver;
804 int len = strlen(drv->name);
805
806 if (strncmp(par_dev->name, drv->name, len))
807 return -ENODEV;
808
809 return 0;
810 }
811
812 static struct parport_driver pp_driver = {
813 .name = CHRDEV,
814 .probe = pp_probe,
815 .match_port = pp_attach,
816 .detach = pp_detach,
817 .devmodel = true,
818 };
819
820 static int __init ppdev_init(void)
821 {
822 int err = 0;
823
824 if (register_chrdev(PP_MAJOR, CHRDEV, &pp_fops)) {
825 printk(KERN_WARNING CHRDEV ": unable to get major %d\n",
826 PP_MAJOR);
827 return -EIO;
828 }
829 ppdev_class = class_create(THIS_MODULE, CHRDEV);
830 if (IS_ERR(ppdev_class)) {
831 err = PTR_ERR(ppdev_class);
832 goto out_chrdev;
833 }
834 err = parport_register_driver(&pp_driver);
835 if (err < 0) {
836 printk(KERN_WARNING CHRDEV ": unable to register with parport\n");
837 goto out_class;
838 }
839
840 printk(KERN_INFO PP_VERSION "\n");
841 goto out;
842
843 out_class:
844 class_destroy(ppdev_class);
845 out_chrdev:
846 unregister_chrdev(PP_MAJOR, CHRDEV);
847 out:
848 return err;
849 }
850
851 static void __exit ppdev_cleanup(void)
852 {
853 /* Clean up all parport stuff */
854 parport_unregister_driver(&pp_driver);
855 class_destroy(ppdev_class);
856 unregister_chrdev(PP_MAJOR, CHRDEV);
857 }
858
859 module_init(ppdev_init);
860 module_exit(ppdev_cleanup);
861
862 MODULE_LICENSE("GPL");
863 MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);