imon_raw.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2018 Sean Young <sean@mess.org>
  4. #include <linux/module.h>
  5. #include <linux/usb.h>
  6. #include <linux/usb/input.h>
  7. #include <media/rc-core.h>
  8. /* Each bit is 250us */
  9. #define BIT_DURATION 250
  10. struct imon {
  11. struct device *dev;
  12. struct urb *ir_urb;
  13. struct rc_dev *rcdev;
  14. __be64 *ir_buf;
  15. char phys[64];
  16. };
  17. /*
  18. * The first 5 bytes of data represent IR pulse or space. Each bit, starting
  19. * from highest bit in the first byte, represents 250µs of data. It is 1
  20. * for space and 0 for pulse.
  21. *
  22. * The station sends 10 packets, and the 7th byte will be number 1 to 10, so
  23. * when we receive 10 we assume all the data has arrived.
  24. */
  25. static void imon_ir_data(struct imon *imon)
  26. {
  27. struct ir_raw_event rawir = {};
  28. u64 data = be64_to_cpup(imon->ir_buf);
  29. u8 packet_no = data & 0xff;
  30. int offset = 40;
  31. int bit;
  32. if (packet_no == 0xff)
  33. return;
  34. dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf);
  35. /*
  36. * Only the first 5 bytes contain IR data. Right shift so we move
  37. * the IR bits to the lower 40 bits.
  38. */
  39. data >>= 24;
  40. do {
  41. /*
  42. * Find highest set bit which is less or equal to offset
  43. *
  44. * offset is the bit above (base 0) where we start looking.
  45. *
  46. * data & (BIT_ULL(offset) - 1) masks off any unwanted bits,
  47. * so we have just bits less than offset.
  48. *
  49. * fls will tell us the highest bit set plus 1 (or 0 if no
  50. * bits are set).
  51. */
  52. rawir.pulse = !rawir.pulse;
  53. bit = fls64(data & (BIT_ULL(offset) - 1));
  54. if (bit < offset) {
  55. dev_dbg(imon->dev, "%s: %d bits",
  56. rawir.pulse ? "pulse" : "space", offset - bit);
  57. rawir.duration = (offset - bit) * BIT_DURATION;
  58. ir_raw_event_store_with_filter(imon->rcdev, &rawir);
  59. offset = bit;
  60. }
  61. data = ~data;
  62. } while (offset > 0);
  63. if (packet_no == 0x0a && !imon->rcdev->idle) {
  64. ir_raw_event_set_idle(imon->rcdev, true);
  65. ir_raw_event_handle(imon->rcdev);
  66. }
  67. }
  68. static void imon_ir_rx(struct urb *urb)
  69. {
  70. struct imon *imon = urb->context;
  71. int ret;
  72. switch (urb->status) {
  73. case 0:
  74. imon_ir_data(imon);
  75. break;
  76. case -ECONNRESET:
  77. case -ENOENT:
  78. case -ESHUTDOWN:
  79. usb_unlink_urb(urb);
  80. return;
  81. case -EPIPE:
  82. default:
  83. dev_dbg(imon->dev, "error: urb status = %d", urb->status);
  84. break;
  85. }
  86. ret = usb_submit_urb(urb, GFP_ATOMIC);
  87. if (ret && ret != -ENODEV)
  88. dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
  89. }
  90. static int imon_probe(struct usb_interface *intf,
  91. const struct usb_device_id *id)
  92. {
  93. struct usb_endpoint_descriptor *ir_ep = NULL;
  94. struct usb_host_interface *idesc;
  95. struct usb_device *udev;
  96. struct rc_dev *rcdev;
  97. struct imon *imon;
  98. int i, ret;
  99. udev = interface_to_usbdev(intf);
  100. idesc = intf->cur_altsetting;
  101. for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
  102. struct usb_endpoint_descriptor *ep = &idesc->endpoint[i].desc;
  103. if (usb_endpoint_is_int_in(ep)) {
  104. ir_ep = ep;
  105. break;
  106. }
  107. }
  108. if (!ir_ep) {
  109. dev_err(&intf->dev, "IR endpoint missing");
  110. return -ENODEV;
  111. }
  112. imon = devm_kmalloc(&intf->dev, sizeof(*imon), GFP_KERNEL);
  113. if (!imon)
  114. return -ENOMEM;
  115. imon->ir_urb = usb_alloc_urb(0, GFP_KERNEL);
  116. if (!imon->ir_urb)
  117. return -ENOMEM;
  118. imon->ir_buf = kmalloc(sizeof(__be64), GFP_KERNEL);
  119. if (!imon->ir_buf) {
  120. ret = -ENOMEM;
  121. goto free_urb;
  122. }
  123. imon->dev = &intf->dev;
  124. usb_fill_int_urb(imon->ir_urb, udev,
  125. usb_rcvintpipe(udev, ir_ep->bEndpointAddress),
  126. imon->ir_buf, sizeof(__be64),
  127. imon_ir_rx, imon, ir_ep->bInterval);
  128. rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);
  129. if (!rcdev) {
  130. ret = -ENOMEM;
  131. goto free_urb;
  132. }
  133. usb_make_path(udev, imon->phys, sizeof(imon->phys));
  134. rcdev->device_name = "iMON Station";
  135. rcdev->driver_name = KBUILD_MODNAME;
  136. rcdev->input_phys = imon->phys;
  137. usb_to_input_id(udev, &rcdev->input_id);
  138. rcdev->dev.parent = &intf->dev;
  139. rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  140. rcdev->map_name = RC_MAP_IMON_RSC;
  141. rcdev->rx_resolution = BIT_DURATION;
  142. rcdev->priv = imon;
  143. ret = devm_rc_register_device(&intf->dev, rcdev);
  144. if (ret)
  145. goto free_urb;
  146. imon->rcdev = rcdev;
  147. ret = usb_submit_urb(imon->ir_urb, GFP_KERNEL);
  148. if (ret)
  149. goto free_urb;
  150. usb_set_intfdata(intf, imon);
  151. return 0;
  152. free_urb:
  153. usb_free_urb(imon->ir_urb);
  154. kfree(imon->ir_buf);
  155. return ret;
  156. }
  157. static void imon_disconnect(struct usb_interface *intf)
  158. {
  159. struct imon *imon = usb_get_intfdata(intf);
  160. usb_kill_urb(imon->ir_urb);
  161. usb_free_urb(imon->ir_urb);
  162. kfree(imon->ir_buf);
  163. }
  164. static const struct usb_device_id imon_table[] = {
  165. /* SoundGraph iMON (IR only) -- sg_imon.inf */
  166. { USB_DEVICE(0x04e8, 0xff30) },
  167. {}
  168. };
  169. static struct usb_driver imon_driver = {
  170. .name = KBUILD_MODNAME,
  171. .probe = imon_probe,
  172. .disconnect = imon_disconnect,
  173. .id_table = imon_table
  174. };
  175. module_usb_driver(imon_driver);
  176. MODULE_DESCRIPTION("Early raw iMON IR devices");
  177. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  178. MODULE_LICENSE("GPL");
  179. MODULE_DEVICE_TABLE(usb, imon_table);