streamzap.c 13 KB

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