rsparser.c 17 KB

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