igorplugusb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * IgorPlug-USB IR Receiver
  3. *
  4. * Copyright (C) 2014 Sean Young <sean@mess.org>
  5. *
  6. * Supports the standard homebrew IgorPlugUSB receiver with Igor's firmware.
  7. * See http://www.cesko.host.sk/IgorPlugUSB/IgorPlug-USB%20(AVR)_eng.htm
  8. *
  9. * Based on the lirc_igorplugusb.c driver:
  10. * Copyright (C) 2004 Jan M. Hochstein
  11. * <hochstein@algo.informatik.tu-darmstadt.de>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/device.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/input.h>
  28. #include <media/rc-core.h>
  29. #define DRIVER_DESC "IgorPlug-USB IR Receiver"
  30. #define DRIVER_NAME "igorplugusb"
  31. #define HEADERLEN 3
  32. #define BUFLEN 36
  33. #define MAX_PACKET (HEADERLEN + BUFLEN)
  34. #define SET_INFRABUFFER_EMPTY 1
  35. #define GET_INFRACODE 2
  36. struct igorplugusb {
  37. struct rc_dev *rc;
  38. struct device *dev;
  39. struct urb *urb;
  40. struct usb_ctrlrequest request;
  41. struct timer_list timer;
  42. uint8_t buf_in[MAX_PACKET];
  43. char phys[64];
  44. };
  45. static void igorplugusb_cmd(struct igorplugusb *ir, int cmd);
  46. static void igorplugusb_irdata(struct igorplugusb *ir, unsigned len)
  47. {
  48. DEFINE_IR_RAW_EVENT(rawir);
  49. unsigned i, start, overflow;
  50. dev_dbg(ir->dev, "irdata: %*ph (len=%u)", len, ir->buf_in, len);
  51. /*
  52. * If more than 36 pulses and spaces follow each other, the igorplugusb
  53. * overwrites its buffer from the beginning. The overflow value is the
  54. * last offset which was not overwritten. Everything from this offset
  55. * onwards occurred before everything until this offset.
  56. */
  57. overflow = ir->buf_in[2];
  58. i = start = overflow + HEADERLEN;
  59. if (start >= len) {
  60. dev_err(ir->dev, "receive overflow invalid: %u", overflow);
  61. } else {
  62. if (overflow > 0)
  63. dev_warn(ir->dev, "receive overflow, at least %u lost",
  64. overflow);
  65. do {
  66. rawir.duration = ir->buf_in[i] * 85333;
  67. rawir.pulse = i & 1;
  68. ir_raw_event_store_with_filter(ir->rc, &rawir);
  69. if (++i == len)
  70. i = HEADERLEN;
  71. } while (i != start);
  72. /* add a trailing space */
  73. rawir.duration = ir->rc->timeout;
  74. rawir.pulse = false;
  75. ir_raw_event_store_with_filter(ir->rc, &rawir);
  76. ir_raw_event_handle(ir->rc);
  77. }
  78. igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
  79. }
  80. static void igorplugusb_callback(struct urb *urb)
  81. {
  82. struct usb_ctrlrequest *req;
  83. struct igorplugusb *ir = urb->context;
  84. req = (struct usb_ctrlrequest *)urb->setup_packet;
  85. switch (urb->status) {
  86. case 0:
  87. if (req->bRequest == GET_INFRACODE &&
  88. urb->actual_length > HEADERLEN)
  89. igorplugusb_irdata(ir, urb->actual_length);
  90. else /* request IR */
  91. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(50));
  92. break;
  93. case -EPROTO:
  94. case -ECONNRESET:
  95. case -ENOENT:
  96. case -ESHUTDOWN:
  97. usb_unlink_urb(urb);
  98. return;
  99. default:
  100. dev_warn(ir->dev, "Error: urb status = %d\n", urb->status);
  101. igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
  102. break;
  103. }
  104. }
  105. static void igorplugusb_cmd(struct igorplugusb *ir, int cmd)
  106. {
  107. int ret;
  108. ir->request.bRequest = cmd;
  109. ir->urb->transfer_flags = 0;
  110. ret = usb_submit_urb(ir->urb, GFP_ATOMIC);
  111. if (ret)
  112. dev_err(ir->dev, "submit urb failed: %d", ret);
  113. }
  114. static void igorplugusb_timer(unsigned long data)
  115. {
  116. struct igorplugusb *ir = (struct igorplugusb *)data;
  117. igorplugusb_cmd(ir, GET_INFRACODE);
  118. }
  119. static int igorplugusb_probe(struct usb_interface *intf,
  120. const struct usb_device_id *id)
  121. {
  122. struct usb_device *udev;
  123. struct usb_host_interface *idesc;
  124. struct usb_endpoint_descriptor *ep;
  125. struct igorplugusb *ir;
  126. struct rc_dev *rc;
  127. int ret;
  128. udev = interface_to_usbdev(intf);
  129. idesc = intf->cur_altsetting;
  130. if (idesc->desc.bNumEndpoints != 1) {
  131. dev_err(&intf->dev, "incorrect number of endpoints");
  132. return -ENODEV;
  133. }
  134. ep = &idesc->endpoint[0].desc;
  135. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_control(ep)) {
  136. dev_err(&intf->dev, "endpoint incorrect");
  137. return -ENODEV;
  138. }
  139. ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL);
  140. if (!ir)
  141. return -ENOMEM;
  142. ir->dev = &intf->dev;
  143. setup_timer(&ir->timer, igorplugusb_timer, (unsigned long)ir);
  144. ir->request.bRequest = GET_INFRACODE;
  145. ir->request.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
  146. ir->request.wLength = cpu_to_le16(sizeof(ir->buf_in));
  147. ir->urb = usb_alloc_urb(0, GFP_KERNEL);
  148. if (!ir->urb)
  149. return -ENOMEM;
  150. usb_fill_control_urb(ir->urb, udev,
  151. usb_rcvctrlpipe(udev, 0), (uint8_t *)&ir->request,
  152. ir->buf_in, sizeof(ir->buf_in), igorplugusb_callback, ir);
  153. usb_make_path(udev, ir->phys, sizeof(ir->phys));
  154. rc = rc_allocate_device();
  155. rc->input_name = DRIVER_DESC;
  156. rc->input_phys = ir->phys;
  157. usb_to_input_id(udev, &rc->input_id);
  158. rc->dev.parent = &intf->dev;
  159. rc->driver_type = RC_DRIVER_IR_RAW;
  160. /*
  161. * This device can only store 36 pulses + spaces, which is not enough
  162. * for the NEC protocol and many others.
  163. */
  164. rc->allowed_protocols = RC_BIT_ALL & ~(RC_BIT_NEC | RC_BIT_RC6_6A_20 |
  165. RC_BIT_RC6_6A_24 | RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE |
  166. RC_BIT_SONY20 | RC_BIT_MCE_KBD | RC_BIT_SANYO);
  167. rc->priv = ir;
  168. rc->driver_name = DRIVER_NAME;
  169. rc->map_name = RC_MAP_HAUPPAUGE;
  170. rc->timeout = MS_TO_NS(100);
  171. rc->rx_resolution = 85333;
  172. ir->rc = rc;
  173. ret = rc_register_device(rc);
  174. if (ret) {
  175. dev_err(&intf->dev, "failed to register rc device: %d", ret);
  176. rc_free_device(rc);
  177. usb_free_urb(ir->urb);
  178. return ret;
  179. }
  180. usb_set_intfdata(intf, ir);
  181. igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
  182. return 0;
  183. }
  184. static void igorplugusb_disconnect(struct usb_interface *intf)
  185. {
  186. struct igorplugusb *ir = usb_get_intfdata(intf);
  187. rc_unregister_device(ir->rc);
  188. del_timer_sync(&ir->timer);
  189. usb_set_intfdata(intf, NULL);
  190. usb_kill_urb(ir->urb);
  191. usb_free_urb(ir->urb);
  192. }
  193. static struct usb_device_id igorplugusb_table[] = {
  194. /* Igor Plug USB (Atmel's Manufact. ID) */
  195. { USB_DEVICE(0x03eb, 0x0002) },
  196. /* Fit PC2 Infrared Adapter */
  197. { USB_DEVICE(0x03eb, 0x21fe) },
  198. /* Terminating entry */
  199. { }
  200. };
  201. static struct usb_driver igorplugusb_driver = {
  202. .name = DRIVER_NAME,
  203. .probe = igorplugusb_probe,
  204. .disconnect = igorplugusb_disconnect,
  205. .id_table = igorplugusb_table
  206. };
  207. module_usb_driver(igorplugusb_driver);
  208. MODULE_DESCRIPTION(DRIVER_DESC);
  209. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  210. MODULE_LICENSE("GPL");
  211. MODULE_DEVICE_TABLE(usb, igorplugusb_table);