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 = 0;
37 module_param(io_speed, int, 0444);
40 #ifdef CONFIG_PCMCIA_PROBE
42 /* mask of IRQs already reserved by other cards, we should avoid using them */
43 static u8 pcmcia_used_irq[NR_IRQS];
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)
56 unsigned int try, align;
58 align = (*base) ? (lines ? 1<<lines : 0) : 1;
59 if (align && (align < num)) {
61 dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
65 while (align && (align < num)) align <<= 1;
67 if (*base & ~(align-1)) {
68 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
72 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
73 *base = s->io_offset | (*base & 0x0fff);
76 /* Check for an already-allocated window that must conflict with
77 * what was asked for. It is a hack because it does not catch all
78 * potential conflicts, just the most obvious ones.
80 for (i = 0; i < MAX_IO_WIN; i++)
81 if ((s->io[i].res) && *base &&
82 ((s->io[i].res->start & (align-1)) == *base))
84 for (i = 0; i < MAX_IO_WIN; i++) {
86 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
88 *base = s->io[i].res->start;
89 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
94 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
96 /* Try to extend top of window */
97 try = s->io[i].res->end + 1;
98 if ((*base == 0) || (*base == try))
99 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
100 s->io[i].res->end + num, s) == 0) {
102 s->io[i].InUse += num;
105 /* Try to extend bottom of window */
106 try = s->io[i].res->start - num;
107 if ((*base == 0) || (*base == try))
108 if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
109 s->io[i].res->end, s) == 0) {
111 s->io[i].InUse += num;
115 return (i == MAX_IO_WIN);
116 } /* alloc_io_space */
119 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
124 for (i = 0; i < MAX_IO_WIN; i++) {
127 if ((s->io[i].res->start <= base) &&
128 (s->io[i].res->end >= base+num-1)) {
129 s->io[i].InUse -= num;
130 /* Free the window if no one else is using it */
131 if (s->io[i].InUse == 0) {
132 release_resource(s->io[i].res);
138 } /* release_io_space */
141 /** pccard_access_configuration_register
143 * Access_configuration_register() reads and writes configuration
144 * registers in attribute memory. Memory window 0 is reserved for
145 * this and the tuple reading services.
148 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
151 struct pcmcia_socket *s;
156 if (!p_dev || !p_dev->function_config)
160 c = p_dev->function_config;
162 if (!(c->state & CONFIG_LOCKED)) {
163 dev_dbg(&s->dev, "Configuration isnt't locked\n");
167 addr = (c->ConfigBase + reg->Offset) >> 1;
169 switch (reg->Action) {
171 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
176 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
179 dev_dbg(&s->dev, "Invalid conf register request\n");
184 } /* pcmcia_access_configuration_register */
185 EXPORT_SYMBOL(pcmcia_access_configuration_register);
188 /** pcmcia_get_window
190 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
191 int idx, win_req_t *req)
196 if (!s || !(s->state & SOCKET_PRESENT))
198 for (w = idx; w < MAX_WIN; w++)
199 if (s->state & SOCKET_WIN_REQ(w))
204 req->Base = win->ctl.res->start;
205 req->Size = win->ctl.res->end - win->ctl.res->start + 1;
206 req->AccessSpeed = win->ctl.speed;
208 if (win->ctl.flags & MAP_ATTRIB)
209 req->Attributes |= WIN_MEMORY_TYPE_AM;
210 if (win->ctl.flags & MAP_ACTIVE)
211 req->Attributes |= WIN_ENABLE;
212 if (win->ctl.flags & MAP_16BIT)
213 req->Attributes |= WIN_DATA_WIDTH_16;
214 if (win->ctl.flags & MAP_USE_WAIT)
215 req->Attributes |= WIN_USE_WAIT;
218 } /* pcmcia_get_window */
219 EXPORT_SYMBOL(pcmcia_get_window);
222 /** pcmcia_get_mem_page
224 * Change the card address of an already open memory window.
226 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
228 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
231 req->CardOffset = win->ctl.card_start;
233 } /* pcmcia_get_mem_page */
234 EXPORT_SYMBOL(pcmcia_get_mem_page);
237 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
239 struct pcmcia_socket *s;
240 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
243 if (req->Page != 0) {
244 dev_dbg(&s->dev, "failure: requested page is zero\n");
247 win->ctl.card_start = req->CardOffset;
248 if (s->ops->set_mem_map(s, &win->ctl) != 0) {
249 dev_dbg(&s->dev, "failed to set_mem_map\n");
253 } /* pcmcia_map_mem_page */
254 EXPORT_SYMBOL(pcmcia_map_mem_page);
257 /** pcmcia_modify_configuration
259 * Modify a locked socket configuration
261 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
264 struct pcmcia_socket *s;
268 c = p_dev->function_config;
270 if (!(s->state & SOCKET_PRESENT)) {
271 dev_dbg(&s->dev, "No card present\n");
274 if (!(c->state & CONFIG_LOCKED)) {
275 dev_dbg(&s->dev, "Configuration isnt't locked\n");
279 if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
280 if (mod->Attributes & CONF_ENABLE_IRQ) {
281 c->Attributes |= CONF_ENABLE_IRQ;
282 s->socket.io_irq = s->irq.AssignedIRQ;
284 c->Attributes &= ~CONF_ENABLE_IRQ;
285 s->socket.io_irq = 0;
287 s->ops->set_socket(s, &s->socket);
290 if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
291 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
295 /* We only allow changing Vpp1 and Vpp2 to the same value */
296 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
297 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
298 if (mod->Vpp1 != mod->Vpp2) {
299 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
302 s->socket.Vpp = mod->Vpp1;
303 if (s->ops->set_socket(s, &s->socket)) {
304 dev_printk(KERN_WARNING, &s->dev,
305 "Unable to set VPP\n");
308 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
309 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
310 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
314 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
315 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
319 io_on.speed = io_speed;
320 for (i = 0; i < MAX_IO_WIN; i++) {
326 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
327 io_on.start = s->io[i].res->start;
328 io_on.stop = s->io[i].res->end;
330 s->ops->set_io_map(s, &io_off);
332 s->ops->set_io_map(s, &io_on);
337 } /* modify_configuration */
338 EXPORT_SYMBOL(pcmcia_modify_configuration);
341 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
343 pccard_io_map io = { 0, 0, 0, 0, 1 };
344 struct pcmcia_socket *s = p_dev->socket;
345 config_t *c = p_dev->function_config;
348 if (p_dev->_locked) {
350 if (--(s->lock_count) == 0) {
351 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
353 s->socket.io_irq = 0;
354 s->ops->set_socket(s, &s->socket);
357 if (c->state & CONFIG_LOCKED) {
358 c->state &= ~CONFIG_LOCKED;
359 if (c->state & CONFIG_IO_REQ)
360 for (i = 0; i < MAX_IO_WIN; i++) {
364 if (s->io[i].Config != 0)
367 s->ops->set_io_map(s, &io);
372 } /* pcmcia_release_configuration */
375 /** pcmcia_release_io
377 * Release_io() releases the I/O ranges allocated by a client. This
378 * may be invoked some time after a card ejection has already dumped
379 * the actual socket configuration, so if the client is "stale", we
380 * don't bother checking the port ranges against the current socket
383 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
385 struct pcmcia_socket *s = p_dev->socket;
386 config_t *c = p_dev->function_config;
393 if ((c->io.BasePort1 != req->BasePort1) ||
394 (c->io.NumPorts1 != req->NumPorts1) ||
395 (c->io.BasePort2 != req->BasePort2) ||
396 (c->io.NumPorts2 != req->NumPorts2))
399 c->state &= ~CONFIG_IO_REQ;
401 release_io_space(s, req->BasePort1, req->NumPorts1);
403 release_io_space(s, req->BasePort2, req->NumPorts2);
406 } /* pcmcia_release_io */
409 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
411 struct pcmcia_socket *s = p_dev->socket;
412 config_t *c= p_dev->function_config;
418 if (c->state & CONFIG_LOCKED)
420 if (c->irq.Attributes != req->Attributes) {
421 dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
424 if (s->irq.AssignedIRQ != req->AssignedIRQ) {
425 dev_dbg(&s->dev, "IRQ must match assigned one\n");
428 if (--s->irq.Config == 0) {
429 c->state &= ~CONFIG_IRQ_REQ;
430 s->irq.AssignedIRQ = 0;
433 if (req->Attributes & IRQ_HANDLE_PRESENT) {
434 free_irq(req->AssignedIRQ, req->Instance);
437 #ifdef CONFIG_PCMCIA_PROBE
438 pcmcia_used_irq[req->AssignedIRQ]--;
442 } /* pcmcia_release_irq */
445 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
447 struct pcmcia_socket *s;
448 window_handle_t win = wh;
450 if ((win == NULL) || (win->magic != WINDOW_MAGIC))
453 if (!(win->handle->_win & CLIENT_WIN_REQ(win->index))) {
454 dev_dbg(&s->dev, "not releasing unknown window\n");
458 /* Shut down memory window */
459 win->ctl.flags &= ~MAP_ACTIVE;
460 s->ops->set_mem_map(s, &win->ctl);
461 s->state &= ~SOCKET_WIN_REQ(win->index);
463 /* Release system memory */
465 release_resource(win->ctl.res);
469 win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
474 } /* pcmcia_release_window */
475 EXPORT_SYMBOL(pcmcia_release_window);
478 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
483 struct pcmcia_socket *s = p_dev->socket;
487 if (!(s->state & SOCKET_PRESENT))
490 if (req->IntType & INT_CARDBUS) {
491 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
494 c = p_dev->function_config;
495 if (c->state & CONFIG_LOCKED) {
496 dev_dbg(&s->dev, "Configuration is locked\n");
500 /* Do power control. We don't allow changes in Vcc. */
501 s->socket.Vpp = req->Vpp;
502 if (s->ops->set_socket(s, &s->socket)) {
503 dev_printk(KERN_WARNING, &s->dev,
504 "Unable to set socket state\n");
508 /* Pick memory or I/O card, DMA mode, interrupt */
509 c->IntType = req->IntType;
510 c->Attributes = req->Attributes;
511 if (req->IntType & INT_MEMORY_AND_IO)
512 s->socket.flags |= SS_IOCARD;
513 if (req->IntType & INT_ZOOMED_VIDEO)
514 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
515 if (req->Attributes & CONF_ENABLE_DMA)
516 s->socket.flags |= SS_DMA_MODE;
517 if (req->Attributes & CONF_ENABLE_SPKR)
518 s->socket.flags |= SS_SPKR_ENA;
519 if (req->Attributes & CONF_ENABLE_IRQ)
520 s->socket.io_irq = s->irq.AssignedIRQ;
522 s->socket.io_irq = 0;
523 s->ops->set_socket(s, &s->socket);
526 /* Set up CIS configuration registers */
527 base = c->ConfigBase = req->ConfigBase;
528 c->CardValues = req->Present;
529 if (req->Present & PRESENT_COPY) {
531 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
533 if (req->Present & PRESENT_OPTION) {
534 if (s->functions == 1) {
535 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
537 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
538 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
539 if (req->Present & PRESENT_IOBASE_0)
540 c->Option |= COR_ADDR_DECODE;
542 if (c->state & CONFIG_IRQ_REQ)
543 if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
544 c->Option |= COR_LEVEL_REQ;
545 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
548 if (req->Present & PRESENT_STATUS) {
549 c->Status = req->Status;
550 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
552 if (req->Present & PRESENT_PIN_REPLACE) {
554 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
556 if (req->Present & PRESENT_EXT_STATUS) {
557 c->ExtStatus = req->ExtStatus;
558 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
560 if (req->Present & PRESENT_IOBASE_0) {
561 u_char b = c->io.BasePort1 & 0xff;
562 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
563 b = (c->io.BasePort1 >> 8) & 0xff;
564 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
566 if (req->Present & PRESENT_IOSIZE) {
567 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
568 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
571 /* Configure I/O windows */
572 if (c->state & CONFIG_IO_REQ) {
573 iomap.speed = io_speed;
574 for (i = 0; i < MAX_IO_WIN; i++)
577 iomap.flags = MAP_ACTIVE;
578 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
579 case IO_DATA_PATH_WIDTH_16:
580 iomap.flags |= MAP_16BIT; break;
581 case IO_DATA_PATH_WIDTH_AUTO:
582 iomap.flags |= MAP_AUTOSZ; break;
586 iomap.start = s->io[i].res->start;
587 iomap.stop = s->io[i].res->end;
588 s->ops->set_io_map(s, &iomap);
593 c->state |= CONFIG_LOCKED;
596 } /* pcmcia_request_configuration */
597 EXPORT_SYMBOL(pcmcia_request_configuration);
600 /** pcmcia_request_io
602 * Request_io() reserves ranges of port addresses for a socket.
603 * I have not implemented range sharing or alias addressing.
605 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
607 struct pcmcia_socket *s = p_dev->socket;
610 if (!(s->state & SOCKET_PRESENT)) {
611 dev_dbg(&s->dev, "No card present\n");
617 c = p_dev->function_config;
618 if (c->state & CONFIG_LOCKED) {
619 dev_dbg(&s->dev, "Configuration is locked\n");
622 if (c->state & CONFIG_IO_REQ) {
623 dev_dbg(&s->dev, "IO already configured\n");
626 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
627 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
630 if ((req->NumPorts2 > 0) &&
631 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
632 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
636 dev_dbg(&s->dev, "trying to allocate resource 1\n");
637 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
638 req->NumPorts1, req->IOAddrLines)) {
639 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
643 if (req->NumPorts2) {
644 dev_dbg(&s->dev, "trying to allocate resource 2\n");
645 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
646 req->NumPorts2, req->IOAddrLines)) {
647 dev_dbg(&s->dev, "allocation of resource 2 failed\n");
648 release_io_space(s, req->BasePort1, req->NumPorts1);
654 c->state |= CONFIG_IO_REQ;
657 } /* pcmcia_request_io */
658 EXPORT_SYMBOL(pcmcia_request_io);
661 /** pcmcia_request_irq
663 * Request_irq() reserves an irq for this client.
665 * Also, since Linux only reserves irq's when they are actually
666 * hooked, we don't guarantee that an irq will still be available
667 * when the configuration is locked. Now that I think about it,
668 * there might be a way to fix this using a dummy handler.
671 #ifdef CONFIG_PCMCIA_PROBE
672 static irqreturn_t test_action(int cpl, void *dev_id)
678 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
680 struct pcmcia_socket *s = p_dev->socket;
682 int ret = -EINVAL, irq = 0;
685 if (!(s->state & SOCKET_PRESENT)) {
686 dev_dbg(&s->dev, "No card present\n");
689 c = p_dev->function_config;
690 if (c->state & CONFIG_LOCKED) {
691 dev_dbg(&s->dev, "Configuration is locked\n");
694 if (c->state & CONFIG_IRQ_REQ) {
695 dev_dbg(&s->dev, "IRQ already configured\n");
699 /* Decide what type of interrupt we are registering */
701 if (s->functions > 1) /* All of this ought to be handled higher up */
703 else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
705 else printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
707 #ifdef CONFIG_PCMCIA_PROBE
710 /* if the underlying IRQ infrastructure allows for it, only allocate
711 * the IRQ, but do not enable it
713 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
714 type |= IRQ_NOAUTOEN;
715 #endif /* IRQ_NOAUTOEN */
717 if (s->irq.AssignedIRQ != 0) {
718 /* If the interrupt is already assigned, it must be the same */
719 irq = s->irq.AssignedIRQ;
722 u32 mask = s->irq_mask;
723 void *data = &p_dev->dev.driver; /* something unique to this device */
725 for (try = 0; try < 64; try++) {
728 /* marked as available by driver, and not blocked by userspace? */
729 if (!((mask >> irq) & 1))
732 /* avoid an IRQ which is already used by a PCMCIA card */
733 if ((try < 32) && pcmcia_used_irq[irq])
736 /* register the correct driver, if possible, of check whether
737 * registering a dummy handle works, i.e. if the IRQ isn't
738 * marked as used by the kernel resource management core */
739 ret = request_irq(irq,
740 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
743 (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
745 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
752 /* only assign PCI irq if no IRQ already assigned */
753 if (ret && !s->irq.AssignedIRQ) {
755 dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
762 if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
763 ret = request_irq(irq, req->Handler, type,
764 p_dev->devname, req->Instance);
766 dev_printk(KERN_INFO, &s->dev,
767 "request_irq() failed\n");
772 /* Make sure the fact the request type was overridden is passed back */
773 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
774 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
775 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
776 "request for exclusive IRQ could not be fulfilled.\n");
777 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
778 "needs updating to supported shared IRQ lines.\n");
780 c->irq.Attributes = req->Attributes;
781 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
784 c->state |= CONFIG_IRQ_REQ;
787 #ifdef CONFIG_PCMCIA_PROBE
788 pcmcia_used_irq[irq]++;
792 } /* pcmcia_request_irq */
793 EXPORT_SYMBOL(pcmcia_request_irq);
796 /** pcmcia_request_window
798 * Request_window() establishes a mapping between card memory space
799 * and system memory space.
801 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
803 struct pcmcia_socket *s = (*p_dev)->socket;
808 if (!(s->state & SOCKET_PRESENT)) {
809 dev_dbg(&s->dev, "No card present\n");
812 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
813 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
817 /* Window size defaults to smallest available */
819 req->Size = s->map_size;
820 align = (((s->features & SS_CAP_MEM_ALIGN) ||
821 (req->Attributes & WIN_STRICT_ALIGN)) ?
822 req->Size : s->map_size);
823 if (req->Size & (s->map_size-1)) {
824 dev_dbg(&s->dev, "invalid map size\n");
827 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
828 (req->Base & (align-1))) {
829 dev_dbg(&s->dev, "invalid base address\n");
835 /* Allocate system memory window */
836 for (w = 0; w < MAX_WIN; w++)
837 if (!(s->state & SOCKET_WIN_REQ(w))) break;
839 dev_dbg(&s->dev, "all windows are used already\n");
844 win->magic = WINDOW_MAGIC;
846 win->handle = *p_dev;
849 if (!(s->features & SS_CAP_STATIC_MAP)) {
850 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
851 (req->Attributes & WIN_MAP_BELOW_1MB), s);
853 dev_dbg(&s->dev, "allocating mem region failed\n");
857 (*p_dev)->_win |= CLIENT_WIN_REQ(w);
859 /* Configure the socket controller */
862 win->ctl.speed = req->AccessSpeed;
863 if (req->Attributes & WIN_MEMORY_TYPE)
864 win->ctl.flags |= MAP_ATTRIB;
865 if (req->Attributes & WIN_ENABLE)
866 win->ctl.flags |= MAP_ACTIVE;
867 if (req->Attributes & WIN_DATA_WIDTH_16)
868 win->ctl.flags |= MAP_16BIT;
869 if (req->Attributes & WIN_USE_WAIT)
870 win->ctl.flags |= MAP_USE_WAIT;
871 win->ctl.card_start = 0;
872 if (s->ops->set_mem_map(s, &win->ctl) != 0) {
873 dev_dbg(&s->dev, "failed to set memory mapping\n");
876 s->state |= SOCKET_WIN_REQ(w);
878 /* Return window handle */
879 if (s->features & SS_CAP_STATIC_MAP) {
880 req->Base = win->ctl.static_start;
882 req->Base = win->ctl.res->start;
887 } /* pcmcia_request_window */
888 EXPORT_SYMBOL(pcmcia_request_window);
890 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
891 pcmcia_release_configuration(p_dev);
892 pcmcia_release_io(p_dev, &p_dev->io);
893 pcmcia_release_irq(p_dev, &p_dev->irq);
895 pcmcia_release_window(p_dev, p_dev->win);
897 EXPORT_SYMBOL(pcmcia_disable_device);
900 struct pcmcia_cfg_mem {
901 struct pcmcia_device *p_dev;
903 int (*conf_check) (struct pcmcia_device *p_dev,
904 cistpl_cftable_entry_t *cfg,
905 cistpl_cftable_entry_t *dflt,
909 cistpl_cftable_entry_t dflt;
913 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
915 * pcmcia_do_loop_config() is the internal callback for the call from
916 * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
917 * by a struct pcmcia_cfg_mem.
919 static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
921 cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
922 struct pcmcia_cfg_mem *cfg_mem = priv;
925 cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
926 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
927 cfg_mem->dflt = *cfg;
929 return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
930 cfg_mem->p_dev->socket->socket.Vcc,
935 * pcmcia_loop_config() - loop over configuration options
936 * @p_dev: the struct pcmcia_device which we need to loop for.
937 * @conf_check: function to call for each configuration option.
938 * It gets passed the struct pcmcia_device, the CIS data
939 * describing the configuration option, and private data
940 * being passed to pcmcia_loop_config()
941 * @priv_data: private data to be passed to the conf_check function.
943 * pcmcia_loop_config() loops over all configuration options, and calls
944 * the driver-specific conf_check() for each one, checking whether
945 * it is a valid one. Returns 0 on success or errorcode otherwise.
947 int pcmcia_loop_config(struct pcmcia_device *p_dev,
948 int (*conf_check) (struct pcmcia_device *p_dev,
949 cistpl_cftable_entry_t *cfg,
950 cistpl_cftable_entry_t *dflt,
955 struct pcmcia_cfg_mem *cfg_mem;
958 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
962 cfg_mem->p_dev = p_dev;
963 cfg_mem->conf_check = conf_check;
964 cfg_mem->priv_data = priv_data;
966 ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
967 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
968 cfg_mem, pcmcia_do_loop_config);
973 EXPORT_SYMBOL(pcmcia_loop_config);
976 struct pcmcia_loop_mem {
977 struct pcmcia_device *p_dev;
979 int (*loop_tuple) (struct pcmcia_device *p_dev,
985 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
987 * pcmcia_do_loop_tuple() is the internal callback for the call from
988 * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
989 * by a struct pcmcia_cfg_mem.
991 static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
993 struct pcmcia_loop_mem *loop = priv;
995 return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
999 * pcmcia_loop_tuple() - loop over tuples in the CIS
1000 * @p_dev: the struct pcmcia_device which we need to loop for.
1001 * @code: which CIS code shall we look for?
1002 * @priv_data: private data to be passed to the loop_tuple function.
1003 * @loop_tuple: function to call for each CIS entry of type @function. IT
1004 * gets passed the raw tuple and @priv_data.
1006 * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
1007 * calls the @loop_tuple function for each entry. If the call to @loop_tuple
1008 * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
1010 int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1011 int (*loop_tuple) (struct pcmcia_device *p_dev,
1016 struct pcmcia_loop_mem loop = {
1018 .loop_tuple = loop_tuple,
1019 .priv_data = priv_data};
1021 return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
1022 &loop, pcmcia_do_loop_tuple);
1024 EXPORT_SYMBOL(pcmcia_loop_tuple);
1027 struct pcmcia_loop_get {
1033 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
1035 * pcmcia_do_get_tuple() is the internal callback for the call from
1036 * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
1037 * the first tuple, return 0 unconditionally. Create a memory buffer large
1038 * enough to hold the content of the tuple, and fill it with the tuple data.
1039 * The caller is responsible to free the buffer.
1041 static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
1044 struct pcmcia_loop_get *get = priv;
1046 *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1048 get->len = tuple->TupleDataLen;
1049 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
1051 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
1056 * pcmcia_get_tuple() - get first tuple from CIS
1057 * @p_dev: the struct pcmcia_device which we need to loop for.
1058 * @code: which CIS code shall we look for?
1059 * @buf: pointer to store the buffer to.
1061 * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
1062 * It returns the buffer length (or zero). The caller is responsible to free
1063 * the buffer passed in @buf.
1065 size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1066 unsigned char **buf)
1068 struct pcmcia_loop_get get = {
1074 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1078 EXPORT_SYMBOL(pcmcia_get_tuple);
1082 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
1084 * pcmcia_do_get_mac() is the internal callback for the call from
1085 * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
1086 * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
1087 * to struct net_device->dev_addr[i].
1089 static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1092 struct net_device *dev = priv;
1095 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1097 if (tuple->TupleDataLen < ETH_ALEN + 2) {
1098 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1103 if (tuple->TupleData[1] != ETH_ALEN) {
1104 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1107 for (i = 0; i < 6; i++)
1108 dev->dev_addr[i] = tuple->TupleData[i+2];
1113 * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
1114 * @p_dev: the struct pcmcia_device for which we want the address.
1115 * @dev: a properly prepared struct net_device to store the info to.
1117 * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
1118 * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
1119 * must be set up properly by the driver (see examples!).
1121 int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1123 return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
1125 EXPORT_SYMBOL(pcmcia_get_mac_from_cis);