2 * linux/drivers/char/ppdev.c
4 * This is the code behind /dev/parport* -- it allows a user-space
5 * application to use the parport subsystem.
7 * Copyright (C) 1998-2000, 2002 Tim Waugh <tim@cyberelk.net>
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.
14 * A /dev/parportx device node represents an arbitrary device
15 * on port 'x'. The following operations are possible:
17 * open do nothing, set up default IEEE 1284 protocol to be COMPAT
18 * close release port and unregister device (if necessary)
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
29 * WCONTROL write_control
30 * RCONTROL read_control
31 * FCONTROL frob_control
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)
48 * Added SETTIME/GETTIME ioctl, Fred Barnes, 1999.
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
55 * Added GETMODES/GETMODE/GETPHASE ioctls, Fred Barnes <frmb2@ukc.ac.uk>, 03/01/2001.
56 * Added GETFLAGS/SETFLAGS ioctls, Fred Barnes, 04/2001
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>
74 #define PP_VERSION "ppdev: user-space parallel port driver"
75 #define CHRDEV "ppdev"
78 struct pardevice * pdev;
79 wait_queue_head_t irq_wait;
84 struct ieee1284_info state;
85 struct ieee1284_info saved_state;
86 long default_inactivity;
89 /* pp_struct.flags bitfields */
90 #define PP_CLAIMED (1<<0)
91 #define PP_EXCL (1<<1)
94 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
95 #define PP_BUFFER_SIZE 1024
96 #define PARDEVICE_MAX 8
98 /* ROUND_UP macro from fs/select.c */
99 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
101 static inline void pp_enable_irq (struct pp_struct *pp)
103 struct parport *port = pp->pdev->port;
104 port->ops->enable_irq (port);
107 static ssize_t pp_read (struct file * file, char __user * buf, size_t count,
110 unsigned int minor = iminor(file->f_dentry->d_inode);
111 struct pp_struct *pp = file->private_data;
113 ssize_t bytes_read = 0;
114 struct parport *pport;
117 if (!(pp->flags & PP_CLAIMED)) {
118 /* Don't have the port claimed */
119 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
128 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
132 pport = pp->pdev->port;
133 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
135 parport_set_timeout (pp->pdev,
136 (file->f_flags & O_NONBLOCK) ?
137 PARPORT_INACTIVITY_O_NONBLOCK :
138 pp->default_inactivity);
140 while (bytes_read == 0) {
141 ssize_t need = min_t(unsigned long, count, PP_BUFFER_SIZE);
143 if (mode == IEEE1284_MODE_EPP) {
144 /* various specials for EPP mode */
146 size_t (*fn)(struct parport *, void *, size_t, int);
148 if (pp->flags & PP_W91284PIC) {
149 flags |= PARPORT_W91284PIC;
151 if (pp->flags & PP_FASTREAD) {
152 flags |= PARPORT_EPP_FAST;
154 if (pport->ieee1284.mode & IEEE1284_ADDR) {
155 fn = pport->ops->epp_read_addr;
157 fn = pport->ops->epp_read_data;
159 bytes_read = (*fn)(pport, kbuffer, need, flags);
161 bytes_read = parport_read (pport, kbuffer, need);
167 if (file->f_flags & O_NONBLOCK) {
168 bytes_read = -EAGAIN;
172 if (signal_pending (current)) {
173 bytes_read = -ERESTARTSYS;
180 parport_set_timeout (pp->pdev, pp->default_inactivity);
182 if (bytes_read > 0 && copy_to_user (buf, kbuffer, bytes_read))
183 bytes_read = -EFAULT;
190 static ssize_t pp_write (struct file * file, const char __user * buf,
191 size_t count, loff_t * ppos)
193 unsigned int minor = iminor(file->f_dentry->d_inode);
194 struct pp_struct *pp = file->private_data;
196 ssize_t bytes_written = 0;
199 struct parport *pport;
201 if (!(pp->flags & PP_CLAIMED)) {
202 /* Don't have the port claimed */
203 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
208 kbuffer = kmalloc(min_t(size_t, count, PP_BUFFER_SIZE), GFP_KERNEL);
212 pport = pp->pdev->port;
213 mode = pport->ieee1284.mode & ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
215 parport_set_timeout (pp->pdev,
216 (file->f_flags & O_NONBLOCK) ?
217 PARPORT_INACTIVITY_O_NONBLOCK :
218 pp->default_inactivity);
220 while (bytes_written < count) {
221 ssize_t n = min_t(unsigned long, count - bytes_written, PP_BUFFER_SIZE);
223 if (copy_from_user (kbuffer, buf + bytes_written, n)) {
224 bytes_written = -EFAULT;
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);
234 wrote = pport->ops->epp_write_data (pport,
235 kbuffer, n, PARPORT_EPP_FAST);
238 wrote = parport_write (pp->pdev->port, kbuffer, n);
242 if (!bytes_written) {
243 bytes_written = wrote;
248 bytes_written += wrote;
250 if (file->f_flags & O_NONBLOCK) {
252 bytes_written = -EAGAIN;
256 if (signal_pending (current)) {
257 if (!bytes_written) {
258 bytes_written = -EINTR;
266 parport_set_timeout (pp->pdev, pp->default_inactivity);
270 return bytes_written;
273 static void pp_irq (int irq, void * private, struct pt_regs * unused)
275 struct pp_struct * pp = (struct pp_struct *) private;
277 if (pp->irqresponse) {
278 parport_write_control (pp->pdev->port, pp->irqctl);
282 atomic_inc (&pp->irqc);
283 wake_up_interruptible (&pp->irq_wait);
286 static int register_device (int minor, struct pp_struct *pp)
288 struct parport *port;
289 struct pardevice * pdev = NULL;
293 name = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
297 sprintf (name, CHRDEV "%x", minor);
299 port = parport_find_number (minor);
301 printk (KERN_WARNING "%s: no associated port!\n", name);
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);
312 printk (KERN_WARNING "%s: failed to register device!\n", name);
318 printk (KERN_DEBUG "%s: registered pardevice\n", name);
322 static enum ieee1284_phase init_phase (int mode)
324 switch (mode & ~(IEEE1284_DEVICEID
326 case IEEE1284_MODE_NIBBLE:
327 case IEEE1284_MODE_BYTE:
328 return IEEE1284_PH_REV_IDLE;
330 return IEEE1284_PH_FWD_IDLE;
333 static int pp_ioctl(struct inode *inode, struct file *file,
334 unsigned int cmd, unsigned long arg)
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;
341 /* First handle the cases that don't take arguments. */
345 struct ieee1284_info *info;
348 if (pp->flags & PP_CLAIMED) {
349 printk (KERN_DEBUG CHRDEV
350 "%x: you've already got it!\n", minor);
354 /* Deferred device registration. */
356 int err = register_device (minor, pp);
362 ret = parport_claim_or_block (pp->pdev);
366 pp->flags |= PP_CLAIMED;
368 /* For interrupt-reporting to work, we need to be
369 * informed of each interrupt. */
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);
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. */
390 /* There's no chance of making the driver happy. */
394 /* Just remember to register the device exclusively
395 * when we finally do the registration. */
396 pp->flags |= PP_EXCL;
401 if (copy_from_user (&mode, argp, sizeof (mode)))
403 /* FIXME: validate mode */
404 pp->state.mode = mode;
405 pp->state.phase = init_phase (mode);
407 if (pp->flags & PP_CLAIMED) {
408 pp->pdev->port->ieee1284.mode = mode;
409 pp->pdev->port->ieee1284.phase = pp->state.phase;
418 if (pp->flags & PP_CLAIMED) {
419 mode = pp->pdev->port->ieee1284.mode;
421 mode = pp->state.mode;
423 if (copy_to_user (argp, &mode, sizeof (mode))) {
431 if (copy_from_user (&phase, argp, sizeof (phase))) {
434 /* FIXME: validate phase */
435 pp->state.phase = phase;
437 if (pp->flags & PP_CLAIMED) {
438 pp->pdev->port->ieee1284.phase = phase;
447 if (pp->flags & PP_CLAIMED) {
448 phase = pp->pdev->port->ieee1284.phase;
450 phase = pp->state.phase;
452 if (copy_to_user (argp, &phase, sizeof (phase))) {
461 port = parport_find_number (minor);
466 if (copy_to_user (argp, &modes, sizeof (modes))) {
475 if (copy_from_user (&uflags, argp, sizeof (uflags))) {
478 pp->flags &= ~PP_FLAGMASK;
479 pp->flags |= (uflags & PP_FLAGMASK);
486 uflags = pp->flags & PP_FLAGMASK;
487 if (copy_to_user (argp, &uflags, sizeof (uflags))) {
494 /* Everything else requires the port to be claimed, so check
496 if ((pp->flags & PP_CLAIMED) == 0) {
497 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
502 port = pp->pdev->port;
504 struct ieee1284_info *info;
509 struct timeval par_timeout;
513 reg = parport_read_status (port);
514 if (copy_to_user (argp, ®, sizeof (reg)))
518 reg = parport_read_data (port);
519 if (copy_to_user (argp, ®, sizeof (reg)))
523 reg = parport_read_control (port);
524 if (copy_to_user (argp, ®, sizeof (reg)))
528 parport_yield_blocking (pp->pdev);
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;
543 if (copy_from_user (®, argp, sizeof (reg)))
545 parport_write_control (port, reg);
549 if (copy_from_user (®, argp, sizeof (reg)))
551 parport_write_data (port, reg);
555 if (copy_from_user (&mask, argp,
558 if (copy_from_user (®, 1 + (unsigned char __user *) arg,
561 parport_frob_control (port, mask, reg);
565 if (copy_from_user (&mode, argp, sizeof (mode)))
568 port->ops->data_reverse (port);
570 port->ops->data_forward (port);
574 if (copy_from_user (&mode, argp, sizeof (mode)))
576 switch ((ret = parport_negotiate (port, mode))) {
578 case -1: /* handshake failed, peripheral not IEEE 1284 */
581 case 1: /* handshake succeeded, peripheral rejected mode */
589 if (copy_from_user (®, argp, sizeof (reg)))
592 /* Remember what to set the control lines to, for next
593 * time we get an interrupt. */
599 ret = atomic_read (&pp->irqc);
600 if (copy_to_user (argp, &ret, sizeof (ret)))
602 atomic_sub (ret, &pp->irqc);
606 if (copy_from_user (&par_timeout, argp, sizeof(struct timeval))) {
609 /* Convert to jiffies, place in pp->pdev->timeout */
610 if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
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) {
618 pp->pdev->timeout = to_jiffies;
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)))
630 printk (KERN_DEBUG CHRDEV "%x: What? (cmd=0x%x)\n", minor,
635 /* Keep the compiler happy */
639 static int pp_open (struct inode * inode, struct file * file)
641 unsigned int minor = iminor(inode);
642 struct pp_struct *pp;
644 if (minor >= PARPORT_MAX)
647 pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
651 pp->state.mode = IEEE1284_MODE_COMPAT;
652 pp->state.phase = init_phase (pp->state.mode);
655 atomic_set (&pp->irqc, 0);
656 init_waitqueue_head (&pp->irq_wait);
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).
663 file->private_data = pp;
668 static int pp_release (struct inode * inode, struct file * file)
670 unsigned int minor = iminor(inode);
671 struct pp_struct *pp = file->private_data;
675 if (!(pp->flags & PP_CLAIMED) && pp->pdev &&
676 (pp->state.mode != IEEE1284_MODE_COMPAT)) {
677 struct ieee1284_info *info;
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;
688 } else if ((pp->flags & PP_CLAIMED) && pp->pdev &&
689 (pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT)) {
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);
699 if (pp->flags & PP_CLAIMED) {
700 struct ieee1284_info *info;
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);
715 const char *name = pp->pdev->name;
716 parport_unregister_device (pp->pdev);
719 printk (KERN_DEBUG CHRDEV "%x: unregistered pardevice\n",
728 /* No kernel lock held - fine */
729 static unsigned int pp_poll (struct file * file, poll_table * wait)
731 struct pp_struct *pp = file->private_data;
732 unsigned int mask = 0;
734 poll_wait (file, &pp->irq_wait, wait);
735 if (atomic_read (&pp->irqc))
736 mask |= POLLIN | POLLRDNORM;
741 static struct class *ppdev_class;
743 static struct file_operations pp_fops = {
744 .owner = THIS_MODULE,
751 .release = pp_release,
754 static void pp_attach(struct parport *port)
756 class_device_create(ppdev_class, NULL, MKDEV(PP_MAJOR, port->number),
757 NULL, "parport%d", port->number);
760 static void pp_detach(struct parport *port)
762 class_device_destroy(ppdev_class, MKDEV(PP_MAJOR, port->number));
765 static struct parport_driver pp_driver = {
771 static int __init ppdev_init (void)
775 if (register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
776 printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
780 ppdev_class = class_create(THIS_MODULE, CHRDEV);
781 if (IS_ERR(ppdev_class)) {
782 err = PTR_ERR(ppdev_class);
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);
790 if (parport_register_driver(&pp_driver)) {
791 printk (KERN_WARNING CHRDEV ": unable to register with parport\n");
795 printk (KERN_INFO PP_VERSION "\n");
799 for (i = 0; i < PARPORT_MAX; i++)
800 devfs_remove("parports/%d", i);
801 devfs_remove("parports");
802 class_destroy(ppdev_class);
804 unregister_chrdev(PP_MAJOR, CHRDEV);
809 static void __exit ppdev_cleanup (void)
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);
821 module_init(ppdev_init);
822 module_exit(ppdev_cleanup);
824 MODULE_LICENSE("GPL");
825 MODULE_ALIAS_CHARDEV_MAJOR(PP_MAJOR);