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