pcmcia_resource.c 29 KB

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