pcmcia_resource.c 24 KB

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