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 * WCTLONIRQ on interrupt, set control lines
34 * CLRIRQ clear (and return) interrupt count
35 * read/write read or write in current IEEE 1284 protocol
36 * select wait for interrupt (in readfds)
39 #include <linux/module.h>
40 #include <linux/sched.h>
41 #include <linux/ioctl.h>
42 #include <linux/parport.h>
43 #include <linux/ctype.h>
44 #include <linux/poll.h>
45 #include <asm/uaccess.h>
48 #define PP_VERSION "ppdev: user-space parallel port driver"
49 #define CHRDEV "ppdev"
52 #define min(a,b) ((a) < (b) ? (a) : (b))
56 struct pardevice
* pdev
;
57 wait_queue_head_t irq_wait
;
63 struct ieee1284_info state
;
64 struct ieee1284_info saved_state
;
67 /* pp_struct.flags bitfields */
68 #define PP_CLAIMED (1<<0)
69 #define PP_EXCL (1<<1)
72 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
73 #define PP_BUFFER_SIZE 256
74 #define PARDEVICE_MAX 8
76 static inline void enable_irq (struct pp_struct
*pp
)
78 struct parport
*port
= pp
->pdev
->port
;
79 port
->ops
->enable_irq (port
);
82 static loff_t
pp_lseek (struct file
* file
, long long offset
, int origin
)
87 /* This looks a bit like parport_read. The difference is that we don't
88 * determine the mode to use from the port data, but rather from the
89 * mode the driver told us to use. */
90 static ssize_t
do_read (struct pp_struct
*pp
, void *buf
, size_t len
)
92 size_t (*fn
) (struct parport
*, void *, size_t, int);
93 struct parport
*port
= pp
->pdev
->port
;
94 int addr
= pp
->mode
& IEEE1284_ADDR
;
95 int mode
= pp
->mode
& ~(IEEE1284_DEVICEID
| IEEE1284_ADDR
);
98 case IEEE1284_MODE_COMPAT
:
99 /* This is a write-only mode. */
102 case IEEE1284_MODE_NIBBLE
:
103 fn
= port
->ops
->nibble_read_data
;
106 case IEEE1284_MODE_BYTE
:
107 fn
= port
->ops
->byte_read_data
;
110 case IEEE1284_MODE_EPP
:
112 fn
= port
->ops
->epp_read_addr
;
114 fn
= port
->ops
->epp_read_data
;
117 case IEEE1284_MODE_ECP
:
118 case IEEE1284_MODE_ECPRLE
:
119 fn
= port
->ops
->ecp_read_data
;
122 case IEEE1284_MODE_ECPSWE
:
123 fn
= parport_ieee1284_ecp_read_data
;
127 printk (KERN_DEBUG
"%s: unknown mode 0x%02x\n",
128 pp
->pdev
->name
, pp
->mode
);
132 return (*fn
) (port
, buf
, len
, 0);
135 /* This looks a bit like parport_write. The difference is that we don't
136 * determine the mode to use from the port data, but rather from the
137 * mode the driver told us to use. */
138 static ssize_t
do_write (struct pp_struct
*pp
, const void *buf
, size_t len
)
140 size_t (*fn
) (struct parport
*, const void *, size_t, int);
141 struct parport
*port
= pp
->pdev
->port
;
142 int addr
= pp
->mode
& IEEE1284_ADDR
;
143 int mode
= pp
->mode
& ~(IEEE1284_DEVICEID
| IEEE1284_ADDR
);
146 case IEEE1284_MODE_NIBBLE
:
147 case IEEE1284_MODE_BYTE
:
148 /* Read-only modes. */
151 case IEEE1284_MODE_COMPAT
:
152 fn
= port
->ops
->compat_write_data
;
155 case IEEE1284_MODE_EPP
:
157 fn
= port
->ops
->epp_write_addr
;
159 fn
= port
->ops
->epp_write_data
;
162 case IEEE1284_MODE_ECP
:
163 case IEEE1284_MODE_ECPRLE
:
165 fn
= port
->ops
->ecp_write_addr
;
167 fn
= port
->ops
->ecp_write_data
;
170 case IEEE1284_MODE_ECPSWE
:
172 fn
= parport_ieee1284_ecp_write_addr
;
174 fn
= parport_ieee1284_ecp_write_data
;
178 printk (KERN_DEBUG
"%s: unknown mode 0x%02x\n",
179 pp
->pdev
->name
, pp
->mode
);
183 return (*fn
) (port
, buf
, len
, 0);
186 static ssize_t
pp_read (struct file
* file
, char * buf
, size_t count
,
189 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
190 struct pp_struct
*pp
= file
->private_data
;
192 ssize_t bytes_read
= 0;
195 if (!(pp
->flags
& PP_CLAIMED
)) {
196 /* Don't have the port claimed */
197 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
202 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
206 while (bytes_read
< count
) {
207 ssize_t need
= min(count
- bytes_read
, PP_BUFFER_SIZE
);
209 got
= do_read (pp
, kbuffer
, need
);
218 if (copy_to_user (buf
+ bytes_read
, kbuffer
, got
)) {
219 bytes_read
= -EFAULT
;
225 if (signal_pending (current
)) {
231 if (current
->need_resched
)
240 static ssize_t
pp_write (struct file
* file
, const char * buf
, size_t count
,
243 unsigned int minor
= MINOR (file
->f_dentry
->d_inode
->i_rdev
);
244 struct pp_struct
*pp
= file
->private_data
;
246 ssize_t bytes_written
= 0;
249 if (!(pp
->flags
& PP_CLAIMED
)) {
250 /* Don't have the port claimed */
251 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
256 kbuffer
= kmalloc (min (count
, PP_BUFFER_SIZE
), GFP_KERNEL
);
260 while (bytes_written
< count
) {
261 ssize_t n
= min(count
- bytes_written
, PP_BUFFER_SIZE
);
263 if (copy_from_user (kbuffer
, buf
+ bytes_written
, n
)) {
264 bytes_written
= -EFAULT
;
268 wrote
= do_write (pp
, kbuffer
, n
);
272 bytes_written
= wrote
;
276 bytes_written
+= wrote
;
278 if (signal_pending (current
)) {
280 bytes_written
= -EINTR
;
284 if (current
->need_resched
)
290 return bytes_written
;
293 static void pp_irq (int irq
, void * private, struct pt_regs
* unused
)
295 struct pp_struct
* pp
= (struct pp_struct
*) private;
297 if (pp
->irqresponse
) {
298 parport_write_control (pp
->pdev
->port
, pp
->irqctl
);
302 atomic_inc (&pp
->irqc
);
303 wake_up_interruptible (&pp
->irq_wait
);
306 static int register_device (int minor
, struct pp_struct
*pp
)
308 struct parport
* port
;
309 struct pardevice
* pdev
= NULL
;
313 name
= kmalloc (strlen (CHRDEV
) + 3, GFP_KERNEL
);
317 sprintf (name
, CHRDEV
"%x", minor
);
318 port
= parport_enumerate (); /* FIXME: use attach/detach */
320 while (port
&& port
->number
!= minor
)
324 printk (KERN_WARNING
"%s: no associated port!\n", name
);
329 fl
= (pp
->flags
& PP_EXCL
) ? PARPORT_FLAG_EXCL
: 0;
330 pdev
= parport_register_device (port
, name
, NULL
, NULL
, pp_irq
, fl
,
334 printk (KERN_WARNING
"%s: failed to register device!\n", name
);
340 printk (KERN_DEBUG
"%s: registered pardevice\n", name
);
344 static int pp_ioctl(struct inode
*inode
, struct file
*file
,
345 unsigned int cmd
, unsigned long arg
)
347 unsigned int minor
= MINOR(inode
->i_rdev
);
348 struct pp_struct
*pp
= file
->private_data
;
349 struct parport
* port
;
351 /* First handle the cases that don't take arguments. */
352 if (cmd
== PPCLAIM
) {
353 struct ieee1284_info
*info
;
356 if (pp
->flags
& PP_CLAIMED
) {
357 printk (KERN_DEBUG CHRDEV
358 "%x: you've already got it!\n", minor
);
362 /* Deferred device registration. */
364 int err
= register_device (minor
, pp
);
371 parport_claim_or_block (pp
->pdev
);
372 pp
->flags
|= PP_CLAIMED
;
374 /* For interrupt-reporting to work, we need to be
375 * informed of each interrupt. */
378 /* We may need to fix up the state machine. */
379 info
= &pp
->pdev
->port
->ieee1284
;
380 pp
->saved_state
.mode
= info
->mode
;
381 pp
->saved_state
.phase
= info
->phase
;
382 if (pp
->mode
!= info
->mode
) {
383 int phase
= IEEE1284_PH_FWD_IDLE
;
386 info
->mode
= pp
->mode
;
387 switch (pp
->mode
& ~(IEEE1284_DEVICEID
389 case IEEE1284_MODE_NIBBLE
:
390 case IEEE1284_MODE_BYTE
:
391 phase
= IEEE1284_PH_REV_IDLE
;
395 /* Just restore the state. */
396 info
->mode
= pp
->state
.mode
;
397 info
->phase
= pp
->state
.phase
;
406 printk (KERN_DEBUG CHRDEV
"%x: too late for PPEXCL; "
407 "already registered\n", minor
);
408 if (pp
->flags
& PP_EXCL
)
409 /* But it's not really an error. */
411 /* There's no chance of making the driver happy. */
415 /* Just remember to register the device exclusively
416 * when we finally do the registration. */
417 pp
->flags
|= PP_EXCL
;
421 if (cmd
== PPSETMODE
) {
423 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
425 /* FIXME: validate mode */
430 /* Everything else requires the port to be claimed, so check
432 if ((pp
->flags
& PP_CLAIMED
) == 0) {
433 printk (KERN_DEBUG CHRDEV
"%x: claim the port first\n",
438 port
= pp
->pdev
->port
;
440 struct ieee1284_info
*info
;
447 reg
= parport_read_status (port
);
448 return copy_to_user ((unsigned char *) arg
, ®
,
452 reg
= parport_read_data (port
);
453 return copy_to_user ((unsigned char *) arg
, ®
,
457 reg
= parport_read_control (port
);
458 return copy_to_user ((unsigned char *) arg
, ®
,
462 parport_yield_blocking (pp
->pdev
);
466 /* Save the state machine's state. */
467 info
= &pp
->pdev
->port
->ieee1284
;
468 pp
->state
.mode
= info
->mode
;
469 pp
->state
.phase
= info
->phase
;
470 info
->mode
= pp
->saved_state
.mode
;
471 info
->phase
= pp
->saved_state
.phase
;
472 parport_release (pp
->pdev
);
473 pp
->flags
&= ~PP_CLAIMED
;
477 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
479 parport_write_control (port
, reg
);
483 if (copy_from_user (®
, (unsigned char *) arg
, sizeof (reg
)))
485 parport_write_data (port
, reg
);
489 if (copy_from_user (&mask
, (unsigned char *) arg
,
492 if (copy_from_user (®
, 1 + (unsigned char *) arg
,
495 parport_frob_control (port
, mask
, reg
);
499 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
502 port
->ops
->data_reverse (port
);
504 port
->ops
->data_forward (port
);
508 if (copy_from_user (&mode
, (int *) arg
, sizeof (mode
)))
510 switch ((ret
= parport_negotiate (port
, mode
))) {
512 case -1: /* handshake failed, peripheral not IEEE 1284 */
515 case 1: /* handshake succeeded, peripheral rejected mode */
523 if (copy_from_user (®
, (unsigned char *) arg
,
527 /* Remember what to set the control lines to, for next
528 * time we get an interrupt. */
534 ret
= atomic_read (&pp
->irqc
);
535 if (copy_to_user ((int *) arg
, &ret
, sizeof (ret
)))
537 atomic_sub (ret
, &pp
->irqc
);
541 printk (KERN_DEBUG CHRDEV
"%x: What? (cmd=0x%x)\n", minor
,
546 /* Keep the compiler happy */
550 static int pp_open (struct inode
* inode
, struct file
* file
)
552 unsigned int minor
= MINOR (inode
->i_rdev
);
553 struct pp_struct
*pp
;
555 if (minor
>= PARPORT_MAX
)
558 pp
= kmalloc (GFP_KERNEL
, sizeof (struct pp_struct
));
562 pp
->mode
= IEEE1284_MODE_COMPAT
;
564 atomic_set (&pp
->irqc
, 0);
565 init_waitqueue_head (&pp
->irq_wait
);
567 /* Defer the actual device registration until the first claim.
568 * That way, we know whether or not the driver wants to have
569 * exclusive access to the port (PPEXCL).
572 file
->private_data
= pp
;
578 static int pp_release (struct inode
* inode
, struct file
* file
)
580 unsigned int minor
= MINOR (inode
->i_rdev
);
581 struct pp_struct
*pp
= file
->private_data
;
583 if (pp
->flags
& PP_CLAIMED
) {
584 parport_release (pp
->pdev
);
585 printk (KERN_DEBUG CHRDEV
"%x: released pardevice because "
586 "user-space forgot\n", minor
);
590 kfree (pp
->pdev
->name
);
591 parport_unregister_device (pp
->pdev
);
593 printk (KERN_DEBUG CHRDEV
"%x: unregistered pardevice\n",
603 static unsigned int pp_poll (struct file
* file
, poll_table
* wait
)
605 struct pp_struct
*pp
= file
->private_data
;
606 unsigned int mask
= 0;
608 if (atomic_read (&pp
->irqc
))
609 mask
|= POLLIN
| POLLRDNORM
;
611 poll_wait (file
, &pp
->irq_wait
, wait
);
615 static struct file_operations pp_fops
= {
619 NULL
, /* pp_readdir */
629 #define pp_init init_module
634 if (register_chrdev (PP_MAJOR
, CHRDEV
, &pp_fops
)) {
635 printk (KERN_WARNING CHRDEV
": unable to get major %d\n",
640 printk (KERN_INFO PP_VERSION
"\n");
645 void cleanup_module (void)
647 /* Clean up all parport stuff */
648 unregister_chrdev (PP_MAJOR
, CHRDEV
);