pcmcia_resource.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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;
  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))
  55. align <<= 1;
  56. }
  57. if (*base & ~(align-1)) {
  58. dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
  59. *base, align);
  60. align = 0;
  61. }
  62. if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
  63. *base = s->io_offset | (*base & 0x0fff);
  64. return 0;
  65. }
  66. /* Check for an already-allocated window that must conflict with
  67. * what was asked for. It is a hack because it does not catch all
  68. * potential conflicts, just the most obvious ones.
  69. */
  70. for (i = 0; i < MAX_IO_WIN; i++)
  71. if ((s->io[i].res) && *base &&
  72. ((s->io[i].res->start & (align-1)) == *base))
  73. return 1;
  74. for (i = 0; i < MAX_IO_WIN; i++) {
  75. if (!s->io[i].res) {
  76. s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
  77. if (s->io[i].res) {
  78. *base = s->io[i].res->start;
  79. s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
  80. s->io[i].InUse = num;
  81. break;
  82. } else
  83. return 1;
  84. } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
  85. continue;
  86. /* Try to extend top of window */
  87. try = s->io[i].res->end + 1;
  88. if ((*base == 0) || (*base == try))
  89. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
  90. s->io[i].res->end + num, s) == 0) {
  91. *base = try;
  92. s->io[i].InUse += num;
  93. break;
  94. }
  95. /* Try to extend bottom of window */
  96. try = s->io[i].res->start - num;
  97. if ((*base == 0) || (*base == try))
  98. if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
  99. s->io[i].res->end, s) == 0) {
  100. *base = try;
  101. s->io[i].InUse += num;
  102. break;
  103. }
  104. }
  105. return (i == MAX_IO_WIN);
  106. } /* alloc_io_space */
  107. static void release_io_space(struct pcmcia_socket *s, unsigned int base,
  108. unsigned int num)
  109. {
  110. int i;
  111. for (i = 0; i < MAX_IO_WIN; i++) {
  112. if (!s->io[i].res)
  113. continue;
  114. if ((s->io[i].res->start <= base) &&
  115. (s->io[i].res->end >= base+num-1)) {
  116. s->io[i].InUse -= num;
  117. /* Free the window if no one else is using it */
  118. if (s->io[i].InUse == 0) {
  119. release_resource(s->io[i].res);
  120. kfree(s->io[i].res);
  121. s->io[i].res = NULL;
  122. }
  123. }
  124. }
  125. } /* release_io_space */
  126. /** pccard_access_configuration_register
  127. *
  128. * Access_configuration_register() reads and writes configuration
  129. * registers in attribute memory. Memory window 0 is reserved for
  130. * this and the tuple reading services.
  131. */
  132. int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
  133. conf_reg_t *reg)
  134. {
  135. struct pcmcia_socket *s;
  136. config_t *c;
  137. int addr;
  138. u_char val;
  139. if (!p_dev || !p_dev->function_config)
  140. return -EINVAL;
  141. s = p_dev->socket;
  142. c = p_dev->function_config;
  143. if (!(c->state & CONFIG_LOCKED)) {
  144. dev_dbg(&s->dev, "Configuration isnt't locked\n");
  145. return -EACCES;
  146. }
  147. addr = (c->ConfigBase + reg->Offset) >> 1;
  148. switch (reg->Action) {
  149. case CS_READ:
  150. pcmcia_read_cis_mem(s, 1, addr, 1, &val);
  151. reg->Value = val;
  152. break;
  153. case CS_WRITE:
  154. val = reg->Value;
  155. pcmcia_write_cis_mem(s, 1, addr, 1, &val);
  156. break;
  157. default:
  158. dev_dbg(&s->dev, "Invalid conf register request\n");
  159. return -EINVAL;
  160. break;
  161. }
  162. return 0;
  163. } /* pcmcia_access_configuration_register */
  164. EXPORT_SYMBOL(pcmcia_access_configuration_register);
  165. int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
  166. memreq_t *req)
  167. {
  168. struct pcmcia_socket *s = p_dev->socket;
  169. wh--;
  170. if (wh >= MAX_WIN)
  171. return -EINVAL;
  172. if (req->Page != 0) {
  173. dev_dbg(&s->dev, "failure: requested page is zero\n");
  174. return -EINVAL;
  175. }
  176. s->win[wh].card_start = req->CardOffset;
  177. if (s->ops->set_mem_map(s, &s->win[wh]) != 0) {
  178. dev_dbg(&s->dev, "failed to set_mem_map\n");
  179. return -EIO;
  180. }
  181. return 0;
  182. } /* pcmcia_map_mem_page */
  183. EXPORT_SYMBOL(pcmcia_map_mem_page);
  184. /** pcmcia_modify_configuration
  185. *
  186. * Modify a locked socket configuration
  187. */
  188. int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
  189. modconf_t *mod)
  190. {
  191. struct pcmcia_socket *s;
  192. config_t *c;
  193. s = p_dev->socket;
  194. c = p_dev->function_config;
  195. if (!(s->state & SOCKET_PRESENT)) {
  196. dev_dbg(&s->dev, "No card present\n");
  197. return -ENODEV;
  198. }
  199. if (!(c->state & CONFIG_LOCKED)) {
  200. dev_dbg(&s->dev, "Configuration isnt't locked\n");
  201. return -EACCES;
  202. }
  203. if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
  204. if (mod->Attributes & CONF_ENABLE_IRQ) {
  205. c->Attributes |= CONF_ENABLE_IRQ;
  206. s->socket.io_irq = s->irq.AssignedIRQ;
  207. } else {
  208. c->Attributes &= ~CONF_ENABLE_IRQ;
  209. s->socket.io_irq = 0;
  210. }
  211. s->ops->set_socket(s, &s->socket);
  212. }
  213. if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
  214. dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
  215. return -EINVAL;
  216. }
  217. /* We only allow changing Vpp1 and Vpp2 to the same value */
  218. if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
  219. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  220. if (mod->Vpp1 != mod->Vpp2) {
  221. dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
  222. return -EINVAL;
  223. }
  224. s->socket.Vpp = mod->Vpp1;
  225. if (s->ops->set_socket(s, &s->socket)) {
  226. dev_printk(KERN_WARNING, &s->dev,
  227. "Unable to set VPP\n");
  228. return -EIO;
  229. }
  230. } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
  231. (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
  232. dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
  233. return -EINVAL;
  234. }
  235. if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
  236. pccard_io_map io_off = { 0, 0, 0, 0, 1 };
  237. pccard_io_map io_on;
  238. int i;
  239. io_on.speed = io_speed;
  240. for (i = 0; i < MAX_IO_WIN; i++) {
  241. if (!s->io[i].res)
  242. continue;
  243. io_off.map = i;
  244. io_on.map = i;
  245. io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
  246. io_on.start = s->io[i].res->start;
  247. io_on.stop = s->io[i].res->end;
  248. s->ops->set_io_map(s, &io_off);
  249. mdelay(40);
  250. s->ops->set_io_map(s, &io_on);
  251. }
  252. }
  253. return 0;
  254. } /* modify_configuration */
  255. EXPORT_SYMBOL(pcmcia_modify_configuration);
  256. int pcmcia_release_configuration(struct pcmcia_device *p_dev)
  257. {
  258. pccard_io_map io = { 0, 0, 0, 0, 1 };
  259. struct pcmcia_socket *s = p_dev->socket;
  260. config_t *c = p_dev->function_config;
  261. int i;
  262. if (p_dev->_locked) {
  263. p_dev->_locked = 0;
  264. if (--(s->lock_count) == 0) {
  265. s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
  266. s->socket.Vpp = 0;
  267. s->socket.io_irq = 0;
  268. s->ops->set_socket(s, &s->socket);
  269. }
  270. }
  271. if (c->state & CONFIG_LOCKED) {
  272. c->state &= ~CONFIG_LOCKED;
  273. if (c->state & CONFIG_IO_REQ)
  274. for (i = 0; i < MAX_IO_WIN; i++) {
  275. if (!s->io[i].res)
  276. continue;
  277. s->io[i].Config--;
  278. if (s->io[i].Config != 0)
  279. continue;
  280. io.map = i;
  281. s->ops->set_io_map(s, &io);
  282. }
  283. }
  284. return 0;
  285. } /* pcmcia_release_configuration */
  286. /** pcmcia_release_io
  287. *
  288. * Release_io() releases the I/O ranges allocated by a client. This
  289. * may be invoked some time after a card ejection has already dumped
  290. * the actual socket configuration, so if the client is "stale", we
  291. * don't bother checking the port ranges against the current socket
  292. * values.
  293. */
  294. static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
  295. {
  296. struct pcmcia_socket *s = p_dev->socket;
  297. config_t *c = p_dev->function_config;
  298. if (!p_dev->_io)
  299. return -EINVAL;
  300. p_dev->_io = 0;
  301. if ((c->io.BasePort1 != req->BasePort1) ||
  302. (c->io.NumPorts1 != req->NumPorts1) ||
  303. (c->io.BasePort2 != req->BasePort2) ||
  304. (c->io.NumPorts2 != req->NumPorts2))
  305. return -EINVAL;
  306. c->state &= ~CONFIG_IO_REQ;
  307. release_io_space(s, req->BasePort1, req->NumPorts1);
  308. if (req->NumPorts2)
  309. release_io_space(s, req->BasePort2, req->NumPorts2);
  310. return 0;
  311. } /* pcmcia_release_io */
  312. static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
  313. {
  314. struct pcmcia_socket *s = p_dev->socket;
  315. config_t *c = p_dev->function_config;
  316. if (!p_dev->_irq)
  317. return -EINVAL;
  318. p_dev->_irq = 0;
  319. if (c->state & CONFIG_LOCKED)
  320. return -EACCES;
  321. if (c->irq.Attributes != req->Attributes) {
  322. dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
  323. return -EINVAL;
  324. }
  325. if (s->irq.AssignedIRQ != req->AssignedIRQ) {
  326. dev_dbg(&s->dev, "IRQ must match assigned one\n");
  327. return -EINVAL;
  328. }
  329. if (--s->irq.Config == 0) {
  330. c->state &= ~CONFIG_IRQ_REQ;
  331. s->irq.AssignedIRQ = 0;
  332. }
  333. if (req->Handler)
  334. free_irq(req->AssignedIRQ, p_dev->priv);
  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
  574. printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
  575. #ifdef CONFIG_PCMCIA_PROBE
  576. #ifdef IRQ_NOAUTOEN
  577. /* if the underlying IRQ infrastructure allows for it, only allocate
  578. * the IRQ, but do not enable it
  579. */
  580. if (!(req->Handler))
  581. type |= IRQ_NOAUTOEN;
  582. #endif /* IRQ_NOAUTOEN */
  583. if (s->irq.AssignedIRQ != 0) {
  584. /* If the interrupt is already assigned, it must be the same */
  585. irq = s->irq.AssignedIRQ;
  586. } else {
  587. int try;
  588. u32 mask = s->irq_mask;
  589. void *data = p_dev; /* something unique to this device */
  590. for (try = 0; try < 64; try++) {
  591. irq = try % 32;
  592. /* marked as available by driver, and not blocked by userspace? */
  593. if (!((mask >> irq) & 1))
  594. continue;
  595. /* avoid an IRQ which is already used by a PCMCIA card */
  596. if ((try < 32) && pcmcia_used_irq[irq])
  597. continue;
  598. /* register the correct driver, if possible, of check whether
  599. * registering a dummy handle works, i.e. if the IRQ isn't
  600. * marked as used by the kernel resource management core */
  601. ret = request_irq(irq,
  602. (req->Handler) ? req->Handler : test_action,
  603. type,
  604. p_dev->devname,
  605. (req->Handler) ? p_dev->priv : data);
  606. if (!ret) {
  607. if (!req->Handler)
  608. free_irq(irq, data);
  609. break;
  610. }
  611. }
  612. }
  613. #endif
  614. /* only assign PCI irq if no IRQ already assigned */
  615. if (ret && !s->irq.AssignedIRQ) {
  616. if (!s->pci_irq) {
  617. dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
  618. return ret;
  619. }
  620. type = IRQF_SHARED;
  621. irq = s->pci_irq;
  622. }
  623. if (ret && req->Handler) {
  624. ret = request_irq(irq, req->Handler, type,
  625. p_dev->devname, p_dev->priv);
  626. if (ret) {
  627. dev_printk(KERN_INFO, &s->dev,
  628. "request_irq() failed\n");
  629. return ret;
  630. }
  631. }
  632. /* Make sure the fact the request type was overridden is passed back */
  633. if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
  634. req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
  635. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
  636. "request for exclusive IRQ could not be fulfilled.\n");
  637. dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
  638. "needs updating to supported shared IRQ lines.\n");
  639. }
  640. c->irq.Attributes = req->Attributes;
  641. s->irq.AssignedIRQ = req->AssignedIRQ = irq;
  642. s->irq.Config++;
  643. c->state |= CONFIG_IRQ_REQ;
  644. p_dev->_irq = 1;
  645. #ifdef CONFIG_PCMCIA_PROBE
  646. pcmcia_used_irq[irq]++;
  647. #endif
  648. return 0;
  649. } /* pcmcia_request_irq */
  650. EXPORT_SYMBOL(pcmcia_request_irq);
  651. /** pcmcia_request_window
  652. *
  653. * Request_window() establishes a mapping between card memory space
  654. * and system memory space.
  655. */
  656. int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
  657. {
  658. struct pcmcia_socket *s = p_dev->socket;
  659. pccard_mem_map *win;
  660. u_long align;
  661. int w;
  662. if (!(s->state & SOCKET_PRESENT)) {
  663. dev_dbg(&s->dev, "No card present\n");
  664. return -ENODEV;
  665. }
  666. if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
  667. dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
  668. return -EINVAL;
  669. }
  670. /* Window size defaults to smallest available */
  671. if (req->Size == 0)
  672. req->Size = s->map_size;
  673. align = (((s->features & SS_CAP_MEM_ALIGN) ||
  674. (req->Attributes & WIN_STRICT_ALIGN)) ?
  675. req->Size : s->map_size);
  676. if (req->Size & (s->map_size-1)) {
  677. dev_dbg(&s->dev, "invalid map size\n");
  678. return -EINVAL;
  679. }
  680. if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
  681. (req->Base & (align-1))) {
  682. dev_dbg(&s->dev, "invalid base address\n");
  683. return -EINVAL;
  684. }
  685. if (req->Base)
  686. align = 0;
  687. /* Allocate system memory window */
  688. for (w = 0; w < MAX_WIN; w++)
  689. if (!(s->state & SOCKET_WIN_REQ(w)))
  690. break;
  691. if (w == MAX_WIN) {
  692. dev_dbg(&s->dev, "all windows are used already\n");
  693. return -EINVAL;
  694. }
  695. win = &s->win[w];
  696. if (!(s->features & SS_CAP_STATIC_MAP)) {
  697. win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
  698. (req->Attributes & WIN_MAP_BELOW_1MB), s);
  699. if (!win->res) {
  700. dev_dbg(&s->dev, "allocating mem region failed\n");
  701. return -EINVAL;
  702. }
  703. }
  704. p_dev->_win |= CLIENT_WIN_REQ(w);
  705. /* Configure the socket controller */
  706. win->map = w+1;
  707. win->flags = 0;
  708. win->speed = req->AccessSpeed;
  709. if (req->Attributes & WIN_MEMORY_TYPE)
  710. win->flags |= MAP_ATTRIB;
  711. if (req->Attributes & WIN_ENABLE)
  712. win->flags |= MAP_ACTIVE;
  713. if (req->Attributes & WIN_DATA_WIDTH_16)
  714. win->flags |= MAP_16BIT;
  715. if (req->Attributes & WIN_USE_WAIT)
  716. win->flags |= MAP_USE_WAIT;
  717. win->card_start = 0;
  718. if (s->ops->set_mem_map(s, win) != 0) {
  719. dev_dbg(&s->dev, "failed to set memory mapping\n");
  720. return -EIO;
  721. }
  722. s->state |= SOCKET_WIN_REQ(w);
  723. /* Return window handle */
  724. if (s->features & SS_CAP_STATIC_MAP)
  725. req->Base = win->static_start;
  726. else
  727. req->Base = win->res->start;
  728. *wh = w + 1;
  729. return 0;
  730. } /* pcmcia_request_window */
  731. EXPORT_SYMBOL(pcmcia_request_window);
  732. void pcmcia_disable_device(struct pcmcia_device *p_dev)
  733. {
  734. pcmcia_release_configuration(p_dev);
  735. pcmcia_release_io(p_dev, &p_dev->io);
  736. pcmcia_release_irq(p_dev, &p_dev->irq);
  737. if (p_dev->win)
  738. pcmcia_release_window(p_dev, p_dev->win);
  739. }
  740. EXPORT_SYMBOL(pcmcia_disable_device);
  741. struct pcmcia_cfg_mem {
  742. struct pcmcia_device *p_dev;
  743. void *priv_data;
  744. int (*conf_check) (struct pcmcia_device *p_dev,
  745. cistpl_cftable_entry_t *cfg,
  746. cistpl_cftable_entry_t *dflt,
  747. unsigned int vcc,
  748. void *priv_data);
  749. cisparse_t parse;
  750. cistpl_cftable_entry_t dflt;
  751. };
  752. /**
  753. * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
  754. *
  755. * pcmcia_do_loop_config() is the internal callback for the call from
  756. * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
  757. * by a struct pcmcia_cfg_mem.
  758. */
  759. static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
  760. {
  761. cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
  762. struct pcmcia_cfg_mem *cfg_mem = priv;
  763. /* default values */
  764. cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
  765. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  766. cfg_mem->dflt = *cfg;
  767. return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
  768. cfg_mem->p_dev->socket->socket.Vcc,
  769. cfg_mem->priv_data);
  770. }
  771. /**
  772. * pcmcia_loop_config() - loop over configuration options
  773. * @p_dev: the struct pcmcia_device which we need to loop for.
  774. * @conf_check: function to call for each configuration option.
  775. * It gets passed the struct pcmcia_device, the CIS data
  776. * describing the configuration option, and private data
  777. * being passed to pcmcia_loop_config()
  778. * @priv_data: private data to be passed to the conf_check function.
  779. *
  780. * pcmcia_loop_config() loops over all configuration options, and calls
  781. * the driver-specific conf_check() for each one, checking whether
  782. * it is a valid one. Returns 0 on success or errorcode otherwise.
  783. */
  784. int pcmcia_loop_config(struct pcmcia_device *p_dev,
  785. int (*conf_check) (struct pcmcia_device *p_dev,
  786. cistpl_cftable_entry_t *cfg,
  787. cistpl_cftable_entry_t *dflt,
  788. unsigned int vcc,
  789. void *priv_data),
  790. void *priv_data)
  791. {
  792. struct pcmcia_cfg_mem *cfg_mem;
  793. int ret;
  794. cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
  795. if (cfg_mem == NULL)
  796. return -ENOMEM;
  797. cfg_mem->p_dev = p_dev;
  798. cfg_mem->conf_check = conf_check;
  799. cfg_mem->priv_data = priv_data;
  800. ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
  801. CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
  802. cfg_mem, pcmcia_do_loop_config);
  803. kfree(cfg_mem);
  804. return ret;
  805. }
  806. EXPORT_SYMBOL(pcmcia_loop_config);
  807. struct pcmcia_loop_mem {
  808. struct pcmcia_device *p_dev;
  809. void *priv_data;
  810. int (*loop_tuple) (struct pcmcia_device *p_dev,
  811. tuple_t *tuple,
  812. void *priv_data);
  813. };
  814. /**
  815. * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
  816. *
  817. * pcmcia_do_loop_tuple() is the internal callback for the call from
  818. * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
  819. * by a struct pcmcia_cfg_mem.
  820. */
  821. static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
  822. {
  823. struct pcmcia_loop_mem *loop = priv;
  824. return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
  825. };
  826. /**
  827. * pcmcia_loop_tuple() - loop over tuples in the CIS
  828. * @p_dev: the struct pcmcia_device which we need to loop for.
  829. * @code: which CIS code shall we look for?
  830. * @priv_data: private data to be passed to the loop_tuple function.
  831. * @loop_tuple: function to call for each CIS entry of type @function. IT
  832. * gets passed the raw tuple and @priv_data.
  833. *
  834. * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
  835. * calls the @loop_tuple function for each entry. If the call to @loop_tuple
  836. * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
  837. */
  838. int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  839. int (*loop_tuple) (struct pcmcia_device *p_dev,
  840. tuple_t *tuple,
  841. void *priv_data),
  842. void *priv_data)
  843. {
  844. struct pcmcia_loop_mem loop = {
  845. .p_dev = p_dev,
  846. .loop_tuple = loop_tuple,
  847. .priv_data = priv_data};
  848. return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
  849. &loop, pcmcia_do_loop_tuple);
  850. }
  851. EXPORT_SYMBOL(pcmcia_loop_tuple);
  852. struct pcmcia_loop_get {
  853. size_t len;
  854. cisdata_t **buf;
  855. };
  856. /**
  857. * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
  858. *
  859. * pcmcia_do_get_tuple() is the internal callback for the call from
  860. * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
  861. * the first tuple, return 0 unconditionally. Create a memory buffer large
  862. * enough to hold the content of the tuple, and fill it with the tuple data.
  863. * The caller is responsible to free the buffer.
  864. */
  865. static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
  866. void *priv)
  867. {
  868. struct pcmcia_loop_get *get = priv;
  869. *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
  870. if (*get->buf) {
  871. get->len = tuple->TupleDataLen;
  872. memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
  873. } else
  874. dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
  875. return 0;
  876. }
  877. /**
  878. * pcmcia_get_tuple() - get first tuple from CIS
  879. * @p_dev: the struct pcmcia_device which we need to loop for.
  880. * @code: which CIS code shall we look for?
  881. * @buf: pointer to store the buffer to.
  882. *
  883. * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
  884. * It returns the buffer length (or zero). The caller is responsible to free
  885. * the buffer passed in @buf.
  886. */
  887. size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
  888. unsigned char **buf)
  889. {
  890. struct pcmcia_loop_get get = {
  891. .len = 0,
  892. .buf = buf,
  893. };
  894. *get.buf = NULL;
  895. pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
  896. return get.len;
  897. }
  898. EXPORT_SYMBOL(pcmcia_get_tuple);
  899. /**
  900. * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
  901. *
  902. * pcmcia_do_get_mac() is the internal callback for the call from
  903. * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
  904. * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
  905. * to struct net_device->dev_addr[i].
  906. */
  907. static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
  908. void *priv)
  909. {
  910. struct net_device *dev = priv;
  911. int i;
  912. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  913. return -EINVAL;
  914. if (tuple->TupleDataLen < ETH_ALEN + 2) {
  915. dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
  916. "LAN_NODE_ID\n");
  917. return -EINVAL;
  918. }
  919. if (tuple->TupleData[1] != ETH_ALEN) {
  920. dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
  921. return -EINVAL;
  922. }
  923. for (i = 0; i < 6; i++)
  924. dev->dev_addr[i] = tuple->TupleData[i+2];
  925. return 0;
  926. }
  927. /**
  928. * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
  929. * @p_dev: the struct pcmcia_device for which we want the address.
  930. * @dev: a properly prepared struct net_device to store the info to.
  931. *
  932. * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
  933. * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
  934. * must be set up properly by the driver (see examples!).
  935. */
  936. int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
  937. {
  938. return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
  939. }
  940. EXPORT_SYMBOL(pcmcia_get_mac_from_cis);