pcmcia_resource.c 28 KB

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