streamzap.c 13 KB

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