pcmcia_resource.c 26 KB

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