rsparser.c 18 KB

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