winbond-cir.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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_idle_rx(struct rc_dev *dev, bool idle)
  296. {
  297. struct wbcir_data *data = dev->priv;
  298. if (!idle && data->rxstate == WBCIR_RXSTATE_INACTIVE) {
  299. data->rxstate = WBCIR_RXSTATE_ACTIVE;
  300. led_trigger_event(data->rxtrigger, LED_FULL);
  301. }
  302. if (idle && data->rxstate != WBCIR_RXSTATE_INACTIVE)
  303. /* Tell hardware to go idle by setting RXINACTIVE */
  304. outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
  305. }
  306. static void
  307. wbcir_irq_rx(struct wbcir_data *data, struct pnp_dev *device)
  308. {
  309. u8 irdata;
  310. DEFINE_IR_RAW_EVENT(rawir);
  311. /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
  312. while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL) {
  313. irdata = inb(data->sbase + WBCIR_REG_SP3_RXDATA);
  314. if (data->rxstate == WBCIR_RXSTATE_ERROR)
  315. continue;
  316. rawir.pulse = irdata & 0x80 ? false : true;
  317. rawir.duration = US_TO_NS((irdata & 0x7F) * 10);
  318. ir_raw_event_store_with_filter(data->dev, &rawir);
  319. }
  320. /* Check if we should go idle */
  321. if (data->dev->idle) {
  322. led_trigger_event(data->rxtrigger, LED_OFF);
  323. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  324. }
  325. ir_raw_event_handle(data->dev);
  326. }
  327. static void
  328. wbcir_irq_tx(struct wbcir_data *data)
  329. {
  330. unsigned int space;
  331. unsigned int used;
  332. u8 bytes[16];
  333. u8 byte;
  334. if (!data->txbuf)
  335. return;
  336. switch (data->txstate) {
  337. case WBCIR_TXSTATE_INACTIVE:
  338. /* TX FIFO empty */
  339. space = 16;
  340. led_trigger_event(data->txtrigger, LED_FULL);
  341. break;
  342. case WBCIR_TXSTATE_ACTIVE:
  343. /* TX FIFO low (3 bytes or less) */
  344. space = 13;
  345. break;
  346. case WBCIR_TXSTATE_ERROR:
  347. space = 0;
  348. break;
  349. default:
  350. return;
  351. }
  352. /*
  353. * TX data is run-length coded in bytes: YXXXXXXX
  354. * Y = space (1) or pulse (0)
  355. * X = duration, encoded as (X + 1) * 10us (i.e 10 to 1280 us)
  356. */
  357. for (used = 0; used < space && data->txoff != data->txlen; used++) {
  358. if (data->txbuf[data->txoff] == 0) {
  359. data->txoff++;
  360. continue;
  361. }
  362. byte = min((u32)0x80, data->txbuf[data->txoff]);
  363. data->txbuf[data->txoff] -= byte;
  364. byte--;
  365. byte |= (data->txoff % 2 ? 0x80 : 0x00); /* pulse/space */
  366. bytes[used] = byte;
  367. }
  368. while (data->txbuf[data->txoff] == 0 && data->txoff != data->txlen)
  369. data->txoff++;
  370. if (used == 0) {
  371. /* Finished */
  372. if (data->txstate == WBCIR_TXSTATE_ERROR)
  373. /* Clear TX underrun bit */
  374. outb(WBCIR_TX_UNDERRUN, data->sbase + WBCIR_REG_SP3_ASCR);
  375. else
  376. data->txstate = WBCIR_TXSTATE_DONE;
  377. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  378. led_trigger_event(data->txtrigger, LED_OFF);
  379. wake_up(&data->txwaitq);
  380. } else if (data->txoff == data->txlen) {
  381. /* At the end of transmission, tell the hw before last byte */
  382. outsb(data->sbase + WBCIR_REG_SP3_TXDATA, bytes, used - 1);
  383. outb(WBCIR_TX_EOT, data->sbase + WBCIR_REG_SP3_ASCR);
  384. outb(bytes[used - 1], data->sbase + WBCIR_REG_SP3_TXDATA);
  385. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  386. WBCIR_IRQ_TX_EMPTY);
  387. } else {
  388. /* More data to follow... */
  389. outsb(data->sbase + WBCIR_REG_SP3_RXDATA, bytes, used);
  390. if (data->txstate == WBCIR_TXSTATE_INACTIVE) {
  391. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  392. WBCIR_IRQ_TX_LOW);
  393. data->txstate = WBCIR_TXSTATE_ACTIVE;
  394. }
  395. }
  396. }
  397. static irqreturn_t
  398. wbcir_irq_handler(int irqno, void *cookie)
  399. {
  400. struct pnp_dev *device = cookie;
  401. struct wbcir_data *data = pnp_get_drvdata(device);
  402. unsigned long flags;
  403. u8 status;
  404. spin_lock_irqsave(&data->spinlock, flags);
  405. wbcir_select_bank(data, WBCIR_BANK_0);
  406. status = inb(data->sbase + WBCIR_REG_SP3_EIR);
  407. status &= data->irqmask;
  408. if (!status) {
  409. spin_unlock_irqrestore(&data->spinlock, flags);
  410. return IRQ_NONE;
  411. }
  412. if (status & WBCIR_IRQ_ERR) {
  413. /* RX overflow? (read clears bit) */
  414. if (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_OVERRUN) {
  415. data->rxstate = WBCIR_RXSTATE_ERROR;
  416. ir_raw_event_reset(data->dev);
  417. }
  418. /* TX underflow? */
  419. if (inb(data->sbase + WBCIR_REG_SP3_ASCR) & WBCIR_TX_UNDERRUN)
  420. data->txstate = WBCIR_TXSTATE_ERROR;
  421. }
  422. if (status & WBCIR_IRQ_RX)
  423. wbcir_irq_rx(data, device);
  424. if (status & (WBCIR_IRQ_TX_LOW | WBCIR_IRQ_TX_EMPTY))
  425. wbcir_irq_tx(data);
  426. spin_unlock_irqrestore(&data->spinlock, flags);
  427. return IRQ_HANDLED;
  428. }
  429. /*****************************************************************************
  430. *
  431. * RC-CORE INTERFACE FUNCTIONS
  432. *
  433. *****************************************************************************/
  434. static int
  435. wbcir_txcarrier(struct rc_dev *dev, u32 carrier)
  436. {
  437. struct wbcir_data *data = dev->priv;
  438. unsigned long flags;
  439. u8 val;
  440. u32 freq;
  441. freq = DIV_ROUND_CLOSEST(carrier, 1000);
  442. if (freq < 30 || freq > 60)
  443. return -EINVAL;
  444. switch (freq) {
  445. case 58:
  446. case 59:
  447. case 60:
  448. val = freq - 58;
  449. freq *= 1000;
  450. break;
  451. case 57:
  452. val = freq - 27;
  453. freq = 56900;
  454. break;
  455. default:
  456. val = freq - 27;
  457. freq *= 1000;
  458. break;
  459. }
  460. spin_lock_irqsave(&data->spinlock, flags);
  461. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  462. spin_unlock_irqrestore(&data->spinlock, flags);
  463. return -EBUSY;
  464. }
  465. if (data->txcarrier != freq) {
  466. wbcir_select_bank(data, WBCIR_BANK_7);
  467. wbcir_set_bits(data->sbase + WBCIR_REG_SP3_IRTXMC, val, 0x1F);
  468. data->txcarrier = freq;
  469. }
  470. spin_unlock_irqrestore(&data->spinlock, flags);
  471. return 0;
  472. }
  473. static int
  474. wbcir_txmask(struct rc_dev *dev, u32 mask)
  475. {
  476. struct wbcir_data *data = dev->priv;
  477. unsigned long flags;
  478. u8 val;
  479. /* Four outputs, only one output can be enabled at a time */
  480. switch (mask) {
  481. case 0x1:
  482. val = 0x0;
  483. break;
  484. case 0x2:
  485. val = 0x1;
  486. break;
  487. case 0x4:
  488. val = 0x2;
  489. break;
  490. case 0x8:
  491. val = 0x3;
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. spin_lock_irqsave(&data->spinlock, flags);
  497. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  498. spin_unlock_irqrestore(&data->spinlock, flags);
  499. return -EBUSY;
  500. }
  501. if (data->txmask != mask) {
  502. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS, val, 0x0c);
  503. data->txmask = mask;
  504. }
  505. spin_unlock_irqrestore(&data->spinlock, flags);
  506. return 0;
  507. }
  508. static int
  509. wbcir_tx(struct rc_dev *dev, int *buf, u32 bufsize)
  510. {
  511. struct wbcir_data *data = dev->priv;
  512. u32 count;
  513. unsigned i;
  514. unsigned long flags;
  515. /* bufsize has been sanity checked by the caller */
  516. count = bufsize / sizeof(int);
  517. /* Not sure if this is possible, but better safe than sorry */
  518. spin_lock_irqsave(&data->spinlock, flags);
  519. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  520. spin_unlock_irqrestore(&data->spinlock, flags);
  521. return -EBUSY;
  522. }
  523. /* Convert values to multiples of 10us */
  524. for (i = 0; i < count; i++)
  525. buf[i] = DIV_ROUND_CLOSEST(buf[i], 10);
  526. /* Fill the TX fifo once, the irq handler will do the rest */
  527. data->txbuf = buf;
  528. data->txlen = count;
  529. data->txoff = 0;
  530. wbcir_irq_tx(data);
  531. /* Wait for the TX to complete */
  532. while (data->txstate == WBCIR_TXSTATE_ACTIVE) {
  533. spin_unlock_irqrestore(&data->spinlock, flags);
  534. wait_event(data->txwaitq, data->txstate != WBCIR_TXSTATE_ACTIVE);
  535. spin_lock_irqsave(&data->spinlock, flags);
  536. }
  537. /* We're done */
  538. if (data->txstate == WBCIR_TXSTATE_ERROR)
  539. count = -EAGAIN;
  540. data->txstate = WBCIR_TXSTATE_INACTIVE;
  541. data->txbuf = NULL;
  542. spin_unlock_irqrestore(&data->spinlock, flags);
  543. return count;
  544. }
  545. /*****************************************************************************
  546. *
  547. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  548. *
  549. *****************************************************************************/
  550. static void
  551. wbcir_shutdown(struct pnp_dev *device)
  552. {
  553. struct device *dev = &device->dev;
  554. struct wbcir_data *data = pnp_get_drvdata(device);
  555. bool do_wake = true;
  556. u8 match[11];
  557. u8 mask[11];
  558. u8 rc6_csl = 0;
  559. int i;
  560. memset(match, 0, sizeof(match));
  561. memset(mask, 0, sizeof(mask));
  562. if (wake_sc == INVALID_SCANCODE || !device_may_wakeup(dev)) {
  563. do_wake = false;
  564. goto finish;
  565. }
  566. switch (protocol) {
  567. case IR_PROTOCOL_RC5:
  568. if (wake_sc > 0xFFF) {
  569. do_wake = false;
  570. dev_err(dev, "RC5 - Invalid wake scancode\n");
  571. break;
  572. }
  573. /* Mask = 13 bits, ex toggle */
  574. mask[0] = 0xFF;
  575. mask[1] = 0x17;
  576. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  577. match[0] |= (wake_sc & 0x0180) >> 1; /* 2 address bits */
  578. match[1] = (wake_sc & 0x0E00) >> 9; /* 3 address bits */
  579. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  580. match[1] |= 0x10;
  581. break;
  582. case IR_PROTOCOL_NEC:
  583. if (wake_sc > 0xFFFFFF) {
  584. do_wake = false;
  585. dev_err(dev, "NEC - Invalid wake scancode\n");
  586. break;
  587. }
  588. mask[0] = mask[1] = mask[2] = mask[3] = 0xFF;
  589. match[1] = bitrev8((wake_sc & 0xFF));
  590. match[0] = ~match[1];
  591. match[3] = bitrev8((wake_sc & 0xFF00) >> 8);
  592. if (wake_sc > 0xFFFF)
  593. match[2] = bitrev8((wake_sc & 0xFF0000) >> 16);
  594. else
  595. match[2] = ~match[3];
  596. break;
  597. case IR_PROTOCOL_RC6:
  598. if (wake_rc6mode == 0) {
  599. if (wake_sc > 0xFFFF) {
  600. do_wake = false;
  601. dev_err(dev, "RC6 - Invalid wake scancode\n");
  602. break;
  603. }
  604. /* Command */
  605. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  606. mask[0] = 0xFF;
  607. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  608. mask[1] = 0xFF;
  609. /* Address */
  610. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  611. mask[2] = 0xFF;
  612. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  613. mask[3] = 0xFF;
  614. /* Header */
  615. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  616. mask[4] = 0xF0;
  617. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  618. mask[5] = 0x0F;
  619. rc6_csl = 44;
  620. } else if (wake_rc6mode == 6) {
  621. i = 0;
  622. /* Command */
  623. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  624. mask[i++] = 0xFF;
  625. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  626. mask[i++] = 0xFF;
  627. /* Address + Toggle */
  628. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  629. mask[i++] = 0xFF;
  630. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  631. mask[i++] = 0x3F;
  632. /* Customer bits 7 - 0 */
  633. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  634. mask[i++] = 0xFF;
  635. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  636. mask[i++] = 0xFF;
  637. if (wake_sc & 0x80000000) {
  638. /* Customer range bit and bits 15 - 8 */
  639. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  640. mask[i++] = 0xFF;
  641. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  642. mask[i++] = 0xFF;
  643. rc6_csl = 76;
  644. } else if (wake_sc <= 0x007FFFFF) {
  645. rc6_csl = 60;
  646. } else {
  647. do_wake = false;
  648. dev_err(dev, "RC6 - Invalid wake scancode\n");
  649. break;
  650. }
  651. /* Header */
  652. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  653. mask[i++] = 0xFF;
  654. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  655. mask[i++] = 0x0F;
  656. } else {
  657. do_wake = false;
  658. dev_err(dev, "RC6 - Invalid wake mode\n");
  659. }
  660. break;
  661. default:
  662. do_wake = false;
  663. break;
  664. }
  665. finish:
  666. if (do_wake) {
  667. /* Set compare and compare mask */
  668. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  669. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  670. 0x3F);
  671. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  672. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  673. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  674. 0x3F);
  675. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  676. /* RC6 Compare String Len */
  677. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  678. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  679. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  680. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  681. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  682. /* Set CEIR_EN */
  683. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x01, 0x01);
  684. } else {
  685. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  686. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  687. /* Clear CEIR_EN */
  688. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  689. }
  690. /*
  691. * ACPI will set the HW disable bit for SP3 which means that the
  692. * output signals are left in an undefined state which may cause
  693. * spurious interrupts which we need to ignore until the hardware
  694. * is reinitialized.
  695. */
  696. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  697. disable_irq(data->irq);
  698. /* Disable LED */
  699. led_trigger_event(data->rxtrigger, LED_OFF);
  700. led_trigger_event(data->txtrigger, LED_OFF);
  701. }
  702. static int
  703. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  704. {
  705. wbcir_shutdown(device);
  706. return 0;
  707. }
  708. static void
  709. wbcir_init_hw(struct wbcir_data *data)
  710. {
  711. u8 tmp;
  712. /* Disable interrupts */
  713. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  714. /* Set PROT_SEL, RX_INV, Clear CEIR_EN (needed for the led) */
  715. tmp = protocol << 4;
  716. if (invert)
  717. tmp |= 0x08;
  718. outb(tmp, data->wbase + WBCIR_REG_WCEIR_CTL);
  719. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  720. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  721. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  722. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  723. /* Set RC5 cell time to correspond to 36 kHz */
  724. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  725. /* Set IRTX_INV */
  726. if (invert)
  727. outb(0x04, data->ebase + WBCIR_REG_ECEIR_CCTL);
  728. else
  729. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  730. /*
  731. * Clear IR LED, set SP3 clock to 24Mhz, set TX mask to IRTX1,
  732. * set SP3_IRRX_SW to binary 01, helpfully not documented
  733. */
  734. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  735. data->txmask = 0x1;
  736. /* Enable extended mode */
  737. wbcir_select_bank(data, WBCIR_BANK_2);
  738. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  739. /*
  740. * Configure baud generator, IR data will be sampled at
  741. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  742. *
  743. * The ECIR registers include a flag to change the
  744. * 24Mhz clock freq to 48Mhz.
  745. *
  746. * It's not documented in the specs, but fifo levels
  747. * other than 16 seems to be unsupported.
  748. */
  749. /* prescaler 1.0, tx/rx fifo lvl 16 */
  750. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  751. /* Set baud divisor to generate one byte per bit/cell */
  752. switch (protocol) {
  753. case IR_PROTOCOL_RC5:
  754. outb(0xA7, data->sbase + WBCIR_REG_SP3_BGDL);
  755. break;
  756. case IR_PROTOCOL_RC6:
  757. outb(0x53, data->sbase + WBCIR_REG_SP3_BGDL);
  758. break;
  759. case IR_PROTOCOL_NEC:
  760. outb(0x69, data->sbase + WBCIR_REG_SP3_BGDL);
  761. break;
  762. }
  763. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  764. /* Set CEIR mode */
  765. wbcir_select_bank(data, WBCIR_BANK_0);
  766. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  767. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  768. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  769. /* Disable RX demod, run-length encoding/decoding, set freq span */
  770. wbcir_select_bank(data, WBCIR_BANK_7);
  771. outb(0x10, data->sbase + WBCIR_REG_SP3_RCCFG);
  772. /* Disable timer */
  773. wbcir_select_bank(data, WBCIR_BANK_4);
  774. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  775. /* Disable MSR interrupt, clear AUX_IRX, mask RX during TX? */
  776. wbcir_select_bank(data, WBCIR_BANK_5);
  777. outb(txandrx ? 0x03 : 0x02, data->sbase + WBCIR_REG_SP3_IRCR2);
  778. /* Disable CRC */
  779. wbcir_select_bank(data, WBCIR_BANK_6);
  780. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  781. /* Set RX demodulation freq, not really used */
  782. wbcir_select_bank(data, WBCIR_BANK_7);
  783. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  784. /* Set TX modulation, 36kHz, 7us pulse width */
  785. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  786. data->txcarrier = 36000;
  787. /* Set invert and pin direction */
  788. if (invert)
  789. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  790. else
  791. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  792. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  793. wbcir_select_bank(data, WBCIR_BANK_0);
  794. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  795. /* Clear AUX status bits */
  796. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  797. /* Clear RX state */
  798. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  799. data->rxev.duration = 0;
  800. ir_raw_event_reset(data->dev);
  801. ir_raw_event_handle(data->dev);
  802. /*
  803. * Check TX state, if we did a suspend/resume cycle while TX was
  804. * active, we will have a process waiting in txwaitq.
  805. */
  806. if (data->txstate == WBCIR_TXSTATE_ACTIVE) {
  807. data->txstate = WBCIR_TXSTATE_ERROR;
  808. wake_up(&data->txwaitq);
  809. }
  810. /* Enable interrupts */
  811. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  812. }
  813. static int
  814. wbcir_resume(struct pnp_dev *device)
  815. {
  816. struct wbcir_data *data = pnp_get_drvdata(device);
  817. wbcir_init_hw(data);
  818. enable_irq(data->irq);
  819. return 0;
  820. }
  821. static int __devinit
  822. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  823. {
  824. struct device *dev = &device->dev;
  825. struct wbcir_data *data;
  826. int err;
  827. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  828. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  829. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  830. dev_err(dev, "Invalid resources\n");
  831. return -ENODEV;
  832. }
  833. data = kzalloc(sizeof(*data), GFP_KERNEL);
  834. if (!data) {
  835. err = -ENOMEM;
  836. goto exit;
  837. }
  838. pnp_set_drvdata(device, data);
  839. spin_lock_init(&data->spinlock);
  840. init_waitqueue_head(&data->txwaitq);
  841. data->ebase = pnp_port_start(device, 0);
  842. data->wbase = pnp_port_start(device, 1);
  843. data->sbase = pnp_port_start(device, 2);
  844. data->irq = pnp_irq(device, 0);
  845. if (data->wbase == 0 || data->ebase == 0 ||
  846. data->sbase == 0 || data->irq == 0) {
  847. err = -ENODEV;
  848. dev_err(dev, "Invalid resources\n");
  849. goto exit_free_data;
  850. }
  851. dev_dbg(&device->dev, "Found device "
  852. "(w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  853. data->wbase, data->ebase, data->sbase, data->irq);
  854. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  855. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  856. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  857. err = -EBUSY;
  858. goto exit_free_data;
  859. }
  860. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  861. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  862. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  863. err = -EBUSY;
  864. goto exit_release_wbase;
  865. }
  866. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  867. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  868. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  869. err = -EBUSY;
  870. goto exit_release_ebase;
  871. }
  872. err = request_irq(data->irq, wbcir_irq_handler,
  873. IRQF_DISABLED, DRVNAME, device);
  874. if (err) {
  875. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  876. err = -EBUSY;
  877. goto exit_release_sbase;
  878. }
  879. led_trigger_register_simple("cir-tx", &data->txtrigger);
  880. if (!data->txtrigger) {
  881. err = -ENOMEM;
  882. goto exit_free_irq;
  883. }
  884. led_trigger_register_simple("cir-rx", &data->rxtrigger);
  885. if (!data->rxtrigger) {
  886. err = -ENOMEM;
  887. goto exit_unregister_txtrigger;
  888. }
  889. data->led.name = "cir::activity";
  890. data->led.default_trigger = "cir-rx";
  891. data->led.brightness_set = wbcir_led_brightness_set;
  892. data->led.brightness_get = wbcir_led_brightness_get;
  893. err = led_classdev_register(&device->dev, &data->led);
  894. if (err)
  895. goto exit_unregister_rxtrigger;
  896. data->dev = rc_allocate_device();
  897. if (!data->dev) {
  898. err = -ENOMEM;
  899. goto exit_unregister_led;
  900. }
  901. data->dev->driver_type = RC_DRIVER_IR_RAW;
  902. data->dev->driver_name = WBCIR_NAME;
  903. data->dev->input_name = WBCIR_NAME;
  904. data->dev->input_phys = "wbcir/cir0";
  905. data->dev->input_id.bustype = BUS_HOST;
  906. data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
  907. data->dev->input_id.product = WBCIR_ID_FAMILY;
  908. data->dev->input_id.version = WBCIR_ID_CHIP;
  909. data->dev->map_name = RC_MAP_RC6_MCE;
  910. data->dev->s_idle = wbcir_idle_rx;
  911. data->dev->s_tx_mask = wbcir_txmask;
  912. data->dev->s_tx_carrier = wbcir_txcarrier;
  913. data->dev->tx_ir = wbcir_tx;
  914. data->dev->priv = data;
  915. data->dev->dev.parent = &device->dev;
  916. err = rc_register_device(data->dev);
  917. if (err)
  918. goto exit_free_rc;
  919. device_init_wakeup(&device->dev, 1);
  920. wbcir_init_hw(data);
  921. return 0;
  922. exit_free_rc:
  923. rc_free_device(data->dev);
  924. exit_unregister_led:
  925. led_classdev_unregister(&data->led);
  926. exit_unregister_rxtrigger:
  927. led_trigger_unregister_simple(data->rxtrigger);
  928. exit_unregister_txtrigger:
  929. led_trigger_unregister_simple(data->txtrigger);
  930. exit_free_irq:
  931. free_irq(data->irq, device);
  932. exit_release_sbase:
  933. release_region(data->sbase, SP_IOMEM_LEN);
  934. exit_release_ebase:
  935. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  936. exit_release_wbase:
  937. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  938. exit_free_data:
  939. kfree(data);
  940. pnp_set_drvdata(device, NULL);
  941. exit:
  942. return err;
  943. }
  944. static void __devexit
  945. wbcir_remove(struct pnp_dev *device)
  946. {
  947. struct wbcir_data *data = pnp_get_drvdata(device);
  948. /* Disable interrupts */
  949. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  950. free_irq(data->irq, device);
  951. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  952. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  953. /* Clear CEIR_EN */
  954. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  955. /* Clear BUFF_EN, END_EN, MATCH_EN */
  956. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  957. rc_unregister_device(data->dev);
  958. led_trigger_unregister_simple(data->rxtrigger);
  959. led_trigger_unregister_simple(data->txtrigger);
  960. led_classdev_unregister(&data->led);
  961. /* This is ok since &data->led isn't actually used */
  962. wbcir_led_brightness_set(&data->led, LED_OFF);
  963. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  964. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  965. release_region(data->sbase, SP_IOMEM_LEN);
  966. kfree(data);
  967. pnp_set_drvdata(device, NULL);
  968. }
  969. static const struct pnp_device_id wbcir_ids[] = {
  970. { "WEC1022", 0 },
  971. { "", 0 }
  972. };
  973. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  974. static struct pnp_driver wbcir_driver = {
  975. .name = WBCIR_NAME,
  976. .id_table = wbcir_ids,
  977. .probe = wbcir_probe,
  978. .remove = __devexit_p(wbcir_remove),
  979. .suspend = wbcir_suspend,
  980. .resume = wbcir_resume,
  981. .shutdown = wbcir_shutdown
  982. };
  983. static int __init
  984. wbcir_init(void)
  985. {
  986. int ret;
  987. switch (protocol) {
  988. case IR_PROTOCOL_RC5:
  989. case IR_PROTOCOL_NEC:
  990. case IR_PROTOCOL_RC6:
  991. break;
  992. default:
  993. printk(KERN_ERR DRVNAME ": Invalid power-on protocol\n");
  994. }
  995. ret = pnp_register_driver(&wbcir_driver);
  996. if (ret)
  997. printk(KERN_ERR DRVNAME ": Unable to register driver\n");
  998. return ret;
  999. }
  1000. static void __exit
  1001. wbcir_exit(void)
  1002. {
  1003. pnp_unregister_driver(&wbcir_driver);
  1004. }
  1005. module_init(wbcir_init);
  1006. module_exit(wbcir_exit);
  1007. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1008. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1009. MODULE_LICENSE("GPL");