igorplugusb.c 6.2 KB

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