rsparser.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. if (option_independent != option)
  394. printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
  395. p = p + 2;
  396. return (unsigned char *)p;
  397. break;
  398. default: /* an unkown tag */
  399. len_err:
  400. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  401. break;
  402. }
  403. /* continue to the next tag */
  404. if (p[0] & LARGE_TAG)
  405. p += len + 3;
  406. else
  407. p += len + 1;
  408. }
  409. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  410. return NULL;
  411. }
  412. /*
  413. * Compatible Device IDs
  414. */
  415. #define HEX(id,a) hex[((id)>>a) & 15]
  416. #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
  417. //
  418. void pnpid32_to_pnpid(u32 id, char *str)
  419. {
  420. const char *hex = "0123456789abcdef";
  421. id = be32_to_cpu(id);
  422. str[0] = CHAR(id, 26);
  423. str[1] = CHAR(id, 21);
  424. str[2] = CHAR(id,16);
  425. str[3] = HEX(id, 12);
  426. str[4] = HEX(id, 8);
  427. str[5] = HEX(id, 4);
  428. str[6] = HEX(id, 0);
  429. str[7] = '\0';
  430. return;
  431. }
  432. //
  433. #undef CHAR
  434. #undef HEX
  435. static unsigned char *
  436. pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
  437. {
  438. int len, tag;
  439. char id[8];
  440. struct pnp_id *dev_id;
  441. if (!p)
  442. return NULL;
  443. while ((char *)p < (char *)end) {
  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. switch (tag) {
  453. case LARGE_TAG_ANSISTR:
  454. strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
  455. dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
  456. break;
  457. case SMALL_TAG_COMPATDEVID: /* compatible ID */
  458. if (len != 4)
  459. goto len_err;
  460. dev_id = kcalloc(1, sizeof (struct pnp_id), GFP_KERNEL);
  461. if (!dev_id)
  462. return NULL;
  463. memset(dev_id, 0, sizeof(struct pnp_id));
  464. pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
  465. memcpy(&dev_id->id, id, 7);
  466. pnp_add_id(dev_id, dev);
  467. break;
  468. case SMALL_TAG_END:
  469. p = p + 2;
  470. return (unsigned char *)p;
  471. break;
  472. default: /* an unkown tag */
  473. len_err:
  474. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  475. break;
  476. }
  477. /* continue to the next tag */
  478. if (p[0] & LARGE_TAG)
  479. p += len + 3;
  480. else
  481. p += len + 1;
  482. }
  483. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  484. return NULL;
  485. }
  486. /*
  487. * Allocated Resource Encoding
  488. */
  489. static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
  490. {
  491. unsigned long base = res->start;
  492. unsigned long len = res->end - res->start + 1;
  493. p[4] = (base >> 8) & 0xff;
  494. p[5] = ((base >> 8) >> 8) & 0xff;
  495. p[6] = (base >> 8) & 0xff;
  496. p[7] = ((base >> 8) >> 8) & 0xff;
  497. p[10] = (len >> 8) & 0xff;
  498. p[11] = ((len >> 8) >> 8) & 0xff;
  499. return;
  500. }
  501. static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
  502. {
  503. unsigned long base = res->start;
  504. unsigned long len = res->end - res->start + 1;
  505. p[4] = base & 0xff;
  506. p[5] = (base >> 8) & 0xff;
  507. p[6] = (base >> 16) & 0xff;
  508. p[7] = (base >> 24) & 0xff;
  509. p[8] = base & 0xff;
  510. p[9] = (base >> 8) & 0xff;
  511. p[10] = (base >> 16) & 0xff;
  512. p[11] = (base >> 24) & 0xff;
  513. p[16] = len & 0xff;
  514. p[17] = (len >> 8) & 0xff;
  515. p[18] = (len >> 16) & 0xff;
  516. p[19] = (len >> 24) & 0xff;
  517. return;
  518. }
  519. static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
  520. { unsigned long base = res->start;
  521. unsigned long len = res->end - res->start + 1;
  522. p[4] = base & 0xff;
  523. p[5] = (base >> 8) & 0xff;
  524. p[6] = (base >> 16) & 0xff;
  525. p[7] = (base >> 24) & 0xff;
  526. p[8] = len & 0xff;
  527. p[9] = (len >> 8) & 0xff;
  528. p[10] = (len >> 16) & 0xff;
  529. p[11] = (len >> 24) & 0xff;
  530. return;
  531. }
  532. static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
  533. {
  534. unsigned long map = 0;
  535. map = 1 << res->start;
  536. p[1] = map & 0xff;
  537. p[2] = (map >> 8) & 0xff;
  538. return;
  539. }
  540. static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
  541. {
  542. unsigned long map = 0;
  543. map = 1 << res->start;
  544. p[1] = map & 0xff;
  545. return;
  546. }
  547. static void pnpbios_encode_port(unsigned char *p, struct resource * res)
  548. {
  549. unsigned long base = res->start;
  550. unsigned long len = res->end - res->start + 1;
  551. p[2] = base & 0xff;
  552. p[3] = (base >> 8) & 0xff;
  553. p[4] = base & 0xff;
  554. p[5] = (base >> 8) & 0xff;
  555. p[7] = len & 0xff;
  556. return;
  557. }
  558. static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
  559. {
  560. unsigned long base = res->start;
  561. unsigned long len = res->end - res->start + 1;
  562. p[1] = base & 0xff;
  563. p[2] = (base >> 8) & 0xff;
  564. p[3] = len & 0xff;
  565. return;
  566. }
  567. static unsigned char *
  568. pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
  569. {
  570. unsigned int len, tag;
  571. int port = 0, irq = 0, dma = 0, mem = 0;
  572. if (!p)
  573. return NULL;
  574. while ((char *)p < (char *)end) {
  575. /* determine the type of tag */
  576. if (p[0] & LARGE_TAG) { /* large tag */
  577. len = (p[2] << 8) | p[1];
  578. tag = p[0];
  579. } else { /* small tag */
  580. len = p[0] & 0x07;
  581. tag = ((p[0]>>3) & 0x0f);
  582. }
  583. switch (tag) {
  584. case LARGE_TAG_MEM:
  585. if (len != 9)
  586. goto len_err;
  587. pnpbios_encode_mem(p, &res->mem_resource[mem]);
  588. mem++;
  589. break;
  590. case LARGE_TAG_MEM32:
  591. if (len != 17)
  592. goto len_err;
  593. pnpbios_encode_mem32(p, &res->mem_resource[mem]);
  594. mem++;
  595. break;
  596. case LARGE_TAG_FIXEDMEM32:
  597. if (len != 9)
  598. goto len_err;
  599. pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
  600. mem++;
  601. break;
  602. case SMALL_TAG_IRQ:
  603. if (len < 2 || len > 3)
  604. goto len_err;
  605. pnpbios_encode_irq(p, &res->irq_resource[irq]);
  606. irq++;
  607. break;
  608. case SMALL_TAG_DMA:
  609. if (len != 2)
  610. goto len_err;
  611. pnpbios_encode_dma(p, &res->dma_resource[dma]);
  612. dma++;
  613. break;
  614. case SMALL_TAG_PORT:
  615. if (len != 7)
  616. goto len_err;
  617. pnpbios_encode_port(p, &res->port_resource[port]);
  618. port++;
  619. break;
  620. case SMALL_TAG_VENDOR:
  621. /* do nothing */
  622. break;
  623. case SMALL_TAG_FIXEDPORT:
  624. if (len != 3)
  625. goto len_err;
  626. pnpbios_encode_fixed_port(p, &res->port_resource[port]);
  627. port++;
  628. break;
  629. case SMALL_TAG_END:
  630. p = p + 2;
  631. return (unsigned char *)p;
  632. break;
  633. default: /* an unkown tag */
  634. len_err:
  635. printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
  636. break;
  637. }
  638. /* continue to the next tag */
  639. if (p[0] & LARGE_TAG)
  640. p += len + 3;
  641. else
  642. p += len + 1;
  643. }
  644. printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
  645. return NULL;
  646. }
  647. /*
  648. * Core Parsing Functions
  649. */
  650. int
  651. pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
  652. {
  653. unsigned char * p = (char *)node->data;
  654. unsigned char * end = (char *)(node->data + node->size);
  655. p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
  656. if (!p)
  657. return -EIO;
  658. p = pnpbios_parse_resource_option_data(p,end,dev);
  659. if (!p)
  660. return -EIO;
  661. p = pnpbios_parse_compatible_ids(p,end,dev);
  662. if (!p)
  663. return -EIO;
  664. return 0;
  665. }
  666. int
  667. pnpbios_read_resources_from_node(struct pnp_resource_table *res,
  668. struct pnp_bios_node * node)
  669. {
  670. unsigned char * p = (char *)node->data;
  671. unsigned char * end = (char *)(node->data + node->size);
  672. p = pnpbios_parse_allocated_resource_data(p,end,res);
  673. if (!p)
  674. return -EIO;
  675. return 0;
  676. }
  677. int
  678. pnpbios_write_resources_to_node(struct pnp_resource_table *res,
  679. struct pnp_bios_node * node)
  680. {
  681. unsigned char * p = (char *)node->data;
  682. unsigned char * end = (char *)(node->data + node->size);
  683. p = pnpbios_encode_allocated_resource_data(p,end,res);
  684. if (!p)
  685. return -EIO;
  686. return 0;
  687. }