rsparser.c 19 KB

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