streamzap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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/input.h>
  38. #include <media/ir-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 int debug = 1;
  44. #else
  45. static int 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() 0
  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 ir_dev_props *props;
  79. /* core device info */
  80. struct device *dev;
  81. struct input_dev *idev;
  82. /* usb */
  83. struct usb_device *usbdev;
  84. struct usb_interface *interface;
  85. struct usb_endpoint_descriptor *endpoint;
  86. struct urb *urb_in;
  87. /* buffer & dma */
  88. unsigned char *buf_in;
  89. dma_addr_t dma_in;
  90. unsigned int buf_in_len;
  91. /* track what state we're in */
  92. enum StreamzapDecoderState decoder_state;
  93. /* tracks whether we are currently receiving some signal */
  94. bool idle;
  95. /* sum of signal lengths received since signal start */
  96. unsigned long sum;
  97. /* start time of signal; necessary for gap tracking */
  98. struct timeval signal_last;
  99. struct timeval signal_start;
  100. bool timeout_enabled;
  101. char name[128];
  102. char phys[64];
  103. };
  104. /* local function prototypes */
  105. static int streamzap_probe(struct usb_interface *interface,
  106. const struct usb_device_id *id);
  107. static void streamzap_disconnect(struct usb_interface *interface);
  108. static void streamzap_callback(struct urb *urb);
  109. static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
  110. static int streamzap_resume(struct usb_interface *intf);
  111. /* usb specific object needed to register this driver with the usb subsystem */
  112. static struct usb_driver streamzap_driver = {
  113. .name = DRIVER_NAME,
  114. .probe = streamzap_probe,
  115. .disconnect = streamzap_disconnect,
  116. .suspend = streamzap_suspend,
  117. .resume = streamzap_resume,
  118. .id_table = streamzap_table,
  119. };
  120. static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
  121. {
  122. ir_raw_event_store(sz->idev, &rawir);
  123. }
  124. static void sz_push_full_pulse(struct streamzap_ir *sz,
  125. unsigned char value)
  126. {
  127. DEFINE_IR_RAW_EVENT(rawir);
  128. if (sz->idle) {
  129. long deltv;
  130. sz->signal_last = sz->signal_start;
  131. do_gettimeofday(&sz->signal_start);
  132. deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
  133. rawir.pulse = false;
  134. if (deltv > 15) {
  135. /* really long time */
  136. rawir.duration = IR_MAX_DURATION;
  137. } else {
  138. rawir.duration = (int)(deltv * 1000000 +
  139. sz->signal_start.tv_usec -
  140. sz->signal_last.tv_usec);
  141. rawir.duration -= sz->sum;
  142. rawir.duration *= 1000;
  143. rawir.duration &= IR_MAX_DURATION;
  144. }
  145. dev_dbg(sz->dev, "ls %u\n", rawir.duration);
  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 *= 1000;
  155. rawir.duration &= IR_MAX_DURATION;
  156. dev_dbg(sz->dev, "p %u\n", rawir.duration);
  157. sz_push(sz, rawir);
  158. }
  159. static void sz_push_half_pulse(struct streamzap_ir *sz,
  160. unsigned char value)
  161. {
  162. sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
  163. }
  164. static void sz_push_full_space(struct streamzap_ir *sz,
  165. unsigned char value)
  166. {
  167. DEFINE_IR_RAW_EVENT(rawir);
  168. rawir.pulse = false;
  169. rawir.duration = ((int) value) * SZ_RESOLUTION;
  170. rawir.duration += SZ_RESOLUTION / 2;
  171. sz->sum += rawir.duration;
  172. rawir.duration *= 1000;
  173. dev_dbg(sz->dev, "s %u\n", rawir.duration);
  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. static int timeout = (((SZ_TIMEOUT * SZ_RESOLUTION * 1000) &
  193. IR_MAX_DURATION) | 0x03000000);
  194. if (!urb)
  195. return;
  196. sz = urb->context;
  197. len = urb->actual_length;
  198. switch (urb->status) {
  199. case -ECONNRESET:
  200. case -ENOENT:
  201. case -ESHUTDOWN:
  202. /*
  203. * this urb is terminated, clean up.
  204. * sz might already be invalid at this point
  205. */
  206. dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
  207. return;
  208. default:
  209. break;
  210. }
  211. dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
  212. for (i = 0; i < len; i++) {
  213. dev_dbg(sz->dev, "sz idx %d: %x\n",
  214. i, (unsigned char)sz->buf_in[i]);
  215. switch (sz->decoder_state) {
  216. case PulseSpace:
  217. if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
  218. SZ_PULSE_MASK) {
  219. sz->decoder_state = FullPulse;
  220. continue;
  221. } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
  222. == SZ_SPACE_MASK) {
  223. sz_push_half_pulse(sz, sz->buf_in[i]);
  224. sz->decoder_state = FullSpace;
  225. continue;
  226. } else {
  227. sz_push_half_pulse(sz, sz->buf_in[i]);
  228. sz_push_half_space(sz, sz->buf_in[i]);
  229. }
  230. break;
  231. case FullPulse:
  232. sz_push_full_pulse(sz, sz->buf_in[i]);
  233. sz->decoder_state = IgnorePulse;
  234. break;
  235. case FullSpace:
  236. if (sz->buf_in[i] == SZ_TIMEOUT) {
  237. DEFINE_IR_RAW_EVENT(rawir);
  238. rawir.pulse = false;
  239. rawir.duration = timeout;
  240. sz->idle = true;
  241. if (sz->timeout_enabled)
  242. sz_push(sz, rawir);
  243. ir_raw_event_handle(sz->idev);
  244. } else {
  245. sz_push_full_space(sz, sz->buf_in[i]);
  246. }
  247. sz->decoder_state = PulseSpace;
  248. break;
  249. case IgnorePulse:
  250. if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
  251. SZ_SPACE_MASK) {
  252. sz->decoder_state = FullSpace;
  253. continue;
  254. }
  255. sz_push_half_space(sz, sz->buf_in[i]);
  256. sz->decoder_state = PulseSpace;
  257. break;
  258. }
  259. }
  260. usb_submit_urb(urb, GFP_ATOMIC);
  261. return;
  262. }
  263. static struct input_dev *streamzap_init_input_dev(struct streamzap_ir *sz)
  264. {
  265. struct input_dev *idev;
  266. struct ir_dev_props *props;
  267. struct device *dev = sz->dev;
  268. int ret;
  269. idev = input_allocate_device();
  270. if (!idev) {
  271. dev_err(dev, "remote input dev allocation failed\n");
  272. goto idev_alloc_failed;
  273. }
  274. props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
  275. if (!props) {
  276. dev_err(dev, "remote ir dev props allocation failed\n");
  277. goto props_alloc_failed;
  278. }
  279. snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
  280. "Receiver (%04x:%04x)",
  281. le16_to_cpu(sz->usbdev->descriptor.idVendor),
  282. le16_to_cpu(sz->usbdev->descriptor.idProduct));
  283. idev->name = sz->name;
  284. usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
  285. strlcat(sz->phys, "/input0", sizeof(sz->phys));
  286. idev->phys = sz->phys;
  287. props->priv = sz;
  288. props->driver_type = RC_DRIVER_IR_RAW;
  289. props->allowed_protos = IR_TYPE_ALL;
  290. sz->props = props;
  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. #if 0
  383. /* not yet supported, depends on patches from maxim */
  384. /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
  385. sz->min_timeout = SZ_TIMEOUT * SZ_RESOLUTION * 1000;
  386. sz->max_timeout = SZ_TIMEOUT * SZ_RESOLUTION * 1000;
  387. #endif
  388. do_gettimeofday(&sz->signal_start);
  389. /* Complete final initialisations */
  390. usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
  391. maxp, (usb_complete_t)streamzap_callback,
  392. sz, sz->endpoint->bInterval);
  393. sz->urb_in->transfer_dma = sz->dma_in;
  394. sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  395. usb_set_intfdata(intf, sz);
  396. if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
  397. dev_err(sz->dev, "urb submit failed\n");
  398. dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
  399. usbdev->bus->busnum, usbdev->devnum);
  400. /* Load the streamzap not-quite-rc5 decoder too */
  401. load_rc5_sz_decode();
  402. return 0;
  403. input_dev_fail:
  404. usb_free_urb(sz->urb_in);
  405. free_buf_in:
  406. usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
  407. free_sz:
  408. kfree(sz);
  409. return retval;
  410. }
  411. /**
  412. * streamzap_disconnect
  413. *
  414. * Called by the usb core when the device is removed from the system.
  415. *
  416. * This routine guarantees that the driver will not submit any more urbs
  417. * by clearing dev->usbdev. It is also supposed to terminate any currently
  418. * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
  419. * does not provide any way to do this.
  420. */
  421. static void streamzap_disconnect(struct usb_interface *interface)
  422. {
  423. struct streamzap_ir *sz = usb_get_intfdata(interface);
  424. struct usb_device *usbdev = interface_to_usbdev(interface);
  425. usb_set_intfdata(interface, NULL);
  426. if (!sz)
  427. return;
  428. sz->usbdev = NULL;
  429. ir_input_unregister(sz->idev);
  430. usb_kill_urb(sz->urb_in);
  431. usb_free_urb(sz->urb_in);
  432. usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
  433. kfree(sz);
  434. }
  435. static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
  436. {
  437. struct streamzap_ir *sz = usb_get_intfdata(intf);
  438. usb_kill_urb(sz->urb_in);
  439. return 0;
  440. }
  441. static int streamzap_resume(struct usb_interface *intf)
  442. {
  443. struct streamzap_ir *sz = usb_get_intfdata(intf);
  444. if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
  445. dev_err(sz->dev, "Error sumbiting urb\n");
  446. return -EIO;
  447. }
  448. return 0;
  449. }
  450. /**
  451. * streamzap_init
  452. */
  453. static int __init streamzap_init(void)
  454. {
  455. int ret;
  456. /* register this driver with the USB subsystem */
  457. ret = usb_register(&streamzap_driver);
  458. if (ret < 0)
  459. printk(KERN_ERR DRIVER_NAME ": usb register failed, "
  460. "result = %d\n", ret);
  461. return ret;
  462. }
  463. /**
  464. * streamzap_exit
  465. */
  466. static void __exit streamzap_exit(void)
  467. {
  468. usb_deregister(&streamzap_driver);
  469. }
  470. module_init(streamzap_init);
  471. module_exit(streamzap_exit);
  472. MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
  473. MODULE_DESCRIPTION(DRIVER_DESC);
  474. MODULE_LICENSE("GPL");
  475. module_param(debug, bool, S_IRUGO | S_IWUSR);
  476. MODULE_PARM_DESC(debug, "Enable debugging messages");