ir-lirc-codec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* ir-lirc-codec.c - rc-core to classic lirc interface bridge
  2. *
  3. * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/wait.h>
  16. #include <linux/module.h>
  17. #include <media/lirc.h>
  18. #include <media/lirc_dev.h>
  19. #include <media/rc-core.h>
  20. #include "rc-core-priv.h"
  21. #define LIRCBUF_SIZE 256
  22. /**
  23. * ir_lirc_decode() - Send raw IR data to lirc_dev to be relayed to the
  24. * lircd userspace daemon for decoding.
  25. * @input_dev: the struct rc_dev descriptor of the device
  26. * @duration: the struct ir_raw_event descriptor of the pulse/space
  27. *
  28. * This function returns -EINVAL if the lirc interfaces aren't wired up.
  29. */
  30. static int ir_lirc_decode(struct rc_dev *dev, struct ir_raw_event ev)
  31. {
  32. struct lirc_codec *lirc = &dev->raw->lirc;
  33. int sample;
  34. if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
  35. return -EINVAL;
  36. /* Packet start */
  37. if (ev.reset) {
  38. /* Userspace expects a long space event before the start of
  39. * the signal to use as a sync. This may be done with repeat
  40. * packets and normal samples. But if a reset has been sent
  41. * then we assume that a long time has passed, so we send a
  42. * space with the maximum time value. */
  43. sample = LIRC_SPACE(LIRC_VALUE_MASK);
  44. IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
  45. /* Carrier reports */
  46. } else if (ev.carrier_report) {
  47. sample = LIRC_FREQUENCY(ev.carrier);
  48. IR_dprintk(2, "carrier report (freq: %d)\n", sample);
  49. /* Packet end */
  50. } else if (ev.timeout) {
  51. if (lirc->gap)
  52. return 0;
  53. lirc->gap_start = ktime_get();
  54. lirc->gap = true;
  55. lirc->gap_duration = ev.duration;
  56. if (!lirc->send_timeout_reports)
  57. return 0;
  58. sample = LIRC_TIMEOUT(ev.duration / 1000);
  59. IR_dprintk(2, "timeout report (duration: %d)\n", sample);
  60. /* Normal sample */
  61. } else {
  62. if (lirc->gap) {
  63. int gap_sample;
  64. lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
  65. lirc->gap_start));
  66. /* Convert to ms and cap by LIRC_VALUE_MASK */
  67. do_div(lirc->gap_duration, 1000);
  68. lirc->gap_duration = min(lirc->gap_duration,
  69. (u64)LIRC_VALUE_MASK);
  70. gap_sample = LIRC_SPACE(lirc->gap_duration);
  71. lirc_buffer_write(dev->raw->lirc.drv->rbuf,
  72. (unsigned char *) &gap_sample);
  73. lirc->gap = false;
  74. }
  75. sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
  76. LIRC_SPACE(ev.duration / 1000);
  77. IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
  78. TO_US(ev.duration), TO_STR(ev.pulse));
  79. }
  80. lirc_buffer_write(dev->raw->lirc.drv->rbuf,
  81. (unsigned char *) &sample);
  82. wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
  83. return 0;
  84. }
  85. static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
  86. size_t n, loff_t *ppos)
  87. {
  88. struct lirc_codec *lirc;
  89. struct rc_dev *dev;
  90. unsigned int *txbuf; /* buffer with values to transmit */
  91. ssize_t ret = -EINVAL;
  92. size_t count;
  93. ktime_t start;
  94. s64 towait;
  95. unsigned int duration = 0; /* signal duration in us */
  96. int i;
  97. start = ktime_get();
  98. lirc = lirc_get_pdata(file);
  99. if (!lirc)
  100. return -EFAULT;
  101. if (n < sizeof(unsigned) || n % sizeof(unsigned))
  102. return -EINVAL;
  103. count = n / sizeof(unsigned);
  104. if (count > LIRCBUF_SIZE || count % 2 == 0)
  105. return -EINVAL;
  106. txbuf = memdup_user(buf, n);
  107. if (IS_ERR(txbuf))
  108. return PTR_ERR(txbuf);
  109. dev = lirc->dev;
  110. if (!dev) {
  111. ret = -EFAULT;
  112. goto out;
  113. }
  114. if (!dev->tx_ir) {
  115. ret = -ENOSYS;
  116. goto out;
  117. }
  118. for (i = 0; i < count; i++) {
  119. if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
  120. ret = -EINVAL;
  121. goto out;
  122. }
  123. duration += txbuf[i];
  124. }
  125. ret = dev->tx_ir(dev, txbuf, count);
  126. if (ret < 0)
  127. goto out;
  128. for (duration = i = 0; i < ret; i++)
  129. duration += txbuf[i];
  130. ret *= sizeof(unsigned int);
  131. /*
  132. * The lircd gap calculation expects the write function to
  133. * wait for the actual IR signal to be transmitted before
  134. * returning.
  135. */
  136. towait = ktime_us_delta(ktime_add_us(start, duration), ktime_get());
  137. if (towait > 0) {
  138. set_current_state(TASK_INTERRUPTIBLE);
  139. schedule_timeout(usecs_to_jiffies(towait));
  140. }
  141. out:
  142. kfree(txbuf);
  143. return ret;
  144. }
  145. static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
  146. unsigned long arg)
  147. {
  148. struct lirc_codec *lirc;
  149. struct rc_dev *dev;
  150. u32 __user *argp = (u32 __user *)(arg);
  151. int ret = 0;
  152. __u32 val = 0, tmp;
  153. lirc = lirc_get_pdata(filep);
  154. if (!lirc)
  155. return -EFAULT;
  156. dev = lirc->dev;
  157. if (!dev)
  158. return -EFAULT;
  159. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  160. ret = get_user(val, argp);
  161. if (ret)
  162. return ret;
  163. }
  164. switch (cmd) {
  165. /* legacy support */
  166. case LIRC_GET_SEND_MODE:
  167. val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
  168. break;
  169. case LIRC_SET_SEND_MODE:
  170. if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
  171. return -EINVAL;
  172. return 0;
  173. /* TX settings */
  174. case LIRC_SET_TRANSMITTER_MASK:
  175. if (!dev->s_tx_mask)
  176. return -ENOSYS;
  177. return dev->s_tx_mask(dev, val);
  178. case LIRC_SET_SEND_CARRIER:
  179. if (!dev->s_tx_carrier)
  180. return -ENOSYS;
  181. return dev->s_tx_carrier(dev, val);
  182. case LIRC_SET_SEND_DUTY_CYCLE:
  183. if (!dev->s_tx_duty_cycle)
  184. return -ENOSYS;
  185. if (val <= 0 || val >= 100)
  186. return -EINVAL;
  187. return dev->s_tx_duty_cycle(dev, val);
  188. /* RX settings */
  189. case LIRC_SET_REC_CARRIER:
  190. if (!dev->s_rx_carrier_range)
  191. return -ENOSYS;
  192. if (val <= 0)
  193. return -EINVAL;
  194. return dev->s_rx_carrier_range(dev,
  195. dev->raw->lirc.carrier_low,
  196. val);
  197. case LIRC_SET_REC_CARRIER_RANGE:
  198. if (val <= 0)
  199. return -EINVAL;
  200. dev->raw->lirc.carrier_low = val;
  201. return 0;
  202. case LIRC_GET_REC_RESOLUTION:
  203. val = dev->rx_resolution / 1000;
  204. break;
  205. case LIRC_SET_WIDEBAND_RECEIVER:
  206. if (!dev->s_learning_mode)
  207. return -ENOSYS;
  208. return dev->s_learning_mode(dev, !!val);
  209. case LIRC_SET_MEASURE_CARRIER_MODE:
  210. if (!dev->s_carrier_report)
  211. return -ENOSYS;
  212. return dev->s_carrier_report(dev, !!val);
  213. /* Generic timeout support */
  214. case LIRC_GET_MIN_TIMEOUT:
  215. if (!dev->max_timeout)
  216. return -ENOSYS;
  217. val = dev->min_timeout / 1000;
  218. break;
  219. case LIRC_GET_MAX_TIMEOUT:
  220. if (!dev->max_timeout)
  221. return -ENOSYS;
  222. val = dev->max_timeout / 1000;
  223. break;
  224. case LIRC_SET_REC_TIMEOUT:
  225. if (!dev->max_timeout)
  226. return -ENOSYS;
  227. /* Check for multiply overflow */
  228. if (val > U32_MAX / 1000)
  229. return -EINVAL;
  230. tmp = val * 1000;
  231. if (tmp < dev->min_timeout || tmp > dev->max_timeout)
  232. return -EINVAL;
  233. dev->timeout = tmp;
  234. break;
  235. case LIRC_SET_REC_TIMEOUT_REPORTS:
  236. lirc->send_timeout_reports = !!val;
  237. break;
  238. default:
  239. return lirc_dev_fop_ioctl(filep, cmd, arg);
  240. }
  241. if (_IOC_DIR(cmd) & _IOC_READ)
  242. ret = put_user(val, argp);
  243. return ret;
  244. }
  245. static int ir_lirc_open(void *data)
  246. {
  247. return 0;
  248. }
  249. static void ir_lirc_close(void *data)
  250. {
  251. return;
  252. }
  253. static const struct file_operations lirc_fops = {
  254. .owner = THIS_MODULE,
  255. .write = ir_lirc_transmit_ir,
  256. .unlocked_ioctl = ir_lirc_ioctl,
  257. #ifdef CONFIG_COMPAT
  258. .compat_ioctl = ir_lirc_ioctl,
  259. #endif
  260. .read = lirc_dev_fop_read,
  261. .poll = lirc_dev_fop_poll,
  262. .open = lirc_dev_fop_open,
  263. .release = lirc_dev_fop_close,
  264. .llseek = no_llseek,
  265. };
  266. static int ir_lirc_register(struct rc_dev *dev)
  267. {
  268. struct lirc_driver *drv;
  269. struct lirc_buffer *rbuf;
  270. int rc = -ENOMEM;
  271. unsigned long features;
  272. drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
  273. if (!drv)
  274. return rc;
  275. rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
  276. if (!rbuf)
  277. goto rbuf_alloc_failed;
  278. rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
  279. if (rc)
  280. goto rbuf_init_failed;
  281. features = LIRC_CAN_REC_MODE2;
  282. if (dev->tx_ir) {
  283. features |= LIRC_CAN_SEND_PULSE;
  284. if (dev->s_tx_mask)
  285. features |= LIRC_CAN_SET_TRANSMITTER_MASK;
  286. if (dev->s_tx_carrier)
  287. features |= LIRC_CAN_SET_SEND_CARRIER;
  288. if (dev->s_tx_duty_cycle)
  289. features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
  290. }
  291. if (dev->s_rx_carrier_range)
  292. features |= LIRC_CAN_SET_REC_CARRIER |
  293. LIRC_CAN_SET_REC_CARRIER_RANGE;
  294. if (dev->s_learning_mode)
  295. features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
  296. if (dev->s_carrier_report)
  297. features |= LIRC_CAN_MEASURE_CARRIER;
  298. if (dev->max_timeout)
  299. features |= LIRC_CAN_SET_REC_TIMEOUT;
  300. snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
  301. dev->driver_name);
  302. drv->minor = -1;
  303. drv->features = features;
  304. drv->data = &dev->raw->lirc;
  305. drv->rbuf = rbuf;
  306. drv->set_use_inc = &ir_lirc_open;
  307. drv->set_use_dec = &ir_lirc_close;
  308. drv->code_length = sizeof(struct ir_raw_event) * 8;
  309. drv->fops = &lirc_fops;
  310. drv->dev = &dev->dev;
  311. drv->rdev = dev;
  312. drv->owner = THIS_MODULE;
  313. drv->minor = lirc_register_driver(drv);
  314. if (drv->minor < 0) {
  315. rc = -ENODEV;
  316. goto lirc_register_failed;
  317. }
  318. dev->raw->lirc.drv = drv;
  319. dev->raw->lirc.dev = dev;
  320. return 0;
  321. lirc_register_failed:
  322. rbuf_init_failed:
  323. kfree(rbuf);
  324. rbuf_alloc_failed:
  325. kfree(drv);
  326. return rc;
  327. }
  328. static int ir_lirc_unregister(struct rc_dev *dev)
  329. {
  330. struct lirc_codec *lirc = &dev->raw->lirc;
  331. lirc_unregister_driver(lirc->drv->minor);
  332. lirc_buffer_free(lirc->drv->rbuf);
  333. kfree(lirc->drv);
  334. return 0;
  335. }
  336. static struct ir_raw_handler lirc_handler = {
  337. .protocols = 0,
  338. .decode = ir_lirc_decode,
  339. .raw_register = ir_lirc_register,
  340. .raw_unregister = ir_lirc_unregister,
  341. };
  342. static int __init ir_lirc_codec_init(void)
  343. {
  344. ir_raw_handler_register(&lirc_handler);
  345. printk(KERN_INFO "IR LIRC bridge handler initialized\n");
  346. return 0;
  347. }
  348. static void __exit ir_lirc_codec_exit(void)
  349. {
  350. ir_raw_handler_unregister(&lirc_handler);
  351. }
  352. module_init(ir_lirc_codec_init);
  353. module_exit(ir_lirc_codec_exit);
  354. MODULE_LICENSE("GPL");
  355. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  356. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  357. MODULE_DESCRIPTION("LIRC IR handler bridge");