pcmcia_resource.c 30 KB

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