Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / ppdev.c
blob4e2be3c8b32498230dbf0101cd80c926eab54cd6
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 struct pp_port_list_struct {
87 struct parport *port;
88 struct pp_port_list_struct *next;
90 static struct pp_port_list_struct *pp_port_list;
91 static DECLARE_MUTEX(pp_port_list_lock);
93 /* pp_attach and pp_detach are for keeping a list of currently
94 * available ports, held under a mutex. We do this rather than
95 * using parport_enumerate because it stops a load of races.
98 static void pp_attach (struct parport *port)
100 struct pp_port_list_struct *add;
102 add = kmalloc (sizeof (struct pp_port_list_struct), GFP_KERNEL);
103 if (!add) {
104 printk (KERN_WARNING CHRDEV ": memory squeeze\n");
105 return;
108 add->port = port;
109 down (&pp_port_list_lock);
110 add->next = pp_port_list;
111 pp_port_list = add;
112 up (&pp_port_list_lock);
115 static void pp_detach (struct parport *port)
117 struct pp_port_list_struct *del;
119 down (&pp_port_list_lock);
120 del = pp_port_list;
121 if (del->port == port)
122 pp_port_list = del->next;
123 else {
124 struct pp_port_list_struct *prev;
125 do {
126 prev = del;
127 del = del->next;
128 } while (del && del->port != port);
129 if (del)
130 prev->next = del->next;
132 up (&pp_port_list_lock);
134 if (del)
135 kfree (del);
138 static struct parport_driver ppdev_driver = {
139 name: CHRDEV,
140 attach: pp_attach,
141 detach: pp_detach
144 static inline void pp_enable_irq (struct pp_struct *pp)
146 struct parport *port = pp->pdev->port;
147 port->ops->enable_irq (port);
150 static loff_t pp_lseek (struct file * file, long long offset, int origin)
152 return -ESPIPE;
155 static ssize_t pp_read (struct file * file, char * buf, size_t count,
156 loff_t * ppos)
158 unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
159 struct pp_struct *pp = file->private_data;
160 char * kbuffer;
161 ssize_t bytes_read = 0;
162 ssize_t got = 0;
164 if (!(pp->flags & PP_CLAIMED)) {
165 /* Don't have the port claimed */
166 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
167 minor);
168 return -EINVAL;
171 kbuffer = kmalloc (min (count, PP_BUFFER_SIZE), GFP_KERNEL);
172 if (!kbuffer)
173 return -ENOMEM;
175 while (bytes_read < count) {
176 ssize_t need = min(count - bytes_read, PP_BUFFER_SIZE);
178 got = parport_read (pp->pdev->port, kbuffer, need);
180 if (got <= 0) {
181 if (!bytes_read)
182 bytes_read = got;
184 break;
187 if (copy_to_user (buf + bytes_read, kbuffer, got)) {
188 bytes_read = -EFAULT;
189 break;
192 bytes_read += got;
194 if (signal_pending (current)) {
195 if (!bytes_read)
196 bytes_read = -EINTR;
197 break;
200 if (current->need_resched)
201 schedule ();
204 kfree (kbuffer);
205 pp_enable_irq (pp);
206 return bytes_read;
209 static ssize_t pp_write (struct file * file, const char * buf, size_t count,
210 loff_t * ppos)
212 unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
213 struct pp_struct *pp = file->private_data;
214 char * kbuffer;
215 ssize_t bytes_written = 0;
216 ssize_t wrote;
218 if (!(pp->flags & PP_CLAIMED)) {
219 /* Don't have the port claimed */
220 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
221 minor);
222 return -EINVAL;
225 kbuffer = kmalloc (min (count, PP_BUFFER_SIZE), GFP_KERNEL);
226 if (!kbuffer)
227 return -ENOMEM;
229 while (bytes_written < count) {
230 ssize_t n = min(count - bytes_written, PP_BUFFER_SIZE);
232 if (copy_from_user (kbuffer, buf + bytes_written, n)) {
233 bytes_written = -EFAULT;
234 break;
237 wrote = parport_write (pp->pdev->port, kbuffer, n);
239 if (wrote < 0) {
240 if (!bytes_written)
241 bytes_written = wrote;
242 break;
245 bytes_written += wrote;
247 if (signal_pending (current)) {
248 if (!bytes_written)
249 bytes_written = -EINTR;
250 break;
253 if (current->need_resched)
254 schedule ();
257 kfree (kbuffer);
258 pp_enable_irq (pp);
259 return bytes_written;
262 static void pp_irq (int irq, void * private, struct pt_regs * unused)
264 struct pp_struct * pp = (struct pp_struct *) private;
266 if (pp->irqresponse) {
267 parport_write_control (pp->pdev->port, pp->irqctl);
268 pp->irqresponse = 0;
271 atomic_inc (&pp->irqc);
272 wake_up_interruptible (&pp->irq_wait);
275 static int register_device (int minor, struct pp_struct *pp)
277 struct pp_port_list_struct *ports;
278 struct pardevice * pdev = NULL;
279 char *name;
280 int fl;
282 name = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
283 if (name == NULL)
284 return -ENOMEM;
286 sprintf (name, CHRDEV "%x", minor);
288 down (&pp_port_list_lock);
289 ports = pp_port_list;
290 while (ports && ports->port->number != minor)
291 ports = ports->next;
292 if (ports->port) {
293 fl = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0;
294 pdev = parport_register_device (ports->port, name, NULL,
295 NULL, pp_irq, fl, pp);
297 up (&pp_port_list_lock);
299 if (!ports->port) {
300 printk (KERN_WARNING "%s: no associated port!\n", name);
301 kfree (name);
302 return -ENXIO;
306 if (!pdev) {
307 printk (KERN_WARNING "%s: failed to register device!\n", name);
308 kfree (name);
309 return -ENXIO;
312 pp->pdev = pdev;
313 printk (KERN_DEBUG "%s: registered pardevice\n", name);
314 return 0;
317 static enum ieee1284_phase init_phase (int mode)
319 switch (mode & ~(IEEE1284_DEVICEID
320 | IEEE1284_ADDR)) {
321 case IEEE1284_MODE_NIBBLE:
322 case IEEE1284_MODE_BYTE:
323 return IEEE1284_PH_REV_IDLE;
325 return IEEE1284_PH_FWD_IDLE;
328 static int pp_ioctl(struct inode *inode, struct file *file,
329 unsigned int cmd, unsigned long arg)
331 unsigned int minor = MINOR(inode->i_rdev);
332 struct pp_struct *pp = file->private_data;
333 struct parport * port;
335 /* First handle the cases that don't take arguments. */
336 if (cmd == PPCLAIM) {
337 struct ieee1284_info *info;
339 if (pp->flags & PP_CLAIMED) {
340 printk (KERN_DEBUG CHRDEV
341 "%x: you've already got it!\n", minor);
342 return -EINVAL;
345 /* Deferred device registration. */
346 if (!pp->pdev) {
347 int err = register_device (minor, pp);
348 if (err)
349 return err;
352 parport_claim_or_block (pp->pdev);
353 pp->flags |= PP_CLAIMED;
355 /* For interrupt-reporting to work, we need to be
356 * informed of each interrupt. */
357 pp_enable_irq (pp);
359 /* We may need to fix up the state machine. */
360 info = &pp->pdev->port->ieee1284;
361 pp->saved_state.mode = info->mode;
362 pp->saved_state.phase = info->phase;
363 info->mode = pp->state.mode;
364 info->phase = pp->state.phase;
366 return 0;
369 if (cmd == PPEXCL) {
370 if (pp->pdev) {
371 printk (KERN_DEBUG CHRDEV "%x: too late for PPEXCL; "
372 "already registered\n", minor);
373 if (pp->flags & PP_EXCL)
374 /* But it's not really an error. */
375 return 0;
376 /* There's no chance of making the driver happy. */
377 return -EINVAL;
380 /* Just remember to register the device exclusively
381 * when we finally do the registration. */
382 pp->flags |= PP_EXCL;
383 return 0;
386 if (cmd == PPSETMODE) {
387 int mode;
388 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
389 return -EFAULT;
390 /* FIXME: validate mode */
391 pp->state.mode = mode;
392 pp->state.phase = init_phase (mode);
394 if (pp->flags & PP_CLAIMED) {
395 pp->pdev->port->ieee1284.mode = mode;
396 pp->pdev->port->ieee1284.phase = pp->state.phase;
399 return 0;
402 if (cmd == PPSETPHASE) {
403 int phase;
404 if (copy_from_user (&phase, (int *) arg, sizeof (phase)))
405 return -EFAULT;
406 /* FIXME: validate phase */
407 pp->state.phase = phase;
409 if (pp->flags & PP_CLAIMED)
410 pp->pdev->port->ieee1284.phase = phase;
412 return 0;
415 /* Everything else requires the port to be claimed, so check
416 * that now. */
417 if ((pp->flags & PP_CLAIMED) == 0) {
418 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
419 minor);
420 return -EINVAL;
423 port = pp->pdev->port;
424 switch (cmd) {
425 struct ieee1284_info *info;
426 unsigned char reg;
427 unsigned char mask;
428 int mode;
429 int ret;
430 struct timeval par_timeout;
431 long to_jiffies;
433 case PPRSTATUS:
434 reg = parport_read_status (port);
435 return copy_to_user ((unsigned char *) arg, &reg,
436 sizeof (reg));
438 case PPRDATA:
439 reg = parport_read_data (port);
440 return copy_to_user ((unsigned char *) arg, &reg,
441 sizeof (reg));
443 case PPRCONTROL:
444 reg = parport_read_control (port);
445 return copy_to_user ((unsigned char *) arg, &reg,
446 sizeof (reg));
448 case PPYIELD:
449 parport_yield_blocking (pp->pdev);
450 return 0;
452 case PPRELEASE:
453 /* Save the state machine's state. */
454 info = &pp->pdev->port->ieee1284;
455 pp->state.mode = info->mode;
456 pp->state.phase = info->phase;
457 info->mode = pp->saved_state.mode;
458 info->phase = pp->saved_state.phase;
459 parport_release (pp->pdev);
460 pp->flags &= ~PP_CLAIMED;
461 return 0;
463 case PPWCONTROL:
464 if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
465 return -EFAULT;
466 parport_write_control (port, reg);
467 return 0;
469 case PPWDATA:
470 if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
471 return -EFAULT;
472 parport_write_data (port, reg);
473 return 0;
475 case PPFCONTROL:
476 if (copy_from_user (&mask, (unsigned char *) arg,
477 sizeof (mask)))
478 return -EFAULT;
479 if (copy_from_user (&reg, 1 + (unsigned char *) arg,
480 sizeof (reg)))
481 return -EFAULT;
482 parport_frob_control (port, mask, reg);
483 return 0;
485 case PPDATADIR:
486 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
487 return -EFAULT;
488 if (mode)
489 port->ops->data_reverse (port);
490 else
491 port->ops->data_forward (port);
492 return 0;
494 case PPNEGOT:
495 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
496 return -EFAULT;
497 switch ((ret = parport_negotiate (port, mode))) {
498 case 0: break;
499 case -1: /* handshake failed, peripheral not IEEE 1284 */
500 ret = -EIO;
501 break;
502 case 1: /* handshake succeeded, peripheral rejected mode */
503 ret = -ENXIO;
504 break;
506 pp_enable_irq (pp);
507 return ret;
509 case PPWCTLONIRQ:
510 if (copy_from_user (&reg, (unsigned char *) arg,
511 sizeof (reg)))
512 return -EFAULT;
514 /* Remember what to set the control lines to, for next
515 * time we get an interrupt. */
516 pp->irqctl = reg;
517 pp->irqresponse = 1;
518 return 0;
520 case PPCLRIRQ:
521 ret = atomic_read (&pp->irqc);
522 if (copy_to_user ((int *) arg, &ret, sizeof (ret)))
523 return -EFAULT;
524 atomic_sub (ret, &pp->irqc);
525 return 0;
527 case PPSETTIME:
528 if (copy_from_user (&par_timeout, (struct timeval *)arg,
529 sizeof(struct timeval))) {
530 return -EFAULT;
532 /* Convert to jiffies, place in pp->pdev->timeout */
533 if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
534 return -EINVAL;
536 to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
537 to_jiffies += par_timeout.tv_sec * (long)HZ;
538 if (to_jiffies <= 0) {
539 return -EINVAL;
541 pp->pdev->timeout = to_jiffies;
542 return 0;
544 case PPGETTIME:
545 to_jiffies = pp->pdev->timeout;
546 par_timeout.tv_sec = to_jiffies / HZ;
547 par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
548 if (copy_to_user ((struct timeval *)arg, &par_timeout,
549 sizeof(struct timeval))) {
550 return -EFAULT;
552 return 0;
554 default:
555 printk (KERN_DEBUG CHRDEV "%x: What? (cmd=0x%x)\n", minor,
556 cmd);
557 return -EINVAL;
560 /* Keep the compiler happy */
561 return 0;
564 static int pp_open (struct inode * inode, struct file * file)
566 unsigned int minor = MINOR (inode->i_rdev);
567 struct pp_struct *pp;
569 if (minor >= PARPORT_MAX)
570 return -ENXIO;
572 pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
573 if (!pp)
574 return -ENOMEM;
576 pp->state.mode = IEEE1284_MODE_COMPAT;
577 pp->state.phase = init_phase (pp->state.mode);
578 pp->flags = 0;
579 pp->irqresponse = 0;
580 atomic_set (&pp->irqc, 0);
581 init_waitqueue_head (&pp->irq_wait);
583 /* Defer the actual device registration until the first claim.
584 * That way, we know whether or not the driver wants to have
585 * exclusive access to the port (PPEXCL).
587 pp->pdev = NULL;
588 file->private_data = pp;
590 return 0;
593 static int pp_release (struct inode * inode, struct file * file)
595 unsigned int minor = MINOR (inode->i_rdev);
596 struct pp_struct *pp = file->private_data;
598 if (pp->pdev && pp->pdev->port->ieee1284.mode != IEEE1284_MODE_COMPAT) {
599 if (!(pp->flags & PP_CLAIMED)) {
600 parport_claim_or_block (pp->pdev);
601 pp->flags |= PP_CLAIMED;
603 parport_negotiate (pp->pdev->port, IEEE1284_MODE_COMPAT);
604 printk (KERN_DEBUG CHRDEV
605 "%x: negotiated back to compatibility mode because "
606 "user-space forgot\n", minor);
609 if (pp->flags & PP_CLAIMED) {
610 parport_release (pp->pdev);
611 printk (KERN_DEBUG CHRDEV "%x: released pardevice because "
612 "user-space forgot\n", minor);
615 if (pp->pdev) {
616 const char *name = pp->pdev->name;
617 parport_unregister_device (pp->pdev);
618 kfree (name);
619 pp->pdev = NULL;
620 printk (KERN_DEBUG CHRDEV "%x: unregistered pardevice\n",
621 minor);
624 kfree (pp);
626 return 0;
629 /* No kernel lock held - fine */
630 static unsigned int pp_poll (struct file * file, poll_table * wait)
632 struct pp_struct *pp = file->private_data;
633 unsigned int mask = 0;
635 poll_wait (file, &pp->irq_wait, wait);
636 if (atomic_read (&pp->irqc))
637 mask |= POLLIN | POLLRDNORM;
639 return mask;
642 static struct file_operations pp_fops = {
643 owner: THIS_MODULE,
644 llseek: pp_lseek,
645 read: pp_read,
646 write: pp_write,
647 poll: pp_poll,
648 ioctl: pp_ioctl,
649 open: pp_open,
650 release: pp_release,
653 static devfs_handle_t devfs_handle = NULL;
655 static int __init ppdev_init (void)
657 if (parport_register_driver (&ppdev_driver)) {
658 printk (KERN_WARNING CHRDEV ": unable to register driver\n");
659 return -EIO;
661 if (devfs_register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
662 printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
663 PP_MAJOR);
664 return -EIO;
666 devfs_handle = devfs_mk_dir (NULL, "parports", NULL);
667 devfs_register_series (devfs_handle, "%u", PARPORT_MAX,
668 DEVFS_FL_DEFAULT, PP_MAJOR, 0,
669 S_IFCHR | S_IRUGO | S_IWUGO,
670 &pp_fops, NULL);
672 printk (KERN_INFO PP_VERSION "\n");
673 return 0;
676 static void __exit ppdev_cleanup (void)
678 /* Clean up all parport stuff */
679 devfs_unregister (devfs_handle);
680 devfs_unregister_chrdev (PP_MAJOR, CHRDEV);
681 parport_unregister_driver (&ppdev_driver);
684 module_init(ppdev_init);
685 module_exit(ppdev_cleanup);