pcmcia_resource.c 30 KB

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