winbond-cir.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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, int scancode, int *keycode)
  345. {
  346. struct wbcir_data *data = input_get_drvdata(dev);
  347. *keycode = (int)wbcir_do_getkeycode(data, (u32)scancode);
  348. return 0;
  349. }
  350. static int
  351. wbcir_setkeycode(struct input_dev *dev, int sscancode, int keycode)
  352. {
  353. struct wbcir_data *data = input_get_drvdata(dev);
  354. struct wbcir_keyentry *keyentry;
  355. struct wbcir_keyentry *new_keyentry;
  356. unsigned long flags;
  357. unsigned int old_keycode = KEY_RESERVED;
  358. u32 scancode = (u32)sscancode;
  359. if (keycode < 0 || keycode > KEY_MAX)
  360. return -EINVAL;
  361. new_keyentry = kmalloc(sizeof(*new_keyentry), GFP_KERNEL);
  362. if (!new_keyentry)
  363. return -ENOMEM;
  364. write_lock_irqsave(&keytable_lock, flags);
  365. list_for_each_entry(keyentry, &data->keytable, list) {
  366. if (keyentry->key.scancode != scancode)
  367. continue;
  368. old_keycode = keyentry->key.keycode;
  369. keyentry->key.keycode = keycode;
  370. if (keyentry->key.keycode == KEY_RESERVED) {
  371. list_del(&keyentry->list);
  372. kfree(keyentry);
  373. }
  374. break;
  375. }
  376. set_bit(keycode, dev->keybit);
  377. if (old_keycode == KEY_RESERVED) {
  378. new_keyentry->key.scancode = scancode;
  379. new_keyentry->key.keycode = keycode;
  380. list_add(&new_keyentry->list, &data->keytable);
  381. } else {
  382. kfree(new_keyentry);
  383. clear_bit(old_keycode, dev->keybit);
  384. list_for_each_entry(keyentry, &data->keytable, list) {
  385. if (keyentry->key.keycode == old_keycode) {
  386. set_bit(old_keycode, dev->keybit);
  387. break;
  388. }
  389. }
  390. }
  391. write_unlock_irqrestore(&keytable_lock, flags);
  392. return 0;
  393. }
  394. /*
  395. * Timer function to report keyup event some time after keydown is
  396. * reported by the ISR.
  397. */
  398. static void
  399. wbcir_keyup(unsigned long cookie)
  400. {
  401. struct wbcir_data *data = (struct wbcir_data *)cookie;
  402. unsigned long flags;
  403. /*
  404. * data->keyup_jiffies is used to prevent a race condition if a
  405. * hardware interrupt occurs at this point and the keyup timer
  406. * event is moved further into the future as a result.
  407. *
  408. * The timer will then be reactivated and this function called
  409. * again in the future. We need to exit gracefully in that case
  410. * to allow the input subsystem to do its auto-repeat magic or
  411. * a keyup event might follow immediately after the keydown.
  412. */
  413. spin_lock_irqsave(&wbcir_lock, flags);
  414. if (time_is_after_eq_jiffies(data->keyup_jiffies) && data->keypressed) {
  415. data->keypressed = 0;
  416. led_trigger_event(data->rxtrigger, LED_OFF);
  417. input_report_key(data->input_dev, data->last_keycode, 0);
  418. input_sync(data->input_dev);
  419. }
  420. spin_unlock_irqrestore(&wbcir_lock, flags);
  421. }
  422. static void
  423. wbcir_keydown(struct wbcir_data *data, u32 scancode, u8 toggle)
  424. {
  425. unsigned int keycode;
  426. /* Repeat? */
  427. if (data->last_scancode == scancode &&
  428. data->last_toggle == toggle &&
  429. data->keypressed)
  430. goto set_timer;
  431. data->last_scancode = scancode;
  432. /* Do we need to release an old keypress? */
  433. if (data->keypressed) {
  434. input_report_key(data->input_dev, data->last_keycode, 0);
  435. input_sync(data->input_dev);
  436. data->keypressed = 0;
  437. }
  438. /* Report scancode */
  439. input_event(data->input_dev, EV_MSC, MSC_SCAN, (int)scancode);
  440. /* Do we know this scancode? */
  441. keycode = wbcir_do_getkeycode(data, scancode);
  442. if (keycode == KEY_RESERVED)
  443. goto set_timer;
  444. /* Register a keypress */
  445. input_report_key(data->input_dev, keycode, 1);
  446. data->keypressed = 1;
  447. data->last_keycode = keycode;
  448. data->last_toggle = toggle;
  449. set_timer:
  450. input_sync(data->input_dev);
  451. led_trigger_event(data->rxtrigger,
  452. data->keypressed ? LED_FULL : LED_OFF);
  453. data->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  454. mod_timer(&data->timer_keyup, data->keyup_jiffies);
  455. }
  456. /*****************************************************************************
  457. *
  458. * IR PARSING FUNCTIONS
  459. *
  460. *****************************************************************************/
  461. /* Resets all irdata */
  462. static void
  463. wbcir_reset_irdata(struct wbcir_data *data)
  464. {
  465. memset(data->irdata, 0, sizeof(data->irdata));
  466. data->irdata_count = 0;
  467. data->irdata_off = 0;
  468. data->irdata_error = 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_info(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. data->idle_count = 0;
  872. }
  873. out:
  874. spin_unlock_irqrestore(&wbcir_lock, flags);
  875. return IRQ_HANDLED;
  876. }
  877. /*****************************************************************************
  878. *
  879. * SUSPEND/RESUME FUNCTIONS
  880. *
  881. *****************************************************************************/
  882. static void
  883. wbcir_shutdown(struct pnp_dev *device)
  884. {
  885. struct device *dev = &device->dev;
  886. struct wbcir_data *data = pnp_get_drvdata(device);
  887. int do_wake = 1;
  888. u8 match[11];
  889. u8 mask[11];
  890. u8 rc6_csl = 0;
  891. int i;
  892. memset(match, 0, sizeof(match));
  893. memset(mask, 0, sizeof(mask));
  894. if (wake_sc == INVALID_SCANCODE || !device_may_wakeup(dev)) {
  895. do_wake = 0;
  896. goto finish;
  897. }
  898. switch (protocol) {
  899. case IR_PROTOCOL_RC5:
  900. if (wake_sc > 0xFFF) {
  901. do_wake = 0;
  902. dev_err(dev, "RC5 - Invalid wake scancode\n");
  903. break;
  904. }
  905. /* Mask = 13 bits, ex toggle */
  906. mask[0] = 0xFF;
  907. mask[1] = 0x17;
  908. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  909. match[0] |= (wake_sc & 0x0180) >> 1; /* 2 address bits */
  910. match[1] = (wake_sc & 0x0E00) >> 9; /* 3 address bits */
  911. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  912. match[1] |= 0x10;
  913. break;
  914. case IR_PROTOCOL_NEC:
  915. if (wake_sc > 0xFFFFFF) {
  916. do_wake = 0;
  917. dev_err(dev, "NEC - Invalid wake scancode\n");
  918. break;
  919. }
  920. mask[0] = mask[1] = mask[2] = mask[3] = 0xFF;
  921. match[1] = bitrev8((wake_sc & 0xFF));
  922. match[0] = ~match[1];
  923. match[3] = bitrev8((wake_sc & 0xFF00) >> 8);
  924. if (wake_sc > 0xFFFF)
  925. match[2] = bitrev8((wake_sc & 0xFF0000) >> 16);
  926. else
  927. match[2] = ~match[3];
  928. break;
  929. case IR_PROTOCOL_RC6:
  930. if (wake_rc6mode == 0) {
  931. if (wake_sc > 0xFFFF) {
  932. do_wake = 0;
  933. dev_err(dev, "RC6 - Invalid wake scancode\n");
  934. break;
  935. }
  936. /* Command */
  937. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  938. mask[0] = 0xFF;
  939. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  940. mask[1] = 0xFF;
  941. /* Address */
  942. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  943. mask[2] = 0xFF;
  944. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  945. mask[3] = 0xFF;
  946. /* Header */
  947. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  948. mask[4] = 0xF0;
  949. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  950. mask[5] = 0x0F;
  951. rc6_csl = 44;
  952. } else if (wake_rc6mode == 6) {
  953. i = 0;
  954. /* Command */
  955. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  956. mask[i++] = 0xFF;
  957. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  958. mask[i++] = 0xFF;
  959. /* Address + Toggle */
  960. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  961. mask[i++] = 0xFF;
  962. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  963. mask[i++] = 0x3F;
  964. /* Customer bits 7 - 0 */
  965. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  966. mask[i++] = 0xFF;
  967. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  968. mask[i++] = 0xFF;
  969. if (wake_sc & 0x80000000) {
  970. /* Customer range bit and bits 15 - 8 */
  971. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  972. mask[i++] = 0xFF;
  973. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  974. mask[i++] = 0xFF;
  975. rc6_csl = 76;
  976. } else if (wake_sc <= 0x007FFFFF) {
  977. rc6_csl = 60;
  978. } else {
  979. do_wake = 0;
  980. dev_err(dev, "RC6 - Invalid wake scancode\n");
  981. break;
  982. }
  983. /* Header */
  984. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  985. mask[i++] = 0xFF;
  986. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  987. mask[i++] = 0x0F;
  988. } else {
  989. do_wake = 0;
  990. dev_err(dev, "RC6 - Invalid wake mode\n");
  991. }
  992. break;
  993. default:
  994. do_wake = 0;
  995. break;
  996. }
  997. finish:
  998. if (do_wake) {
  999. /* Set compare and compare mask */
  1000. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  1001. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  1002. 0x3F);
  1003. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  1004. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  1005. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  1006. 0x3F);
  1007. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  1008. /* RC6 Compare String Len */
  1009. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  1010. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  1011. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  1012. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  1013. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  1014. /* Set CEIR_EN */
  1015. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x01, 0x01);
  1016. } else {
  1017. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  1018. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1019. /* Clear CEIR_EN */
  1020. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  1021. }
  1022. /* Disable interrupts */
  1023. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  1024. }
  1025. static int
  1026. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  1027. {
  1028. wbcir_shutdown(device);
  1029. return 0;
  1030. }
  1031. static int
  1032. wbcir_resume(struct pnp_dev *device)
  1033. {
  1034. struct wbcir_data *data = pnp_get_drvdata(device);
  1035. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  1036. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1037. /* Clear CEIR_EN */
  1038. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  1039. /* Enable interrupts */
  1040. wbcir_reset_irdata(data);
  1041. outb(WBCIR_IRQ_RX | WBCIR_IRQ_ERR, data->sbase + WBCIR_REG_SP3_IER);
  1042. return 0;
  1043. }
  1044. /*****************************************************************************
  1045. *
  1046. * SETUP/INIT FUNCTIONS
  1047. *
  1048. *****************************************************************************/
  1049. static void
  1050. wbcir_cfg_ceir(struct wbcir_data *data)
  1051. {
  1052. u8 tmp;
  1053. /* Set PROT_SEL, RX_INV, Clear CEIR_EN (needed for the led) */
  1054. tmp = protocol << 4;
  1055. if (invert)
  1056. tmp |= 0x08;
  1057. outb(tmp, data->wbase + WBCIR_REG_WCEIR_CTL);
  1058. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  1059. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  1060. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  1061. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1062. /* Set RC5 cell time to correspond to 36 kHz */
  1063. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  1064. /* Set IRTX_INV */
  1065. if (invert)
  1066. outb(0x04, data->ebase + WBCIR_REG_ECEIR_CCTL);
  1067. else
  1068. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  1069. /*
  1070. * Clear IR LED, set SP3 clock to 24Mhz
  1071. * set SP3_IRRX_SW to binary 01, helpfully not documented
  1072. */
  1073. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  1074. }
  1075. static int __devinit
  1076. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  1077. {
  1078. struct device *dev = &device->dev;
  1079. struct wbcir_data *data;
  1080. int err;
  1081. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  1082. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  1083. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  1084. dev_err(dev, "Invalid resources\n");
  1085. return -ENODEV;
  1086. }
  1087. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1088. if (!data) {
  1089. err = -ENOMEM;
  1090. goto exit;
  1091. }
  1092. pnp_set_drvdata(device, data);
  1093. data->ebase = pnp_port_start(device, 0);
  1094. data->wbase = pnp_port_start(device, 1);
  1095. data->sbase = pnp_port_start(device, 2);
  1096. data->irq = pnp_irq(device, 0);
  1097. if (data->wbase == 0 || data->ebase == 0 ||
  1098. data->sbase == 0 || data->irq == 0) {
  1099. err = -ENODEV;
  1100. dev_err(dev, "Invalid resources\n");
  1101. goto exit_free_data;
  1102. }
  1103. dev_dbg(&device->dev, "Found device "
  1104. "(w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  1105. data->wbase, data->ebase, data->sbase, data->irq);
  1106. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  1107. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  1108. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  1109. err = -EBUSY;
  1110. goto exit_free_data;
  1111. }
  1112. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  1113. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  1114. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  1115. err = -EBUSY;
  1116. goto exit_release_wbase;
  1117. }
  1118. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  1119. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  1120. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  1121. err = -EBUSY;
  1122. goto exit_release_ebase;
  1123. }
  1124. err = request_irq(data->irq, wbcir_irq_handler,
  1125. IRQF_DISABLED, DRVNAME, device);
  1126. if (err) {
  1127. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  1128. err = -EBUSY;
  1129. goto exit_release_sbase;
  1130. }
  1131. led_trigger_register_simple("cir-tx", &data->txtrigger);
  1132. if (!data->txtrigger) {
  1133. err = -ENOMEM;
  1134. goto exit_free_irq;
  1135. }
  1136. led_trigger_register_simple("cir-rx", &data->rxtrigger);
  1137. if (!data->rxtrigger) {
  1138. err = -ENOMEM;
  1139. goto exit_unregister_txtrigger;
  1140. }
  1141. data->led.name = "cir::activity";
  1142. data->led.default_trigger = "cir-rx";
  1143. data->led.brightness_set = wbcir_led_brightness_set;
  1144. data->led.brightness_get = wbcir_led_brightness_get;
  1145. err = led_classdev_register(&device->dev, &data->led);
  1146. if (err)
  1147. goto exit_unregister_rxtrigger;
  1148. data->input_dev = input_allocate_device();
  1149. if (!data->input_dev) {
  1150. err = -ENOMEM;
  1151. goto exit_unregister_led;
  1152. }
  1153. data->input_dev->evbit[0] = BIT(EV_KEY);
  1154. data->input_dev->name = WBCIR_NAME;
  1155. data->input_dev->phys = "wbcir/cir0";
  1156. data->input_dev->id.bustype = BUS_HOST;
  1157. data->input_dev->id.vendor = PCI_VENDOR_ID_WINBOND;
  1158. data->input_dev->id.product = WBCIR_ID_FAMILY;
  1159. data->input_dev->id.version = WBCIR_ID_CHIP;
  1160. data->input_dev->getkeycode = wbcir_getkeycode;
  1161. data->input_dev->setkeycode = wbcir_setkeycode;
  1162. input_set_capability(data->input_dev, EV_MSC, MSC_SCAN);
  1163. input_set_drvdata(data->input_dev, data);
  1164. err = input_register_device(data->input_dev);
  1165. if (err)
  1166. goto exit_free_input;
  1167. data->last_scancode = INVALID_SCANCODE;
  1168. INIT_LIST_HEAD(&data->keytable);
  1169. setup_timer(&data->timer_keyup, wbcir_keyup, (unsigned long)data);
  1170. /* Load default keymaps */
  1171. if (protocol == IR_PROTOCOL_RC6) {
  1172. int i;
  1173. for (i = 0; i < ARRAY_SIZE(rc6_def_keymap); i++) {
  1174. err = wbcir_setkeycode(data->input_dev,
  1175. (int)rc6_def_keymap[i].scancode,
  1176. (int)rc6_def_keymap[i].keycode);
  1177. if (err)
  1178. goto exit_unregister_keys;
  1179. }
  1180. }
  1181. device_init_wakeup(&device->dev, 1);
  1182. wbcir_cfg_ceir(data);
  1183. /* Disable interrupts */
  1184. wbcir_select_bank(data, WBCIR_BANK_0);
  1185. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  1186. /* Enable extended mode */
  1187. wbcir_select_bank(data, WBCIR_BANK_2);
  1188. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  1189. /*
  1190. * Configure baud generator, IR data will be sampled at
  1191. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  1192. *
  1193. * The ECIR registers include a flag to change the
  1194. * 24Mhz clock freq to 48Mhz.
  1195. *
  1196. * It's not documented in the specs, but fifo levels
  1197. * other than 16 seems to be unsupported.
  1198. */
  1199. /* prescaler 1.0, tx/rx fifo lvl 16 */
  1200. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  1201. /* Set baud divisor to generate one byte per bit/cell */
  1202. switch (protocol) {
  1203. case IR_PROTOCOL_RC5:
  1204. outb(0xA7, data->sbase + WBCIR_REG_SP3_BGDL);
  1205. break;
  1206. case IR_PROTOCOL_RC6:
  1207. outb(0x53, data->sbase + WBCIR_REG_SP3_BGDL);
  1208. break;
  1209. case IR_PROTOCOL_NEC:
  1210. outb(0x69, data->sbase + WBCIR_REG_SP3_BGDL);
  1211. break;
  1212. }
  1213. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  1214. /* Set CEIR mode */
  1215. wbcir_select_bank(data, WBCIR_BANK_0);
  1216. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  1217. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  1218. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  1219. /* Disable RX demod, run-length encoding/decoding, set freq span */
  1220. wbcir_select_bank(data, WBCIR_BANK_7);
  1221. outb(0x10, data->sbase + WBCIR_REG_SP3_RCCFG);
  1222. /* Disable timer */
  1223. wbcir_select_bank(data, WBCIR_BANK_4);
  1224. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  1225. /* Enable MSR interrupt, Clear AUX_IRX */
  1226. wbcir_select_bank(data, WBCIR_BANK_5);
  1227. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR2);
  1228. /* Disable CRC */
  1229. wbcir_select_bank(data, WBCIR_BANK_6);
  1230. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  1231. /* Set RX/TX (de)modulation freq, not really used */
  1232. wbcir_select_bank(data, WBCIR_BANK_7);
  1233. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  1234. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  1235. /* Set invert and pin direction */
  1236. if (invert)
  1237. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  1238. else
  1239. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  1240. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  1241. wbcir_select_bank(data, WBCIR_BANK_0);
  1242. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  1243. /* Clear AUX status bits */
  1244. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  1245. /* Enable interrupts */
  1246. outb(WBCIR_IRQ_RX | WBCIR_IRQ_ERR, data->sbase + WBCIR_REG_SP3_IER);
  1247. return 0;
  1248. exit_unregister_keys:
  1249. if (!list_empty(&data->keytable)) {
  1250. struct wbcir_keyentry *key;
  1251. struct wbcir_keyentry *keytmp;
  1252. list_for_each_entry_safe(key, keytmp, &data->keytable, list) {
  1253. list_del(&key->list);
  1254. kfree(key);
  1255. }
  1256. }
  1257. input_unregister_device(data->input_dev);
  1258. /* Can't call input_free_device on an unregistered device */
  1259. data->input_dev = NULL;
  1260. exit_free_input:
  1261. input_free_device(data->input_dev);
  1262. exit_unregister_led:
  1263. led_classdev_unregister(&data->led);
  1264. exit_unregister_rxtrigger:
  1265. led_trigger_unregister_simple(data->rxtrigger);
  1266. exit_unregister_txtrigger:
  1267. led_trigger_unregister_simple(data->txtrigger);
  1268. exit_free_irq:
  1269. free_irq(data->irq, device);
  1270. exit_release_sbase:
  1271. release_region(data->sbase, SP_IOMEM_LEN);
  1272. exit_release_ebase:
  1273. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  1274. exit_release_wbase:
  1275. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  1276. exit_free_data:
  1277. kfree(data);
  1278. pnp_set_drvdata(device, NULL);
  1279. exit:
  1280. return err;
  1281. }
  1282. static void __devexit
  1283. wbcir_remove(struct pnp_dev *device)
  1284. {
  1285. struct wbcir_data *data = pnp_get_drvdata(device);
  1286. struct wbcir_keyentry *key;
  1287. struct wbcir_keyentry *keytmp;
  1288. /* Disable interrupts */
  1289. wbcir_select_bank(data, WBCIR_BANK_0);
  1290. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  1291. del_timer_sync(&data->timer_keyup);
  1292. free_irq(data->irq, device);
  1293. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  1294. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  1295. /* Clear CEIR_EN */
  1296. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  1297. /* Clear BUFF_EN, END_EN, MATCH_EN */
  1298. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  1299. /* This will generate a keyup event if necessary */
  1300. input_unregister_device(data->input_dev);
  1301. led_trigger_unregister_simple(data->rxtrigger);
  1302. led_trigger_unregister_simple(data->txtrigger);
  1303. led_classdev_unregister(&data->led);
  1304. /* This is ok since &data->led isn't actually used */
  1305. wbcir_led_brightness_set(&data->led, LED_OFF);
  1306. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  1307. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  1308. release_region(data->sbase, SP_IOMEM_LEN);
  1309. list_for_each_entry_safe(key, keytmp, &data->keytable, list) {
  1310. list_del(&key->list);
  1311. kfree(key);
  1312. }
  1313. kfree(data);
  1314. pnp_set_drvdata(device, NULL);
  1315. }
  1316. static const struct pnp_device_id wbcir_ids[] = {
  1317. { "WEC1022", 0 },
  1318. { "", 0 }
  1319. };
  1320. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  1321. static struct pnp_driver wbcir_driver = {
  1322. .name = WBCIR_NAME,
  1323. .id_table = wbcir_ids,
  1324. .probe = wbcir_probe,
  1325. .remove = __devexit_p(wbcir_remove),
  1326. .suspend = wbcir_suspend,
  1327. .resume = wbcir_resume,
  1328. .shutdown = wbcir_shutdown
  1329. };
  1330. static int __init
  1331. wbcir_init(void)
  1332. {
  1333. int ret;
  1334. switch (protocol) {
  1335. case IR_PROTOCOL_RC5:
  1336. case IR_PROTOCOL_NEC:
  1337. case IR_PROTOCOL_RC6:
  1338. break;
  1339. default:
  1340. printk(KERN_ERR DRVNAME ": Invalid protocol argument\n");
  1341. return -EINVAL;
  1342. }
  1343. ret = pnp_register_driver(&wbcir_driver);
  1344. if (ret)
  1345. printk(KERN_ERR DRVNAME ": Unable to register driver\n");
  1346. return ret;
  1347. }
  1348. static void __exit
  1349. wbcir_exit(void)
  1350. {
  1351. pnp_unregister_driver(&wbcir_driver);
  1352. }
  1353. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1354. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1355. MODULE_LICENSE("GPL");
  1356. module_init(wbcir_init);
  1357. module_exit(wbcir_exit);