ir-lirc-codec.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. int sample;
  33. if (!(ir_dev->raw->enabled_protocols & IR_TYPE_LIRC))
  34. return 0;
  35. if (!ir_dev->raw->lirc.drv || !ir_dev->raw->lirc.drv->rbuf)
  36. return -EINVAL;
  37. if (IS_RESET(ev))
  38. return 0;
  39. IR_dprintk(2, "LIRC data transfer started (%uus %s)\n",
  40. TO_US(ev.duration), TO_STR(ev.pulse));
  41. sample = ev.duration / 1000;
  42. if (ev.pulse)
  43. sample |= PULSE_BIT;
  44. lirc_buffer_write(ir_dev->raw->lirc.drv->rbuf,
  45. (unsigned char *) &sample);
  46. wake_up(&ir_dev->raw->lirc.drv->rbuf->wait_poll);
  47. return 0;
  48. }
  49. static ssize_t ir_lirc_transmit_ir(struct file *file, const char *buf,
  50. size_t n, loff_t *ppos)
  51. {
  52. struct lirc_codec *lirc;
  53. struct ir_input_dev *ir_dev;
  54. int *txbuf; /* buffer with values to transmit */
  55. int ret = 0, count;
  56. lirc = lirc_get_pdata(file);
  57. if (!lirc)
  58. return -EFAULT;
  59. if (n % sizeof(int))
  60. return -EINVAL;
  61. count = n / sizeof(int);
  62. if (count > LIRCBUF_SIZE || count % 2 == 0)
  63. return -EINVAL;
  64. txbuf = memdup_user(buf, n);
  65. if (IS_ERR(txbuf))
  66. return PTR_ERR(txbuf);
  67. ir_dev = lirc->ir_dev;
  68. if (!ir_dev) {
  69. ret = -EFAULT;
  70. goto out;
  71. }
  72. if (ir_dev->props && ir_dev->props->tx_ir)
  73. ret = ir_dev->props->tx_ir(ir_dev->props->priv, txbuf, (u32)n);
  74. out:
  75. kfree(txbuf);
  76. return ret;
  77. }
  78. static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
  79. unsigned long __user arg)
  80. {
  81. struct lirc_codec *lirc;
  82. struct ir_input_dev *ir_dev;
  83. int ret = 0;
  84. void *drv_data;
  85. unsigned long val = 0;
  86. lirc = lirc_get_pdata(filep);
  87. if (!lirc)
  88. return -EFAULT;
  89. ir_dev = lirc->ir_dev;
  90. if (!ir_dev || !ir_dev->props || !ir_dev->props->priv)
  91. return -EFAULT;
  92. drv_data = ir_dev->props->priv;
  93. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  94. ret = get_user(val, (unsigned long *)arg);
  95. if (ret)
  96. return ret;
  97. }
  98. switch (cmd) {
  99. /* legacy support */
  100. case LIRC_GET_SEND_MODE:
  101. val = LIRC_CAN_SEND_PULSE & LIRC_CAN_SEND_MASK;
  102. break;
  103. case LIRC_SET_SEND_MODE:
  104. if (val != (LIRC_MODE_PULSE & LIRC_CAN_SEND_MASK))
  105. return -EINVAL;
  106. break;
  107. /* TX settings */
  108. case LIRC_SET_TRANSMITTER_MASK:
  109. if (ir_dev->props->s_tx_mask)
  110. ret = ir_dev->props->s_tx_mask(drv_data, (u32)val);
  111. else
  112. return -EINVAL;
  113. break;
  114. case LIRC_SET_SEND_CARRIER:
  115. if (ir_dev->props->s_tx_carrier)
  116. ir_dev->props->s_tx_carrier(drv_data, (u32)val);
  117. else
  118. return -EINVAL;
  119. break;
  120. case LIRC_SET_SEND_DUTY_CYCLE:
  121. if (!ir_dev->props->s_tx_duty_cycle)
  122. return -ENOSYS;
  123. if (val <= 0 || val >= 100)
  124. return -EINVAL;
  125. ir_dev->props->s_tx_duty_cycle(ir_dev->props->priv, val);
  126. break;
  127. /* RX settings */
  128. case LIRC_SET_REC_CARRIER:
  129. if (ir_dev->props->s_rx_carrier_range)
  130. ret = ir_dev->props->s_rx_carrier_range(
  131. ir_dev->props->priv,
  132. ir_dev->raw->lirc.carrier_low, val);
  133. else
  134. return -ENOSYS;
  135. if (!ret)
  136. ir_dev->raw->lirc.carrier_low = 0;
  137. break;
  138. case LIRC_SET_REC_CARRIER_RANGE:
  139. if (val >= 0)
  140. ir_dev->raw->lirc.carrier_low = val;
  141. break;
  142. case LIRC_GET_REC_RESOLUTION:
  143. val = ir_dev->props->rx_resolution;
  144. break;
  145. case LIRC_SET_WIDEBAND_RECEIVER:
  146. if (ir_dev->props->s_learning_mode)
  147. return ir_dev->props->s_learning_mode(
  148. ir_dev->props->priv, !!val);
  149. else
  150. return -ENOSYS;
  151. /* Generic timeout support */
  152. case LIRC_GET_MIN_TIMEOUT:
  153. if (!ir_dev->props->max_timeout)
  154. return -ENOSYS;
  155. val = ir_dev->props->min_timeout / 1000;
  156. break;
  157. case LIRC_GET_MAX_TIMEOUT:
  158. if (!ir_dev->props->max_timeout)
  159. return -ENOSYS;
  160. val = ir_dev->props->max_timeout / 1000;
  161. break;
  162. case LIRC_SET_REC_TIMEOUT:
  163. if (val < ir_dev->props->min_timeout ||
  164. val > ir_dev->props->max_timeout)
  165. return -EINVAL;
  166. ir_dev->props->timeout = val * 1000;
  167. break;
  168. default:
  169. return lirc_dev_fop_ioctl(filep, cmd, arg);
  170. }
  171. if (_IOC_DIR(cmd) & _IOC_READ)
  172. ret = put_user(val, (unsigned long *)arg);
  173. return ret;
  174. }
  175. static int ir_lirc_open(void *data)
  176. {
  177. return 0;
  178. }
  179. static void ir_lirc_close(void *data)
  180. {
  181. return;
  182. }
  183. static struct file_operations lirc_fops = {
  184. .owner = THIS_MODULE,
  185. .write = ir_lirc_transmit_ir,
  186. .unlocked_ioctl = ir_lirc_ioctl,
  187. .read = lirc_dev_fop_read,
  188. .poll = lirc_dev_fop_poll,
  189. .open = lirc_dev_fop_open,
  190. .release = lirc_dev_fop_close,
  191. };
  192. static int ir_lirc_register(struct input_dev *input_dev)
  193. {
  194. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  195. struct lirc_driver *drv;
  196. struct lirc_buffer *rbuf;
  197. int rc = -ENOMEM;
  198. unsigned long features;
  199. drv = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
  200. if (!drv)
  201. return rc;
  202. rbuf = kzalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
  203. if (!rbuf)
  204. goto rbuf_alloc_failed;
  205. rc = lirc_buffer_init(rbuf, sizeof(int), LIRCBUF_SIZE);
  206. if (rc)
  207. goto rbuf_init_failed;
  208. features = LIRC_CAN_REC_MODE2;
  209. if (ir_dev->props->tx_ir) {
  210. features |= LIRC_CAN_SEND_PULSE;
  211. if (ir_dev->props->s_tx_mask)
  212. features |= LIRC_CAN_SET_TRANSMITTER_MASK;
  213. if (ir_dev->props->s_tx_carrier)
  214. features |= LIRC_CAN_SET_SEND_CARRIER;
  215. if (ir_dev->props->s_tx_duty_cycle)
  216. features |= LIRC_CAN_SET_REC_DUTY_CYCLE;
  217. }
  218. if (ir_dev->props->s_rx_carrier_range)
  219. features |= LIRC_CAN_SET_REC_CARRIER |
  220. LIRC_CAN_SET_REC_CARRIER_RANGE;
  221. if (ir_dev->props->s_learning_mode)
  222. features |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
  223. if (ir_dev->props->max_timeout)
  224. features |= LIRC_CAN_SET_REC_TIMEOUT;
  225. snprintf(drv->name, sizeof(drv->name), "ir-lirc-codec (%s)",
  226. ir_dev->driver_name);
  227. drv->minor = -1;
  228. drv->features = features;
  229. drv->data = &ir_dev->raw->lirc;
  230. drv->rbuf = rbuf;
  231. drv->set_use_inc = &ir_lirc_open;
  232. drv->set_use_dec = &ir_lirc_close;
  233. drv->code_length = sizeof(struct ir_raw_event) * 8;
  234. drv->fops = &lirc_fops;
  235. drv->dev = &ir_dev->dev;
  236. drv->owner = THIS_MODULE;
  237. drv->minor = lirc_register_driver(drv);
  238. if (drv->minor < 0) {
  239. rc = -ENODEV;
  240. goto lirc_register_failed;
  241. }
  242. ir_dev->raw->lirc.drv = drv;
  243. ir_dev->raw->lirc.ir_dev = ir_dev;
  244. return 0;
  245. lirc_register_failed:
  246. rbuf_init_failed:
  247. kfree(rbuf);
  248. rbuf_alloc_failed:
  249. kfree(drv);
  250. return rc;
  251. }
  252. static int ir_lirc_unregister(struct input_dev *input_dev)
  253. {
  254. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  255. struct lirc_codec *lirc = &ir_dev->raw->lirc;
  256. lirc_unregister_driver(lirc->drv->minor);
  257. lirc_buffer_free(lirc->drv->rbuf);
  258. kfree(lirc->drv);
  259. return 0;
  260. }
  261. static struct ir_raw_handler lirc_handler = {
  262. .protocols = IR_TYPE_LIRC,
  263. .decode = ir_lirc_decode,
  264. .raw_register = ir_lirc_register,
  265. .raw_unregister = ir_lirc_unregister,
  266. };
  267. static int __init ir_lirc_codec_init(void)
  268. {
  269. ir_raw_handler_register(&lirc_handler);
  270. printk(KERN_INFO "IR LIRC bridge handler initialized\n");
  271. return 0;
  272. }
  273. static void __exit ir_lirc_codec_exit(void)
  274. {
  275. ir_raw_handler_unregister(&lirc_handler);
  276. }
  277. module_init(ir_lirc_codec_init);
  278. module_exit(ir_lirc_codec_exit);
  279. MODULE_LICENSE("GPL");
  280. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  281. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  282. MODULE_DESCRIPTION("LIRC IR handler bridge");