pcmcia_resource.c 26 KB

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