xref: /freebsd/sys/dev/usb/controller/atmegadci.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
2802ac6454SAndrew Thompson /*
29fde87526SAndrew Thompson  * This file contains the driver for the ATMEGA series USB OTG Controller. This
30fde87526SAndrew Thompson  * driver currently only supports the DCI mode of the USB hardware.
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson /*
3402ac6454SAndrew Thompson  * NOTE: When the chip detects BUS-reset it will also reset the
3502ac6454SAndrew Thompson  * endpoints, Function-address and more.
3602ac6454SAndrew Thompson  */
3702ac6454SAndrew Thompson 
38d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
39d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
40d2b99310SHans Petter Selasky #else
41ed6d949aSAndrew Thompson #include <sys/stdint.h>
42ed6d949aSAndrew Thompson #include <sys/stddef.h>
43ed6d949aSAndrew Thompson #include <sys/param.h>
44ed6d949aSAndrew Thompson #include <sys/queue.h>
45ed6d949aSAndrew Thompson #include <sys/types.h>
46ed6d949aSAndrew Thompson #include <sys/systm.h>
47ed6d949aSAndrew Thompson #include <sys/kernel.h>
48ed6d949aSAndrew Thompson #include <sys/bus.h>
49ed6d949aSAndrew Thompson #include <sys/module.h>
50ed6d949aSAndrew Thompson #include <sys/lock.h>
51ed6d949aSAndrew Thompson #include <sys/mutex.h>
52ed6d949aSAndrew Thompson #include <sys/condvar.h>
53ed6d949aSAndrew Thompson #include <sys/sysctl.h>
54ed6d949aSAndrew Thompson #include <sys/sx.h>
55ed6d949aSAndrew Thompson #include <sys/unistd.h>
56ed6d949aSAndrew Thompson #include <sys/callout.h>
57ed6d949aSAndrew Thompson #include <sys/malloc.h>
58ed6d949aSAndrew Thompson #include <sys/priv.h>
59ed6d949aSAndrew Thompson 
6002ac6454SAndrew Thompson #include <dev/usb/usb.h>
61ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson #define	USB_DEBUG_VAR atmegadci_debug
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
76d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
77d2b99310SHans Petter Selasky 
7802ac6454SAndrew Thompson #include <dev/usb/controller/atmegadci.h>
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson #define	ATMEGA_BUS2SC(bus) \
81a3cea156SAndrew Turner     __containerof(bus, struct atmegadci_softc, sc_bus)
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #define	ATMEGA_PC2SC(pc) \
84bdc081c6SAndrew Thompson    ATMEGA_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
8502ac6454SAndrew Thompson 
86ed6d949aSAndrew Thompson #ifdef USB_DEBUG
8702ac6454SAndrew Thompson static int atmegadci_debug = 0;
8802ac6454SAndrew Thompson 
89f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, atmegadci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
906472ac3dSEd Schouten     "USB ATMEGA DCI");
91ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_atmegadci, OID_AUTO, debug, CTLFLAG_RWTUN,
9202ac6454SAndrew Thompson     &atmegadci_debug, 0, "ATMEGA DCI debug level");
9302ac6454SAndrew Thompson #endif
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson #define	ATMEGA_INTR_ENDPT 1
9602ac6454SAndrew Thompson 
9702ac6454SAndrew Thompson /* prototypes */
9802ac6454SAndrew Thompson 
99e892b3feSHans Petter Selasky static const struct usb_bus_methods atmegadci_bus_methods;
100e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_non_isoc_methods;
101e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods;
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_setup_rx;
10402ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_rx;
10502ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_tx;
10602ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_tx_sync;
107e0a69b51SAndrew Thompson static void atmegadci_device_done(struct usb_xfer *, usb_error_t);
108760bc48eSAndrew Thompson static void atmegadci_do_poll(struct usb_bus *);
109760bc48eSAndrew Thompson static void atmegadci_standard_done(struct usb_xfer *);
11039307315SAndrew Thompson static void atmegadci_root_intr(struct atmegadci_softc *sc);
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson /*
11302ac6454SAndrew Thompson  * Here is a list of what the chip supports:
11402ac6454SAndrew Thompson  */
115760bc48eSAndrew Thompson static const struct usb_hw_ep_profile
11602ac6454SAndrew Thompson 	atmegadci_ep_profile[2] = {
11702ac6454SAndrew Thompson 	[0] = {
11802ac6454SAndrew Thompson 		.max_in_frame_size = 64,
11902ac6454SAndrew Thompson 		.max_out_frame_size = 64,
12002ac6454SAndrew Thompson 		.is_simplex = 1,
12102ac6454SAndrew Thompson 		.support_control = 1,
12202ac6454SAndrew Thompson 	},
12302ac6454SAndrew Thompson 	[1] = {
12402ac6454SAndrew Thompson 		.max_in_frame_size = 64,
12502ac6454SAndrew Thompson 		.max_out_frame_size = 64,
12602ac6454SAndrew Thompson 		.is_simplex = 1,
12702ac6454SAndrew Thompson 		.support_bulk = 1,
12802ac6454SAndrew Thompson 		.support_interrupt = 1,
12902ac6454SAndrew Thompson 		.support_isochronous = 1,
13002ac6454SAndrew Thompson 		.support_in = 1,
13102ac6454SAndrew Thompson 		.support_out = 1,
13202ac6454SAndrew Thompson 	},
13302ac6454SAndrew Thompson };
13402ac6454SAndrew Thompson 
13502ac6454SAndrew Thompson static void
atmegadci_get_hw_ep_profile(struct usb_device * udev,const struct usb_hw_ep_profile ** ppf,uint8_t ep_addr)136760bc48eSAndrew Thompson atmegadci_get_hw_ep_profile(struct usb_device *udev,
137760bc48eSAndrew Thompson     const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
13802ac6454SAndrew Thompson {
13902ac6454SAndrew Thompson 	if (ep_addr == 0)
14002ac6454SAndrew Thompson 		*ppf = atmegadci_ep_profile;
14102ac6454SAndrew Thompson 	else if (ep_addr < ATMEGA_EP_MAX)
14202ac6454SAndrew Thompson 		*ppf = atmegadci_ep_profile + 1;
14302ac6454SAndrew Thompson 	else
14402ac6454SAndrew Thompson 		*ppf = NULL;
14502ac6454SAndrew Thompson }
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson static void
atmegadci_clocks_on(struct atmegadci_softc * sc)14802ac6454SAndrew Thompson atmegadci_clocks_on(struct atmegadci_softc *sc)
14902ac6454SAndrew Thompson {
15002ac6454SAndrew Thompson 	if (sc->sc_flags.clocks_off &&
15102ac6454SAndrew Thompson 	    sc->sc_flags.port_powered) {
15202ac6454SAndrew Thompson 		DPRINTFN(5, "\n");
15302ac6454SAndrew Thompson 
15402ac6454SAndrew Thompson 		/* turn on clocks */
15502ac6454SAndrew Thompson 		(sc->sc_clocks_on) (&sc->sc_bus);
15602ac6454SAndrew Thompson 
15702ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
15802ac6454SAndrew Thompson 		    ATMEGA_USBCON_USBE |
15902ac6454SAndrew Thompson 		    ATMEGA_USBCON_OTGPADE |
16002ac6454SAndrew Thompson 		    ATMEGA_USBCON_VBUSTE);
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson 		sc->sc_flags.clocks_off = 0;
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 		/* enable transceiver ? */
16502ac6454SAndrew Thompson 	}
16602ac6454SAndrew Thompson }
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson static void
atmegadci_clocks_off(struct atmegadci_softc * sc)16902ac6454SAndrew Thompson atmegadci_clocks_off(struct atmegadci_softc *sc)
17002ac6454SAndrew Thompson {
17102ac6454SAndrew Thompson 	if (!sc->sc_flags.clocks_off) {
17202ac6454SAndrew Thompson 		DPRINTFN(5, "\n");
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson 		/* disable Transceiver ? */
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
17702ac6454SAndrew Thompson 		    ATMEGA_USBCON_USBE |
17802ac6454SAndrew Thompson 		    ATMEGA_USBCON_OTGPADE |
17902ac6454SAndrew Thompson 		    ATMEGA_USBCON_FRZCLK |
18002ac6454SAndrew Thompson 		    ATMEGA_USBCON_VBUSTE);
18102ac6454SAndrew Thompson 
18202ac6454SAndrew Thompson 		/* turn clocks off */
18302ac6454SAndrew Thompson 		(sc->sc_clocks_off) (&sc->sc_bus);
18402ac6454SAndrew Thompson 
18502ac6454SAndrew Thompson 		sc->sc_flags.clocks_off = 1;
18602ac6454SAndrew Thompson 	}
18702ac6454SAndrew Thompson }
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson static void
atmegadci_pull_up(struct atmegadci_softc * sc)19002ac6454SAndrew Thompson atmegadci_pull_up(struct atmegadci_softc *sc)
19102ac6454SAndrew Thompson {
19202ac6454SAndrew Thompson 	/* pullup D+, if possible */
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson 	if (!sc->sc_flags.d_pulled_up &&
19502ac6454SAndrew Thompson 	    sc->sc_flags.port_powered) {
19602ac6454SAndrew Thompson 		sc->sc_flags.d_pulled_up = 1;
19702ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDCON, 0);
19802ac6454SAndrew Thompson 	}
19902ac6454SAndrew Thompson }
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson static void
atmegadci_pull_down(struct atmegadci_softc * sc)20202ac6454SAndrew Thompson atmegadci_pull_down(struct atmegadci_softc *sc)
20302ac6454SAndrew Thompson {
20402ac6454SAndrew Thompson 	/* pulldown D+, if possible */
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 	if (sc->sc_flags.d_pulled_up) {
20702ac6454SAndrew Thompson 		sc->sc_flags.d_pulled_up = 0;
20802ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
20902ac6454SAndrew Thompson 	}
21002ac6454SAndrew Thompson }
21102ac6454SAndrew Thompson 
21202ac6454SAndrew Thompson static void
atmegadci_wakeup_peer(struct atmegadci_softc * sc)21339307315SAndrew Thompson atmegadci_wakeup_peer(struct atmegadci_softc *sc)
21402ac6454SAndrew Thompson {
21502ac6454SAndrew Thompson 	uint8_t temp;
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson 	if (!sc->sc_flags.status_suspend) {
21802ac6454SAndrew Thompson 		return;
21902ac6454SAndrew Thompson 	}
22002ac6454SAndrew Thompson 
22102ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UDCON);
22202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDCON, temp | ATMEGA_UDCON_RMWKUP);
22302ac6454SAndrew Thompson 
22402ac6454SAndrew Thompson 	/* wait 8 milliseconds */
22502ac6454SAndrew Thompson 	/* Wait for reset to complete. */
226a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 	/* hardware should have cleared RMWKUP bit */
22902ac6454SAndrew Thompson }
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson static void
atmegadci_set_address(struct atmegadci_softc * sc,uint8_t addr)23202ac6454SAndrew Thompson atmegadci_set_address(struct atmegadci_softc *sc, uint8_t addr)
23302ac6454SAndrew Thompson {
23402ac6454SAndrew Thompson 	DPRINTFN(5, "addr=%d\n", addr);
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson 	addr |= ATMEGA_UDADDR_ADDEN;
23702ac6454SAndrew Thompson 
23802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, addr);
23902ac6454SAndrew Thompson }
24002ac6454SAndrew Thompson 
24102ac6454SAndrew Thompson static uint8_t
atmegadci_setup_rx(struct atmegadci_td * td)24202ac6454SAndrew Thompson atmegadci_setup_rx(struct atmegadci_td *td)
24302ac6454SAndrew Thompson {
24402ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
245760bc48eSAndrew Thompson 	struct usb_device_request req;
24602ac6454SAndrew Thompson 	uint16_t count;
24702ac6454SAndrew Thompson 	uint8_t temp;
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson 	/* get pointer to softc */
25002ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 	/* select endpoint number */
25302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
25402ac6454SAndrew Thompson 
25502ac6454SAndrew Thompson 	/* check endpoint status */
25602ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 	DPRINTFN(5, "UEINTX=0x%02x\n", temp);
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 	if (!(temp & ATMEGA_UEINTX_RXSTPI)) {
26102ac6454SAndrew Thompson 		goto not_complete;
26202ac6454SAndrew Thompson 	}
263f3464815SAndrew Thompson 	/* clear did stall */
264f3464815SAndrew Thompson 	td->did_stall = 0;
26502ac6454SAndrew Thompson 	/* get the packet byte count */
26602ac6454SAndrew Thompson 	count =
26702ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
26802ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
26902ac6454SAndrew Thompson 
27002ac6454SAndrew Thompson 	/* mask away undefined bits */
27102ac6454SAndrew Thompson 	count &= 0x7FF;
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson 	/* verify data length */
27402ac6454SAndrew Thompson 	if (count != td->remainder) {
27502ac6454SAndrew Thompson 		DPRINTFN(0, "Invalid SETUP packet "
27602ac6454SAndrew Thompson 		    "length, %d bytes\n", count);
27702ac6454SAndrew Thompson 		goto not_complete;
27802ac6454SAndrew Thompson 	}
27902ac6454SAndrew Thompson 	if (count != sizeof(req)) {
28002ac6454SAndrew Thompson 		DPRINTFN(0, "Unsupported SETUP packet "
28102ac6454SAndrew Thompson 		    "length, %d bytes\n", count);
28202ac6454SAndrew Thompson 		goto not_complete;
28302ac6454SAndrew Thompson 	}
28402ac6454SAndrew Thompson 	/* receive data */
28502ac6454SAndrew Thompson 	ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
28602ac6454SAndrew Thompson 	    (void *)&req, sizeof(req));
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 	/* copy data into real buffer */
289a593f6b8SAndrew Thompson 	usbd_copy_in(td->pc, 0, &req, sizeof(req));
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson 	td->offset = sizeof(req);
29202ac6454SAndrew Thompson 	td->remainder = 0;
29302ac6454SAndrew Thompson 
29402ac6454SAndrew Thompson 	/* sneak peek the set address */
29502ac6454SAndrew Thompson 	if ((req.bmRequestType == UT_WRITE_DEVICE) &&
29602ac6454SAndrew Thompson 	    (req.bRequest == UR_SET_ADDRESS)) {
29702ac6454SAndrew Thompson 		sc->sc_dv_addr = req.wValue[0] & 0x7F;
298d59dbd0aSAndrew Thompson 		/* must write address before ZLP */
299d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, sc->sc_dv_addr);
30002ac6454SAndrew Thompson 	} else {
30102ac6454SAndrew Thompson 		sc->sc_dv_addr = 0xFF;
30202ac6454SAndrew Thompson 	}
30302ac6454SAndrew Thompson 
30433ea1323SAndrew Thompson 	/* Clear SETUP packet interrupt and all other previous interrupts */
30533ea1323SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0);
30602ac6454SAndrew Thompson 	return (0);			/* complete */
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson not_complete:
309f3464815SAndrew Thompson 	/* abort any ongoing transfer */
310f3464815SAndrew Thompson 	if (!td->did_stall) {
311f3464815SAndrew Thompson 		DPRINTFN(5, "stalling\n");
312f3464815SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
313f3464815SAndrew Thompson 		    ATMEGA_UECONX_EPEN |
314f3464815SAndrew Thompson 		    ATMEGA_UECONX_STALLRQ);
315f3464815SAndrew Thompson 		td->did_stall = 1;
316f3464815SAndrew Thompson 	}
317eb846b4eSAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
318eb846b4eSAndrew Thompson 		/* clear SETUP packet interrupt */
319eb846b4eSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ~ATMEGA_UEINTX_RXSTPI);
320eb846b4eSAndrew Thompson 	}
32102ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet */
32202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, ATMEGA_UEIENX_RXSTPE);
32302ac6454SAndrew Thompson 	return (1);			/* not complete */
32402ac6454SAndrew Thompson }
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson static uint8_t
atmegadci_data_rx(struct atmegadci_td * td)32702ac6454SAndrew Thompson atmegadci_data_rx(struct atmegadci_td *td)
32802ac6454SAndrew Thompson {
32902ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
330760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
33102ac6454SAndrew Thompson 	uint16_t count;
33202ac6454SAndrew Thompson 	uint8_t temp;
33302ac6454SAndrew Thompson 	uint8_t to;
33402ac6454SAndrew Thompson 	uint8_t got_short;
33502ac6454SAndrew Thompson 
33602ac6454SAndrew Thompson 	to = 3;				/* don't loop forever! */
33702ac6454SAndrew Thompson 	got_short = 0;
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	/* get pointer to softc */
34002ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	/* select endpoint number */
34302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson repeat:
34602ac6454SAndrew Thompson 	/* check if any of the FIFO banks have data */
34702ac6454SAndrew Thompson 	/* check endpoint status */
34802ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
35302ac6454SAndrew Thompson 		if (td->remainder == 0) {
35402ac6454SAndrew Thompson 			/*
35502ac6454SAndrew Thompson 			 * We are actually complete and have
35602ac6454SAndrew Thompson 			 * received the next SETUP
35702ac6454SAndrew Thompson 			 */
35802ac6454SAndrew Thompson 			DPRINTFN(5, "faking complete\n");
35902ac6454SAndrew Thompson 			return (0);	/* complete */
36002ac6454SAndrew Thompson 		}
36102ac6454SAndrew Thompson 		/*
36202ac6454SAndrew Thompson 	         * USB Host Aborted the transfer.
36302ac6454SAndrew Thompson 	         */
36402ac6454SAndrew Thompson 		td->error = 1;
36502ac6454SAndrew Thompson 		return (0);		/* complete */
36602ac6454SAndrew Thompson 	}
36702ac6454SAndrew Thompson 	/* check status */
36802ac6454SAndrew Thompson 	if (!(temp & (ATMEGA_UEINTX_FIFOCON |
36902ac6454SAndrew Thompson 	    ATMEGA_UEINTX_RXOUTI))) {
37002ac6454SAndrew Thompson 		/* no data */
37102ac6454SAndrew Thompson 		goto not_complete;
37202ac6454SAndrew Thompson 	}
37302ac6454SAndrew Thompson 	/* get the packet byte count */
37402ac6454SAndrew Thompson 	count =
37502ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
37602ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	/* mask away undefined bits */
37902ac6454SAndrew Thompson 	count &= 0x7FF;
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	/* verify the packet byte count */
38202ac6454SAndrew Thompson 	if (count != td->max_packet_size) {
38302ac6454SAndrew Thompson 		if (count < td->max_packet_size) {
38402ac6454SAndrew Thompson 			/* we have a short packet */
38502ac6454SAndrew Thompson 			td->short_pkt = 1;
38602ac6454SAndrew Thompson 			got_short = 1;
38702ac6454SAndrew Thompson 		} else {
38802ac6454SAndrew Thompson 			/* invalid USB packet */
38902ac6454SAndrew Thompson 			td->error = 1;
39002ac6454SAndrew Thompson 			return (0);	/* we are complete */
39102ac6454SAndrew Thompson 		}
39202ac6454SAndrew Thompson 	}
39302ac6454SAndrew Thompson 	/* verify the packet byte count */
39402ac6454SAndrew Thompson 	if (count > td->remainder) {
39502ac6454SAndrew Thompson 		/* invalid USB packet */
39602ac6454SAndrew Thompson 		td->error = 1;
39702ac6454SAndrew Thompson 		return (0);		/* we are complete */
39802ac6454SAndrew Thompson 	}
39902ac6454SAndrew Thompson 	while (count > 0) {
400a593f6b8SAndrew Thompson 		usbd_get_page(td->pc, td->offset, &buf_res);
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson 		/* get correct length */
40302ac6454SAndrew Thompson 		if (buf_res.length > count) {
40402ac6454SAndrew Thompson 			buf_res.length = count;
40502ac6454SAndrew Thompson 		}
40602ac6454SAndrew Thompson 		/* receive data */
40702ac6454SAndrew Thompson 		ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
40802ac6454SAndrew Thompson 		    buf_res.buffer, buf_res.length);
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson 		/* update counters */
41102ac6454SAndrew Thompson 		count -= buf_res.length;
41202ac6454SAndrew Thompson 		td->offset += buf_res.length;
41302ac6454SAndrew Thompson 		td->remainder -= buf_res.length;
41402ac6454SAndrew Thompson 	}
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson 	/* clear OUT packet interrupt */
41702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_RXOUTI ^ 0xFF);
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 	/* release FIFO bank */
42002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_FIFOCON ^ 0xFF);
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson 	/* check if we are complete */
42302ac6454SAndrew Thompson 	if ((td->remainder == 0) || got_short) {
42402ac6454SAndrew Thompson 		if (td->short_pkt) {
42502ac6454SAndrew Thompson 			/* we are complete */
42602ac6454SAndrew Thompson 			return (0);
42702ac6454SAndrew Thompson 		}
42802ac6454SAndrew Thompson 		/* else need to receive a zero length packet */
42902ac6454SAndrew Thompson 	}
43002ac6454SAndrew Thompson 	if (--to) {
43102ac6454SAndrew Thompson 		goto repeat;
43202ac6454SAndrew Thompson 	}
43302ac6454SAndrew Thompson not_complete:
43402ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or OUT packet */
43502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
43602ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_RXOUTE);
43702ac6454SAndrew Thompson 	return (1);			/* not complete */
43802ac6454SAndrew Thompson }
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson static uint8_t
atmegadci_data_tx(struct atmegadci_td * td)44102ac6454SAndrew Thompson atmegadci_data_tx(struct atmegadci_td *td)
44202ac6454SAndrew Thompson {
44302ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
444760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
44502ac6454SAndrew Thompson 	uint16_t count;
44602ac6454SAndrew Thompson 	uint8_t to;
44702ac6454SAndrew Thompson 	uint8_t temp;
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	to = 3;				/* don't loop forever! */
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	/* get pointer to softc */
45202ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	/* select endpoint number */
45502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson repeat:
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	/* check endpoint status */
46002ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
46502ac6454SAndrew Thompson 		/*
46602ac6454SAndrew Thompson 	         * The current transfer was aborted
46702ac6454SAndrew Thompson 	         * by the USB Host
46802ac6454SAndrew Thompson 	         */
46902ac6454SAndrew Thompson 		td->error = 1;
47002ac6454SAndrew Thompson 		return (0);		/* complete */
47102ac6454SAndrew Thompson 	}
472c1911c1bSAndrew Thompson 
473c1911c1bSAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
474c1911c1bSAndrew Thompson 	if (temp & 3) {
475c1911c1bSAndrew Thompson 		/* cannot write any data - a bank is busy */
47602ac6454SAndrew Thompson 		goto not_complete;
47702ac6454SAndrew Thompson 	}
478c1911c1bSAndrew Thompson 
47902ac6454SAndrew Thompson 	count = td->max_packet_size;
48002ac6454SAndrew Thompson 	if (td->remainder < count) {
48102ac6454SAndrew Thompson 		/* we have a short packet */
48202ac6454SAndrew Thompson 		td->short_pkt = 1;
48302ac6454SAndrew Thompson 		count = td->remainder;
48402ac6454SAndrew Thompson 	}
48502ac6454SAndrew Thompson 	while (count > 0) {
486a593f6b8SAndrew Thompson 		usbd_get_page(td->pc, td->offset, &buf_res);
48702ac6454SAndrew Thompson 
48802ac6454SAndrew Thompson 		/* get correct length */
48902ac6454SAndrew Thompson 		if (buf_res.length > count) {
49002ac6454SAndrew Thompson 			buf_res.length = count;
49102ac6454SAndrew Thompson 		}
49202ac6454SAndrew Thompson 		/* transmit data */
49302ac6454SAndrew Thompson 		ATMEGA_WRITE_MULTI_1(sc, ATMEGA_UEDATX,
49402ac6454SAndrew Thompson 		    buf_res.buffer, buf_res.length);
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 		/* update counters */
49702ac6454SAndrew Thompson 		count -= buf_res.length;
49802ac6454SAndrew Thompson 		td->offset += buf_res.length;
49902ac6454SAndrew Thompson 		td->remainder -= buf_res.length;
50002ac6454SAndrew Thompson 	}
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson 	/* clear IN packet interrupt */
50302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_TXINI);
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 	/* allocate FIFO bank */
50602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_FIFOCON);
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	/* check remainder */
50902ac6454SAndrew Thompson 	if (td->remainder == 0) {
51002ac6454SAndrew Thompson 		if (td->short_pkt) {
51102ac6454SAndrew Thompson 			return (0);	/* complete */
51202ac6454SAndrew Thompson 		}
51302ac6454SAndrew Thompson 		/* else we need to transmit a short packet */
51402ac6454SAndrew Thompson 	}
51502ac6454SAndrew Thompson 	if (--to) {
51602ac6454SAndrew Thompson 		goto repeat;
51702ac6454SAndrew Thompson 	}
51802ac6454SAndrew Thompson not_complete:
51902ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or free IN packet */
52002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
52102ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
52202ac6454SAndrew Thompson 	return (1);			/* not complete */
52302ac6454SAndrew Thompson }
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson static uint8_t
atmegadci_data_tx_sync(struct atmegadci_td * td)52602ac6454SAndrew Thompson atmegadci_data_tx_sync(struct atmegadci_td *td)
52702ac6454SAndrew Thompson {
52802ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
52902ac6454SAndrew Thompson 	uint8_t temp;
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson 	/* get pointer to softc */
53202ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
53302ac6454SAndrew Thompson 
53402ac6454SAndrew Thompson 	/* select endpoint number */
53502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	/* check endpoint status */
53802ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x\n", temp);
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
54302ac6454SAndrew Thompson 		DPRINTFN(5, "faking complete\n");
54402ac6454SAndrew Thompson 		/* Race condition */
54502ac6454SAndrew Thompson 		return (0);		/* complete */
54602ac6454SAndrew Thompson 	}
54702ac6454SAndrew Thompson 	/*
54802ac6454SAndrew Thompson 	 * The control endpoint has only got one bank, so if that bank
54902ac6454SAndrew Thompson 	 * is free the packet has been transferred!
55002ac6454SAndrew Thompson 	 */
551c1911c1bSAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
552c1911c1bSAndrew Thompson 	if (temp & 3) {
553c1911c1bSAndrew Thompson 		/* cannot write any data - a bank is busy */
55402ac6454SAndrew Thompson 		goto not_complete;
55502ac6454SAndrew Thompson 	}
55602ac6454SAndrew Thompson 	if (sc->sc_dv_addr != 0xFF) {
55702ac6454SAndrew Thompson 		/* set new address */
55802ac6454SAndrew Thompson 		atmegadci_set_address(sc, sc->sc_dv_addr);
55902ac6454SAndrew Thompson 	}
56002ac6454SAndrew Thompson 	return (0);			/* complete */
56102ac6454SAndrew Thompson 
56202ac6454SAndrew Thompson not_complete:
56302ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or free IN packet */
56402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
56502ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
56602ac6454SAndrew Thompson 	return (1);			/* not complete */
56702ac6454SAndrew Thompson }
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson static uint8_t
atmegadci_xfer_do_fifo(struct usb_xfer * xfer)570760bc48eSAndrew Thompson atmegadci_xfer_do_fifo(struct usb_xfer *xfer)
57102ac6454SAndrew Thompson {
57202ac6454SAndrew Thompson 	struct atmegadci_td *td;
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
57702ac6454SAndrew Thompson 	while (1) {
57802ac6454SAndrew Thompson 		if ((td->func) (td)) {
57902ac6454SAndrew Thompson 			/* operation in progress */
58002ac6454SAndrew Thompson 			break;
58102ac6454SAndrew Thompson 		}
58202ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
58302ac6454SAndrew Thompson 			goto done;
58402ac6454SAndrew Thompson 		}
58502ac6454SAndrew Thompson 		if (td->error) {
58602ac6454SAndrew Thompson 			goto done;
58702ac6454SAndrew Thompson 		} else if (td->remainder > 0) {
58802ac6454SAndrew Thompson 			/*
58902ac6454SAndrew Thompson 			 * We had a short transfer. If there is no alternate
59002ac6454SAndrew Thompson 			 * next, stop processing !
59102ac6454SAndrew Thompson 			 */
59202ac6454SAndrew Thompson 			if (!td->alt_next) {
59302ac6454SAndrew Thompson 				goto done;
59402ac6454SAndrew Thompson 			}
59502ac6454SAndrew Thompson 		}
59602ac6454SAndrew Thompson 		/*
59702ac6454SAndrew Thompson 		 * Fetch the next transfer descriptor and transfer
59802ac6454SAndrew Thompson 		 * some flags to the next transfer descriptor
59902ac6454SAndrew Thompson 		 */
60002ac6454SAndrew Thompson 		td = td->obj_next;
60102ac6454SAndrew Thompson 		xfer->td_transfer_cache = td;
60202ac6454SAndrew Thompson 	}
60302ac6454SAndrew Thompson 	return (1);			/* not complete */
60402ac6454SAndrew Thompson 
60502ac6454SAndrew Thompson done:
60602ac6454SAndrew Thompson 	/* compute all actual lengths */
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson 	atmegadci_standard_done(xfer);
60902ac6454SAndrew Thompson 	return (0);			/* complete */
61002ac6454SAndrew Thompson }
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson static void
atmegadci_interrupt_poll(struct atmegadci_softc * sc)61302ac6454SAndrew Thompson atmegadci_interrupt_poll(struct atmegadci_softc *sc)
61402ac6454SAndrew Thompson {
615760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson repeat:
61802ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
61902ac6454SAndrew Thompson 		if (!atmegadci_xfer_do_fifo(xfer)) {
62002ac6454SAndrew Thompson 			/* queue has been modified */
62102ac6454SAndrew Thompson 			goto repeat;
62202ac6454SAndrew Thompson 		}
62302ac6454SAndrew Thompson 	}
62402ac6454SAndrew Thompson }
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson static void
atmegadci_vbus_interrupt(struct atmegadci_softc * sc,uint8_t is_on)62702ac6454SAndrew Thompson atmegadci_vbus_interrupt(struct atmegadci_softc *sc, uint8_t is_on)
62802ac6454SAndrew Thompson {
62902ac6454SAndrew Thompson 	DPRINTFN(5, "vbus = %u\n", is_on);
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson 	if (is_on) {
63202ac6454SAndrew Thompson 		if (!sc->sc_flags.status_vbus) {
63302ac6454SAndrew Thompson 			sc->sc_flags.status_vbus = 1;
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
63602ac6454SAndrew Thompson 
63739307315SAndrew Thompson 			atmegadci_root_intr(sc);
63802ac6454SAndrew Thompson 		}
63902ac6454SAndrew Thompson 	} else {
64002ac6454SAndrew Thompson 		if (sc->sc_flags.status_vbus) {
64102ac6454SAndrew Thompson 			sc->sc_flags.status_vbus = 0;
64202ac6454SAndrew Thompson 			sc->sc_flags.status_bus_reset = 0;
64302ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 0;
64402ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 0;
64502ac6454SAndrew Thompson 			sc->sc_flags.change_connect = 1;
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
64802ac6454SAndrew Thompson 
64939307315SAndrew Thompson 			atmegadci_root_intr(sc);
65002ac6454SAndrew Thompson 		}
65102ac6454SAndrew Thompson 	}
65202ac6454SAndrew Thompson }
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson void
atmegadci_interrupt(struct atmegadci_softc * sc)65502ac6454SAndrew Thompson atmegadci_interrupt(struct atmegadci_softc *sc)
65602ac6454SAndrew Thompson {
65702ac6454SAndrew Thompson 	uint8_t status;
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	/* read interrupt status */
66202ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_UDINT);
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 	/* clear all set interrupts */
665df075012SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDINT, (~status) & 0x7D);
66602ac6454SAndrew Thompson 
667d59dbd0aSAndrew Thompson 	DPRINTFN(14, "UDINT=0x%02x\n", status);
668d59dbd0aSAndrew Thompson 
66902ac6454SAndrew Thompson 	/* check for any bus state change interrupts */
67002ac6454SAndrew Thompson 	if (status & ATMEGA_UDINT_EORSTI) {
67102ac6454SAndrew Thompson 		DPRINTFN(5, "end of reset\n");
67202ac6454SAndrew Thompson 
67302ac6454SAndrew Thompson 		/* set correct state */
67402ac6454SAndrew Thompson 		sc->sc_flags.status_bus_reset = 1;
67502ac6454SAndrew Thompson 		sc->sc_flags.status_suspend = 0;
67602ac6454SAndrew Thompson 		sc->sc_flags.change_suspend = 0;
67702ac6454SAndrew Thompson 		sc->sc_flags.change_connect = 1;
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 		/* disable resume interrupt */
68002ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
68102ac6454SAndrew Thompson 		    ATMEGA_UDINT_SUSPE |
68202ac6454SAndrew Thompson 		    ATMEGA_UDINT_EORSTE);
68302ac6454SAndrew Thompson 
68402ac6454SAndrew Thompson 		/* complete root HUB interrupt endpoint */
68539307315SAndrew Thompson 		atmegadci_root_intr(sc);
68602ac6454SAndrew Thompson 	}
68702ac6454SAndrew Thompson 	/*
68802ac6454SAndrew Thompson 	 * If resume and suspend is set at the same time we interpret
68902ac6454SAndrew Thompson 	 * that like RESUME. Resume is set when there is at least 3
69002ac6454SAndrew Thompson 	 * milliseconds of inactivity on the USB BUS.
69102ac6454SAndrew Thompson 	 */
6927eaa41aeSAndrew Thompson 	if (status & ATMEGA_UDINT_WAKEUPI) {
69302ac6454SAndrew Thompson 		DPRINTFN(5, "resume interrupt\n");
69402ac6454SAndrew Thompson 
69502ac6454SAndrew Thompson 		if (sc->sc_flags.status_suspend) {
69602ac6454SAndrew Thompson 			/* update status bits */
69702ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 0;
69802ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 1;
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 			/* disable resume interrupt */
70102ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
70202ac6454SAndrew Thompson 			    ATMEGA_UDINT_SUSPE |
70302ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSTE);
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
70639307315SAndrew Thompson 			atmegadci_root_intr(sc);
70702ac6454SAndrew Thompson 		}
70802ac6454SAndrew Thompson 	} else if (status & ATMEGA_UDINT_SUSPI) {
70902ac6454SAndrew Thompson 		DPRINTFN(5, "suspend interrupt\n");
71002ac6454SAndrew Thompson 
71102ac6454SAndrew Thompson 		if (!sc->sc_flags.status_suspend) {
71202ac6454SAndrew Thompson 			/* update status bits */
71302ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 1;
71402ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 1;
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 			/* disable suspend interrupt */
71702ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
7187eaa41aeSAndrew Thompson 			    ATMEGA_UDINT_WAKEUPE |
71902ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSTE);
72002ac6454SAndrew Thompson 
72102ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
72239307315SAndrew Thompson 			atmegadci_root_intr(sc);
72302ac6454SAndrew Thompson 		}
72402ac6454SAndrew Thompson 	}
72502ac6454SAndrew Thompson 	/* check VBUS */
72602ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_USBINT);
72702ac6454SAndrew Thompson 
72802ac6454SAndrew Thompson 	/* clear all set interrupts */
729df075012SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBINT, (~status) & 0x03);
73002ac6454SAndrew Thompson 
73102ac6454SAndrew Thompson 	if (status & ATMEGA_USBINT_VBUSTI) {
73202ac6454SAndrew Thompson 		uint8_t temp;
73302ac6454SAndrew Thompson 
734d59dbd0aSAndrew Thompson 		DPRINTFN(5, "USBINT=0x%02x\n", status);
735d59dbd0aSAndrew Thompson 
73602ac6454SAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
73702ac6454SAndrew Thompson 		atmegadci_vbus_interrupt(sc, temp & ATMEGA_USBSTA_VBUS);
73802ac6454SAndrew Thompson 	}
73902ac6454SAndrew Thompson 	/* check for any endpoint interrupts */
74002ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_UEINT);
741df075012SAndrew Thompson 	/* the hardware will clear the UEINT bits automatically */
74202ac6454SAndrew Thompson 	if (status) {
743d59dbd0aSAndrew Thompson 		DPRINTFN(5, "real endpoint interrupt UEINT=0x%02x\n", status);
74402ac6454SAndrew Thompson 
74502ac6454SAndrew Thompson 		atmegadci_interrupt_poll(sc);
74602ac6454SAndrew Thompson 	}
74702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
74802ac6454SAndrew Thompson }
74902ac6454SAndrew Thompson 
75002ac6454SAndrew Thompson static void
atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp * temp)75102ac6454SAndrew Thompson atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp *temp)
75202ac6454SAndrew Thompson {
75302ac6454SAndrew Thompson 	struct atmegadci_td *td;
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 	/* get current Transfer Descriptor */
75602ac6454SAndrew Thompson 	td = temp->td_next;
75702ac6454SAndrew Thompson 	temp->td = td;
75802ac6454SAndrew Thompson 
75902ac6454SAndrew Thompson 	/* prepare for next TD */
76002ac6454SAndrew Thompson 	temp->td_next = td->obj_next;
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 	/* fill out the Transfer Descriptor */
76302ac6454SAndrew Thompson 	td->func = temp->func;
76402ac6454SAndrew Thompson 	td->pc = temp->pc;
76502ac6454SAndrew Thompson 	td->offset = temp->offset;
76602ac6454SAndrew Thompson 	td->remainder = temp->len;
76702ac6454SAndrew Thompson 	td->error = 0;
768476183dfSAndrew Thompson 	td->did_stall = temp->did_stall;
76902ac6454SAndrew Thompson 	td->short_pkt = temp->short_pkt;
77002ac6454SAndrew Thompson 	td->alt_next = temp->setup_alt_next;
77102ac6454SAndrew Thompson }
77202ac6454SAndrew Thompson 
77302ac6454SAndrew Thompson static void
atmegadci_setup_standard_chain(struct usb_xfer * xfer)774760bc48eSAndrew Thompson atmegadci_setup_standard_chain(struct usb_xfer *xfer)
77502ac6454SAndrew Thompson {
77602ac6454SAndrew Thompson 	struct atmegadci_std_temp temp;
77702ac6454SAndrew Thompson 	struct atmegadci_td *td;
77802ac6454SAndrew Thompson 	uint32_t x;
77902ac6454SAndrew Thompson 	uint8_t need_sync;
78002ac6454SAndrew Thompson 
78102ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
782ae60fdfbSAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpointno),
783a593f6b8SAndrew Thompson 	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson 	td = xfer->td_start[0];
78802ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
78902ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 	/* setup temp */
79202ac6454SAndrew Thompson 
79378c94708SAndrew Thompson 	temp.pc = NULL;
79402ac6454SAndrew Thompson 	temp.td = NULL;
79502ac6454SAndrew Thompson 	temp.td_next = xfer->td_start[0];
79602ac6454SAndrew Thompson 	temp.offset = 0;
79740ba168dSHans Petter Selasky 	temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
79840ba168dSHans Petter Selasky 	    xfer->flags_int.isochronous_xfr;
799476183dfSAndrew Thompson 	temp.did_stall = !xfer->flags_int.control_stall;
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
80402ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
80502ac6454SAndrew Thompson 			temp.func = &atmegadci_setup_rx;
80602ac6454SAndrew Thompson 			temp.len = xfer->frlengths[0];
80702ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + 0;
80802ac6454SAndrew Thompson 			temp.short_pkt = temp.len ? 1 : 0;
8090f7d4548SAndrew Thompson 			/* check for last frame */
8100f7d4548SAndrew Thompson 			if (xfer->nframes == 1) {
8110f7d4548SAndrew Thompson 				/* no STATUS stage yet, SETUP is last */
8120f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act)
8130f7d4548SAndrew Thompson 					temp.setup_alt_next = 0;
8140f7d4548SAndrew Thompson 			}
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
81702ac6454SAndrew Thompson 		}
81802ac6454SAndrew Thompson 		x = 1;
81902ac6454SAndrew Thompson 	} else {
82002ac6454SAndrew Thompson 		x = 0;
82102ac6454SAndrew Thompson 	}
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson 	if (x != xfer->nframes) {
824ae60fdfbSAndrew Thompson 		if (xfer->endpointno & UE_DIR_IN) {
82502ac6454SAndrew Thompson 			temp.func = &atmegadci_data_tx;
82602ac6454SAndrew Thompson 			need_sync = 1;
82702ac6454SAndrew Thompson 		} else {
82802ac6454SAndrew Thompson 			temp.func = &atmegadci_data_rx;
82902ac6454SAndrew Thompson 			need_sync = 0;
83002ac6454SAndrew Thompson 		}
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 		/* setup "pc" pointer */
83302ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
83402ac6454SAndrew Thompson 	} else {
83502ac6454SAndrew Thompson 		need_sync = 0;
83602ac6454SAndrew Thompson 	}
83702ac6454SAndrew Thompson 	while (x != xfer->nframes) {
83802ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 		x++;
84302ac6454SAndrew Thompson 
84402ac6454SAndrew Thompson 		if (x == xfer->nframes) {
8450f7d4548SAndrew Thompson 			if (xfer->flags_int.control_xfr) {
8460f7d4548SAndrew Thompson 				if (xfer->flags_int.control_act) {
84702ac6454SAndrew Thompson 					temp.setup_alt_next = 0;
84802ac6454SAndrew Thompson 				}
8490f7d4548SAndrew Thompson 			} else {
8500f7d4548SAndrew Thompson 				temp.setup_alt_next = 0;
8510f7d4548SAndrew Thompson 			}
8520f7d4548SAndrew Thompson 		}
85302ac6454SAndrew Thompson 		if (temp.len == 0) {
85402ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
85502ac6454SAndrew Thompson 
85602ac6454SAndrew Thompson 			temp.short_pkt = 0;
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 		} else {
85902ac6454SAndrew Thompson 			/* regular data transfer */
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson 			temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
86202ac6454SAndrew Thompson 		}
86302ac6454SAndrew Thompson 
86402ac6454SAndrew Thompson 		atmegadci_setup_standard_chain_sub(&temp);
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson 		if (xfer->flags_int.isochronous_xfr) {
86702ac6454SAndrew Thompson 			temp.offset += temp.len;
86802ac6454SAndrew Thompson 		} else {
86902ac6454SAndrew Thompson 			/* get next Page Cache pointer */
87002ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + x;
87102ac6454SAndrew Thompson 		}
87202ac6454SAndrew Thompson 	}
87302ac6454SAndrew Thompson 
8740f7d4548SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
87502ac6454SAndrew Thompson 		/* always setup a valid "pc" pointer for status and sync */
87602ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + 0;
87702ac6454SAndrew Thompson 		temp.len = 0;
87802ac6454SAndrew Thompson 		temp.short_pkt = 0;
8790f7d4548SAndrew Thompson 		temp.setup_alt_next = 0;
88002ac6454SAndrew Thompson 
8810f7d4548SAndrew Thompson 		/* check if we need to sync */
8820f7d4548SAndrew Thompson 		if (need_sync) {
8830f7d4548SAndrew Thompson 			/* we need a SYNC point after TX */
8840f7d4548SAndrew Thompson 			temp.func = &atmegadci_data_tx_sync;
88502ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
88602ac6454SAndrew Thompson 		}
8870f7d4548SAndrew Thompson 
88802ac6454SAndrew Thompson 		/* check if we should append a status stage */
8890f7d4548SAndrew Thompson 		if (!xfer->flags_int.control_act) {
89002ac6454SAndrew Thompson 			/*
89102ac6454SAndrew Thompson 			 * Send a DATA1 message and invert the current
89202ac6454SAndrew Thompson 			 * endpoint direction.
89302ac6454SAndrew Thompson 			 */
894ae60fdfbSAndrew Thompson 			if (xfer->endpointno & UE_DIR_IN) {
89502ac6454SAndrew Thompson 				temp.func = &atmegadci_data_rx;
89602ac6454SAndrew Thompson 				need_sync = 0;
89702ac6454SAndrew Thompson 			} else {
89802ac6454SAndrew Thompson 				temp.func = &atmegadci_data_tx;
89902ac6454SAndrew Thompson 				need_sync = 1;
90002ac6454SAndrew Thompson 			}
90102ac6454SAndrew Thompson 
90202ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
90302ac6454SAndrew Thompson 			if (need_sync) {
90402ac6454SAndrew Thompson 				/* we need a SYNC point after TX */
90502ac6454SAndrew Thompson 				temp.func = &atmegadci_data_tx_sync;
90602ac6454SAndrew Thompson 				atmegadci_setup_standard_chain_sub(&temp);
90702ac6454SAndrew Thompson 			}
90802ac6454SAndrew Thompson 		}
9090f7d4548SAndrew Thompson 	}
91002ac6454SAndrew Thompson 	/* must have at least one frame! */
91102ac6454SAndrew Thompson 	td = temp.td;
91202ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
91302ac6454SAndrew Thompson }
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson static void
atmegadci_timeout(void * arg)91602ac6454SAndrew Thompson atmegadci_timeout(void *arg)
91702ac6454SAndrew Thompson {
918760bc48eSAndrew Thompson 	struct usb_xfer *xfer = arg;
91902ac6454SAndrew Thompson 
92002ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
92102ac6454SAndrew Thompson 
92202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
92302ac6454SAndrew Thompson 
92402ac6454SAndrew Thompson 	/* transfer is transferred */
92502ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_TIMEOUT);
92602ac6454SAndrew Thompson }
92702ac6454SAndrew Thompson 
92802ac6454SAndrew Thompson static void
atmegadci_start_standard_chain(struct usb_xfer * xfer)929760bc48eSAndrew Thompson atmegadci_start_standard_chain(struct usb_xfer *xfer)
93002ac6454SAndrew Thompson {
93102ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
93202ac6454SAndrew Thompson 
93302ac6454SAndrew Thompson 	/* poll one time - will turn on interrupts */
93402ac6454SAndrew Thompson 	if (atmegadci_xfer_do_fifo(xfer)) {
93502ac6454SAndrew Thompson 		/* put transfer on interrupt queue */
936a593f6b8SAndrew Thompson 		usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
93702ac6454SAndrew Thompson 
93802ac6454SAndrew Thompson 		/* start timeout, if any */
93902ac6454SAndrew Thompson 		if (xfer->timeout != 0) {
940a593f6b8SAndrew Thompson 			usbd_transfer_timeout_ms(xfer,
94102ac6454SAndrew Thompson 			    &atmegadci_timeout, xfer->timeout);
94202ac6454SAndrew Thompson 		}
94302ac6454SAndrew Thompson 	}
94402ac6454SAndrew Thompson }
94502ac6454SAndrew Thompson 
94602ac6454SAndrew Thompson static void
atmegadci_root_intr(struct atmegadci_softc * sc)94739307315SAndrew Thompson atmegadci_root_intr(struct atmegadci_softc *sc)
94802ac6454SAndrew Thompson {
94902ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
95202ac6454SAndrew Thompson 
95302ac6454SAndrew Thompson 	/* set port bit */
95402ac6454SAndrew Thompson 	sc->sc_hub_idata[0] = 0x02;	/* we only have one port */
95502ac6454SAndrew Thompson 
95639307315SAndrew Thompson 	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
95739307315SAndrew Thompson 	    sizeof(sc->sc_hub_idata));
95802ac6454SAndrew Thompson  }
95902ac6454SAndrew Thompson 
960e0a69b51SAndrew Thompson static usb_error_t
atmegadci_standard_done_sub(struct usb_xfer * xfer)961760bc48eSAndrew Thompson atmegadci_standard_done_sub(struct usb_xfer *xfer)
96202ac6454SAndrew Thompson {
96302ac6454SAndrew Thompson 	struct atmegadci_td *td;
96402ac6454SAndrew Thompson 	uint32_t len;
96502ac6454SAndrew Thompson 	uint8_t error;
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
96802ac6454SAndrew Thompson 
96902ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson 	do {
97202ac6454SAndrew Thompson 		len = td->remainder;
97302ac6454SAndrew Thompson 
97402ac6454SAndrew Thompson 		if (xfer->aframes != xfer->nframes) {
97502ac6454SAndrew Thompson 			/*
97602ac6454SAndrew Thompson 		         * Verify the length and subtract
97702ac6454SAndrew Thompson 		         * the remainder from "frlengths[]":
97802ac6454SAndrew Thompson 		         */
97902ac6454SAndrew Thompson 			if (len > xfer->frlengths[xfer->aframes]) {
98002ac6454SAndrew Thompson 				td->error = 1;
98102ac6454SAndrew Thompson 			} else {
98202ac6454SAndrew Thompson 				xfer->frlengths[xfer->aframes] -= len;
98302ac6454SAndrew Thompson 			}
98402ac6454SAndrew Thompson 		}
98502ac6454SAndrew Thompson 		/* Check for transfer error */
98602ac6454SAndrew Thompson 		if (td->error) {
98702ac6454SAndrew Thompson 			/* the transfer is finished */
98802ac6454SAndrew Thompson 			error = 1;
98902ac6454SAndrew Thompson 			td = NULL;
99002ac6454SAndrew Thompson 			break;
99102ac6454SAndrew Thompson 		}
99202ac6454SAndrew Thompson 		/* Check for short transfer */
99302ac6454SAndrew Thompson 		if (len > 0) {
99440ba168dSHans Petter Selasky 			if (xfer->flags_int.short_frames_ok ||
99540ba168dSHans Petter Selasky 			    xfer->flags_int.isochronous_xfr) {
99602ac6454SAndrew Thompson 				/* follow alt next */
99702ac6454SAndrew Thompson 				if (td->alt_next) {
99802ac6454SAndrew Thompson 					td = td->obj_next;
99902ac6454SAndrew Thompson 				} else {
100002ac6454SAndrew Thompson 					td = NULL;
100102ac6454SAndrew Thompson 				}
100202ac6454SAndrew Thompson 			} else {
100302ac6454SAndrew Thompson 				/* the transfer is finished */
100402ac6454SAndrew Thompson 				td = NULL;
100502ac6454SAndrew Thompson 			}
100602ac6454SAndrew Thompson 			error = 0;
100702ac6454SAndrew Thompson 			break;
100802ac6454SAndrew Thompson 		}
100902ac6454SAndrew Thompson 		td = td->obj_next;
101002ac6454SAndrew Thompson 
101102ac6454SAndrew Thompson 		/* this USB frame is complete */
101202ac6454SAndrew Thompson 		error = 0;
101302ac6454SAndrew Thompson 		break;
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 	} while (0);
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 	/* update transfer cache */
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 	return (error ?
102202ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
102302ac6454SAndrew Thompson }
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson static void
atmegadci_standard_done(struct usb_xfer * xfer)1026760bc48eSAndrew Thompson atmegadci_standard_done(struct usb_xfer *xfer)
102702ac6454SAndrew Thompson {
1028e0a69b51SAndrew Thompson 	usb_error_t err = 0;
102902ac6454SAndrew Thompson 
1030ae60fdfbSAndrew Thompson 	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1031ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint);
103202ac6454SAndrew Thompson 
103302ac6454SAndrew Thompson 	/* reset scanner */
103402ac6454SAndrew Thompson 
103502ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
103602ac6454SAndrew Thompson 
103702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
103802ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
103902ac6454SAndrew Thompson 			err = atmegadci_standard_done_sub(xfer);
104002ac6454SAndrew Thompson 		}
104102ac6454SAndrew Thompson 		xfer->aframes = 1;
104202ac6454SAndrew Thompson 
104302ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
104402ac6454SAndrew Thompson 			goto done;
104502ac6454SAndrew Thompson 		}
104602ac6454SAndrew Thompson 	}
104702ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
104802ac6454SAndrew Thompson 		err = atmegadci_standard_done_sub(xfer);
104902ac6454SAndrew Thompson 		xfer->aframes++;
105002ac6454SAndrew Thompson 
105102ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
105202ac6454SAndrew Thompson 			goto done;
105302ac6454SAndrew Thompson 		}
105402ac6454SAndrew Thompson 	}
105502ac6454SAndrew Thompson 
105602ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
105702ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
105802ac6454SAndrew Thompson 		err = atmegadci_standard_done_sub(xfer);
105902ac6454SAndrew Thompson 	}
106002ac6454SAndrew Thompson done:
106102ac6454SAndrew Thompson 	atmegadci_device_done(xfer, err);
106202ac6454SAndrew Thompson }
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
106502ac6454SAndrew Thompson  *	atmegadci_device_done
106602ac6454SAndrew Thompson  *
106702ac6454SAndrew Thompson  * NOTE: this function can be called more than one time on the
106802ac6454SAndrew Thompson  * same USB transfer!
106902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
107002ac6454SAndrew Thompson static void
atmegadci_device_done(struct usb_xfer * xfer,usb_error_t error)1071e0a69b51SAndrew Thompson atmegadci_device_done(struct usb_xfer *xfer, usb_error_t error)
107202ac6454SAndrew Thompson {
107302ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
107402ac6454SAndrew Thompson 	uint8_t ep_no;
107502ac6454SAndrew Thompson 
107602ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
107702ac6454SAndrew Thompson 
1078ae60fdfbSAndrew Thompson 	DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
1079ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint, error);
108002ac6454SAndrew Thompson 
1081f29a0724SAndrew Thompson 	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1082ae60fdfbSAndrew Thompson 		ep_no = (xfer->endpointno & UE_ADDR);
108302ac6454SAndrew Thompson 
108402ac6454SAndrew Thompson 		/* select endpoint number */
108502ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
108602ac6454SAndrew Thompson 
108702ac6454SAndrew Thompson 		/* disable endpoint interrupt */
108802ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
108902ac6454SAndrew Thompson 
109002ac6454SAndrew Thompson 		DPRINTFN(15, "disabled interrupts!\n");
109102ac6454SAndrew Thompson 	}
109202ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
1093a593f6b8SAndrew Thompson 	usbd_transfer_done(xfer, error);
109402ac6454SAndrew Thompson }
109502ac6454SAndrew Thompson 
109602ac6454SAndrew Thompson static void
atmegadci_xfer_stall(struct usb_xfer * xfer)1097a5cf1aaaSHans Petter Selasky atmegadci_xfer_stall(struct usb_xfer *xfer)
1098a5cf1aaaSHans Petter Selasky {
1099a5cf1aaaSHans Petter Selasky 	atmegadci_device_done(xfer, USB_ERR_STALLED);
1100a5cf1aaaSHans Petter Selasky }
1101a5cf1aaaSHans Petter Selasky 
1102a5cf1aaaSHans Petter Selasky static void
atmegadci_set_stall(struct usb_device * udev,struct usb_endpoint * ep,uint8_t * did_stall)1103a5cf1aaaSHans Petter Selasky atmegadci_set_stall(struct usb_device *udev,
110429bd7d7eSAndrew Thompson     struct usb_endpoint *ep, uint8_t *did_stall)
110502ac6454SAndrew Thompson {
110602ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
110702ac6454SAndrew Thompson 	uint8_t ep_no;
110802ac6454SAndrew Thompson 
110902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
111002ac6454SAndrew Thompson 
1111ae60fdfbSAndrew Thompson 	DPRINTFN(5, "endpoint=%p\n", ep);
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(udev->bus);
111402ac6454SAndrew Thompson 	/* get endpoint number */
1115ae60fdfbSAndrew Thompson 	ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
111602ac6454SAndrew Thompson 	/* select endpoint number */
111702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
111802ac6454SAndrew Thompson 	/* set stall */
111902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
112002ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
112102ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQ);
112202ac6454SAndrew Thompson }
112302ac6454SAndrew Thompson 
112402ac6454SAndrew Thompson static void
atmegadci_clear_stall_sub(struct atmegadci_softc * sc,uint8_t ep_no,uint8_t ep_type,uint8_t ep_dir)112502ac6454SAndrew Thompson atmegadci_clear_stall_sub(struct atmegadci_softc *sc, uint8_t ep_no,
112602ac6454SAndrew Thompson     uint8_t ep_type, uint8_t ep_dir)
112702ac6454SAndrew Thompson {
112802ac6454SAndrew Thompson 	uint8_t temp;
112902ac6454SAndrew Thompson 
113002ac6454SAndrew Thompson 	if (ep_type == UE_CONTROL) {
113102ac6454SAndrew Thompson 		/* clearing stall is not needed */
113202ac6454SAndrew Thompson 		return;
113302ac6454SAndrew Thompson 	}
113402ac6454SAndrew Thompson 	/* select endpoint number */
113502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
113602ac6454SAndrew Thompson 
113702ac6454SAndrew Thompson 	/* set endpoint reset */
113802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(ep_no));
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson 	/* clear endpoint reset */
114102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
114202ac6454SAndrew Thompson 
114302ac6454SAndrew Thompson 	/* set stall */
114402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
114502ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
114602ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQ);
114702ac6454SAndrew Thompson 
114802ac6454SAndrew Thompson 	/* reset data toggle */
114902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
115002ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
115102ac6454SAndrew Thompson 	    ATMEGA_UECONX_RSTDT);
115202ac6454SAndrew Thompson 
115302ac6454SAndrew Thompson 	/* clear stall */
115402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
115502ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
115602ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQC);
115702ac6454SAndrew Thompson 
1158d59dbd0aSAndrew Thompson 	do {
115902ac6454SAndrew Thompson 		if (ep_type == UE_BULK) {
11607eaa41aeSAndrew Thompson 			temp = ATMEGA_UECFG0X_EPTYPE2;
116102ac6454SAndrew Thompson 		} else if (ep_type == UE_INTERRUPT) {
11627eaa41aeSAndrew Thompson 			temp = ATMEGA_UECFG0X_EPTYPE3;
116302ac6454SAndrew Thompson 		} else {
11647eaa41aeSAndrew Thompson 			temp = ATMEGA_UECFG0X_EPTYPE1;
116502ac6454SAndrew Thompson 		}
116602ac6454SAndrew Thompson 		if (ep_dir & UE_DIR_IN) {
116702ac6454SAndrew Thompson 			temp |= ATMEGA_UECFG0X_EPDIR;
116802ac6454SAndrew Thompson 		}
116902ac6454SAndrew Thompson 		/* two banks, 64-bytes wMaxPacket */
117002ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, temp);
117102ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
117202ac6454SAndrew Thompson 		    ATMEGA_UECFG1X_ALLOC |
1173c1911c1bSAndrew Thompson 		    ATMEGA_UECFG1X_EPBK0 |	/* one bank */
1174d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPSIZE(3));
117502ac6454SAndrew Thompson 
117602ac6454SAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
117702ac6454SAndrew Thompson 		if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
1178767cb2e2SAndrew Thompson 			device_printf(sc->sc_bus.bdev,
1179767cb2e2SAndrew Thompson 			    "Chip rejected configuration\n");
118002ac6454SAndrew Thompson 		}
1181d59dbd0aSAndrew Thompson 	} while (0);
118202ac6454SAndrew Thompson }
118302ac6454SAndrew Thompson 
118402ac6454SAndrew Thompson static void
atmegadci_clear_stall(struct usb_device * udev,struct usb_endpoint * ep)1185ae60fdfbSAndrew Thompson atmegadci_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
118602ac6454SAndrew Thompson {
118702ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
1188760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
118902ac6454SAndrew Thompson 
1190ae60fdfbSAndrew Thompson 	DPRINTFN(5, "endpoint=%p\n", ep);
119102ac6454SAndrew Thompson 
119202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
119302ac6454SAndrew Thompson 
119402ac6454SAndrew Thompson 	/* check mode */
1195f29a0724SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_DEVICE) {
119602ac6454SAndrew Thompson 		/* not supported */
119702ac6454SAndrew Thompson 		return;
119802ac6454SAndrew Thompson 	}
119902ac6454SAndrew Thompson 	/* get softc */
120002ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(udev->bus);
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 	/* get endpoint descriptor */
1203ae60fdfbSAndrew Thompson 	ed = ep->edesc;
120402ac6454SAndrew Thompson 
120502ac6454SAndrew Thompson 	/* reset endpoint */
120602ac6454SAndrew Thompson 	atmegadci_clear_stall_sub(sc,
120702ac6454SAndrew Thompson 	    (ed->bEndpointAddress & UE_ADDR),
120802ac6454SAndrew Thompson 	    (ed->bmAttributes & UE_XFERTYPE),
120902ac6454SAndrew Thompson 	    (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
121002ac6454SAndrew Thompson }
121102ac6454SAndrew Thompson 
1212e0a69b51SAndrew Thompson usb_error_t
atmegadci_init(struct atmegadci_softc * sc)121302ac6454SAndrew Thompson atmegadci_init(struct atmegadci_softc *sc)
121402ac6454SAndrew Thompson {
121502ac6454SAndrew Thompson 	uint8_t n;
121602ac6454SAndrew Thompson 
121702ac6454SAndrew Thompson 	DPRINTF("start\n");
121802ac6454SAndrew Thompson 
121902ac6454SAndrew Thompson 	/* set up the bus structure */
122002ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_1_1;
122102ac6454SAndrew Thompson 	sc->sc_bus.methods = &atmegadci_bus_methods;
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
12247eaa41aeSAndrew Thompson 
12257eaa41aeSAndrew Thompson 	/* make sure USB is enabled */
12267eaa41aeSAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
12277eaa41aeSAndrew Thompson 	    ATMEGA_USBCON_USBE |
12287eaa41aeSAndrew Thompson 	    ATMEGA_USBCON_FRZCLK);
122902ac6454SAndrew Thompson 
123002ac6454SAndrew Thompson 	/* enable USB PAD regulator */
123102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UHWCON,
12327eaa41aeSAndrew Thompson 	    ATMEGA_UHWCON_UVREGE |
12337eaa41aeSAndrew Thompson 	    ATMEGA_UHWCON_UIMOD);
12347eaa41aeSAndrew Thompson 
12357eaa41aeSAndrew Thompson 	/* the following register sets up the USB PLL, assuming 16MHz X-tal */
12367eaa41aeSAndrew Thompson 	ATMEGA_WRITE_1(sc, 0x49 /* PLLCSR */, 0x14 | 0x02);
12377eaa41aeSAndrew Thompson 
12387eaa41aeSAndrew Thompson 	/* wait for PLL to lock */
12397eaa41aeSAndrew Thompson 	for (n = 0; n != 20; n++) {
12407eaa41aeSAndrew Thompson 		if (ATMEGA_READ_1(sc, 0x49) & 0x01)
12417eaa41aeSAndrew Thompson 			break;
12427eaa41aeSAndrew Thompson 		/* wait a little bit for PLL to start */
1243a593f6b8SAndrew Thompson 		usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
12447eaa41aeSAndrew Thompson 	}
12457eaa41aeSAndrew Thompson 
1246df075012SAndrew Thompson 	/* make sure USB is enabled */
1247df075012SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
1248df075012SAndrew Thompson 	    ATMEGA_USBCON_USBE |
1249df075012SAndrew Thompson 	    ATMEGA_USBCON_OTGPADE |
1250df075012SAndrew Thompson 	    ATMEGA_USBCON_VBUSTE);
1251df075012SAndrew Thompson 
125202ac6454SAndrew Thompson 	/* turn on clocks */
125302ac6454SAndrew Thompson 	(sc->sc_clocks_on) (&sc->sc_bus);
125402ac6454SAndrew Thompson 
1255d59dbd0aSAndrew Thompson 	/* make sure device is re-enumerated */
1256d59dbd0aSAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
1257d59dbd0aSAndrew Thompson 
125802ac6454SAndrew Thompson 	/* wait a little for things to stabilise */
1259a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
126002ac6454SAndrew Thompson 
126102ac6454SAndrew Thompson 	/* enable interrupts */
126202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
126302ac6454SAndrew Thompson 	    ATMEGA_UDINT_SUSPE |
126402ac6454SAndrew Thompson 	    ATMEGA_UDINT_EORSTE);
126502ac6454SAndrew Thompson 
126602ac6454SAndrew Thompson 	/* reset all endpoints */
126702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST,
126802ac6454SAndrew Thompson 	    (1 << ATMEGA_EP_MAX) - 1);
126902ac6454SAndrew Thompson 
127002ac6454SAndrew Thompson 	/* disable reset */
127102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
127202ac6454SAndrew Thompson 
127302ac6454SAndrew Thompson 	/* disable all endpoints */
1274d59dbd0aSAndrew Thompson 	for (n = 0; n != ATMEGA_EP_MAX; n++) {
127502ac6454SAndrew Thompson 		/* select endpoint */
127602ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, n);
127702ac6454SAndrew Thompson 
127802ac6454SAndrew Thompson 		/* disable endpoint interrupt */
127902ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
128002ac6454SAndrew Thompson 
128102ac6454SAndrew Thompson 		/* disable endpoint */
128202ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 0);
128302ac6454SAndrew Thompson 	}
128402ac6454SAndrew Thompson 
128502ac6454SAndrew Thompson 	/* turn off clocks */
128602ac6454SAndrew Thompson 
128702ac6454SAndrew Thompson 	atmegadci_clocks_off(sc);
128802ac6454SAndrew Thompson 
1289d59dbd0aSAndrew Thompson 	/* read initial VBUS state */
1290d59dbd0aSAndrew Thompson 
1291d59dbd0aSAndrew Thompson 	n = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
1292d59dbd0aSAndrew Thompson 	atmegadci_vbus_interrupt(sc, n & ATMEGA_USBSTA_VBUS);
1293d59dbd0aSAndrew Thompson 
129402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 	/* catch any lost interrupts */
129702ac6454SAndrew Thompson 
129802ac6454SAndrew Thompson 	atmegadci_do_poll(&sc->sc_bus);
129902ac6454SAndrew Thompson 
130002ac6454SAndrew Thompson 	return (0);			/* success */
130102ac6454SAndrew Thompson }
130202ac6454SAndrew Thompson 
130302ac6454SAndrew Thompson void
atmegadci_uninit(struct atmegadci_softc * sc)130402ac6454SAndrew Thompson atmegadci_uninit(struct atmegadci_softc *sc)
130502ac6454SAndrew Thompson {
130602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
130702ac6454SAndrew Thompson 
130802ac6454SAndrew Thompson 	/* turn on clocks */
130902ac6454SAndrew Thompson 	(sc->sc_clocks_on) (&sc->sc_bus);
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 	/* disable interrupts */
131202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 0);
131302ac6454SAndrew Thompson 
131402ac6454SAndrew Thompson 	/* reset all endpoints */
131502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST,
131602ac6454SAndrew Thompson 	    (1 << ATMEGA_EP_MAX) - 1);
131702ac6454SAndrew Thompson 
131802ac6454SAndrew Thompson 	/* disable reset */
131902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
132002ac6454SAndrew Thompson 
132102ac6454SAndrew Thompson 	sc->sc_flags.port_powered = 0;
132202ac6454SAndrew Thompson 	sc->sc_flags.status_vbus = 0;
132302ac6454SAndrew Thompson 	sc->sc_flags.status_bus_reset = 0;
132402ac6454SAndrew Thompson 	sc->sc_flags.status_suspend = 0;
132502ac6454SAndrew Thompson 	sc->sc_flags.change_suspend = 0;
132602ac6454SAndrew Thompson 	sc->sc_flags.change_connect = 1;
132702ac6454SAndrew Thompson 
132802ac6454SAndrew Thompson 	atmegadci_pull_down(sc);
132902ac6454SAndrew Thompson 	atmegadci_clocks_off(sc);
133002ac6454SAndrew Thompson 
133102ac6454SAndrew Thompson 	/* disable USB PAD regulator */
133202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 0);
133302ac6454SAndrew Thompson 
133402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
133502ac6454SAndrew Thompson }
133602ac6454SAndrew Thompson 
13372e141748SHans Petter Selasky static void
atmegadci_suspend(struct atmegadci_softc * sc)133802ac6454SAndrew Thompson atmegadci_suspend(struct atmegadci_softc *sc)
133902ac6454SAndrew Thompson {
13402e141748SHans Petter Selasky 	/* TODO */
134102ac6454SAndrew Thompson }
134202ac6454SAndrew Thompson 
13432e141748SHans Petter Selasky static void
atmegadci_resume(struct atmegadci_softc * sc)134402ac6454SAndrew Thompson atmegadci_resume(struct atmegadci_softc *sc)
134502ac6454SAndrew Thompson {
13462e141748SHans Petter Selasky 	/* TODO */
134702ac6454SAndrew Thompson }
134802ac6454SAndrew Thompson 
134902ac6454SAndrew Thompson static void
atmegadci_do_poll(struct usb_bus * bus)1350760bc48eSAndrew Thompson atmegadci_do_poll(struct usb_bus *bus)
135102ac6454SAndrew Thompson {
135202ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
135302ac6454SAndrew Thompson 
135402ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
135502ac6454SAndrew Thompson 	atmegadci_interrupt_poll(sc);
135602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
135702ac6454SAndrew Thompson }
135802ac6454SAndrew Thompson 
135902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
136040ba168dSHans Petter Selasky  * atmegadci bulk support
136140ba168dSHans Petter Selasky  * atmegadci control support
136240ba168dSHans Petter Selasky  * atmegadci interrupt support
136302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
136402ac6454SAndrew Thompson static void
atmegadci_device_non_isoc_open(struct usb_xfer * xfer)1365760bc48eSAndrew Thompson atmegadci_device_non_isoc_open(struct usb_xfer *xfer)
136602ac6454SAndrew Thompson {
136702ac6454SAndrew Thompson 	return;
136802ac6454SAndrew Thompson }
136902ac6454SAndrew Thompson 
137002ac6454SAndrew Thompson static void
atmegadci_device_non_isoc_close(struct usb_xfer * xfer)1371760bc48eSAndrew Thompson atmegadci_device_non_isoc_close(struct usb_xfer *xfer)
137202ac6454SAndrew Thompson {
137302ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
137402ac6454SAndrew Thompson }
137502ac6454SAndrew Thompson 
137602ac6454SAndrew Thompson static void
atmegadci_device_non_isoc_enter(struct usb_xfer * xfer)1377760bc48eSAndrew Thompson atmegadci_device_non_isoc_enter(struct usb_xfer *xfer)
137802ac6454SAndrew Thompson {
137902ac6454SAndrew Thompson 	return;
138002ac6454SAndrew Thompson }
138102ac6454SAndrew Thompson 
138202ac6454SAndrew Thompson static void
atmegadci_device_non_isoc_start(struct usb_xfer * xfer)1383760bc48eSAndrew Thompson atmegadci_device_non_isoc_start(struct usb_xfer *xfer)
138402ac6454SAndrew Thompson {
138502ac6454SAndrew Thompson 	/* setup TDs */
138602ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
138702ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
138802ac6454SAndrew Thompson }
138902ac6454SAndrew Thompson 
1390e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_non_isoc_methods =
139102ac6454SAndrew Thompson {
13923b01d513SAndrew Thompson 	.open = atmegadci_device_non_isoc_open,
13933b01d513SAndrew Thompson 	.close = atmegadci_device_non_isoc_close,
13943b01d513SAndrew Thompson 	.enter = atmegadci_device_non_isoc_enter,
13953b01d513SAndrew Thompson 	.start = atmegadci_device_non_isoc_start,
139602ac6454SAndrew Thompson };
139702ac6454SAndrew Thompson 
139802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
139940ba168dSHans Petter Selasky  * atmegadci full speed isochronous support
140002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
140102ac6454SAndrew Thompson static void
atmegadci_device_isoc_fs_open(struct usb_xfer * xfer)1402760bc48eSAndrew Thompson atmegadci_device_isoc_fs_open(struct usb_xfer *xfer)
140302ac6454SAndrew Thompson {
140402ac6454SAndrew Thompson 	return;
140502ac6454SAndrew Thompson }
140602ac6454SAndrew Thompson 
140702ac6454SAndrew Thompson static void
atmegadci_device_isoc_fs_close(struct usb_xfer * xfer)1408760bc48eSAndrew Thompson atmegadci_device_isoc_fs_close(struct usb_xfer *xfer)
140902ac6454SAndrew Thompson {
141002ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
141102ac6454SAndrew Thompson }
141202ac6454SAndrew Thompson 
141302ac6454SAndrew Thompson static void
atmegadci_device_isoc_fs_enter(struct usb_xfer * xfer)1414760bc48eSAndrew Thompson atmegadci_device_isoc_fs_enter(struct usb_xfer *xfer)
141502ac6454SAndrew Thompson {
141602ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
141702ac6454SAndrew Thompson 	uint32_t nframes;
141802ac6454SAndrew Thompson 
141902ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1420ae60fdfbSAndrew Thompson 	    xfer, xfer->endpoint->isoc_next, xfer->nframes);
142102ac6454SAndrew Thompson 
142202ac6454SAndrew Thompson 	/* get the current frame index */
142302ac6454SAndrew Thompson 
142402ac6454SAndrew Thompson 	nframes =
142502ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UDFNUMH) << 8) |
142602ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UDFNUML));
142702ac6454SAndrew Thompson 
14288fc2a3c4SHans Petter Selasky 	if (usbd_xfer_get_isochronous_start_frame(
14298fc2a3c4SHans Petter Selasky 	    xfer, nframes, 0, 1, ATMEGA_FRAME_MASK, NULL))
1430ae60fdfbSAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
143102ac6454SAndrew Thompson 
143202ac6454SAndrew Thompson 	/* setup TDs */
143302ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
143402ac6454SAndrew Thompson }
143502ac6454SAndrew Thompson 
143602ac6454SAndrew Thompson static void
atmegadci_device_isoc_fs_start(struct usb_xfer * xfer)1437760bc48eSAndrew Thompson atmegadci_device_isoc_fs_start(struct usb_xfer *xfer)
143802ac6454SAndrew Thompson {
143902ac6454SAndrew Thompson 	/* start TD chain */
144002ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
144102ac6454SAndrew Thompson }
144202ac6454SAndrew Thompson 
1443e892b3feSHans Petter Selasky static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods =
144402ac6454SAndrew Thompson {
144502ac6454SAndrew Thompson 	.open = atmegadci_device_isoc_fs_open,
144602ac6454SAndrew Thompson 	.close = atmegadci_device_isoc_fs_close,
144702ac6454SAndrew Thompson 	.enter = atmegadci_device_isoc_fs_enter,
144802ac6454SAndrew Thompson 	.start = atmegadci_device_isoc_fs_start,
144902ac6454SAndrew Thompson };
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
145240ba168dSHans Petter Selasky  * atmegadci root control support
145302ac6454SAndrew Thompson  *------------------------------------------------------------------------*
145439307315SAndrew Thompson  * Simulate a hardware HUB by handling all the necessary requests.
145502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
145602ac6454SAndrew Thompson 
1457760bc48eSAndrew Thompson static const struct usb_device_descriptor atmegadci_devd = {
1458760bc48eSAndrew Thompson 	.bLength = sizeof(struct usb_device_descriptor),
145902ac6454SAndrew Thompson 	.bDescriptorType = UDESC_DEVICE,
146002ac6454SAndrew Thompson 	.bcdUSB = {0x00, 0x02},
146102ac6454SAndrew Thompson 	.bDeviceClass = UDCLASS_HUB,
146202ac6454SAndrew Thompson 	.bDeviceSubClass = UDSUBCLASS_HUB,
14633b6f59eeSHans Petter Selasky 	.bDeviceProtocol = UDPROTO_FSHUB,
146402ac6454SAndrew Thompson 	.bMaxPacketSize = 64,
146502ac6454SAndrew Thompson 	.bcdDevice = {0x00, 0x01},
146602ac6454SAndrew Thompson 	.iManufacturer = 1,
146702ac6454SAndrew Thompson 	.iProduct = 2,
146802ac6454SAndrew Thompson 	.bNumConfigurations = 1,
146902ac6454SAndrew Thompson };
147002ac6454SAndrew Thompson 
147102ac6454SAndrew Thompson static const struct atmegadci_config_desc atmegadci_confd = {
147202ac6454SAndrew Thompson 	.confd = {
1473760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_config_descriptor),
147402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
147502ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(atmegadci_confd),
147602ac6454SAndrew Thompson 		.bNumInterface = 1,
147702ac6454SAndrew Thompson 		.bConfigurationValue = 1,
147802ac6454SAndrew Thompson 		.iConfiguration = 0,
147902ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
148002ac6454SAndrew Thompson 		.bMaxPower = 0,
148102ac6454SAndrew Thompson 	},
148202ac6454SAndrew Thompson 	.ifcd = {
1483760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_interface_descriptor),
148402ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
148502ac6454SAndrew Thompson 		.bNumEndpoints = 1,
148602ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
148702ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
14883b6f59eeSHans Petter Selasky 		.bInterfaceProtocol = 0,
148902ac6454SAndrew Thompson 	},
149002ac6454SAndrew Thompson 	.endpd = {
1491760bc48eSAndrew Thompson 		.bLength = sizeof(struct usb_endpoint_descriptor),
149202ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
149302ac6454SAndrew Thompson 		.bEndpointAddress = (UE_DIR_IN | ATMEGA_INTR_ENDPT),
149402ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
149502ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,
149602ac6454SAndrew Thompson 		.bInterval = 255,
149702ac6454SAndrew Thompson 	},
149802ac6454SAndrew Thompson };
14996d917491SHans Petter Selasky #define	HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
15006d917491SHans Petter Selasky 
1501760bc48eSAndrew Thompson static const struct usb_hub_descriptor_min atmegadci_hubd = {
150202ac6454SAndrew Thompson 	.bDescLength = sizeof(atmegadci_hubd),
150302ac6454SAndrew Thompson 	.bDescriptorType = UDESC_HUB,
150402ac6454SAndrew Thompson 	.bNbrPorts = 1,
15056d917491SHans Petter Selasky 	HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
150602ac6454SAndrew Thompson 	.bPwrOn2PwrGood = 50,
150702ac6454SAndrew Thompson 	.bHubContrCurrent = 0,
150802ac6454SAndrew Thompson 	.DeviceRemovable = {0},		/* port is removable */
150902ac6454SAndrew Thompson };
151002ac6454SAndrew Thompson 
151102ac6454SAndrew Thompson #define	STRING_VENDOR \
1512b51875c9SHans Petter Selasky   "A\0T\0M\0E\0G\0A"
151302ac6454SAndrew Thompson 
151402ac6454SAndrew Thompson #define	STRING_PRODUCT \
1515b51875c9SHans Petter Selasky   "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
151602ac6454SAndrew Thompson 
151702ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_VENDOR, atmegadci_vendor);
151802ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_PRODUCT, atmegadci_product);
151902ac6454SAndrew Thompson 
1520e0a69b51SAndrew Thompson static usb_error_t
atmegadci_roothub_exec(struct usb_device * udev,struct usb_device_request * req,const void ** pptr,uint16_t * plength)1521760bc48eSAndrew Thompson atmegadci_roothub_exec(struct usb_device *udev,
1522760bc48eSAndrew Thompson     struct usb_device_request *req, const void **pptr, uint16_t *plength)
152302ac6454SAndrew Thompson {
1524459d369eSAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
1525459d369eSAndrew Thompson 	const void *ptr;
1526459d369eSAndrew Thompson 	uint16_t len;
152702ac6454SAndrew Thompson 	uint16_t value;
152802ac6454SAndrew Thompson 	uint16_t index;
1529d59dbd0aSAndrew Thompson 	uint8_t temp;
1530e0a69b51SAndrew Thompson 	usb_error_t err;
153102ac6454SAndrew Thompson 
153202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
153302ac6454SAndrew Thompson 
153402ac6454SAndrew Thompson 	/* buffer reset */
1535459d369eSAndrew Thompson 	ptr = (const void *)&sc->sc_hub_temp;
1536459d369eSAndrew Thompson 	len = 0;
1537459d369eSAndrew Thompson 	err = 0;
153802ac6454SAndrew Thompson 
1539459d369eSAndrew Thompson 	value = UGETW(req->wValue);
1540459d369eSAndrew Thompson 	index = UGETW(req->wIndex);
154102ac6454SAndrew Thompson 
154202ac6454SAndrew Thompson 	/* demultiplex the control request */
154302ac6454SAndrew Thompson 
1544459d369eSAndrew Thompson 	switch (req->bmRequestType) {
154502ac6454SAndrew Thompson 	case UT_READ_DEVICE:
1546459d369eSAndrew Thompson 		switch (req->bRequest) {
154702ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
154802ac6454SAndrew Thompson 			goto tr_handle_get_descriptor;
154902ac6454SAndrew Thompson 		case UR_GET_CONFIG:
155002ac6454SAndrew Thompson 			goto tr_handle_get_config;
155102ac6454SAndrew Thompson 		case UR_GET_STATUS:
155202ac6454SAndrew Thompson 			goto tr_handle_get_status;
155302ac6454SAndrew Thompson 		default:
155402ac6454SAndrew Thompson 			goto tr_stalled;
155502ac6454SAndrew Thompson 		}
155602ac6454SAndrew Thompson 		break;
155702ac6454SAndrew Thompson 
155802ac6454SAndrew Thompson 	case UT_WRITE_DEVICE:
1559459d369eSAndrew Thompson 		switch (req->bRequest) {
156002ac6454SAndrew Thompson 		case UR_SET_ADDRESS:
156102ac6454SAndrew Thompson 			goto tr_handle_set_address;
156202ac6454SAndrew Thompson 		case UR_SET_CONFIG:
156302ac6454SAndrew Thompson 			goto tr_handle_set_config;
156402ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
156502ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
156602ac6454SAndrew Thompson 		case UR_SET_DESCRIPTOR:
156702ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
156802ac6454SAndrew Thompson 		case UR_SET_FEATURE:
156902ac6454SAndrew Thompson 		default:
157002ac6454SAndrew Thompson 			goto tr_stalled;
157102ac6454SAndrew Thompson 		}
157202ac6454SAndrew Thompson 		break;
157302ac6454SAndrew Thompson 
157402ac6454SAndrew Thompson 	case UT_WRITE_ENDPOINT:
1575459d369eSAndrew Thompson 		switch (req->bRequest) {
157602ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
1577459d369eSAndrew Thompson 			switch (UGETW(req->wValue)) {
157802ac6454SAndrew Thompson 			case UF_ENDPOINT_HALT:
157902ac6454SAndrew Thompson 				goto tr_handle_clear_halt;
158002ac6454SAndrew Thompson 			case UF_DEVICE_REMOTE_WAKEUP:
158102ac6454SAndrew Thompson 				goto tr_handle_clear_wakeup;
158202ac6454SAndrew Thompson 			default:
158302ac6454SAndrew Thompson 				goto tr_stalled;
158402ac6454SAndrew Thompson 			}
158502ac6454SAndrew Thompson 			break;
158602ac6454SAndrew Thompson 		case UR_SET_FEATURE:
1587459d369eSAndrew Thompson 			switch (UGETW(req->wValue)) {
158802ac6454SAndrew Thompson 			case UF_ENDPOINT_HALT:
158902ac6454SAndrew Thompson 				goto tr_handle_set_halt;
159002ac6454SAndrew Thompson 			case UF_DEVICE_REMOTE_WAKEUP:
159102ac6454SAndrew Thompson 				goto tr_handle_set_wakeup;
159202ac6454SAndrew Thompson 			default:
159302ac6454SAndrew Thompson 				goto tr_stalled;
159402ac6454SAndrew Thompson 			}
159502ac6454SAndrew Thompson 			break;
159602ac6454SAndrew Thompson 		case UR_SYNCH_FRAME:
159702ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
159802ac6454SAndrew Thompson 		default:
159902ac6454SAndrew Thompson 			goto tr_stalled;
160002ac6454SAndrew Thompson 		}
160102ac6454SAndrew Thompson 		break;
160202ac6454SAndrew Thompson 
160302ac6454SAndrew Thompson 	case UT_READ_ENDPOINT:
1604459d369eSAndrew Thompson 		switch (req->bRequest) {
160502ac6454SAndrew Thompson 		case UR_GET_STATUS:
160602ac6454SAndrew Thompson 			goto tr_handle_get_ep_status;
160702ac6454SAndrew Thompson 		default:
160802ac6454SAndrew Thompson 			goto tr_stalled;
160902ac6454SAndrew Thompson 		}
161002ac6454SAndrew Thompson 		break;
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 	case UT_WRITE_INTERFACE:
1613459d369eSAndrew Thompson 		switch (req->bRequest) {
161402ac6454SAndrew Thompson 		case UR_SET_INTERFACE:
161502ac6454SAndrew Thompson 			goto tr_handle_set_interface;
161602ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
161702ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
161802ac6454SAndrew Thompson 		case UR_SET_FEATURE:
161902ac6454SAndrew Thompson 		default:
162002ac6454SAndrew Thompson 			goto tr_stalled;
162102ac6454SAndrew Thompson 		}
162202ac6454SAndrew Thompson 		break;
162302ac6454SAndrew Thompson 
162402ac6454SAndrew Thompson 	case UT_READ_INTERFACE:
1625459d369eSAndrew Thompson 		switch (req->bRequest) {
162602ac6454SAndrew Thompson 		case UR_GET_INTERFACE:
162702ac6454SAndrew Thompson 			goto tr_handle_get_interface;
162802ac6454SAndrew Thompson 		case UR_GET_STATUS:
162902ac6454SAndrew Thompson 			goto tr_handle_get_iface_status;
163002ac6454SAndrew Thompson 		default:
163102ac6454SAndrew Thompson 			goto tr_stalled;
163202ac6454SAndrew Thompson 		}
163302ac6454SAndrew Thompson 		break;
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 	case UT_WRITE_CLASS_INTERFACE:
163602ac6454SAndrew Thompson 	case UT_WRITE_VENDOR_INTERFACE:
163702ac6454SAndrew Thompson 		/* XXX forward */
163802ac6454SAndrew Thompson 		break;
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson 	case UT_READ_CLASS_INTERFACE:
164102ac6454SAndrew Thompson 	case UT_READ_VENDOR_INTERFACE:
164202ac6454SAndrew Thompson 		/* XXX forward */
164302ac6454SAndrew Thompson 		break;
164402ac6454SAndrew Thompson 
164502ac6454SAndrew Thompson 	case UT_WRITE_CLASS_DEVICE:
1646459d369eSAndrew Thompson 		switch (req->bRequest) {
164702ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
164802ac6454SAndrew Thompson 			goto tr_valid;
164902ac6454SAndrew Thompson 		case UR_SET_DESCRIPTOR:
165002ac6454SAndrew Thompson 		case UR_SET_FEATURE:
165102ac6454SAndrew Thompson 			break;
165202ac6454SAndrew Thompson 		default:
165302ac6454SAndrew Thompson 			goto tr_stalled;
165402ac6454SAndrew Thompson 		}
165502ac6454SAndrew Thompson 		break;
165602ac6454SAndrew Thompson 
165702ac6454SAndrew Thompson 	case UT_WRITE_CLASS_OTHER:
1658459d369eSAndrew Thompson 		switch (req->bRequest) {
165902ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
166002ac6454SAndrew Thompson 			goto tr_handle_clear_port_feature;
166102ac6454SAndrew Thompson 		case UR_SET_FEATURE:
166202ac6454SAndrew Thompson 			goto tr_handle_set_port_feature;
166302ac6454SAndrew Thompson 		case UR_CLEAR_TT_BUFFER:
166402ac6454SAndrew Thompson 		case UR_RESET_TT:
166502ac6454SAndrew Thompson 		case UR_STOP_TT:
166602ac6454SAndrew Thompson 			goto tr_valid;
166702ac6454SAndrew Thompson 
166802ac6454SAndrew Thompson 		default:
166902ac6454SAndrew Thompson 			goto tr_stalled;
167002ac6454SAndrew Thompson 		}
167102ac6454SAndrew Thompson 		break;
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 	case UT_READ_CLASS_OTHER:
1674459d369eSAndrew Thompson 		switch (req->bRequest) {
167502ac6454SAndrew Thompson 		case UR_GET_TT_STATE:
167602ac6454SAndrew Thompson 			goto tr_handle_get_tt_state;
167702ac6454SAndrew Thompson 		case UR_GET_STATUS:
167802ac6454SAndrew Thompson 			goto tr_handle_get_port_status;
167902ac6454SAndrew Thompson 		default:
168002ac6454SAndrew Thompson 			goto tr_stalled;
168102ac6454SAndrew Thompson 		}
168202ac6454SAndrew Thompson 		break;
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson 	case UT_READ_CLASS_DEVICE:
1685459d369eSAndrew Thompson 		switch (req->bRequest) {
168602ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
168702ac6454SAndrew Thompson 			goto tr_handle_get_class_descriptor;
168802ac6454SAndrew Thompson 		case UR_GET_STATUS:
168902ac6454SAndrew Thompson 			goto tr_handle_get_class_status;
169002ac6454SAndrew Thompson 
169102ac6454SAndrew Thompson 		default:
169202ac6454SAndrew Thompson 			goto tr_stalled;
169302ac6454SAndrew Thompson 		}
169402ac6454SAndrew Thompson 		break;
169502ac6454SAndrew Thompson 	default:
169602ac6454SAndrew Thompson 		goto tr_stalled;
169702ac6454SAndrew Thompson 	}
169802ac6454SAndrew Thompson 	goto tr_valid;
169902ac6454SAndrew Thompson 
170002ac6454SAndrew Thompson tr_handle_get_descriptor:
170102ac6454SAndrew Thompson 	switch (value >> 8) {
170202ac6454SAndrew Thompson 	case UDESC_DEVICE:
170302ac6454SAndrew Thompson 		if (value & 0xff) {
170402ac6454SAndrew Thompson 			goto tr_stalled;
170502ac6454SAndrew Thompson 		}
1706459d369eSAndrew Thompson 		len = sizeof(atmegadci_devd);
1707459d369eSAndrew Thompson 		ptr = (const void *)&atmegadci_devd;
170802ac6454SAndrew Thompson 		goto tr_valid;
170902ac6454SAndrew Thompson 	case UDESC_CONFIG:
171002ac6454SAndrew Thompson 		if (value & 0xff) {
171102ac6454SAndrew Thompson 			goto tr_stalled;
171202ac6454SAndrew Thompson 		}
1713459d369eSAndrew Thompson 		len = sizeof(atmegadci_confd);
1714459d369eSAndrew Thompson 		ptr = (const void *)&atmegadci_confd;
171502ac6454SAndrew Thompson 		goto tr_valid;
171602ac6454SAndrew Thompson 	case UDESC_STRING:
171702ac6454SAndrew Thompson 		switch (value & 0xff) {
171802ac6454SAndrew Thompson 		case 0:		/* Language table */
171923ab0871SHans Petter Selasky 			len = sizeof(usb_string_lang_en);
172023ab0871SHans Petter Selasky 			ptr = (const void *)&usb_string_lang_en;
172102ac6454SAndrew Thompson 			goto tr_valid;
172202ac6454SAndrew Thompson 
172302ac6454SAndrew Thompson 		case 1:		/* Vendor */
1724459d369eSAndrew Thompson 			len = sizeof(atmegadci_vendor);
1725459d369eSAndrew Thompson 			ptr = (const void *)&atmegadci_vendor;
172602ac6454SAndrew Thompson 			goto tr_valid;
172702ac6454SAndrew Thompson 
172802ac6454SAndrew Thompson 		case 2:		/* Product */
1729459d369eSAndrew Thompson 			len = sizeof(atmegadci_product);
1730459d369eSAndrew Thompson 			ptr = (const void *)&atmegadci_product;
173102ac6454SAndrew Thompson 			goto tr_valid;
173202ac6454SAndrew Thompson 		default:
173302ac6454SAndrew Thompson 			break;
173402ac6454SAndrew Thompson 		}
173502ac6454SAndrew Thompson 		break;
173602ac6454SAndrew Thompson 	default:
173702ac6454SAndrew Thompson 		goto tr_stalled;
173802ac6454SAndrew Thompson 	}
173902ac6454SAndrew Thompson 	goto tr_stalled;
174002ac6454SAndrew Thompson 
174102ac6454SAndrew Thompson tr_handle_get_config:
1742459d369eSAndrew Thompson 	len = 1;
174302ac6454SAndrew Thompson 	sc->sc_hub_temp.wValue[0] = sc->sc_conf;
174402ac6454SAndrew Thompson 	goto tr_valid;
174502ac6454SAndrew Thompson 
174602ac6454SAndrew Thompson tr_handle_get_status:
1747459d369eSAndrew Thompson 	len = 2;
174802ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
174902ac6454SAndrew Thompson 	goto tr_valid;
175002ac6454SAndrew Thompson 
175102ac6454SAndrew Thompson tr_handle_set_address:
175202ac6454SAndrew Thompson 	if (value & 0xFF00) {
175302ac6454SAndrew Thompson 		goto tr_stalled;
175402ac6454SAndrew Thompson 	}
175502ac6454SAndrew Thompson 	sc->sc_rt_addr = value;
175602ac6454SAndrew Thompson 	goto tr_valid;
175702ac6454SAndrew Thompson 
175802ac6454SAndrew Thompson tr_handle_set_config:
175902ac6454SAndrew Thompson 	if (value >= 2) {
176002ac6454SAndrew Thompson 		goto tr_stalled;
176102ac6454SAndrew Thompson 	}
176202ac6454SAndrew Thompson 	sc->sc_conf = value;
176302ac6454SAndrew Thompson 	goto tr_valid;
176402ac6454SAndrew Thompson 
176502ac6454SAndrew Thompson tr_handle_get_interface:
1766459d369eSAndrew Thompson 	len = 1;
176702ac6454SAndrew Thompson 	sc->sc_hub_temp.wValue[0] = 0;
176802ac6454SAndrew Thompson 	goto tr_valid;
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson tr_handle_get_tt_state:
177102ac6454SAndrew Thompson tr_handle_get_class_status:
177202ac6454SAndrew Thompson tr_handle_get_iface_status:
177302ac6454SAndrew Thompson tr_handle_get_ep_status:
1774459d369eSAndrew Thompson 	len = 2;
177502ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.wValue, 0);
177602ac6454SAndrew Thompson 	goto tr_valid;
177702ac6454SAndrew Thompson 
177802ac6454SAndrew Thompson tr_handle_set_halt:
177902ac6454SAndrew Thompson tr_handle_set_interface:
178002ac6454SAndrew Thompson tr_handle_set_wakeup:
178102ac6454SAndrew Thompson tr_handle_clear_wakeup:
178202ac6454SAndrew Thompson tr_handle_clear_halt:
178302ac6454SAndrew Thompson 	goto tr_valid;
178402ac6454SAndrew Thompson 
178502ac6454SAndrew Thompson tr_handle_clear_port_feature:
178602ac6454SAndrew Thompson 	if (index != 1) {
178702ac6454SAndrew Thompson 		goto tr_stalled;
178802ac6454SAndrew Thompson 	}
178902ac6454SAndrew Thompson 	DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
179002ac6454SAndrew Thompson 
179102ac6454SAndrew Thompson 	switch (value) {
179202ac6454SAndrew Thompson 	case UHF_PORT_SUSPEND:
179339307315SAndrew Thompson 		atmegadci_wakeup_peer(sc);
179402ac6454SAndrew Thompson 		break;
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 	case UHF_PORT_ENABLE:
179702ac6454SAndrew Thompson 		sc->sc_flags.port_enabled = 0;
179802ac6454SAndrew Thompson 		break;
179902ac6454SAndrew Thompson 
180002ac6454SAndrew Thompson 	case UHF_PORT_TEST:
180102ac6454SAndrew Thompson 	case UHF_PORT_INDICATOR:
180202ac6454SAndrew Thompson 	case UHF_C_PORT_ENABLE:
180302ac6454SAndrew Thompson 	case UHF_C_PORT_OVER_CURRENT:
180402ac6454SAndrew Thompson 	case UHF_C_PORT_RESET:
180502ac6454SAndrew Thompson 		/* nops */
180602ac6454SAndrew Thompson 		break;
180702ac6454SAndrew Thompson 	case UHF_PORT_POWER:
180802ac6454SAndrew Thompson 		sc->sc_flags.port_powered = 0;
180902ac6454SAndrew Thompson 		atmegadci_pull_down(sc);
181002ac6454SAndrew Thompson 		atmegadci_clocks_off(sc);
181102ac6454SAndrew Thompson 		break;
181202ac6454SAndrew Thompson 	case UHF_C_PORT_CONNECTION:
1813d59dbd0aSAndrew Thompson 		/* clear connect change flag */
181402ac6454SAndrew Thompson 		sc->sc_flags.change_connect = 0;
1815d59dbd0aSAndrew Thompson 
18167eaa41aeSAndrew Thompson 		if (!sc->sc_flags.status_bus_reset) {
18177eaa41aeSAndrew Thompson 			/* we are not connected */
18187eaa41aeSAndrew Thompson 			break;
18197eaa41aeSAndrew Thompson 		}
18207eaa41aeSAndrew Thompson 
1821d59dbd0aSAndrew Thompson 		/* configure the control endpoint */
1822d59dbd0aSAndrew Thompson 
1823d59dbd0aSAndrew Thompson 		/* select endpoint number */
1824d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, 0);
1825d59dbd0aSAndrew Thompson 
1826d59dbd0aSAndrew Thompson 		/* set endpoint reset */
1827d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(0));
1828d59dbd0aSAndrew Thompson 
1829d59dbd0aSAndrew Thompson 		/* clear endpoint reset */
1830d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1831d59dbd0aSAndrew Thompson 
1832d59dbd0aSAndrew Thompson 		/* enable and stall endpoint */
1833d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1834d59dbd0aSAndrew Thompson 		    ATMEGA_UECONX_EPEN |
1835d59dbd0aSAndrew Thompson 		    ATMEGA_UECONX_STALLRQ);
1836d59dbd0aSAndrew Thompson 
1837d59dbd0aSAndrew Thompson 		/* one bank, 64-bytes wMaxPacket */
1838d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X,
1839d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG0X_EPTYPE0);
1840d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
1841d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_ALLOC |
1842d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPBK0 |
1843d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPSIZE(3));
1844d59dbd0aSAndrew Thompson 
1845d59dbd0aSAndrew Thompson 		/* check valid config */
1846d59dbd0aSAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
1847d59dbd0aSAndrew Thompson 		if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
1848767cb2e2SAndrew Thompson 			device_printf(sc->sc_bus.bdev,
1849767cb2e2SAndrew Thompson 			    "Chip rejected EP0 configuration\n");
1850d59dbd0aSAndrew Thompson 		}
185102ac6454SAndrew Thompson 		break;
185202ac6454SAndrew Thompson 	case UHF_C_PORT_SUSPEND:
185302ac6454SAndrew Thompson 		sc->sc_flags.change_suspend = 0;
185402ac6454SAndrew Thompson 		break;
185502ac6454SAndrew Thompson 	default:
1856459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
185702ac6454SAndrew Thompson 		goto done;
185802ac6454SAndrew Thompson 	}
185902ac6454SAndrew Thompson 	goto tr_valid;
186002ac6454SAndrew Thompson 
186102ac6454SAndrew Thompson tr_handle_set_port_feature:
186202ac6454SAndrew Thompson 	if (index != 1) {
186302ac6454SAndrew Thompson 		goto tr_stalled;
186402ac6454SAndrew Thompson 	}
186502ac6454SAndrew Thompson 	DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
186602ac6454SAndrew Thompson 
186702ac6454SAndrew Thompson 	switch (value) {
186802ac6454SAndrew Thompson 	case UHF_PORT_ENABLE:
186902ac6454SAndrew Thompson 		sc->sc_flags.port_enabled = 1;
187002ac6454SAndrew Thompson 		break;
187102ac6454SAndrew Thompson 	case UHF_PORT_SUSPEND:
187202ac6454SAndrew Thompson 	case UHF_PORT_RESET:
187302ac6454SAndrew Thompson 	case UHF_PORT_TEST:
187402ac6454SAndrew Thompson 	case UHF_PORT_INDICATOR:
187502ac6454SAndrew Thompson 		/* nops */
187602ac6454SAndrew Thompson 		break;
187702ac6454SAndrew Thompson 	case UHF_PORT_POWER:
187802ac6454SAndrew Thompson 		sc->sc_flags.port_powered = 1;
187902ac6454SAndrew Thompson 		break;
188002ac6454SAndrew Thompson 	default:
1881459d369eSAndrew Thompson 		err = USB_ERR_IOERROR;
188202ac6454SAndrew Thompson 		goto done;
188302ac6454SAndrew Thompson 	}
188402ac6454SAndrew Thompson 	goto tr_valid;
188502ac6454SAndrew Thompson 
188602ac6454SAndrew Thompson tr_handle_get_port_status:
188702ac6454SAndrew Thompson 
188802ac6454SAndrew Thompson 	DPRINTFN(9, "UR_GET_PORT_STATUS\n");
188902ac6454SAndrew Thompson 
189002ac6454SAndrew Thompson 	if (index != 1) {
189102ac6454SAndrew Thompson 		goto tr_stalled;
189202ac6454SAndrew Thompson 	}
189302ac6454SAndrew Thompson 	if (sc->sc_flags.status_vbus) {
189402ac6454SAndrew Thompson 		atmegadci_clocks_on(sc);
189502ac6454SAndrew Thompson 		atmegadci_pull_up(sc);
189602ac6454SAndrew Thompson 	} else {
189702ac6454SAndrew Thompson 		atmegadci_pull_down(sc);
189802ac6454SAndrew Thompson 		atmegadci_clocks_off(sc);
189902ac6454SAndrew Thompson 	}
190002ac6454SAndrew Thompson 
190102ac6454SAndrew Thompson 	/* Select FULL-speed and Device Side Mode */
190202ac6454SAndrew Thompson 
190302ac6454SAndrew Thompson 	value = UPS_PORT_MODE_DEVICE;
190402ac6454SAndrew Thompson 
190502ac6454SAndrew Thompson 	if (sc->sc_flags.port_powered) {
190602ac6454SAndrew Thompson 		value |= UPS_PORT_POWER;
190702ac6454SAndrew Thompson 	}
190802ac6454SAndrew Thompson 	if (sc->sc_flags.port_enabled) {
190902ac6454SAndrew Thompson 		value |= UPS_PORT_ENABLED;
191002ac6454SAndrew Thompson 	}
191102ac6454SAndrew Thompson 	if (sc->sc_flags.status_vbus &&
191202ac6454SAndrew Thompson 	    sc->sc_flags.status_bus_reset) {
191302ac6454SAndrew Thompson 		value |= UPS_CURRENT_CONNECT_STATUS;
191402ac6454SAndrew Thompson 	}
191502ac6454SAndrew Thompson 	if (sc->sc_flags.status_suspend) {
191602ac6454SAndrew Thompson 		value |= UPS_SUSPEND;
191702ac6454SAndrew Thompson 	}
191802ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.ps.wPortStatus, value);
191902ac6454SAndrew Thompson 
192002ac6454SAndrew Thompson 	value = 0;
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson 	if (sc->sc_flags.change_connect) {
192302ac6454SAndrew Thompson 		value |= UPS_C_CONNECT_STATUS;
192402ac6454SAndrew Thompson 	}
192502ac6454SAndrew Thompson 	if (sc->sc_flags.change_suspend) {
192602ac6454SAndrew Thompson 		value |= UPS_C_SUSPEND;
192702ac6454SAndrew Thompson 	}
192802ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.ps.wPortChange, value);
1929459d369eSAndrew Thompson 	len = sizeof(sc->sc_hub_temp.ps);
193002ac6454SAndrew Thompson 	goto tr_valid;
193102ac6454SAndrew Thompson 
193202ac6454SAndrew Thompson tr_handle_get_class_descriptor:
193302ac6454SAndrew Thompson 	if (value & 0xFF) {
193402ac6454SAndrew Thompson 		goto tr_stalled;
193502ac6454SAndrew Thompson 	}
1936459d369eSAndrew Thompson 	ptr = (const void *)&atmegadci_hubd;
1937459d369eSAndrew Thompson 	len = sizeof(atmegadci_hubd);
193802ac6454SAndrew Thompson 	goto tr_valid;
193902ac6454SAndrew Thompson 
194002ac6454SAndrew Thompson tr_stalled:
1941459d369eSAndrew Thompson 	err = USB_ERR_STALLED;
194202ac6454SAndrew Thompson tr_valid:
194302ac6454SAndrew Thompson done:
1944459d369eSAndrew Thompson 	*plength = len;
1945459d369eSAndrew Thompson 	*pptr = ptr;
1946459d369eSAndrew Thompson 	return (err);
194702ac6454SAndrew Thompson }
194802ac6454SAndrew Thompson 
194902ac6454SAndrew Thompson static void
atmegadci_xfer_setup(struct usb_setup_params * parm)1950760bc48eSAndrew Thompson atmegadci_xfer_setup(struct usb_setup_params *parm)
195102ac6454SAndrew Thompson {
1952760bc48eSAndrew Thompson 	const struct usb_hw_ep_profile *pf;
1953760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
195402ac6454SAndrew Thompson 	void *last_obj;
195502ac6454SAndrew Thompson 	uint32_t ntd;
195602ac6454SAndrew Thompson 	uint32_t n;
195702ac6454SAndrew Thompson 	uint8_t ep_no;
195802ac6454SAndrew Thompson 
195902ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
196002ac6454SAndrew Thompson 
196102ac6454SAndrew Thompson 	/*
196202ac6454SAndrew Thompson 	 * NOTE: This driver does not use any of the parameters that
196302ac6454SAndrew Thompson 	 * are computed from the following values. Just set some
196402ac6454SAndrew Thompson 	 * reasonable dummies:
196502ac6454SAndrew Thompson 	 */
196602ac6454SAndrew Thompson 	parm->hc_max_packet_size = 0x500;
196702ac6454SAndrew Thompson 	parm->hc_max_packet_count = 1;
196802ac6454SAndrew Thompson 	parm->hc_max_frame_size = 0x500;
196902ac6454SAndrew Thompson 
1970a593f6b8SAndrew Thompson 	usbd_transfer_setup_sub(parm);
197102ac6454SAndrew Thompson 
197202ac6454SAndrew Thompson 	/*
197302ac6454SAndrew Thompson 	 * compute maximum number of TDs
197402ac6454SAndrew Thompson 	 */
1975ae60fdfbSAndrew Thompson 	if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
197602ac6454SAndrew Thompson 		ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
197702ac6454SAndrew Thompson 		    + 1 /* SYNC 2 */ ;
197802ac6454SAndrew Thompson 	} else {
19793b01d513SAndrew Thompson 		ntd = xfer->nframes + 1 /* SYNC */ ;
198002ac6454SAndrew Thompson 	}
198102ac6454SAndrew Thompson 
198202ac6454SAndrew Thompson 	/*
1983a593f6b8SAndrew Thompson 	 * check if "usbd_transfer_setup_sub" set an error
198402ac6454SAndrew Thompson 	 */
19853b01d513SAndrew Thompson 	if (parm->err)
198602ac6454SAndrew Thompson 		return;
19873b01d513SAndrew Thompson 
198802ac6454SAndrew Thompson 	/*
198902ac6454SAndrew Thompson 	 * allocate transfer descriptors
199002ac6454SAndrew Thompson 	 */
199102ac6454SAndrew Thompson 	last_obj = NULL;
199202ac6454SAndrew Thompson 
199302ac6454SAndrew Thompson 	/*
199402ac6454SAndrew Thompson 	 * get profile stuff
199502ac6454SAndrew Thompson 	 */
1996ae60fdfbSAndrew Thompson 	ep_no = xfer->endpointno & UE_ADDR;
199702ac6454SAndrew Thompson 	atmegadci_get_hw_ep_profile(parm->udev, &pf, ep_no);
199802ac6454SAndrew Thompson 
199902ac6454SAndrew Thompson 	if (pf == NULL) {
200002ac6454SAndrew Thompson 		/* should not happen */
200102ac6454SAndrew Thompson 		parm->err = USB_ERR_INVAL;
200202ac6454SAndrew Thompson 		return;
200302ac6454SAndrew Thompson 	}
200402ac6454SAndrew Thompson 
200502ac6454SAndrew Thompson 	/* align data */
200602ac6454SAndrew Thompson 	parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
200702ac6454SAndrew Thompson 
200802ac6454SAndrew Thompson 	for (n = 0; n != ntd; n++) {
200902ac6454SAndrew Thompson 		struct atmegadci_td *td;
201002ac6454SAndrew Thompson 
201102ac6454SAndrew Thompson 		if (parm->buf) {
201202ac6454SAndrew Thompson 			td = USB_ADD_BYTES(parm->buf, parm->size[0]);
201302ac6454SAndrew Thompson 
201402ac6454SAndrew Thompson 			/* init TD */
201502ac6454SAndrew Thompson 			td->max_packet_size = xfer->max_packet_size;
201602ac6454SAndrew Thompson 			td->ep_no = ep_no;
201702ac6454SAndrew Thompson 			if (pf->support_multi_buffer) {
201802ac6454SAndrew Thompson 				td->support_multi_buffer = 1;
201902ac6454SAndrew Thompson 			}
202002ac6454SAndrew Thompson 			td->obj_next = last_obj;
202102ac6454SAndrew Thompson 
202202ac6454SAndrew Thompson 			last_obj = td;
202302ac6454SAndrew Thompson 		}
202402ac6454SAndrew Thompson 		parm->size[0] += sizeof(*td);
202502ac6454SAndrew Thompson 	}
202602ac6454SAndrew Thompson 
202702ac6454SAndrew Thompson 	xfer->td_start[0] = last_obj;
202802ac6454SAndrew Thompson }
202902ac6454SAndrew Thompson 
203002ac6454SAndrew Thompson static void
atmegadci_xfer_unsetup(struct usb_xfer * xfer)2031760bc48eSAndrew Thompson atmegadci_xfer_unsetup(struct usb_xfer *xfer)
203202ac6454SAndrew Thompson {
203302ac6454SAndrew Thompson 	return;
203402ac6454SAndrew Thompson }
203502ac6454SAndrew Thompson 
203602ac6454SAndrew Thompson static void
atmegadci_ep_init(struct usb_device * udev,struct usb_endpoint_descriptor * edesc,struct usb_endpoint * ep)2037ae60fdfbSAndrew Thompson atmegadci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2038ae60fdfbSAndrew Thompson     struct usb_endpoint *ep)
203902ac6454SAndrew Thompson {
204002ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
204102ac6454SAndrew Thompson 
2042ae60fdfbSAndrew Thompson 	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2043ae60fdfbSAndrew Thompson 	    ep, udev->address,
2044f29a0724SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb_mode,
2045d59dbd0aSAndrew Thompson 	    sc->sc_rt_addr, udev->device_index);
204602ac6454SAndrew Thompson 
204739307315SAndrew Thompson 	if (udev->device_index != sc->sc_rt_addr) {
204802ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_FULL) {
204902ac6454SAndrew Thompson 			/* not supported */
205002ac6454SAndrew Thompson 			return;
205102ac6454SAndrew Thompson 		}
20523b01d513SAndrew Thompson 		if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2053ae60fdfbSAndrew Thompson 			ep->methods = &atmegadci_device_isoc_fs_methods;
20543b01d513SAndrew Thompson 		else
2055ae60fdfbSAndrew Thompson 			ep->methods = &atmegadci_device_non_isoc_methods;
205602ac6454SAndrew Thompson 	}
205702ac6454SAndrew Thompson }
205802ac6454SAndrew Thompson 
20592e141748SHans Petter Selasky static void
atmegadci_set_hw_power_sleep(struct usb_bus * bus,uint32_t state)20602e141748SHans Petter Selasky atmegadci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
20612e141748SHans Petter Selasky {
20622e141748SHans Petter Selasky 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
20632e141748SHans Petter Selasky 
20642e141748SHans Petter Selasky 	switch (state) {
20652e141748SHans Petter Selasky 	case USB_HW_POWER_SUSPEND:
20662e141748SHans Petter Selasky 		atmegadci_suspend(sc);
20672e141748SHans Petter Selasky 		break;
20682e141748SHans Petter Selasky 	case USB_HW_POWER_SHUTDOWN:
20692e141748SHans Petter Selasky 		atmegadci_uninit(sc);
20702e141748SHans Petter Selasky 		break;
20712e141748SHans Petter Selasky 	case USB_HW_POWER_RESUME:
20722e141748SHans Petter Selasky 		atmegadci_resume(sc);
20732e141748SHans Petter Selasky 		break;
20742e141748SHans Petter Selasky 	default:
20752e141748SHans Petter Selasky 		break;
20762e141748SHans Petter Selasky 	}
20772e141748SHans Petter Selasky }
20782e141748SHans Petter Selasky 
2079e892b3feSHans Petter Selasky static const struct usb_bus_methods atmegadci_bus_methods =
208002ac6454SAndrew Thompson {
2081ae60fdfbSAndrew Thompson 	.endpoint_init = &atmegadci_ep_init,
208202ac6454SAndrew Thompson 	.xfer_setup = &atmegadci_xfer_setup,
208302ac6454SAndrew Thompson 	.xfer_unsetup = &atmegadci_xfer_unsetup,
208402ac6454SAndrew Thompson 	.get_hw_ep_profile = &atmegadci_get_hw_ep_profile,
2085a5cf1aaaSHans Petter Selasky 	.xfer_stall = &atmegadci_xfer_stall,
208602ac6454SAndrew Thompson 	.set_stall = &atmegadci_set_stall,
208702ac6454SAndrew Thompson 	.clear_stall = &atmegadci_clear_stall,
208839307315SAndrew Thompson 	.roothub_exec = &atmegadci_roothub_exec,
2089dddb25f9SAlfred Perlstein 	.xfer_poll = &atmegadci_do_poll,
20902e141748SHans Petter Selasky 	.set_hw_power_sleep = &atmegadci_set_hw_power_sleep,
209102ac6454SAndrew Thompson };
2092