imon_raw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 250000
  10. struct imon {
  11. struct device *dev;
  12. struct urb *ir_urb;
  13. struct rc_dev *rcdev;
  14. u8 ir_buf[8];
  15. char phys[64];
  16. };
  17. /*
  18. * ffs/find_next_bit() searches in the wrong direction, so open-code our own.
  19. */
  20. static inline int is_bit_set(const u8 *buf, int bit)
  21. {
  22. return buf[bit / 8] & (0x80 >> (bit & 7));
  23. }
  24. static void imon_ir_data(struct imon *imon)
  25. {
  26. DEFINE_IR_RAW_EVENT(rawir);
  27. int offset = 0, size = 5 * 8;
  28. int bit;
  29. dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf);
  30. while (offset < size) {
  31. bit = offset;
  32. while (!is_bit_set(imon->ir_buf, bit) && bit < size)
  33. bit++;
  34. dev_dbg(imon->dev, "pulse: %d bits", bit - offset);
  35. if (bit > offset) {
  36. rawir.pulse = true;
  37. rawir.duration = (bit - offset) * BIT_DURATION;
  38. ir_raw_event_store_with_filter(imon->rcdev, &rawir);
  39. }
  40. if (bit >= size)
  41. break;
  42. offset = bit;
  43. while (is_bit_set(imon->ir_buf, bit) && bit < size)
  44. bit++;
  45. dev_dbg(imon->dev, "space: %d bits", bit - offset);
  46. rawir.pulse = false;
  47. rawir.duration = (bit - offset) * BIT_DURATION;
  48. ir_raw_event_store_with_filter(imon->rcdev, &rawir);
  49. offset = bit;
  50. }
  51. if (imon->ir_buf[7] == 0x0a) {
  52. ir_raw_event_set_idle(imon->rcdev, true);
  53. ir_raw_event_handle(imon->rcdev);
  54. }
  55. }
  56. static void imon_ir_rx(struct urb *urb)
  57. {
  58. struct imon *imon = urb->context;
  59. int ret;
  60. switch (urb->status) {
  61. case 0:
  62. if (imon->ir_buf[7] != 0xff)
  63. imon_ir_data(imon);
  64. break;
  65. case -ECONNRESET:
  66. case -ENOENT:
  67. case -ESHUTDOWN:
  68. usb_unlink_urb(urb);
  69. return;
  70. case -EPIPE:
  71. default:
  72. dev_dbg(imon->dev, "error: urb status = %d", urb->status);
  73. break;
  74. }
  75. ret = usb_submit_urb(urb, GFP_ATOMIC);
  76. if (ret && ret != -ENODEV)
  77. dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
  78. }
  79. static int imon_probe(struct usb_interface *intf,
  80. const struct usb_device_id *id)
  81. {
  82. struct usb_endpoint_descriptor *ir_ep = NULL;
  83. struct usb_host_interface *idesc;
  84. struct usb_device *udev;
  85. struct rc_dev *rcdev;
  86. struct imon *imon;
  87. int i, ret;
  88. udev = interface_to_usbdev(intf);
  89. idesc = intf->cur_altsetting;
  90. for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
  91. struct usb_endpoint_descriptor *ep = &idesc->endpoint[i].desc;
  92. if (usb_endpoint_is_int_in(ep)) {
  93. ir_ep = ep;
  94. break;
  95. }
  96. }
  97. if (!ir_ep) {
  98. dev_err(&intf->dev, "IR endpoint missing");
  99. return -ENODEV;
  100. }
  101. imon = devm_kmalloc(&intf->dev, sizeof(*imon), GFP_KERNEL);
  102. if (!imon)
  103. return -ENOMEM;
  104. imon->ir_urb = usb_alloc_urb(0, GFP_KERNEL);
  105. if (!imon->ir_urb)
  106. return -ENOMEM;
  107. imon->dev = &intf->dev;
  108. usb_fill_int_urb(imon->ir_urb, udev,
  109. usb_rcvintpipe(udev, ir_ep->bEndpointAddress),
  110. imon->ir_buf, sizeof(imon->ir_buf),
  111. imon_ir_rx, imon, ir_ep->bInterval);
  112. rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);
  113. if (!rcdev) {
  114. ret = -ENOMEM;
  115. goto free_urb;
  116. }
  117. usb_make_path(udev, imon->phys, sizeof(imon->phys));
  118. rcdev->device_name = "iMON Station";
  119. rcdev->driver_name = KBUILD_MODNAME;
  120. rcdev->input_phys = imon->phys;
  121. usb_to_input_id(udev, &rcdev->input_id);
  122. rcdev->dev.parent = &intf->dev;
  123. rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  124. rcdev->map_name = RC_MAP_IMON_RSC;
  125. rcdev->rx_resolution = BIT_DURATION;
  126. rcdev->priv = imon;
  127. ret = devm_rc_register_device(&intf->dev, rcdev);
  128. if (ret)
  129. goto free_urb;
  130. imon->rcdev = rcdev;
  131. ret = usb_submit_urb(imon->ir_urb, GFP_KERNEL);
  132. if (ret)
  133. goto free_urb;
  134. usb_set_intfdata(intf, imon);
  135. return 0;
  136. free_urb:
  137. usb_free_urb(imon->ir_urb);
  138. return ret;
  139. }
  140. static void imon_disconnect(struct usb_interface *intf)
  141. {
  142. struct imon *imon = usb_get_intfdata(intf);
  143. usb_kill_urb(imon->ir_urb);
  144. usb_free_urb(imon->ir_urb);
  145. }
  146. static const struct usb_device_id imon_table[] = {
  147. /* SoundGraph iMON (IR only) -- sg_imon.inf */
  148. { USB_DEVICE(0x04e8, 0xff30) },
  149. {}
  150. };
  151. static struct usb_driver imon_driver = {
  152. .name = KBUILD_MODNAME,
  153. .probe = imon_probe,
  154. .disconnect = imon_disconnect,
  155. .id_table = imon_table
  156. };
  157. module_usb_driver(imon_driver);
  158. MODULE_DESCRIPTION("Early raw iMON IR devices");
  159. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  160. MODULE_LICENSE("GPL");
  161. MODULE_DEVICE_TABLE(usb, imon_table);