mceusb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
  3. *
  4. * Copyright (c) 2010 by 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. * TODO:
  19. * - add rc-core transmit support, once available
  20. * - enable support for forthcoming ir-lirc-codec interface
  21. *
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  36. *
  37. */
  38. #include <linux/device.h>
  39. #include <linux/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/usb.h>
  42. #include <linux/input.h>
  43. #include <media/ir-core.h>
  44. #include <media/ir-common.h>
  45. #define DRIVER_VERSION "1.91"
  46. #define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
  47. #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
  48. "device driver"
  49. #define DRIVER_NAME "mceusb"
  50. #define USB_BUFLEN 32 /* USB reception buffer length */
  51. #define IRBUF_SIZE 256 /* IR work 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_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
  60. #define MCE_CONTROL_HEADER 0x9F /* MCE status header */
  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 /* Val opts: 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. #define MCE_PACKET_LENGTH_MASK 0x1F /* Packet length mask */
  68. /* module parameters */
  69. #ifdef CONFIG_USB_DEBUG
  70. static int debug = 1;
  71. #else
  72. static int debug;
  73. #endif
  74. /* general constants */
  75. #define SEND_FLAG_IN_PROGRESS 1
  76. #define SEND_FLAG_COMPLETE 2
  77. #define RECV_FLAG_IN_PROGRESS 3
  78. #define RECV_FLAG_COMPLETE 4
  79. #define MCEUSB_RX 1
  80. #define MCEUSB_TX 2
  81. #define VENDOR_PHILIPS 0x0471
  82. #define VENDOR_SMK 0x0609
  83. #define VENDOR_TATUNG 0x1460
  84. #define VENDOR_GATEWAY 0x107b
  85. #define VENDOR_SHUTTLE 0x1308
  86. #define VENDOR_SHUTTLE2 0x051c
  87. #define VENDOR_MITSUMI 0x03ee
  88. #define VENDOR_TOPSEED 0x1784
  89. #define VENDOR_RICAVISION 0x179d
  90. #define VENDOR_ITRON 0x195d
  91. #define VENDOR_FIC 0x1509
  92. #define VENDOR_LG 0x043e
  93. #define VENDOR_MICROSOFT 0x045e
  94. #define VENDOR_FORMOSA 0x147a
  95. #define VENDOR_FINTEK 0x1934
  96. #define VENDOR_PINNACLE 0x2304
  97. #define VENDOR_ECS 0x1019
  98. #define VENDOR_WISTRON 0x0fb8
  99. #define VENDOR_COMPRO 0x185b
  100. #define VENDOR_NORTHSTAR 0x04eb
  101. #define VENDOR_REALTEK 0x0bda
  102. #define VENDOR_TIVO 0x105a
  103. static struct usb_device_id mceusb_dev_table[] = {
  104. /* Original Microsoft MCE IR Transceiver (often HP-branded) */
  105. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  106. /* Philips Infrared Transceiver - Sahara branded */
  107. { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
  108. /* Philips Infrared Transceiver - HP branded */
  109. { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
  110. /* Philips SRM5100 */
  111. { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
  112. /* Philips Infrared Transceiver - Omaura */
  113. { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
  114. /* Philips Infrared Transceiver - Spinel plus */
  115. { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
  116. /* Philips eHome Infrared Transceiver */
  117. { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
  118. /* Realtek MCE IR Receiver */
  119. { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
  120. /* SMK/Toshiba G83C0004D410 */
  121. { USB_DEVICE(VENDOR_SMK, 0x031d) },
  122. /* SMK eHome Infrared Transceiver (Sony VAIO) */
  123. { USB_DEVICE(VENDOR_SMK, 0x0322) },
  124. /* bundled with Hauppauge PVR-150 */
  125. { USB_DEVICE(VENDOR_SMK, 0x0334) },
  126. /* SMK eHome Infrared Transceiver */
  127. { USB_DEVICE(VENDOR_SMK, 0x0338) },
  128. /* Tatung eHome Infrared Transceiver */
  129. { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
  130. /* Shuttle eHome Infrared Transceiver */
  131. { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
  132. /* Shuttle eHome Infrared Transceiver */
  133. { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
  134. /* Gateway eHome Infrared Transceiver */
  135. { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
  136. /* Mitsumi */
  137. { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
  138. /* Topseed eHome Infrared Transceiver */
  139. { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
  140. /* Topseed HP eHome Infrared Transceiver */
  141. { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
  142. /* Topseed eHome Infrared Transceiver */
  143. { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
  144. /* Topseed eHome Infrared Transceiver */
  145. { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
  146. /* Topseed eHome Infrared Transceiver */
  147. { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
  148. /* Topseed eHome Infrared Transceiver */
  149. { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
  150. /* Ricavision internal Infrared Transceiver */
  151. { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
  152. /* Itron ione Libra Q-11 */
  153. { USB_DEVICE(VENDOR_ITRON, 0x7002) },
  154. /* FIC eHome Infrared Transceiver */
  155. { USB_DEVICE(VENDOR_FIC, 0x9242) },
  156. /* LG eHome Infrared Transceiver */
  157. { USB_DEVICE(VENDOR_LG, 0x9803) },
  158. /* Microsoft MCE Infrared Transceiver */
  159. { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
  160. /* Formosa eHome Infrared Transceiver */
  161. { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
  162. /* Formosa21 / eHome Infrared Receiver */
  163. { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
  164. /* Formosa aim / Trust MCE Infrared Receiver */
  165. { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
  166. /* Formosa Industrial Computing / Beanbag Emulation Device */
  167. { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
  168. /* Formosa21 / eHome Infrared Receiver */
  169. { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
  170. /* Formosa Industrial Computing AIM IR605/A */
  171. { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
  172. /* Formosa Industrial Computing */
  173. { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
  174. /* Fintek eHome Infrared Transceiver */
  175. { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
  176. /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
  177. { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
  178. /* Pinnacle Remote Kit */
  179. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  180. /* Elitegroup Computer Systems IR */
  181. { USB_DEVICE(VENDOR_ECS, 0x0f38) },
  182. /* Wistron Corp. eHome Infrared Receiver */
  183. { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
  184. /* Compro K100 */
  185. { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
  186. /* Compro K100 v2 */
  187. { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
  188. /* Northstar Systems, Inc. eHome Infrared Transceiver */
  189. { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
  190. /* TiVo PC IR Receiver */
  191. { USB_DEVICE(VENDOR_TIVO, 0x2000) },
  192. /* Terminating entry */
  193. { }
  194. };
  195. static struct usb_device_id gen3_list[] = {
  196. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  197. { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
  198. {}
  199. };
  200. static struct usb_device_id microsoft_gen1_list[] = {
  201. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  202. {}
  203. };
  204. static struct usb_device_id std_tx_mask_list[] = {
  205. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  206. { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
  207. { USB_DEVICE(VENDOR_SMK, 0x031d) },
  208. { USB_DEVICE(VENDOR_SMK, 0x0322) },
  209. { USB_DEVICE(VENDOR_SMK, 0x0334) },
  210. { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
  211. { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
  212. { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
  213. { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
  214. { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
  215. { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
  216. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  217. {}
  218. };
  219. /* data structure for each usb transceiver */
  220. struct mceusb_dev {
  221. /* ir-core bits */
  222. struct ir_input_dev *irdev;
  223. struct ir_dev_props *props;
  224. struct ir_raw_event rawir;
  225. /* core device bits */
  226. struct device *dev;
  227. struct input_dev *idev;
  228. /* usb */
  229. struct usb_device *usbdev;
  230. struct urb *urb_in;
  231. struct usb_endpoint_descriptor *usb_ep_in;
  232. struct usb_endpoint_descriptor *usb_ep_out;
  233. /* buffers and dma */
  234. unsigned char *buf_in;
  235. unsigned int len_in;
  236. u8 cmd; /* MCE command type */
  237. u8 rem; /* Remaining IR data bytes in packet */
  238. dma_addr_t dma_in;
  239. dma_addr_t dma_out;
  240. struct {
  241. u32 connected:1;
  242. u32 tx_mask_inverted:1;
  243. u32 microsoft_gen1:1;
  244. u32 gen3:1;
  245. u32 reserved:28;
  246. } flags;
  247. /* handle sending (init strings) */
  248. int send_flags;
  249. int carrier;
  250. char name[128];
  251. char phys[64];
  252. unsigned char tx_mask;
  253. };
  254. /*
  255. * MCE Device Command Strings
  256. * Device command responses vary from device to device...
  257. * - DEVICE_RESET resets the hardware to its default state
  258. * - GET_REVISION fetches the hardware/software revision, common
  259. * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
  260. * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
  261. * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
  262. * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
  263. * ((clk / frequency) - 1)
  264. * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
  265. * response in the form of 9f 0c msb lsb
  266. * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
  267. * the form of 9f 08 bm, where bm is the bitmask
  268. * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
  269. * general use one or short-range learning one, in the form of
  270. * 9f 14 ss, where ss is either 01 for long-range or 02 for short
  271. * - SET_CARRIER_FREQ sets a new carrier mode and frequency
  272. * - SET_TX_BITMASK sets the transmitter bitmask
  273. * - SET_RX_TIMEOUT sets the receiver timeout
  274. * - SET_RX_SENSOR sets which receiver sensor to use
  275. */
  276. static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
  277. static char GET_REVISION[] = {0xff, 0x0b};
  278. static char GET_UNKNOWN[] = {0xff, 0x18};
  279. static char GET_CARRIER_FREQ[] = {0x9f, 0x07};
  280. static char GET_RX_TIMEOUT[] = {0x9f, 0x0d};
  281. static char GET_TX_BITMASK[] = {0x9f, 0x13};
  282. static char GET_RX_SENSOR[] = {0x9f, 0x15};
  283. /* sub in desired values in lower byte or bytes for full command */
  284. /* FIXME: make use of these for transmit.
  285. static char SET_CARRIER_FREQ[] = {0x9f, 0x06, 0x00, 0x00};
  286. static char SET_TX_BITMASK[] = {0x9f, 0x08, 0x00};
  287. static char SET_RX_TIMEOUT[] = {0x9f, 0x0c, 0x00, 0x00};
  288. static char SET_RX_SENSOR[] = {0x9f, 0x14, 0x00};
  289. */
  290. static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
  291. int len, bool out)
  292. {
  293. char codes[USB_BUFLEN * 3 + 1];
  294. char inout[9];
  295. int i;
  296. u8 cmd, subcmd, data1, data2;
  297. struct device *dev = ir->dev;
  298. int idx = 0;
  299. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  300. if (ir->flags.microsoft_gen1 && !out)
  301. idx = 2;
  302. if (len <= idx)
  303. return;
  304. for (i = 0; i < len && i < USB_BUFLEN; i++)
  305. snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
  306. dev_info(dev, "%sx data: %s (length=%d)\n",
  307. (out ? "t" : "r"), codes, len);
  308. if (out)
  309. strcpy(inout, "Request\0");
  310. else
  311. strcpy(inout, "Got\0");
  312. cmd = buf[idx] & 0xff;
  313. subcmd = buf[idx + 1] & 0xff;
  314. data1 = buf[idx + 2] & 0xff;
  315. data2 = buf[idx + 3] & 0xff;
  316. switch (cmd) {
  317. case 0x00:
  318. if (subcmd == 0xff && data1 == 0xaa)
  319. dev_info(dev, "Device reset requested\n");
  320. else
  321. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  322. cmd, subcmd);
  323. break;
  324. case 0xff:
  325. switch (subcmd) {
  326. case 0x0b:
  327. if (len == 2)
  328. dev_info(dev, "Get hw/sw rev?\n");
  329. else
  330. dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
  331. "0x%02x 0x%02x\n", data1, data2,
  332. buf[idx + 4], buf[idx + 5]);
  333. break;
  334. case 0xaa:
  335. dev_info(dev, "Device reset requested\n");
  336. break;
  337. case 0xfe:
  338. dev_info(dev, "Previous command not supported\n");
  339. break;
  340. case 0x18:
  341. case 0x1b:
  342. default:
  343. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  344. cmd, subcmd);
  345. break;
  346. }
  347. break;
  348. case 0x9f:
  349. switch (subcmd) {
  350. case 0x03:
  351. dev_info(dev, "Ping\n");
  352. break;
  353. case 0x04:
  354. dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
  355. data1, data2);
  356. break;
  357. case 0x06:
  358. dev_info(dev, "%s carrier mode and freq of "
  359. "0x%02x 0x%02x\n", inout, data1, data2);
  360. break;
  361. case 0x07:
  362. dev_info(dev, "Get carrier mode and freq\n");
  363. break;
  364. case 0x08:
  365. dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
  366. inout, data1);
  367. break;
  368. case 0x0c:
  369. /* value is in units of 50us, so x*50/100 or x/2 ms */
  370. dev_info(dev, "%s receive timeout of %d ms\n",
  371. inout, ((data1 << 8) | data2) / 2);
  372. break;
  373. case 0x0d:
  374. dev_info(dev, "Get receive timeout\n");
  375. break;
  376. case 0x13:
  377. dev_info(dev, "Get transmit blaster mask\n");
  378. break;
  379. case 0x14:
  380. dev_info(dev, "%s %s-range receive sensor in use\n",
  381. inout, data1 == 0x02 ? "short" : "long");
  382. break;
  383. case 0x15:
  384. if (len == 2)
  385. dev_info(dev, "Get receive sensor\n");
  386. else
  387. dev_info(dev, "Received pulse count is %d\n",
  388. ((data1 << 8) | data2));
  389. break;
  390. case 0xfe:
  391. dev_info(dev, "Error! Hardware is likely wedged...\n");
  392. break;
  393. case 0x05:
  394. case 0x09:
  395. case 0x0f:
  396. default:
  397. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  398. cmd, subcmd);
  399. break;
  400. }
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
  407. {
  408. struct mceusb_dev *ir;
  409. int len;
  410. if (!urb)
  411. return;
  412. ir = urb->context;
  413. if (ir) {
  414. len = urb->actual_length;
  415. dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
  416. urb->status, len);
  417. if (debug)
  418. mceusb_dev_printdata(ir, urb->transfer_buffer,
  419. len, true);
  420. }
  421. }
  422. /* request incoming or send outgoing usb packet - used to initialize remote */
  423. static void mce_request_packet(struct mceusb_dev *ir,
  424. struct usb_endpoint_descriptor *ep,
  425. unsigned char *data, int size, int urb_type)
  426. {
  427. int res;
  428. struct urb *async_urb;
  429. struct device *dev = ir->dev;
  430. unsigned char *async_buf;
  431. if (urb_type == MCEUSB_TX) {
  432. async_urb = usb_alloc_urb(0, GFP_KERNEL);
  433. if (unlikely(!async_urb)) {
  434. dev_err(dev, "Error, couldn't allocate urb!\n");
  435. return;
  436. }
  437. async_buf = kzalloc(size, GFP_KERNEL);
  438. if (!async_buf) {
  439. dev_err(dev, "Error, couldn't allocate buf!\n");
  440. usb_free_urb(async_urb);
  441. return;
  442. }
  443. /* outbound data */
  444. usb_fill_int_urb(async_urb, ir->usbdev,
  445. usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
  446. async_buf, size, (usb_complete_t) usb_async_callback,
  447. ir, ep->bInterval);
  448. memcpy(async_buf, data, size);
  449. } else if (urb_type == MCEUSB_RX) {
  450. /* standard request */
  451. async_urb = ir->urb_in;
  452. ir->send_flags = RECV_FLAG_IN_PROGRESS;
  453. } else {
  454. dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
  455. return;
  456. }
  457. dev_dbg(dev, "receive request called (size=%#x)\n", size);
  458. async_urb->transfer_buffer_length = size;
  459. async_urb->dev = ir->usbdev;
  460. res = usb_submit_urb(async_urb, GFP_ATOMIC);
  461. if (res) {
  462. dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
  463. return;
  464. }
  465. dev_dbg(dev, "receive request complete (res=%d)\n", res);
  466. }
  467. static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
  468. {
  469. mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
  470. }
  471. static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
  472. {
  473. mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
  474. }
  475. /* Sets active IR outputs -- mce devices typically (all?) have two */
  476. static int mceusb_set_tx_mask(void *priv, u32 mask)
  477. {
  478. struct mceusb_dev *ir = priv;
  479. if (ir->flags.tx_mask_inverted)
  480. ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
  481. else
  482. ir->tx_mask = mask;
  483. return 0;
  484. }
  485. static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
  486. {
  487. struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
  488. int i, start_index = 0;
  489. u8 hdr = MCE_CONTROL_HEADER;
  490. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  491. if (ir->flags.microsoft_gen1)
  492. start_index = 2;
  493. for (i = start_index; i < buf_len;) {
  494. if (ir->rem == 0) {
  495. /* decode mce packets of the form (84),AA,BB,CC,DD */
  496. /* IR data packets can span USB messages - rem */
  497. hdr = ir->buf_in[i];
  498. ir->rem = (hdr & MCE_PACKET_LENGTH_MASK);
  499. ir->cmd = (hdr & ~MCE_PACKET_LENGTH_MASK);
  500. dev_dbg(ir->dev, "New data. rem: 0x%02x, cmd: 0x%02x\n",
  501. ir->rem, ir->cmd);
  502. i++;
  503. }
  504. /* don't process MCE commands */
  505. if (hdr == MCE_CONTROL_HEADER || hdr == 0xff) {
  506. ir->rem = 0;
  507. return;
  508. }
  509. for (; (ir->rem > 0) && (i < buf_len); i++) {
  510. ir->rem--;
  511. rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
  512. rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
  513. * MCE_TIME_UNIT * 1000;
  514. if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
  515. if (ir->rawir.pulse == rawir.pulse)
  516. ir->rawir.duration += rawir.duration;
  517. else {
  518. ir->rawir.duration = rawir.duration;
  519. ir->rawir.pulse = rawir.pulse;
  520. }
  521. continue;
  522. }
  523. rawir.duration += ir->rawir.duration;
  524. ir->rawir.duration = 0;
  525. ir->rawir.pulse = rawir.pulse;
  526. dev_dbg(ir->dev, "Storing %s with duration %d\n",
  527. rawir.pulse ? "pulse" : "space",
  528. rawir.duration);
  529. ir_raw_event_store(ir->idev, &rawir);
  530. }
  531. if (ir->buf_in[i] == 0x80 || ir->buf_in[i] == 0x9f)
  532. ir->rem = 0;
  533. dev_dbg(ir->dev, "calling ir_raw_event_handle\n");
  534. ir_raw_event_handle(ir->idev);
  535. }
  536. }
  537. static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
  538. {
  539. struct mceusb_dev *ir;
  540. int buf_len;
  541. if (!urb)
  542. return;
  543. ir = urb->context;
  544. if (!ir) {
  545. usb_unlink_urb(urb);
  546. return;
  547. }
  548. buf_len = urb->actual_length;
  549. if (debug)
  550. mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
  551. if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
  552. ir->send_flags = SEND_FLAG_COMPLETE;
  553. dev_dbg(&ir->irdev->dev, "setup answer received %d bytes\n",
  554. buf_len);
  555. }
  556. switch (urb->status) {
  557. /* success */
  558. case 0:
  559. mceusb_process_ir_data(ir, buf_len);
  560. break;
  561. case -ECONNRESET:
  562. case -ENOENT:
  563. case -ESHUTDOWN:
  564. usb_unlink_urb(urb);
  565. return;
  566. case -EPIPE:
  567. default:
  568. break;
  569. }
  570. usb_submit_urb(urb, GFP_ATOMIC);
  571. }
  572. static void mceusb_gen1_init(struct mceusb_dev *ir)
  573. {
  574. int i, ret;
  575. int partial = 0;
  576. struct device *dev = ir->dev;
  577. char *junk, *data;
  578. junk = kmalloc(2 * USB_BUFLEN, GFP_KERNEL);
  579. if (!junk) {
  580. dev_err(dev, "%s: memory allocation failed!\n", __func__);
  581. return;
  582. }
  583. data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
  584. if (!data) {
  585. dev_err(dev, "%s: memory allocation failed!\n", __func__);
  586. kfree(junk);
  587. return;
  588. }
  589. /*
  590. * Clear off the first few messages. These look like calibration
  591. * or test data, I can't really tell. This also flushes in case
  592. * we have random ir data queued up.
  593. */
  594. for (i = 0; i < MCE_G1_INIT_MSGS; i++)
  595. usb_bulk_msg(ir->usbdev,
  596. usb_rcvbulkpipe(ir->usbdev,
  597. ir->usb_ep_in->bEndpointAddress),
  598. junk, sizeof(junk), &partial, HZ * 10);
  599. /* Get Status */
  600. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  601. USB_REQ_GET_STATUS, USB_DIR_IN,
  602. 0, 0, data, USB_CTRL_MSG_SZ, HZ * 3);
  603. /* ret = usb_get_status( ir->usbdev, 0, 0, data ); */
  604. dev_dbg(dev, "%s - ret = %d status = 0x%x 0x%x\n", __func__,
  605. ret, data[0], data[1]);
  606. /*
  607. * This is a strange one. They issue a set address to the device
  608. * on the receive control pipe and expect a certain value pair back
  609. */
  610. memset(data, 0, sizeof(data));
  611. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  612. USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
  613. data, USB_CTRL_MSG_SZ, HZ * 3);
  614. dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
  615. dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
  616. __func__, data[0], data[1]);
  617. /* set feature: bit rate 38400 bps */
  618. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  619. USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
  620. 0xc04e, 0x0000, NULL, 0, HZ * 3);
  621. dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
  622. /* bRequest 4: set char length to 8 bits */
  623. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  624. 4, USB_TYPE_VENDOR,
  625. 0x0808, 0x0000, NULL, 0, HZ * 3);
  626. dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
  627. /* bRequest 2: set handshaking to use DTR/DSR */
  628. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  629. 2, USB_TYPE_VENDOR,
  630. 0x0000, 0x0100, NULL, 0, HZ * 3);
  631. dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
  632. kfree(data);
  633. kfree(junk);
  634. };
  635. static void mceusb_gen2_init(struct mceusb_dev *ir)
  636. {
  637. int maxp = ir->len_in;
  638. mce_sync_in(ir, NULL, maxp);
  639. mce_sync_in(ir, NULL, maxp);
  640. /* device reset */
  641. mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
  642. mce_sync_in(ir, NULL, maxp);
  643. /* get hw/sw revision? */
  644. mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
  645. mce_sync_in(ir, NULL, maxp);
  646. /* unknown what this actually returns... */
  647. mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
  648. mce_sync_in(ir, NULL, maxp);
  649. }
  650. static void mceusb_gen3_init(struct mceusb_dev *ir)
  651. {
  652. int maxp = ir->len_in;
  653. mce_sync_in(ir, NULL, maxp);
  654. /* device reset */
  655. mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
  656. mce_sync_in(ir, NULL, maxp);
  657. /* get the carrier and frequency */
  658. mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
  659. mce_sync_in(ir, NULL, maxp);
  660. /* get the transmitter bitmask */
  661. mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
  662. mce_sync_in(ir, NULL, maxp);
  663. /* get receiver timeout value */
  664. mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
  665. mce_sync_in(ir, NULL, maxp);
  666. /* get receiver sensor setting */
  667. mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
  668. mce_sync_in(ir, NULL, maxp);
  669. }
  670. static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
  671. {
  672. struct input_dev *idev;
  673. struct ir_dev_props *props;
  674. struct ir_input_dev *irdev;
  675. struct device *dev = ir->dev;
  676. int ret = -ENODEV;
  677. idev = input_allocate_device();
  678. if (!idev) {
  679. dev_err(dev, "remote input dev allocation failed\n");
  680. goto idev_alloc_failed;
  681. }
  682. ret = -ENOMEM;
  683. props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
  684. if (!props) {
  685. dev_err(dev, "remote ir dev props allocation failed\n");
  686. goto props_alloc_failed;
  687. }
  688. irdev = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
  689. if (!irdev) {
  690. dev_err(dev, "remote ir input dev allocation failed\n");
  691. goto ir_dev_alloc_failed;
  692. }
  693. snprintf(ir->name, sizeof(ir->name), "Media Center Edition eHome "
  694. "Infrared Remote Transceiver (%04x:%04x)",
  695. le16_to_cpu(ir->usbdev->descriptor.idVendor),
  696. le16_to_cpu(ir->usbdev->descriptor.idProduct));
  697. idev->name = ir->name;
  698. usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
  699. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  700. idev->phys = ir->phys;
  701. props->priv = ir;
  702. props->driver_type = RC_DRIVER_IR_RAW;
  703. props->allowed_protos = IR_TYPE_ALL;
  704. ir->props = props;
  705. ir->irdev = irdev;
  706. input_set_drvdata(idev, irdev);
  707. ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
  708. if (ret < 0) {
  709. dev_err(dev, "remote input device register failed\n");
  710. goto irdev_failed;
  711. }
  712. return idev;
  713. irdev_failed:
  714. kfree(irdev);
  715. ir_dev_alloc_failed:
  716. kfree(props);
  717. props_alloc_failed:
  718. input_free_device(idev);
  719. idev_alloc_failed:
  720. return NULL;
  721. }
  722. static int __devinit mceusb_dev_probe(struct usb_interface *intf,
  723. const struct usb_device_id *id)
  724. {
  725. struct usb_device *dev = interface_to_usbdev(intf);
  726. struct usb_host_interface *idesc;
  727. struct usb_endpoint_descriptor *ep = NULL;
  728. struct usb_endpoint_descriptor *ep_in = NULL;
  729. struct usb_endpoint_descriptor *ep_out = NULL;
  730. struct usb_host_config *config;
  731. struct mceusb_dev *ir = NULL;
  732. int pipe, maxp, i;
  733. char buf[63], name[128] = "";
  734. bool is_gen3;
  735. bool is_microsoft_gen1;
  736. bool tx_mask_inverted;
  737. dev_dbg(&intf->dev, ": %s called\n", __func__);
  738. usb_reset_device(dev);
  739. config = dev->actconfig;
  740. idesc = intf->cur_altsetting;
  741. is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
  742. is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
  743. tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
  744. /* step through the endpoints to find first bulk in and out endpoint */
  745. for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
  746. ep = &idesc->endpoint[i].desc;
  747. if ((ep_in == NULL)
  748. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  749. == USB_DIR_IN)
  750. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  751. == USB_ENDPOINT_XFER_BULK)
  752. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  753. == USB_ENDPOINT_XFER_INT))) {
  754. ep_in = ep;
  755. ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
  756. ep_in->bInterval = 1;
  757. dev_dbg(&intf->dev, ": acceptable inbound endpoint "
  758. "found\n");
  759. }
  760. if ((ep_out == NULL)
  761. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  762. == USB_DIR_OUT)
  763. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  764. == USB_ENDPOINT_XFER_BULK)
  765. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  766. == USB_ENDPOINT_XFER_INT))) {
  767. ep_out = ep;
  768. ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
  769. ep_out->bInterval = 1;
  770. dev_dbg(&intf->dev, ": acceptable outbound endpoint "
  771. "found\n");
  772. }
  773. }
  774. if (ep_in == NULL) {
  775. dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
  776. return -ENODEV;
  777. }
  778. pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
  779. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  780. ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
  781. if (!ir)
  782. goto mem_alloc_fail;
  783. ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
  784. if (!ir->buf_in)
  785. goto buf_in_alloc_fail;
  786. ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  787. if (!ir->urb_in)
  788. goto urb_in_alloc_fail;
  789. ir->usbdev = dev;
  790. ir->dev = &intf->dev;
  791. ir->len_in = maxp;
  792. ir->flags.gen3 = is_gen3;
  793. ir->flags.microsoft_gen1 = is_microsoft_gen1;
  794. ir->flags.tx_mask_inverted = tx_mask_inverted;
  795. /* Saving usb interface data for use by the transmitter routine */
  796. ir->usb_ep_in = ep_in;
  797. ir->usb_ep_out = ep_out;
  798. if (dev->descriptor.iManufacturer
  799. && usb_string(dev, dev->descriptor.iManufacturer,
  800. buf, sizeof(buf)) > 0)
  801. strlcpy(name, buf, sizeof(name));
  802. if (dev->descriptor.iProduct
  803. && usb_string(dev, dev->descriptor.iProduct,
  804. buf, sizeof(buf)) > 0)
  805. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  806. " %s", buf);
  807. ir->idev = mceusb_init_input_dev(ir);
  808. if (!ir->idev)
  809. goto input_dev_fail;
  810. /* inbound data */
  811. usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
  812. maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
  813. ir->urb_in->transfer_dma = ir->dma_in;
  814. ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  815. /* initialize device */
  816. if (ir->flags.gen3)
  817. mceusb_gen3_init(ir);
  818. else if (ir->flags.microsoft_gen1)
  819. mceusb_gen1_init(ir);
  820. else
  821. mceusb_gen2_init(ir);
  822. mce_sync_in(ir, NULL, maxp);
  823. mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
  824. usb_set_intfdata(intf, ir);
  825. dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
  826. dev->bus->busnum, dev->devnum);
  827. return 0;
  828. /* Error-handling path */
  829. input_dev_fail:
  830. usb_free_urb(ir->urb_in);
  831. urb_in_alloc_fail:
  832. usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
  833. buf_in_alloc_fail:
  834. kfree(ir);
  835. mem_alloc_fail:
  836. dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
  837. return -ENOMEM;
  838. }
  839. static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
  840. {
  841. struct usb_device *dev = interface_to_usbdev(intf);
  842. struct mceusb_dev *ir = usb_get_intfdata(intf);
  843. usb_set_intfdata(intf, NULL);
  844. if (!ir)
  845. return;
  846. ir->usbdev = NULL;
  847. ir_input_unregister(ir->idev);
  848. usb_kill_urb(ir->urb_in);
  849. usb_free_urb(ir->urb_in);
  850. usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
  851. kfree(ir);
  852. }
  853. static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
  854. {
  855. struct mceusb_dev *ir = usb_get_intfdata(intf);
  856. dev_info(ir->dev, "suspend\n");
  857. usb_kill_urb(ir->urb_in);
  858. return 0;
  859. }
  860. static int mceusb_dev_resume(struct usb_interface *intf)
  861. {
  862. struct mceusb_dev *ir = usb_get_intfdata(intf);
  863. dev_info(ir->dev, "resume\n");
  864. if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
  865. return -EIO;
  866. return 0;
  867. }
  868. static struct usb_driver mceusb_dev_driver = {
  869. .name = DRIVER_NAME,
  870. .probe = mceusb_dev_probe,
  871. .disconnect = mceusb_dev_disconnect,
  872. .suspend = mceusb_dev_suspend,
  873. .resume = mceusb_dev_resume,
  874. .reset_resume = mceusb_dev_resume,
  875. .id_table = mceusb_dev_table
  876. };
  877. static int __init mceusb_dev_init(void)
  878. {
  879. int ret;
  880. ret = usb_register(&mceusb_dev_driver);
  881. if (ret < 0)
  882. printk(KERN_ERR DRIVER_NAME
  883. ": usb register failed, result = %d\n", ret);
  884. return ret;
  885. }
  886. static void __exit mceusb_dev_exit(void)
  887. {
  888. usb_deregister(&mceusb_dev_driver);
  889. }
  890. module_init(mceusb_dev_init);
  891. module_exit(mceusb_dev_exit);
  892. MODULE_DESCRIPTION(DRIVER_DESC);
  893. MODULE_AUTHOR(DRIVER_AUTHOR);
  894. MODULE_LICENSE("GPL");
  895. MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
  896. module_param(debug, bool, S_IRUGO | S_IWUSR);
  897. MODULE_PARM_DESC(debug, "Debug enabled or not");