pcmcia_resource.c 25 KB

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