pcmcia_resource.c 26 KB

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