pcmcia_resource.c 30 KB

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