pcmcia_resource.c 25 KB

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