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