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