pcmcia_resource.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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, window_handle_t wh,
  173. unsigned int offset)
  174. {
  175. struct pcmcia_socket *s = p_dev->socket;
  176. struct resource *res = wh;
  177. unsigned int w;
  178. int ret;
  179. w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
  180. if (w >= MAX_WIN)
  181. return -EINVAL;
  182. mutex_lock(&s->ops_mutex);
  183. s->win[w].card_start = offset;
  184. ret = s->ops->set_mem_map(s, &s->win[w]);
  185. if (ret)
  186. dev_warn(&p_dev->dev, "failed to set_mem_map\n");
  187. mutex_unlock(&s->ops_mutex);
  188. return ret;
  189. } /* pcmcia_map_mem_page */
  190. EXPORT_SYMBOL(pcmcia_map_mem_page);
  191. /** pcmcia_modify_configuration
  192. *
  193. * Modify a locked socket configuration
  194. */
  195. int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
  196. modconf_t *mod)
  197. {
  198. struct pcmcia_socket *s;
  199. config_t *c;
  200. int ret;
  201. s = p_dev->socket;
  202. mutex_lock(&s->ops_mutex);
  203. c = p_dev->function_config;
  204. if (!(s->state & SOCKET_PRESENT)) {
  205. dev_dbg(&p_dev->dev, "No card present\n");
  206. ret = -ENODEV;
  207. goto unlock;
  208. }
  209. if (!(c->state & CONFIG_LOCKED)) {
  210. dev_dbg(&p_dev->dev, "Configuration isnt't locked\n");
  211. ret = -EACCES;
  212. goto unlock;
  213. }
  214. if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
  215. dev_dbg(&p_dev->dev,
  216. "changing Vcc or IRQ is not allowed at this time\n");
  217. ret = -EINVAL;
  218. goto unlock;
  219. }
  220. /* We only allow changing Vpp1 and Vpp2 to the same value */
  221. if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
  222. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  223. if (mod->Vpp1 != mod->Vpp2) {
  224. dev_dbg(&p_dev->dev,
  225. "Vpp1 and Vpp2 must be the same\n");
  226. ret = -EINVAL;
  227. goto unlock;
  228. }
  229. s->socket.Vpp = mod->Vpp1;
  230. if (s->ops->set_socket(s, &s->socket)) {
  231. dev_printk(KERN_WARNING, &p_dev->dev,
  232. "Unable to set VPP\n");
  233. ret = -EIO;
  234. goto unlock;
  235. }
  236. } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
  237. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  238. dev_dbg(&p_dev->dev,
  239. "changing Vcc is not allowed at this time\n");
  240. ret = -EINVAL;
  241. goto unlock;
  242. }
  243. if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
  244. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  245. pccard_io_map io_on;
  246. int i;
  247. io_on.speed = io_speed;
  248. for (i = 0; i < MAX_IO_WIN; i++) {
  249. if (!s->io[i].res)
  250. continue;
  251. io_off.map = i;
  252. io_on.map = i;
  253. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  254. io_on.start = s->io[i].res->start;
  255. io_on.stop = s->io[i].res->end;
  256. s->ops->set_io_map(s, &io_off);
  257. mdelay(40);
  258. s->ops->set_io_map(s, &io_on);
  259. }
  260. }
  261. ret = 0;
  262. unlock:
  263. mutex_unlock(&s->ops_mutex);
  264. return ret;
  265. } /* modify_configuration */
  266. EXPORT_SYMBOL(pcmcia_modify_configuration);
  267. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  268. {
  269. pccard_io_map io = { 0, 0, 0, 0, 1 };
  270. struct pcmcia_socket *s = p_dev->socket;
  271. config_t *c;
  272. int i;
  273. mutex_lock(&s->ops_mutex);
  274. c = p_dev->function_config;
  275. if (p_dev->_locked) {
  276. p_dev->_locked = 0;
  277. if (--(s->lock_count) == 0) {
  278. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  279. s->socket.Vpp = 0;
  280. s->socket.io_irq = 0;
  281. s->ops->set_socket(s, &s->socket);
  282. }
  283. }
  284. if (c->state & CONFIG_LOCKED) {
  285. c->state &= ~CONFIG_LOCKED;
  286. if (c->state & CONFIG_IO_REQ)
  287. for (i = 0; i < MAX_IO_WIN; i++) {
  288. if (!s->io[i].res)
  289. continue;
  290. s->io[i].Config--;
  291. if (s->io[i].Config != 0)
  292. continue;
  293. io.map = i;
  294. s->ops->set_io_map(s, &io);
  295. }
  296. }
  297. mutex_unlock(&s->ops_mutex);
  298. return 0;
  299. } /* pcmcia_release_configuration */
  300. /** pcmcia_release_io
  301. *
  302. * Release_io() releases the I/O ranges allocated by a client. This
  303. * may be invoked some time after a card ejection has already dumped
  304. * the actual socket configuration, so if the client is "stale", we
  305. * don't bother checking the port ranges against the current socket
  306. * values.
  307. */
  308. static int pcmcia_release_io(struct pcmcia_device *p_dev)
  309. {
  310. struct pcmcia_socket *s = p_dev->socket;
  311. int ret = -EINVAL;
  312. config_t *c;
  313. mutex_lock(&s->ops_mutex);
  314. if (!p_dev->_io)
  315. goto out;
  316. c = p_dev->function_config;
  317. release_io_space(s, &c->io[0]);
  318. if (c->io[1].end)
  319. release_io_space(s, &c->io[1]);
  320. p_dev->_io = 0;
  321. c->state &= ~CONFIG_IO_REQ;
  322. out:
  323. mutex_unlock(&s->ops_mutex);
  324. return ret;
  325. } /* pcmcia_release_io */
  326. int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
  327. {
  328. struct pcmcia_socket *s = p_dev->socket;
  329. pccard_mem_map *win;
  330. unsigned int w;
  331. dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
  332. w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
  333. if (w >= MAX_WIN)
  334. return -EINVAL;
  335. mutex_lock(&s->ops_mutex);
  336. win = &s->win[w];
  337. if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
  338. dev_dbg(&p_dev->dev, "not releasing unknown window\n");
  339. mutex_unlock(&s->ops_mutex);
  340. return -EINVAL;
  341. }
  342. /* Shut down memory window */
  343. win->flags &= ~MAP_ACTIVE;
  344. s->ops->set_mem_map(s, win);
  345. s->state &= ~SOCKET_WIN_REQ(w);
  346. /* Release system memory */
  347. if (win->res) {
  348. release_resource(res);
  349. release_resource(win->res);
  350. kfree(win->res);
  351. win->res = NULL;
  352. }
  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. int pcmcia_request_configuration(struct pcmcia_device *p_dev,
  359. config_req_t *req)
  360. {
  361. int i;
  362. u_int base;
  363. struct pcmcia_socket *s = p_dev->socket;
  364. config_t *c;
  365. pccard_io_map iomap;
  366. if (!(s->state & SOCKET_PRESENT))
  367. return -ENODEV;
  368. if (req->IntType & INT_CARDBUS) {
  369. dev_dbg(&p_dev->dev, "IntType may not be INT_CARDBUS\n");
  370. return -EINVAL;
  371. }
  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 = req->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->IntType = req->IntType;
  389. c->Attributes = req->Attributes;
  390. if (req->IntType & INT_MEMORY_AND_IO)
  391. s->socket.flags |= SS_IOCARD;
  392. if (req->IntType & INT_ZOOMED_VIDEO)
  393. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  394. if (req->Attributes & CONF_ENABLE_DMA)
  395. s->socket.flags |= SS_DMA_MODE;
  396. if (req->Attributes & CONF_ENABLE_SPKR)
  397. s->socket.flags |= SS_SPKR_ENA;
  398. if (req->Attributes & CONF_ENABLE_IRQ)
  399. s->socket.io_irq = s->pcmcia_irq;
  400. else
  401. s->socket.io_irq = 0;
  402. s->ops->set_socket(s, &s->socket);
  403. s->lock_count++;
  404. /* Set up CIS configuration registers */
  405. base = c->ConfigBase = req->ConfigBase;
  406. c->CardValues = req->Present;
  407. if (req->Present & PRESENT_COPY) {
  408. c->Copy = req->Copy;
  409. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
  410. }
  411. if (req->Present & PRESENT_OPTION) {
  412. if (s->functions == 1) {
  413. c->Option = req->ConfigIndex & COR_CONFIG_MASK;
  414. } else {
  415. c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
  416. c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
  417. if (req->Present & PRESENT_IOBASE_0)
  418. c->Option |= COR_ADDR_DECODE;
  419. }
  420. if ((req->Attributes & CONF_ENABLE_IRQ) &&
  421. !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
  422. c->Option |= COR_LEVEL_REQ;
  423. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
  424. mdelay(40);
  425. }
  426. if (req->Present & PRESENT_STATUS) {
  427. c->Status = req->Status;
  428. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
  429. }
  430. if (req->Present & PRESENT_PIN_REPLACE) {
  431. c->Pin = req->Pin;
  432. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
  433. }
  434. if (req->Present & PRESENT_EXT_STATUS) {
  435. c->ExtStatus = req->ExtStatus;
  436. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
  437. }
  438. if (req->Present & PRESENT_IOBASE_0) {
  439. u8 b = c->io[0].start & 0xff;
  440. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  441. b = (c->io[0].start >> 8) & 0xff;
  442. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  443. }
  444. if (req->Present & PRESENT_IOSIZE) {
  445. u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
  446. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  447. }
  448. /* Configure I/O windows */
  449. if (c->state & CONFIG_IO_REQ) {
  450. iomap.speed = io_speed;
  451. for (i = 0; i < MAX_IO_WIN; i++)
  452. if (s->io[i].res) {
  453. iomap.map = i;
  454. iomap.flags = MAP_ACTIVE;
  455. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  456. case IO_DATA_PATH_WIDTH_16:
  457. iomap.flags |= MAP_16BIT; break;
  458. case IO_DATA_PATH_WIDTH_AUTO:
  459. iomap.flags |= MAP_AUTOSZ; break;
  460. default:
  461. break;
  462. }
  463. iomap.start = s->io[i].res->start;
  464. iomap.stop = s->io[i].res->end;
  465. s->ops->set_io_map(s, &iomap);
  466. s->io[i].Config++;
  467. }
  468. }
  469. c->state |= CONFIG_LOCKED;
  470. p_dev->_locked = 1;
  471. mutex_unlock(&s->ops_mutex);
  472. return 0;
  473. } /* pcmcia_request_configuration */
  474. EXPORT_SYMBOL(pcmcia_request_configuration);
  475. /**
  476. * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
  477. *
  478. * pcmcia_request_io() attepts to reserve the IO port ranges specified in
  479. * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
  480. * "start" value is the requested start of the IO port resource; "end"
  481. * reflects the number of ports requested. The number of IO lines requested
  482. * is specified in &struct pcmcia_device @p_dev->io_lines.
  483. */
  484. int pcmcia_request_io(struct pcmcia_device *p_dev)
  485. {
  486. struct pcmcia_socket *s = p_dev->socket;
  487. config_t *c = p_dev->function_config;
  488. int ret = -EINVAL;
  489. mutex_lock(&s->ops_mutex);
  490. dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
  491. &c->io[0], &c->io[1]);
  492. if (!(s->state & SOCKET_PRESENT)) {
  493. dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
  494. goto out;
  495. }
  496. if (c->state & CONFIG_LOCKED) {
  497. dev_dbg(&p_dev->dev, "Configuration is locked\n");
  498. goto out;
  499. }
  500. if (c->state & CONFIG_IO_REQ) {
  501. dev_dbg(&p_dev->dev, "IO already configured\n");
  502. goto out;
  503. }
  504. ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
  505. if (ret)
  506. goto out;
  507. if (c->io[1].end) {
  508. ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
  509. if (ret) {
  510. release_io_space(s, &c->io[0]);
  511. goto out;
  512. }
  513. } else
  514. c->io[1].start = 0;
  515. c->state |= CONFIG_IO_REQ;
  516. p_dev->_io = 1;
  517. dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
  518. &c->io[0], &c->io[1]);
  519. out:
  520. mutex_unlock(&s->ops_mutex);
  521. return ret;
  522. } /* pcmcia_request_io */
  523. EXPORT_SYMBOL(pcmcia_request_io);
  524. /**
  525. * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
  526. *
  527. * pcmcia_request_irq() is a wrapper around request_irq which will allow
  528. * the PCMCIA core to clean up the registration in pcmcia_disable_device().
  529. * Drivers are free to use request_irq() directly, but then they need to
  530. * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
  531. * handlers are allowed.
  532. */
  533. int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
  534. irq_handler_t handler)
  535. {
  536. int ret;
  537. if (!p_dev->irq)
  538. return -EINVAL;
  539. ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
  540. p_dev->devname, p_dev->priv);
  541. if (!ret)
  542. p_dev->_irq = 1;
  543. return ret;
  544. }
  545. EXPORT_SYMBOL(pcmcia_request_irq);
  546. /**
  547. * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
  548. *
  549. * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
  550. * attempts first to request an exclusive IRQ. If it fails, it also accepts
  551. * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
  552. * IRQ sharing and either use request_irq directly (then they need to call
  553. * free_irq themselves, too), or the pcmcia_request_irq() function.
  554. */
  555. int __must_check
  556. __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
  557. irq_handler_t handler)
  558. {
  559. int ret;
  560. if (!p_dev->irq)
  561. return -EINVAL;
  562. ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
  563. if (ret) {
  564. ret = pcmcia_request_irq(p_dev, handler);
  565. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
  566. "request for exclusive IRQ could not be fulfilled.\n");
  567. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
  568. "needs updating to supported shared IRQ lines.\n");
  569. }
  570. if (ret)
  571. dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
  572. else
  573. p_dev->_irq = 1;
  574. return ret;
  575. } /* pcmcia_request_exclusive_irq */
  576. EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
  577. #ifdef CONFIG_PCMCIA_PROBE
  578. /* mask of IRQs already reserved by other cards, we should avoid using them */
  579. static u8 pcmcia_used_irq[32];
  580. static irqreturn_t test_action(int cpl, void *dev_id)
  581. {
  582. return IRQ_NONE;
  583. }
  584. /**
  585. * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
  586. * @p_dev - the associated PCMCIA device
  587. *
  588. * locking note: must be called with ops_mutex locked.
  589. */
  590. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  591. {
  592. struct pcmcia_socket *s = p_dev->socket;
  593. unsigned int try, irq;
  594. u32 mask = s->irq_mask;
  595. int ret = -ENODEV;
  596. for (try = 0; try < 64; try++) {
  597. irq = try % 32;
  598. if (irq > NR_IRQS)
  599. continue;
  600. /* marked as available by driver, not blocked by userspace? */
  601. if (!((mask >> irq) & 1))
  602. continue;
  603. /* avoid an IRQ which is already used by another PCMCIA card */
  604. if ((try < 32) && pcmcia_used_irq[irq])
  605. continue;
  606. /* register the correct driver, if possible, to check whether
  607. * registering a dummy handle works, i.e. if the IRQ isn't
  608. * marked as used by the kernel resource management core */
  609. ret = request_irq(irq, test_action, type, p_dev->devname,
  610. p_dev);
  611. if (!ret) {
  612. free_irq(irq, p_dev);
  613. p_dev->irq = s->pcmcia_irq = irq;
  614. pcmcia_used_irq[irq]++;
  615. break;
  616. }
  617. }
  618. return ret;
  619. }
  620. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  621. {
  622. pcmcia_used_irq[s->pcmcia_irq]--;
  623. s->pcmcia_irq = 0;
  624. }
  625. #else /* CONFIG_PCMCIA_PROBE */
  626. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  627. {
  628. return -EINVAL;
  629. }
  630. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  631. {
  632. s->pcmcia_irq = 0;
  633. return;
  634. }
  635. #endif /* CONFIG_PCMCIA_PROBE */
  636. /**
  637. * pcmcia_setup_irq() - determine IRQ to be used for device
  638. * @p_dev - the associated PCMCIA device
  639. *
  640. * locking note: must be called with ops_mutex locked.
  641. */
  642. int pcmcia_setup_irq(struct pcmcia_device *p_dev)
  643. {
  644. struct pcmcia_socket *s = p_dev->socket;
  645. if (p_dev->irq)
  646. return 0;
  647. /* already assigned? */
  648. if (s->pcmcia_irq) {
  649. p_dev->irq = s->pcmcia_irq;
  650. return 0;
  651. }
  652. /* prefer an exclusive ISA irq */
  653. if (!pcmcia_setup_isa_irq(p_dev, 0))
  654. return 0;
  655. /* but accept a shared ISA irq */
  656. if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
  657. return 0;
  658. /* but use the PCI irq otherwise */
  659. if (s->pci_irq) {
  660. p_dev->irq = s->pcmcia_irq = s->pci_irq;
  661. return 0;
  662. }
  663. return -EINVAL;
  664. }
  665. /** pcmcia_request_window
  666. *
  667. * Request_window() establishes a mapping between card memory space
  668. * and system memory space.
  669. */
  670. int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
  671. {
  672. struct pcmcia_socket *s = p_dev->socket;
  673. pccard_mem_map *win;
  674. u_long align;
  675. struct resource *res;
  676. int w;
  677. if (!(s->state & SOCKET_PRESENT)) {
  678. dev_dbg(&p_dev->dev, "No card present\n");
  679. return -ENODEV;
  680. }
  681. /* Window size defaults to smallest available */
  682. if (req->Size == 0)
  683. req->Size = s->map_size;
  684. align = (s->features & SS_CAP_MEM_ALIGN) ? req->Size : s->map_size;
  685. if (req->Size & (s->map_size-1)) {
  686. dev_dbg(&p_dev->dev, "invalid map size\n");
  687. return -EINVAL;
  688. }
  689. if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
  690. (req->Base & (align-1))) {
  691. dev_dbg(&p_dev->dev, "invalid base address\n");
  692. return -EINVAL;
  693. }
  694. if (req->Base)
  695. align = 0;
  696. /* Allocate system memory window */
  697. mutex_lock(&s->ops_mutex);
  698. for (w = 0; w < MAX_WIN; w++)
  699. if (!(s->state & SOCKET_WIN_REQ(w)))
  700. break;
  701. if (w == MAX_WIN) {
  702. dev_dbg(&p_dev->dev, "all windows are used already\n");
  703. mutex_unlock(&s->ops_mutex);
  704. return -EINVAL;
  705. }
  706. win = &s->win[w];
  707. if (!(s->features & SS_CAP_STATIC_MAP)) {
  708. win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
  709. 0, s);
  710. if (!win->res) {
  711. dev_dbg(&p_dev->dev, "allocating mem region failed\n");
  712. mutex_unlock(&s->ops_mutex);
  713. return -EINVAL;
  714. }
  715. }
  716. p_dev->_win |= CLIENT_WIN_REQ(w);
  717. /* Configure the socket controller */
  718. win->map = w+1;
  719. win->flags = req->Attributes;
  720. win->speed = req->AccessSpeed;
  721. win->card_start = 0;
  722. if (s->ops->set_mem_map(s, win) != 0) {
  723. dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
  724. mutex_unlock(&s->ops_mutex);
  725. return -EIO;
  726. }
  727. s->state |= SOCKET_WIN_REQ(w);
  728. /* Return window handle */
  729. if (s->features & SS_CAP_STATIC_MAP)
  730. req->Base = win->static_start;
  731. else
  732. req->Base = win->res->start;
  733. /* convert to new-style resources */
  734. res = p_dev->resource[w + MAX_IO_WIN];
  735. res->start = req->Base;
  736. res->end = req->Base + req->Size - 1;
  737. res->flags &= ~IORESOURCE_BITS;
  738. res->flags |= (req->Attributes & WIN_FLAGS_MAP) | (win->map << 2);
  739. res->flags |= IORESOURCE_MEM;
  740. res->parent = win->res;
  741. if (win->res)
  742. request_resource(&iomem_resource, res);
  743. dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
  744. mutex_unlock(&s->ops_mutex);
  745. *wh = res;
  746. return 0;
  747. } /* pcmcia_request_window */
  748. EXPORT_SYMBOL(pcmcia_request_window);
  749. void pcmcia_disable_device(struct pcmcia_device *p_dev)
  750. {
  751. int i;
  752. for (i = 0; i < MAX_WIN; i++) {
  753. struct resource *res = p_dev->resource[MAX_IO_WIN + i];
  754. if (res->flags & WIN_FLAGS_REQ)
  755. pcmcia_release_window(p_dev, res);
  756. }
  757. pcmcia_release_configuration(p_dev);
  758. pcmcia_release_io(p_dev);
  759. if (p_dev->_irq) {
  760. free_irq(p_dev->irq, p_dev->priv);
  761. p_dev->_irq = 0;
  762. }
  763. }
  764. EXPORT_SYMBOL(pcmcia_disable_device);