pcmcia_resource.c 24 KB

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