rsparser.c 17 KB

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