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