rsrc_nonstatic.c 30 KB

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