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 Tim Waugh <tim@cyberelk.demon.co.uk>
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 * read/write read or write in current IEEE 1284 protocol
40 * select wait for interrupt (in readfds)
42 * Added SETTIME/GETTIME ioctl, Fred Barnes 1999.
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/sched.h>
48 #include <linux/devfs_fs_kernel.h>
49 #include <linux/ioctl.h>
50 #include <linux/parport.h>
51 #include <linux/ctype.h>
52 #include <linux/poll.h>
53 #include <asm/uaccess.h>
54 #include <linux/ppdev.h>
56 #define PP_VERSION "ppdev: user-space parallel port driver"
57 #define CHRDEV "ppdev"
60 #define min(a,b) ((a) < (b) ? (a) : (b))
64 struct pardevice
* pdev
;
65 wait_queue_head_t irq_wait
;
70 struct ieee1284_info state
;
71 struct ieee1284_info saved_state
;
74 /* pp_struct.flags bitfields */
75 #define PP_CLAIMED (1<<0)
76 #define PP_EXCL (1<<1)
79 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
80 #define PP_BUFFER_SIZE 256
81 #define PARDEVICE_MAX 8
83 /* ROUND_UP macro from fs/select.c */
84 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
86 struct pp_port_list_struct
{
88 struct pp_port_list_struct
*next
;
90 static struct pp_port_list_struct
*pp_port_list
;
91 static DECLARE_MUTEX(pp_port_list_lock
);
93 /* pp_attach and pp_detach are for keeping a list of currently
94 * available ports, held under a mutex. We do this rather than
95 * using parport_enumerate because it stops a load of races.
98 static void pp_attach (struct parport
*port
)
100 struct pp_port_list_struct
*add
;
102 add
= kmalloc (sizeof (struct pp_port_list_struct
), GFP_KERNEL
);
104 printk (KERN_WARNING CHRDEV
": memory squeeze\n");
109 down (&pp_port_list_lock
);
110 add
->next
= pp_port_list
;
112 up (&pp_port_list_lock
);
115 static void pp_detach (struct parport
*port
)
117 struct pp_port_list_struct
*del
;
119 down (&pp_port_list_lock
);
121 if (del
->port
== port
)
122 pp_port_list
= del
->next
;
124 struct pp_port_list_struct
*prev
;
128 } while (del
&& del
->port
!= port
);
130 prev
->next
= del
->next
;
132 up (&pp_port_list_lock
);
138 static struct parport_driver ppdev_driver
= {
144 static inline void pp_enable_irq (struct pp_struct
*pp
)
146 struct parport
*port
= pp
->pdev
->port
;
147 port
->ops
->enable_irq (port
);
150 static loff_t
pp_lseek (struct file
* file
, long long offset
, int origin
)
155 static ssize_t
pp_read (struct file
* file
, char * buf
, size_t count
,
158 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
159 struct pp_struct
*pp
= file
->private_data
;
161 ssize_t bytes_read
= 0;
164 if (!(pp
->flags
& PP_CLAIMED
)) {
165 /* Don't have the port claimed */
166 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
171 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
175 while (bytes_read
< count
) {
176 ssize_t need
= min(count
- bytes_read
, PP_BUFFER_SIZE
);
178 got
= parport_read (pp
->pdev
->port
, kbuffer
, need
);
187 if (copy_to_user (buf
+ bytes_read
, kbuffer
, got
)) {
188 bytes_read
= -EFAULT
;
194 if (signal_pending (current
)) {
200 if (current
->need_resched
)
209 static ssize_t
pp_write (struct file
* file
, const char * buf
, size_t count
,
212 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
213 struct pp_struct
*pp
= file
->private_data
;
215 ssize_t bytes_written
= 0;
218 if (!(pp
->flags
& PP_CLAIMED
)) {
219 /* Don't have the port claimed */
220 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
225 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
229 while (bytes_written
< count
) {
230 ssize_t n
= min(count
- bytes_written
, PP_BUFFER_SIZE
);
232 if (copy_from_user (kbuffer
, buf
+ bytes_written
, n
)) {
233 bytes_written
= -EFAULT
;
237 wrote
= parport_write (pp
->pdev
->port
, kbuffer
, n
);
241 bytes_written
= wrote
;
245 bytes_written
+= wrote
;
247 if (signal_pending (current
)) {
249 bytes_written
= -EINTR
;
253 if (current
->need_resched
)
259 return bytes_written
;
262 static void pp_irq (int irq
, void * private, struct pt_regs
* unused
)
264 struct pp_struct
* pp
= (struct pp_struct
*) private;
266 if (pp
->irqresponse
) {
267 parport_write_control (pp
->pdev
->port
, pp
->irqctl
);
271 atomic_inc (&pp
->irqc
);
272 wake_up_interruptible (&pp
->irq_wait
);
275 static int register_device (int minor
, struct pp_struct
*pp
)
277 struct pp_port_list_struct
*ports
;
278 struct pardevice
* pdev
= NULL
;
282 name
= kmalloc (strlen (CHRDEV
) + 3, GFP_KERNEL
);
286 sprintf (name
, CHRDEV
"%x", minor
);
288 down (&pp_port_list_lock
);
289 ports
= pp_port_list
;
290 while (ports
&& ports
->port
->number
!= minor
)
293 fl
= (pp
->flags
& PP_EXCL
) ? PARPORT_FLAG_EXCL
: 0;
294 pdev
= parport_register_device (ports
->port
, name
, NULL
,
295 NULL
, pp_irq
, fl
, pp
);
297 up (&pp_port_list_lock
);
300 printk (KERN_WARNING
"%s: no associated port!\n", name
);
307 printk (KERN_WARNING
"%s: failed to register device!\n", name
);
313 printk (KERN_DEBUG
"%s: registered pardevice\n", name
);
317 static enum ieee1284_phase
init_phase (int mode
)
319 switch (mode
& ~(IEEE1284_DEVICEID
321 case IEEE1284_MODE_NIBBLE
:
322 case IEEE1284_MODE_BYTE
:
323 return IEEE1284_PH_REV_IDLE
;
325 return IEEE1284_PH_FWD_IDLE
;
328 static int pp_ioctl(struct inode
*inode
, struct file
*file
,
329 unsigned int cmd
, unsigned long arg
)
331 unsigned int minor
= MINOR(inode
->i_rdev
);
332 struct pp_struct
*pp
= file
->private_data
;
333 struct parport
* port
;
335 /* First handle the cases that don't take arguments. */
336 if (cmd
== PPCLAIM
) {
337 struct ieee1284_info
*info
;
339 if (pp
->flags
& PP_CLAIMED
) {
340 printk (KERN_DEBUG CHRDEV
341 "%x: you've already got it!\n", minor
);
345 /* Deferred device registration. */
347 int err
= register_device (minor
, pp
);
352 parport_claim_or_block (pp
->pdev
);
353 pp
->flags
|= PP_CLAIMED
;
355 /* For interrupt-reporting to work, we need to be
356 * informed of each interrupt. */
359 /* We may need to fix up the state machine. */
360 info
= &pp
->pdev
->port
->ieee1284
;
361 pp
->saved_state
.mode
= info
->mode
;
362 pp
->saved_state
.phase
= info
->phase
;
363 info
->mode
= pp
->state
.mode
;
364 info
->phase
= pp
->state
.phase
;
371 printk (KERN_DEBUG CHRDEV
"%x: too late for PPEXCL; "
372 "already registered\n", minor
);
373 if (pp
->flags
& PP_EXCL
)
374 /* But it's not really an error. */
376 /* There's no chance of making the driver happy. */
380 /* Just remember to register the device exclusively
381 * when we finally do the registration. */
382 pp
->flags
|= PP_EXCL
;
386 if (cmd
== PPSETMODE
) {
388 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
390 /* FIXME: validate mode */
391 pp
->state
.mode
= mode
;
392 pp
->state
.phase
= init_phase (mode
);
394 if (pp
->flags
& PP_CLAIMED
) {
395 pp
->pdev
->port
->ieee1284
.mode
= mode
;
396 pp
->pdev
->port
->ieee1284
.phase
= pp
->state
.phase
;
402 if (cmd
== PPSETPHASE
) {
404 if (copy_from_user (&phase
, (int *) arg
, sizeof (phase
)))
406 /* FIXME: validate phase */
407 pp
->state
.phase
= phase
;
409 if (pp
->flags
& PP_CLAIMED
)
410 pp
->pdev
->port
->ieee1284
.phase
= phase
;
415 /* Everything else requires the port to be claimed, so check
417 if ((pp
->flags
& PP_CLAIMED
) == 0) {
418 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
423 port
= pp
->pdev
->port
;
425 struct ieee1284_info
*info
;
430 struct timeval par_timeout
;
434 reg
= parport_read_status (port
);
435 return copy_to_user ((unsigned char *) arg
, ®
,
439 reg
= parport_read_data (port
);
440 return copy_to_user ((unsigned char *) arg
, ®
,
444 reg
= parport_read_control (port
);
445 return copy_to_user ((unsigned char *) arg
, ®
,
449 parport_yield_blocking (pp
->pdev
);
453 /* Save the state machine's state. */
454 info
= &pp
->pdev
->port
->ieee1284
;
455 pp
->state
.mode
= info
->mode
;
456 pp
->state
.phase
= info
->phase
;
457 info
->mode
= pp
->saved_state
.mode
;
458 info
->phase
= pp
->saved_state
.phase
;
459 parport_release (pp
->pdev
);
460 pp
->flags
&= ~PP_CLAIMED
;
464 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
466 parport_write_control (port
, reg
);
470 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
472 parport_write_data (port
, reg
);
476 if (copy_from_user (&mask
, (unsigned char *) arg
,
479 if (copy_from_user (®
, 1 + (unsigned char *) arg
,
482 parport_frob_control (port
, mask
, reg
);
486 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
489 port
->ops
->data_reverse (port
);
491 port
->ops
->data_forward (port
);
495 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
497 switch ((ret
= parport_negotiate (port
, mode
))) {
499 case -1: /* handshake failed, peripheral not IEEE 1284 */
502 case 1: /* handshake succeeded, peripheral rejected mode */
510 if (copy_from_user (®
, (unsigned char *) arg
,
514 /* Remember what to set the control lines to, for next
515 * time we get an interrupt. */
521 ret
= atomic_read (&pp
->irqc
);
522 if (copy_to_user ((int *) arg
, &ret
, sizeof (ret
)))
524 atomic_sub (ret
, &pp
->irqc
);
528 if (copy_from_user (&par_timeout
, (struct timeval
*)arg
,
529 sizeof(struct timeval
))) {
532 /* Convert to jiffies, place in pp->pdev->timeout */
533 if ((par_timeout
.tv_sec
< 0) || (par_timeout
.tv_usec
< 0)) {
536 to_jiffies
= ROUND_UP(par_timeout
.tv_usec
, 1000000/HZ
);
537 to_jiffies
+= par_timeout
.tv_sec
* (long)HZ
;
538 if (to_jiffies
<= 0) {
541 pp
->pdev
->timeout
= to_jiffies
;
545 to_jiffies
= pp
->pdev
->timeout
;
546 par_timeout
.tv_sec
= to_jiffies
/ HZ
;
547 par_timeout
.tv_usec
= (to_jiffies
% (long)HZ
) * (1000000/HZ
);
548 if (copy_to_user ((struct timeval
*)arg
, &par_timeout
,
549 sizeof(struct timeval
))) {
555 printk (KERN_DEBUG CHRDEV
"%x: What? (cmd=0x%x)\n", minor
,
560 /* Keep the compiler happy */
564 static int pp_open (struct inode
* inode
, struct file
* file
)
566 unsigned int minor
= MINOR (inode
->i_rdev
);
567 struct pp_struct
*pp
;
569 if (minor
>= PARPORT_MAX
)
572 pp
= kmalloc (sizeof (struct pp_struct
), GFP_KERNEL
);
576 pp
->state
.mode
= IEEE1284_MODE_COMPAT
;
577 pp
->state
.phase
= init_phase (pp
->state
.mode
);
580 atomic_set (&pp
->irqc
, 0);
581 init_waitqueue_head (&pp
->irq_wait
);
583 /* Defer the actual device registration until the first claim.
584 * That way, we know whether or not the driver wants to have
585 * exclusive access to the port (PPEXCL).
588 file
->private_data
= pp
;
593 static int pp_release (struct inode
* inode
, struct file
* file
)
595 unsigned int minor
= MINOR (inode
->i_rdev
);
596 struct pp_struct
*pp
= file
->private_data
;
598 if (pp
->pdev
&& pp
->pdev
->port
->ieee1284
.mode
!= IEEE1284_MODE_COMPAT
) {
599 if (!(pp
->flags
& PP_CLAIMED
)) {
600 parport_claim_or_block (pp
->pdev
);
601 pp
->flags
|= PP_CLAIMED
;
603 parport_negotiate (pp
->pdev
->port
, IEEE1284_MODE_COMPAT
);
604 printk (KERN_DEBUG CHRDEV
605 "%x: negotiated back to compatibility mode because "
606 "user-space forgot\n", minor
);
609 if (pp
->flags
& PP_CLAIMED
) {
610 parport_release (pp
->pdev
);
611 printk (KERN_DEBUG CHRDEV
"%x: released pardevice because "
612 "user-space forgot\n", minor
);
616 const char *name
= pp
->pdev
->name
;
617 parport_unregister_device (pp
->pdev
);
620 printk (KERN_DEBUG CHRDEV
"%x: unregistered pardevice\n",
629 /* No kernel lock held - fine */
630 static unsigned int pp_poll (struct file
* file
, poll_table
* wait
)
632 struct pp_struct
*pp
= file
->private_data
;
633 unsigned int mask
= 0;
635 poll_wait (file
, &pp
->irq_wait
, wait
);
636 if (atomic_read (&pp
->irqc
))
637 mask
|= POLLIN
| POLLRDNORM
;
642 static struct file_operations pp_fops
= {
653 static devfs_handle_t devfs_handle
= NULL
;
655 static int __init
ppdev_init (void)
657 if (parport_register_driver (&ppdev_driver
)) {
658 printk (KERN_WARNING CHRDEV
": unable to register driver\n");
661 if (devfs_register_chrdev (PP_MAJOR
, CHRDEV
, &pp_fops
)) {
662 printk (KERN_WARNING CHRDEV
": unable to get major %d\n",
666 devfs_handle
= devfs_mk_dir (NULL
, "parports", NULL
);
667 devfs_register_series (devfs_handle
, "%u", PARPORT_MAX
,
668 DEVFS_FL_DEFAULT
, PP_MAJOR
, 0,
669 S_IFCHR
| S_IRUGO
| S_IWUGO
,
672 printk (KERN_INFO PP_VERSION
"\n");
676 static void __exit
ppdev_cleanup (void)
678 /* Clean up all parport stuff */
679 devfs_unregister (devfs_handle
);
680 devfs_unregister_chrdev (PP_MAJOR
, CHRDEV
);
681 parport_unregister_driver (&ppdev_driver
);
684 module_init(ppdev_init
);
685 module_exit(ppdev_cleanup
);