rsparser.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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/pnpbios.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #ifdef CONFIG_PCI
  11. #include <linux/pci.h>
  12. #else
  13. inline void pcibios_penalize_isa_irq(int irq, int active) {}
  14. #endif /* CONFIG_PCI */
  15. #include "pnpbios.h"
  16. /* standard resource tags */
  17. #define SMALL_TAG_PNPVERNO 0x01
  18. #define SMALL_TAG_LOGDEVID 0x02
  19. #define SMALL_TAG_COMPATDEVID 0x03
  20. #define SMALL_TAG_IRQ 0x04
  21. #define SMALL_TAG_DMA 0x05
  22. #define SMALL_TAG_STARTDEP 0x06
  23. #define SMALL_TAG_ENDDEP 0x07
  24. #define SMALL_TAG_PORT 0x08
  25. #define SMALL_TAG_FIXEDPORT 0x09
  26. #define SMALL_TAG_VENDOR 0x0e
  27. #define SMALL_TAG_END 0x0f
  28. #define LARGE_TAG 0x80
  29. #define LARGE_TAG_MEM 0x81
  30. #define LARGE_TAG_ANSISTR 0x82
  31. #define LARGE_TAG_UNICODESTR 0x83
  32. #define LARGE_TAG_VENDOR 0x84
  33. #define LARGE_TAG_MEM32 0x85
  34. #define LARGE_TAG_FIXEDMEM32 0x86
  35. /*
  36. * Resource Data Stream Format:
  37. *
  38. * Allocated Resources (required)
  39. * end tag ->
  40. * Resource Configuration Options (optional)
  41. * end tag ->
  42. * Compitable Device IDs (optional)
  43. * final end tag ->
  44. */
  45. /*
  46. * Allocated Resources
  47. */
  48. static void
  49. pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
  50. {
  51. int i = 0;
  52. while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
  53. if (i < PNP_MAX_IRQ) {
  54. res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
  55. if (irq == -1) {
  56. res->irq_resource[i].flags |= IORESOURCE_DISABLED;
  57. return;
  58. }
  59. res->irq_resource[i].start =
  60. res->irq_resource[i].end = (unsigned long) irq;
  61. pcibios_penalize_isa_irq(irq, 1);
  62. }
  63. }
  64. static void
  65. pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
  66. {
  67. int i = 0;
  68. while (i < PNP_MAX_DMA &&
  69. !(res->dma_resource[i].flags & IORESOURCE_UNSET))
  70. i++;
  71. if (i < PNP_MAX_DMA) {
  72. res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
  73. if (dma == -1) {
  74. res->dma_resource[i].flags |= IORESOURCE_DISABLED;
  75. return;
  76. }
  77. res->dma_resource[i].start =
  78. res->dma_resource[i].end = (unsigned long) dma;
  79. }
  80. }
  81. static void
  82. pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
  83. {
  84. int i = 0;
  85. while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
  86. if (i < PNP_MAX_PORT) {
  87. res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
  88. if (len <= 0 || (io + len -1) >= 0x10003) {
  89. res->port_resource[i].flags |= IORESOURCE_DISABLED;
  90. return;
  91. }
  92. res->port_resource[i].start = (unsigned long) io;
  93. res->port_resource[i].end = (unsigned long)(io + len - 1);
  94. }
  95. }
  96. static void
  97. pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
  98. {
  99. int i = 0;
  100. while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
  101. if (i < PNP_MAX_MEM) {
  102. res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
  103. if (len <= 0) {
  104. res->mem_resource[i].flags |= IORESOURCE_DISABLED;
  105. return;
  106. }
  107. res->mem_resource[i].start = (unsigned long) mem;
  108. res->mem_resource[i].end = (unsigned long)(mem + len - 1);
  109. }
  110. }
  111. static unsigned char *
  112. pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
  113. {
  114. unsigned int len, tag;
  115. int io, size, mask, i;
  116. if (!p)
  117. return NULL;
  118. /* Blank the resource table values */
  119. pnp_init_resource_table(res);
  120. while ((char *)p < (char *)end) {
  121. /* determine the type of tag */
  122. if (p[0] & LARGE_TAG) { /* large tag */
  123. len = (p[2] << 8) | p[1];
  124. tag = p[0];
  125. } else { /* small tag */
  126. len = p[0] & 0x07;
  127. tag = ((p[0]>>3) & 0x0f);
  128. }
  129. switch (tag) {
  130. case LARGE_TAG_MEM:
  131. if (len != 9)
  132. goto len_err;
  133. io = *(short *) &p[4];
  134. size = *(short *) &p[10];
  135. pnpbios_parse_allocated_memresource(res, io, size);
  136. break;
  137. case LARGE_TAG_ANSISTR:
  138. /* ignore this for now */
  139. break;
  140. case LARGE_TAG_VENDOR:
  141. /* do nothing */
  142. break;
  143. case LARGE_TAG_MEM32:
  144. if (len != 17)
  145. goto len_err;
  146. io = *(int *) &p[4];
  147. size = *(int *) &p[16];
  148. pnpbios_parse_allocated_memresource(res, io, size);
  149. break;
  150. case LARGE_TAG_FIXEDMEM32:
  151. if (len != 9)
  152. goto len_err;
  153. io = *(int *) &p[4];
  154. size = *(int *) &p[8];
  155. pnpbios_parse_allocated_memresource(res, io, size);
  156. break;
  157. case SMALL_TAG_IRQ:
  158. if (len < 2 || len > 3)
  159. goto len_err;
  160. io = -1;
  161. mask= p[1] + p[2]*256;
  162. for (i=0;i<16;i++, mask=mask>>1)
  163. if(mask & 0x01) io=i;
  164. pnpbios_parse_allocated_irqresource(res, io);
  165. break;
  166. case SMALL_TAG_DMA:
  167. if (len != 2)
  168. goto len_err;
  169. io = -1;
  170. mask = p[1];
  171. for (i=0;i<8;i++, mask = mask>>1)
  172. if(mask & 0x01) io=i;
  173. pnpbios_parse_allocated_dmaresource(res, io);
  174. break;
  175. case SMALL_TAG_PORT:
  176. if (len != 7)
  177. goto len_err;
  178. io = p[2] + p[3] *256;
  179. size = p[7];
  180. pnpbios_parse_allocated_ioresource(res, io, size);
  181. break;
  182. case SMALL_TAG_VENDOR:
  183. /* do nothing */
  184. break;
  185. case SMALL_TAG_FIXEDPORT:
  186. if (len != 3)
  187. goto len_err;
  188. io = p[1] + p[2] * 256;
  189. size = p[3];
  190. pnpbios_parse_allocated_ioresource(res, io, size);
  191. break;
  192. case SMALL_TAG_END:
  193. p = p + 2;
  194. return (unsigned char *)p;
  195. break;
  196. default: /* an unkown tag */
  197. len_err:
  198. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  199. break;
  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. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  208. return NULL;
  209. }
  210. /*
  211. * Resource Configuration Options
  212. */
  213. static void
  214. pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
  215. {
  216. struct pnp_mem * mem;
  217. mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
  218. if (!mem)
  219. return;
  220. mem->min = ((p[5] << 8) | p[4]) << 8;
  221. mem->max = ((p[7] << 8) | p[6]) << 8;
  222. mem->align = (p[9] << 8) | p[8];
  223. mem->size = ((p[11] << 8) | p[10]) << 8;
  224. mem->flags = p[3];
  225. pnp_register_mem_resource(option,mem);
  226. return;
  227. }
  228. static void
  229. pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
  230. {
  231. struct pnp_mem * mem;
  232. mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
  233. if (!mem)
  234. return;
  235. mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
  236. mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
  237. mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
  238. mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
  239. mem->flags = p[3];
  240. pnp_register_mem_resource(option,mem);
  241. return;
  242. }
  243. static void
  244. pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
  245. {
  246. struct pnp_mem * mem;
  247. mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
  248. if (!mem)
  249. return;
  250. mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
  251. mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
  252. mem->align = 0;
  253. mem->flags = p[3];
  254. pnp_register_mem_resource(option,mem);
  255. return;
  256. }
  257. static void
  258. pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
  259. {
  260. struct pnp_irq * irq;
  261. unsigned long bits;
  262. irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
  263. if (!irq)
  264. return;
  265. bits = (p[2] << 8) | p[1];
  266. bitmap_copy(irq->map, &bits, 16);
  267. if (size > 2)
  268. irq->flags = p[3];
  269. else
  270. irq->flags = IORESOURCE_IRQ_HIGHEDGE;
  271. pnp_register_irq_resource(option,irq);
  272. return;
  273. }
  274. static void
  275. pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
  276. {
  277. struct pnp_dma * dma;
  278. dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL);
  279. if (!dma)
  280. return;
  281. dma->map = p[1];
  282. dma->flags = p[2];
  283. pnp_register_dma_resource(option,dma);
  284. return;
  285. }
  286. static void
  287. pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
  288. {
  289. struct pnp_port * port;
  290. port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
  291. if (!port)
  292. return;
  293. port->min = (p[3] << 8) | p[2];
  294. port->max = (p[5] << 8) | p[4];
  295. port->align = p[6];
  296. port->size = p[7];
  297. port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
  298. pnp_register_port_resource(option,port);
  299. return;
  300. }
  301. static void
  302. pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
  303. {
  304. struct pnp_port * port;
  305. port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
  306. if (!port)
  307. return;
  308. port->min = port->max = (p[2] << 8) | p[1];
  309. port->size = p[3];
  310. port->align = 0;
  311. port->flags = PNP_PORT_FLAG_FIXED;
  312. pnp_register_port_resource(option,port);
  313. return;
  314. }
  315. static unsigned char *
  316. pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
  317. {
  318. unsigned int len, tag;
  319. int priority = 0;
  320. struct pnp_option *option, *option_independent;
  321. if (!p)
  322. return NULL;
  323. option_independent = option = pnp_register_independent_option(dev);
  324. if (!option)
  325. return NULL;
  326. while ((char *)p < (char *)end) {
  327. /* determine the type of tag */
  328. if (p[0] & LARGE_TAG) { /* large tag */
  329. len = (p[2] << 8) | p[1];
  330. tag = p[0];
  331. } else { /* small tag */
  332. len = p[0] & 0x07;
  333. tag = ((p[0]>>3) & 0x0f);
  334. }
  335. switch (tag) {
  336. case LARGE_TAG_MEM:
  337. if (len != 9)
  338. goto len_err;
  339. pnpbios_parse_mem_option(p, len, option);
  340. break;
  341. case LARGE_TAG_MEM32:
  342. if (len != 17)
  343. goto len_err;
  344. pnpbios_parse_mem32_option(p, len, option);
  345. break;
  346. case LARGE_TAG_FIXEDMEM32:
  347. if (len != 9)
  348. goto len_err;
  349. pnpbios_parse_fixed_mem32_option(p, len, option);
  350. break;
  351. case SMALL_TAG_IRQ:
  352. if (len < 2 || len > 3)
  353. goto len_err;
  354. pnpbios_parse_irq_option(p, len, option);
  355. break;
  356. case SMALL_TAG_DMA:
  357. if (len != 2)
  358. goto len_err;
  359. pnpbios_parse_dma_option(p, len, option);
  360. break;
  361. case SMALL_TAG_PORT:
  362. if (len != 7)
  363. goto len_err;
  364. pnpbios_parse_port_option(p, len, option);
  365. break;
  366. case SMALL_TAG_VENDOR:
  367. /* do nothing */
  368. break;
  369. case SMALL_TAG_FIXEDPORT:
  370. if (len != 3)
  371. goto len_err;
  372. pnpbios_parse_fixed_port_option(p, len, option);
  373. break;
  374. case SMALL_TAG_STARTDEP:
  375. if (len > 1)
  376. goto len_err;
  377. priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
  378. if (len > 0)
  379. priority = 0x100 | p[1];
  380. option = pnp_register_dependent_option(dev, priority);
  381. if (!option)
  382. return NULL;
  383. break;
  384. case SMALL_TAG_ENDDEP:
  385. if (len != 0)
  386. goto len_err;
  387. if (option_independent == option)
  388. printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
  389. option = option_independent;
  390. break;
  391. case SMALL_TAG_END:
  392. return p + 2;
  393. default: /* an unkown tag */
  394. len_err:
  395. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  396. break;
  397. }
  398. /* continue to the next tag */
  399. if (p[0] & LARGE_TAG)
  400. p += len + 3;
  401. else
  402. p += len + 1;
  403. }
  404. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  405. return NULL;
  406. }
  407. /*
  408. * Compatible Device IDs
  409. */
  410. #define HEX(id,a) hex[((id)>>a) & 15]
  411. #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
  412. //
  413. void pnpid32_to_pnpid(u32 id, char *str)
  414. {
  415. const char *hex = "0123456789abcdef";
  416. id = be32_to_cpu(id);
  417. str[0] = CHAR(id, 26);
  418. str[1] = CHAR(id, 21);
  419. str[2] = CHAR(id,16);
  420. str[3] = HEX(id, 12);
  421. str[4] = HEX(id, 8);
  422. str[5] = HEX(id, 4);
  423. str[6] = HEX(id, 0);
  424. str[7] = '\0';
  425. return;
  426. }
  427. //
  428. #undef CHAR
  429. #undef HEX
  430. static unsigned char *
  431. pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
  432. {
  433. int len, tag;
  434. char id[8];
  435. struct pnp_id *dev_id;
  436. if (!p)
  437. return NULL;
  438. while ((char *)p < (char *)end) {
  439. /* determine the type of tag */
  440. if (p[0] & LARGE_TAG) { /* large tag */
  441. len = (p[2] << 8) | p[1];
  442. tag = p[0];
  443. } else { /* small tag */
  444. len = p[0] & 0x07;
  445. tag = ((p[0]>>3) & 0x0f);
  446. }
  447. switch (tag) {
  448. case LARGE_TAG_ANSISTR:
  449. strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
  450. dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
  451. break;
  452. case SMALL_TAG_COMPATDEVID: /* compatible ID */
  453. if (len != 4)
  454. goto len_err;
  455. dev_id = kcalloc(1, sizeof (struct pnp_id), GFP_KERNEL);
  456. if (!dev_id)
  457. return NULL;
  458. memset(dev_id, 0, sizeof(struct pnp_id));
  459. pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
  460. memcpy(&dev_id->id, id, 7);
  461. pnp_add_id(dev_id, dev);
  462. break;
  463. case SMALL_TAG_END:
  464. p = p + 2;
  465. return (unsigned char *)p;
  466. break;
  467. default: /* an unkown tag */
  468. len_err:
  469. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  470. break;
  471. }
  472. /* continue to the next tag */
  473. if (p[0] & LARGE_TAG)
  474. p += len + 3;
  475. else
  476. p += len + 1;
  477. }
  478. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  479. return NULL;
  480. }
  481. /*
  482. * Allocated Resource Encoding
  483. */
  484. static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
  485. {
  486. unsigned long base = res->start;
  487. unsigned long len = res->end - res->start + 1;
  488. p[4] = (base >> 8) & 0xff;
  489. p[5] = ((base >> 8) >> 8) & 0xff;
  490. p[6] = (base >> 8) & 0xff;
  491. p[7] = ((base >> 8) >> 8) & 0xff;
  492. p[10] = (len >> 8) & 0xff;
  493. p[11] = ((len >> 8) >> 8) & 0xff;
  494. return;
  495. }
  496. static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
  497. {
  498. unsigned long base = res->start;
  499. unsigned long len = res->end - res->start + 1;
  500. p[4] = base & 0xff;
  501. p[5] = (base >> 8) & 0xff;
  502. p[6] = (base >> 16) & 0xff;
  503. p[7] = (base >> 24) & 0xff;
  504. p[8] = base & 0xff;
  505. p[9] = (base >> 8) & 0xff;
  506. p[10] = (base >> 16) & 0xff;
  507. p[11] = (base >> 24) & 0xff;
  508. p[16] = len & 0xff;
  509. p[17] = (len >> 8) & 0xff;
  510. p[18] = (len >> 16) & 0xff;
  511. p[19] = (len >> 24) & 0xff;
  512. return;
  513. }
  514. static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
  515. { unsigned long base = res->start;
  516. unsigned long len = res->end - res->start + 1;
  517. p[4] = base & 0xff;
  518. p[5] = (base >> 8) & 0xff;
  519. p[6] = (base >> 16) & 0xff;
  520. p[7] = (base >> 24) & 0xff;
  521. p[8] = len & 0xff;
  522. p[9] = (len >> 8) & 0xff;
  523. p[10] = (len >> 16) & 0xff;
  524. p[11] = (len >> 24) & 0xff;
  525. return;
  526. }
  527. static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
  528. {
  529. unsigned long map = 0;
  530. map = 1 << res->start;
  531. p[1] = map & 0xff;
  532. p[2] = (map >> 8) & 0xff;
  533. return;
  534. }
  535. static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
  536. {
  537. unsigned long map = 0;
  538. map = 1 << res->start;
  539. p[1] = map & 0xff;
  540. return;
  541. }
  542. static void pnpbios_encode_port(unsigned char *p, struct resource * res)
  543. {
  544. unsigned long base = res->start;
  545. unsigned long len = res->end - res->start + 1;
  546. p[2] = base & 0xff;
  547. p[3] = (base >> 8) & 0xff;
  548. p[4] = base & 0xff;
  549. p[5] = (base >> 8) & 0xff;
  550. p[7] = len & 0xff;
  551. return;
  552. }
  553. static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
  554. {
  555. unsigned long base = res->start;
  556. unsigned long len = res->end - res->start + 1;
  557. p[1] = base & 0xff;
  558. p[2] = (base >> 8) & 0xff;
  559. p[3] = len & 0xff;
  560. return;
  561. }
  562. static unsigned char *
  563. pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
  564. {
  565. unsigned int len, tag;
  566. int port = 0, irq = 0, dma = 0, mem = 0;
  567. if (!p)
  568. return NULL;
  569. while ((char *)p < (char *)end) {
  570. /* determine the type of tag */
  571. if (p[0] & LARGE_TAG) { /* large tag */
  572. len = (p[2] << 8) | p[1];
  573. tag = p[0];
  574. } else { /* small tag */
  575. len = p[0] & 0x07;
  576. tag = ((p[0]>>3) & 0x0f);
  577. }
  578. switch (tag) {
  579. case LARGE_TAG_MEM:
  580. if (len != 9)
  581. goto len_err;
  582. pnpbios_encode_mem(p, &res->mem_resource[mem]);
  583. mem++;
  584. break;
  585. case LARGE_TAG_MEM32:
  586. if (len != 17)
  587. goto len_err;
  588. pnpbios_encode_mem32(p, &res->mem_resource[mem]);
  589. mem++;
  590. break;
  591. case LARGE_TAG_FIXEDMEM32:
  592. if (len != 9)
  593. goto len_err;
  594. pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
  595. mem++;
  596. break;
  597. case SMALL_TAG_IRQ:
  598. if (len < 2 || len > 3)
  599. goto len_err;
  600. pnpbios_encode_irq(p, &res->irq_resource[irq]);
  601. irq++;
  602. break;
  603. case SMALL_TAG_DMA:
  604. if (len != 2)
  605. goto len_err;
  606. pnpbios_encode_dma(p, &res->dma_resource[dma]);
  607. dma++;
  608. break;
  609. case SMALL_TAG_PORT:
  610. if (len != 7)
  611. goto len_err;
  612. pnpbios_encode_port(p, &res->port_resource[port]);
  613. port++;
  614. break;
  615. case SMALL_TAG_VENDOR:
  616. /* do nothing */
  617. break;
  618. case SMALL_TAG_FIXEDPORT:
  619. if (len != 3)
  620. goto len_err;
  621. pnpbios_encode_fixed_port(p, &res->port_resource[port]);
  622. port++;
  623. break;
  624. case SMALL_TAG_END:
  625. p = p + 2;
  626. return (unsigned char *)p;
  627. break;
  628. default: /* an unkown tag */
  629. len_err:
  630. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  631. break;
  632. }
  633. /* continue to the next tag */
  634. if (p[0] & LARGE_TAG)
  635. p += len + 3;
  636. else
  637. p += len + 1;
  638. }
  639. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  640. return NULL;
  641. }
  642. /*
  643. * Core Parsing Functions
  644. */
  645. int
  646. pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
  647. {
  648. unsigned char * p = (char *)node->data;
  649. unsigned char * end = (char *)(node->data + node->size);
  650. p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
  651. if (!p)
  652. return -EIO;
  653. p = pnpbios_parse_resource_option_data(p,end,dev);
  654. if (!p)
  655. return -EIO;
  656. p = pnpbios_parse_compatible_ids(p,end,dev);
  657. if (!p)
  658. return -EIO;
  659. return 0;
  660. }
  661. int
  662. pnpbios_read_resources_from_node(struct pnp_resource_table *res,
  663. struct pnp_bios_node * node)
  664. {
  665. unsigned char * p = (char *)node->data;
  666. unsigned char * end = (char *)(node->data + node->size);
  667. p = pnpbios_parse_allocated_resource_data(p,end,res);
  668. if (!p)
  669. return -EIO;
  670. return 0;
  671. }
  672. int
  673. pnpbios_write_resources_to_node(struct pnp_resource_table *res,
  674. struct pnp_bios_node * node)
  675. {
  676. unsigned char * p = (char *)node->data;
  677. unsigned char * end = (char *)(node->data + node->size);
  678. p = pnpbios_encode_allocated_resource_data(p,end,res);
  679. if (!p)
  680. return -EIO;
  681. return 0;
  682. }