summaryrefslogtreecommitdiffstats
path: root/sys/isa/pnpparse.c
blob: 3f08cab2b182e571628136a93c93b9ae9daeda2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*-
 * Copyright (c) 1999 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <isa/isavar.h>
#include <isa/pnpreg.h>
#include <isa/pnpvar.h>

#define	MAXDEP	8

#define I16(p)	((p)[0] + ((p)[1] << 8))
#define I32(p)	(I16(p) + (I16(p+2) << 16))

/*
 * Parse resource data for Logical Devices.
 *
 * This function exits as soon as it gets an error reading *ANY*
 * Resource Data or it reaches the end of Resource Data.
 */
void
pnp_parse_resources(device_t dev, u_char *resources, int len)
{
	device_t parent = device_get_parent(dev);
	u_char tag, *resp, *resinfo;
	int large_len, scanning = len;
	u_int32_t id, compat_id;
	struct isa_config *config;
	int ncfgs = 1;
	int priorities[1 + MAXDEP];
	struct isa_config *configs;
	char buf[100];
	int i;

	id = isa_get_logicalid(dev);
	configs = (struct isa_config *)malloc(sizeof(*configs) * (1 + MAXDEP),
						M_DEVBUF, M_NOWAIT);
	if (configs == NULL) {
		device_printf(dev, "No memory to parse PNP data\n");
		return;
	}
	bzero(configs, sizeof(*configs) * (1 + MAXDEP));
	config = &configs[0];
	priorities[0] = 0;
	resp = resources;
	while (scanning > 0) {
		tag = *resp++;
		scanning--;
		if (PNP_RES_TYPE(tag) == 0) {
			/* Small resource */
			if (scanning < PNP_SRES_LEN(tag)) {
				scanning = 0;
				continue;
			}
			resinfo = resp;
			resp += PNP_SRES_LEN(tag);
			scanning -= PNP_SRES_LEN(tag);;
			
			switch (PNP_SRES_NUM(tag)) {
			case PNP_TAG_COMPAT_DEVICE:
				/*
				 * Got a compatible device id
				 * resource. Should keep a list of
				 * compat ids in the device.
				 */
				bcopy(resinfo, &compat_id, 4);
				isa_set_compatid(dev, compat_id);
				break;
		    
			case PNP_TAG_IRQ_FORMAT:
				if (bootverbose) {
					printf("%s: adding irq mask %#04x\n",
					       pnp_eisaformat(id),
					       I16(resinfo));
				}
				if (config->ic_nirq == ISA_NIRQ) {
					device_printf(parent, "too many irqs\n");
					scanning = 0;
					break;
				}
				config->ic_irqmask[config->ic_nirq] =
					I16(resinfo);
				config->ic_nirq++;
				break;

			case PNP_TAG_DMA_FORMAT:
				if (bootverbose) {
					printf("%s: adding dma mask %#02x\n",
					       pnp_eisaformat(id),
					       resinfo[0]);
				}
				if (config->ic_ndrq == ISA_NDRQ) {
					device_printf(parent, "too many drqs\n");
					scanning = 0;
					break;
				}
				config->ic_drqmask[config->ic_ndrq] =
					resinfo[0];
				config->ic_ndrq++;
				break;

			case PNP_TAG_START_DEPENDANT:
				if (bootverbose) {
					printf("%s: start dependant\n",
					       pnp_eisaformat(id));
				}
				if (ncfgs >= MAXDEP) {
					device_printf(parent, "too many dependant configs (%d)\n", MAXDEP);
					scanning = 0;
					break;
				}
				config = &configs[ncfgs];
				/*
				 * If the priority is not specified,
				 * then use the default of
				 * 'acceptable'
				 */
				if (PNP_SRES_LEN(tag) > 0)
					priorities[ncfgs] = resinfo[0];
				else
					priorities[ncfgs] = 1;
				ncfgs++;
				break;

			case PNP_TAG_END_DEPENDANT:
				if (bootverbose) {
					printf("%s: end dependant\n",
					       pnp_eisaformat(id));
				}
				config = &configs[0];	/* back to main config */
				break;

			case PNP_TAG_IO_RANGE:
				if (bootverbose) {
					printf("%s: adding io range "
					       "%#x-%#x, size=%#x, "
					       "align=%#x\n",
					       pnp_eisaformat(id),
					       I16(resinfo + 1),
					       I16(resinfo + 3) + resinfo[6]-1,
					       resinfo[6],
					       resinfo[5]);
				}
				if (config->ic_nport == ISA_NPORT) {
					device_printf(parent, "too many ports\n");
					scanning = 0;
					break;
				}
				config->ic_port[config->ic_nport].ir_start =
					I16(resinfo + 1);
				config->ic_port[config->ic_nport].ir_end =
					I16(resinfo + 3) + resinfo[6] - 1;
				config->ic_port[config->ic_nport].ir_size =
					resinfo[6];
				if (resinfo[5] == 0) {
				    /* Make sure align is at least one */
				    resinfo[5] = 1;
				}
				config->ic_port[config->ic_nport].ir_align =
					resinfo[5];
				config->ic_nport++;
				break;

			case PNP_TAG_IO_FIXED:
				if (bootverbose) {
					printf("%s: adding fixed io range "
					       "%#x-%#x, size=%#x, "
					       "align=%#x\n",
					       pnp_eisaformat(id),
					       I16(resinfo),
					       I16(resinfo) + resinfo[2] - 1,
					       resinfo[2],
					       1);
				}
				if (config->ic_nport == ISA_NPORT) {
					device_printf(parent, "too many ports\n");
					scanning = 0;
					break;
				}
				config->ic_port[config->ic_nport].ir_start =
					I16(resinfo);
				config->ic_port[config->ic_nport].ir_end =
					I16(resinfo) + resinfo[2] - 1;
				config->ic_port[config->ic_nport].ir_size
					= resinfo[2];
				config->ic_port[config->ic_nport].ir_align = 1;
				config->ic_nport++;
				break;

			case PNP_TAG_END:
				if (bootverbose) {
					printf("%s: end config\n",
					       pnp_eisaformat(id));
				}
				scanning = 0;
				break;

			default:
				/* Skip this resource */
				device_printf(parent, "unexpected small tag %d\n",
					      PNP_SRES_NUM(tag));
				break;
			}
		} else {
			/* Large resource */
			if (scanning < 2) {
				scanning = 0;
				continue;
			}
			large_len = I16(resp);
			resp += 2;
			scanning -= 2;

			if (scanning < large_len) {
				scanning = 0;
				continue;
			}
			resinfo = resp;
			resp += large_len;
			scanning -= large_len;

			switch (PNP_LRES_NUM(tag)) {
			case PNP_TAG_ID_ANSI:
				if (large_len > sizeof(buf) - 1)
					large_len = sizeof(buf) - 1;
				bcopy(resinfo, buf, large_len);

				/*
				 * Trim trailing spaces and garbage.
				 */
				while (large_len > 0 && buf[large_len - 1] <= ' ')
					large_len--;
				buf[large_len] = '\0';
				device_set_desc_copy(dev, buf);
				break;
				
			case PNP_TAG_MEMORY_RANGE:
				if (bootverbose) {
					int temp = I16(resinfo + 7) << 8;

					printf("%s: adding memory range "
					       "%#x-%#x, size=%#x, "
					       "align=%#x\n",
					       pnp_eisaformat(id),
					       I16(resinfo + 1)<<8,
					       (I16(resinfo + 3)<<8) + temp - 1,
					       temp,
					       I16(resinfo + 5));
				}

				if (config->ic_nmem == ISA_NMEM) {
					device_printf(parent, "too many memory ranges\n");
					scanning = 0;
					break;
				}

				config->ic_mem[config->ic_nmem].ir_start =
					I16(resinfo + 1)<<8;
				config->ic_mem[config->ic_nmem].ir_end =
					(I16(resinfo + 3)<<8)
					+ (I16(resinfo + 7) << 8) - 1;
				config->ic_mem[config->ic_nmem].ir_size =
					I16(resinfo + 7) << 8;
				config->ic_mem[config->ic_nmem].ir_align =
					I16(resinfo + 5);
				if (!config->ic_mem[config->ic_nmem].ir_align)
					config->ic_mem[config->ic_nmem]
						.ir_align = 0x10000;
				config->ic_nmem++;
				break;

			case PNP_TAG_MEMORY32_RANGE:
				if (bootverbose) {
					printf("%s: adding memory32 range "
					       "%#x-%#x, size=%#x, "
					       "align=%#x\n",
					       pnp_eisaformat(id),
					       I32(resinfo + 1),
					       I32(resinfo + 5)
					       + I32(resinfo + 13) - 1,
					       I32(resinfo + 13),
					       I32(resinfo + 9));
				}

				if (config->ic_nmem == ISA_NMEM) {
					device_printf(parent, "too many memory ranges\n");
					scanning = 0;
					break;
				}

				config->ic_mem[config->ic_nmem].ir_start =
					I32(resinfo + 1);
				config->ic_mem[config->ic_nmem].ir_end =
					I32(resinfo + 5)
					+ I32(resinfo + 13) - 1;
				config->ic_mem[config->ic_nmem].ir_size =
					I32(resinfo + 13);
				config->ic_mem[config->ic_nmem].ir_align =
					I32(resinfo + 9);
				config->ic_nmem++;
				break;

			case PNP_TAG_MEMORY32_FIXED:
				if (I32(resinfo + 5) == 0) {
					if (bootverbose) {
						printf("%s: skipping empty range\n",
						       pnp_eisaformat(id));
					}
					continue;
				}
				if (bootverbose) {
					printf("%s: adding fixed memory32 range "
					       "%#x-%#x, size=%#x\n",
					       pnp_eisaformat(id),
					       I32(resinfo + 1),
					       I32(resinfo + 1)
					       + I32(resinfo + 5) - 1,
					       I32(resinfo + 5));
				}

				if (config->ic_nmem == ISA_NMEM) {
					device_printf(parent, "too many memory ranges\n");
					scanning = 0;
					break;
				}

				config->ic_mem[config->ic_nmem].ir_start =
					I32(resinfo + 1);
				config->ic_mem[config->ic_nmem].ir_end =
					I32(resinfo + 1)
					+ I32(resinfo + 5) - 1;
				config->ic_mem[config->ic_nmem].ir_size =
					I32(resinfo + 5);
				config->ic_mem[config->ic_nmem].ir_align = 1;
				config->ic_nmem++;
				break;

			default:
				/* Skip this resource */
				device_printf(parent, "unexpected large tag %d\n",
					      PNP_SRES_NUM(tag));
			}
		}
	}
	if(ncfgs == 1) {
		/* Single config without dependants */
		(void)ISA_ADD_CONFIG(parent, dev, priorities[0], &configs[0]);
		free(configs, M_DEVBUF);
		return;
	}
	/* Cycle through dependant configs merging primary details */
	for(i = 1; i < ncfgs; i++) {
		int j;
		config = &configs[i];
		for(j = 0; j < configs[0].ic_nmem; j++) {
			if (config->ic_nmem == ISA_NMEM) {
				device_printf(parent, "too many memory ranges\n");
				free(configs, M_DEVBUF);
				return;
			}
			config->ic_mem[config->ic_nmem] = configs[0].ic_mem[j];
			config->ic_nmem++;
		}
		for(j = 0; j < configs[0].ic_nport; j++) {
			if (config->ic_nport == ISA_NPORT) {
				device_printf(parent, "too many port ranges\n");
				free(configs, M_DEVBUF);
				return;
			}
			config->ic_port[config->ic_nport] = configs[0].ic_port[j];
			config->ic_nport++;
		}
		for(j = 0; j < configs[0].ic_nirq; j++) {
			if (config->ic_nirq == ISA_NIRQ) {
				device_printf(parent, "too many irq ranges\n");
				free(configs, M_DEVBUF);
				return;
			}
			config->ic_irqmask[config->ic_nirq] = configs[0].ic_irqmask[j];
			config->ic_nirq++;
		}
		for(j = 0; j < configs[0].ic_ndrq; j++) {
			if (config->ic_ndrq == ISA_NDRQ) {
				device_printf(parent, "too many drq ranges\n");
				free(configs, M_DEVBUF);
				return;
			}
			config->ic_drqmask[config->ic_ndrq] = configs[0].ic_drqmask[j];
			config->ic_ndrq++;
		}
		(void)ISA_ADD_CONFIG(parent, dev, priorities[i], &configs[i]);
	}
	free(configs, M_DEVBUF);
}
OpenPOWER on IntegriCloud