pcmcia_resource.c 23 KB

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