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