pcmcia_resource.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*
  2. * PCMCIA 16-bit resource management functions
  3. *
  4. * The initial developer of the original code is David A. Hinds
  5. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  6. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  7. *
  8. * Copyright (C) 1999 David A. Hinds
  9. * Copyright (C) 2004-2005 Dominik Brodowski
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/pci.h>
  21. #include <linux/device.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/slab.h>
  24. #include <pcmcia/cs_types.h>
  25. #include <pcmcia/ss.h>
  26. #include <pcmcia/cs.h>
  27. #include <pcmcia/cistpl.h>
  28. #include <pcmcia/cisreg.h>
  29. #include <pcmcia/ds.h>
  30. #include "cs_internal.h"
  31. /* Access speed for IO windows */
  32. static int io_speed;
  33. module_param(io_speed, int, 0444);
  34. #ifdef CONFIG_PCMCIA_PROBE
  35. #include <asm/irq.h>
  36. /* mask of IRQs already reserved by other cards, we should avoid using them */
  37. static u8 pcmcia_used_irq[NR_IRQS];
  38. #endif
  39. static int pcmcia_adjust_io_region(struct resource *res, unsigned long start,
  40. unsigned long end, struct pcmcia_socket *s)
  41. {
  42. if (s->resource_ops->adjust_io_region)
  43. return s->resource_ops->adjust_io_region(res, start, end, s);
  44. return -ENOMEM;
  45. }
  46. static struct resource *pcmcia_find_io_region(unsigned long base, int num,
  47. unsigned long align,
  48. struct pcmcia_socket *s)
  49. {
  50. if (s->resource_ops->find_io)
  51. return s->resource_ops->find_io(base, num, align, s);
  52. return NULL;
  53. }
  54. int pcmcia_validate_mem(struct pcmcia_socket *s)
  55. {
  56. if (s->resource_ops->validate_mem)
  57. return s->resource_ops->validate_mem(s);
  58. /* if there is no callback, we can assume that everything is OK */
  59. return 0;
  60. }
  61. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  62. int low, struct pcmcia_socket *s)
  63. {
  64. if (s->resource_ops->find_mem)
  65. return s->resource_ops->find_mem(base, num, align, low, s);
  66. return NULL;
  67. }
  68. /** alloc_io_space
  69. *
  70. * Special stuff for managing IO windows, because they are scarce
  71. */
  72. static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
  73. unsigned int *base, unsigned int num, u_int lines)
  74. {
  75. int i;
  76. unsigned int try, align;
  77. align = (*base) ? (lines ? 1<<lines : 0) : 1;
  78. if (align && (align < num)) {
  79. if (*base) {
  80. dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
  81. num, align);
  82. align = 0;
  83. } else
  84. while (align && (align < num))
  85. align <<= 1;
  86. }
  87. if (*base & ~(align-1)) {
  88. dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
  89. *base, align);
  90. align = 0;
  91. }
  92. if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
  93. *base = s->io_offset | (*base & 0x0fff);
  94. return 0;
  95. }
  96. /* Check for an already-allocated window that must conflict with
  97. * what was asked for. It is a hack because it does not catch all
  98. * potential conflicts, just the most obvious ones.
  99. */
  100. for (i = 0; i < MAX_IO_WIN; i++)
  101. if ((s->io[i].res) && *base &&
  102. ((s->io[i].res->start & (align-1)) == *base))
  103. return 1;
  104. for (i = 0; i < MAX_IO_WIN; i++) {
  105. if (!s->io[i].res) {
  106. s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
  107. if (s->io[i].res) {
  108. *base = s->io[i].res->start;
  109. s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
  110. s->io[i].InUse = num;
  111. break;
  112. } else
  113. return 1;
  114. } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
  115. continue;
  116. /* Try to extend top of window */
  117. try = s->io[i].res->end + 1;
  118. if ((*base == 0) || (*base == try))
  119. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
  120. s->io[i].res->end + num, s) == 0) {
  121. *base = try;
  122. s->io[i].InUse += num;
  123. break;
  124. }
  125. /* Try to extend bottom of window */
  126. try = s->io[i].res->start - num;
  127. if ((*base == 0) || (*base == try))
  128. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
  129. s->io[i].res->end, s) == 0) {
  130. *base = try;
  131. s->io[i].InUse += num;
  132. break;
  133. }
  134. }
  135. return (i == MAX_IO_WIN);
  136. } /* alloc_io_space */
  137. static void release_io_space(struct pcmcia_socket *s, unsigned int base,
  138. unsigned int num)
  139. {
  140. int i;
  141. for (i = 0; i < MAX_IO_WIN; i++) {
  142. if (!s->io[i].res)
  143. continue;
  144. if ((s->io[i].res->start <= base) &&
  145. (s->io[i].res->end >= base+num-1)) {
  146. s->io[i].InUse -= num;
  147. /* Free the window if no one else is using it */
  148. if (s->io[i].InUse == 0) {
  149. release_resource(s->io[i].res);
  150. kfree(s->io[i].res);
  151. s->io[i].res = NULL;
  152. }
  153. }
  154. }
  155. } /* release_io_space */
  156. /** pccard_access_configuration_register
  157. *
  158. * Access_configuration_register() reads and writes configuration
  159. * registers in attribute memory. Memory window 0 is reserved for
  160. * this and the tuple reading services.
  161. */
  162. int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
  163. conf_reg_t *reg)
  164. {
  165. struct pcmcia_socket *s;
  166. config_t *c;
  167. int addr;
  168. u_char val;
  169. if (!p_dev || !p_dev->function_config)
  170. return -EINVAL;
  171. s = p_dev->socket;
  172. mutex_lock(&s->ops_mutex);
  173. c = p_dev->function_config;
  174. if (!(c->state & CONFIG_LOCKED)) {
  175. dev_dbg(&s->dev, "Configuration isnt't locked\n");
  176. mutex_unlock(&s->ops_mutex);
  177. return -EACCES;
  178. }
  179. addr = (c->ConfigBase + reg->Offset) >> 1;
  180. mutex_unlock(&s->ops_mutex);
  181. switch (reg->Action) {
  182. case CS_READ:
  183. pcmcia_read_cis_mem(s, 1, addr, 1, &val);
  184. reg->Value = val;
  185. break;
  186. case CS_WRITE:
  187. val = reg->Value;
  188. pcmcia_write_cis_mem(s, 1, addr, 1, &val);
  189. break;
  190. default:
  191. dev_dbg(&s->dev, "Invalid conf register request\n");
  192. return -EINVAL;
  193. break;
  194. }
  195. return 0;
  196. } /* pcmcia_access_configuration_register */
  197. EXPORT_SYMBOL(pcmcia_access_configuration_register);
  198. int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
  199. memreq_t *req)
  200. {
  201. struct pcmcia_socket *s = p_dev->socket;
  202. int ret;
  203. wh--;
  204. if (wh >= MAX_WIN)
  205. return -EINVAL;
  206. if (req->Page != 0) {
  207. dev_dbg(&s->dev, "failure: requested page is zero\n");
  208. return -EINVAL;
  209. }
  210. mutex_lock(&s->ops_mutex);
  211. s->win[wh].card_start = req->CardOffset;
  212. ret = s->ops->set_mem_map(s, &s->win[wh]);
  213. if (ret)
  214. dev_warn(&s->dev, "failed to set_mem_map\n");
  215. mutex_unlock(&s->ops_mutex);
  216. return ret;
  217. } /* pcmcia_map_mem_page */
  218. EXPORT_SYMBOL(pcmcia_map_mem_page);
  219. /** pcmcia_modify_configuration
  220. *
  221. * Modify a locked socket configuration
  222. */
  223. int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
  224. modconf_t *mod)
  225. {
  226. struct pcmcia_socket *s;
  227. config_t *c;
  228. int ret;
  229. s = p_dev->socket;
  230. mutex_lock(&s->ops_mutex);
  231. c = p_dev->function_config;
  232. if (!(s->state & SOCKET_PRESENT)) {
  233. dev_dbg(&s->dev, "No card present\n");
  234. ret = -ENODEV;
  235. goto unlock;
  236. }
  237. if (!(c->state & CONFIG_LOCKED)) {
  238. dev_dbg(&s->dev, "Configuration isnt't locked\n");
  239. ret = -EACCES;
  240. goto unlock;
  241. }
  242. if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
  243. dev_dbg(&s->dev,
  244. "changing Vcc or IRQ is not allowed at this time\n");
  245. ret = -EINVAL;
  246. goto unlock;
  247. }
  248. /* We only allow changing Vpp1 and Vpp2 to the same value */
  249. if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
  250. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  251. if (mod->Vpp1 != mod->Vpp2) {
  252. dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
  253. ret = -EINVAL;
  254. goto unlock;
  255. }
  256. s->socket.Vpp = mod->Vpp1;
  257. if (s->ops->set_socket(s, &s->socket)) {
  258. dev_printk(KERN_WARNING, &s->dev,
  259. "Unable to set VPP\n");
  260. ret = -EIO;
  261. goto unlock;
  262. }
  263. } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
  264. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  265. dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
  266. ret = -EINVAL;
  267. goto unlock;
  268. }
  269. if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
  270. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  271. pccard_io_map io_on;
  272. int i;
  273. io_on.speed = io_speed;
  274. for (i = 0; i < MAX_IO_WIN; i++) {
  275. if (!s->io[i].res)
  276. continue;
  277. io_off.map = i;
  278. io_on.map = i;
  279. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  280. io_on.start = s->io[i].res->start;
  281. io_on.stop = s->io[i].res->end;
  282. s->ops->set_io_map(s, &io_off);
  283. mdelay(40);
  284. s->ops->set_io_map(s, &io_on);
  285. }
  286. }
  287. ret = 0;
  288. unlock:
  289. mutex_unlock(&s->ops_mutex);
  290. return ret;
  291. } /* modify_configuration */
  292. EXPORT_SYMBOL(pcmcia_modify_configuration);
  293. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  294. {
  295. pccard_io_map io = { 0, 0, 0, 0, 1 };
  296. struct pcmcia_socket *s = p_dev->socket;
  297. config_t *c;
  298. int i;
  299. mutex_lock(&s->ops_mutex);
  300. c = p_dev->function_config;
  301. if (p_dev->_locked) {
  302. p_dev->_locked = 0;
  303. if (--(s->lock_count) == 0) {
  304. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  305. s->socket.Vpp = 0;
  306. s->socket.io_irq = 0;
  307. s->ops->set_socket(s, &s->socket);
  308. }
  309. }
  310. if (c->state & CONFIG_LOCKED) {
  311. c->state &= ~CONFIG_LOCKED;
  312. if (c->state & CONFIG_IO_REQ)
  313. for (i = 0; i < MAX_IO_WIN; i++) {
  314. if (!s->io[i].res)
  315. continue;
  316. s->io[i].Config--;
  317. if (s->io[i].Config != 0)
  318. continue;
  319. io.map = i;
  320. s->ops->set_io_map(s, &io);
  321. }
  322. }
  323. mutex_unlock(&s->ops_mutex);
  324. return 0;
  325. } /* pcmcia_release_configuration */
  326. /** pcmcia_release_io
  327. *
  328. * Release_io() releases the I/O ranges allocated by a client. This
  329. * may be invoked some time after a card ejection has already dumped
  330. * the actual socket configuration, so if the client is "stale", we
  331. * don't bother checking the port ranges against the current socket
  332. * values.
  333. */
  334. static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
  335. {
  336. struct pcmcia_socket *s = p_dev->socket;
  337. int ret = -EINVAL;
  338. config_t *c;
  339. mutex_lock(&s->ops_mutex);
  340. c = p_dev->function_config;
  341. if (!p_dev->_io)
  342. goto out;
  343. p_dev->_io = 0;
  344. if ((c->io.BasePort1 != req->BasePort1) ||
  345. (c->io.NumPorts1 != req->NumPorts1) ||
  346. (c->io.BasePort2 != req->BasePort2) ||
  347. (c->io.NumPorts2 != req->NumPorts2))
  348. goto out;
  349. c->state &= ~CONFIG_IO_REQ;
  350. release_io_space(s, req->BasePort1, req->NumPorts1);
  351. if (req->NumPorts2)
  352. release_io_space(s, req->BasePort2, req->NumPorts2);
  353. out:
  354. mutex_unlock(&s->ops_mutex);
  355. return ret;
  356. } /* pcmcia_release_io */
  357. static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  358. {
  359. struct pcmcia_socket *s = p_dev->socket;
  360. config_t *c;
  361. int ret = -EINVAL;
  362. mutex_lock(&s->ops_mutex);
  363. c = p_dev->function_config;
  364. if (!p_dev->_irq)
  365. goto out;
  366. p_dev->_irq = 0;
  367. if (c->state & CONFIG_LOCKED)
  368. goto out;
  369. if (c->irq.Attributes != req->Attributes) {
  370. dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
  371. goto out;
  372. }
  373. if (s->irq.AssignedIRQ != req->AssignedIRQ) {
  374. dev_dbg(&s->dev, "IRQ must match assigned one\n");
  375. goto out;
  376. }
  377. if (--s->irq.Config == 0) {
  378. c->state &= ~CONFIG_IRQ_REQ;
  379. s->irq.AssignedIRQ = 0;
  380. }
  381. if (req->Handler)
  382. free_irq(req->AssignedIRQ, p_dev->priv);
  383. #ifdef CONFIG_PCMCIA_PROBE
  384. pcmcia_used_irq[req->AssignedIRQ]--;
  385. #endif
  386. ret = 0;
  387. out:
  388. mutex_unlock(&s->ops_mutex);
  389. return ret;
  390. } /* pcmcia_release_irq */
  391. int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
  392. {
  393. struct pcmcia_socket *s = p_dev->socket;
  394. pccard_mem_map *win;
  395. wh--;
  396. if (wh >= MAX_WIN)
  397. return -EINVAL;
  398. mutex_lock(&s->ops_mutex);
  399. win = &s->win[wh];
  400. if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
  401. dev_dbg(&s->dev, "not releasing unknown window\n");
  402. mutex_unlock(&s->ops_mutex);
  403. return -EINVAL;
  404. }
  405. /* Shut down memory window */
  406. win->flags &= ~MAP_ACTIVE;
  407. s->ops->set_mem_map(s, win);
  408. s->state &= ~SOCKET_WIN_REQ(wh);
  409. /* Release system memory */
  410. if (win->res) {
  411. release_resource(win->res);
  412. kfree(win->res);
  413. win->res = NULL;
  414. }
  415. p_dev->_win &= ~CLIENT_WIN_REQ(wh);
  416. mutex_unlock(&s->ops_mutex);
  417. return 0;
  418. } /* pcmcia_release_window */
  419. EXPORT_SYMBOL(pcmcia_release_window);
  420. int pcmcia_request_configuration(struct pcmcia_device *p_dev,
  421. config_req_t *req)
  422. {
  423. int i;
  424. u_int base;
  425. struct pcmcia_socket *s = p_dev->socket;
  426. config_t *c;
  427. pccard_io_map iomap;
  428. if (!(s->state & SOCKET_PRESENT))
  429. return -ENODEV;
  430. if (req->IntType & INT_CARDBUS) {
  431. dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
  432. return -EINVAL;
  433. }
  434. mutex_lock(&s->ops_mutex);
  435. c = p_dev->function_config;
  436. if (c->state & CONFIG_LOCKED) {
  437. mutex_unlock(&s->ops_mutex);
  438. dev_dbg(&s->dev, "Configuration is locked\n");
  439. return -EACCES;
  440. }
  441. /* Do power control. We don't allow changes in Vcc. */
  442. s->socket.Vpp = req->Vpp;
  443. if (s->ops->set_socket(s, &s->socket)) {
  444. mutex_unlock(&s->ops_mutex);
  445. dev_printk(KERN_WARNING, &s->dev,
  446. "Unable to set socket state\n");
  447. return -EINVAL;
  448. }
  449. /* Pick memory or I/O card, DMA mode, interrupt */
  450. c->IntType = req->IntType;
  451. c->Attributes = req->Attributes;
  452. if (req->IntType & INT_MEMORY_AND_IO)
  453. s->socket.flags |= SS_IOCARD;
  454. if (req->IntType & INT_ZOOMED_VIDEO)
  455. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  456. if (req->Attributes & CONF_ENABLE_DMA)
  457. s->socket.flags |= SS_DMA_MODE;
  458. if (req->Attributes & CONF_ENABLE_SPKR)
  459. s->socket.flags |= SS_SPKR_ENA;
  460. if (req->Attributes & CONF_ENABLE_IRQ)
  461. s->socket.io_irq = s->irq.AssignedIRQ;
  462. else
  463. s->socket.io_irq = 0;
  464. s->ops->set_socket(s, &s->socket);
  465. s->lock_count++;
  466. mutex_unlock(&s->ops_mutex);
  467. /* Set up CIS configuration registers */
  468. base = c->ConfigBase = req->ConfigBase;
  469. c->CardValues = req->Present;
  470. if (req->Present & PRESENT_COPY) {
  471. c->Copy = req->Copy;
  472. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
  473. }
  474. if (req->Present & PRESENT_OPTION) {
  475. if (s->functions == 1) {
  476. c->Option = req->ConfigIndex & COR_CONFIG_MASK;
  477. } else {
  478. c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
  479. c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
  480. if (req->Present & PRESENT_IOBASE_0)
  481. c->Option |= COR_ADDR_DECODE;
  482. }
  483. if (c->state & CONFIG_IRQ_REQ)
  484. if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
  485. c->Option |= COR_LEVEL_REQ;
  486. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
  487. mdelay(40);
  488. }
  489. if (req->Present & PRESENT_STATUS) {
  490. c->Status = req->Status;
  491. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
  492. }
  493. if (req->Present & PRESENT_PIN_REPLACE) {
  494. c->Pin = req->Pin;
  495. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
  496. }
  497. if (req->Present & PRESENT_EXT_STATUS) {
  498. c->ExtStatus = req->ExtStatus;
  499. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
  500. }
  501. if (req->Present & PRESENT_IOBASE_0) {
  502. u_char b = c->io.BasePort1 & 0xff;
  503. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  504. b = (c->io.BasePort1 >> 8) & 0xff;
  505. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  506. }
  507. if (req->Present & PRESENT_IOSIZE) {
  508. u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
  509. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  510. }
  511. /* Configure I/O windows */
  512. if (c->state & CONFIG_IO_REQ) {
  513. mutex_lock(&s->ops_mutex);
  514. iomap.speed = io_speed;
  515. for (i = 0; i < MAX_IO_WIN; i++)
  516. if (s->io[i].res) {
  517. iomap.map = i;
  518. iomap.flags = MAP_ACTIVE;
  519. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  520. case IO_DATA_PATH_WIDTH_16:
  521. iomap.flags |= MAP_16BIT; break;
  522. case IO_DATA_PATH_WIDTH_AUTO:
  523. iomap.flags |= MAP_AUTOSZ; break;
  524. default:
  525. break;
  526. }
  527. iomap.start = s->io[i].res->start;
  528. iomap.stop = s->io[i].res->end;
  529. s->ops->set_io_map(s, &iomap);
  530. s->io[i].Config++;
  531. }
  532. mutex_unlock(&s->ops_mutex);
  533. }
  534. c->state |= CONFIG_LOCKED;
  535. p_dev->_locked = 1;
  536. return 0;
  537. } /* pcmcia_request_configuration */
  538. EXPORT_SYMBOL(pcmcia_request_configuration);
  539. /** pcmcia_request_io
  540. *
  541. * Request_io() reserves ranges of port addresses for a socket.
  542. * I have not implemented range sharing or alias addressing.
  543. */
  544. int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
  545. {
  546. struct pcmcia_socket *s = p_dev->socket;
  547. config_t *c;
  548. int ret = -EINVAL;
  549. mutex_lock(&s->ops_mutex);
  550. if (!(s->state & SOCKET_PRESENT)) {
  551. dev_dbg(&s->dev, "No card present\n");
  552. goto out;
  553. }
  554. if (!req)
  555. goto out;
  556. c = p_dev->function_config;
  557. if (c->state & CONFIG_LOCKED) {
  558. dev_dbg(&s->dev, "Configuration is locked\n");
  559. goto out;
  560. }
  561. if (c->state & CONFIG_IO_REQ) {
  562. dev_dbg(&s->dev, "IO already configured\n");
  563. goto out;
  564. }
  565. if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
  566. dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
  567. goto out;
  568. }
  569. if ((req->NumPorts2 > 0) &&
  570. (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
  571. dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
  572. goto out;
  573. }
  574. dev_dbg(&s->dev, "trying to allocate resource 1\n");
  575. ret = alloc_io_space(s, req->Attributes1, &req->BasePort1,
  576. req->NumPorts1, req->IOAddrLines);
  577. if (ret) {
  578. dev_dbg(&s->dev, "allocation of resource 1 failed\n");
  579. goto out;
  580. }
  581. if (req->NumPorts2) {
  582. dev_dbg(&s->dev, "trying to allocate resource 2\n");
  583. ret = alloc_io_space(s, req->Attributes2, &req->BasePort2,
  584. req->NumPorts2, req->IOAddrLines);
  585. if (ret) {
  586. dev_dbg(&s->dev, "allocation of resource 2 failed\n");
  587. release_io_space(s, req->BasePort1, req->NumPorts1);
  588. goto out;
  589. }
  590. }
  591. c->io = *req;
  592. c->state |= CONFIG_IO_REQ;
  593. p_dev->_io = 1;
  594. dev_dbg(&s->dev, "allocating resources succeeded: %d\n", ret);
  595. out:
  596. mutex_unlock(&s->ops_mutex);
  597. return ret;
  598. } /* pcmcia_request_io */
  599. EXPORT_SYMBOL(pcmcia_request_io);
  600. /** pcmcia_request_irq
  601. *
  602. * Request_irq() reserves an irq for this client.
  603. *
  604. * Also, since Linux only reserves irq's when they are actually
  605. * hooked, we don't guarantee that an irq will still be available
  606. * when the configuration is locked. Now that I think about it,
  607. * there might be a way to fix this using a dummy handler.
  608. */
  609. #ifdef CONFIG_PCMCIA_PROBE
  610. static irqreturn_t test_action(int cpl, void *dev_id)
  611. {
  612. return IRQ_NONE;
  613. }
  614. #endif
  615. int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  616. {
  617. struct pcmcia_socket *s = p_dev->socket;
  618. config_t *c;
  619. int ret = -EINVAL, irq = 0;
  620. int type;
  621. mutex_lock(&s->ops_mutex);
  622. if (!(s->state & SOCKET_PRESENT)) {
  623. dev_dbg(&s->dev, "No card present\n");
  624. goto out;
  625. }
  626. c = p_dev->function_config;
  627. if (c->state & CONFIG_LOCKED) {
  628. dev_dbg(&s->dev, "Configuration is locked\n");
  629. goto out;
  630. }
  631. if (c->state & CONFIG_IRQ_REQ) {
  632. dev_dbg(&s->dev, "IRQ already configured\n");
  633. goto out;
  634. }
  635. /* Decide what type of interrupt we are registering */
  636. type = 0;
  637. if (s->functions > 1) /* All of this ought to be handled higher up */
  638. type = IRQF_SHARED;
  639. else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
  640. type = IRQF_SHARED;
  641. else
  642. printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
  643. /* If the interrupt is already assigned, it must be the same */
  644. if (s->irq.AssignedIRQ != 0)
  645. irq = s->irq.AssignedIRQ;
  646. #ifdef CONFIG_PCMCIA_PROBE
  647. if (!irq) {
  648. int try;
  649. u32 mask = s->irq_mask;
  650. void *data = p_dev; /* something unique to this device */
  651. for (try = 0; try < 64; try++) {
  652. irq = try % 32;
  653. /* marked as available by driver, and not blocked by userspace? */
  654. if (!((mask >> irq) & 1))
  655. continue;
  656. /* avoid an IRQ which is already used by a PCMCIA card */
  657. if ((try < 32) && pcmcia_used_irq[irq])
  658. continue;
  659. /* register the correct driver, if possible, of check whether
  660. * registering a dummy handle works, i.e. if the IRQ isn't
  661. * marked as used by the kernel resource management core */
  662. ret = request_irq(irq,
  663. (req->Handler) ? req->Handler : test_action,
  664. type,
  665. p_dev->devname,
  666. (req->Handler) ? p_dev->priv : data);
  667. if (!ret) {
  668. if (!req->Handler)
  669. free_irq(irq, data);
  670. break;
  671. }
  672. }
  673. }
  674. #endif
  675. /* only assign PCI irq if no IRQ already assigned */
  676. if (ret && !s->irq.AssignedIRQ) {
  677. if (!s->pci_irq) {
  678. dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
  679. goto out;
  680. }
  681. type = IRQF_SHARED;
  682. irq = s->pci_irq;
  683. }
  684. if (ret && req->Handler) {
  685. ret = request_irq(irq, req->Handler, type,
  686. p_dev->devname, p_dev->priv);
  687. if (ret) {
  688. dev_printk(KERN_INFO, &s->dev,
  689. "request_irq() failed\n");
  690. goto out;
  691. }
  692. }
  693. /* Make sure the fact the request type was overridden is passed back */
  694. if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
  695. req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
  696. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
  697. "request for exclusive IRQ could not be fulfilled.\n");
  698. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
  699. "needs updating to supported shared IRQ lines.\n");
  700. }
  701. c->irq.Attributes = req->Attributes;
  702. s->irq.AssignedIRQ = req->AssignedIRQ = irq;
  703. s->irq.Config++;
  704. c->state |= CONFIG_IRQ_REQ;
  705. p_dev->_irq = 1;
  706. #ifdef CONFIG_PCMCIA_PROBE
  707. pcmcia_used_irq[irq]++;
  708. #endif
  709. ret = 0;
  710. out:
  711. mutex_unlock(&s->ops_mutex);
  712. return ret;
  713. } /* pcmcia_request_irq */
  714. EXPORT_SYMBOL(pcmcia_request_irq);
  715. /** pcmcia_request_window
  716. *
  717. * Request_window() establishes a mapping between card memory space
  718. * and system memory space.
  719. */
  720. int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
  721. {
  722. struct pcmcia_socket *s = p_dev->socket;
  723. pccard_mem_map *win;
  724. u_long align;
  725. int w;
  726. if (!(s->state & SOCKET_PRESENT)) {
  727. dev_dbg(&s->dev, "No card present\n");
  728. return -ENODEV;
  729. }
  730. if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
  731. dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
  732. return -EINVAL;
  733. }
  734. /* Window size defaults to smallest available */
  735. if (req->Size == 0)
  736. req->Size = s->map_size;
  737. align = (((s->features & SS_CAP_MEM_ALIGN) ||
  738. (req->Attributes & WIN_STRICT_ALIGN)) ?
  739. req->Size : s->map_size);
  740. if (req->Size & (s->map_size-1)) {
  741. dev_dbg(&s->dev, "invalid map size\n");
  742. return -EINVAL;
  743. }
  744. if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
  745. (req->Base & (align-1))) {
  746. dev_dbg(&s->dev, "invalid base address\n");
  747. return -EINVAL;
  748. }
  749. if (req->Base)
  750. align = 0;
  751. /* Allocate system memory window */
  752. for (w = 0; w < MAX_WIN; w++)
  753. if (!(s->state & SOCKET_WIN_REQ(w)))
  754. break;
  755. if (w == MAX_WIN) {
  756. dev_dbg(&s->dev, "all windows are used already\n");
  757. return -EINVAL;
  758. }
  759. mutex_lock(&s->ops_mutex);
  760. win = &s->win[w];
  761. if (!(s->features & SS_CAP_STATIC_MAP)) {
  762. win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
  763. (req->Attributes & WIN_MAP_BELOW_1MB), s);
  764. if (!win->res) {
  765. dev_dbg(&s->dev, "allocating mem region failed\n");
  766. mutex_unlock(&s->ops_mutex);
  767. return -EINVAL;
  768. }
  769. }
  770. p_dev->_win |= CLIENT_WIN_REQ(w);
  771. /* Configure the socket controller */
  772. win->map = w+1;
  773. win->flags = 0;
  774. win->speed = req->AccessSpeed;
  775. if (req->Attributes & WIN_MEMORY_TYPE)
  776. win->flags |= MAP_ATTRIB;
  777. if (req->Attributes & WIN_ENABLE)
  778. win->flags |= MAP_ACTIVE;
  779. if (req->Attributes & WIN_DATA_WIDTH_16)
  780. win->flags |= MAP_16BIT;
  781. if (req->Attributes & WIN_USE_WAIT)
  782. win->flags |= MAP_USE_WAIT;
  783. win->card_start = 0;
  784. if (s->ops->set_mem_map(s, win) != 0) {
  785. dev_dbg(&s->dev, "failed to set memory mapping\n");
  786. mutex_unlock(&s->ops_mutex);
  787. return -EIO;
  788. }
  789. s->state |= SOCKET_WIN_REQ(w);
  790. /* Return window handle */
  791. if (s->features & SS_CAP_STATIC_MAP)
  792. req->Base = win->static_start;
  793. else
  794. req->Base = win->res->start;
  795. mutex_unlock(&s->ops_mutex);
  796. *wh = w + 1;
  797. return 0;
  798. } /* pcmcia_request_window */
  799. EXPORT_SYMBOL(pcmcia_request_window);
  800. void pcmcia_disable_device(struct pcmcia_device *p_dev)
  801. {
  802. pcmcia_release_configuration(p_dev);
  803. pcmcia_release_io(p_dev, &p_dev->io);
  804. pcmcia_release_irq(p_dev, &p_dev->irq);
  805. if (p_dev->win)
  806. pcmcia_release_window(p_dev, p_dev->win);
  807. }
  808. EXPORT_SYMBOL(pcmcia_disable_device);
  809. struct pcmcia_cfg_mem {
  810. struct pcmcia_device *p_dev;
  811. void *priv_data;
  812. int (*conf_check) (struct pcmcia_device *p_dev,
  813. cistpl_cftable_entry_t *cfg,
  814. cistpl_cftable_entry_t *dflt,
  815. unsigned int vcc,
  816. void *priv_data);
  817. cisparse_t parse;
  818. cistpl_cftable_entry_t dflt;
  819. };
  820. /**
  821. * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
  822. *
  823. * pcmcia_do_loop_config() is the internal callback for the call from
  824. * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
  825. * by a struct pcmcia_cfg_mem.
  826. */
  827. static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
  828. {
  829. cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
  830. struct pcmcia_cfg_mem *cfg_mem = priv;
  831. /* default values */
  832. cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
  833. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  834. cfg_mem->dflt = *cfg;
  835. return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
  836. cfg_mem->p_dev->socket->socket.Vcc,
  837. cfg_mem->priv_data);
  838. }
  839. /**
  840. * pcmcia_loop_config() - loop over configuration options
  841. * @p_dev: the struct pcmcia_device which we need to loop for.
  842. * @conf_check: function to call for each configuration option.
  843. * It gets passed the struct pcmcia_device, the CIS data
  844. * describing the configuration option, and private data
  845. * being passed to pcmcia_loop_config()
  846. * @priv_data: private data to be passed to the conf_check function.
  847. *
  848. * pcmcia_loop_config() loops over all configuration options, and calls
  849. * the driver-specific conf_check() for each one, checking whether
  850. * it is a valid one. Returns 0 on success or errorcode otherwise.
  851. */
  852. int pcmcia_loop_config(struct pcmcia_device *p_dev,
  853. int (*conf_check) (struct pcmcia_device *p_dev,
  854. cistpl_cftable_entry_t *cfg,
  855. cistpl_cftable_entry_t *dflt,
  856. unsigned int vcc,
  857. void *priv_data),
  858. void *priv_data)
  859. {
  860. struct pcmcia_cfg_mem *cfg_mem;
  861. int ret;
  862. cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
  863. if (cfg_mem == NULL)
  864. return -ENOMEM;
  865. cfg_mem->p_dev = p_dev;
  866. cfg_mem->conf_check = conf_check;
  867. cfg_mem->priv_data = priv_data;
  868. ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
  869. CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
  870. cfg_mem, pcmcia_do_loop_config);
  871. kfree(cfg_mem);
  872. return ret;
  873. }
  874. EXPORT_SYMBOL(pcmcia_loop_config);
  875. struct pcmcia_loop_mem {
  876. struct pcmcia_device *p_dev;
  877. void *priv_data;
  878. int (*loop_tuple) (struct pcmcia_device *p_dev,
  879. tuple_t *tuple,
  880. void *priv_data);
  881. };
  882. /**
  883. * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
  884. *
  885. * pcmcia_do_loop_tuple() is the internal callback for the call from
  886. * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
  887. * by a struct pcmcia_cfg_mem.
  888. */
  889. static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
  890. {
  891. struct pcmcia_loop_mem *loop = priv;
  892. return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
  893. };
  894. /**
  895. * pcmcia_loop_tuple() - loop over tuples in the CIS
  896. * @p_dev: the struct pcmcia_device which we need to loop for.
  897. * @code: which CIS code shall we look for?
  898. * @priv_data: private data to be passed to the loop_tuple function.
  899. * @loop_tuple: function to call for each CIS entry of type @function. IT
  900. * gets passed the raw tuple and @priv_data.
  901. *
  902. * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
  903. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  904. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  905. */
  906. int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  907. int (*loop_tuple) (struct pcmcia_device *p_dev,
  908. tuple_t *tuple,
  909. void *priv_data),
  910. void *priv_data)
  911. {
  912. struct pcmcia_loop_mem loop = {
  913. .p_dev = p_dev,
  914. .loop_tuple = loop_tuple,
  915. .priv_data = priv_data};
  916. return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
  917. &loop, pcmcia_do_loop_tuple);
  918. }
  919. EXPORT_SYMBOL(pcmcia_loop_tuple);
  920. struct pcmcia_loop_get {
  921. size_t len;
  922. cisdata_t **buf;
  923. };
  924. /**
  925. * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
  926. *
  927. * pcmcia_do_get_tuple() is the internal callback for the call from
  928. * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
  929. * the first tuple, return 0 unconditionally. Create a memory buffer large
  930. * enough to hold the content of the tuple, and fill it with the tuple data.
  931. * The caller is responsible to free the buffer.
  932. */
  933. static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
  934. void *priv)
  935. {
  936. struct pcmcia_loop_get *get = priv;
  937. *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
  938. if (*get->buf) {
  939. get->len = tuple->TupleDataLen;
  940. memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
  941. } else
  942. dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
  943. return 0;
  944. }
  945. /**
  946. * pcmcia_get_tuple() - get first tuple from CIS
  947. * @p_dev: the struct pcmcia_device which we need to loop for.
  948. * @code: which CIS code shall we look for?
  949. * @buf: pointer to store the buffer to.
  950. *
  951. * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
  952. * It returns the buffer length (or zero). The caller is responsible to free
  953. * the buffer passed in @buf.
  954. */
  955. size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  956. unsigned char **buf)
  957. {
  958. struct pcmcia_loop_get get = {
  959. .len = 0,
  960. .buf = buf,
  961. };
  962. *get.buf = NULL;
  963. pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
  964. return get.len;
  965. }
  966. EXPORT_SYMBOL(pcmcia_get_tuple);
  967. /**
  968. * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
  969. *
  970. * pcmcia_do_get_mac() is the internal callback for the call from
  971. * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
  972. * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
  973. * to struct net_device->dev_addr[i].
  974. */
  975. static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
  976. void *priv)
  977. {
  978. struct net_device *dev = priv;
  979. int i;
  980. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  981. return -EINVAL;
  982. if (tuple->TupleDataLen < ETH_ALEN + 2) {
  983. dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
  984. "LAN_NODE_ID\n");
  985. return -EINVAL;
  986. }
  987. if (tuple->TupleData[1] != ETH_ALEN) {
  988. dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
  989. return -EINVAL;
  990. }
  991. for (i = 0; i < 6; i++)
  992. dev->dev_addr[i] = tuple->TupleData[i+2];
  993. return 0;
  994. }
  995. /**
  996. * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
  997. * @p_dev: the struct pcmcia_device for which we want the address.
  998. * @dev: a properly prepared struct net_device to store the info to.
  999. *
  1000. * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
  1001. * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
  1002. * must be set up properly by the driver (see examples!).
  1003. */
  1004. int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
  1005. {
  1006. return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
  1007. }
  1008. EXPORT_SYMBOL(pcmcia_get_mac_from_cis);