pcmcia_resource.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. /** pcmcia_get_mem_page
  272. *
  273. * Change the card address of an already open memory window.
  274. */
  275. int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
  276. {
  277. if ((win == NULL) || (win->magic != WINDOW_MAGIC))
  278. return CS_BAD_HANDLE;
  279. req->Page = 0;
  280. req->CardOffset = win->ctl.card_start;
  281. return CS_SUCCESS;
  282. } /* pcmcia_get_mem_page */
  283. EXPORT_SYMBOL(pcmcia_get_mem_page);
  284. int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
  285. {
  286. struct pcmcia_socket *s;
  287. if ((win == NULL) || (win->magic != WINDOW_MAGIC))
  288. return CS_BAD_HANDLE;
  289. if (req->Page != 0)
  290. return CS_BAD_PAGE;
  291. s = win->sock;
  292. win->ctl.card_start = req->CardOffset;
  293. if (s->ops->set_mem_map(s, &win->ctl) != 0)
  294. return CS_BAD_OFFSET;
  295. return CS_SUCCESS;
  296. } /* pcmcia_map_mem_page */
  297. EXPORT_SYMBOL(pcmcia_map_mem_page);
  298. /** pcmcia_modify_configuration
  299. *
  300. * Modify a locked socket configuration
  301. */
  302. int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
  303. modconf_t *mod)
  304. {
  305. struct pcmcia_socket *s;
  306. config_t *c;
  307. s = p_dev->socket;
  308. c = p_dev->function_config;
  309. if (!(s->state & SOCKET_PRESENT))
  310. return CS_NO_CARD;
  311. if (!(c->state & CONFIG_LOCKED))
  312. return CS_CONFIGURATION_LOCKED;
  313. if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
  314. if (mod->Attributes & CONF_ENABLE_IRQ) {
  315. c->Attributes |= CONF_ENABLE_IRQ;
  316. s->socket.io_irq = s->irq.AssignedIRQ;
  317. } else {
  318. c->Attributes &= ~CONF_ENABLE_IRQ;
  319. s->socket.io_irq = 0;
  320. }
  321. s->ops->set_socket(s, &s->socket);
  322. }
  323. if (mod->Attributes & CONF_VCC_CHANGE_VALID)
  324. return CS_BAD_VCC;
  325. /* We only allow changing Vpp1 and Vpp2 to the same value */
  326. if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
  327. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  328. if (mod->Vpp1 != mod->Vpp2)
  329. return CS_BAD_VPP;
  330. s->socket.Vpp = mod->Vpp1;
  331. if (s->ops->set_socket(s, &s->socket))
  332. return CS_BAD_VPP;
  333. } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
  334. (mod->Attributes & CONF_VPP2_CHANGE_VALID))
  335. return CS_BAD_VPP;
  336. if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
  337. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  338. pccard_io_map io_on;
  339. int i;
  340. io_on.speed = io_speed;
  341. for (i = 0; i < MAX_IO_WIN; i++) {
  342. if (!s->io[i].res)
  343. continue;
  344. io_off.map = i;
  345. io_on.map = i;
  346. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  347. io_on.start = s->io[i].res->start;
  348. io_on.stop = s->io[i].res->end;
  349. s->ops->set_io_map(s, &io_off);
  350. mdelay(40);
  351. s->ops->set_io_map(s, &io_on);
  352. }
  353. }
  354. return CS_SUCCESS;
  355. } /* modify_configuration */
  356. EXPORT_SYMBOL(pcmcia_modify_configuration);
  357. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  358. {
  359. pccard_io_map io = { 0, 0, 0, 0, 1 };
  360. struct pcmcia_socket *s = p_dev->socket;
  361. config_t *c = p_dev->function_config;
  362. int i;
  363. if (p_dev->_locked) {
  364. p_dev->_locked = 0;
  365. if (--(s->lock_count) == 0) {
  366. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  367. s->socket.Vpp = 0;
  368. s->socket.io_irq = 0;
  369. s->ops->set_socket(s, &s->socket);
  370. }
  371. }
  372. if (c->state & CONFIG_LOCKED) {
  373. c->state &= ~CONFIG_LOCKED;
  374. if (c->state & CONFIG_IO_REQ)
  375. for (i = 0; i < MAX_IO_WIN; i++) {
  376. if (!s->io[i].res)
  377. continue;
  378. s->io[i].Config--;
  379. if (s->io[i].Config != 0)
  380. continue;
  381. io.map = i;
  382. s->ops->set_io_map(s, &io);
  383. }
  384. }
  385. return CS_SUCCESS;
  386. } /* pcmcia_release_configuration */
  387. /** pcmcia_release_io
  388. *
  389. * Release_io() releases the I/O ranges allocated by a client. This
  390. * may be invoked some time after a card ejection has already dumped
  391. * the actual socket configuration, so if the client is "stale", we
  392. * don't bother checking the port ranges against the current socket
  393. * values.
  394. */
  395. static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
  396. {
  397. struct pcmcia_socket *s = p_dev->socket;
  398. config_t *c = p_dev->function_config;
  399. if (!p_dev->_io )
  400. return CS_BAD_HANDLE;
  401. p_dev->_io = 0;
  402. if ((c->io.BasePort1 != req->BasePort1) ||
  403. (c->io.NumPorts1 != req->NumPorts1) ||
  404. (c->io.BasePort2 != req->BasePort2) ||
  405. (c->io.NumPorts2 != req->NumPorts2))
  406. return CS_BAD_ARGS;
  407. c->state &= ~CONFIG_IO_REQ;
  408. release_io_space(s, req->BasePort1, req->NumPorts1);
  409. if (req->NumPorts2)
  410. release_io_space(s, req->BasePort2, req->NumPorts2);
  411. return CS_SUCCESS;
  412. } /* pcmcia_release_io */
  413. static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  414. {
  415. struct pcmcia_socket *s = p_dev->socket;
  416. config_t *c= p_dev->function_config;
  417. if (!p_dev->_irq)
  418. return CS_BAD_HANDLE;
  419. p_dev->_irq = 0;
  420. if (c->state & CONFIG_LOCKED)
  421. return CS_CONFIGURATION_LOCKED;
  422. if (c->irq.Attributes != req->Attributes)
  423. return CS_BAD_ATTRIBUTE;
  424. if (s->irq.AssignedIRQ != req->AssignedIRQ)
  425. return CS_BAD_IRQ;
  426. if (--s->irq.Config == 0) {
  427. c->state &= ~CONFIG_IRQ_REQ;
  428. s->irq.AssignedIRQ = 0;
  429. }
  430. if (req->Attributes & IRQ_HANDLE_PRESENT) {
  431. free_irq(req->AssignedIRQ, req->Instance);
  432. }
  433. #ifdef CONFIG_PCMCIA_PROBE
  434. pcmcia_used_irq[req->AssignedIRQ]--;
  435. #endif
  436. return CS_SUCCESS;
  437. } /* pcmcia_release_irq */
  438. int pcmcia_release_window(window_handle_t win)
  439. {
  440. struct pcmcia_socket *s;
  441. if ((win == NULL) || (win->magic != WINDOW_MAGIC))
  442. return CS_BAD_HANDLE;
  443. s = win->sock;
  444. if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
  445. return CS_BAD_HANDLE;
  446. /* Shut down memory window */
  447. win->ctl.flags &= ~MAP_ACTIVE;
  448. s->ops->set_mem_map(s, &win->ctl);
  449. s->state &= ~SOCKET_WIN_REQ(win->index);
  450. /* Release system memory */
  451. if (win->ctl.res) {
  452. release_resource(win->ctl.res);
  453. kfree(win->ctl.res);
  454. win->ctl.res = NULL;
  455. }
  456. win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
  457. win->magic = 0;
  458. return CS_SUCCESS;
  459. } /* pcmcia_release_window */
  460. EXPORT_SYMBOL(pcmcia_release_window);
  461. int pcmcia_request_configuration(struct pcmcia_device *p_dev,
  462. config_req_t *req)
  463. {
  464. int i;
  465. u_int base;
  466. struct pcmcia_socket *s = p_dev->socket;
  467. config_t *c;
  468. pccard_io_map iomap;
  469. if (!(s->state & SOCKET_PRESENT))
  470. return CS_NO_CARD;
  471. if (req->IntType & INT_CARDBUS)
  472. return CS_UNSUPPORTED_MODE;
  473. c = p_dev->function_config;
  474. if (c->state & CONFIG_LOCKED)
  475. return CS_CONFIGURATION_LOCKED;
  476. /* Do power control. We don't allow changes in Vcc. */
  477. s->socket.Vpp = req->Vpp;
  478. if (s->ops->set_socket(s, &s->socket))
  479. return CS_BAD_VPP;
  480. /* Pick memory or I/O card, DMA mode, interrupt */
  481. c->IntType = req->IntType;
  482. c->Attributes = req->Attributes;
  483. if (req->IntType & INT_MEMORY_AND_IO)
  484. s->socket.flags |= SS_IOCARD;
  485. if (req->IntType & INT_ZOOMED_VIDEO)
  486. s->socket.flags |= SS_ZVCARD | SS_IOCARD;
  487. if (req->Attributes & CONF_ENABLE_DMA)
  488. s->socket.flags |= SS_DMA_MODE;
  489. if (req->Attributes & CONF_ENABLE_SPKR)
  490. s->socket.flags |= SS_SPKR_ENA;
  491. if (req->Attributes & CONF_ENABLE_IRQ)
  492. s->socket.io_irq = s->irq.AssignedIRQ;
  493. else
  494. s->socket.io_irq = 0;
  495. s->ops->set_socket(s, &s->socket);
  496. s->lock_count++;
  497. /* Set up CIS configuration registers */
  498. base = c->ConfigBase = req->ConfigBase;
  499. c->CardValues = req->Present;
  500. if (req->Present & PRESENT_COPY) {
  501. c->Copy = req->Copy;
  502. pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
  503. }
  504. if (req->Present & PRESENT_OPTION) {
  505. if (s->functions == 1) {
  506. c->Option = req->ConfigIndex & COR_CONFIG_MASK;
  507. } else {
  508. c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
  509. c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
  510. if (req->Present & PRESENT_IOBASE_0)
  511. c->Option |= COR_ADDR_DECODE;
  512. }
  513. if (c->state & CONFIG_IRQ_REQ)
  514. if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
  515. c->Option |= COR_LEVEL_REQ;
  516. pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
  517. mdelay(40);
  518. }
  519. if (req->Present & PRESENT_STATUS) {
  520. c->Status = req->Status;
  521. pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
  522. }
  523. if (req->Present & PRESENT_PIN_REPLACE) {
  524. c->Pin = req->Pin;
  525. pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
  526. }
  527. if (req->Present & PRESENT_EXT_STATUS) {
  528. c->ExtStatus = req->ExtStatus;
  529. pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
  530. }
  531. if (req->Present & PRESENT_IOBASE_0) {
  532. u_char b = c->io.BasePort1 & 0xff;
  533. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
  534. b = (c->io.BasePort1 >> 8) & 0xff;
  535. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
  536. }
  537. if (req->Present & PRESENT_IOSIZE) {
  538. u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
  539. pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
  540. }
  541. /* Configure I/O windows */
  542. if (c->state & CONFIG_IO_REQ) {
  543. iomap.speed = io_speed;
  544. for (i = 0; i < MAX_IO_WIN; i++)
  545. if (s->io[i].res) {
  546. iomap.map = i;
  547. iomap.flags = MAP_ACTIVE;
  548. switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
  549. case IO_DATA_PATH_WIDTH_16:
  550. iomap.flags |= MAP_16BIT; break;
  551. case IO_DATA_PATH_WIDTH_AUTO:
  552. iomap.flags |= MAP_AUTOSZ; break;
  553. default:
  554. break;
  555. }
  556. iomap.start = s->io[i].res->start;
  557. iomap.stop = s->io[i].res->end;
  558. s->ops->set_io_map(s, &iomap);
  559. s->io[i].Config++;
  560. }
  561. }
  562. c->state |= CONFIG_LOCKED;
  563. p_dev->_locked = 1;
  564. return CS_SUCCESS;
  565. } /* pcmcia_request_configuration */
  566. EXPORT_SYMBOL(pcmcia_request_configuration);
  567. /** pcmcia_request_io
  568. *
  569. * Request_io() reserves ranges of port addresses for a socket.
  570. * I have not implemented range sharing or alias addressing.
  571. */
  572. int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
  573. {
  574. struct pcmcia_socket *s = p_dev->socket;
  575. config_t *c;
  576. if (!(s->state & SOCKET_PRESENT))
  577. return CS_NO_CARD;
  578. if (!req)
  579. return CS_UNSUPPORTED_MODE;
  580. c = p_dev->function_config;
  581. if (c->state & CONFIG_LOCKED)
  582. return CS_CONFIGURATION_LOCKED;
  583. if (c->state & CONFIG_IO_REQ)
  584. return CS_IN_USE;
  585. if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
  586. return CS_BAD_ATTRIBUTE;
  587. if ((req->NumPorts2 > 0) &&
  588. (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
  589. return CS_BAD_ATTRIBUTE;
  590. if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
  591. req->NumPorts1, req->IOAddrLines))
  592. return CS_IN_USE;
  593. if (req->NumPorts2) {
  594. if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
  595. req->NumPorts2, req->IOAddrLines)) {
  596. release_io_space(s, req->BasePort1, req->NumPorts1);
  597. return CS_IN_USE;
  598. }
  599. }
  600. c->io = *req;
  601. c->state |= CONFIG_IO_REQ;
  602. p_dev->_io = 1;
  603. return CS_SUCCESS;
  604. } /* pcmcia_request_io */
  605. EXPORT_SYMBOL(pcmcia_request_io);
  606. /** pcmcia_request_irq
  607. *
  608. * Request_irq() reserves an irq for this client.
  609. *
  610. * Also, since Linux only reserves irq's when they are actually
  611. * hooked, we don't guarantee that an irq will still be available
  612. * when the configuration is locked. Now that I think about it,
  613. * there might be a way to fix this using a dummy handler.
  614. */
  615. #ifdef CONFIG_PCMCIA_PROBE
  616. static irqreturn_t test_action(int cpl, void *dev_id)
  617. {
  618. return IRQ_NONE;
  619. }
  620. #endif
  621. int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  622. {
  623. struct pcmcia_socket *s = p_dev->socket;
  624. config_t *c;
  625. int ret = CS_IN_USE, irq = 0;
  626. int type;
  627. if (!(s->state & SOCKET_PRESENT))
  628. return CS_NO_CARD;
  629. c = p_dev->function_config;
  630. if (c->state & CONFIG_LOCKED)
  631. return CS_CONFIGURATION_LOCKED;
  632. if (c->state & CONFIG_IRQ_REQ)
  633. return CS_IN_USE;
  634. /* Decide what type of interrupt we are registering */
  635. type = 0;
  636. if (s->functions > 1) /* All of this ought to be handled higher up */
  637. type = IRQF_SHARED;
  638. if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
  639. type = IRQF_SHARED;
  640. #ifdef CONFIG_PCMCIA_PROBE
  641. #ifdef IRQ_NOAUTOEN
  642. /* if the underlying IRQ infrastructure allows for it, only allocate
  643. * the IRQ, but do not enable it
  644. */
  645. if (!(req->Attributes & IRQ_HANDLE_PRESENT))
  646. type |= IRQ_NOAUTOEN;
  647. #endif /* IRQ_NOAUTOEN */
  648. if (s->irq.AssignedIRQ != 0) {
  649. /* If the interrupt is already assigned, it must be the same */
  650. irq = s->irq.AssignedIRQ;
  651. } else {
  652. int try;
  653. u32 mask = s->irq_mask;
  654. void *data = &p_dev->dev.driver; /* something unique to this device */
  655. for (try = 0; try < 64; try++) {
  656. irq = try % 32;
  657. /* marked as available by driver, and not blocked by userspace? */
  658. if (!((mask >> irq) & 1))
  659. continue;
  660. /* avoid an IRQ which is already used by a PCMCIA card */
  661. if ((try < 32) && pcmcia_used_irq[irq])
  662. continue;
  663. /* register the correct driver, if possible, of check whether
  664. * registering a dummy handle works, i.e. if the IRQ isn't
  665. * marked as used by the kernel resource management core */
  666. ret = request_irq(irq,
  667. (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
  668. type,
  669. p_dev->devname,
  670. (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
  671. if (!ret) {
  672. if (!(req->Attributes & IRQ_HANDLE_PRESENT))
  673. free_irq(irq, data);
  674. break;
  675. }
  676. }
  677. }
  678. #endif
  679. /* only assign PCI irq if no IRQ already assigned */
  680. if (ret && !s->irq.AssignedIRQ) {
  681. if (!s->pci_irq)
  682. return ret;
  683. type = IRQF_SHARED;
  684. irq = s->pci_irq;
  685. }
  686. if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
  687. if (request_irq(irq, req->Handler, type, p_dev->devname, req->Instance))
  688. return CS_IN_USE;
  689. }
  690. /* Make sure the fact the request type was overridden is passed back */
  691. if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
  692. req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
  693. printk(KERN_WARNING "pcmcia: request for exclusive IRQ could not be fulfilled.\n");
  694. printk(KERN_WARNING "pcmcia: the driver needs updating to supported shared IRQ lines.\n");
  695. }
  696. c->irq.Attributes = req->Attributes;
  697. s->irq.AssignedIRQ = req->AssignedIRQ = irq;
  698. s->irq.Config++;
  699. c->state |= CONFIG_IRQ_REQ;
  700. p_dev->_irq = 1;
  701. #ifdef CONFIG_PCMCIA_PROBE
  702. pcmcia_used_irq[irq]++;
  703. #endif
  704. return CS_SUCCESS;
  705. } /* pcmcia_request_irq */
  706. EXPORT_SYMBOL(pcmcia_request_irq);
  707. /** pcmcia_request_window
  708. *
  709. * Request_window() establishes a mapping between card memory space
  710. * and system memory space.
  711. */
  712. int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
  713. {
  714. struct pcmcia_socket *s = (*p_dev)->socket;
  715. window_t *win;
  716. u_long align;
  717. int w;
  718. if (!(s->state & SOCKET_PRESENT))
  719. return CS_NO_CARD;
  720. if (req->Attributes & (WIN_PAGED | WIN_SHARED))
  721. return CS_BAD_ATTRIBUTE;
  722. /* Window size defaults to smallest available */
  723. if (req->Size == 0)
  724. req->Size = s->map_size;
  725. align = (((s->features & SS_CAP_MEM_ALIGN) ||
  726. (req->Attributes & WIN_STRICT_ALIGN)) ?
  727. req->Size : s->map_size);
  728. if (req->Size & (s->map_size-1))
  729. return CS_BAD_SIZE;
  730. if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
  731. (req->Base & (align-1)))
  732. return CS_BAD_BASE;
  733. if (req->Base)
  734. align = 0;
  735. /* Allocate system memory window */
  736. for (w = 0; w < MAX_WIN; w++)
  737. if (!(s->state & SOCKET_WIN_REQ(w))) break;
  738. if (w == MAX_WIN)
  739. return CS_OUT_OF_RESOURCE;
  740. win = &s->win[w];
  741. win->magic = WINDOW_MAGIC;
  742. win->index = w;
  743. win->handle = *p_dev;
  744. win->sock = s;
  745. if (!(s->features & SS_CAP_STATIC_MAP)) {
  746. win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
  747. (req->Attributes & WIN_MAP_BELOW_1MB), s);
  748. if (!win->ctl.res)
  749. return CS_IN_USE;
  750. }
  751. (*p_dev)->_win |= CLIENT_WIN_REQ(w);
  752. /* Configure the socket controller */
  753. win->ctl.map = w+1;
  754. win->ctl.flags = 0;
  755. win->ctl.speed = req->AccessSpeed;
  756. if (req->Attributes & WIN_MEMORY_TYPE)
  757. win->ctl.flags |= MAP_ATTRIB;
  758. if (req->Attributes & WIN_ENABLE)
  759. win->ctl.flags |= MAP_ACTIVE;
  760. if (req->Attributes & WIN_DATA_WIDTH_16)
  761. win->ctl.flags |= MAP_16BIT;
  762. if (req->Attributes & WIN_USE_WAIT)
  763. win->ctl.flags |= MAP_USE_WAIT;
  764. win->ctl.card_start = 0;
  765. if (s->ops->set_mem_map(s, &win->ctl) != 0)
  766. return CS_BAD_ARGS;
  767. s->state |= SOCKET_WIN_REQ(w);
  768. /* Return window handle */
  769. if (s->features & SS_CAP_STATIC_MAP) {
  770. req->Base = win->ctl.static_start;
  771. } else {
  772. req->Base = win->ctl.res->start;
  773. }
  774. *wh = win;
  775. return CS_SUCCESS;
  776. } /* pcmcia_request_window */
  777. EXPORT_SYMBOL(pcmcia_request_window);
  778. void pcmcia_disable_device(struct pcmcia_device *p_dev) {
  779. pcmcia_release_configuration(p_dev);
  780. pcmcia_release_io(p_dev, &p_dev->io);
  781. pcmcia_release_irq(p_dev, &p_dev->irq);
  782. if (p_dev->win)
  783. pcmcia_release_window(p_dev->win);
  784. }
  785. EXPORT_SYMBOL(pcmcia_disable_device);