pcmcia_resource.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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_warn(&p_dev->dev, "Unable to set socket state\n");
  430. return -EINVAL;
  431. }
  432. /* Pick memory or I/O card, DMA mode, interrupt */
  433. if (p_dev->_io || flags & CONF_ENABLE_IRQ)
  434. flags |= CONF_ENABLE_IOCARD;
  435. if (flags & CONF_ENABLE_IOCARD)
  436. s->socket.flags |= SS_IOCARD;
  437. if (flags & CONF_ENABLE_ZVCARD)
  438. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  439. if (flags & CONF_ENABLE_SPKR) {
  440. s->socket.flags |= SS_SPKR_ENA;
  441. status = CCSR_AUDIO_ENA;
  442. if (!(p_dev->config_regs & PRESENT_STATUS))
  443. dev_warn(&p_dev->dev, "speaker requested, but "
  444. "PRESENT_STATUS not set!\n");
  445. }
  446. if (flags & CONF_ENABLE_IRQ)
  447. s->socket.io_irq = s->pcmcia_irq;
  448. else
  449. s->socket.io_irq = 0;
  450. if (flags & CONF_ENABLE_ESR) {
  451. p_dev->config_regs |= PRESENT_EXT_STATUS;
  452. ext_status = ESR_REQ_ATTN_ENA;
  453. }
  454. s->ops->set_socket(s, &s->socket);
  455. s->lock_count++;
  456. dev_dbg(&p_dev->dev,
  457. "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
  458. p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
  459. p_dev->config_index);
  460. /* Set up CIS configuration registers */
  461. base = p_dev->config_base;
  462. if (p_dev->config_regs & PRESENT_COPY) {
  463. u16 tmp = 0;
  464. dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
  465. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
  466. }
  467. if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
  468. u16 tmp = 0;
  469. dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
  470. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
  471. }
  472. if (p_dev->config_regs & PRESENT_OPTION) {
  473. if (s->functions == 1) {
  474. option = p_dev->config_index & COR_CONFIG_MASK;
  475. } else {
  476. option = p_dev->config_index & COR_MFC_CONFIG_MASK;
  477. option |= COR_FUNC_ENA|COR_IREQ_ENA;
  478. if (p_dev->config_regs & PRESENT_IOBASE_0)
  479. option |= COR_ADDR_DECODE;
  480. }
  481. if ((flags & CONF_ENABLE_IRQ) &&
  482. !(flags & CONF_ENABLE_PULSE_IRQ))
  483. option |= COR_LEVEL_REQ;
  484. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
  485. mdelay(40);
  486. }
  487. if (p_dev->config_regs & PRESENT_STATUS)
  488. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
  489. if (p_dev->config_regs & PRESENT_EXT_STATUS)
  490. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
  491. &ext_status);
  492. if (p_dev->config_regs & PRESENT_IOBASE_0) {
  493. u8 b = c->io[0].start & 0xff;
  494. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  495. b = (c->io[0].start >> 8) & 0xff;
  496. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  497. }
  498. if (p_dev->config_regs & PRESENT_IOSIZE) {
  499. u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
  500. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  501. }
  502. /* Configure I/O windows */
  503. if (c->state & CONFIG_IO_REQ) {
  504. iomap.speed = io_speed;
  505. for (i = 0; i < MAX_IO_WIN; i++)
  506. if (s->io[i].res) {
  507. iomap.map = i;
  508. iomap.flags = MAP_ACTIVE;
  509. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  510. case IO_DATA_PATH_WIDTH_16:
  511. iomap.flags |= MAP_16BIT; break;
  512. case IO_DATA_PATH_WIDTH_AUTO:
  513. iomap.flags |= MAP_AUTOSZ; break;
  514. default:
  515. break;
  516. }
  517. iomap.start = s->io[i].res->start;
  518. iomap.stop = s->io[i].res->end;
  519. s->ops->set_io_map(s, &iomap);
  520. s->io[i].Config++;
  521. }
  522. }
  523. c->state |= CONFIG_LOCKED;
  524. p_dev->_locked = 1;
  525. mutex_unlock(&s->ops_mutex);
  526. return 0;
  527. } /* pcmcia_enable_device */
  528. EXPORT_SYMBOL(pcmcia_enable_device);
  529. /**
  530. * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
  531. * @p_dev: the associated PCMCIA device
  532. *
  533. * pcmcia_request_io() attempts to reserve the IO port ranges specified in
  534. * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
  535. * "start" value is the requested start of the IO port resource; "end"
  536. * reflects the number of ports requested. The number of IO lines requested
  537. * is specified in &struct pcmcia_device @p_dev->io_lines.
  538. */
  539. int pcmcia_request_io(struct pcmcia_device *p_dev)
  540. {
  541. struct pcmcia_socket *s = p_dev->socket;
  542. config_t *c = p_dev->function_config;
  543. int ret = -EINVAL;
  544. mutex_lock(&s->ops_mutex);
  545. dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
  546. &c->io[0], &c->io[1]);
  547. if (!(s->state & SOCKET_PRESENT)) {
  548. dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
  549. goto out;
  550. }
  551. if (c->state & CONFIG_LOCKED) {
  552. dev_dbg(&p_dev->dev, "Configuration is locked\n");
  553. goto out;
  554. }
  555. if (c->state & CONFIG_IO_REQ) {
  556. dev_dbg(&p_dev->dev, "IO already configured\n");
  557. goto out;
  558. }
  559. ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
  560. if (ret)
  561. goto out;
  562. if (c->io[1].end) {
  563. ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
  564. if (ret) {
  565. struct resource tmp = c->io[0];
  566. /* release the previously allocated resource */
  567. release_io_space(s, &c->io[0]);
  568. /* but preserve the settings, for they worked... */
  569. c->io[0].end = resource_size(&tmp);
  570. c->io[0].start = tmp.start;
  571. c->io[0].flags = tmp.flags;
  572. goto out;
  573. }
  574. } else
  575. c->io[1].start = 0;
  576. c->state |= CONFIG_IO_REQ;
  577. p_dev->_io = 1;
  578. dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
  579. &c->io[0], &c->io[1]);
  580. out:
  581. mutex_unlock(&s->ops_mutex);
  582. return ret;
  583. } /* pcmcia_request_io */
  584. EXPORT_SYMBOL(pcmcia_request_io);
  585. /**
  586. * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
  587. * @p_dev: the associated PCMCIA device
  588. * @handler: IRQ handler to register
  589. *
  590. * pcmcia_request_irq() is a wrapper around request_irq() which allows
  591. * the PCMCIA core to clean up the registration in pcmcia_disable_device().
  592. * Drivers are free to use request_irq() directly, but then they need to
  593. * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
  594. * handlers are allowed.
  595. */
  596. int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
  597. irq_handler_t handler)
  598. {
  599. int ret;
  600. if (!p_dev->irq)
  601. return -EINVAL;
  602. ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
  603. p_dev->devname, p_dev->priv);
  604. if (!ret)
  605. p_dev->_irq = 1;
  606. return ret;
  607. }
  608. EXPORT_SYMBOL(pcmcia_request_irq);
  609. /**
  610. * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
  611. * @p_dev: the associated PCMCIA device
  612. * @handler: IRQ handler to register
  613. *
  614. * pcmcia_request_exclusive_irq() is a wrapper around request_irq() which
  615. * attempts first to request an exclusive IRQ. If it fails, it also accepts
  616. * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
  617. * IRQ sharing and either use request_irq directly (then they need to call
  618. * free_irq() themselves, too), or the pcmcia_request_irq() function.
  619. */
  620. int __must_check
  621. __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
  622. irq_handler_t handler)
  623. {
  624. int ret;
  625. if (!p_dev->irq)
  626. return -EINVAL;
  627. ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
  628. if (ret) {
  629. ret = pcmcia_request_irq(p_dev, handler);
  630. dev_warn(&p_dev->dev, "pcmcia: request for exclusive IRQ could not be fulfilled\n");
  631. dev_warn(&p_dev->dev, "pcmcia: the driver needs updating to supported shared IRQ lines\n");
  632. }
  633. if (ret)
  634. dev_info(&p_dev->dev, "request_irq() failed\n");
  635. else
  636. p_dev->_irq = 1;
  637. return ret;
  638. } /* pcmcia_request_exclusive_irq */
  639. EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
  640. #ifdef CONFIG_PCMCIA_PROBE
  641. /* mask of IRQs already reserved by other cards, we should avoid using them */
  642. static u8 pcmcia_used_irq[32];
  643. static irqreturn_t test_action(int cpl, void *dev_id)
  644. {
  645. return IRQ_NONE;
  646. }
  647. /**
  648. * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
  649. * @p_dev - the associated PCMCIA device
  650. *
  651. * locking note: must be called with ops_mutex locked.
  652. */
  653. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  654. {
  655. struct pcmcia_socket *s = p_dev->socket;
  656. unsigned int try, irq;
  657. u32 mask = s->irq_mask;
  658. int ret = -ENODEV;
  659. for (try = 0; try < 64; try++) {
  660. irq = try % 32;
  661. if (irq > NR_IRQS)
  662. continue;
  663. /* marked as available by driver, not blocked by userspace? */
  664. if (!((mask >> irq) & 1))
  665. continue;
  666. /* avoid an IRQ which is already used by another PCMCIA card */
  667. if ((try < 32) && pcmcia_used_irq[irq])
  668. continue;
  669. /* register the correct driver, if possible, to check whether
  670. * registering a dummy handle works, i.e. if the IRQ isn't
  671. * marked as used by the kernel resource management core */
  672. ret = request_irq(irq, test_action, type, p_dev->devname,
  673. p_dev);
  674. if (!ret) {
  675. free_irq(irq, p_dev);
  676. p_dev->irq = s->pcmcia_irq = irq;
  677. pcmcia_used_irq[irq]++;
  678. break;
  679. }
  680. }
  681. return ret;
  682. }
  683. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  684. {
  685. pcmcia_used_irq[s->pcmcia_irq]--;
  686. s->pcmcia_irq = 0;
  687. }
  688. #else /* CONFIG_PCMCIA_PROBE */
  689. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  690. {
  691. return -EINVAL;
  692. }
  693. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  694. {
  695. s->pcmcia_irq = 0;
  696. return;
  697. }
  698. #endif /* CONFIG_PCMCIA_PROBE */
  699. /**
  700. * pcmcia_setup_irq() - determine IRQ to be used for device
  701. * @p_dev - the associated PCMCIA device
  702. *
  703. * locking note: must be called with ops_mutex locked.
  704. */
  705. int pcmcia_setup_irq(struct pcmcia_device *p_dev)
  706. {
  707. struct pcmcia_socket *s = p_dev->socket;
  708. if (p_dev->irq)
  709. return 0;
  710. /* already assigned? */
  711. if (s->pcmcia_irq) {
  712. p_dev->irq = s->pcmcia_irq;
  713. return 0;
  714. }
  715. /* prefer an exclusive ISA irq */
  716. if (!pcmcia_setup_isa_irq(p_dev, 0))
  717. return 0;
  718. /* but accept a shared ISA irq */
  719. if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
  720. return 0;
  721. /* but use the PCI irq otherwise */
  722. if (s->pci_irq) {
  723. p_dev->irq = s->pcmcia_irq = s->pci_irq;
  724. return 0;
  725. }
  726. return -EINVAL;
  727. }
  728. /**
  729. * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
  730. * @p_dev: the associated PCMCIA device
  731. * @res: &struct resource pointing to p_dev->resource[2..5]
  732. * @speed: access speed
  733. *
  734. * pcmcia_request_window() attepts to reserve an iomem ranges specified in
  735. * &struct resource @res pointing to one of the entries in
  736. * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
  737. * requested start of the IO mem resource; "end" reflects the size
  738. * requested.
  739. */
  740. int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
  741. unsigned int speed)
  742. {
  743. struct pcmcia_socket *s = p_dev->socket;
  744. pccard_mem_map *win;
  745. u_long align;
  746. int w;
  747. dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
  748. if (!(s->state & SOCKET_PRESENT)) {
  749. dev_dbg(&p_dev->dev, "No card present\n");
  750. return -ENODEV;
  751. }
  752. /* Window size defaults to smallest available */
  753. if (res->end == 0)
  754. res->end = s->map_size;
  755. align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
  756. if (res->end & (s->map_size-1)) {
  757. dev_dbg(&p_dev->dev, "invalid map size\n");
  758. return -EINVAL;
  759. }
  760. if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
  761. (res->start & (align-1))) {
  762. dev_dbg(&p_dev->dev, "invalid base address\n");
  763. return -EINVAL;
  764. }
  765. if (res->start)
  766. align = 0;
  767. /* Allocate system memory window */
  768. mutex_lock(&s->ops_mutex);
  769. for (w = 0; w < MAX_WIN; w++)
  770. if (!(s->state & SOCKET_WIN_REQ(w)))
  771. break;
  772. if (w == MAX_WIN) {
  773. dev_dbg(&p_dev->dev, "all windows are used already\n");
  774. mutex_unlock(&s->ops_mutex);
  775. return -EINVAL;
  776. }
  777. win = &s->win[w];
  778. if (!(s->features & SS_CAP_STATIC_MAP)) {
  779. win->res = pcmcia_find_mem_region(res->start, res->end, align,
  780. 0, s);
  781. if (!win->res) {
  782. dev_dbg(&p_dev->dev, "allocating mem region failed\n");
  783. mutex_unlock(&s->ops_mutex);
  784. return -EINVAL;
  785. }
  786. }
  787. p_dev->_win |= CLIENT_WIN_REQ(w);
  788. /* Configure the socket controller */
  789. win->map = w+1;
  790. win->flags = res->flags & WIN_FLAGS_MAP;
  791. win->speed = speed;
  792. win->card_start = 0;
  793. if (s->ops->set_mem_map(s, win) != 0) {
  794. dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
  795. mutex_unlock(&s->ops_mutex);
  796. return -EIO;
  797. }
  798. s->state |= SOCKET_WIN_REQ(w);
  799. /* Return window handle */
  800. if (s->features & SS_CAP_STATIC_MAP)
  801. res->start = win->static_start;
  802. else
  803. res->start = win->res->start;
  804. /* convert to new-style resources */
  805. res->end += res->start - 1;
  806. res->flags &= ~WIN_FLAGS_REQ;
  807. res->flags |= (win->map << 2) | IORESOURCE_MEM;
  808. res->parent = win->res;
  809. if (win->res)
  810. request_resource(&iomem_resource, res);
  811. dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
  812. mutex_unlock(&s->ops_mutex);
  813. return 0;
  814. } /* pcmcia_request_window */
  815. EXPORT_SYMBOL(pcmcia_request_window);
  816. /**
  817. * pcmcia_disable_device() - disable and clean up a PCMCIA device
  818. * @p_dev: the associated PCMCIA device
  819. *
  820. * pcmcia_disable_device() is the driver-callable counterpart to
  821. * pcmcia_enable_device(): If a PCMCIA device is no longer used,
  822. * drivers are expected to clean up and disable the device by calling
  823. * this function. Any I/O ranges (iomem and ioports) will be released,
  824. * the Vpp voltage will be set to 0, and IRQs will no longer be
  825. * generated -- at least if there is no other card function (of
  826. * multifunction devices) being used.
  827. */
  828. void pcmcia_disable_device(struct pcmcia_device *p_dev)
  829. {
  830. int i;
  831. dev_dbg(&p_dev->dev, "disabling device\n");
  832. for (i = 0; i < MAX_WIN; i++) {
  833. struct resource *res = p_dev->resource[MAX_IO_WIN + i];
  834. if (res->flags & WIN_FLAGS_REQ)
  835. pcmcia_release_window(p_dev, res);
  836. }
  837. pcmcia_release_configuration(p_dev);
  838. pcmcia_release_io(p_dev);
  839. if (p_dev->_irq) {
  840. free_irq(p_dev->irq, p_dev->priv);
  841. p_dev->_irq = 0;
  842. }
  843. }
  844. EXPORT_SYMBOL(pcmcia_disable_device);