cistpl.c 35 KB

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