ir-lirc-codec.c 6.4 KB

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