pcmcia_resource.c 25 KB

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