ir-lirc-codec.c 9.1 KB

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