mceusb.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. */
  35. #include <linux/device.h>
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/usb.h>
  40. #include <linux/usb/input.h>
  41. #include <linux/pm_wakeup.h>
  42. #include <media/rc-core.h>
  43. #define DRIVER_VERSION "1.95"
  44. #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
  45. #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
  46. "device driver"
  47. #define DRIVER_NAME "mceusb"
  48. #define USB_TX_TIMEOUT 1000 /* in milliseconds */
  49. #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
  50. #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
  51. /* MCE constants */
  52. #define MCE_IRBUF_SIZE 128 /* TX IR buffer length */
  53. #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
  54. #define MCE_PACKET_SIZE 31 /* Max length of packet (with header) */
  55. #define MCE_IRDATA_HEADER (0x80 + MCE_PACKET_SIZE - 1)
  56. /* Actual format is 0x80 + num_bytes */
  57. #define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
  58. #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
  59. #define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
  60. #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
  61. #define MCE_PULSE_MASK 0x7f /* Pulse mask */
  62. #define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
  63. /*
  64. * The interface between the host and the IR hardware is command-response
  65. * based. All commands and responses have a consistent format, where a lead
  66. * byte always identifies the type of data following it. The lead byte has
  67. * a port value in the 3 highest bits and a length value in the 5 lowest
  68. * bits.
  69. *
  70. * The length field is overloaded, with a value of 11111 indicating that the
  71. * following byte is a command or response code, and the length of the entire
  72. * message is determined by the code. If the length field is not 11111, then
  73. * it specifies the number of bytes of port data that follow.
  74. */
  75. #define MCE_CMD 0x1f
  76. #define MCE_PORT_IR 0x4 /* (0x4 << 5) | MCE_CMD = 0x9f */
  77. #define MCE_PORT_SYS 0x7 /* (0x7 << 5) | MCE_CMD = 0xff */
  78. #define MCE_PORT_SER 0x6 /* 0xc0 thru 0xdf flush & 0x1f bytes */
  79. #define MCE_PORT_MASK 0xe0 /* Mask out command bits */
  80. /* Command port headers */
  81. #define MCE_CMD_PORT_IR 0x9f /* IR-related cmd/rsp */
  82. #define MCE_CMD_PORT_SYS 0xff /* System (non-IR) device cmd/rsp */
  83. /* Commands that set device state (2-4 bytes in length) */
  84. #define MCE_CMD_RESET 0xfe /* Reset device, 2 bytes */
  85. #define MCE_CMD_RESUME 0xaa /* Resume device after error, 2 bytes */
  86. #define MCE_CMD_SETIRCFS 0x06 /* Set tx carrier, 4 bytes */
  87. #define MCE_CMD_SETIRTIMEOUT 0x0c /* Set timeout, 4 bytes */
  88. #define MCE_CMD_SETIRTXPORTS 0x08 /* Set tx ports, 3 bytes */
  89. #define MCE_CMD_SETIRRXPORTEN 0x14 /* Set rx ports, 3 bytes */
  90. #define MCE_CMD_FLASHLED 0x23 /* Flash receiver LED, 2 bytes */
  91. /* Commands that query device state (all 2 bytes, unless noted) */
  92. #define MCE_CMD_GETIRCFS 0x07 /* Get carrier */
  93. #define MCE_CMD_GETIRTIMEOUT 0x0d /* Get timeout */
  94. #define MCE_CMD_GETIRTXPORTS 0x13 /* Get tx ports */
  95. #define MCE_CMD_GETIRRXPORTEN 0x15 /* Get rx ports */
  96. #define MCE_CMD_GETPORTSTATUS 0x11 /* Get tx port status, 3 bytes */
  97. #define MCE_CMD_GETIRNUMPORTS 0x16 /* Get number of ports */
  98. #define MCE_CMD_GETWAKESOURCE 0x17 /* Get wake source */
  99. #define MCE_CMD_GETEMVER 0x22 /* Get emulator interface version */
  100. #define MCE_CMD_GETDEVDETAILS 0x21 /* Get device details (em ver2 only) */
  101. #define MCE_CMD_GETWAKESUPPORT 0x20 /* Get wake details (em ver2 only) */
  102. #define MCE_CMD_GETWAKEVERSION 0x18 /* Get wake pattern (em ver2 only) */
  103. /* Misc commands */
  104. #define MCE_CMD_NOP 0xff /* No operation */
  105. /* Responses to commands (non-error cases) */
  106. #define MCE_RSP_EQIRCFS 0x06 /* tx carrier, 4 bytes */
  107. #define MCE_RSP_EQIRTIMEOUT 0x0c /* rx timeout, 4 bytes */
  108. #define MCE_RSP_GETWAKESOURCE 0x17 /* wake source, 3 bytes */
  109. #define MCE_RSP_EQIRTXPORTS 0x08 /* tx port mask, 3 bytes */
  110. #define MCE_RSP_EQIRRXPORTEN 0x14 /* rx port mask, 3 bytes */
  111. #define MCE_RSP_GETPORTSTATUS 0x11 /* tx port status, 7 bytes */
  112. #define MCE_RSP_EQIRRXCFCNT 0x15 /* rx carrier count, 4 bytes */
  113. #define MCE_RSP_EQIRNUMPORTS 0x16 /* number of ports, 4 bytes */
  114. #define MCE_RSP_EQWAKESUPPORT 0x20 /* wake capabilities, 3 bytes */
  115. #define MCE_RSP_EQWAKEVERSION 0x18 /* wake pattern details, 6 bytes */
  116. #define MCE_RSP_EQDEVDETAILS 0x21 /* device capabilities, 3 bytes */
  117. #define MCE_RSP_EQEMVER 0x22 /* emulator interface ver, 3 bytes */
  118. #define MCE_RSP_FLASHLED 0x23 /* success flashing LED, 2 bytes */
  119. /* Responses to error cases, must send MCE_CMD_RESUME to clear them */
  120. #define MCE_RSP_CMD_ILLEGAL 0xfe /* illegal command for port, 2 bytes */
  121. #define MCE_RSP_TX_TIMEOUT 0x81 /* tx timed out, 2 bytes */
  122. /* Misc commands/responses not defined in the MCE remote/transceiver spec */
  123. #define MCE_CMD_SIG_END 0x01 /* End of signal */
  124. #define MCE_CMD_PING 0x03 /* Ping device */
  125. #define MCE_CMD_UNKNOWN 0x04 /* Unknown */
  126. #define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
  127. #define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
  128. #define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
  129. #define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
  130. #define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
  131. #define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
  132. #define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
  133. #define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
  134. #define MCE_CMD_NULL 0x00 /* These show up various places... */
  135. /* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
  136. * then we're looking at a raw IR data sample */
  137. #define MCE_COMMAND_IRDATA 0x80
  138. #define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
  139. #define VENDOR_PHILIPS 0x0471
  140. #define VENDOR_SMK 0x0609
  141. #define VENDOR_TATUNG 0x1460
  142. #define VENDOR_GATEWAY 0x107b
  143. #define VENDOR_SHUTTLE 0x1308
  144. #define VENDOR_SHUTTLE2 0x051c
  145. #define VENDOR_MITSUMI 0x03ee
  146. #define VENDOR_TOPSEED 0x1784
  147. #define VENDOR_RICAVISION 0x179d
  148. #define VENDOR_ITRON 0x195d
  149. #define VENDOR_FIC 0x1509
  150. #define VENDOR_LG 0x043e
  151. #define VENDOR_MICROSOFT 0x045e
  152. #define VENDOR_FORMOSA 0x147a
  153. #define VENDOR_FINTEK 0x1934
  154. #define VENDOR_PINNACLE 0x2304
  155. #define VENDOR_ECS 0x1019
  156. #define VENDOR_WISTRON 0x0fb8
  157. #define VENDOR_COMPRO 0x185b
  158. #define VENDOR_NORTHSTAR 0x04eb
  159. #define VENDOR_REALTEK 0x0bda
  160. #define VENDOR_TIVO 0x105a
  161. #define VENDOR_CONEXANT 0x0572
  162. #define VENDOR_TWISTEDMELON 0x2596
  163. #define VENDOR_HAUPPAUGE 0x2040
  164. #define VENDOR_PCTV 0x2013
  165. #define VENDOR_ADAPTEC 0x03f3
  166. enum mceusb_model_type {
  167. MCE_GEN2 = 0, /* Most boards */
  168. MCE_GEN1,
  169. MCE_GEN3,
  170. MCE_GEN3_BROKEN_IRTIMEOUT,
  171. MCE_GEN2_TX_INV,
  172. MCE_GEN2_TX_INV_RX_GOOD,
  173. POLARIS_EVK,
  174. CX_HYBRID_TV,
  175. MULTIFUNCTION,
  176. TIVO_KIT,
  177. MCE_GEN2_NO_TX,
  178. HAUPPAUGE_CX_HYBRID_TV,
  179. EVROMEDIA_FULL_HYBRID_FULLHD,
  180. ASTROMETA_T2HYBRID,
  181. };
  182. struct mceusb_model {
  183. u32 mce_gen1:1;
  184. u32 mce_gen2:1;
  185. u32 mce_gen3:1;
  186. u32 tx_mask_normal:1;
  187. u32 no_tx:1;
  188. u32 broken_irtimeout:1;
  189. /*
  190. * 2nd IR receiver (short-range, wideband) for learning mode:
  191. * 0, absent 2nd receiver (rx2)
  192. * 1, rx2 present
  193. * 2, rx2 which under counts IR carrier cycles
  194. */
  195. u32 rx2;
  196. int ir_intfnum;
  197. const char *rc_map; /* Allow specify a per-board map */
  198. const char *name; /* per-board name */
  199. };
  200. static const struct mceusb_model mceusb_model[] = {
  201. [MCE_GEN1] = {
  202. .mce_gen1 = 1,
  203. .tx_mask_normal = 1,
  204. .rx2 = 2,
  205. },
  206. [MCE_GEN2] = {
  207. .mce_gen2 = 1,
  208. .rx2 = 2,
  209. },
  210. [MCE_GEN2_NO_TX] = {
  211. .mce_gen2 = 1,
  212. .no_tx = 1,
  213. },
  214. [MCE_GEN2_TX_INV] = {
  215. .mce_gen2 = 1,
  216. .tx_mask_normal = 1,
  217. .rx2 = 1,
  218. },
  219. [MCE_GEN2_TX_INV_RX_GOOD] = {
  220. .mce_gen2 = 1,
  221. .tx_mask_normal = 1,
  222. .rx2 = 2,
  223. },
  224. [MCE_GEN3] = {
  225. .mce_gen3 = 1,
  226. .tx_mask_normal = 1,
  227. .rx2 = 2,
  228. },
  229. [MCE_GEN3_BROKEN_IRTIMEOUT] = {
  230. .mce_gen3 = 1,
  231. .tx_mask_normal = 1,
  232. .rx2 = 2,
  233. .broken_irtimeout = 1
  234. },
  235. [POLARIS_EVK] = {
  236. /*
  237. * In fact, the EVK is shipped without
  238. * remotes, but we should have something handy,
  239. * to allow testing it
  240. */
  241. .name = "Conexant Hybrid TV (cx231xx) MCE IR",
  242. .rx2 = 2,
  243. },
  244. [CX_HYBRID_TV] = {
  245. .no_tx = 1, /* tx isn't wired up at all */
  246. .name = "Conexant Hybrid TV (cx231xx) MCE IR",
  247. },
  248. [HAUPPAUGE_CX_HYBRID_TV] = {
  249. .no_tx = 1, /* eeprom says it has no tx */
  250. .name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
  251. },
  252. [MULTIFUNCTION] = {
  253. .mce_gen2 = 1,
  254. .ir_intfnum = 2,
  255. .rx2 = 2,
  256. },
  257. [TIVO_KIT] = {
  258. .mce_gen2 = 1,
  259. .rc_map = RC_MAP_TIVO,
  260. .rx2 = 2,
  261. },
  262. [EVROMEDIA_FULL_HYBRID_FULLHD] = {
  263. .name = "Evromedia USB Full Hybrid Full HD",
  264. .no_tx = 1,
  265. .rc_map = RC_MAP_MSI_DIGIVOX_III,
  266. },
  267. [ASTROMETA_T2HYBRID] = {
  268. .name = "Astrometa T2Hybrid",
  269. .no_tx = 1,
  270. .rc_map = RC_MAP_ASTROMETA_T2HYBRID,
  271. }
  272. };
  273. static const struct usb_device_id mceusb_dev_table[] = {
  274. /* Original Microsoft MCE IR Transceiver (often HP-branded) */
  275. { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
  276. .driver_info = MCE_GEN1 },
  277. /* Philips Infrared Transceiver - Sahara branded */
  278. { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
  279. /* Philips Infrared Transceiver - HP branded */
  280. { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
  281. .driver_info = MCE_GEN2_TX_INV },
  282. /* Philips SRM5100 */
  283. { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
  284. /* Philips Infrared Transceiver - Omaura */
  285. { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
  286. /* Philips Infrared Transceiver - Spinel plus */
  287. { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
  288. /* Philips eHome Infrared Transceiver */
  289. { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
  290. /* Philips/Spinel plus IR transceiver for ASUS */
  291. { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
  292. /* Philips/Spinel plus IR transceiver for ASUS */
  293. { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
  294. /* Philips IR transceiver (Dell branded) */
  295. { USB_DEVICE(VENDOR_PHILIPS, 0x2093),
  296. .driver_info = MCE_GEN2_TX_INV },
  297. /* Realtek MCE IR Receiver and card reader */
  298. { USB_DEVICE(VENDOR_REALTEK, 0x0161),
  299. .driver_info = MULTIFUNCTION },
  300. /* SMK/Toshiba G83C0004D410 */
  301. { USB_DEVICE(VENDOR_SMK, 0x031d),
  302. .driver_info = MCE_GEN2_TX_INV_RX_GOOD },
  303. /* SMK eHome Infrared Transceiver (Sony VAIO) */
  304. { USB_DEVICE(VENDOR_SMK, 0x0322),
  305. .driver_info = MCE_GEN2_TX_INV },
  306. /* bundled with Hauppauge PVR-150 */
  307. { USB_DEVICE(VENDOR_SMK, 0x0334),
  308. .driver_info = MCE_GEN2_TX_INV },
  309. /* SMK eHome Infrared Transceiver */
  310. { USB_DEVICE(VENDOR_SMK, 0x0338) },
  311. /* SMK/I-O Data GV-MC7/RCKIT Receiver */
  312. { USB_DEVICE(VENDOR_SMK, 0x0353),
  313. .driver_info = MCE_GEN2_NO_TX },
  314. /* SMK RXX6000 Infrared Receiver */
  315. { USB_DEVICE(VENDOR_SMK, 0x0357),
  316. .driver_info = MCE_GEN2_NO_TX },
  317. /* Tatung eHome Infrared Transceiver */
  318. { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
  319. /* Shuttle eHome Infrared Transceiver */
  320. { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
  321. /* Shuttle eHome Infrared Transceiver */
  322. { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
  323. /* Gateway eHome Infrared Transceiver */
  324. { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
  325. /* Mitsumi */
  326. { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
  327. /* Topseed eHome Infrared Transceiver */
  328. { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
  329. .driver_info = MCE_GEN2_TX_INV },
  330. /* Topseed HP eHome Infrared Transceiver */
  331. { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
  332. .driver_info = MCE_GEN2_TX_INV },
  333. /* Topseed eHome Infrared Transceiver */
  334. { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
  335. .driver_info = MCE_GEN2_TX_INV },
  336. /* Topseed eHome Infrared Transceiver */
  337. { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
  338. .driver_info = MCE_GEN3 },
  339. /* Topseed eHome Infrared Transceiver */
  340. { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
  341. .driver_info = MCE_GEN2_TX_INV },
  342. /* Topseed eHome Infrared Transceiver */
  343. { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
  344. .driver_info = MCE_GEN3_BROKEN_IRTIMEOUT },
  345. /* Ricavision internal Infrared Transceiver */
  346. { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
  347. /* Itron ione Libra Q-11 */
  348. { USB_DEVICE(VENDOR_ITRON, 0x7002) },
  349. /* FIC eHome Infrared Transceiver */
  350. { USB_DEVICE(VENDOR_FIC, 0x9242) },
  351. /* LG eHome Infrared Transceiver */
  352. { USB_DEVICE(VENDOR_LG, 0x9803) },
  353. /* Microsoft MCE Infrared Transceiver */
  354. { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
  355. /* Formosa eHome Infrared Transceiver */
  356. { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
  357. /* Formosa21 / eHome Infrared Receiver */
  358. { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
  359. /* Formosa aim / Trust MCE Infrared Receiver */
  360. { USB_DEVICE(VENDOR_FORMOSA, 0xe017),
  361. .driver_info = MCE_GEN2_NO_TX },
  362. /* Formosa Industrial Computing / Beanbag Emulation Device */
  363. { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
  364. /* Formosa21 / eHome Infrared Receiver */
  365. { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
  366. /* Formosa Industrial Computing AIM IR605/A */
  367. { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
  368. /* Formosa Industrial Computing */
  369. { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
  370. /* Formosa Industrial Computing */
  371. { USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
  372. /* Fintek eHome Infrared Transceiver (HP branded) */
  373. { USB_DEVICE(VENDOR_FINTEK, 0x5168),
  374. .driver_info = MCE_GEN2_TX_INV },
  375. /* Fintek eHome Infrared Transceiver */
  376. { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
  377. /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
  378. { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
  379. /* Pinnacle Remote Kit */
  380. { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
  381. .driver_info = MCE_GEN3 },
  382. /* Elitegroup Computer Systems IR */
  383. { USB_DEVICE(VENDOR_ECS, 0x0f38) },
  384. /* Wistron Corp. eHome Infrared Receiver */
  385. { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
  386. /* Compro K100 */
  387. { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
  388. /* Compro K100 v2 */
  389. { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
  390. /* Northstar Systems, Inc. eHome Infrared Transceiver */
  391. { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
  392. /* TiVo PC IR Receiver */
  393. { USB_DEVICE(VENDOR_TIVO, 0x2000),
  394. .driver_info = TIVO_KIT },
  395. /* Conexant Hybrid TV "Shelby" Polaris SDK */
  396. { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
  397. .driver_info = POLARIS_EVK },
  398. /* Conexant Hybrid TV RDU253S Polaris */
  399. { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
  400. .driver_info = CX_HYBRID_TV },
  401. /* Twisted Melon Inc. - Manta Mini Receiver */
  402. { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
  403. /* Twisted Melon Inc. - Manta Pico Receiver */
  404. { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
  405. /* Twisted Melon Inc. - Manta Transceiver */
  406. { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
  407. /* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
  408. { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
  409. .driver_info = HAUPPAUGE_CX_HYBRID_TV },
  410. { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
  411. .driver_info = HAUPPAUGE_CX_HYBRID_TV },
  412. { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
  413. .driver_info = HAUPPAUGE_CX_HYBRID_TV },
  414. { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
  415. .driver_info = HAUPPAUGE_CX_HYBRID_TV },
  416. { USB_DEVICE(VENDOR_PCTV, 0x0259),
  417. .driver_info = HAUPPAUGE_CX_HYBRID_TV },
  418. { USB_DEVICE(VENDOR_PCTV, 0x025e),
  419. .driver_info = HAUPPAUGE_CX_HYBRID_TV },
  420. /* Adaptec / HP eHome Receiver */
  421. { USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
  422. /* Evromedia USB Full Hybrid Full HD */
  423. { USB_DEVICE(0x1b80, 0xd3b2),
  424. .driver_info = EVROMEDIA_FULL_HYBRID_FULLHD },
  425. /* Astrometa T2hybrid */
  426. { USB_DEVICE(0x15f4, 0x0135),
  427. .driver_info = ASTROMETA_T2HYBRID },
  428. /* Terminating entry */
  429. { }
  430. };
  431. /* data structure for each usb transceiver */
  432. struct mceusb_dev {
  433. /* ir-core bits */
  434. struct rc_dev *rc;
  435. /* optional features we can enable */
  436. bool carrier_report_enabled;
  437. bool wideband_rx_enabled; /* aka learning mode, short-range rx */
  438. /* core device bits */
  439. struct device *dev;
  440. /* usb */
  441. struct usb_device *usbdev;
  442. struct urb *urb_in;
  443. unsigned int pipe_in;
  444. struct usb_endpoint_descriptor *usb_ep_out;
  445. unsigned int pipe_out;
  446. /* buffers and dma */
  447. unsigned char *buf_in;
  448. unsigned int len_in;
  449. dma_addr_t dma_in;
  450. enum {
  451. CMD_HEADER = 0,
  452. SUBCMD,
  453. CMD_DATA,
  454. PARSE_IRDATA,
  455. } parser_state;
  456. u8 cmd, rem; /* Remaining IR data bytes in packet */
  457. struct {
  458. u32 connected:1;
  459. u32 tx_mask_normal:1;
  460. u32 microsoft_gen1:1;
  461. u32 no_tx:1;
  462. u32 rx2;
  463. } flags;
  464. /* transmit support */
  465. u32 carrier;
  466. unsigned char tx_mask;
  467. char name[128];
  468. char phys[64];
  469. enum mceusb_model_type model;
  470. bool need_reset; /* flag to issue a device resume cmd */
  471. u8 emver; /* emulator interface version */
  472. u8 num_txports; /* number of transmit ports */
  473. u8 num_rxports; /* number of receive sensors */
  474. u8 txports_cabled; /* bitmask of transmitters with cable */
  475. u8 rxports_active; /* bitmask of active receive sensors */
  476. bool learning_active; /* wideband rx is active */
  477. /* receiver carrier frequency detection support */
  478. u32 pulse_tunit; /* IR pulse "on" cumulative time units */
  479. u32 pulse_count; /* pulse "on" count in measurement interval */
  480. /*
  481. * support for async error handler mceusb_deferred_kevent()
  482. * where usb_clear_halt(), usb_reset_configuration(),
  483. * usb_reset_device(), etc. must be done in process context
  484. */
  485. struct work_struct kevent;
  486. unsigned long kevent_flags;
  487. # define EVENT_TX_HALT 0
  488. # define EVENT_RX_HALT 1
  489. };
  490. /* MCE Device Command Strings, generally a port and command pair */
  491. static char DEVICE_RESUME[] = {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
  492. MCE_CMD_RESUME};
  493. static char GET_REVISION[] = {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
  494. static char GET_EMVER[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
  495. static char GET_WAKEVERSION[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
  496. static char FLASH_LED[] = {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
  497. static char GET_UNKNOWN2[] = {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
  498. static char GET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
  499. static char GET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
  500. static char GET_NUM_PORTS[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
  501. static char GET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
  502. static char GET_RX_SENSOR[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
  503. /* sub in desired values in lower byte or bytes for full command */
  504. /* FIXME: make use of these for transmit.
  505. static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR,
  506. MCE_CMD_SETIRCFS, 0x00, 0x00};
  507. static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
  508. static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR,
  509. MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
  510. static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR,
  511. MCE_RSP_EQIRRXPORTEN, 0x00};
  512. */
  513. static int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
  514. {
  515. int datasize = 0;
  516. switch (cmd) {
  517. case MCE_CMD_NULL:
  518. if (subcmd == MCE_CMD_PORT_SYS)
  519. datasize = 1;
  520. break;
  521. case MCE_CMD_PORT_SYS:
  522. switch (subcmd) {
  523. case MCE_RSP_GETPORTSTATUS:
  524. datasize = 5;
  525. break;
  526. case MCE_RSP_EQWAKEVERSION:
  527. datasize = 4;
  528. break;
  529. case MCE_CMD_G_REVISION:
  530. datasize = 2;
  531. break;
  532. case MCE_RSP_EQWAKESUPPORT:
  533. case MCE_RSP_GETWAKESOURCE:
  534. case MCE_RSP_EQDEVDETAILS:
  535. case MCE_RSP_EQEMVER:
  536. datasize = 1;
  537. break;
  538. }
  539. break;
  540. case MCE_CMD_PORT_IR:
  541. switch (subcmd) {
  542. case MCE_CMD_UNKNOWN:
  543. case MCE_RSP_EQIRCFS:
  544. case MCE_RSP_EQIRTIMEOUT:
  545. case MCE_RSP_EQIRRXCFCNT:
  546. case MCE_RSP_EQIRNUMPORTS:
  547. datasize = 2;
  548. break;
  549. case MCE_CMD_SIG_END:
  550. case MCE_RSP_EQIRTXPORTS:
  551. case MCE_RSP_EQIRRXPORTEN:
  552. datasize = 1;
  553. break;
  554. }
  555. }
  556. return datasize;
  557. }
  558. static void mceusb_dev_printdata(struct mceusb_dev *ir, u8 *buf, int buf_len,
  559. int offset, int len, bool out)
  560. {
  561. #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
  562. char *inout;
  563. u8 cmd, subcmd, *data;
  564. struct device *dev = ir->dev;
  565. int start, skip = 0;
  566. u32 carrier, period;
  567. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  568. if (ir->flags.microsoft_gen1 && !out && !offset)
  569. skip = 2;
  570. if (len <= skip)
  571. return;
  572. dev_dbg(dev, "%cx data[%d]: %*ph (len=%d sz=%d)",
  573. (out ? 't' : 'r'), offset,
  574. min(len, buf_len - offset), buf + offset, len, buf_len);
  575. inout = out ? "Request" : "Got";
  576. start = offset + skip;
  577. cmd = buf[start] & 0xff;
  578. subcmd = buf[start + 1] & 0xff;
  579. data = buf + start + 2;
  580. switch (cmd) {
  581. case MCE_CMD_NULL:
  582. if (subcmd == MCE_CMD_NULL)
  583. break;
  584. if ((subcmd == MCE_CMD_PORT_SYS) &&
  585. (data[0] == MCE_CMD_RESUME))
  586. dev_dbg(dev, "Device resume requested");
  587. else
  588. dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
  589. cmd, subcmd);
  590. break;
  591. case MCE_CMD_PORT_SYS:
  592. switch (subcmd) {
  593. case MCE_RSP_EQEMVER:
  594. if (!out)
  595. dev_dbg(dev, "Emulator interface version %x",
  596. data[0]);
  597. break;
  598. case MCE_CMD_G_REVISION:
  599. if (len == 2)
  600. dev_dbg(dev, "Get hw/sw rev?");
  601. else
  602. dev_dbg(dev, "hw/sw rev %*ph",
  603. 4, &buf[start + 2]);
  604. break;
  605. case MCE_CMD_RESUME:
  606. dev_dbg(dev, "Device resume requested");
  607. break;
  608. case MCE_RSP_CMD_ILLEGAL:
  609. dev_dbg(dev, "Illegal PORT_SYS command");
  610. break;
  611. case MCE_RSP_EQWAKEVERSION:
  612. if (!out)
  613. dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
  614. data[0], data[1], data[2], data[3]);
  615. break;
  616. case MCE_RSP_GETPORTSTATUS:
  617. if (!out)
  618. /* We use data1 + 1 here, to match hw labels */
  619. dev_dbg(dev, "TX port %d: blaster is%s connected",
  620. data[0] + 1, data[3] ? " not" : "");
  621. break;
  622. case MCE_CMD_FLASHLED:
  623. dev_dbg(dev, "Attempting to flash LED");
  624. break;
  625. default:
  626. dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
  627. cmd, subcmd);
  628. break;
  629. }
  630. break;
  631. case MCE_CMD_PORT_IR:
  632. switch (subcmd) {
  633. case MCE_CMD_SIG_END:
  634. dev_dbg(dev, "End of signal");
  635. break;
  636. case MCE_CMD_PING:
  637. dev_dbg(dev, "Ping");
  638. break;
  639. case MCE_CMD_UNKNOWN:
  640. dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
  641. data[0], data[1]);
  642. break;
  643. case MCE_RSP_EQIRCFS:
  644. period = DIV_ROUND_CLOSEST((1U << data[0] * 2) *
  645. (data[1] + 1), 10);
  646. if (!period)
  647. break;
  648. carrier = (1000 * 1000) / period;
  649. dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
  650. inout, carrier, period);
  651. break;
  652. case MCE_CMD_GETIRCFS:
  653. dev_dbg(dev, "Get carrier mode and freq");
  654. break;
  655. case MCE_RSP_EQIRTXPORTS:
  656. dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
  657. inout, data[0]);
  658. break;
  659. case MCE_RSP_EQIRTIMEOUT:
  660. /* value is in units of 50us, so x*50/1000 ms */
  661. period = ((data[0] << 8) | data[1]) *
  662. MCE_TIME_UNIT / 1000;
  663. dev_dbg(dev, "%s receive timeout of %d ms",
  664. inout, period);
  665. break;
  666. case MCE_CMD_GETIRTIMEOUT:
  667. dev_dbg(dev, "Get receive timeout");
  668. break;
  669. case MCE_CMD_GETIRTXPORTS:
  670. dev_dbg(dev, "Get transmit blaster mask");
  671. break;
  672. case MCE_RSP_EQIRRXPORTEN:
  673. dev_dbg(dev, "%s %s-range receive sensor in use",
  674. inout, data[0] == 0x02 ? "short" : "long");
  675. break;
  676. case MCE_CMD_GETIRRXPORTEN:
  677. /* aka MCE_RSP_EQIRRXCFCNT */
  678. if (out)
  679. dev_dbg(dev, "Get receive sensor");
  680. else
  681. dev_dbg(dev, "RX carrier cycle count: %d",
  682. ((data[0] << 8) | data[1]));
  683. break;
  684. case MCE_RSP_EQIRNUMPORTS:
  685. if (out)
  686. break;
  687. dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
  688. data[0], data[1]);
  689. break;
  690. case MCE_RSP_CMD_ILLEGAL:
  691. dev_dbg(dev, "Illegal PORT_IR command");
  692. break;
  693. case MCE_RSP_TX_TIMEOUT:
  694. dev_dbg(dev, "IR TX timeout (TX buffer underrun)");
  695. break;
  696. default:
  697. dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
  698. cmd, subcmd);
  699. break;
  700. }
  701. break;
  702. default:
  703. break;
  704. }
  705. if (cmd == MCE_IRDATA_TRAILER)
  706. dev_dbg(dev, "End of raw IR data");
  707. else if ((cmd != MCE_CMD_PORT_IR) &&
  708. ((cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA))
  709. dev_dbg(dev, "Raw IR data, %d pulse/space samples",
  710. cmd & MCE_PACKET_LENGTH_MASK);
  711. #endif
  712. }
  713. /*
  714. * Schedule work that can't be done in interrupt handlers
  715. * (mceusb_dev_recv() and mce_write_callback()) nor tasklets.
  716. * Invokes mceusb_deferred_kevent() for recovering from
  717. * error events specified by the kevent bit field.
  718. */
  719. static void mceusb_defer_kevent(struct mceusb_dev *ir, int kevent)
  720. {
  721. set_bit(kevent, &ir->kevent_flags);
  722. if (!schedule_work(&ir->kevent))
  723. dev_err(ir->dev, "kevent %d may have been dropped", kevent);
  724. else
  725. dev_dbg(ir->dev, "kevent %d scheduled", kevent);
  726. }
  727. static void mce_write_callback(struct urb *urb)
  728. {
  729. if (!urb)
  730. return;
  731. complete(urb->context);
  732. }
  733. /*
  734. * Write (TX/send) data to MCE device USB endpoint out.
  735. * Used for IR blaster TX and MCE device commands.
  736. *
  737. * Return: The number of bytes written (> 0) or errno (< 0).
  738. */
  739. static int mce_write(struct mceusb_dev *ir, u8 *data, int size)
  740. {
  741. int ret;
  742. struct urb *urb;
  743. struct device *dev = ir->dev;
  744. unsigned char *buf_out;
  745. struct completion tx_done;
  746. unsigned long expire;
  747. unsigned long ret_wait;
  748. mceusb_dev_printdata(ir, data, size, 0, size, true);
  749. urb = usb_alloc_urb(0, GFP_KERNEL);
  750. if (unlikely(!urb)) {
  751. dev_err(dev, "Error: mce write couldn't allocate urb");
  752. return -ENOMEM;
  753. }
  754. buf_out = kmalloc(size, GFP_KERNEL);
  755. if (!buf_out) {
  756. usb_free_urb(urb);
  757. return -ENOMEM;
  758. }
  759. init_completion(&tx_done);
  760. /* outbound data */
  761. if (usb_endpoint_xfer_int(ir->usb_ep_out))
  762. usb_fill_int_urb(urb, ir->usbdev, ir->pipe_out,
  763. buf_out, size, mce_write_callback, &tx_done,
  764. ir->usb_ep_out->bInterval);
  765. else
  766. usb_fill_bulk_urb(urb, ir->usbdev, ir->pipe_out,
  767. buf_out, size, mce_write_callback, &tx_done);
  768. memcpy(buf_out, data, size);
  769. ret = usb_submit_urb(urb, GFP_KERNEL);
  770. if (ret) {
  771. dev_err(dev, "Error: mce write submit urb error = %d", ret);
  772. kfree(buf_out);
  773. usb_free_urb(urb);
  774. return ret;
  775. }
  776. expire = msecs_to_jiffies(USB_TX_TIMEOUT);
  777. ret_wait = wait_for_completion_timeout(&tx_done, expire);
  778. if (!ret_wait) {
  779. dev_err(dev, "Error: mce write timed out (expire = %lu (%dms))",
  780. expire, USB_TX_TIMEOUT);
  781. usb_kill_urb(urb);
  782. ret = (urb->status == -ENOENT ? -ETIMEDOUT : urb->status);
  783. } else {
  784. ret = urb->status;
  785. }
  786. if (ret >= 0)
  787. ret = urb->actual_length; /* bytes written */
  788. switch (urb->status) {
  789. /* success */
  790. case 0:
  791. break;
  792. case -ECONNRESET:
  793. case -ENOENT:
  794. case -EILSEQ:
  795. case -ESHUTDOWN:
  796. break;
  797. case -EPIPE:
  798. dev_err(ir->dev, "Error: mce write urb status = %d (TX HALT)",
  799. urb->status);
  800. mceusb_defer_kevent(ir, EVENT_TX_HALT);
  801. break;
  802. default:
  803. dev_err(ir->dev, "Error: mce write urb status = %d",
  804. urb->status);
  805. break;
  806. }
  807. dev_dbg(dev, "tx done status = %d (wait = %lu, expire = %lu (%dms), urb->actual_length = %d, urb->status = %d)",
  808. ret, ret_wait, expire, USB_TX_TIMEOUT,
  809. urb->actual_length, urb->status);
  810. kfree(buf_out);
  811. usb_free_urb(urb);
  812. return ret;
  813. }
  814. static void mce_command_out(struct mceusb_dev *ir, u8 *data, int size)
  815. {
  816. int rsize = sizeof(DEVICE_RESUME);
  817. if (ir->need_reset) {
  818. ir->need_reset = false;
  819. mce_write(ir, DEVICE_RESUME, rsize);
  820. msleep(10);
  821. }
  822. mce_write(ir, data, size);
  823. msleep(10);
  824. }
  825. /*
  826. * Transmit IR out the MCE device IR blaster port(s).
  827. *
  828. * Convert IR pulse/space sequence from LIRC to MCE format.
  829. * Break up a long IR sequence into multiple parts (MCE IR data packets).
  830. *
  831. * u32 txbuf[] consists of IR pulse, space, ..., and pulse times in usec.
  832. * Pulses and spaces are implicit by their position.
  833. * The first IR sample, txbuf[0], is always a pulse.
  834. *
  835. * u8 irbuf[] consists of multiple IR data packets for the MCE device.
  836. * A packet is 1 u8 MCE_IRDATA_HEADER and up to 30 u8 IR samples.
  837. * An IR sample is 1-bit pulse/space flag with 7-bit time
  838. * in MCE time units (50usec).
  839. *
  840. * Return: The number of IR samples sent (> 0) or errno (< 0).
  841. */
  842. static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
  843. {
  844. struct mceusb_dev *ir = dev->priv;
  845. u8 cmdbuf[3] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00 };
  846. u8 irbuf[MCE_IRBUF_SIZE];
  847. int ircount = 0;
  848. unsigned int irsample;
  849. int i, length, ret;
  850. /* Send the set TX ports command */
  851. cmdbuf[2] = ir->tx_mask;
  852. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  853. /* Generate mce IR data packet */
  854. for (i = 0; i < count; i++) {
  855. irsample = txbuf[i] / MCE_TIME_UNIT;
  856. /* loop to support long pulses/spaces > 6350us (127*50us) */
  857. while (irsample > 0) {
  858. /* Insert IR header every 30th entry */
  859. if (ircount % MCE_PACKET_SIZE == 0) {
  860. /* Room for IR header and one IR sample? */
  861. if (ircount >= MCE_IRBUF_SIZE - 1) {
  862. /* Send near full buffer */
  863. ret = mce_write(ir, irbuf, ircount);
  864. if (ret < 0)
  865. return ret;
  866. ircount = 0;
  867. }
  868. irbuf[ircount++] = MCE_IRDATA_HEADER;
  869. }
  870. /* Insert IR sample */
  871. if (irsample <= MCE_MAX_PULSE_LENGTH) {
  872. irbuf[ircount] = irsample;
  873. irsample = 0;
  874. } else {
  875. irbuf[ircount] = MCE_MAX_PULSE_LENGTH;
  876. irsample -= MCE_MAX_PULSE_LENGTH;
  877. }
  878. /*
  879. * Even i = IR pulse
  880. * Odd i = IR space
  881. */
  882. irbuf[ircount] |= (i & 1 ? 0 : MCE_PULSE_BIT);
  883. ircount++;
  884. /* IR buffer full? */
  885. if (ircount >= MCE_IRBUF_SIZE) {
  886. /* Fix packet length in last header */
  887. length = ircount % MCE_PACKET_SIZE;
  888. if (length > 0)
  889. irbuf[ircount - length] -=
  890. MCE_PACKET_SIZE - length;
  891. /* Send full buffer */
  892. ret = mce_write(ir, irbuf, ircount);
  893. if (ret < 0)
  894. return ret;
  895. ircount = 0;
  896. }
  897. }
  898. } /* after for loop, 0 <= ircount < MCE_IRBUF_SIZE */
  899. /* Fix packet length in last header */
  900. length = ircount % MCE_PACKET_SIZE;
  901. if (length > 0)
  902. irbuf[ircount - length] -= MCE_PACKET_SIZE - length;
  903. /* Append IR trailer (0x80) to final partial (or empty) IR buffer */
  904. irbuf[ircount++] = MCE_IRDATA_TRAILER;
  905. /* Send final buffer */
  906. ret = mce_write(ir, irbuf, ircount);
  907. if (ret < 0)
  908. return ret;
  909. return count;
  910. }
  911. /* Sets active IR outputs -- mce devices typically have two */
  912. static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
  913. {
  914. struct mceusb_dev *ir = dev->priv;
  915. /* return number of transmitters */
  916. int emitters = ir->num_txports ? ir->num_txports : 2;
  917. if (mask >= (1 << emitters))
  918. return emitters;
  919. if (ir->flags.tx_mask_normal)
  920. ir->tx_mask = mask;
  921. else
  922. ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
  923. mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
  924. return 0;
  925. }
  926. /* Sets the send carrier frequency and mode */
  927. static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  928. {
  929. struct mceusb_dev *ir = dev->priv;
  930. int clk = 10000000;
  931. int prescaler = 0, divisor = 0;
  932. unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
  933. MCE_CMD_SETIRCFS, 0x00, 0x00 };
  934. /* Carrier has changed */
  935. if (ir->carrier != carrier) {
  936. if (carrier == 0) {
  937. ir->carrier = carrier;
  938. cmdbuf[2] = MCE_CMD_SIG_END;
  939. cmdbuf[3] = MCE_IRDATA_TRAILER;
  940. dev_dbg(ir->dev, "disabling carrier modulation");
  941. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  942. return 0;
  943. }
  944. for (prescaler = 0; prescaler < 4; ++prescaler) {
  945. divisor = (clk >> (2 * prescaler)) / carrier;
  946. if (divisor <= 0xff) {
  947. ir->carrier = carrier;
  948. cmdbuf[2] = prescaler;
  949. cmdbuf[3] = divisor;
  950. dev_dbg(ir->dev, "requesting %u HZ carrier",
  951. carrier);
  952. /* Transmit new carrier to mce device */
  953. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  954. return 0;
  955. }
  956. }
  957. return -EINVAL;
  958. }
  959. return 0;
  960. }
  961. static int mceusb_set_timeout(struct rc_dev *dev, unsigned int timeout)
  962. {
  963. u8 cmdbuf[4] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTIMEOUT, 0, 0 };
  964. struct mceusb_dev *ir = dev->priv;
  965. unsigned int units;
  966. units = DIV_ROUND_CLOSEST(timeout, US_TO_NS(MCE_TIME_UNIT));
  967. cmdbuf[2] = units >> 8;
  968. cmdbuf[3] = units;
  969. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  970. /* get receiver timeout value */
  971. mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
  972. return 0;
  973. }
  974. /*
  975. * Select or deselect the 2nd receiver port.
  976. * Second receiver is learning mode, wide-band, short-range receiver.
  977. * Only one receiver (long or short range) may be active at a time.
  978. */
  979. static int mceusb_set_rx_wideband(struct rc_dev *dev, int enable)
  980. {
  981. struct mceusb_dev *ir = dev->priv;
  982. unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
  983. MCE_CMD_SETIRRXPORTEN, 0x00 };
  984. dev_dbg(ir->dev, "select %s-range receive sensor",
  985. enable ? "short" : "long");
  986. if (enable) {
  987. ir->wideband_rx_enabled = true;
  988. cmdbuf[2] = 2; /* port 2 is short range receiver */
  989. } else {
  990. ir->wideband_rx_enabled = false;
  991. cmdbuf[2] = 1; /* port 1 is long range receiver */
  992. }
  993. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  994. /* response from device sets ir->learning_active */
  995. return 0;
  996. }
  997. /*
  998. * Enable/disable receiver carrier frequency pass through reporting.
  999. * Only the short-range receiver has carrier frequency measuring capability.
  1000. * Implicitly select this receiver when enabling carrier frequency reporting.
  1001. */
  1002. static int mceusb_set_rx_carrier_report(struct rc_dev *dev, int enable)
  1003. {
  1004. struct mceusb_dev *ir = dev->priv;
  1005. unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
  1006. MCE_CMD_SETIRRXPORTEN, 0x00 };
  1007. dev_dbg(ir->dev, "%s short-range receiver carrier reporting",
  1008. enable ? "enable" : "disable");
  1009. if (enable) {
  1010. ir->carrier_report_enabled = true;
  1011. if (!ir->learning_active) {
  1012. cmdbuf[2] = 2; /* port 2 is short range receiver */
  1013. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  1014. }
  1015. } else {
  1016. ir->carrier_report_enabled = false;
  1017. /*
  1018. * Revert to normal (long-range) receiver only if the
  1019. * wideband (short-range) receiver wasn't explicitly
  1020. * enabled.
  1021. */
  1022. if (ir->learning_active && !ir->wideband_rx_enabled) {
  1023. cmdbuf[2] = 1; /* port 1 is long range receiver */
  1024. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  1025. }
  1026. }
  1027. return 0;
  1028. }
  1029. /*
  1030. * We don't do anything but print debug spew for many of the command bits
  1031. * we receive from the hardware, but some of them are useful information
  1032. * we want to store so that we can use them.
  1033. */
  1034. static void mceusb_handle_command(struct mceusb_dev *ir, int index)
  1035. {
  1036. DEFINE_IR_RAW_EVENT(rawir);
  1037. u8 hi = ir->buf_in[index + 1] & 0xff;
  1038. u8 lo = ir->buf_in[index + 2] & 0xff;
  1039. u32 carrier_cycles;
  1040. u32 cycles_fix;
  1041. switch (ir->buf_in[index]) {
  1042. /* the one and only 5-byte return value command */
  1043. case MCE_RSP_GETPORTSTATUS:
  1044. if ((ir->buf_in[index + 4] & 0xff) == 0x00)
  1045. ir->txports_cabled |= 1 << hi;
  1046. break;
  1047. /* 2-byte return value commands */
  1048. case MCE_RSP_EQIRTIMEOUT:
  1049. ir->rc->timeout = US_TO_NS((hi << 8 | lo) * MCE_TIME_UNIT);
  1050. break;
  1051. case MCE_RSP_EQIRNUMPORTS:
  1052. ir->num_txports = hi;
  1053. ir->num_rxports = lo;
  1054. break;
  1055. case MCE_RSP_EQIRRXCFCNT:
  1056. /*
  1057. * The carrier cycle counter can overflow and wrap around
  1058. * without notice from the device. So frequency measurement
  1059. * will be inaccurate with long duration IR.
  1060. *
  1061. * The long-range (non learning) receiver always reports
  1062. * zero count so we always ignore its report.
  1063. */
  1064. if (ir->carrier_report_enabled && ir->learning_active &&
  1065. ir->pulse_tunit > 0) {
  1066. carrier_cycles = (hi << 8 | lo);
  1067. /*
  1068. * Adjust carrier cycle count by adding
  1069. * 1 missed count per pulse "on"
  1070. */
  1071. cycles_fix = ir->flags.rx2 == 2 ? ir->pulse_count : 0;
  1072. rawir.carrier_report = 1;
  1073. rawir.carrier = (1000000u / MCE_TIME_UNIT) *
  1074. (carrier_cycles + cycles_fix) /
  1075. ir->pulse_tunit;
  1076. dev_dbg(ir->dev, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
  1077. rawir.carrier, ir->pulse_count, carrier_cycles,
  1078. ir->pulse_tunit, ir->flags.rx2);
  1079. ir_raw_event_store(ir->rc, &rawir);
  1080. }
  1081. break;
  1082. /* 1-byte return value commands */
  1083. case MCE_RSP_EQEMVER:
  1084. ir->emver = hi;
  1085. break;
  1086. case MCE_RSP_EQIRTXPORTS:
  1087. ir->tx_mask = hi;
  1088. break;
  1089. case MCE_RSP_EQIRRXPORTEN:
  1090. ir->learning_active = ((hi & 0x02) == 0x02);
  1091. if (ir->rxports_active != hi) {
  1092. dev_info(ir->dev, "%s-range (0x%x) receiver active",
  1093. ir->learning_active ? "short" : "long", hi);
  1094. ir->rxports_active = hi;
  1095. }
  1096. break;
  1097. case MCE_RSP_CMD_ILLEGAL:
  1098. case MCE_RSP_TX_TIMEOUT:
  1099. ir->need_reset = true;
  1100. break;
  1101. default:
  1102. break;
  1103. }
  1104. }
  1105. static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
  1106. {
  1107. DEFINE_IR_RAW_EVENT(rawir);
  1108. bool event = false;
  1109. int i = 0;
  1110. /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
  1111. if (ir->flags.microsoft_gen1)
  1112. i = 2;
  1113. /* if there's no data, just return now */
  1114. if (buf_len <= i)
  1115. return;
  1116. for (; i < buf_len; i++) {
  1117. switch (ir->parser_state) {
  1118. case SUBCMD:
  1119. ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
  1120. mceusb_dev_printdata(ir, ir->buf_in, buf_len, i - 1,
  1121. ir->rem + 2, false);
  1122. mceusb_handle_command(ir, i);
  1123. ir->parser_state = CMD_DATA;
  1124. break;
  1125. case PARSE_IRDATA:
  1126. ir->rem--;
  1127. init_ir_raw_event(&rawir);
  1128. rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
  1129. rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK);
  1130. if (unlikely(!rawir.duration)) {
  1131. dev_warn(ir->dev, "nonsensical irdata %02x with duration 0",
  1132. ir->buf_in[i]);
  1133. break;
  1134. }
  1135. if (rawir.pulse) {
  1136. ir->pulse_tunit += rawir.duration;
  1137. ir->pulse_count++;
  1138. }
  1139. rawir.duration *= US_TO_NS(MCE_TIME_UNIT);
  1140. dev_dbg(ir->dev, "Storing %s %u ns (%02x)",
  1141. rawir.pulse ? "pulse" : "space",
  1142. rawir.duration, ir->buf_in[i]);
  1143. if (ir_raw_event_store_with_filter(ir->rc, &rawir))
  1144. event = true;
  1145. break;
  1146. case CMD_DATA:
  1147. ir->rem--;
  1148. break;
  1149. case CMD_HEADER:
  1150. /* decode mce packets of the form (84),AA,BB,CC,DD */
  1151. /* IR data packets can span USB messages - rem */
  1152. ir->cmd = ir->buf_in[i];
  1153. if ((ir->cmd == MCE_CMD_PORT_IR) ||
  1154. ((ir->cmd & MCE_PORT_MASK) !=
  1155. MCE_COMMAND_IRDATA)) {
  1156. ir->parser_state = SUBCMD;
  1157. continue;
  1158. }
  1159. ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
  1160. mceusb_dev_printdata(ir, ir->buf_in, buf_len,
  1161. i, ir->rem + 1, false);
  1162. if (ir->rem) {
  1163. ir->parser_state = PARSE_IRDATA;
  1164. } else {
  1165. init_ir_raw_event(&rawir);
  1166. rawir.timeout = 1;
  1167. rawir.duration = ir->rc->timeout;
  1168. if (ir_raw_event_store_with_filter(ir->rc,
  1169. &rawir))
  1170. event = true;
  1171. ir->pulse_tunit = 0;
  1172. ir->pulse_count = 0;
  1173. }
  1174. break;
  1175. }
  1176. if (ir->parser_state != CMD_HEADER && !ir->rem)
  1177. ir->parser_state = CMD_HEADER;
  1178. }
  1179. if (event) {
  1180. dev_dbg(ir->dev, "processed IR data");
  1181. ir_raw_event_handle(ir->rc);
  1182. }
  1183. }
  1184. static void mceusb_dev_recv(struct urb *urb)
  1185. {
  1186. struct mceusb_dev *ir;
  1187. if (!urb)
  1188. return;
  1189. ir = urb->context;
  1190. if (!ir) {
  1191. usb_unlink_urb(urb);
  1192. return;
  1193. }
  1194. switch (urb->status) {
  1195. /* success */
  1196. case 0:
  1197. mceusb_process_ir_data(ir, urb->actual_length);
  1198. break;
  1199. case -ECONNRESET:
  1200. case -ENOENT:
  1201. case -EILSEQ:
  1202. case -ESHUTDOWN:
  1203. usb_unlink_urb(urb);
  1204. return;
  1205. case -EPIPE:
  1206. dev_err(ir->dev, "Error: urb status = %d (RX HALT)",
  1207. urb->status);
  1208. mceusb_defer_kevent(ir, EVENT_RX_HALT);
  1209. return;
  1210. default:
  1211. dev_err(ir->dev, "Error: urb status = %d", urb->status);
  1212. break;
  1213. }
  1214. usb_submit_urb(urb, GFP_ATOMIC);
  1215. }
  1216. static void mceusb_get_emulator_version(struct mceusb_dev *ir)
  1217. {
  1218. /* If we get no reply or an illegal command reply, its ver 1, says MS */
  1219. ir->emver = 1;
  1220. mce_command_out(ir, GET_EMVER, sizeof(GET_EMVER));
  1221. }
  1222. static void mceusb_gen1_init(struct mceusb_dev *ir)
  1223. {
  1224. int ret;
  1225. struct device *dev = ir->dev;
  1226. char *data;
  1227. data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
  1228. if (!data) {
  1229. dev_err(dev, "%s: memory allocation failed!", __func__);
  1230. return;
  1231. }
  1232. /*
  1233. * This is a strange one. Windows issues a set address to the device
  1234. * on the receive control pipe and expect a certain value pair back
  1235. */
  1236. ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
  1237. USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
  1238. data, USB_CTRL_MSG_SZ, HZ * 3);
  1239. dev_dbg(dev, "set address - ret = %d", ret);
  1240. dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
  1241. data[0], data[1]);
  1242. /* set feature: bit rate 38400 bps */
  1243. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  1244. USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
  1245. 0xc04e, 0x0000, NULL, 0, HZ * 3);
  1246. dev_dbg(dev, "set feature - ret = %d", ret);
  1247. /* bRequest 4: set char length to 8 bits */
  1248. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  1249. 4, USB_TYPE_VENDOR,
  1250. 0x0808, 0x0000, NULL, 0, HZ * 3);
  1251. dev_dbg(dev, "set char length - retB = %d", ret);
  1252. /* bRequest 2: set handshaking to use DTR/DSR */
  1253. ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
  1254. 2, USB_TYPE_VENDOR,
  1255. 0x0000, 0x0100, NULL, 0, HZ * 3);
  1256. dev_dbg(dev, "set handshake - retC = %d", ret);
  1257. /* device resume */
  1258. mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
  1259. /* get hw/sw revision? */
  1260. mce_command_out(ir, GET_REVISION, sizeof(GET_REVISION));
  1261. kfree(data);
  1262. }
  1263. static void mceusb_gen2_init(struct mceusb_dev *ir)
  1264. {
  1265. /* device resume */
  1266. mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
  1267. /* get wake version (protocol, key, address) */
  1268. mce_command_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
  1269. /* unknown what this one actually returns... */
  1270. mce_command_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
  1271. }
  1272. static void mceusb_get_parameters(struct mceusb_dev *ir)
  1273. {
  1274. int i;
  1275. unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
  1276. MCE_CMD_GETPORTSTATUS, 0x00 };
  1277. /* defaults, if the hardware doesn't support querying */
  1278. ir->num_txports = 2;
  1279. ir->num_rxports = 2;
  1280. /* get number of tx and rx ports */
  1281. mce_command_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
  1282. /* get the carrier and frequency */
  1283. mce_command_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
  1284. if (ir->num_txports && !ir->flags.no_tx)
  1285. /* get the transmitter bitmask */
  1286. mce_command_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
  1287. /* get receiver timeout value */
  1288. mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
  1289. /* get receiver sensor setting */
  1290. mce_command_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
  1291. for (i = 0; i < ir->num_txports; i++) {
  1292. cmdbuf[2] = i;
  1293. mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
  1294. }
  1295. }
  1296. static void mceusb_flash_led(struct mceusb_dev *ir)
  1297. {
  1298. if (ir->emver < 2)
  1299. return;
  1300. mce_command_out(ir, FLASH_LED, sizeof(FLASH_LED));
  1301. }
  1302. /*
  1303. * Workqueue function
  1304. * for resetting or recovering device after occurrence of error events
  1305. * specified in ir->kevent bit field.
  1306. * Function runs (via schedule_work()) in non-interrupt context, for
  1307. * calls here (such as usb_clear_halt()) requiring non-interrupt context.
  1308. */
  1309. static void mceusb_deferred_kevent(struct work_struct *work)
  1310. {
  1311. struct mceusb_dev *ir =
  1312. container_of(work, struct mceusb_dev, kevent);
  1313. int status;
  1314. if (test_bit(EVENT_RX_HALT, &ir->kevent_flags)) {
  1315. usb_unlink_urb(ir->urb_in);
  1316. status = usb_clear_halt(ir->usbdev, ir->pipe_in);
  1317. if (status < 0) {
  1318. dev_err(ir->dev, "rx clear halt error %d",
  1319. status);
  1320. }
  1321. clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
  1322. if (status == 0) {
  1323. status = usb_submit_urb(ir->urb_in, GFP_KERNEL);
  1324. if (status < 0) {
  1325. dev_err(ir->dev,
  1326. "rx unhalt submit urb error %d",
  1327. status);
  1328. }
  1329. }
  1330. }
  1331. if (test_bit(EVENT_TX_HALT, &ir->kevent_flags)) {
  1332. status = usb_clear_halt(ir->usbdev, ir->pipe_out);
  1333. if (status < 0)
  1334. dev_err(ir->dev, "tx clear halt error %d", status);
  1335. clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
  1336. }
  1337. }
  1338. static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
  1339. {
  1340. struct usb_device *udev = ir->usbdev;
  1341. struct device *dev = ir->dev;
  1342. struct rc_dev *rc;
  1343. int ret;
  1344. rc = rc_allocate_device(RC_DRIVER_IR_RAW);
  1345. if (!rc) {
  1346. dev_err(dev, "remote dev allocation failed");
  1347. goto out;
  1348. }
  1349. snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
  1350. mceusb_model[ir->model].name ?
  1351. mceusb_model[ir->model].name :
  1352. "Media Center Ed. eHome Infrared Remote Transceiver",
  1353. le16_to_cpu(ir->usbdev->descriptor.idVendor),
  1354. le16_to_cpu(ir->usbdev->descriptor.idProduct));
  1355. usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
  1356. rc->device_name = ir->name;
  1357. rc->input_phys = ir->phys;
  1358. usb_to_input_id(ir->usbdev, &rc->input_id);
  1359. rc->dev.parent = dev;
  1360. rc->priv = ir;
  1361. rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  1362. rc->min_timeout = US_TO_NS(MCE_TIME_UNIT);
  1363. rc->timeout = MS_TO_NS(100);
  1364. if (!mceusb_model[ir->model].broken_irtimeout) {
  1365. rc->s_timeout = mceusb_set_timeout;
  1366. rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  1367. } else {
  1368. /*
  1369. * If we can't set the timeout using CMD_SETIRTIMEOUT, we can
  1370. * rely on software timeouts for timeouts < 100ms.
  1371. */
  1372. rc->max_timeout = rc->timeout;
  1373. }
  1374. if (!ir->flags.no_tx) {
  1375. rc->s_tx_mask = mceusb_set_tx_mask;
  1376. rc->s_tx_carrier = mceusb_set_tx_carrier;
  1377. rc->tx_ir = mceusb_tx_ir;
  1378. }
  1379. if (ir->flags.rx2 > 0) {
  1380. rc->s_learning_mode = mceusb_set_rx_wideband;
  1381. rc->s_carrier_report = mceusb_set_rx_carrier_report;
  1382. }
  1383. rc->driver_name = DRIVER_NAME;
  1384. switch (le16_to_cpu(udev->descriptor.idVendor)) {
  1385. case VENDOR_HAUPPAUGE:
  1386. rc->map_name = RC_MAP_HAUPPAUGE;
  1387. break;
  1388. case VENDOR_PCTV:
  1389. rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
  1390. break;
  1391. default:
  1392. rc->map_name = RC_MAP_RC6_MCE;
  1393. }
  1394. if (mceusb_model[ir->model].rc_map)
  1395. rc->map_name = mceusb_model[ir->model].rc_map;
  1396. ret = rc_register_device(rc);
  1397. if (ret < 0) {
  1398. dev_err(dev, "remote dev registration failed");
  1399. goto out;
  1400. }
  1401. return rc;
  1402. out:
  1403. rc_free_device(rc);
  1404. return NULL;
  1405. }
  1406. static int mceusb_dev_probe(struct usb_interface *intf,
  1407. const struct usb_device_id *id)
  1408. {
  1409. struct usb_device *dev = interface_to_usbdev(intf);
  1410. struct usb_host_interface *idesc;
  1411. struct usb_endpoint_descriptor *ep = NULL;
  1412. struct usb_endpoint_descriptor *ep_in = NULL;
  1413. struct usb_endpoint_descriptor *ep_out = NULL;
  1414. struct mceusb_dev *ir = NULL;
  1415. int pipe, maxp, i, res;
  1416. char buf[63], name[128] = "";
  1417. enum mceusb_model_type model = id->driver_info;
  1418. bool is_gen3;
  1419. bool is_microsoft_gen1;
  1420. bool tx_mask_normal;
  1421. int ir_intfnum;
  1422. dev_dbg(&intf->dev, "%s called", __func__);
  1423. idesc = intf->cur_altsetting;
  1424. is_gen3 = mceusb_model[model].mce_gen3;
  1425. is_microsoft_gen1 = mceusb_model[model].mce_gen1;
  1426. tx_mask_normal = mceusb_model[model].tx_mask_normal;
  1427. ir_intfnum = mceusb_model[model].ir_intfnum;
  1428. /* There are multi-function devices with non-IR interfaces */
  1429. if (idesc->desc.bInterfaceNumber != ir_intfnum)
  1430. return -ENODEV;
  1431. /* step through the endpoints to find first bulk in and out endpoint */
  1432. for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
  1433. ep = &idesc->endpoint[i].desc;
  1434. if (ep_in == NULL) {
  1435. if (usb_endpoint_is_bulk_in(ep)) {
  1436. ep_in = ep;
  1437. dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
  1438. } else if (usb_endpoint_is_int_in(ep)) {
  1439. ep_in = ep;
  1440. ep_in->bInterval = 1;
  1441. dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
  1442. }
  1443. }
  1444. if (ep_out == NULL) {
  1445. if (usb_endpoint_is_bulk_out(ep)) {
  1446. ep_out = ep;
  1447. dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
  1448. } else if (usb_endpoint_is_int_out(ep)) {
  1449. ep_out = ep;
  1450. ep_out->bInterval = 1;
  1451. dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
  1452. }
  1453. }
  1454. }
  1455. if (!ep_in || !ep_out) {
  1456. dev_dbg(&intf->dev, "required endpoints not found\n");
  1457. return -ENODEV;
  1458. }
  1459. if (usb_endpoint_xfer_int(ep_in))
  1460. pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
  1461. else
  1462. pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
  1463. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  1464. ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
  1465. if (!ir)
  1466. goto mem_alloc_fail;
  1467. ir->pipe_in = pipe;
  1468. ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
  1469. if (!ir->buf_in)
  1470. goto buf_in_alloc_fail;
  1471. ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  1472. if (!ir->urb_in)
  1473. goto urb_in_alloc_fail;
  1474. ir->usbdev = usb_get_dev(dev);
  1475. ir->dev = &intf->dev;
  1476. ir->len_in = maxp;
  1477. ir->flags.microsoft_gen1 = is_microsoft_gen1;
  1478. ir->flags.tx_mask_normal = tx_mask_normal;
  1479. ir->flags.no_tx = mceusb_model[model].no_tx;
  1480. ir->flags.rx2 = mceusb_model[model].rx2;
  1481. ir->model = model;
  1482. /* Saving usb interface data for use by the transmitter routine */
  1483. ir->usb_ep_out = ep_out;
  1484. if (usb_endpoint_xfer_int(ep_out))
  1485. ir->pipe_out = usb_sndintpipe(ir->usbdev,
  1486. ep_out->bEndpointAddress);
  1487. else
  1488. ir->pipe_out = usb_sndbulkpipe(ir->usbdev,
  1489. ep_out->bEndpointAddress);
  1490. if (dev->descriptor.iManufacturer
  1491. && usb_string(dev, dev->descriptor.iManufacturer,
  1492. buf, sizeof(buf)) > 0)
  1493. strlcpy(name, buf, sizeof(name));
  1494. if (dev->descriptor.iProduct
  1495. && usb_string(dev, dev->descriptor.iProduct,
  1496. buf, sizeof(buf)) > 0)
  1497. snprintf(name + strlen(name), sizeof(name) - strlen(name),
  1498. " %s", buf);
  1499. /*
  1500. * Initialize async USB error handler before registering
  1501. * or activating any mceusb RX and TX functions
  1502. */
  1503. INIT_WORK(&ir->kevent, mceusb_deferred_kevent);
  1504. ir->rc = mceusb_init_rc_dev(ir);
  1505. if (!ir->rc)
  1506. goto rc_dev_fail;
  1507. /* wire up inbound data handler */
  1508. if (usb_endpoint_xfer_int(ep_in))
  1509. usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
  1510. mceusb_dev_recv, ir, ep_in->bInterval);
  1511. else
  1512. usb_fill_bulk_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
  1513. mceusb_dev_recv, ir);
  1514. ir->urb_in->transfer_dma = ir->dma_in;
  1515. ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1516. /* flush buffers on the device */
  1517. dev_dbg(&intf->dev, "Flushing receive buffers");
  1518. res = usb_submit_urb(ir->urb_in, GFP_KERNEL);
  1519. if (res)
  1520. dev_err(&intf->dev, "failed to flush buffers: %d", res);
  1521. /* figure out which firmware/emulator version this hardware has */
  1522. mceusb_get_emulator_version(ir);
  1523. /* initialize device */
  1524. if (ir->flags.microsoft_gen1)
  1525. mceusb_gen1_init(ir);
  1526. else if (!is_gen3)
  1527. mceusb_gen2_init(ir);
  1528. mceusb_get_parameters(ir);
  1529. mceusb_flash_led(ir);
  1530. if (!ir->flags.no_tx)
  1531. mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
  1532. usb_set_intfdata(intf, ir);
  1533. /* enable wake via this device */
  1534. device_set_wakeup_capable(ir->dev, true);
  1535. device_set_wakeup_enable(ir->dev, true);
  1536. dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
  1537. name, ir->emver);
  1538. dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
  1539. ir->num_txports, ir->txports_cabled,
  1540. ir->num_rxports, ir->rxports_active);
  1541. return 0;
  1542. /* Error-handling path */
  1543. rc_dev_fail:
  1544. cancel_work_sync(&ir->kevent);
  1545. usb_put_dev(ir->usbdev);
  1546. usb_kill_urb(ir->urb_in);
  1547. usb_free_urb(ir->urb_in);
  1548. urb_in_alloc_fail:
  1549. usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
  1550. buf_in_alloc_fail:
  1551. kfree(ir);
  1552. mem_alloc_fail:
  1553. dev_err(&intf->dev, "%s: device setup failed!", __func__);
  1554. return -ENOMEM;
  1555. }
  1556. static void mceusb_dev_disconnect(struct usb_interface *intf)
  1557. {
  1558. struct usb_device *dev = interface_to_usbdev(intf);
  1559. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1560. usb_set_intfdata(intf, NULL);
  1561. if (!ir)
  1562. return;
  1563. ir->usbdev = NULL;
  1564. cancel_work_sync(&ir->kevent);
  1565. rc_unregister_device(ir->rc);
  1566. usb_kill_urb(ir->urb_in);
  1567. usb_free_urb(ir->urb_in);
  1568. usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
  1569. usb_put_dev(dev);
  1570. kfree(ir);
  1571. }
  1572. static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
  1573. {
  1574. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1575. dev_info(ir->dev, "suspend");
  1576. usb_kill_urb(ir->urb_in);
  1577. return 0;
  1578. }
  1579. static int mceusb_dev_resume(struct usb_interface *intf)
  1580. {
  1581. struct mceusb_dev *ir = usb_get_intfdata(intf);
  1582. dev_info(ir->dev, "resume");
  1583. if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
  1584. return -EIO;
  1585. return 0;
  1586. }
  1587. static struct usb_driver mceusb_dev_driver = {
  1588. .name = DRIVER_NAME,
  1589. .probe = mceusb_dev_probe,
  1590. .disconnect = mceusb_dev_disconnect,
  1591. .suspend = mceusb_dev_suspend,
  1592. .resume = mceusb_dev_resume,
  1593. .reset_resume = mceusb_dev_resume,
  1594. .id_table = mceusb_dev_table
  1595. };
  1596. module_usb_driver(mceusb_dev_driver);
  1597. MODULE_DESCRIPTION(DRIVER_DESC);
  1598. MODULE_AUTHOR(DRIVER_AUTHOR);
  1599. MODULE_LICENSE("GPL");
  1600. MODULE_DEVICE_TABLE(usb, mceusb_dev_table);