pcmcia_resource.c 26 KB

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