blob: fdd37543aa79e6d0b159767f791024d4c31e83fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/ppdev.c
3 *
4 * This is the code behind /dev/parport* -- it allows a user-space
5 * application to use the parport subsystem.
6 *
7 * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * A /dev/parportx device node represents an arbitrary device
15 * on port 'x'. The following operations are possible:
16 *
17 * open do nothing, set up default IEEE 1284 protocol to be COMPAT
18 * close release port and unregister device (if necessary)
19 * ioctl
20 * EXCL register device exclusively (may fail)
21 * CLAIM (register device first time) parport_claim_or_block
22 * RELEASE parport_release
23 * SETMODE set the IEEE 1284 protocol to use for read/write
24 * SETPHASE set the IEEE 1284 phase of a particular mode. Not to be
25 * confused with ioctl(fd, SETPHASER, &stun). ;-)
26 * DATADIR data_forward / data_reverse
27 * WDATA write_data
28 * RDATA read_data
29 * WCONTROL write_control
30 * RCONTROL read_control
31 * FCONTROL frob_control
32 * RSTATUS read_status
33 * NEGOT parport_negotiate
34 * YIELD parport_yield_blocking
35 * WCTLONIRQ on interrupt, set control lines
36 * CLRIRQ clear (and return) interrupt count
37 * SETTIME sets device timeout (struct timeval)
38 * GETTIME gets device timeout (struct timeval)
39 * GETMODES gets hardware supported modes (unsigned int)
40 * GETMODE gets the current IEEE1284 mode
41 * GETPHASE gets the current IEEE1284 phase
42 * GETFLAGS gets current (user-visible) flags
43 * SETFLAGS sets current (user-visible) flags
44 * read/write read or write in current IEEE 1284 protocol
45 * select wait for interrupt (in readfds)
46 *
47 * Changes:
48 * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
49 *
50 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
51 * - On error, copy_from_user and copy_to_user do not return -EFAULT,
52 * They return the positive number of bytes *not* copied due to address
53 * space errors.
54 *
55 * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
56 * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
57 */
58
59#include <linux/module.h>
60#include <linux/init.h>
61#include <linux/sched.h>
62#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/ioctl.h>
64#include <linux/parport.h>
65#include <linux/ctype.h>
66#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Rene Hermane6a67842006-03-25 03:07:13 -080068#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/ppdev.h>
Jonathan Corbetf2b98572008-05-18 15:32:43 -060070#include <linux/smp_lock.h>
Alan Cox6d535d32008-07-25 01:48:16 -070071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define PP_VERSION "ppdev: user-space parallel port driver"
74#define CHRDEV "ppdev"
75
76struct pp_struct {
77 struct pardevice * pdev;
78 wait_queue_head_t irq_wait;
79 atomic_t irqc;
80 unsigned int flags;
81 int irqresponse;
82 unsigned char irqctl;
83 struct ieee1284_info state;
84 struct ieee1284_info saved_state;
85 long default_inactivity;
86};
87
88/* pp_struct.flags bitfields */
89#define PP_CLAIMED (1<<0)
90#define PP_EXCL (1<<1)
91
92/* Other constants */
93#define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
94#define PP_BUFFER_SIZE 1024
95#define PARDEVICE_MAX 8
96
97/* ROUND_UP macro from fs/select.c */
98#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
99
100static inline void pp_enable_irq (struct pp_struct *pp)
101{
102 struct parport *port = pp->pdev->port;
103 port->ops->enable_irq (port);
104}
105
106static ssize_t pp_read (struct file * file, char __user * buf, size_t count,
107 loff_t * ppos)
108{
Josef Sipeka7113a92006-12-08 02:36:55 -0800109 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct pp_struct *pp = file->private_data;
111 char * kbuffer;
112 ssize_t bytes_read = 0;
113 struct parport *pport;
114 int mode;
115
116 if (!(pp->flags & PP_CLAIMED)) {
117 /* Don't have the port claimed */
Michael Buesch81fc4012009-06-17 16:27:49 -0700118 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -EINVAL;
120 }
121
122 /* Trivial case. */
123 if (count == 0)
124 return 0;
125
126 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
127 if (!kbuffer) {
128 return -ENOMEM;
129 }
130 pport = pp->pdev->port;
131 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
132
133 parport_set_timeout (pp->pdev,
134 (file->f_flags & O_NONBLOCK) ?
135 PARPORT_INACTIVITY_O_NONBLOCK :
136 pp->default_inactivity);
137
138 while (bytes_read == 0) {
139 ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
140
141 if (mode == IEEE1284_MODE_EPP) {
142 /* various specials for EPP mode */
143 int flags = 0;
144 size_t (*fn)(struct parport *, void *, size_t, int);
145
146 if (pp->flags & PP_W91284PIC) {
147 flags |= PARPORT_W91284PIC;
148 }
149 if (pp->flags & PP_FASTREAD) {
150 flags |= PARPORT_EPP_FAST;
151 }
152 if (pport->ieee1284.mode & IEEE1284_ADDR) {
153 fn = pport->ops->epp_read_addr;
154 } else {
155 fn = pport->ops->epp_read_data;
156 }
157 bytes_read = (*fn)(pport, kbuffer, need, flags);
158 } else {
159 bytes_read = parport_read (pport, kbuffer, need);
160 }
161
162 if (bytes_read != 0)
163 break;
164
165 if (file->f_flags & O_NONBLOCK) {
166 bytes_read = -EAGAIN;
167 break;
168 }
169
170 if (signal_pending (current)) {
171 bytes_read = -ERESTARTSYS;
172 break;
173 }
174
175 cond_resched();
176 }
177
178 parport_set_timeout (pp->pdev, pp->default_inactivity);
179
180 if (bytes_read > 0 && copy_to_user (buf, kbuffer, bytes_read))
181 bytes_read = -EFAULT;
182
183 kfree (kbuffer);
184 pp_enable_irq (pp);
185 return bytes_read;
186}
187
188static ssize_t pp_write (struct file * file, const char __user * buf,
189 size_t count, loff_t * ppos)
190{
Josef Sipeka7113a92006-12-08 02:36:55 -0800191 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct pp_struct *pp = file->private_data;
193 char * kbuffer;
194 ssize_t bytes_written = 0;
195 ssize_t wrote;
196 int mode;
197 struct parport *pport;
198
199 if (!(pp->flags & PP_CLAIMED)) {
200 /* Don't have the port claimed */
Michael Buesch81fc4012009-06-17 16:27:49 -0700201 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EINVAL;
203 }
204
205 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
206 if (!kbuffer) {
207 return -ENOMEM;
208 }
209 pport = pp->pdev->port;
210 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
211
212 parport_set_timeout (pp->pdev,
213 (file->f_flags & O_NONBLOCK) ?
214 PARPORT_INACTIVITY_O_NONBLOCK :
215 pp->default_inactivity);
216
217 while (bytes_written < count) {
218 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
219
220 if (copy_from_user (kbuffer, buf + bytes_written, n)) {
221 bytes_written = -EFAULT;
222 break;
223 }
224
225 if ((pp->flags & PP_FASTWRITE) && (mode == IEEE1284_MODE_EPP)) {
226 /* do a fast EPP write */
227 if (pport->ieee1284.mode & IEEE1284_ADDR) {
228 wrote = pport->ops->epp_write_addr (pport,
229 kbuffer, n, PARPORT_EPP_FAST);
230 } else {
231 wrote = pport->ops->epp_write_data (pport,
232 kbuffer, n, PARPORT_EPP_FAST);
233 }
234 } else {
235 wrote = parport_write (pp->pdev->port, kbuffer, n);
236 }
237
238 if (wrote <= 0) {
239 if (!bytes_written) {
240 bytes_written = wrote;
241 }
242 break;
243 }
244
245 bytes_written += wrote;
246
247 if (file->f_flags & O_NONBLOCK) {
248 if (!bytes_written)
249 bytes_written = -EAGAIN;
250 break;
251 }
252
253 if (signal_pending (current)) {
254 if (!bytes_written) {
255 bytes_written = -EINTR;
256 }
257 break;
258 }
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
Jeff Garzik5712cb32007-10-19 02:54:26 -0400270static void pp_irq (void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Jeff Garzik5712cb32007-10-19 02:54:26 -0400272 struct pp_struct *pp = private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
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
283static int register_device (int minor, struct pp_struct *pp)
284{
285 struct parport *port;
286 struct pardevice * pdev = NULL;
287 char *name;
288 int fl;
289
290 name = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
291 if (name == NULL)
292 return -ENOMEM;
293
294 sprintf (name, CHRDEV "%x", minor);
295
296 port = parport_find_number (minor);
297 if (!port) {
298 printk (KERN_WARNING "%s: no associated port!\n", name);
299 kfree (name);
300 return -ENXIO;
301 }
302
303 fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
304 pdev = parport_register_device (port, name, NULL,
305 NULL, pp_irq, fl, pp);
306 parport_put_port (port);
307
308 if (!pdev) {
309 printk (KERN_WARNING "%s: failed to register device!\n", name);
310 kfree (name);
311 return -ENXIO;
312 }
313
314 pp->pdev = pdev;
Michael Buesch81fc4012009-06-17 16:27:49 -0700315 pr_debug("%s: registered pardevice\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static enum ieee1284_phase init_phase (int mode)
320{
321 switch (mode & ~(IEEE1284_DEVICEID
322 | IEEE1284_ADDR)) {
323 case IEEE1284_MODE_NIBBLE:
324 case IEEE1284_MODE_BYTE:
325 return IEEE1284_PH_REV_IDLE;
326 }
327 return IEEE1284_PH_FWD_IDLE;
328}
329
Alan Cox6d535d32008-07-25 01:48:16 -0700330static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Alan Cox6d535d32008-07-25 01:48:16 -0700332 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct pp_struct *pp = file->private_data;
334 struct parport * port;
335 void __user *argp = (void __user *)arg;
336
337 /* First handle the cases that don't take arguments. */
338 switch (cmd) {
339 case PPCLAIM:
340 {
341 struct ieee1284_info *info;
342 int ret;
343
344 if (pp->flags & PP_CLAIMED) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700345 pr_debug(CHRDEV "%x: you've already got it!\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EINVAL;
347 }
348
349 /* Deferred device registration. */
350 if (!pp->pdev) {
351 int err = register_device (minor, pp);
352 if (err) {
353 return err;
354 }
355 }
356
357 ret = parport_claim_or_block (pp->pdev);
358 if (ret < 0)
359 return ret;
360
361 pp->flags |= PP_CLAIMED;
362
363 /* For interrupt-reporting to work, we need to be
364 * informed of each interrupt. */
365 pp_enable_irq (pp);
366
367 /* We may need to fix up the state machine. */
368 info = &pp->pdev->port->ieee1284;
369 pp->saved_state.mode = info->mode;
370 pp->saved_state.phase = info->phase;
371 info->mode = pp->state.mode;
372 info->phase = pp->state.phase;
373 pp->default_inactivity = parport_set_timeout (pp->pdev, 0);
374 parport_set_timeout (pp->pdev, pp->default_inactivity);
375
376 return 0;
377 }
378 case PPEXCL:
379 if (pp->pdev) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700380 pr_debug(CHRDEV "%x: too late for PPEXCL; "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 "already registered\n", minor);
382 if (pp->flags & PP_EXCL)
383 /* But it's not really an error. */
384 return 0;
385 /* There's no chance of making the driver happy. */
386 return -EINVAL;
387 }
388
389 /* Just remember to register the device exclusively
390 * when we finally do the registration. */
391 pp->flags |= PP_EXCL;
392 return 0;
393 case PPSETMODE:
394 {
395 int mode;
396 if (copy_from_user (&mode, argp, sizeof (mode)))
397 return -EFAULT;
398 /* FIXME: validate mode */
399 pp->state.mode = mode;
400 pp->state.phase = init_phase (mode);
401
402 if (pp->flags & PP_CLAIMED) {
403 pp->pdev->port->ieee1284.mode = mode;
404 pp->pdev->port->ieee1284.phase = pp->state.phase;
405 }
406
407 return 0;
408 }
409 case PPGETMODE:
410 {
411 int mode;
412
413 if (pp->flags & PP_CLAIMED) {
414 mode = pp->pdev->port->ieee1284.mode;
415 } else {
416 mode = pp->state.mode;
417 }
418 if (copy_to_user (argp, &mode, sizeof (mode))) {
419 return -EFAULT;
420 }
421 return 0;
422 }
423 case PPSETPHASE:
424 {
425 int phase;
426 if (copy_from_user (&phase, argp, sizeof (phase))) {
427 return -EFAULT;
428 }
429 /* FIXME: validate phase */
430 pp->state.phase = phase;
431
432 if (pp->flags & PP_CLAIMED) {
433 pp->pdev->port->ieee1284.phase = phase;
434 }
435
436 return 0;
437 }
438 case PPGETPHASE:
439 {
440 int phase;
441
442 if (pp->flags & PP_CLAIMED) {
443 phase = pp->pdev->port->ieee1284.phase;
444 } else {
445 phase = pp->state.phase;
446 }
447 if (copy_to_user (argp, &phase, sizeof (phase))) {
448 return -EFAULT;
449 }
450 return 0;
451 }
452 case PPGETMODES:
453 {
454 unsigned int modes;
455
456 port = parport_find_number (minor);
457 if (!port)
458 return -ENODEV;
459
460 modes = port->modes;
461 if (copy_to_user (argp, &modes, sizeof (modes))) {
462 return -EFAULT;
463 }
464 return 0;
465 }
466 case PPSETFLAGS:
467 {
468 int uflags;
469
470 if (copy_from_user (&uflags, argp, sizeof (uflags))) {
471 return -EFAULT;
472 }
473 pp->flags &= ~PP_FLAGMASK;
474 pp->flags |= (uflags & PP_FLAGMASK);
475 return 0;
476 }
477 case PPGETFLAGS:
478 {
479 int uflags;
480
481 uflags = pp->flags & PP_FLAGMASK;
482 if (copy_to_user (argp, &uflags, sizeof (uflags))) {
483 return -EFAULT;
484 }
485 return 0;
486 }
487 } /* end switch() */
488
489 /* Everything else requires the port to be claimed, so check
490 * that now. */
491 if ((pp->flags & PP_CLAIMED) == 0) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700492 pr_debug(CHRDEV "%x: claim the port first\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -EINVAL;
494 }
495
496 port = pp->pdev->port;
497 switch (cmd) {
498 struct ieee1284_info *info;
499 unsigned char reg;
500 unsigned char mask;
501 int mode;
502 int ret;
503 struct timeval par_timeout;
504 long to_jiffies;
505
506 case PPRSTATUS:
507 reg = parport_read_status (port);
508 if (copy_to_user (argp, &reg, sizeof (reg)))
509 return -EFAULT;
510 return 0;
511 case PPRDATA:
512 reg = parport_read_data (port);
513 if (copy_to_user (argp, &reg, sizeof (reg)))
514 return -EFAULT;
515 return 0;
516 case PPRCONTROL:
517 reg = parport_read_control (port);
518 if (copy_to_user (argp, &reg, sizeof (reg)))
519 return -EFAULT;
520 return 0;
521 case PPYIELD:
522 parport_yield_blocking (pp->pdev);
523 return 0;
524
525 case PPRELEASE:
526 /* Save the state machine's state. */
527 info = &pp->pdev->port->ieee1284;
528 pp->state.mode = info->mode;
529 pp->state.phase = info->phase;
530 info->mode = pp->saved_state.mode;
531 info->phase = pp->saved_state.phase;
532 parport_release (pp->pdev);
533 pp->flags &= ~PP_CLAIMED;
534 return 0;
535
536 case PPWCONTROL:
537 if (copy_from_user (&reg, argp, sizeof (reg)))
538 return -EFAULT;
539 parport_write_control (port, reg);
540 return 0;
541
542 case PPWDATA:
543 if (copy_from_user (&reg, argp, sizeof (reg)))
544 return -EFAULT;
545 parport_write_data (port, reg);
546 return 0;
547
548 case PPFCONTROL:
549 if (copy_from_user (&mask, argp,
550 sizeof (mask)))
551 return -EFAULT;
552 if (copy_from_user (&reg, 1 + (unsigned char __user *) arg,
553 sizeof (reg)))
554 return -EFAULT;
555 parport_frob_control (port, mask, reg);
556 return 0;
557
558 case PPDATADIR:
559 if (copy_from_user (&mode, argp, sizeof (mode)))
560 return -EFAULT;
561 if (mode)
562 port->ops->data_reverse (port);
563 else
564 port->ops->data_forward (port);
565 return 0;
566
567 case PPNEGOT:
568 if (copy_from_user (&mode, argp, sizeof (mode)))
569 return -EFAULT;
570 switch ((ret = parport_negotiate (port, mode))) {
571 case 0: break;
572 case -1: /* handshake failed, peripheral not IEEE 1284 */
573 ret = -EIO;
574 break;
575 case 1: /* handshake succeeded, peripheral rejected mode */
576 ret = -ENXIO;
577 break;
578 }
579 pp_enable_irq (pp);
580 return ret;
581
582 case PPWCTLONIRQ:
583 if (copy_from_user (&reg, argp, sizeof (reg)))
584 return -EFAULT;
585
586 /* Remember what to set the control lines to, for next
587 * time we get an interrupt. */
588 pp->irqctl = reg;
589 pp->irqresponse = 1;
590 return 0;
591
592 case PPCLRIRQ:
593 ret = atomic_read (&pp->irqc);
594 if (copy_to_user (argp, &ret, sizeof (ret)))
595 return -EFAULT;
596 atomic_sub (ret, &pp->irqc);
597 return 0;
598
599 case PPSETTIME:
600 if (copy_from_user (&par_timeout, argp, sizeof(struct timeval))) {
601 return -EFAULT;
602 }
603 /* Convert to jiffies, place in pp->pdev->timeout */
604 if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
605 return -EINVAL;
606 }
607 to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
608 to_jiffies += par_timeout.tv_sec * (long)HZ;
609 if (to_jiffies <= 0) {
610 return -EINVAL;
611 }
612 pp->pdev->timeout = to_jiffies;
613 return 0;
614
615 case PPGETTIME:
616 to_jiffies = pp->pdev->timeout;
617 par_timeout.tv_sec = to_jiffies / HZ;
618 par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
619 if (copy_to_user (argp, &par_timeout, sizeof(struct timeval)))
620 return -EFAULT;
621 return 0;
622
623 default:
Michael Buesch81fc4012009-06-17 16:27:49 -0700624 pr_debug(CHRDEV "%x: What? (cmd=0x%x)\n", minor, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EINVAL;
626 }
627
628 /* Keep the compiler happy */
629 return 0;
630}
631
Alan Cox6d535d32008-07-25 01:48:16 -0700632static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
633{
634 long ret;
635 lock_kernel();
636 ret = pp_do_ioctl(file, cmd, arg);
637 unlock_kernel();
638 return ret;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static int pp_open (struct inode * inode, struct file * file)
642{
643 unsigned int minor = iminor(inode);
644 struct pp_struct *pp;
645
Jonathan Corbetf2b98572008-05-18 15:32:43 -0600646 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (minor >= PARPORT_MAX)
648 return -ENXIO;
649
650 pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
651 if (!pp)
652 return -ENOMEM;
653
654 pp->state.mode = IEEE1284_MODE_COMPAT;
655 pp->state.phase = init_phase (pp->state.mode);
656 pp->flags = 0;
657 pp->irqresponse = 0;
658 atomic_set (&pp->irqc, 0);
659 init_waitqueue_head (&pp->irq_wait);
660
661 /* Defer the actual device registration until the first claim.
662 * That way, we know whether or not the driver wants to have
663 * exclusive access to the port (PPEXCL).
664 */
665 pp->pdev = NULL;
666 file->private_data = pp;
667
668 return 0;
669}
670
671static int pp_release (struct inode * inode, struct file * file)
672{
673 unsigned int minor = iminor(inode);
674 struct pp_struct *pp = file->private_data;
675 int compat_negot;
676
677 compat_negot = 0;
678 if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
679 (pp->state.mode != IEEE1284_MODE_COMPAT)) {
680 struct ieee1284_info *info;
681
682 /* parport released, but not in compatibility mode */
683 parport_claim_or_block (pp->pdev);
684 pp->flags |= PP_CLAIMED;
685 info = &pp->pdev->port->ieee1284;
686 pp->saved_state.mode = info->mode;
687 pp->saved_state.phase = info->phase;
688 info->mode = pp->state.mode;
689 info->phase = pp->state.phase;
690 compat_negot = 1;
691 } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
692 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
693 compat_negot = 2;
694 }
695 if (compat_negot) {
696 parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
Michael Buesch81fc4012009-06-17 16:27:49 -0700697 pr_debug(CHRDEV "%x: negotiated back to compatibility "
698 "mode because user-space forgot\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 if (pp->flags & PP_CLAIMED) {
702 struct ieee1284_info *info;
703
704 info = &pp->pdev->port->ieee1284;
705 pp->state.mode = info->mode;
706 pp->state.phase = info->phase;
707 info->mode = pp->saved_state.mode;
708 info->phase = pp->saved_state.phase;
709 parport_release (pp->pdev);
710 if (compat_negot != 1) {
Michael Buesch81fc4012009-06-17 16:27:49 -0700711 pr_debug(CHRDEV "%x: released pardevice "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 "because user-space forgot\n", minor);
713 }
714 }
715
716 if (pp->pdev) {
717 const char *name = pp->pdev->name;
718 parport_unregister_device (pp->pdev);
719 kfree (name);
720 pp->pdev = NULL;
Michael Buesch81fc4012009-06-17 16:27:49 -0700721 pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 kfree (pp);
725
726 return 0;
727}
728
729/* No kernel lock held - fine */
730static unsigned int pp_poll (struct file * file, poll_table * wait)
731{
732 struct pp_struct *pp = file->private_data;
733 unsigned int mask = 0;
734
735 poll_wait (file, &pp->irq_wait, wait);
736 if (atomic_read (&pp->irqc))
737 mask |= POLLIN | POLLRDNORM;
738
739 return mask;
740}
741
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800742static struct class *ppdev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Arjan van de Ven62322d22006-07-03 00:24:21 -0700744static const struct file_operations pp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .owner = THIS_MODULE,
746 .llseek = no_llseek,
747 .read = pp_read,
748 .write = pp_write,
749 .poll = pp_poll,
Alan Cox6d535d32008-07-25 01:48:16 -0700750 .unlocked_ioctl = pp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .open = pp_open,
752 .release = pp_release,
753};
754
755static void pp_attach(struct parport *port)
756{
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700757 device_create(ppdev_class, port->dev, MKDEV(PP_MAJOR, port->number),
758 NULL, "parport%d", port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761static void pp_detach(struct parport *port)
762{
Greg Kroah-Hartman04880ed2006-09-12 17:00:10 +0200763 device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766static struct parport_driver pp_driver = {
767 .name = CHRDEV,
768 .attach = pp_attach,
769 .detach = pp_detach,
770};
771
772static int __init ppdev_init (void)
773{
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -0700774 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
777 printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
778 PP_MAJOR);
779 return -EIO;
780 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800781 ppdev_class = class_create(THIS_MODULE, CHRDEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (IS_ERR(ppdev_class)) {
783 err = PTR_ERR(ppdev_class);
784 goto out_chrdev;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (parport_register_driver(&pp_driver)) {
787 printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
788 goto out_class;
789 }
790
791 printk (KERN_INFO PP_VERSION "\n");
792 goto out;
793
794out_class:
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800795 class_destroy(ppdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796out_chrdev:
797 unregister_chrdev(PP_MAJOR, CHRDEV);
798out:
799 return err;
800}
801
802static void __exit ppdev_cleanup (void)
803{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Clean up all parport stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 parport_unregister_driver(&pp_driver);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800806 class_destroy(ppdev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unregister_chrdev (PP_MAJOR, CHRDEV);
808}
809
810module_init(ppdev_init);
811module_exit(ppdev_cleanup);
812
813MODULE_LICENSE("GPL");
814MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);