cistpl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /*
  2. * cistpl.c -- 16-bit PCMCIA Card Information Structure parser
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * The initial developer of the original code is David A. Hinds
  9. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  10. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  11. *
  12. * (C) 1999 David A. Hinds
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/major.h>
  19. #include <linux/errno.h>
  20. #include <linux/timer.h>
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <linux/pci.h>
  24. #include <linux/ioport.h>
  25. #include <linux/io.h>
  26. #include <asm/byteorder.h>
  27. #include <asm/unaligned.h>
  28. #include <pcmcia/cs_types.h>
  29. #include <pcmcia/ss.h>
  30. #include <pcmcia/cs.h>
  31. #include <pcmcia/cisreg.h>
  32. #include <pcmcia/cistpl.h>
  33. #include "cs_internal.h"
  34. static const u_char mantissa[] = {
  35. 10, 12, 13, 15, 20, 25, 30, 35,
  36. 40, 45, 50, 55, 60, 70, 80, 90
  37. };
  38. static const u_int exponent[] = {
  39. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
  40. };
  41. /* Convert an extended speed byte to a time in nanoseconds */
  42. #define SPEED_CVT(v) \
  43. (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10)
  44. /* Convert a power byte to a current in 0.1 microamps */
  45. #define POWER_CVT(v) \
  46. (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10)
  47. #define POWER_SCALE(v) (exponent[(v)&7])
  48. /* Upper limit on reasonable # of tuples */
  49. #define MAX_TUPLES 200
  50. /*====================================================================*/
  51. /* Parameters that can be set with 'insmod' */
  52. /* 16-bit CIS? */
  53. static int cis_width;
  54. module_param(cis_width, int, 0444);
  55. void release_cis_mem(struct pcmcia_socket *s)
  56. {
  57. if (s->cis_mem.flags & MAP_ACTIVE) {
  58. s->cis_mem.flags &= ~MAP_ACTIVE;
  59. s->ops->set_mem_map(s, &s->cis_mem);
  60. if (s->cis_mem.res) {
  61. release_resource(s->cis_mem.res);
  62. kfree(s->cis_mem.res);
  63. s->cis_mem.res = NULL;
  64. }
  65. iounmap(s->cis_virt);
  66. s->cis_virt = NULL;
  67. }
  68. }
  69. EXPORT_SYMBOL(release_cis_mem);
  70. /*
  71. * Map the card memory at "card_offset" into virtual space.
  72. * If flags & MAP_ATTRIB, map the attribute space, otherwise
  73. * map the memory space.
  74. */
  75. static void __iomem *
  76. set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
  77. {
  78. pccard_mem_map *mem = &s->cis_mem;
  79. int ret;
  80. if (!(s->features & SS_CAP_STATIC_MAP) && (mem->res == NULL)) {
  81. mem->res = pcmcia_find_mem_region(0, s->map_size, s->map_size, 0, s);
  82. if (mem->res == NULL) {
  83. dev_printk(KERN_NOTICE, &s->dev,
  84. "cs: unable to map card memory!\n");
  85. return NULL;
  86. }
  87. s->cis_virt = NULL;
  88. }
  89. if (!(s->features & SS_CAP_STATIC_MAP) && (!s->cis_virt))
  90. s->cis_virt = ioremap(mem->res->start, s->map_size);
  91. mem->card_start = card_offset;
  92. mem->flags = flags;
  93. ret = s->ops->set_mem_map(s, mem);
  94. if (ret) {
  95. iounmap(s->cis_virt);
  96. s->cis_virt = NULL;
  97. return NULL;
  98. }
  99. if (s->features & SS_CAP_STATIC_MAP) {
  100. if (s->cis_virt)
  101. iounmap(s->cis_virt);
  102. s->cis_virt = ioremap(mem->static_start, s->map_size);
  103. }
  104. return s->cis_virt;
  105. }
  106. /*======================================================================
  107. Low-level functions to read and write CIS memory. I think the
  108. write routine is only useful for writing one-byte registers.
  109. ======================================================================*/
  110. /* Bits in attr field */
  111. #define IS_ATTR 1
  112. #define IS_INDIRECT 8
  113. int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
  114. u_int len, void *ptr)
  115. {
  116. void __iomem *sys, *end;
  117. unsigned char *buf = ptr;
  118. dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
  119. if (attr & IS_INDIRECT) {
  120. /* Indirect accesses use a bunch of special registers at fixed
  121. locations in common memory */
  122. u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
  123. if (attr & IS_ATTR) {
  124. addr *= 2;
  125. flags = ICTRL0_AUTOINC;
  126. }
  127. sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
  128. if (!sys) {
  129. memset(ptr, 0xff, len);
  130. return -1;
  131. }
  132. writeb(flags, sys+CISREG_ICTRL0);
  133. writeb(addr & 0xff, sys+CISREG_IADDR0);
  134. writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
  135. writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
  136. writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
  137. for ( ; len > 0; len--, buf++)
  138. *buf = readb(sys+CISREG_IDATA0);
  139. } else {
  140. u_int inc = 1, card_offset, flags;
  141. flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
  142. if (attr) {
  143. flags |= MAP_ATTRIB;
  144. inc++;
  145. addr *= 2;
  146. }
  147. card_offset = addr & ~(s->map_size-1);
  148. while (len) {
  149. sys = set_cis_map(s, card_offset, flags);
  150. if (!sys) {
  151. memset(ptr, 0xff, len);
  152. return -1;
  153. }
  154. end = sys + s->map_size;
  155. sys = sys + (addr & (s->map_size-1));
  156. for ( ; len > 0; len--, buf++, sys += inc) {
  157. if (sys == end)
  158. break;
  159. *buf = readb(sys);
  160. }
  161. card_offset += s->map_size;
  162. addr = 0;
  163. }
  164. }
  165. dev_dbg(&s->dev, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
  166. *(u_char *)(ptr+0), *(u_char *)(ptr+1),
  167. *(u_char *)(ptr+2), *(u_char *)(ptr+3));
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(pcmcia_read_cis_mem);
  171. void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
  172. u_int len, void *ptr)
  173. {
  174. void __iomem *sys, *end;
  175. unsigned char *buf = ptr;
  176. dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
  177. if (attr & IS_INDIRECT) {
  178. /* Indirect accesses use a bunch of special registers at fixed
  179. locations in common memory */
  180. u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
  181. if (attr & IS_ATTR) {
  182. addr *= 2;
  183. flags = ICTRL0_AUTOINC;
  184. }
  185. sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
  186. if (!sys)
  187. return; /* FIXME: Error */
  188. writeb(flags, sys+CISREG_ICTRL0);
  189. writeb(addr & 0xff, sys+CISREG_IADDR0);
  190. writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
  191. writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
  192. writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
  193. for ( ; len > 0; len--, buf++)
  194. writeb(*buf, sys+CISREG_IDATA0);
  195. } else {
  196. u_int inc = 1, card_offset, flags;
  197. flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
  198. if (attr & IS_ATTR) {
  199. flags |= MAP_ATTRIB;
  200. inc++;
  201. addr *= 2;
  202. }
  203. card_offset = addr & ~(s->map_size-1);
  204. while (len) {
  205. sys = set_cis_map(s, card_offset, flags);
  206. if (!sys)
  207. return; /* FIXME: error */
  208. end = sys + s->map_size;
  209. sys = sys + (addr & (s->map_size-1));
  210. for ( ; len > 0; len--, buf++, sys += inc) {
  211. if (sys == end)
  212. break;
  213. writeb(*buf, sys);
  214. }
  215. card_offset += s->map_size;
  216. addr = 0;
  217. }
  218. }
  219. }
  220. EXPORT_SYMBOL(pcmcia_write_cis_mem);
  221. /*======================================================================
  222. This is a wrapper around read_cis_mem, with the same interface,
  223. but which caches information, for cards whose CIS may not be
  224. readable all the time.
  225. ======================================================================*/
  226. static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
  227. size_t len, void *ptr)
  228. {
  229. struct cis_cache_entry *cis;
  230. int ret;
  231. if (s->fake_cis) {
  232. if (s->fake_cis_len >= addr+len)
  233. memcpy(ptr, s->fake_cis+addr, len);
  234. else
  235. memset(ptr, 0xff, len);
  236. return;
  237. }
  238. list_for_each_entry(cis, &s->cis_cache, node) {
  239. if (cis->addr == addr && cis->len == len && cis->attr == attr) {
  240. memcpy(ptr, cis->cache, len);
  241. return;
  242. }
  243. }
  244. #ifdef CONFIG_CARDBUS
  245. if (s->state & SOCKET_CARDBUS)
  246. ret = read_cb_mem(s, attr, addr, len, ptr);
  247. else
  248. #endif
  249. ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr);
  250. if (ret == 0) {
  251. /* Copy data into the cache */
  252. cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL);
  253. if (cis) {
  254. cis->addr = addr;
  255. cis->len = len;
  256. cis->attr = attr;
  257. memcpy(cis->cache, ptr, len);
  258. list_add(&cis->node, &s->cis_cache);
  259. }
  260. }
  261. }
  262. static void
  263. remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
  264. {
  265. struct cis_cache_entry *cis;
  266. list_for_each_entry(cis, &s->cis_cache, node)
  267. if (cis->addr == addr && cis->len == len && cis->attr == attr) {
  268. list_del(&cis->node);
  269. kfree(cis);
  270. break;
  271. }
  272. }
  273. /**
  274. * destroy_cis_cache() - destroy the CIS cache
  275. * @s: pcmcia_socket for which CIS cache shall be destroyed
  276. *
  277. * This destroys the CIS cache but keeps any fake CIS alive.
  278. */
  279. void destroy_cis_cache(struct pcmcia_socket *s)
  280. {
  281. struct list_head *l, *n;
  282. struct cis_cache_entry *cis;
  283. list_for_each_safe(l, n, &s->cis_cache) {
  284. cis = list_entry(l, struct cis_cache_entry, node);
  285. list_del(&cis->node);
  286. kfree(cis);
  287. }
  288. }
  289. EXPORT_SYMBOL(destroy_cis_cache);
  290. /*======================================================================
  291. This verifies if the CIS of a card matches what is in the CIS
  292. cache.
  293. ======================================================================*/
  294. int verify_cis_cache(struct pcmcia_socket *s)
  295. {
  296. struct cis_cache_entry *cis;
  297. char *buf;
  298. buf = kmalloc(256, GFP_KERNEL);
  299. if (buf == NULL) {
  300. dev_printk(KERN_WARNING, &s->dev,
  301. "no memory for verifying CIS\n");
  302. return -ENOMEM;
  303. }
  304. list_for_each_entry(cis, &s->cis_cache, node) {
  305. int len = cis->len;
  306. if (len > 256)
  307. len = 256;
  308. #ifdef CONFIG_CARDBUS
  309. if (s->state & SOCKET_CARDBUS)
  310. read_cb_mem(s, cis->attr, cis->addr, len, buf);
  311. else
  312. #endif
  313. pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf);
  314. if (memcmp(buf, cis->cache, len) != 0) {
  315. kfree(buf);
  316. return -1;
  317. }
  318. }
  319. kfree(buf);
  320. return 0;
  321. }
  322. /*======================================================================
  323. For really bad cards, we provide a facility for uploading a
  324. replacement CIS.
  325. ======================================================================*/
  326. int pcmcia_replace_cis(struct pcmcia_socket *s,
  327. const u8 *data, const size_t len)
  328. {
  329. if (len > CISTPL_MAX_CIS_SIZE) {
  330. dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n");
  331. return -EINVAL;
  332. }
  333. kfree(s->fake_cis);
  334. s->fake_cis = kmalloc(len, GFP_KERNEL);
  335. if (s->fake_cis == NULL) {
  336. dev_printk(KERN_WARNING, &s->dev, "no memory to replace CIS\n");
  337. return -ENOMEM;
  338. }
  339. s->fake_cis_len = len;
  340. memcpy(s->fake_cis, data, len);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL(pcmcia_replace_cis);
  344. /*======================================================================
  345. The high-level CIS tuple services
  346. ======================================================================*/
  347. typedef struct tuple_flags {
  348. u_int link_space:4;
  349. u_int has_link:1;
  350. u_int mfc_fn:3;
  351. u_int space:4;
  352. } tuple_flags;
  353. #define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space)
  354. #define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link)
  355. #define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn)
  356. #define SPACE(f) (((tuple_flags *)(&(f)))->space)
  357. int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
  358. {
  359. if (!s)
  360. return -EINVAL;
  361. if (!(s->state & SOCKET_PRESENT))
  362. return -ENODEV;
  363. tuple->TupleLink = tuple->Flags = 0;
  364. #ifdef CONFIG_CARDBUS
  365. if (s->state & SOCKET_CARDBUS) {
  366. struct pci_dev *dev = s->cb_dev;
  367. u_int ptr;
  368. pci_bus_read_config_dword(dev->subordinate, 0, PCI_CARDBUS_CIS, &ptr);
  369. tuple->CISOffset = ptr & ~7;
  370. SPACE(tuple->Flags) = (ptr & 7);
  371. } else
  372. #endif
  373. {
  374. /* Assume presence of a LONGLINK_C to address 0 */
  375. tuple->CISOffset = tuple->LinkOffset = 0;
  376. SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1;
  377. }
  378. if (!(s->state & SOCKET_CARDBUS) && (s->functions > 1) &&
  379. !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
  380. cisdata_t req = tuple->DesiredTuple;
  381. tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
  382. if (pccard_get_next_tuple(s, function, tuple) == 0) {
  383. tuple->DesiredTuple = CISTPL_LINKTARGET;
  384. if (pccard_get_next_tuple(s, function, tuple) != 0)
  385. return -ENOSPC;
  386. } else
  387. tuple->CISOffset = tuple->TupleLink = 0;
  388. tuple->DesiredTuple = req;
  389. }
  390. return pccard_get_next_tuple(s, function, tuple);
  391. }
  392. EXPORT_SYMBOL(pccard_get_first_tuple);
  393. static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
  394. {
  395. u_char link[5];
  396. u_int ofs;
  397. if (MFC_FN(tuple->Flags)) {
  398. /* Get indirect link from the MFC tuple */
  399. read_cis_cache(s, LINK_SPACE(tuple->Flags),
  400. tuple->LinkOffset, 5, link);
  401. ofs = get_unaligned_le32(link + 1);
  402. SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
  403. /* Move to the next indirect link */
  404. tuple->LinkOffset += 5;
  405. MFC_FN(tuple->Flags)--;
  406. } else if (HAS_LINK(tuple->Flags)) {
  407. ofs = tuple->LinkOffset;
  408. SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags);
  409. HAS_LINK(tuple->Flags) = 0;
  410. } else {
  411. return -1;
  412. }
  413. if (!(s->state & SOCKET_CARDBUS) && SPACE(tuple->Flags)) {
  414. /* This is ugly, but a common CIS error is to code the long
  415. link offset incorrectly, so we check the right spot... */
  416. read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
  417. if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
  418. (strncmp(link+2, "CIS", 3) == 0))
  419. return ofs;
  420. remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
  421. /* Then, we try the wrong spot... */
  422. ofs = ofs >> 1;
  423. }
  424. read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
  425. if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
  426. (strncmp(link+2, "CIS", 3) == 0))
  427. return ofs;
  428. remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
  429. return -1;
  430. }
  431. int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
  432. {
  433. u_char link[2], tmp;
  434. int ofs, i, attr;
  435. if (!s)
  436. return -EINVAL;
  437. if (!(s->state & SOCKET_PRESENT))
  438. return -ENODEV;
  439. link[1] = tuple->TupleLink;
  440. ofs = tuple->CISOffset + tuple->TupleLink;
  441. attr = SPACE(tuple->Flags);
  442. for (i = 0; i < MAX_TUPLES; i++) {
  443. if (link[1] == 0xff) {
  444. link[0] = CISTPL_END;
  445. } else {
  446. read_cis_cache(s, attr, ofs, 2, link);
  447. if (link[0] == CISTPL_NULL) {
  448. ofs++; continue;
  449. }
  450. }
  451. /* End of chain? Follow long link if possible */
  452. if (link[0] == CISTPL_END) {
  453. ofs = follow_link(s, tuple);
  454. if (ofs < 0)
  455. return -ENOSPC;
  456. attr = SPACE(tuple->Flags);
  457. read_cis_cache(s, attr, ofs, 2, link);
  458. }
  459. /* Is this a link tuple? Make a note of it */
  460. if ((link[0] == CISTPL_LONGLINK_A) ||
  461. (link[0] == CISTPL_LONGLINK_C) ||
  462. (link[0] == CISTPL_LONGLINK_MFC) ||
  463. (link[0] == CISTPL_LINKTARGET) ||
  464. (link[0] == CISTPL_INDIRECT) ||
  465. (link[0] == CISTPL_NO_LINK)) {
  466. switch (link[0]) {
  467. case CISTPL_LONGLINK_A:
  468. HAS_LINK(tuple->Flags) = 1;
  469. LINK_SPACE(tuple->Flags) = attr | IS_ATTR;
  470. read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
  471. break;
  472. case CISTPL_LONGLINK_C:
  473. HAS_LINK(tuple->Flags) = 1;
  474. LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR;
  475. read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
  476. break;
  477. case CISTPL_INDIRECT:
  478. HAS_LINK(tuple->Flags) = 1;
  479. LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT;
  480. tuple->LinkOffset = 0;
  481. break;
  482. case CISTPL_LONGLINK_MFC:
  483. tuple->LinkOffset = ofs + 3;
  484. LINK_SPACE(tuple->Flags) = attr;
  485. if (function == BIND_FN_ALL) {
  486. /* Follow all the MFC links */
  487. read_cis_cache(s, attr, ofs+2, 1, &tmp);
  488. MFC_FN(tuple->Flags) = tmp;
  489. } else {
  490. /* Follow exactly one of the links */
  491. MFC_FN(tuple->Flags) = 1;
  492. tuple->LinkOffset += function * 5;
  493. }
  494. break;
  495. case CISTPL_NO_LINK:
  496. HAS_LINK(tuple->Flags) = 0;
  497. break;
  498. }
  499. if ((tuple->Attributes & TUPLE_RETURN_LINK) &&
  500. (tuple->DesiredTuple == RETURN_FIRST_TUPLE))
  501. break;
  502. } else
  503. if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
  504. break;
  505. if (link[0] == tuple->DesiredTuple)
  506. break;
  507. ofs += link[1] + 2;
  508. }
  509. if (i == MAX_TUPLES) {
  510. dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n");
  511. return -ENOSPC;
  512. }
  513. tuple->TupleCode = link[0];
  514. tuple->TupleLink = link[1];
  515. tuple->CISOffset = ofs + 2;
  516. return 0;
  517. }
  518. EXPORT_SYMBOL(pccard_get_next_tuple);
  519. /*====================================================================*/
  520. #define _MIN(a, b) (((a) < (b)) ? (a) : (b))
  521. int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple)
  522. {
  523. u_int len;
  524. if (!s)
  525. return -EINVAL;
  526. if (tuple->TupleLink < tuple->TupleOffset)
  527. return -ENOSPC;
  528. len = tuple->TupleLink - tuple->TupleOffset;
  529. tuple->TupleDataLen = tuple->TupleLink;
  530. if (len == 0)
  531. return 0;
  532. read_cis_cache(s, SPACE(tuple->Flags),
  533. tuple->CISOffset + tuple->TupleOffset,
  534. _MIN(len, tuple->TupleDataMax), tuple->TupleData);
  535. return 0;
  536. }
  537. EXPORT_SYMBOL(pccard_get_tuple_data);
  538. /*======================================================================
  539. Parsing routines for individual tuples
  540. ======================================================================*/
  541. static int parse_device(tuple_t *tuple, cistpl_device_t *device)
  542. {
  543. int i;
  544. u_char scale;
  545. u_char *p, *q;
  546. p = (u_char *)tuple->TupleData;
  547. q = p + tuple->TupleDataLen;
  548. device->ndev = 0;
  549. for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
  550. if (*p == 0xff)
  551. break;
  552. device->dev[i].type = (*p >> 4);
  553. device->dev[i].wp = (*p & 0x08) ? 1 : 0;
  554. switch (*p & 0x07) {
  555. case 0:
  556. device->dev[i].speed = 0;
  557. break;
  558. case 1:
  559. device->dev[i].speed = 250;
  560. break;
  561. case 2:
  562. device->dev[i].speed = 200;
  563. break;
  564. case 3:
  565. device->dev[i].speed = 150;
  566. break;
  567. case 4:
  568. device->dev[i].speed = 100;
  569. break;
  570. case 7:
  571. if (++p == q)
  572. return -EINVAL;
  573. device->dev[i].speed = SPEED_CVT(*p);
  574. while (*p & 0x80)
  575. if (++p == q)
  576. return -EINVAL;
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. if (++p == q)
  582. return -EINVAL;
  583. if (*p == 0xff)
  584. break;
  585. scale = *p & 7;
  586. if (scale == 7)
  587. return -EINVAL;
  588. device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
  589. device->ndev++;
  590. if (++p == q)
  591. break;
  592. }
  593. return 0;
  594. }
  595. /*====================================================================*/
  596. static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
  597. {
  598. u_char *p;
  599. if (tuple->TupleDataLen < 5)
  600. return -EINVAL;
  601. p = (u_char *) tuple->TupleData;
  602. csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2;
  603. csum->len = get_unaligned_le16(p + 2);
  604. csum->sum = *(p + 4);
  605. return 0;
  606. }
  607. /*====================================================================*/
  608. static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
  609. {
  610. if (tuple->TupleDataLen < 4)
  611. return -EINVAL;
  612. link->addr = get_unaligned_le32(tuple->TupleData);
  613. return 0;
  614. }
  615. /*====================================================================*/
  616. static int parse_longlink_mfc(tuple_t *tuple,
  617. cistpl_longlink_mfc_t *link)
  618. {
  619. u_char *p;
  620. int i;
  621. p = (u_char *)tuple->TupleData;
  622. link->nfn = *p; p++;
  623. if (tuple->TupleDataLen <= link->nfn*5)
  624. return -EINVAL;
  625. for (i = 0; i < link->nfn; i++) {
  626. link->fn[i].space = *p; p++;
  627. link->fn[i].addr = get_unaligned_le32(p);
  628. p += 4;
  629. }
  630. return 0;
  631. }
  632. /*====================================================================*/
  633. static int parse_strings(u_char *p, u_char *q, int max,
  634. char *s, u_char *ofs, u_char *found)
  635. {
  636. int i, j, ns;
  637. if (p == q)
  638. return -EINVAL;
  639. ns = 0; j = 0;
  640. for (i = 0; i < max; i++) {
  641. if (*p == 0xff)
  642. break;
  643. ofs[i] = j;
  644. ns++;
  645. for (;;) {
  646. s[j++] = (*p == 0xff) ? '\0' : *p;
  647. if ((*p == '\0') || (*p == 0xff))
  648. break;
  649. if (++p == q)
  650. return -EINVAL;
  651. }
  652. if ((*p == 0xff) || (++p == q))
  653. break;
  654. }
  655. if (found) {
  656. *found = ns;
  657. return 0;
  658. } else {
  659. return (ns == max) ? 0 : -EINVAL;
  660. }
  661. }
  662. /*====================================================================*/
  663. static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
  664. {
  665. u_char *p, *q;
  666. p = (u_char *)tuple->TupleData;
  667. q = p + tuple->TupleDataLen;
  668. vers_1->major = *p; p++;
  669. vers_1->minor = *p; p++;
  670. if (p >= q)
  671. return -EINVAL;
  672. return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
  673. vers_1->str, vers_1->ofs, &vers_1->ns);
  674. }
  675. /*====================================================================*/
  676. static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
  677. {
  678. u_char *p, *q;
  679. p = (u_char *)tuple->TupleData;
  680. q = p + tuple->TupleDataLen;
  681. return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
  682. altstr->str, altstr->ofs, &altstr->ns);
  683. }
  684. /*====================================================================*/
  685. static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
  686. {
  687. u_char *p, *q;
  688. int nid;
  689. p = (u_char *)tuple->TupleData;
  690. q = p + tuple->TupleDataLen;
  691. for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
  692. if (p > q-2)
  693. break;
  694. jedec->id[nid].mfr = p[0];
  695. jedec->id[nid].info = p[1];
  696. p += 2;
  697. }
  698. jedec->nid = nid;
  699. return 0;
  700. }
  701. /*====================================================================*/
  702. static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
  703. {
  704. if (tuple->TupleDataLen < 4)
  705. return -EINVAL;
  706. m->manf = get_unaligned_le16(tuple->TupleData);
  707. m->card = get_unaligned_le16(tuple->TupleData + 2);
  708. return 0;
  709. }
  710. /*====================================================================*/
  711. static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
  712. {
  713. u_char *p;
  714. if (tuple->TupleDataLen < 2)
  715. return -EINVAL;
  716. p = (u_char *)tuple->TupleData;
  717. f->func = p[0];
  718. f->sysinit = p[1];
  719. return 0;
  720. }
  721. /*====================================================================*/
  722. static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
  723. {
  724. u_char *p;
  725. int i;
  726. if (tuple->TupleDataLen < 1)
  727. return -EINVAL;
  728. p = (u_char *)tuple->TupleData;
  729. f->type = p[0];
  730. for (i = 1; i < tuple->TupleDataLen; i++)
  731. f->data[i-1] = p[i];
  732. return 0;
  733. }
  734. /*====================================================================*/
  735. static int parse_config(tuple_t *tuple, cistpl_config_t *config)
  736. {
  737. int rasz, rmsz, i;
  738. u_char *p;
  739. p = (u_char *)tuple->TupleData;
  740. rasz = *p & 0x03;
  741. rmsz = (*p & 0x3c) >> 2;
  742. if (tuple->TupleDataLen < rasz+rmsz+4)
  743. return -EINVAL;
  744. config->last_idx = *(++p);
  745. p++;
  746. config->base = 0;
  747. for (i = 0; i <= rasz; i++)
  748. config->base += p[i] << (8*i);
  749. p += rasz+1;
  750. for (i = 0; i < 4; i++)
  751. config->rmask[i] = 0;
  752. for (i = 0; i <= rmsz; i++)
  753. config->rmask[i>>2] += p[i] << (8*(i%4));
  754. config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
  755. return 0;
  756. }
  757. /*======================================================================
  758. The following routines are all used to parse the nightmarish
  759. config table entries.
  760. ======================================================================*/
  761. static u_char *parse_power(u_char *p, u_char *q,
  762. cistpl_power_t *pwr)
  763. {
  764. int i;
  765. u_int scale;
  766. if (p == q)
  767. return NULL;
  768. pwr->present = *p;
  769. pwr->flags = 0;
  770. p++;
  771. for (i = 0; i < 7; i++)
  772. if (pwr->present & (1<<i)) {
  773. if (p == q)
  774. return NULL;
  775. pwr->param[i] = POWER_CVT(*p);
  776. scale = POWER_SCALE(*p);
  777. while (*p & 0x80) {
  778. if (++p == q)
  779. return NULL;
  780. if ((*p & 0x7f) < 100)
  781. pwr->param[i] += (*p & 0x7f) * scale / 100;
  782. else if (*p == 0x7d)
  783. pwr->flags |= CISTPL_POWER_HIGHZ_OK;
  784. else if (*p == 0x7e)
  785. pwr->param[i] = 0;
  786. else if (*p == 0x7f)
  787. pwr->flags |= CISTPL_POWER_HIGHZ_REQ;
  788. else
  789. return NULL;
  790. }
  791. p++;
  792. }
  793. return p;
  794. }
  795. /*====================================================================*/
  796. static u_char *parse_timing(u_char *p, u_char *q,
  797. cistpl_timing_t *timing)
  798. {
  799. u_char scale;
  800. if (p == q)
  801. return NULL;
  802. scale = *p;
  803. if ((scale & 3) != 3) {
  804. if (++p == q)
  805. return NULL;
  806. timing->wait = SPEED_CVT(*p);
  807. timing->waitscale = exponent[scale & 3];
  808. } else
  809. timing->wait = 0;
  810. scale >>= 2;
  811. if ((scale & 7) != 7) {
  812. if (++p == q)
  813. return NULL;
  814. timing->ready = SPEED_CVT(*p);
  815. timing->rdyscale = exponent[scale & 7];
  816. } else
  817. timing->ready = 0;
  818. scale >>= 3;
  819. if (scale != 7) {
  820. if (++p == q)
  821. return NULL;
  822. timing->reserved = SPEED_CVT(*p);
  823. timing->rsvscale = exponent[scale];
  824. } else
  825. timing->reserved = 0;
  826. p++;
  827. return p;
  828. }
  829. /*====================================================================*/
  830. static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
  831. {
  832. int i, j, bsz, lsz;
  833. if (p == q)
  834. return NULL;
  835. io->flags = *p;
  836. if (!(*p & 0x80)) {
  837. io->nwin = 1;
  838. io->win[0].base = 0;
  839. io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
  840. return p+1;
  841. }
  842. if (++p == q)
  843. return NULL;
  844. io->nwin = (*p & 0x0f) + 1;
  845. bsz = (*p & 0x30) >> 4;
  846. if (bsz == 3)
  847. bsz++;
  848. lsz = (*p & 0xc0) >> 6;
  849. if (lsz == 3)
  850. lsz++;
  851. p++;
  852. for (i = 0; i < io->nwin; i++) {
  853. io->win[i].base = 0;
  854. io->win[i].len = 1;
  855. for (j = 0; j < bsz; j++, p++) {
  856. if (p == q)
  857. return NULL;
  858. io->win[i].base += *p << (j*8);
  859. }
  860. for (j = 0; j < lsz; j++, p++) {
  861. if (p == q)
  862. return NULL;
  863. io->win[i].len += *p << (j*8);
  864. }
  865. }
  866. return p;
  867. }
  868. /*====================================================================*/
  869. static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
  870. {
  871. int i, j, asz, lsz, has_ha;
  872. u_int len, ca, ha;
  873. if (p == q)
  874. return NULL;
  875. mem->nwin = (*p & 0x07) + 1;
  876. lsz = (*p & 0x18) >> 3;
  877. asz = (*p & 0x60) >> 5;
  878. has_ha = (*p & 0x80);
  879. if (++p == q)
  880. return NULL;
  881. for (i = 0; i < mem->nwin; i++) {
  882. len = ca = ha = 0;
  883. for (j = 0; j < lsz; j++, p++) {
  884. if (p == q)
  885. return NULL;
  886. len += *p << (j*8);
  887. }
  888. for (j = 0; j < asz; j++, p++) {
  889. if (p == q)
  890. return NULL;
  891. ca += *p << (j*8);
  892. }
  893. if (has_ha)
  894. for (j = 0; j < asz; j++, p++) {
  895. if (p == q)
  896. return NULL;
  897. ha += *p << (j*8);
  898. }
  899. mem->win[i].len = len << 8;
  900. mem->win[i].card_addr = ca << 8;
  901. mem->win[i].host_addr = ha << 8;
  902. }
  903. return p;
  904. }
  905. /*====================================================================*/
  906. static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
  907. {
  908. if (p == q)
  909. return NULL;
  910. irq->IRQInfo1 = *p; p++;
  911. if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
  912. if (p+2 > q)
  913. return NULL;
  914. irq->IRQInfo2 = (p[1]<<8) + p[0];
  915. p += 2;
  916. }
  917. return p;
  918. }
  919. /*====================================================================*/
  920. static int parse_cftable_entry(tuple_t *tuple,
  921. cistpl_cftable_entry_t *entry)
  922. {
  923. u_char *p, *q, features;
  924. p = tuple->TupleData;
  925. q = p + tuple->TupleDataLen;
  926. entry->index = *p & 0x3f;
  927. entry->flags = 0;
  928. if (*p & 0x40)
  929. entry->flags |= CISTPL_CFTABLE_DEFAULT;
  930. if (*p & 0x80) {
  931. if (++p == q)
  932. return -EINVAL;
  933. if (*p & 0x10)
  934. entry->flags |= CISTPL_CFTABLE_BVDS;
  935. if (*p & 0x20)
  936. entry->flags |= CISTPL_CFTABLE_WP;
  937. if (*p & 0x40)
  938. entry->flags |= CISTPL_CFTABLE_RDYBSY;
  939. if (*p & 0x80)
  940. entry->flags |= CISTPL_CFTABLE_MWAIT;
  941. entry->interface = *p & 0x0f;
  942. } else
  943. entry->interface = 0;
  944. /* Process optional features */
  945. if (++p == q)
  946. return -EINVAL;
  947. features = *p; p++;
  948. /* Power options */
  949. if ((features & 3) > 0) {
  950. p = parse_power(p, q, &entry->vcc);
  951. if (p == NULL)
  952. return -EINVAL;
  953. } else
  954. entry->vcc.present = 0;
  955. if ((features & 3) > 1) {
  956. p = parse_power(p, q, &entry->vpp1);
  957. if (p == NULL)
  958. return -EINVAL;
  959. } else
  960. entry->vpp1.present = 0;
  961. if ((features & 3) > 2) {
  962. p = parse_power(p, q, &entry->vpp2);
  963. if (p == NULL)
  964. return -EINVAL;
  965. } else
  966. entry->vpp2.present = 0;
  967. /* Timing options */
  968. if (features & 0x04) {
  969. p = parse_timing(p, q, &entry->timing);
  970. if (p == NULL)
  971. return -EINVAL;
  972. } else {
  973. entry->timing.wait = 0;
  974. entry->timing.ready = 0;
  975. entry->timing.reserved = 0;
  976. }
  977. /* I/O window options */
  978. if (features & 0x08) {
  979. p = parse_io(p, q, &entry->io);
  980. if (p == NULL)
  981. return -EINVAL;
  982. } else
  983. entry->io.nwin = 0;
  984. /* Interrupt options */
  985. if (features & 0x10) {
  986. p = parse_irq(p, q, &entry->irq);
  987. if (p == NULL)
  988. return -EINVAL;
  989. } else
  990. entry->irq.IRQInfo1 = 0;
  991. switch (features & 0x60) {
  992. case 0x00:
  993. entry->mem.nwin = 0;
  994. break;
  995. case 0x20:
  996. entry->mem.nwin = 1;
  997. entry->mem.win[0].len = get_unaligned_le16(p) << 8;
  998. entry->mem.win[0].card_addr = 0;
  999. entry->mem.win[0].host_addr = 0;
  1000. p += 2;
  1001. if (p > q)
  1002. return -EINVAL;
  1003. break;
  1004. case 0x40:
  1005. entry->mem.nwin = 1;
  1006. entry->mem.win[0].len = get_unaligned_le16(p) << 8;
  1007. entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8;
  1008. entry->mem.win[0].host_addr = 0;
  1009. p += 4;
  1010. if (p > q)
  1011. return -EINVAL;
  1012. break;
  1013. case 0x60:
  1014. p = parse_mem(p, q, &entry->mem);
  1015. if (p == NULL)
  1016. return -EINVAL;
  1017. break;
  1018. }
  1019. /* Misc features */
  1020. if (features & 0x80) {
  1021. if (p == q)
  1022. return -EINVAL;
  1023. entry->flags |= (*p << 8);
  1024. while (*p & 0x80)
  1025. if (++p == q)
  1026. return -EINVAL;
  1027. p++;
  1028. }
  1029. entry->subtuples = q-p;
  1030. return 0;
  1031. }
  1032. /*====================================================================*/
  1033. #ifdef CONFIG_CARDBUS
  1034. static int parse_bar(tuple_t *tuple, cistpl_bar_t *bar)
  1035. {
  1036. u_char *p;
  1037. if (tuple->TupleDataLen < 6)
  1038. return -EINVAL;
  1039. p = (u_char *)tuple->TupleData;
  1040. bar->attr = *p;
  1041. p += 2;
  1042. bar->size = get_unaligned_le32(p);
  1043. return 0;
  1044. }
  1045. static int parse_config_cb(tuple_t *tuple, cistpl_config_t *config)
  1046. {
  1047. u_char *p;
  1048. p = (u_char *)tuple->TupleData;
  1049. if ((*p != 3) || (tuple->TupleDataLen < 6))
  1050. return -EINVAL;
  1051. config->last_idx = *(++p);
  1052. p++;
  1053. config->base = get_unaligned_le32(p);
  1054. config->subtuples = tuple->TupleDataLen - 6;
  1055. return 0;
  1056. }
  1057. static int parse_cftable_entry_cb(tuple_t *tuple,
  1058. cistpl_cftable_entry_cb_t *entry)
  1059. {
  1060. u_char *p, *q, features;
  1061. p = tuple->TupleData;
  1062. q = p + tuple->TupleDataLen;
  1063. entry->index = *p & 0x3f;
  1064. entry->flags = 0;
  1065. if (*p & 0x40)
  1066. entry->flags |= CISTPL_CFTABLE_DEFAULT;
  1067. /* Process optional features */
  1068. if (++p == q)
  1069. return -EINVAL;
  1070. features = *p; p++;
  1071. /* Power options */
  1072. if ((features & 3) > 0) {
  1073. p = parse_power(p, q, &entry->vcc);
  1074. if (p == NULL)
  1075. return -EINVAL;
  1076. } else
  1077. entry->vcc.present = 0;
  1078. if ((features & 3) > 1) {
  1079. p = parse_power(p, q, &entry->vpp1);
  1080. if (p == NULL)
  1081. return -EINVAL;
  1082. } else
  1083. entry->vpp1.present = 0;
  1084. if ((features & 3) > 2) {
  1085. p = parse_power(p, q, &entry->vpp2);
  1086. if (p == NULL)
  1087. return -EINVAL;
  1088. } else
  1089. entry->vpp2.present = 0;
  1090. /* I/O window options */
  1091. if (features & 0x08) {
  1092. if (p == q)
  1093. return -EINVAL;
  1094. entry->io = *p; p++;
  1095. } else
  1096. entry->io = 0;
  1097. /* Interrupt options */
  1098. if (features & 0x10) {
  1099. p = parse_irq(p, q, &entry->irq);
  1100. if (p == NULL)
  1101. return -EINVAL;
  1102. } else
  1103. entry->irq.IRQInfo1 = 0;
  1104. if (features & 0x20) {
  1105. if (p == q)
  1106. return -EINVAL;
  1107. entry->mem = *p; p++;
  1108. } else
  1109. entry->mem = 0;
  1110. /* Misc features */
  1111. if (features & 0x80) {
  1112. if (p == q)
  1113. return -EINVAL;
  1114. entry->flags |= (*p << 8);
  1115. if (*p & 0x80) {
  1116. if (++p == q)
  1117. return -EINVAL;
  1118. entry->flags |= (*p << 16);
  1119. }
  1120. while (*p & 0x80)
  1121. if (++p == q)
  1122. return -EINVAL;
  1123. p++;
  1124. }
  1125. entry->subtuples = q-p;
  1126. return 0;
  1127. }
  1128. #endif
  1129. /*====================================================================*/
  1130. static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
  1131. {
  1132. u_char *p, *q;
  1133. int n;
  1134. p = (u_char *)tuple->TupleData;
  1135. q = p + tuple->TupleDataLen;
  1136. for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
  1137. if (p > q-6)
  1138. break;
  1139. geo->geo[n].buswidth = p[0];
  1140. geo->geo[n].erase_block = 1 << (p[1]-1);
  1141. geo->geo[n].read_block = 1 << (p[2]-1);
  1142. geo->geo[n].write_block = 1 << (p[3]-1);
  1143. geo->geo[n].partition = 1 << (p[4]-1);
  1144. geo->geo[n].interleave = 1 << (p[5]-1);
  1145. p += 6;
  1146. }
  1147. geo->ngeo = n;
  1148. return 0;
  1149. }
  1150. /*====================================================================*/
  1151. static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
  1152. {
  1153. u_char *p, *q;
  1154. if (tuple->TupleDataLen < 10)
  1155. return -EINVAL;
  1156. p = tuple->TupleData;
  1157. q = p + tuple->TupleDataLen;
  1158. v2->vers = p[0];
  1159. v2->comply = p[1];
  1160. v2->dindex = get_unaligned_le16(p + 2);
  1161. v2->vspec8 = p[6];
  1162. v2->vspec9 = p[7];
  1163. v2->nhdr = p[8];
  1164. p += 9;
  1165. return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL);
  1166. }
  1167. /*====================================================================*/
  1168. static int parse_org(tuple_t *tuple, cistpl_org_t *org)
  1169. {
  1170. u_char *p, *q;
  1171. int i;
  1172. p = tuple->TupleData;
  1173. q = p + tuple->TupleDataLen;
  1174. if (p == q)
  1175. return -EINVAL;
  1176. org->data_org = *p;
  1177. if (++p == q)
  1178. return -EINVAL;
  1179. for (i = 0; i < 30; i++) {
  1180. org->desc[i] = *p;
  1181. if (*p == '\0')
  1182. break;
  1183. if (++p == q)
  1184. return -EINVAL;
  1185. }
  1186. return 0;
  1187. }
  1188. /*====================================================================*/
  1189. static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
  1190. {
  1191. u_char *p;
  1192. if (tuple->TupleDataLen < 10)
  1193. return -EINVAL;
  1194. p = tuple->TupleData;
  1195. fmt->type = p[0];
  1196. fmt->edc = p[1];
  1197. fmt->offset = get_unaligned_le32(p + 2);
  1198. fmt->length = get_unaligned_le32(p + 6);
  1199. return 0;
  1200. }
  1201. /*====================================================================*/
  1202. int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse)
  1203. {
  1204. int ret = 0;
  1205. if (tuple->TupleDataLen > tuple->TupleDataMax)
  1206. return -EINVAL;
  1207. switch (tuple->TupleCode) {
  1208. case CISTPL_DEVICE:
  1209. case CISTPL_DEVICE_A:
  1210. ret = parse_device(tuple, &parse->device);
  1211. break;
  1212. #ifdef CONFIG_CARDBUS
  1213. case CISTPL_BAR:
  1214. ret = parse_bar(tuple, &parse->bar);
  1215. break;
  1216. case CISTPL_CONFIG_CB:
  1217. ret = parse_config_cb(tuple, &parse->config);
  1218. break;
  1219. case CISTPL_CFTABLE_ENTRY_CB:
  1220. ret = parse_cftable_entry_cb(tuple, &parse->cftable_entry_cb);
  1221. break;
  1222. #endif
  1223. case CISTPL_CHECKSUM:
  1224. ret = parse_checksum(tuple, &parse->checksum);
  1225. break;
  1226. case CISTPL_LONGLINK_A:
  1227. case CISTPL_LONGLINK_C:
  1228. ret = parse_longlink(tuple, &parse->longlink);
  1229. break;
  1230. case CISTPL_LONGLINK_MFC:
  1231. ret = parse_longlink_mfc(tuple, &parse->longlink_mfc);
  1232. break;
  1233. case CISTPL_VERS_1:
  1234. ret = parse_vers_1(tuple, &parse->version_1);
  1235. break;
  1236. case CISTPL_ALTSTR:
  1237. ret = parse_altstr(tuple, &parse->altstr);
  1238. break;
  1239. case CISTPL_JEDEC_A:
  1240. case CISTPL_JEDEC_C:
  1241. ret = parse_jedec(tuple, &parse->jedec);
  1242. break;
  1243. case CISTPL_MANFID:
  1244. ret = parse_manfid(tuple, &parse->manfid);
  1245. break;
  1246. case CISTPL_FUNCID:
  1247. ret = parse_funcid(tuple, &parse->funcid);
  1248. break;
  1249. case CISTPL_FUNCE:
  1250. ret = parse_funce(tuple, &parse->funce);
  1251. break;
  1252. case CISTPL_CONFIG:
  1253. ret = parse_config(tuple, &parse->config);
  1254. break;
  1255. case CISTPL_CFTABLE_ENTRY:
  1256. ret = parse_cftable_entry(tuple, &parse->cftable_entry);
  1257. break;
  1258. case CISTPL_DEVICE_GEO:
  1259. case CISTPL_DEVICE_GEO_A:
  1260. ret = parse_device_geo(tuple, &parse->device_geo);
  1261. break;
  1262. case CISTPL_VERS_2:
  1263. ret = parse_vers_2(tuple, &parse->vers_2);
  1264. break;
  1265. case CISTPL_ORG:
  1266. ret = parse_org(tuple, &parse->org);
  1267. break;
  1268. case CISTPL_FORMAT:
  1269. case CISTPL_FORMAT_A:
  1270. ret = parse_format(tuple, &parse->format);
  1271. break;
  1272. case CISTPL_NO_LINK:
  1273. case CISTPL_LINKTARGET:
  1274. ret = 0;
  1275. break;
  1276. default:
  1277. ret = -EINVAL;
  1278. break;
  1279. }
  1280. if (ret)
  1281. pr_debug("parse_tuple failed %d\n", ret);
  1282. return ret;
  1283. }
  1284. EXPORT_SYMBOL(pcmcia_parse_tuple);
  1285. /*======================================================================
  1286. This is used internally by Card Services to look up CIS stuff.
  1287. ======================================================================*/
  1288. int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse)
  1289. {
  1290. tuple_t tuple;
  1291. cisdata_t *buf;
  1292. int ret;
  1293. buf = kmalloc(256, GFP_KERNEL);
  1294. if (buf == NULL) {
  1295. dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
  1296. return -ENOMEM;
  1297. }
  1298. tuple.DesiredTuple = code;
  1299. tuple.Attributes = 0;
  1300. if (function == BIND_FN_ALL)
  1301. tuple.Attributes = TUPLE_RETURN_COMMON;
  1302. ret = pccard_get_first_tuple(s, function, &tuple);
  1303. if (ret != 0)
  1304. goto done;
  1305. tuple.TupleData = buf;
  1306. tuple.TupleOffset = 0;
  1307. tuple.TupleDataMax = 255;
  1308. ret = pccard_get_tuple_data(s, &tuple);
  1309. if (ret != 0)
  1310. goto done;
  1311. ret = pcmcia_parse_tuple(&tuple, parse);
  1312. done:
  1313. kfree(buf);
  1314. return ret;
  1315. }
  1316. EXPORT_SYMBOL(pccard_read_tuple);
  1317. /**
  1318. * pccard_loop_tuple() - loop over tuples in the CIS
  1319. * @s: the struct pcmcia_socket where the card is inserted
  1320. * @function: the device function we loop for
  1321. * @code: which CIS code shall we look for?
  1322. * @parse: buffer where the tuple shall be parsed (or NULL, if no parse)
  1323. * @priv_data: private data to be passed to the loop_tuple function.
  1324. * @loop_tuple: function to call for each CIS entry of type @function. IT
  1325. * gets passed the raw tuple, the paresed tuple (if @parse is
  1326. * set) and @priv_data.
  1327. *
  1328. * pccard_loop_tuple() loops over all CIS entries of type @function, and
  1329. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  1330. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  1331. */
  1332. int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
  1333. cisdata_t code, cisparse_t *parse, void *priv_data,
  1334. int (*loop_tuple) (tuple_t *tuple,
  1335. cisparse_t *parse,
  1336. void *priv_data))
  1337. {
  1338. tuple_t tuple;
  1339. cisdata_t *buf;
  1340. int ret;
  1341. buf = kzalloc(256, GFP_KERNEL);
  1342. if (buf == NULL) {
  1343. dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
  1344. return -ENOMEM;
  1345. }
  1346. tuple.TupleData = buf;
  1347. tuple.TupleDataMax = 255;
  1348. tuple.TupleOffset = 0;
  1349. tuple.DesiredTuple = code;
  1350. tuple.Attributes = 0;
  1351. ret = pccard_get_first_tuple(s, function, &tuple);
  1352. while (!ret) {
  1353. if (pccard_get_tuple_data(s, &tuple))
  1354. goto next_entry;
  1355. if (parse)
  1356. if (pcmcia_parse_tuple(&tuple, parse))
  1357. goto next_entry;
  1358. ret = loop_tuple(&tuple, parse, priv_data);
  1359. if (!ret)
  1360. break;
  1361. next_entry:
  1362. ret = pccard_get_next_tuple(s, function, &tuple);
  1363. }
  1364. kfree(buf);
  1365. return ret;
  1366. }
  1367. EXPORT_SYMBOL(pccard_loop_tuple);
  1368. /**
  1369. * pccard_validate_cis() - check whether card has a sensible CIS
  1370. * @s: the struct pcmcia_socket we are to check
  1371. * @info: returns the number of tuples in the (valid) CIS, or 0
  1372. *
  1373. * This tries to determine if a card has a sensible CIS. In @info, it
  1374. * returns the number of tuples in the CIS, or 0 if the CIS looks bad. The
  1375. * checks include making sure several critical tuples are present and
  1376. * valid; seeing if the total number of tuples is reasonable; and
  1377. * looking for tuples that use reserved codes.
  1378. *
  1379. * The function returns 0 on success.
  1380. */
  1381. int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info)
  1382. {
  1383. tuple_t *tuple;
  1384. cisparse_t *p;
  1385. unsigned int count = 0;
  1386. int ret, reserved, dev_ok = 0, ident_ok = 0;
  1387. if (!s)
  1388. return -EINVAL;
  1389. /* We do not want to validate the CIS cache... */
  1390. destroy_cis_cache(s);
  1391. tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
  1392. if (tuple == NULL) {
  1393. dev_warn(&s->dev, "no memory to validate CIS\n");
  1394. return -ENOMEM;
  1395. }
  1396. p = kmalloc(sizeof(*p), GFP_KERNEL);
  1397. if (p == NULL) {
  1398. kfree(tuple);
  1399. dev_warn(&s->dev, "no memory to validate CIS\n");
  1400. return -ENOMEM;
  1401. }
  1402. count = reserved = 0;
  1403. tuple->DesiredTuple = RETURN_FIRST_TUPLE;
  1404. tuple->Attributes = TUPLE_RETURN_COMMON;
  1405. ret = pccard_get_first_tuple(s, BIND_FN_ALL, tuple);
  1406. if (ret != 0)
  1407. goto done;
  1408. /* First tuple should be DEVICE; we should really have either that
  1409. or a CFTABLE_ENTRY of some sort */
  1410. if ((tuple->TupleCode == CISTPL_DEVICE) ||
  1411. (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY, p)) ||
  1412. (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY_CB, p)))
  1413. dev_ok++;
  1414. /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
  1415. tuple, for card identification. Certain old D-Link and Linksys
  1416. cards have only a broken VERS_2 tuple; hence the bogus test. */
  1417. if ((pccard_read_tuple(s, BIND_FN_ALL, CISTPL_MANFID, p) == 0) ||
  1418. (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_1, p) == 0) ||
  1419. (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_2, p) != -ENOSPC))
  1420. ident_ok++;
  1421. if (!dev_ok && !ident_ok)
  1422. goto done;
  1423. for (count = 1; count < MAX_TUPLES; count++) {
  1424. ret = pccard_get_next_tuple(s, BIND_FN_ALL, tuple);
  1425. if (ret != 0)
  1426. break;
  1427. if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
  1428. ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
  1429. ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
  1430. reserved++;
  1431. }
  1432. if ((count == MAX_TUPLES) || (reserved > 5) ||
  1433. ((!dev_ok || !ident_ok) && (count > 10)))
  1434. count = 0;
  1435. ret = 0;
  1436. done:
  1437. /* invalidate CIS cache on failure */
  1438. if (!dev_ok || !ident_ok || !count) {
  1439. destroy_cis_cache(s);
  1440. ret = -EIO;
  1441. }
  1442. if (info)
  1443. *info = count;
  1444. kfree(tuple);
  1445. kfree(p);
  1446. return ret;
  1447. }
  1448. EXPORT_SYMBOL(pccard_validate_cis);