pcmcia_resource.c 25 KB

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