pcmcia_resource.c 23 KB

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