mceusb.c 30 KB

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