ir-lirc-codec.c 6.3 KB

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