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 static inline void enable_irq (struct pp_struct
*pp
)
88 struct parport
*port
= pp
->pdev
->port
;
89 port
->ops
->enable_irq (port
);
92 static loff_t
pp_lseek (struct file
* file
, long long offset
, int origin
)
97 static ssize_t
pp_read (struct file
* file
, char * buf
, size_t count
,
100 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
101 struct pp_struct
*pp
= file
->private_data
;
103 ssize_t bytes_read
= 0;
106 if (!(pp
->flags
& PP_CLAIMED
)) {
107 /* Don't have the port claimed */
108 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
113 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
117 while (bytes_read
< count
) {
118 ssize_t need
= min(count
- bytes_read
, PP_BUFFER_SIZE
);
120 got
= parport_read (pp
->pdev
->port
, kbuffer
, need
);
129 if (copy_to_user (buf
+ bytes_read
, kbuffer
, got
)) {
130 bytes_read
= -EFAULT
;
136 if (signal_pending (current
)) {
142 if (current
->need_resched
)
151 static ssize_t
pp_write (struct file
* file
, const char * buf
, size_t count
,
154 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
155 struct pp_struct
*pp
= file
->private_data
;
157 ssize_t bytes_written
= 0;
160 if (!(pp
->flags
& PP_CLAIMED
)) {
161 /* Don't have the port claimed */
162 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
167 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
171 while (bytes_written
< count
) {
172 ssize_t n
= min(count
- bytes_written
, PP_BUFFER_SIZE
);
174 if (copy_from_user (kbuffer
, buf
+ bytes_written
, n
)) {
175 bytes_written
= -EFAULT
;
179 wrote
= parport_write (pp
->pdev
->port
, kbuffer
, n
);
183 bytes_written
= wrote
;
187 bytes_written
+= wrote
;
189 if (signal_pending (current
)) {
191 bytes_written
= -EINTR
;
195 if (current
->need_resched
)
201 return bytes_written
;
204 static void pp_irq (int irq
, void * private, struct pt_regs
* unused
)
206 struct pp_struct
* pp
= (struct pp_struct
*) private;
208 if (pp
->irqresponse
) {
209 parport_write_control (pp
->pdev
->port
, pp
->irqctl
);
213 atomic_inc (&pp
->irqc
);
214 wake_up_interruptible (&pp
->irq_wait
);
217 static int register_device (int minor
, struct pp_struct
*pp
)
219 struct parport
* port
;
220 struct pardevice
* pdev
= NULL
;
224 name
= kmalloc (strlen (CHRDEV
) + 3, GFP_KERNEL
);
228 sprintf (name
, CHRDEV
"%x", minor
);
229 port
= parport_enumerate (); /* FIXME: use attach/detach */
231 while (port
&& port
->number
!= minor
)
235 printk (KERN_WARNING
"%s: no associated port!\n", name
);
240 fl
= (pp
->flags
& PP_EXCL
) ? PARPORT_FLAG_EXCL
: 0;
241 pdev
= parport_register_device (port
, name
, NULL
, NULL
, pp_irq
, fl
,
245 printk (KERN_WARNING
"%s: failed to register device!\n", name
);
251 printk (KERN_DEBUG
"%s: registered pardevice\n", name
);
255 static enum ieee1284_phase
init_phase (int mode
)
257 switch (mode
& ~(IEEE1284_DEVICEID
259 case IEEE1284_MODE_NIBBLE
:
260 case IEEE1284_MODE_BYTE
:
261 return IEEE1284_PH_REV_IDLE
;
263 return IEEE1284_PH_FWD_IDLE
;
266 static int pp_ioctl(struct inode
*inode
, struct file
*file
,
267 unsigned int cmd
, unsigned long arg
)
269 unsigned int minor
= MINOR(inode
->i_rdev
);
270 struct pp_struct
*pp
= file
->private_data
;
271 struct parport
* port
;
273 /* First handle the cases that don't take arguments. */
274 if (cmd
== PPCLAIM
) {
275 struct ieee1284_info
*info
;
277 if (pp
->flags
& PP_CLAIMED
) {
278 printk (KERN_DEBUG CHRDEV
279 "%x: you've already got it!\n", minor
);
283 /* Deferred device registration. */
285 int err
= register_device (minor
, pp
);
290 parport_claim_or_block (pp
->pdev
);
291 pp
->flags
|= PP_CLAIMED
;
293 /* For interrupt-reporting to work, we need to be
294 * informed of each interrupt. */
297 /* We may need to fix up the state machine. */
298 info
= &pp
->pdev
->port
->ieee1284
;
299 pp
->saved_state
.mode
= info
->mode
;
300 pp
->saved_state
.phase
= info
->phase
;
301 info
->mode
= pp
->state
.mode
;
302 info
->phase
= pp
->state
.phase
;
309 printk (KERN_DEBUG CHRDEV
"%x: too late for PPEXCL; "
310 "already registered\n", minor
);
311 if (pp
->flags
& PP_EXCL
)
312 /* But it's not really an error. */
314 /* There's no chance of making the driver happy. */
318 /* Just remember to register the device exclusively
319 * when we finally do the registration. */
320 pp
->flags
|= PP_EXCL
;
324 if (cmd
== PPSETMODE
) {
326 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
328 /* FIXME: validate mode */
329 pp
->state
.mode
= mode
;
330 pp
->state
.phase
= init_phase (mode
);
332 if (pp
->flags
& PP_CLAIMED
) {
333 pp
->pdev
->port
->ieee1284
.mode
= mode
;
334 pp
->pdev
->port
->ieee1284
.phase
= pp
->state
.phase
;
340 if (cmd
== PPSETPHASE
) {
342 if (copy_from_user (&phase
, (int *) arg
, sizeof (phase
)))
344 /* FIXME: validate phase */
345 pp
->state
.phase
= phase
;
347 if (pp
->flags
& PP_CLAIMED
)
348 pp
->pdev
->port
->ieee1284
.phase
= phase
;
353 /* Everything else requires the port to be claimed, so check
355 if ((pp
->flags
& PP_CLAIMED
) == 0) {
356 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
361 port
= pp
->pdev
->port
;
363 struct ieee1284_info
*info
;
368 struct timeval par_timeout
;
372 reg
= parport_read_status (port
);
373 return copy_to_user ((unsigned char *) arg
, ®
,
377 reg
= parport_read_data (port
);
378 return copy_to_user ((unsigned char *) arg
, ®
,
382 reg
= parport_read_control (port
);
383 return copy_to_user ((unsigned char *) arg
, ®
,
387 parport_yield_blocking (pp
->pdev
);
391 /* Save the state machine's state. */
392 info
= &pp
->pdev
->port
->ieee1284
;
393 pp
->state
.mode
= info
->mode
;
394 pp
->state
.phase
= info
->phase
;
395 info
->mode
= pp
->saved_state
.mode
;
396 info
->phase
= pp
->saved_state
.phase
;
397 parport_release (pp
->pdev
);
398 pp
->flags
&= ~PP_CLAIMED
;
402 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
404 parport_write_control (port
, reg
);
408 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
410 parport_write_data (port
, reg
);
414 if (copy_from_user (&mask
, (unsigned char *) arg
,
417 if (copy_from_user (®
, 1 + (unsigned char *) arg
,
420 parport_frob_control (port
, mask
, reg
);
424 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
427 port
->ops
->data_reverse (port
);
429 port
->ops
->data_forward (port
);
433 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
435 switch ((ret
= parport_negotiate (port
, mode
))) {
437 case -1: /* handshake failed, peripheral not IEEE 1284 */
440 case 1: /* handshake succeeded, peripheral rejected mode */
448 if (copy_from_user (®
, (unsigned char *) arg
,
452 /* Remember what to set the control lines to, for next
453 * time we get an interrupt. */
459 ret
= atomic_read (&pp
->irqc
);
460 if (copy_to_user ((int *) arg
, &ret
, sizeof (ret
)))
462 atomic_sub (ret
, &pp
->irqc
);
466 if (copy_from_user (&par_timeout
, (struct timeval
*)arg
,
467 sizeof(struct timeval
))) {
470 /* Convert to jiffies, place in pp->pdev->timeout */
471 if ((par_timeout
.tv_sec
< 0) || (par_timeout
.tv_usec
< 0)) {
474 to_jiffies
= ROUND_UP(par_timeout
.tv_usec
, 1000000/HZ
);
475 to_jiffies
+= par_timeout
.tv_sec
* (long)HZ
;
476 if (to_jiffies
<= 0) {
479 pp
->pdev
->timeout
= to_jiffies
;
483 to_jiffies
= pp
->pdev
->timeout
;
484 par_timeout
.tv_sec
= to_jiffies
/ HZ
;
485 par_timeout
.tv_usec
= (to_jiffies
% (long)HZ
) * (1000000/HZ
);
486 if (copy_to_user ((struct timeval
*)arg
, &par_timeout
,
487 sizeof(struct timeval
))) {
493 printk (KERN_DEBUG CHRDEV
"%x: What? (cmd=0x%x)\n", minor
,
498 /* Keep the compiler happy */
502 static int pp_open (struct inode
* inode
, struct file
* file
)
504 unsigned int minor
= MINOR (inode
->i_rdev
);
505 struct pp_struct
*pp
;
507 if (minor
>= PARPORT_MAX
)
512 pp
= kmalloc (sizeof (struct pp_struct
), GFP_KERNEL
);
518 pp
->state
.mode
= IEEE1284_MODE_COMPAT
;
519 pp
->state
.phase
= init_phase (pp
->state
.mode
);
522 atomic_set (&pp
->irqc
, 0);
523 init_waitqueue_head (&pp
->irq_wait
);
525 /* Defer the actual device registration until the first claim.
526 * That way, we know whether or not the driver wants to have
527 * exclusive access to the port (PPEXCL).
530 file
->private_data
= pp
;
535 static int pp_release (struct inode
* inode
, struct file
* file
)
537 unsigned int minor
= MINOR (inode
->i_rdev
);
538 struct pp_struct
*pp
= file
->private_data
;
540 if (pp
->flags
& PP_CLAIMED
) {
541 parport_release (pp
->pdev
);
542 printk (KERN_DEBUG CHRDEV
"%x: released pardevice because "
543 "user-space forgot\n", minor
);
547 const char *name
= pp
->pdev
->name
;
548 parport_unregister_device (pp
->pdev
);
551 printk (KERN_DEBUG CHRDEV
"%x: unregistered pardevice\n",
561 static unsigned int pp_poll (struct file
* file
, poll_table
* wait
)
563 struct pp_struct
*pp
= file
->private_data
;
564 unsigned int mask
= 0;
566 if (atomic_read (&pp
->irqc
))
567 mask
|= POLLIN
| POLLRDNORM
;
569 poll_wait (file
, &pp
->irq_wait
, wait
);
573 static struct file_operations pp_fops
= {
583 static devfs_handle_t devfs_handle
= NULL
;
585 static int __init
ppdev_init (void)
587 if (devfs_register_chrdev (PP_MAJOR
, CHRDEV
, &pp_fops
)) {
588 printk (KERN_WARNING CHRDEV
": unable to get major %d\n",
592 devfs_handle
= devfs_mk_dir (NULL
, "parports", 0, NULL
);
593 devfs_register_series (devfs_handle
, "%u", PARPORT_MAX
,
594 DEVFS_FL_DEFAULT
, PP_MAJOR
, 0,
595 S_IFCHR
| S_IRUGO
| S_IWUGO
, 0, 0,
598 printk (KERN_INFO PP_VERSION
"\n");
602 static void __exit
ppdev_cleanup (void)
604 /* Clean up all parport stuff */
605 devfs_unregister (devfs_handle
);
606 devfs_unregister_chrdev (PP_MAJOR
, CHRDEV
);
609 module_init(ppdev_init
);
610 module_exit(ppdev_cleanup
);