Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/staging/media/lirc/lirc_parallel.c

  1 /*
  2  * lirc_parallel.c
  3  *
  4  * lirc_parallel - device driver for infra-red signal receiving and
  5  *                 transmitting unit built by the author
  6  *
  7  * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
  8  *
  9  *  This program is free software; you can redistribute it and/or modify
 10  *  it under the terms of the GNU General Public License as published by
 11  *  the Free Software Foundation; either version 2 of the License, or
 12  *  (at your option) any later version.
 13  *
 14  *  This program is distributed in the hope that it will be useful,
 15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17  *  GNU General Public License for more details.
 18  *
 19  *  You should have received a copy of the GNU General Public License
 20  *  along with this program; if not, write to the Free Software
 21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 22  *
 23  */
 24 
 25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 26 
 27 /*** Includes ***/
 28 
 29 #include <linux/module.h>
 30 #include <linux/sched.h>
 31 #include <linux/errno.h>
 32 #include <linux/signal.h>
 33 #include <linux/fs.h>
 34 #include <linux/kernel.h>
 35 #include <linux/ioport.h>
 36 #include <linux/time.h>
 37 #include <linux/mm.h>
 38 #include <linux/delay.h>
 39 
 40 #include <linux/io.h>
 41 #include <linux/irq.h>
 42 #include <linux/uaccess.h>
 43 #include <asm/div64.h>
 44 
 45 #include <linux/poll.h>
 46 #include <linux/parport.h>
 47 #include <linux/platform_device.h>
 48 
 49 #include <media/lirc.h>
 50 #include <media/lirc_dev.h>
 51 
 52 #include "lirc_parallel.h"
 53 
 54 #define LIRC_DRIVER_NAME "lirc_parallel"
 55 
 56 #ifndef LIRC_IRQ
 57 #define LIRC_IRQ 7
 58 #endif
 59 #ifndef LIRC_PORT
 60 #define LIRC_PORT 0x378
 61 #endif
 62 #ifndef LIRC_TIMER
 63 #define LIRC_TIMER 65536
 64 #endif
 65 
 66 /*** Global Variables ***/
 67 
 68 static bool debug;
 69 static bool check_pselecd;
 70 
 71 static unsigned int irq = LIRC_IRQ;
 72 static unsigned int io = LIRC_PORT;
 73 #ifdef LIRC_TIMER
 74 static unsigned int timer;
 75 static unsigned int default_timer = LIRC_TIMER;
 76 #endif
 77 
 78 #define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
 79 
 80 static int rbuf[RBUF_SIZE];
 81 
 82 static DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
 83 
 84 static unsigned int rptr;
 85 static unsigned int wptr;
 86 static unsigned int lost_irqs;
 87 static int is_open;
 88 
 89 static struct parport *pport;
 90 static struct pardevice *ppdevice;
 91 static int is_claimed;
 92 
 93 static unsigned int tx_mask = 1;
 94 
 95 /*** Internal Functions ***/
 96 
 97 static unsigned int in(int offset)
 98 {
 99         switch (offset) {
100         case LIRC_LP_BASE:
101                 return parport_read_data(pport);
102         case LIRC_LP_STATUS:
103                 return parport_read_status(pport);
104         case LIRC_LP_CONTROL:
105                 return parport_read_control(pport);
106         }
107         return 0; /* make compiler happy */
108 }
109 
110 static void out(int offset, int value)
111 {
112         switch (offset) {
113         case LIRC_LP_BASE:
114                 parport_write_data(pport, value);
115                 break;
116         case LIRC_LP_CONTROL:
117                 parport_write_control(pport, value);
118                 break;
119         case LIRC_LP_STATUS:
120                 pr_info("attempt to write to status register\n");
121                 break;
122         }
123 }
124 
125 static unsigned int lirc_get_timer(void)
126 {
127         return in(LIRC_PORT_TIMER) & LIRC_PORT_TIMER_BIT;
128 }
129 
130 static unsigned int lirc_get_signal(void)
131 {
132         return in(LIRC_PORT_SIGNAL) & LIRC_PORT_SIGNAL_BIT;
133 }
134 
135 static void lirc_on(void)
136 {
137         out(LIRC_PORT_DATA, tx_mask);
138 }
139 
140 static void lirc_off(void)
141 {
142         out(LIRC_PORT_DATA, 0);
143 }
144 
145 static unsigned int init_lirc_timer(void)
146 {
147         struct timeval tv, now;
148         unsigned int level, newlevel, timeelapsed, newtimer;
149         int count = 0;
150 
151         do_gettimeofday(&tv);
152         tv.tv_sec++;                     /* wait max. 1 sec. */
153         level = lirc_get_timer();
154         do {
155                 newlevel = lirc_get_timer();
156                 if (level == 0 && newlevel != 0)
157                         count++;
158                 level = newlevel;
159                 do_gettimeofday(&now);
160         } while (count < 1000 && (now.tv_sec < tv.tv_sec
161                              || (now.tv_sec == tv.tv_sec
162                                  && now.tv_usec < tv.tv_usec)));
163 
164         timeelapsed = ((now.tv_sec + 1 - tv.tv_sec)*1000000
165                      + (now.tv_usec - tv.tv_usec));
166         if (count >= 1000 && timeelapsed > 0) {
167                 if (default_timer == 0) {
168                         /* autodetect timer */
169                         newtimer = (1000000*count)/timeelapsed;
170                         pr_info("%u Hz timer detected\n", newtimer);
171                         return newtimer;
172                 }
173                 newtimer = (1000000*count)/timeelapsed;
174                 if (abs(newtimer - default_timer) > default_timer/10) {
175                         /* bad timer */
176                         pr_notice("bad timer: %u Hz\n", newtimer);
177                         pr_notice("using default timer: %u Hz\n",
178                                   default_timer);
179                         return default_timer;
180                 } else {
181                         pr_info("%u Hz timer detected\n", newtimer);
182                         return newtimer; /* use detected value */
183                 }
184         }
185 
186         pr_notice("no timer detected\n");
187         return 0;
188 }
189 
190 static int lirc_claim(void)
191 {
192         if (parport_claim(ppdevice) != 0) {
193                 pr_warn("could not claim port\n");
194                 pr_warn("waiting for port becoming available\n");
195                 if (parport_claim_or_block(ppdevice) < 0) {
196                         pr_notice("could not claim port, giving up\n");
197                         return 0;
198                 }
199         }
200         out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
201         is_claimed = 1;
202         return 1;
203 }
204 
205 /*** interrupt handler ***/
206 
207 static void rbuf_write(int signal)
208 {
209         unsigned int nwptr;
210 
211         nwptr = (wptr + 1) & (RBUF_SIZE - 1);
212         if (nwptr == rptr) {
213                 /* no new signals will be accepted */
214                 lost_irqs++;
215                 pr_notice("buffer overrun\n");
216                 return;
217         }
218         rbuf[wptr] = signal;
219         wptr = nwptr;
220 }
221 
222 static void lirc_lirc_irq_handler(void *blah)
223 {
224         struct timeval tv;
225         static struct timeval lasttv;
226         static int init;
227         long signal;
228         int data;
229         unsigned int level, newlevel;
230         unsigned int timeout;
231 
232         if (!is_open)
233                 return;
234 
235         if (!is_claimed)
236                 return;
237 
238 #if 0
239         /* disable interrupt */
240           disable_irq(irq);
241           out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ) & (~LP_PINTEN));
242 #endif
243         if (check_pselecd && (in(1) & LP_PSELECD))
244                 return;
245 
246 #ifdef LIRC_TIMER
247         if (init) {
248                 do_gettimeofday(&tv);
249 
250                 signal = tv.tv_sec - lasttv.tv_sec;
251                 if (signal > 15)
252                         /* really long time */
253                         data = PULSE_MASK;
254                 else
255                         data = (int) (signal*1000000 +
256                                          tv.tv_usec - lasttv.tv_usec +
257                                          LIRC_SFH506_DELAY);
258 
259                 rbuf_write(data); /* space */
260         } else {
261                 if (timer == 0) {
262                         /*
263                          * wake up; we'll lose this signal, but it will be
264                          * garbage if the device is turned on anyway
265                          */
266                         timer = init_lirc_timer();
267                         /* enable_irq(irq); */
268                         return;
269                 }
270                 init = 1;
271         }
272 
273         timeout = timer/10;     /* timeout after 1/10 sec. */
274         signal = 1;
275         level = lirc_get_timer();
276         do {
277                 newlevel = lirc_get_timer();
278                 if (level == 0 && newlevel != 0)
279                         signal++;
280                 level = newlevel;
281 
282                 /* giving up */
283                 if (signal > timeout
284                     || (check_pselecd && (in(1) & LP_PSELECD))) {
285                         signal = 0;
286                         pr_notice("timeout\n");
287                         break;
288                 }
289         } while (lirc_get_signal());
290 
291         if (signal != 0) {
292                 /* adjust value to usecs */
293                 __u64 helper;
294 
295                 helper = ((__u64) signal)*1000000;
296                 do_div(helper, timer);
297                 signal = (long) helper;
298 
299                 if (signal > LIRC_SFH506_DELAY)
300                         data = signal - LIRC_SFH506_DELAY;
301                 else
302                         data = 1;
303                 rbuf_write(PULSE_BIT|data); /* pulse */
304         }
305         do_gettimeofday(&lasttv);
306 #else
307         /* add your code here */
308 #endif
309 
310         wake_up_interruptible(&lirc_wait);
311 
312         /* enable interrupt */
313         /*
314           enable_irq(irq);
315           out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN);
316         */
317 }
318 
319 /*** file operations ***/
320 
321 static loff_t lirc_lseek(struct file *filep, loff_t offset, int orig)
322 {
323         return -ESPIPE;
324 }
325 
326 static ssize_t lirc_read(struct file *filep, char __user *buf, size_t n,
327                          loff_t *ppos)
328 {
329         int result = 0;
330         int count = 0;
331         DECLARE_WAITQUEUE(wait, current);
332 
333         if (n % sizeof(int))
334                 return -EINVAL;
335 
336         add_wait_queue(&lirc_wait, &wait);
337         set_current_state(TASK_INTERRUPTIBLE);
338         while (count < n) {
339                 if (rptr != wptr) {
340                         if (copy_to_user(buf+count, (char *) &rbuf[rptr],
341                                          sizeof(int))) {
342                                 result = -EFAULT;
343                                 break;
344                         }
345                         rptr = (rptr + 1) & (RBUF_SIZE - 1);
346                         count += sizeof(int);
347                 } else {
348                         if (filep->f_flags & O_NONBLOCK) {
349                                 result = -EAGAIN;
350                                 break;
351                         }
352                         if (signal_pending(current)) {
353                                 result = -ERESTARTSYS;
354                                 break;
355                         }
356                         schedule();
357                         set_current_state(TASK_INTERRUPTIBLE);
358                 }
359         }
360         remove_wait_queue(&lirc_wait, &wait);
361         set_current_state(TASK_RUNNING);
362         return count ? count : result;
363 }
364 
365 static ssize_t lirc_write(struct file *filep, const char __user *buf, size_t n,
366                           loff_t *ppos)
367 {
368         int count;
369         unsigned int i;
370         unsigned int level, newlevel;
371         unsigned long flags;
372         int counttimer;
373         int *wbuf;
374         ssize_t ret;
375 
376         if (!is_claimed)
377                 return -EBUSY;
378 
379         count = n / sizeof(int);
380 
381         if (n % sizeof(int) || count % 2 == 0)
382                 return -EINVAL;
383 
384         wbuf = memdup_user(buf, n);
385         if (IS_ERR(wbuf))
386                 return PTR_ERR(wbuf);
387 
388 #ifdef LIRC_TIMER
389         if (timer == 0) {
390                 /* try again if device is ready */
391                 timer = init_lirc_timer();
392                 if (timer == 0) {
393                         ret = -EIO;
394                         goto out;
395                 }
396         }
397 
398         /* adjust values from usecs */
399         for (i = 0; i < count; i++) {
400                 __u64 helper;
401 
402                 helper = ((__u64) wbuf[i])*timer;
403                 do_div(helper, 1000000);
404                 wbuf[i] = (int) helper;
405         }
406 
407         local_irq_save(flags);
408         i = 0;
409         while (i < count) {
410                 level = lirc_get_timer();
411                 counttimer = 0;
412                 lirc_on();
413                 do {
414                         newlevel = lirc_get_timer();
415                         if (level == 0 && newlevel != 0)
416                                 counttimer++;
417                         level = newlevel;
418                         if (check_pselecd && (in(1) & LP_PSELECD)) {
419                                 lirc_off();
420                                 local_irq_restore(flags);
421                                 ret = -EIO;
422                                 goto out;
423                         }
424                 } while (counttimer < wbuf[i]);
425                 i++;
426 
427                 lirc_off();
428                 if (i == count)
429                         break;
430                 counttimer = 0;
431                 do {
432                         newlevel = lirc_get_timer();
433                         if (level == 0 && newlevel != 0)
434                                 counttimer++;
435                         level = newlevel;
436                         if (check_pselecd && (in(1) & LP_PSELECD)) {
437                                 local_irq_restore(flags);
438                                 ret = -EIO;
439                                 goto out;
440                         }
441                 } while (counttimer < wbuf[i]);
442                 i++;
443         }
444         local_irq_restore(flags);
445 #else
446         /* place code that handles write without external timer here */
447 #endif
448         ret = n;
449 out:
450         kfree(wbuf);
451 
452         return ret;
453 }
454 
455 static unsigned int lirc_poll(struct file *file, poll_table *wait)
456 {
457         poll_wait(file, &lirc_wait, wait);
458         if (rptr != wptr)
459                 return POLLIN | POLLRDNORM;
460         return 0;
461 }
462 
463 static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
464 {
465         int result;
466         u32 __user *uptr = (u32 __user *)arg;
467         u32 features = LIRC_CAN_SET_TRANSMITTER_MASK |
468                        LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
469         u32 mode;
470         u32 value;
471 
472         switch (cmd) {
473         case LIRC_GET_FEATURES:
474                 result = put_user(features, uptr);
475                 if (result)
476                         return result;
477                 break;
478         case LIRC_GET_SEND_MODE:
479                 result = put_user(LIRC_MODE_PULSE, uptr);
480                 if (result)
481                         return result;
482                 break;
483         case LIRC_GET_REC_MODE:
484                 result = put_user(LIRC_MODE_MODE2, uptr);
485                 if (result)
486                         return result;
487                 break;
488         case LIRC_SET_SEND_MODE:
489                 result = get_user(mode, uptr);
490                 if (result)
491                         return result;
492                 if (mode != LIRC_MODE_PULSE)
493                         return -EINVAL;
494                 break;
495         case LIRC_SET_REC_MODE:
496                 result = get_user(mode, uptr);
497                 if (result)
498                         return result;
499                 if (mode != LIRC_MODE_MODE2)
500                         return -ENOSYS;
501                 break;
502         case LIRC_SET_TRANSMITTER_MASK:
503                 result = get_user(value, uptr);
504                 if (result)
505                         return result;
506                 if ((value & LIRC_PARALLEL_TRANSMITTER_MASK) != value)
507                         return LIRC_PARALLEL_MAX_TRANSMITTERS;
508                 tx_mask = value;
509                 break;
510         default:
511                 return -ENOIOCTLCMD;
512         }
513         return 0;
514 }
515 
516 static int lirc_open(struct inode *node, struct file *filep)
517 {
518         if (is_open || !lirc_claim())
519                 return -EBUSY;
520 
521         parport_enable_irq(pport);
522 
523         /* init read ptr */
524         rptr = 0;
525         wptr = 0;
526         lost_irqs = 0;
527 
528         is_open = 1;
529         return 0;
530 }
531 
532 static int lirc_close(struct inode *node, struct file *filep)
533 {
534         if (is_claimed) {
535                 is_claimed = 0;
536                 parport_release(ppdevice);
537         }
538         is_open = 0;
539         return 0;
540 }
541 
542 static const struct file_operations lirc_fops = {
543         .owner          = THIS_MODULE,
544         .llseek         = lirc_lseek,
545         .read           = lirc_read,
546         .write          = lirc_write,
547         .poll           = lirc_poll,
548         .unlocked_ioctl = lirc_ioctl,
549 #ifdef CONFIG_COMPAT
550         .compat_ioctl   = lirc_ioctl,
551 #endif
552         .open           = lirc_open,
553         .release        = lirc_close
554 };
555 
556 static int set_use_inc(void *data)
557 {
558         return 0;
559 }
560 
561 static void set_use_dec(void *data)
562 {
563 }
564 
565 static struct lirc_driver driver = {
566         .name           = LIRC_DRIVER_NAME,
567         .minor          = -1,
568         .code_length    = 1,
569         .sample_rate    = 0,
570         .data           = NULL,
571         .add_to_buf     = NULL,
572         .set_use_inc    = set_use_inc,
573         .set_use_dec    = set_use_dec,
574         .fops           = &lirc_fops,
575         .dev            = NULL,
576         .owner          = THIS_MODULE,
577 };
578 
579 static struct platform_device *lirc_parallel_dev;
580 
581 static int lirc_parallel_probe(struct platform_device *dev)
582 {
583         return 0;
584 }
585 
586 static int lirc_parallel_remove(struct platform_device *dev)
587 {
588         return 0;
589 }
590 
591 static int lirc_parallel_suspend(struct platform_device *dev,
592                                         pm_message_t state)
593 {
594         return 0;
595 }
596 
597 static int lirc_parallel_resume(struct platform_device *dev)
598 {
599         return 0;
600 }
601 
602 static struct platform_driver lirc_parallel_driver = {
603         .probe  = lirc_parallel_probe,
604         .remove = lirc_parallel_remove,
605         .suspend        = lirc_parallel_suspend,
606         .resume = lirc_parallel_resume,
607         .driver = {
608                 .name   = LIRC_DRIVER_NAME,
609                 .owner  = THIS_MODULE,
610         },
611 };
612 
613 static int pf(void *handle)
614 {
615         parport_disable_irq(pport);
616         is_claimed = 0;
617         return 0;
618 }
619 
620 static void kf(void *handle)
621 {
622         if (!is_open)
623                 return;
624         if (!lirc_claim())
625                 return;
626         parport_enable_irq(pport);
627         lirc_off();
628         /* this is a bit annoying when you actually print...*/
629         /*
630         printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
631         */
632 }
633 
634 /*** module initialization and cleanup ***/
635 
636 static int __init lirc_parallel_init(void)
637 {
638         int result;
639 
640         result = platform_driver_register(&lirc_parallel_driver);
641         if (result) {
642                 pr_notice("platform_driver_register returned %d\n", result);
643                 return result;
644         }
645 
646         lirc_parallel_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
647         if (!lirc_parallel_dev) {
648                 result = -ENOMEM;
649                 goto exit_driver_unregister;
650         }
651 
652         result = platform_device_add(lirc_parallel_dev);
653         if (result)
654                 goto exit_device_put;
655 
656         pport = parport_find_base(io);
657         if (pport == NULL) {
658                 pr_notice("no port at %x found\n", io);
659                 result = -ENXIO;
660                 goto exit_device_put;
661         }
662         ppdevice = parport_register_device(pport, LIRC_DRIVER_NAME,
663                                            pf, kf, lirc_lirc_irq_handler, 0,
664                                            NULL);
665         parport_put_port(pport);
666         if (ppdevice == NULL) {
667                 pr_notice("parport_register_device() failed\n");
668                 result = -ENXIO;
669                 goto exit_device_put;
670         }
671         if (parport_claim(ppdevice) != 0)
672                 goto skip_init;
673         is_claimed = 1;
674         out(LIRC_LP_CONTROL, LP_PSELECP|LP_PINITP);
675 
676 #ifdef LIRC_TIMER
677         if (debug)
678                 out(LIRC_PORT_DATA, tx_mask);
679 
680         timer = init_lirc_timer();
681 
682 #if 0   /* continue even if device is offline */
683         if (timer == 0) {
684                 is_claimed = 0;
685                 parport_release(pport);
686                 parport_unregister_device(ppdevice);
687                 result = -EIO;
688                 goto exit_device_put;
689         }
690 
691 #endif
692         if (debug)
693                 out(LIRC_PORT_DATA, 0);
694 #endif
695 
696         is_claimed = 0;
697         parport_release(ppdevice);
698  skip_init:
699         driver.dev = &lirc_parallel_dev->dev;
700         driver.minor = lirc_register_driver(&driver);
701         if (driver.minor < 0) {
702                 pr_notice("register_chrdev() failed\n");
703                 parport_unregister_device(ppdevice);
704                 result = -EIO;
705                 goto exit_device_put;
706         }
707         pr_info("installed using port 0x%04x irq %d\n", io, irq);
708         return 0;
709 
710 exit_device_put:
711         platform_device_put(lirc_parallel_dev);
712 exit_driver_unregister:
713         platform_driver_unregister(&lirc_parallel_driver);
714         return result;
715 }
716 
717 static void __exit lirc_parallel_exit(void)
718 {
719         parport_unregister_device(ppdevice);
720         lirc_unregister_driver(driver.minor);
721 
722         platform_device_unregister(lirc_parallel_dev);
723         platform_driver_unregister(&lirc_parallel_driver);
724 }
725 
726 module_init(lirc_parallel_init);
727 module_exit(lirc_parallel_exit);
728 
729 MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
730 MODULE_AUTHOR("Christoph Bartelmus");
731 MODULE_LICENSE("GPL");
732 
733 module_param(io, int, S_IRUGO);
734 MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
735 
736 module_param(irq, int, S_IRUGO);
737 MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
738 
739 module_param(tx_mask, int, S_IRUGO);
740 MODULE_PARM_DESC(tx_maxk, "Transmitter mask (default: 0x01)");
741 
742 module_param(debug, bool, S_IRUGO | S_IWUSR);
743 MODULE_PARM_DESC(debug, "Enable debugging messages");
744 
745 module_param(check_pselecd, bool, S_IRUGO | S_IWUSR);
746 MODULE_PARM_DESC(check_pselecd, "Check for printer (default: 0)");
747 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us