pcmcia_resource.c 24 KB

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