~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/ppdev.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * linux/drivers/char/ppdev.c
  3  *
  4  * This is the code behind /dev/parport* -- it allows a user-space
  5  * application to use the parport subsystem.
  6  *
  7  * Copyright (C) 1998-2000 Tim Waugh <tim@cyberelk.demon.co.uk>
  8  *
  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.
 13  *
 14  * A /dev/parportx device node represents an arbitrary device
 15  * on port 'x'.  The following operations are possible:
 16  *
 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)
 41  *
 42  * Changes:
 43  * Added SETTIME/GETTIME ioctl, Fred Barnes 1999.
 44  *
 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
 48  *   space errors.
 49  */
 50 
 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>
 62 
 63 #define PP_VERSION "ppdev: user-space parallel port driver"
 64 #define CHRDEV "ppdev"
 65 
 66 #ifndef min
 67 #define min(a,b) ((a) < (b) ? (a) : (b))
 68 #endif
 69 
 70 struct pp_struct {
 71         struct pardevice * pdev;
 72         wait_queue_head_t irq_wait;
 73         atomic_t irqc;
 74         unsigned int flags;
 75         int irqresponse;
 76         unsigned char irqctl;
 77         struct ieee1284_info state;
 78         struct ieee1284_info saved_state;
 79 };
 80 
 81 /* pp_struct.flags bitfields */
 82 #define PP_CLAIMED    (1<<0)
 83 #define PP_EXCL       (1<<1)
 84 
 85 /* Other constants */
 86 #define PP_INTERRUPT_TIMEOUT (10 * HZ) /* 10s */
 87 #define PP_BUFFER_SIZE 256
 88 #define PARDEVICE_MAX 8
 89 
 90 /* ROUND_UP macro from fs/select.c */
 91 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
 92 
 93 static inline void pp_enable_irq (struct pp_struct *pp)
 94 {
 95         struct parport *port = pp->pdev->port;
 96         port->ops->enable_irq (port);
 97 }
 98 
 99 static loff_t pp_lseek (struct file * file, long long offset, int origin)
