2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 static const char rcsid[] =
29 "$FreeBSD: src/usr.sbin/pccard/pccardd/cardd.c,v 1.46.2.16 2001/12/10 14:38:19 iwasaki Exp $";
41 #include <machine/resource.h>
42 #include <sys/ioctl.h>
45 static struct card_config *assign_driver(struct slot *, struct card *);
46 static int assign_io(struct slot *);
47 static int setup_slot(struct slot *);
48 static void card_inserted(struct slot *);
49 static void card_removed(struct slot *);
50 static void pr_cmd(struct cmd *);
51 static void read_ether(struct slot *);
52 static void read_ether_attr2(struct slot *sp);
57 * Dump configuration file data.
60 dump_config_file(void)
63 struct card_config *confp;
65 for (cp = cards; cp; cp = cp->next) {
66 printf("Card manuf %s, vers %s\n", cp->manuf, cp->version);
67 printf("Configuration entries:\n");
68 for (confp = cp->config; confp; confp = confp->next) {
69 printf("\tIndex code = ");
70 switch (confp->index_type) {
78 printf("0x%x", confp->index);
81 printf(", driver name = %s\n", confp->driver->name);
84 printf("Insert commands are:\n");
88 printf("Remove commands are:\n");
96 pr_cmd(struct cmd *cp)
99 printf("\t%s\n", cp->line);
105 * readslots - read all the PCMCIA slots, and build
106 * a list of the slots.
116 for (i = 0; i < MAXSLOT; i++) {
117 sprintf(name, CARD_DEVICE, i);
118 fd = open(name, O_RDWR);
121 sp = xmalloc(sizeof(*sp));
123 sp->name = newstr(name);
126 /* Check to see if the controller memory has been set up. */
128 unsigned long mem = 0;
130 if (ioctl(fd, PIOCRWMEM, &mem))
131 logerr("ioctl (PIOCRWMEM)");
133 logmsg("mem=0x%x\n", mem);
136 mem = alloc_memory(4 * 1024);
138 die("can't allocate memory for "
139 "controller access");
140 if (ioctl(fd, PIOCRWMEM, &mem))
141 logerr("ioctl (PIOCRWMEM)");
152 * slot_change - Card status has changed.
153 * read new state and process.
156 slot_change(struct slot *sp)
158 struct slotstate state;
160 if (ioctl(sp->fd, PIOCGSTATE, &state)) {
161 logerr("ioctl (PIOCGSTATE)");
164 switch (state.state) {
168 /* Debounce potentially incorrectly reported removals */
169 if (state.laststate == filled || state.laststate == suspend)
173 /* KLUDGE: if we were suspended, remove card */
174 if (state.laststate == suspend)
182 sp->state = state.state;
187 * card_removed - card has been removed from slot.
188 * Execute the remove commands, and clear the slot's state.
189 * Execute the device commands, then the driver commands
190 * and then the card commands. This is the reverse
191 * order to the insertion commands
194 card_removed(struct slot *sp)
197 struct allocblk *sio;
200 if (sp->config && sp->config->driver && sp->card)
201 logmsg("%s%d: %s removed.", sp->config->driver->kernel,
202 sp->config->driver->unit, sp->card->logstr);
206 if (sp->config->inuse && sp->config->driver->inuse)
208 sp->config->inuse = 0;
209 sp->config->driver->inuse = 0;
211 if ((cp = sp->card) != 0 && in_use)
212 execute(cp->remove, sp);
216 if (sp->flags & IO_ASSIGNED)
217 for (sio = &sp->io; sio; sio = sio->next)
218 if (sio->addr && sio->size)
219 bit_nset(io_avail, sio->addr, sio->addr + sio->size - 1);
221 if (sp->flags & IRQ_ASSIGNED)
222 if (sp->irq >= 1 && sp->irq <= 15)
223 pool_irq[sp->irq] = 1;
226 /* CIS string comparison */
228 #define REGCOMP_FLAGS (REG_EXTENDED | REG_NOSUB)
229 #define REGEXEC_FLAGS (0)
232 cis_strcmp(char *db, char *cis)
244 if (n > 2 && db[0] == '/' && db[n-1] == '/') {
245 /* matching by regex */
248 /* otherwise, matching by strncmp() */
249 if (n != strlen(cis)) {
253 return strncmp(db, cis, n);
256 strncpy(p + 1, db, n-2);
259 if ((err = regcomp(&rx, p, REGCOMP_FLAGS))) {
260 regerror(err, &rx, buf, sizeof buf);
261 logmsg("Warning: REGEX error for\"%s\" -- %s\n", p, buf);
266 res = regexec(&rx, cis, 0, NULL, REGEXEC_FLAGS);
273 * card_inserted - Card has been inserted;
275 * - match the card type.
276 * - Match the driver and allocate a driver instance.
277 * - Allocate I/O ports, memory and IRQ.
279 * - assign the driver (if failed, then terminate).
280 * - Run the card commands.
281 * - Run the driver commands
282 * - Run the device commands
285 card_inserted(struct slot *sp)
290 usleep(pccard_init_sleep);
291 sp->cis = readcis(sp->fd);
293 logmsg("Error reading CIS on %s\n", sp->name);
299 for (cp = cards; cp; cp = cp->next) {
300 switch (cp->deftype) {
302 if (cis_strcmp(cp->manuf, sp->cis->manuf) == 0 &&
303 cis_strcmp(cp->version, sp->cis->vers) == 0) {
304 if (cp->add_info1 != NULL &&
305 cis_strcmp(cp->add_info1, sp->cis->add_info1) != 0) {
308 if (cp->add_info2 != NULL &&
309 cis_strcmp(cp->add_info2, sp->cis->add_info2) != 0) {
313 logmsg("Card \"%s\"(\"%s\") "
315 "matched \"%s\" (\"%s\") "
317 sp->cis->manuf, sp->cis->vers,
318 sp->cis->add_info1, sp->cis->add_info2,
319 cp->manuf, cp->version,
320 cp->add_info1, cp->add_info2);
325 if (cp->func_id == sp->cis->func_id1) {
326 logmsg("Card \"%s\"(\"%s\") "
328 "has function ID %d\n",
329 sp->cis->manuf, sp->cis->vers,
330 sp->cis->add_info1, sp->cis->add_info2,
336 logmsg("Unknown deftype %d\n", cp->deftype);
337 die("cardd.c:card_inserted()");
346 logmsg("No card in database for \"%s\"(\"%s\")",
347 sp->cis->manuf, sp->cis->vers);
350 if (sp->cis->lan_nid && sp->cis->lan_nid[0] == sizeof(sp->eaddr)) {
351 bcopy(sp->cis->lan_nid + 1, sp->eaddr, sizeof(sp->eaddr));
352 sp->flags |= EADDR_CONFIGED;
354 bzero(sp->eaddr, sizeof(sp->eaddr));
362 read_ether_attr2(sp);
369 if ((sp->config = assign_driver(sp, cp)) == NULL)
371 if ((err = assign_io(sp))) {
376 reason = "specified CIS was not found";
379 reason = "memory block allocation failed";
382 reason = "I/O block allocation failed";
385 reason = "requires more than one memory window";
391 logmsg("Resource allocation failure for \"%s\"(\"%s\") "
392 "[%s] [%s]; Reason %s\n",
393 sp->cis->manuf, sp->cis->vers,
394 sp->cis->add_info1, sp->cis->add_info2, reason);
400 * Once assigned, set up the I/O & mem contexts, set up the
401 * windows, and then attach the driver.
404 execute(cp->insert, sp);
412 * read_ether - read ethernet address from card. Offset is
413 * the offset into the attribute memory of the card.
416 read_ether(struct slot *sp)
418 unsigned char net_addr[12];
419 int flags = MDF_ATTR; /* attribute memory */
421 ioctl(sp->fd, PIOCRWFLAG, &flags);
422 lseek(sp->fd, (off_t)sp->card->ether->value, SEEK_SET);
423 if (read(sp->fd, net_addr, sizeof(net_addr)) != sizeof(net_addr)) {
424 logerr("read err on net addr");
427 sp->eaddr[0] = net_addr[0];
428 sp->eaddr[1] = net_addr[2];
429 sp->eaddr[2] = net_addr[4];
430 sp->eaddr[3] = net_addr[6];
431 sp->eaddr[4] = net_addr[8];
432 sp->eaddr[5] = net_addr[10];
433 logmsg("Ether=%02x:%02x:%02x:%02x:%02x:%02x\n",
434 sp->eaddr[0], sp->eaddr[1], sp->eaddr[2],
435 sp->eaddr[3], sp->eaddr[4], sp->eaddr[5]);
436 sp->flags |= EADDR_CONFIGED;
440 * Megahertz X-Jack Ethernet uses unique way to get/set MAC
441 * address of the card.
444 read_ether_attr2(struct slot *sp)
449 hexaddr = sp->cis->add_info2;
450 for (i = 0; i < 6; i++)
454 if (strlen(hexaddr) != 12)
456 for (i = 0; i < 12; i++)
457 if (!isxdigit(hexaddr[i]))
459 for (i = 0; i < 6; i++) {
462 s[0] = hexaddr[i * 2];
463 s[1] = hexaddr[i * 2 + 1];
465 if (!sscanf(s, "%x", &d)) {
467 for (j = 0; j < 6; j++)
471 sp->eaddr[i] = (u_char)d;
473 sp->flags |= EADDR_CONFIGED;
478 * assign_driver - Assign driver to card.
479 * First, see if an existing driver is already setup.
481 static struct card_config *
482 assign_driver(struct slot *sp, struct card *cp)
485 struct card_config *conf;
486 struct pccard_resource res;
490 for (conf = cp->config; conf; conf = conf->next)
491 if (conf->inuse == 0 && conf->driver->card == cp &&
492 conf->driver->config == conf &&
493 conf->driver->inuse == 0) {
494 if (debug_level > 0) {
495 logmsg("Found existing driver (%s) for %s\n",
496 conf->driver->name, cp->manuf);
498 conf->driver->inuse = 1;
503 * New driver must be allocated. Find the first configuration
506 for (conf = cp->config; conf; conf = conf->next)
507 if (conf->inuse == 0 && conf->driver->inuse/*card*/ == 0)
510 logmsg("No free configuration for card %s", cp->manuf);
514 * Now we have a free driver and a matching configuration.
515 * Before assigning and allocating everything, check to
516 * see if a device class can be allocated to this.
520 /* If none available, then we can't use this card. */
522 logmsg("Driver already being used for %s", cp->manuf);
527 * Allocate a free IRQ if none has been specified. When we're
528 * sharing interrupts (cardbus bridge case), then we'll use what
529 * the kernel tells us to use, reguardless of what the user
530 * configured. Asking the kernel for IRQ 0 is our way of asking
531 * if we should use a shared interrupt.
533 res.type = SYS_RES_IRQ;
535 if (conf->irq == 0) {
539 irqmin = irqmax = conf->irq;
540 conf->irq = 0; /* Make sure we get it. */
544 if (ioctl(sp->fd, PIOCSRESOURCE, &res) < 0)
545 err(1, "ioctl (PIOCSRESOURCE)");
546 if (res.resource_addr != ~0ul) {
547 conf->irq = res.resource_addr;
548 pool_irq[conf->irq] = 0;
550 for (i = irqmin; i <= irqmax; i++) {
552 * Skip irqs not in the pool.
554 if (pool_irq[i] == 0)
557 * -I forces us to use the interrupt, so use it.
565 * Ask the kernel if we have an free irq.
569 if (ioctl(sp->fd, PIOCSRESOURCE, &res) < 0)
570 err(1, "ioctl (PIOCSRESOURCE)");
571 if (res.resource_addr == ~0ul)
574 * res.resource_addr might be the kernel's
575 * better idea than i, so we have to check to
576 * see if that's in use too. If not, mark it
577 * in use and break out of the loop. I'm not
578 * sure this can happen when IRQ 0 above fails,
579 * but the test is cheap enough.
581 if (pool_irq[res.resource_addr] == 0)
583 conf->irq = res.resource_addr;
584 pool_irq[conf->irq] = 0;
588 if (conf->irq == 0) {
589 logmsg("Failed to allocate IRQ for %s\n", cp->manuf);
600 * Auto select config index
602 static struct cis_config *
603 assign_card_index(struct slot *sp, struct cis * cis)
605 struct cis_config *cp;
606 struct cis_ioblk *cio;
607 struct pccard_resource res;
610 res.type = SYS_RES_IOPORT;
611 for (cp = cis->conf; cp; cp = cp->next) {
612 if (!cp->iospace || !cp->io)
614 for (cio = cp->io; cio; cio = cio->next) {
615 res.size = cio->size;
617 res.max = res.min + cio->size - 1;
618 if (ioctl(sp->fd, PIOCSRESOURCE, &res) < 0)
619 err(1, "ioctl (PIOCSRESOURCE)");
620 if (res.resource_addr != cio->addr)
622 for (i = cio->addr; i < cio->addr + cio->size - 1; i++)
623 if (!bit_test(io_avail, i))
626 return cp; /* found */
629 return cis->def_config;
633 * assign_io - Allocate resources to slot matching the
634 * configuration index selected.
637 assign_io(struct slot *sp)
640 struct cis_config *cisconf, *defconf;
643 defconf = cis->def_config;
644 switch (sp->config->index_type) {
645 case DEFAULT_INDEX: /* default */
647 sp->config->index = cisconf->id;
649 case AUTO_INDEX: /* auto */
650 cisconf = assign_card_index(sp, cis);
651 sp->config->index = cisconf->id;
653 default: /* normal, use index value */
654 for (cisconf = cis->conf; cisconf; cisconf = cisconf->next)
655 if (cisconf->id == sp->config->index)
660 logmsg("Config id %d not present in this card",
664 sp->card_config = cisconf;
667 * Found a matching configuration. Now look at the I/O, memory and IRQ
668 * to create the desired parameters. Look at memory first.
671 /* Skip ed cards in PIO mode */
672 if ((strncmp(sp->config->driver->name, "ed", 2) == 0) &&
673 (sp->config->flags & 0x10))
676 if (cisconf->memspace || (defconf && defconf->memspace)) {
677 struct cis_memblk *mp;
681 * Currently we do not handle the presence of multiple windows.
682 * Then again neither does the interface to the kernel!
683 * See setup_slot() and readcis.c:cis_conf()
685 if (cisconf->memwins > 1) {
686 logmsg("Card requires %d memory windows.",
691 if (!cisconf->memspace)
693 sp->mem.size = mp->length;
694 sp->mem.cardaddr = mp->address;
696 /* For now, we allocate our own memory from the pool. */
697 sp->mem.addr = sp->config->driver->mem;
699 * Host memory address is required. Allocate one
702 if (sp->mem.size && sp->mem.addr == 0) {
703 sp->mem.addr = alloc_memory(mp->length);
704 if (sp->mem.addr == 0)
706 sp->config->driver->mem = sp->mem.addr;
708 /* Driver specific set up */
709 if (strncmp(sp->config->driver->name, "ed", 2) == 0) {
710 sp->mem.cardaddr = 0x4000;
711 sp->mem.flags = MDF_ACTIVE | MDF_16BITS;
713 sp->mem.flags = MDF_ACTIVE | MDF_16BITS;
716 if (sp->mem.flags & MDF_ACTIVE)
717 sp->flags |= MEM_ASSIGNED;
718 if (debug_level > 0) {
719 logmsg("Using mem addr 0x%x, size %d, card addr 0x%x, flags 0x%x\n",
720 sp->mem.addr, sp->mem.size, sp->mem.cardaddr,
726 /* Now look at I/O. */
727 bzero(&sp->io, sizeof(sp->io));
728 if (cisconf->iospace || (defconf && defconf->iospace)
729 || sp->card->iosize) {
730 struct cis_config *cp;
731 struct cis_ioblk *cio;
732 struct allocblk *sio;
737 if (!cisconf->iospace)
739 iosize = sp->card->iosize;
744 iosize = cp->io->size;
746 iosize = 1 << cp->io_addr;
750 * If # of I/O lines decoded == 10, then card does its
753 * If an I/O block exists, then use it.
754 * If no address (but a length) is available, allocate
760 if (iosize == 0 && cio)
761 xmax = cisconf->io_blks;
762 for (x = 0; x < xmax; x++) {
767 sio->addr = cio->addr;
768 sio->size = cio->size;
771 * No I/O block, assume the address lines
772 * decode gives the size.
774 sio->size = 1 << cp->io_addr;
776 if (sio->addr == 0) {
777 struct pccard_resource res;
780 res.type = SYS_RES_IOPORT;
781 res.size = sio->size;
783 for (i = 0; i < IOPORTS; i++) {
784 j = bit_fns(io_avail, IOPORTS, i,
785 sio->size, sio->size);
786 if ((j & (sio->size - 1)) != 0)
789 res.max = j + sio->size - 1;
790 if (ioctl(sp->fd, PIOCSRESOURCE, &res) < 0)
791 err(1, "ioctl (PIOCSRESOURCE)");
792 if (res.resource_addr == j)
801 bit_nclear(io_avail, sio->addr,
802 sio->addr + sio->size - 1);
803 sp->flags |= IO_ASSIGNED;
805 /* Set up the size to take into account the decode lines. */
806 sio->cardaddr = cp->io_addr;
807 switch (cp->io_bus) {
811 sio->flags = IODF_WS;
814 sio->flags = IODF_WS | IODF_CS16;
817 sio->flags = IODF_WS | IODF_CS16 | IODF_16BIT;
820 if (debug_level > 0) {
821 logmsg("Using I/O addr 0x%x, size %d\n",
822 sio->addr, sio->size);
824 if (cio && cio->next) {
825 sio->next = xmalloc(sizeof(*sio));
831 sp->irq = sp->config->irq;
832 sp->flags |= IRQ_ASSIGNED;
837 * setup_slot - Allocate the I/O and memory contexts
838 * return true if completed OK.
841 setup_slot(struct slot *sp)
846 struct driver *drvp = sp->config->driver;
847 struct allocblk *sio;
854 memset(&io, 0, sizeof io);
855 memset(&drv, 0, sizeof drv);
856 offs = sp->cis->reg_addr;
858 ioctl(sp->fd, PIOCRWFLAG, &rw_flags);
859 #if RESET_MAY_BE_HARMFUL
860 lseek(sp->fd, offs, SEEK_SET);
862 write(sp->fd, &c, sizeof(c));
863 usleep(sp->card->reset_time * 1000);
864 lseek(sp->fd, offs, SEEK_SET);
866 write(sp->fd, &c, sizeof(c));
867 usleep(sp->card->reset_time * 1000);
869 lseek(sp->fd, offs, SEEK_SET);
870 c = sp->config->index;
872 write(sp->fd, &c, sizeof(c));
873 if (debug_level > 0) {
874 logmsg("Setting config reg at offs 0x%lx to 0x%x, "
875 "Reset time = %d ms\n", (unsigned long)offs, c,
876 sp->card->reset_time);
878 usleep(pccard_init_sleep);
879 usleep(sp->card->reset_time * 1000);
881 /* If other config registers exist, set them up. */
882 if (sp->cis->ccrs & 2) {
885 if (sp->cis->def_config && sp->cis->def_config->misc_valid &&
886 (sp->cis->def_config->misc & 0x8))
888 if (sp->card_config->io_bus == 1)
890 lseek(sp->fd, offs + 2, SEEK_SET);
891 write(sp->fd, &c, sizeof(c));
893 if (sp->flags & MEM_ASSIGNED) {
895 mem.flags = sp->mem.flags;
896 mem.start = (caddr_t) sp->mem.addr;
897 mem.card = sp->mem.cardaddr;
898 mem.size = sp->mem.size;
899 if (ioctl(sp->fd, PIOCSMEM, &mem)) {
900 logerr("ioctl (PIOCSMEM)");
904 if (sp->flags & IO_ASSIGNED) {
905 for (iowin = 0, sio = &(sp->io); iowin <= 1; iowin++) {
908 io.flags = sio->flags;
909 io.start = sio->addr;
913 io.start = sp->io.addr & ~((1 << sp->io.cardaddr) - 1);
914 io.size = 1 << sp->io.cardaddr;
915 if (io.start < 0x100) {
920 if (debug_level > 0) {
921 logmsg("Assigning I/O window %d, start 0x%x, "
922 "size 0x%x flags 0x%x\n", io.window, io.start,
925 io.flags |= IODF_ACTIVE;
926 if (ioctl(sp->fd, PIOCSIO, &io)) {
927 logerr("ioctl (PIOCSIO)");
930 if (ioctl(sp->fd, PIOCGIO, &io))
932 logerr("ioctl (PIOCGIO)");
935 if (io.start != sio->addr){
936 logmsg("I/O base address changed from 0x%x to 0x%x\n",
937 sio->addr, io.start);
938 sio->addr = io.start;
946 strcpy(drv.name, drvp->kernel);
947 drv.unit = drvp->unit;
948 drv.irqmask = 1 << sp->irq;
949 drv.flags = sp->config->flags;
950 if (sp->flags & MEM_ASSIGNED) {
951 drv.mem = sp->mem.addr;
952 drv.memsize = sp->mem.size;
957 if (sp->flags & IO_ASSIGNED)
958 drv.iobase = sp->io.addr;
961 #ifdef DEV_DESC_HAS_SIZE
962 drv.iosize = sp->io.size;
964 if (debug_level > 0) {
965 logmsg("Assign %s%d, io 0x%x-0x%x, mem 0x%lx, %d bytes, "
966 "irq %d, flags %x\n", drv.name, drv.unit, drv.iobase,
967 drv.iobase + sp->io.size - 1, drv.mem, drv.memsize,
971 * If the driver fails to be connected to the device,
972 * then it may mean that the driver did not recognise it.
974 memcpy(drv.misc, sp->eaddr, 6);
975 if (ioctl(sp->fd, PIOCSDRV, &drv)) {
976 logmsg("driver allocation failed for %s(%s): %s",
977 sp->card->manuf, sp->card->version, strerror(errno));
980 drv.name[sizeof(drv.name) - 1] = '\0';
981 if (strncmp(drv.name, drvp->kernel, sizeof(drv.name))) {
982 drvp->kernel = newstr(drv.name);
985 if (*p >= '0' && *p <= '9') {
986 drvp->unit = atoi(p);
991 logmsg("%s%d: %s inserted.", sp->config->driver->kernel,
992 sp->config->driver->unit, sp->card->logstr);