From owner-p4-projects@FreeBSD.ORG Mon Sep 18 15:07:12 2006 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 9C0B916A47B; Mon, 18 Sep 2006 15:07:12 +0000 (UTC) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 752C816A415 for ; Mon, 18 Sep 2006 15:07:12 +0000 (UTC) (envelope-from trhodes@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id B929D43D7B for ; Mon, 18 Sep 2006 15:07:10 +0000 (GMT) (envelope-from trhodes@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.6/8.13.6) with ESMTP id k8IF7Aib007716 for ; Mon, 18 Sep 2006 15:07:10 GMT (envelope-from trhodes@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.6/8.13.4/Submit) id k8IF7AvE007713 for perforce@freebsd.org; Mon, 18 Sep 2006 15:07:10 GMT (envelope-from trhodes@freebsd.org) Date: Mon, 18 Sep 2006 15:07:10 GMT Message-Id: <200609181507.k8IF7AvE007713@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to trhodes@freebsd.org using -f From: Tom Rhodes To: Perforce Change Reviews Cc: Subject: PERFORCE change 106298 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Sep 2006 15:07:12 -0000 http://perforce.freebsd.org/chv.cgi?CH=106298 Change 106298 by trhodes@trhodes_local on 2006/09/18 15:06:35 IFC. Affected files ... .. //depot/projects/usb/src/sys/dev/usb/sl811hs.c#5 integrate Differences ... ==== //depot/projects/usb/src/sys/dev/usb/sl811hs.c#5 (text+ko) ==== @@ -1,0 +1,1654 @@ +/* $NetBSD: sl811hs.c,v 1.5 2005/02/27 00:27:02 perry Exp $ */ + +/* + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Tetsuya Isaki. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +/* + * ScanLogic SL811HS/T USB Host Controller + */ +/* + * !! HIGHLY EXPERIMENTAL CODE !! + */ + +#include +//_RCSID(0, "$NetBSD: sl811hs.c,v 1.5 2005/02/27 00:27:02 perry Exp $"); + +#include "opt_slhci.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include "usbdevs.h" + +#include +#include + +__FBSDID("$FreeBSD: src/sys/dev/usb/sl811hs.c,v 1.4 2006/09/07 00:06:41 imp Exp $"); + +static inline u_int8_t sl11read(struct slhci_softc *, int); +static inline void sl11write(struct slhci_softc *, int, u_int8_t); +static inline void sl11read_region(struct slhci_softc *, u_char *, int, int); +static inline void sl11write_region(struct slhci_softc *, int, u_char *, int); + +static void sl11_reset(struct slhci_softc *); +static void sl11_speed(struct slhci_softc *); + +static usbd_status slhci_open(usbd_pipe_handle); +static void slhci_softintr(void *); +static void slhci_poll(struct usbd_bus *); +static void slhci_poll_hub(void *); +static void slhci_poll_device(void *arg); +static usbd_status slhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t); +static void slhci_freem(struct usbd_bus *, usb_dma_t *); +static usbd_xfer_handle slhci_allocx(struct usbd_bus *); +static void slhci_freex(struct usbd_bus *, usbd_xfer_handle); + +static int slhci_str(usb_string_descriptor_t *, int, const char *); + +static usbd_status slhci_root_ctrl_transfer(usbd_xfer_handle); +static usbd_status slhci_root_ctrl_start(usbd_xfer_handle); +static void slhci_root_ctrl_abort(usbd_xfer_handle); +static void slhci_root_ctrl_close(usbd_pipe_handle); +static void slhci_root_ctrl_done(usbd_xfer_handle); + +static usbd_status slhci_root_intr_transfer(usbd_xfer_handle); +static usbd_status slhci_root_intr_start(usbd_xfer_handle); +static void slhci_root_intr_abort(usbd_xfer_handle); +static void slhci_root_intr_close(usbd_pipe_handle); +static void slhci_root_intr_done(usbd_xfer_handle); + +static usbd_status slhci_device_ctrl_transfer(usbd_xfer_handle); +static usbd_status slhci_device_ctrl_start(usbd_xfer_handle); +static void slhci_device_ctrl_abort(usbd_xfer_handle); +static void slhci_device_ctrl_close(usbd_pipe_handle); +static void slhci_device_ctrl_done(usbd_xfer_handle); + +static usbd_status slhci_device_intr_transfer(usbd_xfer_handle); +static usbd_status slhci_device_intr_start(usbd_xfer_handle); +static void slhci_device_intr_abort(usbd_xfer_handle); +static void slhci_device_intr_close(usbd_pipe_handle); +static void slhci_device_intr_done(usbd_xfer_handle); + +static usbd_status slhci_device_isoc_transfer(usbd_xfer_handle); +static usbd_status slhci_device_isoc_start(usbd_xfer_handle); +static void slhci_device_isoc_abort(usbd_xfer_handle); +static void slhci_device_isoc_close(usbd_pipe_handle); +static void slhci_device_isoc_done(usbd_xfer_handle); + +static usbd_status slhci_device_bulk_transfer(usbd_xfer_handle); +static usbd_status slhci_device_bulk_start(usbd_xfer_handle); +static void slhci_device_bulk_abort(usbd_xfer_handle); +static void slhci_device_bulk_close(usbd_pipe_handle); +static void slhci_device_bulk_done(usbd_xfer_handle); + +static int slhci_transaction(struct slhci_softc *, + usbd_pipe_handle, u_int8_t, int, u_char *, u_int8_t); +static void slhci_noop(usbd_pipe_handle); +static void slhci_abort_xfer(usbd_xfer_handle, usbd_status); +static void slhci_device_clear_toggle(usbd_pipe_handle); + +extern int usbdebug; +int slhci_dummy; + +/* For root hub */ +#define SLHCI_INTR_ENDPT (1) + +#ifdef SLHCI_DEBUG +#define D_TRACE (0x0001) /* function trace */ +#define D_MSG (0x0002) /* debug messages */ +#define D_XFER (0x0004) /* transfer messages (noisy!) */ +#define D_MEM (0x0008) /* memory allocation */ + +int slhci_debug = D_MSG | D_XFER; +#define DPRINTF(z,x) if((slhci_debug&(z))!=0)printf x +void print_req(usb_device_request_t *); +void print_req_hub(usb_device_request_t *); +void print_dumpreg(struct slhci_softc *); +void print_xfer(usbd_xfer_handle); +#else +#define DPRINTF(z,x) +#endif + + +/* XXX: sync with argument */ +static const char *sltypestr [] = { + "SL11H/T", + "SL811HS/T", +}; + + +struct usbd_bus_methods slhci_bus_methods = { + slhci_open, + slhci_softintr, + slhci_poll, + slhci_allocm, + slhci_freem, + slhci_allocx, + slhci_freex, +}; + +struct usbd_pipe_methods slhci_root_ctrl_methods = { + slhci_root_ctrl_transfer, + slhci_root_ctrl_start, + slhci_root_ctrl_abort, + slhci_root_ctrl_close, + slhci_noop, + slhci_root_ctrl_done, +}; + +struct usbd_pipe_methods slhci_root_intr_methods = { + slhci_root_intr_transfer, + slhci_root_intr_start, + slhci_root_intr_abort, + slhci_root_intr_close, + slhci_noop, + slhci_root_intr_done, +}; + +struct usbd_pipe_methods slhci_device_ctrl_methods = { + slhci_device_ctrl_transfer, + slhci_device_ctrl_start, + slhci_device_ctrl_abort, + slhci_device_ctrl_close, + slhci_noop, + slhci_device_ctrl_done, +}; + +struct usbd_pipe_methods slhci_device_intr_methods = { + slhci_device_intr_transfer, + slhci_device_intr_start, + slhci_device_intr_abort, + slhci_device_intr_close, + slhci_device_clear_toggle, + slhci_device_intr_done, +}; + +struct usbd_pipe_methods slhci_device_isoc_methods = { + slhci_device_isoc_transfer, + slhci_device_isoc_start, + slhci_device_isoc_abort, + slhci_device_isoc_close, + slhci_noop, + slhci_device_isoc_done, +}; + +struct usbd_pipe_methods slhci_device_bulk_methods = { + slhci_device_bulk_transfer, + slhci_device_bulk_start, + slhci_device_bulk_abort, + slhci_device_bulk_close, + slhci_noop, + slhci_device_bulk_done, +}; + +struct slhci_pipe { + struct usbd_pipe pipe; +}; + + +/* + * SL811HS Register read/write routine + */ +static inline u_int8_t +sl11read(struct slhci_softc *sc, int reg) +{ +#if 1 + int b; + DELAY(80); + bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg); + b = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA); + return b; +#else + outb(0x4000, reg&0xff); + return (inb(0x4001)&0xff); +#endif +} + +static inline void +sl11write(struct slhci_softc *sc, int reg, u_int8_t data) +{ +#if 1 + DELAY(80); + bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg); + bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA, data); +#else + outb(0x4000, reg&0xff); + outb(0x4000, data&0xff); +#endif +} + +static inline void +sl11read_region(struct slhci_softc *sc, u_char *buf, int reg, int len) +{ + int i; + bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg); + for (i = 0; i < len; i++) + buf[i] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA); +} + +static inline void +sl11write_region(struct slhci_softc *sc, int reg, u_char *buf, int len) +{ + int i; + bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg); + for (i = 0; i < len; i++) + bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA, buf[i]); +} + +/* + * USB bus reset. From sl811hs_appnote.pdf, p22 + */ +static void +sl11_reset(struct slhci_softc *sc) +{ + u_int8_t r; + + DPRINTF(D_TRACE, ("%s() ", __FUNCTION__)); + // r = sl11read(sc, SL11_CTRL); + r = 0; + sl11write(sc, SL11_CTRL, r | SL11_CTRL_RESETENGINE); + delay_ms(250); + sl11write(sc, SL11_CTRL, r | SL11_CTRL_JKSTATE | SL11_CTRL_RESETENGINE); delay_ms(150); + sl11write(sc, SL11_CTRL, r | SL11_CTRL_RESETENGINE); + delay_ms(10); + sl11write(sc, SL11_CTRL, r); +} + +/* + * Detect the speed of attached device. + */ +static void +sl11_speed(struct slhci_softc *sc) +{ + u_int8_t r; + + sl11write(sc, SL11_ISR, 0xff); + r = sl11read(sc, SL11_ISR); + if ((r & SL11_ISR_RESET)) { + DPRINTF(D_MSG, ("NC ")); + sl11write(sc, SL11_ISR, SL11_ISR_RESET); + sc->sc_connect = 0; + } + + if ((sl11read(sc, SL11_ISR) & SL11_ISR_RESET)) { + sl11write(sc, SL11_ISR, 0xff); + } else { + u_int8_t pol = 0, ctrl = 0; + + sc->sc_connect = 1; + if (r & SL11_ISR_DATA) { + DPRINTF(D_MSG, ("FS ")); + pol = 0; + ctrl = SL11_CTRL_EOF2; + sc->sc_fullspeed = 1; + } else { + DPRINTF(D_MSG, ("LS ")); + pol = SL811_CSOF_POLARITY; + ctrl = SL11_CTRL_LOWSPEED; + sc->sc_fullspeed = 0; + } + sl11write(sc, SL811_CSOF, pol | SL811_CSOF_MASTER | 0x2e); + sl11write(sc, SL11_DATA, 0xe0); + sl11write(sc, SL11_CTRL, ctrl | SL11_CTRL_ENABLESOF); + } + + sl11write(sc, SL11_E0PID, (SL11_PID_SOF << 4) + 0); + sl11write(sc, SL11_E0DEV, 0); + sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM); + delay_ms(30); +} + +/* + * If detect some known controller, return the type. + * If does not, return -1. + */ +int +sl811hs_find(struct slhci_softc *sc) +{ + int rev; + sc->sc_sltype = -1; + + rev = sl11read(sc, SL11_REV) >> 4; + if (rev >= SLTYPE_SL11H && rev <= SLTYPE_SL811HS_R14) + sc->sc_sltype = rev; + return sc->sc_sltype; +} + +/* + * Attach SL11H/SL811HS. Return 0 if success. + */ +int +slhci_attach(struct slhci_softc *sc) +{ + int rev; + /* Detect and check the controller type */ + + rev = sl811hs_find(sc); + if (rev == -1) + return -1; + + printf("%s: ScanLogic %s USB Host Controller", + device_get_nameunit(sc->sc_bus.bdev), sltypestr[(rev > 0)]); + switch (rev) { + case SLTYPE_SL11H: + break; + case SLTYPE_SL811HS_R12: + printf(" (rev 1.2)"); + break; + case SLTYPE_SL811HS_R14: + printf(" (rev 1.4)"); + break; + default: + printf(" (unknown revision)"); + break; + } + printf("\n"); + + + /* Initialize sc */ + sc->sc_bus.usbrev = USBREV_1_1; + sc->sc_bus.methods = &slhci_bus_methods; + sc->sc_bus.pipe_size = sizeof(struct slhci_pipe); + sc->sc_bus.parent_dmatag = NULL; /* XXX */ + sc->sc_bus.buffer_dmatag = NULL; /* XXX */ + STAILQ_INIT(&sc->sc_free_xfers); + + usb_callout_init(sc->sc_poll_handle); + + /* Disable interrupt, then wait 40msec */ + sl11write(sc, SL11_IER, 0x00); + delay_ms(40); + + /* Initialize controller */ + sl11write(sc, SL811_CSOF, SL811_CSOF_MASTER | 0x2e); + delay_ms(40); + + sl11write(sc, SL11_ISR, 0xff); + + /* Disable interrupt, then wait 40msec */ + sl11write(sc, SL11_IER, 0x00); + + /* Reset USB engine */ + sl11write(sc, SL11_CTRL, SL11_CTRL_JKSTATE| SL11_CTRL_RESETENGINE); + delay_ms(40); + sl11write(sc, SL11_CTRL, 0x00); + delay_ms(10); + + /* USB Bus reset for GET_PORT_STATUS */ + sl11_reset(sc); + sc->sc_flags = SLF_ATTACHED; + + /* Enable interrupt */ + sl11write(sc, SL11_IER, SL11_IER_INSERT); + /* x68k Nereid USB controller needs it */ + if (sc->sc_enable_intr) + sc->sc_enable_intr(sc->sc_arg, INTR_ON); +#ifdef USB_DEBUG + usbdebug = 0; +#endif + + return 0; +} + +int +slhci_intr(void *arg) +{ + struct slhci_softc *sc = arg; + u_int8_t r; +#ifdef SLHCI_DEBUG + char bitbuf[256]; +#endif + + + if((sc->sc_flags & SLF_ATTACHED) == 0) + return 0; + + r = sl11read(sc, SL11_ISR); + + + + sl11write(sc, SL11_ISR, SL11_ISR_DATA | SL11_ISR_SOFTIMER); + + if ((r & SL11_ISR_RESET)) { + sc->sc_flags |= SLF_RESET; + sl11write(sc, SL11_ISR, SL11_ISR_RESET); + } + if ((r & SL11_ISR_INSERT)) { + sc->sc_flags |= SLF_INSERT; + sl11write(sc, SL11_ISR, SL11_ISR_INSERT); + } + +#ifdef SLHCI_DEBUG + bitmask_snprintf(r, + (sl11read(sc, SL11_CTRL) & SL11_CTRL_SUSPEND) + ? "\20\x8""D+\7RESUME\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA" + : "\20\x8""D+\7RESET\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA", + bitbuf, sizeof(bitbuf)); + DPRINTF(D_XFER, ("I=%s ", bitbuf)); +#endif /* SLHCI_DEBUG */ + + return 0; +} + +usbd_status +slhci_open(usbd_pipe_handle pipe) +{ + usbd_device_handle dev = pipe->device; + struct slhci_softc *sc = (struct slhci_softc *)dev->bus; + usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc; + + DPRINTF(D_TRACE, ("slhci_open(addr=%d,ep=%d,scaddr=%d)", + dev->address, ed->bEndpointAddress, sc->sc_addr)); + + if (dev->address == sc->sc_addr) { + switch (ed->bEndpointAddress) { + case USB_CONTROL_ENDPOINT: + pipe->methods = &slhci_root_ctrl_methods; + break; + case UE_DIR_IN | SLHCI_INTR_ENDPT: + pipe->methods = &slhci_root_intr_methods; + break; + default: + printf("open:endpointErr!\n"); + return USBD_INVAL; + } + } else { + switch (ed->bmAttributes & UE_XFERTYPE) { + case UE_CONTROL: + DPRINTF(D_MSG, ("control ")); + pipe->methods = &slhci_device_ctrl_methods; + break; + case UE_INTERRUPT: + DPRINTF(D_MSG, ("interrupt ")); + pipe->methods = &slhci_device_intr_methods; + break; + case UE_ISOCHRONOUS: + DPRINTF(D_MSG, ("isochronous ")); + pipe->methods = &slhci_device_isoc_methods; + break; + case UE_BULK: + DPRINTF(D_MSG, ("bluk ")); + pipe->methods = &slhci_device_bulk_methods; + break; + } + } + return USBD_NORMAL_COMPLETION; +} + +void +slhci_softintr(void *arg) +{ + DPRINTF(D_TRACE, ("%s()", __FUNCTION__)); +} + +void +slhci_poll(struct usbd_bus *bus) +{ + DPRINTF(D_TRACE, ("%s()", __FUNCTION__)); +} + +/* + * Emulation of interrupt transfer for status change endpoint + * of root hub. + */ +void +slhci_poll_hub(void *arg) +{ + usbd_xfer_handle xfer = arg; + usbd_pipe_handle pipe = xfer->pipe; + struct slhci_softc *sc = (struct slhci_softc *)pipe->device->bus; + int s; + u_char *p; + + usb_callout(sc->sc_poll_handle, sc->sc_interval, slhci_poll_hub, xfer); + + /* USB spec 11.13.3 (p.260) */ + p = xfer->buffer; + p[0] = 0; + if ((sc->sc_flags & (SLF_INSERT | SLF_RESET))) { + p[0] = 2; + DPRINTF(D_TRACE, ("!")); + } + + /* no change, return NAK */ + if (p[0] == 0) + return; + + xfer->actlen = 1; + xfer->status = USBD_NORMAL_COMPLETION; + s = splusb(); + xfer->device->bus->intr_context++; + usb_transfer_complete(xfer); + xfer->device->bus->intr_context--; + splx(s); +} + +usbd_status +slhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size) +{ + struct slhci_softc *sc = (struct slhci_softc *)bus; + + DPRINTF(D_MEM, ("SLallocm")); + return usb_allocmem(&sc->sc_bus, size, 0, dma); +} + +void +slhci_freem(struct usbd_bus *bus, usb_dma_t *dma) +{ + struct slhci_softc *sc = (struct slhci_softc *)bus; + + DPRINTF(D_MEM, ("SLfreem")); + usb_freemem(&sc->sc_bus, dma); +} + +usbd_xfer_handle +slhci_allocx(struct usbd_bus *bus) +{ + struct slhci_softc *sc = (struct slhci_softc *)bus; + usbd_xfer_handle xfer; + + DPRINTF(D_MEM, ("SLallocx")); + + xfer = STAILQ_FIRST(&sc->sc_free_xfers); + if (xfer) { + STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next); +#ifdef DIAGNOSTIC + if (xfer->busy_free != XFER_FREE) { + printf("slhci_allocx: xfer=%p not free, 0x%08x\n", + xfer, xfer->busy_free); + } +#endif + } else { + xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT); + } + + if (xfer) { + memset(xfer, 0, sizeof(*xfer)); +#ifdef DIAGNOSTIC + xfer->busy_free = XFER_BUSY; +#endif + } + + return xfer; +} + +void +slhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer) +{ + struct slhci_softc *sc = (struct slhci_softc *)bus; + + DPRINTF(D_MEM, ("SLfreex")); + +#ifdef DIAGNOSTIC + if (xfer->busy_free != XFER_BUSY) { + printf("slhci_freex: xfer=%p not busy, 0x%08x\n", + xfer, xfer->busy_free); + return; + } + xfer->busy_free = XFER_FREE; +#endif + STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next); +} + +void +slhci_noop(usbd_pipe_handle pipe) +{ + DPRINTF(D_TRACE, ("%s()", __FUNCTION__)); +} + +/* + * Data structures and routines to emulate the root hub. + */ +usb_device_descriptor_t slhci_devd = { + USB_DEVICE_DESCRIPTOR_SIZE, + UDESC_DEVICE, /* type */ + {0x01, 0x01}, /* USB version */ + UDCLASS_HUB, /* class */ + UDSUBCLASS_HUB, /* subclass */ + 0, /* protocol */ + 64, /* max packet */ + {USB_VENDOR_SCANLOGIC & 0xff, /* vendor ID (low) */ + USB_VENDOR_SCANLOGIC >> 8 }, /* vendor ID (high) */ + {0} /* ? */, /* product ID */ + {0}, /* device */ + 1, /* index to manufacturer */ + 2, /* index to product */ + 0, /* index to serial number */ + 1 /* number of configurations */ +}; + +usb_config_descriptor_t slhci_confd = { + USB_CONFIG_DESCRIPTOR_SIZE, + UDESC_CONFIG, + {USB_CONFIG_DESCRIPTOR_SIZE + + USB_INTERFACE_DESCRIPTOR_SIZE + + USB_ENDPOINT_DESCRIPTOR_SIZE}, + 1, /* number of interfaces */ + 1, /* configuration value */ + 0, /* index to configuration */ + UC_SELF_POWERED, /* attributes */ + 15 /* max current is 30mA... */ +}; + +usb_interface_descriptor_t slhci_ifcd = { + USB_INTERFACE_DESCRIPTOR_SIZE, + UDESC_INTERFACE, + 0, /* interface number */ + 0, /* alternate setting */ + 1, /* number of endpoint */ + UICLASS_HUB, /* class */ + UISUBCLASS_HUB, /* subclass */ + 0, /* protocol */ + 0 /* index to interface */ +}; + +usb_endpoint_descriptor_t slhci_endpd = { + USB_ENDPOINT_DESCRIPTOR_SIZE, + UDESC_ENDPOINT, + UE_DIR_IN | SLHCI_INTR_ENDPT, /* endpoint address */ + UE_INTERRUPT, /* attributes */ + {8}, /* max packet size */ + 255 /* interval */ +}; + +usb_hub_descriptor_t slhci_hubd = { + USB_HUB_DESCRIPTOR_SIZE, + UDESC_HUB, + 1, /* number of ports */ + {UHD_PWR_INDIVIDUAL | UHD_OC_NONE, 0}, /* hub characteristics */ + 20 /* ? */, /* 5:power on to power good */ + 50, /* 6:maximum current */ + { 0x00 }, /* both ports are removable */ + { 0x00 } /* port power control mask */ +}; + +static int +slhci_str(usb_string_descriptor_t *p, int l, const char *s) +{ + int i; + + if (l == 0) + return 0; + p->bLength = 2 * strlen(s) + 2; + if (l == 1) + return 1; + p->bDescriptorType = UDESC_STRING; + l -= 2; + for (i = 0; s[i] && l > 1; i++, l -= 2) + USETW2(p->bString[i], 0, s[i]); + return 2 * i + 2; +} + +usbd_status +slhci_root_ctrl_transfer(usbd_xfer_handle xfer) +{ + usbd_status error; + + DPRINTF(D_TRACE, ("SLRCtrans ")); + + /* Insert last in queue */ + error = usb_insert_transfer(xfer); + if (error) { + DPRINTF(D_MSG, ("usb_insert_transfer returns err! ")); + return error; + } + + /* + * Pipe isn't running (otherwise error would be USBD_INPROG), + * so start it first. + */ + return slhci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)); +} + +usbd_status +slhci_root_ctrl_start(usbd_xfer_handle xfer) +{ + struct slhci_softc *sc = (struct slhci_softc *)xfer->pipe->device->bus; + usb_device_request_t *req; + int len, value, index, l, s, status; + int totlen = 0; + void *buf = NULL; + usb_port_status_t ps; + usbd_status error; + char slbuf[50]; + u_int8_t r; + + DPRINTF(D_TRACE, ("SLRCstart ")); + + req = &xfer->request; + + len = UGETW(req->wLength); + value = UGETW(req->wValue); + index = UGETW(req->wIndex); + + if (len) + buf = xfer->buffer; + +#ifdef SLHCI_DEBUG + if ((slhci_debug & D_TRACE)) + print_req_hub(req); +#endif + +#define C(x,y) ((x) | ((y) << 8)) + switch (C(req->bRequest, req->bmRequestType)) { + case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): + case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): + case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): + DPRINTF(D_MSG, ("UR_CLEAR_FEATURE ")); + break; + case C(UR_GET_CONFIG, UT_READ_DEVICE): + DPRINTF(D_MSG, ("UR_GET_CONFIG ")); + if (len > 0) { + *(u_int8_t *)buf = sc->sc_conf; + totlen = 1; + } + break; + case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): + switch (value >> 8) { + case UDESC_DEVICE: + DPRINTF(D_MSG, ("UDESC_DEVICE ")); + if ((value & 0xff) != 0) { + error = USBD_IOERROR; + goto ret; + } + totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); + memcpy(buf, &slhci_devd, l); + break; + case UDESC_CONFIG: + DPRINTF(D_MSG, ("UDESC_CONFIG ")); + if ((value & 0xff) != 0) { + error = USBD_IOERROR; + goto ret; + } + totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE); + memcpy(buf, &slhci_confd, l); + buf = (char *)buf + l; + len -= l; + + l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE); + totlen += l; + memcpy(buf, &slhci_ifcd, l); + buf = (char *)buf + l; + len -= l; + + l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE); + totlen += l; + memcpy(buf, &slhci_endpd, l); + break; + case UDESC_STRING: + DPRINTF(D_MSG, ("UDESC_STR ")); + if (len == 0) + break; + *(u_int8_t *)buf = 0; + totlen = 1; + switch (value & 0xff) { + case 0: + break; + case 1: /* Vendor */ + totlen = slhci_str(buf, len, "ScanLogic"); + break; + case 2: /* Product */ + snprintf(slbuf, sizeof(slbuf), "%s root hub", + sltypestr[sc->sc_sltype>0]); + totlen = slhci_str(buf, len, slbuf); + break; + default: + printf("strerr%d ", value & 0xff); + break; + } + break; + default: + printf("unknownGetDescriptor=%x", value); + error = USBD_IOERROR; + break; + } + break; + case C(UR_GET_INTERFACE, UT_READ_INTERFACE): + /* Get Interface, 9.4.4 */ + if (len > 0) { + *(u_int8_t *)buf = 0; + totlen = 1; + } + break; + case C(UR_GET_STATUS, UT_READ_DEVICE): + /* Get Status from device, 9.4.5 */ + if (len > 1) { + USETW(((usb_status_t *)buf)->wStatus, UDS_SELF_POWERED); + totlen = 2; + } + break; + case C(UR_GET_STATUS, UT_READ_INTERFACE): + case C(UR_GET_STATUS, UT_READ_ENDPOINT): + /* Get Status from interface, endpoint, 9.4.5 */ + if (len > 1) { + USETW(((usb_status_t *)buf)->wStatus, 0); + totlen = 2; + } + break; + case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): + /* Set Address, 9.4.6 */ + DPRINTF(D_MSG, ("UR_SET_ADDRESS ")); + if (value >= USB_MAX_DEVICES) { + error = USBD_IOERROR; + goto ret; + } + sc->sc_addr = value; + break; + case C(UR_SET_CONFIG, UT_WRITE_DEVICE): + /* Set Configuration, 9.4.7 */ + DPRINTF(D_MSG, ("UR_SET_CONFIG ")); + if (value != 0 && value != 1) { + error = USBD_IOERROR; + goto ret; + } + sc->sc_conf = value; + break; + case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): + /* Set Descriptor, 9.4.8, not supported */ + DPRINTF(D_MSG, ("UR_SET_DESCRIPTOR,WRITE_DEVICE not supported\n")); + break; + case C(UR_SET_FEATURE, UT_WRITE_DEVICE): + case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): + case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): + /* Set Feature, 9.4.9, not supported */ + DPRINTF(D_MSG, ("UR_SET_FEATURE not supported\n")); + error = USBD_IOERROR; + break; + case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): + /* Set Interface, 9.4.10, not supported */ + break; + case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): + /* Synch Frame, 9.4.11, not supported */ + break; + + /* + * Hub specific requests + */ + case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): + /* Clear Hub Feature, 11.16.2.1, not supported */ + DPRINTF(D_MSG, ("ClearHubFeature not supported\n")); + break; + case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): + /* Clear Port Feature, 11.16.2.2 */ + if (index != 1) { + error = USBD_IOERROR; + goto ret; + } + switch (value) { + case UHF_PORT_POWER: + DPRINTF(D_MSG, ("POWER_OFF ")); + sc->sc_powerstat = POWER_OFF; + /* x68k Nereid USB controller needs it */ + if (sc->sc_enable_power) + sc->sc_enable_power(sc, sc->sc_powerstat); + break; + case UHF_PORT_SUSPEND: + DPRINTF(D_MSG, ("SUSPEND ")); + sl11write(sc, SL11_CTRL, + sl11read(sc, SL11_CTRL) & ~SL11_CTRL_SUSPEND); + break; + case UHF_C_PORT_CONNECTION: + sc->sc_change &= ~UPS_C_CONNECT_STATUS; + break; + case UHF_C_PORT_RESET: + sc->sc_change &= ~UPS_C_PORT_RESET; + break; + case UHF_PORT_ENABLE: + break; + case UHF_C_PORT_SUSPEND: + case UHF_C_PORT_ENABLE: + case UHF_C_PORT_OVER_CURRENT: + default: + printf("ClrPortFeatERR:value=0x%x ", value); + error = USBD_IOERROR; + break; + } + //DPRINTF(D_XFER, ("CH=%04x ", sc->sc_change)); + break; + case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER): + /* Get Bus State, 11.16.2.3, not supported */ + /* shall return a STALL... */ + break; + case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): + /* Get Hub Descriptor, 11.16.2.4 */ + if (value != 0) { + error = USBD_IOERROR; + goto ret; + } + l = min(len, USB_HUB_DESCRIPTOR_SIZE); + totlen = l; + memcpy(buf, &slhci_hubd, l); + break; + case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): + /* Get Hub Status, 11.16.2.5 */ + DPRINTF(D_MSG, ("UR_GET_STATUS RCD")); + if (len != 4) { + error = USBD_IOERROR; + goto ret; + } + memset(buf, 0, len); + totlen = len; + break; + case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): + /* Get Port Status, 11.16.2.6 */ + if (index != 1 || len != 4) { + printf("index=%d,len=%d ", index, len); + error = USBD_IOERROR; + goto ret; + } + /* + * change + * o port is always enabled. >>> TRUNCATED FOR MAIL (1000 lines) <<<