pcmcia: deprecate CS_CONFIGURATION_LOCKED
[deliverable/linux.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
1a8d4663
DB
24#include <pcmcia/cs_types.h>
25#include <pcmcia/ss.h>
26#include <pcmcia/cs.h>
1a8d4663
DB
27#include <pcmcia/cistpl.h>
28#include <pcmcia/cisreg.h>
29#include <pcmcia/ds.h>
30
31#include "cs_internal.h"
32#include "ds_internal.h"
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:
192 return CS_BAD_ARGS;
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)
214 return CS_NO_MORE_ITEMS;
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
DB
254 if (req->Page != 0)
255 return CS_BAD_PAGE;
256 s = win->sock;
257 win->ctl.card_start = req->CardOffset;
258 if (s->ops->set_mem_map(s, &win->ctl) != 0)
259 return CS_BAD_OFFSET;
4c89e88b 260 return 0;
1a8d4663
DB
261} /* pcmcia_map_mem_page */
262EXPORT_SYMBOL(pcmcia_map_mem_page);
263
264
265/** pcmcia_modify_configuration
266 *
267 * Modify a locked socket configuration
268 */
2bc5a9bd 269int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
1a8d4663
DB
270 modconf_t *mod)
271{
272 struct pcmcia_socket *s;
273 config_t *c;
274
2bc5a9bd 275 s = p_dev->socket;
dbb22f0d
DB
276 c = p_dev->function_config;
277
1a8d4663 278 if (!(s->state & SOCKET_PRESENT))
3939c1ef 279 return -ENODEV;
1a8d4663 280 if (!(c->state & CONFIG_LOCKED))
943f70f1 281 return -EACCES;
1a8d4663
DB
282
283 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
284 if (mod->Attributes & CONF_ENABLE_IRQ) {
285 c->Attributes |= CONF_ENABLE_IRQ;
286 s->socket.io_irq = s->irq.AssignedIRQ;
287 } else {
288 c->Attributes &= ~CONF_ENABLE_IRQ;
289 s->socket.io_irq = 0;
290 }
291 s->ops->set_socket(s, &s->socket);
292 }
293
d8b0a49d
DB
294 if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
295 ds_dbg(s, 0, "changing Vcc is not allowed at this time\n");
296 return -EINVAL;
297 }
1a8d4663
DB
298
299 /* We only allow changing Vpp1 and Vpp2 to the same value */
300 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
301 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
302 if (mod->Vpp1 != mod->Vpp2)
d8b0a49d
DB
303 ds_dbg(s, 0, "Vpp1 and Vpp2 must be the same\n");
304 return -EINVAL;
71ed90d8 305 s->socket.Vpp = mod->Vpp1;
d8b0a49d
DB
306 if (s->ops->set_socket(s, &s->socket)) {
307 dev_printk(KERN_WARNING, &s->dev,
308 "Unable to set VPP\n");
309 return -EIO;
310 }
1a8d4663 311 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
d8b0a49d
DB
312 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
313 ds_dbg(s, 0, "changing Vcc is not allowed at this time\n");
314 return -EINVAL;
315 }
1a8d4663 316
4bbed523
DB
317 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
318 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
319 pccard_io_map io_on;
320 int i;
321
322 io_on.speed = io_speed;
323 for (i = 0; i < MAX_IO_WIN; i++) {
324 if (!s->io[i].res)
325 continue;
326 io_off.map = i;
327 io_on.map = i;
328
329 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
330 io_on.start = s->io[i].res->start;
331 io_on.stop = s->io[i].res->end;
332
333 s->ops->set_io_map(s, &io_off);
334 mdelay(40);
335 s->ops->set_io_map(s, &io_on);
336 }
337 }
338
4c89e88b 339 return 0;
1a8d4663
DB
340} /* modify_configuration */
341EXPORT_SYMBOL(pcmcia_modify_configuration);
342
343
2bc5a9bd 344int pcmcia_release_configuration(struct pcmcia_device *p_dev)
1a8d4663
DB
345{
346 pccard_io_map io = { 0, 0, 0, 0, 1 };
2bc5a9bd 347 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc 348 config_t *c = p_dev->function_config;
1a8d4663
DB
349 int i;
350
e2d40963
DB
351 if (p_dev->_locked) {
352 p_dev->_locked = 0;
1a8d4663
DB
353 if (--(s->lock_count) == 0) {
354 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
355 s->socket.Vpp = 0;
356 s->socket.io_irq = 0;
357 s->ops->set_socket(s, &s->socket);
358 }
5f2a71fc
DB
359 }
360 if (c->state & CONFIG_LOCKED) {
361 c->state &= ~CONFIG_LOCKED;
1a8d4663
DB
362 if (c->state & CONFIG_IO_REQ)
363 for (i = 0; i < MAX_IO_WIN; i++) {
c7d00693 364 if (!s->io[i].res)
1a8d4663
DB
365 continue;
366 s->io[i].Config--;
367 if (s->io[i].Config != 0)
368 continue;
369 io.map = i;
370 s->ops->set_io_map(s, &io);
371 }
1a8d4663
DB
372 }
373
4c89e88b 374 return 0;
1a8d4663 375} /* pcmcia_release_configuration */
1a8d4663
DB
376
377
378/** pcmcia_release_io
379 *
380 * Release_io() releases the I/O ranges allocated by a client. This
381 * may be invoked some time after a card ejection has already dumped
382 * the actual socket configuration, so if the client is "stale", we
383 * don't bother checking the port ranges against the current socket
384 * values.
385 */
b4c88400 386static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
1a8d4663 387{
2bc5a9bd 388 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc 389 config_t *c = p_dev->function_config;
1a8d4663 390
e2d40963 391 if (!p_dev->_io )
ffb8da20 392 return -EINVAL;
5f2a71fc 393
e2d40963 394 p_dev->_io = 0;
1a8d4663 395
5f2a71fc
DB
396 if ((c->io.BasePort1 != req->BasePort1) ||
397 (c->io.NumPorts1 != req->NumPorts1) ||
398 (c->io.BasePort2 != req->BasePort2) ||
399 (c->io.NumPorts2 != req->NumPorts2))
400 return CS_BAD_ARGS;
401
402 c->state &= ~CONFIG_IO_REQ;
1a8d4663
DB
403
404 release_io_space(s, req->BasePort1, req->NumPorts1);
405 if (req->NumPorts2)
406 release_io_space(s, req->BasePort2, req->NumPorts2);
407
4c89e88b 408 return 0;
1a8d4663 409} /* pcmcia_release_io */
1a8d4663
DB
410
411
b4c88400 412static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
1a8d4663 413{
2bc5a9bd 414 struct pcmcia_socket *s = p_dev->socket;
5f2a71fc
DB
415 config_t *c= p_dev->function_config;
416
e2d40963 417 if (!p_dev->_irq)
ffb8da20 418 return -EINVAL;
e2d40963 419 p_dev->_irq = 0;
1a8d4663 420
5f2a71fc 421 if (c->state & CONFIG_LOCKED)
943f70f1 422 return -EACCES;
5f2a71fc
DB
423 if (c->irq.Attributes != req->Attributes)
424 return CS_BAD_ATTRIBUTE;
425 if (s->irq.AssignedIRQ != req->AssignedIRQ)
426 return CS_BAD_IRQ;
427 if (--s->irq.Config == 0) {
428 c->state &= ~CONFIG_IRQ_REQ;
429 s->irq.AssignedIRQ = 0;
1a8d4663
DB
430 }
431
432 if (req->Attributes & IRQ_HANDLE_PRESENT) {
433 free_irq(req->AssignedIRQ, req->Instance);
434 }
435
436#ifdef CONFIG_PCMCIA_PROBE
437 pcmcia_used_irq[req->AssignedIRQ]--;
438#endif
439
4c89e88b 440 return 0;
1a8d4663 441} /* pcmcia_release_irq */
1a8d4663
DB
442
443
444int pcmcia_release_window(window_handle_t win)
445{
446 struct pcmcia_socket *s;
447
448 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
ffb8da20 449 return -EINVAL;
1a8d4663 450 s = win->sock;
e2d40963 451 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index)))
ffb8da20 452 return -EINVAL;
1a8d4663
DB
453
454 /* Shut down memory window */
455 win->ctl.flags &= ~MAP_ACTIVE;
456 s->ops->set_mem_map(s, &win->ctl);
457 s->state &= ~SOCKET_WIN_REQ(win->index);
458
459 /* Release system memory */
460 if (win->ctl.res) {
461 release_resource(win->ctl.res);
462 kfree(win->ctl.res);
463 win->ctl.res = NULL;
464 }
e2d40963 465 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
1a8d4663
DB
466
467 win->magic = 0;
468
4c89e88b 469 return 0;
1a8d4663
DB
470} /* pcmcia_release_window */
471EXPORT_SYMBOL(pcmcia_release_window);
472
473
2bc5a9bd 474int pcmcia_request_configuration(struct pcmcia_device *p_dev,
1a8d4663
DB
475 config_req_t *req)
476{
477 int i;
478 u_int base;
2bc5a9bd 479 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
480 config_t *c;
481 pccard_io_map iomap;
482
1a8d4663 483 if (!(s->state & SOCKET_PRESENT))
3939c1ef 484 return -ENODEV;;
1a8d4663 485
de6405e9
DB
486 if (req->IntType & INT_CARDBUS) {
487 ds_dbg(p_dev->socket, 0, "IntType may not be INT_CARDBUS\n");
488 return -EINVAL;
489 }
dbb22f0d 490 c = p_dev->function_config;
1a8d4663 491 if (c->state & CONFIG_LOCKED)
943f70f1 492 return -EACCES;
1a8d4663
DB
493
494 /* Do power control. We don't allow changes in Vcc. */
70294b46 495 s->socket.Vpp = req->Vpp;
d8b0a49d
DB
496 if (s->ops->set_socket(s, &s->socket)) {
497 dev_printk(KERN_WARNING, &s->dev,
498 "Unable to set socket state\n");
499 return -EINVAL;
500 }
1a8d4663 501
1a8d4663
DB
502 /* Pick memory or I/O card, DMA mode, interrupt */
503 c->IntType = req->IntType;
504 c->Attributes = req->Attributes;
505 if (req->IntType & INT_MEMORY_AND_IO)
506 s->socket.flags |= SS_IOCARD;
507 if (req->IntType & INT_ZOOMED_VIDEO)
508 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
509 if (req->Attributes & CONF_ENABLE_DMA)
510 s->socket.flags |= SS_DMA_MODE;
511 if (req->Attributes & CONF_ENABLE_SPKR)
512 s->socket.flags |= SS_SPKR_ENA;
513 if (req->Attributes & CONF_ENABLE_IRQ)
514 s->socket.io_irq = s->irq.AssignedIRQ;
515 else
516 s->socket.io_irq = 0;
517 s->ops->set_socket(s, &s->socket);
518 s->lock_count++;
519
520 /* Set up CIS configuration registers */
521 base = c->ConfigBase = req->ConfigBase;
1ae9c7d8 522 c->CardValues = req->Present;
1a8d4663
DB
523 if (req->Present & PRESENT_COPY) {
524 c->Copy = req->Copy;
525 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
526 }
527 if (req->Present & PRESENT_OPTION) {
528 if (s->functions == 1) {
529 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
530 } else {
531 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
532 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
533 if (req->Present & PRESENT_IOBASE_0)
534 c->Option |= COR_ADDR_DECODE;
535 }
536 if (c->state & CONFIG_IRQ_REQ)
537 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
538 c->Option |= COR_LEVEL_REQ;
539 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
540 mdelay(40);
541 }
542 if (req->Present & PRESENT_STATUS) {
543 c->Status = req->Status;
544 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
545 }
546 if (req->Present & PRESENT_PIN_REPLACE) {
547 c->Pin = req->Pin;
548 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
549 }
550 if (req->Present & PRESENT_EXT_STATUS) {
551 c->ExtStatus = req->ExtStatus;
552 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
553 }
554 if (req->Present & PRESENT_IOBASE_0) {
555 u_char b = c->io.BasePort1 & 0xff;
556 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
557 b = (c->io.BasePort1 >> 8) & 0xff;
558 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
559 }
560 if (req->Present & PRESENT_IOSIZE) {
561 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
562 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
563 }
564
565 /* Configure I/O windows */
566 if (c->state & CONFIG_IO_REQ) {
567 iomap.speed = io_speed;
568 for (i = 0; i < MAX_IO_WIN; i++)
c7d00693 569 if (s->io[i].res) {
1a8d4663
DB
570 iomap.map = i;
571 iomap.flags = MAP_ACTIVE;
c7d00693 572 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
1a8d4663
DB
573 case IO_DATA_PATH_WIDTH_16:
574 iomap.flags |= MAP_16BIT; break;
575 case IO_DATA_PATH_WIDTH_AUTO:
576 iomap.flags |= MAP_AUTOSZ; break;
577 default:
578 break;
579 }
c7d00693
DB
580 iomap.start = s->io[i].res->start;
581 iomap.stop = s->io[i].res->end;
1a8d4663
DB
582 s->ops->set_io_map(s, &iomap);
583 s->io[i].Config++;
584 }
585 }
586
587 c->state |= CONFIG_LOCKED;
e2d40963 588 p_dev->_locked = 1;
4c89e88b 589 return 0;
1a8d4663
DB
590} /* pcmcia_request_configuration */
591EXPORT_SYMBOL(pcmcia_request_configuration);
592
593
594/** pcmcia_request_io
595 *
596 * Request_io() reserves ranges of port addresses for a socket.
597 * I have not implemented range sharing or alias addressing.
598 */
2bc5a9bd 599int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
1a8d4663 600{
2bc5a9bd 601 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
602 config_t *c;
603
1a8d4663 604 if (!(s->state & SOCKET_PRESENT))
3939c1ef 605 return -ENODEV;
1a8d4663 606
1a8d4663 607 if (!req)
de6405e9 608 return -EINVAL;
dbb22f0d 609 c = p_dev->function_config;
1a8d4663 610 if (c->state & CONFIG_LOCKED)
943f70f1 611 return -EACCES;
1a8d4663
DB
612 if (c->state & CONFIG_IO_REQ)
613 return CS_IN_USE;
614 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))
615 return CS_BAD_ATTRIBUTE;
616 if ((req->NumPorts2 > 0) &&
617 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)))
618 return CS_BAD_ATTRIBUTE;
619
620 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
621 req->NumPorts1, req->IOAddrLines))
622 return CS_IN_USE;
623
624 if (req->NumPorts2) {
625 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
626 req->NumPorts2, req->IOAddrLines)) {
627 release_io_space(s, req->BasePort1, req->NumPorts1);
628 return CS_IN_USE;
629 }
630 }
631
632 c->io = *req;
633 c->state |= CONFIG_IO_REQ;
e2d40963 634 p_dev->_io = 1;
4c89e88b 635 return 0;
1a8d4663
DB
636} /* pcmcia_request_io */
637EXPORT_SYMBOL(pcmcia_request_io);
638
639
640/** pcmcia_request_irq
641 *
642 * Request_irq() reserves an irq for this client.
643 *
644 * Also, since Linux only reserves irq's when they are actually
645 * hooked, we don't guarantee that an irq will still be available
646 * when the configuration is locked. Now that I think about it,
647 * there might be a way to fix this using a dummy handler.
648 */
649
650#ifdef CONFIG_PCMCIA_PROBE
7d12e780 651static irqreturn_t test_action(int cpl, void *dev_id)
1a8d4663
DB
652{
653 return IRQ_NONE;
654}
655#endif
656
2bc5a9bd 657int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
1a8d4663 658{
2bc5a9bd 659 struct pcmcia_socket *s = p_dev->socket;
1a8d4663
DB
660 config_t *c;
661 int ret = CS_IN_USE, irq = 0;
c533120b 662 int type;
1a8d4663 663
1a8d4663 664 if (!(s->state & SOCKET_PRESENT))
3939c1ef 665 return -ENODEV;
dbb22f0d 666 c = p_dev->function_config;
1a8d4663 667 if (c->state & CONFIG_LOCKED)
943f70f1 668 return -EACCES;
1a8d4663
DB
669 if (c->state & CONFIG_IRQ_REQ)
670 return CS_IN_USE;
671
c533120b
AC
672 /* Decide what type of interrupt we are registering */
673 type = 0;
674 if (s->functions > 1) /* All of this ought to be handled higher up */
dace1453 675 type = IRQF_SHARED;
c533120b 676 if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
dace1453 677 type = IRQF_SHARED;
c533120b 678
1a8d4663 679#ifdef CONFIG_PCMCIA_PROBE
635416ef
AC
680
681#ifdef IRQ_NOAUTOEN
682 /* if the underlying IRQ infrastructure allows for it, only allocate
683 * the IRQ, but do not enable it
684 */
685 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
686 type |= IRQ_NOAUTOEN;
687#endif /* IRQ_NOAUTOEN */
688
1a8d4663
DB
689 if (s->irq.AssignedIRQ != 0) {
690 /* If the interrupt is already assigned, it must be the same */
691 irq = s->irq.AssignedIRQ;
692 } else {
693 int try;
694 u32 mask = s->irq_mask;
a1b274fb 695 void *data = &p_dev->dev.driver; /* something unique to this device */
1a8d4663
DB
696
697 for (try = 0; try < 64; try++) {
698 irq = try % 32;
699
700 /* marked as available by driver, and not blocked by userspace? */
701 if (!((mask >> irq) & 1))
702 continue;
703
704 /* avoid an IRQ which is already used by a PCMCIA card */
705 if ((try < 32) && pcmcia_used_irq[irq])
706 continue;
707
708 /* register the correct driver, if possible, of check whether
709 * registering a dummy handle works, i.e. if the IRQ isn't
710 * marked as used by the kernel resource management core */
711 ret = request_irq(irq,
712 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
c533120b 713 type,
bd65a685 714 p_dev->devname,
1a8d4663
DB
715 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
716 if (!ret) {
717 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
718 free_irq(irq, data);
719 break;
720 }
721 }
722 }
723#endif
c181e0e0
DR
724 /* only assign PCI irq if no IRQ already assigned */
725 if (ret && !s->irq.AssignedIRQ) {
1a8d4663
DB
726 if (!s->pci_irq)
727 return ret;
dace1453 728 type = IRQF_SHARED;
1a8d4663
DB
729 irq = s->pci_irq;
730 }
731
c533120b
AC
732 if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
733 if (request_irq(irq, req->Handler, type, p_dev->devname, req->Instance))
1a8d4663
DB
734 return CS_IN_USE;
735 }
736
c533120b 737 /* Make sure the fact the request type was overridden is passed back */
dace1453 738 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
c533120b 739 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
ac449d6e
DB
740 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
741 "request for exclusive IRQ could not be fulfilled.\n");
742 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
743 "needs updating to supported shared IRQ lines.\n");
c533120b 744 }
1a8d4663
DB
745 c->irq.Attributes = req->Attributes;
746 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
747 s->irq.Config++;
748
749 c->state |= CONFIG_IRQ_REQ;
e2d40963 750 p_dev->_irq = 1;
1a8d4663
DB
751
752#ifdef CONFIG_PCMCIA_PROBE
753 pcmcia_used_irq[irq]++;
754#endif
755
4c89e88b 756 return 0;
1a8d4663
DB
757} /* pcmcia_request_irq */
758EXPORT_SYMBOL(pcmcia_request_irq);
759
760
761/** pcmcia_request_window
762 *
763 * Request_window() establishes a mapping between card memory space
764 * and system memory space.
765 */
2bc5a9bd 766int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
1a8d4663 767{
2bc5a9bd 768 struct pcmcia_socket *s = (*p_dev)->socket;
1a8d4663
DB
769 window_t *win;
770 u_long align;
771 int w;
772
1a8d4663 773 if (!(s->state & SOCKET_PRESENT))
3939c1ef 774 return -ENODEV;
1a8d4663
DB
775 if (req->Attributes & (WIN_PAGED | WIN_SHARED))
776 return CS_BAD_ATTRIBUTE;
777
778 /* Window size defaults to smallest available */
779 if (req->Size == 0)
780 req->Size = s->map_size;
781 align = (((s->features & SS_CAP_MEM_ALIGN) ||
782 (req->Attributes & WIN_STRICT_ALIGN)) ?
783 req->Size : s->map_size);
784 if (req->Size & (s->map_size-1))
785 return CS_BAD_SIZE;
786 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
787 (req->Base & (align-1)))
788 return CS_BAD_BASE;
789 if (req->Base)
790 align = 0;
791
792 /* Allocate system memory window */
793 for (w = 0; w < MAX_WIN; w++)
794 if (!(s->state & SOCKET_WIN_REQ(w))) break;
795 if (w == MAX_WIN)
1168386a 796 return CS_IN_USE;
1a8d4663
DB
797
798 win = &s->win[w];
799 win->magic = WINDOW_MAGIC;
800 win->index = w;
2bc5a9bd 801 win->handle = *p_dev;
1a8d4663
DB
802 win->sock = s;
803
804 if (!(s->features & SS_CAP_STATIC_MAP)) {
805 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
806 (req->Attributes & WIN_MAP_BELOW_1MB), s);
807 if (!win->ctl.res)
808 return CS_IN_USE;
809 }
e2d40963 810 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
1a8d4663
DB
811
812 /* Configure the socket controller */
813 win->ctl.map = w+1;
814 win->ctl.flags = 0;
815 win->ctl.speed = req->AccessSpeed;
816 if (req->Attributes & WIN_MEMORY_TYPE)
817 win->ctl.flags |= MAP_ATTRIB;
818 if (req->Attributes & WIN_ENABLE)
819 win->ctl.flags |= MAP_ACTIVE;
820 if (req->Attributes & WIN_DATA_WIDTH_16)
821 win->ctl.flags |= MAP_16BIT;
822 if (req->Attributes & WIN_USE_WAIT)
823 win->ctl.flags |= MAP_USE_WAIT;
824 win->ctl.card_start = 0;
825 if (s->ops->set_mem_map(s, &win->ctl) != 0)
826 return CS_BAD_ARGS;
827 s->state |= SOCKET_WIN_REQ(w);
828
829 /* Return window handle */
830 if (s->features & SS_CAP_STATIC_MAP) {
831 req->Base = win->ctl.static_start;
832 } else {
833 req->Base = win->ctl.res->start;
834 }
835 *wh = win;
836
4c89e88b 837 return 0;
1a8d4663
DB
838} /* pcmcia_request_window */
839EXPORT_SYMBOL(pcmcia_request_window);
5f2a71fc
DB
840
841void pcmcia_disable_device(struct pcmcia_device *p_dev) {
5f2a71fc 842 pcmcia_release_configuration(p_dev);
fd238232
DB
843 pcmcia_release_io(p_dev, &p_dev->io);
844 pcmcia_release_irq(p_dev, &p_dev->irq);
c1ac0228 845 if (p_dev->win)
fd238232 846 pcmcia_release_window(p_dev->win);
5f2a71fc
DB
847}
848EXPORT_SYMBOL(pcmcia_disable_device);
a804b574
DB
849
850
851struct pcmcia_cfg_mem {
852 tuple_t tuple;
853 cisparse_t parse;
854 u8 buf[256];
8e2fc39d 855 cistpl_cftable_entry_t dflt;
a804b574
DB
856};
857
858/**
859 * pcmcia_loop_config() - loop over configuration options
860 * @p_dev: the struct pcmcia_device which we need to loop for.
861 * @conf_check: function to call for each configuration option.
862 * It gets passed the struct pcmcia_device, the CIS data
863 * describing the configuration option, and private data
864 * being passed to pcmcia_loop_config()
865 * @priv_data: private data to be passed to the conf_check function.
866 *
867 * pcmcia_loop_config() loops over all configuration options, and calls
868 * the driver-specific conf_check() for each one, checking whether
869 * it is a valid one.
870 */
871int pcmcia_loop_config(struct pcmcia_device *p_dev,
872 int (*conf_check) (struct pcmcia_device *p_dev,
873 cistpl_cftable_entry_t *cfg,
8e2fc39d 874 cistpl_cftable_entry_t *dflt,
ad913c11 875 unsigned int vcc,
a804b574
DB
876 void *priv_data),
877 void *priv_data)
878{
879 struct pcmcia_cfg_mem *cfg_mem;
8e2fc39d 880
a804b574
DB
881 tuple_t *tuple;
882 int ret = -ENODEV;
ad913c11 883 unsigned int vcc;
a804b574
DB
884
885 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
886 if (cfg_mem == NULL)
887 return -ENOMEM;
888
ad913c11
DB
889 /* get the current Vcc setting */
890 vcc = p_dev->socket->socket.Vcc;
891
a804b574
DB
892 tuple = &cfg_mem->tuple;
893 tuple->TupleData = cfg_mem->buf;
894 tuple->TupleDataMax = 255;
895 tuple->TupleOffset = 0;
896 tuple->DesiredTuple = CISTPL_CFTABLE_ENTRY;
897 tuple->Attributes = 0;
898
899 ret = pcmcia_get_first_tuple(p_dev, tuple);
900 while (!ret) {
498ac189
DB
901 cistpl_cftable_entry_t *cfg = &cfg_mem->parse.cftable_entry;
902
a804b574
DB
903 if (pcmcia_get_tuple_data(p_dev, tuple))
904 goto next_entry;
905
906 if (pcmcia_parse_tuple(p_dev, tuple, &cfg_mem->parse))
907 goto next_entry;
908
498ac189
DB
909 /* default values */
910 p_dev->conf.ConfigIndex = cfg->index;
8e2fc39d
DB
911 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
912 cfg_mem->dflt = *cfg;
498ac189 913
ad913c11 914 ret = conf_check(p_dev, cfg, &cfg_mem->dflt, vcc, priv_data);
a804b574
DB
915 if (!ret)
916 break;
917
918next_entry:
919 ret = pcmcia_get_next_tuple(p_dev, tuple);
920 }
921
922 return ret;
923}
924EXPORT_SYMBOL(pcmcia_loop_config);
This page took 0.462327 seconds and 5 git commands to generate.