rsrc_nonstatic.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets
  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/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include <linux/ioport.h>
  23. #include <linux/timer.h>
  24. #include <linux/pci.h>
  25. #include <linux/device.h>
  26. #include <linux/io.h>
  27. #include <asm/irq.h>
  28. #include <pcmcia/ss.h>
  29. #include <pcmcia/cistpl.h>
  30. #include "cs_internal.h"
  31. /* moved to rsrc_mgr.c
  32. MODULE_AUTHOR("David A. Hinds, Dominik Brodowski");
  33. MODULE_LICENSE("GPL");
  34. */
  35. /* Parameters that can be set with 'insmod' */
  36. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
  37. INT_MODULE_PARM(probe_mem, 1); /* memory probe? */
  38. #ifdef CONFIG_PCMCIA_PROBE
  39. INT_MODULE_PARM(probe_io, 1); /* IO port probe? */
  40. INT_MODULE_PARM(mem_limit, 0x10000);
  41. #endif
  42. /* for io_db and mem_db */
  43. struct resource_map {
  44. u_long base, num;
  45. struct resource_map *next;
  46. };
  47. struct socket_data {
  48. struct resource_map mem_db;
  49. struct resource_map mem_db_valid;
  50. struct resource_map io_db;
  51. };
  52. #define MEM_PROBE_LOW (1 << 0)
  53. #define MEM_PROBE_HIGH (1 << 1)
  54. /* Action field */
  55. #define REMOVE_MANAGED_RESOURCE 1
  56. #define ADD_MANAGED_RESOURCE 2
  57. /*======================================================================
  58. Linux resource management extensions
  59. ======================================================================*/
  60. static struct resource *
  61. claim_region(struct pcmcia_socket *s, resource_size_t base,
  62. resource_size_t size, int type, char *name)
  63. {
  64. struct resource *res, *parent;
  65. parent = type & IORESOURCE_MEM ? &iomem_resource : &ioport_resource;
  66. res = pcmcia_make_resource(base, size, type | IORESOURCE_BUSY, name);
  67. if (res) {
  68. #ifdef CONFIG_PCI
  69. if (s && s->cb_dev)
  70. parent = pci_find_parent_resource(s->cb_dev, res);
  71. #endif
  72. if (!parent || request_resource(parent, res)) {
  73. kfree(res);
  74. res = NULL;
  75. }
  76. }
  77. return res;
  78. }
  79. static void free_region(struct resource *res)
  80. {
  81. if (res) {
  82. release_resource(res);
  83. kfree(res);
  84. }
  85. }
  86. /*======================================================================
  87. These manage the internal databases of available resources.
  88. ======================================================================*/
  89. static int add_interval(struct resource_map *map, u_long base, u_long num)
  90. {
  91. struct resource_map *p, *q;
  92. for (p = map; ; p = p->next) {
  93. if ((p != map) && (p->base+p->num >= base)) {
  94. p->num = max(num + base - p->base, p->num);
  95. return 0;
  96. }
  97. if ((p->next == map) || (p->next->base > base+num-1))
  98. break;
  99. }
  100. q = kmalloc(sizeof(struct resource_map), GFP_KERNEL);
  101. if (!q) {
  102. printk(KERN_WARNING "out of memory to update resources\n");
  103. return -ENOMEM;
  104. }
  105. q->base = base; q->num = num;
  106. q->next = p->next; p->next = q;
  107. return 0;
  108. }
  109. /*====================================================================*/
  110. static int sub_interval(struct resource_map *map, u_long base, u_long num)
  111. {
  112. struct resource_map *p, *q;
  113. for (p = map; ; p = q) {
  114. q = p->next;
  115. if (q == map)
  116. break;
  117. if ((q->base+q->num > base) && (base+num > q->base)) {
  118. if (q->base >= base) {
  119. if (q->base+q->num <= base+num) {
  120. /* Delete whole block */
  121. p->next = q->next;
  122. kfree(q);
  123. /* don't advance the pointer yet */
  124. q = p;
  125. } else {
  126. /* Cut off bit from the front */
  127. q->num = q->base + q->num - base - num;
  128. q->base = base + num;
  129. }
  130. } else if (q->base+q->num <= base+num) {
  131. /* Cut off bit from the end */
  132. q->num = base - q->base;
  133. } else {
  134. /* Split the block into two pieces */
  135. p = kmalloc(sizeof(struct resource_map),
  136. GFP_KERNEL);
  137. if (!p) {
  138. printk(KERN_WARNING "out of memory to update resources\n");
  139. return -ENOMEM;
  140. }
  141. p->base = base+num;
  142. p->num = q->base+q->num - p->base;
  143. q->num = base - q->base;
  144. p->next = q->next ; q->next = p;
  145. }
  146. }
  147. }
  148. return 0;
  149. }
  150. /*======================================================================
  151. These routines examine a region of IO or memory addresses to
  152. determine what ranges might be genuinely available.
  153. ======================================================================*/
  154. #ifdef CONFIG_PCMCIA_PROBE
  155. static void do_io_probe(struct pcmcia_socket *s, unsigned int base,
  156. unsigned int num)
  157. {
  158. struct resource *res;
  159. struct socket_data *s_data = s->resource_data;
  160. unsigned int i, j, bad;
  161. int any;
  162. u_char *b, hole, most;
  163. dev_info(&s->dev, "cs: IO port probe %#x-%#x:", base, base+num-1);
  164. /* First, what does a floating port look like? */
  165. b = kzalloc(256, GFP_KERNEL);
  166. if (!b) {
  167. pr_cont("\n");
  168. dev_err(&s->dev, "do_io_probe: unable to kmalloc 256 bytes\n");
  169. return;
  170. }
  171. for (i = base, most = 0; i < base+num; i += 8) {
  172. res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
  173. if (!res)
  174. continue;
  175. hole = inb(i);
  176. for (j = 1; j < 8; j++)
  177. if (inb(i+j) != hole)
  178. break;
  179. free_region(res);
  180. if ((j == 8) && (++b[hole] > b[most]))
  181. most = hole;
  182. if (b[most] == 127)
  183. break;
  184. }
  185. kfree(b);
  186. bad = any = 0;
  187. for (i = base; i < base+num; i += 8) {
  188. res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe");
  189. if (!res) {
  190. if (!any)
  191. pr_cont(" excluding");
  192. if (!bad)
  193. bad = any = i;
  194. continue;
  195. }
  196. for (j = 0; j < 8; j++)
  197. if (inb(i+j) != most)
  198. break;
  199. free_region(res);
  200. if (j < 8) {
  201. if (!any)
  202. pr_cont(" excluding");
  203. if (!bad)
  204. bad = any = i;
  205. } else {
  206. if (bad) {
  207. sub_interval(&s_data->io_db, bad, i-bad);
  208. pr_cont(" %#x-%#x", bad, i-1);
  209. bad = 0;
  210. }
  211. }
  212. }
  213. if (bad) {
  214. if ((num > 16) && (bad == base) && (i == base+num)) {
  215. sub_interval(&s_data->io_db, bad, i-bad);
  216. pr_cont(" nothing: probe failed.\n");
  217. return;
  218. } else {
  219. sub_interval(&s_data->io_db, bad, i-bad);
  220. pr_cont(" %#x-%#x", bad, i-1);
  221. }
  222. }
  223. pr_cont("%s\n", !any ? " clean" : "");
  224. }
  225. #endif
  226. /*======================================================================*/
  227. /**
  228. * readable() - iomem validation function for cards with a valid CIS
  229. */
  230. static int readable(struct pcmcia_socket *s, struct resource *res,
  231. unsigned int *count)
  232. {
  233. int ret = -EINVAL;
  234. if (s->fake_cis) {
  235. dev_dbg(&s->dev, "fake CIS is being used: can't validate mem\n");
  236. return 0;
  237. }
  238. s->cis_mem.res = res;
  239. s->cis_virt = ioremap(res->start, s->map_size);
  240. if (s->cis_virt) {
  241. mutex_unlock(&s->ops_mutex);
  242. /* as we're only called from pcmcia.c, we're safe */
  243. if (s->callback->validate)
  244. ret = s->callback->validate(s, count);
  245. /* invalidate mapping */
  246. mutex_lock(&s->ops_mutex);
  247. iounmap(s->cis_virt);
  248. s->cis_virt = NULL;
  249. }
  250. s->cis_mem.res = NULL;
  251. if ((ret) || (*count == 0))
  252. return -EINVAL;
  253. return 0;
  254. }
  255. /**
  256. * checksum() - iomem validation function for simple memory cards
  257. */
  258. static int checksum(struct pcmcia_socket *s, struct resource *res,
  259. unsigned int *value)
  260. {
  261. pccard_mem_map map;
  262. int i, a = 0, b = -1, d;
  263. void __iomem *virt;
  264. virt = ioremap(res->start, s->map_size);
  265. if (virt) {
  266. map.map = 0;
  267. map.flags = MAP_ACTIVE;
  268. map.speed = 0;
  269. map.res = res;
  270. map.card_start = 0;
  271. s->ops->set_mem_map(s, &map);
  272. /* Don't bother checking every word... */
  273. for (i = 0; i < s->map_size; i += 44) {
  274. d = readl(virt+i);
  275. a += d;
  276. b &= d;
  277. }
  278. map.flags = 0;
  279. s->ops->set_mem_map(s, &map);
  280. iounmap(virt);
  281. }
  282. if (b == -1)
  283. return -EINVAL;
  284. *value = a;
  285. return 0;
  286. }
  287. /**
  288. * do_validate_mem() - low level validate a memory region for PCMCIA use
  289. * @s: PCMCIA socket to validate
  290. * @base: start address of resource to check
  291. * @size: size of resource to check
  292. * @validate: validation function to use
  293. *
  294. * do_validate_mem() splits up the memory region which is to be checked
  295. * into two parts. Both are passed to the @validate() function. If
  296. * @validate() returns non-zero, or the value parameter to @validate()
  297. * is zero, or the value parameter is different between both calls,
  298. * the check fails, and -EINVAL is returned. Else, 0 is returned.
  299. */
  300. static int do_validate_mem(struct pcmcia_socket *s,
  301. unsigned long base, unsigned long size,
  302. int validate (struct pcmcia_socket *s,
  303. struct resource *res,
  304. unsigned int *value))
  305. {
  306. struct socket_data *s_data = s->resource_data;
  307. struct resource *res1, *res2;
  308. unsigned int info1 = 1, info2 = 1;
  309. int ret = -EINVAL;
  310. res1 = claim_region(s, base, size/2, IORESOURCE_MEM, "PCMCIA memprobe");
  311. res2 = claim_region(s, base + size/2, size/2, IORESOURCE_MEM,
  312. "PCMCIA memprobe");
  313. if (res1 && res2) {
  314. ret = 0;
  315. if (validate) {
  316. ret = validate(s, res1, &info1);
  317. ret += validate(s, res2, &info2);
  318. }
  319. }
  320. dev_dbg(&s->dev, "cs: memory probe 0x%06lx-0x%06lx: %p %p %u %u %u",
  321. base, base+size-1, res1, res2, ret, info1, info2);
  322. free_region(res2);
  323. free_region(res1);
  324. if ((ret) || (info1 != info2) || (info1 == 0))
  325. return -EINVAL;
  326. if (validate && !s->fake_cis) {
  327. /* move it to the validated data set */
  328. add_interval(&s_data->mem_db_valid, base, size);
  329. sub_interval(&s_data->mem_db, base, size);
  330. }
  331. return 0;
  332. }
  333. /**
  334. * do_mem_probe() - validate a memory region for PCMCIA use
  335. * @s: PCMCIA socket to validate
  336. * @base: start address of resource to check
  337. * @num: size of resource to check
  338. * @validate: validation function to use
  339. * @fallback: validation function to use if validate fails
  340. *
  341. * do_mem_probe() checks a memory region for use by the PCMCIA subsystem.
  342. * To do so, the area is split up into sensible parts, and then passed
  343. * into the @validate() function. Only if @validate() and @fallback() fail,
  344. * the area is marked as unavaibale for use by the PCMCIA subsystem. The
  345. * function returns the size of the usable memory area.
  346. */
  347. static int do_mem_probe(struct pcmcia_socket *s, u_long base, u_long num,
  348. int validate (struct pcmcia_socket *s,
  349. struct resource *res,
  350. unsigned int *value),
  351. int fallback (struct pcmcia_socket *s,
  352. struct resource *res,
  353. unsigned int *value))
  354. {
  355. struct socket_data *s_data = s->resource_data;
  356. u_long i, j, bad, fail, step;
  357. dev_info(&s->dev, "cs: memory probe 0x%06lx-0x%06lx:",
  358. base, base+num-1);
  359. bad = fail = 0;
  360. step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff);
  361. /* don't allow too large steps */
  362. if (step > 0x800000)
  363. step = 0x800000;
  364. /* cis_readable wants to map 2x map_size */
  365. if (step < 2 * s->map_size)
  366. step = 2 * s->map_size;
  367. for (i = j = base; i < base+num; i = j + step) {
  368. if (!fail) {
  369. for (j = i; j < base+num; j += step) {
  370. if (!do_validate_mem(s, j, step, validate))
  371. break;
  372. }
  373. fail = ((i == base) && (j == base+num));
  374. }
  375. if ((fail) && (fallback)) {
  376. for (j = i; j < base+num; j += step)
  377. if (!do_validate_mem(s, j, step, fallback))
  378. break;
  379. }
  380. if (i != j) {
  381. if (!bad)
  382. pr_cont(" excluding");
  383. pr_cont(" %#05lx-%#05lx", i, j-1);
  384. sub_interval(&s_data->mem_db, i, j-i);
  385. bad += j-i;
  386. }
  387. }
  388. pr_cont("%s\n", !bad ? " clean" : "");
  389. return num - bad;
  390. }
  391. #ifdef CONFIG_PCMCIA_PROBE
  392. /**
  393. * inv_probe() - top-to-bottom search for one usuable high memory area
  394. * @s: PCMCIA socket to validate
  395. * @m: resource_map to check
  396. */
  397. static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s)
  398. {
  399. struct socket_data *s_data = s->resource_data;
  400. u_long ok;
  401. if (m == &s_data->mem_db)
  402. return 0;
  403. ok = inv_probe(m->next, s);
  404. if (ok) {
  405. if (m->base >= 0x100000)
  406. sub_interval(&s_data->mem_db, m->base, m->num);
  407. return ok;
  408. }
  409. if (m->base < 0x100000)
  410. return 0;
  411. return do_mem_probe(s, m->base, m->num, readable, checksum);
  412. }
  413. /**
  414. * validate_mem() - memory probe function
  415. * @s: PCMCIA socket to validate
  416. * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH
  417. *
  418. * The memory probe. If the memory list includes a 64K-aligned block
  419. * below 1MB, we probe in 64K chunks, and as soon as we accumulate at
  420. * least mem_limit free space, we quit. Returns 0 on usuable ports.
  421. */
  422. static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
  423. {
  424. struct resource_map *m, mm;
  425. static unsigned char order[] = { 0xd0, 0xe0, 0xc0, 0xf0 };
  426. unsigned long b, i, ok = 0;
  427. struct socket_data *s_data = s->resource_data;
  428. /* We do up to four passes through the list */
  429. if (probe_mask & MEM_PROBE_HIGH) {
  430. if (inv_probe(s_data->mem_db.next, s) > 0)
  431. return 0;
  432. if (s_data->mem_db_valid.next != &s_data->mem_db_valid)
  433. return 0;
  434. dev_notice(&s->dev,
  435. "cs: warning: no high memory space available!\n");
  436. return -ENODEV;
  437. }
  438. for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
  439. mm = *m;
  440. /* Only probe < 1 MB */
  441. if (mm.base >= 0x100000)
  442. continue;
  443. if ((mm.base | mm.num) & 0xffff) {
  444. ok += do_mem_probe(s, mm.base, mm.num, readable,
  445. checksum);
  446. continue;
  447. }
  448. /* Special probe for 64K-aligned block */
  449. for (i = 0; i < 4; i++) {
  450. b = order[i] << 12;
  451. if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) {
  452. if (ok >= mem_limit)
  453. sub_interval(&s_data->mem_db, b, 0x10000);
  454. else
  455. ok += do_mem_probe(s, b, 0x10000,
  456. readable, checksum);
  457. }
  458. }
  459. }
  460. if (ok > 0)
  461. return 0;
  462. return -ENODEV;
  463. }
  464. #else /* CONFIG_PCMCIA_PROBE */
  465. /**
  466. * validate_mem() - memory probe function
  467. * @s: PCMCIA socket to validate
  468. * @probe_mask: ignored
  469. *
  470. * Returns 0 on usuable ports.
  471. */
  472. static int validate_mem(struct pcmcia_socket *s, unsigned int probe_mask)
  473. {
  474. struct resource_map *m, mm;
  475. struct socket_data *s_data = s->resource_data;
  476. unsigned long ok = 0;
  477. for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
  478. mm = *m;
  479. ok += do_mem_probe(s, mm.base, mm.num, readable, checksum);
  480. }
  481. if (ok > 0)
  482. return 0;
  483. return -ENODEV;
  484. }
  485. #endif /* CONFIG_PCMCIA_PROBE */
  486. /**
  487. * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use
  488. * @s: PCMCIA socket to validate
  489. *
  490. * This is tricky... when we set up CIS memory, we try to validate
  491. * the memory window space allocations.
  492. *
  493. * Locking note: Must be called with skt_mutex held!
  494. */
  495. static int pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s)
  496. {
  497. struct socket_data *s_data = s->resource_data;
  498. unsigned int probe_mask = MEM_PROBE_LOW;
  499. int ret;
  500. if (!probe_mem || !(s->state & SOCKET_PRESENT))
  501. return 0;
  502. if (s->features & SS_CAP_PAGE_REGS)
  503. probe_mask = MEM_PROBE_HIGH;
  504. ret = validate_mem(s, probe_mask);
  505. if (s_data->mem_db_valid.next != &s_data->mem_db_valid)
  506. return 0;
  507. return ret;
  508. }
  509. struct pcmcia_align_data {
  510. unsigned long mask;
  511. unsigned long offset;
  512. struct resource_map *map;
  513. };
  514. static resource_size_t pcmcia_common_align(struct pcmcia_align_data *align_data,
  515. resource_size_t start)
  516. {
  517. resource_size_t ret;
  518. /*
  519. * Ensure that we have the correct start address
  520. */
  521. ret = (start & ~align_data->mask) + align_data->offset;
  522. if (ret < start)
  523. ret += align_data->mask + 1;
  524. return ret;
  525. }
  526. static resource_size_t
  527. pcmcia_align(void *align_data, const struct resource *res,
  528. resource_size_t size, resource_size_t align)
  529. {
  530. struct pcmcia_align_data *data = align_data;
  531. struct resource_map *m;
  532. resource_size_t start;
  533. start = pcmcia_common_align(data, res->start);
  534. for (m = data->map->next; m != data->map; m = m->next) {
  535. unsigned long map_start = m->base;
  536. unsigned long map_end = m->base + m->num - 1;
  537. /*
  538. * If the lower resources are not available, try aligning
  539. * to this entry of the resource database to see if it'll
  540. * fit here.
  541. */
  542. if (start < map_start)
  543. start = pcmcia_common_align(data, map_start);
  544. /*
  545. * If we're above the area which was passed in, there's
  546. * no point proceeding.
  547. */
  548. if (start >= res->end)
  549. break;
  550. if ((start + size - 1) <= map_end)
  551. break;
  552. }
  553. /*
  554. * If we failed to find something suitable, ensure we fail.
  555. */
  556. if (m == data->map)
  557. start = res->end;
  558. return start;
  559. }
  560. /*
  561. * Adjust an existing IO region allocation, but making sure that we don't
  562. * encroach outside the resources which the user supplied.
  563. */
  564. static int __nonstatic_adjust_io_region(struct pcmcia_socket *s,
  565. unsigned long r_start,
  566. unsigned long r_end)
  567. {
  568. struct resource_map *m;
  569. struct socket_data *s_data = s->resource_data;
  570. int ret = -ENOMEM;
  571. for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) {
  572. unsigned long start = m->base;
  573. unsigned long end = m->base + m->num - 1;
  574. if (start > r_start || r_end > end)
  575. continue;
  576. ret = 0;
  577. }
  578. return ret;
  579. }
  580. /*======================================================================
  581. These find ranges of I/O ports or memory addresses that are not
  582. currently allocated by other devices.
  583. The 'align' field should reflect the number of bits of address
  584. that need to be preserved from the initial value of *base. It
  585. should be a power of two, greater than or equal to 'num'. A value
  586. of 0 means that all bits of *base are significant. *base should
  587. also be strictly less than 'align'.
  588. ======================================================================*/
  589. static struct resource *__nonstatic_find_io_region(struct pcmcia_socket *s,
  590. unsigned long base, int num,
  591. unsigned long align)
  592. {
  593. struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
  594. dev_name(&s->dev));
  595. struct socket_data *s_data = s->resource_data;
  596. struct pcmcia_align_data data;
  597. unsigned long min = base;
  598. int ret;
  599. data.mask = align - 1;
  600. data.offset = base & data.mask;
  601. data.map = &s_data->io_db;
  602. #ifdef CONFIG_PCI
  603. if (s->cb_dev) {
  604. ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
  605. min, 0, pcmcia_align, &data);
  606. } else
  607. #endif
  608. ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
  609. 1, pcmcia_align, &data);
  610. if (ret != 0) {
  611. kfree(res);
  612. res = NULL;
  613. }
  614. return res;
  615. }
  616. static int nonstatic_find_io(struct pcmcia_socket *s, unsigned int attr,
  617. unsigned int *base, unsigned int num,
  618. unsigned int align, struct resource **parent)
  619. {
  620. int i, ret = 0;
  621. /* Check for an already-allocated window that must conflict with
  622. * what was asked for. It is a hack because it does not catch all
  623. * potential conflicts, just the most obvious ones.
  624. */
  625. for (i = 0; i < MAX_IO_WIN; i++) {
  626. if (!s->io[i].res)
  627. continue;
  628. if (!*base)
  629. continue;
  630. if ((s->io[i].res->start & (align-1)) == *base)
  631. return -EBUSY;
  632. }
  633. for (i = 0; i < MAX_IO_WIN; i++) {
  634. struct resource *res = s->io[i].res;
  635. unsigned int try;
  636. if (res && (res->flags & IORESOURCE_BITS) !=
  637. (attr & IORESOURCE_BITS))
  638. continue;
  639. if (!res) {
  640. if (align == 0)
  641. align = 0x10000;
  642. res = s->io[i].res = __nonstatic_find_io_region(s,
  643. *base, num,
  644. align);
  645. if (!res)
  646. return -EINVAL;
  647. *base = res->start;
  648. s->io[i].res->flags =
  649. ((res->flags & ~IORESOURCE_BITS) |
  650. (attr & IORESOURCE_BITS));
  651. s->io[i].InUse = num;
  652. *parent = res;
  653. return 0;
  654. }
  655. /* Try to extend top of window */
  656. try = res->end + 1;
  657. if ((*base == 0) || (*base == try)) {
  658. ret = __nonstatic_adjust_io_region(s, res->start,
  659. res->end + num);
  660. if (!ret) {
  661. ret = adjust_resource(s->io[i].res, res->start,
  662. resource_size(res) + num);
  663. if (ret)
  664. continue;
  665. *base = try;
  666. s->io[i].InUse += num;
  667. *parent = res;
  668. return 0;
  669. }
  670. }
  671. /* Try to extend bottom of window */
  672. try = res->start - num;
  673. if ((*base == 0) || (*base == try)) {
  674. ret = __nonstatic_adjust_io_region(s,
  675. res->start - num,
  676. res->end);
  677. if (!ret) {
  678. ret = adjust_resource(s->io[i].res,
  679. res->start - num,
  680. resource_size(res) + num);
  681. if (ret)
  682. continue;
  683. *base = try;
  684. s->io[i].InUse += num;
  685. *parent = res;
  686. return 0;
  687. }
  688. }
  689. }
  690. return -EINVAL;
  691. }
  692. static struct resource *nonstatic_find_mem_region(u_long base, u_long num,
  693. u_long align, int low, struct pcmcia_socket *s)
  694. {
  695. struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_MEM,
  696. dev_name(&s->dev));
  697. struct socket_data *s_data = s->resource_data;
  698. struct pcmcia_align_data data;
  699. unsigned long min, max;
  700. int ret, i, j;
  701. low = low || !(s->features & SS_CAP_PAGE_REGS);
  702. data.mask = align - 1;
  703. data.offset = base & data.mask;
  704. for (i = 0; i < 2; i++) {
  705. data.map = &s_data->mem_db_valid;
  706. if (low) {
  707. max = 0x100000UL;
  708. min = base < max ? base : 0;
  709. } else {
  710. max = ~0UL;
  711. min = 0x100000UL + base;
  712. }
  713. for (j = 0; j < 2; j++) {
  714. #ifdef CONFIG_PCI
  715. if (s->cb_dev) {
  716. ret = pci_bus_alloc_resource(s->cb_dev->bus,
  717. res, num, 1, min, 0,
  718. pcmcia_align, &data);
  719. } else
  720. #endif
  721. {
  722. ret = allocate_resource(&iomem_resource,
  723. res, num, min, max, 1,
  724. pcmcia_align, &data);
  725. }
  726. if (ret == 0)
  727. break;
  728. data.map = &s_data->mem_db;
  729. }
  730. if (ret == 0 || low)
  731. break;
  732. low = 1;
  733. }
  734. if (ret != 0) {
  735. kfree(res);
  736. res = NULL;
  737. }
  738. return res;
  739. }
  740. static int adjust_memory(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
  741. {
  742. struct socket_data *data = s->resource_data;
  743. unsigned long size = end - start + 1;
  744. int ret = 0;
  745. if (end < start)
  746. return -EINVAL;
  747. switch (action) {
  748. case ADD_MANAGED_RESOURCE:
  749. ret = add_interval(&data->mem_db, start, size);
  750. if (!ret)
  751. do_mem_probe(s, start, size, NULL, NULL);
  752. break;
  753. case REMOVE_MANAGED_RESOURCE:
  754. ret = sub_interval(&data->mem_db, start, size);
  755. break;
  756. default:
  757. ret = -EINVAL;
  758. }
  759. return ret;
  760. }
  761. static int adjust_io(struct pcmcia_socket *s, unsigned int action, unsigned long start, unsigned long end)
  762. {
  763. struct socket_data *data = s->resource_data;
  764. unsigned long size;
  765. int ret = 0;
  766. #if defined(CONFIG_X86)
  767. /* on x86, avoid anything < 0x100 for it is often used for
  768. * legacy platform devices */
  769. if (start < 0x100)
  770. start = 0x100;
  771. #endif
  772. size = end - start + 1;
  773. if (end < start)
  774. return -EINVAL;
  775. if (end > IO_SPACE_LIMIT)
  776. return -EINVAL;
  777. switch (action) {
  778. case ADD_MANAGED_RESOURCE:
  779. if (add_interval(&data->io_db, start, size) != 0) {
  780. ret = -EBUSY;
  781. break;
  782. }
  783. #ifdef CONFIG_PCMCIA_PROBE
  784. if (probe_io)
  785. do_io_probe(s, start, size);
  786. #endif
  787. break;
  788. case REMOVE_MANAGED_RESOURCE:
  789. sub_interval(&data->io_db, start, size);
  790. break;
  791. default:
  792. ret = -EINVAL;
  793. break;
  794. }
  795. return ret;
  796. }
  797. #ifdef CONFIG_PCI
  798. static int nonstatic_autoadd_resources(struct pcmcia_socket *s)
  799. {
  800. struct resource *res;
  801. int i, done = 0;
  802. if (!s->cb_dev || !s->cb_dev->bus)
  803. return -ENODEV;
  804. #if defined(CONFIG_X86)
  805. /* If this is the root bus, the risk of hitting some strange
  806. * system devices is too high: If a driver isn't loaded, the
  807. * resources are not claimed; even if a driver is loaded, it
  808. * may not request all resources or even the wrong one. We
  809. * can neither trust the rest of the kernel nor ACPI/PNP and
  810. * CRS parsing to get it right. Therefore, use several
  811. * safeguards:
  812. *
  813. * - Do not auto-add resources if the CardBus bridge is on
  814. * the PCI root bus
  815. *
  816. * - Avoid any I/O ports < 0x100.
  817. *
  818. * - On PCI-PCI bridges, only use resources which are set up
  819. * exclusively for the secondary PCI bus: the risk of hitting
  820. * system devices is quite low, as they usually aren't
  821. * connected to the secondary PCI bus.
  822. */
  823. if (s->cb_dev->bus->number == 0)
  824. return -EINVAL;
  825. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  826. res = s->cb_dev->bus->resource[i];
  827. #else
  828. pci_bus_for_each_resource(s->cb_dev->bus, res, i) {
  829. #endif
  830. if (!res)
  831. continue;
  832. if (res->flags & IORESOURCE_IO) {
  833. /* safeguard against the root resource, where the
  834. * risk of hitting any other device would be too
  835. * high */
  836. if (res == &ioport_resource)
  837. continue;
  838. dev_info(&s->cb_dev->dev,
  839. "pcmcia: parent PCI bridge window: %pR\n",
  840. res);
  841. if (!adjust_io(s, ADD_MANAGED_RESOURCE, res->start, res->end))
  842. done |= IORESOURCE_IO;
  843. }
  844. if (res->flags & IORESOURCE_MEM) {
  845. /* safeguard against the root resource, where the
  846. * risk of hitting any other device would be too
  847. * high */
  848. if (res == &iomem_resource)
  849. continue;
  850. dev_info(&s->cb_dev->dev,
  851. "pcmcia: parent PCI bridge window: %pR\n",
  852. res);
  853. if (!adjust_memory(s, ADD_MANAGED_RESOURCE, res->start, res->end))
  854. done |= IORESOURCE_MEM;
  855. }
  856. }
  857. /* if we got at least one of IO, and one of MEM, we can be glad and
  858. * activate the PCMCIA subsystem */
  859. if (done == (IORESOURCE_MEM | IORESOURCE_IO))
  860. s->resource_setup_done = 1;
  861. return 0;
  862. }
  863. #else
  864. static inline int nonstatic_autoadd_resources(struct pcmcia_socket *s)
  865. {
  866. return -ENODEV;
  867. }
  868. #endif
  869. static int nonstatic_init(struct pcmcia_socket *s)
  870. {
  871. struct socket_data *data;
  872. data = kzalloc(sizeof(struct socket_data), GFP_KERNEL);
  873. if (!data)
  874. return -ENOMEM;
  875. data->mem_db.next = &data->mem_db;
  876. data->mem_db_valid.next = &data->mem_db_valid;
  877. data->io_db.next = &data->io_db;
  878. s->resource_data = (void *) data;
  879. nonstatic_autoadd_resources(s);
  880. return 0;
  881. }
  882. static void nonstatic_release_resource_db(struct pcmcia_socket *s)
  883. {
  884. struct socket_data *data = s->resource_data;
  885. struct resource_map *p, *q;
  886. for (p = data->mem_db_valid.next; p != &data->mem_db_valid; p = q) {
  887. q = p->next;
  888. kfree(p);
  889. }
  890. for (p = data->mem_db.next; p != &data->mem_db; p = q) {
  891. q = p->next;
  892. kfree(p);
  893. }
  894. for (p = data->io_db.next; p != &data->io_db; p = q) {
  895. q = p->next;
  896. kfree(p);
  897. }
  898. }
  899. struct pccard_resource_ops pccard_nonstatic_ops = {
  900. .validate_mem = pcmcia_nonstatic_validate_mem,
  901. .find_io = nonstatic_find_io,
  902. .find_mem = nonstatic_find_mem_region,
  903. .init = nonstatic_init,
  904. .exit = nonstatic_release_resource_db,
  905. };
  906. EXPORT_SYMBOL(pccard_nonstatic_ops);
  907. /* sysfs interface to the resource database */
  908. static ssize_t show_io_db(struct device *dev,
  909. struct device_attribute *attr, char *buf)
  910. {
  911. struct pcmcia_socket *s = dev_get_drvdata(dev);
  912. struct socket_data *data;
  913. struct resource_map *p;
  914. ssize_t ret = 0;
  915. mutex_lock(&s->ops_mutex);
  916. data = s->resource_data;
  917. for (p = data->io_db.next; p != &data->io_db; p = p->next) {
  918. if (ret > (PAGE_SIZE - 10))
  919. continue;
  920. ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
  921. "0x%08lx - 0x%08lx\n",
  922. ((unsigned long) p->base),
  923. ((unsigned long) p->base + p->num - 1));
  924. }
  925. mutex_unlock(&s->ops_mutex);
  926. return ret;
  927. }
  928. static ssize_t store_io_db(struct device *dev,
  929. struct device_attribute *attr,
  930. const char *buf, size_t count)
  931. {
  932. struct pcmcia_socket *s = dev_get_drvdata(dev);
  933. unsigned long start_addr, end_addr;
  934. unsigned int add = ADD_MANAGED_RESOURCE;
  935. ssize_t ret = 0;
  936. ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
  937. if (ret != 2) {
  938. ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
  939. add = REMOVE_MANAGED_RESOURCE;
  940. if (ret != 2) {
  941. ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
  942. &end_addr);
  943. add = ADD_MANAGED_RESOURCE;
  944. if (ret != 2)
  945. return -EINVAL;
  946. }
  947. }
  948. if (end_addr < start_addr)
  949. return -EINVAL;
  950. mutex_lock(&s->ops_mutex);
  951. ret = adjust_io(s, add, start_addr, end_addr);
  952. mutex_unlock(&s->ops_mutex);
  953. return ret ? ret : count;
  954. }
  955. static DEVICE_ATTR(available_resources_io, 0600, show_io_db, store_io_db);
  956. static ssize_t show_mem_db(struct device *dev,
  957. struct device_attribute *attr, char *buf)
  958. {
  959. struct pcmcia_socket *s = dev_get_drvdata(dev);
  960. struct socket_data *data;
  961. struct resource_map *p;
  962. ssize_t ret = 0;
  963. mutex_lock(&s->ops_mutex);
  964. data = s->resource_data;
  965. for (p = data->mem_db_valid.next; p != &data->mem_db_valid;
  966. p = p->next) {
  967. if (ret > (PAGE_SIZE - 10))
  968. continue;
  969. ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
  970. "0x%08lx - 0x%08lx\n",
  971. ((unsigned long) p->base),
  972. ((unsigned long) p->base + p->num - 1));
  973. }
  974. for (p = data->mem_db.next; p != &data->mem_db; p = p->next) {
  975. if (ret > (PAGE_SIZE - 10))
  976. continue;
  977. ret += snprintf(&buf[ret], (PAGE_SIZE - ret - 1),
  978. "0x%08lx - 0x%08lx\n",
  979. ((unsigned long) p->base),
  980. ((unsigned long) p->base + p->num - 1));
  981. }
  982. mutex_unlock(&s->ops_mutex);
  983. return ret;
  984. }
  985. static ssize_t store_mem_db(struct device *dev,
  986. struct device_attribute *attr,
  987. const char *buf, size_t count)
  988. {
  989. struct pcmcia_socket *s = dev_get_drvdata(dev);
  990. unsigned long start_addr, end_addr;
  991. unsigned int add = ADD_MANAGED_RESOURCE;
  992. ssize_t ret = 0;
  993. ret = sscanf(buf, "+ 0x%lx - 0x%lx", &start_addr, &end_addr);
  994. if (ret != 2) {
  995. ret = sscanf(buf, "- 0x%lx - 0x%lx", &start_addr, &end_addr);
  996. add = REMOVE_MANAGED_RESOURCE;
  997. if (ret != 2) {
  998. ret = sscanf(buf, "0x%lx - 0x%lx", &start_addr,
  999. &end_addr);
  1000. add = ADD_MANAGED_RESOURCE;
  1001. if (ret != 2)
  1002. return -EINVAL;
  1003. }
  1004. }
  1005. if (end_addr < start_addr)
  1006. return -EINVAL;
  1007. mutex_lock(&s->ops_mutex);
  1008. ret = adjust_memory(s, add, start_addr, end_addr);
  1009. mutex_unlock(&s->ops_mutex);
  1010. return ret ? ret : count;
  1011. }
  1012. static DEVICE_ATTR(available_resources_mem, 0600, show_mem_db, store_mem_db);
  1013. static struct attribute *pccard_rsrc_attributes[] = {
  1014. &dev_attr_available_resources_io.attr,
  1015. &dev_attr_available_resources_mem.attr,
  1016. NULL,
  1017. };
  1018. static const struct attribute_group rsrc_attributes = {
  1019. .attrs = pccard_rsrc_attributes,
  1020. };
  1021. static int pccard_sysfs_add_rsrc(struct device *dev,
  1022. struct class_interface *class_intf)
  1023. {
  1024. struct pcmcia_socket *s = dev_get_drvdata(dev);
  1025. if (s->resource_ops != &pccard_nonstatic_ops)
  1026. return 0;
  1027. return sysfs_create_group(&dev->kobj, &rsrc_attributes);
  1028. }
  1029. static void pccard_sysfs_remove_rsrc(struct device *dev,
  1030. struct class_interface *class_intf)
  1031. {
  1032. struct pcmcia_socket *s = dev_get_drvdata(dev);
  1033. if (s->resource_ops != &pccard_nonstatic_ops)
  1034. return;
  1035. sysfs_remove_group(&dev->kobj, &rsrc_attributes);
  1036. }
  1037. static struct class_interface pccard_rsrc_interface __refdata = {
  1038. .class = &pcmcia_socket_class,
  1039. .add_dev = &pccard_sysfs_add_rsrc,
  1040. .remove_dev = &pccard_sysfs_remove_rsrc,
  1041. };
  1042. static int __init nonstatic_sysfs_init(void)
  1043. {
  1044. return class_interface_register(&pccard_rsrc_interface);
  1045. }
  1046. static void __exit nonstatic_sysfs_exit(void)
  1047. {
  1048. class_interface_unregister(&pccard_rsrc_interface);
  1049. }
  1050. module_init(nonstatic_sysfs_init);
  1051. module_exit(nonstatic_sysfs_exit);