Advertisement
Guest User

peterbo

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