pcmcia_resource.c 25 KB

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