winbond-cir.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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 - 2010 David Härdeman <david@hardeman.nu>
  11. *
  12. * Dedicated to my daughter Matilda, without whose loving attention this
  13. * 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 IR Receive
  23. * o IR Transmit
  24. * o Wake-On-CIR functionality
  25. *
  26. * To do:
  27. * o Learning
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. */
  43. #include <linux/module.h>
  44. #include <linux/pnp.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/timer.h>
  47. #include <linux/leds.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/pci_ids.h>
  50. #include <linux/io.h>
  51. #include <linux/bitrev.h>
  52. #include <linux/slab.h>
  53. #include <linux/wait.h>
  54. #include <linux/sched.h>
  55. #include <media/rc-core.h>
  56. #define DRVNAME "winbond-cir"
  57. /* CEIR Wake-Up Registers, relative to data->wbase */
  58. #define WBCIR_REG_WCEIR_CTL 0x03 /* CEIR Receiver Control */
  59. #define WBCIR_REG_WCEIR_STS 0x04 /* CEIR Receiver Status */
  60. #define WBCIR_REG_WCEIR_EV_EN 0x05 /* CEIR Receiver Event Enable */
  61. #define WBCIR_REG_WCEIR_CNTL 0x06 /* CEIR Receiver Counter Low */
  62. #define WBCIR_REG_WCEIR_CNTH 0x07 /* CEIR Receiver Counter High */
  63. #define WBCIR_REG_WCEIR_INDEX 0x08 /* CEIR Receiver Index */
  64. #define WBCIR_REG_WCEIR_DATA 0x09 /* CEIR Receiver Data */
  65. #define WBCIR_REG_WCEIR_CSL 0x0A /* CEIR Re. Compare Strlen */
  66. #define WBCIR_REG_WCEIR_CFG1 0x0B /* CEIR Re. Configuration 1 */
  67. #define WBCIR_REG_WCEIR_CFG2 0x0C /* CEIR Re. Configuration 2 */
  68. /* CEIR Enhanced Functionality Registers, relative to data->ebase */
  69. #define WBCIR_REG_ECEIR_CTS 0x00 /* Enhanced IR Control Status */
  70. #define WBCIR_REG_ECEIR_CCTL 0x01 /* Infrared Counter Control */
  71. #define WBCIR_REG_ECEIR_CNT_LO 0x02 /* Infrared Counter LSB */
  72. #define WBCIR_REG_ECEIR_CNT_HI 0x03 /* Infrared Counter MSB */
  73. #define WBCIR_REG_ECEIR_IREM 0x04 /* Infrared Emitter Status */
  74. /* SP3 Banked Registers, relative to data->sbase */
  75. #define WBCIR_REG_SP3_BSR 0x03 /* Bank Select, all banks */
  76. /* Bank 0 */
  77. #define WBCIR_REG_SP3_RXDATA 0x00 /* FIFO RX data (r) */
  78. #define WBCIR_REG_SP3_TXDATA 0x00 /* FIFO TX data (w) */
  79. #define WBCIR_REG_SP3_IER 0x01 /* Interrupt Enable */
  80. #define WBCIR_REG_SP3_EIR 0x02 /* Event Identification (r) */
  81. #define WBCIR_REG_SP3_FCR 0x02 /* FIFO Control (w) */
  82. #define WBCIR_REG_SP3_MCR 0x04 /* Mode Control */
  83. #define WBCIR_REG_SP3_LSR 0x05 /* Link Status */
  84. #define WBCIR_REG_SP3_MSR 0x06 /* Modem Status */
  85. #define WBCIR_REG_SP3_ASCR 0x07 /* Aux Status and Control */
  86. /* Bank 2 */
  87. #define WBCIR_REG_SP3_BGDL 0x00 /* Baud Divisor LSB */
  88. #define WBCIR_REG_SP3_BGDH 0x01 /* Baud Divisor MSB */
  89. #define WBCIR_REG_SP3_EXCR1 0x02 /* Extended Control 1 */
  90. #define WBCIR_REG_SP3_EXCR2 0x04 /* Extended Control 2 */
  91. #define WBCIR_REG_SP3_TXFLV 0x06 /* TX FIFO Level */
  92. #define WBCIR_REG_SP3_RXFLV 0x07 /* RX FIFO Level */
  93. /* Bank 3 */
  94. #define WBCIR_REG_SP3_MRID 0x00 /* Module Identification */
  95. #define WBCIR_REG_SP3_SH_LCR 0x01 /* LCR Shadow */
  96. #define WBCIR_REG_SP3_SH_FCR 0x02 /* FCR Shadow */
  97. /* Bank 4 */
  98. #define WBCIR_REG_SP3_IRCR1 0x02 /* Infrared Control 1 */
  99. /* Bank 5 */
  100. #define WBCIR_REG_SP3_IRCR2 0x04 /* Infrared Control 2 */
  101. /* Bank 6 */
  102. #define WBCIR_REG_SP3_IRCR3 0x00 /* Infrared Control 3 */
  103. #define WBCIR_REG_SP3_SIR_PW 0x02 /* SIR Pulse Width */
  104. /* Bank 7 */
  105. #define WBCIR_REG_SP3_IRRXDC 0x00 /* IR RX Demod Control */
  106. #define WBCIR_REG_SP3_IRTXMC 0x01 /* IR TX Mod Control */
  107. #define WBCIR_REG_SP3_RCCFG 0x02 /* CEIR Config */
  108. #define WBCIR_REG_SP3_IRCFG1 0x04 /* Infrared Config 1 */
  109. #define WBCIR_REG_SP3_IRCFG4 0x07 /* Infrared Config 4 */
  110. /*
  111. * Magic values follow
  112. */
  113. /* No interrupts for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  114. #define WBCIR_IRQ_NONE 0x00
  115. /* RX data bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  116. #define WBCIR_IRQ_RX 0x01
  117. /* TX data low bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  118. #define WBCIR_IRQ_TX_LOW 0x02
  119. /* Over/Under-flow bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  120. #define WBCIR_IRQ_ERR 0x04
  121. /* TX data empty bit for WBCEIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  122. #define WBCIR_IRQ_TX_EMPTY 0x20
  123. /* Led enable/disable bit for WBCIR_REG_ECEIR_CTS */
  124. #define WBCIR_LED_ENABLE 0x80
  125. /* RX data available bit for WBCIR_REG_SP3_LSR */
  126. #define WBCIR_RX_AVAIL 0x01
  127. /* RX data overrun error bit for WBCIR_REG_SP3_LSR */
  128. #define WBCIR_RX_OVERRUN 0x02
  129. /* TX End-Of-Transmission bit for WBCIR_REG_SP3_ASCR */
  130. #define WBCIR_TX_EOT 0x04
  131. /* RX disable bit for WBCIR_REG_SP3_ASCR */
  132. #define WBCIR_RX_DISABLE 0x20
  133. /* TX data underrun error bit for WBCIR_REG_SP3_ASCR */
  134. #define WBCIR_TX_UNDERRUN 0x40
  135. /* Extended mode enable bit for WBCIR_REG_SP3_EXCR1 */
  136. #define WBCIR_EXT_ENABLE 0x01
  137. /* Select compare register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  138. #define WBCIR_REGSEL_COMPARE 0x10
  139. /* Select mask register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  140. #define WBCIR_REGSEL_MASK 0x20
  141. /* Starting address of selected register in WBCIR_REG_WCEIR_INDEX */
  142. #define WBCIR_REG_ADDR0 0x00
  143. /* Valid banks for the SP3 UART */
  144. enum wbcir_bank {
  145. WBCIR_BANK_0 = 0x00,
  146. WBCIR_BANK_1 = 0x80,
  147. WBCIR_BANK_2 = 0xE0,
  148. WBCIR_BANK_3 = 0xE4,
  149. WBCIR_BANK_4 = 0xE8,
  150. WBCIR_BANK_5 = 0xEC,
  151. WBCIR_BANK_6 = 0xF0,
  152. WBCIR_BANK_7 = 0xF4,
  153. };
  154. /* Supported power-on IR Protocols */
  155. enum wbcir_protocol {
  156. IR_PROTOCOL_RC5 = 0x0,
  157. IR_PROTOCOL_NEC = 0x1,
  158. IR_PROTOCOL_RC6 = 0x2,
  159. };
  160. /* Possible states for IR reception */
  161. enum wbcir_rxstate {
  162. WBCIR_RXSTATE_INACTIVE = 0,
  163. WBCIR_RXSTATE_ACTIVE,
  164. WBCIR_RXSTATE_ERROR
  165. };
  166. /* Possible states for IR transmission */
  167. enum wbcir_txstate {
  168. WBCIR_TXSTATE_INACTIVE = 0,
  169. WBCIR_TXSTATE_ACTIVE,
  170. WBCIR_TXSTATE_DONE,
  171. WBCIR_TXSTATE_ERROR
  172. };
  173. /* Misc */
  174. #define WBCIR_NAME "Winbond CIR"
  175. #define WBCIR_ID_FAMILY 0xF1 /* Family ID for the WPCD376I */
  176. #define WBCIR_ID_CHIP 0x04 /* Chip ID for the WPCD376I */
  177. #define INVALID_SCANCODE 0x7FFFFFFF /* Invalid with all protos */
  178. #define WAKEUP_IOMEM_LEN 0x10 /* Wake-Up I/O Reg Len */
  179. #define EHFUNC_IOMEM_LEN 0x10 /* Enhanced Func I/O Reg Len */
  180. #define SP_IOMEM_LEN 0x08 /* Serial Port 3 (IR) Reg Len */
  181. /* Per-device data */
  182. struct wbcir_data {
  183. spinlock_t spinlock;
  184. struct rc_dev *dev;
  185. struct led_classdev led;
  186. unsigned long wbase; /* Wake-Up Baseaddr */
  187. unsigned long ebase; /* Enhanced Func. Baseaddr */
  188. unsigned long sbase; /* Serial Port Baseaddr */
  189. unsigned int irq; /* Serial Port IRQ */
  190. u8 irqmask;
  191. /* RX state */
  192. enum wbcir_rxstate rxstate;
  193. struct led_trigger *rxtrigger;
  194. struct ir_raw_event rxev;
  195. /* TX state */
  196. enum wbcir_txstate txstate;
  197. struct led_trigger *txtrigger;
  198. u32 txlen;
  199. u32 txoff;
  200. u32 *txbuf;
  201. wait_queue_head_t txwaitq;
  202. u8 txmask;
  203. u32 txcarrier;
  204. };
  205. static enum wbcir_protocol protocol = IR_PROTOCOL_RC6;
  206. module_param(protocol, uint, 0444);
  207. MODULE_PARM_DESC(protocol, "IR protocol to use for the power-on command "
  208. "(0 = RC5, 1 = NEC, 2 = RC6A, default)");
  209. static int invert; /* default = 0 */
  210. module_param(invert, bool, 0444);
  211. MODULE_PARM_DESC(invert, "Invert the signal from the IR receiver");
  212. static int txandrx; /* default = 0 */
  213. module_param(txandrx, bool, 0444);
  214. MODULE_PARM_DESC(invert, "Allow simultaneous TX and RX");
  215. static unsigned int wake_sc = 0x800F040C;
  216. module_param(wake_sc, uint, 0644);
  217. MODULE_PARM_DESC(wake_sc, "Scancode of the power-on IR command");
  218. static unsigned int wake_rc6mode = 6;
  219. module_param(wake_rc6mode, uint, 0644);
  220. MODULE_PARM_DESC(wake_rc6mode, "RC6 mode for the power-on command "
  221. "(0 = 0, 6 = 6A, default)");
  222. /*****************************************************************************
  223. *
  224. * UTILITY FUNCTIONS
  225. *
  226. *****************************************************************************/
  227. /* Caller needs to hold wbcir_lock */
  228. static void
  229. wbcir_set_bits(unsigned long addr, u8 bits, u8 mask)
  230. {
  231. u8 val;
  232. val = inb(addr);
  233. val = ((val & ~mask) | (bits & mask));
  234. outb(val, addr);
  235. }
  236. /* Selects the register bank for the serial port */
  237. static inline void
  238. wbcir_select_bank(struct wbcir_data *data, enum wbcir_bank bank)
  239. {
  240. outb(bank, data->sbase + WBCIR_REG_SP3_BSR);
  241. }
  242. static inline void
  243. wbcir_set_irqmask(struct wbcir_data *data, u8 irqmask)
  244. {
  245. if (data->irqmask == irqmask)
  246. return;
  247. wbcir_select_bank(data, WBCIR_BANK_0);
  248. outb(irqmask, data->sbase + WBCIR_REG_SP3_IER);
  249. data->irqmask = irqmask;
  250. }
  251. static enum led_brightness
  252. wbcir_led_brightness_get(struct led_classdev *led_cdev)
  253. {
  254. struct wbcir_data *data = container_of(led_cdev,
  255. struct wbcir_data,
  256. led);
  257. if (inb(data->ebase + WBCIR_REG_ECEIR_CTS) & WBCIR_LED_ENABLE)
  258. return LED_FULL;
  259. else
  260. return LED_OFF;
  261. }
  262. static void
  263. wbcir_led_brightness_set(struct led_classdev *led_cdev,
  264. enum led_brightness brightness)
  265. {
  266. struct wbcir_data *data = container_of(led_cdev,
  267. struct wbcir_data,
  268. led);
  269. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS,
  270. brightness == LED_OFF ? 0x00 : WBCIR_LED_ENABLE,
  271. WBCIR_LED_ENABLE);
  272. }
  273. /* Manchester encodes bits to RC6 message cells (see wbcir_shutdown) */
  274. static u8
  275. wbcir_to_rc6cells(u8 val)
  276. {
  277. u8 coded = 0x00;
  278. int i;
  279. val &= 0x0F;
  280. for (i = 0; i < 4; i++) {
  281. if (val & 0x01)
  282. coded |= 0x02 << (i * 2);
  283. else
  284. coded |= 0x01 << (i * 2);
  285. val >>= 1;
  286. }
  287. return coded;
  288. }
  289. /*****************************************************************************
  290. *
  291. * INTERRUPT FUNCTIONS
  292. *
  293. *****************************************************************************/
  294. static void
  295. wbcir_irq_rx(struct wbcir_data *data, struct pnp_dev *device)
  296. {
  297. u8 irdata[8];
  298. bool disable = true;
  299. unsigned int i;
  300. if (data->rxstate == WBCIR_RXSTATE_INACTIVE) {
  301. data->rxstate = WBCIR_RXSTATE_ACTIVE;
  302. led_trigger_event(data->rxtrigger, LED_FULL);
  303. }
  304. /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
  305. insb(data->sbase + WBCIR_REG_SP3_RXDATA, &irdata[0], 8);
  306. for (i = 0; i < 8; i++) {
  307. u8 pulse;
  308. u32 duration;
  309. if (irdata[i] != 0xFF && irdata[i] != 0x00)
  310. disable = false;
  311. if (data->rxstate == WBCIR_RXSTATE_ERROR)
  312. continue;
  313. pulse = irdata[i] & 0x80 ? false : true;
  314. duration = (irdata[i] & 0x7F) * 10000; /* ns */
  315. if (data->rxev.pulse != pulse) {
  316. if (data->rxev.duration != 0) {
  317. ir_raw_event_store(data->dev, &data->rxev);
  318. data->rxev.duration = 0;
  319. }
  320. data->rxev.pulse = pulse;
  321. }
  322. data->rxev.duration += duration;
  323. }
  324. if (disable) {
  325. if (data->rxev.duration != 0 &&
  326. data->rxstate != WBCIR_RXSTATE_ERROR) {
  327. ir_raw_event_store(data->dev, &data->rxev);
  328. data->rxev.duration = 0;
  329. }
  330. /* Set RXINACTIVE */
  331. outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
  332. /* Drain the FIFO */
  333. while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL)
  334. inb(data->sbase + WBCIR_REG_SP3_RXDATA);
  335. ir_raw_event_reset(data->dev);
  336. led_trigger_event(data->rxtrigger, LED_OFF);
  337. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  338. }
  339. ir_raw_event_handle(data->dev);
  340. }
  341. static void
  342. wbcir_irq_tx(struct wbcir_data *data)
  343. {
  344. unsigned int space;
  345. unsigned int used;
  346. u8 bytes[16];
  347. u8 byte;
  348. if (!data->txbuf)
  349. return;
  350. switch (data->txstate) {
  351. case WBCIR_TXSTATE_INACTIVE:
  352. /* TX FIFO empty */
  353. space = 16;
  354. led_trigger_event(data->txtrigger, LED_FULL);
  355. break;
  356. case WBCIR_TXSTATE_ACTIVE:
  357. /* TX FIFO low (3 bytes or less) */
  358. space = 13;
  359. break;
  360. case WBCIR_TXSTATE_ERROR:
  361. space = 0;
  362. break;
  363. default:
  364. return;
  365. }
  366. /*
  367. * TX data is run-length coded in bytes: YXXXXXXX
  368. * Y = space (1) or pulse (0)
  369. * X = duration, encoded as (X + 1) * 10us (i.e 10 to 1280 us)
  370. */
  371. for (used = 0; used < space && data->txoff != data->txlen; used++) {
  372. if (data->txbuf[data->txoff] == 0) {
  373. data->txoff++;
  374. continue;
  375. }
  376. byte = min((u32)0x80, data->txbuf[data->txoff]);
  377. data->txbuf[data->txoff] -= byte;
  378. byte--;
  379. byte |= (data->txoff % 2 ? 0x80 : 0x00); /* pulse/space */
  380. bytes[used] = byte;
  381. }
  382. while (data->txbuf[data->txoff] == 0 && data->txoff != data->txlen)
  383. data->txoff++;
  384. if (used == 0) {
  385. /* Finished */
  386. if (data->txstate == WBCIR_TXSTATE_ERROR)
  387. /* Clear TX underrun bit */
  388. outb(WBCIR_TX_UNDERRUN, data->sbase + WBCIR_REG_SP3_ASCR);
  389. else
  390. data->txstate = WBCIR_TXSTATE_DONE;
  391. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  392. led_trigger_event(data->txtrigger, LED_OFF);
  393. wake_up(&data->txwaitq);
  394. } else if (data->txoff == data->txlen) {
  395. /* At the end of transmission, tell the hw before last byte */
  396. outsb(data->sbase + WBCIR_REG_SP3_TXDATA, bytes, used - 1);
  397. outb(WBCIR_TX_EOT, data->sbase + WBCIR_REG_SP3_ASCR);
  398. outb(bytes[used - 1], data->sbase + WBCIR_REG_SP3_TXDATA);
  399. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  400. WBCIR_IRQ_TX_EMPTY);
  401. } else {
  402. /* More data to follow... */
  403. outsb(data->sbase + WBCIR_REG_SP3_RXDATA, bytes, used);
  404. if (data->txstate == WBCIR_TXSTATE_INACTIVE) {
  405. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  406. WBCIR_IRQ_TX_LOW);
  407. data->txstate = WBCIR_TXSTATE_ACTIVE;
  408. }
  409. }
  410. }
  411. static irqreturn_t
  412. wbcir_irq_handler(int irqno, void *cookie)
  413. {
  414. struct pnp_dev *device = cookie;
  415. struct wbcir_data *data = pnp_get_drvdata(device);
  416. unsigned long flags;
  417. u8 status;
  418. spin_lock_irqsave(&data->spinlock, flags);
  419. wbcir_select_bank(data, WBCIR_BANK_0);
  420. status = inb(data->sbase + WBCIR_REG_SP3_EIR);
  421. status &= data->irqmask;
  422. if (!status) {
  423. spin_unlock_irqrestore(&data->spinlock, flags);
  424. return IRQ_NONE;
  425. }
  426. if (status & WBCIR_IRQ_ERR) {
  427. /* RX overflow? (read clears bit) */
  428. if (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_OVERRUN) {
  429. data->rxstate = WBCIR_RXSTATE_ERROR;
  430. ir_raw_event_reset(data->dev);
  431. }
  432. /* TX underflow? */
  433. if (inb(data->sbase + WBCIR_REG_SP3_ASCR) & WBCIR_TX_UNDERRUN)
  434. data->txstate = WBCIR_TXSTATE_ERROR;
  435. }
  436. if (status & WBCIR_IRQ_RX)
  437. wbcir_irq_rx(data, device);
  438. if (status & (WBCIR_IRQ_TX_LOW | WBCIR_IRQ_TX_EMPTY))
  439. wbcir_irq_tx(data);
  440. spin_unlock_irqrestore(&data->spinlock, flags);
  441. return IRQ_HANDLED;
  442. }
  443. /*****************************************************************************
  444. *
  445. * RC-CORE INTERFACE FUNCTIONS
  446. *
  447. *****************************************************************************/
  448. static int
  449. wbcir_txcarrier(struct rc_dev *dev, u32 carrier)
  450. {
  451. struct wbcir_data *data = dev->priv;
  452. unsigned long flags;
  453. u8 val;
  454. u32 freq;
  455. freq = DIV_ROUND_CLOSEST(carrier, 1000);
  456. if (freq < 30 || freq > 60)
  457. return -EINVAL;
  458. switch (freq) {
  459. case 58:
  460. case 59:
  461. case 60:
  462. val = freq - 58;
  463. freq *= 1000;
  464. break;
  465. case 57:
  466. val = freq - 27;
  467. freq = 56900;
  468. break;
  469. default:
  470. val = freq - 27;
  471. freq *= 1000;
  472. break;
  473. }
  474. spin_lock_irqsave(&data->spinlock, flags);
  475. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  476. spin_unlock_irqrestore(&data->spinlock, flags);
  477. return -EBUSY;
  478. }
  479. if (data->txcarrier != freq) {
  480. wbcir_select_bank(data, WBCIR_BANK_7);
  481. wbcir_set_bits(data->sbase + WBCIR_REG_SP3_IRTXMC, val, 0x1F);
  482. data->txcarrier = freq;
  483. }
  484. spin_unlock_irqrestore(&data->spinlock, flags);
  485. return 0;
  486. }
  487. static int
  488. wbcir_txmask(struct rc_dev *dev, u32 mask)
  489. {
  490. struct wbcir_data *data = dev->priv;
  491. unsigned long flags;
  492. u8 val;
  493. /* Four outputs, only one output can be enabled at a time */
  494. switch (mask) {
  495. case 0x1:
  496. val = 0x0;
  497. break;
  498. case 0x2:
  499. val = 0x1;
  500. break;
  501. case 0x4:
  502. val = 0x2;
  503. break;
  504. case 0x8:
  505. val = 0x3;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. spin_lock_irqsave(&data->spinlock, flags);
  511. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  512. spin_unlock_irqrestore(&data->spinlock, flags);
  513. return -EBUSY;
  514. }
  515. if (data->txmask != mask) {
  516. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS, val, 0x0c);
  517. data->txmask = mask;
  518. }
  519. spin_unlock_irqrestore(&data->spinlock, flags);
  520. return 0;
  521. }
  522. static int
  523. wbcir_tx(struct rc_dev *dev, int *buf, u32 bufsize)
  524. {
  525. struct wbcir_data *data = dev->priv;
  526. u32 count;
  527. unsigned i;
  528. unsigned long flags;
  529. /* bufsize has been sanity checked by the caller */
  530. count = bufsize / sizeof(int);
  531. /* Not sure if this is possible, but better safe than sorry */
  532. spin_lock_irqsave(&data->spinlock, flags);
  533. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  534. spin_unlock_irqrestore(&data->spinlock, flags);
  535. return -EBUSY;
  536. }
  537. /* Convert values to multiples of 10us */
  538. for (i = 0; i < count; i++)
  539. buf[i] = DIV_ROUND_CLOSEST(buf[i], 10);
  540. /* Fill the TX fifo once, the irq handler will do the rest */
  541. data->txbuf = buf;
  542. data->txlen = count;
  543. data->txoff = 0;
  544. wbcir_irq_tx(data);
  545. /* Wait for the TX to complete */
  546. while (data->txstate == WBCIR_TXSTATE_ACTIVE) {
  547. spin_unlock_irqrestore(&data->spinlock, flags);
  548. wait_event(data->txwaitq, data->txstate != WBCIR_TXSTATE_ACTIVE);
  549. spin_lock_irqsave(&data->spinlock, flags);
  550. }
  551. /* We're done */
  552. if (data->txstate == WBCIR_TXSTATE_ERROR)
  553. count = -EAGAIN;
  554. data->txstate = WBCIR_TXSTATE_INACTIVE;
  555. data->txbuf = NULL;
  556. spin_unlock_irqrestore(&data->spinlock, flags);
  557. return count;
  558. }
  559. /*****************************************************************************
  560. *
  561. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  562. *
  563. *****************************************************************************/
  564. static void
  565. wbcir_shutdown(struct pnp_dev *device)
  566. {
  567. struct device *dev = &device->dev;
  568. struct wbcir_data *data = pnp_get_drvdata(device);
  569. bool do_wake = true;
  570. u8 match[11];
  571. u8 mask[11];
  572. u8 rc6_csl = 0;
  573. int i;
  574. memset(match, 0, sizeof(match));
  575. memset(mask, 0, sizeof(mask));
  576. if (wake_sc == INVALID_SCANCODE || !device_may_wakeup(dev)) {
  577. do_wake = false;
  578. goto finish;
  579. }
  580. switch (protocol) {
  581. case IR_PROTOCOL_RC5:
  582. if (wake_sc > 0xFFF) {
  583. do_wake = false;
  584. dev_err(dev, "RC5 - Invalid wake scancode\n");
  585. break;
  586. }
  587. /* Mask = 13 bits, ex toggle */
  588. mask[0] = 0xFF;
  589. mask[1] = 0x17;
  590. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  591. match[0] |= (wake_sc & 0x0180) >> 1; /* 2 address bits */
  592. match[1] = (wake_sc & 0x0E00) >> 9; /* 3 address bits */
  593. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  594. match[1] |= 0x10;
  595. break;
  596. case IR_PROTOCOL_NEC:
  597. if (wake_sc > 0xFFFFFF) {
  598. do_wake = false;
  599. dev_err(dev, "NEC - Invalid wake scancode\n");
  600. break;
  601. }
  602. mask[0] = mask[1] = mask[2] = mask[3] = 0xFF;
  603. match[1] = bitrev8((wake_sc & 0xFF));
  604. match[0] = ~match[1];
  605. match[3] = bitrev8((wake_sc & 0xFF00) >> 8);
  606. if (wake_sc > 0xFFFF)
  607. match[2] = bitrev8((wake_sc & 0xFF0000) >> 16);
  608. else
  609. match[2] = ~match[3];
  610. break;
  611. case IR_PROTOCOL_RC6:
  612. if (wake_rc6mode == 0) {
  613. if (wake_sc > 0xFFFF) {
  614. do_wake = false;
  615. dev_err(dev, "RC6 - Invalid wake scancode\n");
  616. break;
  617. }
  618. /* Command */
  619. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  620. mask[0] = 0xFF;
  621. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  622. mask[1] = 0xFF;
  623. /* Address */
  624. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  625. mask[2] = 0xFF;
  626. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  627. mask[3] = 0xFF;
  628. /* Header */
  629. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  630. mask[4] = 0xF0;
  631. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  632. mask[5] = 0x0F;
  633. rc6_csl = 44;
  634. } else if (wake_rc6mode == 6) {
  635. i = 0;
  636. /* Command */
  637. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  638. mask[i++] = 0xFF;
  639. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  640. mask[i++] = 0xFF;
  641. /* Address + Toggle */
  642. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  643. mask[i++] = 0xFF;
  644. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  645. mask[i++] = 0x3F;
  646. /* Customer bits 7 - 0 */
  647. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  648. mask[i++] = 0xFF;
  649. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  650. mask[i++] = 0xFF;
  651. if (wake_sc & 0x80000000) {
  652. /* Customer range bit and bits 15 - 8 */
  653. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  654. mask[i++] = 0xFF;
  655. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  656. mask[i++] = 0xFF;
  657. rc6_csl = 76;
  658. } else if (wake_sc <= 0x007FFFFF) {
  659. rc6_csl = 60;
  660. } else {
  661. do_wake = false;
  662. dev_err(dev, "RC6 - Invalid wake scancode\n");
  663. break;
  664. }
  665. /* Header */
  666. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  667. mask[i++] = 0xFF;
  668. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  669. mask[i++] = 0x0F;
  670. } else {
  671. do_wake = false;
  672. dev_err(dev, "RC6 - Invalid wake mode\n");
  673. }
  674. break;
  675. default:
  676. do_wake = false;
  677. break;
  678. }
  679. finish:
  680. if (do_wake) {
  681. /* Set compare and compare mask */
  682. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  683. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  684. 0x3F);
  685. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  686. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  687. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  688. 0x3F);
  689. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  690. /* RC6 Compare String Len */
  691. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  692. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  693. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  694. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  695. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  696. /* Set CEIR_EN */
  697. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x01, 0x01);
  698. } else {
  699. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  700. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  701. /* Clear CEIR_EN */
  702. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  703. }
  704. /*
  705. * ACPI will set the HW disable bit for SP3 which means that the
  706. * output signals are left in an undefined state which may cause
  707. * spurious interrupts which we need to ignore until the hardware
  708. * is reinitialized.
  709. */
  710. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  711. disable_irq(data->irq);
  712. /* Disable LED */
  713. led_trigger_event(data->rxtrigger, LED_OFF);
  714. led_trigger_event(data->txtrigger, LED_OFF);
  715. }
  716. static int
  717. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  718. {
  719. wbcir_shutdown(device);
  720. return 0;
  721. }
  722. static void
  723. wbcir_init_hw(struct wbcir_data *data)
  724. {
  725. u8 tmp;
  726. /* Disable interrupts */
  727. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  728. /* Set PROT_SEL, RX_INV, Clear CEIR_EN (needed for the led) */
  729. tmp = protocol << 4;
  730. if (invert)
  731. tmp |= 0x08;
  732. outb(tmp, data->wbase + WBCIR_REG_WCEIR_CTL);
  733. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  734. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  735. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  736. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  737. /* Set RC5 cell time to correspond to 36 kHz */
  738. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  739. /* Set IRTX_INV */
  740. if (invert)
  741. outb(0x04, data->ebase + WBCIR_REG_ECEIR_CCTL);
  742. else
  743. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  744. /*
  745. * Clear IR LED, set SP3 clock to 24Mhz, set TX mask to IRTX1,
  746. * set SP3_IRRX_SW to binary 01, helpfully not documented
  747. */
  748. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  749. data->txmask = 0x1;
  750. /* Enable extended mode */
  751. wbcir_select_bank(data, WBCIR_BANK_2);
  752. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  753. /*
  754. * Configure baud generator, IR data will be sampled at
  755. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  756. *
  757. * The ECIR registers include a flag to change the
  758. * 24Mhz clock freq to 48Mhz.
  759. *
  760. * It's not documented in the specs, but fifo levels
  761. * other than 16 seems to be unsupported.
  762. */
  763. /* prescaler 1.0, tx/rx fifo lvl 16 */
  764. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  765. /* Set baud divisor to generate one byte per bit/cell */
  766. switch (protocol) {
  767. case IR_PROTOCOL_RC5:
  768. outb(0xA7, data->sbase + WBCIR_REG_SP3_BGDL);
  769. break;
  770. case IR_PROTOCOL_RC6:
  771. outb(0x53, data->sbase + WBCIR_REG_SP3_BGDL);
  772. break;
  773. case IR_PROTOCOL_NEC:
  774. outb(0x69, data->sbase + WBCIR_REG_SP3_BGDL);
  775. break;
  776. }
  777. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  778. /* Set CEIR mode */
  779. wbcir_select_bank(data, WBCIR_BANK_0);
  780. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  781. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  782. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  783. /* Disable RX demod, run-length encoding/decoding, set freq span */
  784. wbcir_select_bank(data, WBCIR_BANK_7);
  785. outb(0x10, data->sbase + WBCIR_REG_SP3_RCCFG);
  786. /* Disable timer */
  787. wbcir_select_bank(data, WBCIR_BANK_4);
  788. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  789. /* Disable MSR interrupt, clear AUX_IRX, mask RX during TX? */
  790. wbcir_select_bank(data, WBCIR_BANK_5);
  791. outb(txandrx ? 0x03 : 0x02, data->sbase + WBCIR_REG_SP3_IRCR2);
  792. /* Disable CRC */
  793. wbcir_select_bank(data, WBCIR_BANK_6);
  794. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  795. /* Set RX demodulation freq, not really used */
  796. wbcir_select_bank(data, WBCIR_BANK_7);
  797. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  798. /* Set TX modulation, 36kHz, 7us pulse width */
  799. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  800. data->txcarrier = 36000;
  801. /* Set invert and pin direction */
  802. if (invert)
  803. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  804. else
  805. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  806. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  807. wbcir_select_bank(data, WBCIR_BANK_0);
  808. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  809. /* Clear AUX status bits */
  810. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  811. /* Clear RX state */
  812. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  813. data->rxev.duration = 0;
  814. ir_raw_event_reset(data->dev);
  815. ir_raw_event_handle(data->dev);
  816. /*
  817. * Check TX state, if we did a suspend/resume cycle while TX was
  818. * active, we will have a process waiting in txwaitq.
  819. */
  820. if (data->txstate == WBCIR_TXSTATE_ACTIVE) {
  821. data->txstate = WBCIR_TXSTATE_ERROR;
  822. wake_up(&data->txwaitq);
  823. }
  824. /* Enable interrupts */
  825. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  826. }
  827. static int
  828. wbcir_resume(struct pnp_dev *device)
  829. {
  830. struct wbcir_data *data = pnp_get_drvdata(device);
  831. wbcir_init_hw(data);
  832. enable_irq(data->irq);
  833. return 0;
  834. }
  835. static int __devinit
  836. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  837. {
  838. struct device *dev = &device->dev;
  839. struct wbcir_data *data;
  840. int err;
  841. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  842. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  843. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  844. dev_err(dev, "Invalid resources\n");
  845. return -ENODEV;
  846. }
  847. data = kzalloc(sizeof(*data), GFP_KERNEL);
  848. if (!data) {
  849. err = -ENOMEM;
  850. goto exit;
  851. }
  852. pnp_set_drvdata(device, data);
  853. spin_lock_init(&data->spinlock);
  854. init_waitqueue_head(&data->txwaitq);
  855. data->ebase = pnp_port_start(device, 0);
  856. data->wbase = pnp_port_start(device, 1);
  857. data->sbase = pnp_port_start(device, 2);
  858. data->irq = pnp_irq(device, 0);
  859. if (data->wbase == 0 || data->ebase == 0 ||
  860. data->sbase == 0 || data->irq == 0) {
  861. err = -ENODEV;
  862. dev_err(dev, "Invalid resources\n");
  863. goto exit_free_data;
  864. }
  865. dev_dbg(&device->dev, "Found device "
  866. "(w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  867. data->wbase, data->ebase, data->sbase, data->irq);
  868. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  869. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  870. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  871. err = -EBUSY;
  872. goto exit_free_data;
  873. }
  874. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  875. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  876. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  877. err = -EBUSY;
  878. goto exit_release_wbase;
  879. }
  880. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  881. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  882. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  883. err = -EBUSY;
  884. goto exit_release_ebase;
  885. }
  886. err = request_irq(data->irq, wbcir_irq_handler,
  887. IRQF_DISABLED, DRVNAME, device);
  888. if (err) {
  889. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  890. err = -EBUSY;
  891. goto exit_release_sbase;
  892. }
  893. led_trigger_register_simple("cir-tx", &data->txtrigger);
  894. if (!data->txtrigger) {
  895. err = -ENOMEM;
  896. goto exit_free_irq;
  897. }
  898. led_trigger_register_simple("cir-rx", &data->rxtrigger);
  899. if (!data->rxtrigger) {
  900. err = -ENOMEM;
  901. goto exit_unregister_txtrigger;
  902. }
  903. data->led.name = "cir::activity";
  904. data->led.default_trigger = "cir-rx";
  905. data->led.brightness_set = wbcir_led_brightness_set;
  906. data->led.brightness_get = wbcir_led_brightness_get;
  907. err = led_classdev_register(&device->dev, &data->led);
  908. if (err)
  909. goto exit_unregister_rxtrigger;
  910. data->dev = rc_allocate_device();
  911. if (!data->dev) {
  912. err = -ENOMEM;
  913. goto exit_unregister_led;
  914. }
  915. data->dev->driver_name = WBCIR_NAME;
  916. data->dev->input_name = WBCIR_NAME;
  917. data->dev->input_phys = "wbcir/cir0";
  918. data->dev->input_id.bustype = BUS_HOST;
  919. data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
  920. data->dev->input_id.product = WBCIR_ID_FAMILY;
  921. data->dev->input_id.version = WBCIR_ID_CHIP;
  922. data->dev->map_name = RC_MAP_RC6_MCE;
  923. data->dev->s_tx_mask = wbcir_txmask;
  924. data->dev->s_tx_carrier = wbcir_txcarrier;
  925. data->dev->tx_ir = wbcir_tx;
  926. data->dev->priv = data;
  927. data->dev->dev.parent = &device->dev;
  928. err = rc_register_device(data->dev);
  929. if (err)
  930. goto exit_free_rc;
  931. device_init_wakeup(&device->dev, 1);
  932. wbcir_init_hw(data);
  933. return 0;
  934. exit_free_rc:
  935. rc_free_device(data->dev);
  936. exit_unregister_led:
  937. led_classdev_unregister(&data->led);
  938. exit_unregister_rxtrigger:
  939. led_trigger_unregister_simple(data->rxtrigger);
  940. exit_unregister_txtrigger:
  941. led_trigger_unregister_simple(data->txtrigger);
  942. exit_free_irq:
  943. free_irq(data->irq, device);
  944. exit_release_sbase:
  945. release_region(data->sbase, SP_IOMEM_LEN);
  946. exit_release_ebase:
  947. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  948. exit_release_wbase:
  949. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  950. exit_free_data:
  951. kfree(data);
  952. pnp_set_drvdata(device, NULL);
  953. exit:
  954. return err;
  955. }
  956. static void __devexit
  957. wbcir_remove(struct pnp_dev *device)
  958. {
  959. struct wbcir_data *data = pnp_get_drvdata(device);
  960. /* Disable interrupts */
  961. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  962. free_irq(data->irq, device);
  963. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  964. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  965. /* Clear CEIR_EN */
  966. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  967. /* Clear BUFF_EN, END_EN, MATCH_EN */
  968. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  969. rc_unregister_device(data->dev);
  970. led_trigger_unregister_simple(data->rxtrigger);
  971. led_trigger_unregister_simple(data->txtrigger);
  972. led_classdev_unregister(&data->led);
  973. /* This is ok since &data->led isn't actually used */
  974. wbcir_led_brightness_set(&data->led, LED_OFF);
  975. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  976. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  977. release_region(data->sbase, SP_IOMEM_LEN);
  978. kfree(data);
  979. pnp_set_drvdata(device, NULL);
  980. }
  981. static const struct pnp_device_id wbcir_ids[] = {
  982. { "WEC1022", 0 },
  983. { "", 0 }
  984. };
  985. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  986. static struct pnp_driver wbcir_driver = {
  987. .name = WBCIR_NAME,
  988. .id_table = wbcir_ids,
  989. .probe = wbcir_probe,
  990. .remove = __devexit_p(wbcir_remove),
  991. .suspend = wbcir_suspend,
  992. .resume = wbcir_resume,
  993. .shutdown = wbcir_shutdown
  994. };
  995. static int __init
  996. wbcir_init(void)
  997. {
  998. int ret;
  999. switch (protocol) {
  1000. case IR_PROTOCOL_RC5:
  1001. case IR_PROTOCOL_NEC:
  1002. case IR_PROTOCOL_RC6:
  1003. break;
  1004. default:
  1005. printk(KERN_ERR DRVNAME ": Invalid power-on protocol\n");
  1006. }
  1007. ret = pnp_register_driver(&wbcir_driver);
  1008. if (ret)
  1009. printk(KERN_ERR DRVNAME ": Unable to register driver\n");
  1010. return ret;
  1011. }
  1012. static void __exit
  1013. wbcir_exit(void)
  1014. {
  1015. pnp_unregister_driver(&wbcir_driver);
  1016. }
  1017. module_init(wbcir_init);
  1018. module_exit(wbcir_exit);
  1019. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1020. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1021. MODULE_LICENSE("GPL");