streamzap.c 14 KB

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