pcmcia_resource.c 21 KB

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