lirc_parallel.c 15 KB

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