streamzap.c 14 KB

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