winbond-cir.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * winbond-cir.c - Driver for the Consumer IR functionality of Winbond
  3. * SuperI/O chips.
  4. *
  5. * Currently supports the Winbond WPCD376i chip (PNP id WEC1022), but
  6. * could probably support others (Winbond WEC102X, NatSemi, etc)
  7. * with minor modifications.
  8. *
  9. * Original Author: David Härdeman <david@hardeman.nu>
  10. * Copyright (C) 2009 David Härdeman <david@hardeman.nu>
  11. *
  12. * Dedicated to Matilda, my newborn daughter, without whose loving attention
  13. * this driver would have been finished in half the time and with a fraction
  14. * of the bugs.
  15. *
  16. * Written using:
  17. * o Winbond WPCD376I datasheet helpfully provided by Jesse Barnes at Intel
  18. * o NatSemi PC87338/PC97338 datasheet (for the serial port stuff)
  19. * o DSDT dumps
  20. *
  21. * Supported features:
  22. * o RC6
  23. * o Wake-On-CIR functionality
  24. *
  25. * To do:
  26. * o Test NEC and RC5
  27. *
  28. * Left as an exercise for the reader:
  29. * o Learning (I have neither the hardware, nor the need)
  30. * o IR Transmit (ibid)
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  45. */
  46. #include <linux/module.h>
  47. #include <linux/pnp.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/timer.h>
  50. #include <linux/input.h>
  51. #include <linux/leds.h>
  52. #include <linux/list.h>
  53. #include <linux/spinlock.h>
  54. #include <linux/pci_ids.h>
  55. #include <linux/io.h>
  56. #include <linux/bitrev.h>
  57. #include <linux/bitops.h>
  58. #define DRVNAME "winbond-cir"
  59. /* CEIR Wake-Up Registers, relative to data->wbase */
  60. #define WBCIR_REG_WCEIR_CTL 0x03 /* CEIR Receiver Control */
  61. #define WBCIR_REG_WCEIR_STS 0x04 /* CEIR Receiver Status */
  62. #define WBCIR_REG_WCEIR_EV_EN 0x05 /* CEIR Receiver Event Enable */
  63. #define WBCIR_REG_WCEIR_CNTL 0x06 /* CEIR Receiver Counter Low */
  64. #define WBCIR_REG_WCEIR_CNTH 0x07 /* CEIR Receiver Counter High */
  65. #define WBCIR_REG_WCEIR_INDEX 0x08 /* CEIR Receiver Index */
  66. #define WBCIR_REG_WCEIR_DATA 0x09 /* CEIR Receiver Data */
  67. #define WBCIR_REG_WCEIR_CSL 0x0A /* CEIR Re. Compare Strlen */
  68. #define WBCIR_REG_WCEIR_CFG1 0x0B /* CEIR Re. Configuration 1 */
  69. #define WBCIR_REG_WCEIR_CFG2 0x0C /* CEIR Re. Configuration 2 */
  70. /* CEIR Enhanced Functionality Registers, relative to data->ebase */
  71. #define WBCIR_REG_ECEIR_CTS 0x00 /* Enhanced IR Control Status */
  72. #define WBCIR_REG_ECEIR_CCTL 0x01 /* Infrared Counter Control */
  73. #define WBCIR_REG_ECEIR_CNT_LO 0x02 /* Infrared Counter LSB */
  74. #define WBCIR_REG_ECEIR_CNT_HI 0x03 /* Infrared Counter MSB */
  75. #define WBCIR_REG_ECEIR_IREM 0x04 /* Infrared Emitter Status */
  76. /* SP3 Banked Registers, relative to data->sbase */
  77. #define WBCIR_REG_SP3_BSR 0x03 /* Bank Select, all banks */
  78. /* Bank 0 */
  79. #define WBCIR_REG_SP3_RXDATA 0x00 /* FIFO RX data (r) */
  80. #define WBCIR_REG_SP3_TXDATA 0x00 /* FIFO TX data (w) */
  81. #define WBCIR_REG_SP3_IER 0x01 /* Interrupt Enable */
  82. #define WBCIR_REG_SP3_EIR 0x02 /* Event Identification (r) */
  83. #define WBCIR_REG_SP3_FCR 0x02 /* FIFO Control (w) */
  84. #define WBCIR_REG_SP3_MCR 0x04 /* Mode Control */
  85. #define WBCIR_REG_SP3_LSR 0x05 /* Link Status */
  86. #define WBCIR_REG_SP3_MSR 0x06 /* Modem Status */
  87. #define WBCIR_REG_SP3_ASCR 0x07 /* Aux Status and Control */
  88. /* Bank 2 */
  89. #define WBCIR_REG_SP3_BGDL 0x00 /* Baud Divisor LSB */
  90. #define WBCIR_REG_SP3_BGDH 0x01 /* Baud Divisor MSB */
  91. #define WBCIR_REG_SP3_EXCR1 0x02 /* Extended Control 1 */
  92. #define WBCIR_REG_SP3_EXCR2 0x04 /* Extended Control 2 */
  93. #define WBCIR_REG_SP3_TXFLV 0x06 /* TX FIFO Level */
  94. #define WBCIR_REG_SP3_RXFLV 0x07 /* RX FIFO Level */
  95. /* Bank 3 */
  96. #define WBCIR_REG_SP3_MRID 0x00 /* Module Identification */
  97. #define WBCIR_REG_SP3_SH_LCR 0x01 /* LCR Shadow */
  98. #define WBCIR_REG_SP3_SH_FCR 0x02 /* FCR Shadow */
  99. /* Bank 4 */
  100. #define WBCIR_REG_SP3_IRCR1 0x02 /* Infrared Control 1 */
  101. /* Bank 5 */
  102. #define WBCIR_REG_SP3_IRCR2 0x04 /* Infrared Control 2 */
  103. /* Bank 6 */
  104. #define WBCIR_REG_SP3_IRCR3 0x00 /* Infrared Control 3 */
  105. #define WBCIR_REG_SP3_SIR_PW 0x02 /* SIR Pulse Width */
  106. /* Bank 7 */
  107. #define WBCIR_REG_SP3_IRRXDC 0x00 /* IR RX Demod Control */
  108. #define WBCIR_REG_SP3_IRTXMC 0x01 /* IR TX Mod Control */
  109. #define WBCIR_REG_SP3_RCCFG 0x02 /* CEIR Config */
  110. #define WBCIR_REG_SP3_IRCFG1 0x04 /* Infrared Config 1 */
  111. #define WBCIR_REG_SP3_IRCFG4 0x07 /* Infrared Config 4 */
  112. /*
  113. * Magic values follow
  114. */
  115. /* No interrupts for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  116. #define WBCIR_IRQ_NONE 0x00
  117. /* RX data bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  118. #define WBCIR_IRQ_RX 0x01
  119. /* Over/Under-flow bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  120. #define WBCIR_IRQ_ERR 0x04
  121. /* Led enable/disable bit for WBCIR_REG_ECEIR_CTS */
  122. #define WBCIR_LED_ENABLE 0x80
  123. /* RX data available bit for WBCIR_REG_SP3_LSR */
  124. #define WBCIR_RX_AVAIL 0x01
  125. /* RX disable bit for WBCIR_REG_SP3_ASCR */
  126. #define WBCIR_RX_DISABLE 0x20
  127. /* Extended mode enable bit for WBCIR_REG_SP3_EXCR1 */
  128. #define WBCIR_EXT_ENABLE 0x01
  129. /* Select compare register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  130. #define WBCIR_REGSEL_COMPARE 0x10
  131. /* Select mask register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  132. #define WBCIR_REGSEL_MASK 0x20
  133. /* Starting address of selected register in WBCIR_REG_WCEIR_INDEX */
  134. #define WBCIR_REG_ADDR0 0x00
  135. /* Valid banks for the SP3 UART */
  136. enum wbcir_bank {
  137. WBCIR_BANK_0 = 0x00,
  138. WBCIR_BANK_1 = 0x80,
  139. WBCIR_BANK_2 = 0xE0,
  140. WBCIR_BANK_3 = 0xE4,
  141. WBCIR_BANK_4 = 0xE8,
  142. WBCIR_BANK_5 = 0xEC,
  143. WBCIR_BANK_6 = 0xF0,
  144. WBCIR_BANK_7 = 0xF4,
  145. };
  146. /* Supported IR Protocols */
  147. enum wbcir_protocol {
  148. IR_PROTOCOL_RC5 = 0x0,
  149. IR_PROTOCOL_NEC = 0x1,
  150. IR_PROTOCOL_RC6 = 0x2,
  151. };
  152. /* Misc */
  153. #define WBCIR_NAME "Winbond CIR"
  154. #define WBCIR_ID_FAMILY 0xF1 /* Family ID for the WPCD376I */
  155. #define WBCIR_ID_CHIP 0x04 /* Chip ID for the WPCD376I */
  156. #define IR_KEYPRESS_TIMEOUT 250 /* FIXME: should be per-protocol? */
  157. #define INVALID_SCANCODE 0x7FFFFFFF /* Invalid with all protos */
  158. #define WAKEUP_IOMEM_LEN 0x10 /* Wake-Up I/O Reg Len */
  159. #define EHFUNC_IOMEM_LEN 0x10 /* Enhanced Func I/O Reg Len */
  160. #define SP_IOMEM_LEN 0x08 /* Serial Port 3 (IR) Reg Len */
  161. #define WBCIR_MAX_IDLE_BYTES 10
  162. static DEFINE_SPINLOCK(wbcir_lock);
  163. static DEFINE_RWLOCK(keytable_lock);
  164. struct wbcir_key {
  165. u32 scancode;
  166. unsigned int keycode;
  167. };
  168. struct wbcir_keyentry {
  169. struct wbcir_key key;
  170. struct list_head list;
  171. };
  172. static struct wbcir_key rc6_def_keymap[] = {
  173. { 0x800F0400, KEY_NUMERIC_0 },
  174. { 0x800F0401, KEY_NUMERIC_1 },
  175. { 0x800F0402, KEY_NUMERIC_2 },
  176. { 0x800F0403, KEY_NUMERIC_3 },
  177. { 0x800F0404, KEY_NUMERIC_4 },
  178. { 0x800F0405, KEY_NUMERIC_5 },
  179. { 0x800F0406, KEY_NUMERIC_6 },
  180. { 0x800F0407, KEY_NUMERIC_7 },
  181. { 0x800F0408, KEY_NUMERIC_8 },
  182. { 0x800F0409, KEY_NUMERIC_9 },
  183. { 0x800F041D, KEY_NUMERIC_STAR },
  184. { 0x800F041C, KEY_NUMERIC_POUND },
  185. { 0x800F0410, KEY_VOLUMEUP },
  186. { 0x800F0411, KEY_VOLUMEDOWN },
  187. { 0x800F0412, KEY_CHANNELUP },
  188. { 0x800F0413, KEY_CHANNELDOWN },
  189. { 0x800F040E, KEY_MUTE },
  190. { 0x800F040D, KEY_VENDOR }, /* Vista Logo Key */
  191. { 0x800F041E, KEY_UP },
  192. { 0x800F041F, KEY_DOWN },
  193. { 0x800F0420, KEY_LEFT },
  194. { 0x800F0421, KEY_RIGHT },
  195. { 0x800F0422, KEY_OK },
  196. { 0x800F0423, KEY_ESC },
  197. { 0x800F040F, KEY_INFO },
  198. { 0x800F040A, KEY_CLEAR },
  199. { 0x800F040B, KEY_ENTER },
  200. { 0x800F045B, KEY_RED },
  201. { 0x800F045C, KEY_GREEN },
  202. { 0x800F045D, KEY_YELLOW },
  203. { 0x800F045E, KEY_BLUE },
  204. { 0x800F045A, KEY_TEXT },
  205. { 0x800F0427, KEY_SWITCHVIDEOMODE },
  206. { 0x800F040C, KEY_POWER },
  207. { 0x800F0450, KEY_RADIO },
  208. { 0x800F0448, KEY_PVR },
  209. { 0x800F0447, KEY_AUDIO },
  210. { 0x800F0426, KEY_EPG },
  211. { 0x800F0449, KEY_CAMERA },
  212. { 0x800F0425, KEY_TV },
  213. { 0x800F044A, KEY_VIDEO },
  214. { 0x800F0424, KEY_DVD },
  215. { 0x800F0416, KEY_PLAY },
  216. { 0x800F0418, KEY_PAUSE },
  217. { 0x800F0419, KEY_STOP },
  218. { 0x800F0414, KEY_FASTFORWARD },
  219. { 0x800F041A, KEY_NEXT },
  220. { 0x800F041B, KEY_PREVIOUS },
  221. { 0x800F0415, KEY_REWIND },
  222. { 0x800F0417, KEY_RECORD },
  223. };
  224. /* Registers and other state is protected by wbcir_lock */
  225. struct wbcir_data {
  226. unsigned long wbase; /* Wake-Up Baseaddr */
  227. unsigned long ebase; /* Enhanced Func. Baseaddr */
  228. unsigned long sbase; /* Serial Port Baseaddr */
  229. unsigned int irq; /* Serial Port IRQ */
  230. struct input_dev *input_dev;
  231. struct timer_list timer_keyup;
  232. struct led_trigger *rxtrigger;
  233. struct led_trigger *txtrigger;
  234. struct led_classdev led;
  235. u32 last_scancode;
  236. unsigned int last_keycode;
  237. u8 last_toggle;
  238. u8 keypressed;
  239. unsigned long keyup_jiffies;
  240. unsigned int idle_count;
  241. /* RX irdata and parsing state */
  242. unsigned long irdata[30];
  243. unsigned int irdata_count;
  244. unsigned int irdata_idle;
  245. unsigned int irdata_off;
  246. unsigned int irdata_error;
  247. /* Protected by keytable_lock */
  248. struct list_head keytable;
  249. };
  250. static enum wbcir_protocol protocol = IR_PROTOCOL_RC6;
  251. module_param(protocol, uint, 0444);
  252. MODULE_PARM_DESC(protocol, "IR protocol to use "
  253. "(0 = RC5, 1 = NEC, 2 = RC6A, default)");
  254. static int invert; /* default = 0 */
  255. module_param(invert, bool, 0444);
  256. MODULE_PARM_DESC(invert, "Invert the signal from the IR receiver");
  257. static unsigned int wake_sc = 0x800F040C;
  258. module_param(wake_sc, uint, 0644);
  259. MODULE_PARM_DESC(wake_sc, "Scancode of the power-on IR command");
  260. static unsigned int wake_rc6mode = 6;
  261. module_param(wake_rc6mode, uint, 0644);
  262. MODULE_PARM_DESC(wake_rc6mode, "RC6 mode for the power-on command "
  263. "(0 = 0, 6 = 6A, default)");
  264. /*****************************************************************************
  265. *
  266. * UTILITY FUNCTIONS
  267. *
  268. *****************************************************************************/
  269. /* Caller needs to hold wbcir_lock */
  270. static void
  271. wbcir_set_bits(unsigned long addr, u8 bits, u8 mask)
  272. {
  273. u8 val;
  274. val = inb(addr);
  275. val = ((val & ~mask) | (bits & mask));
  276. outb(val, addr);
  277. }
  278. /* Selects the register bank for the serial port */
  279. static inline void
  280. wbcir_select_bank(struct wbcir_data *data, enum wbcir_bank bank)
  281. {
  282. outb(bank, data->sbase + WBCIR_REG_SP3_BSR);
  283. }
  284. static enum led_brightness
  285. wbcir_led_brightness_get(struct led_classdev *led_cdev)
  286. {
  287. struct wbcir_data *data = container_of(led_cdev,
  288. struct wbcir_data,
  289. led);
  290. if (inb(data->ebase + WBCIR_REG_ECEIR_CTS) & WBCIR_LED_ENABLE)
  291. return LED_FULL;
  292. else
  293. return LED_OFF;
  294. }
  295. static void
  296. wbcir_led_brightness_set(struct led_classdev *led_cdev,
  297. enum led_brightness brightness)
  298. {
  299. struct wbcir_data *data = container_of(led_cdev,
  300. struct wbcir_data,
  301. led);
  302. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS,
  303. brightness == LED_OFF ? 0x00 : WBCIR_LED_ENABLE,
  304. WBCIR_LED_ENABLE);
  305. }
  306. /* Manchester encodes bits to RC6 message cells (see wbcir_parse_rc6) */
  307. static u8
  308. wbcir_to_rc6cells(u8 val)
  309. {
  310. u8 coded = 0x00;
  311. int i;
  312. val &= 0x0F;
  313. for (i = 0; i < 4; i++) {
  314. if (val & 0x01)
  315. coded |= 0x02 << (i * 2);
  316. else
  317. coded |= 0x01 << (i * 2);
  318. val >>= 1;
  319. }
  320. return coded;
  321. }
  322. /*****************************************************************************
  323. *
  324. * INPUT FUNCTIONS
  325. *
  326. *****************************************************************************/
  327. static unsigned int
  328. wbcir_do_getkeycode(struct wbcir_data *data, u32 scancode)
  329. {
  330. struct wbcir_keyentry *keyentry;
  331. unsigned int keycode = KEY_RESERVED;
  332. unsigned long flags;
  333. read_lock_irqsave(&keytable_lock, flags);
  334. list_for_each_entry(keyentry, &data->keytable, list) {
  335. if (keyentry->key.scancode == scancode) {
  336. keycode = keyentry->key.keycode;
  337. break;
  338. }
  339. }
  340. read_unlock_irqrestore(&keytable_lock, flags);
  341. return keycode;
  342. }
  343. static int
  344. wbcir_getkeycode(struct input_dev *dev,
  345. unsigned int scancode, unsigned int *keycode)
  346. {
  347. struct wbcir_data *data = input_get_drvdata(dev);
  348. *keycode = wbcir_do_getkeycode(data, scancode);
  349. return 0;
  350. }
  351. static int
  352. wbcir_setkeycode(struct input_dev *dev,
  353. unsigned int scancode, unsigned int keycode)
  354. {
  355. struct wbcir_data *data = input_get_drvdata(dev);
  356. struct wbcir_keyentry *keyentry;
  357. struct wbcir_keyentry *new_keyentry;
  358. unsigned long flags;
  359. unsigned int old_keycode = KEY_RESERVED;
  360. new_keyentry = kmalloc(sizeof(*new_keyentry), GFP_KERNEL);
  361. if (!new_keyentry)
  362. return -ENOMEM;
  363. write_lock_irqsave(&keytable_lock, flags);
  364. list_for_each_entry(keyentry, &data->keytable, list) {
  365. if (keyentry->key.scancode != scancode)
  366. continue;
  367. old_keycode = keyentry->key.keycode;
  368. keyentry->key.keycode = keycode;
  369. if (keyentry->key.keycode == KEY_RESERVED) {
  370. list_del(&keyentry->list);
  371. kfree(keyentry);
  372. }
  373. break;
  374. }
  375. set_bit(keycode, dev->keybit);
  376. if (old_keycode == KEY_RESERVED) {
  377. new_keyentry->key.scancode = scancode;
  378. new_keyentry->key.keycode = keycode;
  379. list_add(&new_keyentry->list, &data->keytable);
  380. } else {
  381. kfree(new_keyentry);
  382. clear_bit(old_keycode, dev->keybit);
  383. list_for_each_entry(keyentry, &data->keytable, list) {
  384. if (keyentry->key.keycode == old_keycode) {
  385. set_bit(old_keycode, dev->keybit);
  386. break;
  387. }
  388. }
  389. }
  390. write_unlock_irqrestore(&keytable_lock, flags);
  391. return 0;
  392. }
  393. /*
  394. * Timer function to report keyup event some time after keydown is
  395. * reported by the ISR.
  396. */
  397. static void
  398. wbcir_keyup(unsigned long cookie)
  399. {
  400. struct wbcir_data *data = (struct wbcir_data *)cookie;
  401. unsigned long flags;
  402. /*
  403. * data->keyup_jiffies is used to prevent a race condition if a
  404. * hardware interrupt occurs at this point and the keyup timer
  405. * event is moved further into the future as a result.
  406. *
  407. * The timer will then be reactivated and this function called
  408. * again in the future. We need to exit gracefully in that case
  409. * to allow the input subsystem to do its auto-repeat magic or
  410. * a keyup event might follow immediately after the keydown.
  411. */
  412. spin_lock_irqsave(&wbcir_lock, flags);
  413. if (time_is_after_eq_jiffies(data->keyup_jiffies) && data->keypressed) {
  414. data->keypressed = 0;
  415. led_trigger_event(data->rxtrigger, LED_OFF);
  416. input_report_key(data->input_dev, data->last_keycode, 0);
  417. input_sync(data->input_dev);
  418. }
  419. spin_unlock_irqrestore(&wbcir_lock, flags);
  420. }
  421. static void
  422. wbcir_keydown(struct wbcir_data *data, u32 scancode, u8 toggle)
  423. {
  424. unsigned int keycode;
  425. /* Repeat? */
  426. if (data->last_scancode == scancode &&
  427. data->last_toggle == toggle &&
  428. data->keypressed)
  429. goto set_timer;
  430. data->last_scancode = scancode;
  431. /* Do we need to release an old keypress? */
  432. if (data->keypressed) {
  433. input_report_key(data->input_dev, data->last_keycode, 0);
  434. input_sync(data->input_dev);
  435. data->keypressed = 0;
  436. }
  437. /* Report scancode */
  438. input_event(data->input_dev, EV_MSC, MSC_SCAN, (int)scancode);
  439. /* Do we know this scancode? */
  440. keycode = wbcir_do_getkeycode(data, scancode);
  441. if (keycode == KEY_RESERVED)
  442. goto set_timer;
  443. /* Register a keypress */
  444. input_report_key(data->input_dev, keycode, 1);
  445. data->keypressed = 1;
  446. data->last_keycode = keycode;
  447. data->last_toggle = toggle;
  448. set_timer:
  449. input_sync(data->input_dev);
  450. led_trigger_event(data->rxtrigger,
  451. data->keypressed ? LED_FULL : LED_OFF);
  452. data->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  453. mod_timer(&data->timer_keyup, data->keyup_jiffies);
  454. }
  455. /*****************************************************************************
  456. *
  457. * IR PARSING FUNCTIONS
  458. *
  459. *****************************************************************************/
  460. /* Resets all irdata */
  461. static void
  462. wbcir_reset_irdata(struct wbcir_data *data)
  463. {
  464. memset(data->irdata, 0, sizeof(data->irdata));
  465. data->irdata_count = 0;
  466. data->irdata_off = 0;
  467. data->irdata_error = 0;
  468. data->idle_count = 0;
  469. }
  470. /* Adds one bit of irdata */
  471. static void
  472. add_irdata_bit(struct wbcir_data *data, int set)
  473. {
  474. if (data->irdata_count >= sizeof(data->irdata) * 8) {
  475. data->irdata_error = 1;
  476. return;
  477. }
  478. if (set)
  479. __set_bit(data->irdata_count, data->irdata);
  480. data->irdata_count++;
  481. }
  482. /* Gets count bits of irdata */
  483. static u16
  484. get_bits(struct wbcir_data *data, int count)
  485. {
  486. u16 val = 0x0;
  487. if (data->irdata_count - data->irdata_off < count) {
  488. data->irdata_error = 1;
  489. return 0x0;
  490. }
  491. while (count > 0) {
  492. val <<= 1;
  493. if (test_bit(data->irdata_off, data->irdata))
  494. val |= 0x1;
  495. count--;
  496. data->irdata_off++;
  497. }
  498. return val;
  499. }
  500. /* Reads 16 cells and converts them to a byte */
  501. static u8
  502. wbcir_rc6cells_to_byte(struct wbcir_data *data)
  503. {
  504. u16 raw = get_bits(data, 16);
  505. u8 val = 0x00;
  506. int bit;
  507. for (bit = 0; bit < 8; bit++) {
  508. switch (raw & 0x03) {
  509. case 0x01:
  510. break;
  511. case 0x02:
  512. val |= (0x01 << bit);
  513. break;
  514. default:
  515. data->irdata_error = 1;
  516. break;
  517. }
  518. raw >>= 2;
  519. }
  520. return val;
  521. }
  522. /* Decodes a number of bits from raw RC5 data */
  523. static u8
  524. wbcir_get_rc5bits(struct wbcir_data *data, unsigned int count)
  525. {
  526. u16 raw = get_bits(data, count * 2);
  527. u8 val = 0x00;
  528. int bit;
  529. for (bit = 0; bit < count; bit++) {
  530. switch (raw & 0x03) {
  531. case 0x01:
  532. val |= (0x01 << bit);
  533. break;
  534. case 0x02:
  535. break;
  536. default:
  537. data->irdata_error = 1;
  538. break;
  539. }
  540. raw >>= 2;
  541. }
  542. return val;
  543. }
  544. static void
  545. wbcir_parse_rc6(struct device *dev, struct wbcir_data *data)
  546. {
  547. /*
  548. * Normal bits are manchester coded as follows:
  549. * cell0 + cell1 = logic "0"
  550. * cell1 + cell0 = logic "1"
  551. *
  552. * The IR pulse has the following components:
  553. *
  554. * Leader - 6 * cell1 - discarded
  555. * Gap - 2 * cell0 - discarded
  556. * Start bit - Normal Coding - always "1"
  557. * Mode Bit 2 - 0 - Normal Coding
  558. * Toggle bit - Normal Coding with double bit time,
  559. * e.g. cell0 + cell0 + cell1 + cell1
  560. * means logic "0".
  561. *
  562. * The rest depends on the mode, the following modes are known:
  563. *
  564. * MODE 0:
  565. * Address Bit 7 - 0 - Normal Coding
  566. * Command Bit 7 - 0 - Normal Coding
  567. *
  568. * MODE 6:
  569. * The above Toggle Bit is used as a submode bit, 0 = A, 1 = B.
  570. * Submode B is for pointing devices, only remotes using submode A
  571. * are supported.
  572. *
  573. * Customer range bit - 0 => Customer = 7 bits, 0...127
  574. * 1 => Customer = 15 bits, 32768...65535
  575. * Customer Bits - Normal Coding
  576. *
  577. * Customer codes are allocated by Philips. The rest of the bits
  578. * are customer dependent. The following is commonly used (and the
  579. * only supported config):
  580. *
  581. * Toggle Bit - Normal Coding
  582. * Address Bit 6 - 0 - Normal Coding
  583. * Command Bit 7 - 0 - Normal Coding
  584. *
  585. * All modes are followed by at least 6 * cell0.
  586. *
  587. * MODE 0 msglen:
  588. * 1 * 2 (start bit) + 3 * 2 (mode) + 2 * 2 (toggle) +
  589. * 8 * 2 (address) + 8 * 2 (command) =
  590. * 44 cells
  591. *
  592. * MODE 6A msglen:
  593. * 1 * 2 (start bit) + 3 * 2 (mode) + 2 * 2 (submode) +
  594. * 1 * 2 (customer range bit) + 7/15 * 2 (customer bits) +
  595. * 1 * 2 (toggle bit) + 7 * 2 (address) + 8 * 2 (command) =
  596. * 60 - 76 cells
  597. */
  598. u8 mode;
  599. u8 toggle;
  600. u16 customer = 0x0;
  601. u8 address;
  602. u8 command;
  603. u32 scancode;
  604. /* Leader mark */
  605. while (get_bits(data, 1) && !data->irdata_error)
  606. /* Do nothing */;
  607. /* Leader space */
  608. if (get_bits(data, 1)) {
  609. dev_dbg(dev, "RC6 - Invalid leader space\n");
  610. return;
  611. }
  612. /* Start bit */
  613. if (get_bits(data, 2) != 0x02) {
  614. dev_dbg(dev, "RC6 - Invalid start bit\n");
  615. return;
  616. }
  617. /* Mode */
  618. mode = get_bits(data, 6);
  619. switch (mode) {
  620. case 0x15: /* 010101 = b000 */
  621. mode = 0;
  622. break;
  623. case 0x29: /* 101001 = b110 */
  624. mode = 6;
  625. break;
  626. default:
  627. dev_dbg(dev, "RC6 - Invalid mode\n");
  628. return;
  629. }
  630. /* Toggle bit / Submode bit */
  631. toggle = get_bits(data, 4);
  632. switch (toggle) {
  633. case 0x03:
  634. toggle = 0;
  635. break;
  636. case 0x0C:
  637. toggle = 1;
  638. break;
  639. default:
  640. dev_dbg(dev, "RC6 - Toggle bit error\n");
  641. break;
  642. }
  643. /* Customer */
  644. if (mode == 6) {
  645. if (toggle != 0) {
  646. dev_dbg(dev, "RC6B - Not Supported\n");
  647. return;
  648. }
  649. customer = wbcir_rc6cells_to_byte(data);
  650. if (customer & 0x80) {
  651. /* 15 bit customer value */
  652. customer <<= 8;
  653. customer |= wbcir_rc6cells_to_byte(data);
  654. }
  655. }
  656. /* Address */
  657. address = wbcir_rc6cells_to_byte(data);
  658. if (mode == 6) {
  659. toggle = address >> 7;
  660. address &= 0x7F;
  661. }
  662. /* Command */
  663. command = wbcir_rc6cells_to_byte(data);
  664. /* Create scancode */
  665. scancode = command;
  666. scancode |= address << 8;
  667. scancode |= customer << 16;
  668. /* Last sanity check */
  669. if (data->irdata_error) {
  670. dev_dbg(dev, "RC6 - Cell error(s)\n");
  671. return;
  672. }
  673. dev_dbg(dev, "IR-RC6 ad 0x%02X cm 0x%02X cu 0x%04X "
  674. "toggle %u mode %u scan 0x%08X\n",
  675. address,
  676. command,
  677. customer,
  678. (unsigned int)toggle,
  679. (unsigned int)mode,
  680. scancode);
  681. wbcir_keydown(data, scancode, toggle);
  682. }
  683. static void
  684. wbcir_parse_rc5(struct device *dev, struct wbcir_data *data)
  685. {
  686. /*
  687. * Bits are manchester coded as follows:
  688. * cell1 + cell0 = logic "0"
  689. * cell0 + cell1 = logic "1"
  690. * (i.e. the reverse of RC6)
  691. *
  692. * Start bit 1 - "1" - discarded
  693. * Start bit 2 - Must be inverted to get command bit 6
  694. * Toggle bit
  695. * Address Bit 4 - 0
  696. * Command Bit 5 - 0
  697. */
  698. u8 toggle;
  699. u8 address;
  700. u8 command;
  701. u32 scancode;
  702. /* Start bit 1 */
  703. if (!get_bits(data, 1)) {
  704. dev_dbg(dev, "RC5 - Invalid start bit\n");
  705. return;
  706. }
  707. /* Start bit 2 */
  708. if (!wbcir_get_rc5bits(data, 1))
  709. command = 0x40;
  710. else
  711. command = 0x00;
  712. toggle = wbcir_get_rc5bits(data, 1);
  713. address = wbcir_get_rc5bits(data, 5);
  714. command |= wbcir_get_rc5bits(data, 6);
  715. scancode = address << 7 | command;
  716. /* Last sanity check */
  717. if (data->irdata_error) {
  718. dev_dbg(dev, "RC5 - Invalid message\n");
  719. return;
  720. }
  721. dev_dbg(dev, "IR-RC5 ad %u cm %u t %u s %u\n",
  722. (unsigned int)address,
  723. (unsigned int)command,
  724. (unsigned int)toggle,
  725. (unsigned int)scancode);
  726. wbcir_keydown(data, scancode, toggle);
  727. }
  728. static void
  729. wbcir_parse_nec(struct device *dev, struct wbcir_data *data)
  730. {
  731. /*
  732. * Each bit represents 560 us.
  733. *
  734. * Leader - 9 ms burst
  735. * Gap - 4.5 ms silence
  736. * Address1 bit 0 - 7 - Address 1
  737. * Address2 bit 0 - 7 - Address 2
  738. * Command1 bit 0 - 7 - Command 1
  739. * Command2 bit 0 - 7 - Command 2
  740. *
  741. * Note the bit order!
  742. *
  743. * With the old NEC protocol, Address2 was the inverse of Address1
  744. * and Command2 was the inverse of Command1 and were used as
  745. * an error check.
  746. *
  747. * With NEC extended, Address1 is the LSB of the Address and
  748. * Address2 is the MSB, Command parsing remains unchanged.
  749. *
  750. * A repeat message is coded as:
  751. * Leader - 9 ms burst
  752. * Gap - 2.25 ms silence
  753. * Repeat - 560 us active
  754. */
  755. u8 address1;
  756. u8 address2;
  757. u8 command1;
  758. u8 command2;
  759. u16 address;
  760. u32 scancode;
  761. /* Leader mark */
  762. while (get_bits(data, 1) && !data->irdata_error)
  763. /* Do nothing */;
  764. /* Leader space */
  765. if (get_bits(data, 4)) {
  766. dev_dbg(dev, "NEC - Invalid leader space\n");
  767. return;
  768. }
  769. /* Repeat? */
  770. if (get_bits(data, 1)) {
  771. if (!data->keypressed) {
  772. dev_dbg(dev, "NEC - Stray repeat message\n");
  773. return;
  774. }
  775. dev_dbg(dev, "IR-NEC repeat s %u\n",
  776. (unsigned int)data->last_scancode);
  777. wbcir_keydown(data, data->last_scancode, data->last_toggle);
  778. return;
  779. }
  780. /* Remaining leader space */
  781. if (get_bits(data, 3)) {
  782. dev_dbg(dev, "NEC - Invalid leader space\n");
  783. return;
  784. }
  785. address1 = bitrev8(get_bits(data, 8));
  786. address2 = bitrev8(get_bits(data, 8));
  787. command1 = bitrev8(get_bits(data, 8));
  788. command2 = bitrev8(get_bits(data, 8));
  789. /* Sanity check */
  790. if (data->irdata_error) {
  791. dev_dbg(dev, "NEC - Invalid message\n");
  792. return;
  793. }
  794. /* Check command validity */
  795. if (command1 != ~command2) {
  796. dev_dbg(dev, "NEC - Command bytes mismatch\n");
  797. return;
  798. }
  799. /* Check for extended NEC protocol */
  800. address = address1;
  801. if (address1 != ~address2)
  802. address |= address2 << 8;
  803. scancode = address << 8 | command1;
  804. dev_dbg(dev, "IR-NEC ad %u cm %u s %u\n",
  805. (unsigned int)address,
  806. (unsigned int)command1,
  807. (unsigned int)scancode);
  808. wbcir_keydown(data, scancode, !data->last_toggle);
  809. }
  810. /*****************************************************************************
  811. *
  812. * INTERRUPT FUNCTIONS
  813. *
  814. *****************************************************************************/
  815. static irqreturn_t
  816. wbcir_irq_handler(int irqno, void *cookie)
  817. {
  818. struct pnp_dev *device = cookie;
  819. struct wbcir_data *data = pnp_get_drvdata(device);
  820. struct device *dev = &device->dev;
  821. u8 status;
  822. unsigned long flags;
  823. u8 irdata[8];
  824. int i;
  825. unsigned int hw;
  826. spin_lock_irqsave(&wbcir_lock, flags);
  827. wbcir_select_bank(data, WBCIR_BANK_0);
  828. status = inb(data->sbase + WBCIR_REG_SP3_EIR);
  829. if (!(status & (WBCIR_IRQ_RX | WBCIR_IRQ_ERR))) {
  830. spin_unlock_irqrestore(&wbcir_lock, flags);
  831. return IRQ_NONE;
  832. }
  833. if (status & WBCIR_IRQ_ERR)
  834. data->irdata_error = 1;
  835. if (!(status & WBCIR_IRQ_RX))
  836. goto out;
  837. /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
  838. insb(data->sbase + WBCIR_REG_SP3_RXDATA, &irdata[0], 8);
  839. for (i = 0; i < sizeof(irdata); i++) {
  840. hw = hweight8(irdata[i]);
  841. if (hw > 4)
  842. add_irdata_bit(data, 0);
  843. else
  844. add_irdata_bit(data, 1);
  845. if (hw == 8)
  846. data->idle_count++;
  847. else
  848. data->idle_count = 0;
  849. }
  850. if (data->idle_count > WBCIR_MAX_IDLE_BYTES) {
  851. /* Set RXINACTIVE... */
  852. outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
  853. /* ...and drain the FIFO */
  854. while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL)
  855. inb(data->sbase + WBCIR_REG_SP3_RXDATA);
  856. dev_dbg(dev, "IRDATA:\n");
  857. for (i = 0; i < data->irdata_count; i += BITS_PER_LONG)
  858. dev_dbg(dev, "0x%08lX\n", data->irdata[i/BITS_PER_LONG]);
  859. switch (protocol) {
  860. case IR_PROTOCOL_RC5:
  861. wbcir_parse_rc5(dev, data);
  862. break;
  863. case IR_PROTOCOL_RC6:
  864. wbcir_parse_rc6(dev, data);
  865. break;
  866. case IR_PROTOCOL_NEC:
  867. wbcir_parse_nec(dev, data);
  868. break;
  869. }
  870. wbcir_reset_irdata(data);
  871. }
  872. out:
  873. spin_unlock_irqrestore(&wbcir_lock, flags);
  874. return IRQ_HANDLED;
  875. }
  876. /*****************************************************************************
  877. *
  878. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  879. *
  880. *****************************************************************************/
  881. static void
  882. wbcir_shutdown(struct pnp_dev *device)
  883. {
  884. struct device *dev = &device->dev;
  885. struct wbcir_data *data = pnp_get_drvdata(device);
  886. int do_wake = 1;
  887. u8 match[11];
  888. u8 mask[11];
  889. u8 rc6_csl = 0;
  890. int i;
  891. memset(match, 0, sizeof(match));
  892. memset(mask, 0, sizeof(mask));
  893. if (wake_sc == INVALID_SCANCODE || !device_may_wakeup(dev)) {
  894. do_wake = 0;
  895. goto finish;
  896. }
  897. switch (protocol) {
  898. case IR_PROTOCOL_RC5:
  899. if (wake_sc > 0xFFF) {
  900. do_wake = 0;
  901. dev_err(dev, "RC5 - Invalid wake scancode\n");
  902. break;
  903. }
  904. /* Mask = 13 bits, ex toggle */
  905. mask[0] = 0xFF;
  906. mask[1] = 0x17;
  907. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  908. match[0] |= (wake_sc & 0x0180) >> 1; /* 2 address bits */
  909. match[1] = (wake_sc & 0x0E00) >> 9; /* 3 address bits */
  910. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  911. match[1] |= 0x10;
  912. break;
  913. case IR_PROTOCOL_NEC:
  914. if (wake_sc > 0xFFFFFF) {
  915. do_wake = 0;
  916. dev_err(dev, "NEC - Invalid wake scancode\n");
  917. break;
  918. }
  919. mask[0] = mask[1] = mask[2] = mask[3] = 0xFF;
  920. match[1] = bitrev8((wake_sc & 0xFF));
  921. match[0] = ~match[1];
  922. match[3] = bitrev8((wake_sc & 0xFF00) >> 8);
  923. if (wake_sc > 0xFFFF)
  924. match[2] = bitrev8((wake_sc & 0xFF0000) >> 16);
  925. else
  926. match[2] = ~match[3];
  927. break;
  928. case IR_PROTOCOL_RC6:
  929. if (wake_rc6mode == 0) {
  930. if (wake_sc > 0xFFFF) {
  931. do_wake = 0;
  932. dev_err(dev, "RC6 - Invalid wake scancode\n");
  933. break;
  934. }
  935. /* Command */
  936. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  937. mask[0] = 0xFF;
  938. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  939. mask[1] = 0xFF;
  940. /* Address */
  941. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  942. mask[2] = 0xFF;
  943. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  944. mask[3] = 0xFF;
  945. /* Header */
  946. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  947. mask[4] = 0xF0;
  948. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  949. mask[5] = 0x0F;
  950. rc6_csl = 44;
  951. } else if (wake_rc6mode == 6) {
  952. i = 0;
  953. /* Command */
  954. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  955. mask[i++] = 0xFF;
  956. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  957. mask[i++] = 0xFF;
  958. /* Address + Toggle */
  959. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  960. mask[i++] = 0xFF;
  961. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  962. mask[i++] = 0x3F;
  963. /* Customer bits 7 - 0 */
  964. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  965. mask[i++] = 0xFF;
  966. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  967. mask[i++] = 0xFF;
  968. if (wake_sc & 0x80000000) {
  969. /* Customer range bit and bits 15 - 8 */
  970. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  971. mask[i++] = 0xFF;
  972. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  973. mask[i++] = 0xFF;
  974. rc6_csl = 76;
  975. } else if (wake_sc <= 0x007FFFFF) {
  976. rc6_csl = 60;
  977. } else {
  978. do_wake = 0;
  979. dev_err(dev, "RC6 - Invalid wake scancode\n");
  980. break;
  981. }
  982. /* Header */
  983. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  984. mask[i++] = 0xFF;
  985. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  986. mask[i++] = 0x0F;
  987. } else {
  988. do_wake = 0;
  989. dev_err(dev, "RC6 - Invalid wake mode\n");
  990. }
  991. break;
  992. default:
  993. do_wake = 0;
  994. break;
  995. }
  996. finish:
  997. if (do_wake) {
  998. /* Set compare and compare mask */
  999. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  1000. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  1001. 0x3F);
  1002. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  1003. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  1004. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  1005. 0x3F);
  1006. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  1007. /* RC6 Compare String Len */
  1008. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  1009. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  1010. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  1011. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  1012. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  1013. /* Set CEIR_EN */
  1014. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x01, 0x01);
  1015. } else {
  1016. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  1017. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1018. /* Clear CEIR_EN */
  1019. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  1020. }
  1021. /* Disable interrupts */
  1022. wbcir_select_bank(data, WBCIR_BANK_0);
  1023. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  1024. /*
  1025. * ACPI will set the HW disable bit for SP3 which means that the
  1026. * output signals are left in an undefined state which may cause
  1027. * spurious interrupts which we need to ignore until the hardware
  1028. * is reinitialized.
  1029. */
  1030. disable_irq(data->irq);
  1031. }
  1032. static int
  1033. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  1034. {
  1035. wbcir_shutdown(device);
  1036. return 0;
  1037. }
  1038. static void
  1039. wbcir_init_hw(struct wbcir_data *data)
  1040. {
  1041. u8 tmp;
  1042. /* Disable interrupts */
  1043. wbcir_select_bank(data, WBCIR_BANK_0);
  1044. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  1045. /* Set PROT_SEL, RX_INV, Clear CEIR_EN (needed for the led) */
  1046. tmp = protocol << 4;
  1047. if (invert)
  1048. tmp |= 0x08;
  1049. outb(tmp, data->wbase + WBCIR_REG_WCEIR_CTL);
  1050. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  1051. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  1052. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  1053. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1054. /* Set RC5 cell time to correspond to 36 kHz */
  1055. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  1056. /* Set IRTX_INV */
  1057. if (invert)
  1058. outb(0x04, data->ebase + WBCIR_REG_ECEIR_CCTL);
  1059. else
  1060. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  1061. /*
  1062. * Clear IR LED, set SP3 clock to 24Mhz
  1063. * set SP3_IRRX_SW to binary 01, helpfully not documented
  1064. */
  1065. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  1066. /* Enable extended mode */
  1067. wbcir_select_bank(data, WBCIR_BANK_2);
  1068. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  1069. /*
  1070. * Configure baud generator, IR data will be sampled at
  1071. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  1072. *
  1073. * The ECIR registers include a flag to change the
  1074. * 24Mhz clock freq to 48Mhz.
  1075. *
  1076. * It's not documented in the specs, but fifo levels
  1077. * other than 16 seems to be unsupported.
  1078. */
  1079. /* prescaler 1.0, tx/rx fifo lvl 16 */
  1080. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  1081. /* Set baud divisor to generate one byte per bit/cell */
  1082. switch (protocol) {
  1083. case IR_PROTOCOL_RC5:
  1084. outb(0xA7, data->sbase + WBCIR_REG_SP3_BGDL);
  1085. break;
  1086. case IR_PROTOCOL_RC6:
  1087. outb(0x53, data->sbase + WBCIR_REG_SP3_BGDL);
  1088. break;
  1089. case IR_PROTOCOL_NEC:
  1090. outb(0x69, data->sbase + WBCIR_REG_SP3_BGDL);
  1091. break;
  1092. }
  1093. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  1094. /* Set CEIR mode */
  1095. wbcir_select_bank(data, WBCIR_BANK_0);
  1096. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  1097. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  1098. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  1099. /* Disable RX demod, run-length encoding/decoding, set freq span */
  1100. wbcir_select_bank(data, WBCIR_BANK_7);
  1101. outb(0x10, data->sbase + WBCIR_REG_SP3_RCCFG);
  1102. /* Disable timer */
  1103. wbcir_select_bank(data, WBCIR_BANK_4);
  1104. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  1105. /* Enable MSR interrupt, Clear AUX_IRX */
  1106. wbcir_select_bank(data, WBCIR_BANK_5);
  1107. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR2);
  1108. /* Disable CRC */
  1109. wbcir_select_bank(data, WBCIR_BANK_6);
  1110. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  1111. /* Set RX/TX (de)modulation freq, not really used */
  1112. wbcir_select_bank(data, WBCIR_BANK_7);
  1113. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  1114. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  1115. /* Set invert and pin direction */
  1116. if (invert)
  1117. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  1118. else
  1119. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  1120. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  1121. wbcir_select_bank(data, WBCIR_BANK_0);
  1122. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  1123. /* Clear AUX status bits */
  1124. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  1125. /* Enable interrupts */
  1126. wbcir_reset_irdata(data);
  1127. outb(WBCIR_IRQ_RX | WBCIR_IRQ_ERR, data->sbase + WBCIR_REG_SP3_IER);
  1128. }
  1129. static int
  1130. wbcir_resume(struct pnp_dev *device)
  1131. {
  1132. struct wbcir_data *data = pnp_get_drvdata(device);
  1133. wbcir_init_hw(data);
  1134. enable_irq(data->irq);
  1135. return 0;
  1136. }
  1137. static int __devinit
  1138. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  1139. {
  1140. struct device *dev = &device->dev;
  1141. struct wbcir_data *data;
  1142. int err;
  1143. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  1144. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  1145. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  1146. dev_err(dev, "Invalid resources\n");
  1147. return -ENODEV;
  1148. }
  1149. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1150. if (!data) {
  1151. err = -ENOMEM;
  1152. goto exit;
  1153. }
  1154. pnp_set_drvdata(device, data);
  1155. data->ebase = pnp_port_start(device, 0);
  1156. data->wbase = pnp_port_start(device, 1);
  1157. data->sbase = pnp_port_start(device, 2);
  1158. data->irq = pnp_irq(device, 0);
  1159. if (data->wbase == 0 || data->ebase == 0 ||
  1160. data->sbase == 0 || data->irq == 0) {
  1161. err = -ENODEV;
  1162. dev_err(dev, "Invalid resources\n");
  1163. goto exit_free_data;
  1164. }
  1165. dev_dbg(&device->dev, "Found device "
  1166. "(w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  1167. data->wbase, data->ebase, data->sbase, data->irq);
  1168. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  1169. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  1170. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  1171. err = -EBUSY;
  1172. goto exit_free_data;
  1173. }
  1174. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  1175. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  1176. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  1177. err = -EBUSY;
  1178. goto exit_release_wbase;
  1179. }
  1180. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  1181. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  1182. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  1183. err = -EBUSY;
  1184. goto exit_release_ebase;
  1185. }
  1186. err = request_irq(data->irq, wbcir_irq_handler,
  1187. IRQF_DISABLED, DRVNAME, device);
  1188. if (err) {
  1189. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  1190. err = -EBUSY;
  1191. goto exit_release_sbase;
  1192. }
  1193. led_trigger_register_simple("cir-tx", &data->txtrigger);
  1194. if (!data->txtrigger) {
  1195. err = -ENOMEM;
  1196. goto exit_free_irq;
  1197. }
  1198. led_trigger_register_simple("cir-rx", &data->rxtrigger);
  1199. if (!data->rxtrigger) {
  1200. err = -ENOMEM;
  1201. goto exit_unregister_txtrigger;
  1202. }
  1203. data->led.name = "cir::activity";
  1204. data->led.default_trigger = "cir-rx";
  1205. data->led.brightness_set = wbcir_led_brightness_set;
  1206. data->led.brightness_get = wbcir_led_brightness_get;
  1207. err = led_classdev_register(&device->dev, &data->led);
  1208. if (err)
  1209. goto exit_unregister_rxtrigger;
  1210. data->input_dev = input_allocate_device();
  1211. if (!data->input_dev) {
  1212. err = -ENOMEM;
  1213. goto exit_unregister_led;
  1214. }
  1215. data->input_dev->evbit[0] = BIT(EV_KEY);
  1216. data->input_dev->name = WBCIR_NAME;
  1217. data->input_dev->phys = "wbcir/cir0";
  1218. data->input_dev->id.bustype = BUS_HOST;
  1219. data->input_dev->id.vendor = PCI_VENDOR_ID_WINBOND;
  1220. data->input_dev->id.product = WBCIR_ID_FAMILY;
  1221. data->input_dev->id.version = WBCIR_ID_CHIP;
  1222. data->input_dev->getkeycode = wbcir_getkeycode;
  1223. data->input_dev->setkeycode = wbcir_setkeycode;
  1224. input_set_capability(data->input_dev, EV_MSC, MSC_SCAN);
  1225. input_set_drvdata(data->input_dev, data);
  1226. err = input_register_device(data->input_dev);
  1227. if (err)
  1228. goto exit_free_input;
  1229. data->last_scancode = INVALID_SCANCODE;
  1230. INIT_LIST_HEAD(&data->keytable);
  1231. setup_timer(&data->timer_keyup, wbcir_keyup, (unsigned long)data);
  1232. /* Load default keymaps */
  1233. if (protocol == IR_PROTOCOL_RC6) {
  1234. int i;
  1235. for (i = 0; i < ARRAY_SIZE(rc6_def_keymap); i++) {
  1236. err = wbcir_setkeycode(data->input_dev,
  1237. (int)rc6_def_keymap[i].scancode,
  1238. (int)rc6_def_keymap[i].keycode);
  1239. if (err)
  1240. goto exit_unregister_keys;
  1241. }
  1242. }
  1243. device_init_wakeup(&device->dev, 1);
  1244. wbcir_init_hw(data);
  1245. return 0;
  1246. exit_unregister_keys:
  1247. if (!list_empty(&data->keytable)) {
  1248. struct wbcir_keyentry *key;
  1249. struct wbcir_keyentry *keytmp;
  1250. list_for_each_entry_safe(key, keytmp, &data->keytable, list) {
  1251. list_del(&key->list);
  1252. kfree(key);
  1253. }
  1254. }
  1255. input_unregister_device(data->input_dev);
  1256. /* Can't call input_free_device on an unregistered device */
  1257. data->input_dev = NULL;
  1258. exit_free_input:
  1259. input_free_device(data->input_dev);
  1260. exit_unregister_led:
  1261. led_classdev_unregister(&data->led);
  1262. exit_unregister_rxtrigger:
  1263. led_trigger_unregister_simple(data->rxtrigger);
  1264. exit_unregister_txtrigger:
  1265. led_trigger_unregister_simple(data->txtrigger);
  1266. exit_free_irq:
  1267. free_irq(data->irq, device);
  1268. exit_release_sbase:
  1269. release_region(data->sbase, SP_IOMEM_LEN);
  1270. exit_release_ebase:
  1271. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  1272. exit_release_wbase:
  1273. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  1274. exit_free_data:
  1275. kfree(data);
  1276. pnp_set_drvdata(device, NULL);
  1277. exit:
  1278. return err;
  1279. }
  1280. static void __devexit
  1281. wbcir_remove(struct pnp_dev *device)
  1282. {
  1283. struct wbcir_data *data = pnp_get_drvdata(device);
  1284. struct wbcir_keyentry *key;
  1285. struct wbcir_keyentry *keytmp;
  1286. /* Disable interrupts */
  1287. wbcir_select_bank(data, WBCIR_BANK_0);
  1288. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  1289. del_timer_sync(&data->timer_keyup);
  1290. free_irq(data->irq, device);
  1291. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  1292. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  1293. /* Clear CEIR_EN */
  1294. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  1295. /* Clear BUFF_EN, END_EN, MATCH_EN */
  1296. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1297. /* This will generate a keyup event if necessary */
  1298. input_unregister_device(data->input_dev);
  1299. led_trigger_unregister_simple(data->rxtrigger);
  1300. led_trigger_unregister_simple(data->txtrigger);
  1301. led_classdev_unregister(&data->led);
  1302. /* This is ok since &data->led isn't actually used */
  1303. wbcir_led_brightness_set(&data->led, LED_OFF);
  1304. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  1305. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  1306. release_region(data->sbase, SP_IOMEM_LEN);
  1307. list_for_each_entry_safe(key, keytmp, &data->keytable, list) {
  1308. list_del(&key->list);
  1309. kfree(key);
  1310. }
  1311. kfree(data);
  1312. pnp_set_drvdata(device, NULL);
  1313. }
  1314. static const struct pnp_device_id wbcir_ids[] = {
  1315. { "WEC1022", 0 },
  1316. { "", 0 }
  1317. };
  1318. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  1319. static struct pnp_driver wbcir_driver = {
  1320. .name = WBCIR_NAME,
  1321. .id_table = wbcir_ids,
  1322. .probe = wbcir_probe,
  1323. .remove = __devexit_p(wbcir_remove),
  1324. .suspend = wbcir_suspend,
  1325. .resume = wbcir_resume,
  1326. .shutdown = wbcir_shutdown
  1327. };
  1328. static int __init
  1329. wbcir_init(void)
  1330. {
  1331. int ret;
  1332. switch (protocol) {
  1333. case IR_PROTOCOL_RC5:
  1334. case IR_PROTOCOL_NEC:
  1335. case IR_PROTOCOL_RC6:
  1336. break;
  1337. default:
  1338. printk(KERN_ERR DRVNAME ": Invalid protocol argument\n");
  1339. return -EINVAL;
  1340. }
  1341. ret = pnp_register_driver(&wbcir_driver);
  1342. if (ret)
  1343. printk(KERN_ERR DRVNAME ": Unable to register driver\n");
  1344. return ret;
  1345. }
  1346. static void __exit
  1347. wbcir_exit(void)
  1348. {
  1349. pnp_unregister_driver(&wbcir_driver);
  1350. }
  1351. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1352. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1353. MODULE_LICENSE("GPL");
  1354. module_init(wbcir_init);
  1355. module_exit(wbcir_exit);