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