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. #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 0xF /* 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 pinnacle_list[] = {
  201. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  202. {}
  203. };
  204. static struct usb_device_id microsoft_gen1_list[] = {
  205. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  206. {}
  207. };
  208. static struct usb_device_id std_tx_mask_list[] = {
  209. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
  210. { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
  211. { USB_DEVICE(VENDOR_SMK, 0x031d) },
  212. { USB_DEVICE(VENDOR_SMK, 0x0322) },
  213. { USB_DEVICE(VENDOR_SMK, 0x0334) },
  214. { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
  215. { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
  216. { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
  217. { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
  218. { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
  219. { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
  220. { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
  221. {}
  222. };
  223. /* data structure for each usb transceiver */
  224. struct mceusb_dev {
  225. /* ir-core bits */
  226. struct ir_input_dev *irdev;
  227. struct ir_dev_props *props;
  228. struct ir_raw_event rawir;
  229. /* core device bits */
  230. struct device *dev;
  231. struct input_dev *idev;
  232. /* usb */
  233. struct usb_device *usbdev;
  234. struct urb *urb_in;
  235. struct usb_endpoint_descriptor *usb_ep_in;
  236. struct usb_endpoint_descriptor *usb_ep_out;
  237. /* buffers and dma */
  238. unsigned char *buf_in;
  239. unsigned int len_in;
  240. u8 cmd; /* MCE command type */
  241. u8 rem; /* Remaining IR data bytes in packet */
  242. dma_addr_t dma_in;
  243. dma_addr_t dma_out;
  244. struct {
  245. u32 connected:1;
  246. u32 tx_mask_inverted:1;
  247. u32 microsoft_gen1:1;
  248. u32 gen3:1;
  249. u32 reserved:28;
  250. } flags;
  251. /* handle sending (init strings) */
  252. int send_flags;
  253. int carrier;
  254. char name[128];
  255. char phys[64];
  256. unsigned char tx_mask;
  257. };
  258. /*
  259. * MCE Device Command Strings
  260. * Device command responses vary from device to device...
  261. * - DEVICE_RESET resets the hardware to its default state
  262. * - GET_REVISION fetches the hardware/software revision, common
  263. * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
  264. * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
  265. * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
  266. * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
  267. * ((clk / frequency) - 1)
  268. * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
  269. * response in the form of 9f 0c msb lsb
  270. * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
  271. * the form of 9f 08 bm, where bm is the bitmask
  272. * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
  273. * general use one or short-range learning one, in the form of
  274. * 9f 14 ss, where ss is either 01 for long-range or 02 for short
  275. * - SET_CARRIER_FREQ sets a new carrier mode and frequency
  276. * - SET_TX_BITMASK sets the transmitter bitmask
  277. * - SET_RX_TIMEOUT sets the receiver timeout
  278. * - SET_RX_SENSOR sets which receiver sensor to use
  279. */
  280. static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
  281. static char GET_REVISION[] = {0xff, 0x0b};
  282. static char GET_UNKNOWN[] = {0xff, 0x18};
  283. static char GET_CARRIER_FREQ[] = {0x9f, 0x07};
  284. static char GET_RX_TIMEOUT[] = {0x9f, 0x0d};
  285. static char GET_TX_BITMASK[] = {0x9f, 0x13};
  286. static char GET_RX_SENSOR[] = {0x9f, 0x15};
  287. /* sub in desired values in lower byte or bytes for full command */
  288. /* FIXME: make use of these for transmit.
  289. static char SET_CARRIER_FREQ[] = {0x9f, 0x06, 0x00, 0x00};
  290. static char SET_TX_BITMASK[] = {0x9f, 0x08, 0x00};
  291. static char SET_RX_TIMEOUT[] = {0x9f, 0x0c, 0x00, 0x00};
  292. static char SET_RX_SENSOR[] = {0x9f, 0x14, 0x00};
  293. */
  294. static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
  295. int len, bool out)
  296. {
  297. char codes[USB_BUFLEN * 3 + 1];
  298. char inout[9];
  299. int i;
  300. u8 cmd, subcmd, data1, data2;
  301. struct device *dev = ir->dev;
  302. int idx = 0;
  303. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  304. if (ir->flags.microsoft_gen1 && !out)
  305. idx = 2;
  306. if (len <= idx)
  307. return;
  308. for (i = 0; i < len && i < USB_BUFLEN; i++)
  309. snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
  310. dev_info(dev, "%sx data: %s (length=%d)\n",
  311. (out ? "t" : "r"), codes, len);
  312. if (out)
  313. strcpy(inout, "Request\0");
  314. else
  315. strcpy(inout, "Got\0");
  316. cmd = buf[idx] & 0xff;
  317. subcmd = buf[idx + 1] & 0xff;
  318. data1 = buf[idx + 2] & 0xff;
  319. data2 = buf[idx + 3] & 0xff;
  320. switch (cmd) {
  321. case 0x00:
  322. if (subcmd == 0xff && data1 == 0xaa)
  323. dev_info(dev, "Device reset requested\n");
  324. else
  325. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  326. cmd, subcmd);
  327. break;
  328. case 0xff:
  329. switch (subcmd) {
  330. case 0x0b:
  331. if (len == 2)
  332. dev_info(dev, "Get hw/sw rev?\n");
  333. else
  334. dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
  335. "0x%02x 0x%02x\n", data1, data2,
  336. buf[idx + 4], buf[idx + 5]);
  337. break;
  338. case 0xaa:
  339. dev_info(dev, "Device reset requested\n");
  340. break;
  341. case 0xfe:
  342. dev_info(dev, "Previous command not supported\n");
  343. break;
  344. case 0x18:
  345. case 0x1b:
  346. default:
  347. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  348. cmd, subcmd);
  349. break;
  350. }
  351. break;
  352. case 0x9f:
  353. switch (subcmd) {
  354. case 0x03:
  355. dev_info(dev, "Ping\n");
  356. break;
  357. case 0x04:
  358. dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
  359. data1, data2);
  360. break;
  361. case 0x06:
  362. dev_info(dev, "%s carrier mode and freq of "
  363. "0x%02x 0x%02x\n", inout, data1, data2);
  364. break;
  365. case 0x07:
  366. dev_info(dev, "Get carrier mode and freq\n");
  367. break;
  368. case 0x08:
  369. dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
  370. inout, data1);
  371. break;
  372. case 0x0c:
  373. /* value is in units of 50us, so x*50/100 or x/2 ms */
  374. dev_info(dev, "%s receive timeout of %d ms\n",
  375. inout, ((data1 << 8) | data2) / 2);
  376. break;
  377. case 0x0d:
  378. dev_info(dev, "Get receive timeout\n");
  379. break;
  380. case 0x13:
  381. dev_info(dev, "Get transmit blaster mask\n");
  382. break;
  383. case 0x14:
  384. dev_info(dev, "%s %s-range receive sensor in use\n",
  385. inout, data1 == 0x02 ? "short" : "long");
  386. break;
  387. case 0x15:
  388. if (len == 2)
  389. dev_info(dev, "Get receive sensor\n");
  390. else
  391. dev_info(dev, "Received pulse count is %d\n",
  392. ((data1 << 8) | data2));
  393. break;
  394. case 0xfe:
  395. dev_info(dev, "Error! Hardware is likely wedged...\n");
  396. break;
  397. case 0x05:
  398. case 0x09:
  399. case 0x0f:
  400. default:
  401. dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
  402. cmd, subcmd);
  403. break;
  404. }
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
  411. {
  412. struct mceusb_dev *ir;
  413. int len;
  414. if (!urb)
  415. return;
  416. ir = urb->context;
  417. if (ir) {
  418. len = urb->actual_length;
  419. dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
  420. urb->status, len);
  421. if (debug)
  422. mceusb_dev_printdata(ir, urb->transfer_buffer,
  423. len, true);
  424. }
  425. }
  426. /* request incoming or send outgoing usb packet - used to initialize remote */
  427. static void mce_request_packet(struct mceusb_dev *ir,
  428. struct usb_endpoint_descriptor *ep,
  429. unsigned char *data, int size, int urb_type)
  430. {
  431. int res;
  432. struct urb *async_urb;
  433. struct device *dev = ir->dev;
  434. unsigned char *async_buf;
  435. if (urb_type == MCEUSB_TX) {
  436. async_urb = usb_alloc_urb(0, GFP_KERNEL);
  437. if (unlikely(!async_urb)) {
  438. dev_err(dev, "Error, couldn't allocate urb!\n");
  439. return;
  440. }
  441. async_buf = kzalloc(size, GFP_KERNEL);
  442. if (!async_buf) {
  443. dev_err(dev, "Error, couldn't allocate buf!\n");
  444. usb_free_urb(async_urb);
  445. return;
  446. }
  447. /* outbound data */
  448. usb_fill_int_urb(async_urb, ir->usbdev,
  449. usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
  450. async_buf, size, (usb_complete_t) usb_async_callback,
  451. ir, ep->bInterval);
  452. memcpy(async_buf, data, size);
  453. } else if (urb_type == MCEUSB_RX) {
  454. /* standard request */
  455. async_urb = ir->urb_in;
  456. ir->send_flags = RECV_FLAG_IN_PROGRESS;
  457. } else {
  458. dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
  459. return;
  460. }
  461. dev_dbg(dev, "receive request called (size=%#x)\n", size);
  462. async_urb->transfer_buffer_length = size;
  463. async_urb->dev = ir->usbdev;
  464. res = usb_submit_urb(async_urb, GFP_ATOMIC);
  465. if (res) {
  466. dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
  467. return;
  468. }
  469. dev_dbg(dev, "receive request complete (res=%d)\n", res);
  470. }
  471. static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
  472. {
  473. mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
  474. }
  475. static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
  476. {
  477. mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
  478. }
  479. /* Sets active IR outputs -- mce devices typically (all?) have two */
  480. static int mceusb_set_tx_mask(void *priv, u32 mask)
  481. {
  482. struct mceusb_dev *ir = priv;
  483. if (ir->flags.tx_mask_inverted)
  484. ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
  485. else
  486. ir->tx_mask = mask;
  487. return 0;
  488. }
  489. static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
  490. {
  491. struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
  492. int i, start_index = 0;
  493. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  494. if (ir->flags.microsoft_gen1)
  495. start_index = 2;
  496. for (i = start_index; i < buf_len;) {
  497. if (ir->rem == 0) {
  498. /* decode mce packets of the form (84),AA,BB,CC,DD */
  499. /* IR data packets can span USB messages - rem */
  500. ir->rem = (ir->buf_in[i] & MCE_PACKET_LENGTH_MASK);
  501. ir->cmd = (ir->buf_in[i] & ~MCE_PACKET_LENGTH_MASK);
  502. dev_dbg(ir->dev, "New data. rem: 0x%02x, cmd: 0x%02x\n",
  503. ir->rem, ir->cmd);
  504. i++;
  505. }
  506. /* Only cmd 0x8<bytes> is IR data, don't process MCE commands */
  507. if (ir->cmd != 0x80) {
  508. ir->rem = 0;
  509. return;
  510. }
  511. for (; (ir->rem > 0) && (i < buf_len); i++) {
  512. ir->rem--;
  513. rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
  514. rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
  515. * MCE_TIME_UNIT * 1000;
  516. if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
  517. if (ir->rawir.pulse == rawir.pulse)
  518. ir->rawir.duration += rawir.duration;
  519. else {
  520. ir->rawir.duration = rawir.duration;
  521. ir->rawir.pulse = rawir.pulse;
  522. }
  523. continue;
  524. }
  525. rawir.duration += ir->rawir.duration;
  526. ir->rawir.duration = 0;
  527. ir->rawir.pulse = rawir.pulse;
  528. dev_dbg(ir->dev, "Storing %s with duration %d\n",
  529. rawir.pulse ? "pulse" : "space",
  530. rawir.duration);
  531. ir_raw_event_store(ir->idev, &rawir);
  532. }
  533. if (ir->buf_in[i] == 0x80 || ir->buf_in[i] == 0x9f)
  534. ir->rem = 0;
  535. dev_dbg(ir->dev, "calling ir_raw_event_handle\n");
  536. ir_raw_event_handle(ir->idev);
  537. }
  538. }
  539. static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
  540. {
  541. struct mceusb_dev *ir;
  542. int buf_len;
  543. if (!urb)
  544. return;
  545. ir = urb->context;
  546. if (!ir) {
  547. usb_unlink_urb(urb);
  548. return;
  549. }
  550. buf_len = urb->actual_length;
  551. if (debug)
  552. mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
  553. if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
  554. ir->send_flags = SEND_FLAG_COMPLETE;
  555. dev_dbg(&ir->irdev->dev, "setup answer received %d bytes\n",
  556. buf_len);
  557. }
  558. switch (urb->status) {
  559. /* success */
  560. case 0:
  561. mceusb_process_ir_data(ir, buf_len);
  562. break;
  563. case -ECONNRESET:
  564. case -ENOENT:
  565. case -ESHUTDOWN:
  566. usb_unlink_urb(urb);
  567. return;
  568. case -EPIPE:
  569. default:
  570. break;
  571. }
  572. usb_submit_urb(urb, GFP_ATOMIC);
  573. }
  574. static void mceusb_gen1_init(struct mceusb_dev *ir)
  575. {
  576. int i, ret;
  577. int partial = 0;
  578. struct device *dev = ir->dev;
  579. char *junk, *data;
  580. junk = kmalloc(2 * USB_BUFLEN, GFP_KERNEL);
  581. if (!junk) {
  582. dev_err(dev, "%s: memory allocation failed!\n", __func__);
  583. return;
  584. }
  585. data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
  586. if (!data) {
  587. dev_err(dev, "%s: memory allocation failed!\n", __func__);
  588. kfree(junk);
  589. return;
  590. }
  591. /*
  592. * Clear off the first few messages. These look like calibration
  593. * or test data, I can't really tell. This also flushes in case
  594. * we have random ir data queued up.
  595. */
  596. for (i = 0; i < MCE_G1_INIT_MSGS; i++)
  597. usb_bulk_msg(ir->usbdev,
  598. usb_rcvbulkpipe(ir->usbdev,
  599. ir->usb_ep_in->bEndpointAddress),
  600. junk, sizeof(junk), &partial, HZ * 10);
  601. /* Get Status */
  602. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  603. USB_REQ_GET_STATUS, USB_DIR_IN,
  604. 0, 0, data, USB_CTRL_MSG_SZ, HZ * 3);
  605. /* ret = usb_get_status( ir->usbdev, 0, 0, data ); */
  606. dev_dbg(dev, "%s - ret = %d status = 0x%x 0x%x\n", __func__,
  607. ret, data[0], data[1]);
  608. /*
  609. * This is a strange one. They issue a set address to the device
  610. * on the receive control pipe and expect a certain value pair back
  611. */
  612. memset(data, 0, sizeof(data));
  613. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  614. USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
  615. data, USB_CTRL_MSG_SZ, HZ * 3);
  616. dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
  617. dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
  618. __func__, data[0], data[1]);
  619. /* set feature: bit rate 38400 bps */
  620. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  621. USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
  622. 0xc04e, 0x0000, NULL, 0, HZ * 3);
  623. dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
  624. /* bRequest 4: set char length to 8 bits */
  625. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  626. 4, USB_TYPE_VENDOR,
  627. 0x0808, 0x0000, NULL, 0, HZ * 3);
  628. dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
  629. /* bRequest 2: set handshaking to use DTR/DSR */
  630. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  631. 2, USB_TYPE_VENDOR,
  632. 0x0000, 0x0100, NULL, 0, HZ * 3);
  633. dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
  634. kfree(data);
  635. kfree(junk);
  636. };
  637. static void mceusb_gen2_init(struct mceusb_dev *ir)
  638. {
  639. int maxp = ir->len_in;
  640. mce_sync_in(ir, NULL, maxp);
  641. mce_sync_in(ir, NULL, maxp);
  642. /* device reset */
  643. mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
  644. mce_sync_in(ir, NULL, maxp);
  645. /* get hw/sw revision? */
  646. mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
  647. mce_sync_in(ir, NULL, maxp);
  648. /* unknown what this actually returns... */
  649. mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
  650. mce_sync_in(ir, NULL, maxp);
  651. }
  652. static void mceusb_gen3_init(struct mceusb_dev *ir)
  653. {
  654. int maxp = ir->len_in;
  655. mce_sync_in(ir, NULL, maxp);
  656. /* device reset */
  657. mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
  658. mce_sync_in(ir, NULL, maxp);
  659. /* get the carrier and frequency */
  660. mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
  661. mce_sync_in(ir, NULL, maxp);
  662. /* get the transmitter bitmask */
  663. mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
  664. mce_sync_in(ir, NULL, maxp);
  665. /* get receiver timeout value */
  666. mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
  667. mce_sync_in(ir, NULL, maxp);
  668. /* get receiver sensor setting */
  669. mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
  670. mce_sync_in(ir, NULL, maxp);
  671. }
  672. static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
  673. {
  674. struct input_dev *idev;
  675. struct ir_dev_props *props;
  676. struct ir_input_dev *irdev;
  677. struct device *dev = ir->dev;
  678. int ret = -ENODEV;
  679. idev = input_allocate_device();
  680. if (!idev) {
  681. dev_err(dev, "remote input dev allocation failed\n");
  682. goto idev_alloc_failed;
  683. }
  684. ret = -ENOMEM;
  685. props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
  686. if (!props) {
  687. dev_err(dev, "remote ir dev props allocation failed\n");
  688. goto props_alloc_failed;
  689. }
  690. irdev = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
  691. if (!irdev) {
  692. dev_err(dev, "remote ir input dev allocation failed\n");
  693. goto ir_dev_alloc_failed;
  694. }
  695. snprintf(ir->name, sizeof(ir->name), "Media Center Edition eHome "
  696. "Infrared Remote Transceiver (%04x:%04x)",
  697. le16_to_cpu(ir->usbdev->descriptor.idVendor),
  698. le16_to_cpu(ir->usbdev->descriptor.idProduct));
  699. idev->name = ir->name;
  700. usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
  701. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  702. idev->phys = ir->phys;
  703. props->priv = ir;
  704. props->driver_type = RC_DRIVER_IR_RAW;
  705. props->allowed_protos = IR_TYPE_ALL;
  706. ir->props = props;
  707. ir->irdev = irdev;
  708. input_set_drvdata(idev, irdev);
  709. ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
  710. if (ret < 0) {
  711. dev_err(dev, "remote input device register failed\n");
  712. goto irdev_failed;
  713. }
  714. return idev;
  715. irdev_failed:
  716. kfree(irdev);
  717. ir_dev_alloc_failed:
  718. kfree(props);
  719. props_alloc_failed:
  720. input_free_device(idev);
  721. idev_alloc_failed:
  722. return NULL;
  723. }
  724. static int __devinit mceusb_dev_probe(struct usb_interface *intf,
  725. const struct usb_device_id *id)
  726. {
  727. struct usb_device *dev = interface_to_usbdev(intf);
  728. struct usb_host_interface *idesc;
  729. struct usb_endpoint_descriptor *ep = NULL;
  730. struct usb_endpoint_descriptor *ep_in = NULL;
  731. struct usb_endpoint_descriptor *ep_out = NULL;
  732. struct usb_host_config *config;
  733. struct mceusb_dev *ir = NULL;
  734. int pipe, maxp;
  735. int i, ret;
  736. char buf[63], name[128] = "";
  737. bool is_gen3;
  738. bool is_microsoft_gen1;
  739. bool is_pinnacle;
  740. bool tx_mask_inverted;
  741. dev_dbg(&intf->dev, ": %s called\n", __func__);
  742. usb_reset_device(dev);
  743. config = dev->actconfig;
  744. idesc = intf->cur_altsetting;
  745. is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
  746. is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
  747. is_pinnacle = usb_match_id(intf, pinnacle_list) ? 1 : 0;
  748. tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
  749. /* step through the endpoints to find first bulk in and out endpoint */
  750. for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
  751. ep = &idesc->endpoint[i].desc;
  752. if ((ep_in == NULL)
  753. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  754. == USB_DIR_IN)
  755. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  756. == USB_ENDPOINT_XFER_BULK)
  757. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  758. == USB_ENDPOINT_XFER_INT))) {
  759. dev_dbg(&intf->dev, ": acceptable inbound endpoint "
  760. "found\n");
  761. ep_in = ep;
  762. ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
  763. if (!is_pinnacle)
  764. /*
  765. * Ideally, we'd use what the device offers up,
  766. * but that leads to non-functioning first and
  767. * second-gen devices, and many devices have an
  768. * invalid bInterval of 0. Pinnacle devices
  769. * don't work witha bInterval of 1 though.
  770. */
  771. ep_in->bInterval = 1;
  772. }
  773. if ((ep_out == NULL)
  774. && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  775. == USB_DIR_OUT)
  776. && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  777. == USB_ENDPOINT_XFER_BULK)
  778. || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  779. == USB_ENDPOINT_XFER_INT))) {
  780. dev_dbg(&intf->dev, ": acceptable outbound endpoint "
  781. "found\n");
  782. ep_out = ep;
  783. ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
  784. if (!is_pinnacle)
  785. /*
  786. * Ideally, we'd use what the device offers up,
  787. * but that leads to non-functioning first and
  788. * second-gen devices, and many devices have an
  789. * invalid bInterval of 0. Pinnacle devices
  790. * don't work witha bInterval of 1 though.
  791. */
  792. ep_out->bInterval = 1;
  793. }
  794. }
  795. if (ep_in == NULL) {
  796. dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
  797. return -ENODEV;
  798. }
  799. pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
  800. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  801. ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
  802. if (!ir)
  803. goto mem_alloc_fail;
  804. ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
  805. if (!ir->buf_in)
  806. goto buf_in_alloc_fail;
  807. ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  808. if (!ir->urb_in)
  809. goto urb_in_alloc_fail;
  810. ir->usbdev = dev;
  811. ir->dev = &intf->dev;
  812. ir->len_in = maxp;
  813. ir->flags.gen3 = is_gen3;
  814. ir->flags.microsoft_gen1 = is_microsoft_gen1;
  815. ir->flags.tx_mask_inverted = tx_mask_inverted;
  816. /* Saving usb interface data for use by the transmitter routine */
  817. ir->usb_ep_in = ep_in;
  818. ir->usb_ep_out = ep_out;
  819. if (dev->descriptor.iManufacturer
  820. && usb_string(dev, dev->descriptor.iManufacturer,
  821. buf, sizeof(buf)) > 0)
  822. strlcpy(name, buf, sizeof(name));
  823. if (dev->descriptor.iProduct
  824. && usb_string(dev, dev->descriptor.iProduct,
  825. buf, sizeof(buf)) > 0)
  826. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  827. " %s", buf);
  828. ir->idev = mceusb_init_input_dev(ir);
  829. if (!ir->idev)
  830. goto input_dev_fail;
  831. /* inbound data */
  832. usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
  833. maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
  834. ir->urb_in->transfer_dma = ir->dma_in;
  835. ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  836. if (is_pinnacle) {
  837. /*
  838. * I have no idea why but this reset seems to be crucial to
  839. * getting the device to do outbound IO correctly - without
  840. * this the device seems to hang, ignoring all input - although
  841. * IR signals are correctly sent from the device, no input is
  842. * interpreted by the device and the host never does the
  843. * completion routine
  844. */
  845. ret = usb_reset_configuration(dev);
  846. dev_info(&intf->dev, "usb reset config ret %x\n", ret);
  847. }
  848. /* initialize device */
  849. if (ir->flags.gen3)
  850. mceusb_gen3_init(ir);
  851. else if (ir->flags.microsoft_gen1)
  852. mceusb_gen1_init(ir);
  853. else
  854. mceusb_gen2_init(ir);
  855. mce_sync_in(ir, NULL, maxp);
  856. mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
  857. usb_set_intfdata(intf, ir);
  858. dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
  859. dev->bus->busnum, dev->devnum);
  860. return 0;
  861. /* Error-handling path */
  862. input_dev_fail:
  863. usb_free_urb(ir->urb_in);
  864. urb_in_alloc_fail:
  865. usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
  866. buf_in_alloc_fail:
  867. kfree(ir);
  868. mem_alloc_fail:
  869. dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
  870. return -ENOMEM;
  871. }
  872. static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
  873. {
  874. struct usb_device *dev = interface_to_usbdev(intf);
  875. struct mceusb_dev *ir = usb_get_intfdata(intf);
  876. usb_set_intfdata(intf, NULL);
  877. if (!ir)
  878. return;
  879. ir->usbdev = NULL;
  880. ir_input_unregister(ir->idev);
  881. usb_kill_urb(ir->urb_in);
  882. usb_free_urb(ir->urb_in);
  883. usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
  884. kfree(ir);
  885. }
  886. static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
  887. {
  888. struct mceusb_dev *ir = usb_get_intfdata(intf);
  889. dev_info(ir->dev, "suspend\n");
  890. usb_kill_urb(ir->urb_in);
  891. return 0;
  892. }
  893. static int mceusb_dev_resume(struct usb_interface *intf)
  894. {
  895. struct mceusb_dev *ir = usb_get_intfdata(intf);
  896. dev_info(ir->dev, "resume\n");
  897. if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
  898. return -EIO;
  899. return 0;
  900. }
  901. static struct usb_driver mceusb_dev_driver = {
  902. .name = DRIVER_NAME,
  903. .probe = mceusb_dev_probe,
  904. .disconnect = mceusb_dev_disconnect,
  905. .suspend = mceusb_dev_suspend,
  906. .resume = mceusb_dev_resume,
  907. .reset_resume = mceusb_dev_resume,
  908. .id_table = mceusb_dev_table
  909. };
  910. static int __init mceusb_dev_init(void)
  911. {
  912. int ret;
  913. ret = usb_register(&mceusb_dev_driver);
  914. if (ret < 0)
  915. printk(KERN_ERR DRIVER_NAME
  916. ": usb register failed, result = %d\n", ret);
  917. return ret;
  918. }
  919. static void __exit mceusb_dev_exit(void)
  920. {
  921. usb_deregister(&mceusb_dev_driver);
  922. }
  923. module_init(mceusb_dev_init);
  924. module_exit(mceusb_dev_exit);
  925. MODULE_DESCRIPTION(DRIVER_DESC);
  926. MODULE_AUTHOR(DRIVER_AUTHOR);
  927. MODULE_LICENSE("GPL");
  928. MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
  929. module_param(debug, bool, S_IRUGO | S_IWUSR);
  930. MODULE_PARM_DESC(debug, "Debug enabled or not");