Merge master.kernel.org:/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[pandora-kernel.git] / arch / arm26 / kernel / ecard.c
1 /*
2  *  linux/arch/arm26/kernel/ecard.c
3  *
4  *  Copyright 1995-2001 Russell King
5  *  Copyright 2003 Ian Molton
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *  Find all installed expansion cards, and handle interrupts from them.
12  *
13  *  Created from information from Acorns RiscOS3 PRMs
14  *  15-Jun-2003 IM      Modified from ARM32 (RiscPC capable) version
15  *  10-Jan-1999 RMK     Run loaders in a simulated RISC OS environment.
16  *  06-May-1997 RMK     Added blacklist for cards whose loader doesn't work.
17  *  12-Sep-1997 RMK     Created new handling of interrupt enables/disables
18  *                      - cards can now register their own routine to control
19  *                      interrupts (recommended).
20  *  29-Sep-1997 RMK     Expansion card interrupt hardware not being re-enabled
21  *                      on reset from Linux. (Caused cards not to respond
22  *                      under RiscOS without hard reset).
23  *
24  */
25 #define ECARD_C
26
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/interrupt.h>
32 #include <linux/reboot.h>
33 #include <linux/mm.h>
34 #include <linux/slab.h>
35 #include <linux/proc_fs.h>
36 #include <linux/device.h>
37 #include <linux/init.h>
38
39 #include <asm/dma.h>
40 #include <asm/ecard.h>
41 #include <asm/hardware.h>
42 #include <asm/io.h>
43 #include <asm/irq.h>
44 #include <asm/mmu_context.h>
45 #include <asm/irqchip.h>
46 #include <asm/tlbflush.h>
47
48 enum req {
49         req_readbytes,
50         req_reset
51 };
52
53 struct ecard_request {
54         enum req        req;
55         ecard_t         *ec;
56         unsigned int    address;
57         unsigned int    length;
58         unsigned int    use_loader;
59         void            *buffer;
60 };
61
62 struct expcard_blacklist {
63         unsigned short   manufacturer;
64         unsigned short   product;
65         const char      *type;
66 };
67
68 static ecard_t *cards;
69 static ecard_t *slot_to_expcard[MAX_ECARDS];
70 static unsigned int ectcr;
71
72 /* List of descriptions of cards which don't have an extended
73  * identification, or chunk directories containing a description.
74  */
75 static struct expcard_blacklist __initdata blacklist[] = {
76         { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
77 };
78
79 asmlinkage extern int
80 ecard_loader_reset(volatile unsigned char *pa, loader_t loader);
81 asmlinkage extern int
82 ecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);
83
84 static const struct ecard_id *
85 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec);
86
87 static inline unsigned short
88 ecard_getu16(unsigned char *v)
89 {
90         return v[0] | v[1] << 8;
91 }
92
93 static inline signed long
94 ecard_gets24(unsigned char *v)
95 {
96         return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
97 }
98
99 static inline ecard_t *
100 slot_to_ecard(unsigned int slot)
101 {
102         return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
103 }
104
105 /* ===================== Expansion card daemon ======================== */
106 /*
107  * Since the loader programs on the expansion cards need to be run
108  * in a specific environment, create a separate task with this
109  * environment up, and pass requests to this task as and when we
110  * need to.
111  *
112  * This should allow 99% of loaders to be called from Linux.
113  *
114  * From a security standpoint, we trust the card vendors.  This
115  * may be a misplaced trust.
116  */
117 #define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)
118 #define POD_INT_ADDR(x) ((volatile unsigned char *)\
119                          ((BUS_ADDR((x)) - IO_BASE) + IO_START))
120
121 static inline void ecard_task_reset(struct ecard_request *req)
122 {
123         struct expansion_card *ec = req->ec;
124         if (ec->loader)
125                 ecard_loader_reset(POD_INT_ADDR(ec->podaddr), ec->loader);
126 }
127
128 static void
129 ecard_task_readbytes(struct ecard_request *req)
130 {
131         unsigned char *buf = (unsigned char *)req->buffer;
132         volatile unsigned char *base_addr =
133                 (volatile unsigned char *)POD_INT_ADDR(req->ec->podaddr);
134         unsigned int len = req->length;
135         unsigned int off = req->address;
136
137         if (!req->use_loader || !req->ec->loader) {
138                 off *= 4;
139                 while (len--) {
140                         *buf++ = base_addr[off];
141                         off += 4;
142                 }
143         } else {
144                 while(len--) {
145                         /*
146                          * The following is required by some
147                          * expansion card loader programs.
148                          */
149                         *(unsigned long *)0x108 = 0;
150                         *buf++ = ecard_loader_read(off++, base_addr,
151                                                    req->ec->loader);
152                 }
153         }
154 }
155
156 static void ecard_do_request(struct ecard_request *req)
157 {
158         switch (req->req) {
159         case req_readbytes:
160                 ecard_task_readbytes(req);
161                 break;
162
163         case req_reset:
164                 ecard_task_reset(req);
165                 break;
166         }
167 }
168
169 /*
170  * On 26-bit processors, we don't need the kcardd thread to access the
171  * expansion card loaders.  We do it directly.
172  */
173 #define ecard_call(req) ecard_do_request(req)
174
175 /* ======================= Mid-level card control ===================== */
176
177 static void
178 ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
179 {
180         struct ecard_request req;
181
182         req.req         = req_readbytes;
183         req.ec          = ec;
184         req.address     = off;
185         req.length      = len;
186         req.use_loader  = useld;
187         req.buffer      = addr;
188
189         ecard_call(&req);
190 }
191
192 int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
193 {
194         struct ex_chunk_dir excd;
195         int index = 16;
196         int useld = 0;
197
198         if (!ec->cid.cd)
199                 return 0;
200
201         while(1) {
202                 ecard_readbytes(&excd, ec, index, 8, useld);
203                 index += 8;
204                 if (c_id(&excd) == 0) {
205                         if (!useld && ec->loader) {
206                                 useld = 1;
207                                 index = 0;
208                                 continue;
209                         }
210                         return 0;
211                 }
212                 if (c_id(&excd) == 0xf0) { /* link */
213                         index = c_start(&excd);
214                         continue;
215                 }
216                 if (c_id(&excd) == 0x80) { /* loader */
217                         if (!ec->loader) {
218                                 ec->loader = (loader_t)kmalloc(c_len(&excd),
219                                                                GFP_KERNEL);
220                                 if (ec->loader)
221                                         ecard_readbytes(ec->loader, ec,
222                                                         (int)c_start(&excd),
223                                                         c_len(&excd), useld);
224                                 else
225                                         return 0;
226                         }
227                         continue;
228                 }
229                 if (c_id(&excd) == id && num-- == 0)
230                         break;
231         }
232
233         if (c_id(&excd) & 0x80) {
234                 switch (c_id(&excd) & 0x70) {
235                 case 0x70:
236                         ecard_readbytes((unsigned char *)excd.d.string, ec,
237                                         (int)c_start(&excd), c_len(&excd),
238                                         useld);
239                         break;
240                 case 0x00:
241                         break;
242                 }
243         }
244         cd->start_offset = c_start(&excd);
245         memcpy(cd->d.string, excd.d.string, 256);
246         return 1;
247 }
248
249 /* ======================= Interrupt control ============================ */
250
251 static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
252 {
253 }
254
255 static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
256 {
257 }
258
259 static int ecard_def_irq_pending(ecard_t *ec)
260 {
261         return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;
262 }
263
264 static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
265 {
266         panic("ecard_def_fiq_enable called - impossible");
267 }
268
269 static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
270 {
271         panic("ecard_def_fiq_disable called - impossible");
272 }
273
274 static int ecard_def_fiq_pending(ecard_t *ec)
275 {
276         return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;
277 }
278
279 static expansioncard_ops_t ecard_default_ops = {
280         ecard_def_irq_enable,
281         ecard_def_irq_disable,
282         ecard_def_irq_pending,
283         ecard_def_fiq_enable,
284         ecard_def_fiq_disable,
285         ecard_def_fiq_pending
286 };
287
288 /*
289  * Enable and disable interrupts from expansion cards.
290  * (interrupts are disabled for these functions).
291  *
292  * They are not meant to be called directly, but via enable/disable_irq.
293  */
294 static void ecard_irq_unmask(unsigned int irqnr)
295 {
296         ecard_t *ec = slot_to_ecard(irqnr - 32);
297
298         if (ec) {
299                 if (!ec->ops)
300                         ec->ops = &ecard_default_ops;
301
302                 if (ec->claimed && ec->ops->irqenable)
303                         ec->ops->irqenable(ec, irqnr);
304                 else
305                         printk(KERN_ERR "ecard: rejecting request to "
306                                 "enable IRQs for %d\n", irqnr);
307         }
308 }
309
310 static void ecard_irq_mask(unsigned int irqnr)
311 {
312         ecard_t *ec = slot_to_ecard(irqnr - 32);
313
314         if (ec) {
315                 if (!ec->ops)
316                         ec->ops = &ecard_default_ops;
317
318                 if (ec->ops && ec->ops->irqdisable)
319                         ec->ops->irqdisable(ec, irqnr);
320         }
321 }
322
323 static struct irqchip ecard_chip = {
324         .ack    = ecard_irq_mask,
325         .mask   = ecard_irq_mask,
326         .unmask = ecard_irq_unmask,
327 };
328
329 void ecard_enablefiq(unsigned int fiqnr)
330 {
331         ecard_t *ec = slot_to_ecard(fiqnr);
332
333         if (ec) {
334                 if (!ec->ops)
335                         ec->ops = &ecard_default_ops;
336
337                 if (ec->claimed && ec->ops->fiqenable)
338                         ec->ops->fiqenable(ec, fiqnr);
339                 else
340                         printk(KERN_ERR "ecard: rejecting request to "
341                                 "enable FIQs for %d\n", fiqnr);
342         }
343 }
344
345 void ecard_disablefiq(unsigned int fiqnr)
346 {
347         ecard_t *ec = slot_to_ecard(fiqnr);
348
349         if (ec) {
350                 if (!ec->ops)
351                         ec->ops = &ecard_default_ops;
352
353                 if (ec->ops->fiqdisable)
354                         ec->ops->fiqdisable(ec, fiqnr);
355         }
356 }
357
358 static void
359 ecard_dump_irq_state(ecard_t *ec)
360 {
361         printk("  %d: %sclaimed, ",
362                ec->slot_no,
363                ec->claimed ? "" : "not ");
364
365         if (ec->ops && ec->ops->irqpending &&
366             ec->ops != &ecard_default_ops)
367                 printk("irq %spending\n",
368                        ec->ops->irqpending(ec) ? "" : "not ");
369         else
370                 printk("irqaddr %p, mask = %02X, status = %02X\n",
371                        ec->irqaddr, ec->irqmask, *ec->irqaddr);
372 }
373
374 static void ecard_check_lockup(struct irqdesc *desc)
375 {
376         static int last, lockup;
377         ecard_t *ec;
378
379         /*
380          * If the timer interrupt has not run since the last million
381          * unrecognised expansion card interrupts, then there is
382          * something seriously wrong.  Disable the expansion card
383          * interrupts so at least we can continue.
384          *
385          * Maybe we ought to start a timer to re-enable them some time
386          * later?
387          */
388         if (last == jiffies) {
389                 lockup += 1;
390                 if (lockup > 1000000) {
391                         printk(KERN_ERR "\nInterrupt lockup detected - "
392                                "disabling all expansion card interrupts\n");
393
394                         desc->chip->mask(IRQ_EXPANSIONCARD);
395
396                         printk("Expansion card IRQ state:\n");
397
398                         for (ec = cards; ec; ec = ec->next)
399                                 ecard_dump_irq_state(ec);
400                 }
401         } else
402                 lockup = 0;
403
404         /*
405          * If we did not recognise the source of this interrupt,
406          * warn the user, but don't flood the user with these messages.
407          */
408         if (!last || time_after(jiffies, (unsigned long)(last + 5*HZ))) {
409                 last = jiffies;
410                 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
411         }
412 }
413
414 static void
415 ecard_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
416 {
417         ecard_t *ec;
418         int called = 0;
419
420         desc->chip->mask(irq);
421         for (ec = cards; ec; ec = ec->next) {
422                 int pending;
423
424                 if (!ec->claimed || ec->irq == NO_IRQ)
425                         continue;
426
427                 if (ec->ops && ec->ops->irqpending)
428                         pending = ec->ops->irqpending(ec);
429                 else
430                         pending = ecard_default_ops.irqpending(ec);
431
432                 if (pending) {
433                         struct irqdesc *d = irq_desc + ec->irq;
434                         d->handle(ec->irq, d, regs);
435                         called ++;
436                 }
437         }
438         desc->chip->unmask(irq);
439
440         if (called == 0)
441                 ecard_check_lockup(desc);
442 }
443
444 #define ecard_irqexp_handler NULL
445 #define ecard_probeirqhw() (0)
446
447 unsigned int ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
448 {
449         unsigned long address = 0;
450         int slot = ec->slot_no;
451
452         ectcr &= ~(1 << slot);
453
454         switch (type) {
455         case ECARD_MEMC:
456                 address = IO_EC_MEMC_BASE + (slot << 12);
457                 break;
458
459         case ECARD_IOC:
460                 address = IO_EC_IOC_BASE + (slot << 12) + (speed << 17);
461                 break;
462
463         default:
464                 break;
465         }
466
467         return address;
468 }
469
470 static int ecard_prints(char *buffer, ecard_t *ec)
471 {
472         char *start = buffer;
473
474         buffer += sprintf(buffer, "  %d: ", ec->slot_no);
475
476         if (ec->cid.id == 0) {
477                 struct in_chunk_dir incd;
478
479                 buffer += sprintf(buffer, "[%04X:%04X] ",
480                         ec->cid.manufacturer, ec->cid.product);
481
482                 if (!ec->card_desc && ec->cid.cd &&
483                     ecard_readchunk(&incd, ec, 0xf5, 0)) {
484                         ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
485
486                         if (ec->card_desc)
487                                 strcpy((char *)ec->card_desc, incd.d.string);
488                 }
489
490                 buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
491         } else
492                 buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
493
494         return buffer - start;
495 }
496
497 static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
498 {
499         ecard_t *ec = cards;
500         off_t at = 0;
501         int len, cnt;
502
503         cnt = 0;
504         while (ec && count > cnt) {
505                 len = ecard_prints(buf, ec);
506                 at += len;
507                 if (at >= pos) {
508                         if (!*start) {
509                                 *start = buf + (pos - (at - len));
510                                 cnt = at - pos;
511                         } else
512                                 cnt += len;
513                         buf += len;
514                 }
515                 ec = ec->next;
516         }
517         return (count > cnt) ? cnt : count;
518 }
519
520 static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
521
522 static void ecard_proc_init(void)
523 {
524         proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
525         create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
526                 get_ecard_dev_info);
527 }
528
529 #define ec_set_resource(ec,nr,st,sz,flg)                        \
530         do {                                                    \
531                 (ec)->resource[nr].name = ec->dev.bus_id;       \
532                 (ec)->resource[nr].start = st;                  \
533                 (ec)->resource[nr].end = (st) + (sz) - 1;       \
534                 (ec)->resource[nr].flags = flg;                 \
535         } while (0)
536
537 static void __init ecard_init_resources(struct expansion_card *ec)
538 {
539         unsigned long base = PODSLOT_IOC0_BASE;
540         unsigned int slot = ec->slot_no;
541         int i;
542
543         ec_set_resource(ec, ECARD_RES_MEMC,
544                         PODSLOT_MEMC_BASE + (slot << 14),
545                         PODSLOT_MEMC_SIZE, IORESOURCE_MEM);
546
547         for (i = 0; i < ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++) {
548                 ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
549                                 base + (slot << 14) + (i << 19),
550                                 PODSLOT_IOC_SIZE, IORESOURCE_MEM);
551         }
552
553         for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
554                 if (ec->resource[i].start &&
555                     request_resource(&iomem_resource, &ec->resource[i])) {
556                         printk(KERN_ERR "%s: resource(s) not available\n",
557                                 ec->dev.bus_id);
558                         ec->resource[i].end -= ec->resource[i].start;
559                         ec->resource[i].start = 0;
560                 }
561         }
562 }
563
564 static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
565 {
566         struct expansion_card *ec = ECARD_DEV(dev);
567         return sprintf(buf, "%u\n", ec->irq);
568 }
569
570 static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
571 {
572         struct expansion_card *ec = ECARD_DEV(dev);
573         return sprintf(buf, "%u\n", ec->cid.manufacturer);
574 }
575
576 static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
577 {
578         struct expansion_card *ec = ECARD_DEV(dev);
579         return sprintf(buf, "%u\n", ec->cid.product);
580 }
581
582 static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
583 {
584         struct expansion_card *ec = ECARD_DEV(dev);
585         return sprintf(buf, "%u\n", ec->dma);
586 }
587
588 static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
589 {
590         struct expansion_card *ec = ECARD_DEV(dev);
591         char *str = buf;
592         int i;
593
594         for (i = 0; i < ECARD_NUM_RESOURCES; i++)
595                 str += sprintf(str, "%08lx %08lx %08lx\n",
596                                 ec->resource[i].start,
597                                 ec->resource[i].end,
598                                 ec->resource[i].flags);
599
600         return str - buf;
601 }
602
603 static DEVICE_ATTR(irq, S_IRUGO, ecard_show_irq, NULL);
604 static DEVICE_ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL);
605 static DEVICE_ATTR(device, S_IRUGO, ecard_show_device, NULL);
606 static DEVICE_ATTR(dma, S_IRUGO, ecard_show_dma, NULL);
607 static DEVICE_ATTR(resource, S_IRUGO, ecard_show_resources, NULL);
608
609 /*
610  * Probe for an expansion card.
611  *
612  * If bit 1 of the first byte of the card is set, then the
613  * card does not exist.
614  */
615 static int __init
616 ecard_probe(int slot, card_type_t type)
617 {
618         ecard_t **ecp;
619         ecard_t *ec;
620         struct ex_ecid cid;
621         int i, rc = -ENOMEM;
622
623         ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
624         if (!ec)
625                 goto nomem;
626
627         ec->slot_no     = slot;
628         ec->type        = type;
629         ec->irq         = NO_IRQ;
630         ec->fiq         = NO_IRQ;
631         ec->dma         = NO_DMA;
632         ec->card_desc   = NULL;
633         ec->ops         = &ecard_default_ops;
634
635         rc = -ENODEV;
636         if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
637                 goto nodev;
638
639         cid.r_zero = 1;
640         ecard_readbytes(&cid, ec, 0, 16, 0);
641         if (cid.r_zero)
642                 goto nodev;
643
644         ec->cid.id      = cid.r_id;
645         ec->cid.cd      = cid.r_cd;
646         ec->cid.is      = cid.r_is;
647         ec->cid.w       = cid.r_w;
648         ec->cid.manufacturer = ecard_getu16(cid.r_manu);
649         ec->cid.product = ecard_getu16(cid.r_prod);
650         ec->cid.country = cid.r_country;
651         ec->cid.irqmask = cid.r_irqmask;
652         ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
653         ec->cid.fiqmask = cid.r_fiqmask;
654         ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
655         ec->fiqaddr     =
656         ec->irqaddr     = (unsigned char *)ioaddr(ec->podaddr);
657
658         if (ec->cid.is) {
659                 ec->irqmask = ec->cid.irqmask;
660                 ec->irqaddr += ec->cid.irqoff;
661                 ec->fiqmask = ec->cid.fiqmask;
662                 ec->fiqaddr += ec->cid.fiqoff;
663         } else {
664                 ec->irqmask = 1;
665                 ec->fiqmask = 4;
666         }
667
668         for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
669                 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
670                     blacklist[i].product == ec->cid.product) {
671                         ec->card_desc = blacklist[i].type;
672                         break;
673                 }
674
675         snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
676         ec->dev.parent = NULL;
677         ec->dev.bus    = &ecard_bus_type;
678         ec->dev.dma_mask = &ec->dma_mask;
679         ec->dma_mask = (u64)0xffffffff;
680
681         ecard_init_resources(ec);
682
683         /*
684          * hook the interrupt handlers
685          */
686         ec->irq = 32 + slot;
687         set_irq_chip(ec->irq, &ecard_chip);
688         set_irq_handler(ec->irq, do_level_IRQ);
689         set_irq_flags(ec->irq, IRQF_VALID);
690
691         for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
692
693         *ecp = ec;
694         slot_to_expcard[slot] = ec;
695
696         device_register(&ec->dev);
697         device_create_file(&ec->dev, &dev_attr_dma);
698         device_create_file(&ec->dev, &dev_attr_irq);
699         device_create_file(&ec->dev, &dev_attr_resource);
700         device_create_file(&ec->dev, &dev_attr_vendor);
701         device_create_file(&ec->dev, &dev_attr_device); 
702
703         return 0;
704
705 nodev:
706         kfree(ec);
707 nomem:
708         return rc;
709 }
710
711 /*
712  * Initialise the expansion card system.
713  * Locate all hardware - interrupt management and
714  * actual cards.
715  */
716 static int __init ecard_init(void)
717 {
718         int slot, irqhw;
719
720         printk("Probing expansion cards\n");
721
722         for (slot = 0; slot < MAX_ECARDS; slot ++) {
723                 ecard_probe(slot, ECARD_IOC);
724         }
725
726         irqhw = ecard_probeirqhw();
727
728         set_irq_chained_handler(IRQ_EXPANSIONCARD,
729                                 irqhw ? ecard_irqexp_handler : ecard_irq_handler);
730
731         ecard_proc_init();
732
733         return 0;
734 }
735
736 subsys_initcall(ecard_init);
737
738 /*
739  *      ECARD "bus"
740  */
741 static const struct ecard_id *
742 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
743 {
744         int i;
745
746         for (i = 0; ids[i].manufacturer != 65535; i++)
747                 if (ec->cid.manufacturer == ids[i].manufacturer &&
748                     ec->cid.product == ids[i].product)
749                         return ids + i;
750
751         return NULL;
752 }
753
754 static int ecard_drv_probe(struct device *dev)
755 {
756         struct expansion_card *ec = ECARD_DEV(dev);
757         struct ecard_driver *drv = ECARD_DRV(dev->driver);
758         const struct ecard_id *id;
759         int ret;
760
761         id = ecard_match_device(drv->id_table, ec);
762
763         ecard_claim(ec);
764         ret = drv->probe(ec, id);
765         if (ret)
766                 ecard_release(ec);
767         return ret;
768 }
769
770 static int ecard_drv_remove(struct device *dev)
771 {
772         struct expansion_card *ec = ECARD_DEV(dev);
773         struct ecard_driver *drv = ECARD_DRV(dev->driver);
774
775         drv->remove(ec);
776         ecard_release(ec);
777
778         return 0;
779 }
780
781 /*
782  * Before rebooting, we must make sure that the expansion card is in a
783  * sensible state, so it can be re-detected.  This means that the first
784  * page of the ROM must be visible.  We call the expansion cards reset
785  * handler, if any.
786  */
787 static void ecard_drv_shutdown(struct device *dev)
788 {
789         struct expansion_card *ec = ECARD_DEV(dev);
790         struct ecard_driver *drv = ECARD_DRV(dev->driver);
791         struct ecard_request req;
792
793         if (drv->shutdown)
794                 drv->shutdown(ec);
795         ecard_release(ec);
796         req.req = req_reset;
797         req.ec = ec;
798         ecard_call(&req);
799 }
800
801 int ecard_register_driver(struct ecard_driver *drv)
802 {
803         drv->drv.bus = &ecard_bus_type;
804         drv->drv.probe = ecard_drv_probe;
805         drv->drv.remove = ecard_drv_remove;
806         drv->drv.shutdown = ecard_drv_shutdown;
807
808         return driver_register(&drv->drv);
809 }
810
811 void ecard_remove_driver(struct ecard_driver *drv)
812 {
813         driver_unregister(&drv->drv);
814 }
815
816 static int ecard_match(struct device *_dev, struct device_driver *_drv)
817 {
818         struct expansion_card *ec = ECARD_DEV(_dev);
819         struct ecard_driver *drv = ECARD_DRV(_drv);
820         int ret;
821
822         if (drv->id_table) {
823                 ret = ecard_match_device(drv->id_table, ec) != NULL;
824         } else {
825                 ret = ec->cid.id == drv->id;
826         }
827
828         return ret;
829 }
830
831 struct bus_type ecard_bus_type = {
832         .name   = "ecard",
833         .match  = ecard_match,
834 };
835
836 static int ecard_bus_init(void)
837 {
838         return bus_register(&ecard_bus_type);
839 }
840
841 postcore_initcall(ecard_bus_init);
842
843 EXPORT_SYMBOL(ecard_readchunk);
844 EXPORT_SYMBOL(ecard_address);
845 EXPORT_SYMBOL(ecard_register_driver);
846 EXPORT_SYMBOL(ecard_remove_driver);
847 EXPORT_SYMBOL(ecard_bus_type);