pccard_device.c revision 150362
1/*- 2 * Copyright (c) 2005, M. Warner Losh 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard_device.c 150362 2005-09-20 06:47:33Z imp $"); 30 31#include <sys/param.h> 32#include <sys/conf.h> 33#include <sys/malloc.h> 34#include <sys/systm.h> 35#include <sys/uio.h> 36 37#include <sys/bus.h> 38#include <machine/bus.h> 39#include <sys/rman.h> 40#include <machine/resource.h> 41 42#include <dev/pccard/pccardreg.h> 43#include <dev/pccard/pccardvar.h> 44#include <dev/pccard/pccardvarp.h> 45#include <dev/pccard/pccard_cis.h> 46 47static d_open_t pccard_open; 48static d_close_t pccard_close; 49static d_read_t pccard_read; 50static d_ioctl_t pccard_ioctl; 51 52static struct cdevsw pccard_cdevsw = { 53 .d_version = D_VERSION, 54 .d_open = pccard_open, 55 .d_close = pccard_close, 56 .d_read = pccard_read, 57 .d_ioctl = pccard_ioctl, 58 .d_name = "pccard" 59}; 60 61int 62pccard_device_create(struct pccard_softc *sc) 63{ 64 sc->cisdev = make_dev(&pccard_cdevsw, 0, 0, 0, 0666, "pccard%u.cis", 65 device_get_unit(sc->dev)); 66 sc->cisdev->si_drv1 = sc; 67 return (0); 68} 69 70int 71pccard_device_destroy(struct pccard_softc *sc) 72{ 73 destroy_dev(sc->cisdev); 74 return (0); 75} 76 77static int 78pccard_build_cis(const struct pccard_tuple *tuple, void *argp) 79{ 80 struct cis_buffer *cis; 81 int i; 82 uint8_t ch; 83 84 cis = (struct cis_buffer *)argp; 85 /* 86 * CISTPL_END is a special case, it has no length field. 87 */ 88 if (tuple->code == CISTPL_END) { 89 if (cis->len + 1 > sizeof(cis->buffer)) 90 return (ENOSPC); 91 cis->buffer[cis->len++] = tuple->code; 92 return (0); 93 } 94 if (cis->len + 2 + tuple->code > sizeof(cis->buffer)) 95 return (ENOSPC); 96 cis->buffer[cis->len++] = tuple->code; 97 cis->buffer[cis->len++] = tuple->length; 98 for (i = 0; i < tuple->length; i++) { 99 ch = pccard_tuple_read_1(tuple, i); 100 cis->buffer[cis->len++] = ch; 101 } 102 return (0); 103} 104 105static int 106pccard_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 107{ 108 device_t parent, child; 109 device_t *kids; 110 int cnt, err; 111 struct pccard_softc *sc; 112 113 sc = dev->si_drv1; 114 if (sc->cis_open) 115 return (EBUSY); 116 parent = sc->dev; 117 err = device_get_children(parent, &kids, &cnt); 118 if (err) 119 return err; 120 if (cnt == 0) { 121 free(kids, M_TEMP); 122 sc->cis_open++; 123 sc->cis = NULL; 124 return (0); 125 } 126 child = kids[0]; 127 free(kids, M_TEMP); 128 sc->cis = malloc(sizeof(*sc->cis), M_TEMP, M_ZERO | M_WAITOK); 129 err = pccard_scan_cis(parent, child, pccard_build_cis, sc->cis); 130 if (err) { 131 free(sc->cis, M_TEMP); 132 sc->cis = NULL; 133 return (err); 134 } 135 sc->cis_open++; 136 return (0); 137} 138 139static int 140pccard_close(struct cdev *dev, int fflags, int devtype, struct thread *td) 141{ 142 struct pccard_softc *sc; 143 144 sc = dev->si_drv1; 145 free(sc->cis, M_TEMP); 146 sc->cis = NULL; 147 sc->cis_open = 0; 148 return (0); 149} 150 151static int 152pccard_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 153 struct thread *td) 154{ 155 return (ENOTTY); 156} 157 158static int 159pccard_read(struct cdev *dev, struct uio *uio, int ioflag) 160{ 161 struct pccard_softc *sc; 162 163 sc = dev->si_drv1; 164 /* EOF */ 165 if (sc->cis == NULL || uio->uio_offset > sc->cis->len) 166 return (0); 167 return (uiomove(sc->cis->buffer + uio->uio_offset, 168 MIN(uio->uio_resid, sc->cis->len - uio->uio_offset), uio)); 169} 170