pcmcia_resource.c 22 KB

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