blob: d5db95644b64f69cc5dc61fc47f11a7abc21bc71 [file] [log] [blame]
Dominik Brodowski1a8d4662005-06-27 16:28:53 -07001/*
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
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070017#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>
Dominik Brodowski91284222009-10-18 23:32:33 +020023#include <linux/netdevice.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070024
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070025#include <pcmcia/cs_types.h>
26#include <pcmcia/ss.h>
27#include <pcmcia/cs.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070028#include <pcmcia/cistpl.h>
29#include <pcmcia/cisreg.h>
30#include <pcmcia/ds.h>
31
32#include "cs_internal.h"
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070033
34
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070035/* Access speed for IO windows */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010036static int io_speed;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070037module_param(io_speed, int, 0444);
38
39
40#ifdef CONFIG_PCMCIA_PROBE
Al Viro531e5ca2005-08-23 22:47:01 +010041#include <asm/irq.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070042/* mask of IRQs already reserved by other cards, we should avoid using them */
43static u8 pcmcia_used_irq[NR_IRQS];
44#endif
45
46
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070047/** alloc_io_space
48 *
49 * Special stuff for managing IO windows, because they are scarce
50 */
51
Olof Johanssonecb8a842008-02-04 22:27:34 -080052static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
53 unsigned int *base, unsigned int num, u_int lines)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070054{
55 int i;
Olof Johanssonecb8a842008-02-04 22:27:34 -080056 unsigned int try, align;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070057
58 align = (*base) ? (lines ? 1<<lines : 0) : 1;
59 if (align && (align < num)) {
60 if (*base) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +020061 dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070062 num, align);
63 align = 0;
64 } else
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010065 while (align && (align < num))
66 align <<= 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070067 }
68 if (*base & ~(align-1)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +020069 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070070 *base, align);
71 align = 0;
72 }
73 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
74 *base = s->io_offset | (*base & 0x0fff);
75 return 0;
76 }
77 /* Check for an already-allocated window that must conflict with
78 * what was asked for. It is a hack because it does not catch all
79 * potential conflicts, just the most obvious ones.
80 */
81 for (i = 0; i < MAX_IO_WIN; i++)
Kaustav Majumdar4708b5f2006-10-20 14:44:09 -070082 if ((s->io[i].res) && *base &&
Dominik Brodowskic7d00692006-01-15 08:04:43 +010083 ((s->io[i].res->start & (align-1)) == *base))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070084 return 1;
85 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +010086 if (!s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070087 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
88 if (s->io[i].res) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +010089 *base = s->io[i].res->start;
90 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
91 s->io[i].InUse = num;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070092 break;
93 } else
94 return 1;
Dominik Brodowskic7d00692006-01-15 08:04:43 +010095 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070096 continue;
97 /* Try to extend top of window */
Dominik Brodowskic7d00692006-01-15 08:04:43 +010098 try = s->io[i].res->end + 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070099 if ((*base == 0) || (*base == try))
100 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
101 s->io[i].res->end + num, s) == 0) {
102 *base = try;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700103 s->io[i].InUse += num;
104 break;
105 }
106 /* Try to extend bottom of window */
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100107 try = s->io[i].res->start - num;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700108 if ((*base == 0) || (*base == try))
109 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
110 s->io[i].res->end, s) == 0) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100111 *base = try;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700112 s->io[i].InUse += num;
113 break;
114 }
115 }
116 return (i == MAX_IO_WIN);
117} /* alloc_io_space */
118
119
Olof Johanssonecb8a842008-02-04 22:27:34 -0800120static void release_io_space(struct pcmcia_socket *s, unsigned int base,
121 unsigned int num)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700122{
123 int i;
124
125 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100126 if (!s->io[i].res)
127 continue;
128 if ((s->io[i].res->start <= base) &&
129 (s->io[i].res->end >= base+num-1)) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700130 s->io[i].InUse -= num;
131 /* Free the window if no one else is using it */
132 if (s->io[i].InUse == 0) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700133 release_resource(s->io[i].res);
134 kfree(s->io[i].res);
135 s->io[i].res = NULL;
136 }
137 }
138 }
139} /* release_io_space */
140
141
142/** pccard_access_configuration_register
143 *
144 * Access_configuration_register() reads and writes configuration
145 * registers in attribute memory. Memory window 0 is reserved for
146 * this and the tuple reading services.
147 */
148
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100149int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700150 conf_reg_t *reg)
151{
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100152 struct pcmcia_socket *s;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700153 config_t *c;
154 int addr;
155 u_char val;
156
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100157 if (!p_dev || !p_dev->function_config)
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200158 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700159
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100160 s = p_dev->socket;
161 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700162
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200163 if (!(c->state & CONFIG_LOCKED)) {
164 dev_dbg(&s->dev, "Configuration isnt't locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200165 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200166 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700167
168 addr = (c->ConfigBase + reg->Offset) >> 1;
169
170 switch (reg->Action) {
171 case CS_READ:
172 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
173 reg->Value = val;
174 break;
175 case CS_WRITE:
176 val = reg->Value;
177 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
178 break;
179 default:
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200180 dev_dbg(&s->dev, "Invalid conf register request\n");
Dominik Brodowski926c5402008-08-03 12:15:11 +0200181 return -EINVAL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700182 break;
183 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200184 return 0;
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100185} /* pcmcia_access_configuration_register */
Dominik Brodowski34481392005-06-27 16:28:54 -0700186EXPORT_SYMBOL(pcmcia_access_configuration_register);
187
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700188
Magnus Damm868575d2006-12-13 19:46:43 +0900189int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
190 memreq_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700191{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900192 struct pcmcia_socket *s = p_dev->socket;
Magnus Damm868575d2006-12-13 19:46:43 +0900193
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900194 wh--;
195 if (wh >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200196 return -EINVAL;
Dominik Brodowski610e2372008-08-03 11:58:53 +0200197 if (req->Page != 0) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200198 dev_dbg(&s->dev, "failure: requested page is zero\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200199 return -EINVAL;
200 }
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100201 s->win[wh].card_start = req->CardOffset;
202 if (s->ops->set_mem_map(s, &s->win[wh]) != 0) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200203 dev_dbg(&s->dev, "failed to set_mem_map\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200204 return -EIO;
205 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200206 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700207} /* pcmcia_map_mem_page */
208EXPORT_SYMBOL(pcmcia_map_mem_page);
209
210
211/** pcmcia_modify_configuration
212 *
213 * Modify a locked socket configuration
214 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700215int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700216 modconf_t *mod)
217{
218 struct pcmcia_socket *s;
219 config_t *c;
220
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700221 s = p_dev->socket;
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100222 c = p_dev->function_config;
223
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200224 if (!(s->state & SOCKET_PRESENT)) {
225 dev_dbg(&s->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200226 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200227 }
228 if (!(c->state & CONFIG_LOCKED)) {
229 dev_dbg(&s->dev, "Configuration isnt't locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200230 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200231 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700232
233 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
234 if (mod->Attributes & CONF_ENABLE_IRQ) {
235 c->Attributes |= CONF_ENABLE_IRQ;
236 s->socket.io_irq = s->irq.AssignedIRQ;
237 } else {
238 c->Attributes &= ~CONF_ENABLE_IRQ;
239 s->socket.io_irq = 0;
240 }
241 s->ops->set_socket(s, &s->socket);
242 }
243
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200244 if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200245 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200246 return -EINVAL;
247 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700248
249 /* We only allow changing Vpp1 and Vpp2 to the same value */
250 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
251 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
Ilpo Järvinen60df3de82008-10-30 13:02:54 +0200252 if (mod->Vpp1 != mod->Vpp2) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200253 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200254 return -EINVAL;
Ilpo Järvinen60df3de82008-10-30 13:02:54 +0200255 }
Dominik Brodowski71ed90d2005-09-09 13:03:27 -0700256 s->socket.Vpp = mod->Vpp1;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200257 if (s->ops->set_socket(s, &s->socket)) {
258 dev_printk(KERN_WARNING, &s->dev,
259 "Unable to set VPP\n");
260 return -EIO;
261 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700262 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200263 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200264 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200265 return -EINVAL;
266 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700267
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100268 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
269 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
270 pccard_io_map io_on;
271 int i;
272
273 io_on.speed = io_speed;
274 for (i = 0; i < MAX_IO_WIN; i++) {
275 if (!s->io[i].res)
276 continue;
277 io_off.map = i;
278 io_on.map = i;
279
280 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
281 io_on.start = s->io[i].res->start;
282 io_on.stop = s->io[i].res->end;
283
284 s->ops->set_io_map(s, &io_off);
285 mdelay(40);
286 s->ops->set_io_map(s, &io_on);
287 }
288 }
289
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200290 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700291} /* modify_configuration */
292EXPORT_SYMBOL(pcmcia_modify_configuration);
293
294
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700295int pcmcia_release_configuration(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700296{
297 pccard_io_map io = { 0, 0, 0, 0, 1 };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700298 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100299 config_t *c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700300 int i;
301
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100302 if (p_dev->_locked) {
303 p_dev->_locked = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700304 if (--(s->lock_count) == 0) {
305 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
306 s->socket.Vpp = 0;
307 s->socket.io_irq = 0;
308 s->ops->set_socket(s, &s->socket);
309 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100310 }
311 if (c->state & CONFIG_LOCKED) {
312 c->state &= ~CONFIG_LOCKED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700313 if (c->state & CONFIG_IO_REQ)
314 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100315 if (!s->io[i].res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700316 continue;
317 s->io[i].Config--;
318 if (s->io[i].Config != 0)
319 continue;
320 io.map = i;
321 s->ops->set_io_map(s, &io);
322 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700323 }
324
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200325 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700326} /* pcmcia_release_configuration */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700327
328
329/** pcmcia_release_io
330 *
331 * Release_io() releases the I/O ranges allocated by a client. This
332 * may be invoked some time after a card ejection has already dumped
333 * the actual socket configuration, so if the client is "stale", we
334 * don't bother checking the port ranges against the current socket
335 * values.
336 */
Adrian Bunkb4c88402006-01-18 23:53:13 -0800337static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700338{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700339 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100340 config_t *c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700341
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100342 if (!p_dev->_io)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200343 return -EINVAL;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100344
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100345 p_dev->_io = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700346
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100347 if ((c->io.BasePort1 != req->BasePort1) ||
348 (c->io.NumPorts1 != req->NumPorts1) ||
349 (c->io.BasePort2 != req->BasePort2) ||
350 (c->io.NumPorts2 != req->NumPorts2))
Dominik Brodowski926c5402008-08-03 12:15:11 +0200351 return -EINVAL;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100352
353 c->state &= ~CONFIG_IO_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700354
355 release_io_space(s, req->BasePort1, req->NumPorts1);
356 if (req->NumPorts2)
357 release_io_space(s, req->BasePort2, req->NumPorts2);
358
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200359 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700360} /* pcmcia_release_io */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700361
362
Adrian Bunkb4c88402006-01-18 23:53:13 -0800363static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700364{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700365 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100366 config_t *c = p_dev->function_config;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100367
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100368 if (!p_dev->_irq)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200369 return -EINVAL;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100370 p_dev->_irq = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700371
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100372 if (c->state & CONFIG_LOCKED)
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200373 return -EACCES;
Dominik Brodowski610e2372008-08-03 11:58:53 +0200374 if (c->irq.Attributes != req->Attributes) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200375 dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200376 return -EINVAL;
377 }
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200378 if (s->irq.AssignedIRQ != req->AssignedIRQ) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200379 dev_dbg(&s->dev, "IRQ must match assigned one\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200380 return -EINVAL;
381 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100382 if (--s->irq.Config == 0) {
383 c->state &= ~CONFIG_IRQ_REQ;
384 s->irq.AssignedIRQ = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700385 }
386
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100387 if (req->Handler)
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100388 free_irq(req->AssignedIRQ, p_dev->priv);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700389
390#ifdef CONFIG_PCMCIA_PROBE
391 pcmcia_used_irq[req->AssignedIRQ]--;
392#endif
393
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200394 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700395} /* pcmcia_release_irq */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700396
397
Magnus Dammf5560da2006-12-13 19:46:38 +0900398int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700399{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900400 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100401 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700402
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900403 wh--;
404 if (wh >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200405 return -EINVAL;
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900406
407 win = &s->win[wh];
408
409 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200410 dev_dbg(&s->dev, "not releasing unknown window\n");
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200411 return -EINVAL;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200412 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700413
414 /* Shut down memory window */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100415 win->flags &= ~MAP_ACTIVE;
416 s->ops->set_mem_map(s, win);
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900417 s->state &= ~SOCKET_WIN_REQ(wh);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700418
419 /* Release system memory */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100420 if (win->res) {
421 release_resource(win->res);
422 kfree(win->res);
423 win->res = NULL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700424 }
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900425 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700426
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200427 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700428} /* pcmcia_release_window */
429EXPORT_SYMBOL(pcmcia_release_window);
430
431
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700432int pcmcia_request_configuration(struct pcmcia_device *p_dev,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700433 config_req_t *req)
434{
435 int i;
436 u_int base;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700437 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700438 config_t *c;
439 pccard_io_map iomap;
440
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700441 if (!(s->state & SOCKET_PRESENT))
Joe Perchesd598de02009-06-28 09:26:41 -0700442 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700443
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200444 if (req->IntType & INT_CARDBUS) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200445 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200446 return -EINVAL;
447 }
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100448 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200449 if (c->state & CONFIG_LOCKED) {
450 dev_dbg(&s->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200451 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200452 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700453
454 /* Do power control. We don't allow changes in Vcc. */
Dominik Brodowski70294b42006-01-15 12:43:16 +0100455 s->socket.Vpp = req->Vpp;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200456 if (s->ops->set_socket(s, &s->socket)) {
457 dev_printk(KERN_WARNING, &s->dev,
458 "Unable to set socket state\n");
459 return -EINVAL;
460 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700461
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700462 /* Pick memory or I/O card, DMA mode, interrupt */
463 c->IntType = req->IntType;
464 c->Attributes = req->Attributes;
465 if (req->IntType & INT_MEMORY_AND_IO)
466 s->socket.flags |= SS_IOCARD;
467 if (req->IntType & INT_ZOOMED_VIDEO)
468 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
469 if (req->Attributes & CONF_ENABLE_DMA)
470 s->socket.flags |= SS_DMA_MODE;
471 if (req->Attributes & CONF_ENABLE_SPKR)
472 s->socket.flags |= SS_SPKR_ENA;
473 if (req->Attributes & CONF_ENABLE_IRQ)
474 s->socket.io_irq = s->irq.AssignedIRQ;
475 else
476 s->socket.io_irq = 0;
477 s->ops->set_socket(s, &s->socket);
478 s->lock_count++;
479
480 /* Set up CIS configuration registers */
481 base = c->ConfigBase = req->ConfigBase;
Dominik Brodowski1ae9c7d2006-01-10 20:40:40 +0100482 c->CardValues = req->Present;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700483 if (req->Present & PRESENT_COPY) {
484 c->Copy = req->Copy;
485 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
486 }
487 if (req->Present & PRESENT_OPTION) {
488 if (s->functions == 1) {
489 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
490 } else {
491 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
492 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
493 if (req->Present & PRESENT_IOBASE_0)
494 c->Option |= COR_ADDR_DECODE;
495 }
496 if (c->state & CONFIG_IRQ_REQ)
497 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
498 c->Option |= COR_LEVEL_REQ;
499 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
500 mdelay(40);
501 }
502 if (req->Present & PRESENT_STATUS) {
503 c->Status = req->Status;
504 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
505 }
506 if (req->Present & PRESENT_PIN_REPLACE) {
507 c->Pin = req->Pin;
508 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
509 }
510 if (req->Present & PRESENT_EXT_STATUS) {
511 c->ExtStatus = req->ExtStatus;
512 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
513 }
514 if (req->Present & PRESENT_IOBASE_0) {
515 u_char b = c->io.BasePort1 & 0xff;
516 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
517 b = (c->io.BasePort1 >> 8) & 0xff;
518 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
519 }
520 if (req->Present & PRESENT_IOSIZE) {
521 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
522 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
523 }
524
525 /* Configure I/O windows */
526 if (c->state & CONFIG_IO_REQ) {
527 iomap.speed = io_speed;
528 for (i = 0; i < MAX_IO_WIN; i++)
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100529 if (s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700530 iomap.map = i;
531 iomap.flags = MAP_ACTIVE;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100532 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700533 case IO_DATA_PATH_WIDTH_16:
534 iomap.flags |= MAP_16BIT; break;
535 case IO_DATA_PATH_WIDTH_AUTO:
536 iomap.flags |= MAP_AUTOSZ; break;
537 default:
538 break;
539 }
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100540 iomap.start = s->io[i].res->start;
541 iomap.stop = s->io[i].res->end;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700542 s->ops->set_io_map(s, &iomap);
543 s->io[i].Config++;
544 }
545 }
546
547 c->state |= CONFIG_LOCKED;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100548 p_dev->_locked = 1;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200549 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700550} /* pcmcia_request_configuration */
551EXPORT_SYMBOL(pcmcia_request_configuration);
552
553
554/** pcmcia_request_io
555 *
556 * Request_io() reserves ranges of port addresses for a socket.
557 * I have not implemented range sharing or alias addressing.
558 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700559int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700560{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700561 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700562 config_t *c;
563
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200564 if (!(s->state & SOCKET_PRESENT)) {
565 dev_dbg(&s->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200566 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200567 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700568
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700569 if (!req)
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200570 return -EINVAL;
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100571 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200572 if (c->state & CONFIG_LOCKED) {
573 dev_dbg(&s->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200574 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200575 }
Dominik Brodowskif9580952008-08-03 11:40:19 +0200576 if (c->state & CONFIG_IO_REQ) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200577 dev_dbg(&s->dev, "IO already configured\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200578 return -EBUSY;
579 }
Dominik Brodowski610e2372008-08-03 11:58:53 +0200580 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200581 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200582 return -EINVAL;
583 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700584 if ((req->NumPorts2 > 0) &&
Dominik Brodowski610e2372008-08-03 11:58:53 +0200585 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200586 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200587 return -EINVAL;
588 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700589
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200590 dev_dbg(&s->dev, "trying to allocate resource 1\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700591 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
Dominik Brodowskif9580952008-08-03 11:40:19 +0200592 req->NumPorts1, req->IOAddrLines)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200593 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200594 return -EBUSY;
595 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700596
597 if (req->NumPorts2) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200598 dev_dbg(&s->dev, "trying to allocate resource 2\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700599 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
600 req->NumPorts2, req->IOAddrLines)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200601 dev_dbg(&s->dev, "allocation of resource 2 failed\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700602 release_io_space(s, req->BasePort1, req->NumPorts1);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200603 return -EBUSY;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700604 }
605 }
606
607 c->io = *req;
608 c->state |= CONFIG_IO_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100609 p_dev->_io = 1;
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200610 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700611} /* pcmcia_request_io */
612EXPORT_SYMBOL(pcmcia_request_io);
613
614
615/** pcmcia_request_irq
616 *
617 * Request_irq() reserves an irq for this client.
618 *
619 * Also, since Linux only reserves irq's when they are actually
620 * hooked, we don't guarantee that an irq will still be available
621 * when the configuration is locked. Now that I think about it,
622 * there might be a way to fix this using a dummy handler.
623 */
624
625#ifdef CONFIG_PCMCIA_PROBE
David Howells7d12e782006-10-05 14:55:46 +0100626static irqreturn_t test_action(int cpl, void *dev_id)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700627{
628 return IRQ_NONE;
629}
630#endif
631
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700632int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700633{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700634 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700635 config_t *c;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200636 int ret = -EINVAL, irq = 0;
Alan Coxc5331202006-05-16 16:16:44 +0100637 int type;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700638
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200639 if (!(s->state & SOCKET_PRESENT)) {
640 dev_dbg(&s->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200641 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200642 }
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100643 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200644 if (c->state & CONFIG_LOCKED) {
645 dev_dbg(&s->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200646 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200647 }
Dominik Brodowskif9580952008-08-03 11:40:19 +0200648 if (c->state & CONFIG_IRQ_REQ) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200649 dev_dbg(&s->dev, "IRQ already configured\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200650 return -EBUSY;
651 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700652
Alan Coxc5331202006-05-16 16:16:44 +0100653 /* Decide what type of interrupt we are registering */
654 type = 0;
655 if (s->functions > 1) /* All of this ought to be handled higher up */
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700656 type = IRQF_SHARED;
Alan Cox7bbfd392008-10-05 17:35:59 +0100657 else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700658 type = IRQF_SHARED;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100659 else
660 printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
Alan Coxc5331202006-05-16 16:16:44 +0100661
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700662#ifdef CONFIG_PCMCIA_PROBE
Alan Cox635416e2008-06-16 14:35:15 +0200663
664#ifdef IRQ_NOAUTOEN
665 /* if the underlying IRQ infrastructure allows for it, only allocate
666 * the IRQ, but do not enable it
667 */
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100668 if (!(req->Handler))
Alan Cox635416e2008-06-16 14:35:15 +0200669 type |= IRQ_NOAUTOEN;
670#endif /* IRQ_NOAUTOEN */
671
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700672 if (s->irq.AssignedIRQ != 0) {
673 /* If the interrupt is already assigned, it must be the same */
674 irq = s->irq.AssignedIRQ;
675 } else {
676 int try;
677 u32 mask = s->irq_mask;
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100678 void *data = p_dev; /* something unique to this device */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700679
680 for (try = 0; try < 64; try++) {
681 irq = try % 32;
682
683 /* marked as available by driver, and not blocked by userspace? */
684 if (!((mask >> irq) & 1))
685 continue;
686
687 /* avoid an IRQ which is already used by a PCMCIA card */
688 if ((try < 32) && pcmcia_used_irq[irq])
689 continue;
690
691 /* register the correct driver, if possible, of check whether
692 * registering a dummy handle works, i.e. if the IRQ isn't
693 * marked as used by the kernel resource management core */
694 ret = request_irq(irq,
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100695 (req->Handler) ? req->Handler : test_action,
Alan Coxc5331202006-05-16 16:16:44 +0100696 type,
Brice Goglinbd65a682005-09-09 13:03:29 -0700697 p_dev->devname,
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100698 (req->Handler) ? p_dev->priv : data);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700699 if (!ret) {
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100700 if (!req->Handler)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700701 free_irq(irq, data);
702 break;
703 }
704 }
705 }
706#endif
Daniel Ritzc181e0e2005-09-09 13:03:25 -0700707 /* only assign PCI irq if no IRQ already assigned */
708 if (ret && !s->irq.AssignedIRQ) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200709 if (!s->pci_irq) {
710 dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700711 return ret;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200712 }
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700713 type = IRQF_SHARED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700714 irq = s->pci_irq;
715 }
716
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100717 if (ret && req->Handler) {
Dominik Brodowskif9580952008-08-03 11:40:19 +0200718 ret = request_irq(irq, req->Handler, type,
Dominik Brodowski5fa91672009-11-08 17:24:46 +0100719 p_dev->devname, p_dev->priv);
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200720 if (ret) {
721 dev_printk(KERN_INFO, &s->dev,
722 "request_irq() failed\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200723 return ret;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200724 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700725 }
726
Alan Coxc5331202006-05-16 16:16:44 +0100727 /* Make sure the fact the request type was overridden is passed back */
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700728 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
Alan Coxc5331202006-05-16 16:16:44 +0100729 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
Dominik Brodowskiac449d62008-08-02 18:33:56 +0200730 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
731 "request for exclusive IRQ could not be fulfilled.\n");
732 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
733 "needs updating to supported shared IRQ lines.\n");
Alan Coxc5331202006-05-16 16:16:44 +0100734 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700735 c->irq.Attributes = req->Attributes;
736 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
737 s->irq.Config++;
738
739 c->state |= CONFIG_IRQ_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100740 p_dev->_irq = 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700741
742#ifdef CONFIG_PCMCIA_PROBE
743 pcmcia_used_irq[irq]++;
744#endif
745
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200746 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700747} /* pcmcia_request_irq */
748EXPORT_SYMBOL(pcmcia_request_irq);
749
750
751/** pcmcia_request_window
752 *
753 * Request_window() establishes a mapping between card memory space
754 * and system memory space.
755 */
Dominik Brodowski6838b032009-11-03 01:31:52 +0100756int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700757{
Dominik Brodowski6838b032009-11-03 01:31:52 +0100758 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100759 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700760 u_long align;
761 int w;
762
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200763 if (!(s->state & SOCKET_PRESENT)) {
764 dev_dbg(&s->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200765 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200766 }
Dominik Brodowski610e2372008-08-03 11:58:53 +0200767 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200768 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200769 return -EINVAL;
770 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700771
772 /* Window size defaults to smallest available */
773 if (req->Size == 0)
774 req->Size = s->map_size;
775 align = (((s->features & SS_CAP_MEM_ALIGN) ||
776 (req->Attributes & WIN_STRICT_ALIGN)) ?
777 req->Size : s->map_size);
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200778 if (req->Size & (s->map_size-1)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200779 dev_dbg(&s->dev, "invalid map size\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200780 return -EINVAL;
781 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700782 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200783 (req->Base & (align-1))) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200784 dev_dbg(&s->dev, "invalid base address\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200785 return -EINVAL;
786 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700787 if (req->Base)
788 align = 0;
789
790 /* Allocate system memory window */
791 for (w = 0; w < MAX_WIN; w++)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100792 if (!(s->state & SOCKET_WIN_REQ(w)))
793 break;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200794 if (w == MAX_WIN) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200795 dev_dbg(&s->dev, "all windows are used already\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200796 return -EINVAL;
797 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700798
799 win = &s->win[w];
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700800
801 if (!(s->features & SS_CAP_STATIC_MAP)) {
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100802 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700803 (req->Attributes & WIN_MAP_BELOW_1MB), s);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100804 if (!win->res) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200805 dev_dbg(&s->dev, "allocating mem region failed\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200806 return -EINVAL;
807 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700808 }
Dominik Brodowski6838b032009-11-03 01:31:52 +0100809 p_dev->_win |= CLIENT_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700810
811 /* Configure the socket controller */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100812 win->map = w+1;
813 win->flags = 0;
814 win->speed = req->AccessSpeed;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700815 if (req->Attributes & WIN_MEMORY_TYPE)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100816 win->flags |= MAP_ATTRIB;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700817 if (req->Attributes & WIN_ENABLE)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100818 win->flags |= MAP_ACTIVE;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700819 if (req->Attributes & WIN_DATA_WIDTH_16)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100820 win->flags |= MAP_16BIT;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700821 if (req->Attributes & WIN_USE_WAIT)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100822 win->flags |= MAP_USE_WAIT;
823 win->card_start = 0;
824 if (s->ops->set_mem_map(s, win) != 0) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200825 dev_dbg(&s->dev, "failed to set memory mapping\n");
Dominik Brodowski926c5402008-08-03 12:15:11 +0200826 return -EIO;
827 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700828 s->state |= SOCKET_WIN_REQ(w);
829
830 /* Return window handle */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100831 if (s->features & SS_CAP_STATIC_MAP)
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100832 req->Base = win->static_start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100833 else
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100834 req->Base = win->res->start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100835
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900836 *wh = w + 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700837
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200838 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700839} /* pcmcia_request_window */
840EXPORT_SYMBOL(pcmcia_request_window);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100841
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100842void pcmcia_disable_device(struct pcmcia_device *p_dev)
843{
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100844 pcmcia_release_configuration(p_dev);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100845 pcmcia_release_io(p_dev, &p_dev->io);
846 pcmcia_release_irq(p_dev, &p_dev->irq);
Dominik Brodowskic1ac0222008-06-14 18:51:06 +0200847 if (p_dev->win)
Magnus Dammf5560da2006-12-13 19:46:38 +0900848 pcmcia_release_window(p_dev, p_dev->win);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100849}
850EXPORT_SYMBOL(pcmcia_disable_device);
Dominik Brodowskia804b572008-07-29 08:38:30 +0200851
852
853struct pcmcia_cfg_mem {
Dominik Brodowski91284222009-10-18 23:32:33 +0200854 struct pcmcia_device *p_dev;
855 void *priv_data;
856 int (*conf_check) (struct pcmcia_device *p_dev,
857 cistpl_cftable_entry_t *cfg,
858 cistpl_cftable_entry_t *dflt,
859 unsigned int vcc,
860 void *priv_data);
Dominik Brodowskia804b572008-07-29 08:38:30 +0200861 cisparse_t parse;
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200862 cistpl_cftable_entry_t dflt;
Dominik Brodowskia804b572008-07-29 08:38:30 +0200863};
864
865/**
Dominik Brodowski91284222009-10-18 23:32:33 +0200866 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
867 *
868 * pcmcia_do_loop_config() is the internal callback for the call from
869 * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
870 * by a struct pcmcia_cfg_mem.
871 */
872static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
873{
874 cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
875 struct pcmcia_cfg_mem *cfg_mem = priv;
876
877 /* default values */
878 cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
879 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
880 cfg_mem->dflt = *cfg;
881
882 return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
883 cfg_mem->p_dev->socket->socket.Vcc,
884 cfg_mem->priv_data);
885}
886
887/**
Dominik Brodowskia804b572008-07-29 08:38:30 +0200888 * pcmcia_loop_config() - loop over configuration options
889 * @p_dev: the struct pcmcia_device which we need to loop for.
890 * @conf_check: function to call for each configuration option.
891 * It gets passed the struct pcmcia_device, the CIS data
892 * describing the configuration option, and private data
893 * being passed to pcmcia_loop_config()
894 * @priv_data: private data to be passed to the conf_check function.
895 *
896 * pcmcia_loop_config() loops over all configuration options, and calls
897 * the driver-specific conf_check() for each one, checking whether
Wolfram Sang889c2772009-07-29 14:25:37 +0200898 * it is a valid one. Returns 0 on success or errorcode otherwise.
Dominik Brodowskia804b572008-07-29 08:38:30 +0200899 */
900int pcmcia_loop_config(struct pcmcia_device *p_dev,
901 int (*conf_check) (struct pcmcia_device *p_dev,
902 cistpl_cftable_entry_t *cfg,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200903 cistpl_cftable_entry_t *dflt,
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200904 unsigned int vcc,
Dominik Brodowskia804b572008-07-29 08:38:30 +0200905 void *priv_data),
906 void *priv_data)
907{
908 struct pcmcia_cfg_mem *cfg_mem;
Wolfram Sang889c2772009-07-29 14:25:37 +0200909 int ret;
Dominik Brodowskia804b572008-07-29 08:38:30 +0200910
911 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
912 if (cfg_mem == NULL)
913 return -ENOMEM;
914
Dominik Brodowski91284222009-10-18 23:32:33 +0200915 cfg_mem->p_dev = p_dev;
916 cfg_mem->conf_check = conf_check;
917 cfg_mem->priv_data = priv_data;
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200918
Dominik Brodowski91284222009-10-18 23:32:33 +0200919 ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
920 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
921 cfg_mem, pcmcia_do_loop_config);
Dominik Brodowskia804b572008-07-29 08:38:30 +0200922
Dominik Brodowski91284222009-10-18 23:32:33 +0200923 kfree(cfg_mem);
Dominik Brodowskia804b572008-07-29 08:38:30 +0200924 return ret;
925}
926EXPORT_SYMBOL(pcmcia_loop_config);
Dominik Brodowski91284222009-10-18 23:32:33 +0200927
928
929struct pcmcia_loop_mem {
930 struct pcmcia_device *p_dev;
931 void *priv_data;
932 int (*loop_tuple) (struct pcmcia_device *p_dev,
933 tuple_t *tuple,
934 void *priv_data);
935};
936
937/**
938 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
939 *
940 * pcmcia_do_loop_tuple() is the internal callback for the call from
941 * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
942 * by a struct pcmcia_cfg_mem.
943 */
944static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
945{
946 struct pcmcia_loop_mem *loop = priv;
947
948 return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
949};
950
951/**
952 * pcmcia_loop_tuple() - loop over tuples in the CIS
953 * @p_dev: the struct pcmcia_device which we need to loop for.
954 * @code: which CIS code shall we look for?
955 * @priv_data: private data to be passed to the loop_tuple function.
956 * @loop_tuple: function to call for each CIS entry of type @function. IT
957 * gets passed the raw tuple and @priv_data.
958 *
959 * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
960 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
961 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
962 */
963int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
964 int (*loop_tuple) (struct pcmcia_device *p_dev,
965 tuple_t *tuple,
966 void *priv_data),
967 void *priv_data)
968{
969 struct pcmcia_loop_mem loop = {
970 .p_dev = p_dev,
971 .loop_tuple = loop_tuple,
972 .priv_data = priv_data};
973
974 return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
975 &loop, pcmcia_do_loop_tuple);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100976}
Dominik Brodowski91284222009-10-18 23:32:33 +0200977EXPORT_SYMBOL(pcmcia_loop_tuple);
978
979
980struct pcmcia_loop_get {
981 size_t len;
982 cisdata_t **buf;
983};
984
985/**
986 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
987 *
988 * pcmcia_do_get_tuple() is the internal callback for the call from
989 * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
990 * the first tuple, return 0 unconditionally. Create a memory buffer large
991 * enough to hold the content of the tuple, and fill it with the tuple data.
992 * The caller is responsible to free the buffer.
993 */
994static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
995 void *priv)
996{
997 struct pcmcia_loop_get *get = priv;
998
999 *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1000 if (*get->buf) {
1001 get->len = tuple->TupleDataLen;
1002 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
Dominik Brodowski6d9a2992009-10-24 12:20:18 +02001003 } else
1004 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
Dominik Brodowski91284222009-10-18 23:32:33 +02001005 return 0;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001006}
Dominik Brodowski91284222009-10-18 23:32:33 +02001007
1008/**
1009 * pcmcia_get_tuple() - get first tuple from CIS
1010 * @p_dev: the struct pcmcia_device which we need to loop for.
1011 * @code: which CIS code shall we look for?
1012 * @buf: pointer to store the buffer to.
1013 *
1014 * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
1015 * It returns the buffer length (or zero). The caller is responsible to free
1016 * the buffer passed in @buf.
1017 */
1018size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1019 unsigned char **buf)
1020{
1021 struct pcmcia_loop_get get = {
1022 .len = 0,
1023 .buf = buf,
1024 };
1025
1026 *get.buf = NULL;
1027 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1028
1029 return get.len;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001030}
Dominik Brodowski91284222009-10-18 23:32:33 +02001031EXPORT_SYMBOL(pcmcia_get_tuple);
1032
1033
1034/**
1035 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
1036 *
1037 * pcmcia_do_get_mac() is the internal callback for the call from
1038 * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
1039 * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
1040 * to struct net_device->dev_addr[i].
1041 */
1042static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1043 void *priv)
1044{
1045 struct net_device *dev = priv;
1046 int i;
1047
1048 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1049 return -EINVAL;
1050 if (tuple->TupleDataLen < ETH_ALEN + 2) {
1051 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1052 "LAN_NODE_ID\n");
1053 return -EINVAL;
1054 }
1055
1056 if (tuple->TupleData[1] != ETH_ALEN) {
1057 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1058 return -EINVAL;
1059 }
1060 for (i = 0; i < 6; i++)
1061 dev->dev_addr[i] = tuple->TupleData[i+2];
1062 return 0;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001063}
Dominik Brodowski91284222009-10-18 23:32:33 +02001064
1065/**
1066 * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
1067 * @p_dev: the struct pcmcia_device for which we want the address.
1068 * @dev: a properly prepared struct net_device to store the info to.
1069 *
1070 * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
1071 * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
1072 * must be set up properly by the driver (see examples!).
1073 */
1074int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1075{
1076 return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +01001077}
Dominik Brodowski91284222009-10-18 23:32:33 +02001078EXPORT_SYMBOL(pcmcia_get_mac_from_cis);
1079