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-9 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 * DATADIR data_forward / data_reverse
27 * WCONTROL write_control
28 * RCONTROL read_control
29 * FCONTROL frob_control
31 * NEGOT parport_negotiate
32 * YIELD parport_yield_blocking
33 * read/write read or write in current IEEE 1284 protocol
34 * select wait for interrupt (in readfds)
37 #include <linux/module.h>
38 #include <linux/sched.h>
39 #include <linux/ioctl.h>
40 #include <linux/parport.h>
41 #include <linux/ctype.h>
42 #include <linux/poll.h>
43 #include <asm/uaccess.h>
46 #define PP_VERSION "ppdev: user-space parallel port driver"
47 #define CHRDEV "ppdev"
50 #define min(a,b) ((a) < (b) ? (a) : (b))
54 struct pardevice
* pdev
;
55 wait_queue_head_t irq_wait
;
61 /* pp_struct.flags bitfields */
62 #define PP_CLAIMED (1<<0)
63 #define PP_EXCL (1<<1)
66 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
67 #define PP_BUFFER_SIZE 256
68 #define PARDEVICE_MAX 8
70 static inline void enable_irq (struct pp_struct
*pp
)
72 struct parport
*port
= pp
->pdev
->port
;
73 port
->ops
->enable_irq (port
);
76 static loff_t
pp_lseek (struct file
* file
, long long offset
, int origin
)
81 /* This looks a bit like parport_read. The difference is that we don't
82 * determine the mode to use from the port data, but rather from the
83 * mode the driver told us to use. */
84 static ssize_t
do_read (struct pp_struct
*pp
, void *buf
, size_t len
)
86 size_t (*fn
) (struct parport
*, void *, size_t, int);
87 struct parport
*port
= pp
->pdev
->port
;
88 int addr
= pp
->mode
& IEEE1284_ADDR
;
89 int mode
= pp
->mode
& ~(IEEE1284_DEVICEID
| IEEE1284_ADDR
);
92 case IEEE1284_MODE_COMPAT
:
93 /* This is a write-only mode. */
96 case IEEE1284_MODE_NIBBLE
:
97 fn
= port
->ops
->nibble_read_data
;
100 case IEEE1284_MODE_BYTE
:
101 fn
= port
->ops
->byte_read_data
;
104 case IEEE1284_MODE_EPP
:
106 fn
= port
->ops
->epp_read_addr
;
108 fn
= port
->ops
->epp_read_data
;
111 case IEEE1284_MODE_ECP
:
112 case IEEE1284_MODE_ECPRLE
:
113 fn
= port
->ops
->ecp_read_data
;
116 case IEEE1284_MODE_ECPSWE
:
117 fn
= parport_ieee1284_ecp_read_data
;
121 printk (KERN_DEBUG
"%s: unknown mode 0x%02x\n",
122 pp
->pdev
->name
, pp
->mode
);
126 return (*fn
) (port
, buf
, len
, 0);
129 /* This looks a bit like parport_write. The difference is that we don't
130 * determine the mode to use from the port data, but rather from the
131 * mode the driver told us to use. */
132 static ssize_t
do_write (struct pp_struct
*pp
, const void *buf
, size_t len
)
134 size_t (*fn
) (struct parport
*, const void *, size_t, int);
135 struct parport
*port
= pp
->pdev
->port
;
136 int addr
= pp
->mode
& IEEE1284_ADDR
;
137 int mode
= pp
->mode
& ~(IEEE1284_DEVICEID
| IEEE1284_ADDR
);
140 case IEEE1284_MODE_NIBBLE
:
141 case IEEE1284_MODE_BYTE
:
142 /* Read-only modes. */
145 case IEEE1284_MODE_COMPAT
:
146 fn
= port
->ops
->compat_write_data
;
149 case IEEE1284_MODE_EPP
:
151 fn
= port
->ops
->epp_write_addr
;
153 fn
= port
->ops
->epp_write_data
;
156 case IEEE1284_MODE_ECP
:
157 case IEEE1284_MODE_ECPRLE
:
159 fn
= port
->ops
->ecp_write_addr
;
161 fn
= port
->ops
->ecp_write_data
;
164 case IEEE1284_MODE_ECPSWE
:
166 fn
= parport_ieee1284_ecp_write_addr
;
168 fn
= parport_ieee1284_ecp_write_data
;
172 printk (KERN_DEBUG
"%s: unknown mode 0x%02x\n",
173 pp
->pdev
->name
, pp
->mode
);
177 return (*fn
) (port
, buf
, len
, 0);
180 static ssize_t
pp_read (struct file
* file
, char * buf
, size_t count
,
183 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
184 struct pp_struct
*pp
= file
->private_data
;
186 ssize_t bytes_read
= 0;
189 if (!(pp
->flags
& PP_CLAIMED
)) {
190 /* Don't have the port claimed */
191 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
196 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
200 while (bytes_read
< count
) {
201 ssize_t need
= min(count
- bytes_read
, PP_BUFFER_SIZE
);
203 got
= do_read (pp
, kbuffer
, need
);
212 if (copy_to_user (buf
+ bytes_read
, kbuffer
, got
)) {
213 bytes_read
= -EFAULT
;
219 if (signal_pending (current
)) {
225 if (current
->need_resched
)
234 static ssize_t
pp_write (struct file
* file
, const char * buf
, size_t count
,
237 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
238 struct pp_struct
*pp
= file
->private_data
;
240 ssize_t bytes_written
= 0;
243 if (!(pp
->flags
& PP_CLAIMED
)) {
244 /* Don't have the port claimed */
245 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
250 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
254 while (bytes_written
< count
) {
255 ssize_t n
= min(count
- bytes_written
, PP_BUFFER_SIZE
);
257 if (copy_from_user (kbuffer
, buf
+ bytes_written
, n
)) {
258 bytes_written
= -EFAULT
;
262 wrote
= do_write (pp
, kbuffer
, n
);
266 bytes_written
= wrote
;
270 bytes_written
+= wrote
;
272 if (signal_pending (current
)) {
274 bytes_written
= -EINTR
;
278 if (current
->need_resched
)
284 return bytes_written
;
287 static void pp_irq (int irq
, void * private, struct pt_regs
* unused
)
289 struct pp_struct
* pp
= (struct pp_struct
*) private;
291 wake_up_interruptible (&pp
->irq_wait
);
294 static int register_device (int minor
, struct pp_struct
*pp
)
296 struct parport
* port
;
297 struct pardevice
* pdev
= NULL
;
301 name
= kmalloc (strlen (CHRDEV
) + 3, GFP_KERNEL
);
305 sprintf (name
, CHRDEV
"%x", minor
);
306 port
= parport_enumerate (); /* FIXME: use attach/detach */
308 while (port
&& port
->number
!= minor
)
312 printk (KERN_WARNING
"%s: no associated port!\n", name
);
317 fl
= (pp
->flags
& PP_EXCL
) ? PARPORT_FLAG_EXCL
: 0;
318 pdev
= parport_register_device (port
, name
, NULL
, NULL
, pp_irq
, fl
,
322 printk (KERN_WARNING
"%s: failed to register device!\n", name
);
328 printk (KERN_DEBUG
"%s: registered pardevice\n", name
);
332 static int pp_ioctl(struct inode
*inode
, struct file
*file
,
333 unsigned int cmd
, unsigned long arg
)
335 unsigned int minor
= MINOR(inode
->i_rdev
);
336 struct pp_struct
*pp
= file
->private_data
;
337 struct parport
* port
;
339 /* First handle the cases that don't take arguments. */
340 if (cmd
== PPCLAIM
) {
341 if (pp
->flags
& PP_CLAIMED
) {
342 printk (KERN_DEBUG CHRDEV
343 "%x: you've already got it!\n", minor
);
347 /* Deferred device registration. */
349 int err
= register_device (minor
, pp
);
354 parport_claim_or_block (pp
->pdev
);
355 pp
->flags
|= PP_CLAIMED
;
357 /* For interrupt-reporting to work, we need to be
358 * informed of each interrupt. */
365 printk (KERN_DEBUG CHRDEV
"%x: too late for PPEXCL; "
366 "already registered\n", minor
);
367 if (pp
->flags
& PP_EXCL
)
368 /* But it's not really an error. */
370 /* There's no chance of making the driver happy. */
374 /* Just remember to register the device exclusively
375 * when we finally do the registration. */
376 pp
->flags
|= PP_EXCL
;
380 if (cmd
== PPSETMODE
) {
382 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
384 /* FIXME: validate mode */
389 /* Everything else requires the port to be claimed, so check
391 if ((pp
->flags
& PP_CLAIMED
) == 0) {
392 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
397 port
= pp
->pdev
->port
;
405 reg
= parport_read_status (port
);
406 return copy_to_user ((unsigned char *) arg
, ®
,
410 reg
= parport_read_data (port
);
411 return copy_to_user ((unsigned char *) arg
, ®
,
415 reg
= parport_read_control (port
);
416 return copy_to_user ((unsigned char *) arg
, ®
,
420 parport_yield_blocking (pp
->pdev
);
424 parport_release (pp
->pdev
);
425 pp
->flags
&= ~PP_CLAIMED
;
429 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
431 parport_write_control (port
, reg
);
435 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
437 parport_write_data (port
, reg
);
441 if (copy_from_user (&mask
, (unsigned char *) arg
,
444 if (copy_from_user (®
, 1 + (unsigned char *) arg
,
447 parport_frob_control (port
, mask
, reg
);
451 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
454 port
->ops
->data_reverse (port
);
456 port
->ops
->data_forward (port
);
460 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
462 /* FIXME: validate mode */
463 ret
= parport_negotiate (port
, mode
);
468 printk (KERN_DEBUG CHRDEV
"%x: What? (cmd=0x%x)\n", minor
,
473 /* Keep the compiler happy */
477 static int pp_open (struct inode
* inode
, struct file
* file
)
479 unsigned int minor
= MINOR (inode
->i_rdev
);
480 struct pp_struct
*pp
;
482 if (minor
>= PARPORT_MAX
)
485 pp
= kmalloc (GFP_KERNEL
, sizeof (struct pp_struct
));
489 pp
->mode
= IEEE1284_MODE_COMPAT
;
492 init_waitqueue_head (&pp
->irq_wait
);
494 /* Defer the actual device registration until the first claim.
495 * That way, we know whether or not the driver wants to have
496 * exclusive access to the port (PPEXCL).
499 file
->private_data
= pp
;
505 static int pp_release (struct inode
* inode
, struct file
* file
)
507 unsigned int minor
= MINOR (inode
->i_rdev
);
508 struct pp_struct
*pp
= file
->private_data
;
510 if (pp
->flags
& PP_CLAIMED
) {
511 parport_release (pp
->pdev
);
512 printk (KERN_DEBUG CHRDEV
"%x: released pardevice because "
513 "user-space forgot\n", minor
);
517 kfree (pp
->pdev
->name
);
518 parport_unregister_device (pp
->pdev
);
520 printk (KERN_DEBUG CHRDEV
"%x: unregistered pardevice\n",
530 static unsigned int pp_poll (struct file
* file
, poll_table
* wait
)
532 struct pp_struct
*pp
= file
->private_data
;
533 unsigned int mask
= 0;
536 mask
|= POLLIN
| POLLRDNORM
;
538 poll_wait (file
, &pp
->irq_wait
, wait
);
542 static struct file_operations pp_fops
= {
546 NULL
, /* pp_readdir */
556 #define pp_init init_module
561 if (register_chrdev (PP_MAJOR
, CHRDEV
, &pp_fops
)) {
562 printk (KERN_WARNING CHRDEV
": unable to get major %d\n",
567 printk (KERN_INFO PP_VERSION
"\n");
572 void cleanup_module (void)
574 /* Clean up all parport stuff */
575 unregister_chrdev (PP_MAJOR
, CHRDEV
);