mceusb.c 32 KB

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