pcmcia_resource.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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/cs_types.h>
  26. #include <pcmcia/ss.h>
  27. #include <pcmcia/cs.h>
  28. #include <pcmcia/cistpl.h>
  29. #include <pcmcia/cisreg.h>
  30. #include <pcmcia/ds.h>
  31. #include "cs_internal.h"
  32. /* Access speed for IO windows */
  33. static int io_speed;
  34. module_param(io_speed, int, 0444);
  35. static int pcmcia_adjust_io_region(struct resource *res, unsigned long start,
  36. unsigned long end, struct pcmcia_socket *s)
  37. {
  38. if (s->resource_ops->adjust_io_region)
  39. return s->resource_ops->adjust_io_region(res, start, end, s);
  40. return -ENOMEM;
  41. }
  42. static struct resource *pcmcia_find_io_region(unsigned long base, int num,
  43. unsigned long align,
  44. struct pcmcia_socket *s)
  45. {
  46. if (s->resource_ops->find_io)
  47. return s->resource_ops->find_io(base, num, align, s);
  48. return NULL;
  49. }
  50. int pcmcia_validate_mem(struct pcmcia_socket *s)
  51. {
  52. if (s->resource_ops->validate_mem)
  53. return s->resource_ops->validate_mem(s);
  54. /* if there is no callback, we can assume that everything is OK */
  55. return 0;
  56. }
  57. struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  58. int low, struct pcmcia_socket *s)
  59. {
  60. if (s->resource_ops->find_mem)
  61. return s->resource_ops->find_mem(base, num, align, low, s);
  62. return NULL;
  63. }
  64. /** alloc_io_space
  65. *
  66. * Special stuff for managing IO windows, because they are scarce
  67. */
  68. static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
  69. unsigned int *base, unsigned int num, u_int lines)
  70. {
  71. int i;
  72. unsigned int try, align;
  73. align = (*base) ? (lines ? 1<<lines : 0) : 1;
  74. if (align && (align < num)) {
  75. if (*base) {
  76. dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
  77. num, align);
  78. align = 0;
  79. } else
  80. while (align && (align < num))
  81. align <<= 1;
  82. }
  83. if (*base & ~(align-1)) {
  84. dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
  85. *base, align);
  86. align = 0;
  87. }
  88. if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
  89. *base = s->io_offset | (*base & 0x0fff);
  90. return 0;
  91. }
  92. /* Check for an already-allocated window that must conflict with
  93. * what was asked for. It is a hack because it does not catch all
  94. * potential conflicts, just the most obvious ones.
  95. */
  96. for (i = 0; i < MAX_IO_WIN; i++)
  97. if ((s->io[i].res) && *base &&
  98. ((s->io[i].res->start & (align-1)) == *base))
  99. return 1;
  100. for (i = 0; i < MAX_IO_WIN; i++) {
  101. if (!s->io[i].res) {
  102. s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
  103. if (s->io[i].res) {
  104. *base = s->io[i].res->start;
  105. s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
  106. s->io[i].InUse = num;
  107. break;
  108. } else
  109. return 1;
  110. } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
  111. continue;
  112. /* Try to extend top of window */
  113. try = s->io[i].res->end + 1;
  114. if ((*base == 0) || (*base == try))
  115. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
  116. s->io[i].res->end + num, s) == 0) {
  117. *base = try;
  118. s->io[i].InUse += num;
  119. break;
  120. }
  121. /* Try to extend bottom of window */
  122. try = s->io[i].res->start - num;
  123. if ((*base == 0) || (*base == try))
  124. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
  125. s->io[i].res->end, s) == 0) {
  126. *base = try;
  127. s->io[i].InUse += num;
  128. break;
  129. }
  130. }
  131. return (i == MAX_IO_WIN);
  132. } /* alloc_io_space */
  133. static void release_io_space(struct pcmcia_socket *s, unsigned int base,
  134. unsigned int num)
  135. {
  136. int i;
  137. for (i = 0; i < MAX_IO_WIN; i++) {
  138. if (!s->io[i].res)
  139. continue;
  140. if ((s->io[i].res->start <= base) &&
  141. (s->io[i].res->end >= base+num-1)) {
  142. s->io[i].InUse -= num;
  143. /* Free the window if no one else is using it */
  144. if (s->io[i].InUse == 0) {
  145. release_resource(s->io[i].res);
  146. kfree(s->io[i].res);
  147. s->io[i].res = NULL;
  148. }
  149. }
  150. }
  151. } /* release_io_space */
  152. /** pccard_access_configuration_register
  153. *
  154. * Access_configuration_register() reads and writes configuration
  155. * registers in attribute memory. Memory window 0 is reserved for
  156. * this and the tuple reading services.
  157. */
  158. int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
  159. conf_reg_t *reg)
  160. {
  161. struct pcmcia_socket *s;
  162. config_t *c;
  163. int addr;
  164. u_char val;
  165. if (!p_dev || !p_dev->function_config)
  166. return -EINVAL;
  167. s = p_dev->socket;
  168. mutex_lock(&s->ops_mutex);
  169. c = p_dev->function_config;
  170. if (!(c->state & CONFIG_LOCKED)) {
  171. dev_dbg(&s->dev, "Configuration isnt't locked\n");
  172. mutex_unlock(&s->ops_mutex);
  173. return -EACCES;
  174. }
  175. addr = (c->ConfigBase + reg->Offset) >> 1;
  176. mutex_unlock(&s->ops_mutex);
  177. switch (reg->Action) {
  178. case CS_READ:
  179. pcmcia_read_cis_mem(s, 1, addr, 1, &val);
  180. reg->Value = val;
  181. break;
  182. case CS_WRITE:
  183. val = reg->Value;
  184. pcmcia_write_cis_mem(s, 1, addr, 1, &val);
  185. break;
  186. default:
  187. dev_dbg(&s->dev, "Invalid conf register request\n");
  188. return -EINVAL;
  189. break;
  190. }
  191. return 0;
  192. } /* pcmcia_access_configuration_register */
  193. EXPORT_SYMBOL(pcmcia_access_configuration_register);
  194. int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
  195. memreq_t *req)
  196. {
  197. struct pcmcia_socket *s = p_dev->socket;
  198. int ret;
  199. wh--;
  200. if (wh >= MAX_WIN)
  201. return -EINVAL;
  202. if (req->Page != 0) {
  203. dev_dbg(&s->dev, "failure: requested page is zero\n");
  204. return -EINVAL;
  205. }
  206. mutex_lock(&s->ops_mutex);
  207. s->win[wh].card_start = req->CardOffset;
  208. ret = s->ops->set_mem_map(s, &s->win[wh]);
  209. if (ret)
  210. dev_warn(&s->dev, "failed to set_mem_map\n");
  211. mutex_unlock(&s->ops_mutex);
  212. return ret;
  213. } /* pcmcia_map_mem_page */
  214. EXPORT_SYMBOL(pcmcia_map_mem_page);
  215. /** pcmcia_modify_configuration
  216. *
  217. * Modify a locked socket configuration
  218. */
  219. int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
  220. modconf_t *mod)
  221. {
  222. struct pcmcia_socket *s;
  223. config_t *c;
  224. int ret;
  225. s = p_dev->socket;
  226. mutex_lock(&s->ops_mutex);
  227. c = p_dev->function_config;
  228. if (!(s->state & SOCKET_PRESENT)) {
  229. dev_dbg(&s->dev, "No card present\n");
  230. ret = -ENODEV;
  231. goto unlock;
  232. }
  233. if (!(c->state & CONFIG_LOCKED)) {
  234. dev_dbg(&s->dev, "Configuration isnt't locked\n");
  235. ret = -EACCES;
  236. goto unlock;
  237. }
  238. if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
  239. dev_dbg(&s->dev,
  240. "changing Vcc or IRQ is not allowed at this time\n");
  241. ret = -EINVAL;
  242. goto unlock;
  243. }
  244. /* We only allow changing Vpp1 and Vpp2 to the same value */
  245. if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
  246. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  247. if (mod->Vpp1 != mod->Vpp2) {
  248. dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
  249. ret = -EINVAL;
  250. goto unlock;
  251. }
  252. s->socket.Vpp = mod->Vpp1;
  253. if (s->ops->set_socket(s, &s->socket)) {
  254. dev_printk(KERN_WARNING, &s->dev,
  255. "Unable to set VPP\n");
  256. ret = -EIO;
  257. goto unlock;
  258. }
  259. } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
  260. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  261. dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
  262. ret = -EINVAL;
  263. goto unlock;
  264. }
  265. if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
  266. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  267. pccard_io_map io_on;
  268. int i;
  269. io_on.speed = io_speed;
  270. for (i = 0; i < MAX_IO_WIN; i++) {
  271. if (!s->io[i].res)
  272. continue;
  273. io_off.map = i;
  274. io_on.map = i;
  275. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  276. io_on.start = s->io[i].res->start;
  277. io_on.stop = s->io[i].res->end;
  278. s->ops->set_io_map(s, &io_off);
  279. mdelay(40);
  280. s->ops->set_io_map(s, &io_on);
  281. }
  282. }
  283. ret = 0;
  284. unlock:
  285. mutex_unlock(&s->ops_mutex);
  286. return ret;
  287. } /* modify_configuration */
  288. EXPORT_SYMBOL(pcmcia_modify_configuration);
  289. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  290. {
  291. pccard_io_map io = { 0, 0, 0, 0, 1 };
  292. struct pcmcia_socket *s = p_dev->socket;
  293. config_t *c;
  294. int i;
  295. mutex_lock(&s->ops_mutex);
  296. c = p_dev->function_config;
  297. if (p_dev->_locked) {
  298. p_dev->_locked = 0;
  299. if (--(s->lock_count) == 0) {
  300. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  301. s->socket.Vpp = 0;
  302. s->socket.io_irq = 0;
  303. s->ops->set_socket(s, &s->socket);
  304. }
  305. }
  306. if (c->state & CONFIG_LOCKED) {
  307. c->state &= ~CONFIG_LOCKED;
  308. if (c->state & CONFIG_IO_REQ)
  309. for (i = 0; i < MAX_IO_WIN; i++) {
  310. if (!s->io[i].res)
  311. continue;
  312. s->io[i].Config--;
  313. if (s->io[i].Config != 0)
  314. continue;
  315. io.map = i;
  316. s->ops->set_io_map(s, &io);
  317. }
  318. }
  319. mutex_unlock(&s->ops_mutex);
  320. return 0;
  321. } /* pcmcia_release_configuration */
  322. /** pcmcia_release_io
  323. *
  324. * Release_io() releases the I/O ranges allocated by a client. This
  325. * may be invoked some time after a card ejection has already dumped
  326. * the actual socket configuration, so if the client is "stale", we
  327. * don't bother checking the port ranges against the current socket
  328. * values.
  329. */
  330. static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
  331. {
  332. struct pcmcia_socket *s = p_dev->socket;
  333. int ret = -EINVAL;
  334. config_t *c;
  335. mutex_lock(&s->ops_mutex);
  336. c = p_dev->function_config;
  337. if (!p_dev->_io)
  338. goto out;
  339. p_dev->_io = 0;
  340. if ((c->io.BasePort1 != req->BasePort1) ||
  341. (c->io.NumPorts1 != req->NumPorts1) ||
  342. (c->io.BasePort2 != req->BasePort2) ||
  343. (c->io.NumPorts2 != req->NumPorts2))
  344. goto out;
  345. c->state &= ~CONFIG_IO_REQ;
  346. release_io_space(s, req->BasePort1, req->NumPorts1);
  347. if (req->NumPorts2)
  348. release_io_space(s, req->BasePort2, req->NumPorts2);
  349. out:
  350. mutex_unlock(&s->ops_mutex);
  351. return ret;
  352. } /* pcmcia_release_io */
  353. int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
  354. {
  355. struct pcmcia_socket *s = p_dev->socket;
  356. pccard_mem_map *win;
  357. wh--;
  358. if (wh >= MAX_WIN)
  359. return -EINVAL;
  360. mutex_lock(&s->ops_mutex);
  361. win = &s->win[wh];
  362. if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
  363. dev_dbg(&s->dev, "not releasing unknown window\n");
  364. mutex_unlock(&s->ops_mutex);
  365. return -EINVAL;
  366. }
  367. /* Shut down memory window */
  368. win->flags &= ~MAP_ACTIVE;
  369. s->ops->set_mem_map(s, win);
  370. s->state &= ~SOCKET_WIN_REQ(wh);
  371. /* Release system memory */
  372. if (win->res) {
  373. release_resource(win->res);
  374. kfree(win->res);
  375. win->res = NULL;
  376. }
  377. p_dev->_win &= ~CLIENT_WIN_REQ(wh);
  378. mutex_unlock(&s->ops_mutex);
  379. return 0;
  380. } /* pcmcia_release_window */
  381. EXPORT_SYMBOL(pcmcia_release_window);
  382. int pcmcia_request_configuration(struct pcmcia_device *p_dev,
  383. config_req_t *req)
  384. {
  385. int i;
  386. u_int base;
  387. struct pcmcia_socket *s = p_dev->socket;
  388. config_t *c;
  389. pccard_io_map iomap;
  390. if (!(s->state & SOCKET_PRESENT))
  391. return -ENODEV;
  392. if (req->IntType & INT_CARDBUS) {
  393. dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
  394. return -EINVAL;
  395. }
  396. mutex_lock(&s->ops_mutex);
  397. c = p_dev->function_config;
  398. if (c->state & CONFIG_LOCKED) {
  399. mutex_unlock(&s->ops_mutex);
  400. dev_dbg(&s->dev, "Configuration is locked\n");
  401. return -EACCES;
  402. }
  403. /* Do power control. We don't allow changes in Vcc. */
  404. s->socket.Vpp = req->Vpp;
  405. if (s->ops->set_socket(s, &s->socket)) {
  406. mutex_unlock(&s->ops_mutex);
  407. dev_printk(KERN_WARNING, &s->dev,
  408. "Unable to set socket state\n");
  409. return -EINVAL;
  410. }
  411. /* Pick memory or I/O card, DMA mode, interrupt */
  412. c->IntType = req->IntType;
  413. c->Attributes = req->Attributes;
  414. if (req->IntType & INT_MEMORY_AND_IO)
  415. s->socket.flags |= SS_IOCARD;
  416. if (req->IntType & INT_ZOOMED_VIDEO)
  417. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  418. if (req->Attributes & CONF_ENABLE_DMA)
  419. s->socket.flags |= SS_DMA_MODE;
  420. if (req->Attributes & CONF_ENABLE_SPKR)
  421. s->socket.flags |= SS_SPKR_ENA;
  422. if (req->Attributes & CONF_ENABLE_IRQ)
  423. s->socket.io_irq = s->pcmcia_irq;
  424. else
  425. s->socket.io_irq = 0;
  426. s->ops->set_socket(s, &s->socket);
  427. s->lock_count++;
  428. mutex_unlock(&s->ops_mutex);
  429. /* Set up CIS configuration registers */
  430. base = c->ConfigBase = req->ConfigBase;
  431. c->CardValues = req->Present;
  432. if (req->Present & PRESENT_COPY) {
  433. c->Copy = req->Copy;
  434. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
  435. }
  436. if (req->Present & PRESENT_OPTION) {
  437. if (s->functions == 1) {
  438. c->Option = req->ConfigIndex & COR_CONFIG_MASK;
  439. } else {
  440. c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
  441. c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
  442. if (req->Present & PRESENT_IOBASE_0)
  443. c->Option |= COR_ADDR_DECODE;
  444. }
  445. if ((req->Attributes & CONF_ENABLE_IRQ) &&
  446. !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
  447. c->Option |= COR_LEVEL_REQ;
  448. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
  449. mdelay(40);
  450. }
  451. if (req->Present & PRESENT_STATUS) {
  452. c->Status = req->Status;
  453. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
  454. }
  455. if (req->Present & PRESENT_PIN_REPLACE) {
  456. c->Pin = req->Pin;
  457. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
  458. }
  459. if (req->Present & PRESENT_EXT_STATUS) {
  460. c->ExtStatus = req->ExtStatus;
  461. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
  462. }
  463. if (req->Present & PRESENT_IOBASE_0) {
  464. u_char b = c->io.BasePort1 & 0xff;
  465. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  466. b = (c->io.BasePort1 >> 8) & 0xff;
  467. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  468. }
  469. if (req->Present & PRESENT_IOSIZE) {
  470. u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
  471. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  472. }
  473. /* Configure I/O windows */
  474. if (c->state & CONFIG_IO_REQ) {
  475. mutex_lock(&s->ops_mutex);
  476. iomap.speed = io_speed;
  477. for (i = 0; i < MAX_IO_WIN; i++)
  478. if (s->io[i].res) {
  479. iomap.map = i;
  480. iomap.flags = MAP_ACTIVE;
  481. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  482. case IO_DATA_PATH_WIDTH_16:
  483. iomap.flags |= MAP_16BIT; break;
  484. case IO_DATA_PATH_WIDTH_AUTO:
  485. iomap.flags |= MAP_AUTOSZ; break;
  486. default:
  487. break;
  488. }
  489. iomap.start = s->io[i].res->start;
  490. iomap.stop = s->io[i].res->end;
  491. s->ops->set_io_map(s, &iomap);
  492. s->io[i].Config++;
  493. }
  494. mutex_unlock(&s->ops_mutex);
  495. }
  496. c->state |= CONFIG_LOCKED;
  497. p_dev->_locked = 1;
  498. return 0;
  499. } /* pcmcia_request_configuration */
  500. EXPORT_SYMBOL(pcmcia_request_configuration);
  501. /** pcmcia_request_io
  502. *
  503. * Request_io() reserves ranges of port addresses for a socket.
  504. * I have not implemented range sharing or alias addressing.
  505. */
  506. int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
  507. {
  508. struct pcmcia_socket *s = p_dev->socket;
  509. config_t *c;
  510. int ret = -EINVAL;
  511. mutex_lock(&s->ops_mutex);
  512. if (!(s->state & SOCKET_PRESENT)) {
  513. dev_dbg(&s->dev, "No card present\n");
  514. goto out;
  515. }
  516. if (!req)
  517. goto out;
  518. c = p_dev->function_config;
  519. if (c->state & CONFIG_LOCKED) {
  520. dev_dbg(&s->dev, "Configuration is locked\n");
  521. goto out;
  522. }
  523. if (c->state & CONFIG_IO_REQ) {
  524. dev_dbg(&s->dev, "IO already configured\n");
  525. goto out;
  526. }
  527. if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
  528. dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
  529. goto out;
  530. }
  531. if ((req->NumPorts2 > 0) &&
  532. (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
  533. dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
  534. goto out;
  535. }
  536. dev_dbg(&s->dev, "trying to allocate resource 1\n");
  537. ret = alloc_io_space(s, req->Attributes1, &req->BasePort1,
  538. req->NumPorts1, req->IOAddrLines);
  539. if (ret) {
  540. dev_dbg(&s->dev, "allocation of resource 1 failed\n");
  541. goto out;
  542. }
  543. if (req->NumPorts2) {
  544. dev_dbg(&s->dev, "trying to allocate resource 2\n");
  545. ret = alloc_io_space(s, req->Attributes2, &req->BasePort2,
  546. req->NumPorts2, req->IOAddrLines);
  547. if (ret) {
  548. dev_dbg(&s->dev, "allocation of resource 2 failed\n");
  549. release_io_space(s, req->BasePort1, req->NumPorts1);
  550. goto out;
  551. }
  552. }
  553. c->io = *req;
  554. c->state |= CONFIG_IO_REQ;
  555. p_dev->_io = 1;
  556. dev_dbg(&s->dev, "allocating resources succeeded: %d\n", ret);
  557. out:
  558. mutex_unlock(&s->ops_mutex);
  559. return ret;
  560. } /* pcmcia_request_io */
  561. EXPORT_SYMBOL(pcmcia_request_io);
  562. /**
  563. * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
  564. *
  565. * pcmcia_request_irq() is a wrapper around request_irq which will allow
  566. * the PCMCIA core to clean up the registration in pcmcia_disable_device().
  567. * Drivers are free to use request_irq() directly, but then they need to
  568. * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
  569. * handlers are allowed.
  570. */
  571. int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
  572. irq_handler_t handler)
  573. {
  574. int ret;
  575. if (!p_dev->irq)
  576. return -EINVAL;
  577. ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
  578. p_dev->devname, p_dev->priv);
  579. if (!ret)
  580. p_dev->_irq = 1;
  581. return ret;
  582. }
  583. EXPORT_SYMBOL(pcmcia_request_irq);
  584. /**
  585. * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
  586. *
  587. * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
  588. * attempts first to request an exclusive IRQ. If it fails, it also accepts
  589. * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
  590. * IRQ sharing and either use request_irq directly (then they need to call
  591. * free_irq themselves, too), or the pcmcia_request_irq() function.
  592. */
  593. int __must_check
  594. pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev, irq_handler_t handler)
  595. {
  596. int ret;
  597. if (!p_dev->irq)
  598. return -EINVAL;
  599. ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
  600. if (ret) {
  601. ret = pcmcia_request_irq(p_dev, handler);
  602. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
  603. "request for exclusive IRQ could not be fulfilled.\n");
  604. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
  605. "needs updating to supported shared IRQ lines.\n");
  606. }
  607. if (ret)
  608. dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
  609. else
  610. p_dev->_irq = 1;
  611. return ret;
  612. } /* pcmcia_request_exclusive_irq */
  613. EXPORT_SYMBOL(pcmcia_request_exclusive_irq);
  614. #ifdef CONFIG_PCMCIA_PROBE
  615. /* mask of IRQs already reserved by other cards, we should avoid using them */
  616. static u8 pcmcia_used_irq[NR_IRQS];
  617. static irqreturn_t test_action(int cpl, void *dev_id)
  618. {
  619. return IRQ_NONE;
  620. }
  621. /**
  622. * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
  623. * @p_dev - the associated PCMCIA device
  624. *
  625. * locking note: must be called with ops_mutex locked.
  626. */
  627. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  628. {
  629. struct pcmcia_socket *s = p_dev->socket;
  630. unsigned int try, irq;
  631. u32 mask = s->irq_mask;
  632. int ret = -ENODEV;
  633. for (try = 0; try < 64; try++) {
  634. irq = try % 32;
  635. /* marked as available by driver, not blocked by userspace? */
  636. if (!((mask >> irq) & 1))
  637. continue;
  638. /* avoid an IRQ which is already used by another PCMCIA card */
  639. if ((try < 32) && pcmcia_used_irq[irq])
  640. continue;
  641. /* register the correct driver, if possible, to check whether
  642. * registering a dummy handle works, i.e. if the IRQ isn't
  643. * marked as used by the kernel resource management core */
  644. ret = request_irq(irq, test_action, type, p_dev->devname,
  645. p_dev);
  646. if (!ret) {
  647. free_irq(irq, p_dev);
  648. p_dev->irq = s->pcmcia_irq = irq;
  649. pcmcia_used_irq[irq]++;
  650. break;
  651. }
  652. }
  653. return ret;
  654. }
  655. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  656. {
  657. pcmcia_used_irq[s->pcmcia_irq]--;
  658. s->pcmcia_irq = 0;
  659. }
  660. #else /* CONFIG_PCMCIA_PROBE */
  661. static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
  662. {
  663. return -EINVAL;
  664. }
  665. void pcmcia_cleanup_irq(struct pcmcia_socket *s)
  666. {
  667. s->pcmcia_irq = 0;
  668. return;
  669. }
  670. #endif /* CONFIG_PCMCIA_PROBE */
  671. /**
  672. * pcmcia_setup_irq() - determine IRQ to be used for device
  673. * @p_dev - the associated PCMCIA device
  674. *
  675. * locking note: must be called with ops_mutex locked.
  676. */
  677. int pcmcia_setup_irq(struct pcmcia_device *p_dev)
  678. {
  679. struct pcmcia_socket *s = p_dev->socket;
  680. if (p_dev->irq)
  681. return 0;
  682. /* already assigned? */
  683. if (s->pcmcia_irq) {
  684. p_dev->irq = s->pcmcia_irq;
  685. return 0;
  686. }
  687. /* prefer an exclusive ISA irq */
  688. if (!pcmcia_setup_isa_irq(p_dev, 0))
  689. return 0;
  690. /* but accept a shared ISA irq */
  691. if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
  692. return 0;
  693. /* but use the PCI irq otherwise */
  694. if (s->pci_irq) {
  695. p_dev->irq = s->pcmcia_irq = s->pci_irq;
  696. return 0;
  697. }
  698. return -EINVAL;
  699. }
  700. /** pcmcia_request_window
  701. *
  702. * Request_window() establishes a mapping between card memory space
  703. * and system memory space.
  704. */
  705. int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
  706. {
  707. struct pcmcia_socket *s = p_dev->socket;
  708. pccard_mem_map *win;
  709. u_long align;
  710. int w;
  711. if (!(s->state & SOCKET_PRESENT)) {
  712. dev_dbg(&s->dev, "No card present\n");
  713. return -ENODEV;
  714. }
  715. if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
  716. dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
  717. return -EINVAL;
  718. }
  719. /* Window size defaults to smallest available */
  720. if (req->Size == 0)
  721. req->Size = s->map_size;
  722. align = (((s->features & SS_CAP_MEM_ALIGN) ||
  723. (req->Attributes & WIN_STRICT_ALIGN)) ?
  724. req->Size : s->map_size);
  725. if (req->Size & (s->map_size-1)) {
  726. dev_dbg(&s->dev, "invalid map size\n");
  727. return -EINVAL;
  728. }
  729. if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
  730. (req->Base & (align-1))) {
  731. dev_dbg(&s->dev, "invalid base address\n");
  732. return -EINVAL;
  733. }
  734. if (req->Base)
  735. align = 0;
  736. /* Allocate system memory window */
  737. for (w = 0; w < MAX_WIN; w++)
  738. if (!(s->state & SOCKET_WIN_REQ(w)))
  739. break;
  740. if (w == MAX_WIN) {
  741. dev_dbg(&s->dev, "all windows are used already\n");
  742. return -EINVAL;
  743. }
  744. mutex_lock(&s->ops_mutex);
  745. win = &s->win[w];
  746. if (!(s->features & SS_CAP_STATIC_MAP)) {
  747. win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
  748. (req->Attributes & WIN_MAP_BELOW_1MB), s);
  749. if (!win->res) {
  750. dev_dbg(&s->dev, "allocating mem region failed\n");
  751. mutex_unlock(&s->ops_mutex);
  752. return -EINVAL;
  753. }
  754. }
  755. p_dev->_win |= CLIENT_WIN_REQ(w);
  756. /* Configure the socket controller */
  757. win->map = w+1;
  758. win->flags = 0;
  759. win->speed = req->AccessSpeed;
  760. if (req->Attributes & WIN_MEMORY_TYPE)
  761. win->flags |= MAP_ATTRIB;
  762. if (req->Attributes & WIN_ENABLE)
  763. win->flags |= MAP_ACTIVE;
  764. if (req->Attributes & WIN_DATA_WIDTH_16)
  765. win->flags |= MAP_16BIT;
  766. if (req->Attributes & WIN_USE_WAIT)
  767. win->flags |= MAP_USE_WAIT;
  768. win->card_start = 0;
  769. if (s->ops->set_mem_map(s, win) != 0) {
  770. dev_dbg(&s->dev, "failed to set memory mapping\n");
  771. mutex_unlock(&s->ops_mutex);
  772. return -EIO;
  773. }
  774. s->state |= SOCKET_WIN_REQ(w);
  775. /* Return window handle */
  776. if (s->features & SS_CAP_STATIC_MAP)
  777. req->Base = win->static_start;
  778. else
  779. req->Base = win->res->start;
  780. mutex_unlock(&s->ops_mutex);
  781. *wh = w + 1;
  782. return 0;
  783. } /* pcmcia_request_window */
  784. EXPORT_SYMBOL(pcmcia_request_window);
  785. void pcmcia_disable_device(struct pcmcia_device *p_dev)
  786. {
  787. pcmcia_release_configuration(p_dev);
  788. pcmcia_release_io(p_dev, &p_dev->io);
  789. if (p_dev->_irq)
  790. free_irq(p_dev->irq, p_dev->priv);
  791. if (p_dev->win)
  792. pcmcia_release_window(p_dev, p_dev->win);
  793. }
  794. EXPORT_SYMBOL(pcmcia_disable_device);