ir-lirc-codec.c 6.5 KB

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