Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * rsparser.c - parses and encodes pnpbios resource data streams
  3 */
  4
  5#include <linux/ctype.h>
  6#include <linux/pnp.h>
  7#include <linux/string.h>
  8
  9#ifdef CONFIG_PCI
 10#include <linux/pci.h>
 11#else
 12inline void pcibios_penalize_isa_irq(int irq, int active)
 13{
 14}
 15#endif				/* CONFIG_PCI */
 16
 17#include "../base.h"
 18#include "pnpbios.h"
 19
 20/* standard resource tags */
 21#define SMALL_TAG_PNPVERNO		0x01
 22#define SMALL_TAG_LOGDEVID		0x02
 23#define SMALL_TAG_COMPATDEVID		0x03
 24#define SMALL_TAG_IRQ			0x04
 25#define SMALL_TAG_DMA			0x05
 26#define SMALL_TAG_STARTDEP		0x06
 27#define SMALL_TAG_ENDDEP		0x07
 28#define SMALL_TAG_PORT			0x08
 29#define SMALL_TAG_FIXEDPORT		0x09
 30#define SMALL_TAG_VENDOR		0x0e
 31#define SMALL_TAG_END			0x0f
 32#define LARGE_TAG			0x80
 33#define LARGE_TAG_MEM			0x81
 34#define LARGE_TAG_ANSISTR		0x82
 35#define LARGE_TAG_UNICODESTR		0x83
 36#define LARGE_TAG_VENDOR		0x84
 37#define LARGE_TAG_MEM32			0x85
 38#define LARGE_TAG_FIXEDMEM32		0x86
 39
 40/*
 41 * Resource Data Stream Format:
 42 *
 43 * Allocated Resources (required)
 44 * end tag ->
 45 * Resource Configuration Options (optional)
 46 * end tag ->
 47 * Compitable Device IDs (optional)
 48 * final end tag ->
 49 */
 50
 51/*
 52 * Allocated Resources
 53 */
 54
 55static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
 56					       int start, int len)
 57{
 58	int flags = 0;
 59	int end = start + len - 1;
 60
 61	if (len <= 0 || end >= 0x10003)
 62		flags |= IORESOURCE_DISABLED;
 63
 64	pnp_add_io_resource(dev, start, end, flags);
 65}
 66
 67static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
 68						int start, int len)
 69{
 70	int flags = 0;
 71	int end = start + len - 1;
 72
 73	if (len <= 0)
 74		flags |= IORESOURCE_DISABLED;
 75
 76	pnp_add_mem_resource(dev, start, end, flags);
 77}
 78
 79static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
 80							    unsigned char *p,
 81							    unsigned char *end)
 82{
 83	unsigned int len, tag;
 84	int io, size, mask, i, flags;
 85
 86	if (!p)
 87		return NULL;
 88
 89	pnp_dbg(&dev->dev, "parse allocated resources\n");
 90
 91	pnp_init_resources(dev);
 92
 93	while ((char *)p < (char *)end) {
 94
 95		/* determine the type of tag */
 96		if (p[0] & LARGE_TAG) {	/* large tag */
 97			len = (p[2] << 8) | p[1];
 98			tag = p[0];
 99		} else {	/* small tag */
100			len = p[0] & 0x07;
101			tag = ((p[0] >> 3) & 0x0f);
102		}
103
104		switch (tag) {
105
106		case LARGE_TAG_MEM:
107			if (len != 9)
108				goto len_err;
109			io = *(short *)&p[4];
110			size = *(short *)&p[10];
111			pnpbios_parse_allocated_memresource(dev, io, size);
112			break;
113
114		case LARGE_TAG_ANSISTR:
115			/* ignore this for now */
116			break;
117
118		case LARGE_TAG_VENDOR:
119			/* do nothing */
120			break;
121
122		case LARGE_TAG_MEM32:
123			if (len != 17)
124				goto len_err;
125			io = *(int *)&p[4];
126			size = *(int *)&p[16];
127			pnpbios_parse_allocated_memresource(dev, io, size);
128			break;
129
130		case LARGE_TAG_FIXEDMEM32:
131			if (len != 9)
132				goto len_err;
133			io = *(int *)&p[4];
134			size = *(int *)&p[8];
135			pnpbios_parse_allocated_memresource(dev, io, size);
136			break;
137
138		case SMALL_TAG_IRQ:
139			if (len < 2 || len > 3)
140				goto len_err;
141			flags = 0;
142			io = -1;
143			mask = p[1] + p[2] * 256;
144			for (i = 0; i < 16; i++, mask = mask >> 1)
145				if (mask & 0x01)
146					io = i;
147			if (io != -1)
148				pcibios_penalize_isa_irq(io, 1);
149			else
150				flags = IORESOURCE_DISABLED;
151			pnp_add_irq_resource(dev, io, flags);
152			break;
153
154		case SMALL_TAG_DMA:
155			if (len != 2)
156				goto len_err;
157			flags = 0;
158			io = -1;
159			mask = p[1];
160			for (i = 0; i < 8; i++, mask = mask >> 1)
161				if (mask & 0x01)
162					io = i;
163			if (io == -1)
164				flags = IORESOURCE_DISABLED;
165			pnp_add_dma_resource(dev, io, flags);
166			break;
167
168		case SMALL_TAG_PORT:
169			if (len != 7)
170				goto len_err;
171			io = p[2] + p[3] * 256;
172			size = p[7];
173			pnpbios_parse_allocated_ioresource(dev, io, size);
174			break;
175
176		case SMALL_TAG_VENDOR:
177			/* do nothing */
178			break;
179
180		case SMALL_TAG_FIXEDPORT:
181			if (len != 3)
182				goto len_err;
183			io = p[1] + p[2] * 256;
184			size = p[3];
185			pnpbios_parse_allocated_ioresource(dev, io, size);
186			break;
187
188		case SMALL_TAG_END:
189			p = p + 2;
190			return (unsigned char *)p;
191			break;
192
193		default:	/* an unknown tag */
194len_err:
195			dev_err(&dev->dev, "unknown tag %#x length %d\n",
196				tag, len);
197			break;
198		}
199
200		/* continue to the next tag */
201		if (p[0] & LARGE_TAG)
202			p += len + 3;
203		else
204			p += len + 1;
205	}
206
207	dev_err(&dev->dev, "no end tag in resource structure\n");
208
209	return NULL;
210}
211
212/*
213 * Resource Configuration Options
214 */
215
216static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
217					    unsigned char *p, int size,
218					    unsigned int option_flags)
219{
220	resource_size_t min, max, align, len;
221	unsigned char flags;
222
223	min = ((p[5] << 8) | p[4]) << 8;
224	max = ((p[7] << 8) | p[6]) << 8;
225	align = (p[9] << 8) | p[8];
226	len = ((p[11] << 8) | p[10]) << 8;
227	flags = p[3];
228	pnp_register_mem_resource(dev, option_flags, min, max, align, len,
229				  flags);
230}
231
232static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
233					      unsigned char *p, int size,
234					      unsigned int option_flags)
235{
236	resource_size_t min, max, align, len;
237	unsigned char flags;
238
239	min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
240	max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
241	align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
242	len = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
243	flags = p[3];
244	pnp_register_mem_resource(dev, option_flags, min, max, align, len,
245				  flags);
246}
247
248static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
249						    unsigned char *p, int size,
250						    unsigned int option_flags)
251{
252	resource_size_t base, len;
253	unsigned char flags;
254
255	base = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
256	len = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
257	flags = p[3];
258	pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
259}
260
261static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
262					    unsigned char *p, int size,
263					    unsigned int option_flags)
264{
265	unsigned long bits;
266	pnp_irq_mask_t map;
267	unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
268
269	bits = (p[2] << 8) | p[1];
270
271	bitmap_zero(map.bits, PNP_IRQ_NR);
272	bitmap_copy(map.bits, &bits, 16);
273
274	if (size > 2)
275		flags = p[3];
276
277	pnp_register_irq_resource(dev, option_flags, &map, flags);
278}
279
280static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
281					    unsigned char *p, int size,
282					    unsigned int option_flags)
283{
284	pnp_register_dma_resource(dev, option_flags, p[1], p[2]);
285}
286
287static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
288					     unsigned char *p, int size,
289					     unsigned int option_flags)
290{
291	resource_size_t min, max, align, len;
292	unsigned char flags;
293
294	min = (p[3] << 8) | p[2];
295	max = (p[5] << 8) | p[4];
296	align = p[6];
297	len = p[7];
298	flags = p[1] ? IORESOURCE_IO_16BIT_ADDR : 0;
299	pnp_register_port_resource(dev, option_flags, min, max, align, len,
300				   flags);
301}
302
303static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
304						   unsigned char *p, int size,
305						   unsigned int option_flags)
306{
307	resource_size_t base, len;
308
309	base = (p[2] << 8) | p[1];
310	len = p[3];
311	pnp_register_port_resource(dev, option_flags, base, base, 0, len,
312				   IORESOURCE_IO_FIXED);
313}
314
315static __init unsigned char *
316pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
317				   struct pnp_dev *dev)
318{
319	unsigned int len, tag;
320	int priority;
321	unsigned int option_flags;
322
323	if (!p)
324		return NULL;
325
326	pnp_dbg(&dev->dev, "parse resource options\n");
327	option_flags = 0;
328	while ((char *)p < (char *)end) {
329
330		/* determine the type of tag */
331		if (p[0] & LARGE_TAG) {	/* large tag */
332			len = (p[2] << 8) | p[1];
333			tag = p[0];
334		} else {	/* small tag */
335			len = p[0] & 0x07;
336			tag = ((p[0] >> 3) & 0x0f);
337		}
338
339		switch (tag) {
340
341		case LARGE_TAG_MEM:
342			if (len != 9)
343				goto len_err;
344			pnpbios_parse_mem_option(dev, p, len, option_flags);
345			break;
346
347		case LARGE_TAG_MEM32:
348			if (len != 17)
349				goto len_err;
350			pnpbios_parse_mem32_option(dev, p, len, option_flags);
351			break;
352
353		case LARGE_TAG_FIXEDMEM32:
354			if (len != 9)
355				goto len_err;
356			pnpbios_parse_fixed_mem32_option(dev, p, len,
357							 option_flags);
358			break;
359
360		case SMALL_TAG_IRQ:
361			if (len < 2 || len > 3)
362				goto len_err;
363			pnpbios_parse_irq_option(dev, p, len, option_flags);
364			break;
365
366		case SMALL_TAG_DMA:
367			if (len != 2)
368				goto len_err;
369			pnpbios_parse_dma_option(dev, p, len, option_flags);
370			break;
371
372		case SMALL_TAG_PORT:
373			if (len != 7)
374				goto len_err;
375			pnpbios_parse_port_option(dev, p, len, option_flags);
376			break;
377
378		case SMALL_TAG_VENDOR:
379			/* do nothing */
380			break;
381
382		case SMALL_TAG_FIXEDPORT:
383			if (len != 3)
384				goto len_err;
385			pnpbios_parse_fixed_port_option(dev, p, len,
386							option_flags);
387			break;
388
389		case SMALL_TAG_STARTDEP:
390			if (len > 1)
391				goto len_err;
392			priority = PNP_RES_PRIORITY_ACCEPTABLE;
393			if (len > 0)
394				priority = p[1];
395			option_flags = pnp_new_dependent_set(dev, priority);
396			break;
397
398		case SMALL_TAG_ENDDEP:
399			if (len != 0)
400				goto len_err;
401			option_flags = 0;
402			break;
403
404		case SMALL_TAG_END:
405			return p + 2;
406
407		default:	/* an unknown tag */
408len_err:
409			dev_err(&dev->dev, "unknown tag %#x length %d\n",
410				tag, len);
411			break;
412		}
413
414		/* continue to the next tag */
415		if (p[0] & LARGE_TAG)
416			p += len + 3;
417		else
418			p += len + 1;
419	}
420
421	dev_err(&dev->dev, "no end tag in resource structure\n");
422
423	return NULL;
424}
425
426/*
427 * Compatible Device IDs
428 */
429
430static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
431						   unsigned char *end,
432						   struct pnp_dev *dev)
433{
434	int len, tag;
435	u32 eisa_id;
436	char id[8];
437	struct pnp_id *dev_id;
438
439	if (!p)
440		return NULL;
441
442	while ((char *)p < (char *)end) {
443
444		/* determine the type of tag */
445		if (p[0] & LARGE_TAG) {	/* large tag */
446			len = (p[2] << 8) | p[1];
447			tag = p[0];
448		} else {	/* small tag */
449			len = p[0] & 0x07;
450			tag = ((p[0] >> 3) & 0x0f);
451		}
452
453		switch (tag) {
454
455		case LARGE_TAG_ANSISTR:
456			strncpy(dev->name, p + 3,
457				len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
458			dev->name[len >=
459				  PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
460			break;
461
462		case SMALL_TAG_COMPATDEVID:	/* compatible ID */
463			if (len != 4)
464				goto len_err;
465			eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
466			pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
467			dev_id = pnp_add_id(dev, id);
468			if (!dev_id)
469				return NULL;
470			break;
471
472		case SMALL_TAG_END:
473			p = p + 2;
474			return (unsigned char *)p;
475			break;
476
477		default:	/* an unknown tag */
478len_err:
479			dev_err(&dev->dev, "unknown tag %#x length %d\n",
480				tag, len);
481			break;
482		}
483
484		/* continue to the next tag */
485		if (p[0] & LARGE_TAG)
486			p += len + 3;
487		else
488			p += len + 1;
489	}
490
491	dev_err(&dev->dev, "no end tag in resource structure\n");
492
493	return NULL;
494}
495
496/*
497 * Allocated Resource Encoding
498 */
499
500static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
501			       struct resource *res)
502{
503	unsigned long base;
504	unsigned long len;
505
506	if (pnp_resource_enabled(res)) {
507		base = res->start;
508		len = resource_size(res);
509	} else {
510		base = 0;
511		len = 0;
512	}
513
514	p[4] = (base >> 8) & 0xff;
515	p[5] = ((base >> 8) >> 8) & 0xff;
516	p[6] = (base >> 8) & 0xff;
517	p[7] = ((base >> 8) >> 8) & 0xff;
518	p[10] = (len >> 8) & 0xff;
519	p[11] = ((len >> 8) >> 8) & 0xff;
520
521	pnp_dbg(&dev->dev, "  encode mem %#lx-%#lx\n", base, base + len - 1);
522}
523
524static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
525				 struct resource *res)
526{
527	unsigned long base;
528	unsigned long len;
529
530	if (pnp_resource_enabled(res)) {
531		base = res->start;
532		len = resource_size(res);
533	} else {
534		base = 0;
535		len = 0;
536	}
537
538	p[4] = base & 0xff;
539	p[5] = (base >> 8) & 0xff;
540	p[6] = (base >> 16) & 0xff;
541	p[7] = (base >> 24) & 0xff;
542	p[8] = base & 0xff;
543	p[9] = (base >> 8) & 0xff;
544	p[10] = (base >> 16) & 0xff;
545	p[11] = (base >> 24) & 0xff;
546	p[16] = len & 0xff;
547	p[17] = (len >> 8) & 0xff;
548	p[18] = (len >> 16) & 0xff;
549	p[19] = (len >> 24) & 0xff;
550
551	pnp_dbg(&dev->dev, "  encode mem32 %#lx-%#lx\n", base, base + len - 1);
552}
553
554static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
555				       struct resource *res)
556{
557	unsigned long base;
558	unsigned long len;
559
560	if (pnp_resource_enabled(res)) {
561		base = res->start;
562		len = resource_size(res);
563	} else {
564		base = 0;
565		len = 0;
566	}
567
568	p[4] = base & 0xff;
569	p[5] = (base >> 8) & 0xff;
570	p[6] = (base >> 16) & 0xff;
571	p[7] = (base >> 24) & 0xff;
572	p[8] = len & 0xff;
573	p[9] = (len >> 8) & 0xff;
574	p[10] = (len >> 16) & 0xff;
575	p[11] = (len >> 24) & 0xff;
576
577	pnp_dbg(&dev->dev, "  encode fixed_mem32 %#lx-%#lx\n", base,
578		base + len - 1);
579}
580
581static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
582			       struct resource *res)
583{
584	unsigned long map;
585
586	if (pnp_resource_enabled(res))
587		map = 1 << res->start;
588	else
589		map = 0;
590
591	p[1] = map & 0xff;
592	p[2] = (map >> 8) & 0xff;
593
594	pnp_dbg(&dev->dev, "  encode irq mask %#lx\n", map);
595}
596
597static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
598			       struct resource *res)
599{
600	unsigned long map;
601
602	if (pnp_resource_enabled(res))
603		map = 1 << res->start;
604	else
605		map = 0;
606
607	p[1] = map & 0xff;
608
609	pnp_dbg(&dev->dev, "  encode dma mask %#lx\n", map);
610}
611
612static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
613				struct resource *res)
614{
615	unsigned long base;
616	unsigned long len;
617
618	if (pnp_resource_enabled(res)) {
619		base = res->start;
620		len = resource_size(res);
621	} else {
622		base = 0;
623		len = 0;
624	}
625
626	p[2] = base & 0xff;
627	p[3] = (base >> 8) & 0xff;
628	p[4] = base & 0xff;
629	p[5] = (base >> 8) & 0xff;
630	p[7] = len & 0xff;
631
632	pnp_dbg(&dev->dev, "  encode io %#lx-%#lx\n", base, base + len - 1);
633}
634
635static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
636				      struct resource *res)
637{
638	unsigned long base = res->start;
639	unsigned long len = resource_size(res);
640
641	if (pnp_resource_enabled(res)) {
642		base = res->start;
643		len = resource_size(res);
644	} else {
645		base = 0;
646		len = 0;
647	}
648
649	p[1] = base & 0xff;
650	p[2] = (base >> 8) & 0xff;
651	p[3] = len & 0xff;
652
653	pnp_dbg(&dev->dev, "  encode fixed_io %#lx-%#lx\n", base,
654		base + len - 1);
655}
656
657static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
658								*dev,
659							     unsigned char *p,
660							     unsigned char *end)
661{
662	unsigned int len, tag;
663	int port = 0, irq = 0, dma = 0, mem = 0;
664
665	if (!p)
666		return NULL;
667
668	while ((char *)p < (char *)end) {
669
670		/* determine the type of tag */
671		if (p[0] & LARGE_TAG) {	/* large tag */
672			len = (p[2] << 8) | p[1];
673			tag = p[0];
674		} else {	/* small tag */
675			len = p[0] & 0x07;
676			tag = ((p[0] >> 3) & 0x0f);
677		}
678
679		switch (tag) {
680
681		case LARGE_TAG_MEM:
682			if (len != 9)
683				goto len_err;
684			pnpbios_encode_mem(dev, p,
685				pnp_get_resource(dev, IORESOURCE_MEM, mem));
686			mem++;
687			break;
688
689		case LARGE_TAG_MEM32:
690			if (len != 17)
691				goto len_err;
692			pnpbios_encode_mem32(dev, p,
693				pnp_get_resource(dev, IORESOURCE_MEM, mem));
694			mem++;
695			break;
696
697		case LARGE_TAG_FIXEDMEM32:
698			if (len != 9)
699				goto len_err;
700			pnpbios_encode_fixed_mem32(dev, p,
701				pnp_get_resource(dev, IORESOURCE_MEM, mem));
702			mem++;
703			break;
704
705		case SMALL_TAG_IRQ:
706			if (len < 2 || len > 3)
707				goto len_err;
708			pnpbios_encode_irq(dev, p,
709				pnp_get_resource(dev, IORESOURCE_IRQ, irq));
710			irq++;
711			break;
712
713		case SMALL_TAG_DMA:
714			if (len != 2)
715				goto len_err;
716			pnpbios_encode_dma(dev, p,
717				pnp_get_resource(dev, IORESOURCE_DMA, dma));
718			dma++;
719			break;
720
721		case SMALL_TAG_PORT:
722			if (len != 7)
723				goto len_err;
724			pnpbios_encode_port(dev, p,
725				pnp_get_resource(dev, IORESOURCE_IO, port));
726			port++;
727			break;
728
729		case SMALL_TAG_VENDOR:
730			/* do nothing */
731			break;
732
733		case SMALL_TAG_FIXEDPORT:
734			if (len != 3)
735				goto len_err;
736			pnpbios_encode_fixed_port(dev, p,
737				pnp_get_resource(dev, IORESOURCE_IO, port));
738			port++;
739			break;
740
741		case SMALL_TAG_END:
742			p = p + 2;
743			return (unsigned char *)p;
744			break;
745
746		default:	/* an unknown tag */
747len_err:
748			dev_err(&dev->dev, "unknown tag %#x length %d\n",
749				tag, len);
750			break;
751		}
752
753		/* continue to the next tag */
754		if (p[0] & LARGE_TAG)
755			p += len + 3;
756		else
757			p += len + 1;
758	}
759
760	dev_err(&dev->dev, "no end tag in resource structure\n");
761
762	return NULL;
763}
764
765/*
766 * Core Parsing Functions
767 */
768
769int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
770					struct pnp_bios_node *node)
771{
772	unsigned char *p = (char *)node->data;
773	unsigned char *end = (char *)(node->data + node->size);
774
775	p = pnpbios_parse_allocated_resource_data(dev, p, end);
776	if (!p)
777		return -EIO;
778	p = pnpbios_parse_resource_option_data(p, end, dev);
779	if (!p)
780		return -EIO;
781	p = pnpbios_parse_compatible_ids(p, end, dev);
782	if (!p)
783		return -EIO;
784	return 0;
785}
786
787int pnpbios_read_resources_from_node(struct pnp_dev *dev,
788				     struct pnp_bios_node *node)
789{
790	unsigned char *p = (char *)node->data;
791	unsigned char *end = (char *)(node->data + node->size);
792
793	p = pnpbios_parse_allocated_resource_data(dev, p, end);
794	if (!p)
795		return -EIO;
796	return 0;
797}
798
799int pnpbios_write_resources_to_node(struct pnp_dev *dev,
800				    struct pnp_bios_node *node)
801{
802	unsigned char *p = (char *)node->data;
803	unsigned char *end = (char *)(node->data + node->size);
804
805	p = pnpbios_encode_allocated_resource_data(dev, p, end);
806	if (!p)
807		return -EIO;
808	return 0;
809}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * rsparser.c - parses and encodes pnpbios resource data streams
  4 */
  5
  6#include <linux/ctype.h>
  7#include <linux/pnp.h>
  8#include <linux/string.h>
  9
 10#ifdef CONFIG_PCI
 11#include <linux/pci.h>
 12#else
 13inline void pcibios_penalize_isa_irq(int irq, int active)
 14{
 15}
 16#endif				/* CONFIG_PCI */
 17
 18#include "../base.h"
 19#include "pnpbios.h"
 20
 21/* standard resource tags */
 22#define SMALL_TAG_PNPVERNO		0x01
 23#define SMALL_TAG_LOGDEVID		0x02
 24#define SMALL_TAG_COMPATDEVID		0x03
 25#define SMALL_TAG_IRQ			0x04
 26#define SMALL_TAG_DMA			0x05
 27#define SMALL_TAG_STARTDEP		0x06
 28#define SMALL_TAG_ENDDEP		0x07
 29#define SMALL_TAG_PORT			0x08
 30#define SMALL_TAG_FIXEDPORT		0x09
 31#define SMALL_TAG_VENDOR		0x0e
 32#define SMALL_TAG_END			0x0f
 33#define LARGE_TAG			0x80
 34#define LARGE_TAG_MEM			0x81
 35#define LARGE_TAG_ANSISTR		0x82
 36#define LARGE_TAG_UNICODESTR		0x83
 37#define LARGE_TAG_VENDOR		0x84
 38#define LARGE_TAG_MEM32			0x85
 39#define LARGE_TAG_FIXEDMEM32		0x86
 40
 41/*
 42 * Resource Data Stream Format:
 43 *
 44 * Allocated Resources (required)
 45 * end tag ->
 46 * Resource Configuration Options (optional)
 47 * end tag ->
 48 * Compitable Device IDs (optional)
 49 * final end tag ->
 50 */
 51
 52/*
 53 * Allocated Resources
 54 */
 55
 56static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
 57					       int start, int len)
 58{
 59	int flags = 0;
 60	int end = start + len - 1;
 61
 62	if (len <= 0 || end >= 0x10003)
 63		flags |= IORESOURCE_DISABLED;
 64
 65	pnp_add_io_resource(dev, start, end, flags);
 66}
 67
 68static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
 69						int start, int len)
 70{
 71	int flags = 0;
 72	int end = start + len - 1;
 73
 74	if (len <= 0)
 75		flags |= IORESOURCE_DISABLED;
 76
 77	pnp_add_mem_resource(dev, start, end, flags);
 78}
 79
 80static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
 81							    unsigned char *p,
 82							    unsigned char *end)
 83{
 84	unsigned int len, tag;
 85	int io, size, mask, i, flags;
 86
 87	if (!p)
 88		return NULL;
 89
 90	pnp_dbg(&dev->dev, "parse allocated resources\n");
 91
 92	pnp_init_resources(dev);
 93
 94	while ((char *)p < (char *)end) {
 95
 96		/* determine the type of tag */
 97		if (p[0] & LARGE_TAG) {	/* large tag */
 98			len = (p[2] << 8) | p[1];
 99			tag = p[0];
100		} else {	/* small tag */
101			len = p[0] & 0x07;
102			tag = ((p[0] >> 3) & 0x0f);
103		}
104
105		switch (tag) {
106
107		case LARGE_TAG_MEM:
108			if (len != 9)
109				goto len_err;
110			io = *(short *)&p[4];
111			size = *(short *)&p[10];
112			pnpbios_parse_allocated_memresource(dev, io, size);
113			break;
114
115		case LARGE_TAG_ANSISTR:
116			/* ignore this for now */
117			break;
118
119		case LARGE_TAG_VENDOR:
120			/* do nothing */
121			break;
122
123		case LARGE_TAG_MEM32:
124			if (len != 17)
125				goto len_err;
126			io = *(int *)&p[4];
127			size = *(int *)&p[16];
128			pnpbios_parse_allocated_memresource(dev, io, size);
129			break;
130
131		case LARGE_TAG_FIXEDMEM32:
132			if (len != 9)
133				goto len_err;
134			io = *(int *)&p[4];
135			size = *(int *)&p[8];
136			pnpbios_parse_allocated_memresource(dev, io, size);
137			break;
138
139		case SMALL_TAG_IRQ:
140			if (len < 2 || len > 3)
141				goto len_err;
142			flags = 0;
143			io = -1;
144			mask = p[1] + p[2] * 256;
145			for (i = 0; i < 16; i++, mask = mask >> 1)
146				if (mask & 0x01)
147					io = i;
148			if (io != -1)
149				pcibios_penalize_isa_irq(io, 1);
150			else
151				flags = IORESOURCE_DISABLED;
152			pnp_add_irq_resource(dev, io, flags);
153			break;
154
155		case SMALL_TAG_DMA:
156			if (len != 2)
157				goto len_err;
158			flags = 0;
159			io = -1;
160			mask = p[1];
161			for (i = 0; i < 8; i++, mask = mask >> 1)
162				if (mask & 0x01)
163					io = i;
164			if (io == -1)
165				flags = IORESOURCE_DISABLED;
166			pnp_add_dma_resource(dev, io, flags);
167			break;
168
169		case SMALL_TAG_PORT:
170			if (len != 7)
171				goto len_err;
172			io = p[2] + p[3] * 256;
173			size = p[7];
174			pnpbios_parse_allocated_ioresource(dev, io, size);
175			break;
176
177		case SMALL_TAG_VENDOR:
178			/* do nothing */
179			break;
180
181		case SMALL_TAG_FIXEDPORT:
182			if (len != 3)
183				goto len_err;
184			io = p[1] + p[2] * 256;
185			size = p[3];
186			pnpbios_parse_allocated_ioresource(dev, io, size);
187			break;
188
189		case SMALL_TAG_END:
190			p = p + 2;
191			return (unsigned char *)p;
192			break;
193
194		default:	/* an unknown tag */
195len_err:
196			dev_err(&dev->dev, "unknown tag %#x length %d\n",
197				tag, len);
198			break;
199		}
200
201		/* continue to the next tag */
202		if (p[0] & LARGE_TAG)
203			p += len + 3;
204		else
205			p += len + 1;
206	}
207
208	dev_err(&dev->dev, "no end tag in resource structure\n");
209
210	return NULL;
211}
212
213/*
214 * Resource Configuration Options
215 */
216
217static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
218					    unsigned char *p, int size,
219					    unsigned int option_flags)
220{
221	resource_size_t min, max, align, len;
222	unsigned char flags;
223
224	min = ((p[5] << 8) | p[4]) << 8;
225	max = ((p[7] << 8) | p[6]) << 8;
226	align = (p[9] << 8) | p[8];
227	len = ((p[11] << 8) | p[10]) << 8;
228	flags = p[3];
229	pnp_register_mem_resource(dev, option_flags, min, max, align, len,
230				  flags);
231}
232
233static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
234					      unsigned char *p, int size,
235					      unsigned int option_flags)
236{
237	resource_size_t min, max, align, len;
238	unsigned char flags;
239
240	min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
241	max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
242	align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
243	len = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
244	flags = p[3];
245	pnp_register_mem_resource(dev, option_flags, min, max, align, len,
246				  flags);
247}
248
249static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
250						    unsigned char *p, int size,
251						    unsigned int option_flags)
252{
253	resource_size_t base, len;
254	unsigned char flags;
255
256	base = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
257	len = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
258	flags = p[3];
259	pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
260}
261
262static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
263					    unsigned char *p, int size,
264					    unsigned int option_flags)
265{
266	unsigned long bits;
267	pnp_irq_mask_t map;
268	unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
269
270	bits = (p[2] << 8) | p[1];
271
272	bitmap_zero(map.bits, PNP_IRQ_NR);
273	bitmap_copy(map.bits, &bits, 16);
274
275	if (size > 2)
276		flags = p[3];
277
278	pnp_register_irq_resource(dev, option_flags, &map, flags);
279}
280
281static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
282					    unsigned char *p, int size,
283					    unsigned int option_flags)
284{
285	pnp_register_dma_resource(dev, option_flags, p[1], p[2]);
286}
287
288static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
289					     unsigned char *p, int size,
290					     unsigned int option_flags)
291{
292	resource_size_t min, max, align, len;
293	unsigned char flags;
294
295	min = (p[3] << 8) | p[2];
296	max = (p[5] << 8) | p[4];
297	align = p[6];
298	len = p[7];
299	flags = p[1] ? IORESOURCE_IO_16BIT_ADDR : 0;
300	pnp_register_port_resource(dev, option_flags, min, max, align, len,
301				   flags);
302}
303
304static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
305						   unsigned char *p, int size,
306						   unsigned int option_flags)
307{
308	resource_size_t base, len;
309
310	base = (p[2] << 8) | p[1];
311	len = p[3];
312	pnp_register_port_resource(dev, option_flags, base, base, 0, len,
313				   IORESOURCE_IO_FIXED);
314}
315
316static __init unsigned char *
317pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
318				   struct pnp_dev *dev)
319{
320	unsigned int len, tag;
321	int priority;
322	unsigned int option_flags;
323
324	if (!p)
325		return NULL;
326
327	pnp_dbg(&dev->dev, "parse resource options\n");
328	option_flags = 0;
329	while ((char *)p < (char *)end) {
330
331		/* determine the type of tag */
332		if (p[0] & LARGE_TAG) {	/* large tag */
333			len = (p[2] << 8) | p[1];
334			tag = p[0];
335		} else {	/* small tag */
336			len = p[0] & 0x07;
337			tag = ((p[0] >> 3) & 0x0f);
338		}
339
340		switch (tag) {
341
342		case LARGE_TAG_MEM:
343			if (len != 9)
344				goto len_err;
345			pnpbios_parse_mem_option(dev, p, len, option_flags);
346			break;
347
348		case LARGE_TAG_MEM32:
349			if (len != 17)
350				goto len_err;
351			pnpbios_parse_mem32_option(dev, p, len, option_flags);
352			break;
353
354		case LARGE_TAG_FIXEDMEM32:
355			if (len != 9)
356				goto len_err;
357			pnpbios_parse_fixed_mem32_option(dev, p, len,
358							 option_flags);
359			break;
360
361		case SMALL_TAG_IRQ:
362			if (len < 2 || len > 3)
363				goto len_err;
364			pnpbios_parse_irq_option(dev, p, len, option_flags);
365			break;
366
367		case SMALL_TAG_DMA:
368			if (len != 2)
369				goto len_err;
370			pnpbios_parse_dma_option(dev, p, len, option_flags);
371			break;
372
373		case SMALL_TAG_PORT:
374			if (len != 7)
375				goto len_err;
376			pnpbios_parse_port_option(dev, p, len, option_flags);
377			break;
378
379		case SMALL_TAG_VENDOR:
380			/* do nothing */
381			break;
382
383		case SMALL_TAG_FIXEDPORT:
384			if (len != 3)
385				goto len_err;
386			pnpbios_parse_fixed_port_option(dev, p, len,
387							option_flags);
388			break;
389
390		case SMALL_TAG_STARTDEP:
391			if (len > 1)
392				goto len_err;
393			priority = PNP_RES_PRIORITY_ACCEPTABLE;
394			if (len > 0)
395				priority = p[1];
396			option_flags = pnp_new_dependent_set(dev, priority);
397			break;
398
399		case SMALL_TAG_ENDDEP:
400			if (len != 0)
401				goto len_err;
402			option_flags = 0;
403			break;
404
405		case SMALL_TAG_END:
406			return p + 2;
407
408		default:	/* an unknown tag */
409len_err:
410			dev_err(&dev->dev, "unknown tag %#x length %d\n",
411				tag, len);
412			break;
413		}
414
415		/* continue to the next tag */
416		if (p[0] & LARGE_TAG)
417			p += len + 3;
418		else
419			p += len + 1;
420	}
421
422	dev_err(&dev->dev, "no end tag in resource structure\n");
423
424	return NULL;
425}
426
427/*
428 * Compatible Device IDs
429 */
430
431static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
432						   unsigned char *end,
433						   struct pnp_dev *dev)
434{
435	int len, tag;
436	u32 eisa_id;
437	char id[8];
438	struct pnp_id *dev_id;
439
440	if (!p)
441		return NULL;
442
443	while ((char *)p < (char *)end) {
444
445		/* determine the type of tag */
446		if (p[0] & LARGE_TAG) {	/* large tag */
447			len = (p[2] << 8) | p[1];
448			tag = p[0];
449		} else {	/* small tag */
450			len = p[0] & 0x07;
451			tag = ((p[0] >> 3) & 0x0f);
452		}
453
454		switch (tag) {
455
456		case LARGE_TAG_ANSISTR:
457			strncpy(dev->name, p + 3,
458				len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
459			dev->name[len >=
460				  PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
461			break;
462
463		case SMALL_TAG_COMPATDEVID:	/* compatible ID */
464			if (len != 4)
465				goto len_err;
466			eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
467			pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
468			dev_id = pnp_add_id(dev, id);
469			if (!dev_id)
470				return NULL;
471			break;
472
473		case SMALL_TAG_END:
474			p = p + 2;
475			return (unsigned char *)p;
476			break;
477
478		default:	/* an unknown tag */
479len_err:
480			dev_err(&dev->dev, "unknown tag %#x length %d\n",
481				tag, len);
482			break;
483		}
484
485		/* continue to the next tag */
486		if (p[0] & LARGE_TAG)
487			p += len + 3;
488		else
489			p += len + 1;
490	}
491
492	dev_err(&dev->dev, "no end tag in resource structure\n");
493
494	return NULL;
495}
496
497/*
498 * Allocated Resource Encoding
499 */
500
501static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
502			       struct resource *res)
503{
504	unsigned long base;
505	unsigned long len;
506
507	if (pnp_resource_enabled(res)) {
508		base = res->start;
509		len = resource_size(res);
510	} else {
511		base = 0;
512		len = 0;
513	}
514
515	p[4] = (base >> 8) & 0xff;
516	p[5] = ((base >> 8) >> 8) & 0xff;
517	p[6] = (base >> 8) & 0xff;
518	p[7] = ((base >> 8) >> 8) & 0xff;
519	p[10] = (len >> 8) & 0xff;
520	p[11] = ((len >> 8) >> 8) & 0xff;
521
522	pnp_dbg(&dev->dev, "  encode mem %#lx-%#lx\n", base, base + len - 1);
523}
524
525static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
526				 struct resource *res)
527{
528	unsigned long base;
529	unsigned long len;
530
531	if (pnp_resource_enabled(res)) {
532		base = res->start;
533		len = resource_size(res);
534	} else {
535		base = 0;
536		len = 0;
537	}
538
539	p[4] = base & 0xff;
540	p[5] = (base >> 8) & 0xff;
541	p[6] = (base >> 16) & 0xff;
542	p[7] = (base >> 24) & 0xff;
543	p[8] = base & 0xff;
544	p[9] = (base >> 8) & 0xff;
545	p[10] = (base >> 16) & 0xff;
546	p[11] = (base >> 24) & 0xff;
547	p[16] = len & 0xff;
548	p[17] = (len >> 8) & 0xff;
549	p[18] = (len >> 16) & 0xff;
550	p[19] = (len >> 24) & 0xff;
551
552	pnp_dbg(&dev->dev, "  encode mem32 %#lx-%#lx\n", base, base + len - 1);
553}
554
555static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
556				       struct resource *res)
557{
558	unsigned long base;
559	unsigned long len;
560
561	if (pnp_resource_enabled(res)) {
562		base = res->start;
563		len = resource_size(res);
564	} else {
565		base = 0;
566		len = 0;
567	}
568
569	p[4] = base & 0xff;
570	p[5] = (base >> 8) & 0xff;
571	p[6] = (base >> 16) & 0xff;
572	p[7] = (base >> 24) & 0xff;
573	p[8] = len & 0xff;
574	p[9] = (len >> 8) & 0xff;
575	p[10] = (len >> 16) & 0xff;
576	p[11] = (len >> 24) & 0xff;
577
578	pnp_dbg(&dev->dev, "  encode fixed_mem32 %#lx-%#lx\n", base,
579		base + len - 1);
580}
581
582static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
583			       struct resource *res)
584{
585	unsigned long map;
586
587	if (pnp_resource_enabled(res))
588		map = 1 << res->start;
589	else
590		map = 0;
591
592	p[1] = map & 0xff;
593	p[2] = (map >> 8) & 0xff;
594
595	pnp_dbg(&dev->dev, "  encode irq mask %#lx\n", map);
596}
597
598static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
599			       struct resource *res)
600{
601	unsigned long map;
602
603	if (pnp_resource_enabled(res))
604		map = 1 << res->start;
605	else
606		map = 0;
607
608	p[1] = map & 0xff;
609
610	pnp_dbg(&dev->dev, "  encode dma mask %#lx\n", map);
611}
612
613static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
614				struct resource *res)
615{
616	unsigned long base;
617	unsigned long len;
618
619	if (pnp_resource_enabled(res)) {
620		base = res->start;
621		len = resource_size(res);
622	} else {
623		base = 0;
624		len = 0;
625	}
626
627	p[2] = base & 0xff;
628	p[3] = (base >> 8) & 0xff;
629	p[4] = base & 0xff;
630	p[5] = (base >> 8) & 0xff;
631	p[7] = len & 0xff;
632
633	pnp_dbg(&dev->dev, "  encode io %#lx-%#lx\n", base, base + len - 1);
634}
635
636static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
637				      struct resource *res)
638{
639	unsigned long base = res->start;
640	unsigned long len = resource_size(res);
641
642	if (pnp_resource_enabled(res)) {
643		base = res->start;
644		len = resource_size(res);
645	} else {
646		base = 0;
647		len = 0;
648	}
649
650	p[1] = base & 0xff;
651	p[2] = (base >> 8) & 0xff;
652	p[3] = len & 0xff;
653
654	pnp_dbg(&dev->dev, "  encode fixed_io %#lx-%#lx\n", base,
655		base + len - 1);
656}
657
658static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
659								*dev,
660							     unsigned char *p,
661							     unsigned char *end)
662{
663	unsigned int len, tag;
664	int port = 0, irq = 0, dma = 0, mem = 0;
665
666	if (!p)
667		return NULL;
668
669	while ((char *)p < (char *)end) {
670
671		/* determine the type of tag */
672		if (p[0] & LARGE_TAG) {	/* large tag */
673			len = (p[2] << 8) | p[1];
674			tag = p[0];
675		} else {	/* small tag */
676			len = p[0] & 0x07;
677			tag = ((p[0] >> 3) & 0x0f);
678		}
679
680		switch (tag) {
681
682		case LARGE_TAG_MEM:
683			if (len != 9)
684				goto len_err;
685			pnpbios_encode_mem(dev, p,
686				pnp_get_resource(dev, IORESOURCE_MEM, mem));
687			mem++;
688			break;
689
690		case LARGE_TAG_MEM32:
691			if (len != 17)
692				goto len_err;
693			pnpbios_encode_mem32(dev, p,
694				pnp_get_resource(dev, IORESOURCE_MEM, mem));
695			mem++;
696			break;
697
698		case LARGE_TAG_FIXEDMEM32:
699			if (len != 9)
700				goto len_err;
701			pnpbios_encode_fixed_mem32(dev, p,
702				pnp_get_resource(dev, IORESOURCE_MEM, mem));
703			mem++;
704			break;
705
706		case SMALL_TAG_IRQ:
707			if (len < 2 || len > 3)
708				goto len_err;
709			pnpbios_encode_irq(dev, p,
710				pnp_get_resource(dev, IORESOURCE_IRQ, irq));
711			irq++;
712			break;
713
714		case SMALL_TAG_DMA:
715			if (len != 2)
716				goto len_err;
717			pnpbios_encode_dma(dev, p,
718				pnp_get_resource(dev, IORESOURCE_DMA, dma));
719			dma++;
720			break;
721
722		case SMALL_TAG_PORT:
723			if (len != 7)
724				goto len_err;
725			pnpbios_encode_port(dev, p,
726				pnp_get_resource(dev, IORESOURCE_IO, port));
727			port++;
728			break;
729
730		case SMALL_TAG_VENDOR:
731			/* do nothing */
732			break;
733
734		case SMALL_TAG_FIXEDPORT:
735			if (len != 3)
736				goto len_err;
737			pnpbios_encode_fixed_port(dev, p,
738				pnp_get_resource(dev, IORESOURCE_IO, port));
739			port++;
740			break;
741
742		case SMALL_TAG_END:
743			p = p + 2;
744			return (unsigned char *)p;
745			break;
746
747		default:	/* an unknown tag */
748len_err:
749			dev_err(&dev->dev, "unknown tag %#x length %d\n",
750				tag, len);
751			break;
752		}
753
754		/* continue to the next tag */
755		if (p[0] & LARGE_TAG)
756			p += len + 3;
757		else
758			p += len + 1;
759	}
760
761	dev_err(&dev->dev, "no end tag in resource structure\n");
762
763	return NULL;
764}
765
766/*
767 * Core Parsing Functions
768 */
769
770int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
771					struct pnp_bios_node *node)
772{
773	unsigned char *p = (char *)node->data;
774	unsigned char *end = (char *)(node->data + node->size);
775
776	p = pnpbios_parse_allocated_resource_data(dev, p, end);
777	if (!p)
778		return -EIO;
779	p = pnpbios_parse_resource_option_data(p, end, dev);
780	if (!p)
781		return -EIO;
782	p = pnpbios_parse_compatible_ids(p, end, dev);
783	if (!p)
784		return -EIO;
785	return 0;
786}
787
788int pnpbios_read_resources_from_node(struct pnp_dev *dev,
789				     struct pnp_bios_node *node)
790{
791	unsigned char *p = (char *)node->data;
792	unsigned char *end = (char *)(node->data + node->size);
793
794	p = pnpbios_parse_allocated_resource_data(dev, p, end);
795	if (!p)
796		return -EIO;
797	return 0;
798}
799
800int pnpbios_write_resources_to_node(struct pnp_dev *dev,
801				    struct pnp_bios_node *node)
802{
803	unsigned char *p = (char *)node->data;
804	unsigned char *end = (char *)(node->data + node->size);
805
806	p = pnpbios_encode_allocated_resource_data(dev, p, end);
807	if (!p)
808		return -EIO;
809	return 0;
810}