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)
43 * Added SETTIME/GETTIME ioctl, Fred Barnes 1999.
45 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 2000/08/25
46 * - On error, copy_from_user and copy_to_user do not return -EFAULT,
47 * They return the positive number of bytes *not* copied due to address
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/sched.h>
54 #include <linux/devfs_fs_kernel.h>
55 #include <linux/ioctl.h>
56 #include <linux/parport.h>
57 #include <linux/ctype.h>
58 #include <linux/poll.h>
59 #include <asm/uaccess.h>
60 #include <linux/ppdev.h>
61 #include <linux/smp_lock.h>
63 #define PP_VERSION "ppdev: user-space parallel port driver"
64 #define CHRDEV "ppdev"
67 #define min(a,b) ((a) < (b) ? (a) : (b))
71 struct pardevice
* pdev
;
72 wait_queue_head_t irq_wait
;
77 struct ieee1284_info state
;
78 struct ieee1284_info saved_state
;
81 /* pp_struct.flags bitfields */
82 #define PP_CLAIMED (1<<0)
83 #define PP_EXCL (1<<1)
86 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
87 #define PP_BUFFER_SIZE 256
88 #define PARDEVICE_MAX 8
90 /* ROUND_UP macro from fs/select.c */
91 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
93 static inline void pp_enable_irq (struct pp_struct
*pp
)
95 struct parport
*port
= pp
->pdev
->port
;
96 port
->ops
->enable_irq (port
);
99 static loff_t
pp_lseek (struct file
* file
, long long offset
, int origin
)
104 static ssize_t
pp_read (struct file
* file
, char * buf
, size_t count
,
107 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
108 struct pp_struct
*pp
= file
->private_data
;
110 ssize_t bytes_read
= 0;
113 if (!(pp
->flags
& PP_CLAIMED
)) {
114 /* Don't have the port claimed */
115 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
120 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
124 while (bytes_read
< count
) {
125 ssize_t need
= min(count
- bytes_read
, PP_BUFFER_SIZE
);
127 got
= parport_read (pp
->pdev
->port
, kbuffer
, need
);
136 if (copy_to_user (buf
+ bytes_read
, kbuffer
, got
)) {
137 bytes_read
= -EFAULT
;
143 if (signal_pending (current
)) {
149 if (current
->need_resched
)
158 static ssize_t
pp_write (struct file
* file
, const char * buf
, size_t count
,
161 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
162 struct pp_struct
*pp
= file
->private_data
;
164 ssize_t bytes_written
= 0;
167 if (!(pp
->flags
& PP_CLAIMED
)) {
168 /* Don't have the port claimed */
169 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
174 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
178 while (bytes_written
< count
) {
179 ssize_t n
= min(count
- bytes_written
, PP_BUFFER_SIZE
);
181 if (copy_from_user (kbuffer
, buf
+ bytes_written
, n
)) {
182 bytes_written
= -EFAULT
;
186 wrote
= parport_write (pp
->pdev
->port
, kbuffer
, n
);
190 bytes_written
= wrote
;
194 bytes_written
+= wrote
;
196 if (signal_pending (current
)) {
198 bytes_written
= -EINTR
;
202 if (current
->need_resched
)
208 return bytes_written
;
211 static void pp_irq (int irq
, void * private, struct pt_regs
* unused
)
213 struct pp_struct
* pp
= (struct pp_struct
*) private;
215 if (pp
->irqresponse
) {
216 parport_write_control (pp
->pdev
->port
, pp
->irqctl
);
220 atomic_inc (&pp
->irqc
);
221 wake_up_interruptible (&pp
->irq_wait
);
224 static int register_device (int minor
, struct pp_struct
*pp
)
226 struct parport
*port
;
227 struct pardevice
* pdev
= NULL
;
231 name
= kmalloc (strlen (CHRDEV
) + 3, GFP_KERNEL
);
235 sprintf (name
, CHRDEV
"%x", minor
);
237 port
= parport_find_number (minor
);
239 printk (KERN_WARNING
"%s: no associated port!\n", name
);
244 fl
= (pp
->flags
& PP_EXCL
) ? PARPORT_FLAG_EXCL
: 0;
245 pdev
= parport_register_device (port
, name
, NULL
,
246 NULL
, pp_irq
, fl
, pp
);
247 parport_put_port (port
);
250 printk (KERN_WARNING
"%s: failed to register device!\n", name
);
256 printk (KERN_DEBUG
"%s: registered pardevice\n", name
);
260 static enum ieee1284_phase
init_phase (int mode
)
262 switch (mode
& ~(IEEE1284_DEVICEID
264 case IEEE1284_MODE_NIBBLE
:
265 case IEEE1284_MODE_BYTE
:
266 return IEEE1284_PH_REV_IDLE
;
268 return IEEE1284_PH_FWD_IDLE
;
271 static int pp_ioctl(struct inode
*inode
, struct file
*file
,
272 unsigned int cmd
, unsigned long arg
)
274 unsigned int minor
= MINOR(inode
->i_rdev
);
275 struct pp_struct
*pp
= file
->private_data
;
276 struct parport
* port
;
278 /* First handle the cases that don't take arguments. */
279 if (cmd
== PPCLAIM
) {
280 struct ieee1284_info
*info
;
282 if (pp
->flags
& PP_CLAIMED
) {
283 printk (KERN_DEBUG CHRDEV
284 "%x: you've already got it!\n", minor
);
288 /* Deferred device registration. */
290 int err
= register_device (minor
, pp
);
295 parport_claim_or_block (pp
->pdev
);
296 pp
->flags
|= PP_CLAIMED
;
298 /* For interrupt-reporting to work, we need to be
299 * informed of each interrupt. */
302 /* We may need to fix up the state machine. */
303 info
= &pp
->pdev
->port
->ieee1284
;
304 pp
->saved_state
.mode
= info
->mode
;
305 pp
->saved_state
.phase
= info
->phase
;
306 info
->mode
= pp
->state
.mode
;
307 info
->phase
= pp
->state
.phase
;
314 printk (KERN_DEBUG CHRDEV
"%x: too late for PPEXCL; "
315 "already registered\n", minor
);
316 if (pp
->flags
& PP_EXCL
)
317 /* But it's not really an error. */
319 /* There's no chance of making the driver happy. */
323 /* Just remember to register the device exclusively
324 * when we finally do the registration. */
325 pp
->flags
|= PP_EXCL
;
329 if (cmd
== PPSETMODE
) {
331 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
333 /* FIXME: validate mode */
334 pp
->state
.mode
= mode
;
335 pp
->state
.phase
= init_phase (mode
);
337 if (pp
->flags
& PP_CLAIMED
) {
338 pp
->pdev
->port
->ieee1284
.mode
= mode
;
339 pp
->pdev
->port
->ieee1284
.phase
= pp
->state
.phase
;
345 if (cmd
== PPSETPHASE
) {
347 if (copy_from_user (&phase
, (int *) arg
, sizeof (phase
)))
349 /* FIXME: validate phase */
350 pp
->state
.phase
= phase
;
352 if (pp
->flags
& PP_CLAIMED
)
353 pp
->pdev
->port
->ieee1284
.phase
= phase
;
358 /* Everything else requires the port to be claimed, so check
360 if ((pp
->flags
& PP_CLAIMED
) == 0) {
361 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
366 port
= pp
->pdev
->port
;
368 struct ieee1284_info
*info
;
373 struct timeval par_timeout
;
377 reg
= parport_read_status (port
);
378 if (copy_to_user ((unsigned char *) arg
, ®
, sizeof (reg
)))
382 reg
= parport_read_data (port
);
383 if (copy_to_user ((unsigned char *) arg
, ®
, sizeof (reg
)))
387 reg
= parport_read_control (port
);
388 if (copy_to_user ((unsigned char *) arg
, ®
, sizeof (reg
)))
392 parport_yield_blocking (pp
->pdev
);
396 /* Save the state machine's state. */
397 info
= &pp
->pdev
->port
->ieee1284
;
398 pp
->state
.mode
= info
->mode
;
399 pp
->state
.phase
= info
->phase
;
400 info
->mode
= pp
->saved_state
.mode
;
401 info
->phase
= pp
->saved_state
.phase
;
402 parport_release (pp
->pdev
);
403 pp
->flags
&= ~PP_CLAIMED
;
407 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
409 parport_write_control (port
, reg
);
413 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
415 parport_write_data (port
, reg
);
419 if (copy_from_user (&mask
, (unsigned char *) arg
,
422 if (copy_from_user (®
, 1 + (unsigned char *) arg
,
425 parport_frob_control (port
, mask
, reg
);
429 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
432 port
->ops
->data_reverse (port
);
434 port
->ops
->data_forward (port
);
438 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
440 switch ((ret
= parport_negotiate (port
, mode
))) {
442 case -1: /* handshake failed, peripheral not IEEE 1284 */
445 case 1: /* handshake succeeded, peripheral rejected mode */
453 if (copy_from_user (®
, (unsigned char *) arg
,
457 /* Remember what to set the control lines to, for next
458 * time we get an interrupt. */
464 ret
= atomic_read (&pp
->irqc
);
465 if (copy_to_user ((int *) arg
, &ret
, sizeof (ret
)))
467 atomic_sub (ret
, &pp
->irqc
);
471 if (copy_from_user (&par_timeout
, (struct timeval
*)arg
,
472 sizeof(struct timeval
))) {
475 /* Convert to jiffies, place in pp->pdev->timeout */
476 if ((par_timeout
.tv_sec
< 0) || (par_timeout
.tv_usec
< 0)) {
479 to_jiffies
= ROUND_UP(par_timeout
.tv_usec
, 1000000/HZ
);
480 to_jiffies
+= par_timeout
.tv_sec
* (long)HZ
;
481 if (to_jiffies
<= 0) {
484 pp
->pdev
->timeout
= to_jiffies
;
488 to_jiffies
= pp
->pdev
->timeout
;
489 par_timeout
.tv_sec
= to_jiffies
/ HZ
;
490 par_timeout
.tv_usec
= (to_jiffies
% (long)HZ
) * (1000000/HZ
);
491 if (copy_to_user ((struct timeval
*)arg
, &par_timeout
,
492 sizeof(struct timeval
))) {
498 printk (KERN_DEBUG CHRDEV
"%x: What? (cmd=0x%x)\n", minor
,
503 /* Keep the compiler happy */
507 static int pp_open (struct inode
* inode
, struct file
* file
)
509 unsigned int minor
= MINOR (inode
->i_rdev
);
510 struct pp_struct
*pp
;
512 if (minor
>= PARPORT_MAX
)
515 pp
= kmalloc (sizeof (struct pp_struct
), GFP_KERNEL
);
519 pp
->state
.mode
= IEEE1284_MODE_COMPAT
;
520 pp
->state
.phase
= init_phase (pp
->state
.mode
);
523 atomic_set (&pp
->irqc
, 0);
524 init_waitqueue_head (&pp
->irq_wait
);
526 /* Defer the actual device registration until the first claim.
527 * That way, we know whether or not the driver wants to have
528 * exclusive access to the port (PPEXCL).
531 file
->private_data
= pp
;
536 static int pp_release (struct inode
* inode
, struct file
* file
)
538 unsigned int minor
= MINOR (inode
->i_rdev
);
539 struct pp_struct
*pp
= file
->private_data
;
542 if (pp
->pdev
&& pp
->pdev
->port
->ieee1284
.mode
!= IEEE1284_MODE_COMPAT
) {
543 if (!(pp
->flags
& PP_CLAIMED
)) {
544 parport_claim_or_block (pp
->pdev
);
545 pp
->flags
|= PP_CLAIMED
;
547 parport_negotiate (pp
->pdev
->port
, IEEE1284_MODE_COMPAT
);
548 printk (KERN_DEBUG CHRDEV
549 "%x: negotiated back to compatibility mode because "
550 "user-space forgot\n", minor
);
553 if (pp
->flags
& PP_CLAIMED
) {
554 parport_release (pp
->pdev
);
555 printk (KERN_DEBUG CHRDEV
"%x: released pardevice because "
556 "user-space forgot\n", minor
);
560 const char *name
= pp
->pdev
->name
;
561 parport_unregister_device (pp
->pdev
);
564 printk (KERN_DEBUG CHRDEV
"%x: unregistered pardevice\n",
574 /* No kernel lock held - fine */
575 static unsigned int pp_poll (struct file
* file
, poll_table
* wait
)
577 struct pp_struct
*pp
= file
->private_data
;
578 unsigned int mask
= 0;
580 poll_wait (file
, &pp
->irq_wait
, wait
);
581 if (atomic_read (&pp
->irqc
))
582 mask
|= POLLIN
| POLLRDNORM
;
587 static struct file_operations pp_fops
= {
598 static devfs_handle_t devfs_handle
;
600 static int __init
ppdev_init (void)
602 if (devfs_register_chrdev (PP_MAJOR
, CHRDEV
, &pp_fops
)) {
603 printk (KERN_WARNING CHRDEV
": unable to get major %d\n",
607 devfs_handle
= devfs_mk_dir (NULL
, "parports", NULL
);
608 devfs_register_series (devfs_handle
, "%u", PARPORT_MAX
,
609 DEVFS_FL_DEFAULT
, PP_MAJOR
, 0,
610 S_IFCHR
| S_IRUGO
| S_IWUGO
,
613 printk (KERN_INFO PP_VERSION
"\n");
617 static void __exit
ppdev_cleanup (void)
619 /* Clean up all parport stuff */
620 devfs_unregister (devfs_handle
);
621 devfs_unregister_chrdev (PP_MAJOR
, CHRDEV
);
624 module_init(ppdev_init
);
625 module_exit(ppdev_cleanup
);