rsparser.c 18 KB

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