pcmcia_resource.c 26 KB

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