1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <linux/module.h>
33#include <linux/pnp.h>
34#include <linux/interrupt.h>
35#include <linux/timer.h>
36#include <linux/leds.h>
37#include <linux/spinlock.h>
38#include <linux/pci_ids.h>
39#include <linux/io.h>
40#include <linux/bitrev.h>
41#include <linux/slab.h>
42#include <linux/wait.h>
43#include <linux/sched.h>
44#include <media/rc-core.h>
45
46#define DRVNAME "winbond-cir"
47
48
49#define WBCIR_REG_WCEIR_CTL 0x03
50#define WBCIR_REG_WCEIR_STS 0x04
51#define WBCIR_REG_WCEIR_EV_EN 0x05
52#define WBCIR_REG_WCEIR_CNTL 0x06
53#define WBCIR_REG_WCEIR_CNTH 0x07
54#define WBCIR_REG_WCEIR_INDEX 0x08
55#define WBCIR_REG_WCEIR_DATA 0x09
56#define WBCIR_REG_WCEIR_CSL 0x0A
57#define WBCIR_REG_WCEIR_CFG1 0x0B
58#define WBCIR_REG_WCEIR_CFG2 0x0C
59
60
61#define WBCIR_REG_ECEIR_CTS 0x00
62#define WBCIR_REG_ECEIR_CCTL 0x01
63#define WBCIR_REG_ECEIR_CNT_LO 0x02
64#define WBCIR_REG_ECEIR_CNT_HI 0x03
65#define WBCIR_REG_ECEIR_IREM 0x04
66
67
68#define WBCIR_REG_SP3_BSR 0x03
69
70#define WBCIR_REG_SP3_RXDATA 0x00
71#define WBCIR_REG_SP3_TXDATA 0x00
72#define WBCIR_REG_SP3_IER 0x01
73#define WBCIR_REG_SP3_EIR 0x02
74#define WBCIR_REG_SP3_FCR 0x02
75#define WBCIR_REG_SP3_MCR 0x04
76#define WBCIR_REG_SP3_LSR 0x05
77#define WBCIR_REG_SP3_MSR 0x06
78#define WBCIR_REG_SP3_ASCR 0x07
79
80#define WBCIR_REG_SP3_BGDL 0x00
81#define WBCIR_REG_SP3_BGDH 0x01
82#define WBCIR_REG_SP3_EXCR1 0x02
83#define WBCIR_REG_SP3_EXCR2 0x04
84#define WBCIR_REG_SP3_TXFLV 0x06
85#define WBCIR_REG_SP3_RXFLV 0x07
86
87#define WBCIR_REG_SP3_MRID 0x00
88#define WBCIR_REG_SP3_SH_LCR 0x01
89#define WBCIR_REG_SP3_SH_FCR 0x02
90
91#define WBCIR_REG_SP3_IRCR1 0x02
92
93#define WBCIR_REG_SP3_IRCR2 0x04
94
95#define WBCIR_REG_SP3_IRCR3 0x00
96#define WBCIR_REG_SP3_SIR_PW 0x02
97
98#define WBCIR_REG_SP3_IRRXDC 0x00
99#define WBCIR_REG_SP3_IRTXMC 0x01
100#define WBCIR_REG_SP3_RCCFG 0x02
101#define WBCIR_REG_SP3_IRCFG1 0x04
102#define WBCIR_REG_SP3_IRCFG4 0x07
103
104
105
106
107
108
109#define WBCIR_IRQ_NONE 0x00
110
111#define WBCIR_IRQ_RX 0x01
112
113#define WBCIR_IRQ_TX_LOW 0x02
114
115#define WBCIR_IRQ_ERR 0x04
116
117#define WBCIR_IRQ_TX_EMPTY 0x20
118
119#define WBCIR_LED_ENABLE 0x80
120
121#define WBCIR_RX_AVAIL 0x01
122
123#define WBCIR_RX_OVERRUN 0x02
124
125#define WBCIR_TX_EOT 0x04
126
127#define WBCIR_RX_DISABLE 0x20
128
129#define WBCIR_TX_UNDERRUN 0x40
130
131#define WBCIR_EXT_ENABLE 0x01
132
133#define WBCIR_REGSEL_COMPARE 0x10
134
135#define WBCIR_REGSEL_MASK 0x20
136
137#define WBCIR_REG_ADDR0 0x00
138
139#define WBCIR_CNTR_EN 0x01
140
141#define WBCIR_CNTR_R 0x02
142
143#define WBCIR_IRTX_INV 0x04
144
145#define WBCIR_RX_T_OV 0x40
146
147
148enum wbcir_bank {
149 WBCIR_BANK_0 = 0x00,
150 WBCIR_BANK_1 = 0x80,
151 WBCIR_BANK_2 = 0xE0,
152 WBCIR_BANK_3 = 0xE4,
153 WBCIR_BANK_4 = 0xE8,
154 WBCIR_BANK_5 = 0xEC,
155 WBCIR_BANK_6 = 0xF0,
156 WBCIR_BANK_7 = 0xF4,
157};
158
159
160enum wbcir_protocol {
161 IR_PROTOCOL_RC5 = 0x0,
162 IR_PROTOCOL_NEC = 0x1,
163 IR_PROTOCOL_RC6 = 0x2,
164};
165
166
167enum wbcir_rxstate {
168 WBCIR_RXSTATE_INACTIVE = 0,
169 WBCIR_RXSTATE_ACTIVE,
170 WBCIR_RXSTATE_ERROR
171};
172
173
174enum wbcir_txstate {
175 WBCIR_TXSTATE_INACTIVE = 0,
176 WBCIR_TXSTATE_ACTIVE,
177 WBCIR_TXSTATE_ERROR
178};
179
180
181#define WBCIR_NAME "Winbond CIR"
182#define WBCIR_ID_FAMILY 0xF1
183#define WBCIR_ID_CHIP 0x04
184#define WAKEUP_IOMEM_LEN 0x10
185#define EHFUNC_IOMEM_LEN 0x10
186#define SP_IOMEM_LEN 0x08
187
188
189struct wbcir_data {
190 spinlock_t spinlock;
191 struct rc_dev *dev;
192 struct led_classdev led;
193
194 unsigned long wbase;
195 unsigned long ebase;
196 unsigned long sbase;
197 unsigned int irq;
198 u8 irqmask;
199
200
201 enum wbcir_rxstate rxstate;
202 int carrier_report_enabled;
203 u32 pulse_duration;
204
205
206 enum wbcir_txstate txstate;
207 u32 txlen;
208 u32 txoff;
209 u32 *txbuf;
210 u8 txmask;
211 u32 txcarrier;
212};
213
214static bool invert;
215module_param(invert, bool, 0444);
216MODULE_PARM_DESC(invert, "Invert the signal from the IR receiver");
217
218static bool txandrx;
219module_param(txandrx, bool, 0444);
220MODULE_PARM_DESC(txandrx, "Allow simultaneous TX and RX");
221
222
223
224
225
226
227
228
229
230static void
231wbcir_set_bits(unsigned long addr, u8 bits, u8 mask)
232{
233 u8 val;
234
235 val = inb(addr);
236 val = ((val & ~mask) | (bits & mask));
237 outb(val, addr);
238}
239
240
241static inline void
242wbcir_select_bank(struct wbcir_data *data, enum wbcir_bank bank)
243{
244 outb(bank, data->sbase + WBCIR_REG_SP3_BSR);
245}
246
247static inline void
248wbcir_set_irqmask(struct wbcir_data *data, u8 irqmask)
249{
250 if (data->irqmask == irqmask)
251 return;
252
253 wbcir_select_bank(data, WBCIR_BANK_0);
254 outb(irqmask, data->sbase + WBCIR_REG_SP3_IER);
255 data->irqmask = irqmask;
256}
257
258static enum led_brightness
259wbcir_led_brightness_get(struct led_classdev *led_cdev)
260{
261 struct wbcir_data *data = container_of(led_cdev,
262 struct wbcir_data,
263 led);
264
265 if (inb(data->ebase + WBCIR_REG_ECEIR_CTS) & WBCIR_LED_ENABLE)
266 return LED_FULL;
267 else
268 return LED_OFF;
269}
270
271static void
272wbcir_led_brightness_set(struct led_classdev *led_cdev,
273 enum led_brightness brightness)
274{
275 struct wbcir_data *data = container_of(led_cdev,
276 struct wbcir_data,
277 led);
278
279 wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS,
280 brightness == LED_OFF ? 0x00 : WBCIR_LED_ENABLE,
281 WBCIR_LED_ENABLE);
282}
283
284
285static u8
286wbcir_to_rc6cells(u8 val)
287{
288 u8 coded = 0x00;
289 int i;
290
291 val &= 0x0F;
292 for (i = 0; i < 4; i++) {
293 if (val & 0x01)
294 coded |= 0x02 << (i * 2);
295 else
296 coded |= 0x01 << (i * 2);
297 val >>= 1;
298 }
299
300 return coded;
301}
302
303
304
305
306
307
308
309static void
310wbcir_carrier_report(struct wbcir_data *data)
311{
312 unsigned counter = inb(data->ebase + WBCIR_REG_ECEIR_CNT_LO) |
313 inb(data->ebase + WBCIR_REG_ECEIR_CNT_HI) << 8;
314
315 if (counter > 0 && counter < 0xffff) {
316 struct ir_raw_event ev = {
317 .carrier_report = 1,
318 .carrier = DIV_ROUND_CLOSEST(counter * 1000000u,
319 data->pulse_duration)
320 };
321
322 ir_raw_event_store(data->dev, &ev);
323 }
324
325
326 data->pulse_duration = 0;
327 wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_R,
328 WBCIR_CNTR_EN | WBCIR_CNTR_R);
329 wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_EN,
330 WBCIR_CNTR_EN | WBCIR_CNTR_R);
331}
332
333static void
334wbcir_idle_rx(struct rc_dev *dev, bool idle)
335{
336 struct wbcir_data *data = dev->priv;
337
338 if (!idle && data->rxstate == WBCIR_RXSTATE_INACTIVE)
339 data->rxstate = WBCIR_RXSTATE_ACTIVE;
340
341 if (idle && data->rxstate != WBCIR_RXSTATE_INACTIVE) {
342 data->rxstate = WBCIR_RXSTATE_INACTIVE;
343
344 if (data->carrier_report_enabled)
345 wbcir_carrier_report(data);
346
347
348 outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
349 }
350}
351
352static void
353wbcir_irq_rx(struct wbcir_data *data, struct pnp_dev *device)
354{
355 u8 irdata;
356 struct ir_raw_event rawir = {};
357
358
359 while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL) {
360 irdata = inb(data->sbase + WBCIR_REG_SP3_RXDATA);
361 if (data->rxstate == WBCIR_RXSTATE_ERROR)
362 continue;
363
364 rawir.duration = ((irdata & 0x7F) + 1) *
365 (data->carrier_report_enabled ? 2 : 10);
366 rawir.pulse = irdata & 0x80 ? false : true;
367
368 if (rawir.pulse)
369 data->pulse_duration += rawir.duration;
370
371 ir_raw_event_store_with_filter(data->dev, &rawir);
372 }
373
374 ir_raw_event_handle(data->dev);
375}
376
377static void
378wbcir_irq_tx(struct wbcir_data *data)
379{
380 unsigned int space;
381 unsigned int used;
382 u8 bytes[16];
383 u8 byte;
384
385 if (!data->txbuf)
386 return;
387
388 switch (data->txstate) {
389 case WBCIR_TXSTATE_INACTIVE:
390
391 space = 16;
392 break;
393 case WBCIR_TXSTATE_ACTIVE:
394
395 space = 13;
396 break;
397 case WBCIR_TXSTATE_ERROR:
398 space = 0;
399 break;
400 default:
401 return;
402 }
403
404
405
406
407
408
409 for (used = 0; used < space && data->txoff != data->txlen; used++) {
410 if (data->txbuf[data->txoff] == 0) {
411 data->txoff++;
412 continue;
413 }
414 byte = min((u32)0x80, data->txbuf[data->txoff]);
415 data->txbuf[data->txoff] -= byte;
416 byte--;
417 byte |= (data->txoff % 2 ? 0x80 : 0x00);
418 bytes[used] = byte;
419 }
420
421 while (data->txoff != data->txlen && data->txbuf[data->txoff] == 0)
422 data->txoff++;
423
424 if (used == 0) {
425
426 if (data->txstate == WBCIR_TXSTATE_ERROR)
427
428 outb(WBCIR_TX_UNDERRUN, data->sbase + WBCIR_REG_SP3_ASCR);
429 wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
430 kfree(data->txbuf);
431 data->txbuf = NULL;
432 data->txstate = WBCIR_TXSTATE_INACTIVE;
433 } else if (data->txoff == data->txlen) {
434
435 outsb(data->sbase + WBCIR_REG_SP3_TXDATA, bytes, used - 1);
436 outb(WBCIR_TX_EOT, data->sbase + WBCIR_REG_SP3_ASCR);
437 outb(bytes[used - 1], data->sbase + WBCIR_REG_SP3_TXDATA);
438 wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
439 WBCIR_IRQ_TX_EMPTY);
440 } else {
441
442 outsb(data->sbase + WBCIR_REG_SP3_RXDATA, bytes, used);
443 if (data->txstate == WBCIR_TXSTATE_INACTIVE) {
444 wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
445 WBCIR_IRQ_TX_LOW);
446 data->txstate = WBCIR_TXSTATE_ACTIVE;
447 }
448 }
449}
450
451static irqreturn_t
452wbcir_irq_handler(int irqno, void *cookie)
453{
454 struct pnp_dev *device = cookie;
455 struct wbcir_data *data = pnp_get_drvdata(device);
456 unsigned long flags;
457 u8 status;
458
459 spin_lock_irqsave(&data->spinlock, flags);
460 wbcir_select_bank(data, WBCIR_BANK_0);
461 status = inb(data->sbase + WBCIR_REG_SP3_EIR);
462 status &= data->irqmask;
463
464 if (!status) {
465 spin_unlock_irqrestore(&data->spinlock, flags);
466 return IRQ_NONE;
467 }
468
469 if (status & WBCIR_IRQ_ERR) {
470
471 if (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_OVERRUN) {
472 data->rxstate = WBCIR_RXSTATE_ERROR;
473 ir_raw_event_reset(data->dev);
474 }
475
476
477 if (inb(data->sbase + WBCIR_REG_SP3_ASCR) & WBCIR_TX_UNDERRUN)
478 data->txstate = WBCIR_TXSTATE_ERROR;
479 }
480
481 if (status & WBCIR_IRQ_RX)
482 wbcir_irq_rx(data, device);
483
484 if (status & (WBCIR_IRQ_TX_LOW | WBCIR_IRQ_TX_EMPTY))
485 wbcir_irq_tx(data);
486
487 spin_unlock_irqrestore(&data->spinlock, flags);
488 return IRQ_HANDLED;
489}
490
491
492
493
494
495
496
497static int
498wbcir_set_carrier_report(struct rc_dev *dev, int enable)
499{
500 struct wbcir_data *data = dev->priv;
501 unsigned long flags;
502
503 spin_lock_irqsave(&data->spinlock, flags);
504
505 if (data->carrier_report_enabled == enable) {
506 spin_unlock_irqrestore(&data->spinlock, flags);
507 return 0;
508 }
509
510 data->pulse_duration = 0;
511 wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_R,
512 WBCIR_CNTR_EN | WBCIR_CNTR_R);
513
514 if (enable && data->dev->idle)
515 wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL,
516 WBCIR_CNTR_EN, WBCIR_CNTR_EN | WBCIR_CNTR_R);
517
518
519 wbcir_select_bank(data, WBCIR_BANK_2);
520 data->dev->rx_resolution = enable ? 2 : 10;
521 outb(enable ? 0x03 : 0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
522 outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
523
524
525 wbcir_select_bank(data, WBCIR_BANK_7);
526 wbcir_set_bits(data->sbase + WBCIR_REG_SP3_RCCFG,
527 enable ? WBCIR_RX_T_OV : 0, WBCIR_RX_T_OV);
528
529 data->carrier_report_enabled = enable;
530 spin_unlock_irqrestore(&data->spinlock, flags);
531
532 return 0;
533}
534
535static int
536wbcir_txcarrier(struct rc_dev *dev, u32 carrier)
537{
538 struct wbcir_data *data = dev->priv;
539 unsigned long flags;
540 u8 val;
541 u32 freq;
542
543 freq = DIV_ROUND_CLOSEST(carrier, 1000);
544 if (freq < 30 || freq > 60)
545 return -EINVAL;
546
547 switch (freq) {
548 case 58:
549 case 59:
550 case 60:
551 val = freq - 58;
552 freq *= 1000;
553 break;
554 case 57:
555 val = freq - 27;
556 freq = 56900;
557 break;
558 default:
559 val = freq - 27;
560 freq *= 1000;
561 break;
562 }
563
564 spin_lock_irqsave(&data->spinlock, flags);
565 if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
566 spin_unlock_irqrestore(&data->spinlock, flags);
567 return -EBUSY;
568 }
569
570 if (data->txcarrier != freq) {
571 wbcir_select_bank(data, WBCIR_BANK_7);
572 wbcir_set_bits(data->sbase + WBCIR_REG_SP3_IRTXMC, val, 0x1F);
573 data->txcarrier = freq;
574 }
575
576 spin_unlock_irqrestore(&data->spinlock, flags);
577 return 0;
578}
579
580static int
581wbcir_txmask(struct rc_dev *dev, u32 mask)
582{
583 struct wbcir_data *data = dev->priv;
584 unsigned long flags;
585 u8 val;
586
587
588 if (mask > 15)
589 return 4;
590
591
592 switch (mask) {
593 case 0x1:
594 val = 0x0;
595 break;
596 case 0x2:
597 val = 0x1;
598 break;
599 case 0x4:
600 val = 0x2;
601 break;
602 case 0x8:
603 val = 0x3;
604 break;
605 default:
606 return -EINVAL;
607 }
608
609 spin_lock_irqsave(&data->spinlock, flags);
610 if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
611 spin_unlock_irqrestore(&data->spinlock, flags);
612 return -EBUSY;
613 }
614
615 if (data->txmask != mask) {
616 wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS, val, 0x0c);
617 data->txmask = mask;
618 }
619
620 spin_unlock_irqrestore(&data->spinlock, flags);
621 return 0;
622}
623
624static int
625wbcir_tx(struct rc_dev *dev, unsigned *b, unsigned count)
626{
627 struct wbcir_data *data = dev->priv;
628 unsigned *buf;
629 unsigned i;
630 unsigned long flags;
631
632 buf = kmalloc_array(count, sizeof(*b), GFP_KERNEL);
633 if (!buf)
634 return -ENOMEM;
635
636
637 for (i = 0; i < count; i++)
638 buf[i] = DIV_ROUND_CLOSEST(b[i], 10);
639
640
641 spin_lock_irqsave(&data->spinlock, flags);
642 if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
643 spin_unlock_irqrestore(&data->spinlock, flags);
644 kfree(buf);
645 return -EBUSY;
646 }
647
648
649 data->txbuf = buf;
650 data->txlen = count;
651 data->txoff = 0;
652 wbcir_irq_tx(data);
653
654
655 spin_unlock_irqrestore(&data->spinlock, flags);
656 return count;
657}
658
659
660
661
662
663
664
665static void
666wbcir_shutdown(struct pnp_dev *device)
667{
668 struct device *dev = &device->dev;
669 struct wbcir_data *data = pnp_get_drvdata(device);
670 struct rc_dev *rc = data->dev;
671 bool do_wake = true;
672 u8 match[11];
673 u8 mask[11];
674 u8 rc6_csl = 0;
675 u8 proto;
676 u32 wake_sc = rc->scancode_wakeup_filter.data;
677 u32 mask_sc = rc->scancode_wakeup_filter.mask;
678 int i;
679
680 memset(match, 0, sizeof(match));
681 memset(mask, 0, sizeof(mask));
682
683 if (!mask_sc || !device_may_wakeup(dev)) {
684 do_wake = false;
685 goto finish;
686 }
687
688 switch (rc->wakeup_protocol) {
689 case RC_PROTO_RC5:
690
691 mask[0] = (mask_sc & 0x003f);
692 mask[0] |= (mask_sc & 0x0300) >> 2;
693 mask[1] = (mask_sc & 0x1c00) >> 10;
694 if (mask_sc & 0x0040)
695 match[1] |= 0x10;
696
697 match[0] = (wake_sc & 0x003F);
698 match[0] |= (wake_sc & 0x0300) >> 2;
699 match[1] = (wake_sc & 0x1c00) >> 10;
700 if (!(wake_sc & 0x0040))
701 match[1] |= 0x10;
702
703 proto = IR_PROTOCOL_RC5;
704 break;
705
706 case RC_PROTO_NEC:
707 mask[1] = bitrev8(mask_sc);
708 mask[0] = mask[1];
709 mask[3] = bitrev8(mask_sc >> 8);
710 mask[2] = mask[3];
711
712 match[1] = bitrev8(wake_sc);
713 match[0] = ~match[1];
714 match[3] = bitrev8(wake_sc >> 8);
715 match[2] = ~match[3];
716
717 proto = IR_PROTOCOL_NEC;
718 break;
719
720 case RC_PROTO_NECX:
721 mask[1] = bitrev8(mask_sc);
722 mask[0] = mask[1];
723 mask[2] = bitrev8(mask_sc >> 8);
724 mask[3] = bitrev8(mask_sc >> 16);
725
726 match[1] = bitrev8(wake_sc);
727 match[0] = ~match[1];
728 match[2] = bitrev8(wake_sc >> 8);
729 match[3] = bitrev8(wake_sc >> 16);
730
731 proto = IR_PROTOCOL_NEC;
732 break;
733
734 case RC_PROTO_NEC32:
735 mask[0] = bitrev8(mask_sc);
736 mask[1] = bitrev8(mask_sc >> 8);
737 mask[2] = bitrev8(mask_sc >> 16);
738 mask[3] = bitrev8(mask_sc >> 24);
739
740 match[0] = bitrev8(wake_sc);
741 match[1] = bitrev8(wake_sc >> 8);
742 match[2] = bitrev8(wake_sc >> 16);
743 match[3] = bitrev8(wake_sc >> 24);
744
745 proto = IR_PROTOCOL_NEC;
746 break;
747
748 case RC_PROTO_RC6_0:
749
750 match[0] = wbcir_to_rc6cells(wake_sc >> 0);
751 mask[0] = wbcir_to_rc6cells(mask_sc >> 0);
752 match[1] = wbcir_to_rc6cells(wake_sc >> 4);
753 mask[1] = wbcir_to_rc6cells(mask_sc >> 4);
754
755
756 match[2] = wbcir_to_rc6cells(wake_sc >> 8);
757 mask[2] = wbcir_to_rc6cells(mask_sc >> 8);
758 match[3] = wbcir_to_rc6cells(wake_sc >> 12);
759 mask[3] = wbcir_to_rc6cells(mask_sc >> 12);
760
761
762 match[4] = 0x50;
763 mask[4] = 0xF0;
764 match[5] = 0x09;
765 mask[5] = 0x0F;
766
767 rc6_csl = 44;
768 proto = IR_PROTOCOL_RC6;
769 break;
770
771 case RC_PROTO_RC6_6A_24:
772 case RC_PROTO_RC6_6A_32:
773 case RC_PROTO_RC6_MCE:
774 i = 0;
775
776
777 match[i] = wbcir_to_rc6cells(wake_sc >> 0);
778 mask[i++] = wbcir_to_rc6cells(mask_sc >> 0);
779 match[i] = wbcir_to_rc6cells(wake_sc >> 4);
780 mask[i++] = wbcir_to_rc6cells(mask_sc >> 4);
781
782
783 match[i] = wbcir_to_rc6cells(wake_sc >> 8);
784 mask[i++] = wbcir_to_rc6cells(mask_sc >> 8);
785 match[i] = wbcir_to_rc6cells(wake_sc >> 12);
786 mask[i++] = wbcir_to_rc6cells(mask_sc >> 12);
787
788
789 match[i] = wbcir_to_rc6cells(wake_sc >> 16);
790 mask[i++] = wbcir_to_rc6cells(mask_sc >> 16);
791
792 if (rc->wakeup_protocol == RC_PROTO_RC6_6A_20) {
793 rc6_csl = 52;
794 } else {
795 match[i] = wbcir_to_rc6cells(wake_sc >> 20);
796 mask[i++] = wbcir_to_rc6cells(mask_sc >> 20);
797
798 if (rc->wakeup_protocol == RC_PROTO_RC6_6A_24) {
799 rc6_csl = 60;
800 } else {
801
802 match[i] = wbcir_to_rc6cells(wake_sc >> 24);
803 mask[i++] = wbcir_to_rc6cells(mask_sc >> 24);
804 match[i] = wbcir_to_rc6cells(wake_sc >> 28);
805 mask[i++] = wbcir_to_rc6cells(mask_sc >> 28);
806 rc6_csl = 76;
807 }
808 }
809
810
811 match[i] = 0x93;
812 mask[i++] = 0xFF;
813 match[i] = 0x0A;
814 mask[i++] = 0x0F;
815 proto = IR_PROTOCOL_RC6;
816 break;
817 default:
818 do_wake = false;
819 break;
820 }
821
822finish:
823 if (do_wake) {
824
825 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
826 WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
827 0x3F);
828 outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
829 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
830 WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
831 0x3F);
832 outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
833
834
835 outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
836
837
838 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
839
840
841 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
842
843
844 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL,
845 (proto << 4) | 0x01, 0x31);
846
847 } else {
848
849 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
850
851
852 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
853 }
854
855
856
857
858
859
860
861 wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
862 disable_irq(data->irq);
863}
864
865
866
867
868static int
869wbcir_set_wakeup_filter(struct rc_dev *rc, struct rc_scancode_filter *filter)
870{
871 return 0;
872}
873
874static int
875wbcir_suspend(struct pnp_dev *device, pm_message_t state)
876{
877 struct wbcir_data *data = pnp_get_drvdata(device);
878 led_classdev_suspend(&data->led);
879 wbcir_shutdown(device);
880 return 0;
881}
882
883static void
884wbcir_init_hw(struct wbcir_data *data)
885{
886
887 wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
888
889
890 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, invert ? 8 : 0, 0x09);
891
892
893 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
894
895
896 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
897
898
899 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
900
901
902 if (invert)
903 outb(WBCIR_IRTX_INV, data->ebase + WBCIR_REG_ECEIR_CCTL);
904 else
905 outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
906
907
908
909
910
911 outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
912 data->txmask = 0x1;
913
914
915 wbcir_select_bank(data, WBCIR_BANK_2);
916 outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
917
918
919
920
921
922
923
924
925
926
927
928
929
930 outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
931
932
933 outb(0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
934 outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
935
936
937 wbcir_select_bank(data, WBCIR_BANK_0);
938 outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
939 inb(data->sbase + WBCIR_REG_SP3_LSR);
940 inb(data->sbase + WBCIR_REG_SP3_MSR);
941
942
943 wbcir_select_bank(data, WBCIR_BANK_7);
944 outb(0x90, data->sbase + WBCIR_REG_SP3_RCCFG);
945
946
947 wbcir_select_bank(data, WBCIR_BANK_4);
948 outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
949
950
951 wbcir_select_bank(data, WBCIR_BANK_5);
952 outb(txandrx ? 0x03 : 0x02, data->sbase + WBCIR_REG_SP3_IRCR2);
953
954
955 wbcir_select_bank(data, WBCIR_BANK_6);
956 outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
957
958
959 wbcir_select_bank(data, WBCIR_BANK_7);
960 outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
961
962
963 outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
964 data->txcarrier = 36000;
965
966
967 if (invert)
968 outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
969 else
970 outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
971
972
973 wbcir_select_bank(data, WBCIR_BANK_0);
974 outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
975
976
977 outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
978
979
980 data->rxstate = WBCIR_RXSTATE_INACTIVE;
981 wbcir_idle_rx(data->dev, true);
982
983
984 if (data->txstate == WBCIR_TXSTATE_ACTIVE) {
985 kfree(data->txbuf);
986 data->txbuf = NULL;
987 data->txstate = WBCIR_TXSTATE_INACTIVE;
988 }
989
990
991 wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
992}
993
994static int
995wbcir_resume(struct pnp_dev *device)
996{
997 struct wbcir_data *data = pnp_get_drvdata(device);
998
999 wbcir_init_hw(data);
1000 ir_raw_event_reset(data->dev);
1001 enable_irq(data->irq);
1002 led_classdev_resume(&data->led);
1003
1004 return 0;
1005}
1006
1007static int
1008wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
1009{
1010 struct device *dev = &device->dev;
1011 struct wbcir_data *data;
1012 int err;
1013
1014 if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
1015 pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
1016 pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
1017 dev_err(dev, "Invalid resources\n");
1018 return -ENODEV;
1019 }
1020
1021 data = kzalloc(sizeof(*data), GFP_KERNEL);
1022 if (!data) {
1023 err = -ENOMEM;
1024 goto exit;
1025 }
1026
1027 pnp_set_drvdata(device, data);
1028
1029 spin_lock_init(&data->spinlock);
1030 data->ebase = pnp_port_start(device, 0);
1031 data->wbase = pnp_port_start(device, 1);
1032 data->sbase = pnp_port_start(device, 2);
1033 data->irq = pnp_irq(device, 0);
1034
1035 if (data->wbase == 0 || data->ebase == 0 ||
1036 data->sbase == 0 || data->irq == -1) {
1037 err = -ENODEV;
1038 dev_err(dev, "Invalid resources\n");
1039 goto exit_free_data;
1040 }
1041
1042 dev_dbg(&device->dev, "Found device (w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
1043 data->wbase, data->ebase, data->sbase, data->irq);
1044
1045 data->led.name = "cir::activity";
1046 data->led.default_trigger = "rc-feedback";
1047 data->led.brightness_set = wbcir_led_brightness_set;
1048 data->led.brightness_get = wbcir_led_brightness_get;
1049 err = led_classdev_register(&device->dev, &data->led);
1050 if (err)
1051 goto exit_free_data;
1052
1053 data->dev = rc_allocate_device(RC_DRIVER_IR_RAW);
1054 if (!data->dev) {
1055 err = -ENOMEM;
1056 goto exit_unregister_led;
1057 }
1058
1059 data->dev->driver_name = DRVNAME;
1060 data->dev->device_name = WBCIR_NAME;
1061 data->dev->input_phys = "wbcir/cir0";
1062 data->dev->input_id.bustype = BUS_HOST;
1063 data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
1064 data->dev->input_id.product = WBCIR_ID_FAMILY;
1065 data->dev->input_id.version = WBCIR_ID_CHIP;
1066 data->dev->map_name = RC_MAP_RC6_MCE;
1067 data->dev->s_idle = wbcir_idle_rx;
1068 data->dev->s_carrier_report = wbcir_set_carrier_report;
1069 data->dev->s_tx_mask = wbcir_txmask;
1070 data->dev->s_tx_carrier = wbcir_txcarrier;
1071 data->dev->tx_ir = wbcir_tx;
1072 data->dev->priv = data;
1073 data->dev->dev.parent = &device->dev;
1074 data->dev->min_timeout = 1;
1075 data->dev->timeout = IR_DEFAULT_TIMEOUT;
1076 data->dev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
1077 data->dev->rx_resolution = 2;
1078 data->dev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
1079 data->dev->allowed_wakeup_protocols = RC_PROTO_BIT_NEC |
1080 RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC5 |
1081 RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
1082 RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
1083 RC_PROTO_BIT_RC6_MCE;
1084 data->dev->wakeup_protocol = RC_PROTO_RC6_MCE;
1085 data->dev->scancode_wakeup_filter.data = 0x800f040c;
1086 data->dev->scancode_wakeup_filter.mask = 0xffff7fff;
1087 data->dev->s_wakeup_filter = wbcir_set_wakeup_filter;
1088
1089 err = rc_register_device(data->dev);
1090 if (err)
1091 goto exit_free_rc;
1092
1093 if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
1094 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
1095 data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
1096 err = -EBUSY;
1097 goto exit_unregister_device;
1098 }
1099
1100 if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
1101 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
1102 data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
1103 err = -EBUSY;
1104 goto exit_release_wbase;
1105 }
1106
1107 if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
1108 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
1109 data->sbase, data->sbase + SP_IOMEM_LEN - 1);
1110 err = -EBUSY;
1111 goto exit_release_ebase;
1112 }
1113
1114 err = request_irq(data->irq, wbcir_irq_handler,
1115 0, DRVNAME, device);
1116 if (err) {
1117 dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
1118 err = -EBUSY;
1119 goto exit_release_sbase;
1120 }
1121
1122 device_init_wakeup(&device->dev, 1);
1123
1124 wbcir_init_hw(data);
1125
1126 return 0;
1127
1128exit_release_sbase:
1129 release_region(data->sbase, SP_IOMEM_LEN);
1130exit_release_ebase:
1131 release_region(data->ebase, EHFUNC_IOMEM_LEN);
1132exit_release_wbase:
1133 release_region(data->wbase, WAKEUP_IOMEM_LEN);
1134exit_unregister_device:
1135 rc_unregister_device(data->dev);
1136 data->dev = NULL;
1137exit_free_rc:
1138 rc_free_device(data->dev);
1139exit_unregister_led:
1140 led_classdev_unregister(&data->led);
1141exit_free_data:
1142 kfree(data);
1143 pnp_set_drvdata(device, NULL);
1144exit:
1145 return err;
1146}
1147
1148static void
1149wbcir_remove(struct pnp_dev *device)
1150{
1151 struct wbcir_data *data = pnp_get_drvdata(device);
1152
1153
1154 wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
1155 free_irq(data->irq, device);
1156
1157
1158 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
1159
1160
1161 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
1162
1163
1164 wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
1165
1166 rc_unregister_device(data->dev);
1167
1168 led_classdev_unregister(&data->led);
1169
1170
1171 wbcir_led_brightness_set(&data->led, LED_OFF);
1172
1173 release_region(data->wbase, WAKEUP_IOMEM_LEN);
1174 release_region(data->ebase, EHFUNC_IOMEM_LEN);
1175 release_region(data->sbase, SP_IOMEM_LEN);
1176
1177 kfree(data);
1178
1179 pnp_set_drvdata(device, NULL);
1180}
1181
1182static const struct pnp_device_id wbcir_ids[] = {
1183 { "WEC1022", 0 },
1184 { "", 0 }
1185};
1186MODULE_DEVICE_TABLE(pnp, wbcir_ids);
1187
1188static struct pnp_driver wbcir_driver = {
1189 .name = DRVNAME,
1190 .id_table = wbcir_ids,
1191 .probe = wbcir_probe,
1192 .remove = wbcir_remove,
1193 .suspend = wbcir_suspend,
1194 .resume = wbcir_resume,
1195 .shutdown = wbcir_shutdown
1196};
1197
1198static int __init
1199wbcir_init(void)
1200{
1201 int ret;
1202
1203 ret = pnp_register_driver(&wbcir_driver);
1204 if (ret)
1205 pr_err("Unable to register driver\n");
1206
1207 return ret;
1208}
1209
1210static void __exit
1211wbcir_exit(void)
1212{
1213 pnp_unregister_driver(&wbcir_driver);
1214}
1215
1216module_init(wbcir_init);
1217module_exit(wbcir_exit);
1218
1219MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
1220MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
1221MODULE_LICENSE("GPL");
1222