xref: /freebsd/sys/dev/usb/controller/atmegadci.c (revision f8d2b1f3f72086bd169f1a0890b6faba8434a01a)
1d2b99310SHans Petter Selasky /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
3718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4718cf2ccSPedro F. Giffuni  *
502ac6454SAndrew Thompson  * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson /*
30fde87526SAndrew Thompson  * This file contains the driver for the ATMEGA series USB OTG Controller. This
31fde87526SAndrew Thompson  * driver currently only supports the DCI mode of the USB hardware.
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson /*
3502ac6454SAndrew Thompson  * NOTE: When the chip detects BUS-reset it will also reset the
3602ac6454SAndrew Thompson  * endpoints, Function-address and more.
3702ac6454SAndrew Thompson  */
3802ac6454SAndrew Thompson 
39d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
40d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
41d2b99310SHans Petter Selasky #else
42ed6d949aSAndrew Thompson #include <sys/stdint.h>
43ed6d949aSAndrew Thompson #include <sys/stddef.h>
44ed6d949aSAndrew Thompson #include <sys/param.h>
45ed6d949aSAndrew Thompson #include <sys/queue.h>
46ed6d949aSAndrew Thompson #include <sys/types.h>
47ed6d949aSAndrew Thompson #include <sys/systm.h>
48ed6d949aSAndrew Thompson #include <sys/kernel.h>
49ed6d949aSAndrew Thompson #include <sys/bus.h>
50ed6d949aSAndrew Thompson #include <sys/module.h>
51ed6d949aSAndrew Thompson #include <sys/lock.h>
52ed6d949aSAndrew Thompson #include <sys/mutex.h>
53ed6d949aSAndrew Thompson #include <sys/condvar.h>
54ed6d949aSAndrew Thompson #include <sys/sysctl.h>
55ed6d949aSAndrew Thompson #include <sys/sx.h>
56ed6d949aSAndrew Thompson #include <sys/unistd.h>
57ed6d949aSAndrew Thompson #include <sys/callout.h>
58ed6d949aSAndrew Thompson #include <sys/malloc.h>
59ed6d949aSAndrew Thompson #include <sys/priv.h>
60ed6d949aSAndrew Thompson 
6102ac6454SAndrew Thompson #include <dev/usb/usb.h>
62ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #define	USB_DEBUG_VAR atmegadci_debug
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
7302ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
77d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
78d2b99310SHans Petter Selasky 
7902ac6454SAndrew Thompson #include <dev/usb/controller/atmegadci.h>
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson #define	ATMEGA_BUS2SC(bus) \
8202ac6454SAndrew Thompson    ((struct atmegadci_softc *)(((uint8_t *)(bus)) - \
83578d0effSAndrew Thompson     ((uint8_t *)&(((struct atmegadci_softc *)0)->sc_bus))))
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson #define	ATMEGA_PC2SC(pc) \
86bdc081c6SAndrew Thompson    ATMEGA_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
8702ac6454SAndrew Thompson 
88ed6d949aSAndrew Thompson #ifdef USB_DEBUG
8902ac6454SAndrew Thompson static int atmegadci_debug = 0;
9002ac6454SAndrew Thompson 
91*f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, atmegadci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
926472ac3dSEd Schouten     "USB ATMEGA DCI");
93ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_atmegadci, OID_AUTO, debug, CTLFLAG_RWTUN,
9402ac6454SAndrew Thompson     &atmegadci_debug, 0, "ATMEGA DCI debug level");
9502ac6454SAndrew Thompson #endif
9602ac6454SAndrew Thompson 
9702ac6454SAndrew Thompson #define	ATMEGA_INTR_ENDPT 1
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson /* prototypes */
10002ac6454SAndrew Thompson 
101e892b3feSHans Petter Selasky static const struct usb_bus_methods atmegadci_bus_methods;
102e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_non_isoc_methods;
103e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods;
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_setup_rx;
10602ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_rx;
10702ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_tx;
10802ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_tx_sync;
109e0a69b51SAndrew Thompson static void atmegadci_device_done(struct usb_xfer *, usb_error_t);
110760bc48eSAndrew Thompson static void atmegadci_do_poll(struct usb_bus *);
111760bc48eSAndrew Thompson static void atmegadci_standard_done(struct usb_xfer *);
11239307315SAndrew Thompson static void atmegadci_root_intr(struct atmegadci_softc *sc);
11302ac6454SAndrew Thompson 
11402ac6454SAndrew Thompson /*
11502ac6454SAndrew Thompson  * Here is a list of what the chip supports:
11602ac6454SAndrew Thompson  */
117760bc48eSAndrew Thompson static const struct usb_hw_ep_profile
11802ac6454SAndrew Thompson 	atmegadci_ep_profile[2] = {
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson 	[0] = {
12102ac6454SAndrew Thompson 		.max_in_frame_size = 64,
12202ac6454SAndrew Thompson 		.max_out_frame_size = 64,
12302ac6454SAndrew Thompson 		.is_simplex = 1,
12402ac6454SAndrew Thompson 		.support_control = 1,
12502ac6454SAndrew Thompson 	},
12602ac6454SAndrew Thompson 	[1] = {
12702ac6454SAndrew Thompson 		.max_in_frame_size = 64,
12802ac6454SAndrew Thompson 		.max_out_frame_size = 64,
12902ac6454SAndrew Thompson 		.is_simplex = 1,
13002ac6454SAndrew Thompson 		.support_bulk = 1,
13102ac6454SAndrew Thompson 		.support_interrupt = 1,
13202ac6454SAndrew Thompson 		.support_isochronous = 1,
13302ac6454SAndrew Thompson 		.support_in = 1,
13402ac6454SAndrew Thompson 		.support_out = 1,
13502ac6454SAndrew Thompson 	},
13602ac6454SAndrew Thompson };
13702ac6454SAndrew Thompson 
13802ac6454SAndrew Thompson static void
139760bc48eSAndrew Thompson atmegadci_get_hw_ep_profile(struct usb_device *udev,
140760bc48eSAndrew Thompson     const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
14102ac6454SAndrew Thompson {
14202ac6454SAndrew Thompson 	if (ep_addr == 0)
14302ac6454SAndrew Thompson 		*ppf = atmegadci_ep_profile;
14402ac6454SAndrew Thompson 	else if (ep_addr < ATMEGA_EP_MAX)
14502ac6454SAndrew Thompson 		*ppf = atmegadci_ep_profile + 1;
14602ac6454SAndrew Thompson 	else
14702ac6454SAndrew Thompson 		*ppf = NULL;
14802ac6454SAndrew Thompson }
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson static void
15102ac6454SAndrew Thompson atmegadci_clocks_on(struct atmegadci_softc *sc)
15202ac6454SAndrew Thompson {
15302ac6454SAndrew Thompson 	if (sc->sc_flags.clocks_off &&
15402ac6454SAndrew Thompson 	    sc->sc_flags.port_powered) {
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson 		DPRINTFN(5, "\n");
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 		/* turn on clocks */
15902ac6454SAndrew Thompson 		(sc->sc_clocks_on) (&sc->sc_bus);
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
16202ac6454SAndrew Thompson 		    ATMEGA_USBCON_USBE |
16302ac6454SAndrew Thompson 		    ATMEGA_USBCON_OTGPADE |
16402ac6454SAndrew Thompson 		    ATMEGA_USBCON_VBUSTE);
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson 		sc->sc_flags.clocks_off = 0;
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson 		/* enable transceiver ? */
16902ac6454SAndrew Thompson 	}
17002ac6454SAndrew Thompson }
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson static void
17302ac6454SAndrew Thompson atmegadci_clocks_off(struct atmegadci_softc *sc)
17402ac6454SAndrew Thompson {
17502ac6454SAndrew Thompson 	if (!sc->sc_flags.clocks_off) {
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson 		DPRINTFN(5, "\n");
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson 		/* disable Transceiver ? */
18002ac6454SAndrew Thompson 
18102ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
18202ac6454SAndrew Thompson 		    ATMEGA_USBCON_USBE |
18302ac6454SAndrew Thompson 		    ATMEGA_USBCON_OTGPADE |
18402ac6454SAndrew Thompson 		    ATMEGA_USBCON_FRZCLK |
18502ac6454SAndrew Thompson 		    ATMEGA_USBCON_VBUSTE);
18602ac6454SAndrew Thompson 
18702ac6454SAndrew Thompson 		/* turn clocks off */
18802ac6454SAndrew Thompson 		(sc->sc_clocks_off) (&sc->sc_bus);
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson 		sc->sc_flags.clocks_off = 1;
19102ac6454SAndrew Thompson 	}
19202ac6454SAndrew Thompson }
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson static void
19502ac6454SAndrew Thompson atmegadci_pull_up(struct atmegadci_softc *sc)
19602ac6454SAndrew Thompson {
19702ac6454SAndrew Thompson 	/* pullup D+, if possible */
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	if (!sc->sc_flags.d_pulled_up &&
20002ac6454SAndrew Thompson 	    sc->sc_flags.port_powered) {
20102ac6454SAndrew Thompson 		sc->sc_flags.d_pulled_up = 1;
20202ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDCON, 0);
20302ac6454SAndrew Thompson 	}
20402ac6454SAndrew Thompson }
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson static void
20702ac6454SAndrew Thompson atmegadci_pull_down(struct atmegadci_softc *sc)
20802ac6454SAndrew Thompson {
20902ac6454SAndrew Thompson 	/* pulldown D+, if possible */
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	if (sc->sc_flags.d_pulled_up) {
21202ac6454SAndrew Thompson 		sc->sc_flags.d_pulled_up = 0;
21302ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
21402ac6454SAndrew Thompson 	}
21502ac6454SAndrew Thompson }
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson static void
21839307315SAndrew Thompson atmegadci_wakeup_peer(struct atmegadci_softc *sc)
21902ac6454SAndrew Thompson {
22002ac6454SAndrew Thompson 	uint8_t temp;
22102ac6454SAndrew Thompson 
22202ac6454SAndrew Thompson 	if (!sc->sc_flags.status_suspend) {
22302ac6454SAndrew Thompson 		return;
22402ac6454SAndrew Thompson 	}
22502ac6454SAndrew Thompson 
22602ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UDCON);
22702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDCON, temp | ATMEGA_UDCON_RMWKUP);
22802ac6454SAndrew Thompson 
22902ac6454SAndrew Thompson 	/* wait 8 milliseconds */
23002ac6454SAndrew Thompson 	/* Wait for reset to complete. */
231a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson 	/* hardware should have cleared RMWKUP bit */
23402ac6454SAndrew Thompson }
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson static void
23702ac6454SAndrew Thompson atmegadci_set_address(struct atmegadci_softc *sc, uint8_t addr)
23802ac6454SAndrew Thompson {
23902ac6454SAndrew Thompson 	DPRINTFN(5, "addr=%d\n", addr);
24002ac6454SAndrew Thompson 
24102ac6454SAndrew Thompson 	addr |= ATMEGA_UDADDR_ADDEN;
24202ac6454SAndrew Thompson 
24302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, addr);
24402ac6454SAndrew Thompson }
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson static uint8_t
24702ac6454SAndrew Thompson atmegadci_setup_rx(struct atmegadci_td *td)
24802ac6454SAndrew Thompson {
24902ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
250760bc48eSAndrew Thompson 	struct usb_device_request req;
25102ac6454SAndrew Thompson 	uint16_t count;
25202ac6454SAndrew Thompson 	uint8_t temp;
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson 	/* get pointer to softc */
25502ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
25602ac6454SAndrew Thompson 
25702ac6454SAndrew Thompson 	/* select endpoint number */
25802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 	/* check endpoint status */
26102ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson 	DPRINTFN(5, "UEINTX=0x%02x\n", temp);
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson 	if (!(temp & ATMEGA_UEINTX_RXSTPI)) {
26602ac6454SAndrew Thompson 		goto not_complete;
26702ac6454SAndrew Thompson 	}
268f3464815SAndrew Thompson 	/* clear did stall */
269f3464815SAndrew Thompson 	td->did_stall = 0;
27002ac6454SAndrew Thompson 	/* get the packet byte count */
27102ac6454SAndrew Thompson 	count =
27202ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
27302ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
27402ac6454SAndrew Thompson 
27502ac6454SAndrew Thompson 	/* mask away undefined bits */
27602ac6454SAndrew Thompson 	count &= 0x7FF;
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson 	/* verify data length */
27902ac6454SAndrew Thompson 	if (count != td->remainder) {
28002ac6454SAndrew Thompson 		DPRINTFN(0, "Invalid SETUP packet "
28102ac6454SAndrew Thompson 		    "length, %d bytes\n", count);
28202ac6454SAndrew Thompson 		goto not_complete;
28302ac6454SAndrew Thompson 	}
28402ac6454SAndrew Thompson 	if (count != sizeof(req)) {
28502ac6454SAndrew Thompson 		DPRINTFN(0, "Unsupported SETUP packet "
28602ac6454SAndrew Thompson 		    "length, %d bytes\n", count);
28702ac6454SAndrew Thompson 		goto not_complete;
28802ac6454SAndrew Thompson 	}
28902ac6454SAndrew Thompson 	/* receive data */
29002ac6454SAndrew Thompson 	ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
29102ac6454SAndrew Thompson 	    (void *)&req, sizeof(req));
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	/* copy data into real buffer */
294a593f6b8SAndrew Thompson 	usbd_copy_in(td->pc, 0, &req, sizeof(req));
29502ac6454SAndrew Thompson 
29602ac6454SAndrew Thompson 	td->offset = sizeof(req);
29702ac6454SAndrew Thompson 	td->remainder = 0;
29802ac6454SAndrew Thompson 
29902ac6454SAndrew Thompson 	/* sneak peek the set address */
30002ac6454SAndrew Thompson 	if ((req.bmRequestType == UT_WRITE_DEVICE) &&
30102ac6454SAndrew Thompson 	    (req.bRequest == UR_SET_ADDRESS)) {
30202ac6454SAndrew Thompson 		sc->sc_dv_addr = req.wValue[0] & 0x7F;
303d59dbd0aSAndrew Thompson 		/* must write address before ZLP */
304d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, sc->sc_dv_addr);
30502ac6454SAndrew Thompson 	} else {
30602ac6454SAndrew Thompson 		sc->sc_dv_addr = 0xFF;
30702ac6454SAndrew Thompson 	}
30802ac6454SAndrew Thompson 
30933ea1323SAndrew Thompson 	/* Clear SETUP packet interrupt and all other previous interrupts */
31033ea1323SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0);
31102ac6454SAndrew Thompson 	return (0);			/* complete */
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson not_complete:
314f3464815SAndrew Thompson 	/* abort any ongoing transfer */
315f3464815SAndrew Thompson 	if (!td->did_stall) {
316f3464815SAndrew Thompson 		DPRINTFN(5, "stalling\n");
317f3464815SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
318f3464815SAndrew Thompson 		    ATMEGA_UECONX_EPEN |
319f3464815SAndrew Thompson 		    ATMEGA_UECONX_STALLRQ);
320f3464815SAndrew Thompson 		td->did_stall = 1;
321f3464815SAndrew Thompson 	}
322eb846b4eSAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
323eb846b4eSAndrew Thompson 		/* clear SETUP packet interrupt */
324eb846b4eSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ~ATMEGA_UEINTX_RXSTPI);
325eb846b4eSAndrew Thompson 	}
32602ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet */
32702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, ATMEGA_UEIENX_RXSTPE);
32802ac6454SAndrew Thompson 	return (1);			/* not complete */
32902ac6454SAndrew Thompson }
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson static uint8_t
33202ac6454SAndrew Thompson atmegadci_data_rx(struct atmegadci_td *td)
33302ac6454SAndrew Thompson {
33402ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
335760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
33602ac6454SAndrew Thompson 	uint16_t count;
33702ac6454SAndrew Thompson 	uint8_t temp;
33802ac6454SAndrew Thompson 	uint8_t to;
33902ac6454SAndrew Thompson 	uint8_t got_short;
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	to = 3;				/* don't loop forever! */
34202ac6454SAndrew Thompson 	got_short = 0;
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	/* get pointer to softc */
34502ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 	/* select endpoint number */
34802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson repeat:
35102ac6454SAndrew Thompson 	/* check if any of the FIFO banks have data */
35202ac6454SAndrew Thompson 	/* check endpoint status */
35302ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
35802ac6454SAndrew Thompson 		if (td->remainder == 0) {
35902ac6454SAndrew Thompson 			/*
36002ac6454SAndrew Thompson 			 * We are actually complete and have
36102ac6454SAndrew Thompson 			 * received the next SETUP
36202ac6454SAndrew Thompson 			 */
36302ac6454SAndrew Thompson 			DPRINTFN(5, "faking complete\n");
36402ac6454SAndrew Thompson 			return (0);	/* complete */
36502ac6454SAndrew Thompson 		}
36602ac6454SAndrew Thompson 		/*
36702ac6454SAndrew Thompson 	         * USB Host Aborted the transfer.
36802ac6454SAndrew Thompson 	         */
36902ac6454SAndrew Thompson 		td->error = 1;
37002ac6454SAndrew Thompson 		return (0);		/* complete */
37102ac6454SAndrew Thompson 	}
37202ac6454SAndrew Thompson 	/* check status */
37302ac6454SAndrew Thompson 	if (!(temp & (ATMEGA_UEINTX_FIFOCON |
37402ac6454SAndrew Thompson 	    ATMEGA_UEINTX_RXOUTI))) {
37502ac6454SAndrew Thompson 		/* no data */
37602ac6454SAndrew Thompson 		goto not_complete;
37702ac6454SAndrew Thompson 	}
37802ac6454SAndrew Thompson 	/* get the packet byte count */
37902ac6454SAndrew Thompson 	count =
38002ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
38102ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 	/* mask away undefined bits */
38402ac6454SAndrew Thompson 	count &= 0x7FF;
38502ac6454SAndrew Thompson 
38602ac6454SAndrew Thompson 	/* verify the packet byte count */
38702ac6454SAndrew Thompson 	if (count != td->max_packet_size) {
38802ac6454SAndrew Thompson 		if (count < td->max_packet_size) {
38902ac6454SAndrew Thompson 			/* we have a short packet */
39002ac6454SAndrew Thompson 			td->short_pkt = 1;
39102ac6454SAndrew Thompson 			got_short = 1;
39202ac6454SAndrew Thompson 		} else {
39302ac6454SAndrew Thompson 			/* invalid USB packet */
39402ac6454SAndrew Thompson 			td->error = 1;
39502ac6454SAndrew Thompson 			return (0);	/* we are complete */
39602ac6454SAndrew Thompson 		}
39702ac6454SAndrew Thompson 	}
39802ac6454SAndrew Thompson 	/* verify the packet byte count */
39902ac6454SAndrew Thompson 	if (count > td->remainder) {
40002ac6454SAndrew Thompson 		/* invalid USB packet */
40102ac6454SAndrew Thompson 		td->error = 1;
40202ac6454SAndrew Thompson 		return (0);		/* we are complete */
40302ac6454SAndrew Thompson 	}
40402ac6454SAndrew Thompson 	while (count > 0) {
405a593f6b8SAndrew Thompson 		usbd_get_page(td->pc, td->offset, &buf_res);
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 		/* get correct length */
40802ac6454SAndrew Thompson 		if (buf_res.length > count) {
40902ac6454SAndrew Thompson 			buf_res.length = count;
41002ac6454SAndrew Thompson 		}
41102ac6454SAndrew Thompson 		/* receive data */
41202ac6454SAndrew Thompson 		ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
41302ac6454SAndrew Thompson 		    buf_res.buffer, buf_res.length);
41402ac6454SAndrew Thompson 
41502ac6454SAndrew Thompson 		/* update counters */
41602ac6454SAndrew Thompson 		count -= buf_res.length;
41702ac6454SAndrew Thompson 		td->offset += buf_res.length;
41802ac6454SAndrew Thompson 		td->remainder -= buf_res.length;
41902ac6454SAndrew Thompson 	}
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson 	/* clear OUT packet interrupt */
42202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_RXOUTI ^ 0xFF);
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 	/* release FIFO bank */
42502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_FIFOCON ^ 0xFF);
42602ac6454SAndrew Thompson 
42702ac6454SAndrew Thompson 	/* check if we are complete */
42802ac6454SAndrew Thompson 	if ((td->remainder == 0) || got_short) {
42902ac6454SAndrew Thompson 		if (td->short_pkt) {
43002ac6454SAndrew Thompson 			/* we are complete */
43102ac6454SAndrew Thompson 			return (0);
43202ac6454SAndrew Thompson 		}
43302ac6454SAndrew Thompson 		/* else need to receive a zero length packet */
43402ac6454SAndrew Thompson 	}
43502ac6454SAndrew Thompson 	if (--to) {
43602ac6454SAndrew Thompson 		goto repeat;
43702ac6454SAndrew Thompson 	}
43802ac6454SAndrew Thompson not_complete:
43902ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or OUT packet */
44002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
44102ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_RXOUTE);
44202ac6454SAndrew Thompson 	return (1);			/* not complete */
44302ac6454SAndrew Thompson }
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson static uint8_t
44602ac6454SAndrew Thompson atmegadci_data_tx(struct atmegadci_td *td)
44702ac6454SAndrew Thompson {
44802ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
449760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
45002ac6454SAndrew Thompson 	uint16_t count;
45102ac6454SAndrew Thompson 	uint8_t to;
45202ac6454SAndrew Thompson 	uint8_t temp;
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	to = 3;				/* don't loop forever! */
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson 	/* get pointer to softc */
45702ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	/* select endpoint number */
46002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson repeat:
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	/* check endpoint status */
46502ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
46602ac6454SAndrew Thompson 
46702ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
47002ac6454SAndrew Thompson 		/*
47102ac6454SAndrew Thompson 	         * The current transfer was aborted
47202ac6454SAndrew Thompson 	         * by the USB Host
47302ac6454SAndrew Thompson 	         */
47402ac6454SAndrew Thompson 		td->error = 1;
47502ac6454SAndrew Thompson 		return (0);		/* complete */
47602ac6454SAndrew Thompson 	}
477c1911c1bSAndrew Thompson 
478c1911c1bSAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
479c1911c1bSAndrew Thompson 	if (temp & 3) {
480c1911c1bSAndrew Thompson 		/* cannot write any data - a bank is busy */
48102ac6454SAndrew Thompson 		goto not_complete;
48202ac6454SAndrew Thompson 	}
483c1911c1bSAndrew Thompson 
48402ac6454SAndrew Thompson 	count = td->max_packet_size;
48502ac6454SAndrew Thompson 	if (td->remainder < count) {
48602ac6454SAndrew Thompson 		/* we have a short packet */
48702ac6454SAndrew Thompson 		td->short_pkt = 1;
48802ac6454SAndrew Thompson 		count = td->remainder;
48902ac6454SAndrew Thompson 	}
49002ac6454SAndrew Thompson 	while (count > 0) {
49102ac6454SAndrew Thompson 
492a593f6b8SAndrew Thompson 		usbd_get_page(td->pc, td->offset, &buf_res);
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson 		/* get correct length */
49502ac6454SAndrew Thompson 		if (buf_res.length > count) {
49602ac6454SAndrew Thompson 			buf_res.length = count;
49702ac6454SAndrew Thompson 		}
49802ac6454SAndrew Thompson 		/* transmit data */
49902ac6454SAndrew Thompson 		ATMEGA_WRITE_MULTI_1(sc, ATMEGA_UEDATX,
50002ac6454SAndrew Thompson 		    buf_res.buffer, buf_res.length);
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson 		/* update counters */
50302ac6454SAndrew Thompson 		count -= buf_res.length;
50402ac6454SAndrew Thompson 		td->offset += buf_res.length;
50502ac6454SAndrew Thompson 		td->remainder -= buf_res.length;
50602ac6454SAndrew Thompson 	}
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	/* clear IN packet interrupt */
50902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_TXINI);
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 	/* allocate FIFO bank */
51202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_FIFOCON);
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	/* check remainder */
51502ac6454SAndrew Thompson 	if (td->remainder == 0) {
51602ac6454SAndrew Thompson 		if (td->short_pkt) {
51702ac6454SAndrew Thompson 			return (0);	/* complete */
51802ac6454SAndrew Thompson 		}
51902ac6454SAndrew Thompson 		/* else we need to transmit a short packet */
52002ac6454SAndrew Thompson 	}
52102ac6454SAndrew Thompson 	if (--to) {
52202ac6454SAndrew Thompson 		goto repeat;
52302ac6454SAndrew Thompson 	}
52402ac6454SAndrew Thompson not_complete:
52502ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or free IN packet */
52602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
52702ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
52802ac6454SAndrew Thompson 	return (1);			/* not complete */
52902ac6454SAndrew Thompson }
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson static uint8_t
53202ac6454SAndrew Thompson atmegadci_data_tx_sync(struct atmegadci_td *td)
53302ac6454SAndrew Thompson {
53402ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
53502ac6454SAndrew Thompson 	uint8_t temp;
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	/* get pointer to softc */
53802ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 	/* select endpoint number */
54102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	/* check endpoint status */
54402ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x\n", temp);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
54902ac6454SAndrew Thompson 		DPRINTFN(5, "faking complete\n");
55002ac6454SAndrew Thompson 		/* Race condition */
55102ac6454SAndrew Thompson 		return (0);		/* complete */
55202ac6454SAndrew Thompson 	}
55302ac6454SAndrew Thompson 	/*
55402ac6454SAndrew Thompson 	 * The control endpoint has only got one bank, so if that bank
55502ac6454SAndrew Thompson 	 * is free the packet has been transferred!
55602ac6454SAndrew Thompson 	 */
557c1911c1bSAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
558c1911c1bSAndrew Thompson 	if (temp & 3) {
559c1911c1bSAndrew Thompson 		/* cannot write any data - a bank is busy */
56002ac6454SAndrew Thompson 		goto not_complete;
56102ac6454SAndrew Thompson 	}
56202ac6454SAndrew Thompson 	if (sc->sc_dv_addr != 0xFF) {
56302ac6454SAndrew Thompson 		/* set new address */
56402ac6454SAndrew Thompson 		atmegadci_set_address(sc, sc->sc_dv_addr);
56502ac6454SAndrew Thompson 	}
56602ac6454SAndrew Thompson 	return (0);			/* complete */
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson not_complete:
56902ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or free IN packet */
57002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
57102ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
57202ac6454SAndrew Thompson 	return (1);			/* not complete */
57302ac6454SAndrew Thompson }
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson static uint8_t
576760bc48eSAndrew Thompson atmegadci_xfer_do_fifo(struct usb_xfer *xfer)
57702ac6454SAndrew Thompson {
57802ac6454SAndrew Thompson 	struct atmegadci_td *td;
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
58302ac6454SAndrew Thompson 	while (1) {
58402ac6454SAndrew Thompson 		if ((td->func) (td)) {
58502ac6454SAndrew Thompson 			/* operation in progress */
58602ac6454SAndrew Thompson 			break;
58702ac6454SAndrew Thompson 		}
58802ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
58902ac6454SAndrew Thompson 			goto done;
59002ac6454SAndrew Thompson 		}
59102ac6454SAndrew Thompson 		if (td->error) {
59202ac6454SAndrew Thompson 			goto done;
59302ac6454SAndrew Thompson 		} else if (td->remainder > 0) {
59402ac6454SAndrew Thompson 			/*
59502ac6454SAndrew Thompson 			 * We had a short transfer. If there is no alternate
59602ac6454SAndrew Thompson 			 * next, stop processing !
59702ac6454SAndrew Thompson 			 */
59802ac6454SAndrew Thompson 			if (!td->alt_next) {
59902ac6454SAndrew Thompson 				goto done;
60002ac6454SAndrew Thompson 			}
60102ac6454SAndrew Thompson 		}
60202ac6454SAndrew Thompson 		/*
60302ac6454SAndrew Thompson 		 * Fetch the next transfer descriptor and transfer
60402ac6454SAndrew Thompson 		 * some flags to the next transfer descriptor
60502ac6454SAndrew Thompson 		 */
60602ac6454SAndrew Thompson 		td = td->obj_next;
60702ac6454SAndrew Thompson 		xfer->td_transfer_cache = td;
60802ac6454SAndrew Thompson 	}
60902ac6454SAndrew Thompson 	return (1);			/* not complete */
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson done:
61202ac6454SAndrew Thompson 	/* compute all actual lengths */
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	atmegadci_standard_done(xfer);
61502ac6454SAndrew Thompson 	return (0);			/* complete */
61602ac6454SAndrew Thompson }
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson static void
61902ac6454SAndrew Thompson atmegadci_interrupt_poll(struct atmegadci_softc *sc)
62002ac6454SAndrew Thompson {
621760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson repeat:
62402ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
62502ac6454SAndrew Thompson 		if (!atmegadci_xfer_do_fifo(xfer)) {
62602ac6454SAndrew Thompson 			/* queue has been modified */
62702ac6454SAndrew Thompson 			goto repeat;
62802ac6454SAndrew Thompson 		}
62902ac6454SAndrew Thompson 	}
63002ac6454SAndrew Thompson }
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson static void
63302ac6454SAndrew Thompson atmegadci_vbus_interrupt(struct atmegadci_softc *sc, uint8_t is_on)
63402ac6454SAndrew Thompson {
63502ac6454SAndrew Thompson 	DPRINTFN(5, "vbus = %u\n", is_on);
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	if (is_on) {
63802ac6454SAndrew Thompson 		if (!sc->sc_flags.status_vbus) {
63902ac6454SAndrew Thompson 			sc->sc_flags.status_vbus = 1;
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
64202ac6454SAndrew Thompson 
64339307315SAndrew Thompson 			atmegadci_root_intr(sc);
64402ac6454SAndrew Thompson 		}
64502ac6454SAndrew Thompson 	} else {
64602ac6454SAndrew Thompson 		if (sc->sc_flags.status_vbus) {
64702ac6454SAndrew Thompson 			sc->sc_flags.status_vbus = 0;
64802ac6454SAndrew Thompson 			sc->sc_flags.status_bus_reset = 0;
64902ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 0;
65002ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 0;
65102ac6454SAndrew Thompson 			sc->sc_flags.change_connect = 1;
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
65402ac6454SAndrew Thompson 
65539307315SAndrew Thompson 			atmegadci_root_intr(sc);
65602ac6454SAndrew Thompson 		}
65702ac6454SAndrew Thompson 	}
65802ac6454SAndrew Thompson }
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson void
66102ac6454SAndrew Thompson atmegadci_interrupt(struct atmegadci_softc *sc)
66202ac6454SAndrew Thompson {
66302ac6454SAndrew Thompson 	uint8_t status;
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	/* read interrupt status */
66802ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_UDINT);
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	/* clear all set interrupts */
671df075012SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDINT, (~status) & 0x7D);
67202ac6454SAndrew Thompson 
673d59dbd0aSAndrew Thompson 	DPRINTFN(14, "UDINT=0x%02x\n", status);
674d59dbd0aSAndrew Thompson 
67502ac6454SAndrew Thompson 	/* check for any bus state change interrupts */
67602ac6454SAndrew Thompson 	if (status & ATMEGA_UDINT_EORSTI) {
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson 		DPRINTFN(5, "end of reset\n");
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson 		/* set correct state */
68102ac6454SAndrew Thompson 		sc->sc_flags.status_bus_reset = 1;
68202ac6454SAndrew Thompson 		sc->sc_flags.status_suspend = 0;
68302ac6454SAndrew Thompson 		sc->sc_flags.change_suspend = 0;
68402ac6454SAndrew Thompson 		sc->sc_flags.change_connect = 1;
68502ac6454SAndrew Thompson 
68602ac6454SAndrew Thompson 		/* disable resume interrupt */
68702ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
68802ac6454SAndrew Thompson 		    ATMEGA_UDINT_SUSPE |
68902ac6454SAndrew Thompson 		    ATMEGA_UDINT_EORSTE);
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 		/* complete root HUB interrupt endpoint */
69239307315SAndrew Thompson 		atmegadci_root_intr(sc);
69302ac6454SAndrew Thompson 	}
69402ac6454SAndrew Thompson 	/*
69502ac6454SAndrew Thompson 	 * If resume and suspend is set at the same time we interpret
69602ac6454SAndrew Thompson 	 * that like RESUME. Resume is set when there is at least 3
69702ac6454SAndrew Thompson 	 * milliseconds of inactivity on the USB BUS.
69802ac6454SAndrew Thompson 	 */
6997eaa41aeSAndrew Thompson 	if (status & ATMEGA_UDINT_WAKEUPI) {
70002ac6454SAndrew Thompson 
70102ac6454SAndrew Thompson 		DPRINTFN(5, "resume interrupt\n");
70202ac6454SAndrew Thompson 
70302ac6454SAndrew Thompson 		if (sc->sc_flags.status_suspend) {
70402ac6454SAndrew Thompson 			/* update status bits */
70502ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 0;
70602ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 1;
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 			/* disable resume interrupt */
70902ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
71002ac6454SAndrew Thompson 			    ATMEGA_UDINT_SUSPE |
71102ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSTE);
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
71439307315SAndrew Thompson 			atmegadci_root_intr(sc);
71502ac6454SAndrew Thompson 		}
71602ac6454SAndrew Thompson 	} else if (status & ATMEGA_UDINT_SUSPI) {
71702ac6454SAndrew Thompson 
71802ac6454SAndrew Thompson 		DPRINTFN(5, "suspend interrupt\n");
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson 		if (!sc->sc_flags.status_suspend) {
72102ac6454SAndrew Thompson 			/* update status bits */
72202ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 1;
72302ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 1;
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 			/* disable suspend interrupt */
72602ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
7277eaa41aeSAndrew Thompson 			    ATMEGA_UDINT_WAKEUPE |
72802ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSTE);
72902ac6454SAndrew Thompson 
73002ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
73139307315SAndrew Thompson 			atmegadci_root_intr(sc);
73202ac6454SAndrew Thompson 		}
73302ac6454SAndrew Thompson 	}
73402ac6454SAndrew Thompson 	/* check VBUS */
73502ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_USBINT);
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 	/* clear all set interrupts */
738df075012SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBINT, (~status) & 0x03);
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 	if (status & ATMEGA_USBINT_VBUSTI) {
74102ac6454SAndrew Thompson 		uint8_t temp;
74202ac6454SAndrew Thompson 
743d59dbd0aSAndrew Thompson 		DPRINTFN(5, "USBINT=0x%02x\n", status);
744d59dbd0aSAndrew Thompson 
74502ac6454SAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
74602ac6454SAndrew Thompson 		atmegadci_vbus_interrupt(sc, temp & ATMEGA_USBSTA_VBUS);
74702ac6454SAndrew Thompson 	}
74802ac6454SAndrew Thompson 	/* check for any endpoint interrupts */
74902ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_UEINT);
750df075012SAndrew Thompson 	/* the hardware will clear the UEINT bits automatically */
75102ac6454SAndrew Thompson 	if (status) {
75202ac6454SAndrew Thompson 
753d59dbd0aSAndrew Thompson 		DPRINTFN(5, "real endpoint interrupt UEINT=0x%02x\n", status);
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 		atmegadci_interrupt_poll(sc);
75602ac6454SAndrew Thompson 	}
75702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
75802ac6454SAndrew Thompson }
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson static void
76102ac6454SAndrew Thompson atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp *temp)
76202ac6454SAndrew Thompson {
76302ac6454SAndrew Thompson 	struct atmegadci_td *td;
76402ac6454SAndrew Thompson 
76502ac6454SAndrew Thompson 	/* get current Transfer Descriptor */
76602ac6454SAndrew Thompson 	td = temp->td_next;
76702ac6454SAndrew Thompson 	temp->td = td;
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson 	/* prepare for next TD */
77002ac6454SAndrew Thompson 	temp->td_next = td->obj_next;
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 	/* fill out the Transfer Descriptor */
77302ac6454SAndrew Thompson 	td->func = temp->func;
77402ac6454SAndrew Thompson 	td->pc = temp->pc;
77502ac6454SAndrew Thompson 	td->offset = temp->offset;
77602ac6454SAndrew Thompson 	td->remainder = temp->len;
77702ac6454SAndrew Thompson 	td->error = 0;
778476183dfSAndrew Thompson 	td->did_stall = temp->did_stall;
77902ac6454SAndrew Thompson 	td->short_pkt = temp->short_pkt;
78002ac6454SAndrew Thompson 	td->alt_next = temp->setup_alt_next;
78102ac6454SAndrew Thompson }
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson static void
784760bc48eSAndrew Thompson atmegadci_setup_standard_chain(struct usb_xfer *xfer)
78502ac6454SAndrew Thompson {
78602ac6454SAndrew Thompson 	struct atmegadci_std_temp temp;
78702ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
78802ac6454SAndrew Thompson 	struct atmegadci_td *td;
78902ac6454SAndrew Thompson 	uint32_t x;
79002ac6454SAndrew Thompson 	uint8_t ep_no;
79102ac6454SAndrew Thompson 	uint8_t need_sync;
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
794ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
795a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson 	td = xfer->td_start[0];
80002ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
80102ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 	/* setup temp */
80402ac6454SAndrew Thompson 
80578c94708SAndrew Thompson 	temp.pc = NULL;
80602ac6454SAndrew Thompson 	temp.td = NULL;
80702ac6454SAndrew Thompson 	temp.td_next = xfer->td_start[0];
80802ac6454SAndrew Thompson 	temp.offset = 0;
80940ba168dSHans Petter Selasky 	temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
81040ba168dSHans Petter Selasky 	    xfer->flags_int.isochronous_xfr;
811476183dfSAndrew Thompson 	temp.did_stall = !xfer->flags_int.control_stall;
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(xfer->xroot->bus);
814ae60fdfbSAndrew Thompson 	ep_no = (xfer->endpointno & UE_ADDR);
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
81902ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson 			temp.func = &atmegadci_setup_rx;
82202ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
82302ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
82402ac6454SAndrew Thompson 			temp.short_pkt = temp.len ? 1 : 0;
8250f7d4548SAndrew Thompson 			/* check for last frame */
8260f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
8270f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
8280f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act)
8290f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
8300f7d4548SAndrew Thompson 			}
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
83302ac6454SAndrew Thompson 		}
83402ac6454SAndrew Thompson 		x = 1;
83502ac6454SAndrew Thompson 	} else {
83602ac6454SAndrew Thompson 		x = 0;
83702ac6454SAndrew Thompson 	}
83802ac6454SAndrew Thompson 
83902ac6454SAndrew Thompson 	if (x != xfer->nframes) {
840ae60fdfbSAndrew Thompson 		if (xfer->endpointno & UE_DIR_IN) {
84102ac6454SAndrew Thompson 			temp.func = &atmegadci_data_tx;
84202ac6454SAndrew Thompson 			need_sync = 1;
84302ac6454SAndrew Thompson 		} else {
84402ac6454SAndrew Thompson 			temp.func = &atmegadci_data_rx;
84502ac6454SAndrew Thompson 			need_sync = 0;
84602ac6454SAndrew Thompson 		}
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 		/* setup "pc" pointer */
84902ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
85002ac6454SAndrew Thompson 	} else {
85102ac6454SAndrew Thompson 		need_sync = 0;
85202ac6454SAndrew Thompson 	}
85302ac6454SAndrew Thompson 	while (x != xfer->nframes) {
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
85602ac6454SAndrew Thompson 
85702ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson 		x++;
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson 		if (x == xfer->nframes) {
8620f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
8630f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
86402ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
86502ac6454SAndrew Thompson 				}
8660f7d4548SAndrew Thompson 			} else {
8670f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
8680f7d4548SAndrew Thompson 			}
8690f7d4548SAndrew Thompson 		}
87002ac6454SAndrew Thompson 		if (temp.len == 0) {
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 			temp.short_pkt = 0;
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 		} else {
87702ac6454SAndrew Thompson 
87802ac6454SAndrew Thompson 			/* regular data transfer */
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson 			temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
88102ac6454SAndrew Thompson 		}
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 		atmegadci_setup_standard_chain_sub(&temp);
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 		if (xfer->flags_int.isochronous_xfr) {
88602ac6454SAndrew Thompson 			temp.offset += temp.len;
88702ac6454SAndrew Thompson 		} else {
88802ac6454SAndrew Thompson 			/* get next Page Cache pointer */
88902ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + x;
89002ac6454SAndrew Thompson 		}
89102ac6454SAndrew Thompson 	}
89202ac6454SAndrew Thompson 
8930f7d4548SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
8940f7d4548SAndrew Thompson 
89502ac6454SAndrew Thompson 		/* always setup a valid "pc" pointer for status and sync */
89602ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + 0;
89702ac6454SAndrew Thompson 		temp.len = 0;
89802ac6454SAndrew Thompson 		temp.short_pkt = 0;
8990f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
90002ac6454SAndrew Thompson 
9010f7d4548SAndrew Thompson 		/* check if we need to sync */
9020f7d4548SAndrew Thompson 		if (need_sync) {
9030f7d4548SAndrew Thompson 			/* we need a SYNC point after TX */
9040f7d4548SAndrew Thompson 			temp.func = &atmegadci_data_tx_sync;
90502ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
90602ac6454SAndrew Thompson 		}
9070f7d4548SAndrew Thompson 
90802ac6454SAndrew Thompson 		/* check if we should append a status stage */
9090f7d4548SAndrew Thompson 		if (!xfer->flags_int.control_act) {
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson 			/*
91202ac6454SAndrew Thompson 			 * Send a DATA1 message and invert the current
91302ac6454SAndrew Thompson 			 * endpoint direction.
91402ac6454SAndrew Thompson 			 */
915ae60fdfbSAndrew Thompson 			if (xfer->endpointno & UE_DIR_IN) {
91602ac6454SAndrew Thompson 				temp.func = &atmegadci_data_rx;
91702ac6454SAndrew Thompson 				need_sync = 0;
91802ac6454SAndrew Thompson 			} else {
91902ac6454SAndrew Thompson 				temp.func = &atmegadci_data_tx;
92002ac6454SAndrew Thompson 				need_sync = 1;
92102ac6454SAndrew Thompson 			}
92202ac6454SAndrew Thompson 
92302ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
92402ac6454SAndrew Thompson 			if (need_sync) {
92502ac6454SAndrew Thompson 				/* we need a SYNC point after TX */
92602ac6454SAndrew Thompson 				temp.func = &atmegadci_data_tx_sync;
92702ac6454SAndrew Thompson 				atmegadci_setup_standard_chain_sub(&temp);
92802ac6454SAndrew Thompson 			}
92902ac6454SAndrew Thompson 		}
9300f7d4548SAndrew Thompson 	}
93102ac6454SAndrew Thompson 	/* must have at least one frame! */
93202ac6454SAndrew Thompson 	td = temp.td;
93302ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
93402ac6454SAndrew Thompson }
93502ac6454SAndrew Thompson 
93602ac6454SAndrew Thompson static void
93702ac6454SAndrew Thompson atmegadci_timeout(void *arg)
93802ac6454SAndrew Thompson {
939760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	/* transfer is transferred */
94602ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_TIMEOUT);
94702ac6454SAndrew Thompson }
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson static void
950760bc48eSAndrew Thompson atmegadci_start_standard_chain(struct usb_xfer *xfer)
95102ac6454SAndrew Thompson {
95202ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
95302ac6454SAndrew Thompson 
95402ac6454SAndrew Thompson 	/* poll one time - will turn on interrupts */
95502ac6454SAndrew Thompson 	if (atmegadci_xfer_do_fifo(xfer)) {
95602ac6454SAndrew Thompson 
95702ac6454SAndrew Thompson 		/* put transfer on interrupt queue */
958a593f6b8SAndrew Thompson 		usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
95902ac6454SAndrew Thompson 
96002ac6454SAndrew Thompson 		/* start timeout, if any */
96102ac6454SAndrew Thompson 		if (xfer->timeout != 0) {
962a593f6b8SAndrew Thompson 			usbd_transfer_timeout_ms(xfer,
96302ac6454SAndrew Thompson 			    &atmegadci_timeout, xfer->timeout);
96402ac6454SAndrew Thompson 		}
96502ac6454SAndrew Thompson 	}
96602ac6454SAndrew Thompson }
96702ac6454SAndrew Thompson 
96802ac6454SAndrew Thompson static void
96939307315SAndrew Thompson atmegadci_root_intr(struct atmegadci_softc *sc)
97002ac6454SAndrew Thompson {
97102ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
97202ac6454SAndrew Thompson 
97302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson 	/* set port bit */
97602ac6454SAndrew Thompson 	sc->sc_hub_idata[0] = 0x02;	/* we only have one port */
97702ac6454SAndrew Thompson 
97839307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
97939307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
98002ac6454SAndrew Thompson  }
98102ac6454SAndrew Thompson 
982e0a69b51SAndrew Thompson static usb_error_t
983760bc48eSAndrew Thompson atmegadci_standard_done_sub(struct usb_xfer *xfer)
98402ac6454SAndrew Thompson {
98502ac6454SAndrew Thompson 	struct atmegadci_td *td;
98602ac6454SAndrew Thompson 	uint32_t len;
98702ac6454SAndrew Thompson 	uint8_t error;
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
99202ac6454SAndrew Thompson 
99302ac6454SAndrew Thompson 	do {
99402ac6454SAndrew Thompson 		len = td->remainder;
99502ac6454SAndrew Thompson 
99602ac6454SAndrew Thompson 		if (xfer->aframes != xfer->nframes) {
99702ac6454SAndrew Thompson 			/*
99802ac6454SAndrew Thompson 		         * Verify the length and subtract
99902ac6454SAndrew Thompson 		         * the remainder from "frlengths[]":
100002ac6454SAndrew Thompson 		         */
100102ac6454SAndrew Thompson 			if (len > xfer->frlengths[xfer->aframes]) {
100202ac6454SAndrew Thompson 				td->error = 1;
100302ac6454SAndrew Thompson 			} else {
100402ac6454SAndrew Thompson 				xfer->frlengths[xfer->aframes] -= len;
100502ac6454SAndrew Thompson 			}
100602ac6454SAndrew Thompson 		}
100702ac6454SAndrew Thompson 		/* Check for transfer error */
100802ac6454SAndrew Thompson 		if (td->error) {
100902ac6454SAndrew Thompson 			/* the transfer is finished */
101002ac6454SAndrew Thompson 			error = 1;
101102ac6454SAndrew Thompson 			td = NULL;
101202ac6454SAndrew Thompson 			break;
101302ac6454SAndrew Thompson 		}
101402ac6454SAndrew Thompson 		/* Check for short transfer */
101502ac6454SAndrew Thompson 		if (len > 0) {
101640ba168dSHans Petter Selasky 			if (xfer->flags_int.short_frames_ok ||
101740ba168dSHans Petter Selasky 			    xfer->flags_int.isochronous_xfr) {
101802ac6454SAndrew Thompson 				/* follow alt next */
101902ac6454SAndrew Thompson 				if (td->alt_next) {
102002ac6454SAndrew Thompson 					td = td->obj_next;
102102ac6454SAndrew Thompson 				} else {
102202ac6454SAndrew Thompson 					td = NULL;
102302ac6454SAndrew Thompson 				}
102402ac6454SAndrew Thompson 			} else {
102502ac6454SAndrew Thompson 				/* the transfer is finished */
102602ac6454SAndrew Thompson 				td = NULL;
102702ac6454SAndrew Thompson 			}
102802ac6454SAndrew Thompson 			error = 0;
102902ac6454SAndrew Thompson 			break;
103002ac6454SAndrew Thompson 		}
103102ac6454SAndrew Thompson 		td = td->obj_next;
103202ac6454SAndrew Thompson 
103302ac6454SAndrew Thompson 		/* this USB frame is complete */
103402ac6454SAndrew Thompson 		error = 0;
103502ac6454SAndrew Thompson 		break;
103602ac6454SAndrew Thompson 
103702ac6454SAndrew Thompson 	} while (0);
103802ac6454SAndrew Thompson 
103902ac6454SAndrew Thompson 	/* update transfer cache */
104002ac6454SAndrew Thompson 
104102ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
104202ac6454SAndrew Thompson 
104302ac6454SAndrew Thompson 	return (error ?
104402ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
104502ac6454SAndrew Thompson }
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson static void
1048760bc48eSAndrew Thompson atmegadci_standard_done(struct usb_xfer *xfer)
104902ac6454SAndrew Thompson {
1050e0a69b51SAndrew Thompson 	usb_error_t err = 0;
105102ac6454SAndrew Thompson 
1052ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1053ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 	/* reset scanner */
105602ac6454SAndrew Thompson 
105702ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
105802ac6454SAndrew Thompson 
105902ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
106002ac6454SAndrew Thompson 
106102ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
106202ac6454SAndrew Thompson 
106302ac6454SAndrew Thompson 			err = atmegadci_standard_done_sub(xfer);
106402ac6454SAndrew Thompson 		}
106502ac6454SAndrew Thompson 		xfer->aframes = 1;
106602ac6454SAndrew Thompson 
106702ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
106802ac6454SAndrew Thompson 			goto done;
106902ac6454SAndrew Thompson 		}
107002ac6454SAndrew Thompson 	}
107102ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
107202ac6454SAndrew Thompson 
107302ac6454SAndrew Thompson 		err = atmegadci_standard_done_sub(xfer);
107402ac6454SAndrew Thompson 		xfer->aframes++;
107502ac6454SAndrew Thompson 
107602ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
107702ac6454SAndrew Thompson 			goto done;
107802ac6454SAndrew Thompson 		}
107902ac6454SAndrew Thompson 	}
108002ac6454SAndrew Thompson 
108102ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
108202ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
108302ac6454SAndrew Thompson 
108402ac6454SAndrew Thompson 		err = atmegadci_standard_done_sub(xfer);
108502ac6454SAndrew Thompson 	}
108602ac6454SAndrew Thompson done:
108702ac6454SAndrew Thompson 	atmegadci_device_done(xfer, err);
108802ac6454SAndrew Thompson }
108902ac6454SAndrew Thompson 
109002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
109102ac6454SAndrew Thompson  *	atmegadci_device_done
109202ac6454SAndrew Thompson  *
109302ac6454SAndrew Thompson  * NOTE: this function can be called more than one time on the
109402ac6454SAndrew Thompson  * same USB transfer!
109502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
109602ac6454SAndrew Thompson static void
1097e0a69b51SAndrew Thompson atmegadci_device_done(struct usb_xfer *xfer, usb_error_t error)
109802ac6454SAndrew Thompson {
109902ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
110002ac6454SAndrew Thompson 	uint8_t ep_no;
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
110302ac6454SAndrew Thompson 
1104ae60fdfbSAndrew Thompson 	DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
1105ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
110602ac6454SAndrew Thompson 
1107f29a0724SAndrew Thompson 	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1108ae60fdfbSAndrew Thompson 		ep_no = (xfer->endpointno & UE_ADDR);
110902ac6454SAndrew Thompson 
111002ac6454SAndrew Thompson 		/* select endpoint number */
111102ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson 		/* disable endpoint interrupt */
111402ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
111502ac6454SAndrew Thompson 
111602ac6454SAndrew Thompson 		DPRINTFN(15, "disabled interrupts!\n");
111702ac6454SAndrew Thompson 	}
111802ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
1119a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
112002ac6454SAndrew Thompson }
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson static void
1123a5cf1aaaSHans Petter Selasky atmegadci_xfer_stall(struct usb_xfer *xfer)
1124a5cf1aaaSHans Petter Selasky {
1125a5cf1aaaSHans Petter Selasky 	atmegadci_device_done(xfer, USB_ERR_STALLED);
1126a5cf1aaaSHans Petter Selasky }
1127a5cf1aaaSHans Petter Selasky 
1128a5cf1aaaSHans Petter Selasky static void
1129a5cf1aaaSHans Petter Selasky atmegadci_set_stall(struct usb_device *udev,
113029bd7d7eSAndrew Thompson     struct usb_endpoint *ep, uint8_t *did_stall)
113102ac6454SAndrew Thompson {
113202ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
113302ac6454SAndrew Thompson 	uint8_t ep_no;
113402ac6454SAndrew Thompson 
113502ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
113602ac6454SAndrew Thompson 
1137ae60fdfbSAndrew Thompson 	DPRINTFN(5, "endpoint=%p\n", ep);
113802ac6454SAndrew Thompson 
113902ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(udev->bus);
114002ac6454SAndrew Thompson 	/* get endpoint number */
1141ae60fdfbSAndrew Thompson 	ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
114202ac6454SAndrew Thompson 	/* select endpoint number */
114302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
114402ac6454SAndrew Thompson 	/* set stall */
114502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
114602ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
114702ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQ);
114802ac6454SAndrew Thompson }
114902ac6454SAndrew Thompson 
115002ac6454SAndrew Thompson static void
115102ac6454SAndrew Thompson atmegadci_clear_stall_sub(struct atmegadci_softc *sc, uint8_t ep_no,
115202ac6454SAndrew Thompson     uint8_t ep_type, uint8_t ep_dir)
115302ac6454SAndrew Thompson {
115402ac6454SAndrew Thompson 	uint8_t temp;
115502ac6454SAndrew Thompson 
115602ac6454SAndrew Thompson 	if (ep_type == UE_CONTROL) {
115702ac6454SAndrew Thompson 		/* clearing stall is not needed */
115802ac6454SAndrew Thompson 		return;
115902ac6454SAndrew Thompson 	}
116002ac6454SAndrew Thompson 	/* select endpoint number */
116102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 	/* set endpoint reset */
116402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(ep_no));
116502ac6454SAndrew Thompson 
116602ac6454SAndrew Thompson 	/* clear endpoint reset */
116702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
116802ac6454SAndrew Thompson 
116902ac6454SAndrew Thompson 	/* set stall */
117002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
117102ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
117202ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQ);
117302ac6454SAndrew Thompson 
117402ac6454SAndrew Thompson 	/* reset data toggle */
117502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
117602ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
117702ac6454SAndrew Thompson 	    ATMEGA_UECONX_RSTDT);
117802ac6454SAndrew Thompson 
117902ac6454SAndrew Thompson 	/* clear stall */
118002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
118102ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
118202ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQC);
118302ac6454SAndrew Thompson 
1184d59dbd0aSAndrew Thompson 	do {
118502ac6454SAndrew Thompson 		if (ep_type == UE_BULK) {
11867eaa41aeSAndrew Thompson 			temp = ATMEGA_UECFG0X_EPTYPE2;
118702ac6454SAndrew Thompson 		} else if (ep_type == UE_INTERRUPT) {
11887eaa41aeSAndrew Thompson 			temp = ATMEGA_UECFG0X_EPTYPE3;
118902ac6454SAndrew Thompson 		} else {
11907eaa41aeSAndrew Thompson 			temp = ATMEGA_UECFG0X_EPTYPE1;
119102ac6454SAndrew Thompson 		}
119202ac6454SAndrew Thompson 		if (ep_dir & UE_DIR_IN) {
119302ac6454SAndrew Thompson 			temp |= ATMEGA_UECFG0X_EPDIR;
119402ac6454SAndrew Thompson 		}
119502ac6454SAndrew Thompson 		/* two banks, 64-bytes wMaxPacket */
119602ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, temp);
119702ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
119802ac6454SAndrew Thompson 		    ATMEGA_UECFG1X_ALLOC |
1199c1911c1bSAndrew Thompson 		    ATMEGA_UECFG1X_EPBK0 |	/* one bank */
1200d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPSIZE(3));
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
120302ac6454SAndrew Thompson 		if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
1204767cb2e2SAndrew Thompson 			device_printf(sc->sc_bus.bdev,
1205767cb2e2SAndrew Thompson 			    "Chip rejected configuration\n");
120602ac6454SAndrew Thompson 		}
1207d59dbd0aSAndrew Thompson 	} while (0);
120802ac6454SAndrew Thompson }
120902ac6454SAndrew Thompson 
121002ac6454SAndrew Thompson static void
1211ae60fdfbSAndrew Thompson atmegadci_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
121202ac6454SAndrew Thompson {
121302ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
1214760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
121502ac6454SAndrew Thompson 
1216ae60fdfbSAndrew Thompson 	DPRINTFN(5, "endpoint=%p\n", ep);
121702ac6454SAndrew Thompson 
121802ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
121902ac6454SAndrew Thompson 
122002ac6454SAndrew Thompson 	/* check mode */
1221f29a0724SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_DEVICE) {
122202ac6454SAndrew Thompson 		/* not supported */
122302ac6454SAndrew Thompson 		return;
122402ac6454SAndrew Thompson 	}
122502ac6454SAndrew Thompson 	/* get softc */
122602ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(udev->bus);
122702ac6454SAndrew Thompson 
122802ac6454SAndrew Thompson 	/* get endpoint descriptor */
1229ae60fdfbSAndrew Thompson 	ed = ep->edesc;
123002ac6454SAndrew Thompson 
123102ac6454SAndrew Thompson 	/* reset endpoint */
123202ac6454SAndrew Thompson 	atmegadci_clear_stall_sub(sc,
123302ac6454SAndrew Thompson 	    (ed->bEndpointAddress & UE_ADDR),
123402ac6454SAndrew Thompson 	    (ed->bmAttributes & UE_XFERTYPE),
123502ac6454SAndrew Thompson 	    (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
123602ac6454SAndrew Thompson }
123702ac6454SAndrew Thompson 
1238e0a69b51SAndrew Thompson usb_error_t
123902ac6454SAndrew Thompson atmegadci_init(struct atmegadci_softc *sc)
124002ac6454SAndrew Thompson {
124102ac6454SAndrew Thompson 	uint8_t n;
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson 	DPRINTF("start\n");
124402ac6454SAndrew Thompson 
124502ac6454SAndrew Thompson 	/* set up the bus structure */
124602ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_1_1;
124702ac6454SAndrew Thompson 	sc->sc_bus.methods = &atmegadci_bus_methods;
124802ac6454SAndrew Thompson 
124902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
12507eaa41aeSAndrew Thompson 
12517eaa41aeSAndrew Thompson 	/* make sure USB is enabled */
12527eaa41aeSAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
12537eaa41aeSAndrew Thompson 	    ATMEGA_USBCON_USBE |
12547eaa41aeSAndrew Thompson 	    ATMEGA_USBCON_FRZCLK);
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson 	/* enable USB PAD regulator */
125702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UHWCON,
12587eaa41aeSAndrew Thompson 	    ATMEGA_UHWCON_UVREGE |
12597eaa41aeSAndrew Thompson 	    ATMEGA_UHWCON_UIMOD);
12607eaa41aeSAndrew Thompson 
12617eaa41aeSAndrew Thompson 	/* the following register sets up the USB PLL, assuming 16MHz X-tal */
12627eaa41aeSAndrew Thompson 	ATMEGA_WRITE_1(sc, 0x49 /* PLLCSR */, 0x14 | 0x02);
12637eaa41aeSAndrew Thompson 
12647eaa41aeSAndrew Thompson 	/* wait for PLL to lock */
12657eaa41aeSAndrew Thompson 	for (n = 0; n != 20; n++) {
12667eaa41aeSAndrew Thompson 		if (ATMEGA_READ_1(sc, 0x49) & 0x01)
12677eaa41aeSAndrew Thompson 			break;
12687eaa41aeSAndrew Thompson 		/* wait a little bit for PLL to start */
1269a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
12707eaa41aeSAndrew Thompson 	}
12717eaa41aeSAndrew Thompson 
1272df075012SAndrew Thompson 	/* make sure USB is enabled */
1273df075012SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
1274df075012SAndrew Thompson 	    ATMEGA_USBCON_USBE |
1275df075012SAndrew Thompson 	    ATMEGA_USBCON_OTGPADE |
1276df075012SAndrew Thompson 	    ATMEGA_USBCON_VBUSTE);
1277df075012SAndrew Thompson 
127802ac6454SAndrew Thompson 	/* turn on clocks */
127902ac6454SAndrew Thompson 	(sc->sc_clocks_on) (&sc->sc_bus);
128002ac6454SAndrew Thompson 
1281d59dbd0aSAndrew Thompson 	/* make sure device is re-enumerated */
1282d59dbd0aSAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
1283d59dbd0aSAndrew Thompson 
128402ac6454SAndrew Thompson 	/* wait a little for things to stabilise */
1285a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
128602ac6454SAndrew Thompson 
128702ac6454SAndrew Thompson 	/* enable interrupts */
128802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
128902ac6454SAndrew Thompson 	    ATMEGA_UDINT_SUSPE |
129002ac6454SAndrew Thompson 	    ATMEGA_UDINT_EORSTE);
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 	/* reset all endpoints */
129302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST,
129402ac6454SAndrew Thompson 	    (1 << ATMEGA_EP_MAX) - 1);
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 	/* disable reset */
129702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
129802ac6454SAndrew Thompson 
129902ac6454SAndrew Thompson 	/* disable all endpoints */
1300d59dbd0aSAndrew Thompson 	for (n = 0; n != ATMEGA_EP_MAX; n++) {
130102ac6454SAndrew Thompson 
130202ac6454SAndrew Thompson 		/* select endpoint */
130302ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, n);
130402ac6454SAndrew Thompson 
130502ac6454SAndrew Thompson 		/* disable endpoint interrupt */
130602ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
130702ac6454SAndrew Thompson 
130802ac6454SAndrew Thompson 		/* disable endpoint */
130902ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 0);
131002ac6454SAndrew Thompson 	}
131102ac6454SAndrew Thompson 
131202ac6454SAndrew Thompson 	/* turn off clocks */
131302ac6454SAndrew Thompson 
131402ac6454SAndrew Thompson 	atmegadci_clocks_off(sc);
131502ac6454SAndrew Thompson 
1316d59dbd0aSAndrew Thompson 	/* read initial VBUS state */
1317d59dbd0aSAndrew Thompson 
1318d59dbd0aSAndrew Thompson 	n = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
1319d59dbd0aSAndrew Thompson 	atmegadci_vbus_interrupt(sc, n & ATMEGA_USBSTA_VBUS);
1320d59dbd0aSAndrew Thompson 
132102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 	/* catch any lost interrupts */
132402ac6454SAndrew Thompson 
132502ac6454SAndrew Thompson 	atmegadci_do_poll(&sc->sc_bus);
132602ac6454SAndrew Thompson 
132702ac6454SAndrew Thompson 	return (0);			/* success */
132802ac6454SAndrew Thompson }
132902ac6454SAndrew Thompson 
133002ac6454SAndrew Thompson void
133102ac6454SAndrew Thompson atmegadci_uninit(struct atmegadci_softc *sc)
133202ac6454SAndrew Thompson {
133302ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson 	/* turn on clocks */
133602ac6454SAndrew Thompson 	(sc->sc_clocks_on) (&sc->sc_bus);
133702ac6454SAndrew Thompson 
133802ac6454SAndrew Thompson 	/* disable interrupts */
133902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 0);
134002ac6454SAndrew Thompson 
134102ac6454SAndrew Thompson 	/* reset all endpoints */
134202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST,
134302ac6454SAndrew Thompson 	    (1 << ATMEGA_EP_MAX) - 1);
134402ac6454SAndrew Thompson 
134502ac6454SAndrew Thompson 	/* disable reset */
134602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
134702ac6454SAndrew Thompson 
134802ac6454SAndrew Thompson 	sc->sc_flags.port_powered = 0;
134902ac6454SAndrew Thompson 	sc->sc_flags.status_vbus = 0;
135002ac6454SAndrew Thompson 	sc->sc_flags.status_bus_reset = 0;
135102ac6454SAndrew Thompson 	sc->sc_flags.status_suspend = 0;
135202ac6454SAndrew Thompson 	sc->sc_flags.change_suspend = 0;
135302ac6454SAndrew Thompson 	sc->sc_flags.change_connect = 1;
135402ac6454SAndrew Thompson 
135502ac6454SAndrew Thompson 	atmegadci_pull_down(sc);
135602ac6454SAndrew Thompson 	atmegadci_clocks_off(sc);
135702ac6454SAndrew Thompson 
135802ac6454SAndrew Thompson 	/* disable USB PAD regulator */
135902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 0);
136002ac6454SAndrew Thompson 
136102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
136202ac6454SAndrew Thompson }
136302ac6454SAndrew Thompson 
13642e141748SHans Petter Selasky static void
136502ac6454SAndrew Thompson atmegadci_suspend(struct atmegadci_softc *sc)
136602ac6454SAndrew Thompson {
13672e141748SHans Petter Selasky 	/* TODO */
136802ac6454SAndrew Thompson }
136902ac6454SAndrew Thompson 
13702e141748SHans Petter Selasky static void
137102ac6454SAndrew Thompson atmegadci_resume(struct atmegadci_softc *sc)
137202ac6454SAndrew Thompson {
13732e141748SHans Petter Selasky 	/* TODO */
137402ac6454SAndrew Thompson }
137502ac6454SAndrew Thompson 
137602ac6454SAndrew Thompson static void
1377760bc48eSAndrew Thompson atmegadci_do_poll(struct usb_bus *bus)
137802ac6454SAndrew Thompson {
137902ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
138202ac6454SAndrew Thompson 	atmegadci_interrupt_poll(sc);
138302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
138402ac6454SAndrew Thompson }
138502ac6454SAndrew Thompson 
138602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
138740ba168dSHans Petter Selasky  * atmegadci bulk support
138840ba168dSHans Petter Selasky  * atmegadci control support
138940ba168dSHans Petter Selasky  * atmegadci interrupt support
139002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
139102ac6454SAndrew Thompson static void
1392760bc48eSAndrew Thompson atmegadci_device_non_isoc_open(struct usb_xfer *xfer)
139302ac6454SAndrew Thompson {
139402ac6454SAndrew Thompson 	return;
139502ac6454SAndrew Thompson }
139602ac6454SAndrew Thompson 
139702ac6454SAndrew Thompson static void
1398760bc48eSAndrew Thompson atmegadci_device_non_isoc_close(struct usb_xfer *xfer)
139902ac6454SAndrew Thompson {
140002ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
140102ac6454SAndrew Thompson }
140202ac6454SAndrew Thompson 
140302ac6454SAndrew Thompson static void
1404760bc48eSAndrew Thompson atmegadci_device_non_isoc_enter(struct usb_xfer *xfer)
140502ac6454SAndrew Thompson {
140602ac6454SAndrew Thompson 	return;
140702ac6454SAndrew Thompson }
140802ac6454SAndrew Thompson 
140902ac6454SAndrew Thompson static void
1410760bc48eSAndrew Thompson atmegadci_device_non_isoc_start(struct usb_xfer *xfer)
141102ac6454SAndrew Thompson {
141202ac6454SAndrew Thompson 	/* setup TDs */
141302ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
141402ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
141502ac6454SAndrew Thompson }
141602ac6454SAndrew Thompson 
1417e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_non_isoc_methods =
141802ac6454SAndrew Thompson {
14193b01d513SAndrew Thompson 	.open = atmegadci_device_non_isoc_open,
14203b01d513SAndrew Thompson 	.close = atmegadci_device_non_isoc_close,
14213b01d513SAndrew Thompson 	.enter = atmegadci_device_non_isoc_enter,
14223b01d513SAndrew Thompson 	.start = atmegadci_device_non_isoc_start,
142302ac6454SAndrew Thompson };
142402ac6454SAndrew Thompson 
142502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
142640ba168dSHans Petter Selasky  * atmegadci full speed isochronous support
142702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
142802ac6454SAndrew Thompson static void
1429760bc48eSAndrew Thompson atmegadci_device_isoc_fs_open(struct usb_xfer *xfer)
143002ac6454SAndrew Thompson {
143102ac6454SAndrew Thompson 	return;
143202ac6454SAndrew Thompson }
143302ac6454SAndrew Thompson 
143402ac6454SAndrew Thompson static void
1435760bc48eSAndrew Thompson atmegadci_device_isoc_fs_close(struct usb_xfer *xfer)
143602ac6454SAndrew Thompson {
143702ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
143802ac6454SAndrew Thompson }
143902ac6454SAndrew Thompson 
144002ac6454SAndrew Thompson static void
1441760bc48eSAndrew Thompson atmegadci_device_isoc_fs_enter(struct usb_xfer *xfer)
144202ac6454SAndrew Thompson {
144302ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
144402ac6454SAndrew Thompson 	uint32_t temp;
144502ac6454SAndrew Thompson 	uint32_t nframes;
144602ac6454SAndrew Thompson 
144702ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1448ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
144902ac6454SAndrew Thompson 
145002ac6454SAndrew Thompson 	/* get the current frame index */
145102ac6454SAndrew Thompson 
145202ac6454SAndrew Thompson 	nframes =
145302ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UDFNUMH) << 8) |
145402ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UDFNUML));
145502ac6454SAndrew Thompson 
145602ac6454SAndrew Thompson 	nframes &= ATMEGA_FRAME_MASK;
145702ac6454SAndrew Thompson 
145802ac6454SAndrew Thompson 	/*
145902ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
146002ac6454SAndrew Thompson 	 * will be inserted
146102ac6454SAndrew Thompson 	 */
1462ae60fdfbSAndrew Thompson 	temp = (nframes - xfer->endpoint->isoc_next) & ATMEGA_FRAME_MASK;
146302ac6454SAndrew Thompson 
1464ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->is_synced == 0) ||
146502ac6454SAndrew Thompson 	    (temp < xfer->nframes)) {
146602ac6454SAndrew Thompson 		/*
146702ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is
146802ac6454SAndrew Thompson 		 * empty we schedule the transfer a few frames ahead
146902ac6454SAndrew Thompson 		 * of the current frame position. Else two isochronous
147002ac6454SAndrew Thompson 		 * transfers might overlap.
147102ac6454SAndrew Thompson 		 */
1472ae60fdfbSAndrew Thompson 		xfer->endpoint->isoc_next = (nframes + 3) & ATMEGA_FRAME_MASK;
1473ae60fdfbSAndrew Thompson 		xfer->endpoint->is_synced = 1;
1474ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
147502ac6454SAndrew Thompson 	}
147602ac6454SAndrew Thompson 	/*
147702ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
147802ac6454SAndrew Thompson 	 * current frame position:
147902ac6454SAndrew Thompson 	 */
1480ae60fdfbSAndrew Thompson 	temp = (xfer->endpoint->isoc_next - nframes) & ATMEGA_FRAME_MASK;
148102ac6454SAndrew Thompson 
148202ac6454SAndrew Thompson 	/*
148302ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
148402ac6454SAndrew Thompson 	 */
148502ac6454SAndrew Thompson 	xfer->isoc_time_complete =
1486a593f6b8SAndrew Thompson 	    usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
148702ac6454SAndrew Thompson 	    xfer->nframes;
148802ac6454SAndrew Thompson 
148902ac6454SAndrew Thompson 	/* compute frame number for next insertion */
1490ae60fdfbSAndrew Thompson 	xfer->endpoint->isoc_next += xfer->nframes;
149102ac6454SAndrew Thompson 
149202ac6454SAndrew Thompson 	/* setup TDs */
149302ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
149402ac6454SAndrew Thompson }
149502ac6454SAndrew Thompson 
149602ac6454SAndrew Thompson static void
1497760bc48eSAndrew Thompson atmegadci_device_isoc_fs_start(struct usb_xfer *xfer)
149802ac6454SAndrew Thompson {
149902ac6454SAndrew Thompson 	/* start TD chain */
150002ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
150102ac6454SAndrew Thompson }
150202ac6454SAndrew Thompson 
1503e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods =
150402ac6454SAndrew Thompson {
150502ac6454SAndrew Thompson 	.open = atmegadci_device_isoc_fs_open,
150602ac6454SAndrew Thompson 	.close = atmegadci_device_isoc_fs_close,
150702ac6454SAndrew Thompson 	.enter = atmegadci_device_isoc_fs_enter,
150802ac6454SAndrew Thompson 	.start = atmegadci_device_isoc_fs_start,
150902ac6454SAndrew Thompson };
151002ac6454SAndrew Thompson 
151102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
151240ba168dSHans Petter Selasky  * atmegadci root control support
151302ac6454SAndrew Thompson  *------------------------------------------------------------------------*
151439307315SAndrew Thompson  * Simulate a hardware HUB by handling all the necessary requests.
151502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
151602ac6454SAndrew Thompson 
1517760bc48eSAndrew Thompson static const struct usb_device_descriptor atmegadci_devd = {
1518760bc48eSAndrew Thompson 	.bLength = sizeof(struct usb_device_descriptor),
151902ac6454SAndrew Thompson 	.bDescriptorType = UDESC_DEVICE,
152002ac6454SAndrew Thompson 	.bcdUSB = {0x00, 0x02},
152102ac6454SAndrew Thompson 	.bDeviceClass = UDCLASS_HUB,
152202ac6454SAndrew Thompson 	.bDeviceSubClass = UDSUBCLASS_HUB,
15233b6f59eeSHans Petter Selasky 	.bDeviceProtocol = UDPROTO_FSHUB,
152402ac6454SAndrew Thompson 	.bMaxPacketSize = 64,
152502ac6454SAndrew Thompson 	.bcdDevice = {0x00, 0x01},
152602ac6454SAndrew Thompson 	.iManufacturer = 1,
152702ac6454SAndrew Thompson 	.iProduct = 2,
152802ac6454SAndrew Thompson 	.bNumConfigurations = 1,
152902ac6454SAndrew Thompson };
153002ac6454SAndrew Thompson 
153102ac6454SAndrew Thompson static const struct atmegadci_config_desc atmegadci_confd = {
153202ac6454SAndrew Thompson 	.confd = {
1533760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
153402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
153502ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(atmegadci_confd),
153602ac6454SAndrew Thompson 		.bNumInterface = 1,
153702ac6454SAndrew Thompson 		.bConfigurationValue = 1,
153802ac6454SAndrew Thompson 		.iConfiguration = 0,
153902ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
154002ac6454SAndrew Thompson 		.bMaxPower = 0,
154102ac6454SAndrew Thompson 	},
154202ac6454SAndrew Thompson 	.ifcd = {
1543760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
154402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
154502ac6454SAndrew Thompson 		.bNumEndpoints = 1,
154602ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
154702ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
15483b6f59eeSHans Petter Selasky 		.bInterfaceProtocol = 0,
154902ac6454SAndrew Thompson 	},
155002ac6454SAndrew Thompson 	.endpd = {
1551760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
155202ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
155302ac6454SAndrew Thompson 		.bEndpointAddress = (UE_DIR_IN | ATMEGA_INTR_ENDPT),
155402ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
155502ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,
155602ac6454SAndrew Thompson 		.bInterval = 255,
155702ac6454SAndrew Thompson 	},
155802ac6454SAndrew Thompson };
155902ac6454SAndrew Thompson 
15606d917491SHans Petter Selasky #define	HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
15616d917491SHans Petter Selasky 
1562760bc48eSAndrew Thompson static const struct usb_hub_descriptor_min atmegadci_hubd = {
156302ac6454SAndrew Thompson 	.bDescLength = sizeof(atmegadci_hubd),
156402ac6454SAndrew Thompson 	.bDescriptorType = UDESC_HUB,
156502ac6454SAndrew Thompson 	.bNbrPorts = 1,
15666d917491SHans Petter Selasky 	HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
156702ac6454SAndrew Thompson 	.bPwrOn2PwrGood = 50,
156802ac6454SAndrew Thompson 	.bHubContrCurrent = 0,
156902ac6454SAndrew Thompson 	.DeviceRemovable = {0},		/* port is removable */
157002ac6454SAndrew Thompson };
157102ac6454SAndrew Thompson 
157202ac6454SAndrew Thompson #define	STRING_VENDOR \
1573b51875c9SHans Petter Selasky   "A\0T\0M\0E\0G\0A"
157402ac6454SAndrew Thompson 
157502ac6454SAndrew Thompson #define	STRING_PRODUCT \
1576b51875c9SHans Petter Selasky   "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
157702ac6454SAndrew Thompson 
157802ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_VENDOR, atmegadci_vendor);
157902ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_PRODUCT, atmegadci_product);
158002ac6454SAndrew Thompson 
1581e0a69b51SAndrew Thompson static usb_error_t
1582760bc48eSAndrew Thompson atmegadci_roothub_exec(struct usb_device *udev,
1583760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
158402ac6454SAndrew Thompson {
1585459d369eSAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
1586459d369eSAndrew Thompson 	const void *ptr;
1587459d369eSAndrew Thompson 	uint16_t len;
158802ac6454SAndrew Thompson 	uint16_t value;
158902ac6454SAndrew Thompson 	uint16_t index;
1590d59dbd0aSAndrew Thompson 	uint8_t temp;
1591e0a69b51SAndrew Thompson 	usb_error_t err;
159202ac6454SAndrew Thompson 
159302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
159402ac6454SAndrew Thompson 
159502ac6454SAndrew Thompson 	/* buffer reset */
1596459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_temp;
1597459d369eSAndrew Thompson 	len = 0;
1598459d369eSAndrew Thompson 	err = 0;
159902ac6454SAndrew Thompson 
1600459d369eSAndrew Thompson 	value = UGETW(req->wValue);
1601459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
160202ac6454SAndrew Thompson 
160302ac6454SAndrew Thompson 	/* demultiplex the control request */
160402ac6454SAndrew Thompson 
1605459d369eSAndrew Thompson 	switch (req->bmRequestType) {
160602ac6454SAndrew Thompson 	case UT_READ_DEVICE:
1607459d369eSAndrew Thompson 		switch (req->bRequest) {
160802ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
160902ac6454SAndrew Thompson 			goto tr_handle_get_descriptor;
161002ac6454SAndrew Thompson 		case UR_GET_CONFIG:
161102ac6454SAndrew Thompson 			goto tr_handle_get_config;
161202ac6454SAndrew Thompson 		case UR_GET_STATUS:
161302ac6454SAndrew Thompson 			goto tr_handle_get_status;
161402ac6454SAndrew Thompson 		default:
161502ac6454SAndrew Thompson 			goto tr_stalled;
161602ac6454SAndrew Thompson 		}
161702ac6454SAndrew Thompson 		break;
161802ac6454SAndrew Thompson 
161902ac6454SAndrew Thompson 	case UT_WRITE_DEVICE:
1620459d369eSAndrew Thompson 		switch (req->bRequest) {
162102ac6454SAndrew Thompson 		case UR_SET_ADDRESS:
162202ac6454SAndrew Thompson 			goto tr_handle_set_address;
162302ac6454SAndrew Thompson 		case UR_SET_CONFIG:
162402ac6454SAndrew Thompson 			goto tr_handle_set_config;
162502ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
162602ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
162702ac6454SAndrew Thompson 		case UR_SET_DESCRIPTOR:
162802ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
162902ac6454SAndrew Thompson 		case UR_SET_FEATURE:
163002ac6454SAndrew Thompson 		default:
163102ac6454SAndrew Thompson 			goto tr_stalled;
163202ac6454SAndrew Thompson 		}
163302ac6454SAndrew Thompson 		break;
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 	case UT_WRITE_ENDPOINT:
1636459d369eSAndrew Thompson 		switch (req->bRequest) {
163702ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
1638459d369eSAndrew Thompson 			switch (UGETW(req->wValue)) {
163902ac6454SAndrew Thompson 			case UF_ENDPOINT_HALT:
164002ac6454SAndrew Thompson 				goto tr_handle_clear_halt;
164102ac6454SAndrew Thompson 			case UF_DEVICE_REMOTE_WAKEUP:
164202ac6454SAndrew Thompson 				goto tr_handle_clear_wakeup;
164302ac6454SAndrew Thompson 			default:
164402ac6454SAndrew Thompson 				goto tr_stalled;
164502ac6454SAndrew Thompson 			}
164602ac6454SAndrew Thompson 			break;
164702ac6454SAndrew Thompson 		case UR_SET_FEATURE:
1648459d369eSAndrew Thompson 			switch (UGETW(req->wValue)) {
164902ac6454SAndrew Thompson 			case UF_ENDPOINT_HALT:
165002ac6454SAndrew Thompson 				goto tr_handle_set_halt;
165102ac6454SAndrew Thompson 			case UF_DEVICE_REMOTE_WAKEUP:
165202ac6454SAndrew Thompson 				goto tr_handle_set_wakeup;
165302ac6454SAndrew Thompson 			default:
165402ac6454SAndrew Thompson 				goto tr_stalled;
165502ac6454SAndrew Thompson 			}
165602ac6454SAndrew Thompson 			break;
165702ac6454SAndrew Thompson 		case UR_SYNCH_FRAME:
165802ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
165902ac6454SAndrew Thompson 		default:
166002ac6454SAndrew Thompson 			goto tr_stalled;
166102ac6454SAndrew Thompson 		}
166202ac6454SAndrew Thompson 		break;
166302ac6454SAndrew Thompson 
166402ac6454SAndrew Thompson 	case UT_READ_ENDPOINT:
1665459d369eSAndrew Thompson 		switch (req->bRequest) {
166602ac6454SAndrew Thompson 		case UR_GET_STATUS:
166702ac6454SAndrew Thompson 			goto tr_handle_get_ep_status;
166802ac6454SAndrew Thompson 		default:
166902ac6454SAndrew Thompson 			goto tr_stalled;
167002ac6454SAndrew Thompson 		}
167102ac6454SAndrew Thompson 		break;
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 	case UT_WRITE_INTERFACE:
1674459d369eSAndrew Thompson 		switch (req->bRequest) {
167502ac6454SAndrew Thompson 		case UR_SET_INTERFACE:
167602ac6454SAndrew Thompson 			goto tr_handle_set_interface;
167702ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
167802ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
167902ac6454SAndrew Thompson 		case UR_SET_FEATURE:
168002ac6454SAndrew Thompson 		default:
168102ac6454SAndrew Thompson 			goto tr_stalled;
168202ac6454SAndrew Thompson 		}
168302ac6454SAndrew Thompson 		break;
168402ac6454SAndrew Thompson 
168502ac6454SAndrew Thompson 	case UT_READ_INTERFACE:
1686459d369eSAndrew Thompson 		switch (req->bRequest) {
168702ac6454SAndrew Thompson 		case UR_GET_INTERFACE:
168802ac6454SAndrew Thompson 			goto tr_handle_get_interface;
168902ac6454SAndrew Thompson 		case UR_GET_STATUS:
169002ac6454SAndrew Thompson 			goto tr_handle_get_iface_status;
169102ac6454SAndrew Thompson 		default:
169202ac6454SAndrew Thompson 			goto tr_stalled;
169302ac6454SAndrew Thompson 		}
169402ac6454SAndrew Thompson 		break;
169502ac6454SAndrew Thompson 
169602ac6454SAndrew Thompson 	case UT_WRITE_CLASS_INTERFACE:
169702ac6454SAndrew Thompson 	case UT_WRITE_VENDOR_INTERFACE:
169802ac6454SAndrew Thompson 		/* XXX forward */
169902ac6454SAndrew Thompson 		break;
170002ac6454SAndrew Thompson 
170102ac6454SAndrew Thompson 	case UT_READ_CLASS_INTERFACE:
170202ac6454SAndrew Thompson 	case UT_READ_VENDOR_INTERFACE:
170302ac6454SAndrew Thompson 		/* XXX forward */
170402ac6454SAndrew Thompson 		break;
170502ac6454SAndrew Thompson 
170602ac6454SAndrew Thompson 	case UT_WRITE_CLASS_DEVICE:
1707459d369eSAndrew Thompson 		switch (req->bRequest) {
170802ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
170902ac6454SAndrew Thompson 			goto tr_valid;
171002ac6454SAndrew Thompson 		case UR_SET_DESCRIPTOR:
171102ac6454SAndrew Thompson 		case UR_SET_FEATURE:
171202ac6454SAndrew Thompson 			break;
171302ac6454SAndrew Thompson 		default:
171402ac6454SAndrew Thompson 			goto tr_stalled;
171502ac6454SAndrew Thompson 		}
171602ac6454SAndrew Thompson 		break;
171702ac6454SAndrew Thompson 
171802ac6454SAndrew Thompson 	case UT_WRITE_CLASS_OTHER:
1719459d369eSAndrew Thompson 		switch (req->bRequest) {
172002ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
172102ac6454SAndrew Thompson 			goto tr_handle_clear_port_feature;
172202ac6454SAndrew Thompson 		case UR_SET_FEATURE:
172302ac6454SAndrew Thompson 			goto tr_handle_set_port_feature;
172402ac6454SAndrew Thompson 		case UR_CLEAR_TT_BUFFER:
172502ac6454SAndrew Thompson 		case UR_RESET_TT:
172602ac6454SAndrew Thompson 		case UR_STOP_TT:
172702ac6454SAndrew Thompson 			goto tr_valid;
172802ac6454SAndrew Thompson 
172902ac6454SAndrew Thompson 		default:
173002ac6454SAndrew Thompson 			goto tr_stalled;
173102ac6454SAndrew Thompson 		}
173202ac6454SAndrew Thompson 		break;
173302ac6454SAndrew Thompson 
173402ac6454SAndrew Thompson 	case UT_READ_CLASS_OTHER:
1735459d369eSAndrew Thompson 		switch (req->bRequest) {
173602ac6454SAndrew Thompson 		case UR_GET_TT_STATE:
173702ac6454SAndrew Thompson 			goto tr_handle_get_tt_state;
173802ac6454SAndrew Thompson 		case UR_GET_STATUS:
173902ac6454SAndrew Thompson 			goto tr_handle_get_port_status;
174002ac6454SAndrew Thompson 		default:
174102ac6454SAndrew Thompson 			goto tr_stalled;
174202ac6454SAndrew Thompson 		}
174302ac6454SAndrew Thompson 		break;
174402ac6454SAndrew Thompson 
174502ac6454SAndrew Thompson 	case UT_READ_CLASS_DEVICE:
1746459d369eSAndrew Thompson 		switch (req->bRequest) {
174702ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
174802ac6454SAndrew Thompson 			goto tr_handle_get_class_descriptor;
174902ac6454SAndrew Thompson 		case UR_GET_STATUS:
175002ac6454SAndrew Thompson 			goto tr_handle_get_class_status;
175102ac6454SAndrew Thompson 
175202ac6454SAndrew Thompson 		default:
175302ac6454SAndrew Thompson 			goto tr_stalled;
175402ac6454SAndrew Thompson 		}
175502ac6454SAndrew Thompson 		break;
175602ac6454SAndrew Thompson 	default:
175702ac6454SAndrew Thompson 		goto tr_stalled;
175802ac6454SAndrew Thompson 	}
175902ac6454SAndrew Thompson 	goto tr_valid;
176002ac6454SAndrew Thompson 
176102ac6454SAndrew Thompson tr_handle_get_descriptor:
176202ac6454SAndrew Thompson 	switch (value >> 8) {
176302ac6454SAndrew Thompson 	case UDESC_DEVICE:
176402ac6454SAndrew Thompson 		if (value & 0xff) {
176502ac6454SAndrew Thompson 			goto tr_stalled;
176602ac6454SAndrew Thompson 		}
1767459d369eSAndrew Thompson 		len = sizeof(atmegadci_devd);
1768459d369eSAndrew Thompson 		ptr = (const void *)&atmegadci_devd;
176902ac6454SAndrew Thompson 		goto tr_valid;
177002ac6454SAndrew Thompson 	case UDESC_CONFIG:
177102ac6454SAndrew Thompson 		if (value & 0xff) {
177202ac6454SAndrew Thompson 			goto tr_stalled;
177302ac6454SAndrew Thompson 		}
1774459d369eSAndrew Thompson 		len = sizeof(atmegadci_confd);
1775459d369eSAndrew Thompson 		ptr = (const void *)&atmegadci_confd;
177602ac6454SAndrew Thompson 		goto tr_valid;
177702ac6454SAndrew Thompson 	case UDESC_STRING:
177802ac6454SAndrew Thompson 		switch (value & 0xff) {
177902ac6454SAndrew Thompson 		case 0:		/* Language table */
178023ab0871SHans Petter Selasky 			len = sizeof(usb_string_lang_en);
178123ab0871SHans Petter Selasky 			ptr = (const void *)&usb_string_lang_en;
178202ac6454SAndrew Thompson 			goto tr_valid;
178302ac6454SAndrew Thompson 
178402ac6454SAndrew Thompson 		case 1:		/* Vendor */
1785459d369eSAndrew Thompson 			len = sizeof(atmegadci_vendor);
1786459d369eSAndrew Thompson 			ptr = (const void *)&atmegadci_vendor;
178702ac6454SAndrew Thompson 			goto tr_valid;
178802ac6454SAndrew Thompson 
178902ac6454SAndrew Thompson 		case 2:		/* Product */
1790459d369eSAndrew Thompson 			len = sizeof(atmegadci_product);
1791459d369eSAndrew Thompson 			ptr = (const void *)&atmegadci_product;
179202ac6454SAndrew Thompson 			goto tr_valid;
179302ac6454SAndrew Thompson 		default:
179402ac6454SAndrew Thompson 			break;
179502ac6454SAndrew Thompson 		}
179602ac6454SAndrew Thompson 		break;
179702ac6454SAndrew Thompson 	default:
179802ac6454SAndrew Thompson 		goto tr_stalled;
179902ac6454SAndrew Thompson 	}
180002ac6454SAndrew Thompson 	goto tr_stalled;
180102ac6454SAndrew Thompson 
180202ac6454SAndrew Thompson tr_handle_get_config:
1803459d369eSAndrew Thompson 	len = 1;
180402ac6454SAndrew Thompson 	sc->sc_hub_temp.wValue[0] = sc->sc_conf;
180502ac6454SAndrew Thompson 	goto tr_valid;
180602ac6454SAndrew Thompson 
180702ac6454SAndrew Thompson tr_handle_get_status:
1808459d369eSAndrew Thompson 	len = 2;
180902ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
181002ac6454SAndrew Thompson 	goto tr_valid;
181102ac6454SAndrew Thompson 
181202ac6454SAndrew Thompson tr_handle_set_address:
181302ac6454SAndrew Thompson 	if (value & 0xFF00) {
181402ac6454SAndrew Thompson 		goto tr_stalled;
181502ac6454SAndrew Thompson 	}
181602ac6454SAndrew Thompson 	sc->sc_rt_addr = value;
181702ac6454SAndrew Thompson 	goto tr_valid;
181802ac6454SAndrew Thompson 
181902ac6454SAndrew Thompson tr_handle_set_config:
182002ac6454SAndrew Thompson 	if (value >= 2) {
182102ac6454SAndrew Thompson 		goto tr_stalled;
182202ac6454SAndrew Thompson 	}
182302ac6454SAndrew Thompson 	sc->sc_conf = value;
182402ac6454SAndrew Thompson 	goto tr_valid;
182502ac6454SAndrew Thompson 
182602ac6454SAndrew Thompson tr_handle_get_interface:
1827459d369eSAndrew Thompson 	len = 1;
182802ac6454SAndrew Thompson 	sc->sc_hub_temp.wValue[0] = 0;
182902ac6454SAndrew Thompson 	goto tr_valid;
183002ac6454SAndrew Thompson 
183102ac6454SAndrew Thompson tr_handle_get_tt_state:
183202ac6454SAndrew Thompson tr_handle_get_class_status:
183302ac6454SAndrew Thompson tr_handle_get_iface_status:
183402ac6454SAndrew Thompson tr_handle_get_ep_status:
1835459d369eSAndrew Thompson 	len = 2;
183602ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.wValue, 0);
183702ac6454SAndrew Thompson 	goto tr_valid;
183802ac6454SAndrew Thompson 
183902ac6454SAndrew Thompson tr_handle_set_halt:
184002ac6454SAndrew Thompson tr_handle_set_interface:
184102ac6454SAndrew Thompson tr_handle_set_wakeup:
184202ac6454SAndrew Thompson tr_handle_clear_wakeup:
184302ac6454SAndrew Thompson tr_handle_clear_halt:
184402ac6454SAndrew Thompson 	goto tr_valid;
184502ac6454SAndrew Thompson 
184602ac6454SAndrew Thompson tr_handle_clear_port_feature:
184702ac6454SAndrew Thompson 	if (index != 1) {
184802ac6454SAndrew Thompson 		goto tr_stalled;
184902ac6454SAndrew Thompson 	}
185002ac6454SAndrew Thompson 	DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
185102ac6454SAndrew Thompson 
185202ac6454SAndrew Thompson 	switch (value) {
185302ac6454SAndrew Thompson 	case UHF_PORT_SUSPEND:
185439307315SAndrew Thompson 		atmegadci_wakeup_peer(sc);
185502ac6454SAndrew Thompson 		break;
185602ac6454SAndrew Thompson 
185702ac6454SAndrew Thompson 	case UHF_PORT_ENABLE:
185802ac6454SAndrew Thompson 		sc->sc_flags.port_enabled = 0;
185902ac6454SAndrew Thompson 		break;
186002ac6454SAndrew Thompson 
186102ac6454SAndrew Thompson 	case UHF_PORT_TEST:
186202ac6454SAndrew Thompson 	case UHF_PORT_INDICATOR:
186302ac6454SAndrew Thompson 	case UHF_C_PORT_ENABLE:
186402ac6454SAndrew Thompson 	case UHF_C_PORT_OVER_CURRENT:
186502ac6454SAndrew Thompson 	case UHF_C_PORT_RESET:
186602ac6454SAndrew Thompson 		/* nops */
186702ac6454SAndrew Thompson 		break;
186802ac6454SAndrew Thompson 	case UHF_PORT_POWER:
186902ac6454SAndrew Thompson 		sc->sc_flags.port_powered = 0;
187002ac6454SAndrew Thompson 		atmegadci_pull_down(sc);
187102ac6454SAndrew Thompson 		atmegadci_clocks_off(sc);
187202ac6454SAndrew Thompson 		break;
187302ac6454SAndrew Thompson 	case UHF_C_PORT_CONNECTION:
1874d59dbd0aSAndrew Thompson 		/* clear connect change flag */
187502ac6454SAndrew Thompson 		sc->sc_flags.change_connect = 0;
1876d59dbd0aSAndrew Thompson 
18777eaa41aeSAndrew Thompson 		if (!sc->sc_flags.status_bus_reset) {
18787eaa41aeSAndrew Thompson 			/* we are not connected */
18797eaa41aeSAndrew Thompson 			break;
18807eaa41aeSAndrew Thompson 		}
18817eaa41aeSAndrew Thompson 
1882d59dbd0aSAndrew Thompson 		/* configure the control endpoint */
1883d59dbd0aSAndrew Thompson 
1884d59dbd0aSAndrew Thompson 		/* select endpoint number */
1885d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, 0);
1886d59dbd0aSAndrew Thompson 
1887d59dbd0aSAndrew Thompson 		/* set endpoint reset */
1888d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(0));
1889d59dbd0aSAndrew Thompson 
1890d59dbd0aSAndrew Thompson 		/* clear endpoint reset */
1891d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1892d59dbd0aSAndrew Thompson 
1893d59dbd0aSAndrew Thompson 		/* enable and stall endpoint */
1894d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1895d59dbd0aSAndrew Thompson 		    ATMEGA_UECONX_EPEN |
1896d59dbd0aSAndrew Thompson 		    ATMEGA_UECONX_STALLRQ);
1897d59dbd0aSAndrew Thompson 
1898d59dbd0aSAndrew Thompson 		/* one bank, 64-bytes wMaxPacket */
1899d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X,
1900d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG0X_EPTYPE0);
1901d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
1902d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_ALLOC |
1903d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPBK0 |
1904d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPSIZE(3));
1905d59dbd0aSAndrew Thompson 
1906d59dbd0aSAndrew Thompson 		/* check valid config */
1907d59dbd0aSAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
1908d59dbd0aSAndrew Thompson 		if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
1909767cb2e2SAndrew Thompson 			device_printf(sc->sc_bus.bdev,
1910767cb2e2SAndrew Thompson 			    "Chip rejected EP0 configuration\n");
1911d59dbd0aSAndrew Thompson 		}
191202ac6454SAndrew Thompson 		break;
191302ac6454SAndrew Thompson 	case UHF_C_PORT_SUSPEND:
191402ac6454SAndrew Thompson 		sc->sc_flags.change_suspend = 0;
191502ac6454SAndrew Thompson 		break;
191602ac6454SAndrew Thompson 	default:
1917459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
191802ac6454SAndrew Thompson 		goto done;
191902ac6454SAndrew Thompson 	}
192002ac6454SAndrew Thompson 	goto tr_valid;
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson tr_handle_set_port_feature:
192302ac6454SAndrew Thompson 	if (index != 1) {
192402ac6454SAndrew Thompson 		goto tr_stalled;
192502ac6454SAndrew Thompson 	}
192602ac6454SAndrew Thompson 	DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
192702ac6454SAndrew Thompson 
192802ac6454SAndrew Thompson 	switch (value) {
192902ac6454SAndrew Thompson 	case UHF_PORT_ENABLE:
193002ac6454SAndrew Thompson 		sc->sc_flags.port_enabled = 1;
193102ac6454SAndrew Thompson 		break;
193202ac6454SAndrew Thompson 	case UHF_PORT_SUSPEND:
193302ac6454SAndrew Thompson 	case UHF_PORT_RESET:
193402ac6454SAndrew Thompson 	case UHF_PORT_TEST:
193502ac6454SAndrew Thompson 	case UHF_PORT_INDICATOR:
193602ac6454SAndrew Thompson 		/* nops */
193702ac6454SAndrew Thompson 		break;
193802ac6454SAndrew Thompson 	case UHF_PORT_POWER:
193902ac6454SAndrew Thompson 		sc->sc_flags.port_powered = 1;
194002ac6454SAndrew Thompson 		break;
194102ac6454SAndrew Thompson 	default:
1942459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
194302ac6454SAndrew Thompson 		goto done;
194402ac6454SAndrew Thompson 	}
194502ac6454SAndrew Thompson 	goto tr_valid;
194602ac6454SAndrew Thompson 
194702ac6454SAndrew Thompson tr_handle_get_port_status:
194802ac6454SAndrew Thompson 
194902ac6454SAndrew Thompson 	DPRINTFN(9, "UR_GET_PORT_STATUS\n");
195002ac6454SAndrew Thompson 
195102ac6454SAndrew Thompson 	if (index != 1) {
195202ac6454SAndrew Thompson 		goto tr_stalled;
195302ac6454SAndrew Thompson 	}
195402ac6454SAndrew Thompson 	if (sc->sc_flags.status_vbus) {
195502ac6454SAndrew Thompson 		atmegadci_clocks_on(sc);
195602ac6454SAndrew Thompson 		atmegadci_pull_up(sc);
195702ac6454SAndrew Thompson 	} else {
195802ac6454SAndrew Thompson 		atmegadci_pull_down(sc);
195902ac6454SAndrew Thompson 		atmegadci_clocks_off(sc);
196002ac6454SAndrew Thompson 	}
196102ac6454SAndrew Thompson 
196202ac6454SAndrew Thompson 	/* Select FULL-speed and Device Side Mode */
196302ac6454SAndrew Thompson 
196402ac6454SAndrew Thompson 	value = UPS_PORT_MODE_DEVICE;
196502ac6454SAndrew Thompson 
196602ac6454SAndrew Thompson 	if (sc->sc_flags.port_powered) {
196702ac6454SAndrew Thompson 		value |= UPS_PORT_POWER;
196802ac6454SAndrew Thompson 	}
196902ac6454SAndrew Thompson 	if (sc->sc_flags.port_enabled) {
197002ac6454SAndrew Thompson 		value |= UPS_PORT_ENABLED;
197102ac6454SAndrew Thompson 	}
197202ac6454SAndrew Thompson 	if (sc->sc_flags.status_vbus &&
197302ac6454SAndrew Thompson 	    sc->sc_flags.status_bus_reset) {
197402ac6454SAndrew Thompson 		value |= UPS_CURRENT_CONNECT_STATUS;
197502ac6454SAndrew Thompson 	}
197602ac6454SAndrew Thompson 	if (sc->sc_flags.status_suspend) {
197702ac6454SAndrew Thompson 		value |= UPS_SUSPEND;
197802ac6454SAndrew Thompson 	}
197902ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.ps.wPortStatus, value);
198002ac6454SAndrew Thompson 
198102ac6454SAndrew Thompson 	value = 0;
198202ac6454SAndrew Thompson 
198302ac6454SAndrew Thompson 	if (sc->sc_flags.change_connect) {
198402ac6454SAndrew Thompson 		value |= UPS_C_CONNECT_STATUS;
198502ac6454SAndrew Thompson 	}
198602ac6454SAndrew Thompson 	if (sc->sc_flags.change_suspend) {
198702ac6454SAndrew Thompson 		value |= UPS_C_SUSPEND;
198802ac6454SAndrew Thompson 	}
198902ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.ps.wPortChange, value);
1990459d369eSAndrew Thompson 	len = sizeof(sc->sc_hub_temp.ps);
199102ac6454SAndrew Thompson 	goto tr_valid;
199202ac6454SAndrew Thompson 
199302ac6454SAndrew Thompson tr_handle_get_class_descriptor:
199402ac6454SAndrew Thompson 	if (value & 0xFF) {
199502ac6454SAndrew Thompson 		goto tr_stalled;
199602ac6454SAndrew Thompson 	}
1997459d369eSAndrew Thompson 	ptr = (const void *)&atmegadci_hubd;
1998459d369eSAndrew Thompson 	len = sizeof(atmegadci_hubd);
199902ac6454SAndrew Thompson 	goto tr_valid;
200002ac6454SAndrew Thompson 
200102ac6454SAndrew Thompson tr_stalled:
2002459d369eSAndrew Thompson 	err = USB_ERR_STALLED;
200302ac6454SAndrew Thompson tr_valid:
200402ac6454SAndrew Thompson done:
2005459d369eSAndrew Thompson 	*plength = len;
2006459d369eSAndrew Thompson 	*pptr = ptr;
2007459d369eSAndrew Thompson 	return (err);
200802ac6454SAndrew Thompson }
200902ac6454SAndrew Thompson 
201002ac6454SAndrew Thompson static void
2011760bc48eSAndrew Thompson atmegadci_xfer_setup(struct usb_setup_params *parm)
201202ac6454SAndrew Thompson {
2013760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
201402ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
2015760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
201602ac6454SAndrew Thompson 	void *last_obj;
201702ac6454SAndrew Thompson 	uint32_t ntd;
201802ac6454SAndrew Thompson 	uint32_t n;
201902ac6454SAndrew Thompson 	uint8_t ep_no;
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(parm->udev->bus);
202202ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
202302ac6454SAndrew Thompson 
202402ac6454SAndrew Thompson 	/*
202502ac6454SAndrew Thompson 	 * NOTE: This driver does not use any of the parameters that
202602ac6454SAndrew Thompson 	 * are computed from the following values. Just set some
202702ac6454SAndrew Thompson 	 * reasonable dummies:
202802ac6454SAndrew Thompson 	 */
202902ac6454SAndrew Thompson 	parm->hc_max_packet_size = 0x500;
203002ac6454SAndrew Thompson 	parm->hc_max_packet_count = 1;
203102ac6454SAndrew Thompson 	parm->hc_max_frame_size = 0x500;
203202ac6454SAndrew Thompson 
2033a593f6b8SAndrew Thompson 	usbd_transfer_setup_sub(parm);
203402ac6454SAndrew Thompson 
203502ac6454SAndrew Thompson 	/*
203602ac6454SAndrew Thompson 	 * compute maximum number of TDs
203702ac6454SAndrew Thompson 	 */
2038ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
203902ac6454SAndrew Thompson 
204002ac6454SAndrew Thompson 		ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
204102ac6454SAndrew Thompson 		    + 1 /* SYNC 2 */ ;
204202ac6454SAndrew Thompson 	} else {
204302ac6454SAndrew Thompson 
20443b01d513SAndrew Thompson 		ntd = xfer->nframes + 1 /* SYNC */ ;
204502ac6454SAndrew Thompson 	}
204602ac6454SAndrew Thompson 
204702ac6454SAndrew Thompson 	/*
2048a593f6b8SAndrew Thompson 	 * check if "usbd_transfer_setup_sub" set an error
204902ac6454SAndrew Thompson 	 */
20503b01d513SAndrew Thompson 	if (parm->err)
205102ac6454SAndrew Thompson 		return;
20523b01d513SAndrew Thompson 
205302ac6454SAndrew Thompson 	/*
205402ac6454SAndrew Thompson 	 * allocate transfer descriptors
205502ac6454SAndrew Thompson 	 */
205602ac6454SAndrew Thompson 	last_obj = NULL;
205702ac6454SAndrew Thompson 
205802ac6454SAndrew Thompson 	/*
205902ac6454SAndrew Thompson 	 * get profile stuff
206002ac6454SAndrew Thompson 	 */
2061ae60fdfbSAndrew Thompson 	ep_no = xfer->endpointno & UE_ADDR;
206202ac6454SAndrew Thompson 	atmegadci_get_hw_ep_profile(parm->udev, &pf, ep_no);
206302ac6454SAndrew Thompson 
206402ac6454SAndrew Thompson 	if (pf == NULL) {
206502ac6454SAndrew Thompson 		/* should not happen */
206602ac6454SAndrew Thompson 		parm->err = USB_ERR_INVAL;
206702ac6454SAndrew Thompson 		return;
206802ac6454SAndrew Thompson 	}
206902ac6454SAndrew Thompson 
207002ac6454SAndrew Thompson 	/* align data */
207102ac6454SAndrew Thompson 	parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
207202ac6454SAndrew Thompson 
207302ac6454SAndrew Thompson 	for (n = 0; n != ntd; n++) {
207402ac6454SAndrew Thompson 
207502ac6454SAndrew Thompson 		struct atmegadci_td *td;
207602ac6454SAndrew Thompson 
207702ac6454SAndrew Thompson 		if (parm->buf) {
207802ac6454SAndrew Thompson 
207902ac6454SAndrew Thompson 			td = USB_ADD_BYTES(parm->buf, parm->size[0]);
208002ac6454SAndrew Thompson 
208102ac6454SAndrew Thompson 			/* init TD */
208202ac6454SAndrew Thompson 			td->max_packet_size = xfer->max_packet_size;
208302ac6454SAndrew Thompson 			td->ep_no = ep_no;
208402ac6454SAndrew Thompson 			if (pf->support_multi_buffer) {
208502ac6454SAndrew Thompson 				td->support_multi_buffer = 1;
208602ac6454SAndrew Thompson 			}
208702ac6454SAndrew Thompson 			td->obj_next = last_obj;
208802ac6454SAndrew Thompson 
208902ac6454SAndrew Thompson 			last_obj = td;
209002ac6454SAndrew Thompson 		}
209102ac6454SAndrew Thompson 		parm->size[0] += sizeof(*td);
209202ac6454SAndrew Thompson 	}
209302ac6454SAndrew Thompson 
209402ac6454SAndrew Thompson 	xfer->td_start[0] = last_obj;
209502ac6454SAndrew Thompson }
209602ac6454SAndrew Thompson 
209702ac6454SAndrew Thompson static void
2098760bc48eSAndrew Thompson atmegadci_xfer_unsetup(struct usb_xfer *xfer)
209902ac6454SAndrew Thompson {
210002ac6454SAndrew Thompson 	return;
210102ac6454SAndrew Thompson }
210202ac6454SAndrew Thompson 
210302ac6454SAndrew Thompson static void
2104ae60fdfbSAndrew Thompson atmegadci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2105ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
210602ac6454SAndrew Thompson {
210702ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
210802ac6454SAndrew Thompson 
2109ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2110ae60fdfbSAndrew Thompson 	    ep, udev->address,
2111f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
2112d59dbd0aSAndrew Thompson 	    sc->sc_rt_addr, udev->device_index);
211302ac6454SAndrew Thompson 
211439307315SAndrew Thompson 	if (udev->device_index != sc->sc_rt_addr) {
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_FULL) {
211702ac6454SAndrew Thompson 			/* not supported */
211802ac6454SAndrew Thompson 			return;
211902ac6454SAndrew Thompson 		}
21203b01d513SAndrew Thompson 		if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2121ae60fdfbSAndrew Thompson 			ep->methods = &atmegadci_device_isoc_fs_methods;
21223b01d513SAndrew Thompson 		else
2123ae60fdfbSAndrew Thompson 			ep->methods = &atmegadci_device_non_isoc_methods;
212402ac6454SAndrew Thompson 	}
212502ac6454SAndrew Thompson }
212602ac6454SAndrew Thompson 
21272e141748SHans Petter Selasky static void
21282e141748SHans Petter Selasky atmegadci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
21292e141748SHans Petter Selasky {
21302e141748SHans Petter Selasky 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
21312e141748SHans Petter Selasky 
21322e141748SHans Petter Selasky 	switch (state) {
21332e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
21342e141748SHans Petter Selasky 		atmegadci_suspend(sc);
21352e141748SHans Petter Selasky 		break;
21362e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
21372e141748SHans Petter Selasky 		atmegadci_uninit(sc);
21382e141748SHans Petter Selasky 		break;
21392e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
21402e141748SHans Petter Selasky 		atmegadci_resume(sc);
21412e141748SHans Petter Selasky 		break;
21422e141748SHans Petter Selasky 	default:
21432e141748SHans Petter Selasky 		break;
21442e141748SHans Petter Selasky 	}
21452e141748SHans Petter Selasky }
21462e141748SHans Petter Selasky 
2147e892b3feSHans Petter Selasky static const struct usb_bus_methods atmegadci_bus_methods =
214802ac6454SAndrew Thompson {
2149ae60fdfbSAndrew Thompson 	.endpoint_init = &atmegadci_ep_init,
215002ac6454SAndrew Thompson 	.xfer_setup = &atmegadci_xfer_setup,
215102ac6454SAndrew Thompson 	.xfer_unsetup = &atmegadci_xfer_unsetup,
215202ac6454SAndrew Thompson 	.get_hw_ep_profile = &atmegadci_get_hw_ep_profile,
2153a5cf1aaaSHans Petter Selasky 	.xfer_stall = &atmegadci_xfer_stall,
215402ac6454SAndrew Thompson 	.set_stall = &atmegadci_set_stall,
215502ac6454SAndrew Thompson 	.clear_stall = &atmegadci_clear_stall,
215639307315SAndrew Thompson 	.roothub_exec = &atmegadci_roothub_exec,
2157dddb25f9SAlfred Perlstein 	.xfer_poll = &atmegadci_do_poll,
21582e141748SHans Petter Selasky 	.set_hw_power_sleep = &atmegadci_set_hw_power_sleep,
215902ac6454SAndrew Thompson };
2160