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];
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))
68 if (*base & ~(align-1)) {
69 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
73 if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
74 *base = s->io_offset | (*base & 0x0fff);
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))
85 for (i = 0; i < MAX_IO_WIN; i++) {
87 s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
89 *base = s->io[i].res->start;
90 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
95 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
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) {
103 s->io[i].InUse += num;
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) {
112 s->io[i].InUse += num;
116 return (i == MAX_IO_WIN);
117 } /* alloc_io_space */
120 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
125 for (i = 0; i < MAX_IO_WIN; i++) {
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);
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,
152 struct pcmcia_socket *s;
157 if (!p_dev || !p_dev->function_config)
161 c = p_dev->function_config;
163 if (!(c->state & CONFIG_LOCKED)) {
164 dev_dbg(&s->dev, "Configuration isnt't locked\n");
168 addr = (c->ConfigBase + reg->Offset) >> 1;
170 switch (reg->Action) {
172 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
177 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
180 dev_dbg(&s->dev, "Invalid conf register request\n");
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,
192 struct pcmcia_socket *s = p_dev->socket;
197 if (req->Page != 0) {
198 dev_dbg(&s->dev, "failure: requested page is zero\n");
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");
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,
218 struct pcmcia_socket *s;
222 c = p_dev->function_config;
224 if (!(s->state & SOCKET_PRESENT)) {
225 dev_dbg(&s->dev, "No card present\n");
228 if (!(c->state & CONFIG_LOCKED)) {
229 dev_dbg(&s->dev, "Configuration isnt't locked\n");
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;
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");
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");
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");
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");
268 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
269 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
273 io_on.speed = io_speed;
274 for (i = 0; i < MAX_IO_WIN; 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);
286 s->ops->set_io_map(s, &io_on);
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;
302 if (p_dev->_locked) {
304 if (--(s->lock_count) == 0) {
305 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
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++) {
318 if (s->io[i].Config != 0)
321 s->ops->set_io_map(s, &io);
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
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;
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))
353 c->state &= ~CONFIG_IO_REQ;
355 release_io_space(s, req->BasePort1, req->NumPorts1);
357 release_io_space(s, req->BasePort2, req->NumPorts2);
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;
372 if (c->state & CONFIG_LOCKED)
374 if (c->irq.Attributes != req->Attributes) {
375 dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
378 if (s->irq.AssignedIRQ != req->AssignedIRQ) {
379 dev_dbg(&s->dev, "IRQ must match assigned one\n");
382 if (--s->irq.Config == 0) {
383 c->state &= ~CONFIG_IRQ_REQ;
384 s->irq.AssignedIRQ = 0;
388 free_irq(req->AssignedIRQ, p_dev->priv);
390 #ifdef CONFIG_PCMCIA_PROBE
391 pcmcia_used_irq[req->AssignedIRQ]--;
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;
409 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
410 dev_dbg(&s->dev, "not releasing unknown window\n");
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 */
421 release_resource(win->res);
425 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
428 } /* pcmcia_release_window */
429 EXPORT_SYMBOL(pcmcia_release_window);
432 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
437 struct pcmcia_socket *s = p_dev->socket;
441 if (!(s->state & SOCKET_PRESENT))
444 if (req->IntType & INT_CARDBUS) {
445 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
448 c = p_dev->function_config;
449 if (c->state & CONFIG_LOCKED) {
450 dev_dbg(&s->dev, "Configuration is locked\n");
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");
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;
476 s->socket.io_irq = 0;
477 s->ops->set_socket(s, &s->socket);
480 /* Set up CIS configuration registers */
481 base = c->ConfigBase = req->ConfigBase;
482 c->CardValues = req->Present;
483 if (req->Present & PRESENT_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;
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);
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) {
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++)
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;
540 iomap.start = s->io[i].res->start;
541 iomap.stop = s->io[i].res->end;
542 s->ops->set_io_map(s, &iomap);
547 c->state |= CONFIG_LOCKED;
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;
564 if (!(s->state & SOCKET_PRESENT)) {
565 dev_dbg(&s->dev, "No card present\n");
571 c = p_dev->function_config;
572 if (c->state & CONFIG_LOCKED) {
573 dev_dbg(&s->dev, "Configuration is locked\n");
576 if (c->state & CONFIG_IO_REQ) {
577 dev_dbg(&s->dev, "IO already configured\n");
580 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
581 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
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");
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");
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);
608 c->state |= CONFIG_IO_REQ;
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)
632 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
634 struct pcmcia_socket *s = p_dev->socket;
636 int ret = -EINVAL, irq = 0;
639 if (!(s->state & SOCKET_PRESENT)) {
640 dev_dbg(&s->dev, "No card present\n");
643 c = p_dev->function_config;
644 if (c->state & CONFIG_LOCKED) {
645 dev_dbg(&s->dev, "Configuration is locked\n");
648 if (c->state & CONFIG_IRQ_REQ) {
649 dev_dbg(&s->dev, "IRQ already configured\n");
653 /* Decide what type of interrupt we are registering */
655 if (s->functions > 1) /* All of this ought to be handled higher up */
657 else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
660 printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
662 #ifdef CONFIG_PCMCIA_PROBE
665 /* if the underlying IRQ infrastructure allows for it, only allocate
666 * the IRQ, but do not enable it
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;
677 u32 mask = s->irq_mask;
678 void *data = p_dev; /* something unique to this device */
680 for (try = 0; try < 64; try++) {
683 /* marked as available by driver, and not blocked by userspace? */
684 if (!((mask >> irq) & 1))
687 /* avoid an IRQ which is already used by a PCMCIA card */
688 if ((try < 32) && pcmcia_used_irq[irq])
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,
698 (req->Handler) ? p_dev->priv : data);
707 /* only assign PCI irq if no IRQ already assigned */
708 if (ret && !s->irq.AssignedIRQ) {
710 dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
717 if (ret && req->Handler) {
718 ret = request_irq(irq, req->Handler, type,
719 p_dev->devname, p_dev->priv);
721 dev_printk(KERN_INFO, &s->dev,
722 "request_irq() failed\n");
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;
739 c->state |= CONFIG_IRQ_REQ;
742 #ifdef CONFIG_PCMCIA_PROBE
743 pcmcia_used_irq[irq]++;
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;
763 if (!(s->state & SOCKET_PRESENT)) {
764 dev_dbg(&s->dev, "No card present\n");
767 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
768 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
772 /* Window size defaults to smallest available */
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");
782 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
783 (req->Base & (align-1))) {
784 dev_dbg(&s->dev, "invalid base address\n");
790 /* Allocate system memory window */
791 for (w = 0; w < MAX_WIN; w++)
792 if (!(s->state & SOCKET_WIN_REQ(w)))
795 dev_dbg(&s->dev, "all windows are used already\n");
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);
805 dev_dbg(&s->dev, "allocating mem region failed\n");
809 p_dev->_win |= CLIENT_WIN_REQ(w);
811 /* Configure the socket controller */
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;
824 if (s->ops->set_mem_map(s, win) != 0) {
825 dev_dbg(&s->dev, "failed to set memory mapping\n");
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;
834 req->Base = win->res->start;
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);
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;
856 int (*conf_check) (struct pcmcia_device *p_dev,
857 cistpl_cftable_entry_t *cfg,
858 cistpl_cftable_entry_t *dflt,
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;
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,
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,
908 struct pcmcia_cfg_mem *cfg_mem;
911 cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
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);
926 EXPORT_SYMBOL(pcmcia_loop_config);
929 struct pcmcia_loop_mem {
930 struct pcmcia_device *p_dev;
932 int (*loop_tuple) (struct pcmcia_device *p_dev,
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,
969 struct pcmcia_loop_mem loop = {
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 {
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,
997 struct pcmcia_loop_get *get = priv;
999 *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1001 get->len = tuple->TupleDataLen;
1002 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
1004 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
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 = {
1027 pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
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,
1045 struct net_device *dev = priv;
1048 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1050 if (tuple->TupleDataLen < ETH_ALEN + 2) {
1051 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1056 if (tuple->TupleData[1] != ETH_ALEN) {
1057 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1060 for (i = 0; i < 6; i++)
1061 dev->dev_addr[i] = tuple->TupleData[i+2];
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);