Merge upstream into ieee80211.
[linux-2.6] / drivers / pcmcia / vrc4171_card.c
1 /*
2  * vrc4171_card.c, NEC VRC4171 Card Controller driver for Socket Services.
3  *
4  * Copyright (C) 2003-2005  Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/sched.h>
26 #include <linux/types.h>
27
28 #include <asm/io.h>
29
30 #include <pcmcia/ss.h>
31
32 #include "i82365.h"
33
34 MODULE_DESCRIPTION("NEC VRC4171 Card Controllers driver for Socket Services");
35 MODULE_AUTHOR("Yoichi Yuasa <yuasa@hh.iij4u.or.jp>");
36 MODULE_LICENSE("GPL");
37
38 #define CARD_MAX_SLOTS          2
39 #define CARD_SLOTA              0
40 #define CARD_SLOTB              1
41 #define CARD_SLOTB_OFFSET       0x40
42
43 #define CARD_MEM_START          0x10000000
44 #define CARD_MEM_END            0x13ffffff
45 #define CARD_MAX_MEM_OFFSET     0x3ffffff
46 #define CARD_MAX_MEM_SPEED      1000
47
48 #define CARD_CONTROLLER_INDEX   0x03e0
49 #define CARD_CONTROLLER_DATA    0x03e1
50  /* Power register */
51   #define VPP_GET_VCC           0x01
52   #define POWER_ENABLE          0x10
53  #define CARD_VOLTAGE_SENSE     0x1f
54   #define VCC_3VORXV_CAPABLE    0x00
55   #define VCC_XV_ONLY           0x01
56   #define VCC_3V_CAPABLE        0x02
57   #define VCC_5V_ONLY           0x03
58  #define CARD_VOLTAGE_SELECT    0x2f
59   #define VCC_3V                0x01
60   #define VCC_5V                0x00
61   #define VCC_XV                0x02
62   #define VCC_STATUS_3V         0x02
63   #define VCC_STATUS_5V         0x01
64   #define VCC_STATUS_XV         0x03
65  #define GLOBAL_CONTROL         0x1e
66   #define EXWRBK                0x04
67   #define IRQPM_EN              0x08
68   #define CLRPMIRQ              0x10
69
70 #define INTERRUPT_STATUS        0x05fa
71  #define IRQ_A                  0x02
72  #define IRQ_B                  0x04
73
74 #define CONFIGURATION1          0x05fe
75  #define SLOTB_CONFIG           0xc000
76  #define SLOTB_NONE             0x0000
77  #define SLOTB_PCCARD           0x4000
78  #define SLOTB_CF               0x8000
79  #define SLOTB_FLASHROM         0xc000
80
81 #define CARD_CONTROLLER_START   CARD_CONTROLLER_INDEX
82 #define CARD_CONTROLLER_END     CARD_CONTROLLER_DATA
83
84 #define IO_MAX_MAPS     2
85 #define MEM_MAX_MAPS    5
86
87 typedef enum {
88         SLOT_PROBE = 0,
89         SLOT_NOPROBE_IO,
90         SLOT_NOPROBE_MEM,
91         SLOT_NOPROBE_ALL,
92         SLOT_INITIALIZED,
93 } vrc4171_slot_t;
94
95 typedef enum {
96         SLOTB_IS_NONE,
97         SLOTB_IS_PCCARD,
98         SLOTB_IS_CF,
99         SLOTB_IS_FLASHROM,
100 } vrc4171_slotb_t;
101
102 typedef struct vrc4171_socket {
103         vrc4171_slot_t slot;
104         struct pcmcia_socket pcmcia_socket;
105         char name[24];
106         int csc_irq;
107         int io_irq;
108 } vrc4171_socket_t;
109
110 static vrc4171_socket_t vrc4171_sockets[CARD_MAX_SLOTS];
111 static vrc4171_slotb_t vrc4171_slotb = SLOTB_IS_NONE;
112 static char vrc4171_card_name[] = "NEC VRC4171 Card Controller";
113 static unsigned int vrc4171_irq;
114 static uint16_t vrc4171_irq_mask = 0xdeb8;
115
116 static struct resource vrc4171_card_resource[3] = {
117         {       .name           = vrc4171_card_name,
118                 .start          = CARD_CONTROLLER_START,
119                 .end            = CARD_CONTROLLER_END,
120                 .flags          = IORESOURCE_IO,        },
121         {       .name           = vrc4171_card_name,
122                 .start          = INTERRUPT_STATUS,
123                 .end            = INTERRUPT_STATUS,
124                 .flags          = IORESOURCE_IO,        },
125         {       .name           = vrc4171_card_name,
126                 .start          = CONFIGURATION1,
127                 .end            = CONFIGURATION1,
128                 .flags          = IORESOURCE_IO,        },
129 };
130
131 static struct platform_device vrc4171_card_device = {
132         .name           = vrc4171_card_name,
133         .id             = 0,
134         .num_resources  = 3,
135         .resource       = vrc4171_card_resource,
136 };
137
138 static inline uint16_t vrc4171_get_irq_status(void)
139 {
140         return inw(INTERRUPT_STATUS);
141 }
142
143 static inline void vrc4171_set_multifunction_pin(vrc4171_slotb_t config)
144 {
145         uint16_t config1;
146
147         config1 = inw(CONFIGURATION1);
148         config1 &= ~SLOTB_CONFIG;
149
150         switch (config) {
151         case SLOTB_IS_NONE:
152                 config1 |= SLOTB_NONE;
153                 break;
154         case SLOTB_IS_PCCARD:
155                 config1 |= SLOTB_PCCARD;
156                 break;
157         case SLOTB_IS_CF:
158                 config1 |= SLOTB_CF;
159                 break;
160         case SLOTB_IS_FLASHROM:
161                 config1 |= SLOTB_FLASHROM;
162                 break;
163         default:
164                 break;
165         }
166
167         outw(config1, CONFIGURATION1);
168 }
169
170 static inline uint8_t exca_read_byte(int slot, uint8_t index)
171 {
172         if (slot == CARD_SLOTB)
173                 index += CARD_SLOTB_OFFSET;
174
175         outb(index, CARD_CONTROLLER_INDEX);
176         return inb(CARD_CONTROLLER_DATA);
177 }
178
179 static inline uint16_t exca_read_word(int slot, uint8_t index)
180 {
181         uint16_t data;
182
183         if (slot == CARD_SLOTB)
184                 index += CARD_SLOTB_OFFSET;
185
186         outb(index++, CARD_CONTROLLER_INDEX);
187         data = inb(CARD_CONTROLLER_DATA);
188
189         outb(index, CARD_CONTROLLER_INDEX);
190         data |= ((uint16_t)inb(CARD_CONTROLLER_DATA)) << 8;
191
192         return data;
193 }
194
195 static inline uint8_t exca_write_byte(int slot, uint8_t index, uint8_t data)
196 {
197         if (slot == CARD_SLOTB)
198                 index += CARD_SLOTB_OFFSET;
199
200         outb(index, CARD_CONTROLLER_INDEX);
201         outb(data, CARD_CONTROLLER_DATA);
202
203         return data;
204 }
205
206 static inline uint16_t exca_write_word(int slot, uint8_t index, uint16_t data)
207 {
208         if (slot == CARD_SLOTB)
209                 index += CARD_SLOTB_OFFSET;
210
211         outb(index++, CARD_CONTROLLER_INDEX);
212         outb(data, CARD_CONTROLLER_DATA);
213
214         outb(index, CARD_CONTROLLER_INDEX);
215         outb((uint8_t)(data >> 8), CARD_CONTROLLER_DATA);
216
217         return data;
218 }
219
220 static inline int search_nonuse_irq(void)
221 {
222         int i;
223
224         for (i = 0; i < 16; i++) {
225                 if (vrc4171_irq_mask & (1 << i)) {
226                         vrc4171_irq_mask &= ~(1 << i);
227                         return i;
228                 }
229         }
230
231         return -1;
232 }
233
234 static int pccard_init(struct pcmcia_socket *sock)
235 {
236         vrc4171_socket_t *socket;
237         unsigned int slot;
238
239         sock->features |= SS_CAP_PCCARD | SS_CAP_PAGE_REGS;
240         sock->irq_mask = 0;
241         sock->map_size = 0x1000;
242         sock->pci_irq = vrc4171_irq;
243
244         slot = sock->sock;
245         socket = &vrc4171_sockets[slot];
246         socket->csc_irq = search_nonuse_irq();
247         socket->io_irq = search_nonuse_irq();
248
249         return 0;
250 }
251
252 static int pccard_get_status(struct pcmcia_socket *sock, u_int *value)
253 {
254         unsigned int slot;
255         uint8_t status, sense;
256         u_int val = 0;
257
258         if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || value == NULL)
259                 return -EINVAL;
260
261         slot = sock->sock;
262
263         status = exca_read_byte(slot, I365_STATUS);
264         if (exca_read_byte(slot, I365_INTCTL) & I365_PC_IOCARD) {
265                 if (status & I365_CS_STSCHG)
266                         val |= SS_STSCHG;
267         } else {
268                 if (!(status & I365_CS_BVD1))
269                         val |= SS_BATDEAD;
270                 else if ((status & (I365_CS_BVD1 | I365_CS_BVD2)) == I365_CS_BVD1)
271                         val |= SS_BATWARN;
272         }
273         if ((status & I365_CS_DETECT) == I365_CS_DETECT)
274                 val |= SS_DETECT;
275         if (status & I365_CS_WRPROT)
276                 val |= SS_WRPROT;
277         if (status & I365_CS_READY)
278                 val |= SS_READY;
279         if (status & I365_CS_POWERON)
280                 val |= SS_POWERON;
281
282         sense = exca_read_byte(slot, CARD_VOLTAGE_SENSE);
283         switch (sense) {
284         case VCC_3VORXV_CAPABLE:
285                 val |= SS_3VCARD | SS_XVCARD;
286                 break;
287         case VCC_XV_ONLY:
288                 val |= SS_XVCARD;
289                 break;
290         case VCC_3V_CAPABLE:
291                 val |= SS_3VCARD;
292                 break;
293         default:
294                 /* 5V only */
295                 break;
296         }
297
298         *value = val;
299
300         return 0;
301 }
302
303 static inline u_char get_Vcc_value(uint8_t voltage)
304 {
305         switch (voltage) {
306         case VCC_STATUS_3V:
307                 return 33;
308         case VCC_STATUS_5V:
309                 return 50;
310         default:
311                 break;
312         }
313
314         return 0;
315 }
316
317 static inline u_char get_Vpp_value(uint8_t power, u_char Vcc)
318 {
319         if ((power & 0x03) == 0x01 || (power & 0x03) == 0x02)
320                 return Vcc;
321
322         return 0;
323 }
324
325 static int pccard_get_socket(struct pcmcia_socket *sock, socket_state_t *state)
326 {
327         unsigned int slot;
328         uint8_t power, voltage, control, cscint;
329
330         if (sock == NULL || sock->sock >= CARD_MAX_SLOTS || state == NULL)
331                 return -EINVAL;
332
333         slot = sock->sock;
334
335         power = exca_read_byte(slot, I365_POWER);
336         voltage = exca_read_byte(slot, CARD_VOLTAGE_SELECT);
337
338         state->Vcc = get_Vcc_value(voltage);
339         state->Vpp = get_Vpp_value(power, state->Vcc);
340
341         state->flags = 0;
342         if (power & POWER_ENABLE)
343                 state->flags |= SS_PWR_AUTO;
344         if (power & I365_PWR_OUT)
345                 state->flags |= SS_OUTPUT_ENA;
346
347         control = exca_read_byte(slot, I365_INTCTL);
348         if (control & I365_PC_IOCARD)
349                 state->flags |= SS_IOCARD;
350         if (!(control & I365_PC_RESET))
351                 state->flags |= SS_RESET;
352
353         cscint = exca_read_byte(slot, I365_CSCINT);
354         state->csc_mask = 0;
355         if (state->flags & SS_IOCARD) {
356                 if (cscint & I365_CSC_STSCHG)
357                         state->flags |= SS_STSCHG;
358         } else {
359                 if (cscint & I365_CSC_BVD1)
360                         state->csc_mask |= SS_BATDEAD;
361                 if (cscint & I365_CSC_BVD2)
362                         state->csc_mask |= SS_BATWARN;
363         }
364         if (cscint & I365_CSC_READY)
365                 state->csc_mask |= SS_READY;
366         if (cscint & I365_CSC_DETECT)
367                 state->csc_mask |= SS_DETECT;
368
369         return 0;
370 }
371
372 static inline uint8_t set_Vcc_value(u_char Vcc)
373 {
374         switch (Vcc) {
375         case 33:
376                 return VCC_3V;
377         case 50:
378                 return VCC_5V;
379         }
380
381         /* Small voltage is chosen for safety. */
382         return VCC_3V;
383 }
384
385 static int pccard_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
386 {
387         vrc4171_socket_t *socket;
388         unsigned int slot;
389         uint8_t voltage, power, control, cscint;
390
391         if (sock == NULL || sock->sock >= CARD_MAX_SLOTS ||
392             (state->Vpp != state->Vcc && state->Vpp != 0) ||
393             (state->Vcc != 50 && state->Vcc != 33 && state->Vcc != 0))
394                 return -EINVAL;
395
396         slot = sock->sock;
397         socket = &vrc4171_sockets[slot];
398
399         spin_lock_irq(&sock->lock);
400
401         voltage = set_Vcc_value(state->Vcc);
402         exca_write_byte(slot, CARD_VOLTAGE_SELECT, voltage);
403
404         power = POWER_ENABLE;
405         if (state->Vpp == state->Vcc)
406                 power |= VPP_GET_VCC;
407         if (state->flags & SS_OUTPUT_ENA)
408                 power |= I365_PWR_OUT;
409         exca_write_byte(slot, I365_POWER, power);
410
411         control = 0;
412         if (state->io_irq != 0)
413                 control |= socket->io_irq;
414         if (state->flags & SS_IOCARD)
415                 control |= I365_PC_IOCARD;
416         if (state->flags & SS_RESET)
417                 control &= ~I365_PC_RESET;
418         else
419                 control |= I365_PC_RESET;
420         exca_write_byte(slot, I365_INTCTL, control);
421
422         cscint = 0;
423         exca_write_byte(slot, I365_CSCINT, cscint);
424         exca_read_byte(slot, I365_CSC); /* clear CardStatus change */
425         if (state->csc_mask != 0)
426                 cscint |= socket->csc_irq << 8;
427         if (state->flags & SS_IOCARD) {
428                 if (state->csc_mask & SS_STSCHG)
429                         cscint |= I365_CSC_STSCHG;
430         } else {
431                 if (state->csc_mask & SS_BATDEAD)
432                         cscint |= I365_CSC_BVD1;
433                 if (state->csc_mask & SS_BATWARN)
434                         cscint |= I365_CSC_BVD2;
435         }
436         if (state->csc_mask & SS_READY)
437                 cscint |= I365_CSC_READY;
438         if (state->csc_mask & SS_DETECT)
439                 cscint |= I365_CSC_DETECT;
440         exca_write_byte(slot, I365_CSCINT, cscint);
441
442         spin_unlock_irq(&sock->lock);
443
444         return 0;
445 }
446
447 static int pccard_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *io)
448 {
449         unsigned int slot;
450         uint8_t ioctl, addrwin;
451         u_char map;
452
453         if (sock == NULL || sock->sock >= CARD_MAX_SLOTS ||
454             io == NULL || io->map >= IO_MAX_MAPS ||
455             io->start > 0xffff || io->stop > 0xffff || io->start > io->stop)
456                 return -EINVAL;
457
458         slot = sock->sock;
459         map = io->map;
460
461         addrwin = exca_read_byte(slot, I365_ADDRWIN);
462         if (addrwin & I365_ENA_IO(map)) {
463                 addrwin &= ~I365_ENA_IO(map);
464                 exca_write_byte(slot, I365_ADDRWIN, addrwin);
465         }
466
467         exca_write_word(slot, I365_IO(map)+I365_W_START, io->start);
468         exca_write_word(slot, I365_IO(map)+I365_W_STOP, io->stop);
469
470         ioctl = 0;
471         if (io->speed > 0)
472                 ioctl |= I365_IOCTL_WAIT(map);
473         if (io->flags & MAP_16BIT)
474                 ioctl |= I365_IOCTL_16BIT(map);
475         if (io->flags & MAP_AUTOSZ)
476                 ioctl |= I365_IOCTL_IOCS16(map);
477         if (io->flags & MAP_0WS)
478                 ioctl |= I365_IOCTL_0WS(map);
479         exca_write_byte(slot, I365_IOCTL, ioctl);
480
481         if (io->flags & MAP_ACTIVE) {
482                 addrwin |= I365_ENA_IO(map);
483                 exca_write_byte(slot, I365_ADDRWIN, addrwin);
484         }
485
486         return 0;
487 }
488
489 static int pccard_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *mem)
490 {
491         unsigned int slot;
492         uint16_t start, stop, offset;
493         uint8_t addrwin;
494         u_char map;
495
496         if (sock == NULL || sock->sock >= CARD_MAX_SLOTS ||
497             mem == NULL || mem->map >= MEM_MAX_MAPS ||
498             mem->res->start < CARD_MEM_START || mem->res->start > CARD_MEM_END ||
499             mem->res->end < CARD_MEM_START || mem->res->end > CARD_MEM_END ||
500             mem->res->start > mem->res->end ||
501             mem->card_start > CARD_MAX_MEM_OFFSET ||
502             mem->speed > CARD_MAX_MEM_SPEED)
503                 return -EINVAL;
504
505         slot = sock->sock;
506         map = mem->map;
507
508         addrwin = exca_read_byte(slot, I365_ADDRWIN);
509         if (addrwin & I365_ENA_MEM(map)) {
510                 addrwin &= ~I365_ENA_MEM(map);
511                 exca_write_byte(slot, I365_ADDRWIN, addrwin);
512         }
513
514         start = (mem->res->start >> 12) & 0x3fff;
515         if (mem->flags & MAP_16BIT)
516                 start |= I365_MEM_16BIT;
517         exca_write_word(slot, I365_MEM(map)+I365_W_START, start);
518
519         stop = (mem->res->end >> 12) & 0x3fff;
520         switch (mem->speed) {
521         case 0:
522                 break;
523         case 1:
524                 stop |= I365_MEM_WS0;
525                 break;
526         case 2:
527                 stop |= I365_MEM_WS1;
528                 break;
529         default:
530                 stop |= I365_MEM_WS0 | I365_MEM_WS1;
531                 break;
532         }
533         exca_write_word(slot, I365_MEM(map)+I365_W_STOP, stop);
534
535         offset = (mem->card_start >> 12) & 0x3fff;
536         if (mem->flags & MAP_ATTRIB)
537                 offset |= I365_MEM_REG;
538         if (mem->flags & MAP_WRPROT)
539                 offset |= I365_MEM_WRPROT;
540         exca_write_word(slot, I365_MEM(map)+I365_W_OFF, offset);
541
542         if (mem->flags & MAP_ACTIVE) {
543                 addrwin |= I365_ENA_MEM(map);
544                 exca_write_byte(slot, I365_ADDRWIN, addrwin);
545         }
546
547         return 0;
548 }
549
550 static struct pccard_operations vrc4171_pccard_operations = {
551         .init                   = pccard_init,
552         .get_status             = pccard_get_status,
553         .get_socket             = pccard_get_socket,
554         .set_socket             = pccard_set_socket,
555         .set_io_map             = pccard_set_io_map,
556         .set_mem_map            = pccard_set_mem_map,
557 };
558
559 static inline unsigned int get_events(int slot)
560 {
561         unsigned int events = 0;
562         uint8_t status, csc;
563
564         status = exca_read_byte(slot, I365_STATUS);
565         csc = exca_read_byte(slot, I365_CSC);
566
567         if (exca_read_byte(slot, I365_INTCTL) & I365_PC_IOCARD) {
568                 if ((csc & I365_CSC_STSCHG) && (status & I365_CS_STSCHG))
569                         events |= SS_STSCHG;
570         } else {
571                 if (csc & (I365_CSC_BVD1 | I365_CSC_BVD2)) {
572                         if (!(status & I365_CS_BVD1))
573                                 events |= SS_BATDEAD;
574                         else if ((status & (I365_CS_BVD1 | I365_CS_BVD2)) == I365_CS_BVD1)
575                                 events |= SS_BATWARN;
576                 }
577         }
578         if ((csc & I365_CSC_READY) && (status & I365_CS_READY))
579                 events |= SS_READY;
580         if ((csc & I365_CSC_DETECT) && ((status & I365_CS_DETECT) == I365_CS_DETECT))
581                 events |= SS_DETECT;
582
583         return events;
584 }
585
586 static irqreturn_t pccard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
587 {
588         vrc4171_socket_t *socket;
589         unsigned int events;
590         irqreturn_t retval = IRQ_NONE;
591         uint16_t status;
592
593         status = vrc4171_get_irq_status();
594         if (status & IRQ_A) {
595                 socket = &vrc4171_sockets[CARD_SLOTA];
596                 if (socket->slot == SLOT_INITIALIZED) {
597                         if (status & (1 << socket->csc_irq)) {
598                                 events = get_events(CARD_SLOTA);
599                                 if (events != 0) {
600                                         pcmcia_parse_events(&socket->pcmcia_socket, events);
601                                         retval = IRQ_HANDLED;
602                                 }
603                         }
604                 }
605         }
606
607         if (status & IRQ_B) {
608                 socket = &vrc4171_sockets[CARD_SLOTB];
609                 if (socket->slot == SLOT_INITIALIZED) {
610                         if (status & (1 << socket->csc_irq)) {
611                                 events = get_events(CARD_SLOTB);
612                                 if (events != 0) {
613                                         pcmcia_parse_events(&socket->pcmcia_socket, events);
614                                         retval = IRQ_HANDLED;
615                                 }
616                         }
617                 }
618         }
619
620         return retval;
621 }
622
623 static inline void reserve_using_irq(int slot)
624 {
625         unsigned int irq;
626
627         irq = exca_read_byte(slot, I365_INTCTL);
628         irq &= 0x0f;
629         vrc4171_irq_mask &= ~(1 << irq);
630
631         irq = exca_read_byte(slot, I365_CSCINT);
632         irq = (irq & 0xf0) >> 4;
633         vrc4171_irq_mask &= ~(1 << irq);
634 }
635
636 static int __devinit vrc4171_add_sockets(void)
637 {
638         vrc4171_socket_t *socket;
639         int slot, retval;
640
641         for (slot = 0; slot < CARD_MAX_SLOTS; slot++) {
642                 if (slot == CARD_SLOTB && vrc4171_slotb == SLOTB_IS_NONE)
643                         continue;
644
645                 socket = &vrc4171_sockets[slot];
646                 if (socket->slot != SLOT_PROBE) {
647                         uint8_t addrwin;
648
649                         switch (socket->slot) {
650                         case SLOT_NOPROBE_MEM:
651                                 addrwin = exca_read_byte(slot, I365_ADDRWIN);
652                                 addrwin &= 0x1f;
653                                 exca_write_byte(slot, I365_ADDRWIN, addrwin);
654                                 break;
655                         case SLOT_NOPROBE_IO:
656                                 addrwin = exca_read_byte(slot, I365_ADDRWIN);
657                                 addrwin &= 0xc0;
658                                 exca_write_byte(slot, I365_ADDRWIN, addrwin);
659                                 break;
660                         default:
661                                 break;
662                         }
663
664                         reserve_using_irq(slot);
665                         continue;
666                 }
667
668                 sprintf(socket->name, "NEC VRC4171 Card Slot %1c", 'A' + slot);
669                 socket->pcmcia_socket.dev.dev = &vrc4171_card_device.dev;
670                 socket->pcmcia_socket.ops = &vrc4171_pccard_operations;
671                 socket->pcmcia_socket.owner = THIS_MODULE;
672
673                 retval = pcmcia_register_socket(&socket->pcmcia_socket);
674                 if (retval < 0)
675                         return retval;
676
677                 exca_write_byte(slot, I365_ADDRWIN, 0);
678                 exca_write_byte(slot, GLOBAL_CONTROL, 0);
679
680                 socket->slot = SLOT_INITIALIZED;
681         }
682
683         return 0;
684 }
685
686 static void vrc4171_remove_sockets(void)
687 {
688         vrc4171_socket_t *socket;
689         int slot;
690
691         for (slot = 0; slot < CARD_MAX_SLOTS; slot++) {
692                 if (slot == CARD_SLOTB && vrc4171_slotb == SLOTB_IS_NONE)
693                         continue;
694
695                 socket = &vrc4171_sockets[slot];
696                 if (socket->slot == SLOT_INITIALIZED)
697                         pcmcia_unregister_socket(&socket->pcmcia_socket);
698
699                 socket->slot = SLOT_PROBE;
700         }
701 }
702
703 static int __devinit vrc4171_card_setup(char *options)
704 {
705         if (options == NULL || *options == '\0')
706                 return 0;
707
708         if (strncmp(options, "irq:", 4) == 0) {
709                 int irq;
710                 options += 4;
711                 irq = simple_strtoul(options, &options, 0);
712                 if (irq >= 0 && irq < NR_IRQS)
713                         vrc4171_irq = irq;
714
715                 if (*options != ',')
716                         return 0;
717                 options++;
718         }
719
720         if (strncmp(options, "slota:", 6) == 0) {
721                 options += 6;
722                 if (*options != '\0') {
723                         if (strncmp(options, "memnoprobe", 10) == 0) {
724                                 vrc4171_sockets[CARD_SLOTA].slot = SLOT_NOPROBE_MEM;
725                                 options += 10;
726                         } else if (strncmp(options, "ionoprobe", 9) == 0) {
727                                 vrc4171_sockets[CARD_SLOTA].slot = SLOT_NOPROBE_IO;
728                                 options += 9;
729                         } else if ( strncmp(options, "noprobe", 7) == 0) {
730                                 vrc4171_sockets[CARD_SLOTA].slot = SLOT_NOPROBE_ALL;
731                                 options += 7;
732                         }
733
734                         if (*options != ',')
735                                 return 0;
736                         options++;
737                 } else
738                         return 0;
739
740         }
741
742         if (strncmp(options, "slotb:", 6) == 0) {
743                 options += 6;
744                 if (*options != '\0') {
745                         if (strncmp(options, "pccard", 6) == 0) {
746                                 vrc4171_slotb = SLOTB_IS_PCCARD;
747                                 options += 6;
748                         } else if (strncmp(options, "cf", 2) == 0) {
749                                 vrc4171_slotb = SLOTB_IS_CF;
750                                 options += 2;
751                         } else if (strncmp(options, "flashrom", 8) == 0) {
752                                 vrc4171_slotb = SLOTB_IS_FLASHROM;
753                                 options += 8;
754                         } else if (strncmp(options, "none", 4) == 0) {
755                                 vrc4171_slotb = SLOTB_IS_NONE;
756                                 options += 4;
757                         }
758
759                         if (*options != ',')
760                                 return 0;
761                         options++;
762
763                         if (strncmp(options, "memnoprobe", 10) == 0)
764                                 vrc4171_sockets[CARD_SLOTB].slot = SLOT_NOPROBE_MEM;
765                         if (strncmp(options, "ionoprobe", 9) == 0)
766                                 vrc4171_sockets[CARD_SLOTB].slot = SLOT_NOPROBE_IO;
767                         if (strncmp(options, "noprobe", 7) == 0)
768                                 vrc4171_sockets[CARD_SLOTB].slot = SLOT_NOPROBE_ALL;
769                 }
770         }
771
772         return 0;
773 }
774
775 __setup("vrc4171_card=", vrc4171_card_setup);
776
777 static int vrc4171_card_suspend(struct device *dev, pm_message_t state, u32 level)
778 {
779         int retval = 0;
780
781         if (level == SUSPEND_SAVE_STATE)
782                 retval = pcmcia_socket_dev_suspend(dev, state);
783
784         return retval;
785 }
786
787 static int vrc4171_card_resume(struct device *dev, u32 level)
788 {
789         int retval = 0;
790
791         if (level == RESUME_RESTORE_STATE)
792                 retval = pcmcia_socket_dev_resume(dev);
793
794         return retval;
795 }
796
797 static struct device_driver vrc4171_card_driver = {
798         .name           = vrc4171_card_name,
799         .bus            = &platform_bus_type,
800         .suspend        = vrc4171_card_suspend,
801         .resume         = vrc4171_card_resume,
802 };
803
804 static int __devinit vrc4171_card_init(void)
805 {
806         int retval;
807
808         retval = driver_register(&vrc4171_card_driver);
809         if (retval < 0)
810                 return retval;
811
812         retval = platform_device_register(&vrc4171_card_device);
813         if (retval < 0) {
814                 driver_unregister(&vrc4171_card_driver);
815                 return retval;
816         }
817
818         vrc4171_set_multifunction_pin(vrc4171_slotb);
819
820         retval = vrc4171_add_sockets();
821         if (retval == 0)
822                 retval = request_irq(vrc4171_irq, pccard_interrupt, SA_SHIRQ,
823                                      vrc4171_card_name, vrc4171_sockets);
824
825         if (retval < 0) {
826                 vrc4171_remove_sockets();
827                 platform_device_unregister(&vrc4171_card_device);
828                 driver_unregister(&vrc4171_card_driver);
829                 return retval;
830         }
831
832         printk(KERN_INFO "%s, connected to IRQ %d\n", vrc4171_card_driver.name, vrc4171_irq);
833
834         return 0;
835 }
836
837 static void __devexit vrc4171_card_exit(void)
838 {
839         free_irq(vrc4171_irq, vrc4171_sockets);
840         vrc4171_remove_sockets();
841         platform_device_unregister(&vrc4171_card_device);
842         driver_unregister(&vrc4171_card_driver);
843 }
844
845 module_init(vrc4171_card_init);
846 module_exit(vrc4171_card_exit);