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 "../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_resource_table *res,
  51. int irq)
  52. {
  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_resource_table *res,
  69. int dma)
  70. {
  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_resource_table *res,
  86. int io, int len)
  87. {
  88. int i = 0;
  89. while (!(res->port_resource[i].flags & IORESOURCE_UNSET)
  90. && i < PNP_MAX_PORT)
  91. i++;
  92. if (i < PNP_MAX_PORT) {
  93. res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
  94. if (len <= 0 || (io + len - 1) >= 0x10003) {
  95. res->port_resource[i].flags |= IORESOURCE_DISABLED;
  96. return;
  97. }
  98. res->port_resource[i].start = (unsigned long)io;
  99. res->port_resource[i].end = (unsigned long)(io + len - 1);
  100. }
  101. }
  102. static void pnpbios_parse_allocated_memresource(struct pnp_resource_table *res,
  103. int mem, int len)
  104. {
  105. int i = 0;
  106. while (!(res->mem_resource[i].flags & IORESOURCE_UNSET)
  107. && i < PNP_MAX_MEM)
  108. i++;
  109. if (i < PNP_MAX_MEM) {
  110. res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
  111. if (len <= 0) {
  112. res->mem_resource[i].flags |= IORESOURCE_DISABLED;
  113. return;
  114. }
  115. res->mem_resource[i].start = (unsigned long)mem;
  116. res->mem_resource[i].end = (unsigned long)(mem + len - 1);
  117. }
  118. }
  119. static unsigned char *pnpbios_parse_allocated_resource_data(unsigned char *p,
  120. unsigned char *end,
  121. struct
  122. pnp_resource_table
  123. *res)
  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(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(res, 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(res, 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(res, 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(res, 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(res, 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(res, 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(res, 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(unsigned char *p, int size,
  230. struct pnp_option *option)
  231. {
  232. struct pnp_mem *mem;
  233. mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
  234. if (!mem)
  235. return;
  236. mem->min = ((p[5] << 8) | p[4]) << 8;
  237. mem->max = ((p[7] << 8) | p[6]) << 8;
  238. mem->align = (p[9] << 8) | p[8];
  239. mem->size = ((p[11] << 8) | p[10]) << 8;
  240. mem->flags = p[3];
  241. pnp_register_mem_resource(option, mem);
  242. }
  243. static __init void pnpbios_parse_mem32_option(unsigned char *p, int size,
  244. struct pnp_option *option)
  245. {
  246. struct pnp_mem *mem;
  247. mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
  248. if (!mem)
  249. return;
  250. mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
  251. mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
  252. mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
  253. mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
  254. mem->flags = p[3];
  255. pnp_register_mem_resource(option, mem);
  256. }
  257. static __init void pnpbios_parse_fixed_mem32_option(unsigned char *p, int size,
  258. struct pnp_option *option)
  259. {
  260. struct pnp_mem *mem;
  261. mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
  262. if (!mem)
  263. return;
  264. mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
  265. mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
  266. mem->align = 0;
  267. mem->flags = p[3];
  268. pnp_register_mem_resource(option, mem);
  269. }
  270. static __init void pnpbios_parse_irq_option(unsigned char *p, int size,
  271. struct pnp_option *option)
  272. {
  273. struct pnp_irq *irq;
  274. unsigned long bits;
  275. irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
  276. if (!irq)
  277. return;
  278. bits = (p[2] << 8) | p[1];
  279. bitmap_copy(irq->map, &bits, 16);
  280. if (size > 2)
  281. irq->flags = p[3];
  282. else
  283. irq->flags = IORESOURCE_IRQ_HIGHEDGE;
  284. pnp_register_irq_resource(option, irq);
  285. }
  286. static __init void pnpbios_parse_dma_option(unsigned char *p, int size,
  287. struct pnp_option *option)
  288. {
  289. struct pnp_dma *dma;
  290. dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
  291. if (!dma)
  292. return;
  293. dma->map = p[1];
  294. dma->flags = p[2];
  295. pnp_register_dma_resource(option, dma);
  296. }
  297. static __init void pnpbios_parse_port_option(unsigned char *p, int size,
  298. struct pnp_option *option)
  299. {
  300. struct pnp_port *port;
  301. port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
  302. if (!port)
  303. return;
  304. port->min = (p[3] << 8) | p[2];
  305. port->max = (p[5] << 8) | p[4];
  306. port->align = p[6];
  307. port->size = p[7];
  308. port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
  309. pnp_register_port_resource(option, port);
  310. }
  311. static __init void pnpbios_parse_fixed_port_option(unsigned char *p, int size,
  312. struct pnp_option *option)
  313. {
  314. struct pnp_port *port;
  315. port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
  316. if (!port)
  317. return;
  318. port->min = port->max = (p[2] << 8) | p[1];
  319. port->size = p[3];
  320. port->align = 0;
  321. port->flags = PNP_PORT_FLAG_FIXED;
  322. pnp_register_port_resource(option, port);
  323. }
  324. static __init unsigned char *
  325. pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
  326. struct pnp_dev *dev)
  327. {
  328. unsigned int len, tag;
  329. int priority = 0;
  330. struct pnp_option *option, *option_independent;
  331. if (!p)
  332. return NULL;
  333. option_independent = option = pnp_register_independent_option(dev);
  334. if (!option)
  335. return NULL;
  336. while ((char *)p < (char *)end) {
  337. /* determine the type of tag */
  338. if (p[0] & LARGE_TAG) { /* large tag */
  339. len = (p[2] << 8) | p[1];
  340. tag = p[0];
  341. } else { /* small tag */
  342. len = p[0] & 0x07;
  343. tag = ((p[0] >> 3) & 0x0f);
  344. }
  345. switch (tag) {
  346. case LARGE_TAG_MEM:
  347. if (len != 9)
  348. goto len_err;
  349. pnpbios_parse_mem_option(p, len, option);
  350. break;
  351. case LARGE_TAG_MEM32:
  352. if (len != 17)
  353. goto len_err;
  354. pnpbios_parse_mem32_option(p, len, option);
  355. break;
  356. case LARGE_TAG_FIXEDMEM32:
  357. if (len != 9)
  358. goto len_err;
  359. pnpbios_parse_fixed_mem32_option(p, len, option);
  360. break;
  361. case SMALL_TAG_IRQ:
  362. if (len < 2 || len > 3)
  363. goto len_err;
  364. pnpbios_parse_irq_option(p, len, option);
  365. break;
  366. case SMALL_TAG_DMA:
  367. if (len != 2)
  368. goto len_err;
  369. pnpbios_parse_dma_option(p, len, option);
  370. break;
  371. case SMALL_TAG_PORT:
  372. if (len != 7)
  373. goto len_err;
  374. pnpbios_parse_port_option(p, len, option);
  375. break;
  376. case SMALL_TAG_VENDOR:
  377. /* do nothing */
  378. break;
  379. case SMALL_TAG_FIXEDPORT:
  380. if (len != 3)
  381. goto len_err;
  382. pnpbios_parse_fixed_port_option(p, len, option);
  383. break;
  384. case SMALL_TAG_STARTDEP:
  385. if (len > 1)
  386. goto len_err;
  387. priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
  388. if (len > 0)
  389. priority = 0x100 | p[1];
  390. option = pnp_register_dependent_option(dev, priority);
  391. if (!option)
  392. return NULL;
  393. break;
  394. case SMALL_TAG_ENDDEP:
  395. if (len != 0)
  396. goto len_err;
  397. if (option_independent == option)
  398. printk(KERN_WARNING
  399. "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
  400. option = option_independent;
  401. break;
  402. case SMALL_TAG_END:
  403. return p + 2;
  404. default: /* an unkown tag */
  405. len_err:
  406. printk(KERN_ERR
  407. "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
  408. tag, len);
  409. break;
  410. }
  411. /* continue to the next tag */
  412. if (p[0] & LARGE_TAG)
  413. p += len + 3;
  414. else
  415. p += len + 1;
  416. }
  417. printk(KERN_ERR
  418. "PnPBIOS: Resource structure does not contain an end tag.\n");
  419. return NULL;
  420. }
  421. /*
  422. * Compatible Device IDs
  423. */
  424. #define HEX(id,a) hex[((id)>>a) & 15]
  425. #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
  426. void pnpid32_to_pnpid(u32 id, char *str)
  427. {
  428. const char *hex = "0123456789abcdef";
  429. id = be32_to_cpu(id);
  430. str[0] = CHAR(id, 26);
  431. str[1] = CHAR(id, 21);
  432. str[2] = CHAR(id, 16);
  433. str[3] = HEX(id, 12);
  434. str[4] = HEX(id, 8);
  435. str[5] = HEX(id, 4);
  436. str[6] = HEX(id, 0);
  437. str[7] = '\0';
  438. }
  439. #undef CHAR
  440. #undef HEX
  441. static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
  442. unsigned char *end,
  443. struct pnp_dev *dev)
  444. {
  445. int len, tag;
  446. char id[8];
  447. struct pnp_id *dev_id;
  448. if (!p)
  449. return NULL;
  450. while ((char *)p < (char *)end) {
  451. /* determine the type of tag */
  452. if (p[0] & LARGE_TAG) { /* large tag */
  453. len = (p[2] << 8) | p[1];
  454. tag = p[0];
  455. } else { /* small tag */
  456. len = p[0] & 0x07;
  457. tag = ((p[0] >> 3) & 0x0f);
  458. }
  459. switch (tag) {
  460. case LARGE_TAG_ANSISTR:
  461. strncpy(dev->name, p + 3,
  462. len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
  463. dev->name[len >=
  464. PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
  465. break;
  466. case SMALL_TAG_COMPATDEVID: /* compatible ID */
  467. if (len != 4)
  468. goto len_err;
  469. pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] <<
  470. 24, id);
  471. dev_id = pnp_add_id(dev, id);
  472. if (!dev_id)
  473. return NULL;
  474. break;
  475. case SMALL_TAG_END:
  476. p = p + 2;
  477. return (unsigned char *)p;
  478. break;
  479. default: /* an unkown tag */
  480. len_err:
  481. printk(KERN_ERR
  482. "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
  483. tag, len);
  484. break;
  485. }
  486. /* continue to the next tag */
  487. if (p[0] & LARGE_TAG)
  488. p += len + 3;
  489. else
  490. p += len + 1;
  491. }
  492. printk(KERN_ERR
  493. "PnPBIOS: Resource structure does not contain an end tag.\n");
  494. return NULL;
  495. }
  496. /*
  497. * Allocated Resource Encoding
  498. */
  499. static void pnpbios_encode_mem(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 >> 8) & 0xff;
  504. p[5] = ((base >> 8) >> 8) & 0xff;
  505. p[6] = (base >> 8) & 0xff;
  506. p[7] = ((base >> 8) >> 8) & 0xff;
  507. p[10] = (len >> 8) & 0xff;
  508. p[11] = ((len >> 8) >> 8) & 0xff;
  509. }
  510. static void pnpbios_encode_mem32(unsigned char *p, struct resource *res)
  511. {
  512. unsigned long base = res->start;
  513. unsigned long len = res->end - res->start + 1;
  514. p[4] = base & 0xff;
  515. p[5] = (base >> 8) & 0xff;
  516. p[6] = (base >> 16) & 0xff;
  517. p[7] = (base >> 24) & 0xff;
  518. p[8] = base & 0xff;
  519. p[9] = (base >> 8) & 0xff;
  520. p[10] = (base >> 16) & 0xff;
  521. p[11] = (base >> 24) & 0xff;
  522. p[16] = len & 0xff;
  523. p[17] = (len >> 8) & 0xff;
  524. p[18] = (len >> 16) & 0xff;
  525. p[19] = (len >> 24) & 0xff;
  526. }
  527. static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource *res)
  528. {
  529. unsigned long base = res->start;
  530. unsigned long len = res->end - res->start + 1;
  531. p[4] = base & 0xff;
  532. p[5] = (base >> 8) & 0xff;
  533. p[6] = (base >> 16) & 0xff;
  534. p[7] = (base >> 24) & 0xff;
  535. p[8] = len & 0xff;
  536. p[9] = (len >> 8) & 0xff;
  537. p[10] = (len >> 16) & 0xff;
  538. p[11] = (len >> 24) & 0xff;
  539. }
  540. static void pnpbios_encode_irq(unsigned char *p, struct resource *res)
  541. {
  542. unsigned long map = 0;
  543. map = 1 << res->start;
  544. p[1] = map & 0xff;
  545. p[2] = (map >> 8) & 0xff;
  546. }
  547. static void pnpbios_encode_dma(unsigned char *p, struct resource *res)
  548. {
  549. unsigned long map = 0;
  550. map = 1 << res->start;
  551. p[1] = map & 0xff;
  552. }
  553. static void pnpbios_encode_port(unsigned char *p, struct resource *res)
  554. {
  555. unsigned long base = res->start;
  556. unsigned long len = res->end - res->start + 1;
  557. p[2] = base & 0xff;
  558. p[3] = (base >> 8) & 0xff;
  559. p[4] = base & 0xff;
  560. p[5] = (base >> 8) & 0xff;
  561. p[7] = len & 0xff;
  562. }
  563. static void pnpbios_encode_fixed_port(unsigned char *p, struct resource *res)
  564. {
  565. unsigned long base = res->start;
  566. unsigned long len = res->end - res->start + 1;
  567. p[1] = base & 0xff;
  568. p[2] = (base >> 8) & 0xff;
  569. p[3] = len & 0xff;
  570. }
  571. static unsigned char *pnpbios_encode_allocated_resource_data(unsigned char *p,
  572. unsigned char *end,
  573. struct
  574. pnp_resource_table
  575. *res)
  576. {
  577. unsigned int len, tag;
  578. int port = 0, irq = 0, dma = 0, mem = 0;
  579. if (!p)
  580. return NULL;
  581. while ((char *)p < (char *)end) {
  582. /* determine the type of tag */
  583. if (p[0] & LARGE_TAG) { /* large tag */
  584. len = (p[2] << 8) | p[1];
  585. tag = p[0];
  586. } else { /* small tag */
  587. len = p[0] & 0x07;
  588. tag = ((p[0] >> 3) & 0x0f);
  589. }
  590. switch (tag) {
  591. case LARGE_TAG_MEM:
  592. if (len != 9)
  593. goto len_err;
  594. pnpbios_encode_mem(p, &res->mem_resource[mem]);
  595. mem++;
  596. break;
  597. case LARGE_TAG_MEM32:
  598. if (len != 17)
  599. goto len_err;
  600. pnpbios_encode_mem32(p, &res->mem_resource[mem]);
  601. mem++;
  602. break;
  603. case LARGE_TAG_FIXEDMEM32:
  604. if (len != 9)
  605. goto len_err;
  606. pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
  607. mem++;
  608. break;
  609. case SMALL_TAG_IRQ:
  610. if (len < 2 || len > 3)
  611. goto len_err;
  612. pnpbios_encode_irq(p, &res->irq_resource[irq]);
  613. irq++;
  614. break;
  615. case SMALL_TAG_DMA:
  616. if (len != 2)
  617. goto len_err;
  618. pnpbios_encode_dma(p, &res->dma_resource[dma]);
  619. dma++;
  620. break;
  621. case SMALL_TAG_PORT:
  622. if (len != 7)
  623. goto len_err;
  624. pnpbios_encode_port(p, &res->port_resource[port]);
  625. port++;
  626. break;
  627. case SMALL_TAG_VENDOR:
  628. /* do nothing */
  629. break;
  630. case SMALL_TAG_FIXEDPORT:
  631. if (len != 3)
  632. goto len_err;
  633. pnpbios_encode_fixed_port(p, &res->port_resource[port]);
  634. port++;
  635. break;
  636. case SMALL_TAG_END:
  637. p = p + 2;
  638. return (unsigned char *)p;
  639. break;
  640. default: /* an unkown tag */
  641. len_err:
  642. printk(KERN_ERR
  643. "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
  644. tag, len);
  645. break;
  646. }
  647. /* continue to the next tag */
  648. if (p[0] & LARGE_TAG)
  649. p += len + 3;
  650. else
  651. p += len + 1;
  652. }
  653. printk(KERN_ERR
  654. "PnPBIOS: Resource structure does not contain an end tag.\n");
  655. return NULL;
  656. }
  657. /*
  658. * Core Parsing Functions
  659. */
  660. int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
  661. 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. }