pcmcia_resource.c 26 KB

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