streamzap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Streamzap Remote Control driver
  4. *
  5. * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
  6. * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
  7. *
  8. * This driver was based on the work of Greg Wickham and Adrian
  9. * Dewhurst. It was substantially rewritten to support correct signal
  10. * gaps and now maintains a delay buffer, which is used to present
  11. * consistent timing behaviour to user space applications. Without the
  12. * delay buffer an ugly hack would be required in lircd, which can
  13. * cause sluggish signal decoding in certain situations.
  14. *
  15. * Ported to in-kernel ir-core interface by Jarod Wilson
  16. *
  17. * This driver is based on the USB skeleton driver packaged with the
  18. * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
  19. */
  20. #include <linux/device.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/ktime.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/input.h>
  26. #include <media/rc-core.h>
  27. #define DRIVER_VERSION "1.61"
  28. #define DRIVER_NAME "streamzap"
  29. #define DRIVER_DESC "Streamzap Remote Control driver"
  30. #define USB_STREAMZAP_VENDOR_ID 0x0e9c
  31. #define USB_STREAMZAP_PRODUCT_ID 0x0000
  32. /* table of devices that work with this driver */
  33. static const struct usb_device_id streamzap_table[] = {
  34. /* Streamzap Remote Control */
  35. { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
  36. /* Terminating entry */
  37. { }
  38. };
  39. MODULE_DEVICE_TABLE(usb, streamzap_table);
  40. #define SZ_PULSE_MASK 0xf0
  41. #define SZ_SPACE_MASK 0x0f
  42. #define SZ_TIMEOUT 0xff
  43. #define SZ_RESOLUTION 256
  44. /* number of samples buffered */
  45. #define SZ_BUF_LEN 128
  46. enum StreamzapDecoderState {
  47. PulseSpace,
  48. FullPulse,
  49. FullSpace,
  50. IgnorePulse
  51. };
  52. /* structure to hold our device specific stuff */
  53. struct streamzap_ir {
  54. /* ir-core */
  55. struct rc_dev *rdev;
  56. /* core device info */
  57. struct device *dev;
  58. /* usb */
  59. struct usb_device *usbdev;
  60. struct usb_interface *interface;
  61. struct usb_endpoint_descriptor *endpoint;
  62. struct urb *urb_in;
  63. /* buffer & dma */
  64. unsigned char *buf_in;
  65. dma_addr_t dma_in;
  66. unsigned int buf_in_len;
  67. /* track what state we're in */
  68. enum StreamzapDecoderState decoder_state;
  69. /* tracks whether we are currently receiving some signal */
  70. bool idle;
  71. /* sum of signal lengths received since signal start */
  72. unsigned long sum;
  73. /* start time of signal; necessary for gap tracking */
  74. ktime_t signal_last;
  75. ktime_t signal_start;
  76. bool timeout_enabled;
  77. char name[128];
  78. char phys[64];
  79. };
  80. /* local function prototypes */
  81. static int streamzap_probe(struct usb_interface *interface,
  82. const struct usb_device_id *id);
  83. static void streamzap_disconnect(struct usb_interface *interface);
  84. static void streamzap_callback(struct urb *urb);
  85. static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
  86. static int streamzap_resume(struct usb_interface *intf);
  87. /* usb specific object needed to register this driver with the usb subsystem */
  88. static struct usb_driver streamzap_driver = {
  89. .name = DRIVER_NAME,
  90. .probe = streamzap_probe,
  91. .disconnect = streamzap_disconnect,
  92. .suspend = streamzap_suspend,
  93. .resume = streamzap_resume,
  94. .id_table = streamzap_table,
  95. };
  96. static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
  97. {
  98. dev_dbg(sz->dev, "Storing %s with duration %u us\n",
  99. (rawir.pulse ? "pulse" : "space"), rawir.duration);
  100. ir_raw_event_store_with_filter(sz->rdev, &rawir);
  101. }
  102. static void sz_push_full_pulse(struct streamzap_ir *sz,
  103. unsigned char value)
  104. {
  105. struct ir_raw_event rawir = {};
  106. if (sz->idle) {
  107. int delta;
  108. sz->signal_last = sz->signal_start;
  109. sz->signal_start = ktime_get_real();
  110. delta = ktime_us_delta(sz->signal_start, sz->signal_last);
  111. rawir.pulse = false;
  112. if (delta > (15 * USEC_PER_SEC)) {
  113. /* really long time */
  114. rawir.duration = IR_MAX_DURATION;
  115. } else {
  116. rawir.duration = delta;
  117. rawir.duration -= sz->sum;
  118. rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
  119. IR_MAX_DURATION : rawir.duration;
  120. }
  121. sz_push(sz, rawir);
  122. sz->idle = false;
  123. sz->sum = 0;
  124. }
  125. rawir.pulse = true;
  126. rawir.duration = ((int) value) * SZ_RESOLUTION;
  127. rawir.duration += SZ_RESOLUTION / 2;
  128. sz->sum += rawir.duration;
  129. rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
  130. IR_MAX_DURATION : rawir.duration;
  131. sz_push(sz, rawir);
  132. }
  133. static void sz_push_half_pulse(struct streamzap_ir *sz,
  134. unsigned char value)
  135. {
  136. sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
  137. }
  138. static void sz_push_full_space(struct streamzap_ir *sz,
  139. unsigned char value)
  140. {
  141. struct ir_raw_event rawir = {};
  142. rawir.pulse = false;
  143. rawir.duration = ((int) value) * SZ_RESOLUTION;
  144. rawir.duration += SZ_RESOLUTION / 2;
  145. sz->sum += rawir.duration;
  146. sz_push(sz, rawir);
  147. }
  148. static void sz_push_half_space(struct streamzap_ir *sz,
  149. unsigned long value)
  150. {
  151. sz_push_full_space(sz, value & SZ_SPACE_MASK);
  152. }
  153. /*
  154. * streamzap_callback - usb IRQ handler callback
  155. *
  156. * This procedure is invoked on reception of data from
  157. * the usb remote.
  158. */
  159. static void streamzap_callback(struct urb *urb)
  160. {
  161. struct streamzap_ir *sz;
  162. unsigned int i;
  163. int len;
  164. if (!urb)
  165. return;
  166. sz = urb->context;
  167. len = urb->actual_length;
  168. switch (urb->status) {
  169. case -ECONNRESET:
  170. case -ENOENT:
  171. case -ESHUTDOWN:
  172. /*
  173. * this urb is terminated, clean up.
  174. * sz might already be invalid at this point
  175. */
  176. dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
  177. return;
  178. default:
  179. break;
  180. }
  181. dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
  182. for (i = 0; i < len; i++) {
  183. dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
  184. i, (unsigned char)sz->buf_in[i]);
  185. switch (sz->decoder_state) {
  186. case PulseSpace:
  187. if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
  188. SZ_PULSE_MASK) {
  189. sz->decoder_state = FullPulse;
  190. continue;
  191. } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
  192. == SZ_SPACE_MASK) {
  193. sz_push_half_pulse(sz, sz->buf_in[i]);
  194. sz->decoder_state = FullSpace;
  195. continue;
  196. } else {
  197. sz_push_half_pulse(sz, sz->buf_in[i]);
  198. sz_push_half_space(sz, sz->buf_in[i]);
  199. }
  200. break;
  201. case FullPulse:
  202. sz_push_full_pulse(sz, sz->buf_in[i]);
  203. sz->decoder_state = IgnorePulse;
  204. break;
  205. case FullSpace:
  206. if (sz->buf_in[i] == SZ_TIMEOUT) {
  207. struct ir_raw_event rawir = {
  208. .pulse = false,
  209. .duration = sz->rdev->timeout
  210. };
  211. sz->idle = true;
  212. if (sz->timeout_enabled)
  213. sz_push(sz, rawir);
  214. ir_raw_event_handle(sz->rdev);
  215. ir_raw_event_reset(sz->rdev);
  216. } else {
  217. sz_push_full_space(sz, sz->buf_in[i]);
  218. }
  219. sz->decoder_state = PulseSpace;
  220. break;
  221. case IgnorePulse:
  222. if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
  223. SZ_SPACE_MASK) {
  224. sz->decoder_state = FullSpace;
  225. continue;
  226. }
  227. sz_push_half_space(sz, sz->buf_in[i]);
  228. sz->decoder_state = PulseSpace;
  229. break;
  230. }
  231. }
  232. ir_raw_event_handle(sz->rdev);
  233. usb_submit_urb(urb, GFP_ATOMIC);
  234. return;
  235. }
  236. static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
  237. {
  238. struct rc_dev *rdev;
  239. struct device *dev = sz->dev;
  240. int ret;
  241. rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
  242. if (!rdev) {
  243. dev_err(dev, "remote dev allocation failed\n");
  244. goto out;
  245. }
  246. snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
  247. le16_to_cpu(sz->usbdev->descriptor.idVendor),
  248. le16_to_cpu(sz->usbdev->descriptor.idProduct));
  249. usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
  250. strlcat(sz->phys, "/input0", sizeof(sz->phys));
  251. rdev->device_name = sz->name;
  252. rdev->input_phys = sz->phys;
  253. usb_to_input_id(sz->usbdev, &rdev->input_id);
  254. rdev->dev.parent = dev;
  255. rdev->priv = sz;
  256. rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  257. rdev->driver_name = DRIVER_NAME;
  258. rdev->map_name = RC_MAP_STREAMZAP;
  259. ret = rc_register_device(rdev);
  260. if (ret < 0) {
  261. dev_err(dev, "remote input device register failed\n");
  262. goto out;
  263. }
  264. return rdev;
  265. out:
  266. rc_free_device(rdev);
  267. return NULL;
  268. }
  269. /*
  270. * streamzap_probe
  271. *
  272. * Called by usb-core to associated with a candidate device
  273. * On any failure the return value is the ERROR
  274. * On success return 0
  275. */
  276. static int streamzap_probe(struct usb_interface *intf,
  277. const struct usb_device_id *id)
  278. {
  279. struct usb_device *usbdev = interface_to_usbdev(intf);
  280. struct usb_host_interface *iface_host;
  281. struct streamzap_ir *sz = NULL;
  282. char buf[63], name[128] = "";
  283. int retval = -ENOMEM;
  284. int pipe, maxp;
  285. /* Allocate space for device driver specific data */
  286. sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
  287. if (!sz)
  288. return -ENOMEM;
  289. sz->usbdev = usbdev;
  290. sz->interface = intf;
  291. /* Check to ensure endpoint information matches requirements */
  292. iface_host = intf->cur_altsetting;
  293. if (iface_host->desc.bNumEndpoints != 1) {
  294. dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
  295. __func__, iface_host->desc.bNumEndpoints);
  296. retval = -ENODEV;
  297. goto free_sz;
  298. }
  299. sz->endpoint = &(iface_host->endpoint[0].desc);
  300. if (!usb_endpoint_dir_in(sz->endpoint)) {
  301. dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
  302. __func__, sz->endpoint->bEndpointAddress);
  303. retval = -ENODEV;
  304. goto free_sz;
  305. }
  306. if (!usb_endpoint_xfer_int(sz->endpoint)) {
  307. dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
  308. __func__, sz->endpoint->bmAttributes);
  309. retval = -ENODEV;
  310. goto free_sz;
  311. }
  312. pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
  313. maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
  314. if (maxp == 0) {
  315. dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
  316. __func__);
  317. retval = -ENODEV;
  318. goto free_sz;
  319. }
  320. /* Allocate the USB buffer and IRQ URB */
  321. sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
  322. if (!sz->buf_in)
  323. goto free_sz;
  324. sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  325. if (!sz->urb_in)
  326. goto free_buf_in;
  327. sz->dev = &intf->dev;
  328. sz->buf_in_len = maxp;
  329. if (usbdev->descriptor.iManufacturer
  330. && usb_string(usbdev, usbdev->descriptor.iManufacturer,
  331. buf, sizeof(buf)) > 0)
  332. strscpy(name, buf, sizeof(name));
  333. if (usbdev->descriptor.iProduct
  334. && usb_string(usbdev, usbdev->descriptor.iProduct,
  335. buf, sizeof(buf)) > 0)
  336. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  337. " %s", buf);
  338. sz->rdev = streamzap_init_rc_dev(sz);
  339. if (!sz->rdev)
  340. goto rc_dev_fail;
  341. sz->idle = true;
  342. sz->decoder_state = PulseSpace;
  343. /* FIXME: don't yet have a way to set this */
  344. sz->timeout_enabled = true;
  345. sz->rdev->timeout = SZ_TIMEOUT * SZ_RESOLUTION;
  346. #if 0
  347. /* not yet supported, depends on patches from maxim */
  348. /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
  349. sz->min_timeout = SZ_TIMEOUT * SZ_RESOLUTION;
  350. sz->max_timeout = SZ_TIMEOUT * SZ_RESOLUTION;
  351. #endif
  352. sz->signal_start = ktime_get_real();
  353. /* Complete final initialisations */
  354. usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
  355. maxp, (usb_complete_t)streamzap_callback,
  356. sz, sz->endpoint->bInterval);
  357. sz->urb_in->transfer_dma = sz->dma_in;
  358. sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  359. usb_set_intfdata(intf, sz);
  360. if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
  361. dev_err(sz->dev, "urb submit failed\n");
  362. dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
  363. usbdev->bus->busnum, usbdev->devnum);
  364. return 0;
  365. rc_dev_fail:
  366. usb_free_urb(sz->urb_in);
  367. free_buf_in:
  368. usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
  369. free_sz:
  370. kfree(sz);
  371. return retval;
  372. }
  373. /*
  374. * streamzap_disconnect
  375. *
  376. * Called by the usb core when the device is removed from the system.
  377. *
  378. * This routine guarantees that the driver will not submit any more urbs
  379. * by clearing dev->usbdev. It is also supposed to terminate any currently
  380. * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
  381. * does not provide any way to do this.
  382. */
  383. static void streamzap_disconnect(struct usb_interface *interface)
  384. {
  385. struct streamzap_ir *sz = usb_get_intfdata(interface);
  386. struct usb_device *usbdev = interface_to_usbdev(interface);
  387. usb_set_intfdata(interface, NULL);
  388. if (!sz)
  389. return;
  390. sz->usbdev = NULL;
  391. rc_unregister_device(sz->rdev);
  392. usb_kill_urb(sz->urb_in);
  393. usb_free_urb(sz->urb_in);
  394. usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
  395. kfree(sz);
  396. }
  397. static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
  398. {
  399. struct streamzap_ir *sz = usb_get_intfdata(intf);
  400. usb_kill_urb(sz->urb_in);
  401. return 0;
  402. }
  403. static int streamzap_resume(struct usb_interface *intf)
  404. {
  405. struct streamzap_ir *sz = usb_get_intfdata(intf);
  406. if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
  407. dev_err(sz->dev, "Error submitting urb\n");
  408. return -EIO;
  409. }
  410. return 0;
  411. }
  412. module_usb_driver(streamzap_driver);
  413. MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
  414. MODULE_DESCRIPTION(DRIVER_DESC);
  415. MODULE_LICENSE("GPL");