pcmcia_resource.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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-2010 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 <asm/irq.h>
  25. #include <pcmcia/ss.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. int pcmcia_validate_mem(struct pcmcia_socket *s)
  34. {
  35. if (s->resource_ops->validate_mem)
  36. return s->resource_ops->validate_mem(s);
  37. /* if there is no callback, we can assume that everything is OK */
  38. return 0;
  39. }
  40. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  41. int low, struct pcmcia_socket *s)
  42. {
  43. if (s->resource_ops->find_mem)
  44. return s->resource_ops->find_mem(base, num, align, low, s);
  45. return NULL;
  46. }
  47. /**
  48. * release_io_space() - release IO ports allocated with alloc_io_space()
  49. * @s: pcmcia socket
  50. * @res: resource to release
  51. *
  52. */
  53. static void release_io_space(struct pcmcia_socket *s, struct resource *res)
  54. {
  55. resource_size_t num = resource_size(res);
  56. int i;
  57. dev_dbg(&s->dev, "release_io_space for %pR\n", res);
  58. for (i = 0; i < MAX_IO_WIN; i++) {
  59. if (!s->io[i].res)
  60. continue;
  61. if ((s->io[i].res->start <= res->start) &&
  62. (s->io[i].res->end >= res->end)) {
  63. s->io[i].InUse -= num;
  64. if (res->parent)
  65. release_resource(res);
  66. res->start = res->end = 0;
  67. res->flags = IORESOURCE_IO;
  68. /* Free the window if no one else is using it */
  69. if (s->io[i].InUse == 0) {
  70. release_resource(s->io[i].res);
  71. kfree(s->io[i].res);
  72. s->io[i].res = NULL;
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * alloc_io_space() - allocate IO ports for use by a PCMCIA device
  79. * @s: pcmcia socket
  80. * @res: resource to allocate (begin: begin, end: size)
  81. * @lines: number of IO lines decoded by the PCMCIA card
  82. *
  83. * Special stuff for managing IO windows, because they are scarce
  84. */
  85. static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
  86. unsigned int lines)
  87. {
  88. unsigned int align;
  89. unsigned int base = res->start;
  90. unsigned int num = res->end;
  91. int ret;
  92. res->flags |= IORESOURCE_IO;
  93. dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
  94. res, lines);
  95. align = base ? (lines ? 1<<lines : 0) : 1;
  96. if (align && (align < num)) {
  97. if (base) {
  98. dev_dbg(&s->dev, "odd IO request\n");
  99. align = 0;
  100. } else
  101. while (align && (align < num))
  102. align <<= 1;
  103. }
  104. if (base & ~(align-1)) {
  105. dev_dbg(&s->dev, "odd IO request\n");
  106. align = 0;
  107. }
  108. ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
  109. &res->parent);
  110. if (ret) {
  111. dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
  112. return -EINVAL;
  113. }
  114. res->start = base;
  115. res->end = res->start + num - 1;
  116. if (res->parent) {
  117. ret = request_resource(res->parent, res);
  118. if (ret) {
  119. dev_warn(&s->dev,
  120. "request_resource %pR failed: %d\n", res, ret);
  121. res->parent = NULL;
  122. release_io_space(s, res);
  123. }
  124. }
  125. dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
  126. return ret;
  127. }
  128. /**
  129. * pcmcia_access_config() - read or write card configuration registers
  130. *
  131. * pcmcia_access_config() reads and writes configuration registers in
  132. * attribute memory. Memory window 0 is reserved for this and the tuple
  133. * reading services. Drivers must use pcmcia_read_config_byte() or
  134. * pcmcia_write_config_byte().
  135. */
  136. static int pcmcia_access_config(struct pcmcia_device *p_dev,
  137. off_t where, u8 *val,
  138. int (*accessf) (struct pcmcia_socket *s,
  139. int attr, unsigned int addr,
  140. unsigned int len, void *ptr))
  141. {
  142. struct pcmcia_socket *s;
  143. config_t *c;
  144. int addr;
  145. int ret = 0;
  146. s = p_dev->socket;
  147. mutex_lock(&s->ops_mutex);
  148. c = p_dev->function_config;
  149. if (!(c->state & CONFIG_LOCKED)) {
  150. dev_dbg(&p_dev->dev, "Configuration isn't locked\n");
  151. mutex_unlock(&s->ops_mutex);
  152. return -EACCES;
  153. }
  154. addr = (p_dev->config_base + where) >> 1;
  155. ret = accessf(s, 1, addr, 1, val);
  156. mutex_unlock(&s->ops_mutex);
  157. return ret;
  158. }
  159. /**
  160. * pcmcia_read_config_byte() - read a byte from a card configuration register
  161. *
  162. * pcmcia_read_config_byte() reads a byte from a configuration register in
  163. * attribute memory.
  164. */
  165. int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
  166. {
  167. return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
  168. }
  169. EXPORT_SYMBOL(pcmcia_read_config_byte);
  170. /**
  171. * pcmcia_write_config_byte() - write a byte to a card configuration register
  172. *
  173. * pcmcia_write_config_byte() writes a byte to a configuration register in
  174. * attribute memory.
  175. */
  176. int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
  177. {
  178. return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
  179. }
  180. EXPORT_SYMBOL(pcmcia_write_config_byte);
  181. /**
  182. * pcmcia_map_mem_page() - modify iomem window to point to a different offset
  183. * @p_dev: pcmcia device
  184. * @res: iomem resource already enabled by pcmcia_request_window()
  185. * @offset: card_offset to map
  186. *
  187. * pcmcia_map_mem_page() modifies what can be read and written by accessing
  188. * an iomem range previously enabled by pcmcia_request_window(), by setting
  189. * the card_offset value to @offset.
  190. */
  191. int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
  192. unsigned int offset)
  193. {
  194. struct pcmcia_socket *s = p_dev->socket;
  195. unsigned int w;
  196. int ret;
  197. w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
  198. if (w >= MAX_WIN)
  199. return -EINVAL;
  200. mutex_lock(&s->ops_mutex);
  201. s->win[w].card_start = offset;
  202. ret = s->ops->set_mem_map(s, &s->win[w]);
  203. if (ret)
  204. dev_warn(&p_dev->dev, "failed to set_mem_map\n");
  205. mutex_unlock(&s->ops_mutex);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL(pcmcia_map_mem_page);
  209. /**
  210. * pcmcia_fixup_iowidth() - reduce io width to 8bit
  211. * @p_dev: pcmcia device
  212. *
  213. * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
  214. * IO width to 8bit after having called pcmcia_enable_device()
  215. * previously.
  216. */
  217. int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
  218. {
  219. struct pcmcia_socket *s = p_dev->socket;
  220. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  221. pccard_io_map io_on;
  222. int i, ret = 0;
  223. mutex_lock(&s->ops_mutex);
  224. dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
  225. if (!(s->state & SOCKET_PRESENT) ||
  226. !(p_dev->function_config->state & CONFIG_LOCKED)) {
  227. dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
  228. ret = -EACCES;
  229. goto unlock;
  230. }
  231. io_on.speed = io_speed;
  232. for (i = 0; i < MAX_IO_WIN; i++) {
  233. if (!s->io[i].res)
  234. continue;
  235. io_off.map = i;
  236. io_on.map = i;
  237. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  238. io_on.start = s->io[i].res->start;
  239. io_on.stop = s->io[i].res->end;
  240. s->ops->set_io_map(s, &io_off);
  241. mdelay(40);
  242. s->ops->set_io_map(s, &io_on);
  243. }
  244. unlock:
  245. mutex_unlock(&s->ops_mutex);
  246. return ret;
  247. }
  248. EXPORT_SYMBOL(pcmcia_fixup_iowidth);
  249. /**
  250. * pcmcia_fixup_vpp() - set Vpp to a new voltage level
  251. * @p_dev: pcmcia device
  252. * @new_vpp: new Vpp voltage
  253. *
  254. * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
  255. * a new voltage level between calls to pcmcia_enable_device()
  256. * and pcmcia_disable_device().
  257. */
  258. int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
  259. {
  260. struct pcmcia_socket *s = p_dev->socket;
  261. int ret = 0;
  262. mutex_lock(&s->ops_mutex);
  263. dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
  264. if (!(s->state & SOCKET_PRESENT) ||
  265. !(p_dev->function_config->state & CONFIG_LOCKED)) {
  266. dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
  267. ret = -EACCES;
  268. goto unlock;
  269. }
  270. s->socket.Vpp = new_vpp;
  271. if (s->ops->set_socket(s, &s->socket)) {
  272. dev_warn(&p_dev->dev, "Unable to set VPP\n");
  273. ret = -EIO;
  274. goto unlock;
  275. }
  276. p_dev->vpp = new_vpp;
  277. unlock:
  278. mutex_unlock(&s->ops_mutex);
  279. return ret;
  280. }
  281. EXPORT_SYMBOL(pcmcia_fixup_vpp);
  282. /**
  283. * pcmcia_release_configuration() - physically disable a PCMCIA device
  284. * @p_dev: pcmcia device
  285. *
  286. * pcmcia_release_configuration() is the 1:1 counterpart to
  287. * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
  288. * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
  289. * and I/O ranges will be disabled. As pcmcia_release_io() and
  290. * pcmcia_release_window() still need to be called, device drivers are
  291. * expected to call pcmcia_disable_device() instead.
  292. */
  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. }
  326. /**
  327. * pcmcia_release_io() - release I/O allocated by a PCMCIA device
  328. * @p_dev: pcmcia device
  329. *
  330. * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
  331. * device. This may be invoked some time after a card ejection has
  332. * already dumped the actual socket configuration, so if the client is
  333. * "stale", we don't bother checking the port ranges against the
  334. * current socket values.
  335. */
  336. static int pcmcia_release_io(struct pcmcia_device *p_dev)
  337. {
  338. struct pcmcia_socket *s = p_dev->socket;
  339. int ret = -EINVAL;
  340. config_t *c;
  341. mutex_lock(&s->ops_mutex);
  342. if (!p_dev->_io)
  343. goto out;
  344. c = p_dev->function_config;
  345. release_io_space(s, &c->io[0]);
  346. if (c->io[1].end)
  347. release_io_space(s, &c->io[1]);
  348. p_dev->_io = 0;
  349. c->state &= ~CONFIG_IO_REQ;
  350. out:
  351. mutex_unlock(&s->ops_mutex);
  352. return ret;
  353. } /* pcmcia_release_io */
  354. /**
  355. * pcmcia_release_window() - release reserved iomem for PCMCIA devices
  356. * @p_dev: pcmcia device
  357. * @res: iomem resource to release
  358. *
  359. * pcmcia_release_window() releases &struct resource *res which was
  360. * previously reserved by calling pcmcia_request_window().
  361. */
  362. int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
  363. {
  364. struct pcmcia_socket *s = p_dev->socket;
  365. pccard_mem_map *win;
  366. unsigned int w;
  367. dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
  368. w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
  369. if (w >= MAX_WIN)
  370. return -EINVAL;
  371. mutex_lock(&s->ops_mutex);
  372. win = &s->win[w];
  373. if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
  374. dev_dbg(&p_dev->dev, "not releasing unknown window\n");
  375. mutex_unlock(&s->ops_mutex);
  376. return -EINVAL;
  377. }
  378. /* Shut down memory window */
  379. win->flags &= ~MAP_ACTIVE;
  380. s->ops->set_mem_map(s, win);
  381. s->state &= ~SOCKET_WIN_REQ(w);
  382. /* Release system memory */
  383. if (win->res) {
  384. release_resource(res);
  385. release_resource(win->res);
  386. kfree(win->res);
  387. win->res = NULL;
  388. }
  389. res->start = res->end = 0;
  390. res->flags = IORESOURCE_MEM;
  391. p_dev->_win &= ~CLIENT_WIN_REQ(w);
  392. mutex_unlock(&s->ops_mutex);
  393. return 0;
  394. } /* pcmcia_release_window */
  395. EXPORT_SYMBOL(pcmcia_release_window);
  396. /**
  397. * pcmcia_enable_device() - set up and activate a PCMCIA device
  398. * @p_dev: the associated PCMCIA device
  399. *
  400. * pcmcia_enable_device() physically enables a PCMCIA device. It parses
  401. * the flags passed to in @flags and stored in @p_dev->flags and sets up
  402. * the Vpp voltage, enables the speaker line, I/O ports and store proper
  403. * values to configuration registers.
  404. */
  405. int pcmcia_enable_device(struct pcmcia_device *p_dev)
  406. {
  407. int i;
  408. unsigned int base;
  409. struct pcmcia_socket *s = p_dev->socket;
  410. config_t *c;
  411. pccard_io_map iomap;
  412. unsigned char status = 0;
  413. unsigned char ext_status = 0;
  414. unsigned char option = 0;
  415. unsigned int flags = p_dev->config_flags;
  416. if (!(s->state & SOCKET_PRESENT))
  417. return -ENODEV;
  418. mutex_lock(&s->ops_mutex);
  419. c = p_dev->function_config;
  420. if (c->state & CONFIG_LOCKED) {
  421. mutex_unlock(&s->ops_mutex);
  422. dev_dbg(&p_dev->dev, "Configuration is locked\n");
  423. return -EACCES;
  424. }
  425. /* Do power control. We don't allow changes in Vcc. */
  426. s->socket.Vpp = p_dev->vpp;
  427. if (s->ops->set_socket(s, &s->socket)) {
  428. mutex_unlock(&s->ops_mutex);
  429. dev_printk(KERN_WARNING, &p_dev->dev,
  430. "Unable to set socket state\n");
  431. return -EINVAL;
  432. }
  433. /* Pick memory or I/O card, DMA mode, interrupt */
  434. if (p_dev->_io || flags & CONF_ENABLE_IRQ)
  435. flags |= CONF_ENABLE_IOCARD;
  436. if (flags & CONF_ENABLE_IOCARD)
  437. s->socket.flags |= SS_IOCARD;
  438. if (flags & CONF_ENABLE_ZVCARD)
  439. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  440. if (flags & CONF_ENABLE_SPKR) {
  441. s->socket.flags |= SS_SPKR_ENA;
  442. status = CCSR_AUDIO_ENA;
  443. if (!(p_dev->config_regs & PRESENT_STATUS))
  444. dev_warn(&p_dev->dev, "speaker requested, but "
  445. "PRESENT_STATUS not set!\n");
  446. }
  447. if (flags & CONF_ENABLE_IRQ)
  448. s->socket.io_irq = s->pcmcia_irq;
  449. else
  450. s->socket.io_irq = 0;
  451. if (flags & CONF_ENABLE_ESR) {
  452. p_dev->config_regs |= PRESENT_EXT_STATUS;
  453. ext_status = ESR_REQ_ATTN_ENA;
  454. }
  455. s->ops->set_socket(s, &s->socket);
  456. s->lock_count++;
  457. dev_dbg(&p_dev->dev,
  458. "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
  459. p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
  460. p_dev->config_index);
  461. /* Set up CIS configuration registers */
  462. base = p_dev->config_base;
  463. if (p_dev->config_regs & PRESENT_COPY) {
  464. u16 tmp = 0;
  465. dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
  466. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
  467. }
  468. if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
  469. u16 tmp = 0;
  470. dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
  471. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
  472. }
  473. if (p_dev->config_regs & PRESENT_OPTION) {
  474. if (s->functions == 1) {
  475. option = p_dev->config_index & COR_CONFIG_MASK;
  476. } else {
  477. option = p_dev->config_index & COR_MFC_CONFIG_MASK;
  478. option |= COR_FUNC_ENA|COR_IREQ_ENA;
  479. if (p_dev->config_regs & PRESENT_IOBASE_0)
  480. option |= COR_ADDR_DECODE;
  481. }
  482. if ((flags & CONF_ENABLE_IRQ) &&
  483. !(flags & CONF_ENABLE_PULSE_IRQ))
  484. option |= COR_LEVEL_REQ;
  485. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
  486. mdelay(40);
  487. }
  488. if (p_dev->config_regs & PRESENT_STATUS)
  489. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
  490. if (p_dev->config_regs & PRESENT_EXT_STATUS)
  491. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
  492. &ext_status);
  493. if (p_dev->config_regs & PRESENT_IOBASE_0) {
  494. u8 b = c->io[0].start & 0xff;
  495. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  496. b = (c->io[0].start >> 8) & 0xff;
  497. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  498. }
  499. if (p_dev->config_regs & PRESENT_IOSIZE) {
  500. u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
  501. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  502. }
  503. /* Configure I/O windows */
  504. if (c->state & CONFIG_IO_REQ) {
  505. iomap.speed = io_speed;
  506. for (i = 0; i < MAX_IO_WIN; i++)
  507. if (s->io[i].res) {
  508. iomap.map = i;
  509. iomap.flags = MAP_ACTIVE;
  510. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  511. case IO_DATA_PATH_WIDTH_16:
  512. iomap.flags |= MAP_16BIT; break;
  513. case IO_DATA_PATH_WIDTH_AUTO:
  514. iomap.flags |= MAP_AUTOSZ; break;
  515. default:
  516. break;
  517. }
  518. iomap.start = s->io[i].res->start;
  519. iomap.stop = s->io[i].res->end;
  520. s->ops->set_io_map(s, &iomap);
  521. s->io[i].Config++;
  522. }
  523. }
  524. c->state |= CONFIG_LOCKED;
  525. p_dev->_locked = 1;
  526. mutex_unlock(&s->ops_mutex);
  527. return 0;
  528. } /* pcmcia_enable_device */
  529. EXPORT_SYMBOL(pcmcia_enable_device);
  530. /**
  531. * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
  532. * @p_dev: the associated PCMCIA device
  533. *
  534. * pcmcia_request_io() attempts to reserve the IO port ranges specified in
  535. * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
  536. * "start" value is the requested start of the IO port resource; "end"
  537. * reflects the number of ports requested. The number of IO lines requested
  538. * is specified in &struct pcmcia_device @p_dev->io_lines.
  539. */
  540. int pcmcia_request_io(struct pcmcia_device *p_dev)
  541. {
  542. struct pcmcia_socket *s = p_dev->socket;
  543. config_t *c = p_dev->function_config;
  544. int ret = -EINVAL;
  545. mutex_lock(&s->ops_mutex);
  546. dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
  547. &c->io[0], &c->io[1]);
  548. if (!(s->state & SOCKET_PRESENT)) {
  549. dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
  550. goto out;
  551. }
  552. if (c->state & CONFIG_LOCKED) {
  553. dev_dbg(&p_dev->dev, "Configuration is locked\n");
  554. goto out;
  555. }
  556. if (c->state & CONFIG_IO_REQ) {
  557. dev_dbg(&p_dev->dev, "IO already configured\n");
  558. goto out;
  559. }
  560. ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
  561. if (ret)
  562. goto out;
  563. if (c->io[1].end) {
  564. ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
  565. if (ret) {
  566. struct resource tmp = c->io[0];
  567. /* release the previously allocated resource */
  568. release_io_space(s, &c->io[0]);
  569. /* but preserve the settings, for they worked... */
  570. c->io[0].end = resource_size(&tmp);
  571. c->io[0].start = tmp.start;
  572. c->io[0].flags = tmp.flags;
  573. goto out;
  574. }
  575. } else
  576. c->io[1].start = 0;
  577. c->state |= CONFIG_IO_REQ;
  578. p_dev->_io = 1;
  579. dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
  580. &c->io[0], &c->io[1]);
  581. out:
  582. mutex_unlock(&s->ops_mutex);
  583. return ret;
  584. } /* pcmcia_request_io */
  585. EXPORT_SYMBOL(pcmcia_request_io);
  586. /**
  587. * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
  588. * @p_dev: the associated PCMCIA device
  589. * @handler: IRQ handler to register
  590. *
  591. * pcmcia_request_irq() is a wrapper around request_irq() which allows
  592. * the PCMCIA core to clean up the registration in pcmcia_disable_device().
  593. * Drivers are free to use request_irq() directly, but then they need to
  594. * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
  595. * handlers are allowed.
  596. */
  597. int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
  598. irq_handler_t handler)
  599. {
  600. int ret;
  601. if (!p_dev->irq)
  602. return -EINVAL;
  603. ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
  604. p_dev->devname, p_dev->priv);
  605. if (!ret)
  606. p_dev->_irq = 1;
  607. return ret;
  608. }
  609. EXPORT_SYMBOL(pcmcia_request_irq);
  610. /**
  611. * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
  612. * @p_dev: the associated PCMCIA device
  613. * @handler: IRQ handler to register
  614. *
  615. * pcmcia_request_exclusive_irq() is a wrapper around request_irq() which
  616. * attempts first to request an exclusive IRQ. If it fails, it also accepts
  617. * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
  618. * IRQ sharing and either use request_irq directly (then they need to call
  619. * free_irq() themselves, too), or the pcmcia_request_irq() function.
  620. */
  621. int __must_check
  622. __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
  623. irq_handler_t handler)
  624. {
  625. int ret;
  626. if (!p_dev->irq)
  627. return -EINVAL;
  628. ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
  629. if (ret) {
  630. ret = pcmcia_request_irq(p_dev, handler);
  631. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
  632. "request for exclusive IRQ could not be fulfilled.\n");
  633. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
  634. "needs updating to supported shared IRQ lines.\n");
  635. }
  636. if (ret)
  637. dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
  638. else
  639. p_dev->_irq = 1;
  640. return ret;
  641. } /* pcmcia_request_exclusive_irq */
  642. EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
  643. #ifdef CONFIG_PCMCIA_PROBE
  644. /* mask of IRQs already reserved by other cards, we should avoid using them */
  645. static u8 pcmcia_used_irq[32];
  646. static irqreturn_t test_action(int cpl, void *dev_id)
  647. {
  648. return IRQ_NONE;
  649. }
  650. /**
  651. * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
  652. * @p_dev - the associated PCMCIA device
  653. *
  654. * locking note: must be called with ops_mutex locked.
  655. */
  656. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  657. {
  658. struct pcmcia_socket *s = p_dev->socket;
  659. unsigned int try, irq;
  660. u32 mask = s->irq_mask;
  661. int ret = -ENODEV;
  662. for (try = 0; try < 64; try++) {
  663. irq = try % 32;
  664. if (irq > NR_IRQS)
  665. continue;
  666. /* marked as available by driver, not blocked by userspace? */
  667. if (!((mask >> irq) & 1))
  668. continue;
  669. /* avoid an IRQ which is already used by another PCMCIA card */
  670. if ((try < 32) && pcmcia_used_irq[irq])
  671. continue;
  672. /* register the correct driver, if possible, to check whether
  673. * registering a dummy handle works, i.e. if the IRQ isn't
  674. * marked as used by the kernel resource management core */
  675. ret = request_irq(irq, test_action, type, p_dev->devname,
  676. p_dev);
  677. if (!ret) {
  678. free_irq(irq, p_dev);
  679. p_dev->irq = s->pcmcia_irq = irq;
  680. pcmcia_used_irq[irq]++;
  681. break;
  682. }
  683. }
  684. return ret;
  685. }
  686. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  687. {
  688. pcmcia_used_irq[s->pcmcia_irq]--;
  689. s->pcmcia_irq = 0;
  690. }
  691. #else /* CONFIG_PCMCIA_PROBE */
  692. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  693. {
  694. return -EINVAL;
  695. }
  696. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  697. {
  698. s->pcmcia_irq = 0;
  699. return;
  700. }
  701. #endif /* CONFIG_PCMCIA_PROBE */
  702. /**
  703. * pcmcia_setup_irq() - determine IRQ to be used for device
  704. * @p_dev - the associated PCMCIA device
  705. *
  706. * locking note: must be called with ops_mutex locked.
  707. */
  708. int pcmcia_setup_irq(struct pcmcia_device *p_dev)
  709. {
  710. struct pcmcia_socket *s = p_dev->socket;
  711. if (p_dev->irq)
  712. return 0;
  713. /* already assigned? */
  714. if (s->pcmcia_irq) {
  715. p_dev->irq = s->pcmcia_irq;
  716. return 0;
  717. }
  718. /* prefer an exclusive ISA irq */
  719. if (!pcmcia_setup_isa_irq(p_dev, 0))
  720. return 0;
  721. /* but accept a shared ISA irq */
  722. if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
  723. return 0;
  724. /* but use the PCI irq otherwise */
  725. if (s->pci_irq) {
  726. p_dev->irq = s->pcmcia_irq = s->pci_irq;
  727. return 0;
  728. }
  729. return -EINVAL;
  730. }
  731. /**
  732. * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
  733. * @p_dev: the associated PCMCIA device
  734. * @res: &struct resource pointing to p_dev->resource[2..5]
  735. * @speed: access speed
  736. *
  737. * pcmcia_request_window() attepts to reserve an iomem ranges specified in
  738. * &struct resource @res pointing to one of the entries in
  739. * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
  740. * requested start of the IO mem resource; "end" reflects the size
  741. * requested.
  742. */
  743. int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
  744. unsigned int speed)
  745. {
  746. struct pcmcia_socket *s = p_dev->socket;
  747. pccard_mem_map *win;
  748. u_long align;
  749. int w;
  750. dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
  751. if (!(s->state & SOCKET_PRESENT)) {
  752. dev_dbg(&p_dev->dev, "No card present\n");
  753. return -ENODEV;
  754. }
  755. /* Window size defaults to smallest available */
  756. if (res->end == 0)
  757. res->end = s->map_size;
  758. align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
  759. if (res->end & (s->map_size-1)) {
  760. dev_dbg(&p_dev->dev, "invalid map size\n");
  761. return -EINVAL;
  762. }
  763. if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
  764. (res->start & (align-1))) {
  765. dev_dbg(&p_dev->dev, "invalid base address\n");
  766. return -EINVAL;
  767. }
  768. if (res->start)
  769. align = 0;
  770. /* Allocate system memory window */
  771. mutex_lock(&s->ops_mutex);
  772. for (w = 0; w < MAX_WIN; w++)
  773. if (!(s->state & SOCKET_WIN_REQ(w)))
  774. break;
  775. if (w == MAX_WIN) {
  776. dev_dbg(&p_dev->dev, "all windows are used already\n");
  777. mutex_unlock(&s->ops_mutex);
  778. return -EINVAL;
  779. }
  780. win = &s->win[w];
  781. if (!(s->features & SS_CAP_STATIC_MAP)) {
  782. win->res = pcmcia_find_mem_region(res->start, res->end, align,
  783. 0, s);
  784. if (!win->res) {
  785. dev_dbg(&p_dev->dev, "allocating mem region failed\n");
  786. mutex_unlock(&s->ops_mutex);
  787. return -EINVAL;
  788. }
  789. }
  790. p_dev->_win |= CLIENT_WIN_REQ(w);
  791. /* Configure the socket controller */
  792. win->map = w+1;
  793. win->flags = res->flags & WIN_FLAGS_MAP;
  794. win->speed = speed;
  795. win->card_start = 0;
  796. if (s->ops->set_mem_map(s, win) != 0) {
  797. dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
  798. mutex_unlock(&s->ops_mutex);
  799. return -EIO;
  800. }
  801. s->state |= SOCKET_WIN_REQ(w);
  802. /* Return window handle */
  803. if (s->features & SS_CAP_STATIC_MAP)
  804. res->start = win->static_start;
  805. else
  806. res->start = win->res->start;
  807. /* convert to new-style resources */
  808. res->end += res->start - 1;
  809. res->flags &= ~WIN_FLAGS_REQ;
  810. res->flags |= (win->map << 2) | IORESOURCE_MEM;
  811. res->parent = win->res;
  812. if (win->res)
  813. request_resource(&iomem_resource, res);
  814. dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
  815. mutex_unlock(&s->ops_mutex);
  816. return 0;
  817. } /* pcmcia_request_window */
  818. EXPORT_SYMBOL(pcmcia_request_window);
  819. /**
  820. * pcmcia_disable_device() - disable and clean up a PCMCIA device
  821. * @p_dev: the associated PCMCIA device
  822. *
  823. * pcmcia_disable_device() is the driver-callable counterpart to
  824. * pcmcia_enable_device(): If a PCMCIA device is no longer used,
  825. * drivers are expected to clean up and disable the device by calling
  826. * this function. Any I/O ranges (iomem and ioports) will be released,
  827. * the Vpp voltage will be set to 0, and IRQs will no longer be
  828. * generated -- at least if there is no other card function (of
  829. * multifunction devices) being used.
  830. */
  831. void pcmcia_disable_device(struct pcmcia_device *p_dev)
  832. {
  833. int i;
  834. dev_dbg(&p_dev->dev, "disabling device\n");
  835. for (i = 0; i < MAX_WIN; i++) {
  836. struct resource *res = p_dev->resource[MAX_IO_WIN + i];
  837. if (res->flags & WIN_FLAGS_REQ)
  838. pcmcia_release_window(p_dev, res);
  839. }
  840. pcmcia_release_configuration(p_dev);
  841. pcmcia_release_io(p_dev);
  842. if (p_dev->_irq) {
  843. free_irq(p_dev->irq, p_dev->priv);
  844. p_dev->_irq = 0;
  845. }
  846. }
  847. EXPORT_SYMBOL(pcmcia_disable_device);