mceusb.c 42 KB

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