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