pcmcia_resource.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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/cs.h>
  27. #include <pcmcia/cistpl.h>
  28. #include <pcmcia/cisreg.h>
  29. #include <pcmcia/ds.h>
  30. #include "cs_internal.h"
  31. /* Access speed for IO windows */
  32. static int io_speed;
  33. module_param(io_speed, int, 0444);
  34. int pcmcia_validate_mem(struct pcmcia_socket *s)
  35. {
  36. if (s->resource_ops->validate_mem)
  37. return s->resource_ops->validate_mem(s);
  38. /* if there is no callback, we can assume that everything is OK */
  39. return 0;
  40. }
  41. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  42. int low, struct pcmcia_socket *s)
  43. {
  44. if (s->resource_ops->find_mem)
  45. return s->resource_ops->find_mem(base, num, align, low, s);
  46. return NULL;
  47. }
  48. static void release_io_space(struct pcmcia_socket *s, struct resource *res)
  49. {
  50. resource_size_t num = resource_size(res);
  51. int i;
  52. dev_dbg(&s->dev, "release_io_space for %pR\n", res);
  53. for (i = 0; i < MAX_IO_WIN; i++) {
  54. if (!s->io[i].res)
  55. continue;
  56. if ((s->io[i].res->start <= res->start) &&
  57. (s->io[i].res->end >= res->end)) {
  58. s->io[i].InUse -= num;
  59. if (res->parent)
  60. release_resource(res);
  61. res->start = res->end = 0;
  62. res->flags = IORESOURCE_IO;
  63. /* Free the window if no one else is using it */
  64. if (s->io[i].InUse == 0) {
  65. release_resource(s->io[i].res);
  66. kfree(s->io[i].res);
  67. s->io[i].res = NULL;
  68. }
  69. }
  70. }
  71. } /* release_io_space */
  72. /** alloc_io_space
  73. *
  74. * Special stuff for managing IO windows, because they are scarce
  75. */
  76. static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
  77. unsigned int lines)
  78. {
  79. unsigned int align;
  80. unsigned int base = res->start;
  81. unsigned int num = res->end;
  82. int ret;
  83. res->flags |= IORESOURCE_IO;
  84. dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
  85. res, lines);
  86. align = base ? (lines ? 1<<lines : 0) : 1;
  87. if (align && (align < num)) {
  88. if (base) {
  89. dev_dbg(&s->dev, "odd IO request\n");
  90. align = 0;
  91. } else
  92. while (align && (align < num))
  93. align <<= 1;
  94. }
  95. if (base & ~(align-1)) {
  96. dev_dbg(&s->dev, "odd IO request\n");
  97. align = 0;
  98. }
  99. ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
  100. &res->parent);
  101. if (ret) {
  102. dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
  103. return -EINVAL;
  104. }
  105. res->start = base;
  106. res->end = res->start + num - 1;
  107. if (res->parent) {
  108. ret = request_resource(res->parent, res);
  109. if (ret) {
  110. dev_warn(&s->dev,
  111. "request_resource %pR failed: %d\n", res, ret);
  112. res->parent = NULL;
  113. release_io_space(s, res);
  114. }
  115. }
  116. dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
  117. return ret;
  118. } /* alloc_io_space */
  119. /**
  120. * pcmcia_access_config() - read or write card configuration registers
  121. *
  122. * pcmcia_access_config() reads and writes configuration registers in
  123. * attribute memory. Memory window 0 is reserved for this and the tuple
  124. * reading services. Drivers must use pcmcia_read_config_byte() or
  125. * pcmcia_write_config_byte().
  126. */
  127. static int pcmcia_access_config(struct pcmcia_device *p_dev,
  128. off_t where, u8 *val,
  129. int (*accessf) (struct pcmcia_socket *s,
  130. int attr, unsigned int addr,
  131. unsigned int len, void *ptr))
  132. {
  133. struct pcmcia_socket *s;
  134. config_t *c;
  135. int addr;
  136. int ret = 0;
  137. s = p_dev->socket;
  138. mutex_lock(&s->ops_mutex);
  139. c = p_dev->function_config;
  140. if (!(c->state & CONFIG_LOCKED)) {
  141. dev_dbg(&p_dev->dev, "Configuration isnt't locked\n");
  142. mutex_unlock(&s->ops_mutex);
  143. return -EACCES;
  144. }
  145. addr = (p_dev->config_base + where) >> 1;
  146. ret = accessf(s, 1, addr, 1, val);
  147. mutex_unlock(&s->ops_mutex);
  148. return ret;
  149. } /* pcmcia_access_config */
  150. /**
  151. * pcmcia_read_config_byte() - read a byte from a card configuration register
  152. *
  153. * pcmcia_read_config_byte() reads a byte from a configuration register in
  154. * attribute memory.
  155. */
  156. int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
  157. {
  158. return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
  159. }
  160. EXPORT_SYMBOL(pcmcia_read_config_byte);
  161. /**
  162. * pcmcia_write_config_byte() - write a byte to a card configuration register
  163. *
  164. * pcmcia_write_config_byte() writes a byte to a configuration register in
  165. * attribute memory.
  166. */
  167. int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
  168. {
  169. return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
  170. }
  171. EXPORT_SYMBOL(pcmcia_write_config_byte);
  172. int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
  173. unsigned int offset)
  174. {
  175. struct pcmcia_socket *s = p_dev->socket;
  176. unsigned int w;
  177. int ret;
  178. w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
  179. if (w >= MAX_WIN)
  180. return -EINVAL;
  181. mutex_lock(&s->ops_mutex);
  182. s->win[w].card_start = offset;
  183. ret = s->ops->set_mem_map(s, &s->win[w]);
  184. if (ret)
  185. dev_warn(&p_dev->dev, "failed to set_mem_map\n");
  186. mutex_unlock(&s->ops_mutex);
  187. return ret;
  188. } /* pcmcia_map_mem_page */
  189. EXPORT_SYMBOL(pcmcia_map_mem_page);
  190. /**
  191. * pcmcia_fixup_iowidth() - reduce io width to 8bit
  192. *
  193. * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
  194. * IO width to 8bit after having called pcmcia_request_configuration()
  195. * previously.
  196. */
  197. int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
  198. {
  199. struct pcmcia_socket *s = p_dev->socket;
  200. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  201. pccard_io_map io_on;
  202. int i, ret = 0;
  203. mutex_lock(&s->ops_mutex);
  204. dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
  205. if (!(s->state & SOCKET_PRESENT) ||
  206. !(p_dev->function_config->state & CONFIG_LOCKED)) {
  207. dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
  208. ret = -EACCES;
  209. goto unlock;
  210. }
  211. io_on.speed = io_speed;
  212. for (i = 0; i < MAX_IO_WIN; i++) {
  213. if (!s->io[i].res)
  214. continue;
  215. io_off.map = i;
  216. io_on.map = i;
  217. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  218. io_on.start = s->io[i].res->start;
  219. io_on.stop = s->io[i].res->end;
  220. s->ops->set_io_map(s, &io_off);
  221. mdelay(40);
  222. s->ops->set_io_map(s, &io_on);
  223. }
  224. unlock:
  225. mutex_unlock(&s->ops_mutex);
  226. return ret;
  227. }
  228. EXPORT_SYMBOL(pcmcia_fixup_iowidth);
  229. /**
  230. * pcmcia_fixup_vpp() - set Vpp to a new voltage level
  231. *
  232. * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
  233. * a new voltage level between calls to pcmcia_request_configuration()
  234. * and pcmcia_disable_device().
  235. */
  236. int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
  237. {
  238. struct pcmcia_socket *s = p_dev->socket;
  239. int ret = 0;
  240. mutex_lock(&s->ops_mutex);
  241. dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
  242. if (!(s->state & SOCKET_PRESENT) ||
  243. !(p_dev->function_config->state & CONFIG_LOCKED)) {
  244. dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
  245. ret = -EACCES;
  246. goto unlock;
  247. }
  248. s->socket.Vpp = new_vpp;
  249. if (s->ops->set_socket(s, &s->socket)) {
  250. dev_warn(&p_dev->dev, "Unable to set VPP\n");
  251. ret = -EIO;
  252. goto unlock;
  253. }
  254. p_dev->vpp = new_vpp;
  255. unlock:
  256. mutex_unlock(&s->ops_mutex);
  257. return ret;
  258. }
  259. EXPORT_SYMBOL(pcmcia_fixup_vpp);
  260. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  261. {
  262. pccard_io_map io = { 0, 0, 0, 0, 1 };
  263. struct pcmcia_socket *s = p_dev->socket;
  264. config_t *c;
  265. int i;
  266. mutex_lock(&s->ops_mutex);
  267. c = p_dev->function_config;
  268. if (p_dev->_locked) {
  269. p_dev->_locked = 0;
  270. if (--(s->lock_count) == 0) {
  271. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  272. s->socket.Vpp = 0;
  273. s->socket.io_irq = 0;
  274. s->ops->set_socket(s, &s->socket);
  275. }
  276. }
  277. if (c->state & CONFIG_LOCKED) {
  278. c->state &= ~CONFIG_LOCKED;
  279. if (c->state & CONFIG_IO_REQ)
  280. for (i = 0; i < MAX_IO_WIN; i++) {
  281. if (!s->io[i].res)
  282. continue;
  283. s->io[i].Config--;
  284. if (s->io[i].Config != 0)
  285. continue;
  286. io.map = i;
  287. s->ops->set_io_map(s, &io);
  288. }
  289. }
  290. mutex_unlock(&s->ops_mutex);
  291. return 0;
  292. } /* pcmcia_release_configuration */
  293. /** pcmcia_release_io
  294. *
  295. * Release_io() releases the I/O ranges allocated by a client. This
  296. * may be invoked some time after a card ejection has already dumped
  297. * the actual socket configuration, so if the client is "stale", we
  298. * don't bother checking the port ranges against the current socket
  299. * values.
  300. */
  301. static int pcmcia_release_io(struct pcmcia_device *p_dev)
  302. {
  303. struct pcmcia_socket *s = p_dev->socket;
  304. int ret = -EINVAL;
  305. config_t *c;
  306. mutex_lock(&s->ops_mutex);
  307. if (!p_dev->_io)
  308. goto out;
  309. c = p_dev->function_config;
  310. release_io_space(s, &c->io[0]);
  311. if (c->io[1].end)
  312. release_io_space(s, &c->io[1]);
  313. p_dev->_io = 0;
  314. c->state &= ~CONFIG_IO_REQ;
  315. out:
  316. mutex_unlock(&s->ops_mutex);
  317. return ret;
  318. } /* pcmcia_release_io */
  319. /**
  320. * pcmcia_release_window() - release reserved iomem for PCMCIA devices
  321. *
  322. * pcmcia_release_window() releases struct resource *res which was
  323. * previously reserved by calling pcmcia_request_window().
  324. */
  325. int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
  326. {
  327. struct pcmcia_socket *s = p_dev->socket;
  328. pccard_mem_map *win;
  329. unsigned int w;
  330. dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
  331. w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
  332. if (w >= MAX_WIN)
  333. return -EINVAL;
  334. mutex_lock(&s->ops_mutex);
  335. win = &s->win[w];
  336. if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
  337. dev_dbg(&p_dev->dev, "not releasing unknown window\n");
  338. mutex_unlock(&s->ops_mutex);
  339. return -EINVAL;
  340. }
  341. /* Shut down memory window */
  342. win->flags &= ~MAP_ACTIVE;
  343. s->ops->set_mem_map(s, win);
  344. s->state &= ~SOCKET_WIN_REQ(w);
  345. /* Release system memory */
  346. if (win->res) {
  347. release_resource(res);
  348. release_resource(win->res);
  349. kfree(win->res);
  350. win->res = NULL;
  351. }
  352. res->start = res->end = 0;
  353. res->flags = IORESOURCE_MEM;
  354. p_dev->_win &= ~CLIENT_WIN_REQ(w);
  355. mutex_unlock(&s->ops_mutex);
  356. return 0;
  357. } /* pcmcia_release_window */
  358. EXPORT_SYMBOL(pcmcia_release_window);
  359. int pcmcia_request_configuration(struct pcmcia_device *p_dev,
  360. config_req_t *req)
  361. {
  362. int i;
  363. u_int base;
  364. struct pcmcia_socket *s = p_dev->socket;
  365. config_t *c;
  366. pccard_io_map iomap;
  367. unsigned char status = 0;
  368. unsigned char ext_status = 0;
  369. unsigned char option = 0;
  370. if (!(s->state & SOCKET_PRESENT))
  371. return -ENODEV;
  372. mutex_lock(&s->ops_mutex);
  373. c = p_dev->function_config;
  374. if (c->state & CONFIG_LOCKED) {
  375. mutex_unlock(&s->ops_mutex);
  376. dev_dbg(&p_dev->dev, "Configuration is locked\n");
  377. return -EACCES;
  378. }
  379. /* Do power control. We don't allow changes in Vcc. */
  380. s->socket.Vpp = p_dev->vpp;
  381. if (s->ops->set_socket(s, &s->socket)) {
  382. mutex_unlock(&s->ops_mutex);
  383. dev_printk(KERN_WARNING, &p_dev->dev,
  384. "Unable to set socket state\n");
  385. return -EINVAL;
  386. }
  387. /* Pick memory or I/O card, DMA mode, interrupt */
  388. c->Attributes = req->Attributes;
  389. if (p_dev->_io)
  390. s->socket.flags |= SS_IOCARD;
  391. if (req->Attributes & CONF_ENABLE_DMA)
  392. s->socket.flags |= SS_DMA_MODE;
  393. if (req->Attributes & 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 (req->Attributes & CONF_ENABLE_IRQ)
  401. s->socket.io_irq = s->pcmcia_irq;
  402. else
  403. s->socket.io_irq = 0;
  404. if (req->Attributes & 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 ((req->Attributes & CONF_ENABLE_IRQ) &&
  432. !(req->Attributes & 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_request_configuration */
  478. EXPORT_SYMBOL(pcmcia_request_configuration);
  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);