pcmcia_resource.c 30 KB

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