pcmcia_resource.c 24 KB

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