pcmcia_resource.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. #define IN_CARD_SERVICES
  23. #include <pcmcia/cs_types.h>
  24. #include <pcmcia/ss.h>
  25. #include <pcmcia/cs.h>
  26. #include <pcmcia/bulkmem.h>
  27. #include <pcmcia/cistpl.h>
  28. #include <pcmcia/cisreg.h>
  29. #include <pcmcia/ds.h>
  30. #include "cs_internal.h"
  31. #include "ds_internal.h"
  32. /* Access speed for IO windows */
  33. static int io_speed = 0;
  34. module_param(io_speed, int, 0444);
  35. #ifdef CONFIG_PCMCIA_PROBE
  36. #include <asm/irq.h>
  37. /* mask of IRQs already reserved by other cards, we should avoid using them */
  38. static u8 pcmcia_used_irq[NR_IRQS];
  39. #endif
  40. #ifdef DEBUG
  41. extern int ds_pc_debug;
  42. #define cs_socket_name(skt) ((skt)->dev.class_id)
  43. #define ds_dbg(skt, lvl, fmt, arg...) do { \
  44. if (ds_pc_debug >= lvl) \
  45. printk(KERN_DEBUG "pcmcia_resource: %s: " fmt, \
  46. cs_socket_name(skt) , ## arg); \
  47. } while (0)
  48. #else
  49. #define ds_dbg(lvl, fmt, arg...) do { } while (0)
  50. #endif
  51. /** alloc_io_space
  52. *
  53. * Special stuff for managing IO windows, because they are scarce
  54. */
  55. static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
  56. ioaddr_t num, u_int lines)
  57. {
  58. int i;
  59. kio_addr_t try, align;
  60. align = (*base) ? (lines ? 1<<lines : 0) : 1;
  61. if (align && (align < num)) {
  62. if (*base) {
  63. ds_dbg(s, 0, "odd IO request: num %#x align %#lx\n",
  64. num, align);
  65. align = 0;
  66. } else
  67. while (align && (align < num)) align <<= 1;
  68. }
  69. if (*base & ~(align-1)) {
  70. ds_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
  71. *base, align);
  72. align = 0;
  73. }
  74. if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
  75. *base = s->io_offset | (*base & 0x0fff);
  76. return 0;
  77. }
  78. /* Check for an already-allocated window that must conflict with
  79. * what was asked for. It is a hack because it does not catch all
  80. * potential conflicts, just the most obvious ones.
  81. */
  82. for (i = 0; i < MAX_IO_WIN; i++)
  83. if ((s->io[i].res) && *base &&
  84. ((s->io[i].res->start & (align-1)) == *base))
  85. return 1;
  86. for (i = 0; i < MAX_IO_WIN; i++) {
  87. if (!s->io[i].res) {
  88. s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
  89. if (s->io[i].res) {
  90. *base = s->io[i].res->start;
  91. s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
  92. s->io[i].InUse = num;
  93. break;
  94. } else
  95. return 1;
  96. } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
  97. continue;
  98. /* Try to extend top of window */
  99. try = s->io[i].res->end + 1;
  100. if ((*base == 0) || (*base == try))
  101. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
  102. s->io[i].res->end + num, s) == 0) {
  103. *base = try;
  104. s->io[i].InUse += num;
  105. break;
  106. }
  107. /* Try to extend bottom of window */
  108. try = s->io[i].res->start - num;
  109. if ((*base == 0) || (*base == try))
  110. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
  111. s->io[i].res->end, s) == 0) {
  112. *base = try;
  113. s->io[i].InUse += num;
  114. break;
  115. }
  116. }
  117. return (i == MAX_IO_WIN);
  118. } /* alloc_io_space */
  119. static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
  120. ioaddr_t num)
  121. {
  122. int i;
  123. for (i = 0; i < MAX_IO_WIN; i++) {
  124. if (!s->io[i].res)
  125. continue;
  126. if ((s->io[i].res->start <= base) &&
  127. (s->io[i].res->end >= base+num-1)) {
  128. s->io[i].InUse -= num;
  129. /* Free the window if no one else is using it */
  130. if (s->io[i].InUse == 0) {
  131. release_resource(s->io[i].res);
  132. kfree(s->io[i].res);
  133. s->io[i].res = NULL;
  134. }
  135. }
  136. }
  137. } /* release_io_space */
  138. /** pccard_access_configuration_register
  139. *
  140. * Access_configuration_register() reads and writes configuration
  141. * registers in attribute memory. Memory window 0 is reserved for
  142. * this and the tuple reading services.
  143. */
  144. int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
  145. conf_reg_t *reg)
  146. {
  147. struct pcmcia_socket *s;
  148. config_t *c;
  149. int addr;
  150. u_char val;
  151. if (!p_dev || !p_dev->function_config)
  152. return CS_NO_CARD;
  153. s = p_dev->socket;
  154. c = p_dev->function_config;
  155. if (!(c->state & CONFIG_LOCKED))
  156. return CS_CONFIGURATION_LOCKED;
  157. addr = (c->ConfigBase + reg->Offset) >> 1;
  158. switch (reg->Action) {
  159. case CS_READ:
  160. pcmcia_read_cis_mem(s, 1, addr, 1, &val);
  161. reg->Value = val;
  162. break;
  163. case CS_WRITE:
  164. val = reg->Value;
  165. pcmcia_write_cis_mem(s, 1, addr, 1, &val);
  166. break;
  167. default:
  168. return CS_BAD_ARGS;
  169. break;
  170. }
  171. return CS_SUCCESS;
  172. } /* pcmcia_access_configuration_register */
  173. EXPORT_SYMBOL(pcmcia_access_configuration_register);
  174. int pccard_get_configuration_info(struct pcmcia_socket *s,
  175. struct pcmcia_device *p_dev,
  176. config_info_t *config)
  177. {
  178. config_t *c;
  179. if (!(s->state & SOCKET_PRESENT))
  180. return CS_NO_CARD;
  181. #ifdef CONFIG_CARDBUS
  182. if (s->state & SOCKET_CARDBUS) {
  183. memset(config, 0, sizeof(config_info_t));
  184. config->Vcc = s->socket.Vcc;
  185. config->Vpp1 = config->Vpp2 = s->socket.Vpp;
  186. config->Option = s->cb_dev->subordinate->number;
  187. if (s->state & SOCKET_CARDBUS_CONFIG) {
  188. config->Attributes = CONF_VALID_CLIENT;
  189. config->IntType = INT_CARDBUS;
  190. config->AssignedIRQ = s->irq.AssignedIRQ;
  191. if (config->AssignedIRQ)
  192. config->Attributes |= CONF_ENABLE_IRQ;
  193. if (s->io[0].res) {
  194. config->BasePort1 = s->io[0].res->start;
  195. config->NumPorts1 = s->io[0].res->end - config->BasePort1 + 1;
  196. }
  197. }
  198. return CS_SUCCESS;
  199. }
  200. #endif
  201. if (p_dev) {
  202. c = p_dev->function_config;
  203. config->Function = p_dev->func;
  204. } else {
  205. c = NULL;
  206. config->Function = 0;
  207. }
  208. if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
  209. config->Attributes = 0;
  210. config->Vcc = s->socket.Vcc;
  211. config->Vpp1 = config->Vpp2 = s->socket.Vpp;
  212. return CS_SUCCESS;
  213. }
  214. config->Attributes = c->Attributes | CONF_VALID_CLIENT;
  215. config->Vcc = s->socket.Vcc;
  216. config->Vpp1 = config->Vpp2 = s->socket.Vpp;
  217. config->IntType = c->IntType;
  218. config->ConfigBase = c->ConfigBase;
  219. config->Status = c->Status;
  220. config->Pin = c->Pin;
  221. config->Copy = c->Copy;
  222. config->Option = c->Option;
  223. config->ExtStatus = c->ExtStatus;
  224. config->Present = config->CardValues = c->CardValues;
  225. config->IRQAttributes = c->irq.Attributes;
  226. config->AssignedIRQ = s->irq.AssignedIRQ;
  227. config->BasePort1 = c->io.BasePort1;
  228. config->NumPorts1 = c->io.NumPorts1;
  229. config->Attributes1 = c->io.Attributes1;
  230. config->BasePort2 = c->io.BasePort2;
  231. config->NumPorts2 = c->io.NumPorts2;
  232. config->Attributes2 = c->io.Attributes2;
  233. config->IOAddrLines = c->io.IOAddrLines;
  234. return CS_SUCCESS;
  235. } /* pccard_get_configuration_info */
  236. int pcmcia_get_configuration_info(struct pcmcia_device *p_dev,
  237. config_info_t *config)
  238. {
  239. return pccard_get_configuration_info(p_dev->socket, p_dev,
  240. config);
  241. }
  242. EXPORT_SYMBOL(pcmcia_get_configuration_info);
  243. /** pcmcia_get_window
  244. */
  245. int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
  246. int idx, win_req_t *req)
  247. {
  248. window_t *win;
  249. int w;
  250. if (!s || !(s->state & SOCKET_PRESENT))
  251. return CS_NO_CARD;
  252. for (w = idx; w < MAX_WIN; w++)
  253. if (s->state & SOCKET_WIN_REQ(w))
  254. break;
  255. if (w == MAX_WIN)
  256. return CS_NO_MORE_ITEMS;
  257. win = &s->win[w];
  258. req->Base = win->ctl.res->start;
  259. req->Size = win->ctl.res->end - win->ctl.res->start + 1;
  260. req->AccessSpeed = win->ctl.speed;
  261. req->Attributes = 0;
  262. if (win->ctl.flags & MAP_ATTRIB)
  263. req->Attributes |= WIN_MEMORY_TYPE_AM;
  264. if (win->ctl.flags & MAP_ACTIVE)
  265. req->Attributes |= WIN_ENABLE;
  266. if (win->ctl.flags & MAP_16BIT)
  267. req->Attributes |= WIN_DATA_WIDTH_16;
  268. if (win->ctl.flags & MAP_USE_WAIT)
  269. req->Attributes |= WIN_USE_WAIT;
  270. *handle = win;
  271. return CS_SUCCESS;
  272. } /* pcmcia_get_window */
  273. EXPORT_SYMBOL(pcmcia_get_window);
  274. /** pccard_get_status
  275. *
  276. * Get the current socket state bits. We don't support the latched
  277. * SocketState yet: I haven't seen any point for it.
  278. */
  279. int pccard_get_status(struct pcmcia_socket *s, struct pcmcia_device *p_dev,
  280. cs_status_t *status)
  281. {
  282. config_t *c;
  283. int val;
  284. s->ops->get_status(s, &val);
  285. status->CardState = status->SocketState = 0;
  286. status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
  287. status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
  288. status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
  289. status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
  290. if (s->state & SOCKET_SUSPEND)
  291. status->CardState |= CS_EVENT_PM_SUSPEND;
  292. if (!(s->state & SOCKET_PRESENT))
  293. return CS_NO_CARD;
  294. c = (p_dev) ? p_dev->function_config : NULL;
  295. if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
  296. (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
  297. u_char reg;
  298. if (c->CardValues & PRESENT_PIN_REPLACE) {
  299. pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
  300. status->CardState |=
  301. (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
  302. status->CardState |=
  303. (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
  304. status->CardState |=
  305. (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
  306. status->CardState |=
  307. (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
  308. } else {
  309. /* No PRR? Then assume we're always ready */
  310. status->CardState |= CS_EVENT_READY_CHANGE;
  311. }
  312. if (c->CardValues & PRESENT_EXT_STATUS) {
  313. pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
  314. status->CardState |=
  315. (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
  316. }
  317. return CS_SUCCESS;
  318. }
  319. status->CardState |=
  320. (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
  321. status->CardState |=
  322. (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
  323. status->CardState |=
  324. (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
  325. status->CardState |=
  326. (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
  327. return CS_SUCCESS;
  328. } /* pccard_get_status */
  329. int pcmcia_get_status(struct pcmcia_device *p_dev, cs_status_t *status)
  330. {
  331. return pccard_get_status(p_dev->socket, p_dev, status);
  332. }
  333. EXPORT_SYMBOL(pcmcia_get_status);
  334. /** pcmcia_get_mem_page
  335. *
  336. * Change the card address of an already open memory window.
  337. */
  338. int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
  339. {
  340. if ((win == NULL) || (win->magic != WINDOW_MAGIC))
  341. return CS_BAD_HANDLE;
  342. req->Page = 0;
  343. req->CardOffset = win->ctl.card_start;
  344. return CS_SUCCESS;
  345. } /* pcmcia_get_mem_page */
  346. EXPORT_SYMBOL(pcmcia_get_mem_page);
  347. int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
  348. {
  349. struct pcmcia_socket *s;
  350. if ((win == NULL) || (win->magic != WINDOW_MAGIC))
  351. return CS_BAD_HANDLE;
  352. if (req->Page != 0)
  353. return CS_BAD_PAGE;
  354. s = win->sock;
  355. win->ctl.card_start = req->CardOffset;
  356. if (s->ops->set_mem_map(s, &win->ctl) != 0)
  357. return CS_BAD_OFFSET;
  358. return CS_SUCCESS;
  359. } /* pcmcia_map_mem_page */
  360. EXPORT_SYMBOL(pcmcia_map_mem_page);
  361. /** pcmcia_modify_configuration
  362. *
  363. * Modify a locked socket configuration
  364. */
  365. int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
  366. modconf_t *mod)
  367. {
  368. struct pcmcia_socket *s;
  369. config_t *c;
  370. s = p_dev->socket;
  371. c = p_dev->function_config;
  372. if (!(s->state & SOCKET_PRESENT))
  373. return CS_NO_CARD;
  374. if (!(c->state & CONFIG_LOCKED))
  375. return CS_CONFIGURATION_LOCKED;
  376. if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
  377. if (mod->Attributes & CONF_ENABLE_IRQ) {
  378. c->Attributes |= CONF_ENABLE_IRQ;
  379. s->socket.io_irq = s->irq.AssignedIRQ;
  380. } else {
  381. c->Attributes &= ~CONF_ENABLE_IRQ;
  382. s->socket.io_irq = 0;
  383. }
  384. s->ops->set_socket(s, &s->socket);
  385. }
  386. if (mod->Attributes & CONF_VCC_CHANGE_VALID)
  387. return CS_BAD_VCC;
  388. /* We only allow changing Vpp1 and Vpp2 to the same value */
  389. if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
  390. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  391. if (mod->Vpp1 != mod->Vpp2)
  392. return CS_BAD_VPP;
  393. s->socket.Vpp = mod->Vpp1;
  394. if (s->ops->set_socket(s, &s->socket))
  395. return CS_BAD_VPP;
  396. } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
  397. (mod->Attributes & CONF_VPP2_CHANGE_VALID))
  398. return CS_BAD_VPP;
  399. if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
  400. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  401. pccard_io_map io_on;
  402. int i;
  403. io_on.speed = io_speed;
  404. for (i = 0; i < MAX_IO_WIN; i++) {
  405. if (!s->io[i].res)
  406. continue;
  407. io_off.map = i;
  408. io_on.map = i;
  409. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  410. io_on.start = s->io[i].res->start;
  411. io_on.stop = s->io[i].res->end;
  412. s->ops->set_io_map(s, &io_off);
  413. mdelay(40);
  414. s->ops->set_io_map(s, &io_on);
  415. }
  416. }
  417. return CS_SUCCESS;
  418. } /* modify_configuration */
  419. EXPORT_SYMBOL(pcmcia_modify_configuration);
  420. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  421. {
  422. pccard_io_map io = { 0, 0, 0, 0, 1 };
  423. struct pcmcia_socket *s = p_dev->socket;
  424. config_t *c = p_dev->function_config;
  425. int i;
  426. if (p_dev->_locked) {
  427. p_dev->_locked = 0;
  428. if (--(s->lock_count) == 0) {
  429. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  430. s->socket.Vpp = 0;
  431. s->socket.io_irq = 0;
  432. s->ops->set_socket(s, &s->socket);
  433. }
  434. }
  435. if (c->state & CONFIG_LOCKED) {
  436. c->state &= ~CONFIG_LOCKED;
  437. if (c->state & CONFIG_IO_REQ)
  438. for (i = 0; i < MAX_IO_WIN; i++) {
  439. if (!s->io[i].res)
  440. continue;
  441. s->io[i].Config--;
  442. if (s->io[i].Config != 0)
  443. continue;
  444. io.map = i;
  445. s->ops->set_io_map(s, &io);
  446. }
  447. }
  448. return CS_SUCCESS;
  449. } /* pcmcia_release_configuration */
  450. /** pcmcia_release_io
  451. *
  452. * Release_io() releases the I/O ranges allocated by a client. This
  453. * may be invoked some time after a card ejection has already dumped
  454. * the actual socket configuration, so if the client is "stale", we
  455. * don't bother checking the port ranges against the current socket
  456. * values.
  457. */
  458. static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
  459. {
  460. struct pcmcia_socket *s = p_dev->socket;
  461. config_t *c = p_dev->function_config;
  462. if (!p_dev->_io )
  463. return CS_BAD_HANDLE;
  464. p_dev->_io = 0;
  465. if ((c->io.BasePort1 != req->BasePort1) ||
  466. (c->io.NumPorts1 != req->NumPorts1) ||
  467. (c->io.BasePort2 != req->BasePort2) ||
  468. (c->io.NumPorts2 != req->NumPorts2))
  469. return CS_BAD_ARGS;
  470. c->state &= ~CONFIG_IO_REQ;
  471. release_io_space(s, req->BasePort1, req->NumPorts1);
  472. if (req->NumPorts2)
  473. release_io_space(s, req->BasePort2, req->NumPorts2);
  474. return CS_SUCCESS;
  475. } /* pcmcia_release_io */
  476. static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  477. {
  478. struct pcmcia_socket *s = p_dev->socket;
  479. config_t *c= p_dev->function_config;
  480. if (!p_dev->_irq)
  481. return CS_BAD_HANDLE;
  482. p_dev->_irq = 0;
  483. if (c->state & CONFIG_LOCKED)
  484. return CS_CONFIGURATION_LOCKED;
  485. if (c->irq.Attributes != req->Attributes)
  486. return CS_BAD_ATTRIBUTE;
  487. if (s->irq.AssignedIRQ != req->AssignedIRQ)
  488. return CS_BAD_IRQ;
  489. if (--s->irq.Config == 0) {
  490. c->state &= ~CONFIG_IRQ_REQ;
  491. s->irq.AssignedIRQ = 0;
  492. }
  493. if (req->Attributes & IRQ_HANDLE_PRESENT) {
  494. free_irq(req->AssignedIRQ, req->Instance);
  495. }
  496. #ifdef CONFIG_PCMCIA_PROBE
  497. pcmcia_used_irq[req->AssignedIRQ]--;
  498. #endif
  499. return CS_SUCCESS;
  500. } /* pcmcia_release_irq */
  501. int pcmcia_release_window(window_handle_t win)
  502. {
  503. struct pcmcia_socket *s;
  504. if ((win == NULL) || (win->magic != WINDOW_MAGIC))
  505. return CS_BAD_HANDLE;
  506. s = win->sock;
  507. if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
  508. return CS_BAD_HANDLE;
  509. /* Shut down memory window */
  510. win->ctl.flags &= ~MAP_ACTIVE;
  511. s->ops->set_mem_map(s, &win->ctl);
  512. s->state &= ~SOCKET_WIN_REQ(win->index);
  513. /* Release system memory */
  514. if (win->ctl.res) {
  515. release_resource(win->ctl.res);
  516. kfree(win->ctl.res);
  517. win->ctl.res = NULL;
  518. }
  519. win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
  520. win->magic = 0;
  521. return CS_SUCCESS;
  522. } /* pcmcia_release_window */
  523. EXPORT_SYMBOL(pcmcia_release_window);
  524. int pcmcia_request_configuration(struct pcmcia_device *p_dev,
  525. config_req_t *req)
  526. {
  527. int i;
  528. u_int base;
  529. struct pcmcia_socket *s = p_dev->socket;
  530. config_t *c;
  531. pccard_io_map iomap;
  532. if (!(s->state & SOCKET_PRESENT))
  533. return CS_NO_CARD;
  534. if (req->IntType & INT_CARDBUS)
  535. return CS_UNSUPPORTED_MODE;
  536. c = p_dev->function_config;
  537. if (c->state & CONFIG_LOCKED)
  538. return CS_CONFIGURATION_LOCKED;
  539. /* Do power control. We don't allow changes in Vcc. */
  540. s->socket.Vpp = req->Vpp;
  541. if (s->ops->set_socket(s, &s->socket))
  542. return CS_BAD_VPP;
  543. /* Pick memory or I/O card, DMA mode, interrupt */
  544. c->IntType = req->IntType;
  545. c->Attributes = req->Attributes;
  546. if (req->IntType & INT_MEMORY_AND_IO)
  547. s->socket.flags |= SS_IOCARD;
  548. if (req->IntType & INT_ZOOMED_VIDEO)
  549. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  550. if (req->Attributes & CONF_ENABLE_DMA)
  551. s->socket.flags |= SS_DMA_MODE;
  552. if (req->Attributes & CONF_ENABLE_SPKR)
  553. s->socket.flags |= SS_SPKR_ENA;
  554. if (req->Attributes & CONF_ENABLE_IRQ)
  555. s->socket.io_irq = s->irq.AssignedIRQ;
  556. else
  557. s->socket.io_irq = 0;
  558. s->ops->set_socket(s, &s->socket);
  559. s->lock_count++;
  560. /* Set up CIS configuration registers */
  561. base = c->ConfigBase = req->ConfigBase;
  562. c->CardValues = req->Present;
  563. if (req->Present & PRESENT_COPY) {
  564. c->Copy = req->Copy;
  565. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
  566. }
  567. if (req->Present & PRESENT_OPTION) {
  568. if (s->functions == 1) {
  569. c->Option = req->ConfigIndex & COR_CONFIG_MASK;
  570. } else {
  571. c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
  572. c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
  573. if (req->Present & PRESENT_IOBASE_0)
  574. c->Option |= COR_ADDR_DECODE;
  575. }
  576. if (c->state & CONFIG_IRQ_REQ)
  577. if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
  578. c->Option |= COR_LEVEL_REQ;
  579. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
  580. mdelay(40);
  581. }
  582. if (req->Present & PRESENT_STATUS) {
  583. c->Status = req->Status;
  584. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
  585. }
  586. if (req->Present & PRESENT_PIN_REPLACE) {
  587. c->Pin = req->Pin;
  588. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
  589. }
  590. if (req->Present & PRESENT_EXT_STATUS) {
  591. c->ExtStatus = req->ExtStatus;
  592. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
  593. }
  594. if (req->Present & PRESENT_IOBASE_0) {
  595. u_char b = c->io.BasePort1 & 0xff;
  596. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  597. b = (c->io.BasePort1 >> 8) & 0xff;
  598. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  599. }
  600. if (req->Present & PRESENT_IOSIZE) {
  601. u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
  602. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  603. }
  604. /* Configure I/O windows */
  605. if (c->state & CONFIG_IO_REQ) {
  606. iomap.speed = io_speed;
  607. for (i = 0; i < MAX_IO_WIN; i++)
  608. if (s->io[i].res) {
  609. iomap.map = i;
  610. iomap.flags = MAP_ACTIVE;
  611. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  612. case IO_DATA_PATH_WIDTH_16:
  613. iomap.flags |= MAP_16BIT; break;
  614. case IO_DATA_PATH_WIDTH_AUTO:
  615. iomap.flags |= MAP_AUTOSZ; break;
  616. default:
  617. break;
  618. }
  619. iomap.start = s->io[i].res->start;
  620. iomap.stop = s->io[i].res->end;
  621. s->ops->set_io_map(s, &iomap);
  622. s->io[i].Config++;
  623. }
  624. }
  625. c->state |= CONFIG_LOCKED;
  626. p_dev->_locked = 1;
  627. return CS_SUCCESS;
  628. } /* pcmcia_request_configuration */
  629. EXPORT_SYMBOL(pcmcia_request_configuration);
  630. /** pcmcia_request_io
  631. *
  632. * Request_io() reserves ranges of port addresses for a socket.
  633. * I have not implemented range sharing or alias addressing.
  634. */
  635. int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
  636. {
  637. struct pcmcia_socket *s = p_dev->socket;
  638. config_t *c;
  639. if (!(s->state & SOCKET_PRESENT))
  640. return CS_NO_CARD;
  641. if (!req)
  642. return CS_UNSUPPORTED_MODE;
  643. c = p_dev->function_config;
  644. if (c->state & CONFIG_LOCKED)
  645. return CS_CONFIGURATION_LOCKED;
  646. if (c->state & CONFIG_IO_REQ)
  647. return CS_IN_USE;
  648. if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
  649. return CS_BAD_ATTRIBUTE;
  650. if ((req->NumPorts2 > 0) &&
  651. (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
  652. return CS_BAD_ATTRIBUTE;
  653. if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
  654. req->NumPorts1, req->IOAddrLines))
  655. return CS_IN_USE;
  656. if (req->NumPorts2) {
  657. if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
  658. req->NumPorts2, req->IOAddrLines)) {
  659. release_io_space(s, req->BasePort1, req->NumPorts1);
  660. return CS_IN_USE;
  661. }
  662. }
  663. c->io = *req;
  664. c->state |= CONFIG_IO_REQ;
  665. p_dev->_io = 1;
  666. return CS_SUCCESS;
  667. } /* pcmcia_request_io */
  668. EXPORT_SYMBOL(pcmcia_request_io);
  669. /** pcmcia_request_irq
  670. *
  671. * Request_irq() reserves an irq for this client.
  672. *
  673. * Also, since Linux only reserves irq's when they are actually
  674. * hooked, we don't guarantee that an irq will still be available
  675. * when the configuration is locked. Now that I think about it,
  676. * there might be a way to fix this using a dummy handler.
  677. */
  678. #ifdef CONFIG_PCMCIA_PROBE
  679. static irqreturn_t test_action(int cpl, void *dev_id)
  680. {
  681. return IRQ_NONE;
  682. }
  683. #endif
  684. int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  685. {
  686. struct pcmcia_socket *s = p_dev->socket;
  687. config_t *c;
  688. int ret = CS_IN_USE, irq = 0;
  689. int type;
  690. if (!(s->state & SOCKET_PRESENT))
  691. return CS_NO_CARD;
  692. c = p_dev->function_config;
  693. if (c->state & CONFIG_LOCKED)
  694. return CS_CONFIGURATION_LOCKED;
  695. if (c->state & CONFIG_IRQ_REQ)
  696. return CS_IN_USE;
  697. /* Decide what type of interrupt we are registering */
  698. type = 0;
  699. if (s->functions > 1) /* All of this ought to be handled higher up */
  700. type = IRQF_SHARED;
  701. if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
  702. type = IRQF_SHARED;
  703. #ifdef CONFIG_PCMCIA_PROBE
  704. if (s->irq.AssignedIRQ != 0) {
  705. /* If the interrupt is already assigned, it must be the same */
  706. irq = s->irq.AssignedIRQ;
  707. } else {
  708. int try;
  709. u32 mask = s->irq_mask;
  710. void *data = &p_dev->dev.driver; /* something unique to this device */
  711. for (try = 0; try < 64; try++) {
  712. irq = try % 32;
  713. /* marked as available by driver, and not blocked by userspace? */
  714. if (!((mask >> irq) & 1))
  715. continue;
  716. /* avoid an IRQ which is already used by a PCMCIA card */
  717. if ((try < 32) && pcmcia_used_irq[irq])
  718. continue;
  719. /* register the correct driver, if possible, of check whether
  720. * registering a dummy handle works, i.e. if the IRQ isn't
  721. * marked as used by the kernel resource management core */
  722. ret = request_irq(irq,
  723. (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
  724. type,
  725. p_dev->devname,
  726. (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
  727. if (!ret) {
  728. if (!(req->Attributes & IRQ_HANDLE_PRESENT))
  729. free_irq(irq, data);
  730. break;
  731. }
  732. }
  733. }
  734. #endif
  735. /* only assign PCI irq if no IRQ already assigned */
  736. if (ret && !s->irq.AssignedIRQ) {
  737. if (!s->pci_irq)
  738. return ret;
  739. type = IRQF_SHARED;
  740. irq = s->pci_irq;
  741. }
  742. if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
  743. if (request_irq(irq, req->Handler, type, p_dev->devname, req->Instance))
  744. return CS_IN_USE;
  745. }
  746. /* Make sure the fact the request type was overridden is passed back */
  747. if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
  748. req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
  749. printk(KERN_WARNING "pcmcia: request for exclusive IRQ could not be fulfilled.\n");
  750. printk(KERN_WARNING "pcmcia: the driver needs updating to supported shared IRQ lines.\n");
  751. }
  752. c->irq.Attributes = req->Attributes;
  753. s->irq.AssignedIRQ = req->AssignedIRQ = irq;
  754. s->irq.Config++;
  755. c->state |= CONFIG_IRQ_REQ;
  756. p_dev->_irq = 1;
  757. #ifdef CONFIG_PCMCIA_PROBE
  758. pcmcia_used_irq[irq]++;
  759. #endif
  760. return CS_SUCCESS;
  761. } /* pcmcia_request_irq */
  762. EXPORT_SYMBOL(pcmcia_request_irq);
  763. /** pcmcia_request_window
  764. *
  765. * Request_window() establishes a mapping between card memory space
  766. * and system memory space.
  767. */
  768. int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
  769. {
  770. struct pcmcia_socket *s = (*p_dev)->socket;
  771. window_t *win;
  772. u_long align;
  773. int w;
  774. if (!(s->state & SOCKET_PRESENT))
  775. return CS_NO_CARD;
  776. if (req->Attributes & (WIN_PAGED | WIN_SHARED))
  777. return CS_BAD_ATTRIBUTE;
  778. /* Window size defaults to smallest available */
  779. if (req->Size == 0)
  780. req->Size = s->map_size;
  781. align = (((s->features & SS_CAP_MEM_ALIGN) ||
  782. (req->Attributes & WIN_STRICT_ALIGN)) ?
  783. req->Size : s->map_size);
  784. if (req->Size & (s->map_size-1))
  785. return CS_BAD_SIZE;
  786. if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
  787. (req->Base & (align-1)))
  788. return CS_BAD_BASE;
  789. if (req->Base)
  790. align = 0;
  791. /* Allocate system memory window */
  792. for (w = 0; w < MAX_WIN; w++)
  793. if (!(s->state & SOCKET_WIN_REQ(w))) break;
  794. if (w == MAX_WIN)
  795. return CS_OUT_OF_RESOURCE;
  796. win = &s->win[w];
  797. win->magic = WINDOW_MAGIC;
  798. win->index = w;
  799. win->handle = *p_dev;
  800. win->sock = s;
  801. if (!(s->features & SS_CAP_STATIC_MAP)) {
  802. win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
  803. (req->Attributes & WIN_MAP_BELOW_1MB), s);
  804. if (!win->ctl.res)
  805. return CS_IN_USE;
  806. }
  807. (*p_dev)->_win |= CLIENT_WIN_REQ(w);
  808. /* Configure the socket controller */
  809. win->ctl.map = w+1;
  810. win->ctl.flags = 0;
  811. win->ctl.speed = req->AccessSpeed;
  812. if (req->Attributes & WIN_MEMORY_TYPE)
  813. win->ctl.flags |= MAP_ATTRIB;
  814. if (req->Attributes & WIN_ENABLE)
  815. win->ctl.flags |= MAP_ACTIVE;
  816. if (req->Attributes & WIN_DATA_WIDTH_16)
  817. win->ctl.flags |= MAP_16BIT;
  818. if (req->Attributes & WIN_USE_WAIT)
  819. win->ctl.flags |= MAP_USE_WAIT;
  820. win->ctl.card_start = 0;
  821. if (s->ops->set_mem_map(s, &win->ctl) != 0)
  822. return CS_BAD_ARGS;
  823. s->state |= SOCKET_WIN_REQ(w);
  824. /* Return window handle */
  825. if (s->features & SS_CAP_STATIC_MAP) {
  826. req->Base = win->ctl.static_start;
  827. } else {
  828. req->Base = win->ctl.res->start;
  829. }
  830. *wh = win;
  831. return CS_SUCCESS;
  832. } /* pcmcia_request_window */
  833. EXPORT_SYMBOL(pcmcia_request_window);
  834. void pcmcia_disable_device(struct pcmcia_device *p_dev) {
  835. pcmcia_release_configuration(p_dev);
  836. pcmcia_release_io(p_dev, &p_dev->io);
  837. pcmcia_release_irq(p_dev, &p_dev->irq);
  838. if (&p_dev->win)
  839. pcmcia_release_window(p_dev->win);
  840. }
  841. EXPORT_SYMBOL(pcmcia_disable_device);