rsrc_nonstatic.c 27 KB

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