pcmcia_resource.c 29 KB

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