pcmcia_resource.c 23 KB

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