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 */
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];
46 static int pcmcia_adjust_io_region(struct resource *res, unsigned long start,
47 unsigned long end, struct pcmcia_socket *s)
49 if (s->resource_ops->adjust_io_region)
50 return s->resource_ops->adjust_io_region(res, start, end, s);
54 static struct resource *pcmcia_find_io_region(unsigned long base, int num,
56 struct pcmcia_socket *s)
58 if (s->resource_ops->find_io)
59 return s->resource_ops->find_io(base, num, align, s);
63 int pcmcia_validate_mem(struct pcmcia_socket *s)
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 */
71 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
72 int low, struct pcmcia_socket *s)
74 if (s->resource_ops->find_mem)
75 return s->resource_ops->find_mem(base, num, align, low, s);
82 * Special stuff for managing IO windows, because they are scarce
85 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
86 unsigned int *base, unsigned int num, u_int lines)
89 unsigned int try, align;
91 align = (*base) ? (lines ? 1<<lines : 0) : 1;
92 if (align && (align < num)) {
94 dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
98 while (align && (align < num))
101 if (*base & ~(align-1)) {
102 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
106 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
107 *base = s->io_offset | (*base & 0x0fff);
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.
114 for (i = 0; i < MAX_IO_WIN; i++)
115 if ((s->io[i].res) && *base &&
116 ((s->io[i].res->start & (align-1)) == *base))
118 for (i = 0; i < MAX_IO_WIN; i++) {
120 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
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;
128 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
130 /* Try to extend top of window */
131 try = s->io[i].res->end + 1;
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) {
136 s->io[i].InUse += num;
139 /* Try to extend bottom of window */
140 try = s->io[i].res->start - num;
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) {
145 s->io[i].InUse += num;
149 return (i == MAX_IO_WIN);
150 } /* alloc_io_space */
153 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
158 for (i = 0; i < MAX_IO_WIN; i++) {
161 if ((s->io[i].res->start <= base) &&
162 (s->io[i].res->end >= base+num-1)) {
163 s->io[i].InUse -= num;
164 /* Free the window if no one else is using it */
165 if (s->io[i].InUse == 0) {
166 release_resource(s->io[i].res);
172 } /* release_io_space */
175 /** pccard_access_configuration_register
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.
182 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
185 struct pcmcia_socket *s;
190 if (!p_dev || !p_dev->function_config)
194 c = p_dev->function_config;
196 if (!(c->state & CONFIG_LOCKED)) {
197 dev_dbg(&s->dev, "Configuration isnt't locked\n");
201 addr = (c->ConfigBase + reg->Offset) >> 1;
203 switch (reg->Action) {
205 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
210 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
213 dev_dbg(&s->dev, "Invalid conf register request\n");
218 } /* pcmcia_access_configuration_register */
219 EXPORT_SYMBOL(pcmcia_access_configuration_register);
222 int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
225 struct pcmcia_socket *s = p_dev->socket;
230 if (req->Page != 0) {
231 dev_dbg(&s->dev, "failure: requested page is zero\n");
234 s->win[wh].card_start = req->CardOffset;
235 if (s->ops->set_mem_map(s, &s->win[wh]) != 0) {
236 dev_dbg(&s->dev, "failed to set_mem_map\n");
240 } /* pcmcia_map_mem_page */
241 EXPORT_SYMBOL(pcmcia_map_mem_page);
244 /** pcmcia_modify_configuration
246 * Modify a locked socket configuration
248 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
251 struct pcmcia_socket *s;
255 c = p_dev->function_config;
257 if (!(s->state & SOCKET_PRESENT)) {
258 dev_dbg(&s->dev, "No card present\n");
261 if (!(c->state & CONFIG_LOCKED)) {
262 dev_dbg(&s->dev, "Configuration isnt't locked\n");
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;
271 c->Attributes &= ~CONF_ENABLE_IRQ;
272 s->socket.io_irq = 0;
274 s->ops->set_socket(s, &s->socket);
277 if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
278 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
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)) {
285 if (mod->Vpp1 != mod->Vpp2) {
286 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
289 s->socket.Vpp = mod->Vpp1;
290 if (s->ops->set_socket(s, &s->socket)) {
291 dev_printk(KERN_WARNING, &s->dev,
292 "Unable to set VPP\n");
295 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
296 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
297 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
301 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
302 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
306 io_on.speed = io_speed;
307 for (i = 0; i < MAX_IO_WIN; i++) {
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;
317 s->ops->set_io_map(s, &io_off);
319 s->ops->set_io_map(s, &io_on);
324 } /* modify_configuration */
325 EXPORT_SYMBOL(pcmcia_modify_configuration);
328 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
330 pccard_io_map io = { 0, 0, 0, 0, 1 };
331 struct pcmcia_socket *s = p_dev->socket;
332 config_t *c = p_dev->function_config;
335 if (p_dev->_locked) {
337 if (--(s->lock_count) == 0) {
338 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
340 s->socket.io_irq = 0;
341 s->ops->set_socket(s, &s->socket);
344 if (c->state & CONFIG_LOCKED) {
345 c->state &= ~CONFIG_LOCKED;
346 if (c->state & CONFIG_IO_REQ)
347 for (i = 0; i < MAX_IO_WIN; i++) {
351 if (s->io[i].Config != 0)
354 s->ops->set_io_map(s, &io);
359 } /* pcmcia_release_configuration */
362 /** pcmcia_release_io
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
370 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
372 struct pcmcia_socket *s = p_dev->socket;
373 config_t *c = p_dev->function_config;
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))
386 c->state &= ~CONFIG_IO_REQ;
388 release_io_space(s, req->BasePort1, req->NumPorts1);
390 release_io_space(s, req->BasePort2, req->NumPorts2);
393 } /* pcmcia_release_io */
396 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
398 struct pcmcia_socket *s = p_dev->socket;
399 config_t *c = p_dev->function_config;
405 if (c->state & CONFIG_LOCKED)
407 if (c->irq.Attributes != req->Attributes) {
408 dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
411 if (s->irq.AssignedIRQ != req->AssignedIRQ) {
412 dev_dbg(&s->dev, "IRQ must match assigned one\n");
415 if (--s->irq.Config == 0) {
416 c->state &= ~CONFIG_IRQ_REQ;
417 s->irq.AssignedIRQ = 0;
421 free_irq(req->AssignedIRQ, p_dev->priv);
423 #ifdef CONFIG_PCMCIA_PROBE
424 pcmcia_used_irq[req->AssignedIRQ]--;
428 } /* pcmcia_release_irq */
431 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
433 struct pcmcia_socket *s = p_dev->socket;
442 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
443 dev_dbg(&s->dev, "not releasing unknown window\n");
447 /* Shut down memory window */
448 win->flags &= ~MAP_ACTIVE;
449 s->ops->set_mem_map(s, win);
450 s->state &= ~SOCKET_WIN_REQ(wh);
452 /* Release system memory */
454 release_resource(win->res);
458 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
461 } /* pcmcia_release_window */
462 EXPORT_SYMBOL(pcmcia_release_window);
465 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
470 struct pcmcia_socket *s = p_dev->socket;
474 if (!(s->state & SOCKET_PRESENT))
477 if (req->IntType & INT_CARDBUS) {
478 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
481 c = p_dev->function_config;
482 if (c->state & CONFIG_LOCKED) {
483 dev_dbg(&s->dev, "Configuration is locked\n");
487 /* Do power control. We don't allow changes in Vcc. */
488 s->socket.Vpp = req->Vpp;
489 if (s->ops->set_socket(s, &s->socket)) {
490 dev_printk(KERN_WARNING, &s->dev,
491 "Unable to set socket state\n");
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;
509 s->socket.io_irq = 0;
510 s->ops->set_socket(s, &s->socket);
513 /* Set up CIS configuration registers */
514 base = c->ConfigBase = req->ConfigBase;
515 c->CardValues = req->Present;
516 if (req->Present & PRESENT_COPY) {
518 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
520 if (req->Present & PRESENT_OPTION) {
521 if (s->functions == 1) {
522 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
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;
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);
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);
539 if (req->Present & PRESENT_PIN_REPLACE) {
541 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
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);
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);
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);
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++)
564 iomap.flags = MAP_ACTIVE;
565 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
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;
573 iomap.start = s->io[i].res->start;
574 iomap.stop = s->io[i].res->end;
575 s->ops->set_io_map(s, &iomap);
580 c->state |= CONFIG_LOCKED;
583 } /* pcmcia_request_configuration */
584 EXPORT_SYMBOL(pcmcia_request_configuration);
587 /** pcmcia_request_io
589 * Request_io() reserves ranges of port addresses for a socket.
590 * I have not implemented range sharing or alias addressing.
592 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
594 struct pcmcia_socket *s = p_dev->socket;
597 if (!(s->state & SOCKET_PRESENT)) {
598 dev_dbg(&s->dev, "No card present\n");
604 c = p_dev->function_config;
605 if (c->state & CONFIG_LOCKED) {
606 dev_dbg(&s->dev, "Configuration is locked\n");
609 if (c->state & CONFIG_IO_REQ) {
610 dev_dbg(&s->dev, "IO already configured\n");
613 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
614 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
617 if ((req->NumPorts2 > 0) &&
618 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
619 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
623 dev_dbg(&s->dev, "trying to allocate resource 1\n");
624 if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
625 req->NumPorts1, req->IOAddrLines)) {
626 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
630 if (req->NumPorts2) {
631 dev_dbg(&s->dev, "trying to allocate resource 2\n");
632 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
633 req->NumPorts2, req->IOAddrLines)) {
634 dev_dbg(&s->dev, "allocation of resource 2 failed\n");
635 release_io_space(s, req->BasePort1, req->NumPorts1);
641 c->state |= CONFIG_IO_REQ;
644 } /* pcmcia_request_io */
645 EXPORT_SYMBOL(pcmcia_request_io);
648 /** pcmcia_request_irq
650 * Request_irq() reserves an irq for this client.
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.
658 #ifdef CONFIG_PCMCIA_PROBE
659 static irqreturn_t test_action(int cpl, void *dev_id)
665 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
667 struct pcmcia_socket *s = p_dev->socket;
669 int ret = -EINVAL, irq = 0;
672 if (!(s->state & SOCKET_PRESENT)) {
673 dev_dbg(&s->dev, "No card present\n");
676 c = p_dev->function_config;
677 if (c->state & CONFIG_LOCKED) {
678 dev_dbg(&s->dev, "Configuration is locked\n");
681 if (c->state & CONFIG_IRQ_REQ) {
682 dev_dbg(&s->dev, "IRQ already configured\n");
686 /* Decide what type of interrupt we are registering */
688 if (s->functions > 1) /* All of this ought to be handled higher up */
690 else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
693 printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
695 #ifdef CONFIG_PCMCIA_PROBE
698 /* if the underlying IRQ infrastructure allows for it, only allocate
699 * the IRQ, but do not enable it
702 type |= IRQ_NOAUTOEN;
703 #endif /* IRQ_NOAUTOEN */
705 if (s->irq.AssignedIRQ != 0) {
706 /* If the interrupt is already assigned, it must be the same */
707 irq = s->irq.AssignedIRQ;
710 u32 mask = s->irq_mask;
711 void *data = p_dev; /* something unique to this device */
713 for (try = 0; try < 64; try++) {
716 /* marked as available by driver, and not blocked by userspace? */
717 if (!((mask >> irq) & 1))
720 /* avoid an IRQ which is already used by a PCMCIA card */
721 if ((try < 32) && pcmcia_used_irq[irq])
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,
728 (req->Handler) ? req->Handler : test_action,
731 (req->Handler) ? p_dev->priv : data);
740 /* only assign PCI irq if no IRQ already assigned */
741 if (ret && !s->irq.AssignedIRQ) {
743 dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
750 if (ret && req->Handler) {
751 ret = request_irq(irq, req->Handler, type,
752 p_dev->devname, p_dev->priv);
754 dev_printk(KERN_INFO, &s->dev,
755 "request_irq() failed\n");
760 /* Make sure the fact the request type was overridden is passed back */
761 if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
762 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
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");
768 c->irq.Attributes = req->Attributes;
769 s->irq.AssignedIRQ = req->AssignedIRQ = irq;
772 c->state |= CONFIG_IRQ_REQ;
775 #ifdef CONFIG_PCMCIA_PROBE
776 pcmcia_used_irq[irq]++;
780 } /* pcmcia_request_irq */
781 EXPORT_SYMBOL(pcmcia_request_irq);
784 /** pcmcia_request_window
786 * Request_window() establishes a mapping between card memory space
787 * and system memory space.
789 int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
791 struct pcmcia_socket *s = p_dev->socket;
796 if (!(s->state & SOCKET_PRESENT)) {
797 dev_dbg(&s->dev, "No card present\n");
800 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
801 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
805 /* Window size defaults to smallest available */
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);
811 if (req->Size & (s->map_size-1)) {
812 dev_dbg(&s->dev, "invalid map size\n");
815 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
816 (req->Base & (align-1))) {
817 dev_dbg(&s->dev, "invalid base address\n");
823 /* Allocate system memory window */
824 for (w = 0; w < MAX_WIN; w++)
825 if (!(s->state & SOCKET_WIN_REQ(w)))
828 dev_dbg(&s->dev, "all windows are used already\n");
834 if (!(s->features & SS_CAP_STATIC_MAP)) {
835 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
836 (req->Attributes & WIN_MAP_BELOW_1MB), s);
838 dev_dbg(&s->dev, "allocating mem region failed\n");
842 p_dev->_win |= CLIENT_WIN_REQ(w);
844 /* Configure the socket controller */
847 win->speed = req->AccessSpeed;
848 if (req->Attributes & WIN_MEMORY_TYPE)
849 win->flags |= MAP_ATTRIB;
850 if (req->Attributes & WIN_ENABLE)
851 win->flags |= MAP_ACTIVE;
852 if (req->Attributes & WIN_DATA_WIDTH_16)
853 win->flags |= MAP_16BIT;
854 if (req->Attributes & WIN_USE_WAIT)
855 win->flags |= MAP_USE_WAIT;
857 if (s->ops->set_mem_map(s, win) != 0) {
858 dev_dbg(&s->dev, "failed to set memory mapping\n");
861 s->state |= SOCKET_WIN_REQ(w);
863 /* Return window handle */
864 if (s->features & SS_CAP_STATIC_MAP)
865 req->Base = win->static_start;
867 req->Base = win->res->start;
872 } /* pcmcia_request_window */
873 EXPORT_SYMBOL(pcmcia_request_window);
875 void pcmcia_disable_device(struct pcmcia_device *p_dev)
877 pcmcia_release_configuration(p_dev);
878 pcmcia_release_io(p_dev, &p_dev->io);
879 pcmcia_release_irq(p_dev, &p_dev->irq);
881 pcmcia_release_window(p_dev, p_dev->win);
883 EXPORT_SYMBOL(pcmcia_disable_device);
886 struct pcmcia_cfg_mem {
887 struct pcmcia_device *p_dev;
889 int (*conf_check) (struct pcmcia_device *p_dev,
890 cistpl_cftable_entry_t *cfg,
891 cistpl_cftable_entry_t *dflt,
895 cistpl_cftable_entry_t dflt;
899 * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
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.
905 static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
907 cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
908 struct pcmcia_cfg_mem *cfg_mem = priv;
911 cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
912 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
913 cfg_mem->dflt = *cfg;
915 return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
916 cfg_mem->p_dev->socket->socket.Vcc,
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.
929 * pcmcia_loop_config() loops over all configuration options, and calls
930 * the driver-specific conf_check() for each one, checking whether
931 * it is a valid one. Returns 0 on success or errorcode otherwise.
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,
936 cistpl_cftable_entry_t *dflt,
941 struct pcmcia_cfg_mem *cfg_mem;
944 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
948 cfg_mem->p_dev = p_dev;
949 cfg_mem->conf_check = conf_check;
950 cfg_mem->priv_data = priv_data;
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);
959 EXPORT_SYMBOL(pcmcia_loop_config);
962 struct pcmcia_loop_mem {
963 struct pcmcia_device *p_dev;
965 int (*loop_tuple) (struct pcmcia_device *p_dev,
971 * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
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.
977 static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
979 struct pcmcia_loop_mem *loop = priv;
981 return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
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.
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.
996 int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
997 int (*loop_tuple) (struct pcmcia_device *p_dev,
1002 struct pcmcia_loop_mem loop = {
1004 .loop_tuple = loop_tuple,
1005 .priv_data = priv_data};
1007 return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
1008 &loop, pcmcia_do_loop_tuple);
1010 EXPORT_SYMBOL(pcmcia_loop_tuple);
1013 struct pcmcia_loop_get {
1019 * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
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.
1027 static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
1030 struct pcmcia_loop_get *get = priv;
1032 *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1034 get->len = tuple->TupleDataLen;
1035 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
1037 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
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.
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.
1051 size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1052 unsigned char **buf)
1054 struct pcmcia_loop_get get = {
1060 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1064 EXPORT_SYMBOL(pcmcia_get_tuple);
1068 * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
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].
1075 static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1078 struct net_device *dev = priv;
1081 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1083 if (tuple->TupleDataLen < ETH_ALEN + 2) {
1084 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1089 if (tuple->TupleData[1] != ETH_ALEN) {
1090 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1093 for (i = 0; i < 6; i++)
1094 dev->dev_addr[i] = tuple->TupleData[i+2];
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.
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!).
1107 int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1109 return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
1111 EXPORT_SYMBOL(pcmcia_get_mac_from_cis);