mceusb.c 42 KB

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