ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / pcmcia / cistpl.c
1 /*======================================================================
2
3     PCMCIA Card Information Structure parser
4
5     cistpl.c 1.99 2002/10/24 06:11:48
6
7     The contents of this file are subject to the Mozilla Public
8     License Version 1.1 (the "License"); you may not use this file
9     except in compliance with the License. You may obtain a copy of
10     the License at http://www.mozilla.org/MPL/
11
12     Software distributed under the License is distributed on an "AS
13     IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14     implied. See the License for the specific language governing
15     rights and limitations under the License.
16
17     The initial developer of the original code is David A. Hinds
18     <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
19     are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
20
21     Alternatively, the contents of this file may be used under the
22     terms of the GNU General Public License version 2 (the "GPL"), in
23     which case the provisions of the GPL are applicable instead of the
24     above.  If you wish to allow the use of your version of this file
25     only under the terms of the GPL and not to allow others to use
26     your version of this file under the MPL, indicate your decision
27     by deleting the provisions above and replace them with the notice
28     and other provisions required by the GPL.  If you do not delete
29     the provisions above, a recipient may use your version of this
30     file under either the MPL or the GPL.
31     
32 ======================================================================*/
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/kernel.h>
38 #include <linux/string.h>
39 #include <linux/major.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <linux/slab.h>
43 #include <linux/mm.h>
44 #include <linux/sched.h>
45 #include <linux/pci.h>
46 #include <linux/ioport.h>
47 #include <asm/io.h>
48 #include <asm/byteorder.h>
49
50 #include <pcmcia/cs_types.h>
51 #include <pcmcia/ss.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/bulkmem.h>
54 #include <pcmcia/cisreg.h>
55 #include <pcmcia/cistpl.h>
56 #include "cs_internal.h"
57
58 static const u_char mantissa[] = {
59     10, 12, 13, 15, 20, 25, 30, 35,
60     40, 45, 50, 55, 60, 70, 80, 90
61 };
62
63 static const u_int exponent[] = {
64     1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
65 };
66
67 /* Convert an extended speed byte to a time in nanoseconds */
68 #define SPEED_CVT(v) \
69     (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10)
70 /* Convert a power byte to a current in 0.1 microamps */
71 #define POWER_CVT(v) \
72     (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10)
73 #define POWER_SCALE(v)          (exponent[(v)&7])
74
75 /* Upper limit on reasonable # of tuples */
76 #define MAX_TUPLES              200
77
78 /*====================================================================*/
79
80 /* Parameters that can be set with 'insmod' */
81
82 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
83
84 INT_MODULE_PARM(cis_width,      0);             /* 16-bit CIS? */
85
86 void release_cis_mem(struct pcmcia_socket *s)
87 {
88     if (s->cis_mem.sys_start != 0) {
89         s->cis_mem.flags &= ~MAP_ACTIVE;
90         s->ops->set_mem_map(s, &s->cis_mem);
91         if (!(s->features & SS_CAP_STATIC_MAP))
92             release_mem_region(s->cis_mem.sys_start, s->map_size);
93         iounmap(s->cis_virt);
94         s->cis_mem.sys_start = 0;
95         s->cis_virt = NULL;
96     }
97 }
98
99 /*
100  * Map the card memory at "card_offset" into virtual space.
101  * If flags & MAP_ATTRIB, map the attribute space, otherwise
102  * map the memory space.
103  */
104 static unsigned char *
105 set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
106 {
107     pccard_mem_map *mem = &s->cis_mem;
108     if (!(s->features & SS_CAP_STATIC_MAP) &&
109         mem->sys_start == 0) {
110         validate_mem(s);
111         mem->sys_start = 0;
112         if (find_mem_region(&mem->sys_start, s->map_size,
113                             s->map_size, 0, "card services", s)) {
114             printk(KERN_NOTICE "cs: unable to map card memory!\n");
115             return NULL;
116         }
117         mem->sys_stop = mem->sys_start+s->map_size-1;
118         s->cis_virt = ioremap(mem->sys_start, s->map_size);
119     }
120     mem->card_start = card_offset;
121     mem->flags = flags;
122     s->ops->set_mem_map(s, mem);
123     if (s->features & SS_CAP_STATIC_MAP) {
124         if (s->cis_virt)
125             iounmap(s->cis_virt);
126         s->cis_virt = ioremap(mem->sys_start, s->map_size);
127     }
128     return s->cis_virt;
129 }
130
131 /*======================================================================
132
133     Low-level functions to read and write CIS memory.  I think the
134     write routine is only useful for writing one-byte registers.
135     
136 ======================================================================*/
137
138 /* Bits in attr field */
139 #define IS_ATTR         1
140 #define IS_INDIRECT     8
141
142 int read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
143                  u_int len, void *ptr)
144 {
145     u_char *sys, *end, *buf = ptr;
146     
147     cs_dbg(s, 3, "read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
148
149     if (attr & IS_INDIRECT) {
150         /* Indirect accesses use a bunch of special registers at fixed
151            locations in common memory */
152         u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
153         if (attr & IS_ATTR) {
154             addr *= 2;
155             flags = ICTRL0_AUTOINC;
156         }
157
158         sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
159         if (!sys) {
160             memset(ptr, 0xff, len);
161             return -1;
162         }
163
164         writeb(flags, sys+CISREG_ICTRL0);
165         writeb(addr & 0xff, sys+CISREG_IADDR0);
166         writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
167         writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
168         writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
169         for ( ; len > 0; len--, buf++)
170             *buf = readb(sys+CISREG_IDATA0);
171     } else {
172         u_int inc = 1, card_offset, flags;
173
174         flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
175         if (attr) {
176             flags |= MAP_ATTRIB;
177             inc++;
178             addr *= 2;
179         }
180
181         card_offset = addr & ~(s->map_size-1);
182         while (len) {
183             sys = set_cis_map(s, card_offset, flags);
184             if (!sys) {
185                 memset(ptr, 0xff, len);
186                 return -1;
187             }
188             end = sys + s->map_size;
189             sys = sys + (addr & (s->map_size-1));
190             for ( ; len > 0; len--, buf++, sys += inc) {
191                 if (sys == end)
192                     break;
193                 *buf = readb(sys);
194             }
195             card_offset += s->map_size;
196             addr = 0;
197         }
198     }
199     cs_dbg(s, 3, "  %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
200           *(u_char *)(ptr+0), *(u_char *)(ptr+1),
201           *(u_char *)(ptr+2), *(u_char *)(ptr+3));
202     return 0;
203 }
204
205 void write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
206                    u_int len, void *ptr)
207 {
208     u_char *sys, *end, *buf = ptr;
209     
210     cs_dbg(s, 3, "write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
211
212     if (attr & IS_INDIRECT) {
213         /* Indirect accesses use a bunch of special registers at fixed
214            locations in common memory */
215         u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
216         if (attr & IS_ATTR) {
217             addr *= 2;
218             flags = ICTRL0_AUTOINC;
219         }
220
221         sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
222         if (!sys)
223                 return; /* FIXME: Error */
224
225         writeb(flags, sys+CISREG_ICTRL0);
226         writeb(addr & 0xff, sys+CISREG_IADDR0);
227         writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
228         writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
229         writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
230         for ( ; len > 0; len--, buf++)
231             writeb(*buf, sys+CISREG_IDATA0);
232     } else {
233         u_int inc = 1, card_offset, flags;
234
235         flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
236         if (attr & IS_ATTR) {
237             flags |= MAP_ATTRIB;
238             inc++;
239             addr *= 2;
240         }
241
242         card_offset = addr & ~(s->map_size-1);
243         while (len) {
244             sys = set_cis_map(s, card_offset, flags);
245             if (!sys)
246                 return; /* FIXME: error */
247
248             end = sys + s->map_size;
249             sys = sys + (addr & (s->map_size-1));
250             for ( ; len > 0; len--, buf++, sys += inc) {
251                 if (sys == end)
252                     break;
253                 writeb(*buf, sys);
254             }
255             card_offset += s->map_size;
256             addr = 0;
257         }
258     }
259 }
260
261 /*======================================================================
262
263     This is a wrapper around read_cis_mem, with the same interface,
264     but which caches information, for cards whose CIS may not be
265     readable all the time.
266     
267 ======================================================================*/
268
269 static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
270                            u_int len, void *ptr)
271 {
272     struct cis_cache_entry *cis;
273     int ret;
274
275     if (s->fake_cis) {
276         if (s->fake_cis_len > addr+len)
277             memcpy(ptr, s->fake_cis+addr, len);
278         else
279             memset(ptr, 0xff, len);
280         return;
281     }
282
283     list_for_each_entry(cis, &s->cis_cache, node) {
284         if (cis->addr == addr && cis->len == len && cis->attr == attr) {
285             memcpy(ptr, cis->cache, len);
286             return;
287         }
288     }
289
290 #ifdef CONFIG_CARDBUS
291     if (s->state & SOCKET_CARDBUS)
292         ret = read_cb_mem(s, attr, addr, len, ptr);
293     else
294 #endif
295         ret = read_cis_mem(s, attr, addr, len, ptr);
296
297         if (ret == 0) {
298                 /* Copy data into the cache */
299                 cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL);
300                 if (cis) {
301                         cis->addr = addr;
302                         cis->len = len;
303                         cis->attr = attr;
304                         memcpy(cis->cache, ptr, len);
305                         list_add(&cis->node, &s->cis_cache);
306                 }
307         }
308 }
309
310 static void
311 remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
312 {
313         struct cis_cache_entry *cis;
314
315         list_for_each_entry(cis, &s->cis_cache, node)
316                 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
317                         list_del(&cis->node);
318                         kfree(cis);
319                         break;
320                 }
321 }
322
323 void destroy_cis_cache(struct pcmcia_socket *s)
324 {
325         struct list_head *l, *n;
326
327         list_for_each_safe(l, n, &s->cis_cache) {
328                 struct cis_cache_entry *cis = list_entry(l, struct cis_cache_entry, node);
329
330                 list_del(&cis->node);
331                 kfree(cis);
332         }
333 }
334
335 /*======================================================================
336
337     This verifies if the CIS of a card matches what is in the CIS
338     cache.
339     
340 ======================================================================*/
341
342 int verify_cis_cache(struct pcmcia_socket *s)
343 {
344         struct cis_cache_entry *cis;
345         char *buf;
346
347         buf = kmalloc(256, GFP_KERNEL);
348         if (buf == NULL)
349                 return -1;
350         list_for_each_entry(cis, &s->cis_cache, node) {
351                 int len = cis->len;
352
353                 if (len > 256)
354                         len = 256;
355 #ifdef CONFIG_CARDBUS
356                 if (s->state & SOCKET_CARDBUS)
357                         read_cb_mem(s, cis->attr, cis->addr, len, buf);
358                 else
359 #endif
360                         read_cis_mem(s, cis->attr, cis->addr, len, buf);
361
362                 if (memcmp(buf, cis->cache, len) != 0) {
363                         kfree(buf);
364                         return -1;
365                 }
366         }
367         kfree(buf);
368         return 0;
369 }
370
371 /*======================================================================
372
373     For really bad cards, we provide a facility for uploading a
374     replacement CIS.
375     
376 ======================================================================*/
377
378 int pcmcia_replace_cis(client_handle_t handle, cisdump_t *cis)
379 {
380     struct pcmcia_socket *s;
381     if (CHECK_HANDLE(handle))
382         return CS_BAD_HANDLE;
383     s = SOCKET(handle);
384     if (s->fake_cis != NULL) {
385         kfree(s->fake_cis);
386         s->fake_cis = NULL;
387     }
388     if (cis->Length > CISTPL_MAX_CIS_SIZE)
389         return CS_BAD_SIZE;
390     s->fake_cis = kmalloc(cis->Length, GFP_KERNEL);
391     if (s->fake_cis == NULL)
392         return CS_OUT_OF_RESOURCE;
393     s->fake_cis_len = cis->Length;
394     memcpy(s->fake_cis, cis->Data, cis->Length);
395     return CS_SUCCESS;
396 }
397
398 /*======================================================================
399
400     The high-level CIS tuple services
401     
402 ======================================================================*/
403
404 typedef struct tuple_flags {
405     u_int               link_space:4;
406     u_int               has_link:1;
407     u_int               mfc_fn:3;
408     u_int               space:4;
409 } tuple_flags;
410
411 #define LINK_SPACE(f)   (((tuple_flags *)(&(f)))->link_space)
412 #define HAS_LINK(f)     (((tuple_flags *)(&(f)))->has_link)
413 #define MFC_FN(f)       (((tuple_flags *)(&(f)))->mfc_fn)
414 #define SPACE(f)        (((tuple_flags *)(&(f)))->space)
415
416 int pcmcia_get_next_tuple(client_handle_t handle, tuple_t *tuple);
417
418 int pcmcia_get_first_tuple(client_handle_t handle, tuple_t *tuple)
419 {
420     struct pcmcia_socket *s;
421     if (CHECK_HANDLE(handle))
422         return CS_BAD_HANDLE;
423     s = SOCKET(handle);
424     if (!(s->state & SOCKET_PRESENT))
425         return CS_NO_CARD;
426     tuple->TupleLink = tuple->Flags = 0;
427 #ifdef CONFIG_CARDBUS
428     if (s->state & SOCKET_CARDBUS) {
429         struct pci_dev *dev = s->cb_dev;
430         u_int ptr;
431         pci_bus_read_config_dword(dev->subordinate, 0, PCI_CARDBUS_CIS, &ptr);
432         tuple->CISOffset = ptr & ~7;
433         SPACE(tuple->Flags) = (ptr & 7);
434     } else
435 #endif
436     {
437         /* Assume presence of a LONGLINK_C to address 0 */
438         tuple->CISOffset = tuple->LinkOffset = 0;
439         SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1;
440     }
441     if (!(s->state & SOCKET_CARDBUS) && (s->functions > 1) &&
442         !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
443         cisdata_t req = tuple->DesiredTuple;
444         tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
445         if (pcmcia_get_next_tuple(handle, tuple) == CS_SUCCESS) {
446             tuple->DesiredTuple = CISTPL_LINKTARGET;
447             if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
448                 return CS_NO_MORE_ITEMS;
449         } else
450             tuple->CISOffset = tuple->TupleLink = 0;
451         tuple->DesiredTuple = req;
452     }
453     return pcmcia_get_next_tuple(handle, tuple);
454 }
455
456 static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
457 {
458     u_char link[5];
459     u_int ofs;
460
461     if (MFC_FN(tuple->Flags)) {
462         /* Get indirect link from the MFC tuple */
463         read_cis_cache(s, LINK_SPACE(tuple->Flags),
464                        tuple->LinkOffset, 5, link);
465         ofs = le32_to_cpu(*(u_int *)(link+1));
466         SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
467         /* Move to the next indirect link */
468         tuple->LinkOffset += 5;
469         MFC_FN(tuple->Flags)--;
470     } else if (HAS_LINK(tuple->Flags)) {
471         ofs = tuple->LinkOffset;
472         SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags);
473         HAS_LINK(tuple->Flags) = 0;
474     } else {
475         return -1;
476     }
477     if (!(s->state & SOCKET_CARDBUS) && SPACE(tuple->Flags)) {
478         /* This is ugly, but a common CIS error is to code the long
479            link offset incorrectly, so we check the right spot... */
480         read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
481         if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
482             (strncmp(link+2, "CIS", 3) == 0))
483             return ofs;
484         remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
485         /* Then, we try the wrong spot... */
486         ofs = ofs >> 1;
487     }
488     read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
489     if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
490         (strncmp(link+2, "CIS", 3) == 0))
491         return ofs;
492     remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
493     return -1;
494 }
495
496 int pcmcia_get_next_tuple(client_handle_t handle, tuple_t *tuple)
497 {
498     struct pcmcia_socket *s;
499     u_char link[2], tmp;
500     int ofs, i, attr;
501     
502     if (CHECK_HANDLE(handle))
503         return CS_BAD_HANDLE;
504     s = SOCKET(handle);
505     if (!(s->state & SOCKET_PRESENT))
506         return CS_NO_CARD;
507
508     link[1] = tuple->TupleLink;
509     ofs = tuple->CISOffset + tuple->TupleLink;
510     attr = SPACE(tuple->Flags);
511
512     for (i = 0; i < MAX_TUPLES; i++) {
513         if (link[1] == 0xff) {
514             link[0] = CISTPL_END;
515         } else {
516             read_cis_cache(s, attr, ofs, 2, link);
517             if (link[0] == CISTPL_NULL) {
518                 ofs++; continue;
519             }
520         }
521         
522         /* End of chain?  Follow long link if possible */
523         if (link[0] == CISTPL_END) {
524             if ((ofs = follow_link(s, tuple)) < 0)
525                 return CS_NO_MORE_ITEMS;
526             attr = SPACE(tuple->Flags);
527             read_cis_cache(s, attr, ofs, 2, link);
528         }
529
530         /* Is this a link tuple?  Make a note of it */
531         if ((link[0] == CISTPL_LONGLINK_A) ||
532             (link[0] == CISTPL_LONGLINK_C) ||
533             (link[0] == CISTPL_LONGLINK_MFC) ||
534             (link[0] == CISTPL_LINKTARGET) ||
535             (link[0] == CISTPL_INDIRECT) ||
536             (link[0] == CISTPL_NO_LINK)) {
537             switch (link[0]) {
538             case CISTPL_LONGLINK_A:
539                 HAS_LINK(tuple->Flags) = 1;
540                 LINK_SPACE(tuple->Flags) = attr | IS_ATTR;
541                 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
542                 break;
543             case CISTPL_LONGLINK_C:
544                 HAS_LINK(tuple->Flags) = 1;
545                 LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR;
546                 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
547                 break;
548             case CISTPL_INDIRECT:
549                 HAS_LINK(tuple->Flags) = 1;
550                 LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT;
551                 tuple->LinkOffset = 0;
552                 break;
553             case CISTPL_LONGLINK_MFC:
554                 tuple->LinkOffset = ofs + 3;
555                 LINK_SPACE(tuple->Flags) = attr;
556                 if (handle->Function == BIND_FN_ALL) {
557                     /* Follow all the MFC links */
558                     read_cis_cache(s, attr, ofs+2, 1, &tmp);
559                     MFC_FN(tuple->Flags) = tmp;
560                 } else {
561                     /* Follow exactly one of the links */
562                     MFC_FN(tuple->Flags) = 1;
563                     tuple->LinkOffset += handle->Function * 5;
564                 }
565                 break;
566             case CISTPL_NO_LINK:
567                 HAS_LINK(tuple->Flags) = 0;
568                 break;
569             }
570             if ((tuple->Attributes & TUPLE_RETURN_LINK) &&
571                 (tuple->DesiredTuple == RETURN_FIRST_TUPLE))
572                 break;
573         } else
574             if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
575                 break;
576         
577         if (link[0] == tuple->DesiredTuple)
578             break;
579         ofs += link[1] + 2;
580     }
581     if (i == MAX_TUPLES) {
582         cs_dbg(s, 1, "cs: overrun in pcmcia_get_next_tuple\n");
583         return CS_NO_MORE_ITEMS;
584     }
585     
586     tuple->TupleCode = link[0];
587     tuple->TupleLink = link[1];
588     tuple->CISOffset = ofs + 2;
589     return CS_SUCCESS;
590 }
591
592 /*====================================================================*/
593
594 #define _MIN(a, b)              (((a) < (b)) ? (a) : (b))
595
596 int pcmcia_get_tuple_data(client_handle_t handle, tuple_t *tuple)
597 {
598     struct pcmcia_socket *s;
599     u_int len;
600     
601     if (CHECK_HANDLE(handle))
602         return CS_BAD_HANDLE;
603
604     s = SOCKET(handle);
605
606     if (tuple->TupleLink < tuple->TupleOffset)
607         return CS_NO_MORE_ITEMS;
608     len = tuple->TupleLink - tuple->TupleOffset;
609     tuple->TupleDataLen = tuple->TupleLink;
610     if (len == 0)
611         return CS_SUCCESS;
612     read_cis_cache(s, SPACE(tuple->Flags),
613                    tuple->CISOffset + tuple->TupleOffset,
614                    _MIN(len, tuple->TupleDataMax), tuple->TupleData);
615     return CS_SUCCESS;
616 }
617
618 /*======================================================================
619
620     Parsing routines for individual tuples
621     
622 ======================================================================*/
623
624 static int parse_device(tuple_t *tuple, cistpl_device_t *device)
625 {
626     int i;
627     u_char scale;
628     u_char *p, *q;
629
630     p = (u_char *)tuple->TupleData;
631     q = p + tuple->TupleDataLen;
632
633     device->ndev = 0;
634     for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
635         
636         if (*p == 0xff) break;
637         device->dev[i].type = (*p >> 4);
638         device->dev[i].wp = (*p & 0x08) ? 1 : 0;
639         switch (*p & 0x07) {
640         case 0: device->dev[i].speed = 0;   break;
641         case 1: device->dev[i].speed = 250; break;
642         case 2: device->dev[i].speed = 200; break;
643         case 3: device->dev[i].speed = 150; break;
644         case 4: device->dev[i].speed = 100; break;
645         case 7:
646             if (++p == q) return CS_BAD_TUPLE;
647             device->dev[i].speed = SPEED_CVT(*p);
648             while (*p & 0x80)
649                 if (++p == q) return CS_BAD_TUPLE;
650             break;
651         default:
652             return CS_BAD_TUPLE;
653         }
654
655         if (++p == q) return CS_BAD_TUPLE;
656         if (*p == 0xff) break;
657         scale = *p & 7;
658         if (scale == 7) return CS_BAD_TUPLE;
659         device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
660         device->ndev++;
661         if (++p == q) break;
662     }
663     
664     return CS_SUCCESS;
665 }
666
667 /*====================================================================*/
668
669 static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
670 {
671     u_char *p;
672     if (tuple->TupleDataLen < 5)
673         return CS_BAD_TUPLE;
674     p = (u_char *)tuple->TupleData;
675     csum->addr = tuple->CISOffset+(short)le16_to_cpu(*(u_short *)p)-2;
676     csum->len = le16_to_cpu(*(u_short *)(p + 2));
677     csum->sum = *(p+4);
678     return CS_SUCCESS;
679 }
680
681 /*====================================================================*/
682
683 static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
684 {
685     if (tuple->TupleDataLen < 4)
686         return CS_BAD_TUPLE;
687     link->addr = le32_to_cpu(*(u_int *)tuple->TupleData);
688     return CS_SUCCESS;
689 }
690
691 /*====================================================================*/
692
693 static int parse_longlink_mfc(tuple_t *tuple,
694                               cistpl_longlink_mfc_t *link)
695 {
696     u_char *p;
697     int i;
698     
699     p = (u_char *)tuple->TupleData;
700     
701     link->nfn = *p; p++;
702     if (tuple->TupleDataLen <= link->nfn*5)
703         return CS_BAD_TUPLE;
704     for (i = 0; i < link->nfn; i++) {
705         link->fn[i].space = *p; p++;
706         link->fn[i].addr = le32_to_cpu(*(u_int *)p); p += 4;
707     }
708     return CS_SUCCESS;
709 }
710
711 /*====================================================================*/
712
713 static int parse_strings(u_char *p, u_char *q, int max,
714                          char *s, u_char *ofs, u_char *found)
715 {
716     int i, j, ns;
717
718     if (p == q) return CS_BAD_TUPLE;
719     ns = 0; j = 0;
720     for (i = 0; i < max; i++) {
721         if (*p == 0xff) break;
722         ofs[i] = j;
723         ns++;
724         for (;;) {
725             s[j++] = (*p == 0xff) ? '\0' : *p;
726             if ((*p == '\0') || (*p == 0xff)) break;
727             if (++p == q) return CS_BAD_TUPLE;
728         }
729         if ((*p == 0xff) || (++p == q)) break;
730     }
731     if (found) {
732         *found = ns;
733         return CS_SUCCESS;
734     } else {
735         return (ns == max) ? CS_SUCCESS : CS_BAD_TUPLE;
736     }
737 }
738
739 /*====================================================================*/
740
741 static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
742 {
743     u_char *p, *q;
744     
745     p = (u_char *)tuple->TupleData;
746     q = p + tuple->TupleDataLen;
747     
748     vers_1->major = *p; p++;
749     vers_1->minor = *p; p++;
750     if (p >= q) return CS_BAD_TUPLE;
751
752     return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
753                          vers_1->str, vers_1->ofs, &vers_1->ns);
754 }
755
756 /*====================================================================*/
757
758 static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
759 {
760     u_char *p, *q;
761     
762     p = (u_char *)tuple->TupleData;
763     q = p + tuple->TupleDataLen;
764     
765     return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
766                          altstr->str, altstr->ofs, &altstr->ns);
767 }
768
769 /*====================================================================*/
770
771 static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
772 {
773     u_char *p, *q;
774     int nid;
775
776     p = (u_char *)tuple->TupleData;
777     q = p + tuple->TupleDataLen;
778
779     for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
780         if (p > q-2) break;
781         jedec->id[nid].mfr = p[0];
782         jedec->id[nid].info = p[1];
783         p += 2;
784     }
785     jedec->nid = nid;
786     return CS_SUCCESS;
787 }
788
789 /*====================================================================*/
790
791 static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
792 {
793     u_short *p;
794     if (tuple->TupleDataLen < 4)
795         return CS_BAD_TUPLE;
796     p = (u_short *)tuple->TupleData;
797     m->manf = le16_to_cpu(p[0]);
798     m->card = le16_to_cpu(p[1]);
799     return CS_SUCCESS;
800 }
801
802 /*====================================================================*/
803
804 static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
805 {
806     u_char *p;
807     if (tuple->TupleDataLen < 2)
808         return CS_BAD_TUPLE;
809     p = (u_char *)tuple->TupleData;
810     f->func = p[0];
811     f->sysinit = p[1];
812     return CS_SUCCESS;
813 }
814
815 /*====================================================================*/
816
817 static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
818 {
819     u_char *p;
820     int i;
821     if (tuple->TupleDataLen < 1)
822         return CS_BAD_TUPLE;
823     p = (u_char *)tuple->TupleData;
824     f->type = p[0];
825     for (i = 1; i < tuple->TupleDataLen; i++)
826         f->data[i-1] = p[i];
827     return CS_SUCCESS;
828 }
829
830 /*====================================================================*/
831
832 static int parse_config(tuple_t *tuple, cistpl_config_t *config)
833 {
834     int rasz, rmsz, i;
835     u_char *p;
836
837     p = (u_char *)tuple->TupleData;
838     rasz = *p & 0x03;
839     rmsz = (*p & 0x3c) >> 2;
840     if (tuple->TupleDataLen < rasz+rmsz+4)
841         return CS_BAD_TUPLE;
842     config->last_idx = *(++p);
843     p++;
844     config->base = 0;
845     for (i = 0; i <= rasz; i++)
846         config->base += p[i] << (8*i);
847     p += rasz+1;
848     for (i = 0; i < 4; i++)
849         config->rmask[i] = 0;
850     for (i = 0; i <= rmsz; i++)
851         config->rmask[i>>2] += p[i] << (8*(i%4));
852     config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
853     return CS_SUCCESS;
854 }
855
856 /*======================================================================
857
858     The following routines are all used to parse the nightmarish
859     config table entries.
860     
861 ======================================================================*/
862
863 static u_char *parse_power(u_char *p, u_char *q,
864                            cistpl_power_t *pwr)
865 {
866     int i;
867     u_int scale;
868
869     if (p == q) return NULL;
870     pwr->present = *p;
871     pwr->flags = 0;
872     p++;
873     for (i = 0; i < 7; i++)
874         if (pwr->present & (1<<i)) {
875             if (p == q) return NULL;
876             pwr->param[i] = POWER_CVT(*p);
877             scale = POWER_SCALE(*p);
878             while (*p & 0x80) {
879                 if (++p == q) return NULL;
880                 if ((*p & 0x7f) < 100)
881                     pwr->param[i] += (*p & 0x7f) * scale / 100;
882                 else if (*p == 0x7d)
883                     pwr->flags |= CISTPL_POWER_HIGHZ_OK;
884                 else if (*p == 0x7e)
885                     pwr->param[i] = 0;
886                 else if (*p == 0x7f)
887                     pwr->flags |= CISTPL_POWER_HIGHZ_REQ;
888                 else
889                     return NULL;
890             }
891             p++;
892         }
893     return p;
894 }
895
896 /*====================================================================*/
897
898 static u_char *parse_timing(u_char *p, u_char *q,
899                             cistpl_timing_t *timing)
900 {
901     u_char scale;
902
903     if (p == q) return NULL;
904     scale = *p;
905     if ((scale & 3) != 3) {
906         if (++p == q) return NULL;
907         timing->wait = SPEED_CVT(*p);
908         timing->waitscale = exponent[scale & 3];
909     } else
910         timing->wait = 0;
911     scale >>= 2;
912     if ((scale & 7) != 7) {
913         if (++p == q) return NULL;
914         timing->ready = SPEED_CVT(*p);
915         timing->rdyscale = exponent[scale & 7];
916     } else
917         timing->ready = 0;
918     scale >>= 3;
919     if (scale != 7) {
920         if (++p == q) return NULL;
921         timing->reserved = SPEED_CVT(*p);
922         timing->rsvscale = exponent[scale];
923     } else
924         timing->reserved = 0;
925     p++;
926     return p;
927 }
928
929 /*====================================================================*/
930
931 static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
932 {
933     int i, j, bsz, lsz;
934
935     if (p == q) return NULL;
936     io->flags = *p;
937
938     if (!(*p & 0x80)) {
939         io->nwin = 1;
940         io->win[0].base = 0;
941         io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
942         return p+1;
943     }
944     
945     if (++p == q) return NULL;
946     io->nwin = (*p & 0x0f) + 1;
947     bsz = (*p & 0x30) >> 4;
948     if (bsz == 3) bsz++;
949     lsz = (*p & 0xc0) >> 6;
950     if (lsz == 3) lsz++;
951     p++;
952     
953     for (i = 0; i < io->nwin; i++) {
954         io->win[i].base = 0;
955         io->win[i].len = 1;
956         for (j = 0; j < bsz; j++, p++) {
957             if (p == q) return NULL;
958             io->win[i].base += *p << (j*8);
959         }
960         for (j = 0; j < lsz; j++, p++) {
961             if (p == q) return NULL;
962             io->win[i].len += *p << (j*8);
963         }
964     }
965     return p;
966 }
967
968 /*====================================================================*/
969
970 static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
971 {
972     int i, j, asz, lsz, has_ha;
973     u_int len, ca, ha;
974
975     if (p == q) return NULL;
976
977     mem->nwin = (*p & 0x07) + 1;
978     lsz = (*p & 0x18) >> 3;
979     asz = (*p & 0x60) >> 5;
980     has_ha = (*p & 0x80);
981     if (++p == q) return NULL;
982     
983     for (i = 0; i < mem->nwin; i++) {
984         len = ca = ha = 0;
985         for (j = 0; j < lsz; j++, p++) {
986             if (p == q) return NULL;
987             len += *p << (j*8);
988         }
989         for (j = 0; j < asz; j++, p++) {
990             if (p == q) return NULL;
991             ca += *p << (j*8);
992         }
993         if (has_ha)
994             for (j = 0; j < asz; j++, p++) {
995                 if (p == q) return NULL;
996                 ha += *p << (j*8);
997             }
998         mem->win[i].len = len << 8;
999         mem->win[i].card_addr = ca << 8;
1000         mem->win[i].host_addr = ha << 8;
1001     }
1002     return p;
1003 }
1004
1005 /*====================================================================*/
1006
1007 static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
1008 {
1009     if (p == q) return NULL;
1010     irq->IRQInfo1 = *p; p++;
1011     if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
1012         if (p+2 > q) return NULL;
1013         irq->IRQInfo2 = (p[1]<<8) + p[0];
1014         p += 2;
1015     }
1016     return p;
1017 }
1018
1019 /*====================================================================*/
1020
1021 static int parse_cftable_entry(tuple_t *tuple,
1022                                cistpl_cftable_entry_t *entry)
1023 {
1024     u_char *p, *q, features;
1025
1026     p = tuple->TupleData;
1027     q = p + tuple->TupleDataLen;
1028     entry->index = *p & 0x3f;
1029     entry->flags = 0;
1030     if (*p & 0x40)
1031         entry->flags |= CISTPL_CFTABLE_DEFAULT;
1032     if (*p & 0x80) {
1033         if (++p == q) return CS_BAD_TUPLE;
1034         if (*p & 0x10)
1035             entry->flags |= CISTPL_CFTABLE_BVDS;
1036         if (*p & 0x20)
1037             entry->flags |= CISTPL_CFTABLE_WP;
1038         if (*p & 0x40)
1039             entry->flags |= CISTPL_CFTABLE_RDYBSY;
1040         if (*p & 0x80)
1041             entry->flags |= CISTPL_CFTABLE_MWAIT;
1042         entry->interface = *p & 0x0f;
1043     } else
1044         entry->interface = 0;
1045
1046     /* Process optional features */
1047     if (++p == q) return CS_BAD_TUPLE;
1048     features = *p; p++;
1049
1050     /* Power options */
1051     if ((features & 3) > 0) {
1052         p = parse_power(p, q, &entry->vcc);
1053         if (p == NULL) return CS_BAD_TUPLE;
1054     } else
1055         entry->vcc.present = 0;
1056     if ((features & 3) > 1) {
1057         p = parse_power(p, q, &entry->vpp1);
1058         if (p == NULL) return CS_BAD_TUPLE;
1059     } else
1060         entry->vpp1.present = 0;
1061     if ((features & 3) > 2) {
1062         p = parse_power(p, q, &entry->vpp2);
1063         if (p == NULL) return CS_BAD_TUPLE;
1064     } else
1065         entry->vpp2.present = 0;
1066
1067     /* Timing options */
1068     if (features & 0x04) {
1069         p = parse_timing(p, q, &entry->timing);
1070         if (p == NULL) return CS_BAD_TUPLE;
1071     } else {
1072         entry->timing.wait = 0;
1073         entry->timing.ready = 0;
1074         entry->timing.reserved = 0;
1075     }
1076     
1077     /* I/O window options */
1078     if (features & 0x08) {
1079         p = parse_io(p, q, &entry->io);
1080         if (p == NULL) return CS_BAD_TUPLE;
1081     } else
1082         entry->io.nwin = 0;
1083     
1084     /* Interrupt options */
1085     if (features & 0x10) {
1086         p = parse_irq(p, q, &entry->irq);
1087         if (p == NULL) return CS_BAD_TUPLE;
1088     } else
1089         entry->irq.IRQInfo1 = 0;
1090
1091     switch (features & 0x60) {
1092     case 0x00:
1093         entry->mem.nwin = 0;
1094         break;
1095     case 0x20:
1096         entry->mem.nwin = 1;
1097         entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
1098         entry->mem.win[0].card_addr = 0;
1099         entry->mem.win[0].host_addr = 0;
1100         p += 2;
1101         if (p > q) return CS_BAD_TUPLE;
1102         break;
1103     case 0x40:
1104         entry->mem.nwin = 1;
1105         entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
1106         entry->mem.win[0].card_addr =
1107             le16_to_cpu(*(u_short *)(p+2)) << 8;
1108         entry->mem.win[0].host_addr = 0;
1109         p += 4;
1110         if (p > q) return CS_BAD_TUPLE;
1111         break;
1112     case 0x60:
1113         p = parse_mem(p, q, &entry->mem);
1114         if (p == NULL) return CS_BAD_TUPLE;
1115         break;
1116     }
1117
1118     /* Misc features */
1119     if (features & 0x80) {
1120         if (p == q) return CS_BAD_TUPLE;
1121         entry->flags |= (*p << 8);
1122         while (*p & 0x80)
1123             if (++p == q) return CS_BAD_TUPLE;
1124         p++;
1125     }
1126
1127     entry->subtuples = q-p;
1128     
1129     return CS_SUCCESS;
1130 }
1131
1132 /*====================================================================*/
1133
1134 #ifdef CONFIG_CARDBUS
1135
1136 static int parse_bar(tuple_t *tuple, cistpl_bar_t *bar)
1137 {
1138     u_char *p;
1139     if (tuple->TupleDataLen < 6)
1140         return CS_BAD_TUPLE;
1141     p = (u_char *)tuple->TupleData;
1142     bar->attr = *p;
1143     p += 2;
1144     bar->size = le32_to_cpu(*(u_int *)p);
1145     return CS_SUCCESS;
1146 }
1147
1148 static int parse_config_cb(tuple_t *tuple, cistpl_config_t *config)
1149 {
1150     u_char *p;
1151     
1152     p = (u_char *)tuple->TupleData;
1153     if ((*p != 3) || (tuple->TupleDataLen < 6))
1154         return CS_BAD_TUPLE;
1155     config->last_idx = *(++p);
1156     p++;
1157     config->base = le32_to_cpu(*(u_int *)p);
1158     config->subtuples = tuple->TupleDataLen - 6;
1159     return CS_SUCCESS;
1160 }
1161
1162 static int parse_cftable_entry_cb(tuple_t *tuple,
1163                                   cistpl_cftable_entry_cb_t *entry)
1164 {
1165     u_char *p, *q, features;
1166
1167     p = tuple->TupleData;
1168     q = p + tuple->TupleDataLen;
1169     entry->index = *p & 0x3f;
1170     entry->flags = 0;
1171     if (*p & 0x40)
1172         entry->flags |= CISTPL_CFTABLE_DEFAULT;
1173
1174     /* Process optional features */
1175     if (++p == q) return CS_BAD_TUPLE;
1176     features = *p; p++;
1177
1178     /* Power options */
1179     if ((features & 3) > 0) {
1180         p = parse_power(p, q, &entry->vcc);
1181         if (p == NULL) return CS_BAD_TUPLE;
1182     } else
1183         entry->vcc.present = 0;
1184     if ((features & 3) > 1) {
1185         p = parse_power(p, q, &entry->vpp1);
1186         if (p == NULL) return CS_BAD_TUPLE;
1187     } else
1188         entry->vpp1.present = 0;
1189     if ((features & 3) > 2) {
1190         p = parse_power(p, q, &entry->vpp2);
1191         if (p == NULL) return CS_BAD_TUPLE;
1192     } else
1193         entry->vpp2.present = 0;
1194
1195     /* I/O window options */
1196     if (features & 0x08) {
1197         if (p == q) return CS_BAD_TUPLE;
1198         entry->io = *p; p++;
1199     } else
1200         entry->io = 0;
1201     
1202     /* Interrupt options */
1203     if (features & 0x10) {
1204         p = parse_irq(p, q, &entry->irq);
1205         if (p == NULL) return CS_BAD_TUPLE;
1206     } else
1207         entry->irq.IRQInfo1 = 0;
1208
1209     if (features & 0x20) {
1210         if (p == q) return CS_BAD_TUPLE;
1211         entry->mem = *p; p++;
1212     } else
1213         entry->mem = 0;
1214
1215     /* Misc features */
1216     if (features & 0x80) {
1217         if (p == q) return CS_BAD_TUPLE;
1218         entry->flags |= (*p << 8);
1219         if (*p & 0x80) {
1220             if (++p == q) return CS_BAD_TUPLE;
1221             entry->flags |= (*p << 16);
1222         }
1223         while (*p & 0x80)
1224             if (++p == q) return CS_BAD_TUPLE;
1225         p++;
1226     }
1227
1228     entry->subtuples = q-p;
1229     
1230     return CS_SUCCESS;
1231 }
1232
1233 #endif
1234
1235 /*====================================================================*/
1236
1237 static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
1238 {
1239     u_char *p, *q;
1240     int n;
1241
1242     p = (u_char *)tuple->TupleData;
1243     q = p + tuple->TupleDataLen;
1244
1245     for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
1246         if (p > q-6) break;
1247         geo->geo[n].buswidth = p[0];
1248         geo->geo[n].erase_block = 1 << (p[1]-1);
1249         geo->geo[n].read_block  = 1 << (p[2]-1);
1250         geo->geo[n].write_block = 1 << (p[3]-1);
1251         geo->geo[n].partition   = 1 << (p[4]-1);
1252         geo->geo[n].interleave  = 1 << (p[5]-1);
1253         p += 6;
1254     }
1255     geo->ngeo = n;
1256     return CS_SUCCESS;
1257 }
1258
1259 /*====================================================================*/
1260
1261 static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
1262 {
1263     u_char *p, *q;
1264
1265     if (tuple->TupleDataLen < 10)
1266         return CS_BAD_TUPLE;
1267     
1268     p = tuple->TupleData;
1269     q = p + tuple->TupleDataLen;
1270
1271     v2->vers = p[0];
1272     v2->comply = p[1];
1273     v2->dindex = le16_to_cpu(*(u_short *)(p+2));
1274     v2->vspec8 = p[6];
1275     v2->vspec9 = p[7];
1276     v2->nhdr = p[8];
1277     p += 9;
1278     return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL);
1279 }
1280
1281 /*====================================================================*/
1282
1283 static int parse_org(tuple_t *tuple, cistpl_org_t *org)
1284 {
1285     u_char *p, *q;
1286     int i;
1287     
1288     p = tuple->TupleData;
1289     q = p + tuple->TupleDataLen;
1290     if (p == q) return CS_BAD_TUPLE;
1291     org->data_org = *p;
1292     if (++p == q) return CS_BAD_TUPLE;
1293     for (i = 0; i < 30; i++) {
1294         org->desc[i] = *p;
1295         if (*p == '\0') break;
1296         if (++p == q) return CS_BAD_TUPLE;
1297     }
1298     return CS_SUCCESS;
1299 }
1300
1301 /*====================================================================*/
1302
1303 static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
1304 {
1305     u_char *p;
1306
1307     if (tuple->TupleDataLen < 10)
1308         return CS_BAD_TUPLE;
1309
1310     p = tuple->TupleData;
1311
1312     fmt->type = p[0];
1313     fmt->edc = p[1];
1314     fmt->offset = le32_to_cpu(*(u_int *)(p+2));
1315     fmt->length = le32_to_cpu(*(u_int *)(p+6));
1316
1317     return CS_SUCCESS;
1318 }
1319
1320 /*====================================================================*/
1321
1322 int pcmcia_parse_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
1323 {
1324     int ret = CS_SUCCESS;
1325     
1326     if (tuple->TupleDataLen > tuple->TupleDataMax)
1327         return CS_BAD_TUPLE;
1328     switch (tuple->TupleCode) {
1329     case CISTPL_DEVICE:
1330     case CISTPL_DEVICE_A:
1331         ret = parse_device(tuple, &parse->device);
1332         break;
1333 #ifdef CONFIG_CARDBUS
1334     case CISTPL_BAR:
1335         ret = parse_bar(tuple, &parse->bar);
1336         break;
1337     case CISTPL_CONFIG_CB:
1338         ret = parse_config_cb(tuple, &parse->config);
1339         break;
1340     case CISTPL_CFTABLE_ENTRY_CB:
1341         ret = parse_cftable_entry_cb(tuple, &parse->cftable_entry_cb);
1342         break;
1343 #endif
1344     case CISTPL_CHECKSUM:
1345         ret = parse_checksum(tuple, &parse->checksum);
1346         break;
1347     case CISTPL_LONGLINK_A:
1348     case CISTPL_LONGLINK_C:
1349         ret = parse_longlink(tuple, &parse->longlink);
1350         break;
1351     case CISTPL_LONGLINK_MFC:
1352         ret = parse_longlink_mfc(tuple, &parse->longlink_mfc);
1353         break;
1354     case CISTPL_VERS_1:
1355         ret = parse_vers_1(tuple, &parse->version_1);
1356         break;
1357     case CISTPL_ALTSTR:
1358         ret = parse_altstr(tuple, &parse->altstr);
1359         break;
1360     case CISTPL_JEDEC_A:
1361     case CISTPL_JEDEC_C:
1362         ret = parse_jedec(tuple, &parse->jedec);
1363         break;
1364     case CISTPL_MANFID:
1365         ret = parse_manfid(tuple, &parse->manfid);
1366         break;
1367     case CISTPL_FUNCID:
1368         ret = parse_funcid(tuple, &parse->funcid);
1369         break;
1370     case CISTPL_FUNCE:
1371         ret = parse_funce(tuple, &parse->funce);
1372         break;
1373     case CISTPL_CONFIG:
1374         ret = parse_config(tuple, &parse->config);
1375         break;
1376     case CISTPL_CFTABLE_ENTRY:
1377         ret = parse_cftable_entry(tuple, &parse->cftable_entry);
1378         break;
1379     case CISTPL_DEVICE_GEO:
1380     case CISTPL_DEVICE_GEO_A:
1381         ret = parse_device_geo(tuple, &parse->device_geo);
1382         break;
1383     case CISTPL_VERS_2:
1384         ret = parse_vers_2(tuple, &parse->vers_2);
1385         break;
1386     case CISTPL_ORG:
1387         ret = parse_org(tuple, &parse->org);
1388         break;
1389     case CISTPL_FORMAT:
1390     case CISTPL_FORMAT_A:
1391         ret = parse_format(tuple, &parse->format);
1392         break;
1393     case CISTPL_NO_LINK:
1394     case CISTPL_LINKTARGET:
1395         ret = CS_SUCCESS;
1396         break;
1397     default:
1398         ret = CS_UNSUPPORTED_FUNCTION;
1399         break;
1400     }
1401     return ret;
1402 }
1403
1404 /*======================================================================
1405
1406     This is used internally by Card Services to look up CIS stuff.
1407     
1408 ======================================================================*/
1409
1410 int read_tuple(client_handle_t handle, cisdata_t code, void *parse)
1411 {
1412     tuple_t tuple;
1413     cisdata_t *buf;
1414     int ret;
1415
1416     buf = kmalloc(256, GFP_KERNEL);
1417     if (buf == NULL)
1418         return CS_OUT_OF_RESOURCE;
1419     tuple.DesiredTuple = code;
1420     tuple.Attributes = TUPLE_RETURN_COMMON;
1421     ret = pcmcia_get_first_tuple(handle, &tuple);
1422     if (ret != CS_SUCCESS) goto done;
1423     tuple.TupleData = buf;
1424     tuple.TupleOffset = 0;
1425     tuple.TupleDataMax = 255;
1426     ret = pcmcia_get_tuple_data(handle, &tuple);
1427     if (ret != CS_SUCCESS) goto done;
1428     ret = pcmcia_parse_tuple(handle, &tuple, parse);
1429 done:
1430     kfree(buf);
1431     return ret;
1432 }
1433
1434 /*======================================================================
1435
1436     This tries to determine if a card has a sensible CIS.  It returns
1437     the number of tuples in the CIS, or 0 if the CIS looks bad.  The
1438     checks include making sure several critical tuples are present and
1439     valid; seeing if the total number of tuples is reasonable; and
1440     looking for tuples that use reserved codes.
1441     
1442 ======================================================================*/
1443
1444 int pcmcia_validate_cis(client_handle_t handle, cisinfo_t *info)
1445 {
1446     tuple_t *tuple;
1447     cisparse_t *p;
1448     int ret, reserved, dev_ok = 0, ident_ok = 0;
1449
1450     if (CHECK_HANDLE(handle))
1451         return CS_BAD_HANDLE;
1452     tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
1453     if (tuple == NULL)
1454         return CS_OUT_OF_RESOURCE;
1455     p = kmalloc(sizeof(*p), GFP_KERNEL);
1456     if (p == NULL) {
1457         kfree(tuple);
1458         return CS_OUT_OF_RESOURCE;
1459     }
1460
1461     info->Chains = reserved = 0;
1462     tuple->DesiredTuple = RETURN_FIRST_TUPLE;
1463     tuple->Attributes = TUPLE_RETURN_COMMON;
1464     ret = pcmcia_get_first_tuple(handle, tuple);
1465     if (ret != CS_SUCCESS)
1466         goto done;
1467
1468     /* First tuple should be DEVICE; we should really have either that
1469        or a CFTABLE_ENTRY of some sort */
1470     if ((tuple->TupleCode == CISTPL_DEVICE) ||
1471         (read_tuple(handle, CISTPL_CFTABLE_ENTRY, p) == CS_SUCCESS) ||
1472         (read_tuple(handle, CISTPL_CFTABLE_ENTRY_CB, p) == CS_SUCCESS))
1473         dev_ok++;
1474
1475     /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
1476        tuple, for card identification.  Certain old D-Link and Linksys
1477        cards have only a broken VERS_2 tuple; hence the bogus test. */
1478     if ((read_tuple(handle, CISTPL_MANFID, p) == CS_SUCCESS) ||
1479         (read_tuple(handle, CISTPL_VERS_1, p) == CS_SUCCESS) ||
1480         (read_tuple(handle, CISTPL_VERS_2, p) != CS_NO_MORE_ITEMS))
1481         ident_ok++;
1482
1483     if (!dev_ok && !ident_ok)
1484         goto done;
1485
1486     for (info->Chains = 1; info->Chains < MAX_TUPLES; info->Chains++) {
1487         ret = pcmcia_get_next_tuple(handle, tuple);
1488         if (ret != CS_SUCCESS) break;
1489         if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
1490             ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
1491             ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
1492             reserved++;
1493     }
1494     if ((info->Chains == MAX_TUPLES) || (reserved > 5) ||
1495         ((!dev_ok || !ident_ok) && (info->Chains > 10)))
1496         info->Chains = 0;
1497
1498 done:
1499     kfree(tuple);
1500     kfree(p);
1501     return CS_SUCCESS;
1502 }
1503