streamzap.c 14 KB

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