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