PageRenderTime 157ms CodeModel.GetById 47ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/media/rc/mceusb.c

https://github.com/alwold/rpi-linux
C | 1464 lines | 1065 code | 189 blank | 210 comment | 132 complexity | d69da6fe6853950f19e2f1ac8cade588 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
  3. *
  4. * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
  5. *
  6. * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
  7. * Conti, Martin Blatter and Daniel Melander, the latter of which was
  8. * in turn also based on the lirc_atiusb driver by Paul Miller. The
  9. * two mce drivers were merged into one by Jarod Wilson, with transmit
  10. * support for the 1st-gen device added primarily by Patrick Calhoun,
  11. * with a bit of tweaks by Jarod. Debugging improvements and proper
  12. * support for what appears to be 3rd-gen hardware added by Jarod.
  13. * Initial port from lirc driver to ir-core drivery by Jarod, based
  14. * partially on a port to an earlier proposed IR infrastructure by
  15. * Jon Smirl, which included enhancements and simplifications to the
  16. * incoming IR buffer parsing routines.
  17. *
  18. * Updated in July of 2011 with the aid of Microsoft's official
  19. * remote/transceiver requirements and specification document, found at
  20. * download.microsoft.com, title
  21. * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
  22. *
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  37. *
  38. */
  39. #include <linux/device.h>
  40. #include <linux/module.h>
  41. #include <linux/slab.h>
  42. #include <linux/usb.h>
  43. #include <linux/usb/input.h>
  44. #include <linux/pm_wakeup.h>
  45. #include <media/rc-core.h>
  46. #define DRIVER_VERSION "1.92"
  47. #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
  48. #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
  49. "device driver"
  50. #define DRIVER_NAME "mceusb"
  51. #define USB_BUFLEN 32 /* USB reception buffer length */
  52. #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
  53. #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
  54. /* MCE constants */
  55. #define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
  56. #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
  57. #define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
  58. #define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
  59. #define MCE_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
  60. #define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
  61. #define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
  62. #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
  63. #define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
  64. #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
  65. #define MCE_PULSE_MASK 0x7f /* Pulse mask */
  66. #define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
  67. /*
  68. * The interface between the host and the IR hardware is command-response
  69. * based. All commands and responses have a consistent format, where a lead
  70. * byte always identifies the type of data following it. The lead byte has
  71. * a port value in the 3 highest bits and a length value in the 5 lowest
  72. * bits.
  73. *
  74. * The length field is overloaded, with a value of 11111 indicating that the
  75. * following byte is a command or response code, and the length of the entire
  76. * message is determined by the code. If the length field is not 11111, then
  77. * it specifies the number of bytes of port data that follow.
  78. */
  79. #define MCE_CMD 0x1f
  80. #define MCE_PORT_IR 0x4 /* (0x4 << 5) | MCE_CMD = 0x9f */
  81. #define MCE_PORT_SYS 0x7 /* (0x7 << 5) | MCE_CMD = 0xff */
  82. #define MCE_PORT_SER 0x6 /* 0xc0 thru 0xdf flush & 0x1f bytes */
  83. #define MCE_PORT_MASK 0xe0 /* Mask out command bits */
  84. /* Command port headers */
  85. #define MCE_CMD_PORT_IR 0x9f /* IR-related cmd/rsp */
  86. #define MCE_CMD_PORT_SYS 0xff /* System (non-IR) device cmd/rsp */
  87. /* Commands that set device state (2-4 bytes in length) */
  88. #define MCE_CMD_RESET 0xfe /* Reset device, 2 bytes */
  89. #define MCE_CMD_RESUME 0xaa /* Resume device after error, 2 bytes */
  90. #define MCE_CMD_SETIRCFS 0x06 /* Set tx carrier, 4 bytes */
  91. #define MCE_CMD_SETIRTIMEOUT 0x0c /* Set timeout, 4 bytes */
  92. #define MCE_CMD_SETIRTXPORTS 0x08 /* Set tx ports, 3 bytes */
  93. #define MCE_CMD_SETIRRXPORTEN 0x14 /* Set rx ports, 3 bytes */
  94. #define MCE_CMD_FLASHLED 0x23 /* Flash receiver LED, 2 bytes */
  95. /* Commands that query device state (all 2 bytes, unless noted) */
  96. #define MCE_CMD_GETIRCFS 0x07 /* Get carrier */
  97. #define MCE_CMD_GETIRTIMEOUT 0x0d /* Get timeout */
  98. #define MCE_CMD_GETIRTXPORTS 0x13 /* Get tx ports */
  99. #define MCE_CMD_GETIRRXPORTEN 0x15 /* Get rx ports */
  100. #define MCE_CMD_GETPORTSTATUS 0x11 /* Get tx port status, 3 bytes */
  101. #define MCE_CMD_GETIRNUMPORTS 0x16 /* Get number of ports */
  102. #define MCE_CMD_GETWAKESOURCE 0x17 /* Get wake source */
  103. #define MCE_CMD_GETEMVER 0x22 /* Get emulator interface version */
  104. #define MCE_CMD_GETDEVDETAILS 0x21 /* Get device details (em ver2 only) */
  105. #define MCE_CMD_GETWAKESUPPORT 0x20 /* Get wake details (em ver2 only) */
  106. #define MCE_CMD_GETWAKEVERSION 0x18 /* Get wake pattern (em ver2 only) */
  107. /* Misc commands */
  108. #define MCE_CMD_NOP 0xff /* No operation */
  109. /* Responses to commands (non-error cases) */
  110. #define MCE_RSP_EQIRCFS 0x06 /* tx carrier, 4 bytes */
  111. #define MCE_RSP_EQIRTIMEOUT 0x0c /* rx timeout, 4 bytes */
  112. #define MCE_RSP_GETWAKESOURCE 0x17 /* wake source, 3 bytes */
  113. #define MCE_RSP_EQIRTXPORTS 0x08 /* tx port mask, 3 bytes */
  114. #define MCE_RSP_EQIRRXPORTEN 0x14 /* rx port mask, 3 bytes */
  115. #define MCE_RSP_GETPORTSTATUS 0x11 /* tx port status, 7 bytes */
  116. #define MCE_RSP_EQIRRXCFCNT 0x15 /* rx carrier count, 4 bytes */
  117. #define MCE_RSP_EQIRNUMPORTS 0x16 /* number of ports, 4 bytes */
  118. #define MCE_RSP_EQWAKESUPPORT 0x20 /* wake capabilities, 3 bytes */
  119. #define MCE_RSP_EQWAKEVERSION 0x18 /* wake pattern details, 6 bytes */
  120. #define MCE_RSP_EQDEVDETAILS 0x21 /* device capabilities, 3 bytes */
  121. #define MCE_RSP_EQEMVER 0x22 /* emulator interface ver, 3 bytes */
  122. #define MCE_RSP_FLASHLED 0x23 /* success flashing LED, 2 bytes */
  123. /* Responses to error cases, must send MCE_CMD_RESUME to clear them */
  124. #define MCE_RSP_CMD_ILLEGAL 0xfe /* illegal command for port, 2 bytes */
  125. #define MCE_RSP_TX_TIMEOUT 0x81 /* tx timed out, 2 bytes */
  126. /* Misc commands/responses not defined in the MCE remote/transceiver spec */
  127. #define MCE_CMD_SIG_END 0x01 /* End of signal */
  128. #define MCE_CMD_PING 0x03 /* Ping device */
  129. #define MCE_CMD_UNKNOWN 0x04 /* Unknown */
  130. #define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
  131. #define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
  132. #define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
  133. #define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
  134. #define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
  135. #define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
  136. #define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
  137. #define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
  138. #define MCE_CMD_NULL 0x00 /* These show up various places... */
  139. /* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
  140. * then we're looking at a raw IR data sample */
  141. #define MCE_COMMAND_IRDATA 0x80
  142. #define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
  143. /* module parameters */
  144. #ifdef CONFIG_USB_DEBUG
  145. static bool debug = 1;
  146. #else
  147. static bool debug;
  148. #endif
  149. #define mce_dbg(dev, fmt, ...) \
  150. do { \
  151. if (debug) \
  152. dev_info(dev, fmt, ## __VA_ARGS__); \
  153. } while (0)
  154. /* general constants */
  155. #define SEND_FLAG_IN_PROGRESS 1
  156. #define SEND_FLAG_COMPLETE 2
  157. #define RECV_FLAG_IN_PROGRESS 3
  158. #define RECV_FLAG_COMPLETE 4
  159. #define MCEUSB_RX 1
  160. #define MCEUSB_TX 2
  161. #define VENDOR_PHILIPS 0x0471
  162. #define VENDOR_SMK 0x0609
  163. #define VENDOR_TATUNG 0x1460
  164. #define VENDOR_GATEWAY 0x107b
  165. #define VENDOR_SHUTTLE 0x1308
  166. #define VENDOR_SHUTTLE2 0x051c
  167. #define VENDOR_MITSUMI 0x03ee
  168. #define VENDOR_TOPSEED 0x1784
  169. #define VENDOR_RICAVISION 0x179d
  170. #define VENDOR_ITRON 0x195d
  171. #define VENDOR_FIC 0x1509
  172. #define VENDOR_LG 0x043e
  173. #define VENDOR_MICROSOFT 0x045e
  174. #define VENDOR_FORMOSA 0x147a
  175. #define VENDOR_FINTEK 0x1934
  176. #define VENDOR_PINNACLE 0x2304
  177. #define VENDOR_ECS 0x1019
  178. #define VENDOR_WISTRON 0x0fb8
  179. #define VENDOR_COMPRO 0x185b
  180. #define VENDOR_NORTHSTAR 0x04eb
  181. #define VENDOR_REALTEK 0x0bda
  182. #define VENDOR_TIVO 0x105a
  183. #define VENDOR_CONEXANT 0x0572
  184. #define VENDOR_TWISTEDMELON 0x2596
  185. enum mceusb_model_type {
  186. MCE_GEN2 = 0, /* Most boards */
  187. MCE_GEN1,
  188. MCE_GEN3,
  189. MCE_GEN2_TX_INV,
  190. POLARIS_EVK,
  191. CX_HYBRID_TV,
  192. MULTIFUNCTION,
  193. TIVO_KIT,
  194. MCE_GEN2_NO_TX,
  195. };
  196. struct mceusb_model {
  197. u32 mce_gen1:1;
  198. u32 mce_gen2:1;
  199. u32 mce_gen3:1;
  200. u32 tx_mask_normal:1;
  201. u32 no_tx:1;
  202. int ir_intfnum;
  203. const char *rc_map; /* Allow specify a per-board map */
  204. const char *name; /* per-board name */
  205. };
  206. static const struct mceusb_model mceusb_model[] = {
  207. [MCE_GEN1] = {
  208. .mce_gen1 = 1,
  209. .tx_mask_normal = 1,
  210. },
  211. [MCE_GEN2] = {
  212. .mce_gen2 = 1,
  213. },
  214. [MCE_GEN2_NO_TX] = {
  215. .mce_gen2 = 1,
  216. .no_tx = 1,
  217. },
  218. [MCE_GEN2_TX_INV] = {
  219. .mce_gen2 = 1,
  220. .tx_mask_normal = 1,
  221. },
  222. [MCE_GEN3] = {
  223. .mce_gen3 = 1,
  224. .tx_mask_normal = 1,
  225. },
  226. [POLARIS_EVK] = {
  227. /*
  228. * In fact, the EVK is shipped without
  229. * remotes, but we should have something handy,
  230. * to allow testing it
  231. */
  232. .rc_map = RC_MAP_HAUPPAUGE,
  233. .name = "Conexant Hybrid TV (cx231xx) MCE IR",
  234. },
  235. [CX_HYBRID_TV] = {
  236. .no_tx = 1, /* tx isn't wired up at all */
  237. .name = "Conexant Hybrid TV (cx231xx) MCE IR",
  238. },
  239. [MULTIFUNCTION] = {
  240. .mce_gen2 = 1,
  241. .ir_intfnum = 2,
  242. },
  243. [TIVO_KIT] = {
  244. .mce_gen2 = 1,
  245. .rc_map = RC_MAP_TIVO,
  246. },
  247. };
  248. static struct usb_device_id mceusb_dev_table[] = {
  249. /* Original Microsoft MCE IR Transceiver (often HP-branded) */
  250. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
  251. .driver_info = MCE_GEN1 },
  252. /* Philips Infrared Transceiver - Sahara branded */
  253. { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
  254. /* Philips Infrared Transceiver - HP branded */
  255. { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
  256. .driver_info = MCE_GEN2_TX_INV },
  257. /* Philips SRM5100 */
  258. { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
  259. /* Philips Infrared Transceiver - Omaura */
  260. { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
  261. /* Philips Infrared Transceiver - Spinel plus */
  262. { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
  263. /* Philips eHome Infrared Transceiver */
  264. { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
  265. /* Philips/Spinel plus IR transceiver for ASUS */
  266. { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
  267. /* Philips/Spinel plus IR transceiver for ASUS */
  268. { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
  269. /* Philips IR transceiver (Dell branded) */
  270. { USB_DEVICE(VENDOR_PHILIPS, 0x2093) },
  271. /* Realtek MCE IR Receiver and card reader */
  272. { USB_DEVICE(VENDOR_REALTEK, 0x0161),
  273. .driver_info = MULTIFUNCTION },
  274. /* SMK/Toshiba G83C0004D410 */
  275. { USB_DEVICE(VENDOR_SMK, 0x031d),
  276. .driver_info = MCE_GEN2_TX_INV },
  277. /* SMK eHome Infrared Transceiver (Sony VAIO) */
  278. { USB_DEVICE(VENDOR_SMK, 0x0322),
  279. .driver_info = MCE_GEN2_TX_INV },
  280. /* bundled with Hauppauge PVR-150 */
  281. { USB_DEVICE(VENDOR_SMK, 0x0334),
  282. .driver_info = MCE_GEN2_TX_INV },
  283. /* SMK eHome Infrared Transceiver */
  284. { USB_DEVICE(VENDOR_SMK, 0x0338) },
  285. /* SMK/I-O Data GV-MC7/RCKIT Receiver */
  286. { USB_DEVICE(VENDOR_SMK, 0x0353),
  287. .driver_info = MCE_GEN2_NO_TX },
  288. /* Tatung eHome Infrared Transceiver */
  289. { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
  290. /* Shuttle eHome Infrared Transceiver */
  291. { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
  292. /* Shuttle eHome Infrared Transceiver */
  293. { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
  294. /* Gateway eHome Infrared Transceiver */
  295. { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
  296. /* Mitsumi */
  297. { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
  298. /* Topseed eHome Infrared Transceiver */
  299. { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
  300. .driver_info = MCE_GEN2_TX_INV },
  301. /* Topseed HP eHome Infrared Transceiver */
  302. { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
  303. .driver_info = MCE_GEN2_TX_INV },
  304. /* Topseed eHome Infrared Transceiver */
  305. { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
  306. .driver_info = MCE_GEN2_TX_INV },
  307. /* Topseed eHome Infrared Transceiver */
  308. { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
  309. .driver_info = MCE_GEN3 },
  310. /* Topseed eHome Infrared Transceiver */
  311. { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
  312. .driver_info = MCE_GEN2_TX_INV },
  313. /* Topseed eHome Infrared Transceiver */
  314. { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
  315. .driver_info = MCE_GEN3 },
  316. /* Ricavision internal Infrared Transceiver */
  317. { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
  318. /* Itron ione Libra Q-11 */
  319. { USB_DEVICE(VENDOR_ITRON, 0x7002) },
  320. /* FIC eHome Infrared Transceiver */
  321. { USB_DEVICE(VENDOR_FIC, 0x9242) },
  322. /* LG eHome Infrared Transceiver */
  323. { USB_DEVICE(VENDOR_LG, 0x9803) },
  324. /* Microsoft MCE Infrared Transceiver */
  325. { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
  326. /* Formosa eHome Infrared Transceiver */
  327. { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
  328. /* Formosa21 / eHome Infrared Receiver */
  329. { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
  330. /* Formosa aim / Trust MCE Infrared Receiver */
  331. { USB_DEVICE(VENDOR_FORMOSA, 0xe017),
  332. .driver_info = MCE_GEN2_NO_TX },
  333. /* Formosa Industrial Computing / Beanbag Emulation Device */
  334. { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
  335. /* Formosa21 / eHome Infrared Receiver */
  336. { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
  337. /* Formosa Industrial Computing AIM IR605/A */
  338. { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
  339. /* Formosa Industrial Computing */
  340. { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
  341. /* Formosa Industrial Computing */
  342. { USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
  343. /* Fintek eHome Infrared Transceiver (HP branded) */
  344. { USB_DEVICE(VENDOR_FINTEK, 0x5168) },
  345. /* Fintek eHome Infrared Transceiver */
  346. { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
  347. /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
  348. { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
  349. /* Pinnacle Remote Kit */
  350. { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
  351. .driver_info = MCE_GEN3 },
  352. /* Elitegroup Computer Systems IR */
  353. { USB_DEVICE(VENDOR_ECS, 0x0f38) },
  354. /* Wistron Corp. eHome Infrared Receiver */
  355. { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
  356. /* Compro K100 */
  357. { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
  358. /* Compro K100 v2 */
  359. { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
  360. /* Northstar Systems, Inc. eHome Infrared Transceiver */
  361. { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
  362. /* TiVo PC IR Receiver */
  363. { USB_DEVICE(VENDOR_TIVO, 0x2000),
  364. .driver_info = TIVO_KIT },
  365. /* Conexant Hybrid TV "Shelby" Polaris SDK */
  366. { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
  367. .driver_info = POLARIS_EVK },
  368. /* Conexant Hybrid TV RDU253S Polaris */
  369. { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
  370. .driver_info = CX_HYBRID_TV },
  371. /* Twisted Melon Inc. - Manta Mini Receiver */
  372. { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
  373. /* Twisted Melon Inc. - Manta Pico Receiver */
  374. { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
  375. /* Twisted Melon Inc. - Manta Transceiver */
  376. { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
  377. /* Terminating entry */
  378. { }
  379. };
  380. /* data structure for each usb transceiver */
  381. struct mceusb_dev {
  382. /* ir-core bits */
  383. struct rc_dev *rc;
  384. /* optional features we can enable */
  385. bool carrier_report_enabled;
  386. bool learning_enabled;
  387. /* core device bits */
  388. struct device *dev;
  389. /* usb */
  390. struct usb_device *usbdev;
  391. struct urb *urb_in;
  392. struct usb_endpoint_descriptor *usb_ep_out;
  393. /* buffers and dma */
  394. unsigned char *buf_in;
  395. unsigned int len_in;
  396. dma_addr_t dma_in;
  397. enum {
  398. CMD_HEADER = 0,
  399. SUBCMD,
  400. CMD_DATA,
  401. PARSE_IRDATA,
  402. } parser_state;
  403. u8 cmd, rem; /* Remaining IR data bytes in packet */
  404. struct {
  405. u32 connected:1;
  406. u32 tx_mask_normal:1;
  407. u32 microsoft_gen1:1;
  408. u32 no_tx:1;
  409. } flags;
  410. /* transmit support */
  411. int send_flags;
  412. u32 carrier;
  413. unsigned char tx_mask;
  414. char name[128];
  415. char phys[64];
  416. enum mceusb_model_type model;
  417. bool need_reset; /* flag to issue a device resume cmd */
  418. u8 emver; /* emulator interface version */
  419. u8 num_txports; /* number of transmit ports */
  420. u8 num_rxports; /* number of receive sensors */
  421. u8 txports_cabled; /* bitmask of transmitters with cable */
  422. u8 rxports_active; /* bitmask of active receive sensors */
  423. };
  424. /* MCE Device Command Strings, generally a port and command pair */
  425. static char DEVICE_RESUME[] = {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
  426. MCE_CMD_RESUME};
  427. static char GET_REVISION[] = {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
  428. static char GET_EMVER[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
  429. static char GET_WAKEVERSION[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
  430. static char FLASH_LED[] = {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
  431. static char GET_UNKNOWN2[] = {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
  432. static char GET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
  433. static char GET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
  434. static char GET_NUM_PORTS[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
  435. static char GET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
  436. static char GET_RX_SENSOR[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
  437. /* sub in desired values in lower byte or bytes for full command */
  438. /* FIXME: make use of these for transmit.
  439. static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR,
  440. MCE_CMD_SETIRCFS, 0x00, 0x00};
  441. static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
  442. static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR,
  443. MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
  444. static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR,
  445. MCE_RSP_EQIRRXPORTEN, 0x00};
  446. */
  447. static int mceusb_cmdsize(u8 cmd, u8 subcmd)
  448. {
  449. int datasize = 0;
  450. switch (cmd) {
  451. case MCE_CMD_NULL:
  452. if (subcmd == MCE_CMD_PORT_SYS)
  453. datasize = 1;
  454. break;
  455. case MCE_CMD_PORT_SYS:
  456. switch (subcmd) {
  457. case MCE_RSP_EQWAKEVERSION:
  458. datasize = 4;
  459. break;
  460. case MCE_CMD_G_REVISION:
  461. datasize = 2;
  462. break;
  463. case MCE_RSP_EQWAKESUPPORT:
  464. datasize = 1;
  465. break;
  466. }
  467. case MCE_CMD_PORT_IR:
  468. switch (subcmd) {
  469. case MCE_CMD_UNKNOWN:
  470. case MCE_RSP_EQIRCFS:
  471. case MCE_RSP_EQIRTIMEOUT:
  472. case MCE_RSP_EQIRRXCFCNT:
  473. datasize = 2;
  474. break;
  475. case MCE_CMD_SIG_END:
  476. case MCE_RSP_EQIRTXPORTS:
  477. case MCE_RSP_EQIRRXPORTEN:
  478. datasize = 1;
  479. break;
  480. }
  481. }
  482. return datasize;
  483. }
  484. static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
  485. int offset, int len, bool out)
  486. {
  487. char codes[USB_BUFLEN * 3 + 1];
  488. char inout[9];
  489. u8 cmd, subcmd, data1, data2, data3, data4;
  490. struct device *dev = ir->dev;
  491. int i, start, skip = 0;
  492. u32 carrier, period;
  493. if (!debug)
  494. return;
  495. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  496. if (ir->flags.microsoft_gen1 && !out && !offset)
  497. skip = 2;
  498. if (len <= skip)
  499. return;
  500. for (i = 0; i < len && i < USB_BUFLEN; i++)
  501. snprintf(codes + i * 3, 4, "%02x ", buf[i + offset] & 0xff);
  502. dev_info(dev, "%sx data: %s(length=%d)\n",
  503. (out ? "t" : "r"), codes, len);
  504. if (out)
  505. strcpy(inout, "Request\0");
  506. else
  507. strcpy(inout, "Got\0");
  508. start = offset + skip;
  509. cmd = buf[start] & 0xff;
  510. subcmd = buf[start + 1] & 0xff;
  511. data1 = buf[start + 2] & 0xff;
  512. data2 = buf[start + 3] & 0xff;
  513. data3 = buf[start + 4] & 0xff;
  514. data4 = buf[start + 5] & 0xff;
  515. switch (cmd) {
  516. case MCE_CMD_NULL:
  517. if (subcmd == MCE_CMD_NULL)
  518. break;
  519. if ((subcmd == MCE_CMD_PORT_SYS) &&
  520. (data1 == MCE_CMD_RESUME))
  521. dev_info(dev, "Device resume requested\n");
  522. else
  523. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  524. cmd, subcmd);
  525. break;
  526. case MCE_CMD_PORT_SYS:
  527. switch (subcmd) {
  528. case MCE_RSP_EQEMVER:
  529. if (!out)
  530. dev_info(dev, "Emulator interface version %x\n",
  531. data1);
  532. break;
  533. case MCE_CMD_G_REVISION:
  534. if (len == 2)
  535. dev_info(dev, "Get hw/sw rev?\n");
  536. else
  537. dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
  538. "0x%02x 0x%02x\n", data1, data2,
  539. buf[start + 4], buf[start + 5]);
  540. break;
  541. case MCE_CMD_RESUME:
  542. dev_info(dev, "Device resume requested\n");
  543. break;
  544. case MCE_RSP_CMD_ILLEGAL:
  545. dev_info(dev, "Illegal PORT_SYS command\n");
  546. break;
  547. case MCE_RSP_EQWAKEVERSION:
  548. if (!out)
  549. dev_info(dev, "Wake version, proto: 0x%02x, "
  550. "payload: 0x%02x, address: 0x%02x, "
  551. "version: 0x%02x\n",
  552. data1, data2, data3, data4);
  553. break;
  554. case MCE_RSP_GETPORTSTATUS:
  555. if (!out)
  556. /* We use data1 + 1 here, to match hw labels */
  557. dev_info(dev, "TX port %d: blaster is%s connected\n",
  558. data1 + 1, data4 ? " not" : "");
  559. break;
  560. case MCE_CMD_FLASHLED:
  561. dev_info(dev, "Attempting to flash LED\n");
  562. break;
  563. default:
  564. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  565. cmd, subcmd);
  566. break;
  567. }
  568. break;
  569. case MCE_CMD_PORT_IR:
  570. switch (subcmd) {
  571. case MCE_CMD_SIG_END:
  572. dev_info(dev, "End of signal\n");
  573. break;
  574. case MCE_CMD_PING:
  575. dev_info(dev, "Ping\n");
  576. break;
  577. case MCE_CMD_UNKNOWN:
  578. dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
  579. data1, data2);
  580. break;
  581. case MCE_RSP_EQIRCFS:
  582. period = DIV_ROUND_CLOSEST(
  583. (1 << data1 * 2) * (data2 + 1), 10);
  584. if (!period)
  585. break;
  586. carrier = (1000 * 1000) / period;
  587. dev_info(dev, "%s carrier of %u Hz (period %uus)\n",
  588. inout, carrier, period);
  589. break;
  590. case MCE_CMD_GETIRCFS:
  591. dev_info(dev, "Get carrier mode and freq\n");
  592. break;
  593. case MCE_RSP_EQIRTXPORTS:
  594. dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
  595. inout, data1);
  596. break;
  597. case MCE_RSP_EQIRTIMEOUT:
  598. /* value is in units of 50us, so x*50/1000 ms */
  599. period = ((data1 << 8) | data2) * MCE_TIME_UNIT / 1000;
  600. dev_info(dev, "%s receive timeout of %d ms\n",
  601. inout, period);
  602. break;
  603. case MCE_CMD_GETIRTIMEOUT:
  604. dev_info(dev, "Get receive timeout\n");
  605. break;
  606. case MCE_CMD_GETIRTXPORTS:
  607. dev_info(dev, "Get transmit blaster mask\n");
  608. break;
  609. case MCE_RSP_EQIRRXPORTEN:
  610. dev_info(dev, "%s %s-range receive sensor in use\n",
  611. inout, data1 == 0x02 ? "short" : "long");
  612. break;
  613. case MCE_CMD_GETIRRXPORTEN:
  614. /* aka MCE_RSP_EQIRRXCFCNT */
  615. if (out)
  616. dev_info(dev, "Get receive sensor\n");
  617. else if (ir->learning_enabled)
  618. dev_info(dev, "RX pulse count: %d\n",
  619. ((data1 << 8) | data2));
  620. break;
  621. case MCE_RSP_EQIRNUMPORTS:
  622. if (out)
  623. break;
  624. dev_info(dev, "Num TX ports: %x, num RX ports: %x\n",
  625. data1, data2);
  626. break;
  627. case MCE_RSP_CMD_ILLEGAL:
  628. dev_info(dev, "Illegal PORT_IR command\n");
  629. break;
  630. default:
  631. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  632. cmd, subcmd);
  633. break;
  634. }
  635. break;
  636. default:
  637. break;
  638. }
  639. if (cmd == MCE_IRDATA_TRAILER)
  640. dev_info(dev, "End of raw IR data\n");
  641. else if ((cmd != MCE_CMD_PORT_IR) &&
  642. ((cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA))
  643. dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
  644. }
  645. static void mce_async_callback(struct urb *urb)
  646. {
  647. struct mceusb_dev *ir;
  648. int len;
  649. if (!urb)
  650. return;
  651. ir = urb->context;
  652. if (ir) {
  653. len = urb->actual_length;
  654. mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
  655. }
  656. /* the transfer buffer and urb were allocated in mce_request_packet */
  657. kfree(urb->transfer_buffer);
  658. usb_free_urb(urb);
  659. }
  660. /* request incoming or send outgoing usb packet - used to initialize remote */
  661. static void mce_request_packet(struct mceusb_dev *ir, unsigned char *data,
  662. int size, int urb_type)
  663. {
  664. int res, pipe;
  665. struct urb *async_urb;
  666. struct device *dev = ir->dev;
  667. unsigned char *async_buf;
  668. if (urb_type == MCEUSB_TX) {
  669. async_urb = usb_alloc_urb(0, GFP_KERNEL);
  670. if (unlikely(!async_urb)) {
  671. dev_err(dev, "Error, couldn't allocate urb!\n");
  672. return;
  673. }
  674. async_buf = kzalloc(size, GFP_KERNEL);
  675. if (!async_buf) {
  676. dev_err(dev, "Error, couldn't allocate buf!\n");
  677. usb_free_urb(async_urb);
  678. return;
  679. }
  680. /* outbound data */
  681. pipe = usb_sndintpipe(ir->usbdev,
  682. ir->usb_ep_out->bEndpointAddress);
  683. usb_fill_int_urb(async_urb, ir->usbdev, pipe,
  684. async_buf, size, mce_async_callback,
  685. ir, ir->usb_ep_out->bInterval);
  686. memcpy(async_buf, data, size);
  687. } else if (urb_type == MCEUSB_RX) {
  688. /* standard request */
  689. async_urb = ir->urb_in;
  690. ir->send_flags = RECV_FLAG_IN_PROGRESS;
  691. } else {
  692. dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
  693. return;
  694. }
  695. mce_dbg(dev, "receive request called (size=%#x)\n", size);
  696. async_urb->transfer_buffer_length = size;
  697. async_urb->dev = ir->usbdev;
  698. res = usb_submit_urb(async_urb, GFP_ATOMIC);
  699. if (res) {
  700. mce_dbg(dev, "receive request FAILED! (res=%d)\n", res);
  701. return;
  702. }
  703. mce_dbg(dev, "receive request complete (res=%d)\n", res);
  704. }
  705. static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
  706. {
  707. int rsize = sizeof(DEVICE_RESUME);
  708. if (ir->need_reset) {
  709. ir->need_reset = false;
  710. mce_request_packet(ir, DEVICE_RESUME, rsize, MCEUSB_TX);
  711. msleep(10);
  712. }
  713. mce_request_packet(ir, data, size, MCEUSB_TX);
  714. msleep(10);
  715. }
  716. static void mce_flush_rx_buffer(struct mceusb_dev *ir, int size)
  717. {
  718. mce_request_packet(ir, NULL, size, MCEUSB_RX);
  719. }
  720. /* Send data out the IR blaster port(s) */
  721. static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
  722. {
  723. struct mceusb_dev *ir = dev->priv;
  724. int i, ret = 0;
  725. int cmdcount = 0;
  726. unsigned char *cmdbuf; /* MCE command buffer */
  727. long signal_duration = 0; /* Singnal length in us */
  728. struct timeval start_time, end_time;
  729. do_gettimeofday(&start_time);
  730. cmdbuf = kzalloc(sizeof(unsigned) * MCE_CMDBUF_SIZE, GFP_KERNEL);
  731. if (!cmdbuf)
  732. return -ENOMEM;
  733. /* MCE tx init header */
  734. cmdbuf[cmdcount++] = MCE_CMD_PORT_IR;
  735. cmdbuf[cmdcount++] = MCE_CMD_SETIRTXPORTS;
  736. cmdbuf[cmdcount++] = ir->tx_mask;
  737. /* Generate mce packet data */
  738. for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
  739. signal_duration += txbuf[i];
  740. txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
  741. do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
  742. /* Insert mce packet header every 4th entry */
  743. if ((cmdcount < MCE_CMDBUF_SIZE) &&
  744. (cmdcount - MCE_TX_HEADER_LENGTH) %
  745. MCE_CODE_LENGTH == 0)
  746. cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
  747. /* Insert mce packet data */
  748. if (cmdcount < MCE_CMDBUF_SIZE)
  749. cmdbuf[cmdcount++] =
  750. (txbuf[i] < MCE_PULSE_BIT ?
  751. txbuf[i] : MCE_MAX_PULSE_LENGTH) |
  752. (i & 1 ? 0x00 : MCE_PULSE_BIT);
  753. else {
  754. ret = -EINVAL;
  755. goto out;
  756. }
  757. } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
  758. (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
  759. }
  760. /* Fix packet length in last header */
  761. cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
  762. MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
  763. MCE_CODE_LENGTH - 1;
  764. /* Check if we have room for the empty packet at the end */
  765. if (cmdcount >= MCE_CMDBUF_SIZE) {
  766. ret = -EINVAL;
  767. goto out;
  768. }
  769. /* All mce commands end with an empty packet (0x80) */
  770. cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
  771. /* Transmit the command to the mce device */
  772. mce_async_out(ir, cmdbuf, cmdcount);
  773. /*
  774. * The lircd gap calculation expects the write function to
  775. * wait the time it takes for the ircommand to be sent before
  776. * it returns.
  777. */
  778. do_gettimeofday(&end_time);
  779. signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
  780. (end_time.tv_sec - start_time.tv_sec) * 1000000;
  781. /* delay with the closest number of ticks */
  782. set_current_state(TASK_INTERRUPTIBLE);
  783. schedule_timeout(usecs_to_jiffies(signal_duration));
  784. out:
  785. kfree(cmdbuf);
  786. return ret ? ret : count;
  787. }
  788. /* Sets active IR outputs -- mce devices typically have two */
  789. static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
  790. {
  791. struct mceusb_dev *ir = dev->priv;
  792. if (ir->flags.tx_mask_normal)
  793. ir->tx_mask = mask;
  794. else
  795. ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
  796. mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
  797. return 0;
  798. }
  799. /* Sets the send carrier frequency and mode */
  800. static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  801. {
  802. struct mceusb_dev *ir = dev->priv;
  803. int clk = 10000000;
  804. int prescaler = 0, divisor = 0;
  805. unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
  806. MCE_CMD_SETIRCFS, 0x00, 0x00 };
  807. /* Carrier has changed */
  808. if (ir->carrier != carrier) {
  809. if (carrier == 0) {
  810. ir->carrier = carrier;
  811. cmdbuf[2] = MCE_CMD_SIG_END;
  812. cmdbuf[3] = MCE_IRDATA_TRAILER;
  813. mce_dbg(ir->dev, "%s: disabling carrier "
  814. "modulation\n", __func__);
  815. mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
  816. return carrier;
  817. }
  818. for (prescaler = 0; prescaler < 4; ++prescaler) {
  819. divisor = (clk >> (2 * prescaler)) / carrier;
  820. if (divisor <= 0xff) {
  821. ir->carrier = carrier;
  822. cmdbuf[2] = prescaler;
  823. cmdbuf[3] = divisor;
  824. mce_dbg(ir->dev, "%s: requesting %u HZ "
  825. "carrier\n", __func__, carrier);
  826. /* Transmit new carrier to mce device */
  827. mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
  828. return carrier;
  829. }
  830. }
  831. return -EINVAL;
  832. }
  833. return carrier;
  834. }
  835. /*
  836. * We don't do anything but print debug spew for many of the command bits
  837. * we receive from the hardware, but some of them are useful information
  838. * we want to store so that we can use them.
  839. */
  840. static void mceusb_handle_command(struct mceusb_dev *ir, int index)
  841. {
  842. u8 hi = ir->buf_in[index + 1] & 0xff;
  843. u8 lo = ir->buf_in[index + 2] & 0xff;
  844. switch (ir->buf_in[index]) {
  845. /* the one and only 5-byte return value command */
  846. case MCE_RSP_GETPORTSTATUS:
  847. if ((ir->buf_in[index + 4] & 0xff) == 0x00)
  848. ir->txports_cabled |= 1 << hi;
  849. break;
  850. /* 2-byte return value commands */
  851. case MCE_RSP_EQIRTIMEOUT:
  852. ir->rc->timeout = US_TO_NS((hi << 8 | lo) * MCE_TIME_UNIT);
  853. break;
  854. case MCE_RSP_EQIRNUMPORTS:
  855. ir->num_txports = hi;
  856. ir->num_rxports = lo;
  857. break;
  858. /* 1-byte return value commands */
  859. case MCE_RSP_EQEMVER:
  860. ir->emver = hi;
  861. break;
  862. case MCE_RSP_EQIRTXPORTS:
  863. ir->tx_mask = hi;
  864. break;
  865. case MCE_RSP_EQIRRXPORTEN:
  866. ir->learning_enabled = ((hi & 0x02) == 0x02);
  867. ir->rxports_active = hi;
  868. break;
  869. case MCE_RSP_CMD_ILLEGAL:
  870. ir->need_reset = true;
  871. break;
  872. default:
  873. break;
  874. }
  875. }
  876. static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
  877. {
  878. DEFINE_IR_RAW_EVENT(rawir);
  879. int i = 0;
  880. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  881. if (ir->flags.microsoft_gen1)
  882. i = 2;
  883. /* if there's no data, just return now */
  884. if (buf_len <= i)
  885. return;
  886. for (; i < buf_len; i++) {
  887. switch (ir->parser_state) {
  888. case SUBCMD:
  889. ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
  890. mceusb_dev_printdata(ir, ir->buf_in, i - 1,
  891. ir->rem + 2, false);
  892. mceusb_handle_command(ir, i);
  893. ir->parser_state = CMD_DATA;
  894. break;
  895. case PARSE_IRDATA:
  896. ir->rem--;
  897. init_ir_raw_event(&rawir);
  898. rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
  899. rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
  900. * US_TO_NS(MCE_TIME_UNIT);
  901. mce_dbg(ir->dev, "Storing %s with duration %d\n",
  902. rawir.pulse ? "pulse" : "space",
  903. rawir.duration);
  904. ir_raw_event_store_with_filter(ir->rc, &rawir);
  905. break;
  906. case CMD_DATA:
  907. ir->rem--;
  908. break;
  909. case CMD_HEADER:
  910. /* decode mce packets of the form (84),AA,BB,CC,DD */
  911. /* IR data packets can span USB messages - rem */
  912. ir->cmd = ir->buf_in[i];
  913. if ((ir->cmd == MCE_CMD_PORT_IR) ||
  914. ((ir->cmd & MCE_PORT_MASK) !=
  915. MCE_COMMAND_IRDATA)) {
  916. ir->parser_state = SUBCMD;
  917. continue;
  918. }
  919. ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
  920. mceusb_dev_printdata(ir, ir->buf_in,
  921. i, ir->rem + 1, false);
  922. if (ir->rem)
  923. ir->parser_state = PARSE_IRDATA;
  924. else
  925. ir_raw_event_reset(ir->rc);
  926. break;
  927. }
  928. if (ir->parser_state != CMD_HEADER && !ir->rem)
  929. ir->parser_state = CMD_HEADER;
  930. }
  931. mce_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
  932. ir_raw_event_handle(ir->rc);
  933. }
  934. static void mceusb_dev_recv(struct urb *urb)
  935. {
  936. struct mceusb_dev *ir;
  937. int buf_len;
  938. if (!urb)
  939. return;
  940. ir = urb->context;
  941. if (!ir) {
  942. usb_unlink_urb(urb);
  943. return;
  944. }
  945. buf_len = urb->actual_length;
  946. if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
  947. ir->send_flags = SEND_FLAG_COMPLETE;
  948. mce_dbg(ir->dev, "setup answer received %d bytes\n",
  949. buf_len);
  950. }
  951. switch (urb->status) {
  952. /* success */
  953. case 0:
  954. mceusb_process_ir_data(ir, buf_len);
  955. break;
  956. case -ECONNRESET:
  957. case -ENOENT:
  958. case -ESHUTDOWN:
  959. usb_unlink_urb(urb);
  960. return;
  961. case -EPIPE:
  962. default:
  963. mce_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
  964. break;
  965. }
  966. usb_submit_urb(urb, GFP_ATOMIC);
  967. }
  968. static void mceusb_get_emulator_version(struct mceusb_dev *ir)
  969. {
  970. /* If we get no reply or an illegal command reply, its ver 1, says MS */
  971. ir->emver = 1;
  972. mce_async_out(ir, GET_EMVER, sizeof(GET_EMVER));
  973. }
  974. static void mceusb_gen1_init(struct mceusb_dev *ir)
  975. {
  976. int ret;
  977. struct device *dev = ir->dev;
  978. char *data;
  979. data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
  980. if (!data) {
  981. dev_err(dev, "%s: memory allocation failed!\n", __func__);
  982. return;
  983. }
  984. /*
  985. * This is a strange one. Windows issues a set address to the device
  986. * on the receive control pipe and expect a certain value pair back
  987. */
  988. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  989. USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
  990. data, USB_CTRL_MSG_SZ, HZ * 3);
  991. mce_dbg(dev, "%s - ret = %d\n", __func__, ret);
  992. mce_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
  993. __func__, data[0], data[1]);
  994. /* set feature: bit rate 38400 bps */
  995. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  996. USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
  997. 0xc04e, 0x0000, NULL, 0, HZ * 3);
  998. mce_dbg(dev, "%s - ret = %d\n", __func__, ret);
  999. /* bRequest 4: set char length to 8 bits */
  1000. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  1001. 4, USB_TYPE_VENDOR,
  1002. 0x0808, 0x0000, NULL, 0, HZ * 3);
  1003. mce_dbg(dev, "%s - retB = %d\n", __func__, ret);
  1004. /* bRequest 2: set handshaking to use DTR/DSR */
  1005. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  1006. 2, USB_TYPE_VENDOR,
  1007. 0x0000, 0x0100, NULL, 0, HZ * 3);
  1008. mce_dbg(dev, "%s - retC = %d\n", __func__, ret);
  1009. /* device resume */
  1010. mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
  1011. /* get hw/sw revision? */
  1012. mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
  1013. kfree(data);
  1014. };
  1015. static void mceusb_gen2_init(struct mceusb_dev *ir)
  1016. {
  1017. /* device resume */
  1018. mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
  1019. /* get hw/sw revision? */
  1020. mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
  1021. /* get wake version (protocol, key, address) */
  1022. mce_async_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
  1023. /* unknown what this one actually returns... */
  1024. mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
  1025. }
  1026. static void mceusb_get_parameters(struct mceusb_dev *ir)
  1027. {
  1028. int i;
  1029. unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
  1030. MCE_CMD_GETPORTSTATUS, 0x00 };
  1031. /* defaults, if the hardware doesn't support querying */
  1032. ir->num_txports = 2;
  1033. ir->num_rxports = 2;
  1034. /* get number of tx and rx ports */
  1035. mce_async_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
  1036. /* get the carrier and frequency */
  1037. mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
  1038. if (ir->num_txports && !ir->flags.no_tx)
  1039. /* get the transmitter bitmask */
  1040. mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
  1041. /* get receiver timeout value */
  1042. mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
  1043. /* get receiver sensor setting */
  1044. mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
  1045. for (i = 0; i < ir->num_txports; i++) {
  1046. cmdbuf[2] = i;
  1047. mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
  1048. }
  1049. }
  1050. static void mceusb_flash_led(struct mceusb_dev *ir)
  1051. {
  1052. if (ir->emver < 2)
  1053. return;
  1054. mce_async_out(ir, FLASH_LED, sizeof(FLASH_LED));
  1055. }
  1056. static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
  1057. {
  1058. struct device *dev = ir->dev;
  1059. struct rc_dev *rc;
  1060. int ret;
  1061. rc = rc_allocate_device();
  1062. if (!rc) {
  1063. dev_err(dev, "remote dev allocation failed\n");
  1064. goto out;
  1065. }
  1066. snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
  1067. mceusb_model[ir->model].name ?
  1068. mceusb_model[ir->model].name :
  1069. "Media Center Ed. eHome Infrared Remote Transceiver",
  1070. le16_to_cpu(ir->usbdev->descriptor.idVendor),
  1071. le16_to_cpu(ir->usbdev->descriptor.idProduct));
  1072. usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
  1073. rc->input_name = ir->name;
  1074. rc->input_phys = ir->phys;
  1075. usb_to_input_id(ir->usbdev, &rc->input_id);
  1076. rc->dev.parent = dev;
  1077. rc->priv = ir;
  1078. rc->driver_type = RC_DRIVER_IR_RAW;
  1079. rc->allowed_protos = RC_TYPE_ALL;
  1080. rc->timeout = MS_TO_NS(100);
  1081. if (!ir->flags.no_tx) {
  1082. rc->s_tx_mask = mceusb_set_tx_mask;
  1083. rc->s_tx_carrier = mceusb_set_tx_carrier;
  1084. rc->tx_ir = mceusb_tx_ir;
  1085. }
  1086. rc->driver_name = DRIVER_NAME;
  1087. rc->map_name = mceusb_model[ir->model].rc_map ?
  1088. mceusb_model[ir->model].rc_map : RC_MAP_RC6_MCE;
  1089. ret = rc_register_device(rc);
  1090. if (ret < 0) {
  1091. dev_err(dev, "remote dev registration failed\n");
  1092. goto out;
  1093. }
  1094. return rc;
  1095. out:
  1096. rc_free_device(rc);
  1097. return NULL;
  1098. }
  1099. static int __devinit mceusb_dev_probe(struct usb_interface *intf,
  1100. const struct usb_device_id *id)
  1101. {
  1102. struct usb_device *dev = interface_to_usbdev(intf);
  1103. struct usb_host_interface *idesc;
  1104. struct usb_endpoint_descriptor *ep = NULL;
  1105. struct usb_endpoint_descriptor *ep_in = NULL;
  1106. struct usb_endpoint_descriptor *ep_out = NULL;
  1107. struct mceusb_dev *ir = NULL;
  1108. int pipe, maxp, i;
  1109. char buf[63], name[128] = "";
  1110. enum mceusb_model_type model = id->driver_info;
  1111. bool is_gen3;
  1112. bool is_microsoft_gen1;
  1113. bool tx_mask_normal;
  1114. int ir_intfnum;
  1115. mce_dbg(&intf->dev, "%s called\n", __func__);
  1116. idesc = intf->cur_altsetting;
  1117. is_gen3 = mceusb_model[model].mce_gen3;
  1118. is_microsoft_gen1 = mceusb_model[model].mce_gen1;
  1119. tx_mask_normal = mceusb_model[model].tx_mask_normal;
  1120. ir_intfnum = mceusb_model[model].ir_intfnum;
  1121. /* There are multi-function devices with non-IR interfaces */
  1122. if (idesc->desc.bInterfaceNumber != ir_intfnum)
  1123. return -ENODEV;
  1124. /* step through the endpoints to find first bulk in and out endpoint */
  1125. for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
  1126. ep = &idesc->endpoint[i].desc;
  1127. if ((ep_in == NULL)
  1128. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1129. == USB_DIR_IN)
  1130. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1131. == USB_ENDPOINT_XFER_BULK)
  1132. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1133. == USB_ENDPOINT_XFER_INT))) {
  1134. ep_in = ep;
  1135. ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
  1136. ep_in->bInterval = 1;
  1137. mce_dbg(&intf->dev, "acceptable inbound endpoint "
  1138. "found\n");
  1139. }
  1140. if ((ep_out == NULL)
  1141. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1142. == USB_DIR_OUT)
  1143. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1144. == USB_ENDPOINT_XFER_BULK)
  1145. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1146. == USB_ENDPOINT_XFER_INT))) {
  1147. ep_out = ep;
  1148. ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
  1149. ep_out->bInterval = 1;
  1150. mce_dbg(&intf->dev, "acceptable outbound endpoint "
  1151. "found\n");
  1152. }
  1153. }
  1154. if (ep_in == NULL) {
  1155. mce_dbg(&intf->dev, "inbound and/or endpoint not found\n");
  1156. return -ENODEV;
  1157. }
  1158. pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
  1159. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  1160. ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
  1161. if (!ir)
  1162. goto mem_alloc_fail;
  1163. ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
  1164. if (!ir->buf_in)
  1165. goto buf_in_alloc_fail;
  1166. ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  1167. if (!ir->urb_in)
  1168. goto urb_in_alloc_fail;
  1169. ir->usbdev = dev;
  1170. ir->dev = &intf->dev;
  1171. ir->len_in = maxp;
  1172. ir->flags.microsoft_gen1 = is_microsoft_gen1;
  1173. ir->flags.tx_mask_normal = tx_mask_normal;
  1174. ir->flags.no_tx = mceusb_model[model].no_tx;
  1175. ir->model = model;
  1176. /* Saving usb interface data for use by the transmitter routine */
  1177. ir->usb_ep_out = ep_out;
  1178. if (dev->descriptor.iManufacturer
  1179. && usb_string(dev, dev->descriptor.iManufacturer,
  1180. buf, sizeof(buf)) > 0)
  1181. strlcpy(name, buf, sizeof(name));
  1182. if (dev->descriptor.iProduct
  1183. && usb_string(dev, dev->descriptor.iProduct,
  1184. buf, sizeof(buf)) > 0)
  1185. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  1186. " %s", buf);
  1187. ir->rc = mceusb_init_rc_dev(ir);
  1188. if (!ir->rc)
  1189. goto rc_dev_fail;
  1190. /* wire up inbound data handler */
  1191. usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
  1192. mceusb_dev_recv, ir, ep_in->bInterval);
  1193. ir->urb_in->transfer_dma = ir->dma_in;
  1194. ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1195. /* flush buffers on the device */
  1196. mce_dbg(&intf->dev, "Flushing receive buffers\n");
  1197. mce_flush_rx_buffer(ir, maxp);
  1198. /* figure out which firmware/emulator version this hardware has */
  1199. mceusb_get_emulator_version(ir);
  1200. /* initialize device */
  1201. if (ir->flags.microsoft_gen1)
  1202. mceusb_gen1_init(ir);
  1203. else if (!is_gen3)
  1204. mceusb_gen2_init(ir);
  1205. mceusb_get_parameters(ir);
  1206. mceusb_flash_led(ir);
  1207. if (!ir->flags.no_tx)
  1208. mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
  1209. usb_set_intfdata(intf, ir);
  1210. /* enable wake via this device */
  1211. device_set_wakeup_capable(ir->dev, true);
  1212. device_set_wakeup_enable(ir->dev, true);
  1213. dev_info(&intf->dev, "Registered %s with mce emulator interface "
  1214. "version %x\n", name, ir->emver);
  1215. dev_info(&intf->dev, "%x tx ports (0x%x cabled) and "
  1216. "%x rx sensors (0x%x active)\n",
  1217. ir->num_txports, ir->txports_cabled,
  1218. ir->num_rxports, ir->rxports_active);
  1219. return 0;
  1220. /* Error-handling path */
  1221. rc_dev_fail:
  1222. usb_free_urb(ir->urb_in);
  1223. urb_in_alloc_fail:
  1224. usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
  1225. buf_in_alloc_fail:
  1226. kfree(ir);
  1227. mem_alloc_fail:
  1228. dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
  1229. return -ENOMEM;
  1230. }
  1231. static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
  1232. {
  1233. struct usb_device *dev = interface_to_usbdev(intf);
  1234. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1235. usb_set_intfdata(intf, NULL);
  1236. if (!ir)
  1237. return;
  1238. ir->usbdev = NULL;
  1239. rc_unregister_device(ir->rc);
  1240. usb_kill_urb(ir->urb_in);
  1241. usb_free_urb(ir->urb_in);
  1242. usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
  1243. kfree(ir);
  1244. }
  1245. static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
  1246. {
  1247. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1248. dev_info(ir->dev, "suspend\n");
  1249. usb_kill_urb(ir->urb_in);
  1250. return 0;
  1251. }
  1252. static int mceusb_dev_resume(struct usb_interface *intf)
  1253. {
  1254. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1255. dev_info(ir->dev, "resume\n");
  1256. if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
  1257. return -EIO;
  1258. return 0;
  1259. }
  1260. static struct usb_driver mceusb_dev_driver = {
  1261. .name = DRIVER_NAME,
  1262. .probe = mceusb_dev_probe,
  1263. .disconnect = __devexit_p(mceusb_dev_disconnect),
  1264. .suspend = mceusb_dev_suspend,
  1265. .resume = mceusb_dev_resume,
  1266. .reset_resume = mceusb_dev_resume,
  1267. .id_table = mceusb_dev_table
  1268. };
  1269. module_usb_driver(mceusb_dev_driver);
  1270. MODULE_DESCRIPTION(DRIVER_DESC);
  1271. MODULE_AUTHOR(DRIVER_AUTHOR);
  1272. MODULE_LICENSE("GPL");
  1273. MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
  1274. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1275. MODULE_PARM_DESC(debug, "Debug enabled or not");