streamzap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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.60"
  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 STREAMZAP_PULSE_MASK 0xf0
  58. #define STREAMZAP_SPACE_MASK 0x0f
  59. #define STREAMZAP_TIMEOUT 0xff
  60. #define STREAMZAP_RESOLUTION 256
  61. /* number of samples buffered */
  62. #define SZ_BUF_LEN 128
  63. enum StreamzapDecoderState {
  64. PulseSpace,
  65. FullPulse,
  66. FullSpace,
  67. IgnorePulse
  68. };
  69. /* structure to hold our device specific stuff */
  70. struct streamzap_ir {
  71. /* ir-core */
  72. struct ir_dev_props *props;
  73. struct ir_raw_event rawir;
  74. /* core device info */
  75. struct device *dev;
  76. struct input_dev *idev;
  77. /* usb */
  78. struct usb_device *usbdev;
  79. struct usb_interface *interface;
  80. struct usb_endpoint_descriptor *endpoint;
  81. struct urb *urb_in;
  82. /* buffer & dma */
  83. unsigned char *buf_in;
  84. dma_addr_t dma_in;
  85. unsigned int buf_in_len;
  86. /* timer used to support delay buffering */
  87. struct timer_list delay_timer;
  88. bool timer_running;
  89. spinlock_t timer_lock;
  90. struct timer_list flush_timer;
  91. bool flush;
  92. /* delay buffer */
  93. struct kfifo fifo;
  94. bool fifo_initialized;
  95. /* track what state we're in */
  96. enum StreamzapDecoderState decoder_state;
  97. /* tracks whether we are currently receiving some signal */
  98. bool idle;
  99. /* sum of signal lengths received since signal start */
  100. unsigned long sum;
  101. /* start time of signal; necessary for gap tracking */
  102. struct timeval signal_last;
  103. struct timeval signal_start;
  104. /* bool timeout_enabled; */
  105. char name[128];
  106. char phys[64];
  107. };
  108. /* local function prototypes */
  109. static int streamzap_probe(struct usb_interface *interface,
  110. const struct usb_device_id *id);
  111. static void streamzap_disconnect(struct usb_interface *interface);
  112. static void streamzap_callback(struct urb *urb);
  113. static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
  114. static int streamzap_resume(struct usb_interface *intf);
  115. /* usb specific object needed to register this driver with the usb subsystem */
  116. static struct usb_driver streamzap_driver = {
  117. .name = DRIVER_NAME,
  118. .probe = streamzap_probe,
  119. .disconnect = streamzap_disconnect,
  120. .suspend = streamzap_suspend,
  121. .resume = streamzap_resume,
  122. .id_table = streamzap_table,
  123. };
  124. static void streamzap_stop_timer(struct streamzap_ir *sz)
  125. {
  126. unsigned long flags;
  127. spin_lock_irqsave(&sz->timer_lock, flags);
  128. if (sz->timer_running) {
  129. sz->timer_running = false;
  130. spin_unlock_irqrestore(&sz->timer_lock, flags);
  131. del_timer_sync(&sz->delay_timer);
  132. } else {
  133. spin_unlock_irqrestore(&sz->timer_lock, flags);
  134. }
  135. }
  136. static void streamzap_flush_timeout(unsigned long arg)
  137. {
  138. struct streamzap_ir *sz = (struct streamzap_ir *)arg;
  139. dev_info(sz->dev, "%s: callback firing\n", __func__);
  140. /* finally start accepting data */
  141. sz->flush = false;
  142. }
  143. static void streamzap_delay_timeout(unsigned long arg)
  144. {
  145. struct streamzap_ir *sz = (struct streamzap_ir *)arg;
  146. struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
  147. unsigned long flags;
  148. int len, ret;
  149. static unsigned long delay;
  150. bool wake = false;
  151. /* deliver data every 10 ms */
  152. delay = msecs_to_jiffies(10);
  153. spin_lock_irqsave(&sz->timer_lock, flags);
  154. if (kfifo_len(&sz->fifo) > 0) {
  155. ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
  156. if (ret != sizeof(rawir))
  157. dev_err(sz->dev, "Problem w/kfifo_out...\n");
  158. ir_raw_event_store(sz->idev, &rawir);
  159. wake = true;
  160. }
  161. len = kfifo_len(&sz->fifo);
  162. if (len > 0) {
  163. while ((len < SZ_BUF_LEN / 2) &&
  164. (len < SZ_BUF_LEN * sizeof(int))) {
  165. ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
  166. if (ret != sizeof(rawir))
  167. dev_err(sz->dev, "Problem w/kfifo_out...\n");
  168. ir_raw_event_store(sz->idev, &rawir);
  169. wake = true;
  170. len = kfifo_len(&sz->fifo);
  171. }
  172. if (sz->timer_running)
  173. mod_timer(&sz->delay_timer, jiffies + delay);
  174. } else {
  175. sz->timer_running = false;
  176. }
  177. if (wake)
  178. ir_raw_event_handle(sz->idev);
  179. spin_unlock_irqrestore(&sz->timer_lock, flags);
  180. }
  181. static void streamzap_flush_delay_buffer(struct streamzap_ir *sz)
  182. {
  183. struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
  184. bool wake = false;
  185. int ret;
  186. while (kfifo_len(&sz->fifo) > 0) {
  187. ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
  188. if (ret != sizeof(rawir))
  189. dev_err(sz->dev, "Problem w/kfifo_out...\n");
  190. ir_raw_event_store(sz->idev, &rawir);
  191. wake = true;
  192. }
  193. if (wake)
  194. ir_raw_event_handle(sz->idev);
  195. }
  196. static void sz_push(struct streamzap_ir *sz)
  197. {
  198. struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
  199. unsigned long flags;
  200. int ret;
  201. spin_lock_irqsave(&sz->timer_lock, flags);
  202. if (kfifo_len(&sz->fifo) >= sizeof(int) * SZ_BUF_LEN) {
  203. ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
  204. if (ret != sizeof(rawir))
  205. dev_err(sz->dev, "Problem w/kfifo_out...\n");
  206. ir_raw_event_store(sz->idev, &rawir);
  207. }
  208. kfifo_in(&sz->fifo, &sz->rawir, sizeof(rawir));
  209. if (!sz->timer_running) {
  210. sz->delay_timer.expires = jiffies + (HZ / 10);
  211. add_timer(&sz->delay_timer);
  212. sz->timer_running = true;
  213. }
  214. spin_unlock_irqrestore(&sz->timer_lock, flags);
  215. }
  216. static void sz_push_full_pulse(struct streamzap_ir *sz,
  217. unsigned char value)
  218. {
  219. if (sz->idle) {
  220. long deltv;
  221. sz->signal_last = sz->signal_start;
  222. do_gettimeofday(&sz->signal_start);
  223. deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
  224. sz->rawir.pulse = false;
  225. if (deltv > 15) {
  226. /* really long time */
  227. sz->rawir.duration = IR_MAX_DURATION;
  228. } else {
  229. sz->rawir.duration = (int)(deltv * 1000000 +
  230. sz->signal_start.tv_usec -
  231. sz->signal_last.tv_usec);
  232. sz->rawir.duration -= sz->sum;
  233. sz->rawir.duration *= 1000;
  234. sz->rawir.duration &= IR_MAX_DURATION;
  235. }
  236. dev_dbg(sz->dev, "ls %u\n", sz->rawir.duration);
  237. sz_push(sz);
  238. sz->idle = 0;
  239. sz->sum = 0;
  240. }
  241. sz->rawir.pulse = true;
  242. sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION;
  243. sz->rawir.duration += STREAMZAP_RESOLUTION / 2;
  244. sz->sum += sz->rawir.duration;
  245. sz->rawir.duration *= 1000;
  246. sz->rawir.duration &= IR_MAX_DURATION;
  247. dev_dbg(sz->dev, "p %u\n", sz->rawir.duration);
  248. sz_push(sz);
  249. }
  250. static void sz_push_half_pulse(struct streamzap_ir *sz,
  251. unsigned char value)
  252. {
  253. sz_push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK) >> 4);
  254. }
  255. static void sz_push_full_space(struct streamzap_ir *sz,
  256. unsigned char value)
  257. {
  258. sz->rawir.pulse = false;
  259. sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION;
  260. sz->rawir.duration += STREAMZAP_RESOLUTION / 2;
  261. sz->sum += sz->rawir.duration;
  262. sz->rawir.duration *= 1000;
  263. dev_dbg(sz->dev, "s %u\n", sz->rawir.duration);
  264. sz_push(sz);
  265. }
  266. static void sz_push_half_space(struct streamzap_ir *sz,
  267. unsigned long value)
  268. {
  269. sz_push_full_space(sz, value & STREAMZAP_SPACE_MASK);
  270. }
  271. /**
  272. * streamzap_callback - usb IRQ handler callback
  273. *
  274. * This procedure is invoked on reception of data from
  275. * the usb remote.
  276. */
  277. static void streamzap_callback(struct urb *urb)
  278. {
  279. struct streamzap_ir *sz;
  280. unsigned int i;
  281. int len;
  282. #if 0
  283. static int timeout = (((STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION) &
  284. IR_MAX_DURATION) | 0x03000000);
  285. #endif
  286. if (!urb)
  287. return;
  288. sz = urb->context;
  289. len = urb->actual_length;
  290. switch (urb->status) {
  291. case -ECONNRESET:
  292. case -ENOENT:
  293. case -ESHUTDOWN:
  294. /*
  295. * this urb is terminated, clean up.
  296. * sz might already be invalid at this point
  297. */
  298. dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
  299. return;
  300. default:
  301. break;
  302. }
  303. dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
  304. if (!sz->flush) {
  305. for (i = 0; i < urb->actual_length; i++) {
  306. dev_dbg(sz->dev, "%d: %x\n", i,
  307. (unsigned char)sz->buf_in[i]);
  308. switch (sz->decoder_state) {
  309. case PulseSpace:
  310. if ((sz->buf_in[i] & STREAMZAP_PULSE_MASK) ==
  311. STREAMZAP_PULSE_MASK) {
  312. sz->decoder_state = FullPulse;
  313. continue;
  314. } else if ((sz->buf_in[i] & STREAMZAP_SPACE_MASK)
  315. == STREAMZAP_SPACE_MASK) {
  316. sz_push_half_pulse(sz, sz->buf_in[i]);
  317. sz->decoder_state = FullSpace;
  318. continue;
  319. } else {
  320. sz_push_half_pulse(sz, sz->buf_in[i]);
  321. sz_push_half_space(sz, sz->buf_in[i]);
  322. }
  323. break;
  324. case FullPulse:
  325. sz_push_full_pulse(sz, sz->buf_in[i]);
  326. sz->decoder_state = IgnorePulse;
  327. break;
  328. case FullSpace:
  329. if (sz->buf_in[i] == STREAMZAP_TIMEOUT) {
  330. sz->idle = 1;
  331. streamzap_stop_timer(sz);
  332. #if 0
  333. if (sz->timeout_enabled) {
  334. sz->rawir.pulse = false;
  335. sz->rawir.duration = timeout;
  336. sz->rawir.duration *= 1000;
  337. sz_push(sz);
  338. }
  339. #endif
  340. streamzap_flush_delay_buffer(sz);
  341. } else
  342. sz_push_full_space(sz, sz->buf_in[i]);
  343. sz->decoder_state = PulseSpace;
  344. break;
  345. case IgnorePulse:
  346. if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) ==
  347. STREAMZAP_SPACE_MASK) {
  348. sz->decoder_state = FullSpace;
  349. continue;
  350. }
  351. sz_push_half_space(sz, sz->buf_in[i]);
  352. sz->decoder_state = PulseSpace;
  353. break;
  354. }
  355. }
  356. }
  357. usb_submit_urb(urb, GFP_ATOMIC);
  358. return;
  359. }
  360. static struct input_dev *streamzap_init_input_dev(struct streamzap_ir *sz)
  361. {
  362. struct input_dev *idev;
  363. struct ir_dev_props *props;
  364. struct device *dev = sz->dev;
  365. int ret;
  366. idev = input_allocate_device();
  367. if (!idev) {
  368. dev_err(dev, "remote input dev allocation failed\n");
  369. goto idev_alloc_failed;
  370. }
  371. props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
  372. if (!props) {
  373. dev_err(dev, "remote ir dev props allocation failed\n");
  374. goto props_alloc_failed;
  375. }
  376. snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
  377. "Receiver (%04x:%04x)",
  378. le16_to_cpu(sz->usbdev->descriptor.idVendor),
  379. le16_to_cpu(sz->usbdev->descriptor.idProduct));
  380. idev->name = sz->name;
  381. usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
  382. strlcat(sz->phys, "/input0", sizeof(sz->phys));
  383. idev->phys = sz->phys;
  384. props->priv = sz;
  385. props->driver_type = RC_DRIVER_IR_RAW;
  386. /* FIXME: not sure about supported protocols, check on this */
  387. props->allowed_protos = IR_TYPE_RC5 | IR_TYPE_RC6;
  388. sz->props = props;
  389. ret = ir_input_register(idev, RC_MAP_RC5_STREAMZAP, props, DRIVER_NAME);
  390. if (ret < 0) {
  391. dev_err(dev, "remote input device register failed\n");
  392. goto irdev_failed;
  393. }
  394. return idev;
  395. irdev_failed:
  396. kfree(props);
  397. props_alloc_failed:
  398. input_free_device(idev);
  399. idev_alloc_failed:
  400. return NULL;
  401. }
  402. static int streamzap_delay_buf_init(struct streamzap_ir *sz)
  403. {
  404. int ret;
  405. ret = kfifo_alloc(&sz->fifo, sizeof(int) * SZ_BUF_LEN,
  406. GFP_KERNEL);
  407. if (ret == 0)
  408. sz->fifo_initialized = 1;
  409. return ret;
  410. }
  411. static void streamzap_start_flush_timer(struct streamzap_ir *sz)
  412. {
  413. sz->flush_timer.expires = jiffies + HZ;
  414. sz->flush = true;
  415. add_timer(&sz->flush_timer);
  416. sz->urb_in->dev = sz->usbdev;
  417. if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
  418. dev_err(sz->dev, "urb submit failed\n");
  419. }
  420. /**
  421. * streamzap_probe
  422. *
  423. * Called by usb-core to associated with a candidate device
  424. * On any failure the return value is the ERROR
  425. * On success return 0
  426. */
  427. static int __devinit streamzap_probe(struct usb_interface *intf,
  428. const struct usb_device_id *id)
  429. {
  430. struct usb_device *usbdev = interface_to_usbdev(intf);
  431. struct usb_host_interface *iface_host;
  432. struct streamzap_ir *sz = NULL;
  433. char buf[63], name[128] = "";
  434. int retval = -ENOMEM;
  435. int pipe, maxp;
  436. /* Allocate space for device driver specific data */
  437. sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
  438. if (!sz)
  439. return -ENOMEM;
  440. sz->usbdev = usbdev;
  441. sz->interface = intf;
  442. /* Check to ensure endpoint information matches requirements */
  443. iface_host = intf->cur_altsetting;
  444. if (iface_host->desc.bNumEndpoints != 1) {
  445. dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
  446. __func__, iface_host->desc.bNumEndpoints);
  447. retval = -ENODEV;
  448. goto free_sz;
  449. }
  450. sz->endpoint = &(iface_host->endpoint[0].desc);
  451. if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  452. != USB_DIR_IN) {
  453. dev_err(&intf->dev, "%s: endpoint doesn't match input device "
  454. "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
  455. retval = -ENODEV;
  456. goto free_sz;
  457. }
  458. if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  459. != USB_ENDPOINT_XFER_INT) {
  460. dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
  461. "02%02x\n", __func__, sz->endpoint->bmAttributes);
  462. retval = -ENODEV;
  463. goto free_sz;
  464. }
  465. pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
  466. maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
  467. if (maxp == 0) {
  468. dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
  469. __func__);
  470. retval = -ENODEV;
  471. goto free_sz;
  472. }
  473. /* Allocate the USB buffer and IRQ URB */
  474. sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
  475. if (!sz->buf_in)
  476. goto free_sz;
  477. sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  478. if (!sz->urb_in)
  479. goto free_buf_in;
  480. sz->dev = &intf->dev;
  481. sz->buf_in_len = maxp;
  482. if (usbdev->descriptor.iManufacturer
  483. && usb_string(usbdev, usbdev->descriptor.iManufacturer,
  484. buf, sizeof(buf)) > 0)
  485. strlcpy(name, buf, sizeof(name));
  486. if (usbdev->descriptor.iProduct
  487. && usb_string(usbdev, usbdev->descriptor.iProduct,
  488. buf, sizeof(buf)) > 0)
  489. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  490. " %s", buf);
  491. retval = streamzap_delay_buf_init(sz);
  492. if (retval) {
  493. dev_err(&intf->dev, "%s: delay buffer init failed\n", __func__);
  494. goto free_urb_in;
  495. }
  496. sz->idev = streamzap_init_input_dev(sz);
  497. if (!sz->idev)
  498. goto input_dev_fail;
  499. sz->idle = true;
  500. sz->decoder_state = PulseSpace;
  501. #if 0
  502. /* not yet supported, depends on patches from maxim */
  503. /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
  504. sz->timeout_enabled = false;
  505. sz->min_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000;
  506. sz->max_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000;
  507. #endif
  508. init_timer(&sz->delay_timer);
  509. sz->delay_timer.function = streamzap_delay_timeout;
  510. sz->delay_timer.data = (unsigned long)sz;
  511. spin_lock_init(&sz->timer_lock);
  512. init_timer(&sz->flush_timer);
  513. sz->flush_timer.function = streamzap_flush_timeout;
  514. sz->flush_timer.data = (unsigned long)sz;
  515. do_gettimeofday(&sz->signal_start);
  516. /* Complete final initialisations */
  517. usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
  518. maxp, (usb_complete_t)streamzap_callback,
  519. sz, sz->endpoint->bInterval);
  520. sz->urb_in->transfer_dma = sz->dma_in;
  521. sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  522. usb_set_intfdata(intf, sz);
  523. streamzap_start_flush_timer(sz);
  524. dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
  525. usbdev->bus->busnum, usbdev->devnum);
  526. return 0;
  527. input_dev_fail:
  528. kfifo_free(&sz->fifo);
  529. free_urb_in:
  530. usb_free_urb(sz->urb_in);
  531. free_buf_in:
  532. usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
  533. free_sz:
  534. kfree(sz);
  535. return retval;
  536. }
  537. /**
  538. * streamzap_disconnect
  539. *
  540. * Called by the usb core when the device is removed from the system.
  541. *
  542. * This routine guarantees that the driver will not submit any more urbs
  543. * by clearing dev->usbdev. It is also supposed to terminate any currently
  544. * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
  545. * does not provide any way to do this.
  546. */
  547. static void streamzap_disconnect(struct usb_interface *interface)
  548. {
  549. struct streamzap_ir *sz = usb_get_intfdata(interface);
  550. struct usb_device *usbdev = interface_to_usbdev(interface);
  551. usb_set_intfdata(interface, NULL);
  552. if (!sz)
  553. return;
  554. if (sz->flush) {
  555. sz->flush = false;
  556. del_timer_sync(&sz->flush_timer);
  557. }
  558. streamzap_stop_timer(sz);
  559. sz->usbdev = NULL;
  560. ir_input_unregister(sz->idev);
  561. usb_kill_urb(sz->urb_in);
  562. usb_free_urb(sz->urb_in);
  563. usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
  564. kfree(sz);
  565. }
  566. static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
  567. {
  568. struct streamzap_ir *sz = usb_get_intfdata(intf);
  569. if (sz->flush) {
  570. sz->flush = false;
  571. del_timer_sync(&sz->flush_timer);
  572. }
  573. streamzap_stop_timer(sz);
  574. usb_kill_urb(sz->urb_in);
  575. return 0;
  576. }
  577. static int streamzap_resume(struct usb_interface *intf)
  578. {
  579. struct streamzap_ir *sz = usb_get_intfdata(intf);
  580. if (sz->fifo_initialized)
  581. kfifo_reset(&sz->fifo);
  582. sz->flush_timer.expires = jiffies + HZ;
  583. sz->flush = true;
  584. add_timer(&sz->flush_timer);
  585. if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
  586. dev_err(sz->dev, "Error sumbiting urb\n");
  587. return -EIO;
  588. }
  589. return 0;
  590. }
  591. /**
  592. * streamzap_init
  593. */
  594. static int __init streamzap_init(void)
  595. {
  596. int ret;
  597. /* register this driver with the USB subsystem */
  598. ret = usb_register(&streamzap_driver);
  599. if (ret < 0)
  600. printk(KERN_ERR DRIVER_NAME ": usb register failed, "
  601. "result = %d\n", ret);
  602. return ret;
  603. }
  604. /**
  605. * streamzap_exit
  606. */
  607. static void __exit streamzap_exit(void)
  608. {
  609. usb_deregister(&streamzap_driver);
  610. }
  611. module_init(streamzap_init);
  612. module_exit(streamzap_exit);
  613. MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
  614. MODULE_DESCRIPTION(DRIVER_DESC);
  615. MODULE_LICENSE("GPL");
  616. module_param(debug, bool, S_IRUGO | S_IWUSR);
  617. MODULE_PARM_DESC(debug, "Enable debugging messages");