]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/pcmcia/pcmcia_resource.c
[PATCH] vrc4171: update config
[mirror_ubuntu-artful-kernel.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>
23
24#define IN_CARD_SERVICES
1a8d4663
DB
25#include <pcmcia/cs_types.h>
26#include <pcmcia/ss.h>
27#include <pcmcia/cs.h>
28#include <pcmcia/bulkmem.h>
29#include <pcmcia/cistpl.h>
30#include <pcmcia/cisreg.h>
31#include <pcmcia/ds.h>
32
33#include "cs_internal.h"
34#include "ds_internal.h"
35
36
1a8d4663
DB
37/* Access speed for IO windows */
38static int io_speed = 0;
39module_param(io_speed, int, 0444);
40
41
42#ifdef CONFIG_PCMCIA_PROBE
531e5ca6 43#include <asm/irq.h>
1a8d4663
DB
44/* mask of IRQs already reserved by other cards, we should avoid using them */
45static u8 pcmcia_used_irq[NR_IRQS];
46#endif
47
48
49#ifdef DEBUG
50extern int ds_pc_debug;
51#define cs_socket_name(skt) ((skt)->dev.class_id)
52
53#define ds_dbg(skt, lvl, fmt, arg...) do { \
54 if (ds_pc_debug >= lvl) \
55 printk(KERN_DEBUG "pcmcia_resource: %s: " fmt, \
56 cs_socket_name(skt) , ## arg); \
57} while (0)
58#else
59#define ds_dbg(lvl, fmt, arg...) do { } while (0)
60#endif
61
62
63
64/** alloc_io_space
65 *
66 * Special stuff for managing IO windows, because they are scarce
67 */
68
69static int alloc_io_space(struct pcmcia_socket *s, u_int attr, ioaddr_t *base,
70 ioaddr_t num, u_int lines)
71{
72 int i;
73 kio_addr_t try, align;
74
75 align = (*base) ? (lines ? 1<<lines : 0) : 1;
76 if (align && (align < num)) {
77 if (*base) {
78 ds_dbg(s, 0, "odd IO request: num %#x align %#lx\n",
79 num, align);
80 align = 0;
81 } else
82 while (align && (align < num)) align <<= 1;
83 }
84 if (*base & ~(align-1)) {
85 ds_dbg(s, 0, "odd IO request: base %#x align %#lx\n",
86 *base, align);
87 align = 0;
88 }
89 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
90 *base = s->io_offset | (*base & 0x0fff);
91 return 0;
92 }
93 /* Check for an already-allocated window that must conflict with
94 * what was asked for. It is a hack because it does not catch all
95 * potential conflicts, just the most obvious ones.
96 */
97 for (i = 0; i < MAX_IO_WIN; i++)
c7d00693
DB
98 if ((s->io[i].res) &&
99 ((s->io[i].res->start & (align-1)) == *base))
1a8d4663
DB
100 return 1;
101 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693 102 if (!s->io[i].res) {
1a8d4663
DB
103 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
104 if (s->io[i].res) {
c7d00693
DB
105 *base = s->io[i].res->start;
106 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
107 s->io[i].InUse = num;
1a8d4663
DB
108 break;
109 } else
110 return 1;
c7d00693 111 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
1a8d4663
DB
112 continue;
113 /* Try to extend top of window */
c7d00693 114 try = s->io[i].res->end + 1;
1a8d4663
DB
115 if ((*base == 0) || (*base == try))
116 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
117 s->io[i].res->end + num, s) == 0) {
118 *base = try;
1a8d4663
DB
119 s->io[i].InUse += num;
120 break;
121 }
122 /* Try to extend bottom of window */
c7d00693 123 try = s->io[i].res->start - num;
1a8d4663
DB
124 if ((*base == 0) || (*base == try))
125 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
126 s->io[i].res->end, s) == 0) {
c7d00693 127 *base = try;
1a8d4663
DB
128 s->io[i].InUse += num;
129 break;
130 }
131 }
132 return (i == MAX_IO_WIN);
133} /* alloc_io_space */
134
135
136static void release_io_space(struct pcmcia_socket *s, ioaddr_t base,
137 ioaddr_t num)
138{
139 int i;
140
141 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693
DB
142 if (!s->io[i].res)
143 continue;
144 if ((s->io[i].res->start <= base) &&
145 (s->io[i].res->end >= base+num-1)) {
1a8d4663
DB
146 s->io[i].InUse -= num;
147 /* Free the window if no one else is using it */
148 if (s->io[i].InUse == 0) {
1a8d4663
DB
149 release_resource(s->io[i].res);
150 kfree(s->io[i].res);
151 s->io[i].res = NULL;
152 }
153 }
154 }
155} /* release_io_space */
156
157
158/** pccard_access_configuration_register
159 *
160 * Access_configuration_register() reads and writes configuration
161 * registers in attribute memory. Memory window 0 is reserved for
162 * this and the tuple reading services.
163 */
164
855cdf13 165int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
1a8d4663
DB
166 conf_reg_t *reg)
167{
855cdf13 168 struct pcmcia_socket *s;
1a8d4663
DB
169 config_t *c;
170 int addr;
171 u_char val;
172
855cdf13 173 if (!p_dev || !p_dev->function_config)
1a8d4663
DB
174 return CS_NO_CARD;
175
855cdf13
DB
176 s = p_dev->socket;
177 c = p_dev->function_config;
1a8d4663
DB
178
179 if (!(c->state & CONFIG_LOCKED))
180 return CS_CONFIGURATION_LOCKED;
181
182 addr = (c->ConfigBase + reg->Offset) >> 1;
183
184 switch (reg->Action) {
185 case CS_READ:
186 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
187 reg->Value = val;
188 break;
189 case CS_WRITE:
190 val = reg->Value;
191 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
192 break;
193 default:
194 return CS_BAD_ARGS;
195 break;
196 }
197 return CS_SUCCESS;
855cdf13 198} /* pcmcia_access_configuration_register */
3448139b
DB
199EXPORT_SYMBOL(pcmcia_access_configuration_register);
200
1a8d4663 201
1a8d4663 202int pccard_get_configuration_info(struct pcmcia_socket *s,
855cdf13 203 struct pcmcia_device *p_dev,
1a8d4663
DB
204 config_info_t *config)
205{
206 config_t *c;
207
208 if (!(s->state & SOCKET_PRESENT))
209 return CS_NO_CARD;
210
855cdf13 211 config->Function = p_dev->func;
1a8d4663
DB
212
213#ifdef CONFIG_CARDBUS
214 if (s->state & SOCKET_CARDBUS) {
215 memset(config, 0, sizeof(config_info_t));
216 config->Vcc = s->socket.Vcc;
217 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
218 config->Option = s->cb_dev->subordinate->number;
219 if (s->state & SOCKET_CARDBUS_CONFIG) {
220 config->Attributes = CONF_VALID_CLIENT;
221 config->IntType = INT_CARDBUS;
222 config->AssignedIRQ = s->irq.AssignedIRQ;
223 if (config->AssignedIRQ)
224 config->Attributes |= CONF_ENABLE_IRQ;
c7d00693
DB
225 config->BasePort1 = s->io[0].res->start;
226 config->NumPorts1 = s->io[0].res->end - config->BasePort1 + 1;
1a8d4663
DB
227 }
228 return CS_SUCCESS;
229 }
230#endif
231
855cdf13 232 c = (p_dev) ? p_dev->function_config : NULL;
1a8d4663
DB
233
234 if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
235 config->Attributes = 0;
236 config->Vcc = s->socket.Vcc;
237 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
238 return CS_SUCCESS;
239 }
240
241 /* !!! This is a hack !!! */
242 memcpy(&config->Attributes, &c->Attributes, sizeof(config_t));
243 config->Attributes |= CONF_VALID_CLIENT;
244 config->CardValues = c->CardValues;
245 config->IRQAttributes = c->irq.Attributes;
246 config->AssignedIRQ = s->irq.AssignedIRQ;
247 config->BasePort1 = c->io.BasePort1;
248 config->NumPorts1 = c->io.NumPorts1;
249 config->Attributes1 = c->io.Attributes1;
250 config->BasePort2 = c->io.BasePort2;
251 config->NumPorts2 = c->io.NumPorts2;
252 config->Attributes2 = c->io.Attributes2;
253 config->IOAddrLines = c->io.IOAddrLines;
254
255 return CS_SUCCESS;
256} /* pccard_get_configuration_info */
1a8d4663 257
2bc5a9bd 258int pcmcia_get_configuration_info(struct pcmcia_device *p_dev,
3448139b 259 config_info_t *config)
1a8d4663 260{
855cdf13 261 return pccard_get_configuration_info(p_dev->socket, p_dev,
2bc5a9bd 262 config);
3448139b
DB
263}
264EXPORT_SYMBOL(pcmcia_get_configuration_info);
1a8d4663
DB
265
266
267/** pcmcia_get_window
268 */
269int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
270 int idx, win_req_t *req)
271{
272 window_t *win;
273 int w;
274
275 if (!s || !(s->state & SOCKET_PRESENT))
276 return CS_NO_CARD;
277 for (w = idx; w < MAX_WIN; w++)
278 if (s->state & SOCKET_WIN_REQ(w))
279 break;
280 if (w == MAX_WIN)
281 return CS_NO_MORE_ITEMS;
282 win = &s->win[w];
283 req->Base = win->ctl.res->start;
284 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
285 req->AccessSpeed = win->ctl.speed;
286 req->Attributes = 0;
287 if (win->ctl.flags & MAP_ATTRIB)
288 req->Attributes |= WIN_MEMORY_TYPE_AM;
289 if (win->ctl.flags & MAP_ACTIVE)
290 req->Attributes |= WIN_ENABLE;
291 if (win->ctl.flags & MAP_16BIT)
292 req->Attributes |= WIN_DATA_WIDTH_16;
293 if (win->ctl.flags & MAP_USE_WAIT)
294 req->Attributes |= WIN_USE_WAIT;
295 *handle = win;
296 return CS_SUCCESS;
297} /* pcmcia_get_window */
298EXPORT_SYMBOL(pcmcia_get_window);
299
300
301/** pccard_get_status
302 *
303 * Get the current socket state bits. We don't support the latched
304 * SocketState yet: I haven't seen any point for it.
305 */
306
855cdf13 307int pccard_get_status(struct pcmcia_socket *s, struct pcmcia_device *p_dev,
1a8d4663
DB
308 cs_status_t *status)
309{
310 config_t *c;
311 int val;
312
313 s->ops->get_status(s, &val);
314 status->CardState = status->SocketState = 0;
315 status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
316 status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
317 status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
318 status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
319 if (s->state & SOCKET_SUSPEND)
320 status->CardState |= CS_EVENT_PM_SUSPEND;
321 if (!(s->state & SOCKET_PRESENT))
322 return CS_NO_CARD;
323
855cdf13
DB
324 c = (p_dev) ? p_dev->function_config : NULL;
325
1a8d4663
DB
326 if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
327 (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
328 u_char reg;
1ae9c7d8 329 if (c->CardValues & PRESENT_PIN_REPLACE) {
1a8d4663
DB
330 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
331 status->CardState |=
332 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
333 status->CardState |=
334 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
335 status->CardState |=
336 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
337 status->CardState |=
338 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
339 } else {
340 /* No PRR? Then assume we're always ready */
341 status->CardState |= CS_EVENT_READY_CHANGE;
342 }
1ae9c7d8 343 if (c->CardValues & PRESENT_EXT_STATUS) {
1a8d4663
DB
344 pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
345 status->CardState |=
346 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
347 }
348 return CS_SUCCESS;
349 }
350 status->CardState |=
351 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
352 status->CardState |=
353 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
354 status->CardState |=
355 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
356 status->CardState |=
357 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
358 return CS_SUCCESS;
359} /* pccard_get_status */
3448139b 360
855cdf13 361int pcmcia_get_status(struct pcmcia_device *p_dev, cs_status_t *status)
3448139b 362{
855cdf13 363 return pccard_get_status(p_dev->socket, p_dev, status);
3448139b
DB
364}
365EXPORT_SYMBOL(pcmcia_get_status);
366
1a8d4663
DB
367
368
369/** pcmcia_get_mem_page
370 *
371 * Change the card address of an already open memory window.
372 */
373int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
374{
375 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
376 return CS_BAD_HANDLE;
377 req->Page = 0;
378 req->CardOffset = win->ctl.card_start;
379 return CS_SUCCESS;
380} /* pcmcia_get_mem_page */
381EXPORT_SYMBOL(pcmcia_get_mem_page);
382
383
384int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
385{
386 struct pcmcia_socket *s;
387 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
388 return CS_BAD_HANDLE;
389 if (req->Page != 0)
390 return CS_BAD_PAGE;
391 s = win->sock;
392 win->ctl.card_start = req->CardOffset;
393 if (s->ops->set_mem_map(s, &win->ctl) != 0)
394 return CS_BAD_OFFSET;
395 return CS_SUCCESS;
396} /* pcmcia_map_mem_page */
397EXPORT_SYMBOL(pcmcia_map_mem_page);
398
399
400/** pcmcia_modify_configuration
401 *
402 * Modify a locked socket configuration
403 */
2bc5a9bd 404int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
1a8d4663
DB
405 modconf_t *mod)
406{
407 struct pcmcia_socket *s;
408 config_t *c;
409
2bc5a9bd 410 s = p_dev->socket;
dbb22f0d
DB
411 c = p_dev->function_config;
412
1a8d4663
DB
413 if (!(s->state & SOCKET_PRESENT))
414 return CS_NO_CARD;
415 if (!(c->state & CONFIG_LOCKED))
416 return CS_CONFIGURATION_LOCKED;
417
418 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
419 if (mod->Attributes & CONF_ENABLE_IRQ) {
420 c->Attributes |= CONF_ENABLE_IRQ;
421 s->socket.io_irq = s->irq.AssignedIRQ;
422 } else {
423 c->Attributes &= ~CONF_ENABLE_IRQ;
424 s->socket.io_irq = 0;
425 }
426 s->ops->set_socket(s, &s->socket);
427 }
428
429 if (mod->Attributes & CONF_VCC_CHANGE_VALID)
430 return CS_BAD_VCC;
431
432 /* We only allow changing Vpp1 and Vpp2 to the same value */
433 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
434 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
435 if (mod->Vpp1 != mod->Vpp2)
436 return CS_BAD_VPP;
71ed90d8 437 s->socket.Vpp = mod->Vpp1;
1a8d4663
DB
438 if (s->ops->set_socket(s, &s->socket))
439 return CS_BAD_VPP;
440 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
441 (mod->Attributes & CONF_VPP2_CHANGE_VALID))
442 return CS_BAD_VPP;
443
4bbed523
DB
444 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
445 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
446 pccard_io_map io_on;
447 int i;
448
449 io_on.speed = io_speed;
450 for (i = 0; i < MAX_IO_WIN; i++) {
451 if (!s->io[i].res)
452 continue;
453 io_off.map = i;
454 io_on.map = i;
455
456 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
457 io_on.start = s->io[i].res->start;
458 io_on.stop = s->io[i].res->end;
459
460 s->ops->set_io_map(s, &io_off);
461 mdelay(40);
462 s->ops->set_io_map(s, &io_on);
463 }
464 }
465
1a8d4663
DB
466 return CS_SUCCESS;
467} /* modify_configuration */
468EXPORT_SYMBOL(pcmcia_modify_configuration);
469
470
2bc5a9bd 471int pcmcia_release_configuration(struct pcmcia_device *p_dev)
1a8d4663
DB
472{
473 pccard_io_map io = { 0, 0, 0, 0, 1 };
2bc5a9bd 474 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc 475 config_t *c = p_dev->function_config;
1a8d4663
DB
476 int i;
477
e2d40963
DB
478 if (p_dev->_locked) {
479 p_dev->_locked = 0;
1a8d4663
DB
480 if (--(s->lock_count) == 0) {
481 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
482 s->socket.Vpp = 0;
483 s->socket.io_irq = 0;
484 s->ops->set_socket(s, &s->socket);
485 }
5f2a71fc
DB
486 }
487 if (c->state & CONFIG_LOCKED) {
488 c->state &= ~CONFIG_LOCKED;
1a8d4663
DB
489 if (c->state & CONFIG_IO_REQ)
490 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693 491 if (!s->io[i].res)
1a8d4663
DB
492 continue;
493 s->io[i].Config--;
494 if (s->io[i].Config != 0)
495 continue;
496 io.map = i;
497 s->ops->set_io_map(s, &io);
498 }
1a8d4663
DB
499 }
500
501 return CS_SUCCESS;
502} /* pcmcia_release_configuration */
1a8d4663
DB
503
504
505/** pcmcia_release_io
506 *
507 * Release_io() releases the I/O ranges allocated by a client. This
508 * may be invoked some time after a card ejection has already dumped
509 * the actual socket configuration, so if the client is "stale", we
510 * don't bother checking the port ranges against the current socket
511 * values.
512 */
b4c88400 513static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
1a8d4663 514{
2bc5a9bd 515 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc 516 config_t *c = p_dev->function_config;
1a8d4663 517
e2d40963 518 if (!p_dev->_io )
1a8d4663 519 return CS_BAD_HANDLE;
5f2a71fc 520
e2d40963 521 p_dev->_io = 0;
1a8d4663 522
5f2a71fc
DB
523 if ((c->io.BasePort1 != req->BasePort1) ||
524 (c->io.NumPorts1 != req->NumPorts1) ||
525 (c->io.BasePort2 != req->BasePort2) ||
526 (c->io.NumPorts2 != req->NumPorts2))
527 return CS_BAD_ARGS;
528
529 c->state &= ~CONFIG_IO_REQ;
1a8d4663
DB
530
531 release_io_space(s, req->BasePort1, req->NumPorts1);
532 if (req->NumPorts2)
533 release_io_space(s, req->BasePort2, req->NumPorts2);
534
535 return CS_SUCCESS;
536} /* pcmcia_release_io */
1a8d4663
DB
537
538
b4c88400 539static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
1a8d4663 540{
2bc5a9bd 541 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc
DB
542 config_t *c= p_dev->function_config;
543
e2d40963 544 if (!p_dev->_irq)
1a8d4663 545 return CS_BAD_HANDLE;
e2d40963 546 p_dev->_irq = 0;
1a8d4663 547
5f2a71fc
DB
548 if (c->state & CONFIG_LOCKED)
549 return CS_CONFIGURATION_LOCKED;
550 if (c->irq.Attributes != req->Attributes)
551 return CS_BAD_ATTRIBUTE;
552 if (s->irq.AssignedIRQ != req->AssignedIRQ)
553 return CS_BAD_IRQ;
554 if (--s->irq.Config == 0) {
555 c->state &= ~CONFIG_IRQ_REQ;
556 s->irq.AssignedIRQ = 0;
1a8d4663
DB
557 }
558
559 if (req->Attributes & IRQ_HANDLE_PRESENT) {
560 free_irq(req->AssignedIRQ, req->Instance);
561 }
562
563#ifdef CONFIG_PCMCIA_PROBE
564 pcmcia_used_irq[req->AssignedIRQ]--;
565#endif
566
567 return CS_SUCCESS;
568} /* pcmcia_release_irq */
1a8d4663
DB
569
570
571int pcmcia_release_window(window_handle_t win)
572{
573 struct pcmcia_socket *s;
574
575 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
576 return CS_BAD_HANDLE;
577 s = win->sock;
e2d40963 578 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
1a8d4663
DB
579 return CS_BAD_HANDLE;
580
581 /* Shut down memory window */
582 win->ctl.flags &= ~MAP_ACTIVE;
583 s->ops->set_mem_map(s, &win->ctl);
584 s->state &= ~SOCKET_WIN_REQ(win->index);
585
586 /* Release system memory */
587 if (win->ctl.res) {
588 release_resource(win->ctl.res);
589 kfree(win->ctl.res);
590 win->ctl.res = NULL;
591 }
e2d40963 592 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
1a8d4663
DB
593
594 win->magic = 0;
595
596 return CS_SUCCESS;
597} /* pcmcia_release_window */
598EXPORT_SYMBOL(pcmcia_release_window);
599
600
2bc5a9bd 601int pcmcia_request_configuration(struct pcmcia_device *p_dev,
1a8d4663
DB
602 config_req_t *req)
603{
604 int i;
605 u_int base;
2bc5a9bd 606 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
607 config_t *c;
608 pccard_io_map iomap;
609
1a8d4663
DB
610 if (!(s->state & SOCKET_PRESENT))
611 return CS_NO_CARD;
612
1a8d4663
DB
613 if (req->IntType & INT_CARDBUS)
614 return CS_UNSUPPORTED_MODE;
dbb22f0d 615 c = p_dev->function_config;
1a8d4663
DB
616 if (c->state & CONFIG_LOCKED)
617 return CS_CONFIGURATION_LOCKED;
618
619 /* Do power control. We don't allow changes in Vcc. */
70294b46 620 s->socket.Vpp = req->Vpp;
1a8d4663
DB
621 if (s->ops->set_socket(s, &s->socket))
622 return CS_BAD_VPP;
623
1a8d4663
DB
624 /* Pick memory or I/O card, DMA mode, interrupt */
625 c->IntType = req->IntType;
626 c->Attributes = req->Attributes;
627 if (req->IntType & INT_MEMORY_AND_IO)
628 s->socket.flags |= SS_IOCARD;
629 if (req->IntType & INT_ZOOMED_VIDEO)
630 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
631 if (req->Attributes & CONF_ENABLE_DMA)
632 s->socket.flags |= SS_DMA_MODE;
633 if (req->Attributes & CONF_ENABLE_SPKR)
634 s->socket.flags |= SS_SPKR_ENA;
635 if (req->Attributes & CONF_ENABLE_IRQ)
636 s->socket.io_irq = s->irq.AssignedIRQ;
637 else
638 s->socket.io_irq = 0;
639 s->ops->set_socket(s, &s->socket);
640 s->lock_count++;
641
642 /* Set up CIS configuration registers */
643 base = c->ConfigBase = req->ConfigBase;
1ae9c7d8 644 c->CardValues = req->Present;
1a8d4663
DB
645 if (req->Present & PRESENT_COPY) {
646 c->Copy = req->Copy;
647 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
648 }
649 if (req->Present & PRESENT_OPTION) {
650 if (s->functions == 1) {
651 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
652 } else {
653 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
654 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
655 if (req->Present & PRESENT_IOBASE_0)
656 c->Option |= COR_ADDR_DECODE;
657 }
658 if (c->state & CONFIG_IRQ_REQ)
659 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
660 c->Option |= COR_LEVEL_REQ;
661 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
662 mdelay(40);
663 }
664 if (req->Present & PRESENT_STATUS) {
665 c->Status = req->Status;
666 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
667 }
668 if (req->Present & PRESENT_PIN_REPLACE) {
669 c->Pin = req->Pin;
670 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
671 }
672 if (req->Present & PRESENT_EXT_STATUS) {
673 c->ExtStatus = req->ExtStatus;
674 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
675 }
676 if (req->Present & PRESENT_IOBASE_0) {
677 u_char b = c->io.BasePort1 & 0xff;
678 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
679 b = (c->io.BasePort1 >> 8) & 0xff;
680 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
681 }
682 if (req->Present & PRESENT_IOSIZE) {
683 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
684 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
685 }
686
687 /* Configure I/O windows */
688 if (c->state & CONFIG_IO_REQ) {
689 iomap.speed = io_speed;
690 for (i = 0; i < MAX_IO_WIN; i++)
c7d00693 691 if (s->io[i].res) {
1a8d4663
DB
692 iomap.map = i;
693 iomap.flags = MAP_ACTIVE;
c7d00693 694 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
1a8d4663
DB
695 case IO_DATA_PATH_WIDTH_16:
696 iomap.flags |= MAP_16BIT; break;
697 case IO_DATA_PATH_WIDTH_AUTO:
698 iomap.flags |= MAP_AUTOSZ; break;
699 default:
700 break;
701 }
c7d00693
DB
702 iomap.start = s->io[i].res->start;
703 iomap.stop = s->io[i].res->end;
1a8d4663
DB
704 s->ops->set_io_map(s, &iomap);
705 s->io[i].Config++;
706 }
707 }
708
709 c->state |= CONFIG_LOCKED;
e2d40963 710 p_dev->_locked = 1;
1a8d4663
DB
711 return CS_SUCCESS;
712} /* pcmcia_request_configuration */
713EXPORT_SYMBOL(pcmcia_request_configuration);
714
715
716/** pcmcia_request_io
717 *
718 * Request_io() reserves ranges of port addresses for a socket.
719 * I have not implemented range sharing or alias addressing.
720 */
2bc5a9bd 721int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
1a8d4663 722{
2bc5a9bd 723 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
724 config_t *c;
725
1a8d4663
DB
726 if (!(s->state & SOCKET_PRESENT))
727 return CS_NO_CARD;
728
1a8d4663
DB
729 if (!req)
730 return CS_UNSUPPORTED_MODE;
dbb22f0d 731 c = p_dev->function_config;
1a8d4663
DB
732 if (c->state & CONFIG_LOCKED)
733 return CS_CONFIGURATION_LOCKED;
734 if (c->state & CONFIG_IO_REQ)
735 return CS_IN_USE;
736 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
737 return CS_BAD_ATTRIBUTE;
738 if ((req->NumPorts2 > 0) &&
739 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
740 return CS_BAD_ATTRIBUTE;
741
742 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
743 req->NumPorts1, req->IOAddrLines))
744 return CS_IN_USE;
745
746 if (req->NumPorts2) {
747 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
748 req->NumPorts2, req->IOAddrLines)) {
749 release_io_space(s, req->BasePort1, req->NumPorts1);
750 return CS_IN_USE;
751 }
752 }
753
754 c->io = *req;
755 c->state |= CONFIG_IO_REQ;
e2d40963 756 p_dev->_io = 1;
1a8d4663
DB
757 return CS_SUCCESS;
758} /* pcmcia_request_io */
759EXPORT_SYMBOL(pcmcia_request_io);
760
761
762/** pcmcia_request_irq
763 *
764 * Request_irq() reserves an irq for this client.
765 *
766 * Also, since Linux only reserves irq's when they are actually
767 * hooked, we don't guarantee that an irq will still be available
768 * when the configuration is locked. Now that I think about it,
769 * there might be a way to fix this using a dummy handler.
770 */
771
772#ifdef CONFIG_PCMCIA_PROBE
773static irqreturn_t test_action(int cpl, void *dev_id, struct pt_regs *regs)
774{
775 return IRQ_NONE;
776}
777#endif
778
2bc5a9bd 779int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
1a8d4663 780{
2bc5a9bd 781 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
782 config_t *c;
783 int ret = CS_IN_USE, irq = 0;
1a8d4663 784
1a8d4663
DB
785 if (!(s->state & SOCKET_PRESENT))
786 return CS_NO_CARD;
dbb22f0d 787 c = p_dev->function_config;
1a8d4663
DB
788 if (c->state & CONFIG_LOCKED)
789 return CS_CONFIGURATION_LOCKED;
790 if (c->state & CONFIG_IRQ_REQ)
791 return CS_IN_USE;
792
793#ifdef CONFIG_PCMCIA_PROBE
794 if (s->irq.AssignedIRQ != 0) {
795 /* If the interrupt is already assigned, it must be the same */
796 irq = s->irq.AssignedIRQ;
797 } else {
798 int try;
799 u32 mask = s->irq_mask;
a1b274fb 800 void *data = &p_dev->dev.driver; /* something unique to this device */
1a8d4663
DB
801
802 for (try = 0; try < 64; try++) {
803 irq = try % 32;
804
805 /* marked as available by driver, and not blocked by userspace? */
806 if (!((mask >> irq) & 1))
807 continue;
808
809 /* avoid an IRQ which is already used by a PCMCIA card */
810 if ((try < 32) && pcmcia_used_irq[irq])
811 continue;
812
813 /* register the correct driver, if possible, of check whether
814 * registering a dummy handle works, i.e. if the IRQ isn't
815 * marked as used by the kernel resource management core */
816 ret = request_irq(irq,
817 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
818 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
819 (s->functions > 1) ||
820 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
bd65a685 821 p_dev->devname,
1a8d4663
DB
822 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
823 if (!ret) {
824 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
825 free_irq(irq, data);
826 break;
827 }
828 }
829 }
830#endif
c181e0e0
DR
831 /* only assign PCI irq if no IRQ already assigned */
832 if (ret && !s->irq.AssignedIRQ) {
1a8d4663
DB
833 if (!s->pci_irq)
834 return ret;
835 irq = s->pci_irq;
836 }
837
838 if (ret && req->Attributes & IRQ_HANDLE_PRESENT) {
839 if (request_irq(irq, req->Handler,
840 ((req->Attributes & IRQ_TYPE_DYNAMIC_SHARING) ||
841 (s->functions > 1) ||
842 (irq == s->pci_irq)) ? SA_SHIRQ : 0,
bd65a685 843 p_dev->devname, req->Instance))
1a8d4663
DB
844 return CS_IN_USE;
845 }
846
847 c->irq.Attributes = req->Attributes;
848 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
849 s->irq.Config++;
850
851 c->state |= CONFIG_IRQ_REQ;
e2d40963 852 p_dev->_irq = 1;
1a8d4663
DB
853
854#ifdef CONFIG_PCMCIA_PROBE
855 pcmcia_used_irq[irq]++;
856#endif
857
858 return CS_SUCCESS;
859} /* pcmcia_request_irq */
860EXPORT_SYMBOL(pcmcia_request_irq);
861
862
863/** pcmcia_request_window
864 *
865 * Request_window() establishes a mapping between card memory space
866 * and system memory space.
867 */
2bc5a9bd 868int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
1a8d4663 869{
2bc5a9bd 870 struct pcmcia_socket *s = (*p_dev)->socket;
1a8d4663
DB
871 window_t *win;
872 u_long align;
873 int w;
874
1a8d4663
DB
875 if (!(s->state & SOCKET_PRESENT))
876 return CS_NO_CARD;
877 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
878 return CS_BAD_ATTRIBUTE;
879
880 /* Window size defaults to smallest available */
881 if (req->Size == 0)
882 req->Size = s->map_size;
883 align = (((s->features & SS_CAP_MEM_ALIGN) ||
884 (req->Attributes & WIN_STRICT_ALIGN)) ?
885 req->Size : s->map_size);
886 if (req->Size & (s->map_size-1))
887 return CS_BAD_SIZE;
888 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
889 (req->Base & (align-1)))
890 return CS_BAD_BASE;
891 if (req->Base)
892 align = 0;
893
894 /* Allocate system memory window */
895 for (w = 0; w < MAX_WIN; w++)
896 if (!(s->state & SOCKET_WIN_REQ(w))) break;
897 if (w == MAX_WIN)
898 return CS_OUT_OF_RESOURCE;
899
900 win = &s->win[w];
901 win->magic = WINDOW_MAGIC;
902 win->index = w;
2bc5a9bd 903 win->handle = *p_dev;
1a8d4663
DB
904 win->sock = s;
905
906 if (!(s->features & SS_CAP_STATIC_MAP)) {
907 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
908 (req->Attributes & WIN_MAP_BELOW_1MB), s);
909 if (!win->ctl.res)
910 return CS_IN_USE;
911 }
e2d40963 912 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
1a8d4663
DB
913
914 /* Configure the socket controller */
915 win->ctl.map = w+1;
916 win->ctl.flags = 0;
917 win->ctl.speed = req->AccessSpeed;
918 if (req->Attributes & WIN_MEMORY_TYPE)
919 win->ctl.flags |= MAP_ATTRIB;
920 if (req->Attributes & WIN_ENABLE)
921 win->ctl.flags |= MAP_ACTIVE;
922 if (req->Attributes & WIN_DATA_WIDTH_16)
923 win->ctl.flags |= MAP_16BIT;
924 if (req->Attributes & WIN_USE_WAIT)
925 win->ctl.flags |= MAP_USE_WAIT;
926 win->ctl.card_start = 0;
927 if (s->ops->set_mem_map(s, &win->ctl) != 0)
928 return CS_BAD_ARGS;
929 s->state |= SOCKET_WIN_REQ(w);
930
931 /* Return window handle */
932 if (s->features & SS_CAP_STATIC_MAP) {
933 req->Base = win->ctl.static_start;
934 } else {
935 req->Base = win->ctl.res->start;
936 }
937 *wh = win;
938
939 return CS_SUCCESS;
940} /* pcmcia_request_window */
941EXPORT_SYMBOL(pcmcia_request_window);
5f2a71fc
DB
942
943void pcmcia_disable_device(struct pcmcia_device *p_dev) {
5f2a71fc 944 pcmcia_release_configuration(p_dev);
fd238232
DB
945 pcmcia_release_io(p_dev, &p_dev->io);
946 pcmcia_release_irq(p_dev, &p_dev->irq);
947 if (&p_dev->win)
948 pcmcia_release_window(p_dev->win);
5f2a71fc
DB
949}
950EXPORT_SYMBOL(pcmcia_disable_device);