rsrc_nonstatic.c 30 KB

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