Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / bus / pccard / pccard_cis_quirks.c
1 /*      $NetBSD: pcmcia_cis_quirks.c,v 1.6 2000/04/12 21:07:55 scw Exp $ */
2 /* $FreeBSD: src/sys/dev/pccard/pccard_cis_quirks.c,v 1.7 2002/02/09 21:34:06 imp Exp $ */
3 /* $DragonFly: src/sys/bus/pccard/pccard_cis_quirks.c,v 1.2 2004/03/15 17:15:18 dillon Exp $ */
4
5 #define PCCARDDEBUG
6
7 /*
8  * Copyright (c) 1998 Marc Horowitz.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Marc Horowitz.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/kernel.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43
44 #include <sys/bus.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <machine/resource.h>
48
49 #include <bus/pccard/pccarddevs.h>
50 #include <bus/pccard/pccardreg.h>
51 #include <bus/pccard/pccardvar.h>
52
53 /* There are cards out there whose CIS flat-out lies.  This file
54    contains struct pccard_function chains for those devices. */
55
56 /* these structures are just static templates which are then copied
57    into "live" allocated structures */
58
59 struct pccard_function pccard_3cxem556_func0 = {
60         0,                      /* function number */
61         PCCARD_FUNCTION_NETWORK,
62         0x07,                   /* last cfe number */
63         0x800,                  /* ccr_base */
64         0x63,                   /* ccr_mask */
65 };
66
67 struct pccard_config_entry pccard_3cxem556_func0_cfe0 = {
68         0x07,                   /* cfe number */
69         PCCARD_CFE_IO8 | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
70         PCCARD_IFTYPE_IO,
71         1,                      /* num_iospace */
72         4,                      /* iomask */
73         { { 0x0010, 0 } },      /* iospace */
74         0xffff,                 /* irqmask */
75         0,                      /* num_memspace */
76         { },                    /* memspace */
77         0,                      /* maxtwins */
78 };
79
80 static struct pccard_function pccard_3cxem556_func1 = {
81         1,                      /* function number */
82         PCCARD_FUNCTION_SERIAL,
83         0x27,                   /* last cfe number */
84         0x900,                  /* ccr_base */
85         0x63,                   /* ccr_mask */
86 };
87
88 static struct pccard_config_entry pccard_3cxem556_func1_cfe0 = {
89         0x27,                   /* cfe number */
90         PCCARD_CFE_IO8 | PCCARD_CFE_IRQLEVEL,
91         PCCARD_IFTYPE_IO,
92         1,                      /* num_iospace */
93         3,                      /* iomask */
94         { { 0x0008, 0 } },      /* iospace */
95         0xffff,                 /* irqmask */
96         0,                      /* num_memspace */
97         { },                    /* memspace */
98         0,                      /* maxtwins */
99 };
100
101 static struct pccard_function pccard_3ccfem556bi_func0 = {
102         0,                      /* function number */
103         PCCARD_FUNCTION_NETWORK,
104         0x07,                   /* last cfe number */
105         0x1000,                 /* ccr_base */
106         0x267,                  /* ccr_mask */
107 };
108
109 static struct pccard_config_entry pccard_3ccfem556bi_func0_cfe0 = {
110         0x07,                   /* cfe number */
111         PCCARD_CFE_IO8 | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
112         PCCARD_IFTYPE_IO,
113         1,                      /* num_iospace */
114         5,                      /* iomask */
115         { { 0x0020, 0 } },      /* iospace */
116         0xffff,                 /* irqmask */
117         0,                      /* num_memspace */
118         { },                    /* memspace */
119         0,                      /* maxtwins */
120 };
121
122 static struct pccard_function pccard_3ccfem556bi_func1 = {
123         1,                      /* function number */
124         PCCARD_FUNCTION_SERIAL,
125         0x27,                   /* last cfe number */
126         0x1100,                 /* ccr_base */
127         0x277,                  /* ccr_mask */
128 };
129
130 static struct pccard_config_entry pccard_3ccfem556bi_func1_cfe0 = {
131         0x27,                   /* cfe number */
132         PCCARD_CFE_IO8 | PCCARD_CFE_IRQLEVEL,
133         PCCARD_IFTYPE_IO,
134         1,                      /* num_iospace */
135         3,                      /* iomask */
136         { { 0x0008, 0 } },      /* iospace */
137         0xffff,                 /* irqmask */
138         0,                      /* num_memspace */
139         { },                    /* memspace */
140         0,                      /* maxtwins */
141 };
142
143 static struct pccard_function pccard_sveclancard_func0 = {
144         0,                      /* function number */
145         PCCARD_FUNCTION_NETWORK,
146         0x1,                    /* last cfe number */
147         0x100,                  /* ccr_base */
148         0x1,                    /* ccr_mask */
149 };
150
151 static struct pccard_config_entry pccard_sveclancard_func0_cfe0 = {
152         0x1,                    /* cfe number */
153         PCCARD_CFE_MWAIT_REQUIRED | PCCARD_CFE_RDYBSY_ACTIVE |
154         PCCARD_CFE_WP_ACTIVE | PCCARD_CFE_BVD_ACTIVE | PCCARD_CFE_IO16,
155         PCCARD_IFTYPE_IO,
156         1,                      /* num_iospace */
157         5,                      /* iomask */
158         { { 0x20, 0x300 } },    /* iospace */
159         0xdeb8,                 /* irqmask */
160         0,                      /* num_memspace */
161         { },                    /* memspace */
162         0,                      /* maxtwins */
163 };
164
165 static struct pccard_function pccard_ndc_nd5100_func0 = {
166         0,                      /* function number */
167         PCCARD_FUNCTION_NETWORK,
168         0x23,                   /* last cfe number */
169         0x3f8,                  /* ccr_base */
170         0x3,                    /* ccr_mask */
171 };
172
173 static struct pccard_config_entry pccard_ndc_nd5100_func0_cfe0 = {
174         0x20,                   /* cfe number */
175         PCCARD_CFE_MWAIT_REQUIRED | PCCARD_CFE_IO16 | PCCARD_CFE_IRQLEVEL,
176         PCCARD_IFTYPE_IO,
177         1,                      /* num_iospace */
178         5,                      /* iomask */
179         { { 0x20, 0x300 } },    /* iospace */
180         0xdeb8,                 /* irqmask */
181         0,                      /* num_memspace */
182         { },                    /* memspace */
183         0,                      /* maxtwins */
184 };
185
186 static struct pccard_cis_quirk pccard_cis_quirks[] = {
187         { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556, PCMCIA_CIS_INVALID, 
188           &pccard_3cxem556_func0, &pccard_3cxem556_func0_cfe0 },
189         { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556, PCMCIA_CIS_INVALID,
190           &pccard_3cxem556_func1, &pccard_3cxem556_func1_cfe0 },
191         { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556INT, PCMCIA_CIS_INVALID, 
192           &pccard_3cxem556_func0, &pccard_3cxem556_func0_cfe0 },
193         { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556INT, PCMCIA_CIS_INVALID,
194           &pccard_3cxem556_func1, &pccard_3cxem556_func1_cfe0 },
195         { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CCFEM556BI,
196           PCMCIA_CIS_INVALID,
197           &pccard_3ccfem556bi_func0, &pccard_3ccfem556bi_func0_cfe0 },
198         { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CCFEM556BI,
199           PCMCIA_CIS_INVALID,
200           &pccard_3ccfem556bi_func1, &pccard_3ccfem556bi_func1_cfe0 },
201         { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, PCMCIA_CIS_SVEC_LANCARD,
202           &pccard_sveclancard_func0, &pccard_sveclancard_func0_cfe0 },
203         { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, PCMCIA_CIS_NDC_ND5100_E,
204           &pccard_ndc_nd5100_func0, &pccard_ndc_nd5100_func0_cfe0 },
205 };
206         
207 static int n_pccard_cis_quirks =
208         sizeof(pccard_cis_quirks)/sizeof(pccard_cis_quirks[0]);
209
210 void pccard_check_cis_quirks(device_t dev)
211 {
212         struct pccard_softc *sc = PCCARD_SOFTC(dev);
213         int wiped = 0;
214         int i, j;
215         struct pccard_function *pf, *pf_next, *pf_last;
216         struct pccard_config_entry *cfe, *cfe_next;
217
218         pf = NULL;
219         pf_last = NULL;
220
221         for (i=0; i<n_pccard_cis_quirks; i++) {
222                 if ((sc->card.manufacturer == pccard_cis_quirks[i].manufacturer) &&
223                         (sc->card.product == pccard_cis_quirks[i].product) &&
224                         (((sc->card.manufacturer != PCMCIA_VENDOR_INVALID) &&
225                           (sc->card.product != PCMCIA_PRODUCT_INVALID)) ||
226                          ((sc->card.manufacturer == PCMCIA_VENDOR_INVALID) &&
227                           (sc->card.product == PCMCIA_PRODUCT_INVALID) &&
228                           sc->card.cis1_info[0] &&
229                           (strcmp(sc->card.cis1_info[0],
230                                           pccard_cis_quirks[i].cis1_info[0]) == 0) &&
231                           sc->card.cis1_info[1] &&
232                           (strcmp(sc->card.cis1_info[1],
233                                           pccard_cis_quirks[i].cis1_info[1]) == 0)))) {
234                         if (!wiped) {
235                                 if (bootverbose) {
236                                         device_printf(dev, "using CIS quirks for ");
237                                         for (j = 0; j < 4; j++) {
238                                                 if (sc->card.cis1_info[j] == NULL)
239                                                         break;
240                                                 if (j)
241                                                         printf(", ");
242                                                 printf("%s", sc->card.cis1_info[j]);
243                                         }
244                                         printf("\n");
245                                 }
246
247                                 for (pf = STAILQ_FIRST(&sc->card.pf_head); pf != NULL;
248                                      pf = pf_next) {
249                                         for (cfe = STAILQ_FIRST(&pf->cfe_head); cfe != NULL;
250                                              cfe = cfe_next) {
251                                                 cfe_next = STAILQ_NEXT(cfe, cfe_list);
252                                                 free(cfe, M_DEVBUF);
253                                         }
254                                         pf_next = STAILQ_NEXT(pf, pf_list);
255                                         free(pf, M_DEVBUF);
256                                 }
257
258                                 STAILQ_INIT(&sc->card.pf_head);
259                                 wiped = 1;
260                         }
261
262                         if (pf_last == pccard_cis_quirks[i].pf) {
263                                 cfe = malloc(sizeof(*cfe), M_DEVBUF, M_INTWAIT);
264                                 *cfe = *pccard_cis_quirks[i].cfe;
265
266                                 STAILQ_INSERT_TAIL(&pf->cfe_head, cfe, cfe_list);
267                         } else {
268                                 pf = malloc(sizeof(*pf), M_DEVBUF, M_INTWAIT);
269                                 *pf = *pccard_cis_quirks[i].pf;
270                                 STAILQ_INIT(&pf->cfe_head);
271
272                                 cfe = malloc(sizeof(*cfe), M_DEVBUF, M_INTWAIT);
273                                 *cfe = *pccard_cis_quirks[i].cfe;
274
275                                 STAILQ_INSERT_TAIL(&pf->cfe_head, cfe, cfe_list);
276                                 STAILQ_INSERT_TAIL(&sc->card.pf_head, pf, pf_list);
277
278                                 pf_last = pccard_cis_quirks[i].pf;
279                         }
280                 }
281         }
282 }