iwlwifi: fix skb usage after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / pcmcia_resource.c
blobd5db95644b64f69cc5dc61fc47f11a7abc21bc71
1 /*
2 * PCMCIA 16-bit resource management functions
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.
8 * Copyright (C) 1999 David A. Hinds
9 * Copyright (C) 2004-2005 Dominik Brodowski
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.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/pci.h>
22 #include <linux/device.h>
23 #include <linux/netdevice.h>
25 #include <pcmcia/cs_types.h>
26 #include <pcmcia/ss.h>
27 #include <pcmcia/cs.h>
28 #include <pcmcia/cistpl.h>
29 #include <pcmcia/cisreg.h>
30 #include <pcmcia/ds.h>
32 #include "cs_internal.h"
35 /* Access speed for IO windows */
36 static int io_speed;
37 module_param(io_speed, int, 0444);
40 #ifdef CONFIG_PCMCIA_PROBE
41 #include <asm/irq.h>
42 /* mask of IRQs already reserved by other cards, we should avoid using them */
43 static u8 pcmcia_used_irq[NR_IRQS];
44 #endif
47 /** alloc_io_space
49 * Special stuff for managing IO windows, because they are scarce
52 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
53 unsigned int *base, unsigned int num, u_int lines)
55 int i;
56 unsigned int try, align;
58 align = (*base) ? (lines ? 1<<lines : 0) : 1;
59 if (align && (align < num)) {
60 if (*base) {
61 dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
62 num, align);
63 align = 0;
64 } else
65 while (align && (align < num))
66 align <<= 1;
68 if (*base & ~(align-1)) {
69 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
70 *base, align);
71 align = 0;
73 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
74 *base = s->io_offset | (*base & 0x0fff);
75 return 0;
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.
81 for (i = 0; i < MAX_IO_WIN; i++)
82 if ((s->io[i].res) && *base &&
83 ((s->io[i].res->start & (align-1)) == *base))
84 return 1;
85 for (i = 0; i < MAX_IO_WIN; i++) {
86 if (!s->io[i].res) {
87 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
88 if (s->io[i].res) {
89 *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;
92 break;
93 } else
94 return 1;
95 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
96 continue;
97 /* Try to extend top of window */
98 try = s->io[i].res->end + 1;
99 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;
103 s->io[i].InUse += num;
104 break;
106 /* Try to extend bottom of window */
107 try = s->io[i].res->start - num;
108 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) {
111 *base = try;
112 s->io[i].InUse += num;
113 break;
116 return (i == MAX_IO_WIN);
117 } /* alloc_io_space */
120 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
121 unsigned int num)
123 int i;
125 for (i = 0; i < MAX_IO_WIN; i++) {
126 if (!s->io[i].res)
127 continue;
128 if ((s->io[i].res->start <= base) &&
129 (s->io[i].res->end >= base+num-1)) {
130 s->io[i].InUse -= num;
131 /* Free the window if no one else is using it */
132 if (s->io[i].InUse == 0) {
133 release_resource(s->io[i].res);
134 kfree(s->io[i].res);
135 s->io[i].res = NULL;
139 } /* release_io_space */
142 /** pccard_access_configuration_register
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.
149 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
150 conf_reg_t *reg)
152 struct pcmcia_socket *s;
153 config_t *c;
154 int addr;
155 u_char val;
157 if (!p_dev || !p_dev->function_config)
158 return -EINVAL;
160 s = p_dev->socket;
161 c = p_dev->function_config;
163 if (!(c->state & CONFIG_LOCKED)) {
164 dev_dbg(&s->dev, "Configuration isnt't locked\n");
165 return -EACCES;
168 addr = (c->ConfigBase + reg->Offset) >> 1;
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:
180 dev_dbg(&s->dev, "Invalid conf register request\n");
181 return -EINVAL;
182 break;
184 return 0;
185 } /* pcmcia_access_configuration_register */
186 EXPORT_SYMBOL(pcmcia_access_configuration_register);
189 int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
190 memreq_t *req)
192 struct pcmcia_socket *s = p_dev->socket;
194 wh--;
195 if (wh >= MAX_WIN)
196 return -EINVAL;
197 if (req->Page != 0) {
198 dev_dbg(&s->dev, "failure: requested page is zero\n");
199 return -EINVAL;
201 s->win[wh].card_start = req->CardOffset;
202 if (s->ops->set_mem_map(s, &s->win[wh]) != 0) {
203 dev_dbg(&s->dev, "failed to set_mem_map\n");
204 return -EIO;
206 return 0;
207 } /* pcmcia_map_mem_page */
208 EXPORT_SYMBOL(pcmcia_map_mem_page);
211 /** pcmcia_modify_configuration
213 * Modify a locked socket configuration
215 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
216 modconf_t *mod)
218 struct pcmcia_socket *s;
219 config_t *c;
221 s = p_dev->socket;
222 c = p_dev->function_config;
224 if (!(s->state & SOCKET_PRESENT)) {
225 dev_dbg(&s->dev, "No card present\n");
226 return -ENODEV;
228 if (!(c->state & CONFIG_LOCKED)) {
229 dev_dbg(&s->dev, "Configuration isnt't locked\n");
230 return -EACCES;
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;
241 s->ops->set_socket(s, &s->socket);
244 if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
245 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
246 return -EINVAL;
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)) {
252 if (mod->Vpp1 != mod->Vpp2) {
253 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
254 return -EINVAL;
256 s->socket.Vpp = mod->Vpp1;
257 if (s->ops->set_socket(s, &s->socket)) {
258 dev_printk(KERN_WARNING, &s->dev,
259 "Unable to set VPP\n");
260 return -EIO;
262 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
263 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
264 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
265 return -EINVAL;
268 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;
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;
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;
284 s->ops->set_io_map(s, &io_off);
285 mdelay(40);
286 s->ops->set_io_map(s, &io_on);
290 return 0;
291 } /* modify_configuration */
292 EXPORT_SYMBOL(pcmcia_modify_configuration);
295 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
297 pccard_io_map io = { 0, 0, 0, 0, 1 };
298 struct pcmcia_socket *s = p_dev->socket;
299 config_t *c = p_dev->function_config;
300 int i;
302 if (p_dev->_locked) {
303 p_dev->_locked = 0;
304 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);
311 if (c->state & CONFIG_LOCKED) {
312 c->state &= ~CONFIG_LOCKED;
313 if (c->state & CONFIG_IO_REQ)
314 for (i = 0; i < MAX_IO_WIN; i++) {
315 if (!s->io[i].res)
316 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);
325 return 0;
326 } /* pcmcia_release_configuration */
329 /** pcmcia_release_io
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.
337 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
339 struct pcmcia_socket *s = p_dev->socket;
340 config_t *c = p_dev->function_config;
342 if (!p_dev->_io)
343 return -EINVAL;
345 p_dev->_io = 0;
347 if ((c->io.BasePort1 != req->BasePort1) ||
348 (c->io.NumPorts1 != req->NumPorts1) ||
349 (c->io.BasePort2 != req->BasePort2) ||
350 (c->io.NumPorts2 != req->NumPorts2))
351 return -EINVAL;
353 c->state &= ~CONFIG_IO_REQ;
355 release_io_space(s, req->BasePort1, req->NumPorts1);
356 if (req->NumPorts2)
357 release_io_space(s, req->BasePort2, req->NumPorts2);
359 return 0;
360 } /* pcmcia_release_io */
363 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
365 struct pcmcia_socket *s = p_dev->socket;
366 config_t *c = p_dev->function_config;
368 if (!p_dev->_irq)
369 return -EINVAL;
370 p_dev->_irq = 0;
372 if (c->state & CONFIG_LOCKED)
373 return -EACCES;
374 if (c->irq.Attributes != req->Attributes) {
375 dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
376 return -EINVAL;
378 if (s->irq.AssignedIRQ != req->AssignedIRQ) {
379 dev_dbg(&s->dev, "IRQ must match assigned one\n");
380 return -EINVAL;
382 if (--s->irq.Config == 0) {
383 c->state &= ~CONFIG_IRQ_REQ;
384 s->irq.AssignedIRQ = 0;
387 if (req->Handler)
388 free_irq(req->AssignedIRQ, p_dev->priv);
390 #ifdef CONFIG_PCMCIA_PROBE
391 pcmcia_used_irq[req->AssignedIRQ]--;
392 #endif
394 return 0;
395 } /* pcmcia_release_irq */
398 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
400 struct pcmcia_socket *s = p_dev->socket;
401 pccard_mem_map *win;
403 wh--;
404 if (wh >= MAX_WIN)
405 return -EINVAL;
407 win = &s->win[wh];
409 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
410 dev_dbg(&s->dev, "not releasing unknown window\n");
411 return -EINVAL;
414 /* Shut down memory window */
415 win->flags &= ~MAP_ACTIVE;
416 s->ops->set_mem_map(s, win);
417 s->state &= ~SOCKET_WIN_REQ(wh);
419 /* Release system memory */
420 if (win->res) {
421 release_resource(win->res);
422 kfree(win->res);
423 win->res = NULL;
425 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
427 return 0;
428 } /* pcmcia_release_window */
429 EXPORT_SYMBOL(pcmcia_release_window);
432 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
433 config_req_t *req)
435 int i;
436 u_int base;
437 struct pcmcia_socket *s = p_dev->socket;
438 config_t *c;
439 pccard_io_map iomap;
441 if (!(s->state & SOCKET_PRESENT))
442 return -ENODEV;
444 if (req->IntType & INT_CARDBUS) {
445 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
446 return -EINVAL;
448 c = p_dev->function_config;
449 if (c->state & CONFIG_LOCKED) {
450 dev_dbg(&s->dev, "Configuration is locked\n");
451 return -EACCES;
454 /* Do power control. We don't allow changes in Vcc. */
455 s->socket.Vpp = req->Vpp;
456 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;
462 /* 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++;
480 /* Set up CIS configuration registers */
481 base = c->ConfigBase = req->ConfigBase;
482 c->CardValues = req->Present;
483 if (req->Present & PRESENT_COPY) {
484 c->Copy = req->Copy;
485 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
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;
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);
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);
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);
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);
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);
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);
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++)
529 if (s->io[i].res) {
530 iomap.map = i;
531 iomap.flags = MAP_ACTIVE;
532 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
533 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;
540 iomap.start = s->io[i].res->start;
541 iomap.stop = s->io[i].res->end;
542 s->ops->set_io_map(s, &iomap);
543 s->io[i].Config++;
547 c->state |= CONFIG_LOCKED;
548 p_dev->_locked = 1;
549 return 0;
550 } /* pcmcia_request_configuration */
551 EXPORT_SYMBOL(pcmcia_request_configuration);
554 /** pcmcia_request_io
556 * Request_io() reserves ranges of port addresses for a socket.
557 * I have not implemented range sharing or alias addressing.
559 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
561 struct pcmcia_socket *s = p_dev->socket;
562 config_t *c;
564 if (!(s->state & SOCKET_PRESENT)) {
565 dev_dbg(&s->dev, "No card present\n");
566 return -ENODEV;
569 if (!req)
570 return -EINVAL;
571 c = p_dev->function_config;
572 if (c->state & CONFIG_LOCKED) {
573 dev_dbg(&s->dev, "Configuration is locked\n");
574 return -EACCES;
576 if (c->state & CONFIG_IO_REQ) {
577 dev_dbg(&s->dev, "IO already configured\n");
578 return -EBUSY;
580 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
581 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
582 return -EINVAL;
584 if ((req->NumPorts2 > 0) &&
585 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
586 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
587 return -EINVAL;
590 dev_dbg(&s->dev, "trying to allocate resource 1\n");
591 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
592 req->NumPorts1, req->IOAddrLines)) {
593 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
594 return -EBUSY;
597 if (req->NumPorts2) {
598 dev_dbg(&s->dev, "trying to allocate resource 2\n");
599 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
600 req->NumPorts2, req->IOAddrLines)) {
601 dev_dbg(&s->dev, "allocation of resource 2 failed\n");
602 release_io_space(s, req->BasePort1, req->NumPorts1);
603 return -EBUSY;
607 c->io = *req;
608 c->state |= CONFIG_IO_REQ;
609 p_dev->_io = 1;
610 return 0;
611 } /* pcmcia_request_io */
612 EXPORT_SYMBOL(pcmcia_request_io);
615 /** pcmcia_request_irq
617 * Request_irq() reserves an irq for this client.
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.
625 #ifdef CONFIG_PCMCIA_PROBE
626 static irqreturn_t test_action(int cpl, void *dev_id)
628 return IRQ_NONE;
630 #endif
632 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
634 struct pcmcia_socket *s = p_dev->socket;
635 config_t *c;
636 int ret = -EINVAL, irq = 0;
637 int type;
639 if (!(s->state & SOCKET_PRESENT)) {
640 dev_dbg(&s->dev, "No card present\n");
641 return -ENODEV;
643 c = p_dev->function_config;
644 if (c->state & CONFIG_LOCKED) {
645 dev_dbg(&s->dev, "Configuration is locked\n");
646 return -EACCES;
648 if (c->state & CONFIG_IRQ_REQ) {
649 dev_dbg(&s->dev, "IRQ already configured\n");
650 return -EBUSY;
653 /* 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 */
656 type = IRQF_SHARED;
657 else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
658 type = IRQF_SHARED;
659 else
660 printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
662 #ifdef CONFIG_PCMCIA_PROBE
664 #ifdef IRQ_NOAUTOEN
665 /* if the underlying IRQ infrastructure allows for it, only allocate
666 * the IRQ, but do not enable it
668 if (!(req->Handler))
669 type |= IRQ_NOAUTOEN;
670 #endif /* IRQ_NOAUTOEN */
672 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;
678 void *data = p_dev; /* something unique to this device */
680 for (try = 0; try < 64; try++) {
681 irq = try % 32;
683 /* marked as available by driver, and not blocked by userspace? */
684 if (!((mask >> irq) & 1))
685 continue;
687 /* avoid an IRQ which is already used by a PCMCIA card */
688 if ((try < 32) && pcmcia_used_irq[irq])
689 continue;
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,
695 (req->Handler) ? req->Handler : test_action,
696 type,
697 p_dev->devname,
698 (req->Handler) ? p_dev->priv : data);
699 if (!ret) {
700 if (!req->Handler)
701 free_irq(irq, data);
702 break;
706 #endif
707 /* only assign PCI irq if no IRQ already assigned */
708 if (ret && !s->irq.AssignedIRQ) {
709 if (!s->pci_irq) {
710 dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
711 return ret;
713 type = IRQF_SHARED;
714 irq = s->pci_irq;
717 if (ret && req->Handler) {
718 ret = request_irq(irq, req->Handler, type,
719 p_dev->devname, p_dev->priv);
720 if (ret) {
721 dev_printk(KERN_INFO, &s->dev,
722 "request_irq() failed\n");
723 return ret;
727 /* Make sure the fact the request type was overridden is passed back */
728 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
729 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
730 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");
735 c->irq.Attributes = req->Attributes;
736 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
737 s->irq.Config++;
739 c->state |= CONFIG_IRQ_REQ;
740 p_dev->_irq = 1;
742 #ifdef CONFIG_PCMCIA_PROBE
743 pcmcia_used_irq[irq]++;
744 #endif
746 return 0;
747 } /* pcmcia_request_irq */
748 EXPORT_SYMBOL(pcmcia_request_irq);
751 /** pcmcia_request_window
753 * Request_window() establishes a mapping between card memory space
754 * and system memory space.
756 int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
758 struct pcmcia_socket *s = p_dev->socket;
759 pccard_mem_map *win;
760 u_long align;
761 int w;
763 if (!(s->state & SOCKET_PRESENT)) {
764 dev_dbg(&s->dev, "No card present\n");
765 return -ENODEV;
767 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
768 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
769 return -EINVAL;
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);
778 if (req->Size & (s->map_size-1)) {
779 dev_dbg(&s->dev, "invalid map size\n");
780 return -EINVAL;
782 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
783 (req->Base & (align-1))) {
784 dev_dbg(&s->dev, "invalid base address\n");
785 return -EINVAL;
787 if (req->Base)
788 align = 0;
790 /* Allocate system memory window */
791 for (w = 0; w < MAX_WIN; w++)
792 if (!(s->state & SOCKET_WIN_REQ(w)))
793 break;
794 if (w == MAX_WIN) {
795 dev_dbg(&s->dev, "all windows are used already\n");
796 return -EINVAL;
799 win = &s->win[w];
801 if (!(s->features & SS_CAP_STATIC_MAP)) {
802 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
803 (req->Attributes & WIN_MAP_BELOW_1MB), s);
804 if (!win->res) {
805 dev_dbg(&s->dev, "allocating mem region failed\n");
806 return -EINVAL;
809 p_dev->_win |= CLIENT_WIN_REQ(w);
811 /* Configure the socket controller */
812 win->map = w+1;
813 win->flags = 0;
814 win->speed = req->AccessSpeed;
815 if (req->Attributes & WIN_MEMORY_TYPE)
816 win->flags |= MAP_ATTRIB;
817 if (req->Attributes & WIN_ENABLE)
818 win->flags |= MAP_ACTIVE;
819 if (req->Attributes & WIN_DATA_WIDTH_16)
820 win->flags |= MAP_16BIT;
821 if (req->Attributes & WIN_USE_WAIT)
822 win->flags |= MAP_USE_WAIT;
823 win->card_start = 0;
824 if (s->ops->set_mem_map(s, win) != 0) {
825 dev_dbg(&s->dev, "failed to set memory mapping\n");
826 return -EIO;
828 s->state |= SOCKET_WIN_REQ(w);
830 /* Return window handle */
831 if (s->features & SS_CAP_STATIC_MAP)
832 req->Base = win->static_start;
833 else
834 req->Base = win->res->start;
836 *wh = w + 1;
838 return 0;
839 } /* pcmcia_request_window */
840 EXPORT_SYMBOL(pcmcia_request_window);
842 void pcmcia_disable_device(struct pcmcia_device *p_dev)
844 pcmcia_release_configuration(p_dev);
845 pcmcia_release_io(p_dev, &p_dev->io);
846 pcmcia_release_irq(p_dev, &p_dev->irq);
847 if (p_dev->win)
848 pcmcia_release_window(p_dev, p_dev->win);
850 EXPORT_SYMBOL(pcmcia_disable_device);
853 struct pcmcia_cfg_mem {
854 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);
861 cisparse_t parse;
862 cistpl_cftable_entry_t dflt;
866 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
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.
872 static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
874 cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
875 struct pcmcia_cfg_mem *cfg_mem = priv;
877 /* default values */
878 cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
879 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
880 cfg_mem->dflt = *cfg;
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);
888 * 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.
896 * pcmcia_loop_config() loops over all configuration options, and calls
897 * the driver-specific conf_check() for each one, checking whether
898 * it is a valid one. Returns 0 on success or errorcode otherwise.
900 int pcmcia_loop_config(struct pcmcia_device *p_dev,
901 int (*conf_check) (struct pcmcia_device *p_dev,
902 cistpl_cftable_entry_t *cfg,
903 cistpl_cftable_entry_t *dflt,
904 unsigned int vcc,
905 void *priv_data),
906 void *priv_data)
908 struct pcmcia_cfg_mem *cfg_mem;
909 int ret;
911 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
912 if (cfg_mem == NULL)
913 return -ENOMEM;
915 cfg_mem->p_dev = p_dev;
916 cfg_mem->conf_check = conf_check;
917 cfg_mem->priv_data = priv_data;
919 ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
920 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
921 cfg_mem, pcmcia_do_loop_config);
923 kfree(cfg_mem);
924 return ret;
926 EXPORT_SYMBOL(pcmcia_loop_config);
929 struct 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);
938 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
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.
944 static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
946 struct pcmcia_loop_mem *loop = priv;
948 return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
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.
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.
963 int 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)
969 struct pcmcia_loop_mem loop = {
970 .p_dev = p_dev,
971 .loop_tuple = loop_tuple,
972 .priv_data = priv_data};
974 return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
975 &loop, pcmcia_do_loop_tuple);
977 EXPORT_SYMBOL(pcmcia_loop_tuple);
980 struct pcmcia_loop_get {
981 size_t len;
982 cisdata_t **buf;
986 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
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.
994 static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
995 void *priv)
997 struct pcmcia_loop_get *get = priv;
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);
1003 } else
1004 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
1005 return 0;
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.
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.
1018 size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1019 unsigned char **buf)
1021 struct pcmcia_loop_get get = {
1022 .len = 0,
1023 .buf = buf,
1026 *get.buf = NULL;
1027 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1029 return get.len;
1031 EXPORT_SYMBOL(pcmcia_get_tuple);
1035 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
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].
1042 static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1043 void *priv)
1045 struct net_device *dev = priv;
1046 int i;
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;
1056 if (tuple->TupleData[1] != ETH_ALEN) {
1057 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1058 return -EINVAL;
1060 for (i = 0; i < 6; i++)
1061 dev->dev_addr[i] = tuple->TupleData[i+2];
1062 return 0;
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.
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!).
1074 int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1076 return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
1078 EXPORT_SYMBOL(pcmcia_get_mac_from_cis);