pcmcia_resource.c 25 KB

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