102ac6454SAndrew Thompson /*- 202ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 302ac6454SAndrew Thompson * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 402ac6454SAndrew Thompson * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 502ac6454SAndrew Thompson * 602ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 702ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 802ac6454SAndrew Thompson * are met: 902ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1002ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1102ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1302ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1402ac6454SAndrew Thompson * 1502ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1602ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1702ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1802ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1902ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2002ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2102ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2202ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2302ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2402ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2502ac6454SAndrew Thompson * SUCH DAMAGE. 2602ac6454SAndrew Thompson */ 2702ac6454SAndrew Thompson 2802ac6454SAndrew Thompson #include <sys/cdefs.h> 2902ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 3002ac6454SAndrew Thompson 3102ac6454SAndrew Thompson /* 3202ac6454SAndrew Thompson * USB Open Host Controller driver. 3302ac6454SAndrew Thompson * 3402ac6454SAndrew Thompson * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html 3502ac6454SAndrew Thompson * USB spec: http://www.usb.org/developers/docs/usbspec.zip 3602ac6454SAndrew Thompson */ 3702ac6454SAndrew Thompson 3802ac6454SAndrew Thompson #include <dev/usb/usb.h> 3902ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h> 4002ac6454SAndrew Thompson #include <dev/usb/usb_error.h> 4102ac6454SAndrew Thompson 4202ac6454SAndrew Thompson #define USB_DEBUG_VAR ohcidebug 4302ac6454SAndrew Thompson 4402ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 4502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 4602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 4702ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 4802ac6454SAndrew Thompson #include <dev/usb/usb_sw_transfer.h> 4902ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 5002ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 5102ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 5202ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 5302ac6454SAndrew Thompson 5402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 5502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 5602ac6454SAndrew Thompson #include <dev/usb/controller/ohci.h> 5702ac6454SAndrew Thompson 58578d0effSAndrew Thompson #define OHCI_BUS2SC(bus) \ 59578d0effSAndrew Thompson ((ohci_softc_t *)(((uint8_t *)(bus)) - \ 60578d0effSAndrew Thompson ((uint8_t *)&(((ohci_softc_t *)0)->sc_bus)))) 6102ac6454SAndrew Thompson 6202ac6454SAndrew Thompson #if USB_DEBUG 6302ac6454SAndrew Thompson static int ohcidebug = 0; 6402ac6454SAndrew Thompson 6502ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci"); 6602ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_ohci, OID_AUTO, debug, CTLFLAG_RW, 6702ac6454SAndrew Thompson &ohcidebug, 0, "ohci debug level"); 6802ac6454SAndrew Thompson static void ohci_dumpregs(ohci_softc_t *); 6902ac6454SAndrew Thompson static void ohci_dump_tds(ohci_td_t *); 7002ac6454SAndrew Thompson static uint8_t ohci_dump_td(ohci_td_t *); 7102ac6454SAndrew Thompson static void ohci_dump_ed(ohci_ed_t *); 7202ac6454SAndrew Thompson static uint8_t ohci_dump_itd(ohci_itd_t *); 7302ac6454SAndrew Thompson static void ohci_dump_itds(ohci_itd_t *); 7402ac6454SAndrew Thompson 7502ac6454SAndrew Thompson #endif 7602ac6454SAndrew Thompson 7702ac6454SAndrew Thompson #define OBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \ 7802ac6454SAndrew Thompson BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE) 7902ac6454SAndrew Thompson #define OWRITE1(sc, r, x) \ 8002ac6454SAndrew Thompson do { OBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0) 8102ac6454SAndrew Thompson #define OWRITE2(sc, r, x) \ 8202ac6454SAndrew Thompson do { OBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0) 8302ac6454SAndrew Thompson #define OWRITE4(sc, r, x) \ 8402ac6454SAndrew Thompson do { OBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0) 8502ac6454SAndrew Thompson #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r))) 8602ac6454SAndrew Thompson #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r))) 8702ac6454SAndrew Thompson #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r))) 8802ac6454SAndrew Thompson 8902ac6454SAndrew Thompson #define OHCI_INTR_ENDPT 1 9002ac6454SAndrew Thompson 9102ac6454SAndrew Thompson extern struct usb2_bus_methods ohci_bus_methods; 9202ac6454SAndrew Thompson extern struct usb2_pipe_methods ohci_device_bulk_methods; 9302ac6454SAndrew Thompson extern struct usb2_pipe_methods ohci_device_ctrl_methods; 9402ac6454SAndrew Thompson extern struct usb2_pipe_methods ohci_device_intr_methods; 9502ac6454SAndrew Thompson extern struct usb2_pipe_methods ohci_device_isoc_methods; 9602ac6454SAndrew Thompson extern struct usb2_pipe_methods ohci_root_ctrl_methods; 9702ac6454SAndrew Thompson extern struct usb2_pipe_methods ohci_root_intr_methods; 9802ac6454SAndrew Thompson 9902ac6454SAndrew Thompson static void ohci_root_ctrl_poll(struct ohci_softc *sc); 10002ac6454SAndrew Thompson static void ohci_do_poll(struct usb2_bus *bus); 10102ac6454SAndrew Thompson static void ohci_device_done(struct usb2_xfer *xfer, usb2_error_t error); 10202ac6454SAndrew Thompson 10302ac6454SAndrew Thompson static usb2_sw_transfer_func_t ohci_root_intr_done; 10402ac6454SAndrew Thompson static usb2_sw_transfer_func_t ohci_root_ctrl_done; 10502ac6454SAndrew Thompson static void ohci_timeout(void *arg); 10602ac6454SAndrew Thompson static uint8_t ohci_check_transfer(struct usb2_xfer *xfer); 10702ac6454SAndrew Thompson 10802ac6454SAndrew Thompson struct ohci_std_temp { 10902ac6454SAndrew Thompson struct usb2_page_cache *pc; 11002ac6454SAndrew Thompson ohci_td_t *td; 11102ac6454SAndrew Thompson ohci_td_t *td_next; 11202ac6454SAndrew Thompson uint32_t average; 11302ac6454SAndrew Thompson uint32_t td_flags; 11402ac6454SAndrew Thompson uint32_t len; 11502ac6454SAndrew Thompson uint16_t max_frame_size; 11602ac6454SAndrew Thompson uint8_t shortpkt; 11702ac6454SAndrew Thompson uint8_t setup_alt_next; 1180f7d4548SAndrew Thompson uint8_t last_frame; 11902ac6454SAndrew Thompson }; 12002ac6454SAndrew Thompson 12102ac6454SAndrew Thompson static struct ohci_hcca * 12202ac6454SAndrew Thompson ohci_get_hcca(ohci_softc_t *sc) 12302ac6454SAndrew Thompson { 12402ac6454SAndrew Thompson usb2_pc_cpu_invalidate(&sc->sc_hw.hcca_pc); 12502ac6454SAndrew Thompson return (sc->sc_hcca_p); 12602ac6454SAndrew Thompson } 12702ac6454SAndrew Thompson 12802ac6454SAndrew Thompson void 12902ac6454SAndrew Thompson ohci_iterate_hw_softc(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *cb) 13002ac6454SAndrew Thompson { 13102ac6454SAndrew Thompson struct ohci_softc *sc = OHCI_BUS2SC(bus); 13202ac6454SAndrew Thompson uint32_t i; 13302ac6454SAndrew Thompson 13402ac6454SAndrew Thompson cb(bus, &sc->sc_hw.hcca_pc, &sc->sc_hw.hcca_pg, 13502ac6454SAndrew Thompson sizeof(ohci_hcca_t), OHCI_HCCA_ALIGN); 13602ac6454SAndrew Thompson 13702ac6454SAndrew Thompson cb(bus, &sc->sc_hw.ctrl_start_pc, &sc->sc_hw.ctrl_start_pg, 13802ac6454SAndrew Thompson sizeof(ohci_ed_t), OHCI_ED_ALIGN); 13902ac6454SAndrew Thompson 14002ac6454SAndrew Thompson cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg, 14102ac6454SAndrew Thompson sizeof(ohci_ed_t), OHCI_ED_ALIGN); 14202ac6454SAndrew Thompson 14302ac6454SAndrew Thompson cb(bus, &sc->sc_hw.isoc_start_pc, &sc->sc_hw.isoc_start_pg, 14402ac6454SAndrew Thompson sizeof(ohci_ed_t), OHCI_ED_ALIGN); 14502ac6454SAndrew Thompson 14602ac6454SAndrew Thompson for (i = 0; i != OHCI_NO_EDS; i++) { 14702ac6454SAndrew Thompson cb(bus, sc->sc_hw.intr_start_pc + i, sc->sc_hw.intr_start_pg + i, 14802ac6454SAndrew Thompson sizeof(ohci_ed_t), OHCI_ED_ALIGN); 14902ac6454SAndrew Thompson } 15002ac6454SAndrew Thompson } 15102ac6454SAndrew Thompson 15202ac6454SAndrew Thompson static usb2_error_t 15302ac6454SAndrew Thompson ohci_controller_init(ohci_softc_t *sc) 15402ac6454SAndrew Thompson { 15502ac6454SAndrew Thompson struct usb2_page_search buf_res; 15602ac6454SAndrew Thompson uint32_t i; 15702ac6454SAndrew Thompson uint32_t ctl; 15802ac6454SAndrew Thompson uint32_t ival; 15902ac6454SAndrew Thompson uint32_t hcr; 16002ac6454SAndrew Thompson uint32_t fm; 16102ac6454SAndrew Thompson uint32_t per; 16202ac6454SAndrew Thompson uint32_t desca; 16302ac6454SAndrew Thompson 16402ac6454SAndrew Thompson /* Determine in what context we are running. */ 16502ac6454SAndrew Thompson ctl = OREAD4(sc, OHCI_CONTROL); 16602ac6454SAndrew Thompson if (ctl & OHCI_IR) { 16702ac6454SAndrew Thompson /* SMM active, request change */ 16802ac6454SAndrew Thompson DPRINTF("SMM active, request owner change\n"); 16902ac6454SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_OCR); 17002ac6454SAndrew Thompson for (i = 0; (i < 100) && (ctl & OHCI_IR); i++) { 17102ac6454SAndrew Thompson usb2_pause_mtx(NULL, hz / 1000); 17202ac6454SAndrew Thompson ctl = OREAD4(sc, OHCI_CONTROL); 17302ac6454SAndrew Thompson } 17402ac6454SAndrew Thompson if (ctl & OHCI_IR) { 17502ac6454SAndrew Thompson device_printf(sc->sc_bus.bdev, 17602ac6454SAndrew Thompson "SMM does not respond, resetting\n"); 17702ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET); 17802ac6454SAndrew Thompson goto reset; 17902ac6454SAndrew Thompson } 18002ac6454SAndrew Thompson } else { 18102ac6454SAndrew Thompson DPRINTF("cold started\n"); 18202ac6454SAndrew Thompson reset: 18302ac6454SAndrew Thompson /* controller was cold started */ 18402ac6454SAndrew Thompson usb2_pause_mtx(NULL, 18502ac6454SAndrew Thompson USB_MS_TO_TICKS(USB_BUS_RESET_DELAY)); 18602ac6454SAndrew Thompson } 18702ac6454SAndrew Thompson 18802ac6454SAndrew Thompson /* 18902ac6454SAndrew Thompson * This reset should not be necessary according to the OHCI spec, but 19002ac6454SAndrew Thompson * without it some controllers do not start. 19102ac6454SAndrew Thompson */ 19202ac6454SAndrew Thompson DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)); 19302ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET); 19402ac6454SAndrew Thompson 19502ac6454SAndrew Thompson usb2_pause_mtx(NULL, 19602ac6454SAndrew Thompson USB_MS_TO_TICKS(USB_BUS_RESET_DELAY)); 19702ac6454SAndrew Thompson 19802ac6454SAndrew Thompson /* we now own the host controller and the bus has been reset */ 19902ac6454SAndrew Thompson ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL)); 20002ac6454SAndrew Thompson 20102ac6454SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */ 20202ac6454SAndrew Thompson /* nominal time for a reset is 10 us */ 20302ac6454SAndrew Thompson for (i = 0; i < 10; i++) { 20402ac6454SAndrew Thompson DELAY(10); 20502ac6454SAndrew Thompson hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR; 20602ac6454SAndrew Thompson if (!hcr) { 20702ac6454SAndrew Thompson break; 20802ac6454SAndrew Thompson } 20902ac6454SAndrew Thompson } 21002ac6454SAndrew Thompson if (hcr) { 21102ac6454SAndrew Thompson device_printf(sc->sc_bus.bdev, "reset timeout\n"); 21202ac6454SAndrew Thompson return (USB_ERR_IOERROR); 21302ac6454SAndrew Thompson } 21402ac6454SAndrew Thompson #if USB_DEBUG 21502ac6454SAndrew Thompson if (ohcidebug > 15) { 21602ac6454SAndrew Thompson ohci_dumpregs(sc); 21702ac6454SAndrew Thompson } 21802ac6454SAndrew Thompson #endif 21902ac6454SAndrew Thompson 22002ac6454SAndrew Thompson /* The controller is now in SUSPEND state, we have 2ms to finish. */ 22102ac6454SAndrew Thompson 22202ac6454SAndrew Thompson /* set up HC registers */ 22302ac6454SAndrew Thompson usb2_get_page(&sc->sc_hw.hcca_pc, 0, &buf_res); 22402ac6454SAndrew Thompson OWRITE4(sc, OHCI_HCCA, buf_res.physaddr); 22502ac6454SAndrew Thompson 22602ac6454SAndrew Thompson usb2_get_page(&sc->sc_hw.ctrl_start_pc, 0, &buf_res); 22702ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL_HEAD_ED, buf_res.physaddr); 22802ac6454SAndrew Thompson 22902ac6454SAndrew Thompson usb2_get_page(&sc->sc_hw.bulk_start_pc, 0, &buf_res); 23002ac6454SAndrew Thompson OWRITE4(sc, OHCI_BULK_HEAD_ED, buf_res.physaddr); 23102ac6454SAndrew Thompson 23202ac6454SAndrew Thompson /* disable all interrupts and then switch on all desired interrupts */ 23302ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS); 23402ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE); 23502ac6454SAndrew Thompson /* switch on desired functional features */ 23602ac6454SAndrew Thompson ctl = OREAD4(sc, OHCI_CONTROL); 23702ac6454SAndrew Thompson ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR); 23802ac6454SAndrew Thompson ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE | 23902ac6454SAndrew Thompson OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL; 24002ac6454SAndrew Thompson /* And finally start it! */ 24102ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, ctl); 24202ac6454SAndrew Thompson 24302ac6454SAndrew Thompson /* 24402ac6454SAndrew Thompson * The controller is now OPERATIONAL. Set a some final 24502ac6454SAndrew Thompson * registers that should be set earlier, but that the 24602ac6454SAndrew Thompson * controller ignores when in the SUSPEND state. 24702ac6454SAndrew Thompson */ 24802ac6454SAndrew Thompson fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT; 24902ac6454SAndrew Thompson fm |= OHCI_FSMPS(ival) | ival; 25002ac6454SAndrew Thompson OWRITE4(sc, OHCI_FM_INTERVAL, fm); 25102ac6454SAndrew Thompson per = OHCI_PERIODIC(ival); /* 90% periodic */ 25202ac6454SAndrew Thompson OWRITE4(sc, OHCI_PERIODIC_START, per); 25302ac6454SAndrew Thompson 25402ac6454SAndrew Thompson /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */ 25502ac6454SAndrew Thompson desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A); 25602ac6454SAndrew Thompson OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP); 25702ac6454SAndrew Thompson OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */ 25802ac6454SAndrew Thompson usb2_pause_mtx(NULL, 25902ac6454SAndrew Thompson USB_MS_TO_TICKS(OHCI_ENABLE_POWER_DELAY)); 26002ac6454SAndrew Thompson OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca); 26102ac6454SAndrew Thompson 26202ac6454SAndrew Thompson /* 26302ac6454SAndrew Thompson * The AMD756 requires a delay before re-reading the register, 26402ac6454SAndrew Thompson * otherwise it will occasionally report 0 ports. 26502ac6454SAndrew Thompson */ 26602ac6454SAndrew Thompson sc->sc_noport = 0; 26702ac6454SAndrew Thompson for (i = 0; (i < 10) && (sc->sc_noport == 0); i++) { 26802ac6454SAndrew Thompson usb2_pause_mtx(NULL, 26902ac6454SAndrew Thompson USB_MS_TO_TICKS(OHCI_READ_DESC_DELAY)); 27002ac6454SAndrew Thompson sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A)); 27102ac6454SAndrew Thompson } 27202ac6454SAndrew Thompson 27302ac6454SAndrew Thompson #if USB_DEBUG 27402ac6454SAndrew Thompson if (ohcidebug > 5) { 27502ac6454SAndrew Thompson ohci_dumpregs(sc); 27602ac6454SAndrew Thompson } 27702ac6454SAndrew Thompson #endif 27802ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 27902ac6454SAndrew Thompson } 28002ac6454SAndrew Thompson 28102ac6454SAndrew Thompson static struct ohci_ed * 28202ac6454SAndrew Thompson ohci_init_ed(struct usb2_page_cache *pc) 28302ac6454SAndrew Thompson { 28402ac6454SAndrew Thompson struct usb2_page_search buf_res; 28502ac6454SAndrew Thompson struct ohci_ed *ed; 28602ac6454SAndrew Thompson 28702ac6454SAndrew Thompson usb2_get_page(pc, 0, &buf_res); 28802ac6454SAndrew Thompson 28902ac6454SAndrew Thompson ed = buf_res.buffer; 29002ac6454SAndrew Thompson 29102ac6454SAndrew Thompson ed->ed_self = htole32(buf_res.physaddr); 29202ac6454SAndrew Thompson ed->ed_flags = htole32(OHCI_ED_SKIP); 29302ac6454SAndrew Thompson ed->page_cache = pc; 29402ac6454SAndrew Thompson 29502ac6454SAndrew Thompson return (ed); 29602ac6454SAndrew Thompson } 29702ac6454SAndrew Thompson 29802ac6454SAndrew Thompson usb2_error_t 29902ac6454SAndrew Thompson ohci_init(ohci_softc_t *sc) 30002ac6454SAndrew Thompson { 30102ac6454SAndrew Thompson struct usb2_page_search buf_res; 30202ac6454SAndrew Thompson uint16_t i; 30302ac6454SAndrew Thompson uint16_t bit; 30402ac6454SAndrew Thompson uint16_t x; 30502ac6454SAndrew Thompson uint16_t y; 30602ac6454SAndrew Thompson 30702ac6454SAndrew Thompson DPRINTF("start\n"); 30802ac6454SAndrew Thompson 30902ac6454SAndrew Thompson sc->sc_eintrs = OHCI_NORMAL_INTRS; 31002ac6454SAndrew Thompson 31102ac6454SAndrew Thompson /* 31202ac6454SAndrew Thompson * Setup all ED's 31302ac6454SAndrew Thompson */ 31402ac6454SAndrew Thompson 31502ac6454SAndrew Thompson sc->sc_ctrl_p_last = 31602ac6454SAndrew Thompson ohci_init_ed(&sc->sc_hw.ctrl_start_pc); 31702ac6454SAndrew Thompson 31802ac6454SAndrew Thompson sc->sc_bulk_p_last = 31902ac6454SAndrew Thompson ohci_init_ed(&sc->sc_hw.bulk_start_pc); 32002ac6454SAndrew Thompson 32102ac6454SAndrew Thompson sc->sc_isoc_p_last = 32202ac6454SAndrew Thompson ohci_init_ed(&sc->sc_hw.isoc_start_pc); 32302ac6454SAndrew Thompson 32402ac6454SAndrew Thompson for (i = 0; i != OHCI_NO_EDS; i++) { 32502ac6454SAndrew Thompson sc->sc_intr_p_last[i] = 32602ac6454SAndrew Thompson ohci_init_ed(sc->sc_hw.intr_start_pc + i); 32702ac6454SAndrew Thompson } 32802ac6454SAndrew Thompson 32902ac6454SAndrew Thompson /* 33002ac6454SAndrew Thompson * the QHs are arranged to give poll intervals that are 33102ac6454SAndrew Thompson * powers of 2 times 1ms 33202ac6454SAndrew Thompson */ 33302ac6454SAndrew Thompson bit = OHCI_NO_EDS / 2; 33402ac6454SAndrew Thompson while (bit) { 33502ac6454SAndrew Thompson x = bit; 33602ac6454SAndrew Thompson while (x & bit) { 33702ac6454SAndrew Thompson ohci_ed_t *ed_x; 33802ac6454SAndrew Thompson ohci_ed_t *ed_y; 33902ac6454SAndrew Thompson 34002ac6454SAndrew Thompson y = (x ^ bit) | (bit / 2); 34102ac6454SAndrew Thompson 34202ac6454SAndrew Thompson /* 34302ac6454SAndrew Thompson * the next QH has half the poll interval 34402ac6454SAndrew Thompson */ 34502ac6454SAndrew Thompson ed_x = sc->sc_intr_p_last[x]; 34602ac6454SAndrew Thompson ed_y = sc->sc_intr_p_last[y]; 34702ac6454SAndrew Thompson 34802ac6454SAndrew Thompson ed_x->next = NULL; 34902ac6454SAndrew Thompson ed_x->ed_next = ed_y->ed_self; 35002ac6454SAndrew Thompson 35102ac6454SAndrew Thompson x++; 35202ac6454SAndrew Thompson } 35302ac6454SAndrew Thompson bit >>= 1; 35402ac6454SAndrew Thompson } 35502ac6454SAndrew Thompson 35602ac6454SAndrew Thompson if (1) { 35702ac6454SAndrew Thompson 35802ac6454SAndrew Thompson ohci_ed_t *ed_int; 35902ac6454SAndrew Thompson ohci_ed_t *ed_isc; 36002ac6454SAndrew Thompson 36102ac6454SAndrew Thompson ed_int = sc->sc_intr_p_last[0]; 36202ac6454SAndrew Thompson ed_isc = sc->sc_isoc_p_last; 36302ac6454SAndrew Thompson 36402ac6454SAndrew Thompson /* the last (1ms) QH */ 36502ac6454SAndrew Thompson ed_int->next = ed_isc; 36602ac6454SAndrew Thompson ed_int->ed_next = ed_isc->ed_self; 36702ac6454SAndrew Thompson } 36802ac6454SAndrew Thompson usb2_get_page(&sc->sc_hw.hcca_pc, 0, &buf_res); 36902ac6454SAndrew Thompson 37002ac6454SAndrew Thompson sc->sc_hcca_p = buf_res.buffer; 37102ac6454SAndrew Thompson 37202ac6454SAndrew Thompson /* 37302ac6454SAndrew Thompson * Fill HCCA interrupt table. The bit reversal is to get 37402ac6454SAndrew Thompson * the tree set up properly to spread the interrupts. 37502ac6454SAndrew Thompson */ 37602ac6454SAndrew Thompson for (i = 0; i != OHCI_NO_INTRS; i++) { 37702ac6454SAndrew Thompson sc->sc_hcca_p->hcca_interrupt_table[i] = 37802ac6454SAndrew Thompson sc->sc_intr_p_last[i | (OHCI_NO_EDS / 2)]->ed_self; 37902ac6454SAndrew Thompson } 38002ac6454SAndrew Thompson /* flush all cache into memory */ 38102ac6454SAndrew Thompson 38202ac6454SAndrew Thompson usb2_bus_mem_flush_all(&sc->sc_bus, &ohci_iterate_hw_softc); 38302ac6454SAndrew Thompson 38402ac6454SAndrew Thompson /* set up the bus struct */ 38502ac6454SAndrew Thompson sc->sc_bus.methods = &ohci_bus_methods; 38602ac6454SAndrew Thompson 38702ac6454SAndrew Thompson usb2_callout_init_mtx(&sc->sc_tmo_rhsc, &sc->sc_bus.bus_mtx, 0); 38802ac6454SAndrew Thompson 38902ac6454SAndrew Thompson #if USB_DEBUG 39002ac6454SAndrew Thompson if (ohcidebug > 15) { 39102ac6454SAndrew Thompson for (i = 0; i != OHCI_NO_EDS; i++) { 39202ac6454SAndrew Thompson printf("ed#%d ", i); 39302ac6454SAndrew Thompson ohci_dump_ed(sc->sc_intr_p_last[i]); 39402ac6454SAndrew Thompson } 39502ac6454SAndrew Thompson printf("iso "); 39602ac6454SAndrew Thompson ohci_dump_ed(sc->sc_isoc_p_last); 39702ac6454SAndrew Thompson } 39802ac6454SAndrew Thompson #endif 39902ac6454SAndrew Thompson 40002ac6454SAndrew Thompson sc->sc_bus.usbrev = USB_REV_1_0; 40102ac6454SAndrew Thompson 40202ac6454SAndrew Thompson if (ohci_controller_init(sc)) { 40302ac6454SAndrew Thompson return (USB_ERR_INVAL); 40402ac6454SAndrew Thompson } else { 40502ac6454SAndrew Thompson /* catch any lost interrupts */ 40602ac6454SAndrew Thompson ohci_do_poll(&sc->sc_bus); 40702ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 40802ac6454SAndrew Thompson } 40902ac6454SAndrew Thompson } 41002ac6454SAndrew Thompson 41102ac6454SAndrew Thompson /* 41202ac6454SAndrew Thompson * shut down the controller when the system is going down 41302ac6454SAndrew Thompson */ 41402ac6454SAndrew Thompson void 41502ac6454SAndrew Thompson ohci_detach(struct ohci_softc *sc) 41602ac6454SAndrew Thompson { 41702ac6454SAndrew Thompson USB_BUS_LOCK(&sc->sc_bus); 41802ac6454SAndrew Thompson 41902ac6454SAndrew Thompson usb2_callout_stop(&sc->sc_tmo_rhsc); 42002ac6454SAndrew Thompson 42102ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS); 42202ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET); 42302ac6454SAndrew Thompson 42402ac6454SAndrew Thompson USB_BUS_UNLOCK(&sc->sc_bus); 42502ac6454SAndrew Thompson 42602ac6454SAndrew Thompson /* XXX let stray task complete */ 42702ac6454SAndrew Thompson usb2_pause_mtx(NULL, hz / 20); 42802ac6454SAndrew Thompson 42902ac6454SAndrew Thompson usb2_callout_drain(&sc->sc_tmo_rhsc); 43002ac6454SAndrew Thompson } 43102ac6454SAndrew Thompson 43202ac6454SAndrew Thompson /* NOTE: suspend/resume is called from 43302ac6454SAndrew Thompson * interrupt context and cannot sleep! 43402ac6454SAndrew Thompson */ 43502ac6454SAndrew Thompson void 43602ac6454SAndrew Thompson ohci_suspend(ohci_softc_t *sc) 43702ac6454SAndrew Thompson { 43802ac6454SAndrew Thompson uint32_t ctl; 43902ac6454SAndrew Thompson 44002ac6454SAndrew Thompson USB_BUS_LOCK(&sc->sc_bus); 44102ac6454SAndrew Thompson 44202ac6454SAndrew Thompson #if USB_DEBUG 44302ac6454SAndrew Thompson DPRINTF("\n"); 44402ac6454SAndrew Thompson if (ohcidebug > 2) { 44502ac6454SAndrew Thompson ohci_dumpregs(sc); 44602ac6454SAndrew Thompson } 44702ac6454SAndrew Thompson #endif 44802ac6454SAndrew Thompson 44902ac6454SAndrew Thompson ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK; 45002ac6454SAndrew Thompson if (sc->sc_control == 0) { 45102ac6454SAndrew Thompson /* 45202ac6454SAndrew Thompson * Preserve register values, in case that APM BIOS 45302ac6454SAndrew Thompson * does not recover them. 45402ac6454SAndrew Thompson */ 45502ac6454SAndrew Thompson sc->sc_control = ctl; 45602ac6454SAndrew Thompson sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE); 45702ac6454SAndrew Thompson } 45802ac6454SAndrew Thompson ctl |= OHCI_HCFS_SUSPEND; 45902ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, ctl); 46002ac6454SAndrew Thompson 46102ac6454SAndrew Thompson usb2_pause_mtx(&sc->sc_bus.bus_mtx, 46202ac6454SAndrew Thompson USB_MS_TO_TICKS(USB_RESUME_WAIT)); 46302ac6454SAndrew Thompson 46402ac6454SAndrew Thompson USB_BUS_UNLOCK(&sc->sc_bus); 46502ac6454SAndrew Thompson } 46602ac6454SAndrew Thompson 46702ac6454SAndrew Thompson void 46802ac6454SAndrew Thompson ohci_resume(ohci_softc_t *sc) 46902ac6454SAndrew Thompson { 47002ac6454SAndrew Thompson uint32_t ctl; 47102ac6454SAndrew Thompson 47202ac6454SAndrew Thompson #if USB_DEBUG 47302ac6454SAndrew Thompson DPRINTF("\n"); 47402ac6454SAndrew Thompson if (ohcidebug > 2) { 47502ac6454SAndrew Thompson ohci_dumpregs(sc); 47602ac6454SAndrew Thompson } 47702ac6454SAndrew Thompson #endif 47802ac6454SAndrew Thompson /* some broken BIOSes never initialize the Controller chip */ 47902ac6454SAndrew Thompson ohci_controller_init(sc); 48002ac6454SAndrew Thompson 48102ac6454SAndrew Thompson USB_BUS_LOCK(&sc->sc_bus); 48202ac6454SAndrew Thompson if (sc->sc_intre) { 48302ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_ENABLE, 48402ac6454SAndrew Thompson sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE)); 48502ac6454SAndrew Thompson } 48602ac6454SAndrew Thompson if (sc->sc_control) 48702ac6454SAndrew Thompson ctl = sc->sc_control; 48802ac6454SAndrew Thompson else 48902ac6454SAndrew Thompson ctl = OREAD4(sc, OHCI_CONTROL); 49002ac6454SAndrew Thompson ctl |= OHCI_HCFS_RESUME; 49102ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, ctl); 49202ac6454SAndrew Thompson usb2_pause_mtx(&sc->sc_bus.bus_mtx, 49302ac6454SAndrew Thompson USB_MS_TO_TICKS(USB_RESUME_DELAY)); 49402ac6454SAndrew Thompson ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL; 49502ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, ctl); 49602ac6454SAndrew Thompson usb2_pause_mtx(&sc->sc_bus.bus_mtx, 49702ac6454SAndrew Thompson USB_MS_TO_TICKS(USB_RESUME_RECOVERY)); 49802ac6454SAndrew Thompson sc->sc_control = sc->sc_intre = 0; 49902ac6454SAndrew Thompson 50002ac6454SAndrew Thompson USB_BUS_UNLOCK(&sc->sc_bus); 50102ac6454SAndrew Thompson 50202ac6454SAndrew Thompson /* catch any lost interrupts */ 50302ac6454SAndrew Thompson ohci_do_poll(&sc->sc_bus); 50402ac6454SAndrew Thompson } 50502ac6454SAndrew Thompson 50602ac6454SAndrew Thompson #if USB_DEBUG 50702ac6454SAndrew Thompson static void 50802ac6454SAndrew Thompson ohci_dumpregs(ohci_softc_t *sc) 50902ac6454SAndrew Thompson { 51002ac6454SAndrew Thompson struct ohci_hcca *hcca; 51102ac6454SAndrew Thompson 51202ac6454SAndrew Thompson DPRINTF("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n", 51302ac6454SAndrew Thompson OREAD4(sc, OHCI_REVISION), 51402ac6454SAndrew Thompson OREAD4(sc, OHCI_CONTROL), 51502ac6454SAndrew Thompson OREAD4(sc, OHCI_COMMAND_STATUS)); 51602ac6454SAndrew Thompson DPRINTF(" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n", 51702ac6454SAndrew Thompson OREAD4(sc, OHCI_INTERRUPT_STATUS), 51802ac6454SAndrew Thompson OREAD4(sc, OHCI_INTERRUPT_ENABLE), 51902ac6454SAndrew Thompson OREAD4(sc, OHCI_INTERRUPT_DISABLE)); 52002ac6454SAndrew Thompson DPRINTF(" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n", 52102ac6454SAndrew Thompson OREAD4(sc, OHCI_HCCA), 52202ac6454SAndrew Thompson OREAD4(sc, OHCI_PERIOD_CURRENT_ED), 52302ac6454SAndrew Thompson OREAD4(sc, OHCI_CONTROL_HEAD_ED)); 52402ac6454SAndrew Thompson DPRINTF(" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n", 52502ac6454SAndrew Thompson OREAD4(sc, OHCI_CONTROL_CURRENT_ED), 52602ac6454SAndrew Thompson OREAD4(sc, OHCI_BULK_HEAD_ED), 52702ac6454SAndrew Thompson OREAD4(sc, OHCI_BULK_CURRENT_ED)); 52802ac6454SAndrew Thompson DPRINTF(" done=0x%08x fmival=0x%08x fmrem=0x%08x\n", 52902ac6454SAndrew Thompson OREAD4(sc, OHCI_DONE_HEAD), 53002ac6454SAndrew Thompson OREAD4(sc, OHCI_FM_INTERVAL), 53102ac6454SAndrew Thompson OREAD4(sc, OHCI_FM_REMAINING)); 53202ac6454SAndrew Thompson DPRINTF(" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n", 53302ac6454SAndrew Thompson OREAD4(sc, OHCI_FM_NUMBER), 53402ac6454SAndrew Thompson OREAD4(sc, OHCI_PERIODIC_START), 53502ac6454SAndrew Thompson OREAD4(sc, OHCI_LS_THRESHOLD)); 53602ac6454SAndrew Thompson DPRINTF(" desca=0x%08x descb=0x%08x stat=0x%08x\n", 53702ac6454SAndrew Thompson OREAD4(sc, OHCI_RH_DESCRIPTOR_A), 53802ac6454SAndrew Thompson OREAD4(sc, OHCI_RH_DESCRIPTOR_B), 53902ac6454SAndrew Thompson OREAD4(sc, OHCI_RH_STATUS)); 54002ac6454SAndrew Thompson DPRINTF(" port1=0x%08x port2=0x%08x\n", 54102ac6454SAndrew Thompson OREAD4(sc, OHCI_RH_PORT_STATUS(1)), 54202ac6454SAndrew Thompson OREAD4(sc, OHCI_RH_PORT_STATUS(2))); 54302ac6454SAndrew Thompson 54402ac6454SAndrew Thompson hcca = ohci_get_hcca(sc); 54502ac6454SAndrew Thompson 54602ac6454SAndrew Thompson DPRINTF(" HCCA: frame_number=0x%04x done_head=0x%08x\n", 54702ac6454SAndrew Thompson le32toh(hcca->hcca_frame_number), 54802ac6454SAndrew Thompson le32toh(hcca->hcca_done_head)); 54902ac6454SAndrew Thompson } 55002ac6454SAndrew Thompson static void 55102ac6454SAndrew Thompson ohci_dump_tds(ohci_td_t *std) 55202ac6454SAndrew Thompson { 55302ac6454SAndrew Thompson for (; std; std = std->obj_next) { 55402ac6454SAndrew Thompson if (ohci_dump_td(std)) { 55502ac6454SAndrew Thompson break; 55602ac6454SAndrew Thompson } 55702ac6454SAndrew Thompson } 55802ac6454SAndrew Thompson } 55902ac6454SAndrew Thompson 56002ac6454SAndrew Thompson static uint8_t 56102ac6454SAndrew Thompson ohci_dump_td(ohci_td_t *std) 56202ac6454SAndrew Thompson { 56302ac6454SAndrew Thompson uint32_t td_flags; 56402ac6454SAndrew Thompson uint8_t temp; 56502ac6454SAndrew Thompson 56602ac6454SAndrew Thompson usb2_pc_cpu_invalidate(std->page_cache); 56702ac6454SAndrew Thompson 56802ac6454SAndrew Thompson td_flags = le32toh(std->td_flags); 56902ac6454SAndrew Thompson temp = (std->td_next == 0); 57002ac6454SAndrew Thompson 57102ac6454SAndrew Thompson printf("TD(%p) at 0x%08x: %s%s%s%s%s delay=%d ec=%d " 57202ac6454SAndrew Thompson "cc=%d\ncbp=0x%08x next=0x%08x be=0x%08x\n", 57302ac6454SAndrew Thompson std, le32toh(std->td_self), 57402ac6454SAndrew Thompson (td_flags & OHCI_TD_R) ? "-R" : "", 57502ac6454SAndrew Thompson (td_flags & OHCI_TD_OUT) ? "-OUT" : "", 57602ac6454SAndrew Thompson (td_flags & OHCI_TD_IN) ? "-IN" : "", 57702ac6454SAndrew Thompson ((td_flags & OHCI_TD_TOGGLE_MASK) == OHCI_TD_TOGGLE_1) ? "-TOG1" : "", 57802ac6454SAndrew Thompson ((td_flags & OHCI_TD_TOGGLE_MASK) == OHCI_TD_TOGGLE_0) ? "-TOG0" : "", 57902ac6454SAndrew Thompson OHCI_TD_GET_DI(td_flags), 58002ac6454SAndrew Thompson OHCI_TD_GET_EC(td_flags), 58102ac6454SAndrew Thompson OHCI_TD_GET_CC(td_flags), 58202ac6454SAndrew Thompson le32toh(std->td_cbp), 58302ac6454SAndrew Thompson le32toh(std->td_next), 58402ac6454SAndrew Thompson le32toh(std->td_be)); 58502ac6454SAndrew Thompson 58602ac6454SAndrew Thompson return (temp); 58702ac6454SAndrew Thompson } 58802ac6454SAndrew Thompson 58902ac6454SAndrew Thompson static uint8_t 59002ac6454SAndrew Thompson ohci_dump_itd(ohci_itd_t *sitd) 59102ac6454SAndrew Thompson { 59202ac6454SAndrew Thompson uint32_t itd_flags; 59302ac6454SAndrew Thompson uint16_t i; 59402ac6454SAndrew Thompson uint8_t temp; 59502ac6454SAndrew Thompson 59602ac6454SAndrew Thompson usb2_pc_cpu_invalidate(sitd->page_cache); 59702ac6454SAndrew Thompson 59802ac6454SAndrew Thompson itd_flags = le32toh(sitd->itd_flags); 59902ac6454SAndrew Thompson temp = (sitd->itd_next == 0); 60002ac6454SAndrew Thompson 60102ac6454SAndrew Thompson printf("ITD(%p) at 0x%08x: sf=%d di=%d fc=%d cc=%d\n" 60202ac6454SAndrew Thompson "bp0=0x%08x next=0x%08x be=0x%08x\n", 60302ac6454SAndrew Thompson sitd, le32toh(sitd->itd_self), 60402ac6454SAndrew Thompson OHCI_ITD_GET_SF(itd_flags), 60502ac6454SAndrew Thompson OHCI_ITD_GET_DI(itd_flags), 60602ac6454SAndrew Thompson OHCI_ITD_GET_FC(itd_flags), 60702ac6454SAndrew Thompson OHCI_ITD_GET_CC(itd_flags), 60802ac6454SAndrew Thompson le32toh(sitd->itd_bp0), 60902ac6454SAndrew Thompson le32toh(sitd->itd_next), 61002ac6454SAndrew Thompson le32toh(sitd->itd_be)); 61102ac6454SAndrew Thompson for (i = 0; i < OHCI_ITD_NOFFSET; i++) { 61202ac6454SAndrew Thompson printf("offs[%d]=0x%04x ", i, 61302ac6454SAndrew Thompson (uint32_t)le16toh(sitd->itd_offset[i])); 61402ac6454SAndrew Thompson } 61502ac6454SAndrew Thompson printf("\n"); 61602ac6454SAndrew Thompson 61702ac6454SAndrew Thompson return (temp); 61802ac6454SAndrew Thompson } 61902ac6454SAndrew Thompson 62002ac6454SAndrew Thompson static void 62102ac6454SAndrew Thompson ohci_dump_itds(ohci_itd_t *sitd) 62202ac6454SAndrew Thompson { 62302ac6454SAndrew Thompson for (; sitd; sitd = sitd->obj_next) { 62402ac6454SAndrew Thompson if (ohci_dump_itd(sitd)) { 62502ac6454SAndrew Thompson break; 62602ac6454SAndrew Thompson } 62702ac6454SAndrew Thompson } 62802ac6454SAndrew Thompson } 62902ac6454SAndrew Thompson 63002ac6454SAndrew Thompson static void 63102ac6454SAndrew Thompson ohci_dump_ed(ohci_ed_t *sed) 63202ac6454SAndrew Thompson { 63302ac6454SAndrew Thompson uint32_t ed_flags; 63402ac6454SAndrew Thompson uint32_t ed_headp; 63502ac6454SAndrew Thompson 63602ac6454SAndrew Thompson usb2_pc_cpu_invalidate(sed->page_cache); 63702ac6454SAndrew Thompson 63802ac6454SAndrew Thompson ed_flags = le32toh(sed->ed_flags); 63902ac6454SAndrew Thompson ed_headp = le32toh(sed->ed_headp); 64002ac6454SAndrew Thompson 64102ac6454SAndrew Thompson printf("ED(%p) at 0x%08x: addr=%d endpt=%d maxp=%d flags=%s%s%s%s%s\n" 64202ac6454SAndrew Thompson "tailp=0x%08x headflags=%s%s headp=0x%08x nexted=0x%08x\n", 64302ac6454SAndrew Thompson sed, le32toh(sed->ed_self), 64402ac6454SAndrew Thompson OHCI_ED_GET_FA(ed_flags), 64502ac6454SAndrew Thompson OHCI_ED_GET_EN(ed_flags), 64602ac6454SAndrew Thompson OHCI_ED_GET_MAXP(ed_flags), 64702ac6454SAndrew Thompson (ed_flags & OHCI_ED_DIR_OUT) ? "-OUT" : "", 64802ac6454SAndrew Thompson (ed_flags & OHCI_ED_DIR_IN) ? "-IN" : "", 64902ac6454SAndrew Thompson (ed_flags & OHCI_ED_SPEED) ? "-LOWSPEED" : "", 65002ac6454SAndrew Thompson (ed_flags & OHCI_ED_SKIP) ? "-SKIP" : "", 65102ac6454SAndrew Thompson (ed_flags & OHCI_ED_FORMAT_ISO) ? "-ISO" : "", 65202ac6454SAndrew Thompson le32toh(sed->ed_tailp), 65302ac6454SAndrew Thompson (ed_headp & OHCI_HALTED) ? "-HALTED" : "", 65402ac6454SAndrew Thompson (ed_headp & OHCI_TOGGLECARRY) ? "-CARRY" : "", 65502ac6454SAndrew Thompson le32toh(sed->ed_headp), 65602ac6454SAndrew Thompson le32toh(sed->ed_next)); 65702ac6454SAndrew Thompson } 65802ac6454SAndrew Thompson 65902ac6454SAndrew Thompson #endif 66002ac6454SAndrew Thompson 66102ac6454SAndrew Thompson static void 66202ac6454SAndrew Thompson ohci_transfer_intr_enqueue(struct usb2_xfer *xfer) 66302ac6454SAndrew Thompson { 66402ac6454SAndrew Thompson /* check for early completion */ 66502ac6454SAndrew Thompson if (ohci_check_transfer(xfer)) { 66602ac6454SAndrew Thompson return; 66702ac6454SAndrew Thompson } 66802ac6454SAndrew Thompson /* put transfer on interrupt queue */ 66902ac6454SAndrew Thompson usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 67002ac6454SAndrew Thompson 67102ac6454SAndrew Thompson /* start timeout, if any */ 67202ac6454SAndrew Thompson if (xfer->timeout != 0) { 67302ac6454SAndrew Thompson usb2_transfer_timeout_ms(xfer, &ohci_timeout, xfer->timeout); 67402ac6454SAndrew Thompson } 67502ac6454SAndrew Thompson } 67602ac6454SAndrew Thompson 67702ac6454SAndrew Thompson #define OHCI_APPEND_QH(sed,last) (last) = _ohci_append_qh(sed,last) 67802ac6454SAndrew Thompson static ohci_ed_t * 67902ac6454SAndrew Thompson _ohci_append_qh(ohci_ed_t *sed, ohci_ed_t *last) 68002ac6454SAndrew Thompson { 68102ac6454SAndrew Thompson DPRINTFN(11, "%p to %p\n", sed, last); 68202ac6454SAndrew Thompson 68302ac6454SAndrew Thompson if (sed->prev != NULL) { 68402ac6454SAndrew Thompson /* should not happen */ 68502ac6454SAndrew Thompson DPRINTFN(0, "ED already linked!\n"); 68602ac6454SAndrew Thompson return (last); 68702ac6454SAndrew Thompson } 68802ac6454SAndrew Thompson /* (sc->sc_bus.bus_mtx) must be locked */ 68902ac6454SAndrew Thompson 69002ac6454SAndrew Thompson sed->next = last->next; 69102ac6454SAndrew Thompson sed->ed_next = last->ed_next; 69202ac6454SAndrew Thompson sed->ed_tailp = 0; 69302ac6454SAndrew Thompson 69402ac6454SAndrew Thompson sed->prev = last; 69502ac6454SAndrew Thompson 69602ac6454SAndrew Thompson usb2_pc_cpu_flush(sed->page_cache); 69702ac6454SAndrew Thompson 69802ac6454SAndrew Thompson /* 69902ac6454SAndrew Thompson * the last->next->prev is never followed: sed->next->prev = sed; 70002ac6454SAndrew Thompson */ 70102ac6454SAndrew Thompson 70202ac6454SAndrew Thompson last->next = sed; 70302ac6454SAndrew Thompson last->ed_next = sed->ed_self; 70402ac6454SAndrew Thompson 70502ac6454SAndrew Thompson usb2_pc_cpu_flush(last->page_cache); 70602ac6454SAndrew Thompson 70702ac6454SAndrew Thompson return (sed); 70802ac6454SAndrew Thompson } 70902ac6454SAndrew Thompson 71002ac6454SAndrew Thompson #define OHCI_REMOVE_QH(sed,last) (last) = _ohci_remove_qh(sed,last) 71102ac6454SAndrew Thompson static ohci_ed_t * 71202ac6454SAndrew Thompson _ohci_remove_qh(ohci_ed_t *sed, ohci_ed_t *last) 71302ac6454SAndrew Thompson { 71402ac6454SAndrew Thompson DPRINTFN(11, "%p from %p\n", sed, last); 71502ac6454SAndrew Thompson 71602ac6454SAndrew Thompson /* (sc->sc_bus.bus_mtx) must be locked */ 71702ac6454SAndrew Thompson 71802ac6454SAndrew Thompson /* only remove if not removed from a queue */ 71902ac6454SAndrew Thompson if (sed->prev) { 72002ac6454SAndrew Thompson 72102ac6454SAndrew Thompson sed->prev->next = sed->next; 72202ac6454SAndrew Thompson sed->prev->ed_next = sed->ed_next; 72302ac6454SAndrew Thompson 72402ac6454SAndrew Thompson usb2_pc_cpu_flush(sed->prev->page_cache); 72502ac6454SAndrew Thompson 72602ac6454SAndrew Thompson if (sed->next) { 72702ac6454SAndrew Thompson sed->next->prev = sed->prev; 72802ac6454SAndrew Thompson usb2_pc_cpu_flush(sed->next->page_cache); 72902ac6454SAndrew Thompson } 73002ac6454SAndrew Thompson last = ((last == sed) ? sed->prev : last); 73102ac6454SAndrew Thompson 73202ac6454SAndrew Thompson sed->prev = 0; 73302ac6454SAndrew Thompson 73402ac6454SAndrew Thompson usb2_pc_cpu_flush(sed->page_cache); 73502ac6454SAndrew Thompson } 73602ac6454SAndrew Thompson return (last); 73702ac6454SAndrew Thompson } 73802ac6454SAndrew Thompson 73902ac6454SAndrew Thompson static void 74002ac6454SAndrew Thompson ohci_isoc_done(struct usb2_xfer *xfer) 74102ac6454SAndrew Thompson { 74202ac6454SAndrew Thompson uint8_t nframes; 74302ac6454SAndrew Thompson uint32_t *plen = xfer->frlengths; 74402ac6454SAndrew Thompson volatile uint16_t *olen; 74502ac6454SAndrew Thompson uint16_t len = 0; 74602ac6454SAndrew Thompson ohci_itd_t *td = xfer->td_transfer_first; 74702ac6454SAndrew Thompson 74802ac6454SAndrew Thompson while (1) { 74902ac6454SAndrew Thompson if (td == NULL) { 75002ac6454SAndrew Thompson panic("%s:%d: out of TD's\n", 75102ac6454SAndrew Thompson __FUNCTION__, __LINE__); 75202ac6454SAndrew Thompson } 75302ac6454SAndrew Thompson #if USB_DEBUG 75402ac6454SAndrew Thompson if (ohcidebug > 5) { 75502ac6454SAndrew Thompson DPRINTF("isoc TD\n"); 75602ac6454SAndrew Thompson ohci_dump_itd(td); 75702ac6454SAndrew Thompson } 75802ac6454SAndrew Thompson #endif 75902ac6454SAndrew Thompson usb2_pc_cpu_invalidate(td->page_cache); 76002ac6454SAndrew Thompson 76102ac6454SAndrew Thompson nframes = td->frames; 76202ac6454SAndrew Thompson olen = &td->itd_offset[0]; 76302ac6454SAndrew Thompson 76402ac6454SAndrew Thompson if (nframes > 8) { 76502ac6454SAndrew Thompson nframes = 8; 76602ac6454SAndrew Thompson } 76702ac6454SAndrew Thompson while (nframes--) { 76802ac6454SAndrew Thompson len = le16toh(*olen); 76902ac6454SAndrew Thompson 77002ac6454SAndrew Thompson if ((len >> 12) == OHCI_CC_NOT_ACCESSED) { 77102ac6454SAndrew Thompson len = 0; 77202ac6454SAndrew Thompson } else { 77302ac6454SAndrew Thompson len &= ((1 << 12) - 1); 77402ac6454SAndrew Thompson } 77502ac6454SAndrew Thompson 77602ac6454SAndrew Thompson if (len > *plen) { 77702ac6454SAndrew Thompson len = 0;/* invalid length */ 77802ac6454SAndrew Thompson } 77902ac6454SAndrew Thompson *plen = len; 78002ac6454SAndrew Thompson plen++; 78102ac6454SAndrew Thompson olen++; 78202ac6454SAndrew Thompson } 78302ac6454SAndrew Thompson 78402ac6454SAndrew Thompson if (((void *)td) == xfer->td_transfer_last) { 78502ac6454SAndrew Thompson break; 78602ac6454SAndrew Thompson } 78702ac6454SAndrew Thompson td = td->obj_next; 78802ac6454SAndrew Thompson } 78902ac6454SAndrew Thompson 79002ac6454SAndrew Thompson xfer->aframes = xfer->nframes; 79102ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_NORMAL_COMPLETION); 79202ac6454SAndrew Thompson } 79302ac6454SAndrew Thompson 79402ac6454SAndrew Thompson #if USB_DEBUG 79502ac6454SAndrew Thompson static const char *const 79602ac6454SAndrew Thompson ohci_cc_strs[] = 79702ac6454SAndrew Thompson { 79802ac6454SAndrew Thompson "NO_ERROR", 79902ac6454SAndrew Thompson "CRC", 80002ac6454SAndrew Thompson "BIT_STUFFING", 80102ac6454SAndrew Thompson "DATA_TOGGLE_MISMATCH", 80202ac6454SAndrew Thompson 80302ac6454SAndrew Thompson "STALL", 80402ac6454SAndrew Thompson "DEVICE_NOT_RESPONDING", 80502ac6454SAndrew Thompson "PID_CHECK_FAILURE", 80602ac6454SAndrew Thompson "UNEXPECTED_PID", 80702ac6454SAndrew Thompson 80802ac6454SAndrew Thompson "DATA_OVERRUN", 80902ac6454SAndrew Thompson "DATA_UNDERRUN", 81002ac6454SAndrew Thompson "BUFFER_OVERRUN", 81102ac6454SAndrew Thompson "BUFFER_UNDERRUN", 81202ac6454SAndrew Thompson 81302ac6454SAndrew Thompson "reserved", 81402ac6454SAndrew Thompson "reserved", 81502ac6454SAndrew Thompson "NOT_ACCESSED", 81602ac6454SAndrew Thompson "NOT_ACCESSED" 81702ac6454SAndrew Thompson }; 81802ac6454SAndrew Thompson 81902ac6454SAndrew Thompson #endif 82002ac6454SAndrew Thompson 82102ac6454SAndrew Thompson static usb2_error_t 82202ac6454SAndrew Thompson ohci_non_isoc_done_sub(struct usb2_xfer *xfer) 82302ac6454SAndrew Thompson { 82402ac6454SAndrew Thompson ohci_td_t *td; 82502ac6454SAndrew Thompson ohci_td_t *td_alt_next; 82602ac6454SAndrew Thompson uint32_t temp; 82702ac6454SAndrew Thompson uint32_t phy_start; 82802ac6454SAndrew Thompson uint32_t phy_end; 82902ac6454SAndrew Thompson uint32_t td_flags; 83002ac6454SAndrew Thompson uint16_t cc; 83102ac6454SAndrew Thompson 83202ac6454SAndrew Thompson td = xfer->td_transfer_cache; 83302ac6454SAndrew Thompson td_alt_next = td->alt_next; 83402ac6454SAndrew Thompson td_flags = 0; 83502ac6454SAndrew Thompson 83602ac6454SAndrew Thompson if (xfer->aframes != xfer->nframes) { 83702ac6454SAndrew Thompson xfer->frlengths[xfer->aframes] = 0; 83802ac6454SAndrew Thompson } 83902ac6454SAndrew Thompson while (1) { 84002ac6454SAndrew Thompson 84102ac6454SAndrew Thompson usb2_pc_cpu_invalidate(td->page_cache); 84202ac6454SAndrew Thompson phy_start = le32toh(td->td_cbp); 84302ac6454SAndrew Thompson td_flags = le32toh(td->td_flags); 84402ac6454SAndrew Thompson cc = OHCI_TD_GET_CC(td_flags); 84502ac6454SAndrew Thompson 84602ac6454SAndrew Thompson if (phy_start) { 84702ac6454SAndrew Thompson /* 84802ac6454SAndrew Thompson * short transfer - compute the number of remaining 84902ac6454SAndrew Thompson * bytes in the hardware buffer: 85002ac6454SAndrew Thompson */ 85102ac6454SAndrew Thompson phy_end = le32toh(td->td_be); 85202ac6454SAndrew Thompson temp = (OHCI_PAGE(phy_start ^ phy_end) ? 85302ac6454SAndrew Thompson (OHCI_PAGE_SIZE + 1) : 0x0001); 85402ac6454SAndrew Thompson temp += OHCI_PAGE_OFFSET(phy_end); 85502ac6454SAndrew Thompson temp -= OHCI_PAGE_OFFSET(phy_start); 85602ac6454SAndrew Thompson 85702ac6454SAndrew Thompson if (temp > td->len) { 85802ac6454SAndrew Thompson /* guard against corruption */ 85902ac6454SAndrew Thompson cc = OHCI_CC_STALL; 86002ac6454SAndrew Thompson } else if (xfer->aframes != xfer->nframes) { 86102ac6454SAndrew Thompson /* 86202ac6454SAndrew Thompson * Sum up total transfer length 86302ac6454SAndrew Thompson * in "frlengths[]": 86402ac6454SAndrew Thompson */ 86502ac6454SAndrew Thompson xfer->frlengths[xfer->aframes] += td->len - temp; 86602ac6454SAndrew Thompson } 86702ac6454SAndrew Thompson } else { 86802ac6454SAndrew Thompson if (xfer->aframes != xfer->nframes) { 86902ac6454SAndrew Thompson /* transfer was complete */ 87002ac6454SAndrew Thompson xfer->frlengths[xfer->aframes] += td->len; 87102ac6454SAndrew Thompson } 87202ac6454SAndrew Thompson } 87302ac6454SAndrew Thompson /* Check for last transfer */ 87402ac6454SAndrew Thompson if (((void *)td) == xfer->td_transfer_last) { 87502ac6454SAndrew Thompson td = NULL; 87602ac6454SAndrew Thompson break; 87702ac6454SAndrew Thompson } 87802ac6454SAndrew Thompson /* Check transfer status */ 87902ac6454SAndrew Thompson if (cc) { 88002ac6454SAndrew Thompson /* the transfer is finished */ 88102ac6454SAndrew Thompson td = NULL; 88202ac6454SAndrew Thompson break; 88302ac6454SAndrew Thompson } 88402ac6454SAndrew Thompson /* Check for short transfer */ 88502ac6454SAndrew Thompson if (phy_start) { 88602ac6454SAndrew Thompson if (xfer->flags_int.short_frames_ok) { 88702ac6454SAndrew Thompson /* follow alt next */ 88802ac6454SAndrew Thompson td = td->alt_next; 88902ac6454SAndrew Thompson } else { 89002ac6454SAndrew Thompson /* the transfer is finished */ 89102ac6454SAndrew Thompson td = NULL; 89202ac6454SAndrew Thompson } 89302ac6454SAndrew Thompson break; 89402ac6454SAndrew Thompson } 89502ac6454SAndrew Thompson td = td->obj_next; 89602ac6454SAndrew Thompson 89702ac6454SAndrew Thompson if (td->alt_next != td_alt_next) { 89802ac6454SAndrew Thompson /* this USB frame is complete */ 89902ac6454SAndrew Thompson break; 90002ac6454SAndrew Thompson } 90102ac6454SAndrew Thompson } 90202ac6454SAndrew Thompson 90302ac6454SAndrew Thompson /* update transfer cache */ 90402ac6454SAndrew Thompson 90502ac6454SAndrew Thompson xfer->td_transfer_cache = td; 90602ac6454SAndrew Thompson 90702ac6454SAndrew Thompson DPRINTFN(16, "error cc=%d (%s)\n", 90802ac6454SAndrew Thompson cc, ohci_cc_strs[cc]); 90902ac6454SAndrew Thompson 91002ac6454SAndrew Thompson return ((cc == 0) ? USB_ERR_NORMAL_COMPLETION : 91102ac6454SAndrew Thompson (cc == OHCI_CC_STALL) ? USB_ERR_STALLED : USB_ERR_IOERROR); 91202ac6454SAndrew Thompson } 91302ac6454SAndrew Thompson 91402ac6454SAndrew Thompson static void 91502ac6454SAndrew Thompson ohci_non_isoc_done(struct usb2_xfer *xfer) 91602ac6454SAndrew Thompson { 91702ac6454SAndrew Thompson usb2_error_t err = 0; 91802ac6454SAndrew Thompson 91902ac6454SAndrew Thompson DPRINTFN(13, "xfer=%p pipe=%p transfer done\n", 92002ac6454SAndrew Thompson xfer, xfer->pipe); 92102ac6454SAndrew Thompson 92202ac6454SAndrew Thompson #if USB_DEBUG 92302ac6454SAndrew Thompson if (ohcidebug > 10) { 92402ac6454SAndrew Thompson ohci_dump_tds(xfer->td_transfer_first); 92502ac6454SAndrew Thompson } 92602ac6454SAndrew Thompson #endif 92702ac6454SAndrew Thompson 92802ac6454SAndrew Thompson /* reset scanner */ 92902ac6454SAndrew Thompson 93002ac6454SAndrew Thompson xfer->td_transfer_cache = xfer->td_transfer_first; 93102ac6454SAndrew Thompson 93202ac6454SAndrew Thompson if (xfer->flags_int.control_xfr) { 93302ac6454SAndrew Thompson 93402ac6454SAndrew Thompson if (xfer->flags_int.control_hdr) { 93502ac6454SAndrew Thompson 93602ac6454SAndrew Thompson err = ohci_non_isoc_done_sub(xfer); 93702ac6454SAndrew Thompson } 93802ac6454SAndrew Thompson xfer->aframes = 1; 93902ac6454SAndrew Thompson 94002ac6454SAndrew Thompson if (xfer->td_transfer_cache == NULL) { 94102ac6454SAndrew Thompson goto done; 94202ac6454SAndrew Thompson } 94302ac6454SAndrew Thompson } 94402ac6454SAndrew Thompson while (xfer->aframes != xfer->nframes) { 94502ac6454SAndrew Thompson 94602ac6454SAndrew Thompson err = ohci_non_isoc_done_sub(xfer); 94702ac6454SAndrew Thompson xfer->aframes++; 94802ac6454SAndrew Thompson 94902ac6454SAndrew Thompson if (xfer->td_transfer_cache == NULL) { 95002ac6454SAndrew Thompson goto done; 95102ac6454SAndrew Thompson } 95202ac6454SAndrew Thompson } 95302ac6454SAndrew Thompson 95402ac6454SAndrew Thompson if (xfer->flags_int.control_xfr && 95502ac6454SAndrew Thompson !xfer->flags_int.control_act) { 95602ac6454SAndrew Thompson 95702ac6454SAndrew Thompson err = ohci_non_isoc_done_sub(xfer); 95802ac6454SAndrew Thompson } 95902ac6454SAndrew Thompson done: 96002ac6454SAndrew Thompson ohci_device_done(xfer, err); 96102ac6454SAndrew Thompson } 96202ac6454SAndrew Thompson 96302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 96402ac6454SAndrew Thompson * ohci_check_transfer_sub 96502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 96602ac6454SAndrew Thompson static void 96702ac6454SAndrew Thompson ohci_check_transfer_sub(struct usb2_xfer *xfer) 96802ac6454SAndrew Thompson { 96902ac6454SAndrew Thompson ohci_td_t *td; 97002ac6454SAndrew Thompson ohci_ed_t *ed; 97102ac6454SAndrew Thompson uint32_t phy_start; 97202ac6454SAndrew Thompson uint32_t td_flags; 97302ac6454SAndrew Thompson uint32_t td_next; 97402ac6454SAndrew Thompson uint16_t cc; 97502ac6454SAndrew Thompson 97602ac6454SAndrew Thompson td = xfer->td_transfer_cache; 97702ac6454SAndrew Thompson 97802ac6454SAndrew Thompson while (1) { 97902ac6454SAndrew Thompson 98002ac6454SAndrew Thompson usb2_pc_cpu_invalidate(td->page_cache); 98102ac6454SAndrew Thompson phy_start = le32toh(td->td_cbp); 98202ac6454SAndrew Thompson td_flags = le32toh(td->td_flags); 98302ac6454SAndrew Thompson td_next = le32toh(td->td_next); 98402ac6454SAndrew Thompson 98502ac6454SAndrew Thompson /* Check for last transfer */ 98602ac6454SAndrew Thompson if (((void *)td) == xfer->td_transfer_last) { 98702ac6454SAndrew Thompson /* the transfer is finished */ 98802ac6454SAndrew Thompson td = NULL; 98902ac6454SAndrew Thompson break; 99002ac6454SAndrew Thompson } 99102ac6454SAndrew Thompson /* Check transfer status */ 99202ac6454SAndrew Thompson cc = OHCI_TD_GET_CC(td_flags); 99302ac6454SAndrew Thompson if (cc) { 99402ac6454SAndrew Thompson /* the transfer is finished */ 99502ac6454SAndrew Thompson td = NULL; 99602ac6454SAndrew Thompson break; 99702ac6454SAndrew Thompson } 99802ac6454SAndrew Thompson /* 99902ac6454SAndrew Thompson * Check if we reached the last packet 100002ac6454SAndrew Thompson * or if there is a short packet: 100102ac6454SAndrew Thompson */ 100202ac6454SAndrew Thompson 100302ac6454SAndrew Thompson if (((td_next & (~0xF)) == OHCI_TD_NEXT_END) || phy_start) { 100402ac6454SAndrew Thompson /* follow alt next */ 100502ac6454SAndrew Thompson td = td->alt_next; 100602ac6454SAndrew Thompson break; 100702ac6454SAndrew Thompson } 100802ac6454SAndrew Thompson td = td->obj_next; 100902ac6454SAndrew Thompson } 101002ac6454SAndrew Thompson 101102ac6454SAndrew Thompson /* update transfer cache */ 101202ac6454SAndrew Thompson 101302ac6454SAndrew Thompson xfer->td_transfer_cache = td; 101402ac6454SAndrew Thompson 101502ac6454SAndrew Thompson if (td) { 101602ac6454SAndrew Thompson 101702ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 101802ac6454SAndrew Thompson 101902ac6454SAndrew Thompson ed->ed_headp = td->td_self; 102002ac6454SAndrew Thompson usb2_pc_cpu_flush(ed->page_cache); 102102ac6454SAndrew Thompson 102202ac6454SAndrew Thompson DPRINTFN(13, "xfer=%p following alt next\n", xfer); 10230f7d4548SAndrew Thompson 10240f7d4548SAndrew Thompson /* 10250f7d4548SAndrew Thompson * Make sure that the OHCI re-scans the schedule by 10260f7d4548SAndrew Thompson * writing the BLF and CLF bits: 10270f7d4548SAndrew Thompson */ 10280f7d4548SAndrew Thompson 10290f7d4548SAndrew Thompson if (xfer->xroot->udev->pwr_save.suspended) { 10300f7d4548SAndrew Thompson /* nothing to do */ 10310f7d4548SAndrew Thompson } else if (xfer->pipe->methods == &ohci_device_bulk_methods) { 10320f7d4548SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 10330f7d4548SAndrew Thompson 10340f7d4548SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF); 10350f7d4548SAndrew Thompson } else if (xfer->pipe->methods == &ohci_device_ctrl_methods) { 10360f7d4548SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 10370f7d4548SAndrew Thompson 10380f7d4548SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF); 10390f7d4548SAndrew Thompson } 104002ac6454SAndrew Thompson } 104102ac6454SAndrew Thompson } 104202ac6454SAndrew Thompson 104302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 104402ac6454SAndrew Thompson * ohci_check_transfer 104502ac6454SAndrew Thompson * 104602ac6454SAndrew Thompson * Return values: 104702ac6454SAndrew Thompson * 0: USB transfer is not finished 104802ac6454SAndrew Thompson * Else: USB transfer is finished 104902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 105002ac6454SAndrew Thompson static uint8_t 105102ac6454SAndrew Thompson ohci_check_transfer(struct usb2_xfer *xfer) 105202ac6454SAndrew Thompson { 105302ac6454SAndrew Thompson ohci_ed_t *ed; 105402ac6454SAndrew Thompson uint32_t ed_headp; 105502ac6454SAndrew Thompson uint32_t ed_tailp; 105602ac6454SAndrew Thompson 105702ac6454SAndrew Thompson DPRINTFN(13, "xfer=%p checking transfer\n", xfer); 105802ac6454SAndrew Thompson 105902ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 106002ac6454SAndrew Thompson 106102ac6454SAndrew Thompson usb2_pc_cpu_invalidate(ed->page_cache); 106202ac6454SAndrew Thompson ed_headp = le32toh(ed->ed_headp); 106302ac6454SAndrew Thompson ed_tailp = le32toh(ed->ed_tailp); 106402ac6454SAndrew Thompson 10650f7d4548SAndrew Thompson if ((ed_headp & OHCI_HALTED) || 106602ac6454SAndrew Thompson (((ed_headp ^ ed_tailp) & (~0xF)) == 0)) { 106702ac6454SAndrew Thompson if (xfer->pipe->methods == &ohci_device_isoc_methods) { 106802ac6454SAndrew Thompson /* isochronous transfer */ 106902ac6454SAndrew Thompson ohci_isoc_done(xfer); 107002ac6454SAndrew Thompson } else { 107102ac6454SAndrew Thompson if (xfer->flags_int.short_frames_ok) { 107202ac6454SAndrew Thompson ohci_check_transfer_sub(xfer); 107302ac6454SAndrew Thompson if (xfer->td_transfer_cache) { 107402ac6454SAndrew Thompson /* not finished yet */ 107502ac6454SAndrew Thompson return (0); 107602ac6454SAndrew Thompson } 107702ac6454SAndrew Thompson } 107802ac6454SAndrew Thompson /* store data-toggle */ 107902ac6454SAndrew Thompson if (ed_headp & OHCI_TOGGLECARRY) { 108002ac6454SAndrew Thompson xfer->pipe->toggle_next = 1; 108102ac6454SAndrew Thompson } else { 108202ac6454SAndrew Thompson xfer->pipe->toggle_next = 0; 108302ac6454SAndrew Thompson } 108402ac6454SAndrew Thompson 108502ac6454SAndrew Thompson /* non-isochronous transfer */ 108602ac6454SAndrew Thompson ohci_non_isoc_done(xfer); 108702ac6454SAndrew Thompson } 108802ac6454SAndrew Thompson return (1); 108902ac6454SAndrew Thompson } 109002ac6454SAndrew Thompson DPRINTFN(13, "xfer=%p is still active\n", xfer); 109102ac6454SAndrew Thompson return (0); 109202ac6454SAndrew Thompson } 109302ac6454SAndrew Thompson 109402ac6454SAndrew Thompson static void 109502ac6454SAndrew Thompson ohci_rhsc_enable(ohci_softc_t *sc) 109602ac6454SAndrew Thompson { 109702ac6454SAndrew Thompson DPRINTFN(5, "\n"); 109802ac6454SAndrew Thompson 109902ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 110002ac6454SAndrew Thompson 110102ac6454SAndrew Thompson sc->sc_eintrs |= OHCI_RHSC; 110202ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC); 110302ac6454SAndrew Thompson 110402ac6454SAndrew Thompson /* acknowledge any RHSC interrupt */ 110502ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_STATUS, OHCI_RHSC); 110602ac6454SAndrew Thompson 110702ac6454SAndrew Thompson usb2_sw_transfer(&sc->sc_root_intr, 110802ac6454SAndrew Thompson &ohci_root_intr_done); 110902ac6454SAndrew Thompson } 111002ac6454SAndrew Thompson 111102ac6454SAndrew Thompson static void 111202ac6454SAndrew Thompson ohci_interrupt_poll(ohci_softc_t *sc) 111302ac6454SAndrew Thompson { 111402ac6454SAndrew Thompson struct usb2_xfer *xfer; 111502ac6454SAndrew Thompson 111602ac6454SAndrew Thompson repeat: 111702ac6454SAndrew Thompson TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 111802ac6454SAndrew Thompson /* 111902ac6454SAndrew Thompson * check if transfer is transferred 112002ac6454SAndrew Thompson */ 112102ac6454SAndrew Thompson if (ohci_check_transfer(xfer)) { 112202ac6454SAndrew Thompson /* queue has been modified */ 112302ac6454SAndrew Thompson goto repeat; 112402ac6454SAndrew Thompson } 112502ac6454SAndrew Thompson } 112602ac6454SAndrew Thompson } 112702ac6454SAndrew Thompson 112802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 112902ac6454SAndrew Thompson * ohci_interrupt - OHCI interrupt handler 113002ac6454SAndrew Thompson * 113102ac6454SAndrew Thompson * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler, 113202ac6454SAndrew Thompson * hence the interrupt handler will be setup before "sc->sc_bus.bdev" 113302ac6454SAndrew Thompson * is present ! 113402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 113502ac6454SAndrew Thompson void 113602ac6454SAndrew Thompson ohci_interrupt(ohci_softc_t *sc) 113702ac6454SAndrew Thompson { 113802ac6454SAndrew Thompson struct ohci_hcca *hcca; 113902ac6454SAndrew Thompson uint32_t status; 114002ac6454SAndrew Thompson uint32_t done; 114102ac6454SAndrew Thompson 114202ac6454SAndrew Thompson USB_BUS_LOCK(&sc->sc_bus); 114302ac6454SAndrew Thompson 114402ac6454SAndrew Thompson hcca = ohci_get_hcca(sc); 114502ac6454SAndrew Thompson 114602ac6454SAndrew Thompson DPRINTFN(16, "real interrupt\n"); 114702ac6454SAndrew Thompson 114802ac6454SAndrew Thompson #if USB_DEBUG 114902ac6454SAndrew Thompson if (ohcidebug > 15) { 115002ac6454SAndrew Thompson ohci_dumpregs(sc); 115102ac6454SAndrew Thompson } 115202ac6454SAndrew Thompson #endif 115302ac6454SAndrew Thompson 115402ac6454SAndrew Thompson done = le32toh(hcca->hcca_done_head); 115502ac6454SAndrew Thompson 115602ac6454SAndrew Thompson /* 115702ac6454SAndrew Thompson * The LSb of done is used to inform the HC Driver that an interrupt 115802ac6454SAndrew Thompson * condition exists for both the Done list and for another event 115902ac6454SAndrew Thompson * recorded in HcInterruptStatus. On an interrupt from the HC, the 116002ac6454SAndrew Thompson * HC Driver checks the HccaDoneHead Value. If this value is 0, then 116102ac6454SAndrew Thompson * the interrupt was caused by other than the HccaDoneHead update 116202ac6454SAndrew Thompson * and the HcInterruptStatus register needs to be accessed to 116302ac6454SAndrew Thompson * determine that exact interrupt cause. If HccaDoneHead is nonzero, 116402ac6454SAndrew Thompson * then a Done list update interrupt is indicated and if the LSb of 116502ac6454SAndrew Thompson * done is nonzero, then an additional interrupt event is indicated 116602ac6454SAndrew Thompson * and HcInterruptStatus should be checked to determine its cause. 116702ac6454SAndrew Thompson */ 116802ac6454SAndrew Thompson if (done != 0) { 116902ac6454SAndrew Thompson status = 0; 117002ac6454SAndrew Thompson 117102ac6454SAndrew Thompson if (done & ~OHCI_DONE_INTRS) { 117202ac6454SAndrew Thompson status |= OHCI_WDH; 117302ac6454SAndrew Thompson } 117402ac6454SAndrew Thompson if (done & OHCI_DONE_INTRS) { 117502ac6454SAndrew Thompson status |= OREAD4(sc, OHCI_INTERRUPT_STATUS); 117602ac6454SAndrew Thompson } 117702ac6454SAndrew Thompson hcca->hcca_done_head = 0; 117802ac6454SAndrew Thompson 117902ac6454SAndrew Thompson usb2_pc_cpu_flush(&sc->sc_hw.hcca_pc); 118002ac6454SAndrew Thompson } else { 118102ac6454SAndrew Thompson status = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH; 118202ac6454SAndrew Thompson } 118302ac6454SAndrew Thompson 118402ac6454SAndrew Thompson status &= ~OHCI_MIE; 118502ac6454SAndrew Thompson if (status == 0) { 118602ac6454SAndrew Thompson /* 118702ac6454SAndrew Thompson * nothing to be done (PCI shared 118802ac6454SAndrew Thompson * interrupt) 118902ac6454SAndrew Thompson */ 119002ac6454SAndrew Thompson goto done; 119102ac6454SAndrew Thompson } 119202ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_STATUS, status); /* Acknowledge */ 119302ac6454SAndrew Thompson 119402ac6454SAndrew Thompson status &= sc->sc_eintrs; 119502ac6454SAndrew Thompson if (status == 0) { 119602ac6454SAndrew Thompson goto done; 119702ac6454SAndrew Thompson } 119802ac6454SAndrew Thompson if (status & (OHCI_SO | OHCI_RD | OHCI_UE | OHCI_RHSC)) { 119902ac6454SAndrew Thompson #if 0 120002ac6454SAndrew Thompson if (status & OHCI_SO) { 120102ac6454SAndrew Thompson /* XXX do what */ 120202ac6454SAndrew Thompson } 120302ac6454SAndrew Thompson #endif 120402ac6454SAndrew Thompson if (status & OHCI_RD) { 120502ac6454SAndrew Thompson printf("%s: resume detect\n", __FUNCTION__); 120602ac6454SAndrew Thompson /* XXX process resume detect */ 120702ac6454SAndrew Thompson } 120802ac6454SAndrew Thompson if (status & OHCI_UE) { 120902ac6454SAndrew Thompson printf("%s: unrecoverable error, " 121002ac6454SAndrew Thompson "controller halted\n", __FUNCTION__); 121102ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET); 121202ac6454SAndrew Thompson /* XXX what else */ 121302ac6454SAndrew Thompson } 121402ac6454SAndrew Thompson if (status & OHCI_RHSC) { 121502ac6454SAndrew Thompson /* 121602ac6454SAndrew Thompson * Disable RHSC interrupt for now, because it will be 121702ac6454SAndrew Thompson * on until the port has been reset. 121802ac6454SAndrew Thompson */ 121902ac6454SAndrew Thompson sc->sc_eintrs &= ~OHCI_RHSC; 122002ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC); 122102ac6454SAndrew Thompson 122202ac6454SAndrew Thompson usb2_sw_transfer(&sc->sc_root_intr, 122302ac6454SAndrew Thompson &ohci_root_intr_done); 122402ac6454SAndrew Thompson 122502ac6454SAndrew Thompson /* do not allow RHSC interrupts > 1 per second */ 122602ac6454SAndrew Thompson usb2_callout_reset(&sc->sc_tmo_rhsc, hz, 122702ac6454SAndrew Thompson (void *)&ohci_rhsc_enable, sc); 122802ac6454SAndrew Thompson } 122902ac6454SAndrew Thompson } 123002ac6454SAndrew Thompson status &= ~(OHCI_RHSC | OHCI_WDH | OHCI_SO); 123102ac6454SAndrew Thompson if (status != 0) { 123202ac6454SAndrew Thompson /* Block unprocessed interrupts. XXX */ 123302ac6454SAndrew Thompson OWRITE4(sc, OHCI_INTERRUPT_DISABLE, status); 123402ac6454SAndrew Thompson sc->sc_eintrs &= ~status; 123502ac6454SAndrew Thompson printf("%s: blocking intrs 0x%x\n", 123602ac6454SAndrew Thompson __FUNCTION__, status); 123702ac6454SAndrew Thompson } 123802ac6454SAndrew Thompson /* poll all the USB transfers */ 123902ac6454SAndrew Thompson ohci_interrupt_poll(sc); 124002ac6454SAndrew Thompson 124102ac6454SAndrew Thompson done: 124202ac6454SAndrew Thompson USB_BUS_UNLOCK(&sc->sc_bus); 124302ac6454SAndrew Thompson } 124402ac6454SAndrew Thompson 124502ac6454SAndrew Thompson /* 124602ac6454SAndrew Thompson * called when a request does not complete 124702ac6454SAndrew Thompson */ 124802ac6454SAndrew Thompson static void 124902ac6454SAndrew Thompson ohci_timeout(void *arg) 125002ac6454SAndrew Thompson { 125102ac6454SAndrew Thompson struct usb2_xfer *xfer = arg; 125202ac6454SAndrew Thompson 125302ac6454SAndrew Thompson DPRINTF("xfer=%p\n", xfer); 125402ac6454SAndrew Thompson 125502ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 125602ac6454SAndrew Thompson 125702ac6454SAndrew Thompson /* transfer is transferred */ 125802ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_TIMEOUT); 125902ac6454SAndrew Thompson } 126002ac6454SAndrew Thompson 126102ac6454SAndrew Thompson static void 126202ac6454SAndrew Thompson ohci_do_poll(struct usb2_bus *bus) 126302ac6454SAndrew Thompson { 126402ac6454SAndrew Thompson struct ohci_softc *sc = OHCI_BUS2SC(bus); 126502ac6454SAndrew Thompson 126602ac6454SAndrew Thompson USB_BUS_LOCK(&sc->sc_bus); 126702ac6454SAndrew Thompson ohci_interrupt_poll(sc); 126802ac6454SAndrew Thompson ohci_root_ctrl_poll(sc); 126902ac6454SAndrew Thompson USB_BUS_UNLOCK(&sc->sc_bus); 127002ac6454SAndrew Thompson } 127102ac6454SAndrew Thompson 127202ac6454SAndrew Thompson static void 127302ac6454SAndrew Thompson ohci_setup_standard_chain_sub(struct ohci_std_temp *temp) 127402ac6454SAndrew Thompson { 127502ac6454SAndrew Thompson struct usb2_page_search buf_res; 127602ac6454SAndrew Thompson ohci_td_t *td; 127702ac6454SAndrew Thompson ohci_td_t *td_next; 127802ac6454SAndrew Thompson ohci_td_t *td_alt_next; 127902ac6454SAndrew Thompson uint32_t buf_offset; 128002ac6454SAndrew Thompson uint32_t average; 128102ac6454SAndrew Thompson uint32_t len_old; 128202ac6454SAndrew Thompson uint8_t shortpkt_old; 128302ac6454SAndrew Thompson uint8_t precompute; 128402ac6454SAndrew Thompson 128502ac6454SAndrew Thompson td_alt_next = NULL; 128602ac6454SAndrew Thompson buf_offset = 0; 128702ac6454SAndrew Thompson shortpkt_old = temp->shortpkt; 128802ac6454SAndrew Thompson len_old = temp->len; 128902ac6454SAndrew Thompson precompute = 1; 129002ac6454SAndrew Thompson 129102ac6454SAndrew Thompson /* software is used to detect short incoming transfers */ 129202ac6454SAndrew Thompson 129302ac6454SAndrew Thompson if ((temp->td_flags & htole32(OHCI_TD_DP_MASK)) == htole32(OHCI_TD_IN)) { 129402ac6454SAndrew Thompson temp->td_flags |= htole32(OHCI_TD_R); 129502ac6454SAndrew Thompson } else { 129602ac6454SAndrew Thompson temp->td_flags &= ~htole32(OHCI_TD_R); 129702ac6454SAndrew Thompson } 129802ac6454SAndrew Thompson 129902ac6454SAndrew Thompson restart: 130002ac6454SAndrew Thompson 130102ac6454SAndrew Thompson td = temp->td; 130202ac6454SAndrew Thompson td_next = temp->td_next; 130302ac6454SAndrew Thompson 130402ac6454SAndrew Thompson while (1) { 130502ac6454SAndrew Thompson 130602ac6454SAndrew Thompson if (temp->len == 0) { 130702ac6454SAndrew Thompson 130802ac6454SAndrew Thompson if (temp->shortpkt) { 130902ac6454SAndrew Thompson break; 131002ac6454SAndrew Thompson } 131102ac6454SAndrew Thompson /* send a Zero Length Packet, ZLP, last */ 131202ac6454SAndrew Thompson 131302ac6454SAndrew Thompson temp->shortpkt = 1; 131402ac6454SAndrew Thompson average = 0; 131502ac6454SAndrew Thompson 131602ac6454SAndrew Thompson } else { 131702ac6454SAndrew Thompson 131802ac6454SAndrew Thompson average = temp->average; 131902ac6454SAndrew Thompson 132002ac6454SAndrew Thompson if (temp->len < average) { 132102ac6454SAndrew Thompson if (temp->len % temp->max_frame_size) { 132202ac6454SAndrew Thompson temp->shortpkt = 1; 132302ac6454SAndrew Thompson } 132402ac6454SAndrew Thompson average = temp->len; 132502ac6454SAndrew Thompson } 132602ac6454SAndrew Thompson } 132702ac6454SAndrew Thompson 132802ac6454SAndrew Thompson if (td_next == NULL) { 132902ac6454SAndrew Thompson panic("%s: out of OHCI transfer descriptors!", __FUNCTION__); 133002ac6454SAndrew Thompson } 133102ac6454SAndrew Thompson /* get next TD */ 133202ac6454SAndrew Thompson 133302ac6454SAndrew Thompson td = td_next; 133402ac6454SAndrew Thompson td_next = td->obj_next; 133502ac6454SAndrew Thompson 133602ac6454SAndrew Thompson /* check if we are pre-computing */ 133702ac6454SAndrew Thompson 133802ac6454SAndrew Thompson if (precompute) { 133902ac6454SAndrew Thompson 134002ac6454SAndrew Thompson /* update remaining length */ 134102ac6454SAndrew Thompson 134202ac6454SAndrew Thompson temp->len -= average; 134302ac6454SAndrew Thompson 134402ac6454SAndrew Thompson continue; 134502ac6454SAndrew Thompson } 134602ac6454SAndrew Thompson /* fill out current TD */ 134702ac6454SAndrew Thompson td->td_flags = temp->td_flags; 134802ac6454SAndrew Thompson 134902ac6454SAndrew Thompson /* the next TD uses TOGGLE_CARRY */ 135002ac6454SAndrew Thompson temp->td_flags &= ~htole32(OHCI_TD_TOGGLE_MASK); 135102ac6454SAndrew Thompson 135202ac6454SAndrew Thompson if (average == 0) { 135302ac6454SAndrew Thompson 135402ac6454SAndrew Thompson td->td_cbp = 0; 135502ac6454SAndrew Thompson td->td_be = ~0; 135602ac6454SAndrew Thompson td->len = 0; 135702ac6454SAndrew Thompson 135802ac6454SAndrew Thompson } else { 135902ac6454SAndrew Thompson 136002ac6454SAndrew Thompson usb2_get_page(temp->pc, buf_offset, &buf_res); 136102ac6454SAndrew Thompson td->td_cbp = htole32(buf_res.physaddr); 136202ac6454SAndrew Thompson buf_offset += (average - 1); 136302ac6454SAndrew Thompson 136402ac6454SAndrew Thompson usb2_get_page(temp->pc, buf_offset, &buf_res); 136502ac6454SAndrew Thompson td->td_be = htole32(buf_res.physaddr); 136602ac6454SAndrew Thompson buf_offset++; 136702ac6454SAndrew Thompson 136802ac6454SAndrew Thompson td->len = average; 136902ac6454SAndrew Thompson 137002ac6454SAndrew Thompson /* update remaining length */ 137102ac6454SAndrew Thompson 137202ac6454SAndrew Thompson temp->len -= average; 137302ac6454SAndrew Thompson } 137402ac6454SAndrew Thompson 137502ac6454SAndrew Thompson if ((td_next == td_alt_next) && temp->setup_alt_next) { 137602ac6454SAndrew Thompson /* we need to receive these frames one by one ! */ 137702ac6454SAndrew Thompson td->td_flags &= htole32(~OHCI_TD_INTR_MASK); 137802ac6454SAndrew Thompson td->td_flags |= htole32(OHCI_TD_SET_DI(1)); 137902ac6454SAndrew Thompson td->td_next = htole32(OHCI_TD_NEXT_END); 138002ac6454SAndrew Thompson } else { 138102ac6454SAndrew Thompson if (td_next) { 138202ac6454SAndrew Thompson /* link the current TD with the next one */ 138302ac6454SAndrew Thompson td->td_next = td_next->td_self; 138402ac6454SAndrew Thompson } 138502ac6454SAndrew Thompson } 138602ac6454SAndrew Thompson 138702ac6454SAndrew Thompson td->alt_next = td_alt_next; 138802ac6454SAndrew Thompson 138902ac6454SAndrew Thompson usb2_pc_cpu_flush(td->page_cache); 139002ac6454SAndrew Thompson } 139102ac6454SAndrew Thompson 139202ac6454SAndrew Thompson if (precompute) { 139302ac6454SAndrew Thompson precompute = 0; 139402ac6454SAndrew Thompson 139502ac6454SAndrew Thompson /* setup alt next pointer, if any */ 13960f7d4548SAndrew Thompson if (temp->last_frame) { 13970f7d4548SAndrew Thompson /* no alternate next */ 13980f7d4548SAndrew Thompson td_alt_next = NULL; 139902ac6454SAndrew Thompson } else { 140002ac6454SAndrew Thompson /* we use this field internally */ 140102ac6454SAndrew Thompson td_alt_next = td_next; 140202ac6454SAndrew Thompson } 140302ac6454SAndrew Thompson 140402ac6454SAndrew Thompson /* restore */ 140502ac6454SAndrew Thompson temp->shortpkt = shortpkt_old; 140602ac6454SAndrew Thompson temp->len = len_old; 140702ac6454SAndrew Thompson goto restart; 140802ac6454SAndrew Thompson } 140902ac6454SAndrew Thompson temp->td = td; 141002ac6454SAndrew Thompson temp->td_next = td_next; 141102ac6454SAndrew Thompson } 141202ac6454SAndrew Thompson 141302ac6454SAndrew Thompson static void 141402ac6454SAndrew Thompson ohci_setup_standard_chain(struct usb2_xfer *xfer, ohci_ed_t **ed_last) 141502ac6454SAndrew Thompson { 141602ac6454SAndrew Thompson struct ohci_std_temp temp; 141702ac6454SAndrew Thompson struct usb2_pipe_methods *methods; 141802ac6454SAndrew Thompson ohci_ed_t *ed; 141902ac6454SAndrew Thompson ohci_td_t *td; 142002ac6454SAndrew Thompson uint32_t ed_flags; 142102ac6454SAndrew Thompson uint32_t x; 142202ac6454SAndrew Thompson 142302ac6454SAndrew Thompson DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n", 142402ac6454SAndrew Thompson xfer->address, UE_GET_ADDR(xfer->endpoint), 142502ac6454SAndrew Thompson xfer->sumlen, usb2_get_speed(xfer->xroot->udev)); 142602ac6454SAndrew Thompson 1427578d0effSAndrew Thompson temp.average = xfer->max_hc_frame_size; 142802ac6454SAndrew Thompson temp.max_frame_size = xfer->max_frame_size; 142902ac6454SAndrew Thompson 143002ac6454SAndrew Thompson /* toggle the DMA set we are using */ 143102ac6454SAndrew Thompson xfer->flags_int.curr_dma_set ^= 1; 143202ac6454SAndrew Thompson 143302ac6454SAndrew Thompson /* get next DMA set */ 143402ac6454SAndrew Thompson td = xfer->td_start[xfer->flags_int.curr_dma_set]; 143502ac6454SAndrew Thompson 143602ac6454SAndrew Thompson xfer->td_transfer_first = td; 143702ac6454SAndrew Thompson xfer->td_transfer_cache = td; 143802ac6454SAndrew Thompson 143902ac6454SAndrew Thompson temp.td = NULL; 144002ac6454SAndrew Thompson temp.td_next = td; 14410f7d4548SAndrew Thompson temp.last_frame = 0; 144202ac6454SAndrew Thompson temp.setup_alt_next = xfer->flags_int.short_frames_ok; 144302ac6454SAndrew Thompson 144402ac6454SAndrew Thompson methods = xfer->pipe->methods; 144502ac6454SAndrew Thompson 144602ac6454SAndrew Thompson /* check if we should prepend a setup message */ 144702ac6454SAndrew Thompson 144802ac6454SAndrew Thompson if (xfer->flags_int.control_xfr) { 144902ac6454SAndrew Thompson if (xfer->flags_int.control_hdr) { 145002ac6454SAndrew Thompson 145102ac6454SAndrew Thompson temp.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC | 145202ac6454SAndrew Thompson OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR); 145302ac6454SAndrew Thompson 145402ac6454SAndrew Thompson temp.len = xfer->frlengths[0]; 145502ac6454SAndrew Thompson temp.pc = xfer->frbuffers + 0; 145602ac6454SAndrew Thompson temp.shortpkt = temp.len ? 1 : 0; 14570f7d4548SAndrew Thompson /* check for last frame */ 14580f7d4548SAndrew Thompson if (xfer->nframes == 1) { 14590f7d4548SAndrew Thompson /* no STATUS stage yet, SETUP is last */ 14600f7d4548SAndrew Thompson if (xfer->flags_int.control_act) { 14610f7d4548SAndrew Thompson temp.last_frame = 1; 14620f7d4548SAndrew Thompson temp.setup_alt_next = 0; 14630f7d4548SAndrew Thompson } 14640f7d4548SAndrew Thompson } 146502ac6454SAndrew Thompson ohci_setup_standard_chain_sub(&temp); 146602ac6454SAndrew Thompson 146702ac6454SAndrew Thompson /* 146802ac6454SAndrew Thompson * XXX assume that the setup message is 146902ac6454SAndrew Thompson * contained within one USB packet: 147002ac6454SAndrew Thompson */ 147102ac6454SAndrew Thompson xfer->pipe->toggle_next = 1; 147202ac6454SAndrew Thompson } 147302ac6454SAndrew Thompson x = 1; 147402ac6454SAndrew Thompson } else { 147502ac6454SAndrew Thompson x = 0; 147602ac6454SAndrew Thompson } 147702ac6454SAndrew Thompson temp.td_flags = htole32(OHCI_TD_NOCC | OHCI_TD_NOINTR); 147802ac6454SAndrew Thompson 147902ac6454SAndrew Thompson /* set data toggle */ 148002ac6454SAndrew Thompson 148102ac6454SAndrew Thompson if (xfer->pipe->toggle_next) { 148202ac6454SAndrew Thompson temp.td_flags |= htole32(OHCI_TD_TOGGLE_1); 148302ac6454SAndrew Thompson } else { 148402ac6454SAndrew Thompson temp.td_flags |= htole32(OHCI_TD_TOGGLE_0); 148502ac6454SAndrew Thompson } 148602ac6454SAndrew Thompson 148702ac6454SAndrew Thompson /* set endpoint direction */ 148802ac6454SAndrew Thompson 148902ac6454SAndrew Thompson if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) { 149002ac6454SAndrew Thompson temp.td_flags |= htole32(OHCI_TD_IN); 149102ac6454SAndrew Thompson } else { 149202ac6454SAndrew Thompson temp.td_flags |= htole32(OHCI_TD_OUT); 149302ac6454SAndrew Thompson } 149402ac6454SAndrew Thompson 149502ac6454SAndrew Thompson while (x != xfer->nframes) { 149602ac6454SAndrew Thompson 149702ac6454SAndrew Thompson /* DATA0 / DATA1 message */ 149802ac6454SAndrew Thompson 149902ac6454SAndrew Thompson temp.len = xfer->frlengths[x]; 150002ac6454SAndrew Thompson temp.pc = xfer->frbuffers + x; 150102ac6454SAndrew Thompson 150202ac6454SAndrew Thompson x++; 150302ac6454SAndrew Thompson 150402ac6454SAndrew Thompson if (x == xfer->nframes) { 15050f7d4548SAndrew Thompson if (xfer->flags_int.control_xfr) { 15060f7d4548SAndrew Thompson /* no STATUS stage yet, DATA is last */ 15070f7d4548SAndrew Thompson if (xfer->flags_int.control_act) { 15080f7d4548SAndrew Thompson temp.last_frame = 1; 150902ac6454SAndrew Thompson temp.setup_alt_next = 0; 151002ac6454SAndrew Thompson } 15110f7d4548SAndrew Thompson } else { 15120f7d4548SAndrew Thompson temp.last_frame = 1; 15130f7d4548SAndrew Thompson temp.setup_alt_next = 0; 15140f7d4548SAndrew Thompson } 15150f7d4548SAndrew Thompson } 151602ac6454SAndrew Thompson if (temp.len == 0) { 151702ac6454SAndrew Thompson 151802ac6454SAndrew Thompson /* make sure that we send an USB packet */ 151902ac6454SAndrew Thompson 152002ac6454SAndrew Thompson temp.shortpkt = 0; 152102ac6454SAndrew Thompson 152202ac6454SAndrew Thompson } else { 152302ac6454SAndrew Thompson 152402ac6454SAndrew Thompson /* regular data transfer */ 152502ac6454SAndrew Thompson 152602ac6454SAndrew Thompson temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1; 152702ac6454SAndrew Thompson } 152802ac6454SAndrew Thompson 152902ac6454SAndrew Thompson ohci_setup_standard_chain_sub(&temp); 153002ac6454SAndrew Thompson } 153102ac6454SAndrew Thompson 153202ac6454SAndrew Thompson /* check if we should append a status stage */ 153302ac6454SAndrew Thompson 153402ac6454SAndrew Thompson if (xfer->flags_int.control_xfr && 153502ac6454SAndrew Thompson !xfer->flags_int.control_act) { 153602ac6454SAndrew Thompson 153702ac6454SAndrew Thompson /* 153802ac6454SAndrew Thompson * Send a DATA1 message and invert the current endpoint 153902ac6454SAndrew Thompson * direction. 154002ac6454SAndrew Thompson */ 154102ac6454SAndrew Thompson 154202ac6454SAndrew Thompson /* set endpoint direction and data toggle */ 154302ac6454SAndrew Thompson 154402ac6454SAndrew Thompson if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) { 154502ac6454SAndrew Thompson temp.td_flags = htole32(OHCI_TD_OUT | 154602ac6454SAndrew Thompson OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1)); 154702ac6454SAndrew Thompson } else { 154802ac6454SAndrew Thompson temp.td_flags = htole32(OHCI_TD_IN | 154902ac6454SAndrew Thompson OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1)); 155002ac6454SAndrew Thompson } 155102ac6454SAndrew Thompson 155202ac6454SAndrew Thompson temp.len = 0; 155302ac6454SAndrew Thompson temp.pc = NULL; 155402ac6454SAndrew Thompson temp.shortpkt = 0; 15550f7d4548SAndrew Thompson temp.last_frame = 1; 15560f7d4548SAndrew Thompson temp.setup_alt_next = 0; 155702ac6454SAndrew Thompson 155802ac6454SAndrew Thompson ohci_setup_standard_chain_sub(&temp); 155902ac6454SAndrew Thompson } 156002ac6454SAndrew Thompson td = temp.td; 156102ac6454SAndrew Thompson 15620f7d4548SAndrew Thompson /* Ensure that last TD is terminating: */ 156302ac6454SAndrew Thompson td->td_next = htole32(OHCI_TD_NEXT_END); 156402ac6454SAndrew Thompson td->td_flags &= ~htole32(OHCI_TD_INTR_MASK); 156502ac6454SAndrew Thompson td->td_flags |= htole32(OHCI_TD_SET_DI(1)); 156602ac6454SAndrew Thompson 156702ac6454SAndrew Thompson usb2_pc_cpu_flush(td->page_cache); 156802ac6454SAndrew Thompson 156902ac6454SAndrew Thompson /* must have at least one frame! */ 157002ac6454SAndrew Thompson 157102ac6454SAndrew Thompson xfer->td_transfer_last = td; 157202ac6454SAndrew Thompson 157302ac6454SAndrew Thompson #if USB_DEBUG 157402ac6454SAndrew Thompson if (ohcidebug > 8) { 157502ac6454SAndrew Thompson DPRINTF("nexttog=%d; data before transfer:\n", 157602ac6454SAndrew Thompson xfer->pipe->toggle_next); 157702ac6454SAndrew Thompson ohci_dump_tds(xfer->td_transfer_first); 157802ac6454SAndrew Thompson } 157902ac6454SAndrew Thompson #endif 158002ac6454SAndrew Thompson 158102ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 158202ac6454SAndrew Thompson 158302ac6454SAndrew Thompson ed_flags = (OHCI_ED_SET_FA(xfer->address) | 158402ac6454SAndrew Thompson OHCI_ED_SET_EN(UE_GET_ADDR(xfer->endpoint)) | 158502ac6454SAndrew Thompson OHCI_ED_SET_MAXP(xfer->max_frame_size)); 158602ac6454SAndrew Thompson 158702ac6454SAndrew Thompson ed_flags |= (OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD); 158802ac6454SAndrew Thompson 158902ac6454SAndrew Thompson if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 159002ac6454SAndrew Thompson ed_flags |= OHCI_ED_SPEED; 159102ac6454SAndrew Thompson } 159202ac6454SAndrew Thompson ed->ed_flags = htole32(ed_flags); 159302ac6454SAndrew Thompson 159402ac6454SAndrew Thompson td = xfer->td_transfer_first; 159502ac6454SAndrew Thompson 159602ac6454SAndrew Thompson ed->ed_headp = td->td_self; 159702ac6454SAndrew Thompson 159802ac6454SAndrew Thompson if (xfer->xroot->udev->pwr_save.suspended == 0) { 159902ac6454SAndrew Thompson /* the append function will flush the endpoint descriptor */ 160002ac6454SAndrew Thompson OHCI_APPEND_QH(ed, *ed_last); 160102ac6454SAndrew Thompson 160202ac6454SAndrew Thompson if (methods == &ohci_device_bulk_methods) { 160302ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 160402ac6454SAndrew Thompson 160502ac6454SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF); 160602ac6454SAndrew Thompson } 160702ac6454SAndrew Thompson if (methods == &ohci_device_ctrl_methods) { 160802ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 160902ac6454SAndrew Thompson 161002ac6454SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF); 161102ac6454SAndrew Thompson } 161202ac6454SAndrew Thompson } else { 161302ac6454SAndrew Thompson usb2_pc_cpu_flush(ed->page_cache); 161402ac6454SAndrew Thompson } 161502ac6454SAndrew Thompson } 161602ac6454SAndrew Thompson 161702ac6454SAndrew Thompson static void 161802ac6454SAndrew Thompson ohci_root_intr_done(struct usb2_xfer *xfer, 161902ac6454SAndrew Thompson struct usb2_sw_transfer *std) 162002ac6454SAndrew Thompson { 162102ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 162202ac6454SAndrew Thompson uint32_t hstatus; 162302ac6454SAndrew Thompson uint16_t i; 162402ac6454SAndrew Thompson uint16_t m; 162502ac6454SAndrew Thompson 162602ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 162702ac6454SAndrew Thompson 162802ac6454SAndrew Thompson if (std->state != USB_SW_TR_PRE_DATA) { 162902ac6454SAndrew Thompson if (std->state == USB_SW_TR_PRE_CALLBACK) { 163002ac6454SAndrew Thompson /* transfer transferred */ 163102ac6454SAndrew Thompson ohci_device_done(xfer, std->err); 163202ac6454SAndrew Thompson } 163302ac6454SAndrew Thompson goto done; 163402ac6454SAndrew Thompson } 163502ac6454SAndrew Thompson /* setup buffer */ 163602ac6454SAndrew Thompson std->ptr = sc->sc_hub_idata; 163702ac6454SAndrew Thompson std->len = sizeof(sc->sc_hub_idata); 163802ac6454SAndrew Thompson 163902ac6454SAndrew Thompson /* clear any old interrupt data */ 164002ac6454SAndrew Thompson bzero(sc->sc_hub_idata, sizeof(sc->sc_hub_idata)); 164102ac6454SAndrew Thompson 164202ac6454SAndrew Thompson hstatus = OREAD4(sc, OHCI_RH_STATUS); 164302ac6454SAndrew Thompson DPRINTF("sc=%p xfer=%p hstatus=0x%08x\n", 164402ac6454SAndrew Thompson sc, xfer, hstatus); 164502ac6454SAndrew Thompson 164602ac6454SAndrew Thompson /* set bits */ 164702ac6454SAndrew Thompson m = (sc->sc_noport + 1); 164802ac6454SAndrew Thompson if (m > (8 * sizeof(sc->sc_hub_idata))) { 164902ac6454SAndrew Thompson m = (8 * sizeof(sc->sc_hub_idata)); 165002ac6454SAndrew Thompson } 165102ac6454SAndrew Thompson for (i = 1; i < m; i++) { 165202ac6454SAndrew Thompson /* pick out CHANGE bits from the status register */ 165302ac6454SAndrew Thompson if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16) { 165402ac6454SAndrew Thompson sc->sc_hub_idata[i / 8] |= 1 << (i % 8); 165502ac6454SAndrew Thompson DPRINTF("port %d changed\n", i); 165602ac6454SAndrew Thompson } 165702ac6454SAndrew Thompson } 165802ac6454SAndrew Thompson done: 165902ac6454SAndrew Thompson return; 166002ac6454SAndrew Thompson } 166102ac6454SAndrew Thompson 166202ac6454SAndrew Thompson /* NOTE: "done" can be run two times in a row, 166302ac6454SAndrew Thompson * from close and from interrupt 166402ac6454SAndrew Thompson */ 166502ac6454SAndrew Thompson static void 166602ac6454SAndrew Thompson ohci_device_done(struct usb2_xfer *xfer, usb2_error_t error) 166702ac6454SAndrew Thompson { 166802ac6454SAndrew Thompson struct usb2_pipe_methods *methods = xfer->pipe->methods; 166902ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 167002ac6454SAndrew Thompson ohci_ed_t *ed; 167102ac6454SAndrew Thompson 167202ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 167302ac6454SAndrew Thompson 167402ac6454SAndrew Thompson 167502ac6454SAndrew Thompson DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n", 167602ac6454SAndrew Thompson xfer, xfer->pipe, error); 167702ac6454SAndrew Thompson 167802ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 167902ac6454SAndrew Thompson if (ed) { 168002ac6454SAndrew Thompson usb2_pc_cpu_invalidate(ed->page_cache); 168102ac6454SAndrew Thompson } 168202ac6454SAndrew Thompson if (methods == &ohci_device_bulk_methods) { 168302ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_bulk_p_last); 168402ac6454SAndrew Thompson } 168502ac6454SAndrew Thompson if (methods == &ohci_device_ctrl_methods) { 168602ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_ctrl_p_last); 168702ac6454SAndrew Thompson } 168802ac6454SAndrew Thompson if (methods == &ohci_device_intr_methods) { 168902ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]); 169002ac6454SAndrew Thompson } 169102ac6454SAndrew Thompson if (methods == &ohci_device_isoc_methods) { 169202ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_isoc_p_last); 169302ac6454SAndrew Thompson } 169402ac6454SAndrew Thompson xfer->td_transfer_first = NULL; 169502ac6454SAndrew Thompson xfer->td_transfer_last = NULL; 169602ac6454SAndrew Thompson 169702ac6454SAndrew Thompson /* dequeue transfer and start next transfer */ 169802ac6454SAndrew Thompson usb2_transfer_done(xfer, error); 169902ac6454SAndrew Thompson } 170002ac6454SAndrew Thompson 170102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 170202ac6454SAndrew Thompson * ohci bulk support 170302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 170402ac6454SAndrew Thompson static void 170502ac6454SAndrew Thompson ohci_device_bulk_open(struct usb2_xfer *xfer) 170602ac6454SAndrew Thompson { 170702ac6454SAndrew Thompson return; 170802ac6454SAndrew Thompson } 170902ac6454SAndrew Thompson 171002ac6454SAndrew Thompson static void 171102ac6454SAndrew Thompson ohci_device_bulk_close(struct usb2_xfer *xfer) 171202ac6454SAndrew Thompson { 171302ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_CANCELLED); 171402ac6454SAndrew Thompson } 171502ac6454SAndrew Thompson 171602ac6454SAndrew Thompson static void 171702ac6454SAndrew Thompson ohci_device_bulk_enter(struct usb2_xfer *xfer) 171802ac6454SAndrew Thompson { 171902ac6454SAndrew Thompson return; 172002ac6454SAndrew Thompson } 172102ac6454SAndrew Thompson 172202ac6454SAndrew Thompson static void 172302ac6454SAndrew Thompson ohci_device_bulk_start(struct usb2_xfer *xfer) 172402ac6454SAndrew Thompson { 172502ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 172602ac6454SAndrew Thompson 172702ac6454SAndrew Thompson /* setup TD's and QH */ 172802ac6454SAndrew Thompson ohci_setup_standard_chain(xfer, &sc->sc_bulk_p_last); 172902ac6454SAndrew Thompson 173002ac6454SAndrew Thompson /* put transfer on interrupt queue */ 173102ac6454SAndrew Thompson ohci_transfer_intr_enqueue(xfer); 173202ac6454SAndrew Thompson } 173302ac6454SAndrew Thompson 173402ac6454SAndrew Thompson struct usb2_pipe_methods ohci_device_bulk_methods = 173502ac6454SAndrew Thompson { 173602ac6454SAndrew Thompson .open = ohci_device_bulk_open, 173702ac6454SAndrew Thompson .close = ohci_device_bulk_close, 173802ac6454SAndrew Thompson .enter = ohci_device_bulk_enter, 173902ac6454SAndrew Thompson .start = ohci_device_bulk_start, 174002ac6454SAndrew Thompson .enter_is_cancelable = 1, 174102ac6454SAndrew Thompson .start_is_cancelable = 1, 174202ac6454SAndrew Thompson }; 174302ac6454SAndrew Thompson 174402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 174502ac6454SAndrew Thompson * ohci control support 174602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 174702ac6454SAndrew Thompson static void 174802ac6454SAndrew Thompson ohci_device_ctrl_open(struct usb2_xfer *xfer) 174902ac6454SAndrew Thompson { 175002ac6454SAndrew Thompson return; 175102ac6454SAndrew Thompson } 175202ac6454SAndrew Thompson 175302ac6454SAndrew Thompson static void 175402ac6454SAndrew Thompson ohci_device_ctrl_close(struct usb2_xfer *xfer) 175502ac6454SAndrew Thompson { 175602ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_CANCELLED); 175702ac6454SAndrew Thompson } 175802ac6454SAndrew Thompson 175902ac6454SAndrew Thompson static void 176002ac6454SAndrew Thompson ohci_device_ctrl_enter(struct usb2_xfer *xfer) 176102ac6454SAndrew Thompson { 176202ac6454SAndrew Thompson return; 176302ac6454SAndrew Thompson } 176402ac6454SAndrew Thompson 176502ac6454SAndrew Thompson static void 176602ac6454SAndrew Thompson ohci_device_ctrl_start(struct usb2_xfer *xfer) 176702ac6454SAndrew Thompson { 176802ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 176902ac6454SAndrew Thompson 177002ac6454SAndrew Thompson /* setup TD's and QH */ 177102ac6454SAndrew Thompson ohci_setup_standard_chain(xfer, &sc->sc_ctrl_p_last); 177202ac6454SAndrew Thompson 177302ac6454SAndrew Thompson /* put transfer on interrupt queue */ 177402ac6454SAndrew Thompson ohci_transfer_intr_enqueue(xfer); 177502ac6454SAndrew Thompson } 177602ac6454SAndrew Thompson 177702ac6454SAndrew Thompson struct usb2_pipe_methods ohci_device_ctrl_methods = 177802ac6454SAndrew Thompson { 177902ac6454SAndrew Thompson .open = ohci_device_ctrl_open, 178002ac6454SAndrew Thompson .close = ohci_device_ctrl_close, 178102ac6454SAndrew Thompson .enter = ohci_device_ctrl_enter, 178202ac6454SAndrew Thompson .start = ohci_device_ctrl_start, 178302ac6454SAndrew Thompson .enter_is_cancelable = 1, 178402ac6454SAndrew Thompson .start_is_cancelable = 1, 178502ac6454SAndrew Thompson }; 178602ac6454SAndrew Thompson 178702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 178802ac6454SAndrew Thompson * ohci interrupt support 178902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 179002ac6454SAndrew Thompson static void 179102ac6454SAndrew Thompson ohci_device_intr_open(struct usb2_xfer *xfer) 179202ac6454SAndrew Thompson { 179302ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 179402ac6454SAndrew Thompson uint16_t best; 179502ac6454SAndrew Thompson uint16_t bit; 179602ac6454SAndrew Thompson uint16_t x; 179702ac6454SAndrew Thompson 179802ac6454SAndrew Thompson best = 0; 179902ac6454SAndrew Thompson bit = OHCI_NO_EDS / 2; 180002ac6454SAndrew Thompson while (bit) { 180102ac6454SAndrew Thompson if (xfer->interval >= bit) { 180202ac6454SAndrew Thompson x = bit; 180302ac6454SAndrew Thompson best = bit; 180402ac6454SAndrew Thompson while (x & bit) { 180502ac6454SAndrew Thompson if (sc->sc_intr_stat[x] < 180602ac6454SAndrew Thompson sc->sc_intr_stat[best]) { 180702ac6454SAndrew Thompson best = x; 180802ac6454SAndrew Thompson } 180902ac6454SAndrew Thompson x++; 181002ac6454SAndrew Thompson } 181102ac6454SAndrew Thompson break; 181202ac6454SAndrew Thompson } 181302ac6454SAndrew Thompson bit >>= 1; 181402ac6454SAndrew Thompson } 181502ac6454SAndrew Thompson 181602ac6454SAndrew Thompson sc->sc_intr_stat[best]++; 181702ac6454SAndrew Thompson xfer->qh_pos = best; 181802ac6454SAndrew Thompson 181902ac6454SAndrew Thompson DPRINTFN(3, "best=%d interval=%d\n", 182002ac6454SAndrew Thompson best, xfer->interval); 182102ac6454SAndrew Thompson } 182202ac6454SAndrew Thompson 182302ac6454SAndrew Thompson static void 182402ac6454SAndrew Thompson ohci_device_intr_close(struct usb2_xfer *xfer) 182502ac6454SAndrew Thompson { 182602ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 182702ac6454SAndrew Thompson 182802ac6454SAndrew Thompson sc->sc_intr_stat[xfer->qh_pos]--; 182902ac6454SAndrew Thompson 183002ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_CANCELLED); 183102ac6454SAndrew Thompson } 183202ac6454SAndrew Thompson 183302ac6454SAndrew Thompson static void 183402ac6454SAndrew Thompson ohci_device_intr_enter(struct usb2_xfer *xfer) 183502ac6454SAndrew Thompson { 183602ac6454SAndrew Thompson return; 183702ac6454SAndrew Thompson } 183802ac6454SAndrew Thompson 183902ac6454SAndrew Thompson static void 184002ac6454SAndrew Thompson ohci_device_intr_start(struct usb2_xfer *xfer) 184102ac6454SAndrew Thompson { 184202ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 184302ac6454SAndrew Thompson 184402ac6454SAndrew Thompson /* setup TD's and QH */ 184502ac6454SAndrew Thompson ohci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]); 184602ac6454SAndrew Thompson 184702ac6454SAndrew Thompson /* put transfer on interrupt queue */ 184802ac6454SAndrew Thompson ohci_transfer_intr_enqueue(xfer); 184902ac6454SAndrew Thompson } 185002ac6454SAndrew Thompson 185102ac6454SAndrew Thompson struct usb2_pipe_methods ohci_device_intr_methods = 185202ac6454SAndrew Thompson { 185302ac6454SAndrew Thompson .open = ohci_device_intr_open, 185402ac6454SAndrew Thompson .close = ohci_device_intr_close, 185502ac6454SAndrew Thompson .enter = ohci_device_intr_enter, 185602ac6454SAndrew Thompson .start = ohci_device_intr_start, 185702ac6454SAndrew Thompson .enter_is_cancelable = 1, 185802ac6454SAndrew Thompson .start_is_cancelable = 1, 185902ac6454SAndrew Thompson }; 186002ac6454SAndrew Thompson 186102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 186202ac6454SAndrew Thompson * ohci isochronous support 186302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 186402ac6454SAndrew Thompson static void 186502ac6454SAndrew Thompson ohci_device_isoc_open(struct usb2_xfer *xfer) 186602ac6454SAndrew Thompson { 186702ac6454SAndrew Thompson return; 186802ac6454SAndrew Thompson } 186902ac6454SAndrew Thompson 187002ac6454SAndrew Thompson static void 187102ac6454SAndrew Thompson ohci_device_isoc_close(struct usb2_xfer *xfer) 187202ac6454SAndrew Thompson { 187302ac6454SAndrew Thompson /**/ 187402ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_CANCELLED); 187502ac6454SAndrew Thompson } 187602ac6454SAndrew Thompson 187702ac6454SAndrew Thompson static void 187802ac6454SAndrew Thompson ohci_device_isoc_enter(struct usb2_xfer *xfer) 187902ac6454SAndrew Thompson { 188002ac6454SAndrew Thompson struct usb2_page_search buf_res; 188102ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 188202ac6454SAndrew Thompson struct ohci_hcca *hcca; 188302ac6454SAndrew Thompson uint32_t buf_offset; 188402ac6454SAndrew Thompson uint32_t nframes; 188502ac6454SAndrew Thompson uint32_t ed_flags; 188602ac6454SAndrew Thompson uint32_t *plen; 188702ac6454SAndrew Thompson uint16_t itd_offset[OHCI_ITD_NOFFSET]; 188802ac6454SAndrew Thompson uint16_t length; 188902ac6454SAndrew Thompson uint8_t ncur; 189002ac6454SAndrew Thompson ohci_itd_t *td; 189102ac6454SAndrew Thompson ohci_itd_t *td_last = NULL; 189202ac6454SAndrew Thompson ohci_ed_t *ed; 189302ac6454SAndrew Thompson 189402ac6454SAndrew Thompson hcca = ohci_get_hcca(sc); 189502ac6454SAndrew Thompson 189602ac6454SAndrew Thompson nframes = le32toh(hcca->hcca_frame_number); 189702ac6454SAndrew Thompson 189802ac6454SAndrew Thompson DPRINTFN(6, "xfer=%p isoc_next=%u nframes=%u hcca_fn=%u\n", 189902ac6454SAndrew Thompson xfer, xfer->pipe->isoc_next, xfer->nframes, nframes); 190002ac6454SAndrew Thompson 190102ac6454SAndrew Thompson if ((xfer->pipe->is_synced == 0) || 190202ac6454SAndrew Thompson (((nframes - xfer->pipe->isoc_next) & 0xFFFF) < xfer->nframes) || 190302ac6454SAndrew Thompson (((xfer->pipe->isoc_next - nframes) & 0xFFFF) >= 128)) { 190402ac6454SAndrew Thompson /* 190502ac6454SAndrew Thompson * If there is data underflow or the pipe queue is empty we 190602ac6454SAndrew Thompson * schedule the transfer a few frames ahead of the current 190702ac6454SAndrew Thompson * frame position. Else two isochronous transfers might 190802ac6454SAndrew Thompson * overlap. 190902ac6454SAndrew Thompson */ 191002ac6454SAndrew Thompson xfer->pipe->isoc_next = (nframes + 3) & 0xFFFF; 191102ac6454SAndrew Thompson xfer->pipe->is_synced = 1; 191202ac6454SAndrew Thompson DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next); 191302ac6454SAndrew Thompson } 191402ac6454SAndrew Thompson /* 191502ac6454SAndrew Thompson * compute how many milliseconds the insertion is ahead of the 191602ac6454SAndrew Thompson * current frame position: 191702ac6454SAndrew Thompson */ 191802ac6454SAndrew Thompson buf_offset = ((xfer->pipe->isoc_next - nframes) & 0xFFFF); 191902ac6454SAndrew Thompson 192002ac6454SAndrew Thompson /* 192102ac6454SAndrew Thompson * pre-compute when the isochronous transfer will be finished: 192202ac6454SAndrew Thompson */ 192302ac6454SAndrew Thompson xfer->isoc_time_complete = 192402ac6454SAndrew Thompson (usb2_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset + 192502ac6454SAndrew Thompson xfer->nframes); 192602ac6454SAndrew Thompson 192702ac6454SAndrew Thompson /* get the real number of frames */ 192802ac6454SAndrew Thompson 192902ac6454SAndrew Thompson nframes = xfer->nframes; 193002ac6454SAndrew Thompson 193102ac6454SAndrew Thompson buf_offset = 0; 193202ac6454SAndrew Thompson 193302ac6454SAndrew Thompson plen = xfer->frlengths; 193402ac6454SAndrew Thompson 193502ac6454SAndrew Thompson /* toggle the DMA set we are using */ 193602ac6454SAndrew Thompson xfer->flags_int.curr_dma_set ^= 1; 193702ac6454SAndrew Thompson 193802ac6454SAndrew Thompson /* get next DMA set */ 193902ac6454SAndrew Thompson td = xfer->td_start[xfer->flags_int.curr_dma_set]; 194002ac6454SAndrew Thompson 194102ac6454SAndrew Thompson xfer->td_transfer_first = td; 194202ac6454SAndrew Thompson 194302ac6454SAndrew Thompson ncur = 0; 194402ac6454SAndrew Thompson length = 0; 194502ac6454SAndrew Thompson 194602ac6454SAndrew Thompson while (nframes--) { 194702ac6454SAndrew Thompson if (td == NULL) { 194802ac6454SAndrew Thompson panic("%s:%d: out of TD's\n", 194902ac6454SAndrew Thompson __FUNCTION__, __LINE__); 195002ac6454SAndrew Thompson } 195102ac6454SAndrew Thompson itd_offset[ncur] = length; 195202ac6454SAndrew Thompson buf_offset += *plen; 195302ac6454SAndrew Thompson length += *plen; 195402ac6454SAndrew Thompson plen++; 195502ac6454SAndrew Thompson ncur++; 195602ac6454SAndrew Thompson 195702ac6454SAndrew Thompson if ( /* check if the ITD is full */ 195802ac6454SAndrew Thompson (ncur == OHCI_ITD_NOFFSET) || 195902ac6454SAndrew Thompson /* check if we have put more than 4K into the ITD */ 196002ac6454SAndrew Thompson (length & 0xF000) || 196102ac6454SAndrew Thompson /* check if it is the last frame */ 196202ac6454SAndrew Thompson (nframes == 0)) { 196302ac6454SAndrew Thompson 196402ac6454SAndrew Thompson /* fill current ITD */ 196502ac6454SAndrew Thompson td->itd_flags = htole32( 196602ac6454SAndrew Thompson OHCI_ITD_NOCC | 196702ac6454SAndrew Thompson OHCI_ITD_SET_SF(xfer->pipe->isoc_next) | 196802ac6454SAndrew Thompson OHCI_ITD_NOINTR | 196902ac6454SAndrew Thompson OHCI_ITD_SET_FC(ncur)); 197002ac6454SAndrew Thompson 197102ac6454SAndrew Thompson td->frames = ncur; 197202ac6454SAndrew Thompson xfer->pipe->isoc_next += ncur; 197302ac6454SAndrew Thompson 197402ac6454SAndrew Thompson if (length == 0) { 197502ac6454SAndrew Thompson /* all zero */ 197602ac6454SAndrew Thompson td->itd_bp0 = 0; 197702ac6454SAndrew Thompson td->itd_be = ~0; 197802ac6454SAndrew Thompson 197902ac6454SAndrew Thompson while (ncur--) { 198002ac6454SAndrew Thompson td->itd_offset[ncur] = 198102ac6454SAndrew Thompson htole16(OHCI_ITD_MK_OFFS(0)); 198202ac6454SAndrew Thompson } 198302ac6454SAndrew Thompson } else { 198402ac6454SAndrew Thompson usb2_get_page(xfer->frbuffers, buf_offset - length, &buf_res); 198502ac6454SAndrew Thompson length = OHCI_PAGE_MASK(buf_res.physaddr); 198602ac6454SAndrew Thompson buf_res.physaddr = 198702ac6454SAndrew Thompson OHCI_PAGE(buf_res.physaddr); 198802ac6454SAndrew Thompson td->itd_bp0 = htole32(buf_res.physaddr); 198902ac6454SAndrew Thompson usb2_get_page(xfer->frbuffers, buf_offset - 1, &buf_res); 199002ac6454SAndrew Thompson td->itd_be = htole32(buf_res.physaddr); 199102ac6454SAndrew Thompson 199202ac6454SAndrew Thompson while (ncur--) { 199302ac6454SAndrew Thompson itd_offset[ncur] += length; 199402ac6454SAndrew Thompson itd_offset[ncur] = 199502ac6454SAndrew Thompson OHCI_ITD_MK_OFFS(itd_offset[ncur]); 199602ac6454SAndrew Thompson td->itd_offset[ncur] = 199702ac6454SAndrew Thompson htole16(itd_offset[ncur]); 199802ac6454SAndrew Thompson } 199902ac6454SAndrew Thompson } 200002ac6454SAndrew Thompson ncur = 0; 200102ac6454SAndrew Thompson length = 0; 200202ac6454SAndrew Thompson td_last = td; 200302ac6454SAndrew Thompson td = td->obj_next; 200402ac6454SAndrew Thompson 200502ac6454SAndrew Thompson if (td) { 200602ac6454SAndrew Thompson /* link the last TD with the next one */ 200702ac6454SAndrew Thompson td_last->itd_next = td->itd_self; 200802ac6454SAndrew Thompson } 200902ac6454SAndrew Thompson usb2_pc_cpu_flush(td_last->page_cache); 201002ac6454SAndrew Thompson } 201102ac6454SAndrew Thompson } 201202ac6454SAndrew Thompson 201302ac6454SAndrew Thompson /* update the last TD */ 201402ac6454SAndrew Thompson td_last->itd_flags &= ~htole32(OHCI_ITD_NOINTR); 201502ac6454SAndrew Thompson td_last->itd_flags |= htole32(OHCI_ITD_SET_DI(0)); 201602ac6454SAndrew Thompson td_last->itd_next = 0; 201702ac6454SAndrew Thompson 201802ac6454SAndrew Thompson usb2_pc_cpu_flush(td_last->page_cache); 201902ac6454SAndrew Thompson 202002ac6454SAndrew Thompson xfer->td_transfer_last = td_last; 202102ac6454SAndrew Thompson 202202ac6454SAndrew Thompson #if USB_DEBUG 202302ac6454SAndrew Thompson if (ohcidebug > 8) { 202402ac6454SAndrew Thompson DPRINTF("data before transfer:\n"); 202502ac6454SAndrew Thompson ohci_dump_itds(xfer->td_transfer_first); 202602ac6454SAndrew Thompson } 202702ac6454SAndrew Thompson #endif 202802ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 202902ac6454SAndrew Thompson 203002ac6454SAndrew Thompson if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) 203102ac6454SAndrew Thompson ed_flags = (OHCI_ED_DIR_IN | OHCI_ED_FORMAT_ISO); 203202ac6454SAndrew Thompson else 203302ac6454SAndrew Thompson ed_flags = (OHCI_ED_DIR_OUT | OHCI_ED_FORMAT_ISO); 203402ac6454SAndrew Thompson 203502ac6454SAndrew Thompson ed_flags |= (OHCI_ED_SET_FA(xfer->address) | 203602ac6454SAndrew Thompson OHCI_ED_SET_EN(UE_GET_ADDR(xfer->endpoint)) | 203702ac6454SAndrew Thompson OHCI_ED_SET_MAXP(xfer->max_frame_size)); 203802ac6454SAndrew Thompson 203902ac6454SAndrew Thompson if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 204002ac6454SAndrew Thompson ed_flags |= OHCI_ED_SPEED; 204102ac6454SAndrew Thompson } 204202ac6454SAndrew Thompson ed->ed_flags = htole32(ed_flags); 204302ac6454SAndrew Thompson 204402ac6454SAndrew Thompson td = xfer->td_transfer_first; 204502ac6454SAndrew Thompson 204602ac6454SAndrew Thompson ed->ed_headp = td->itd_self; 204702ac6454SAndrew Thompson 204802ac6454SAndrew Thompson /* isochronous transfers are not affected by suspend / resume */ 204902ac6454SAndrew Thompson /* the append function will flush the endpoint descriptor */ 205002ac6454SAndrew Thompson 205102ac6454SAndrew Thompson OHCI_APPEND_QH(ed, sc->sc_isoc_p_last); 205202ac6454SAndrew Thompson } 205302ac6454SAndrew Thompson 205402ac6454SAndrew Thompson static void 205502ac6454SAndrew Thompson ohci_device_isoc_start(struct usb2_xfer *xfer) 205602ac6454SAndrew Thompson { 205702ac6454SAndrew Thompson /* put transfer on interrupt queue */ 205802ac6454SAndrew Thompson ohci_transfer_intr_enqueue(xfer); 205902ac6454SAndrew Thompson } 206002ac6454SAndrew Thompson 206102ac6454SAndrew Thompson struct usb2_pipe_methods ohci_device_isoc_methods = 206202ac6454SAndrew Thompson { 206302ac6454SAndrew Thompson .open = ohci_device_isoc_open, 206402ac6454SAndrew Thompson .close = ohci_device_isoc_close, 206502ac6454SAndrew Thompson .enter = ohci_device_isoc_enter, 206602ac6454SAndrew Thompson .start = ohci_device_isoc_start, 206702ac6454SAndrew Thompson .enter_is_cancelable = 1, 206802ac6454SAndrew Thompson .start_is_cancelable = 1, 206902ac6454SAndrew Thompson }; 207002ac6454SAndrew Thompson 207102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 207202ac6454SAndrew Thompson * ohci root control support 207302ac6454SAndrew Thompson *------------------------------------------------------------------------* 207402ac6454SAndrew Thompson * simulate a hardware hub by handling 207502ac6454SAndrew Thompson * all the necessary requests 207602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 207702ac6454SAndrew Thompson 207802ac6454SAndrew Thompson static void 207902ac6454SAndrew Thompson ohci_root_ctrl_open(struct usb2_xfer *xfer) 208002ac6454SAndrew Thompson { 208102ac6454SAndrew Thompson return; 208202ac6454SAndrew Thompson } 208302ac6454SAndrew Thompson 208402ac6454SAndrew Thompson static void 208502ac6454SAndrew Thompson ohci_root_ctrl_close(struct usb2_xfer *xfer) 208602ac6454SAndrew Thompson { 208702ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 208802ac6454SAndrew Thompson 208902ac6454SAndrew Thompson if (sc->sc_root_ctrl.xfer == xfer) { 209002ac6454SAndrew Thompson sc->sc_root_ctrl.xfer = NULL; 209102ac6454SAndrew Thompson } 209202ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_CANCELLED); 209302ac6454SAndrew Thompson } 209402ac6454SAndrew Thompson 209502ac6454SAndrew Thompson /* data structures and routines 209602ac6454SAndrew Thompson * to emulate the root hub: 209702ac6454SAndrew Thompson */ 209802ac6454SAndrew Thompson static const 209902ac6454SAndrew Thompson struct usb2_device_descriptor ohci_devd = 210002ac6454SAndrew Thompson { 210102ac6454SAndrew Thompson sizeof(struct usb2_device_descriptor), 210202ac6454SAndrew Thompson UDESC_DEVICE, /* type */ 210302ac6454SAndrew Thompson {0x00, 0x01}, /* USB version */ 210402ac6454SAndrew Thompson UDCLASS_HUB, /* class */ 210502ac6454SAndrew Thompson UDSUBCLASS_HUB, /* subclass */ 210602ac6454SAndrew Thompson UDPROTO_FSHUB, /* protocol */ 210702ac6454SAndrew Thompson 64, /* max packet */ 210802ac6454SAndrew Thompson {0}, {0}, {0x00, 0x01}, /* device id */ 210902ac6454SAndrew Thompson 1, 2, 0, /* string indicies */ 211002ac6454SAndrew Thompson 1 /* # of configurations */ 211102ac6454SAndrew Thompson }; 211202ac6454SAndrew Thompson 211302ac6454SAndrew Thompson static const 211402ac6454SAndrew Thompson struct ohci_config_desc ohci_confd = 211502ac6454SAndrew Thompson { 211602ac6454SAndrew Thompson .confd = { 211702ac6454SAndrew Thompson .bLength = sizeof(struct usb2_config_descriptor), 211802ac6454SAndrew Thompson .bDescriptorType = UDESC_CONFIG, 211902ac6454SAndrew Thompson .wTotalLength[0] = sizeof(ohci_confd), 212002ac6454SAndrew Thompson .bNumInterface = 1, 212102ac6454SAndrew Thompson .bConfigurationValue = 1, 212202ac6454SAndrew Thompson .iConfiguration = 0, 212302ac6454SAndrew Thompson .bmAttributes = UC_SELF_POWERED, 212402ac6454SAndrew Thompson .bMaxPower = 0, /* max power */ 212502ac6454SAndrew Thompson }, 212602ac6454SAndrew Thompson 212702ac6454SAndrew Thompson .ifcd = { 212802ac6454SAndrew Thompson .bLength = sizeof(struct usb2_interface_descriptor), 212902ac6454SAndrew Thompson .bDescriptorType = UDESC_INTERFACE, 213002ac6454SAndrew Thompson .bNumEndpoints = 1, 213102ac6454SAndrew Thompson .bInterfaceClass = UICLASS_HUB, 213202ac6454SAndrew Thompson .bInterfaceSubClass = UISUBCLASS_HUB, 213302ac6454SAndrew Thompson .bInterfaceProtocol = UIPROTO_FSHUB, 213402ac6454SAndrew Thompson }, 213502ac6454SAndrew Thompson 213602ac6454SAndrew Thompson .endpd = { 213702ac6454SAndrew Thompson .bLength = sizeof(struct usb2_endpoint_descriptor), 213802ac6454SAndrew Thompson .bDescriptorType = UDESC_ENDPOINT, 213902ac6454SAndrew Thompson .bEndpointAddress = UE_DIR_IN | OHCI_INTR_ENDPT, 214002ac6454SAndrew Thompson .bmAttributes = UE_INTERRUPT, 214102ac6454SAndrew Thompson .wMaxPacketSize[0] = 32,/* max packet (255 ports) */ 214202ac6454SAndrew Thompson .bInterval = 255, 214302ac6454SAndrew Thompson }, 214402ac6454SAndrew Thompson }; 214502ac6454SAndrew Thompson 214602ac6454SAndrew Thompson static const 214702ac6454SAndrew Thompson struct usb2_hub_descriptor ohci_hubd = 214802ac6454SAndrew Thompson { 214902ac6454SAndrew Thompson 0, /* dynamic length */ 215002ac6454SAndrew Thompson UDESC_HUB, 215102ac6454SAndrew Thompson 0, 215202ac6454SAndrew Thompson {0, 0}, 215302ac6454SAndrew Thompson 0, 215402ac6454SAndrew Thompson 0, 215502ac6454SAndrew Thompson {0}, 215602ac6454SAndrew Thompson }; 215702ac6454SAndrew Thompson 215802ac6454SAndrew Thompson static void 215902ac6454SAndrew Thompson ohci_root_ctrl_enter(struct usb2_xfer *xfer) 216002ac6454SAndrew Thompson { 216102ac6454SAndrew Thompson return; 216202ac6454SAndrew Thompson } 216302ac6454SAndrew Thompson 216402ac6454SAndrew Thompson static void 216502ac6454SAndrew Thompson ohci_root_ctrl_start(struct usb2_xfer *xfer) 216602ac6454SAndrew Thompson { 216702ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 216802ac6454SAndrew Thompson 216902ac6454SAndrew Thompson sc->sc_root_ctrl.xfer = xfer; 217002ac6454SAndrew Thompson 217102ac6454SAndrew Thompson usb2_bus_roothub_exec(xfer->xroot->bus); 217202ac6454SAndrew Thompson } 217302ac6454SAndrew Thompson 217402ac6454SAndrew Thompson static void 217502ac6454SAndrew Thompson ohci_root_ctrl_task(struct usb2_bus *bus) 217602ac6454SAndrew Thompson { 217702ac6454SAndrew Thompson ohci_root_ctrl_poll(OHCI_BUS2SC(bus)); 217802ac6454SAndrew Thompson } 217902ac6454SAndrew Thompson 218002ac6454SAndrew Thompson static void 218102ac6454SAndrew Thompson ohci_root_ctrl_done(struct usb2_xfer *xfer, 218202ac6454SAndrew Thompson struct usb2_sw_transfer *std) 218302ac6454SAndrew Thompson { 218402ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 218502ac6454SAndrew Thompson char *ptr; 218602ac6454SAndrew Thompson uint32_t port; 218702ac6454SAndrew Thompson uint32_t v; 218802ac6454SAndrew Thompson uint16_t value; 218902ac6454SAndrew Thompson uint16_t index; 219002ac6454SAndrew Thompson uint8_t l; 219102ac6454SAndrew Thompson 219202ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 219302ac6454SAndrew Thompson 219402ac6454SAndrew Thompson if (std->state != USB_SW_TR_SETUP) { 219502ac6454SAndrew Thompson if (std->state == USB_SW_TR_PRE_CALLBACK) { 219602ac6454SAndrew Thompson /* transfer transferred */ 219702ac6454SAndrew Thompson ohci_device_done(xfer, std->err); 219802ac6454SAndrew Thompson } 219902ac6454SAndrew Thompson goto done; 220002ac6454SAndrew Thompson } 220102ac6454SAndrew Thompson /* buffer reset */ 220202ac6454SAndrew Thompson std->ptr = sc->sc_hub_desc.temp; 220302ac6454SAndrew Thompson std->len = 0; 220402ac6454SAndrew Thompson 220502ac6454SAndrew Thompson value = UGETW(std->req.wValue); 220602ac6454SAndrew Thompson index = UGETW(std->req.wIndex); 220702ac6454SAndrew Thompson 220802ac6454SAndrew Thompson DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x " 220902ac6454SAndrew Thompson "wValue=0x%04x wIndex=0x%04x\n", 221002ac6454SAndrew Thompson std->req.bmRequestType, std->req.bRequest, 221102ac6454SAndrew Thompson UGETW(std->req.wLength), value, index); 221202ac6454SAndrew Thompson 221302ac6454SAndrew Thompson #define C(x,y) ((x) | ((y) << 8)) 221402ac6454SAndrew Thompson switch (C(std->req.bRequest, std->req.bmRequestType)) { 221502ac6454SAndrew Thompson case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 221602ac6454SAndrew Thompson case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 221702ac6454SAndrew Thompson case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 221802ac6454SAndrew Thompson /* 221902ac6454SAndrew Thompson * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 222002ac6454SAndrew Thompson * for the integrated root hub. 222102ac6454SAndrew Thompson */ 222202ac6454SAndrew Thompson break; 222302ac6454SAndrew Thompson case C(UR_GET_CONFIG, UT_READ_DEVICE): 222402ac6454SAndrew Thompson std->len = 1; 222502ac6454SAndrew Thompson sc->sc_hub_desc.temp[0] = sc->sc_conf; 222602ac6454SAndrew Thompson break; 222702ac6454SAndrew Thompson case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 222802ac6454SAndrew Thompson switch (value >> 8) { 222902ac6454SAndrew Thompson case UDESC_DEVICE: 223002ac6454SAndrew Thompson if ((value & 0xff) != 0) { 223102ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 223202ac6454SAndrew Thompson goto done; 223302ac6454SAndrew Thompson } 223402ac6454SAndrew Thompson std->len = sizeof(ohci_devd); 223502ac6454SAndrew Thompson sc->sc_hub_desc.devd = ohci_devd; 223602ac6454SAndrew Thompson break; 223702ac6454SAndrew Thompson 223802ac6454SAndrew Thompson case UDESC_CONFIG: 223902ac6454SAndrew Thompson if ((value & 0xff) != 0) { 224002ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 224102ac6454SAndrew Thompson goto done; 224202ac6454SAndrew Thompson } 224302ac6454SAndrew Thompson std->len = sizeof(ohci_confd); 224402ac6454SAndrew Thompson std->ptr = USB_ADD_BYTES(&ohci_confd, 0); 224502ac6454SAndrew Thompson break; 224602ac6454SAndrew Thompson 224702ac6454SAndrew Thompson case UDESC_STRING: 224802ac6454SAndrew Thompson switch (value & 0xff) { 224902ac6454SAndrew Thompson case 0: /* Language table */ 225002ac6454SAndrew Thompson ptr = "\001"; 225102ac6454SAndrew Thompson break; 225202ac6454SAndrew Thompson 225302ac6454SAndrew Thompson case 1: /* Vendor */ 225402ac6454SAndrew Thompson ptr = sc->sc_vendor; 225502ac6454SAndrew Thompson break; 225602ac6454SAndrew Thompson 225702ac6454SAndrew Thompson case 2: /* Product */ 225802ac6454SAndrew Thompson ptr = "OHCI root HUB"; 225902ac6454SAndrew Thompson break; 226002ac6454SAndrew Thompson 226102ac6454SAndrew Thompson default: 226202ac6454SAndrew Thompson ptr = ""; 226302ac6454SAndrew Thompson break; 226402ac6454SAndrew Thompson } 226502ac6454SAndrew Thompson 226602ac6454SAndrew Thompson std->len = usb2_make_str_desc 226702ac6454SAndrew Thompson (sc->sc_hub_desc.temp, 226802ac6454SAndrew Thompson sizeof(sc->sc_hub_desc.temp), 226902ac6454SAndrew Thompson ptr); 227002ac6454SAndrew Thompson break; 227102ac6454SAndrew Thompson 227202ac6454SAndrew Thompson default: 227302ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 227402ac6454SAndrew Thompson goto done; 227502ac6454SAndrew Thompson } 227602ac6454SAndrew Thompson break; 227702ac6454SAndrew Thompson case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 227802ac6454SAndrew Thompson std->len = 1; 227902ac6454SAndrew Thompson sc->sc_hub_desc.temp[0] = 0; 228002ac6454SAndrew Thompson break; 228102ac6454SAndrew Thompson case C(UR_GET_STATUS, UT_READ_DEVICE): 228202ac6454SAndrew Thompson std->len = 2; 228302ac6454SAndrew Thompson USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED); 228402ac6454SAndrew Thompson break; 228502ac6454SAndrew Thompson case C(UR_GET_STATUS, UT_READ_INTERFACE): 228602ac6454SAndrew Thompson case C(UR_GET_STATUS, UT_READ_ENDPOINT): 228702ac6454SAndrew Thompson std->len = 2; 228802ac6454SAndrew Thompson USETW(sc->sc_hub_desc.stat.wStatus, 0); 228902ac6454SAndrew Thompson break; 229002ac6454SAndrew Thompson case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 2291ab42e8b2SAndrew Thompson if (value >= OHCI_MAX_DEVICES) { 229202ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 229302ac6454SAndrew Thompson goto done; 229402ac6454SAndrew Thompson } 229502ac6454SAndrew Thompson sc->sc_addr = value; 229602ac6454SAndrew Thompson break; 229702ac6454SAndrew Thompson case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 229802ac6454SAndrew Thompson if ((value != 0) && (value != 1)) { 229902ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 230002ac6454SAndrew Thompson goto done; 230102ac6454SAndrew Thompson } 230202ac6454SAndrew Thompson sc->sc_conf = value; 230302ac6454SAndrew Thompson break; 230402ac6454SAndrew Thompson case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 230502ac6454SAndrew Thompson break; 230602ac6454SAndrew Thompson case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 230702ac6454SAndrew Thompson case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 230802ac6454SAndrew Thompson case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 230902ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 231002ac6454SAndrew Thompson goto done; 231102ac6454SAndrew Thompson case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 231202ac6454SAndrew Thompson break; 231302ac6454SAndrew Thompson case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 231402ac6454SAndrew Thompson break; 231502ac6454SAndrew Thompson /* Hub requests */ 231602ac6454SAndrew Thompson case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 231702ac6454SAndrew Thompson break; 231802ac6454SAndrew Thompson case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 231902ac6454SAndrew Thompson DPRINTFN(9, "UR_CLEAR_PORT_FEATURE " 232002ac6454SAndrew Thompson "port=%d feature=%d\n", 232102ac6454SAndrew Thompson index, value); 232202ac6454SAndrew Thompson if ((index < 1) || 232302ac6454SAndrew Thompson (index > sc->sc_noport)) { 232402ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 232502ac6454SAndrew Thompson goto done; 232602ac6454SAndrew Thompson } 232702ac6454SAndrew Thompson port = OHCI_RH_PORT_STATUS(index); 232802ac6454SAndrew Thompson switch (value) { 232902ac6454SAndrew Thompson case UHF_PORT_ENABLE: 233002ac6454SAndrew Thompson OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS); 233102ac6454SAndrew Thompson break; 233202ac6454SAndrew Thompson case UHF_PORT_SUSPEND: 233302ac6454SAndrew Thompson OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR); 233402ac6454SAndrew Thompson break; 233502ac6454SAndrew Thompson case UHF_PORT_POWER: 233602ac6454SAndrew Thompson /* Yes, writing to the LOW_SPEED bit clears power. */ 233702ac6454SAndrew Thompson OWRITE4(sc, port, UPS_LOW_SPEED); 233802ac6454SAndrew Thompson break; 233902ac6454SAndrew Thompson case UHF_C_PORT_CONNECTION: 234002ac6454SAndrew Thompson OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16); 234102ac6454SAndrew Thompson break; 234202ac6454SAndrew Thompson case UHF_C_PORT_ENABLE: 234302ac6454SAndrew Thompson OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16); 234402ac6454SAndrew Thompson break; 234502ac6454SAndrew Thompson case UHF_C_PORT_SUSPEND: 234602ac6454SAndrew Thompson OWRITE4(sc, port, UPS_C_SUSPEND << 16); 234702ac6454SAndrew Thompson break; 234802ac6454SAndrew Thompson case UHF_C_PORT_OVER_CURRENT: 234902ac6454SAndrew Thompson OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16); 235002ac6454SAndrew Thompson break; 235102ac6454SAndrew Thompson case UHF_C_PORT_RESET: 235202ac6454SAndrew Thompson OWRITE4(sc, port, UPS_C_PORT_RESET << 16); 235302ac6454SAndrew Thompson break; 235402ac6454SAndrew Thompson default: 235502ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 235602ac6454SAndrew Thompson goto done; 235702ac6454SAndrew Thompson } 235802ac6454SAndrew Thompson switch (value) { 235902ac6454SAndrew Thompson case UHF_C_PORT_CONNECTION: 236002ac6454SAndrew Thompson case UHF_C_PORT_ENABLE: 236102ac6454SAndrew Thompson case UHF_C_PORT_SUSPEND: 236202ac6454SAndrew Thompson case UHF_C_PORT_OVER_CURRENT: 236302ac6454SAndrew Thompson case UHF_C_PORT_RESET: 236402ac6454SAndrew Thompson /* enable RHSC interrupt if condition is cleared. */ 236502ac6454SAndrew Thompson if ((OREAD4(sc, port) >> 16) == 0) 236602ac6454SAndrew Thompson ohci_rhsc_enable(sc); 236702ac6454SAndrew Thompson break; 236802ac6454SAndrew Thompson default: 236902ac6454SAndrew Thompson break; 237002ac6454SAndrew Thompson } 237102ac6454SAndrew Thompson break; 237202ac6454SAndrew Thompson case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 237302ac6454SAndrew Thompson if ((value & 0xff) != 0) { 237402ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 237502ac6454SAndrew Thompson goto done; 237602ac6454SAndrew Thompson } 237702ac6454SAndrew Thompson v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A); 237802ac6454SAndrew Thompson 237902ac6454SAndrew Thompson sc->sc_hub_desc.hubd = ohci_hubd; 238002ac6454SAndrew Thompson sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport; 238102ac6454SAndrew Thompson USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, 238202ac6454SAndrew Thompson (v & OHCI_NPS ? UHD_PWR_NO_SWITCH : 238302ac6454SAndrew Thompson v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL) 238402ac6454SAndrew Thompson /* XXX overcurrent */ 238502ac6454SAndrew Thompson ); 238602ac6454SAndrew Thompson sc->sc_hub_desc.hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v); 238702ac6454SAndrew Thompson v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B); 238802ac6454SAndrew Thompson 238902ac6454SAndrew Thompson for (l = 0; l < sc->sc_noport; l++) { 239002ac6454SAndrew Thompson if (v & 1) { 239102ac6454SAndrew Thompson sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] |= (1 << (l % 8)); 239202ac6454SAndrew Thompson } 239302ac6454SAndrew Thompson v >>= 1; 239402ac6454SAndrew Thompson } 239502ac6454SAndrew Thompson sc->sc_hub_desc.hubd.bDescLength = 239602ac6454SAndrew Thompson 8 + ((sc->sc_noport + 7) / 8); 239702ac6454SAndrew Thompson std->len = sc->sc_hub_desc.hubd.bDescLength; 239802ac6454SAndrew Thompson break; 239902ac6454SAndrew Thompson 240002ac6454SAndrew Thompson case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 240102ac6454SAndrew Thompson std->len = 16; 240202ac6454SAndrew Thompson bzero(sc->sc_hub_desc.temp, 16); 240302ac6454SAndrew Thompson break; 240402ac6454SAndrew Thompson case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 240502ac6454SAndrew Thompson DPRINTFN(9, "get port status i=%d\n", 240602ac6454SAndrew Thompson index); 240702ac6454SAndrew Thompson if ((index < 1) || 240802ac6454SAndrew Thompson (index > sc->sc_noport)) { 240902ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 241002ac6454SAndrew Thompson goto done; 241102ac6454SAndrew Thompson } 241202ac6454SAndrew Thompson v = OREAD4(sc, OHCI_RH_PORT_STATUS(index)); 241302ac6454SAndrew Thompson DPRINTFN(9, "port status=0x%04x\n", v); 241402ac6454SAndrew Thompson USETW(sc->sc_hub_desc.ps.wPortStatus, v); 241502ac6454SAndrew Thompson USETW(sc->sc_hub_desc.ps.wPortChange, v >> 16); 241602ac6454SAndrew Thompson std->len = sizeof(sc->sc_hub_desc.ps); 241702ac6454SAndrew Thompson break; 241802ac6454SAndrew Thompson case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 241902ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 242002ac6454SAndrew Thompson goto done; 242102ac6454SAndrew Thompson case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 242202ac6454SAndrew Thompson break; 242302ac6454SAndrew Thompson case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 242402ac6454SAndrew Thompson if ((index < 1) || 242502ac6454SAndrew Thompson (index > sc->sc_noport)) { 242602ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 242702ac6454SAndrew Thompson goto done; 242802ac6454SAndrew Thompson } 242902ac6454SAndrew Thompson port = OHCI_RH_PORT_STATUS(index); 243002ac6454SAndrew Thompson switch (value) { 243102ac6454SAndrew Thompson case UHF_PORT_ENABLE: 243202ac6454SAndrew Thompson OWRITE4(sc, port, UPS_PORT_ENABLED); 243302ac6454SAndrew Thompson break; 243402ac6454SAndrew Thompson case UHF_PORT_SUSPEND: 243502ac6454SAndrew Thompson OWRITE4(sc, port, UPS_SUSPEND); 243602ac6454SAndrew Thompson break; 243702ac6454SAndrew Thompson case UHF_PORT_RESET: 243802ac6454SAndrew Thompson DPRINTFN(6, "reset port %d\n", index); 243902ac6454SAndrew Thompson OWRITE4(sc, port, UPS_RESET); 244002ac6454SAndrew Thompson for (v = 0;; v++) { 244102ac6454SAndrew Thompson if (v < 12) { 244202ac6454SAndrew Thompson usb2_pause_mtx(&sc->sc_bus.bus_mtx, 244302ac6454SAndrew Thompson USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY)); 244402ac6454SAndrew Thompson 244502ac6454SAndrew Thompson if ((OREAD4(sc, port) & UPS_RESET) == 0) { 244602ac6454SAndrew Thompson break; 244702ac6454SAndrew Thompson } 244802ac6454SAndrew Thompson } else { 244902ac6454SAndrew Thompson std->err = USB_ERR_TIMEOUT; 245002ac6454SAndrew Thompson goto done; 245102ac6454SAndrew Thompson } 245202ac6454SAndrew Thompson } 245302ac6454SAndrew Thompson DPRINTFN(9, "ohci port %d reset, status = 0x%04x\n", 245402ac6454SAndrew Thompson index, OREAD4(sc, port)); 245502ac6454SAndrew Thompson break; 245602ac6454SAndrew Thompson case UHF_PORT_POWER: 245702ac6454SAndrew Thompson DPRINTFN(3, "set port power %d\n", index); 245802ac6454SAndrew Thompson OWRITE4(sc, port, UPS_PORT_POWER); 245902ac6454SAndrew Thompson break; 246002ac6454SAndrew Thompson default: 246102ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 246202ac6454SAndrew Thompson goto done; 246302ac6454SAndrew Thompson } 246402ac6454SAndrew Thompson break; 246502ac6454SAndrew Thompson default: 246602ac6454SAndrew Thompson std->err = USB_ERR_IOERROR; 246702ac6454SAndrew Thompson goto done; 246802ac6454SAndrew Thompson } 246902ac6454SAndrew Thompson done: 247002ac6454SAndrew Thompson return; 247102ac6454SAndrew Thompson } 247202ac6454SAndrew Thompson 247302ac6454SAndrew Thompson static void 247402ac6454SAndrew Thompson ohci_root_ctrl_poll(struct ohci_softc *sc) 247502ac6454SAndrew Thompson { 247602ac6454SAndrew Thompson usb2_sw_transfer(&sc->sc_root_ctrl, 247702ac6454SAndrew Thompson &ohci_root_ctrl_done); 247802ac6454SAndrew Thompson } 247902ac6454SAndrew Thompson 248002ac6454SAndrew Thompson struct usb2_pipe_methods ohci_root_ctrl_methods = 248102ac6454SAndrew Thompson { 248202ac6454SAndrew Thompson .open = ohci_root_ctrl_open, 248302ac6454SAndrew Thompson .close = ohci_root_ctrl_close, 248402ac6454SAndrew Thompson .enter = ohci_root_ctrl_enter, 248502ac6454SAndrew Thompson .start = ohci_root_ctrl_start, 248602ac6454SAndrew Thompson .enter_is_cancelable = 1, 248702ac6454SAndrew Thompson .start_is_cancelable = 0, 248802ac6454SAndrew Thompson }; 248902ac6454SAndrew Thompson 249002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 249102ac6454SAndrew Thompson * ohci root interrupt support 249202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 249302ac6454SAndrew Thompson static void 249402ac6454SAndrew Thompson ohci_root_intr_open(struct usb2_xfer *xfer) 249502ac6454SAndrew Thompson { 249602ac6454SAndrew Thompson return; 249702ac6454SAndrew Thompson } 249802ac6454SAndrew Thompson 249902ac6454SAndrew Thompson static void 250002ac6454SAndrew Thompson ohci_root_intr_close(struct usb2_xfer *xfer) 250102ac6454SAndrew Thompson { 250202ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 250302ac6454SAndrew Thompson 250402ac6454SAndrew Thompson if (sc->sc_root_intr.xfer == xfer) { 250502ac6454SAndrew Thompson sc->sc_root_intr.xfer = NULL; 250602ac6454SAndrew Thompson } 250702ac6454SAndrew Thompson ohci_device_done(xfer, USB_ERR_CANCELLED); 250802ac6454SAndrew Thompson } 250902ac6454SAndrew Thompson 251002ac6454SAndrew Thompson static void 251102ac6454SAndrew Thompson ohci_root_intr_enter(struct usb2_xfer *xfer) 251202ac6454SAndrew Thompson { 251302ac6454SAndrew Thompson return; 251402ac6454SAndrew Thompson } 251502ac6454SAndrew Thompson 251602ac6454SAndrew Thompson static void 251702ac6454SAndrew Thompson ohci_root_intr_start(struct usb2_xfer *xfer) 251802ac6454SAndrew Thompson { 251902ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus); 252002ac6454SAndrew Thompson 252102ac6454SAndrew Thompson sc->sc_root_intr.xfer = xfer; 252202ac6454SAndrew Thompson } 252302ac6454SAndrew Thompson 252402ac6454SAndrew Thompson struct usb2_pipe_methods ohci_root_intr_methods = 252502ac6454SAndrew Thompson { 252602ac6454SAndrew Thompson .open = ohci_root_intr_open, 252702ac6454SAndrew Thompson .close = ohci_root_intr_close, 252802ac6454SAndrew Thompson .enter = ohci_root_intr_enter, 252902ac6454SAndrew Thompson .start = ohci_root_intr_start, 253002ac6454SAndrew Thompson .enter_is_cancelable = 1, 253102ac6454SAndrew Thompson .start_is_cancelable = 1, 253202ac6454SAndrew Thompson }; 253302ac6454SAndrew Thompson 253402ac6454SAndrew Thompson static void 253502ac6454SAndrew Thompson ohci_xfer_setup(struct usb2_setup_params *parm) 253602ac6454SAndrew Thompson { 253702ac6454SAndrew Thompson struct usb2_page_search page_info; 253802ac6454SAndrew Thompson struct usb2_page_cache *pc; 253902ac6454SAndrew Thompson ohci_softc_t *sc; 254002ac6454SAndrew Thompson struct usb2_xfer *xfer; 254102ac6454SAndrew Thompson void *last_obj; 254202ac6454SAndrew Thompson uint32_t ntd; 254302ac6454SAndrew Thompson uint32_t nitd; 254402ac6454SAndrew Thompson uint32_t nqh; 254502ac6454SAndrew Thompson uint32_t n; 254602ac6454SAndrew Thompson 254702ac6454SAndrew Thompson sc = OHCI_BUS2SC(parm->udev->bus); 254802ac6454SAndrew Thompson xfer = parm->curr_xfer; 254902ac6454SAndrew Thompson 255002ac6454SAndrew Thompson parm->hc_max_packet_size = 0x500; 255102ac6454SAndrew Thompson parm->hc_max_packet_count = 1; 255202ac6454SAndrew Thompson parm->hc_max_frame_size = OHCI_PAGE_SIZE; 255302ac6454SAndrew Thompson 255402ac6454SAndrew Thompson /* 255502ac6454SAndrew Thompson * calculate ntd and nqh 255602ac6454SAndrew Thompson */ 255702ac6454SAndrew Thompson if (parm->methods == &ohci_device_ctrl_methods) { 255802ac6454SAndrew Thompson xfer->flags_int.bdma_enable = 1; 255902ac6454SAndrew Thompson 256002ac6454SAndrew Thompson usb2_transfer_setup_sub(parm); 256102ac6454SAndrew Thompson 256202ac6454SAndrew Thompson nitd = 0; 256302ac6454SAndrew Thompson ntd = ((2 * xfer->nframes) + 1 /* STATUS */ 2564578d0effSAndrew Thompson + (xfer->max_data_length / xfer->max_hc_frame_size)); 256502ac6454SAndrew Thompson nqh = 1; 256602ac6454SAndrew Thompson 256702ac6454SAndrew Thompson } else if (parm->methods == &ohci_device_bulk_methods) { 256802ac6454SAndrew Thompson xfer->flags_int.bdma_enable = 1; 256902ac6454SAndrew Thompson 257002ac6454SAndrew Thompson usb2_transfer_setup_sub(parm); 257102ac6454SAndrew Thompson 257202ac6454SAndrew Thompson nitd = 0; 257302ac6454SAndrew Thompson ntd = ((2 * xfer->nframes) 2574578d0effSAndrew Thompson + (xfer->max_data_length / xfer->max_hc_frame_size)); 257502ac6454SAndrew Thompson nqh = 1; 257602ac6454SAndrew Thompson 257702ac6454SAndrew Thompson } else if (parm->methods == &ohci_device_intr_methods) { 257802ac6454SAndrew Thompson xfer->flags_int.bdma_enable = 1; 257902ac6454SAndrew Thompson 258002ac6454SAndrew Thompson usb2_transfer_setup_sub(parm); 258102ac6454SAndrew Thompson 258202ac6454SAndrew Thompson nitd = 0; 258302ac6454SAndrew Thompson ntd = ((2 * xfer->nframes) 2584578d0effSAndrew Thompson + (xfer->max_data_length / xfer->max_hc_frame_size)); 258502ac6454SAndrew Thompson nqh = 1; 258602ac6454SAndrew Thompson 258702ac6454SAndrew Thompson } else if (parm->methods == &ohci_device_isoc_methods) { 258802ac6454SAndrew Thompson xfer->flags_int.bdma_enable = 1; 258902ac6454SAndrew Thompson 259002ac6454SAndrew Thompson usb2_transfer_setup_sub(parm); 259102ac6454SAndrew Thompson 259202ac6454SAndrew Thompson nitd = ((xfer->max_data_length / OHCI_PAGE_SIZE) + 259302ac6454SAndrew Thompson ((xfer->nframes + OHCI_ITD_NOFFSET - 1) / OHCI_ITD_NOFFSET) + 259402ac6454SAndrew Thompson 1 /* EXTRA */ ); 259502ac6454SAndrew Thompson ntd = 0; 259602ac6454SAndrew Thompson nqh = 1; 259702ac6454SAndrew Thompson 259802ac6454SAndrew Thompson } else { 259902ac6454SAndrew Thompson 260002ac6454SAndrew Thompson usb2_transfer_setup_sub(parm); 260102ac6454SAndrew Thompson 260202ac6454SAndrew Thompson nitd = 0; 260302ac6454SAndrew Thompson ntd = 0; 260402ac6454SAndrew Thompson nqh = 0; 260502ac6454SAndrew Thompson } 260602ac6454SAndrew Thompson 260702ac6454SAndrew Thompson alloc_dma_set: 260802ac6454SAndrew Thompson 260902ac6454SAndrew Thompson if (parm->err) { 261002ac6454SAndrew Thompson return; 261102ac6454SAndrew Thompson } 261202ac6454SAndrew Thompson last_obj = NULL; 261302ac6454SAndrew Thompson 261402ac6454SAndrew Thompson if (usb2_transfer_setup_sub_malloc( 261502ac6454SAndrew Thompson parm, &pc, sizeof(ohci_td_t), 261602ac6454SAndrew Thompson OHCI_TD_ALIGN, ntd)) { 261702ac6454SAndrew Thompson parm->err = USB_ERR_NOMEM; 261802ac6454SAndrew Thompson return; 261902ac6454SAndrew Thompson } 262002ac6454SAndrew Thompson if (parm->buf) { 262102ac6454SAndrew Thompson for (n = 0; n != ntd; n++) { 262202ac6454SAndrew Thompson ohci_td_t *td; 262302ac6454SAndrew Thompson 262402ac6454SAndrew Thompson usb2_get_page(pc + n, 0, &page_info); 262502ac6454SAndrew Thompson 262602ac6454SAndrew Thompson td = page_info.buffer; 262702ac6454SAndrew Thompson 262802ac6454SAndrew Thompson /* init TD */ 262902ac6454SAndrew Thompson td->td_self = htole32(page_info.physaddr); 263002ac6454SAndrew Thompson td->obj_next = last_obj; 263102ac6454SAndrew Thompson td->page_cache = pc + n; 263202ac6454SAndrew Thompson 263302ac6454SAndrew Thompson last_obj = td; 263402ac6454SAndrew Thompson 263502ac6454SAndrew Thompson usb2_pc_cpu_flush(pc + n); 263602ac6454SAndrew Thompson } 263702ac6454SAndrew Thompson } 263802ac6454SAndrew Thompson if (usb2_transfer_setup_sub_malloc( 263902ac6454SAndrew Thompson parm, &pc, sizeof(ohci_itd_t), 264002ac6454SAndrew Thompson OHCI_ITD_ALIGN, nitd)) { 264102ac6454SAndrew Thompson parm->err = USB_ERR_NOMEM; 264202ac6454SAndrew Thompson return; 264302ac6454SAndrew Thompson } 264402ac6454SAndrew Thompson if (parm->buf) { 264502ac6454SAndrew Thompson for (n = 0; n != nitd; n++) { 264602ac6454SAndrew Thompson ohci_itd_t *itd; 264702ac6454SAndrew Thompson 264802ac6454SAndrew Thompson usb2_get_page(pc + n, 0, &page_info); 264902ac6454SAndrew Thompson 265002ac6454SAndrew Thompson itd = page_info.buffer; 265102ac6454SAndrew Thompson 265202ac6454SAndrew Thompson /* init TD */ 265302ac6454SAndrew Thompson itd->itd_self = htole32(page_info.physaddr); 265402ac6454SAndrew Thompson itd->obj_next = last_obj; 265502ac6454SAndrew Thompson itd->page_cache = pc + n; 265602ac6454SAndrew Thompson 265702ac6454SAndrew Thompson last_obj = itd; 265802ac6454SAndrew Thompson 265902ac6454SAndrew Thompson usb2_pc_cpu_flush(pc + n); 266002ac6454SAndrew Thompson } 266102ac6454SAndrew Thompson } 266202ac6454SAndrew Thompson xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj; 266302ac6454SAndrew Thompson 266402ac6454SAndrew Thompson last_obj = NULL; 266502ac6454SAndrew Thompson 266602ac6454SAndrew Thompson if (usb2_transfer_setup_sub_malloc( 266702ac6454SAndrew Thompson parm, &pc, sizeof(ohci_ed_t), 266802ac6454SAndrew Thompson OHCI_ED_ALIGN, nqh)) { 266902ac6454SAndrew Thompson parm->err = USB_ERR_NOMEM; 267002ac6454SAndrew Thompson return; 267102ac6454SAndrew Thompson } 267202ac6454SAndrew Thompson if (parm->buf) { 267302ac6454SAndrew Thompson for (n = 0; n != nqh; n++) { 267402ac6454SAndrew Thompson ohci_ed_t *ed; 267502ac6454SAndrew Thompson 267602ac6454SAndrew Thompson usb2_get_page(pc + n, 0, &page_info); 267702ac6454SAndrew Thompson 267802ac6454SAndrew Thompson ed = page_info.buffer; 267902ac6454SAndrew Thompson 268002ac6454SAndrew Thompson /* init QH */ 268102ac6454SAndrew Thompson ed->ed_self = htole32(page_info.physaddr); 268202ac6454SAndrew Thompson ed->obj_next = last_obj; 268302ac6454SAndrew Thompson ed->page_cache = pc + n; 268402ac6454SAndrew Thompson 268502ac6454SAndrew Thompson last_obj = ed; 268602ac6454SAndrew Thompson 268702ac6454SAndrew Thompson usb2_pc_cpu_flush(pc + n); 268802ac6454SAndrew Thompson } 268902ac6454SAndrew Thompson } 269002ac6454SAndrew Thompson xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj; 269102ac6454SAndrew Thompson 269202ac6454SAndrew Thompson if (!xfer->flags_int.curr_dma_set) { 269302ac6454SAndrew Thompson xfer->flags_int.curr_dma_set = 1; 269402ac6454SAndrew Thompson goto alloc_dma_set; 269502ac6454SAndrew Thompson } 269602ac6454SAndrew Thompson } 269702ac6454SAndrew Thompson 269802ac6454SAndrew Thompson static void 269902ac6454SAndrew Thompson ohci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc, 270002ac6454SAndrew Thompson struct usb2_pipe *pipe) 270102ac6454SAndrew Thompson { 270202ac6454SAndrew Thompson ohci_softc_t *sc = OHCI_BUS2SC(udev->bus); 270302ac6454SAndrew Thompson 270402ac6454SAndrew Thompson DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n", 270502ac6454SAndrew Thompson pipe, udev->address, 270602ac6454SAndrew Thompson edesc->bEndpointAddress, udev->flags.usb2_mode, 270702ac6454SAndrew Thompson sc->sc_addr); 270802ac6454SAndrew Thompson 270902ac6454SAndrew Thompson if (udev->flags.usb2_mode != USB_MODE_HOST) { 271002ac6454SAndrew Thompson /* not supported */ 271102ac6454SAndrew Thompson return; 271202ac6454SAndrew Thompson } 271302ac6454SAndrew Thompson if (udev->device_index == sc->sc_addr) { 271402ac6454SAndrew Thompson switch (edesc->bEndpointAddress) { 271502ac6454SAndrew Thompson case USB_CONTROL_ENDPOINT: 271602ac6454SAndrew Thompson pipe->methods = &ohci_root_ctrl_methods; 271702ac6454SAndrew Thompson break; 271802ac6454SAndrew Thompson case UE_DIR_IN | OHCI_INTR_ENDPT: 271902ac6454SAndrew Thompson pipe->methods = &ohci_root_intr_methods; 272002ac6454SAndrew Thompson break; 272102ac6454SAndrew Thompson default: 272202ac6454SAndrew Thompson /* do nothing */ 272302ac6454SAndrew Thompson break; 272402ac6454SAndrew Thompson } 272502ac6454SAndrew Thompson } else { 272602ac6454SAndrew Thompson switch (edesc->bmAttributes & UE_XFERTYPE) { 272702ac6454SAndrew Thompson case UE_CONTROL: 272802ac6454SAndrew Thompson pipe->methods = &ohci_device_ctrl_methods; 272902ac6454SAndrew Thompson break; 273002ac6454SAndrew Thompson case UE_INTERRUPT: 273102ac6454SAndrew Thompson pipe->methods = &ohci_device_intr_methods; 273202ac6454SAndrew Thompson break; 273302ac6454SAndrew Thompson case UE_ISOCHRONOUS: 273402ac6454SAndrew Thompson if (udev->speed == USB_SPEED_FULL) { 273502ac6454SAndrew Thompson pipe->methods = &ohci_device_isoc_methods; 273602ac6454SAndrew Thompson } 273702ac6454SAndrew Thompson break; 273802ac6454SAndrew Thompson case UE_BULK: 273902ac6454SAndrew Thompson if (udev->speed != USB_SPEED_LOW) { 274002ac6454SAndrew Thompson pipe->methods = &ohci_device_bulk_methods; 274102ac6454SAndrew Thompson } 274202ac6454SAndrew Thompson break; 274302ac6454SAndrew Thompson default: 274402ac6454SAndrew Thompson /* do nothing */ 274502ac6454SAndrew Thompson break; 274602ac6454SAndrew Thompson } 274702ac6454SAndrew Thompson } 274802ac6454SAndrew Thompson } 274902ac6454SAndrew Thompson 275002ac6454SAndrew Thompson static void 275102ac6454SAndrew Thompson ohci_xfer_unsetup(struct usb2_xfer *xfer) 275202ac6454SAndrew Thompson { 275302ac6454SAndrew Thompson return; 275402ac6454SAndrew Thompson } 275502ac6454SAndrew Thompson 275602ac6454SAndrew Thompson static void 275702ac6454SAndrew Thompson ohci_get_dma_delay(struct usb2_bus *bus, uint32_t *pus) 275802ac6454SAndrew Thompson { 275902ac6454SAndrew Thompson /* 276002ac6454SAndrew Thompson * Wait until hardware has finished any possible use of the 276102ac6454SAndrew Thompson * transfer descriptor(s) and QH 276202ac6454SAndrew Thompson */ 276302ac6454SAndrew Thompson *pus = (1125); /* microseconds */ 276402ac6454SAndrew Thompson } 276502ac6454SAndrew Thompson 276602ac6454SAndrew Thompson static void 276702ac6454SAndrew Thompson ohci_device_resume(struct usb2_device *udev) 276802ac6454SAndrew Thompson { 276902ac6454SAndrew Thompson struct ohci_softc *sc = OHCI_BUS2SC(udev->bus); 277002ac6454SAndrew Thompson struct usb2_xfer *xfer; 277102ac6454SAndrew Thompson struct usb2_pipe_methods *methods; 277202ac6454SAndrew Thompson ohci_ed_t *ed; 277302ac6454SAndrew Thompson 277402ac6454SAndrew Thompson DPRINTF("\n"); 277502ac6454SAndrew Thompson 277602ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 277702ac6454SAndrew Thompson 277802ac6454SAndrew Thompson TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 277902ac6454SAndrew Thompson 278002ac6454SAndrew Thompson if (xfer->xroot->udev == udev) { 278102ac6454SAndrew Thompson 278202ac6454SAndrew Thompson methods = xfer->pipe->methods; 278302ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 278402ac6454SAndrew Thompson 278502ac6454SAndrew Thompson if (methods == &ohci_device_bulk_methods) { 278602ac6454SAndrew Thompson OHCI_APPEND_QH(ed, sc->sc_bulk_p_last); 278702ac6454SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF); 278802ac6454SAndrew Thompson } 278902ac6454SAndrew Thompson if (methods == &ohci_device_ctrl_methods) { 279002ac6454SAndrew Thompson OHCI_APPEND_QH(ed, sc->sc_ctrl_p_last); 279102ac6454SAndrew Thompson OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF); 279202ac6454SAndrew Thompson } 279302ac6454SAndrew Thompson if (methods == &ohci_device_intr_methods) { 279402ac6454SAndrew Thompson OHCI_APPEND_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]); 279502ac6454SAndrew Thompson } 279602ac6454SAndrew Thompson } 279702ac6454SAndrew Thompson } 279802ac6454SAndrew Thompson 279902ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 280002ac6454SAndrew Thompson 280102ac6454SAndrew Thompson return; 280202ac6454SAndrew Thompson } 280302ac6454SAndrew Thompson 280402ac6454SAndrew Thompson static void 280502ac6454SAndrew Thompson ohci_device_suspend(struct usb2_device *udev) 280602ac6454SAndrew Thompson { 280702ac6454SAndrew Thompson struct ohci_softc *sc = OHCI_BUS2SC(udev->bus); 280802ac6454SAndrew Thompson struct usb2_xfer *xfer; 280902ac6454SAndrew Thompson struct usb2_pipe_methods *methods; 281002ac6454SAndrew Thompson ohci_ed_t *ed; 281102ac6454SAndrew Thompson 281202ac6454SAndrew Thompson DPRINTF("\n"); 281302ac6454SAndrew Thompson 281402ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 281502ac6454SAndrew Thompson 281602ac6454SAndrew Thompson TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 281702ac6454SAndrew Thompson 281802ac6454SAndrew Thompson if (xfer->xroot->udev == udev) { 281902ac6454SAndrew Thompson 282002ac6454SAndrew Thompson methods = xfer->pipe->methods; 282102ac6454SAndrew Thompson ed = xfer->qh_start[xfer->flags_int.curr_dma_set]; 282202ac6454SAndrew Thompson 282302ac6454SAndrew Thompson if (methods == &ohci_device_bulk_methods) { 282402ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_bulk_p_last); 282502ac6454SAndrew Thompson } 282602ac6454SAndrew Thompson if (methods == &ohci_device_ctrl_methods) { 282702ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_ctrl_p_last); 282802ac6454SAndrew Thompson } 282902ac6454SAndrew Thompson if (methods == &ohci_device_intr_methods) { 283002ac6454SAndrew Thompson OHCI_REMOVE_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]); 283102ac6454SAndrew Thompson } 283202ac6454SAndrew Thompson } 283302ac6454SAndrew Thompson } 283402ac6454SAndrew Thompson 283502ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 283602ac6454SAndrew Thompson 283702ac6454SAndrew Thompson return; 283802ac6454SAndrew Thompson } 283902ac6454SAndrew Thompson 284002ac6454SAndrew Thompson static void 284102ac6454SAndrew Thompson ohci_set_hw_power(struct usb2_bus *bus) 284202ac6454SAndrew Thompson { 284302ac6454SAndrew Thompson struct ohci_softc *sc = OHCI_BUS2SC(bus); 284402ac6454SAndrew Thompson uint32_t temp; 284502ac6454SAndrew Thompson uint32_t flags; 284602ac6454SAndrew Thompson 284702ac6454SAndrew Thompson DPRINTF("\n"); 284802ac6454SAndrew Thompson 284902ac6454SAndrew Thompson USB_BUS_LOCK(bus); 285002ac6454SAndrew Thompson 285102ac6454SAndrew Thompson flags = bus->hw_power_state; 285202ac6454SAndrew Thompson 285302ac6454SAndrew Thompson temp = OREAD4(sc, OHCI_CONTROL); 285402ac6454SAndrew Thompson temp &= ~(OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE); 285502ac6454SAndrew Thompson 285602ac6454SAndrew Thompson if (flags & USB_HW_POWER_CONTROL) 285702ac6454SAndrew Thompson temp |= OHCI_CLE; 285802ac6454SAndrew Thompson 285902ac6454SAndrew Thompson if (flags & USB_HW_POWER_BULK) 286002ac6454SAndrew Thompson temp |= OHCI_BLE; 286102ac6454SAndrew Thompson 286202ac6454SAndrew Thompson if (flags & USB_HW_POWER_INTERRUPT) 286302ac6454SAndrew Thompson temp |= OHCI_PLE; 286402ac6454SAndrew Thompson 286502ac6454SAndrew Thompson if (flags & USB_HW_POWER_ISOC) 286602ac6454SAndrew Thompson temp |= OHCI_IE | OHCI_PLE; 286702ac6454SAndrew Thompson 286802ac6454SAndrew Thompson OWRITE4(sc, OHCI_CONTROL, temp); 286902ac6454SAndrew Thompson 287002ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 287102ac6454SAndrew Thompson 287202ac6454SAndrew Thompson return; 287302ac6454SAndrew Thompson } 287402ac6454SAndrew Thompson 287502ac6454SAndrew Thompson struct usb2_bus_methods ohci_bus_methods = 287602ac6454SAndrew Thompson { 287702ac6454SAndrew Thompson .pipe_init = ohci_pipe_init, 287802ac6454SAndrew Thompson .xfer_setup = ohci_xfer_setup, 287902ac6454SAndrew Thompson .xfer_unsetup = ohci_xfer_unsetup, 288002ac6454SAndrew Thompson .get_dma_delay = ohci_get_dma_delay, 288102ac6454SAndrew Thompson .device_resume = ohci_device_resume, 288202ac6454SAndrew Thompson .device_suspend = ohci_device_suspend, 288302ac6454SAndrew Thompson .set_hw_power = ohci_set_hw_power, 288402ac6454SAndrew Thompson .roothub_exec = ohci_root_ctrl_task, 288502ac6454SAndrew Thompson }; 2886