ir-lirc-codec.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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->enabled_protocols & RC_BIT_LIRC))
  35. return 0;
  36. if (!dev->raw->lirc.drv || !dev->raw->lirc.drv->rbuf)
  37. return -EINVAL;
  38. /* Packet start */
  39. if (ev.reset)
  40. return 0;
  41. /* Carrier reports */
  42. if (ev.carrier_report) {
  43. sample = LIRC_FREQUENCY(ev.carrier);
  44. IR_dprintk(2, "carrier report (freq: %d)\n", sample);
  45. /* Packet end */
  46. } else if (ev.timeout) {
  47. if (lirc->gap)
  48. return 0;
  49. lirc->gap_start = ktime_get();
  50. lirc->gap = true;
  51. lirc->gap_duration = ev.duration;
  52. if (!lirc->send_timeout_reports)
  53. return 0;
  54. sample = LIRC_TIMEOUT(ev.duration / 1000);
  55. IR_dprintk(2, "timeout report (duration: %d)\n", sample);
  56. /* Normal sample */
  57. } else {
  58. if (lirc->gap) {
  59. int gap_sample;
  60. lirc->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
  61. lirc->gap_start));
  62. /* Convert to ms and cap by LIRC_VALUE_MASK */
  63. do_div(lirc->gap_duration, 1000);
  64. lirc->gap_duration = min(lirc->gap_duration,
  65. (u64)LIRC_VALUE_MASK);
  66. gap_sample = LIRC_SPACE(lirc->gap_duration);
  67. lirc_buffer_write(dev->raw->lirc.drv->rbuf,
  68. (unsigned char *) &gap_sample);
  69. lirc->gap = false;
  70. }
  71. sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
  72. LIRC_SPACE(ev.duration / 1000);
  73. IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
  74. TO_US(ev.duration), TO_STR(ev.pulse));
  75. }
  76. lirc_buffer_write(dev->raw->lirc.drv->rbuf,
  77. (unsigned char *) &sample);
  78. wake_up(&dev->raw->lirc.drv->rbuf->wait_poll);
  79. return 0;
  80. }
  81. static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
  82. size_t n, loff_t *ppos)
  83. {
  84. struct lirc_codec *lirc;
  85. struct rc_dev *dev;
  86. unsigned int *txbuf; /* buffer with values to transmit */
  87. ssize_t ret = -EINVAL;
  88. size_t count;
  89. ktime_t start;
  90. s64 towait;
  91. unsigned int duration = 0; /* signal duration in us */
  92. int i;
  93. start = ktime_get();
  94. lirc = lirc_get_pdata(file);
  95. if (!lirc)
  96. return -EFAULT;
  97. if (n < sizeof(unsigned) || n % sizeof(unsigned))
  98. return -EINVAL;
  99. count = n / sizeof(unsigned);
  100. if (count > LIRCBUF_SIZE || count % 2 == 0)
  101. return -EINVAL;
  102. txbuf = memdup_user(buf, n);
  103. if (IS_ERR(txbuf))
  104. return PTR_ERR(txbuf);
  105. dev = lirc->dev;
  106. if (!dev) {
  107. ret = -EFAULT;
  108. goto out;
  109. }
  110. if (!dev->tx_ir) {
  111. ret = -ENOSYS;
  112. goto out;
  113. }
  114. ret = dev->tx_ir(dev, txbuf, count);
  115. if (ret < 0)
  116. goto out;
  117. for (i = 0; i < ret; i++)
  118. duration += txbuf[i];
  119. ret *= sizeof(unsigned int);
  120. /*
  121. * The lircd gap calculation expects the write function to
  122. * wait for the actual IR signal to be transmitted before
  123. * returning.
  124. */
  125. towait = ktime_us_delta(ktime_add_us(start, duration), ktime_get());
  126. if (towait > 0) {
  127. set_current_state(TASK_INTERRUPTIBLE);
  128. schedule_timeout(usecs_to_jiffies(towait));
  129. }
  130. out:
  131. kfree(txbuf);
  132. return ret;
  133. }
  134. static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
  135. unsigned long arg)
  136. {
  137. struct lirc_codec *lirc;
  138. struct rc_dev *dev;
  139. u32 __user *argp = (u32 __user *)(arg);
  140. int ret = 0;
  141. __u32 val = 0, tmp;
  142. lirc = lirc_get_pdata(filep);
  143. if (!lirc)
  144. return -EFAULT;
  145. dev = lirc->dev;
  146. if (!dev)
  147. return -EFAULT;
  148. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  149. ret = get_user(val, argp);
  150. if (ret)
  151. return ret;
  152. }
  153. switch (cmd) {
  154. /* legacy support */
  155. case LIRC_GET_SEND_MODE:
  156. val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
  157. break;
  158. case LIRC_SET_SEND_MODE:
  159. if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
  160. return -EINVAL;
  161. return 0;
  162. /* TX settings */
  163. case LIRC_SET_TRANSMITTER_MASK:
  164. if (!dev->s_tx_mask)
  165. return -ENOSYS;
  166. return dev->s_tx_mask(dev, val);
  167. case LIRC_SET_SEND_CARRIER:
  168. if (!dev->s_tx_carrier)
  169. return -ENOSYS;
  170. return dev->s_tx_carrier(dev, val);
  171. case LIRC_SET_SEND_DUTY_CYCLE:
  172. if (!dev->s_tx_duty_cycle)
  173. return -ENOSYS;
  174. if (val <= 0 || val >= 100)
  175. return -EINVAL;
  176. return dev->s_tx_duty_cycle(dev, val);
  177. /* RX settings */
  178. case LIRC_SET_REC_CARRIER:
  179. if (!dev->s_rx_carrier_range)
  180. return -ENOSYS;
  181. if (val <= 0)
  182. return -EINVAL;
  183. return dev->s_rx_carrier_range(dev,
  184. dev->raw->lirc.carrier_low,
  185. val);
  186. case LIRC_SET_REC_CARRIER_RANGE:
  187. if (val <= 0)
  188. return -EINVAL;
  189. dev->raw->lirc.carrier_low = val;
  190. return 0;
  191. case LIRC_GET_REC_RESOLUTION:
  192. val = dev->rx_resolution;
  193. break;
  194. case LIRC_SET_WIDEBAND_RECEIVER:
  195. if (!dev->s_learning_mode)
  196. return -ENOSYS;
  197. return dev->s_learning_mode(dev, !!val);
  198. case LIRC_SET_MEASURE_CARRIER_MODE:
  199. if (!dev->s_carrier_report)
  200. return -ENOSYS;
  201. return dev->s_carrier_report(dev, !!val);
  202. /* Generic timeout support */
  203. case LIRC_GET_MIN_TIMEOUT:
  204. if (!dev->max_timeout)
  205. return -ENOSYS;
  206. val = dev->min_timeout / 1000;
  207. break;
  208. case LIRC_GET_MAX_TIMEOUT:
  209. if (!dev->max_timeout)
  210. return -ENOSYS;
  211. val = dev->max_timeout / 1000;
  212. break;
  213. case LIRC_SET_REC_TIMEOUT:
  214. if (!dev->max_timeout)
  215. return -ENOSYS;
  216. tmp = val * 1000;
  217. if (tmp < dev->min_timeout ||
  218. tmp > dev->max_timeout)
  219. return -EINVAL;
  220. dev->timeout = tmp;
  221. break;
  222. case LIRC_SET_REC_TIMEOUT_REPORTS:
  223. lirc->send_timeout_reports = !!val;
  224. break;
  225. default:
  226. return lirc_dev_fop_ioctl(filep, cmd, arg);
  227. }
  228. if (_IOC_DIR(cmd) & _IOC_READ)
  229. ret = put_user(val, argp);
  230. return ret;
  231. }
  232. static int ir_lirc_open(void *data)
  233. {
  234. return 0;
  235. }
  236. static void ir_lirc_close(void *data)
  237. {
  238. return;
  239. }
  240. static struct file_operations lirc_fops = {
  241. .owner = THIS_MODULE,
  242. .write = ir_lirc_transmit_ir,
  243. .unlocked_ioctl = ir_lirc_ioctl,
  244. #ifdef CONFIG_COMPAT
  245. .compat_ioctl = ir_lirc_ioctl,
  246. #endif
  247. .read = lirc_dev_fop_read,
  248. .poll = lirc_dev_fop_poll,
  249. .open = lirc_dev_fop_open,
  250. .release = lirc_dev_fop_close,
  251. .llseek = no_llseek,
  252. };
  253. static int ir_lirc_register(struct rc_dev *dev)
  254. {
  255. struct lirc_driver *drv;
  256. struct lirc_buffer *rbuf;
  257. int rc = -ENOMEM;
  258. unsigned long features;
  259. drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
  260. if (!drv)
  261. return rc;
  262. rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
  263. if (!rbuf)
  264. goto rbuf_alloc_failed;
  265. rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
  266. if (rc)
  267. goto rbuf_init_failed;
  268. features = LIRC_CAN_REC_MODE2;
  269. if (dev->tx_ir) {
  270. features |= LIRC_CAN_SEND_PULSE;
  271. if (dev->s_tx_mask)
  272. features |= LIRC_CAN_SET_TRANSMITTER_MASK;
  273. if (dev->s_tx_carrier)
  274. features |= LIRC_CAN_SET_SEND_CARRIER;
  275. if (dev->s_tx_duty_cycle)
  276. features |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
  277. }
  278. if (dev->s_rx_carrier_range)
  279. features |= LIRC_CAN_SET_REC_CARRIER |
  280. LIRC_CAN_SET_REC_CARRIER_RANGE;
  281. if (dev->s_learning_mode)
  282. features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
  283. if (dev->s_carrier_report)
  284. features |= LIRC_CAN_MEASURE_CARRIER;
  285. if (dev->max_timeout)
  286. features |= LIRC_CAN_SET_REC_TIMEOUT;
  287. snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
  288. dev->driver_name);
  289. drv->minor = -1;
  290. drv->features = features;
  291. drv->data = &dev->raw->lirc;
  292. drv->rbuf = rbuf;
  293. drv->set_use_inc = &ir_lirc_open;
  294. drv->set_use_dec = &ir_lirc_close;
  295. drv->code_length = sizeof(struct ir_raw_event) * 8;
  296. drv->fops = &lirc_fops;
  297. drv->dev = &dev->dev;
  298. drv->owner = THIS_MODULE;
  299. drv->minor = lirc_register_driver(drv);
  300. if (drv->minor < 0) {
  301. rc = -ENODEV;
  302. goto lirc_register_failed;
  303. }
  304. dev->raw->lirc.drv = drv;
  305. dev->raw->lirc.dev = dev;
  306. return 0;
  307. lirc_register_failed:
  308. rbuf_init_failed:
  309. kfree(rbuf);
  310. rbuf_alloc_failed:
  311. kfree(drv);
  312. return rc;
  313. }
  314. static int ir_lirc_unregister(struct rc_dev *dev)
  315. {
  316. struct lirc_codec *lirc = &dev->raw->lirc;
  317. lirc_unregister_driver(lirc->drv->minor);
  318. lirc_buffer_free(lirc->drv->rbuf);
  319. kfree(lirc->drv);
  320. return 0;
  321. }
  322. static struct ir_raw_handler lirc_handler = {
  323. .protocols = RC_BIT_LIRC,
  324. .decode = ir_lirc_decode,
  325. .raw_register = ir_lirc_register,
  326. .raw_unregister = ir_lirc_unregister,
  327. };
  328. static int __init ir_lirc_codec_init(void)
  329. {
  330. ir_raw_handler_register(&lirc_handler);
  331. printk(KERN_INFO "IR LIRC bridge handler initialized\n");
  332. return 0;
  333. }
  334. static void __exit ir_lirc_codec_exit(void)
  335. {
  336. ir_raw_handler_unregister(&lirc_handler);
  337. }
  338. module_init(ir_lirc_codec_init);
  339. module_exit(ir_lirc_codec_exit);
  340. MODULE_LICENSE("GPL");
  341. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  342. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  343. MODULE_DESCRIPTION("LIRC IR handler bridge");