Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.83 KB | None | 0 0
  1. /*
  2. * LIRC driver for Windows Media Center Edition USB Infrared Transceivers
  3. *
  4. * (C) by Martin A. Blatter <martin_a_blatter@yahoo.com>
  5. *
  6. * Transmitter support and reception code cleanup.
  7. * (C) by Daniel Melander <lirc@rajidae.se>
  8. *
  9. * Original lirc_mceusb driver for 1st-gen device:
  10. * Copyright (c) 2003-2004 Dan Conti <dconti@acm.wwu.edu>
  11. *
  12. * Original lirc_mceusb driver deprecated in favor of this driver, which
  13. * supports the 1st-gen device now too. Transmitting on the 1st-gen device
  14. * only functions on port #2 at the moment.
  15. *
  16. * Support for 1st-gen device added June 2009,
  17. * by Jarod Wilson <jarod@wilsonet.com>
  18. *
  19. * Initial transmission support for 1st-gen device added August 2009,
  20. * by Patrick Calhoun <phineas@ou.edu>
  21. *
  22. * Derived from ATI USB driver by Paul Miller and the original
  23. * MCE USB driver by Dan Conti ((and now including chunks of the latter
  24. * relevant to the 1st-gen device initialization)
  25. *
  26. * This driver will only work reliably with kernel version 2.6.10
  27. * or higher, probably because of differences in USB device enumeration
  28. * in the kernel code. Device initialization fails most of the time
  29. * with earlier kernel versions.
  30. *
  31. **********************************************************************
  32. *
  33. * This program is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  46. *
  47. */
  48.  
  49. #include <linux/version.h>
  50. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
  51. #error "*******************************************************"
  52. #error "Sorry, this driver needs kernel version 2.6.5 or higher"
  53. #error "*******************************************************"
  54. #endif
  55. #include <linux/autoconf.h>
  56. #include <linux/kernel.h>
  57. #include <linux/errno.h>
  58. #include <linux/init.h>
  59. #include <linux/slab.h>
  60. #include <linux/module.h>
  61. #include <linux/kmod.h>
  62. #include <linux/smp_lock.h>
  63. #include <linux/completion.h>
  64. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
  65. #include <asm/uaccess.h>
  66. #else
  67. #include <linux/uaccess.h>
  68. #endif
  69. #include <linux/usb.h>
  70. #include <linux/wait.h>
  71. #include <linux/time.h>
  72.  
  73. #include "drivers/lirc.h"
  74. #include "drivers/kcompat.h"
  75. #include "drivers/lirc_dev/lirc_dev.h"
  76.  
  77. #define DRIVER_VERSION "1.90"
  78. #define DRIVER_AUTHOR "Daniel Melander <lirc@rajidae.se>, " \
  79. "Martin Blatter <martin_a_blatter@yahoo.com>, " \
  80. "Dan Conti <dconti@acm.wwu.edu>"
  81. #define DRIVER_DESC "Windows Media Center Edition USB IR Transceiver " \
  82. "driver for LIRC"
  83. #define DRIVER_NAME "lirc_mceusb"
  84.  
  85. #define USB_BUFLEN 32 /* USB reception buffer length */
  86. #define LIRCBUF_SIZE 256 /* LIRC work buffer length */
  87.  
  88. /* MCE constants */
  89. #define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
  90. #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
  91. #define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
  92. #define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
  93. #define MCE_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
  94. #define MCE_CONTROL_HEADER 0x9F /* MCE status header */
  95. #define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
  96. #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
  97. #define MCE_DEFAULT_TX_MASK 0x03 /* Val opts: TX1=0x01, TX2=0x02, ALL=0x03 */
  98. #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
  99. #define MCE_PULSE_MASK 0x7F /* Pulse mask */
  100. #define MCE_MAX_PULSE_LENGTH 0x7F /* Longest transmittable pulse symbol */
  101. #define MCE_PACKET_LENGTH_MASK 0x7F /* Pulse mask */
  102.  
  103.  
  104. /* module parameters */
  105. #ifdef CONFIG_USB_DEBUG
  106. static int debug = 1;
  107. #else
  108. static int debug;
  109. #endif
  110. #define dprintk(fmt, args...) \
  111. do { \
  112. if (debug) \
  113. printk(KERN_DEBUG fmt, ## args); \
  114. } while (0)
  115.  
  116. /* general constants */
  117. #define SEND_FLAG_IN_PROGRESS 1
  118. #define SEND_FLAG_COMPLETE 2
  119. #define RECV_FLAG_IN_PROGRESS 3
  120. #define RECV_FLAG_COMPLETE 4
  121.  
  122. #define MCEUSB_INBOUND 1
  123. #define MCEUSB_OUTBOUND 2
  124.  
  125. #define VENDOR_PHILIPS 0x0471
  126. #define VENDOR_SMK 0x0609
  127. #define VENDOR_TATUNG 0x1460
  128. #define VENDOR_GATEWAY 0x107b
  129. #define VENDOR_SHUTTLE 0x1308
  130. #define VENDOR_SHUTTLE2 0x051c
  131. #define VENDOR_MITSUMI 0x03ee
  132. #define VENDOR_TOPSEED 0x1784
  133. #define VENDOR_RICAVISION 0x179d
  134. #define VENDOR_ITRON 0x195d
  135. #define VENDOR_FIC 0x1509
  136. #define VENDOR_LG 0x043e
  137. #define VENDOR_MICROSOFT 0x045e
  138. #define VENDOR_FORMOSA 0x147a
  139. #define VENDOR_FINTEK 0x1934
  140. #define VENDOR_PINNACLE 0x2304
  141. #define VENDOR_ECS 0x1019
  142. #define VENDOR_WISTRON 0x0fb8
  143. #define VENDOR_COMPRO 0x185b
  144. #define VENDOR_NORTHSTAR 0x04eb
  145.  
  146.  
  147. static struct usb_device_id mceusb_dev_table[] = {
  148. /* Original Microsoft MCE IR Transceiver (often HP-branded) */
  149. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  150. /* Philips Infrared Transceiver - Sahara branded */
  151. { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
  152. /* Philips Infrared Transceiver - HP branded */
  153. { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
  154. /* Philips SRM5100 */
  155. { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
  156. /* Philips Infrared Transceiver - Omaura */
  157. { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
  158. /* Philips Infrared Transceiver - Spinel plus */
  159. { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
  160. /* Philips eHome Infrared Transceiver */
  161. { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
  162. /* SMK/Toshiba G83C0004D410 */
  163. { USB_DEVICE(VENDOR_SMK, 0x031d) },
  164. /* SMK eHome Infrared Transceiver (Sony VAIO) */
  165. { USB_DEVICE(VENDOR_SMK, 0x0322) },
  166. /* bundled with Hauppauge PVR-150 */
  167. { USB_DEVICE(VENDOR_SMK, 0x0334) },
  168. /* Tatung eHome Infrared Transceiver */
  169. { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
  170. /* Shuttle eHome Infrared Transceiver */
  171. { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
  172. /* Shuttle eHome Infrared Transceiver */
  173. { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
  174. /* Gateway eHome Infrared Transceiver */
  175. { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
  176. /* Mitsumi */
  177. { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
  178. /* Topseed eHome Infrared Transceiver */
  179. { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
  180. /* Topseed HP eHome Infrared Transceiver */
  181. { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
  182. /* Topseed eHome Infrared Transceiver */
  183. { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
  184. /* Topseed eHome Infrared Transceiver */
  185. { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
  186. /* Topseed eHome Infrared Transceiver */
  187. { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
  188. /* Ricavision internal Infrared Transceiver */
  189. { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
  190. /* Itron ione Libra Q-11 */
  191. { USB_DEVICE(VENDOR_ITRON, 0x7002) },
  192. /* FIC eHome Infrared Transceiver */
  193. { USB_DEVICE(VENDOR_FIC, 0x9242) },
  194. /* LG eHome Infrared Transceiver */
  195. { USB_DEVICE(VENDOR_LG, 0x9803) },
  196. /* Microsoft MCE Infrared Transceiver */
  197. { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
  198. /* Formosa eHome Infrared Transceiver */
  199. { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
  200. /* Formosa21 / eHome Infrared Receiver */
  201. { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
  202. /* Formosa aim / Trust MCE Infrared Receiver */
  203. { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
  204. /* Formosa Industrial Computing / Beanbag Emulation Device */
  205. { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
  206. /* Formosa21 / eHome Infrared Receiver */
  207. { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
  208. /* Formosa Industrial Computing AIM IR605/A */
  209. { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
  210. /* Fintek eHome Infrared Transceiver */
  211. { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
  212. /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
  213. { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
  214. /* Pinnacle Remote Kit */
  215. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  216. /* Elitegroup Computer Systems IR */
  217. { USB_DEVICE(VENDOR_ECS, 0x0f38) },
  218. /* Wistron Corp. eHome Infrared Receiver */
  219. { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
  220. /* Compro K100 */
  221. { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
  222. /* Compro K100 v2 */
  223. { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
  224. /* Northstar Systems eHome Infrared Transceiver */
  225. { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
  226. /* HP Reciever From China */
  227. { USB_DEVICE(VENDOR_FINTEK, 0x5168) },
  228. /* Terminating entry */
  229. { }
  230. };
  231.  
  232. static struct usb_device_id pinnacle_list[] = {
  233. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  234. {}
  235. };
  236.  
  237. static struct usb_device_id microsoft_gen1_list[] = {
  238. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  239. {}
  240. };
  241.  
  242. static struct usb_device_id transmitter_mask_list[] = {
  243. { USB_DEVICE(VENDOR_SMK, 0x031d) },
  244. { USB_DEVICE(VENDOR_SMK, 0x0322) },
  245. { USB_DEVICE(VENDOR_SMK, 0x0334) },
  246. { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
  247. { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
  248. { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
  249. { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
  250. { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
  251. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  252. {}
  253. };
  254.  
  255. /* data structure for each usb transceiver */
  256. struct mceusb_dev {
  257.  
  258. /* usb */
  259. struct usb_device *usbdev;
  260. struct urb *urb_in;
  261. int devnum;
  262. struct usb_endpoint_descriptor *usb_ep_in;
  263. struct usb_endpoint_descriptor *usb_ep_out;
  264.  
  265. /* buffers and dma */
  266. unsigned char *buf_in;
  267. unsigned int len_in;
  268. dma_addr_t dma_in;
  269. dma_addr_t dma_out;
  270. unsigned int overflow_len;
  271.  
  272. /* lirc */
  273. struct lirc_driver *d;
  274. lirc_t lircdata;
  275. unsigned char is_pulse;
  276. struct {
  277. u32 connected:1;
  278. u32 pinnacle:1;
  279. u32 transmitter_mask_inverted:1;
  280. u32 microsoft_gen1:1;
  281. u32 reserved:28;
  282. } flags;
  283.  
  284. unsigned char transmitter_mask;
  285. unsigned int carrier_freq;
  286.  
  287. /* handle sending (init strings) */
  288. int send_flags;
  289. wait_queue_head_t wait_out;
  290.  
  291. struct mutex lock;
  292. };
  293.  
  294. /* init strings */
  295. static char init1[] = {0x00, 0xff, 0xaa, 0xff, 0x0b};
  296. static char init2[] = {0xff, 0x18};
  297.  
  298. static char pin_init1[] = { 0x9f, 0x07};
  299. static char pin_init2[] = { 0x9f, 0x13};
  300. static char pin_init3[] = { 0x9f, 0x0d};
  301.  
  302. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
  303. static unsigned long usecs_to_jiffies(const unsigned int u)
  304. {
  305. if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
  306. return MAX_JIFFY_OFFSET;
  307. #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
  308. return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ);
  309. #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
  310. return u * (HZ / USEC_PER_SEC);
  311. #else
  312. return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC;
  313. #endif
  314. }
  315. #endif
  316. static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf, int len)
  317. {
  318. char codes[USB_BUFLEN * 3 + 1];
  319. int i;
  320.  
  321. if (len <= 0)
  322. return;
  323.  
  324. if (ir->flags.microsoft_gen1 && len <= 2)
  325. return;
  326.  
  327. for (i = 0; i < len && i < USB_BUFLEN; i++)
  328. snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
  329.  
  330. printk(KERN_INFO "" DRIVER_NAME "[%d]: data received %s (length=%d)\n",
  331. ir->devnum, codes, len);
  332. }
  333.  
  334. static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
  335. {
  336. struct mceusb_dev *ir;
  337. int len;
  338.  
  339. if (!urb)
  340. return;
  341.  
  342. ir = urb->context;
  343. if (ir) {
  344. len = urb->actual_length;
  345.  
  346. dprintk(DRIVER_NAME
  347. "[%d]: callback called (status=%d len=%d)\n",
  348. ir->devnum, urb->status, len);
  349.  
  350. if (debug)
  351. mceusb_dev_printdata(ir, urb->transfer_buffer, len);
  352. }
  353.  
  354. }
  355.  
  356. /* request incoming or send outgoing usb packet - used to initialize remote */
  357. static void request_packet_async(struct mceusb_dev *ir,
  358. struct usb_endpoint_descriptor *ep,
  359. unsigned char *data, int size, int urb_type)
  360. {
  361. int res;
  362. struct urb *async_urb;
  363. unsigned char *async_buf;
  364.  
  365. if (urb_type) {
  366. async_urb = usb_alloc_urb(0, GFP_KERNEL);
  367. if (unlikely(!async_urb))
  368. return;
  369.  
  370. async_buf = kmalloc(size, GFP_KERNEL);
  371. if (!async_buf) {
  372. usb_free_urb(async_urb);
  373. return;
  374. }
  375.  
  376. if (urb_type == MCEUSB_OUTBOUND) {
  377. /* outbound data */
  378. usb_fill_int_urb(async_urb, ir->usbdev,
  379. usb_sndintpipe(ir->usbdev,
  380. ep->bEndpointAddress),
  381. async_buf, size,
  382. (usb_complete_t) usb_async_callback,
  383. ir, ep->bInterval);
  384. memcpy(async_buf, data, size);
  385. } else {
  386. /* inbound data */
  387. usb_fill_int_urb(async_urb, ir->usbdev,
  388. usb_rcvintpipe(ir->usbdev,
  389. ep->bEndpointAddress),
  390. async_buf, size,
  391. (usb_complete_t) usb_async_callback,
  392. ir, ep->bInterval);
  393. }
  394. async_urb->transfer_flags = URB_ASYNC_UNLINK;
  395. } else {
  396. /* standard request */
  397. async_urb = ir->urb_in;
  398. ir->send_flags = RECV_FLAG_IN_PROGRESS;
  399. }
  400.  
  401. dprintk(DRIVER_NAME "[%d]: receive request called (size=%#x)\n",
  402. ir->devnum, size);
  403.  
  404. async_urb->transfer_buffer_length = size;
  405. async_urb->dev = ir->usbdev;
  406.  
  407. res = usb_submit_urb(async_urb, GFP_ATOMIC);
  408. if (res) {
  409. dprintk(DRIVER_NAME "[%d]: receive request FAILED! (res=%d)\n",
  410. ir->devnum, res);
  411. return;
  412. }
  413. dprintk(DRIVER_NAME "[%d]: receive request complete (res=%d)\n",
  414. ir->devnum, res);
  415. }
  416.  
  417. static int unregister_from_lirc(struct mceusb_dev *ir)
  418. {
  419. struct lirc_driver *d = ir->d;
  420. int devnum;
  421. int rtn;
  422.  
  423. devnum = ir->devnum;
  424. dprintk(DRIVER_NAME "[%d]: unregister from lirc called\n", devnum);
  425.  
  426. rtn = lirc_unregister_driver(d->minor);
  427. if (rtn > 0) {
  428. printk(DRIVER_NAME "[%d]: error in lirc_unregister minor: %d\n"
  429. "Trying again...\n", devnum, d->minor);
  430. if (rtn == -EBUSY) {
  431. printk(DRIVER_NAME
  432. "[%d]: device is opened, will unregister"
  433. " on close\n", devnum);
  434. return -EAGAIN;
  435. }
  436. set_current_state(TASK_INTERRUPTIBLE);
  437. schedule_timeout(HZ);
  438.  
  439. rtn = lirc_unregister_driver(d->minor);
  440. if (rtn > 0)
  441. printk(DRIVER_NAME "[%d]: lirc_unregister failed\n",
  442. devnum);
  443. }
  444.  
  445. if (rtn) {
  446. printk(DRIVER_NAME "[%d]: didn't free resources\n", devnum);
  447. return -EAGAIN;
  448. }
  449.  
  450. printk(DRIVER_NAME "[%d]: usb remote disconnected\n", devnum);
  451.  
  452. lirc_buffer_free(d->rbuf);
  453. kfree(d->rbuf);
  454. kfree(d);
  455. kfree(ir);
  456. return 0;
  457. }
  458.  
  459. static int mceusb_ir_open(void *data)
  460. {
  461. struct mceusb_dev *ir = data;
  462.  
  463. if (!ir) {
  464. printk(DRIVER_NAME "[?]: %s called with no context\n",
  465. __func__);
  466. return -EIO;
  467. }
  468. dprintk(DRIVER_NAME "[%d]: mceusb IR device opened\n", ir->devnum);
  469.  
  470. MOD_INC_USE_COUNT;
  471. if (!ir->flags.connected) {
  472. if (!ir->usbdev)
  473. return -ENOENT;
  474. ir->flags.connected = 1;
  475. }
  476.  
  477. return 0;
  478. }
  479.  
  480. static void mceusb_ir_close(void *data)
  481. {
  482. struct mceusb_dev *ir = data;
  483.  
  484. if (!ir) {
  485. printk(DRIVER_NAME "[?]: %s called with no context\n",
  486. __func__);
  487. return;
  488. }
  489. dprintk(DRIVER_NAME "[%d]: mceusb IR device closed\n", ir->devnum);
  490.  
  491. if (ir->flags.connected) {
  492. mutex_lock(&ir->lock);
  493. ir->flags.connected = 0;
  494. mutex_unlock(&ir->lock);
  495. }
  496. MOD_DEC_USE_COUNT;
  497. }
  498.  
  499. static void send_packet_to_lirc(struct mceusb_dev *ir)
  500. {
  501. if (ir->lircdata) {
  502. lirc_buffer_write(ir->d->rbuf,
  503. (unsigned char *) &ir->lircdata);
  504. wake_up(&ir->d->rbuf->wait_poll);
  505. ir->lircdata = 0;
  506. }
  507. }
  508.  
  509. static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
  510. {
  511. int i, j;
  512. int packet_len = 0;
  513. int start_index = 0;
  514.  
  515. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  516. if (ir->flags.microsoft_gen1)
  517. start_index = 2;
  518.  
  519. /* this should only trigger w/the 1st-gen mce receiver */
  520. for (i = start_index; i < (start_index + ir->overflow_len) &&
  521. i < buf_len; i++) {
  522. /* rising/falling flank */
  523. if (ir->is_pulse != (ir->buf_in[i] & MCE_PULSE_BIT)) {
  524. send_packet_to_lirc(ir);
  525. ir->is_pulse = ir->buf_in[i] & MCE_PULSE_BIT;
  526. }
  527.  
  528. /* accumulate mce pulse/space values */
  529. ir->lircdata += (ir->buf_in[i] & MCE_PULSE_MASK) *
  530. MCE_TIME_UNIT;
  531. ir->lircdata |= (ir->is_pulse ? PULSE_BIT : 0);
  532. }
  533. start_index += ir->overflow_len;
  534. ir->overflow_len = 0;
  535.  
  536. for (i = start_index; i < buf_len; i++) {
  537. /* decode mce packets of the form (84),AA,BB,CC,DD */
  538. if (ir->buf_in[i] >= 0x80 && ir->buf_in[i] <= 0x9e) {
  539. /* data headers */
  540. /* decode packet data */
  541. packet_len = ir->buf_in[i] & MCE_PACKET_LENGTH_MASK;
  542. ir->overflow_len = i + 1 + packet_len - buf_len;
  543. for (j = 1; j <= packet_len && (i + j < buf_len); j++) {
  544. /* rising/falling flank */
  545. if (ir->is_pulse !=
  546. (ir->buf_in[i + j] & MCE_PULSE_BIT)) {
  547. send_packet_to_lirc(ir);
  548. ir->is_pulse =
  549. ir->buf_in[i + j] &
  550. MCE_PULSE_BIT;
  551. }
  552.  
  553. /* accumulate mce pulse/space values */
  554. ir->lircdata +=
  555. (ir->buf_in[i + j] & MCE_PULSE_MASK) *
  556. MCE_TIME_UNIT;
  557. ir->lircdata |= (ir->is_pulse ? PULSE_BIT : 0);
  558. }
  559.  
  560. i += packet_len;
  561. } else if (ir->buf_in[i] == MCE_CONTROL_HEADER) {
  562. /* status header (0x9F) */
  563. /*
  564. * A transmission containing one or more consecutive ir
  565. * commands always ends with a GAP of 100ms followed by
  566. * the sequence 0x9F 0x01 0x01 0x9F 0x15 0x00 0x00 0x80
  567. */
  568.  
  569. #if 0
  570. Uncomment this if the last 100ms "infinity"-space should be transmitted
  571. to lirc directly instead of at the beginning of the next transmission.
  572. Changes pulse/space order.
  573.  
  574. if (++i < buf_len && ir->buf_in[i]==0x01)
  575. send_packet_to_lirc(ir);
  576.  
  577. #endif
  578.  
  579. /* end decode loop */
  580. dprintk(DRIVER_NAME "[%d] %s: found control header\n",
  581. ir->devnum, __func__);
  582. ir->overflow_len = 0;
  583. break;
  584. } else {
  585. dprintk(DRIVER_NAME "[%d] %s: stray packet?\n",
  586. ir->devnum, __func__);
  587. ir->overflow_len = 0;
  588. }
  589. }
  590.  
  591. return;
  592. }
  593.  
  594. static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
  595. {
  596. struct mceusb_dev *ir;
  597. int buf_len;
  598.  
  599. if (!urb)
  600. return;
  601.  
  602. ir = urb->context;
  603. if (!ir) {
  604. urb->transfer_flags |= URB_ASYNC_UNLINK;
  605. usb_unlink_urb(urb);
  606. return;
  607. }
  608.  
  609. buf_len = urb->actual_length;
  610.  
  611. if (debug)
  612. mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len);
  613.  
  614. if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
  615. ir->send_flags = SEND_FLAG_COMPLETE;
  616. dprintk(DRIVER_NAME "[%d]: setup answer received %d bytes\n",
  617. ir->devnum, buf_len);
  618. }
  619.  
  620. switch (urb->status) {
  621. /* success */
  622. case 0:
  623. mceusb_process_ir_data(ir, buf_len);
  624. break;
  625.  
  626. case -ECONNRESET:
  627. case -ENOENT:
  628. case -ESHUTDOWN:
  629. urb->transfer_flags |= URB_ASYNC_UNLINK;
  630. usb_unlink_urb(urb);
  631. return;
  632.  
  633. case -EPIPE:
  634. default:
  635. break;
  636. }
  637.  
  638. usb_submit_urb(urb, GFP_ATOMIC);
  639. }
  640.  
  641.  
  642. static ssize_t mceusb_transmit_ir(struct file *file, const char *buf,
  643. size_t n, loff_t *ppos)
  644. {
  645. int i, count = 0, cmdcount = 0;
  646. struct mceusb_dev *ir = NULL;
  647. lirc_t wbuf[LIRCBUF_SIZE]; /* Workbuffer with values from lirc */
  648. unsigned char cmdbuf[MCE_CMDBUF_SIZE]; /* MCE command buffer */
  649. unsigned long signal_duration = 0; /* Singnal length in us */
  650. struct timeval start_time, end_time;
  651.  
  652. do_gettimeofday(&start_time);
  653.  
  654. /* Retrieve lirc_driver data for the device */
  655. ir = lirc_get_pdata(file);
  656. if (!ir || !ir->usb_ep_out)
  657. return -EFAULT;
  658.  
  659. if (n % sizeof(lirc_t))
  660. return -EINVAL;
  661. count = n / sizeof(lirc_t);
  662.  
  663. /* Check if command is within limits */
  664. if (count > LIRCBUF_SIZE || count%2 == 0)
  665. return -EINVAL;
  666. if (copy_from_user(wbuf, buf, n))
  667. return -EFAULT;
  668.  
  669. /* MCE tx init header */
  670. cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
  671. cmdbuf[cmdcount++] = 0x08;
  672. cmdbuf[cmdcount++] = ir->transmitter_mask;
  673.  
  674. /* Generate mce packet data */
  675. for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
  676. signal_duration += wbuf[i];
  677. wbuf[i] = wbuf[i] / MCE_TIME_UNIT;
  678.  
  679. do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
  680.  
  681. /* Insert mce packet header every 4th entry */
  682. if ((cmdcount < MCE_CMDBUF_SIZE) &&
  683. (cmdcount - MCE_TX_HEADER_LENGTH) %
  684. MCE_CODE_LENGTH == 0)
  685. cmdbuf[cmdcount++] = MCE_PACKET_HEADER;
  686.  
  687. /* Insert mce packet data */
  688. if (cmdcount < MCE_CMDBUF_SIZE)
  689. cmdbuf[cmdcount++] =
  690. (wbuf[i] < MCE_PULSE_BIT ?
  691. wbuf[i] : MCE_MAX_PULSE_LENGTH) |
  692. (i & 1 ? 0x00 : MCE_PULSE_BIT);
  693. else
  694. return -EINVAL;
  695. } while ((wbuf[i] > MCE_MAX_PULSE_LENGTH) &&
  696. (wbuf[i] -= MCE_MAX_PULSE_LENGTH));
  697. }
  698.  
  699. /* Fix packet length in last header */
  700. cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
  701. 0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1;
  702.  
  703. /* Check if we have room for the empty packet at the end */
  704. if (cmdcount >= MCE_CMDBUF_SIZE)
  705. return -EINVAL;
  706.  
  707. /* All mce commands end with an empty packet (0x80) */
  708. cmdbuf[cmdcount++] = 0x80;
  709.  
  710. /* Transmit the command to the mce device */
  711. request_packet_async(ir, ir->usb_ep_out, cmdbuf,
  712. cmdcount, MCEUSB_OUTBOUND);
  713.  
  714. /*
  715. * The lircd gap calculation expects the write function to
  716. * wait the time it takes for the ircommand to be sent before
  717. * it returns.
  718. */
  719. do_gettimeofday(&end_time);
  720. signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
  721. (end_time.tv_sec - start_time.tv_sec) * 1000000;
  722.  
  723. /* delay with the closest number of ticks */
  724. set_current_state(TASK_INTERRUPTIBLE);
  725. schedule_timeout(usecs_to_jiffies(signal_duration));
  726.  
  727. return n;
  728. }
  729.  
  730. static void set_transmitter_mask(struct mceusb_dev *ir, unsigned int mask)
  731. {
  732. if (ir->flags.transmitter_mask_inverted)
  733. /*
  734. * The mask begins at 0x02 and has an inverted
  735. * numbering scheme
  736. */
  737. ir->transmitter_mask =
  738. (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
  739. else
  740. ir->transmitter_mask = mask;
  741. }
  742.  
  743.  
  744. /* Sets the send carrier frequency */
  745. static int set_send_carrier(struct mceusb_dev *ir, int carrier)
  746. {
  747. int clk = 10000000;
  748. int prescaler = 0, divisor = 0;
  749. unsigned char cmdbuf[] = { 0x9F, 0x06, 0x01, 0x80 };
  750.  
  751. /* Carrier is changed */
  752. if (ir->carrier_freq != carrier) {
  753.  
  754. if (carrier <= 0) {
  755. ir->carrier_freq = carrier;
  756. dprintk(DRIVER_NAME "[%d]: SET_CARRIER disabling "
  757. "carrier modulation\n", ir->devnum);
  758. request_packet_async(ir, ir->usb_ep_out,
  759. cmdbuf, sizeof(cmdbuf),
  760. MCEUSB_OUTBOUND);
  761. return carrier;
  762. }
  763.  
  764. for (prescaler = 0; prescaler < 4; ++prescaler) {
  765. divisor = (clk >> (2 * prescaler)) / carrier;
  766. if (divisor <= 0xFF) {
  767. ir->carrier_freq = carrier;
  768. cmdbuf[2] = prescaler;
  769. cmdbuf[3] = divisor;
  770. dprintk(DRIVER_NAME "[%d]: SET_CARRIER "
  771. "requesting %d Hz\n",
  772. ir->devnum, carrier);
  773.  
  774. /* Transmit new carrier to mce device */
  775. request_packet_async(ir, ir->usb_ep_out,
  776. cmdbuf, sizeof(cmdbuf),
  777. MCEUSB_OUTBOUND);
  778. return carrier;
  779. }
  780. }
  781.  
  782. return -EINVAL;
  783.  
  784. }
  785.  
  786. return carrier;
  787. }
  788.  
  789.  
  790. static int mceusb_lirc_ioctl(struct inode *node, struct file *filep,
  791. unsigned int cmd, unsigned long arg)
  792. {
  793. int result;
  794. unsigned int ivalue;
  795. unsigned long lvalue;
  796. struct mceusb_dev *ir = NULL;
  797.  
  798. /* Retrieve lirc_driver data for the device */
  799. ir = lirc_get_pdata(filep);
  800. if (!ir || !ir->usb_ep_out)
  801. return -EFAULT;
  802.  
  803.  
  804. switch (cmd) {
  805. case LIRC_SET_TRANSMITTER_MASK:
  806.  
  807. result = get_user(ivalue, (unsigned int *) arg);
  808. if (result)
  809. return result;
  810. switch (ivalue) {
  811. case 0x01: /* Transmitter 1 => 0x04 */
  812. case 0x02: /* Transmitter 2 => 0x02 */
  813. case 0x03: /* Transmitter 1 & 2 => 0x06 */
  814. set_transmitter_mask(ir, ivalue);
  815. break;
  816.  
  817. default: /* Unsupported transmitter mask */
  818. return MCE_MAX_CHANNELS;
  819. }
  820.  
  821. dprintk(DRIVER_NAME ": SET_TRANSMITTERS mask=%d\n", ivalue);
  822. break;
  823.  
  824. case LIRC_GET_SEND_MODE:
  825.  
  826. result = put_user(LIRC_SEND2MODE(LIRC_CAN_SEND_PULSE &
  827. LIRC_CAN_SEND_MASK),
  828. (unsigned long *) arg);
  829.  
  830. if (result)
  831. return result;
  832. break;
  833.  
  834. case LIRC_SET_SEND_MODE:
  835.  
  836. result = get_user(lvalue, (unsigned long *) arg);
  837.  
  838. if (result)
  839. return result;
  840. if (lvalue != (LIRC_MODE_PULSE&LIRC_CAN_SEND_MASK))
  841. return -EINVAL;
  842. break;
  843.  
  844. case LIRC_SET_SEND_CARRIER:
  845.  
  846. result = get_user(ivalue, (unsigned int *) arg);
  847. if (result)
  848. return result;
  849.  
  850. set_send_carrier(ir, ivalue);
  851. break;
  852.  
  853. default:
  854. return -ENOIOCTLCMD;
  855. }
  856.  
  857. return 0;
  858. }
  859.  
  860. static struct file_operations lirc_fops = {
  861. .owner = THIS_MODULE,
  862. .write = mceusb_transmit_ir,
  863. .ioctl = mceusb_lirc_ioctl,
  864. };
  865.  
  866. static int mceusb_gen1_init(struct mceusb_dev *ir)
  867. {
  868. int i, ret;
  869. char junk[64], data[8];
  870. int partial = 0;
  871.  
  872. /*
  873. * Clear off the first few messages. These look like calibration
  874. * or test data, I can't really tell. This also flushes in case
  875. * we have random ir data queued up.
  876. */
  877. for (i = 0; i < 40; i++)
  878. usb_bulk_msg(ir->usbdev,
  879. usb_rcvbulkpipe(ir->usbdev,
  880. ir->usb_ep_in->bEndpointAddress),
  881. junk, 64, &partial, HZ * 10);
  882.  
  883. ir->is_pulse = 1;
  884.  
  885. memset(data, 0, 8);
  886.  
  887. /* Get Status */
  888. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  889. USB_REQ_GET_STATUS, USB_DIR_IN,
  890. 0, 0, data, 2, HZ * 3);
  891.  
  892. /* ret = usb_get_status( ir->usbdev, 0, 0, data ); */
  893. dprintk("%s - ret = %d status = 0x%x 0x%x\n", __func__,
  894. ret, data[0], data[1]);
  895.  
  896. /*
  897. * This is a strange one. They issue a set address to the device
  898. * on the receive control pipe and expect a certain value pair back
  899. */
  900. memset(data, 0, 8);
  901.  
  902. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  903. USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
  904. data, 2, HZ * 3);
  905. dprintk("%s - ret = %d, devnum = %d\n",
  906. __func__, ret, ir->usbdev->devnum);
  907. dprintk("%s - data[0] = %d, data[1] = %d\n",
  908. __func__, data[0], data[1]);
  909.  
  910. /* set feature */
  911. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  912. USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
  913. 0xc04e, 0x0000, NULL, 0, HZ * 3);
  914.  
  915. dprintk("%s - ret = %d\n", __func__, ret);
  916.  
  917. /* strange: bRequest == 4 */
  918. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  919. 4, USB_TYPE_VENDOR,
  920. 0x0808, 0x0000, NULL, 0, HZ * 3);
  921. dprintk("%s - retB = %d\n", __func__, ret);
  922.  
  923. /* strange: bRequest == 2 */
  924. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  925. 2, USB_TYPE_VENDOR,
  926. 0x0000, 0x0100, NULL, 0, HZ * 3);
  927. dprintk("%s - retC = %d\n", __func__, ret);
  928.  
  929. return ret;
  930.  
  931. };
  932.  
  933. static int mceusb_dev_probe(struct usb_interface *intf,
  934. const struct usb_device_id *id)
  935. {
  936. struct usb_device *dev = interface_to_usbdev(intf);
  937. struct usb_host_interface *idesc;
  938. struct usb_endpoint_descriptor *ep = NULL;
  939. struct usb_endpoint_descriptor *ep_in = NULL;
  940. struct usb_endpoint_descriptor *ep_out = NULL;
  941. struct usb_host_config *config;
  942. struct mceusb_dev *ir = NULL;
  943. struct lirc_driver *driver = NULL;
  944. struct lirc_buffer *rbuf = NULL;
  945. int devnum, pipe, maxp;
  946. int minor = 0;
  947. int i;
  948. char buf[63], name[128] = "";
  949. int mem_failure = 0;
  950. int is_pinnacle;
  951. int is_microsoft_gen1;
  952.  
  953. dprintk(DRIVER_NAME ": %s called\n", __func__);
  954.  
  955. usb_reset_device(dev);
  956.  
  957. config = dev->actconfig;
  958.  
  959. idesc = intf->cur_altsetting;
  960.  
  961. is_pinnacle = usb_match_id(intf, pinnacle_list) ? 1 : 0;
  962.  
  963. is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
  964.  
  965. /* step through the endpoints to find first bulk in and out endpoint */
  966. for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
  967. ep = &idesc->endpoint[i].desc;
  968.  
  969. if ((ep_in == NULL)
  970. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  971. == USB_DIR_IN)
  972. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  973. == USB_ENDPOINT_XFER_BULK)
  974. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  975. == USB_ENDPOINT_XFER_INT))) {
  976.  
  977. dprintk(DRIVER_NAME ": acceptable inbound endpoint "
  978. "found\n");
  979. ep_in = ep;
  980. ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
  981. if (is_pinnacle)
  982. /*
  983. * setting seems to 1 seem to cause issues with
  984. * Pinnacle timing out on transfer.
  985. */
  986. ep_in->bInterval = ep->bInterval;
  987. else
  988. ep_in->bInterval = 1;
  989. }
  990.  
  991. if ((ep_out == NULL)
  992. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  993. == USB_DIR_OUT)
  994. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  995. == USB_ENDPOINT_XFER_BULK)
  996. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  997. == USB_ENDPOINT_XFER_INT))) {
  998.  
  999. dprintk(DRIVER_NAME ": acceptable outbound endpoint "
  1000. "found\n");
  1001. ep_out = ep;
  1002. ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
  1003. if (is_pinnacle)
  1004. /*
  1005. * setting seems to 1 seem to cause issues with
  1006. * Pinnacle timing out on transfer.
  1007. */
  1008. ep_out->bInterval = ep->bInterval;
  1009. else
  1010. ep_out->bInterval = 1;
  1011. }
  1012. }
  1013. if (ep_in == NULL || ep_out == NULL) {
  1014. dprintk(DRIVER_NAME ": inbound and/or "
  1015. "outbound endpoint not found\n");
  1016. return -ENODEV;
  1017. }
  1018.  
  1019. devnum = dev->devnum;
  1020. pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
  1021. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  1022.  
  1023. mem_failure = 0;
  1024. ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
  1025. if (!ir) {
  1026. mem_failure = 1;
  1027. goto mem_failure_switch;
  1028. }
  1029.  
  1030. driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
  1031. if (!driver) {
  1032. mem_failure = 2;
  1033. goto mem_failure_switch;
  1034. }
  1035.  
  1036. rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
  1037. if (!rbuf) {
  1038. mem_failure = 3;
  1039. goto mem_failure_switch;
  1040. }
  1041.  
  1042. if (lirc_buffer_init(rbuf, sizeof(lirc_t), LIRCBUF_SIZE)) {
  1043. mem_failure = 4;
  1044. goto mem_failure_switch;
  1045. }
  1046.  
  1047. ir->buf_in = usb_buffer_alloc(dev, maxp, GFP_ATOMIC, &ir->dma_in);
  1048. if (!ir->buf_in) {
  1049. mem_failure = 5;
  1050. goto mem_failure_switch;
  1051. }
  1052.  
  1053. ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  1054. if (!ir->urb_in) {
  1055. mem_failure = 7;
  1056. goto mem_failure_switch;
  1057. }
  1058.  
  1059. strcpy(driver->name, DRIVER_NAME " ");
  1060. driver->minor = -1;
  1061. driver->features = LIRC_CAN_SEND_PULSE |
  1062. LIRC_CAN_SET_TRANSMITTER_MASK |
  1063. LIRC_CAN_REC_MODE2 |
  1064. LIRC_CAN_SET_SEND_CARRIER;
  1065. driver->data = ir;
  1066. driver->rbuf = rbuf;
  1067. driver->set_use_inc = &mceusb_ir_open;
  1068. driver->set_use_dec = &mceusb_ir_close;
  1069. driver->code_length = sizeof(lirc_t) * 8;
  1070. driver->fops = &lirc_fops;
  1071. driver->dev = &intf->dev;
  1072. driver->owner = THIS_MODULE;
  1073.  
  1074. mutex_init(&ir->lock);
  1075. init_waitqueue_head(&ir->wait_out);
  1076.  
  1077. minor = lirc_register_driver(driver);
  1078. if (minor < 0)
  1079. mem_failure = 9;
  1080.  
  1081. mem_failure_switch:
  1082.  
  1083. switch (mem_failure) {
  1084. case 9:
  1085. usb_free_urb(ir->urb_in);
  1086. case 7:
  1087. usb_buffer_free(dev, maxp, ir->buf_in, ir->dma_in);
  1088. case 5:
  1089. lirc_buffer_free(rbuf);
  1090. case 4:
  1091. kfree(rbuf);
  1092. case 3:
  1093. kfree(driver);
  1094. case 2:
  1095. kfree(ir);
  1096. case 1:
  1097. printk(DRIVER_NAME "[%d]: out of memory (code=%d)\n",
  1098. devnum, mem_failure);
  1099. return -ENOMEM;
  1100. }
  1101.  
  1102. driver->minor = minor;
  1103. ir->d = driver;
  1104. ir->devnum = devnum;
  1105. ir->usbdev = dev;
  1106. ir->len_in = maxp;
  1107. ir->overflow_len = 0;
  1108. ir->flags.connected = 0;
  1109. ir->flags.pinnacle = is_pinnacle;
  1110. ir->flags.microsoft_gen1 = is_microsoft_gen1;
  1111. ir->flags.transmitter_mask_inverted =
  1112. usb_match_id(intf, transmitter_mask_list) ? 0 : 1;
  1113.  
  1114. ir->lircdata = PULSE_MASK;
  1115. ir->is_pulse = 0;
  1116.  
  1117. /* ir->flags.transmitter_mask_inverted must be set */
  1118. set_transmitter_mask(ir, MCE_DEFAULT_TX_MASK);
  1119. /* Saving usb interface data for use by the transmitter routine */
  1120. ir->usb_ep_in = ep_in;
  1121. ir->usb_ep_out = ep_out;
  1122.  
  1123. if (dev->descriptor.iManufacturer
  1124. && usb_string(dev, dev->descriptor.iManufacturer,
  1125. buf, sizeof(buf)) > 0)
  1126. strlcpy(name, buf, sizeof(name));
  1127. if (dev->descriptor.iProduct
  1128. && usb_string(dev, dev->descriptor.iProduct,
  1129. buf, sizeof(buf)) > 0)
  1130. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  1131. " %s", buf);
  1132. printk(DRIVER_NAME "[%d]: %s on usb%d:%d\n", devnum, name,
  1133. dev->bus->busnum, devnum);
  1134.  
  1135. /* inbound data */
  1136. usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
  1137. maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
  1138. ir->urb_in->transfer_dma = ir->dma_in;
  1139. ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1140.  
  1141. /* initialize device */
  1142. if (ir->flags.pinnacle) {
  1143. int usbret;
  1144.  
  1145. /*
  1146. * I have no idea why but this reset seems to be crucial to
  1147. * getting the device to do outbound IO correctly - without
  1148. * this the device seems to hang, ignoring all input - although
  1149. * IR signals are correctly sent from the device, no input is
  1150. * interpreted by the device and the host never does the
  1151. * completion routine
  1152. */
  1153.  
  1154. usbret = usb_reset_configuration(dev);
  1155. printk(DRIVER_NAME "[%d]: usb reset config ret %x\n",
  1156. devnum, usbret);
  1157.  
  1158. /*
  1159. * its possible we really should wait for a return
  1160. * for each of these...
  1161. */
  1162. request_packet_async(ir, ep_in, NULL, maxp, MCEUSB_INBOUND);
  1163. request_packet_async(ir, ep_out, pin_init1, sizeof(pin_init1),
  1164. MCEUSB_OUTBOUND);
  1165. request_packet_async(ir, ep_in, NULL, maxp, MCEUSB_INBOUND);
  1166. request_packet_async(ir, ep_out, pin_init2, sizeof(pin_init2),
  1167. MCEUSB_OUTBOUND);
  1168. request_packet_async(ir, ep_in, NULL, maxp, MCEUSB_INBOUND);
  1169. request_packet_async(ir, ep_out, pin_init3, sizeof(pin_init3),
  1170. MCEUSB_OUTBOUND);
  1171. } else if (ir->flags.microsoft_gen1) {
  1172. /* original ms mce device requires some additional setup */
  1173. mceusb_gen1_init(ir);
  1174. } else {
  1175.  
  1176. request_packet_async(ir, ep_in, NULL, maxp, MCEUSB_INBOUND);
  1177. request_packet_async(ir, ep_in, NULL, maxp, MCEUSB_INBOUND);
  1178. request_packet_async(ir, ep_out, init1,
  1179. sizeof(init1), MCEUSB_OUTBOUND);
  1180. request_packet_async(ir, ep_in, NULL, maxp, MCEUSB_INBOUND);
  1181. request_packet_async(ir, ep_out, init2,
  1182. sizeof(init2), MCEUSB_OUTBOUND);
  1183. }
  1184.  
  1185. /*
  1186. * if we don't issue the correct number of receives (MCEUSB_INBOUND)
  1187. * for each outbound, then the first few ir pulses will be interpreted
  1188. * by the usb_async_callback routine - we should ensure we have the
  1189. * right amount OR less - as the meusb_dev_recv routine will handle
  1190. * the control packets OK - they start with 0x9f - but the async
  1191. * callback doesn't handle ir pulse packets
  1192. */
  1193. request_packet_async(ir, ep_in, NULL, maxp, 0);
  1194.  
  1195. usb_set_intfdata(intf, ir);
  1196.  
  1197. return 0;
  1198. }
  1199.  
  1200.  
  1201. static void mceusb_dev_disconnect(struct usb_interface *intf)
  1202. {
  1203. struct usb_device *dev = interface_to_usbdev(intf);
  1204. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1205.  
  1206. usb_set_intfdata(intf, NULL);
  1207.  
  1208. if (!ir || !ir->d)
  1209. return;
  1210.  
  1211. ir->usbdev = NULL;
  1212. wake_up_all(&ir->wait_out);
  1213.  
  1214. mutex_lock(&ir->lock);
  1215. usb_kill_urb(ir->urb_in);
  1216. usb_free_urb(ir->urb_in);
  1217. usb_buffer_free(dev, ir->len_in, ir->buf_in, ir->dma_in);
  1218. mutex_unlock(&ir->lock);
  1219.  
  1220. unregister_from_lirc(ir);
  1221. }
  1222.  
  1223. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
  1224. static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
  1225. {
  1226. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1227. printk(DRIVER_NAME "[%d]: suspend\n", ir->devnum);
  1228. usb_kill_urb(ir->urb_in);
  1229. return 0;
  1230. }
  1231.  
  1232. static int mceusb_dev_resume(struct usb_interface *intf)
  1233. {
  1234. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1235. printk(DRIVER_NAME "[%d]: resume\n", ir->devnum);
  1236. if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
  1237. return -EIO;
  1238. return 0;
  1239. }
  1240. #endif
  1241.  
  1242. static struct usb_driver mceusb_dev_driver = {
  1243. LIRC_THIS_MODULE(.owner = THIS_MODULE)
  1244. .name = DRIVER_NAME,
  1245. .probe = mceusb_dev_probe,
  1246. .disconnect = mceusb_dev_disconnect,
  1247. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
  1248. .suspend = mceusb_dev_suspend,
  1249. .resume = mceusb_dev_resume,
  1250. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
  1251. .reset_resume = mceusb_dev_resume,
  1252. #endif
  1253. #endif
  1254. .id_table = mceusb_dev_table
  1255. };
  1256.  
  1257. static int __init mceusb_dev_init(void)
  1258. {
  1259. int i;
  1260.  
  1261. printk(KERN_INFO DRIVER_NAME ": " DRIVER_DESC " " DRIVER_VERSION "\n");
  1262. printk(KERN_INFO DRIVER_NAME ": " DRIVER_AUTHOR "\n");
  1263. dprintk(DRIVER_NAME ": debug mode enabled\n");
  1264.  
  1265. i = usb_register(&mceusb_dev_driver);
  1266. if (i < 0) {
  1267. printk(DRIVER_NAME ": usb register failed, result = %d\n", i);
  1268. return -ENODEV;
  1269. }
  1270.  
  1271. return 0;
  1272. }
  1273.  
  1274. static void __exit mceusb_dev_exit(void)
  1275. {
  1276. usb_deregister(&mceusb_dev_driver);
  1277. }
  1278.  
  1279. module_init(mceusb_dev_init);
  1280. module_exit(mceusb_dev_exit);
  1281.  
  1282. MODULE_DESCRIPTION(DRIVER_DESC);
  1283. MODULE_AUTHOR(DRIVER_AUTHOR);
  1284. MODULE_LICENSE("GPL");
  1285. MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
  1286. /* this was originally lirc_mceusb2, lirc_mceusb and lirc_mceusb2 merged now */
  1287. MODULE_ALIAS("lirc_mceusb2");
  1288.  
  1289. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1290. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1291.  
  1292. EXPORT_NO_SYMBOLS;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement