rsparser.c 18 KB

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