pcmcia_resource.c 30 KB

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