atmegadci.c revision 190581
150724Scg#include <sys/cdefs.h> 250724Scg__FBSDID("$FreeBSD: head/sys/dev/usb/controller/atmegadci.c 190581 2009-03-30 22:18:38Z mav $"); 350724Scg 450724Scg/*- 550724Scg * Copyright (c) 2009 Hans Petter Selasky. All rights reserved. 650724Scg * 750724Scg * Redistribution and use in source and binary forms, with or without 850724Scg * modification, are permitted provided that the following conditions 950724Scg * are met: 1050724Scg * 1. Redistributions of source code must retain the above copyright 1150724Scg * notice, this list of conditions and the following disclaimer. 1250724Scg * 2. Redistributions in binary form must reproduce the above copyright 1350724Scg * notice, this list of conditions and the following disclaimer in the 1450724Scg * documentation and/or other materials provided with the distribution. 1550724Scg * 1650724Scg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1750724Scg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1850724Scg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1950724Scg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2050724Scg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2150724Scg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2250724Scg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2350724Scg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2450724Scg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2550724Scg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2650724Scg * SUCH DAMAGE. 2753465Scg */ 2850724Scg 2970134Scg/* 3070134Scg * This file contains the driver for the ATMEGA series USB OTG 3182180Scg * Controller. This driver currently only supports the DCI mode of the 3282180Scg * USB hardware. 3370134Scg */ 3470134Scg 3566308Scg/* 3665645Scg * NOTE: When the chip detects BUS-reset it will also reset the 3750724Scg * endpoints, Function-address and more. 3864881Scg */ 3964881Scg 4070134Scg#include <dev/usb/usb.h> 4164881Scg#include <dev/usb/usb_mfunc.h> 4264881Scg#include <dev/usb/usb_error.h> 4364881Scg 4464881Scg#define USB_DEBUG_VAR atmegadci_debug 4564881Scg 4664881Scg#include <dev/usb/usb_core.h> 4750724Scg#include <dev/usb/usb_debug.h> 4850724Scg#include <dev/usb/usb_busdma.h> 4964881Scg#include <dev/usb/usb_process.h> 5064881Scg#include <dev/usb/usb_sw_transfer.h> 5164881Scg#include <dev/usb/usb_transfer.h> 5289834Scg#include <dev/usb/usb_device.h> 5389834Scg#include <dev/usb/usb_hub.h> 5470134Scg#include <dev/usb/usb_util.h> 5564881Scg 5664881Scg#include <dev/usb/usb_controller.h> 5764881Scg#include <dev/usb/usb_bus.h> 5864881Scg#include <dev/usb/controller/atmegadci.h> 5989834Scg 6089834Scg#define ATMEGA_BUS2SC(bus) \ 6164881Scg ((struct atmegadci_softc *)(((uint8_t *)(bus)) - \ 62111909Sorion ((uint8_t *)&(((struct atmegadci_softc *)0)->sc_bus)))) 6389834Scg 6497274Sbde#define ATMEGA_PC2SC(pc) \ 6597274Sbde ATMEGA_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus) 6689834Scg 6789834Scg#if USB_DEBUG 6889834Scgstatic int atmegadci_debug = 0; 6970134Scg 7064881ScgSYSCTL_NODE(_hw_usb2, OID_AUTO, atmegadci, CTLFLAG_RW, 0, "USB ATMEGA DCI"); 7164881ScgSYSCTL_INT(_hw_usb2_atmegadci, OID_AUTO, debug, CTLFLAG_RW, 7264881Scg &atmegadci_debug, 0, "ATMEGA DCI debug level"); 7364881Scg#endif 7489834Scg 7589834Scg#define ATMEGA_INTR_ENDPT 1 7689834Scg 7789834Scg/* prototypes */ 7864881Scg 7964881Scgstruct usb2_bus_methods atmegadci_bus_methods; 8064881Scgstruct usb2_pipe_methods atmegadci_device_bulk_methods; 8189834Scgstruct usb2_pipe_methods atmegadci_device_ctrl_methods; 8289834Scgstruct usb2_pipe_methods atmegadci_device_intr_methods; 8389834Scgstruct usb2_pipe_methods atmegadci_device_isoc_fs_methods; 8464881Scgstruct usb2_pipe_methods atmegadci_root_ctrl_methods; 8570134Scgstruct usb2_pipe_methods atmegadci_root_intr_methods; 8675355Scg 87111909Sorionstatic atmegadci_cmd_t atmegadci_setup_rx; 8889834Scgstatic atmegadci_cmd_t atmegadci_data_rx; 8989834Scgstatic atmegadci_cmd_t atmegadci_data_tx; 9089834Scgstatic atmegadci_cmd_t atmegadci_data_tx_sync; 9189834Scgstatic void atmegadci_device_done(struct usb2_xfer *, usb2_error_t); 9289834Scgstatic void atmegadci_do_poll(struct usb2_bus *); 9370134Scgstatic void atmegadci_root_ctrl_poll(struct atmegadci_softc *); 9470134Scgstatic void atmegadci_standard_done(struct usb2_xfer *); 9564881Scg 9664881Scgstatic usb2_sw_transfer_func_t atmegadci_root_intr_done; 9764881Scgstatic usb2_sw_transfer_func_t atmegadci_root_ctrl_done; 9864881Scg 9964881Scg/* 10064881Scg * Here is a list of what the chip supports: 10164881Scg */ 10289834Scgstatic const struct usb2_hw_ep_profile 10364881Scg atmegadci_ep_profile[2] = { 10464881Scg 10574363Scg [0] = { 10674363Scg .max_in_frame_size = 64, 10774363Scg .max_out_frame_size = 64, 10874363Scg .is_simplex = 1, 10974363Scg .support_control = 1, 11074363Scg }, 11174363Scg [1] = { 11274363Scg .max_in_frame_size = 64, 11374363Scg .max_out_frame_size = 64, 11474363Scg .is_simplex = 1, 11574363Scg .support_multi_buffer = 1, 11674363Scg .support_bulk = 1, 11774363Scg .support_interrupt = 1, 11850724Scg .support_isochronous = 1, 11964881Scg .support_in = 1, 12050724Scg .support_out = 1, 12166308Scg }, 12266308Scg}; 12366308Scg 12466308Scgstatic void 12550724Scgatmegadci_get_hw_ep_profile(struct usb2_device *udev, 12650724Scg const struct usb2_hw_ep_profile **ppf, uint8_t ep_addr) 12770134Scg{ 12874763Scg if (ep_addr == 0) 12950724Scg *ppf = atmegadci_ep_profile; 13070134Scg else if (ep_addr < ATMEGA_EP_MAX) 13170134Scg *ppf = atmegadci_ep_profile + 1; 13270134Scg else 13364881Scg *ppf = NULL; 13474763Scg} 13570134Scg 13670134Scgstatic void 13774763Scgatmegadci_clocks_on(struct atmegadci_softc *sc) 13870134Scg{ 13970134Scg if (sc->sc_flags.clocks_off && 140111909Sorion sc->sc_flags.port_powered) { 14189834Scg 14289834Scg DPRINTFN(5, "\n"); 14389834Scg 14470134Scg /* turn on clocks */ 14589834Scg (sc->sc_clocks_on) (&sc->sc_bus); 14689834Scg 14789834Scg ATMEGA_WRITE_1(sc, ATMEGA_USBCON, 14889834Scg ATMEGA_USBCON_USBE | 14989834Scg ATMEGA_USBCON_OTGPADE | 15089834Scg ATMEGA_USBCON_VBUSTE); 15189834Scg 15270134Scg sc->sc_flags.clocks_off = 0; 15389834Scg 15470134Scg /* enable transceiver ? */ 15570134Scg } 15670134Scg} 15770134Scg 15870134Scgstatic void 15964881Scgatmegadci_clocks_off(struct atmegadci_softc *sc) 16089834Scg{ 16170134Scg if (!sc->sc_flags.clocks_off) { 16270134Scg 16375319Scg DPRINTFN(5, "\n"); 16470134Scg 16589834Scg /* disable Transceiver ? */ 16670134Scg 16789834Scg ATMEGA_WRITE_1(sc, ATMEGA_USBCON, 16889834Scg ATMEGA_USBCON_USBE | 16989834Scg ATMEGA_USBCON_OTGPADE | 17050724Scg ATMEGA_USBCON_FRZCLK | 17150724Scg ATMEGA_USBCON_VBUSTE); 17270134Scg 17370134Scg /* turn clocks off */ 17450724Scg (sc->sc_clocks_off) (&sc->sc_bus); 17564881Scg 17650724Scg sc->sc_flags.clocks_off = 1; 17764881Scg } 17870134Scg} 17970134Scg 18070134Scgstatic void 18170134Scgatmegadci_pull_up(struct atmegadci_softc *sc) 18264881Scg{ 18364881Scg /* pullup D+, if possible */ 18450724Scg 18550724Scg if (!sc->sc_flags.d_pulled_up && 18650724Scg sc->sc_flags.port_powered) { 18774763Scg sc->sc_flags.d_pulled_up = 1; 18866308Scg ATMEGA_WRITE_1(sc, ATMEGA_UDCON, 0); 18974763Scg } 19070134Scg} 19170134Scg 19270134Scgstatic void 19389834Scgatmegadci_pull_down(struct atmegadci_softc *sc) 19466308Scg{ 19570134Scg /* pulldown D+, if possible */ 19666308Scg 19766308Scg if (sc->sc_flags.d_pulled_up) { 19866308Scg sc->sc_flags.d_pulled_up = 0; 19966308Scg ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH); 20066308Scg } 20166308Scg} 20266308Scg 20366308Scgstatic void 20466308Scgatmegadci_wakeup_peer(struct usb2_xfer *xfer) 20566308Scg{ 20666308Scg struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 20766308Scg uint8_t temp; 20874763Scg 20950724Scg if (!sc->sc_flags.status_suspend) { 21074763Scg return; 21150724Scg } 21270134Scg 21364881Scg temp = ATMEGA_READ_1(sc, ATMEGA_UDCON); 21470134Scg ATMEGA_WRITE_1(sc, ATMEGA_UDCON, temp | ATMEGA_UDCON_RMWKUP); 21570134Scg 21670134Scg /* wait 8 milliseconds */ 21789834Scg /* Wait for reset to complete. */ 21864881Scg usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125); 21950724Scg 22050724Scg /* hardware should have cleared RMWKUP bit */ 22174763Scg} 22274763Scg 22366308Scgstatic void 22474763Scgatmegadci_set_address(struct atmegadci_softc *sc, uint8_t addr) 22566308Scg{ 22666308Scg DPRINTFN(5, "addr=%d\n", addr); 22766308Scg 22866308Scg addr |= ATMEGA_UDADDR_ADDEN; 22966308Scg 23066308Scg ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, addr); 23166308Scg} 23289834Scg 23366308Scgstatic uint8_t 23466308Scgatmegadci_setup_rx(struct atmegadci_td *td) 23566308Scg{ 23650724Scg struct atmegadci_softc *sc; 23774763Scg struct usb2_device_request req; 23850724Scg uint16_t count; 23964881Scg uint8_t temp; 24064881Scg 24164881Scg /* get pointer to softc */ 24264881Scg sc = ATMEGA_PC2SC(td->pc); 24364881Scg 24464881Scg /* select endpoint number */ 24564881Scg ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no); 24664881Scg 24764881Scg /* check endpoint status */ 24864881Scg temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX); 24964881Scg 25064881Scg DPRINTFN(5, "UEINTX=0x%02x\n", temp); 25164881Scg 25264881Scg if (!(temp & ATMEGA_UEINTX_RXSTPI)) { 25364881Scg /* abort any ongoing transfer */ 25464881Scg if (!td->did_stall) { 25564881Scg DPRINTFN(5, "stalling\n"); 25689834Scg ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 25764881Scg ATMEGA_UECONX_EPEN | 25850724Scg ATMEGA_UECONX_STALLRQ); 25950724Scg td->did_stall = 1; 26074763Scg } 26174763Scg goto not_complete; 26250724Scg } 26364881Scg /* get the packet byte count */ 26474763Scg count = 26564881Scg (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) | 26675319Scg (ATMEGA_READ_1(sc, ATMEGA_UEBCLX)); 26764881Scg 26864881Scg /* mask away undefined bits */ 26964881Scg count &= 0x7FF; 27064881Scg 27164881Scg /* verify data length */ 27264881Scg if (count != td->remainder) { 27364881Scg DPRINTFN(0, "Invalid SETUP packet " 27464881Scg "length, %d bytes\n", count); 27564881Scg goto not_complete; 27675319Scg } 27796928Speter if (count != sizeof(req)) { 27875319Scg DPRINTFN(0, "Unsupported SETUP packet " 27996928Speter "length, %d bytes\n", count); 28075319Scg goto not_complete; 28175319Scg } 28275319Scg /* receive data */ 28375319Scg ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX, 28475319Scg (void *)&req, sizeof(req)); 28575319Scg 28675319Scg /* copy data into real buffer */ 28775319Scg usb2_copy_in(td->pc, 0, &req, sizeof(req)); 28875319Scg 28964881Scg td->offset = sizeof(req); 29064881Scg td->remainder = 0; 29175319Scg 29289834Scg /* sneak peek the set address */ 29364881Scg if ((req.bmRequestType == UT_WRITE_DEVICE) && 29450724Scg (req.bRequest == UR_SET_ADDRESS)) { 29550724Scg sc->sc_dv_addr = req.wValue[0] & 0x7F; 29664881Scg /* must write address before ZLP */ 29774763Scg ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, sc->sc_dv_addr); 29864881Scg } else { 29989686Scg sc->sc_dv_addr = 0xFF; 30089686Scg } 30189686Scg 30264881Scg /* clear SETUP packet interrupt */ 30389686Scg ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ~ATMEGA_UEINTX_RXSTPI); 30489686Scg return (0); /* complete */ 30589686Scg 30689686Scgnot_complete: 30789686Scg /* we only want to know if there is a SETUP packet */ 30864881Scg ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, ATMEGA_UEIENX_RXSTPE); 30989686Scg return (1); /* not complete */ 31089686Scg} 31189686Scg 31289686Scgstatic uint8_t 31389686Scgatmegadci_data_rx(struct atmegadci_td *td) 31489686Scg{ 31589686Scg struct atmegadci_softc *sc; 31665645Scg struct usb2_page_search buf_res; 31789686Scg uint16_t count; 31889686Scg uint8_t temp; 31989686Scg uint8_t to; 32089686Scg uint8_t got_short; 32189686Scg 32289686Scg to = 3; /* don't loop forever! */ 32389686Scg got_short = 0; 32489686Scg 32589686Scg /* get pointer to softc */ 32689686Scg sc = ATMEGA_PC2SC(td->pc); 32789686Scg 32889686Scg /* select endpoint number */ 32989686Scg ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no); 33089686Scg 33189686Scgrepeat: 33289686Scg /* check if any of the FIFO banks have data */ 33389686Scg /* check endpoint status */ 33489686Scg temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX); 33589686Scg 33689686Scg DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder); 33789686Scg 33889686Scg if (temp & ATMEGA_UEINTX_RXSTPI) { 33989686Scg if (td->remainder == 0) { 34089686Scg /* 34189686Scg * We are actually complete and have 34289686Scg * received the next SETUP 34389686Scg */ 34489686Scg DPRINTFN(5, "faking complete\n"); 34589686Scg return (0); /* complete */ 34664881Scg } 34764881Scg /* 34889686Scg * USB Host Aborted the transfer. 34964881Scg */ 35064881Scg td->error = 1; 35165645Scg return (0); /* complete */ 35289686Scg } 35365645Scg /* check status */ 35464881Scg if (!(temp & (ATMEGA_UEINTX_FIFOCON | 35565645Scg ATMEGA_UEINTX_RXOUTI))) { 35675319Scg /* no data */ 35765645Scg goto not_complete; 35865645Scg } 35965645Scg /* get the packet byte count */ 36089686Scg count = 36189686Scg (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) | 36264881Scg (ATMEGA_READ_1(sc, ATMEGA_UEBCLX)); 36364881Scg 36464881Scg /* mask away undefined bits */ 36564881Scg count &= 0x7FF; 36664881Scg 36764881Scg /* verify the packet byte count */ 36865645Scg if (count != td->max_packet_size) { 36975319Scg if (count < td->max_packet_size) { 37065645Scg /* we have a short packet */ 37189834Scg td->short_pkt = 1; 37289686Scg got_short = 1; 37364881Scg } else { 37466308Scg /* invalid USB packet */ 37566308Scg td->error = 1; 37666308Scg return (0); /* we are complete */ 37766308Scg } 37874763Scg } 37966308Scg /* verify the packet byte count */ 38074763Scg if (count > td->remainder) { 38174763Scg /* invalid USB packet */ 38274763Scg td->error = 1; 38366308Scg return (0); /* we are complete */ 38474763Scg } 38574763Scg while (count > 0) { 38674763Scg usb2_get_page(td->pc, td->offset, &buf_res); 38766308Scg 38874763Scg /* get correct length */ 38974763Scg if (buf_res.length > count) { 39066308Scg buf_res.length = count; 391110287Sorion } 392110287Sorion /* receive data */ 393110287Sorion ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX, 394110287Sorion buf_res.buffer, buf_res.length); 39574763Scg 39674763Scg /* update counters */ 39774763Scg count -= buf_res.length; 39874763Scg td->offset += buf_res.length; 39974763Scg td->remainder -= buf_res.length; 40074763Scg } 40174763Scg 40274763Scg /* clear OUT packet interrupt */ 40374763Scg ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_RXOUTI ^ 0xFF); 40466308Scg 40566308Scg /* release FIFO bank */ 40670134Scg ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_FIFOCON ^ 0xFF); 40770134Scg 40870134Scg /* check if we are complete */ 40970134Scg if ((td->remainder == 0) || got_short) { 41066308Scg if (td->short_pkt) { 41170134Scg /* we are complete */ 41270134Scg return (0); 41370134Scg } 41474763Scg /* else need to receive a zero length packet */ 41570134Scg } 41670134Scg if (--to) { 41770134Scg goto repeat; 41870134Scg } 41970134Scgnot_complete: 42074363Scg /* we only want to know if there is a SETUP packet or OUT packet */ 42166308Scg ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 42266308Scg ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_RXOUTE); 42366308Scg return (1); /* not complete */ 42466308Scg} 42570134Scg 426static uint8_t 427atmegadci_data_tx(struct atmegadci_td *td) 428{ 429 struct atmegadci_softc *sc; 430 struct usb2_page_search buf_res; 431 uint16_t count; 432 uint8_t to; 433 uint8_t temp; 434 435 to = 3; /* don't loop forever! */ 436 437 /* get pointer to softc */ 438 sc = ATMEGA_PC2SC(td->pc); 439 440 /* select endpoint number */ 441 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no); 442 443repeat: 444 445 /* check endpoint status */ 446 temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX); 447 448 DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder); 449 450 if (temp & ATMEGA_UEINTX_RXSTPI) { 451 /* 452 * The current transfer was aborted 453 * by the USB Host 454 */ 455 td->error = 1; 456 return (0); /* complete */ 457 } 458 if (!(temp & (ATMEGA_UEINTX_FIFOCON | 459 ATMEGA_UEINTX_TXINI))) { 460 /* cannot write any data */ 461 goto not_complete; 462 } 463 count = td->max_packet_size; 464 if (td->remainder < count) { 465 /* we have a short packet */ 466 td->short_pkt = 1; 467 count = td->remainder; 468 } 469 while (count > 0) { 470 471 usb2_get_page(td->pc, td->offset, &buf_res); 472 473 /* get correct length */ 474 if (buf_res.length > count) { 475 buf_res.length = count; 476 } 477 /* transmit data */ 478 ATMEGA_WRITE_MULTI_1(sc, ATMEGA_UEDATX, 479 buf_res.buffer, buf_res.length); 480 481 /* update counters */ 482 count -= buf_res.length; 483 td->offset += buf_res.length; 484 td->remainder -= buf_res.length; 485 } 486 487 /* clear IN packet interrupt */ 488 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_TXINI); 489 490 /* allocate FIFO bank */ 491 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_FIFOCON); 492 493 /* check remainder */ 494 if (td->remainder == 0) { 495 if (td->short_pkt) { 496 return (0); /* complete */ 497 } 498 /* else we need to transmit a short packet */ 499 } 500 if (--to) { 501 goto repeat; 502 } 503not_complete: 504 /* we only want to know if there is a SETUP packet or free IN packet */ 505 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 506 ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE); 507 return (1); /* not complete */ 508} 509 510static uint8_t 511atmegadci_data_tx_sync(struct atmegadci_td *td) 512{ 513 struct atmegadci_softc *sc; 514 uint8_t temp; 515 516 /* get pointer to softc */ 517 sc = ATMEGA_PC2SC(td->pc); 518 519 /* select endpoint number */ 520 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no); 521 522 /* check endpoint status */ 523 temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX); 524 525 DPRINTFN(5, "temp=0x%02x\n", temp); 526 527 if (temp & ATMEGA_UEINTX_RXSTPI) { 528 DPRINTFN(5, "faking complete\n"); 529 /* Race condition */ 530 return (0); /* complete */ 531 } 532 /* 533 * The control endpoint has only got one bank, so if that bank 534 * is free the packet has been transferred! 535 */ 536 if (!(temp & (ATMEGA_UEINTX_FIFOCON | 537 ATMEGA_UEINTX_TXINI))) { 538 /* cannot write any data */ 539 goto not_complete; 540 } 541 if (sc->sc_dv_addr != 0xFF) { 542 /* set new address */ 543 atmegadci_set_address(sc, sc->sc_dv_addr); 544 } 545 return (0); /* complete */ 546 547not_complete: 548 /* we only want to know if there is a SETUP packet or free IN packet */ 549 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 550 ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE); 551 return (1); /* not complete */ 552} 553 554static uint8_t 555atmegadci_xfer_do_fifo(struct usb2_xfer *xfer) 556{ 557 struct atmegadci_td *td; 558 559 DPRINTFN(9, "\n"); 560 561 td = xfer->td_transfer_cache; 562 while (1) { 563 if ((td->func) (td)) { 564 /* operation in progress */ 565 break; 566 } 567 if (((void *)td) == xfer->td_transfer_last) { 568 goto done; 569 } 570 if (td->error) { 571 goto done; 572 } else if (td->remainder > 0) { 573 /* 574 * We had a short transfer. If there is no alternate 575 * next, stop processing ! 576 */ 577 if (!td->alt_next) { 578 goto done; 579 } 580 } 581 /* 582 * Fetch the next transfer descriptor and transfer 583 * some flags to the next transfer descriptor 584 */ 585 td = td->obj_next; 586 xfer->td_transfer_cache = td; 587 } 588 return (1); /* not complete */ 589 590done: 591 /* compute all actual lengths */ 592 593 atmegadci_standard_done(xfer); 594 return (0); /* complete */ 595} 596 597static void 598atmegadci_interrupt_poll(struct atmegadci_softc *sc) 599{ 600 struct usb2_xfer *xfer; 601 602repeat: 603 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 604 if (!atmegadci_xfer_do_fifo(xfer)) { 605 /* queue has been modified */ 606 goto repeat; 607 } 608 } 609} 610 611static void 612atmegadci_vbus_interrupt(struct atmegadci_softc *sc, uint8_t is_on) 613{ 614 DPRINTFN(5, "vbus = %u\n", is_on); 615 616 if (is_on) { 617 if (!sc->sc_flags.status_vbus) { 618 sc->sc_flags.status_vbus = 1; 619 620 /* complete root HUB interrupt endpoint */ 621 622 usb2_sw_transfer(&sc->sc_root_intr, 623 &atmegadci_root_intr_done); 624 } 625 } else { 626 if (sc->sc_flags.status_vbus) { 627 sc->sc_flags.status_vbus = 0; 628 sc->sc_flags.status_bus_reset = 0; 629 sc->sc_flags.status_suspend = 0; 630 sc->sc_flags.change_suspend = 0; 631 sc->sc_flags.change_connect = 1; 632 633 /* complete root HUB interrupt endpoint */ 634 635 usb2_sw_transfer(&sc->sc_root_intr, 636 &atmegadci_root_intr_done); 637 } 638 } 639} 640 641void 642atmegadci_interrupt(struct atmegadci_softc *sc) 643{ 644 uint8_t status; 645 646 USB_BUS_LOCK(&sc->sc_bus); 647 648 /* read interrupt status */ 649 status = ATMEGA_READ_1(sc, ATMEGA_UDINT); 650 651 /* clear all set interrupts */ 652 ATMEGA_WRITE_1(sc, ATMEGA_UDINT, ~status); 653 654 DPRINTFN(14, "UDINT=0x%02x\n", status); 655 656 /* check for any bus state change interrupts */ 657 if (status & ATMEGA_UDINT_EORSTI) { 658 659 DPRINTFN(5, "end of reset\n"); 660 661 /* set correct state */ 662 sc->sc_flags.status_bus_reset = 1; 663 sc->sc_flags.status_suspend = 0; 664 sc->sc_flags.change_suspend = 0; 665 sc->sc_flags.change_connect = 1; 666 667 /* disable resume interrupt */ 668 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 669 ATMEGA_UDINT_SUSPE | 670 ATMEGA_UDINT_EORSTE); 671 672 /* complete root HUB interrupt endpoint */ 673 usb2_sw_transfer(&sc->sc_root_intr, 674 &atmegadci_root_intr_done); 675 } 676 /* 677 * If resume and suspend is set at the same time we interpret 678 * that like RESUME. Resume is set when there is at least 3 679 * milliseconds of inactivity on the USB BUS. 680 */ 681 if (status & ATMEGA_UDINT_EORSMI) { 682 683 DPRINTFN(5, "resume interrupt\n"); 684 685 if (sc->sc_flags.status_suspend) { 686 /* update status bits */ 687 sc->sc_flags.status_suspend = 0; 688 sc->sc_flags.change_suspend = 1; 689 690 /* disable resume interrupt */ 691 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 692 ATMEGA_UDINT_SUSPE | 693 ATMEGA_UDINT_EORSTE); 694 695 /* complete root HUB interrupt endpoint */ 696 usb2_sw_transfer(&sc->sc_root_intr, 697 &atmegadci_root_intr_done); 698 } 699 } else if (status & ATMEGA_UDINT_SUSPI) { 700 701 DPRINTFN(5, "suspend interrupt\n"); 702 703 if (!sc->sc_flags.status_suspend) { 704 /* update status bits */ 705 sc->sc_flags.status_suspend = 1; 706 sc->sc_flags.change_suspend = 1; 707 708 /* disable suspend interrupt */ 709 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 710 ATMEGA_UDINT_EORSMI | 711 ATMEGA_UDINT_EORSTE); 712 713 /* complete root HUB interrupt endpoint */ 714 usb2_sw_transfer(&sc->sc_root_intr, 715 &atmegadci_root_intr_done); 716 } 717 } 718 /* check VBUS */ 719 status = ATMEGA_READ_1(sc, ATMEGA_USBINT); 720 721 /* clear all set interrupts */ 722 ATMEGA_WRITE_1(sc, ATMEGA_USBINT, ~status); 723 724 if (status & ATMEGA_USBINT_VBUSTI) { 725 uint8_t temp; 726 727 DPRINTFN(5, "USBINT=0x%02x\n", status); 728 729 temp = ATMEGA_READ_1(sc, ATMEGA_USBSTA); 730 atmegadci_vbus_interrupt(sc, temp & ATMEGA_USBSTA_VBUS); 731 } 732 /* check for any endpoint interrupts */ 733 status = ATMEGA_READ_1(sc, ATMEGA_UEINT); 734 735 /* clear all set interrupts */ 736 ATMEGA_WRITE_1(sc, ATMEGA_UEINT, ~status); 737 738 if (status) { 739 740 DPRINTFN(5, "real endpoint interrupt UEINT=0x%02x\n", status); 741 742 atmegadci_interrupt_poll(sc); 743 } 744 USB_BUS_UNLOCK(&sc->sc_bus); 745} 746 747static void 748atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp *temp) 749{ 750 struct atmegadci_td *td; 751 752 /* get current Transfer Descriptor */ 753 td = temp->td_next; 754 temp->td = td; 755 756 /* prepare for next TD */ 757 temp->td_next = td->obj_next; 758 759 /* fill out the Transfer Descriptor */ 760 td->func = temp->func; 761 td->pc = temp->pc; 762 td->offset = temp->offset; 763 td->remainder = temp->len; 764 td->error = 0; 765 td->did_stall = 0; 766 td->short_pkt = temp->short_pkt; 767 td->alt_next = temp->setup_alt_next; 768} 769 770static void 771atmegadci_setup_standard_chain(struct usb2_xfer *xfer) 772{ 773 struct atmegadci_std_temp temp; 774 struct atmegadci_softc *sc; 775 struct atmegadci_td *td; 776 uint32_t x; 777 uint8_t ep_no; 778 uint8_t need_sync; 779 780 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n", 781 xfer->address, UE_GET_ADDR(xfer->endpoint), 782 xfer->sumlen, usb2_get_speed(xfer->xroot->udev)); 783 784 temp.max_frame_size = xfer->max_frame_size; 785 786 td = xfer->td_start[0]; 787 xfer->td_transfer_first = td; 788 xfer->td_transfer_cache = td; 789 790 /* setup temp */ 791 792 temp.td = NULL; 793 temp.td_next = xfer->td_start[0]; 794 temp.offset = 0; 795 temp.setup_alt_next = xfer->flags_int.short_frames_ok; 796 797 sc = ATMEGA_BUS2SC(xfer->xroot->bus); 798 ep_no = (xfer->endpoint & UE_ADDR); 799 800 /* check if we should prepend a setup message */ 801 802 if (xfer->flags_int.control_xfr) { 803 if (xfer->flags_int.control_hdr) { 804 805 temp.func = &atmegadci_setup_rx; 806 temp.len = xfer->frlengths[0]; 807 temp.pc = xfer->frbuffers + 0; 808 temp.short_pkt = temp.len ? 1 : 0; 809 /* check for last frame */ 810 if (xfer->nframes == 1) { 811 /* no STATUS stage yet, SETUP is last */ 812 if (xfer->flags_int.control_act) 813 temp.setup_alt_next = 0; 814 } 815 816 atmegadci_setup_standard_chain_sub(&temp); 817 } 818 x = 1; 819 } else { 820 x = 0; 821 } 822 823 if (x != xfer->nframes) { 824 if (xfer->endpoint & UE_DIR_IN) { 825 temp.func = &atmegadci_data_tx; 826 need_sync = 1; 827 } else { 828 temp.func = &atmegadci_data_rx; 829 need_sync = 0; 830 } 831 832 /* setup "pc" pointer */ 833 temp.pc = xfer->frbuffers + x; 834 } else { 835 need_sync = 0; 836 } 837 while (x != xfer->nframes) { 838 839 /* DATA0 / DATA1 message */ 840 841 temp.len = xfer->frlengths[x]; 842 843 x++; 844 845 if (x == xfer->nframes) { 846 if (xfer->flags_int.control_xfr) { 847 if (xfer->flags_int.control_act) { 848 temp.setup_alt_next = 0; 849 } 850 } else { 851 temp.setup_alt_next = 0; 852 } 853 } 854 if (temp.len == 0) { 855 856 /* make sure that we send an USB packet */ 857 858 temp.short_pkt = 0; 859 860 } else { 861 862 /* regular data transfer */ 863 864 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1; 865 } 866 867 atmegadci_setup_standard_chain_sub(&temp); 868 869 if (xfer->flags_int.isochronous_xfr) { 870 temp.offset += temp.len; 871 } else { 872 /* get next Page Cache pointer */ 873 temp.pc = xfer->frbuffers + x; 874 } 875 } 876 877 if (xfer->flags_int.control_xfr) { 878 879 /* always setup a valid "pc" pointer for status and sync */ 880 temp.pc = xfer->frbuffers + 0; 881 temp.len = 0; 882 temp.short_pkt = 0; 883 temp.setup_alt_next = 0; 884 885 /* check if we need to sync */ 886 if (need_sync) { 887 /* we need a SYNC point after TX */ 888 temp.func = &atmegadci_data_tx_sync; 889 atmegadci_setup_standard_chain_sub(&temp); 890 } 891 892 /* check if we should append a status stage */ 893 if (!xfer->flags_int.control_act) { 894 895 /* 896 * Send a DATA1 message and invert the current 897 * endpoint direction. 898 */ 899 if (xfer->endpoint & UE_DIR_IN) { 900 temp.func = &atmegadci_data_rx; 901 need_sync = 0; 902 } else { 903 temp.func = &atmegadci_data_tx; 904 need_sync = 1; 905 } 906 907 atmegadci_setup_standard_chain_sub(&temp); 908 if (need_sync) { 909 /* we need a SYNC point after TX */ 910 temp.func = &atmegadci_data_tx_sync; 911 atmegadci_setup_standard_chain_sub(&temp); 912 } 913 } 914 } 915 /* must have at least one frame! */ 916 td = temp.td; 917 xfer->td_transfer_last = td; 918} 919 920static void 921atmegadci_timeout(void *arg) 922{ 923 struct usb2_xfer *xfer = arg; 924 925 DPRINTF("xfer=%p\n", xfer); 926 927 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 928 929 /* transfer is transferred */ 930 atmegadci_device_done(xfer, USB_ERR_TIMEOUT); 931} 932 933static void 934atmegadci_start_standard_chain(struct usb2_xfer *xfer) 935{ 936 DPRINTFN(9, "\n"); 937 938 /* poll one time - will turn on interrupts */ 939 if (atmegadci_xfer_do_fifo(xfer)) { 940 941 /* put transfer on interrupt queue */ 942 usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 943 944 /* start timeout, if any */ 945 if (xfer->timeout != 0) { 946 usb2_transfer_timeout_ms(xfer, 947 &atmegadci_timeout, xfer->timeout); 948 } 949 } 950} 951 952static void 953atmegadci_root_intr_done(struct usb2_xfer *xfer, 954 struct usb2_sw_transfer *std) 955{ 956 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 957 958 DPRINTFN(9, "\n"); 959 960 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 961 962 if (std->state != USB_SW_TR_PRE_DATA) { 963 if (std->state == USB_SW_TR_PRE_CALLBACK) { 964 /* transfer transferred */ 965 atmegadci_device_done(xfer, std->err); 966 } 967 goto done; 968 } 969 /* setup buffer */ 970 std->ptr = sc->sc_hub_idata; 971 std->len = sizeof(sc->sc_hub_idata); 972 973 /* set port bit */ 974 sc->sc_hub_idata[0] = 0x02; /* we only have one port */ 975 976done: 977 return; 978} 979 980static usb2_error_t 981atmegadci_standard_done_sub(struct usb2_xfer *xfer) 982{ 983 struct atmegadci_td *td; 984 uint32_t len; 985 uint8_t error; 986 987 DPRINTFN(9, "\n"); 988 989 td = xfer->td_transfer_cache; 990 991 do { 992 len = td->remainder; 993 994 if (xfer->aframes != xfer->nframes) { 995 /* 996 * Verify the length and subtract 997 * the remainder from "frlengths[]": 998 */ 999 if (len > xfer->frlengths[xfer->aframes]) { 1000 td->error = 1; 1001 } else { 1002 xfer->frlengths[xfer->aframes] -= len; 1003 } 1004 } 1005 /* Check for transfer error */ 1006 if (td->error) { 1007 /* the transfer is finished */ 1008 error = 1; 1009 td = NULL; 1010 break; 1011 } 1012 /* Check for short transfer */ 1013 if (len > 0) { 1014 if (xfer->flags_int.short_frames_ok) { 1015 /* follow alt next */ 1016 if (td->alt_next) { 1017 td = td->obj_next; 1018 } else { 1019 td = NULL; 1020 } 1021 } else { 1022 /* the transfer is finished */ 1023 td = NULL; 1024 } 1025 error = 0; 1026 break; 1027 } 1028 td = td->obj_next; 1029 1030 /* this USB frame is complete */ 1031 error = 0; 1032 break; 1033 1034 } while (0); 1035 1036 /* update transfer cache */ 1037 1038 xfer->td_transfer_cache = td; 1039 1040 return (error ? 1041 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION); 1042} 1043 1044static void 1045atmegadci_standard_done(struct usb2_xfer *xfer) 1046{ 1047 usb2_error_t err = 0; 1048 1049 DPRINTFN(13, "xfer=%p pipe=%p transfer done\n", 1050 xfer, xfer->pipe); 1051 1052 /* reset scanner */ 1053 1054 xfer->td_transfer_cache = xfer->td_transfer_first; 1055 1056 if (xfer->flags_int.control_xfr) { 1057 1058 if (xfer->flags_int.control_hdr) { 1059 1060 err = atmegadci_standard_done_sub(xfer); 1061 } 1062 xfer->aframes = 1; 1063 1064 if (xfer->td_transfer_cache == NULL) { 1065 goto done; 1066 } 1067 } 1068 while (xfer->aframes != xfer->nframes) { 1069 1070 err = atmegadci_standard_done_sub(xfer); 1071 xfer->aframes++; 1072 1073 if (xfer->td_transfer_cache == NULL) { 1074 goto done; 1075 } 1076 } 1077 1078 if (xfer->flags_int.control_xfr && 1079 !xfer->flags_int.control_act) { 1080 1081 err = atmegadci_standard_done_sub(xfer); 1082 } 1083done: 1084 atmegadci_device_done(xfer, err); 1085} 1086 1087/*------------------------------------------------------------------------* 1088 * atmegadci_device_done 1089 * 1090 * NOTE: this function can be called more than one time on the 1091 * same USB transfer! 1092 *------------------------------------------------------------------------*/ 1093static void 1094atmegadci_device_done(struct usb2_xfer *xfer, usb2_error_t error) 1095{ 1096 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 1097 uint8_t ep_no; 1098 1099 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1100 1101 DPRINTFN(9, "xfer=%p, pipe=%p, error=%d\n", 1102 xfer, xfer->pipe, error); 1103 1104 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1105 ep_no = (xfer->endpoint & UE_ADDR); 1106 1107 /* select endpoint number */ 1108 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no); 1109 1110 /* disable endpoint interrupt */ 1111 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0); 1112 1113 DPRINTFN(15, "disabled interrupts!\n"); 1114 } 1115 /* dequeue transfer and start next transfer */ 1116 usb2_transfer_done(xfer, error); 1117} 1118 1119static void 1120atmegadci_set_stall(struct usb2_device *udev, struct usb2_xfer *xfer, 1121 struct usb2_pipe *pipe) 1122{ 1123 struct atmegadci_softc *sc; 1124 uint8_t ep_no; 1125 1126 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1127 1128 DPRINTFN(5, "pipe=%p\n", pipe); 1129 1130 if (xfer) { 1131 /* cancel any ongoing transfers */ 1132 atmegadci_device_done(xfer, USB_ERR_STALLED); 1133 } 1134 sc = ATMEGA_BUS2SC(udev->bus); 1135 /* get endpoint number */ 1136 ep_no = (pipe->edesc->bEndpointAddress & UE_ADDR); 1137 /* select endpoint number */ 1138 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no); 1139 /* set stall */ 1140 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 1141 ATMEGA_UECONX_EPEN | 1142 ATMEGA_UECONX_STALLRQ); 1143} 1144 1145static void 1146atmegadci_clear_stall_sub(struct atmegadci_softc *sc, uint8_t ep_no, 1147 uint8_t ep_type, uint8_t ep_dir) 1148{ 1149 uint8_t temp; 1150 1151 if (ep_type == UE_CONTROL) { 1152 /* clearing stall is not needed */ 1153 return; 1154 } 1155 /* select endpoint number */ 1156 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no); 1157 1158 /* set endpoint reset */ 1159 ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(ep_no)); 1160 1161 /* clear endpoint reset */ 1162 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0); 1163 1164 /* set stall */ 1165 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 1166 ATMEGA_UECONX_EPEN | 1167 ATMEGA_UECONX_STALLRQ); 1168 1169 /* reset data toggle */ 1170 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 1171 ATMEGA_UECONX_EPEN | 1172 ATMEGA_UECONX_RSTDT); 1173 1174 /* clear stall */ 1175 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 1176 ATMEGA_UECONX_EPEN | 1177 ATMEGA_UECONX_STALLRQC); 1178 1179 do { 1180 temp = 0; 1181 if (ep_type == UE_BULK) { 1182 temp |= ATMEGA_UECFG0X_EPTYPE2; 1183 } else if (ep_type == UE_INTERRUPT) { 1184 temp |= ATMEGA_UECFG0X_EPTYPE3; 1185 } else { 1186 temp |= ATMEGA_UECFG0X_EPTYPE1; 1187 } 1188 if (ep_dir & UE_DIR_IN) { 1189 temp |= ATMEGA_UECFG0X_EPDIR; 1190 } 1191 /* two banks, 64-bytes wMaxPacket */ 1192 ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, temp); 1193 ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X, 1194 ATMEGA_UECFG1X_ALLOC | 1195 ATMEGA_UECFG1X_EPBK1 | 1196 ATMEGA_UECFG1X_EPSIZE(3)); 1197 1198 temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X); 1199 if (!(temp & ATMEGA_UESTA0X_CFGOK)) { 1200 DPRINTFN(0, "Chip rejected configuration\n"); 1201 } 1202 } while (0); 1203} 1204 1205static void 1206atmegadci_clear_stall(struct usb2_device *udev, struct usb2_pipe *pipe) 1207{ 1208 struct atmegadci_softc *sc; 1209 struct usb2_endpoint_descriptor *ed; 1210 1211 DPRINTFN(5, "pipe=%p\n", pipe); 1212 1213 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1214 1215 /* check mode */ 1216 if (udev->flags.usb2_mode != USB_MODE_DEVICE) { 1217 /* not supported */ 1218 return; 1219 } 1220 /* get softc */ 1221 sc = ATMEGA_BUS2SC(udev->bus); 1222 1223 /* get endpoint descriptor */ 1224 ed = pipe->edesc; 1225 1226 /* reset endpoint */ 1227 atmegadci_clear_stall_sub(sc, 1228 (ed->bEndpointAddress & UE_ADDR), 1229 (ed->bmAttributes & UE_XFERTYPE), 1230 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT))); 1231} 1232 1233usb2_error_t 1234atmegadci_init(struct atmegadci_softc *sc) 1235{ 1236 uint8_t n; 1237 1238 DPRINTF("start\n"); 1239 1240 /* set up the bus structure */ 1241 sc->sc_bus.usbrev = USB_REV_1_1; 1242 sc->sc_bus.methods = &atmegadci_bus_methods; 1243 1244 USB_BUS_LOCK(&sc->sc_bus); 1245#if 0 1246 /* XXX TODO - currently done by boot strap */ 1247 1248 /* enable USB PAD regulator */ 1249 ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 1250 ATMEGA_UHWCON_UVREGE | ATMEGA_UHWCON_UIMOD); 1251#endif 1252 /* turn on clocks */ 1253 (sc->sc_clocks_on) (&sc->sc_bus); 1254 1255 /* make sure device is re-enumerated */ 1256 ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH); 1257 1258 /* wait a little for things to stabilise */ 1259 usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20); 1260 1261 /* enable interrupts */ 1262 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 1263 ATMEGA_UDINT_SUSPE | 1264 ATMEGA_UDINT_EORSTE); 1265 1266 /* reset all endpoints */ 1267 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 1268 (1 << ATMEGA_EP_MAX) - 1); 1269 1270 /* disable reset */ 1271 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0); 1272 1273 /* disable all endpoints */ 1274 for (n = 0; n != ATMEGA_EP_MAX; n++) { 1275 1276 /* select endpoint */ 1277 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, n); 1278 1279 /* disable endpoint interrupt */ 1280 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0); 1281 1282 /* disable endpoint */ 1283 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 0); 1284 } 1285 1286 /* turn off clocks */ 1287 1288 atmegadci_clocks_off(sc); 1289 1290 /* read initial VBUS state */ 1291 1292 n = ATMEGA_READ_1(sc, ATMEGA_USBSTA); 1293 atmegadci_vbus_interrupt(sc, n & ATMEGA_USBSTA_VBUS); 1294 1295 USB_BUS_UNLOCK(&sc->sc_bus); 1296 1297 /* catch any lost interrupts */ 1298 1299 atmegadci_do_poll(&sc->sc_bus); 1300 1301 return (0); /* success */ 1302} 1303 1304void 1305atmegadci_uninit(struct atmegadci_softc *sc) 1306{ 1307 USB_BUS_LOCK(&sc->sc_bus); 1308 1309 /* turn on clocks */ 1310 (sc->sc_clocks_on) (&sc->sc_bus); 1311 1312 /* disable interrupts */ 1313 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 0); 1314 1315 /* reset all endpoints */ 1316 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 1317 (1 << ATMEGA_EP_MAX) - 1); 1318 1319 /* disable reset */ 1320 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0); 1321 1322 sc->sc_flags.port_powered = 0; 1323 sc->sc_flags.status_vbus = 0; 1324 sc->sc_flags.status_bus_reset = 0; 1325 sc->sc_flags.status_suspend = 0; 1326 sc->sc_flags.change_suspend = 0; 1327 sc->sc_flags.change_connect = 1; 1328 1329 atmegadci_pull_down(sc); 1330 atmegadci_clocks_off(sc); 1331 1332 /* disable USB PAD regulator */ 1333 ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 0); 1334 1335 USB_BUS_UNLOCK(&sc->sc_bus); 1336} 1337 1338void 1339atmegadci_suspend(struct atmegadci_softc *sc) 1340{ 1341 return; 1342} 1343 1344void 1345atmegadci_resume(struct atmegadci_softc *sc) 1346{ 1347 return; 1348} 1349 1350static void 1351atmegadci_do_poll(struct usb2_bus *bus) 1352{ 1353 struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus); 1354 1355 USB_BUS_LOCK(&sc->sc_bus); 1356 atmegadci_interrupt_poll(sc); 1357 atmegadci_root_ctrl_poll(sc); 1358 USB_BUS_UNLOCK(&sc->sc_bus); 1359} 1360 1361/*------------------------------------------------------------------------* 1362 * at91dci bulk support 1363 *------------------------------------------------------------------------*/ 1364static void 1365atmegadci_device_bulk_open(struct usb2_xfer *xfer) 1366{ 1367 return; 1368} 1369 1370static void 1371atmegadci_device_bulk_close(struct usb2_xfer *xfer) 1372{ 1373 atmegadci_device_done(xfer, USB_ERR_CANCELLED); 1374} 1375 1376static void 1377atmegadci_device_bulk_enter(struct usb2_xfer *xfer) 1378{ 1379 return; 1380} 1381 1382static void 1383atmegadci_device_bulk_start(struct usb2_xfer *xfer) 1384{ 1385 /* setup TDs */ 1386 atmegadci_setup_standard_chain(xfer); 1387 atmegadci_start_standard_chain(xfer); 1388} 1389 1390struct usb2_pipe_methods atmegadci_device_bulk_methods = 1391{ 1392 .open = atmegadci_device_bulk_open, 1393 .close = atmegadci_device_bulk_close, 1394 .enter = atmegadci_device_bulk_enter, 1395 .start = atmegadci_device_bulk_start, 1396 .enter_is_cancelable = 1, 1397 .start_is_cancelable = 1, 1398}; 1399 1400/*------------------------------------------------------------------------* 1401 * at91dci control support 1402 *------------------------------------------------------------------------*/ 1403static void 1404atmegadci_device_ctrl_open(struct usb2_xfer *xfer) 1405{ 1406 return; 1407} 1408 1409static void 1410atmegadci_device_ctrl_close(struct usb2_xfer *xfer) 1411{ 1412 atmegadci_device_done(xfer, USB_ERR_CANCELLED); 1413} 1414 1415static void 1416atmegadci_device_ctrl_enter(struct usb2_xfer *xfer) 1417{ 1418 return; 1419} 1420 1421static void 1422atmegadci_device_ctrl_start(struct usb2_xfer *xfer) 1423{ 1424 /* setup TDs */ 1425 atmegadci_setup_standard_chain(xfer); 1426 atmegadci_start_standard_chain(xfer); 1427} 1428 1429struct usb2_pipe_methods atmegadci_device_ctrl_methods = 1430{ 1431 .open = atmegadci_device_ctrl_open, 1432 .close = atmegadci_device_ctrl_close, 1433 .enter = atmegadci_device_ctrl_enter, 1434 .start = atmegadci_device_ctrl_start, 1435 .enter_is_cancelable = 1, 1436 .start_is_cancelable = 1, 1437}; 1438 1439/*------------------------------------------------------------------------* 1440 * at91dci interrupt support 1441 *------------------------------------------------------------------------*/ 1442static void 1443atmegadci_device_intr_open(struct usb2_xfer *xfer) 1444{ 1445 return; 1446} 1447 1448static void 1449atmegadci_device_intr_close(struct usb2_xfer *xfer) 1450{ 1451 atmegadci_device_done(xfer, USB_ERR_CANCELLED); 1452} 1453 1454static void 1455atmegadci_device_intr_enter(struct usb2_xfer *xfer) 1456{ 1457 return; 1458} 1459 1460static void 1461atmegadci_device_intr_start(struct usb2_xfer *xfer) 1462{ 1463 /* setup TDs */ 1464 atmegadci_setup_standard_chain(xfer); 1465 atmegadci_start_standard_chain(xfer); 1466} 1467 1468struct usb2_pipe_methods atmegadci_device_intr_methods = 1469{ 1470 .open = atmegadci_device_intr_open, 1471 .close = atmegadci_device_intr_close, 1472 .enter = atmegadci_device_intr_enter, 1473 .start = atmegadci_device_intr_start, 1474 .enter_is_cancelable = 1, 1475 .start_is_cancelable = 1, 1476}; 1477 1478/*------------------------------------------------------------------------* 1479 * at91dci full speed isochronous support 1480 *------------------------------------------------------------------------*/ 1481static void 1482atmegadci_device_isoc_fs_open(struct usb2_xfer *xfer) 1483{ 1484 return; 1485} 1486 1487static void 1488atmegadci_device_isoc_fs_close(struct usb2_xfer *xfer) 1489{ 1490 atmegadci_device_done(xfer, USB_ERR_CANCELLED); 1491} 1492 1493static void 1494atmegadci_device_isoc_fs_enter(struct usb2_xfer *xfer) 1495{ 1496 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 1497 uint32_t temp; 1498 uint32_t nframes; 1499 1500 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n", 1501 xfer, xfer->pipe->isoc_next, xfer->nframes); 1502 1503 /* get the current frame index */ 1504 1505 nframes = 1506 (ATMEGA_READ_1(sc, ATMEGA_UDFNUMH) << 8) | 1507 (ATMEGA_READ_1(sc, ATMEGA_UDFNUML)); 1508 1509 nframes &= ATMEGA_FRAME_MASK; 1510 1511 /* 1512 * check if the frame index is within the window where the frames 1513 * will be inserted 1514 */ 1515 temp = (nframes - xfer->pipe->isoc_next) & ATMEGA_FRAME_MASK; 1516 1517 if ((xfer->pipe->is_synced == 0) || 1518 (temp < xfer->nframes)) { 1519 /* 1520 * If there is data underflow or the pipe queue is 1521 * empty we schedule the transfer a few frames ahead 1522 * of the current frame position. Else two isochronous 1523 * transfers might overlap. 1524 */ 1525 xfer->pipe->isoc_next = (nframes + 3) & ATMEGA_FRAME_MASK; 1526 xfer->pipe->is_synced = 1; 1527 DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next); 1528 } 1529 /* 1530 * compute how many milliseconds the insertion is ahead of the 1531 * current frame position: 1532 */ 1533 temp = (xfer->pipe->isoc_next - nframes) & ATMEGA_FRAME_MASK; 1534 1535 /* 1536 * pre-compute when the isochronous transfer will be finished: 1537 */ 1538 xfer->isoc_time_complete = 1539 usb2_isoc_time_expand(&sc->sc_bus, nframes) + temp + 1540 xfer->nframes; 1541 1542 /* compute frame number for next insertion */ 1543 xfer->pipe->isoc_next += xfer->nframes; 1544 1545 /* setup TDs */ 1546 atmegadci_setup_standard_chain(xfer); 1547} 1548 1549static void 1550atmegadci_device_isoc_fs_start(struct usb2_xfer *xfer) 1551{ 1552 /* start TD chain */ 1553 atmegadci_start_standard_chain(xfer); 1554} 1555 1556struct usb2_pipe_methods atmegadci_device_isoc_fs_methods = 1557{ 1558 .open = atmegadci_device_isoc_fs_open, 1559 .close = atmegadci_device_isoc_fs_close, 1560 .enter = atmegadci_device_isoc_fs_enter, 1561 .start = atmegadci_device_isoc_fs_start, 1562 .enter_is_cancelable = 1, 1563 .start_is_cancelable = 1, 1564}; 1565 1566/*------------------------------------------------------------------------* 1567 * at91dci root control support 1568 *------------------------------------------------------------------------* 1569 * simulate a hardware HUB by handling 1570 * all the necessary requests 1571 *------------------------------------------------------------------------*/ 1572 1573static void 1574atmegadci_root_ctrl_open(struct usb2_xfer *xfer) 1575{ 1576 return; 1577} 1578 1579static void 1580atmegadci_root_ctrl_close(struct usb2_xfer *xfer) 1581{ 1582 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 1583 1584 if (sc->sc_root_ctrl.xfer == xfer) { 1585 sc->sc_root_ctrl.xfer = NULL; 1586 } 1587 atmegadci_device_done(xfer, USB_ERR_CANCELLED); 1588} 1589 1590/* 1591 * USB descriptors for the virtual Root HUB: 1592 */ 1593 1594static const struct usb2_device_descriptor atmegadci_devd = { 1595 .bLength = sizeof(struct usb2_device_descriptor), 1596 .bDescriptorType = UDESC_DEVICE, 1597 .bcdUSB = {0x00, 0x02}, 1598 .bDeviceClass = UDCLASS_HUB, 1599 .bDeviceSubClass = UDSUBCLASS_HUB, 1600 .bDeviceProtocol = UDPROTO_HSHUBSTT, 1601 .bMaxPacketSize = 64, 1602 .bcdDevice = {0x00, 0x01}, 1603 .iManufacturer = 1, 1604 .iProduct = 2, 1605 .bNumConfigurations = 1, 1606}; 1607 1608static const struct usb2_device_qualifier atmegadci_odevd = { 1609 .bLength = sizeof(struct usb2_device_qualifier), 1610 .bDescriptorType = UDESC_DEVICE_QUALIFIER, 1611 .bcdUSB = {0x00, 0x02}, 1612 .bDeviceClass = UDCLASS_HUB, 1613 .bDeviceSubClass = UDSUBCLASS_HUB, 1614 .bDeviceProtocol = UDPROTO_FSHUB, 1615 .bMaxPacketSize0 = 0, 1616 .bNumConfigurations = 0, 1617}; 1618 1619static const struct atmegadci_config_desc atmegadci_confd = { 1620 .confd = { 1621 .bLength = sizeof(struct usb2_config_descriptor), 1622 .bDescriptorType = UDESC_CONFIG, 1623 .wTotalLength[0] = sizeof(atmegadci_confd), 1624 .bNumInterface = 1, 1625 .bConfigurationValue = 1, 1626 .iConfiguration = 0, 1627 .bmAttributes = UC_SELF_POWERED, 1628 .bMaxPower = 0, 1629 }, 1630 .ifcd = { 1631 .bLength = sizeof(struct usb2_interface_descriptor), 1632 .bDescriptorType = UDESC_INTERFACE, 1633 .bNumEndpoints = 1, 1634 .bInterfaceClass = UICLASS_HUB, 1635 .bInterfaceSubClass = UISUBCLASS_HUB, 1636 .bInterfaceProtocol = UIPROTO_HSHUBSTT, 1637 }, 1638 1639 .endpd = { 1640 .bLength = sizeof(struct usb2_endpoint_descriptor), 1641 .bDescriptorType = UDESC_ENDPOINT, 1642 .bEndpointAddress = (UE_DIR_IN | ATMEGA_INTR_ENDPT), 1643 .bmAttributes = UE_INTERRUPT, 1644 .wMaxPacketSize[0] = 8, 1645 .bInterval = 255, 1646 }, 1647}; 1648 1649static const struct usb2_hub_descriptor_min atmegadci_hubd = { 1650 .bDescLength = sizeof(atmegadci_hubd), 1651 .bDescriptorType = UDESC_HUB, 1652 .bNbrPorts = 1, 1653 .wHubCharacteristics[0] = 1654 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) & 0xFF, 1655 .wHubCharacteristics[1] = 1656 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) >> 8, 1657 .bPwrOn2PwrGood = 50, 1658 .bHubContrCurrent = 0, 1659 .DeviceRemovable = {0}, /* port is removable */ 1660}; 1661 1662#define STRING_LANG \ 1663 0x09, 0x04, /* American English */ 1664 1665#define STRING_VENDOR \ 1666 'A', 0, 'T', 0, 'M', 0, 'E', 0, 'G', 0, 'A', 0 1667 1668#define STRING_PRODUCT \ 1669 'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \ 1670 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \ 1671 'U', 0, 'B', 0, 1672 1673USB_MAKE_STRING_DESC(STRING_LANG, atmegadci_langtab); 1674USB_MAKE_STRING_DESC(STRING_VENDOR, atmegadci_vendor); 1675USB_MAKE_STRING_DESC(STRING_PRODUCT, atmegadci_product); 1676 1677static void 1678atmegadci_root_ctrl_enter(struct usb2_xfer *xfer) 1679{ 1680 return; 1681} 1682 1683static void 1684atmegadci_root_ctrl_start(struct usb2_xfer *xfer) 1685{ 1686 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 1687 1688 sc->sc_root_ctrl.xfer = xfer; 1689 1690 usb2_bus_roothub_exec(xfer->xroot->bus); 1691} 1692 1693static void 1694atmegadci_root_ctrl_task(struct usb2_bus *bus) 1695{ 1696 atmegadci_root_ctrl_poll(ATMEGA_BUS2SC(bus)); 1697} 1698 1699static void 1700atmegadci_root_ctrl_done(struct usb2_xfer *xfer, 1701 struct usb2_sw_transfer *std) 1702{ 1703 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 1704 uint16_t value; 1705 uint16_t index; 1706 uint8_t temp; 1707 1708 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1709 1710 if (std->state != USB_SW_TR_SETUP) { 1711 if (std->state == USB_SW_TR_PRE_CALLBACK) { 1712 /* transfer transferred */ 1713 atmegadci_device_done(xfer, std->err); 1714 } 1715 goto done; 1716 } 1717 /* buffer reset */ 1718 std->ptr = USB_ADD_BYTES(&sc->sc_hub_temp, 0); 1719 std->len = 0; 1720 1721 value = UGETW(std->req.wValue); 1722 index = UGETW(std->req.wIndex); 1723 1724 /* demultiplex the control request */ 1725 1726 switch (std->req.bmRequestType) { 1727 case UT_READ_DEVICE: 1728 switch (std->req.bRequest) { 1729 case UR_GET_DESCRIPTOR: 1730 goto tr_handle_get_descriptor; 1731 case UR_GET_CONFIG: 1732 goto tr_handle_get_config; 1733 case UR_GET_STATUS: 1734 goto tr_handle_get_status; 1735 default: 1736 goto tr_stalled; 1737 } 1738 break; 1739 1740 case UT_WRITE_DEVICE: 1741 switch (std->req.bRequest) { 1742 case UR_SET_ADDRESS: 1743 goto tr_handle_set_address; 1744 case UR_SET_CONFIG: 1745 goto tr_handle_set_config; 1746 case UR_CLEAR_FEATURE: 1747 goto tr_valid; /* nop */ 1748 case UR_SET_DESCRIPTOR: 1749 goto tr_valid; /* nop */ 1750 case UR_SET_FEATURE: 1751 default: 1752 goto tr_stalled; 1753 } 1754 break; 1755 1756 case UT_WRITE_ENDPOINT: 1757 switch (std->req.bRequest) { 1758 case UR_CLEAR_FEATURE: 1759 switch (UGETW(std->req.wValue)) { 1760 case UF_ENDPOINT_HALT: 1761 goto tr_handle_clear_halt; 1762 case UF_DEVICE_REMOTE_WAKEUP: 1763 goto tr_handle_clear_wakeup; 1764 default: 1765 goto tr_stalled; 1766 } 1767 break; 1768 case UR_SET_FEATURE: 1769 switch (UGETW(std->req.wValue)) { 1770 case UF_ENDPOINT_HALT: 1771 goto tr_handle_set_halt; 1772 case UF_DEVICE_REMOTE_WAKEUP: 1773 goto tr_handle_set_wakeup; 1774 default: 1775 goto tr_stalled; 1776 } 1777 break; 1778 case UR_SYNCH_FRAME: 1779 goto tr_valid; /* nop */ 1780 default: 1781 goto tr_stalled; 1782 } 1783 break; 1784 1785 case UT_READ_ENDPOINT: 1786 switch (std->req.bRequest) { 1787 case UR_GET_STATUS: 1788 goto tr_handle_get_ep_status; 1789 default: 1790 goto tr_stalled; 1791 } 1792 break; 1793 1794 case UT_WRITE_INTERFACE: 1795 switch (std->req.bRequest) { 1796 case UR_SET_INTERFACE: 1797 goto tr_handle_set_interface; 1798 case UR_CLEAR_FEATURE: 1799 goto tr_valid; /* nop */ 1800 case UR_SET_FEATURE: 1801 default: 1802 goto tr_stalled; 1803 } 1804 break; 1805 1806 case UT_READ_INTERFACE: 1807 switch (std->req.bRequest) { 1808 case UR_GET_INTERFACE: 1809 goto tr_handle_get_interface; 1810 case UR_GET_STATUS: 1811 goto tr_handle_get_iface_status; 1812 default: 1813 goto tr_stalled; 1814 } 1815 break; 1816 1817 case UT_WRITE_CLASS_INTERFACE: 1818 case UT_WRITE_VENDOR_INTERFACE: 1819 /* XXX forward */ 1820 break; 1821 1822 case UT_READ_CLASS_INTERFACE: 1823 case UT_READ_VENDOR_INTERFACE: 1824 /* XXX forward */ 1825 break; 1826 1827 case UT_WRITE_CLASS_DEVICE: 1828 switch (std->req.bRequest) { 1829 case UR_CLEAR_FEATURE: 1830 goto tr_valid; 1831 case UR_SET_DESCRIPTOR: 1832 case UR_SET_FEATURE: 1833 break; 1834 default: 1835 goto tr_stalled; 1836 } 1837 break; 1838 1839 case UT_WRITE_CLASS_OTHER: 1840 switch (std->req.bRequest) { 1841 case UR_CLEAR_FEATURE: 1842 goto tr_handle_clear_port_feature; 1843 case UR_SET_FEATURE: 1844 goto tr_handle_set_port_feature; 1845 case UR_CLEAR_TT_BUFFER: 1846 case UR_RESET_TT: 1847 case UR_STOP_TT: 1848 goto tr_valid; 1849 1850 default: 1851 goto tr_stalled; 1852 } 1853 break; 1854 1855 case UT_READ_CLASS_OTHER: 1856 switch (std->req.bRequest) { 1857 case UR_GET_TT_STATE: 1858 goto tr_handle_get_tt_state; 1859 case UR_GET_STATUS: 1860 goto tr_handle_get_port_status; 1861 default: 1862 goto tr_stalled; 1863 } 1864 break; 1865 1866 case UT_READ_CLASS_DEVICE: 1867 switch (std->req.bRequest) { 1868 case UR_GET_DESCRIPTOR: 1869 goto tr_handle_get_class_descriptor; 1870 case UR_GET_STATUS: 1871 goto tr_handle_get_class_status; 1872 1873 default: 1874 goto tr_stalled; 1875 } 1876 break; 1877 default: 1878 goto tr_stalled; 1879 } 1880 goto tr_valid; 1881 1882tr_handle_get_descriptor: 1883 switch (value >> 8) { 1884 case UDESC_DEVICE: 1885 if (value & 0xff) { 1886 goto tr_stalled; 1887 } 1888 std->len = sizeof(atmegadci_devd); 1889 std->ptr = USB_ADD_BYTES(&atmegadci_devd, 0); 1890 goto tr_valid; 1891 case UDESC_CONFIG: 1892 if (value & 0xff) { 1893 goto tr_stalled; 1894 } 1895 std->len = sizeof(atmegadci_confd); 1896 std->ptr = USB_ADD_BYTES(&atmegadci_confd, 0); 1897 goto tr_valid; 1898 case UDESC_STRING: 1899 switch (value & 0xff) { 1900 case 0: /* Language table */ 1901 std->len = sizeof(atmegadci_langtab); 1902 std->ptr = USB_ADD_BYTES(&atmegadci_langtab, 0); 1903 goto tr_valid; 1904 1905 case 1: /* Vendor */ 1906 std->len = sizeof(atmegadci_vendor); 1907 std->ptr = USB_ADD_BYTES(&atmegadci_vendor, 0); 1908 goto tr_valid; 1909 1910 case 2: /* Product */ 1911 std->len = sizeof(atmegadci_product); 1912 std->ptr = USB_ADD_BYTES(&atmegadci_product, 0); 1913 goto tr_valid; 1914 default: 1915 break; 1916 } 1917 break; 1918 default: 1919 goto tr_stalled; 1920 } 1921 goto tr_stalled; 1922 1923tr_handle_get_config: 1924 std->len = 1; 1925 sc->sc_hub_temp.wValue[0] = sc->sc_conf; 1926 goto tr_valid; 1927 1928tr_handle_get_status: 1929 std->len = 2; 1930 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED); 1931 goto tr_valid; 1932 1933tr_handle_set_address: 1934 if (value & 0xFF00) { 1935 goto tr_stalled; 1936 } 1937 sc->sc_rt_addr = value; 1938 goto tr_valid; 1939 1940tr_handle_set_config: 1941 if (value >= 2) { 1942 goto tr_stalled; 1943 } 1944 sc->sc_conf = value; 1945 goto tr_valid; 1946 1947tr_handle_get_interface: 1948 std->len = 1; 1949 sc->sc_hub_temp.wValue[0] = 0; 1950 goto tr_valid; 1951 1952tr_handle_get_tt_state: 1953tr_handle_get_class_status: 1954tr_handle_get_iface_status: 1955tr_handle_get_ep_status: 1956 std->len = 2; 1957 USETW(sc->sc_hub_temp.wValue, 0); 1958 goto tr_valid; 1959 1960tr_handle_set_halt: 1961tr_handle_set_interface: 1962tr_handle_set_wakeup: 1963tr_handle_clear_wakeup: 1964tr_handle_clear_halt: 1965 goto tr_valid; 1966 1967tr_handle_clear_port_feature: 1968 if (index != 1) { 1969 goto tr_stalled; 1970 } 1971 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index); 1972 1973 switch (value) { 1974 case UHF_PORT_SUSPEND: 1975 atmegadci_wakeup_peer(xfer); 1976 break; 1977 1978 case UHF_PORT_ENABLE: 1979 sc->sc_flags.port_enabled = 0; 1980 break; 1981 1982 case UHF_PORT_TEST: 1983 case UHF_PORT_INDICATOR: 1984 case UHF_C_PORT_ENABLE: 1985 case UHF_C_PORT_OVER_CURRENT: 1986 case UHF_C_PORT_RESET: 1987 /* nops */ 1988 break; 1989 case UHF_PORT_POWER: 1990 sc->sc_flags.port_powered = 0; 1991 atmegadci_pull_down(sc); 1992 atmegadci_clocks_off(sc); 1993 break; 1994 case UHF_C_PORT_CONNECTION: 1995 /* clear connect change flag */ 1996 sc->sc_flags.change_connect = 0; 1997 1998 /* configure the control endpoint */ 1999 2000 /* select endpoint number */ 2001 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, 0); 2002 2003 /* set endpoint reset */ 2004 ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(0)); 2005 2006 /* clear endpoint reset */ 2007 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0); 2008 2009 /* enable and stall endpoint */ 2010 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 2011 ATMEGA_UECONX_EPEN | 2012 ATMEGA_UECONX_STALLRQ); 2013 2014 /* one bank, 64-bytes wMaxPacket */ 2015 ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, 2016 ATMEGA_UECFG0X_EPTYPE0); 2017 ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X, 2018 ATMEGA_UECFG1X_ALLOC | 2019 ATMEGA_UECFG1X_EPBK0 | 2020 ATMEGA_UECFG1X_EPSIZE(3)); 2021 2022 /* check valid config */ 2023 temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X); 2024 if (!(temp & ATMEGA_UESTA0X_CFGOK)) { 2025 DPRINTFN(0, "Chip rejected EP0 configuration\n"); 2026 } 2027 break; 2028 case UHF_C_PORT_SUSPEND: 2029 sc->sc_flags.change_suspend = 0; 2030 break; 2031 default: 2032 std->err = USB_ERR_IOERROR; 2033 goto done; 2034 } 2035 goto tr_valid; 2036 2037tr_handle_set_port_feature: 2038 if (index != 1) { 2039 goto tr_stalled; 2040 } 2041 DPRINTFN(9, "UR_SET_PORT_FEATURE\n"); 2042 2043 switch (value) { 2044 case UHF_PORT_ENABLE: 2045 sc->sc_flags.port_enabled = 1; 2046 break; 2047 case UHF_PORT_SUSPEND: 2048 case UHF_PORT_RESET: 2049 case UHF_PORT_TEST: 2050 case UHF_PORT_INDICATOR: 2051 /* nops */ 2052 break; 2053 case UHF_PORT_POWER: 2054 sc->sc_flags.port_powered = 1; 2055 break; 2056 default: 2057 std->err = USB_ERR_IOERROR; 2058 goto done; 2059 } 2060 goto tr_valid; 2061 2062tr_handle_get_port_status: 2063 2064 DPRINTFN(9, "UR_GET_PORT_STATUS\n"); 2065 2066 if (index != 1) { 2067 goto tr_stalled; 2068 } 2069 if (sc->sc_flags.status_vbus) { 2070 atmegadci_clocks_on(sc); 2071 atmegadci_pull_up(sc); 2072 } else { 2073 atmegadci_pull_down(sc); 2074 atmegadci_clocks_off(sc); 2075 } 2076 2077 /* Select FULL-speed and Device Side Mode */ 2078 2079 value = UPS_PORT_MODE_DEVICE; 2080 2081 if (sc->sc_flags.port_powered) { 2082 value |= UPS_PORT_POWER; 2083 } 2084 if (sc->sc_flags.port_enabled) { 2085 value |= UPS_PORT_ENABLED; 2086 } 2087 if (sc->sc_flags.status_vbus && 2088 sc->sc_flags.status_bus_reset) { 2089 value |= UPS_CURRENT_CONNECT_STATUS; 2090 } 2091 if (sc->sc_flags.status_suspend) { 2092 value |= UPS_SUSPEND; 2093 } 2094 USETW(sc->sc_hub_temp.ps.wPortStatus, value); 2095 2096 value = 0; 2097 2098 if (sc->sc_flags.change_connect) { 2099 value |= UPS_C_CONNECT_STATUS; 2100 } 2101 if (sc->sc_flags.change_suspend) { 2102 value |= UPS_C_SUSPEND; 2103 } 2104 USETW(sc->sc_hub_temp.ps.wPortChange, value); 2105 std->len = sizeof(sc->sc_hub_temp.ps); 2106 goto tr_valid; 2107 2108tr_handle_get_class_descriptor: 2109 if (value & 0xFF) { 2110 goto tr_stalled; 2111 } 2112 std->ptr = USB_ADD_BYTES(&atmegadci_hubd, 0); 2113 std->len = sizeof(atmegadci_hubd); 2114 goto tr_valid; 2115 2116tr_stalled: 2117 std->err = USB_ERR_STALLED; 2118tr_valid: 2119done: 2120 return; 2121} 2122 2123static void 2124atmegadci_root_ctrl_poll(struct atmegadci_softc *sc) 2125{ 2126 usb2_sw_transfer(&sc->sc_root_ctrl, 2127 &atmegadci_root_ctrl_done); 2128} 2129 2130struct usb2_pipe_methods atmegadci_root_ctrl_methods = 2131{ 2132 .open = atmegadci_root_ctrl_open, 2133 .close = atmegadci_root_ctrl_close, 2134 .enter = atmegadci_root_ctrl_enter, 2135 .start = atmegadci_root_ctrl_start, 2136 .enter_is_cancelable = 1, 2137 .start_is_cancelable = 0, 2138}; 2139 2140/*------------------------------------------------------------------------* 2141 * at91dci root interrupt support 2142 *------------------------------------------------------------------------*/ 2143static void 2144atmegadci_root_intr_open(struct usb2_xfer *xfer) 2145{ 2146 return; 2147} 2148 2149static void 2150atmegadci_root_intr_close(struct usb2_xfer *xfer) 2151{ 2152 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 2153 2154 if (sc->sc_root_intr.xfer == xfer) { 2155 sc->sc_root_intr.xfer = NULL; 2156 } 2157 atmegadci_device_done(xfer, USB_ERR_CANCELLED); 2158} 2159 2160static void 2161atmegadci_root_intr_enter(struct usb2_xfer *xfer) 2162{ 2163 return; 2164} 2165 2166static void 2167atmegadci_root_intr_start(struct usb2_xfer *xfer) 2168{ 2169 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus); 2170 2171 sc->sc_root_intr.xfer = xfer; 2172} 2173 2174struct usb2_pipe_methods atmegadci_root_intr_methods = 2175{ 2176 .open = atmegadci_root_intr_open, 2177 .close = atmegadci_root_intr_close, 2178 .enter = atmegadci_root_intr_enter, 2179 .start = atmegadci_root_intr_start, 2180 .enter_is_cancelable = 1, 2181 .start_is_cancelable = 1, 2182}; 2183 2184static void 2185atmegadci_xfer_setup(struct usb2_setup_params *parm) 2186{ 2187 const struct usb2_hw_ep_profile *pf; 2188 struct atmegadci_softc *sc; 2189 struct usb2_xfer *xfer; 2190 void *last_obj; 2191 uint32_t ntd; 2192 uint32_t n; 2193 uint8_t ep_no; 2194 2195 sc = ATMEGA_BUS2SC(parm->udev->bus); 2196 xfer = parm->curr_xfer; 2197 2198 /* 2199 * NOTE: This driver does not use any of the parameters that 2200 * are computed from the following values. Just set some 2201 * reasonable dummies: 2202 */ 2203 parm->hc_max_packet_size = 0x500; 2204 parm->hc_max_packet_count = 1; 2205 parm->hc_max_frame_size = 0x500; 2206 2207 usb2_transfer_setup_sub(parm); 2208 2209 /* 2210 * compute maximum number of TDs 2211 */ 2212 if (parm->methods == &atmegadci_device_ctrl_methods) { 2213 2214 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */ 2215 + 1 /* SYNC 2 */ ; 2216 2217 } else if (parm->methods == &atmegadci_device_bulk_methods) { 2218 2219 ntd = xfer->nframes + 1 /* SYNC */ ; 2220 2221 } else if (parm->methods == &atmegadci_device_intr_methods) { 2222 2223 ntd = xfer->nframes + 1 /* SYNC */ ; 2224 2225 } else if (parm->methods == &atmegadci_device_isoc_fs_methods) { 2226 2227 ntd = xfer->nframes + 1 /* SYNC */ ; 2228 2229 } else { 2230 2231 ntd = 0; 2232 } 2233 2234 /* 2235 * check if "usb2_transfer_setup_sub" set an error 2236 */ 2237 if (parm->err) { 2238 return; 2239 } 2240 /* 2241 * allocate transfer descriptors 2242 */ 2243 last_obj = NULL; 2244 2245 /* 2246 * get profile stuff 2247 */ 2248 if (ntd) { 2249 2250 ep_no = xfer->endpoint & UE_ADDR; 2251 atmegadci_get_hw_ep_profile(parm->udev, &pf, ep_no); 2252 2253 if (pf == NULL) { 2254 /* should not happen */ 2255 parm->err = USB_ERR_INVAL; 2256 return; 2257 } 2258 } else { 2259 ep_no = 0; 2260 pf = NULL; 2261 } 2262 2263 /* align data */ 2264 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 2265 2266 for (n = 0; n != ntd; n++) { 2267 2268 struct atmegadci_td *td; 2269 2270 if (parm->buf) { 2271 2272 td = USB_ADD_BYTES(parm->buf, parm->size[0]); 2273 2274 /* init TD */ 2275 td->max_packet_size = xfer->max_packet_size; 2276 td->ep_no = ep_no; 2277 if (pf->support_multi_buffer) { 2278 td->support_multi_buffer = 1; 2279 } 2280 td->obj_next = last_obj; 2281 2282 last_obj = td; 2283 } 2284 parm->size[0] += sizeof(*td); 2285 } 2286 2287 xfer->td_start[0] = last_obj; 2288} 2289 2290static void 2291atmegadci_xfer_unsetup(struct usb2_xfer *xfer) 2292{ 2293 return; 2294} 2295 2296static void 2297atmegadci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc, 2298 struct usb2_pipe *pipe) 2299{ 2300 struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus); 2301 2302 DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n", 2303 pipe, udev->address, 2304 edesc->bEndpointAddress, udev->flags.usb2_mode, 2305 sc->sc_rt_addr, udev->device_index); 2306 2307 if (udev->device_index == sc->sc_rt_addr) { 2308 2309 if (udev->flags.usb2_mode != USB_MODE_HOST) { 2310 /* not supported */ 2311 return; 2312 } 2313 switch (edesc->bEndpointAddress) { 2314 case USB_CONTROL_ENDPOINT: 2315 pipe->methods = &atmegadci_root_ctrl_methods; 2316 break; 2317 case UE_DIR_IN | ATMEGA_INTR_ENDPT: 2318 pipe->methods = &atmegadci_root_intr_methods; 2319 break; 2320 default: 2321 /* do nothing */ 2322 break; 2323 } 2324 } else { 2325 2326 if (udev->flags.usb2_mode != USB_MODE_DEVICE) { 2327 /* not supported */ 2328 return; 2329 } 2330 if (udev->speed != USB_SPEED_FULL) { 2331 /* not supported */ 2332 return; 2333 } 2334 switch (edesc->bmAttributes & UE_XFERTYPE) { 2335 case UE_CONTROL: 2336 pipe->methods = &atmegadci_device_ctrl_methods; 2337 break; 2338 case UE_INTERRUPT: 2339 pipe->methods = &atmegadci_device_intr_methods; 2340 break; 2341 case UE_ISOCHRONOUS: 2342 pipe->methods = &atmegadci_device_isoc_fs_methods; 2343 break; 2344 case UE_BULK: 2345 pipe->methods = &atmegadci_device_bulk_methods; 2346 break; 2347 default: 2348 /* do nothing */ 2349 break; 2350 } 2351 } 2352} 2353 2354struct usb2_bus_methods atmegadci_bus_methods = 2355{ 2356 .pipe_init = &atmegadci_pipe_init, 2357 .xfer_setup = &atmegadci_xfer_setup, 2358 .xfer_unsetup = &atmegadci_xfer_unsetup, 2359 .get_hw_ep_profile = &atmegadci_get_hw_ep_profile, 2360 .set_stall = &atmegadci_set_stall, 2361 .clear_stall = &atmegadci_clear_stall, 2362 .roothub_exec = &atmegadci_root_ctrl_task, 2363}; 2364