rsparser.c 18 KB

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