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> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
1a8d4663 | 25 | |
6f0f38c4 DB |
26 | #include <asm/irq.h> |
27 | ||
1a8d4663 DB |
28 | #include <pcmcia/ss.h> |
29 | #include <pcmcia/cs.h> | |
1a8d4663 DB |
30 | #include <pcmcia/cistpl.h> |
31 | #include <pcmcia/cisreg.h> | |
32 | #include <pcmcia/ds.h> | |
33 | ||
34 | #include "cs_internal.h" | |
1a8d4663 DB |
35 | |
36 | ||
1a8d4663 | 37 | /* Access speed for IO windows */ |
9fea84f4 | 38 | static int io_speed; |
1a8d4663 DB |
39 | module_param(io_speed, int, 0444); |
40 | ||
41 | ||
a3ac9af5 DB |
42 | int pcmcia_validate_mem(struct pcmcia_socket *s) |
43 | { | |
44 | if (s->resource_ops->validate_mem) | |
45 | return s->resource_ops->validate_mem(s); | |
46 | /* if there is no callback, we can assume that everything is OK */ | |
47 | return 0; | |
48 | } | |
49 | ||
50 | struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align, | |
51 | int low, struct pcmcia_socket *s) | |
52 | { | |
53 | if (s->resource_ops->find_mem) | |
54 | return s->resource_ops->find_mem(base, num, align, low, s); | |
55 | return NULL; | |
56 | } | |
57 | ||
1a8d4663 | 58 | |
1a8d4663 DB |
59 | /** alloc_io_space |
60 | * | |
61 | * Special stuff for managing IO windows, because they are scarce | |
62 | */ | |
63 | ||
ecb8a847 OJ |
64 | static int alloc_io_space(struct pcmcia_socket *s, u_int attr, |
65 | unsigned int *base, unsigned int num, u_int lines) | |
1a8d4663 | 66 | { |
b19a7275 | 67 | unsigned int align; |
1a8d4663 DB |
68 | |
69 | align = (*base) ? (lines ? 1<<lines : 0) : 1; | |
70 | if (align && (align < num)) { | |
71 | if (*base) { | |
d50dbec3 | 72 | dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n", |
1a8d4663 DB |
73 | num, align); |
74 | align = 0; | |
75 | } else | |
9fea84f4 DB |
76 | while (align && (align < num)) |
77 | align <<= 1; | |
1a8d4663 DB |
78 | } |
79 | if (*base & ~(align-1)) { | |
d50dbec3 | 80 | dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n", |
1a8d4663 DB |
81 | *base, align); |
82 | align = 0; | |
83 | } | |
b19a7275 DB |
84 | |
85 | return s->resource_ops->find_io(s, attr, base, num, align); | |
1a8d4663 DB |
86 | } /* alloc_io_space */ |
87 | ||
88 | ||
ecb8a847 OJ |
89 | static void release_io_space(struct pcmcia_socket *s, unsigned int base, |
90 | unsigned int num) | |
1a8d4663 DB |
91 | { |
92 | int i; | |
93 | ||
94 | for (i = 0; i < MAX_IO_WIN; i++) { | |
c7d00693 DB |
95 | if (!s->io[i].res) |
96 | continue; | |
97 | if ((s->io[i].res->start <= base) && | |
98 | (s->io[i].res->end >= base+num-1)) { | |
1a8d4663 DB |
99 | s->io[i].InUse -= num; |
100 | /* Free the window if no one else is using it */ | |
101 | if (s->io[i].InUse == 0) { | |
1a8d4663 DB |
102 | release_resource(s->io[i].res); |
103 | kfree(s->io[i].res); | |
104 | s->io[i].res = NULL; | |
105 | } | |
106 | } | |
107 | } | |
108 | } /* release_io_space */ | |
109 | ||
110 | ||
1d5cc192 DB |
111 | /** |
112 | * pcmcia_access_config() - read or write card configuration registers | |
1a8d4663 | 113 | * |
1d5cc192 DB |
114 | * pcmcia_access_config() reads and writes configuration registers in |
115 | * attribute memory. Memory window 0 is reserved for this and the tuple | |
116 | * reading services. Drivers must use pcmcia_read_config_byte() or | |
117 | * pcmcia_write_config_byte(). | |
1a8d4663 | 118 | */ |
1d5cc192 DB |
119 | static int pcmcia_access_config(struct pcmcia_device *p_dev, |
120 | off_t where, u8 *val, | |
121 | int (*accessf) (struct pcmcia_socket *s, | |
122 | int attr, unsigned int addr, | |
123 | unsigned int len, void *ptr)) | |
1a8d4663 | 124 | { |
855cdf13 | 125 | struct pcmcia_socket *s; |
1a8d4663 DB |
126 | config_t *c; |
127 | int addr; | |
059f667d | 128 | int ret = 0; |
1a8d4663 | 129 | |
855cdf13 | 130 | s = p_dev->socket; |
94a819f8 DB |
131 | |
132 | mutex_lock(&s->ops_mutex); | |
855cdf13 | 133 | c = p_dev->function_config; |
1a8d4663 | 134 | |
6d9a299f DB |
135 | if (!(c->state & CONFIG_LOCKED)) { |
136 | dev_dbg(&s->dev, "Configuration isnt't locked\n"); | |
94a819f8 | 137 | mutex_unlock(&s->ops_mutex); |
943f70f1 | 138 | return -EACCES; |
6d9a299f | 139 | } |
1a8d4663 | 140 | |
1d5cc192 DB |
141 | addr = (c->ConfigBase + where) >> 1; |
142 | ||
143 | ret = accessf(s, 1, addr, 1, val); | |
144 | ||
059f667d | 145 | mutex_unlock(&s->ops_mutex); |
1d5cc192 | 146 | |
059f667d | 147 | return ret; |
1d5cc192 DB |
148 | } /* pcmcia_access_config */ |
149 | ||
150 | ||
151 | /** | |
152 | * pcmcia_read_config_byte() - read a byte from a card configuration register | |
153 | * | |
154 | * pcmcia_read_config_byte() reads a byte from a configuration register in | |
155 | * attribute memory. | |
156 | */ | |
157 | int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val) | |
158 | { | |
159 | return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem); | |
160 | } | |
161 | EXPORT_SYMBOL(pcmcia_read_config_byte); | |
162 | ||
163 | ||
164 | /** | |
165 | * pcmcia_write_config_byte() - write a byte to a card configuration register | |
166 | * | |
167 | * pcmcia_write_config_byte() writes a byte to a configuration register in | |
168 | * attribute memory. | |
169 | */ | |
170 | int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val) | |
171 | { | |
172 | return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem); | |
173 | } | |
174 | EXPORT_SYMBOL(pcmcia_write_config_byte); | |
3448139b | 175 | |
1a8d4663 | 176 | |
868575d1 MD |
177 | int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh, |
178 | memreq_t *req) | |
1a8d4663 | 179 | { |
0bdf9b3d | 180 | struct pcmcia_socket *s = p_dev->socket; |
6b8e087b | 181 | int ret; |
868575d1 | 182 | |
0bdf9b3d MD |
183 | wh--; |
184 | if (wh >= MAX_WIN) | |
ffb8da20 | 185 | return -EINVAL; |
610e2374 | 186 | if (req->Page != 0) { |
d50dbec3 | 187 | dev_dbg(&s->dev, "failure: requested page is zero\n"); |
610e2374 DB |
188 | return -EINVAL; |
189 | } | |
6b8e087b | 190 | mutex_lock(&s->ops_mutex); |
82f88e36 | 191 | s->win[wh].card_start = req->CardOffset; |
6b8e087b DB |
192 | ret = s->ops->set_mem_map(s, &s->win[wh]); |
193 | if (ret) | |
194 | dev_warn(&s->dev, "failed to set_mem_map\n"); | |
195 | mutex_unlock(&s->ops_mutex); | |
196 | return ret; | |
1a8d4663 DB |
197 | } /* pcmcia_map_mem_page */ |
198 | EXPORT_SYMBOL(pcmcia_map_mem_page); | |
199 | ||
200 | ||
201 | /** pcmcia_modify_configuration | |
202 | * | |
203 | * Modify a locked socket configuration | |
204 | */ | |
2bc5a9bd | 205 | int pcmcia_modify_configuration(struct pcmcia_device *p_dev, |
1a8d4663 DB |
206 | modconf_t *mod) |
207 | { | |
208 | struct pcmcia_socket *s; | |
209 | config_t *c; | |
4e06e240 | 210 | int ret; |
1a8d4663 | 211 | |
2bc5a9bd | 212 | s = p_dev->socket; |
94a819f8 DB |
213 | |
214 | mutex_lock(&s->ops_mutex); | |
dbb22f0d DB |
215 | c = p_dev->function_config; |
216 | ||
6d9a299f DB |
217 | if (!(s->state & SOCKET_PRESENT)) { |
218 | dev_dbg(&s->dev, "No card present\n"); | |
4e06e240 JS |
219 | ret = -ENODEV; |
220 | goto unlock; | |
6d9a299f DB |
221 | } |
222 | if (!(c->state & CONFIG_LOCKED)) { | |
223 | dev_dbg(&s->dev, "Configuration isnt't locked\n"); | |
4e06e240 JS |
224 | ret = -EACCES; |
225 | goto unlock; | |
6d9a299f | 226 | } |
1a8d4663 | 227 | |
0cb3c49c DB |
228 | if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) { |
229 | dev_dbg(&s->dev, | |
230 | "changing Vcc or IRQ is not allowed at this time\n"); | |
4e06e240 JS |
231 | ret = -EINVAL; |
232 | goto unlock; | |
d8b0a49d | 233 | } |
1a8d4663 DB |
234 | |
235 | /* We only allow changing Vpp1 and Vpp2 to the same value */ | |
236 | if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) && | |
237 | (mod->Attributes & CONF_VPP2_CHANGE_VALID)) { | |
60df3de8 | 238 | if (mod->Vpp1 != mod->Vpp2) { |
d50dbec3 | 239 | dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n"); |
4e06e240 JS |
240 | ret = -EINVAL; |
241 | goto unlock; | |
60df3de8 | 242 | } |
71ed90d8 | 243 | s->socket.Vpp = mod->Vpp1; |
d8b0a49d DB |
244 | if (s->ops->set_socket(s, &s->socket)) { |
245 | dev_printk(KERN_WARNING, &s->dev, | |
246 | "Unable to set VPP\n"); | |
4e06e240 JS |
247 | ret = -EIO; |
248 | goto unlock; | |
d8b0a49d | 249 | } |
1a8d4663 | 250 | } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) || |
d8b0a49d | 251 | (mod->Attributes & CONF_VPP2_CHANGE_VALID)) { |
d50dbec3 | 252 | dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n"); |
4e06e240 JS |
253 | ret = -EINVAL; |
254 | goto unlock; | |
d8b0a49d | 255 | } |
1a8d4663 | 256 | |
4bbed523 DB |
257 | if (mod->Attributes & CONF_IO_CHANGE_WIDTH) { |
258 | pccard_io_map io_off = { 0, 0, 0, 0, 1 }; | |
259 | pccard_io_map io_on; | |
260 | int i; | |
261 | ||
262 | io_on.speed = io_speed; | |
263 | for (i = 0; i < MAX_IO_WIN; i++) { | |
264 | if (!s->io[i].res) | |
265 | continue; | |
266 | io_off.map = i; | |
267 | io_on.map = i; | |
268 | ||
269 | io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8; | |
270 | io_on.start = s->io[i].res->start; | |
271 | io_on.stop = s->io[i].res->end; | |
272 | ||
273 | s->ops->set_io_map(s, &io_off); | |
274 | mdelay(40); | |
275 | s->ops->set_io_map(s, &io_on); | |
276 | } | |
277 | } | |
4e06e240 JS |
278 | ret = 0; |
279 | unlock: | |
94a819f8 | 280 | mutex_unlock(&s->ops_mutex); |
4bbed523 | 281 | |
4e06e240 | 282 | return ret; |
1a8d4663 DB |
283 | } /* modify_configuration */ |
284 | EXPORT_SYMBOL(pcmcia_modify_configuration); | |
285 | ||
286 | ||
2bc5a9bd | 287 | int pcmcia_release_configuration(struct pcmcia_device *p_dev) |
1a8d4663 DB |
288 | { |
289 | pccard_io_map io = { 0, 0, 0, 0, 1 }; | |
2bc5a9bd | 290 | struct pcmcia_socket *s = p_dev->socket; |
94a819f8 | 291 | config_t *c; |
1a8d4663 DB |
292 | int i; |
293 | ||
9e86749c | 294 | mutex_lock(&s->ops_mutex); |
94a819f8 | 295 | c = p_dev->function_config; |
e2d40963 DB |
296 | if (p_dev->_locked) { |
297 | p_dev->_locked = 0; | |
1a8d4663 DB |
298 | if (--(s->lock_count) == 0) { |
299 | s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */ | |
300 | s->socket.Vpp = 0; | |
301 | s->socket.io_irq = 0; | |
302 | s->ops->set_socket(s, &s->socket); | |
303 | } | |
5f2a71fc DB |
304 | } |
305 | if (c->state & CONFIG_LOCKED) { | |
306 | c->state &= ~CONFIG_LOCKED; | |
1a8d4663 DB |
307 | if (c->state & CONFIG_IO_REQ) |
308 | for (i = 0; i < MAX_IO_WIN; i++) { | |
c7d00693 | 309 | if (!s->io[i].res) |
1a8d4663 DB |
310 | continue; |
311 | s->io[i].Config--; | |
312 | if (s->io[i].Config != 0) | |
313 | continue; | |
314 | io.map = i; | |
315 | s->ops->set_io_map(s, &io); | |
316 | } | |
1a8d4663 | 317 | } |
9e86749c | 318 | mutex_unlock(&s->ops_mutex); |
1a8d4663 | 319 | |
4c89e88b | 320 | return 0; |
1a8d4663 | 321 | } /* pcmcia_release_configuration */ |
1a8d4663 DB |
322 | |
323 | ||
324 | /** pcmcia_release_io | |
325 | * | |
326 | * Release_io() releases the I/O ranges allocated by a client. This | |
327 | * may be invoked some time after a card ejection has already dumped | |
328 | * the actual socket configuration, so if the client is "stale", we | |
329 | * don't bother checking the port ranges against the current socket | |
330 | * values. | |
331 | */ | |
b4c88400 | 332 | static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req) |
1a8d4663 | 333 | { |
2bc5a9bd | 334 | struct pcmcia_socket *s = p_dev->socket; |
94a819f8 DB |
335 | int ret = -EINVAL; |
336 | config_t *c; | |
337 | ||
338 | mutex_lock(&s->ops_mutex); | |
339 | c = p_dev->function_config; | |
1a8d4663 | 340 | |
9fea84f4 | 341 | if (!p_dev->_io) |
94a819f8 | 342 | goto out; |
5f2a71fc | 343 | |
e2d40963 | 344 | p_dev->_io = 0; |
1a8d4663 | 345 | |
5f2a71fc DB |
346 | if ((c->io.BasePort1 != req->BasePort1) || |
347 | (c->io.NumPorts1 != req->NumPorts1) || | |
348 | (c->io.BasePort2 != req->BasePort2) || | |
349 | (c->io.NumPorts2 != req->NumPorts2)) | |
94a819f8 | 350 | goto out; |
5f2a71fc DB |
351 | |
352 | c->state &= ~CONFIG_IO_REQ; | |
1a8d4663 DB |
353 | |
354 | release_io_space(s, req->BasePort1, req->NumPorts1); | |
355 | if (req->NumPorts2) | |
356 | release_io_space(s, req->BasePort2, req->NumPorts2); | |
357 | ||
94a819f8 DB |
358 | out: |
359 | mutex_unlock(&s->ops_mutex); | |
360 | ||
361 | return ret; | |
1a8d4663 | 362 | } /* pcmcia_release_io */ |
1a8d4663 DB |
363 | |
364 | ||
f5560da5 | 365 | int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh) |
1a8d4663 | 366 | { |
0bdf9b3d | 367 | struct pcmcia_socket *s = p_dev->socket; |
82f88e36 | 368 | pccard_mem_map *win; |
1a8d4663 | 369 | |
0bdf9b3d MD |
370 | wh--; |
371 | if (wh >= MAX_WIN) | |
ffb8da20 | 372 | return -EINVAL; |
0bdf9b3d | 373 | |
6b8e087b | 374 | mutex_lock(&s->ops_mutex); |
0bdf9b3d MD |
375 | win = &s->win[wh]; |
376 | ||
377 | if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) { | |
6d9a299f | 378 | dev_dbg(&s->dev, "not releasing unknown window\n"); |
6b8e087b | 379 | mutex_unlock(&s->ops_mutex); |
ffb8da20 | 380 | return -EINVAL; |
6d9a299f | 381 | } |
1a8d4663 DB |
382 | |
383 | /* Shut down memory window */ | |
82f88e36 DB |
384 | win->flags &= ~MAP_ACTIVE; |
385 | s->ops->set_mem_map(s, win); | |
0bdf9b3d | 386 | s->state &= ~SOCKET_WIN_REQ(wh); |
1a8d4663 DB |
387 | |
388 | /* Release system memory */ | |
82f88e36 DB |
389 | if (win->res) { |
390 | release_resource(win->res); | |
391 | kfree(win->res); | |
392 | win->res = NULL; | |
1a8d4663 | 393 | } |
0bdf9b3d | 394 | p_dev->_win &= ~CLIENT_WIN_REQ(wh); |
6b8e087b | 395 | mutex_unlock(&s->ops_mutex); |
1a8d4663 | 396 | |
4c89e88b | 397 | return 0; |
1a8d4663 DB |
398 | } /* pcmcia_release_window */ |
399 | EXPORT_SYMBOL(pcmcia_release_window); | |
400 | ||
401 | ||
2bc5a9bd | 402 | int pcmcia_request_configuration(struct pcmcia_device *p_dev, |
1a8d4663 DB |
403 | config_req_t *req) |
404 | { | |
405 | int i; | |
406 | u_int base; | |
2bc5a9bd | 407 | struct pcmcia_socket *s = p_dev->socket; |
1a8d4663 DB |
408 | config_t *c; |
409 | pccard_io_map iomap; | |
410 | ||
1a8d4663 | 411 | if (!(s->state & SOCKET_PRESENT)) |
d598de02 | 412 | return -ENODEV; |
1a8d4663 | 413 | |
de6405e9 | 414 | if (req->IntType & INT_CARDBUS) { |
d50dbec3 | 415 | dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n"); |
de6405e9 DB |
416 | return -EINVAL; |
417 | } | |
94a819f8 DB |
418 | |
419 | mutex_lock(&s->ops_mutex); | |
dbb22f0d | 420 | c = p_dev->function_config; |
6d9a299f | 421 | if (c->state & CONFIG_LOCKED) { |
94a819f8 | 422 | mutex_unlock(&s->ops_mutex); |
6d9a299f | 423 | dev_dbg(&s->dev, "Configuration is locked\n"); |
943f70f1 | 424 | return -EACCES; |
6d9a299f | 425 | } |
1a8d4663 DB |
426 | |
427 | /* Do power control. We don't allow changes in Vcc. */ | |
70294b46 | 428 | s->socket.Vpp = req->Vpp; |
d8b0a49d | 429 | if (s->ops->set_socket(s, &s->socket)) { |
9e86749c | 430 | mutex_unlock(&s->ops_mutex); |
d8b0a49d DB |
431 | dev_printk(KERN_WARNING, &s->dev, |
432 | "Unable to set socket state\n"); | |
433 | return -EINVAL; | |
434 | } | |
1a8d4663 | 435 | |
1a8d4663 DB |
436 | /* Pick memory or I/O card, DMA mode, interrupt */ |
437 | c->IntType = req->IntType; | |
438 | c->Attributes = req->Attributes; | |
439 | if (req->IntType & INT_MEMORY_AND_IO) | |
440 | s->socket.flags |= SS_IOCARD; | |
441 | if (req->IntType & INT_ZOOMED_VIDEO) | |
442 | s->socket.flags |= SS_ZVCARD | SS_IOCARD; | |
443 | if (req->Attributes & CONF_ENABLE_DMA) | |
444 | s->socket.flags |= SS_DMA_MODE; | |
445 | if (req->Attributes & CONF_ENABLE_SPKR) | |
446 | s->socket.flags |= SS_SPKR_ENA; | |
447 | if (req->Attributes & CONF_ENABLE_IRQ) | |
6f840afb | 448 | s->socket.io_irq = s->pcmcia_irq; |
1a8d4663 DB |
449 | else |
450 | s->socket.io_irq = 0; | |
451 | s->ops->set_socket(s, &s->socket); | |
452 | s->lock_count++; | |
453 | ||
454 | /* Set up CIS configuration registers */ | |
455 | base = c->ConfigBase = req->ConfigBase; | |
1ae9c7d8 | 456 | c->CardValues = req->Present; |
1a8d4663 DB |
457 | if (req->Present & PRESENT_COPY) { |
458 | c->Copy = req->Copy; | |
459 | pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy); | |
460 | } | |
461 | if (req->Present & PRESENT_OPTION) { | |
462 | if (s->functions == 1) { | |
463 | c->Option = req->ConfigIndex & COR_CONFIG_MASK; | |
464 | } else { | |
465 | c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK; | |
466 | c->Option |= COR_FUNC_ENA|COR_IREQ_ENA; | |
467 | if (req->Present & PRESENT_IOBASE_0) | |
468 | c->Option |= COR_ADDR_DECODE; | |
469 | } | |
a7debe78 DB |
470 | if ((req->Attributes & CONF_ENABLE_IRQ) && |
471 | !(req->Attributes & CONF_ENABLE_PULSE_IRQ)) | |
472 | c->Option |= COR_LEVEL_REQ; | |
1a8d4663 DB |
473 | pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option); |
474 | mdelay(40); | |
475 | } | |
476 | if (req->Present & PRESENT_STATUS) { | |
477 | c->Status = req->Status; | |
478 | pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status); | |
479 | } | |
480 | if (req->Present & PRESENT_PIN_REPLACE) { | |
481 | c->Pin = req->Pin; | |
482 | pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin); | |
483 | } | |
484 | if (req->Present & PRESENT_EXT_STATUS) { | |
485 | c->ExtStatus = req->ExtStatus; | |
486 | pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus); | |
487 | } | |
488 | if (req->Present & PRESENT_IOBASE_0) { | |
489 | u_char b = c->io.BasePort1 & 0xff; | |
490 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b); | |
491 | b = (c->io.BasePort1 >> 8) & 0xff; | |
492 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b); | |
493 | } | |
494 | if (req->Present & PRESENT_IOSIZE) { | |
495 | u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1; | |
496 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b); | |
497 | } | |
498 | ||
499 | /* Configure I/O windows */ | |
500 | if (c->state & CONFIG_IO_REQ) { | |
501 | iomap.speed = io_speed; | |
502 | for (i = 0; i < MAX_IO_WIN; i++) | |
c7d00693 | 503 | if (s->io[i].res) { |
1a8d4663 DB |
504 | iomap.map = i; |
505 | iomap.flags = MAP_ACTIVE; | |
c7d00693 | 506 | switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) { |
1a8d4663 DB |
507 | case IO_DATA_PATH_WIDTH_16: |
508 | iomap.flags |= MAP_16BIT; break; | |
509 | case IO_DATA_PATH_WIDTH_AUTO: | |
510 | iomap.flags |= MAP_AUTOSZ; break; | |
511 | default: | |
512 | break; | |
513 | } | |
c7d00693 DB |
514 | iomap.start = s->io[i].res->start; |
515 | iomap.stop = s->io[i].res->end; | |
1a8d4663 DB |
516 | s->ops->set_io_map(s, &iomap); |
517 | s->io[i].Config++; | |
518 | } | |
519 | } | |
520 | ||
521 | c->state |= CONFIG_LOCKED; | |
e2d40963 | 522 | p_dev->_locked = 1; |
059f667d | 523 | mutex_unlock(&s->ops_mutex); |
4c89e88b | 524 | return 0; |
1a8d4663 DB |
525 | } /* pcmcia_request_configuration */ |
526 | EXPORT_SYMBOL(pcmcia_request_configuration); | |
527 | ||
528 | ||
529 | /** pcmcia_request_io | |
530 | * | |
531 | * Request_io() reserves ranges of port addresses for a socket. | |
532 | * I have not implemented range sharing or alias addressing. | |
533 | */ | |
2bc5a9bd | 534 | int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req) |
1a8d4663 | 535 | { |
2bc5a9bd | 536 | struct pcmcia_socket *s = p_dev->socket; |
1a8d4663 | 537 | config_t *c; |
94a819f8 DB |
538 | int ret = -EINVAL; |
539 | ||
540 | mutex_lock(&s->ops_mutex); | |
1a8d4663 | 541 | |
6d9a299f DB |
542 | if (!(s->state & SOCKET_PRESENT)) { |
543 | dev_dbg(&s->dev, "No card present\n"); | |
94a819f8 | 544 | goto out; |
6d9a299f | 545 | } |
1a8d4663 | 546 | |
1a8d4663 | 547 | if (!req) |
94a819f8 DB |
548 | goto out; |
549 | ||
dbb22f0d | 550 | c = p_dev->function_config; |
6d9a299f DB |
551 | if (c->state & CONFIG_LOCKED) { |
552 | dev_dbg(&s->dev, "Configuration is locked\n"); | |
94a819f8 | 553 | goto out; |
6d9a299f | 554 | } |
f958095e | 555 | if (c->state & CONFIG_IO_REQ) { |
d50dbec3 | 556 | dev_dbg(&s->dev, "IO already configured\n"); |
94a819f8 | 557 | goto out; |
f958095e | 558 | } |
610e2374 | 559 | if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) { |
d50dbec3 | 560 | dev_dbg(&s->dev, "bad attribute setting for IO region 1\n"); |
94a819f8 | 561 | goto out; |
610e2374 | 562 | } |
1a8d4663 | 563 | if ((req->NumPorts2 > 0) && |
610e2374 | 564 | (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) { |
d50dbec3 | 565 | dev_dbg(&s->dev, "bad attribute setting for IO region 2\n"); |
94a819f8 | 566 | goto out; |
610e2374 | 567 | } |
1a8d4663 | 568 | |
d50dbec3 | 569 | dev_dbg(&s->dev, "trying to allocate resource 1\n"); |
94a819f8 DB |
570 | ret = alloc_io_space(s, req->Attributes1, &req->BasePort1, |
571 | req->NumPorts1, req->IOAddrLines); | |
572 | if (ret) { | |
d50dbec3 | 573 | dev_dbg(&s->dev, "allocation of resource 1 failed\n"); |
94a819f8 | 574 | goto out; |
f958095e | 575 | } |
1a8d4663 DB |
576 | |
577 | if (req->NumPorts2) { | |
d50dbec3 | 578 | dev_dbg(&s->dev, "trying to allocate resource 2\n"); |
94a819f8 DB |
579 | ret = alloc_io_space(s, req->Attributes2, &req->BasePort2, |
580 | req->NumPorts2, req->IOAddrLines); | |
581 | if (ret) { | |
d50dbec3 | 582 | dev_dbg(&s->dev, "allocation of resource 2 failed\n"); |
1a8d4663 | 583 | release_io_space(s, req->BasePort1, req->NumPorts1); |
94a819f8 | 584 | goto out; |
1a8d4663 DB |
585 | } |
586 | } | |
587 | ||
588 | c->io = *req; | |
589 | c->state |= CONFIG_IO_REQ; | |
e2d40963 | 590 | p_dev->_io = 1; |
94a819f8 DB |
591 | dev_dbg(&s->dev, "allocating resources succeeded: %d\n", ret); |
592 | ||
593 | out: | |
594 | mutex_unlock(&s->ops_mutex); | |
595 | ||
596 | return ret; | |
1a8d4663 DB |
597 | } /* pcmcia_request_io */ |
598 | EXPORT_SYMBOL(pcmcia_request_io); | |
599 | ||
600 | ||
eb14120f DB |
601 | /** |
602 | * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device | |
1a8d4663 | 603 | * |
eb14120f DB |
604 | * pcmcia_request_irq() is a wrapper around request_irq which will allow |
605 | * the PCMCIA core to clean up the registration in pcmcia_disable_device(). | |
606 | * Drivers are free to use request_irq() directly, but then they need to | |
607 | * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ | |
608 | * handlers are allowed. | |
1a8d4663 | 609 | */ |
eb14120f DB |
610 | int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev, |
611 | irq_handler_t handler) | |
1a8d4663 | 612 | { |
eb14120f | 613 | int ret; |
1a8d4663 | 614 | |
eb14120f DB |
615 | if (!p_dev->irq) |
616 | return -EINVAL; | |
94a819f8 | 617 | |
eb14120f DB |
618 | ret = request_irq(p_dev->irq, handler, IRQF_SHARED, |
619 | p_dev->devname, p_dev->priv); | |
620 | if (!ret) | |
621 | p_dev->_irq = 1; | |
1a8d4663 | 622 | |
eb14120f DB |
623 | return ret; |
624 | } | |
625 | EXPORT_SYMBOL(pcmcia_request_irq); | |
6f0f38c4 | 626 | |
1a8d4663 | 627 | |
eb14120f DB |
628 | /** |
629 | * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first | |
630 | * | |
631 | * pcmcia_request_exclusive_irq() is a wrapper around request_irq which | |
632 | * attempts first to request an exclusive IRQ. If it fails, it also accepts | |
633 | * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for | |
634 | * IRQ sharing and either use request_irq directly (then they need to call | |
635 | * free_irq themselves, too), or the pcmcia_request_irq() function. | |
636 | */ | |
637 | int __must_check | |
b19a7275 DB |
638 | __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev, |
639 | irq_handler_t handler) | |
eb14120f DB |
640 | { |
641 | int ret; | |
1a8d4663 | 642 | |
eb14120f DB |
643 | if (!p_dev->irq) |
644 | return -EINVAL; | |
1a8d4663 | 645 | |
eb14120f DB |
646 | ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv); |
647 | if (ret) { | |
648 | ret = pcmcia_request_irq(p_dev, handler); | |
649 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: " | |
650 | "request for exclusive IRQ could not be fulfilled.\n"); | |
651 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver " | |
652 | "needs updating to supported shared IRQ lines.\n"); | |
653 | } | |
654 | if (ret) | |
655 | dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n"); | |
656 | else | |
657 | p_dev->_irq = 1; | |
1a8d4663 | 658 | |
94a819f8 | 659 | return ret; |
eb14120f | 660 | } /* pcmcia_request_exclusive_irq */ |
b19a7275 | 661 | EXPORT_SYMBOL(__pcmcia_request_exclusive_irq); |
1a8d4663 DB |
662 | |
663 | ||
6f0f38c4 DB |
664 | #ifdef CONFIG_PCMCIA_PROBE |
665 | ||
666 | /* mask of IRQs already reserved by other cards, we should avoid using them */ | |
667 | static u8 pcmcia_used_irq[NR_IRQS]; | |
668 | ||
669 | static irqreturn_t test_action(int cpl, void *dev_id) | |
670 | { | |
671 | return IRQ_NONE; | |
672 | } | |
673 | ||
674 | /** | |
675 | * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used | |
676 | * @p_dev - the associated PCMCIA device | |
677 | * | |
678 | * locking note: must be called with ops_mutex locked. | |
679 | */ | |
680 | static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type) | |
681 | { | |
682 | struct pcmcia_socket *s = p_dev->socket; | |
683 | unsigned int try, irq; | |
684 | u32 mask = s->irq_mask; | |
685 | int ret = -ENODEV; | |
686 | ||
687 | for (try = 0; try < 64; try++) { | |
688 | irq = try % 32; | |
689 | ||
690 | /* marked as available by driver, not blocked by userspace? */ | |
691 | if (!((mask >> irq) & 1)) | |
692 | continue; | |
693 | ||
694 | /* avoid an IRQ which is already used by another PCMCIA card */ | |
695 | if ((try < 32) && pcmcia_used_irq[irq]) | |
696 | continue; | |
697 | ||
698 | /* register the correct driver, if possible, to check whether | |
699 | * registering a dummy handle works, i.e. if the IRQ isn't | |
700 | * marked as used by the kernel resource management core */ | |
701 | ret = request_irq(irq, test_action, type, p_dev->devname, | |
702 | p_dev); | |
703 | if (!ret) { | |
704 | free_irq(irq, p_dev); | |
eb14120f | 705 | p_dev->irq = s->pcmcia_irq = irq; |
6f0f38c4 DB |
706 | pcmcia_used_irq[irq]++; |
707 | break; | |
708 | } | |
709 | } | |
710 | ||
711 | return ret; | |
712 | } | |
713 | ||
714 | void pcmcia_cleanup_irq(struct pcmcia_socket *s) | |
715 | { | |
6f840afb DB |
716 | pcmcia_used_irq[s->pcmcia_irq]--; |
717 | s->pcmcia_irq = 0; | |
6f0f38c4 DB |
718 | } |
719 | ||
720 | #else /* CONFIG_PCMCIA_PROBE */ | |
721 | ||
722 | static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type) | |
723 | { | |
724 | return -EINVAL; | |
725 | } | |
726 | ||
727 | void pcmcia_cleanup_irq(struct pcmcia_socket *s) | |
728 | { | |
6f840afb | 729 | s->pcmcia_irq = 0; |
6f0f38c4 DB |
730 | return; |
731 | } | |
732 | ||
733 | #endif /* CONFIG_PCMCIA_PROBE */ | |
734 | ||
735 | ||
736 | /** | |
737 | * pcmcia_setup_irq() - determine IRQ to be used for device | |
738 | * @p_dev - the associated PCMCIA device | |
739 | * | |
740 | * locking note: must be called with ops_mutex locked. | |
741 | */ | |
742 | int pcmcia_setup_irq(struct pcmcia_device *p_dev) | |
743 | { | |
744 | struct pcmcia_socket *s = p_dev->socket; | |
745 | ||
eb14120f | 746 | if (p_dev->irq) |
6f0f38c4 DB |
747 | return 0; |
748 | ||
749 | /* already assigned? */ | |
6f840afb | 750 | if (s->pcmcia_irq) { |
eb14120f | 751 | p_dev->irq = s->pcmcia_irq; |
6f0f38c4 DB |
752 | return 0; |
753 | } | |
754 | ||
755 | /* prefer an exclusive ISA irq */ | |
756 | if (!pcmcia_setup_isa_irq(p_dev, 0)) | |
757 | return 0; | |
758 | ||
759 | /* but accept a shared ISA irq */ | |
760 | if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED)) | |
761 | return 0; | |
762 | ||
763 | /* but use the PCI irq otherwise */ | |
764 | if (s->pci_irq) { | |
eb14120f | 765 | p_dev->irq = s->pcmcia_irq = s->pci_irq; |
6f0f38c4 DB |
766 | return 0; |
767 | } | |
768 | ||
769 | return -EINVAL; | |
770 | } | |
771 | ||
772 | ||
1a8d4663 DB |
773 | /** pcmcia_request_window |
774 | * | |
775 | * Request_window() establishes a mapping between card memory space | |
776 | * and system memory space. | |
777 | */ | |
6838b03f | 778 | int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh) |
1a8d4663 | 779 | { |
6838b03f | 780 | struct pcmcia_socket *s = p_dev->socket; |
82f88e36 | 781 | pccard_mem_map *win; |
1a8d4663 DB |
782 | u_long align; |
783 | int w; | |
784 | ||
6d9a299f DB |
785 | if (!(s->state & SOCKET_PRESENT)) { |
786 | dev_dbg(&s->dev, "No card present\n"); | |
3939c1ef | 787 | return -ENODEV; |
6d9a299f | 788 | } |
610e2374 | 789 | if (req->Attributes & (WIN_PAGED | WIN_SHARED)) { |
d50dbec3 | 790 | dev_dbg(&s->dev, "bad attribute setting for iomem region\n"); |
610e2374 DB |
791 | return -EINVAL; |
792 | } | |
1a8d4663 DB |
793 | |
794 | /* Window size defaults to smallest available */ | |
795 | if (req->Size == 0) | |
796 | req->Size = s->map_size; | |
797 | align = (((s->features & SS_CAP_MEM_ALIGN) || | |
798 | (req->Attributes & WIN_STRICT_ALIGN)) ? | |
799 | req->Size : s->map_size); | |
69ba4433 | 800 | if (req->Size & (s->map_size-1)) { |
d50dbec3 | 801 | dev_dbg(&s->dev, "invalid map size\n"); |
69ba4433 DB |
802 | return -EINVAL; |
803 | } | |
1a8d4663 | 804 | if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) || |
69ba4433 | 805 | (req->Base & (align-1))) { |
d50dbec3 | 806 | dev_dbg(&s->dev, "invalid base address\n"); |
69ba4433 DB |
807 | return -EINVAL; |
808 | } | |
1a8d4663 DB |
809 | if (req->Base) |
810 | align = 0; | |
811 | ||
812 | /* Allocate system memory window */ | |
813 | for (w = 0; w < MAX_WIN; w++) | |
9fea84f4 DB |
814 | if (!(s->state & SOCKET_WIN_REQ(w))) |
815 | break; | |
f958095e | 816 | if (w == MAX_WIN) { |
d50dbec3 | 817 | dev_dbg(&s->dev, "all windows are used already\n"); |
f958095e DB |
818 | return -EINVAL; |
819 | } | |
1a8d4663 | 820 | |
6b8e087b | 821 | mutex_lock(&s->ops_mutex); |
1a8d4663 | 822 | win = &s->win[w]; |
1a8d4663 DB |
823 | |
824 | if (!(s->features & SS_CAP_STATIC_MAP)) { | |
82f88e36 | 825 | win->res = pcmcia_find_mem_region(req->Base, req->Size, align, |
1a8d4663 | 826 | (req->Attributes & WIN_MAP_BELOW_1MB), s); |
82f88e36 | 827 | if (!win->res) { |
d50dbec3 | 828 | dev_dbg(&s->dev, "allocating mem region failed\n"); |
6b8e087b | 829 | mutex_unlock(&s->ops_mutex); |
f958095e DB |
830 | return -EINVAL; |
831 | } | |
1a8d4663 | 832 | } |
6838b03f | 833 | p_dev->_win |= CLIENT_WIN_REQ(w); |
1a8d4663 DB |
834 | |
835 | /* Configure the socket controller */ | |
82f88e36 DB |
836 | win->map = w+1; |
837 | win->flags = 0; | |
838 | win->speed = req->AccessSpeed; | |
1a8d4663 | 839 | if (req->Attributes & WIN_MEMORY_TYPE) |
82f88e36 | 840 | win->flags |= MAP_ATTRIB; |
1a8d4663 | 841 | if (req->Attributes & WIN_ENABLE) |
82f88e36 | 842 | win->flags |= MAP_ACTIVE; |
1a8d4663 | 843 | if (req->Attributes & WIN_DATA_WIDTH_16) |
82f88e36 | 844 | win->flags |= MAP_16BIT; |
1a8d4663 | 845 | if (req->Attributes & WIN_USE_WAIT) |
82f88e36 DB |
846 | win->flags |= MAP_USE_WAIT; |
847 | win->card_start = 0; | |
6b8e087b | 848 | |
82f88e36 | 849 | if (s->ops->set_mem_map(s, win) != 0) { |
d50dbec3 | 850 | dev_dbg(&s->dev, "failed to set memory mapping\n"); |
6b8e087b | 851 | mutex_unlock(&s->ops_mutex); |
926c5402 DB |
852 | return -EIO; |
853 | } | |
1a8d4663 DB |
854 | s->state |= SOCKET_WIN_REQ(w); |
855 | ||
856 | /* Return window handle */ | |
9fea84f4 | 857 | if (s->features & SS_CAP_STATIC_MAP) |
82f88e36 | 858 | req->Base = win->static_start; |
9fea84f4 | 859 | else |
82f88e36 | 860 | req->Base = win->res->start; |
9fea84f4 | 861 | |
6b8e087b | 862 | mutex_unlock(&s->ops_mutex); |
0bdf9b3d | 863 | *wh = w + 1; |
1a8d4663 | 864 | |
4c89e88b | 865 | return 0; |
1a8d4663 DB |
866 | } /* pcmcia_request_window */ |
867 | EXPORT_SYMBOL(pcmcia_request_window); | |
5f2a71fc | 868 | |
9fea84f4 DB |
869 | void pcmcia_disable_device(struct pcmcia_device *p_dev) |
870 | { | |
5f2a71fc | 871 | pcmcia_release_configuration(p_dev); |
fd238232 | 872 | pcmcia_release_io(p_dev, &p_dev->io); |
418c5278 | 873 | if (p_dev->_irq) { |
eb14120f | 874 | free_irq(p_dev->irq, p_dev->priv); |
418c5278 PM |
875 | p_dev->_irq = 0; |
876 | } | |
c1ac0228 | 877 | if (p_dev->win) |
f5560da5 | 878 | pcmcia_release_window(p_dev, p_dev->win); |
5f2a71fc DB |
879 | } |
880 | EXPORT_SYMBOL(pcmcia_disable_device); |