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