Import 2.3.47pre4
[davej-history.git] / drivers / char / ppdev.c
blobd73c0969520391fff35d756ec48a15879a44da01
1 /*
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)
19 * ioctl
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
27 * WDATA write_data
28 * RDATA read_data
29 * WCONTROL write_control
30 * RCONTROL read_control
31 * FCONTROL frob_control
32 * RSTATUS read_status
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"
59 #ifndef min
60 #define min(a,b) ((a) < (b) ? (a) : (b))
61 #endif
63 struct pp_struct {
64 struct pardevice * pdev;
65 wait_queue_head_t irq_wait;
66 atomic_t irqc;
67 unsigned int flags;
68 int irqresponse;
69 unsigned char irqctl;
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)
78 /* Other constants */
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)
94 return -ESPIPE;
97 static ssize_t pp_read (struct file * file, char * buf, size_t count,
98 loff_t * ppos)
100 unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
101 struct pp_struct *pp = file->private_data;
102 char * kbuffer;
103 ssize_t bytes_read = 0;
104 ssize_t got = 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",
109 minor);
110 return -EINVAL;
113 kbuffer = kmalloc (min (count, PP_BUFFER_SIZE), GFP_KERNEL);
114 if (!kbuffer)
115 return -ENOMEM;
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);
122 if (got <= 0) {
123 if (!bytes_read)
124 bytes_read = got;
126 break;
129 if (copy_to_user (buf + bytes_read, kbuffer, got)) {
130 bytes_read = -EFAULT;
131 break;
134 bytes_read += got;
136 if (signal_pending (current)) {
137 if (!bytes_read)
138 bytes_read = -EINTR;
139 break;
142 if (current->need_resched)
143 schedule ();
146 kfree (kbuffer);
147 enable_irq (pp);
148 return bytes_read;
151 static ssize_t pp_write (struct file * file, const char * buf, size_t count,
152 loff_t * ppos)
154 unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
155 struct pp_struct *pp = file->private_data;
156 char * kbuffer;
157 ssize_t bytes_written = 0;
158 ssize_t wrote;
160 if (!(pp->flags & PP_CLAIMED)) {
161 /* Don't have the port claimed */
162 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
163 minor);
164 return -EINVAL;
167 kbuffer = kmalloc (min (count, PP_BUFFER_SIZE), GFP_KERNEL);
168 if (!kbuffer)
169 return -ENOMEM;
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;
176 break;
179 wrote = parport_write (pp->pdev->port, kbuffer, n);
181 if (wrote < 0) {
182 if (!bytes_written)
183 bytes_written = wrote;
184 break;
187 bytes_written += wrote;
189 if (signal_pending (current)) {
190 if (!bytes_written)
191 bytes_written = -EINTR;
192 break;
195 if (current->need_resched)
196 schedule ();
199 kfree (kbuffer);
200 enable_irq (pp);
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);
210 pp->irqresponse = 0;
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;
221 char *name;
222 int fl;
224 name = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
225 if (name == NULL)
226 return -ENOMEM;
228 sprintf (name, CHRDEV "%x", minor);
229 port = parport_enumerate (); /* FIXME: use attach/detach */
231 while (port && port->number != minor)
232 port = port->next;
234 if (!port) {
235 printk (KERN_WARNING "%s: no associated port!\n", name);
236 kfree (name);
237 return -ENXIO;
240 fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
241 pdev = parport_register_device (port, name, NULL, NULL, pp_irq, fl,
242 pp);
244 if (!pdev) {
245 printk (KERN_WARNING "%s: failed to register device!\n", name);
246 kfree (name);
247 return -ENXIO;
250 pp->pdev = pdev;
251 printk (KERN_DEBUG "%s: registered pardevice\n", name);
252 return 0;
255 static enum ieee1284_phase init_phase (int mode)
257 switch (mode & ~(IEEE1284_DEVICEID
258 | IEEE1284_ADDR)) {
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);
280 return -EINVAL;
283 /* Deferred device registration. */
284 if (!pp->pdev) {
285 int err = register_device (minor, pp);
286 if (err)
287 return err;
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. */
295 enable_irq (pp);
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;
304 return 0;
307 if (cmd == PPEXCL) {
308 if (pp->pdev) {
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. */
313 return 0;
314 /* There's no chance of making the driver happy. */
315 return -EINVAL;
318 /* Just remember to register the device exclusively
319 * when we finally do the registration. */
320 pp->flags |= PP_EXCL;
321 return 0;
324 if (cmd == PPSETMODE) {
325 int mode;
326 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
327 return -EFAULT;
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;
337 return 0;
340 if (cmd == PPSETPHASE) {
341 int phase;
342 if (copy_from_user (&phase, (int *) arg, sizeof (phase)))
343 return -EFAULT;
344 /* FIXME: validate phase */
345 pp->state.phase = phase;
347 if (pp->flags & PP_CLAIMED)
348 pp->pdev->port->ieee1284.phase = phase;
350 return 0;
353 /* Everything else requires the port to be claimed, so check
354 * that now. */
355 if ((pp->flags & PP_CLAIMED) == 0) {
356 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
357 minor);
358 return -EINVAL;
361 port = pp->pdev->port;
362 switch (cmd) {
363 struct ieee1284_info *info;
364 unsigned char reg;
365 unsigned char mask;
366 int mode;
367 int ret;
368 struct timeval par_timeout;
369 long to_jiffies;
371 case PPRSTATUS:
372 reg = parport_read_status (port);
373 return copy_to_user ((unsigned char *) arg, &reg,
374 sizeof (reg));
376 case PPRDATA:
377 reg = parport_read_data (port);
378 return copy_to_user ((unsigned char *) arg, &reg,
379 sizeof (reg));
381 case PPRCONTROL:
382 reg = parport_read_control (port);
383 return copy_to_user ((unsigned char *) arg, &reg,
384 sizeof (reg));
386 case PPYIELD:
387 parport_yield_blocking (pp->pdev);
388 return 0;
390 case PPRELEASE:
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;
399 return 0;
401 case PPWCONTROL:
402 if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
403 return -EFAULT;
404 parport_write_control (port, reg);
405 return 0;
407 case PPWDATA:
408 if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
409 return -EFAULT;
410 parport_write_data (port, reg);
411 return 0;
413 case PPFCONTROL:
414 if (copy_from_user (&mask, (unsigned char *) arg,
415 sizeof (mask)))
416 return -EFAULT;
417 if (copy_from_user (&reg, 1 + (unsigned char *) arg,
418 sizeof (reg)))
419 return -EFAULT;
420 parport_frob_control (port, mask, reg);
421 return 0;
423 case PPDATADIR:
424 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
425 return -EFAULT;
426 if (mode)
427 port->ops->data_reverse (port);
428 else
429 port->ops->data_forward (port);
430 return 0;
432 case PPNEGOT:
433 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
434 return -EFAULT;
435 switch ((ret = parport_negotiate (port, mode))) {
436 case 0: break;
437 case -1: /* handshake failed, peripheral not IEEE 1284 */
438 ret = -EIO;
439 break;
440 case 1: /* handshake succeeded, peripheral rejected mode */
441 ret = -ENXIO;
442 break;
444 enable_irq (pp);
445 return ret;
447 case PPWCTLONIRQ:
448 if (copy_from_user (&reg, (unsigned char *) arg,
449 sizeof (reg)))
450 return -EFAULT;
452 /* Remember what to set the control lines to, for next
453 * time we get an interrupt. */
454 pp->irqctl = reg;
455 pp->irqresponse = 1;
456 return 0;
458 case PPCLRIRQ:
459 ret = atomic_read (&pp->irqc);
460 if (copy_to_user ((int *) arg, &ret, sizeof (ret)))
461 return -EFAULT;
462 atomic_sub (ret, &pp->irqc);
463 return 0;
465 case PPSETTIME:
466 if (copy_from_user (&par_timeout, (struct timeval *)arg,
467 sizeof(struct timeval))) {
468 return -EFAULT;
470 /* Convert to jiffies, place in pp->pdev->timeout */
471 if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
472 return -EINVAL;
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) {
477 return -EINVAL;
479 pp->pdev->timeout = to_jiffies;
480 return 0;
482 case PPGETTIME:
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))) {
488 return -EFAULT;
490 return 0;
492 default:
493 printk (KERN_DEBUG CHRDEV "%x: What? (cmd=0x%x)\n", minor,
494 cmd);
495 return -EINVAL;
498 /* Keep the compiler happy */
499 return 0;
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)
508 return -ENXIO;
510 MOD_INC_USE_COUNT;
512 pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
513 if (!pp) {
514 MOD_DEC_USE_COUNT;
515 return -ENOMEM;
518 pp->state.mode = IEEE1284_MODE_COMPAT;
519 pp->state.phase = init_phase (pp->state.mode);
520 pp->flags = 0;
521 pp->irqresponse = 0;
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).
529 pp->pdev = NULL;
530 file->private_data = pp;
532 return 0;
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);
546 if (pp->pdev) {
547 const char *name = pp->pdev->name;
548 parport_unregister_device (pp->pdev);
549 kfree (name);
550 pp->pdev = NULL;
551 printk (KERN_DEBUG CHRDEV "%x: unregistered pardevice\n",
552 minor);
555 kfree (pp);
557 MOD_DEC_USE_COUNT;
558 return 0;
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);
570 return mask;
573 static struct file_operations pp_fops = {
574 llseek: pp_lseek,
575 read: pp_read,
576 write: pp_write,
577 poll: pp_poll,
578 ioctl: pp_ioctl,
579 open: pp_open,
580 release: pp_release,
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",
589 PP_MAJOR);
590 return -EIO;
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,
596 &pp_fops, NULL);
598 printk (KERN_INFO PP_VERSION "\n");
599 return 0;
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);