100 {
101         return -ESPIPE;
102 }
103 
104 static ssize_t pp_read (struct file * file, char * buf, size_t count,
105                         loff_t * ppos)
106 {
107         unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
108         struct pp_struct *pp = file->private_data;
109         char * kbuffer;
110         ssize_t bytes_read = 0;
111         ssize_t got = 0;
112 
113         if (!(pp->flags & PP_CLAIMED)) {
114                 /* Don't have the port claimed */
115                 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
116                         minor);
117                 return -EINVAL;
118         }
119 
120         kbuffer = kmalloc (min (count, PP_BUFFER_SIZE), GFP_KERNEL);
121         if (!kbuffer)
122                 return -ENOMEM;
123 
124         while (bytes_read < count) {
125                 ssize_t need = min(count - bytes_read, PP_BUFFER_SIZE);
126 
127                 got = parport_read (pp->pdev->port, kbuffer, need);
128 
129                 if (got <= 0) {
130                         if (!bytes_read)
131                                 bytes_read = got;
132 
133                         break;
134                 }
135 
136                 if (copy_to_user (buf + bytes_read, kbuffer, got)) {
137                         bytes_read = -EFAULT;
138                         break;
139                 }
140 
141                 bytes_read += got;
142 
143                 if (signal_pending (current)) {
144                         if (!bytes_read)
145                                 bytes_read = -EINTR;
146                         break;
147                 }
148 
149                 if (current->need_resched)
150                         schedule ();
151         }
152 
153         kfree (kbuffer);
154         pp_enable_irq (pp);
155         return bytes_read;
156 }
157 
158 static ssize_t pp_write (struct file * file, const char * buf, size_t count,
159                          loff_t * ppos)
160 {
161         unsigned int minor = MINOR (file->f_dentry->d_inode->i_rdev);
162         struct pp_struct *pp = file->private_data;
163         char * kbuffer;
164         ssize_t bytes_written = 0;
165         ssize_t wrote;
166 
167         if (!(pp->flags & PP_CLAIMED)) {
168                 /* Don't have the port claimed */
169                 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
170                         minor);
171                 return -EINVAL;
172         }
173 
174         kbuffer = kmalloc (min (count, PP_BUFFER_SIZE), GFP_KERNEL);
175         if (!kbuffer)
176                 return -ENOMEM;
177 
178         while (bytes_written < count) {
179                 ssize_t n = min(count - bytes_written, PP_BUFFER_SIZE);
180 
181                 if (copy_from_user (kbuffer, buf + bytes_written, n)) {
182                         bytes_written = -EFAULT;
183                         break;
184                 }
185 
186                 wrote = parport_write (pp->pdev->port, kbuffer, n);
187 
188                 if (wrote <= 0) {
189                         if (!bytes_written)
190                                 bytes_written = wrote;
191                         break;
192                 }
193 
194                 bytes_written += wrote;
195 
196                 if (signal_pending (current)) {
197                         if (!bytes_written)
198                                 bytes_written = -EINTR;
199                         break;
200                 }
201 
202                 if (current->need_resched)
203                         schedule ();
204         }
205 
206         kfree (kbuffer);
207         pp_enable_irq (pp);
208         return bytes_written;
209 }
210 
211 static void pp_irq (int irq, void * private, struct pt_regs * unused)
212 {
213         struct pp_struct * pp = (struct pp_struct *) private;
214 
215         if (pp->irqresponse) {
216                 parport_write_control (pp->pdev->port, pp->irqctl);
217                 pp->irqresponse = 0;
218         }
219 
220         atomic_inc (&pp->irqc);
221         wake_up_interruptible (&pp->irq_wait);
222 }
223 
224 static int register_device (int minor, struct pp_struct *pp)
225 {
226         struct parport *port;
227         struct pardevice * pdev = NULL;
228         char *name;
229         int fl;
230 
231         name = kmalloc (strlen (CHRDEV) + 3, GFP_KERNEL);
232         if (name == NULL)
233                 return -ENOMEM;
234 
235         sprintf (name, CHRDEV "%x", minor);
236 
237         port = parport_find_number (minor);
238         if (!port) {
239                 printk (KERN_WARNING "%s: no associated port!\n", name);
240                 kfree (name);
241                 return -ENXIO;
242         }
243 
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);
248 
249         if (!pdev) {
250                 printk (KERN_WARNING "%s: failed to register device!\n", name);
251                 kfree (name);
252                 return -ENXIO;
253         }
254 
255         pp->pdev = pdev;
256         printk (KERN_DEBUG "%s: registered pardevice\n", name);
257         return 0;
258 }
259 
260 static enum ieee1284_phase init_phase (int mode)
261 {
262         switch (mode & ~(IEEE1284_DEVICEID
263                          | IEEE1284_ADDR)) {
264         case IEEE1284_MODE_NIBBLE:
265         case IEEE1284_MODE_BYTE:
266                 return IEEE1284_PH_REV_IDLE;
267         }
268         return IEEE1284_PH_FWD_IDLE;
269 }
270 
271 static int pp_ioctl(struct inode *inode, struct file *file,
272                     unsigned int cmd, unsigned long arg)
273 {
274         unsigned int minor = MINOR(inode->i_rdev);
275         struct pp_struct *pp = file->private_data;
276         struct parport * port;
277 
278         /* First handle the cases that don't take arguments. */
279         if (cmd == PPCLAIM) {
280                 struct ieee1284_info *info;
281 
282                 if (pp->flags & PP_CLAIMED) {
283                         printk (KERN_DEBUG CHRDEV
284                                 "%x: you've already got it!\n", minor);
285                         return -EINVAL;
286                 }
287 
288                 /* Deferred device registration. */
289                 if (!pp->pdev) {
290                         int err = register_device (minor, pp);
291                         if (err)
292                                 return err;
293                 }
294 
295                 parport_claim_or_block (pp->pdev);
296                 pp->flags |= PP_CLAIMED;
297 
298                 /* For interrupt-reporting to work, we need to be
299                  * informed of each interrupt. */
300                 pp_enable_irq (pp);
301 
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;
308 
309                 return 0;
310         }
311 
312         if (cmd == PPEXCL) {
313                 if (pp->pdev) {
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. */
318                                 return 0;
319                         /* There's no chance of making the driver happy. */
320                         return -EINVAL;
321                 }
322 
323                 /* Just remember to register the device exclusively
324                  * when we finally do the registration. */
325                 pp->flags |= PP_EXCL;
326                 return 0;
327         }
328 
329         if (cmd == PPSETMODE) {
330                 int mode;
331                 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
332                         return -EFAULT;
333                 /* FIXME: validate mode */
334                 pp->state.mode = mode;
335                 pp->state.phase = init_phase (mode);
336 
337                 if (pp->flags & PP_CLAIMED) {
338                         pp->pdev->port->ieee1284.mode = mode;
339                         pp->pdev->port->ieee1284.phase = pp->state.phase;
340                 }
341 
342                 return 0;
343         }
344 
345         if (cmd == PPSETPHASE) {
346                 int phase;
347                 if (copy_from_user (&phase, (int *) arg, sizeof (phase)))
348                         return -EFAULT;
349                 /* FIXME: validate phase */
350                 pp->state.phase = phase;
351 
352                 if (pp->flags & PP_CLAIMED)
353                         pp->pdev->port->ieee1284.phase = phase;
354 
355                 return 0;
356         }
357 
358         /* Everything else requires the port to be claimed, so check
359          * that now. */
360         if ((pp->flags & PP_CLAIMED) == 0) {
361                 printk (KERN_DEBUG CHRDEV "%x: claim the port first\n",
362                         minor);
363                 return -EINVAL;
364         }
365 
366         port = pp->pdev->port;
367         switch (cmd) {
368                 struct ieee1284_info *info;
369                 unsigned char reg;
370                 unsigned char mask;
371                 int mode;
372                 int ret;
373                 struct timeval par_timeout;
374                 long to_jiffies;
375 
376         case PPRSTATUS:
377                 reg = parport_read_status (port);
378                 if (copy_to_user ((unsigned char *) arg, &reg, sizeof (reg)))
379                         return -EFAULT;
380                 return 0;
381         case PPRDATA:
382                 reg = parport_read_data (port);
383                 if (copy_to_user ((unsigned char *) arg, &reg, sizeof (reg)))
384                         return -EFAULT;
385                 return 0;
386         case PPRCONTROL:
387                 reg = parport_read_control (port);
388                 if (copy_to_user ((unsigned char *) arg, &reg, sizeof (reg)))
389                         return -EFAULT;
390                 return 0;
391         case PPYIELD:
392                 parport_yield_blocking (pp->pdev);
393                 return 0;
394 
395         case PPRELEASE:
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;
404                 return 0;
405 
406         case PPWCONTROL:
407                 if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
408                         return -EFAULT;
409                 parport_write_control (port, reg);
410                 return 0;
411 
412         case PPWDATA:
413                 if (copy_from_user (&reg, (unsigned char *) arg, sizeof (reg)))
414                         return -EFAULT;
415                 parport_write_data (port, reg);
416                 return 0;
417 
418         case PPFCONTROL:
419                 if (copy_from_user (&mask, (unsigned char *) arg,
420                                     sizeof (mask)))
421                         return -EFAULT;
422                 if (copy_from_user (&reg, 1 + (unsigned char *) arg,
423                                     sizeof (reg)))
424                         return -EFAULT;
425                 parport_frob_control (port, mask, reg);
426                 return 0;
427 
428         case PPDATADIR:
429                 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
430                         return -EFAULT;
431                 if (mode)
432                         port->ops->data_reverse (port);
433                 else
434                         port->ops->data_forward (port);
435                 return 0;
436 
437         case PPNEGOT:
438                 if (copy_from_user (&mode, (int *) arg, sizeof (mode)))
439                         return -EFAULT;
440                 switch ((ret = parport_negotiate (port, mode))) {
441                 case 0: break;
442                 case -1: /* handshake failed, peripheral not IEEE 1284 */
443                         ret = -EIO;
444                         break;
445                 case 1:  /* handshake succeeded, peripheral rejected mode */
446                         ret = -ENXIO;
447                         break;
448                 }
449                 pp_enable_irq (pp);
450                 return ret;
451 
452         case PPWCTLONIRQ:
453                 if (copy_from_user (&reg, (unsigned char *) arg,
454                                     sizeof (reg)))
455                         return -EFAULT;
456 
457                 /* Remember what to set the control lines to, for next
458                  * time we get an interrupt. */
459                 pp->irqctl = reg;
460                 pp->irqresponse = 1;
461                 return 0;
462 
463         case PPCLRIRQ:
464                 ret = atomic_read (&pp->irqc);
465                 if (copy_to_user ((int *) arg, &ret, sizeof (ret)))
466                         return -EFAULT;
467                 atomic_sub (ret, &pp->irqc);
468                 return 0;
469 
470         case PPSETTIME:
471                 if (copy_from_user (&par_timeout, (struct timeval *)arg,
472                                     sizeof(struct timeval))) {
473                         return -EFAULT;
474                 }
475                 /* Convert to jiffies, place in pp->pdev->timeout */
476                 if ((par_timeout.tv_sec < 0) || (par_timeout.tv_usec < 0)) {
477                         return -EINVAL;
478                 }
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) {
482                         return -EINVAL;
483                 }
484                 pp->pdev->timeout = to_jiffies;
485                 return 0;
486 
487         case PPGETTIME:
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))) {
493                         return -EFAULT;
494                 }
495                 return 0;
496 
497         default:
498                 printk (KERN_DEBUG CHRDEV "%x: What? (cmd=0x%x)\n", minor,
499                         cmd);
500                 return -EINVAL;
501         }
502 
503         /* Keep the compiler happy */
504         return 0;
505 }
506 
507 static int pp_open (struct inode * inode, struct file * file)
508 {
509         unsigned int minor = MINOR (inode->i_rdev);
510         struct pp_struct *pp;
511 
512         if (minor >= PARPORT_MAX)
513                 return -ENXIO;
514 
515         pp = kmalloc (sizeof (struct pp_struct), GFP_KERNEL);
516         if (!pp)
517                 return -ENOMEM;
518 
519         pp->state.mode = IEEE1284_MODE_COMPAT;
520         pp->state.phase = init_phase (pp->state.mode);
521         pp->flags = 0;
522         pp->irqresponse = 0;
523         atomic_set (&pp->irqc, 0);
524         init_waitqueue_head (&pp->irq_wait);
525 
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).
529          */
530         pp->pdev = NULL;
531         file->private_data = pp;
532 
533         return 0;
534 }
535 
536 static int pp_release (struct inode * inode, struct file * file)
537 {
538         unsigned int minor = MINOR (inode->i_rdev);
539         struct pp_struct *pp = file->private_data;
540 
541         lock_kernel();
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;
546                 }
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);
551         }
552 
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);
557         }
558 
559         if (pp->pdev) {
560                 const char *name = pp->pdev->name;
561                 parport_unregister_device (pp->pdev);
562                 kfree (name);
563                 pp->pdev = NULL;
564                 printk (KERN_DEBUG CHRDEV "%x: unregistered pardevice\n",
565                         minor);
566         }
567         unlock_kernel();
568 
569         kfree (pp);
570 
571         return 0;
572 }
573 
574 /* No kernel lock held - fine */
575 static unsigned int pp_poll (struct file * file, poll_table * wait)
576 {
577         struct pp_struct *pp = file->private_data;
578         unsigned int mask = 0;
579 
580         poll_wait (file, &pp->irq_wait, wait);
581         if (atomic_read (&pp->irqc))
582                 mask |= POLLIN | POLLRDNORM;
583 
584         return mask;
585 }
586 
587 static struct file_operations pp_fops = {
588         owner:          THIS_MODULE,
589         llseek:         pp_lseek,
590         read:           pp_read,
591         write:          pp_write,
592         poll:           pp_poll,
593         ioctl:          pp_ioctl,
594         open:           pp_open,
595         release:        pp_release,
596 };
597 
598 static devfs_handle_t devfs_handle;
599 
600 static int __init ppdev_init (void)
601 {
602         if (devfs_register_chrdev (PP_MAJOR, CHRDEV, &pp_fops)) {
603                 printk (KERN_WARNING CHRDEV ": unable to get major %d\n",
604                         PP_MAJOR);
605                 return -EIO;
606         }
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,
611                                &pp_fops, NULL);
612 
613         printk (KERN_INFO PP_VERSION "\n");
614         return 0;
615 }
616 
617 static void __exit ppdev_cleanup (void)
618 {
619         /* Clean up all parport stuff */
620         devfs_unregister (devfs_handle);
621         devfs_unregister_chrdev (PP_MAJOR, CHRDEV);
622 }
623 
624 module_init(ppdev_init);
625 module_exit(ppdev_cleanup);
626 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.