streamzap.c 13 KB

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