rsparser.c 18 KB

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