pcmcia_resource.c 22 KB

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