pcmcia_resource.c 22 KB

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