xref: /freebsd/sys/dev/usb/controller/atmegadci.c (revision bdc081c60ff09707a85971b94c9177d16cc72033)
102ac6454SAndrew Thompson #include <sys/cdefs.h>
202ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
302ac6454SAndrew Thompson 
402ac6454SAndrew Thompson /*-
502ac6454SAndrew Thompson  * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson /*
30d59dbd0aSAndrew Thompson  * This file contains the driver for the ATMEGA series USB OTG
31d59dbd0aSAndrew Thompson  * Controller. This driver currently only supports the DCI mode of the
32d59dbd0aSAndrew Thompson  * USB hardware.
3302ac6454SAndrew Thompson  */
3402ac6454SAndrew Thompson 
3502ac6454SAndrew Thompson /*
3602ac6454SAndrew Thompson  * NOTE: When the chip detects BUS-reset it will also reset the
3702ac6454SAndrew Thompson  * endpoints, Function-address and more.
3802ac6454SAndrew Thompson  */
3902ac6454SAndrew Thompson 
4002ac6454SAndrew Thompson #include <dev/usb/usb.h>
4102ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
4302ac6454SAndrew Thompson 
4402ac6454SAndrew Thompson #define	USB_DEBUG_VAR atmegadci_debug
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
4702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
4802ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
4902ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_sw_transfer.h>
5102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
5202ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5302ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
5802ac6454SAndrew Thompson #include <dev/usb/controller/atmegadci.h>
5902ac6454SAndrew Thompson 
6002ac6454SAndrew Thompson #define	ATMEGA_BUS2SC(bus) \
6102ac6454SAndrew Thompson    ((struct atmegadci_softc *)(((uint8_t *)(bus)) - \
6202ac6454SAndrew Thompson    USB_P2U(&(((struct atmegadci_softc *)0)->sc_bus))))
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #define	ATMEGA_PC2SC(pc) \
65bdc081c6SAndrew Thompson    ATMEGA_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson #if USB_DEBUG
6802ac6454SAndrew Thompson static int atmegadci_debug = 0;
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, atmegadci, CTLFLAG_RW, 0, "USB ATMEGA DCI");
7102ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_atmegadci, OID_AUTO, debug, CTLFLAG_RW,
7202ac6454SAndrew Thompson     &atmegadci_debug, 0, "ATMEGA DCI debug level");
7302ac6454SAndrew Thompson #endif
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson #define	ATMEGA_INTR_ENDPT 1
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson /* prototypes */
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson struct usb2_bus_methods atmegadci_bus_methods;
8002ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_bulk_methods;
8102ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_ctrl_methods;
8202ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_intr_methods;
8302ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_isoc_fs_methods;
8402ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_root_ctrl_methods;
8502ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_root_intr_methods;
8602ac6454SAndrew Thompson 
8702ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_setup_rx;
8802ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_rx;
8902ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_tx;
9002ac6454SAndrew Thompson static atmegadci_cmd_t atmegadci_data_tx_sync;
9102ac6454SAndrew Thompson static void atmegadci_device_done(struct usb2_xfer *, usb2_error_t);
9202ac6454SAndrew Thompson static void atmegadci_do_poll(struct usb2_bus *);
9302ac6454SAndrew Thompson static void atmegadci_root_ctrl_poll(struct atmegadci_softc *);
9402ac6454SAndrew Thompson static void atmegadci_standard_done(struct usb2_xfer *);
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson static usb2_sw_transfer_func_t atmegadci_root_intr_done;
9702ac6454SAndrew Thompson static usb2_sw_transfer_func_t atmegadci_root_ctrl_done;
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson /*
10002ac6454SAndrew Thompson  * Here is a list of what the chip supports:
10102ac6454SAndrew Thompson  */
10202ac6454SAndrew Thompson static const struct usb2_hw_ep_profile
10302ac6454SAndrew Thompson 	atmegadci_ep_profile[2] = {
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson 	[0] = {
10602ac6454SAndrew Thompson 		.max_in_frame_size = 64,
10702ac6454SAndrew Thompson 		.max_out_frame_size = 64,
10802ac6454SAndrew Thompson 		.is_simplex = 1,
10902ac6454SAndrew Thompson 		.support_control = 1,
11002ac6454SAndrew Thompson 	},
11102ac6454SAndrew Thompson 	[1] = {
11202ac6454SAndrew Thompson 		.max_in_frame_size = 64,
11302ac6454SAndrew Thompson 		.max_out_frame_size = 64,
11402ac6454SAndrew Thompson 		.is_simplex = 1,
11502ac6454SAndrew Thompson 		.support_multi_buffer = 1,
11602ac6454SAndrew Thompson 		.support_bulk = 1,
11702ac6454SAndrew Thompson 		.support_interrupt = 1,
11802ac6454SAndrew Thompson 		.support_isochronous = 1,
11902ac6454SAndrew Thompson 		.support_in = 1,
12002ac6454SAndrew Thompson 		.support_out = 1,
12102ac6454SAndrew Thompson 	},
12202ac6454SAndrew Thompson };
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson static void
12502ac6454SAndrew Thompson atmegadci_get_hw_ep_profile(struct usb2_device *udev,
12602ac6454SAndrew Thompson     const struct usb2_hw_ep_profile **ppf, uint8_t ep_addr)
12702ac6454SAndrew Thompson {
12802ac6454SAndrew Thompson 	if (ep_addr == 0)
12902ac6454SAndrew Thompson 		*ppf = atmegadci_ep_profile;
13002ac6454SAndrew Thompson 	else if (ep_addr < ATMEGA_EP_MAX)
13102ac6454SAndrew Thompson 		*ppf = atmegadci_ep_profile + 1;
13202ac6454SAndrew Thompson 	else
13302ac6454SAndrew Thompson 		*ppf = NULL;
13402ac6454SAndrew Thompson }
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson static void
13702ac6454SAndrew Thompson atmegadci_clocks_on(struct atmegadci_softc *sc)
13802ac6454SAndrew Thompson {
13902ac6454SAndrew Thompson 	if (sc->sc_flags.clocks_off &&
14002ac6454SAndrew Thompson 	    sc->sc_flags.port_powered) {
14102ac6454SAndrew Thompson 
14202ac6454SAndrew Thompson 		DPRINTFN(5, "\n");
14302ac6454SAndrew Thompson 
14402ac6454SAndrew Thompson 		/* turn on clocks */
14502ac6454SAndrew Thompson 		(sc->sc_clocks_on) (&sc->sc_bus);
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
14802ac6454SAndrew Thompson 		    ATMEGA_USBCON_USBE |
14902ac6454SAndrew Thompson 		    ATMEGA_USBCON_OTGPADE |
15002ac6454SAndrew Thompson 		    ATMEGA_USBCON_VBUSTE);
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 		sc->sc_flags.clocks_off = 0;
15302ac6454SAndrew Thompson 
15402ac6454SAndrew Thompson 		/* enable transceiver ? */
15502ac6454SAndrew Thompson 	}
15602ac6454SAndrew Thompson }
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson static void
15902ac6454SAndrew Thompson atmegadci_clocks_off(struct atmegadci_softc *sc)
16002ac6454SAndrew Thompson {
16102ac6454SAndrew Thompson 	if (!sc->sc_flags.clocks_off) {
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson 		DPRINTFN(5, "\n");
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson 		/* disable Transceiver ? */
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
16802ac6454SAndrew Thompson 		    ATMEGA_USBCON_USBE |
16902ac6454SAndrew Thompson 		    ATMEGA_USBCON_OTGPADE |
17002ac6454SAndrew Thompson 		    ATMEGA_USBCON_FRZCLK |
17102ac6454SAndrew Thompson 		    ATMEGA_USBCON_VBUSTE);
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 		/* turn clocks off */
17402ac6454SAndrew Thompson 		(sc->sc_clocks_off) (&sc->sc_bus);
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson 		sc->sc_flags.clocks_off = 1;
17702ac6454SAndrew Thompson 	}
17802ac6454SAndrew Thompson }
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson static void
18102ac6454SAndrew Thompson atmegadci_pull_up(struct atmegadci_softc *sc)
18202ac6454SAndrew Thompson {
18302ac6454SAndrew Thompson 	/* pullup D+, if possible */
18402ac6454SAndrew Thompson 
18502ac6454SAndrew Thompson 	if (!sc->sc_flags.d_pulled_up &&
18602ac6454SAndrew Thompson 	    sc->sc_flags.port_powered) {
18702ac6454SAndrew Thompson 		sc->sc_flags.d_pulled_up = 1;
18802ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDCON, 0);
18902ac6454SAndrew Thompson 	}
19002ac6454SAndrew Thompson }
19102ac6454SAndrew Thompson 
19202ac6454SAndrew Thompson static void
19302ac6454SAndrew Thompson atmegadci_pull_down(struct atmegadci_softc *sc)
19402ac6454SAndrew Thompson {
19502ac6454SAndrew Thompson 	/* pulldown D+, if possible */
19602ac6454SAndrew Thompson 
19702ac6454SAndrew Thompson 	if (sc->sc_flags.d_pulled_up) {
19802ac6454SAndrew Thompson 		sc->sc_flags.d_pulled_up = 0;
19902ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
20002ac6454SAndrew Thompson 	}
20102ac6454SAndrew Thompson }
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson static void
20402ac6454SAndrew Thompson atmegadci_wakeup_peer(struct usb2_xfer *xfer)
20502ac6454SAndrew Thompson {
20602ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
20702ac6454SAndrew Thompson 	uint8_t temp;
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 	if (!sc->sc_flags.status_suspend) {
21002ac6454SAndrew Thompson 		return;
21102ac6454SAndrew Thompson 	}
21202ac6454SAndrew Thompson 
21302ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UDCON);
21402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDCON, temp | ATMEGA_UDCON_RMWKUP);
21502ac6454SAndrew Thompson 
21602ac6454SAndrew Thompson 	/* wait 8 milliseconds */
21702ac6454SAndrew Thompson 	/* Wait for reset to complete. */
21802ac6454SAndrew Thompson 	usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson 	/* hardware should have cleared RMWKUP bit */
22102ac6454SAndrew Thompson }
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson static void
22402ac6454SAndrew Thompson atmegadci_set_address(struct atmegadci_softc *sc, uint8_t addr)
22502ac6454SAndrew Thompson {
22602ac6454SAndrew Thompson 	DPRINTFN(5, "addr=%d\n", addr);
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 	addr |= ATMEGA_UDADDR_ADDEN;
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, addr);
23102ac6454SAndrew Thompson }
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson static uint8_t
23402ac6454SAndrew Thompson atmegadci_setup_rx(struct atmegadci_td *td)
23502ac6454SAndrew Thompson {
23602ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
23702ac6454SAndrew Thompson 	struct usb2_device_request req;
23802ac6454SAndrew Thompson 	uint16_t count;
23902ac6454SAndrew Thompson 	uint8_t temp;
24002ac6454SAndrew Thompson 
24102ac6454SAndrew Thompson 	/* get pointer to softc */
24202ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
24302ac6454SAndrew Thompson 
24402ac6454SAndrew Thompson 	/* select endpoint number */
24502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
24602ac6454SAndrew Thompson 
24702ac6454SAndrew Thompson 	/* check endpoint status */
24802ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 	DPRINTFN(5, "UEINTX=0x%02x\n", temp);
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 	if (!(temp & ATMEGA_UEINTX_RXSTPI)) {
25302ac6454SAndrew Thompson 		/* abort any ongoing transfer */
25402ac6454SAndrew Thompson 		if (!td->did_stall) {
25502ac6454SAndrew Thompson 			DPRINTFN(5, "stalling\n");
25602ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
25702ac6454SAndrew Thompson 			    ATMEGA_UECONX_EPEN |
25802ac6454SAndrew Thompson 			    ATMEGA_UECONX_STALLRQ);
25902ac6454SAndrew Thompson 			td->did_stall = 1;
26002ac6454SAndrew Thompson 		}
26102ac6454SAndrew Thompson 		goto not_complete;
26202ac6454SAndrew Thompson 	}
26302ac6454SAndrew Thompson 	/* get the packet byte count */
26402ac6454SAndrew Thompson 	count =
26502ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
26602ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 	/* mask away undefined bits */
26902ac6454SAndrew Thompson 	count &= 0x7FF;
27002ac6454SAndrew Thompson 
27102ac6454SAndrew Thompson 	/* verify data length */
27202ac6454SAndrew Thompson 	if (count != td->remainder) {
27302ac6454SAndrew Thompson 		DPRINTFN(0, "Invalid SETUP packet "
27402ac6454SAndrew Thompson 		    "length, %d bytes\n", count);
27502ac6454SAndrew Thompson 		goto not_complete;
27602ac6454SAndrew Thompson 	}
27702ac6454SAndrew Thompson 	if (count != sizeof(req)) {
27802ac6454SAndrew Thompson 		DPRINTFN(0, "Unsupported SETUP packet "
27902ac6454SAndrew Thompson 		    "length, %d bytes\n", count);
28002ac6454SAndrew Thompson 		goto not_complete;
28102ac6454SAndrew Thompson 	}
28202ac6454SAndrew Thompson 	/* receive data */
28302ac6454SAndrew Thompson 	ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
28402ac6454SAndrew Thompson 	    (void *)&req, sizeof(req));
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson 	/* copy data into real buffer */
28702ac6454SAndrew Thompson 	usb2_copy_in(td->pc, 0, &req, sizeof(req));
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson 	td->offset = sizeof(req);
29002ac6454SAndrew Thompson 	td->remainder = 0;
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson 	/* sneak peek the set address */
29302ac6454SAndrew Thompson 	if ((req.bmRequestType == UT_WRITE_DEVICE) &&
29402ac6454SAndrew Thompson 	    (req.bRequest == UR_SET_ADDRESS)) {
29502ac6454SAndrew Thompson 		sc->sc_dv_addr = req.wValue[0] & 0x7F;
296d59dbd0aSAndrew Thompson 		/* must write address before ZLP */
297d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, sc->sc_dv_addr);
29802ac6454SAndrew Thompson 	} else {
29902ac6454SAndrew Thompson 		sc->sc_dv_addr = 0xFF;
30002ac6454SAndrew Thompson 	}
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	/* clear SETUP packet interrupt */
30302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ~ATMEGA_UEINTX_RXSTPI);
30402ac6454SAndrew Thompson 	return (0);			/* complete */
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson not_complete:
30702ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet */
30802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, ATMEGA_UEIENX_RXSTPE);
30902ac6454SAndrew Thompson 	return (1);			/* not complete */
31002ac6454SAndrew Thompson }
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson static uint8_t
31302ac6454SAndrew Thompson atmegadci_data_rx(struct atmegadci_td *td)
31402ac6454SAndrew Thompson {
31502ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
31602ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
31702ac6454SAndrew Thompson 	uint16_t count;
31802ac6454SAndrew Thompson 	uint8_t temp;
31902ac6454SAndrew Thompson 	uint8_t to;
32002ac6454SAndrew Thompson 	uint8_t got_short;
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 	to = 3;				/* don't loop forever! */
32302ac6454SAndrew Thompson 	got_short = 0;
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	/* get pointer to softc */
32602ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson 	/* select endpoint number */
32902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson repeat:
33202ac6454SAndrew Thompson 	/* check if any of the FIFO banks have data */
33302ac6454SAndrew Thompson 	/* check endpoint status */
33402ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
33502ac6454SAndrew Thompson 
33602ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
33702ac6454SAndrew Thompson 
33802ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
33902ac6454SAndrew Thompson 		if (td->remainder == 0) {
34002ac6454SAndrew Thompson 			/*
34102ac6454SAndrew Thompson 			 * We are actually complete and have
34202ac6454SAndrew Thompson 			 * received the next SETUP
34302ac6454SAndrew Thompson 			 */
34402ac6454SAndrew Thompson 			DPRINTFN(5, "faking complete\n");
34502ac6454SAndrew Thompson 			return (0);	/* complete */
34602ac6454SAndrew Thompson 		}
34702ac6454SAndrew Thompson 		/*
34802ac6454SAndrew Thompson 	         * USB Host Aborted the transfer.
34902ac6454SAndrew Thompson 	         */
35002ac6454SAndrew Thompson 		td->error = 1;
35102ac6454SAndrew Thompson 		return (0);		/* complete */
35202ac6454SAndrew Thompson 	}
35302ac6454SAndrew Thompson 	/* check status */
35402ac6454SAndrew Thompson 	if (!(temp & (ATMEGA_UEINTX_FIFOCON |
35502ac6454SAndrew Thompson 	    ATMEGA_UEINTX_RXOUTI))) {
35602ac6454SAndrew Thompson 		/* no data */
35702ac6454SAndrew Thompson 		goto not_complete;
35802ac6454SAndrew Thompson 	}
35902ac6454SAndrew Thompson 	/* get the packet byte count */
36002ac6454SAndrew Thompson 	count =
36102ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
36202ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 	/* mask away undefined bits */
36502ac6454SAndrew Thompson 	count &= 0x7FF;
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 	/* verify the packet byte count */
36802ac6454SAndrew Thompson 	if (count != td->max_packet_size) {
36902ac6454SAndrew Thompson 		if (count < td->max_packet_size) {
37002ac6454SAndrew Thompson 			/* we have a short packet */
37102ac6454SAndrew Thompson 			td->short_pkt = 1;
37202ac6454SAndrew Thompson 			got_short = 1;
37302ac6454SAndrew Thompson 		} else {
37402ac6454SAndrew Thompson 			/* invalid USB packet */
37502ac6454SAndrew Thompson 			td->error = 1;
37602ac6454SAndrew Thompson 			return (0);	/* we are complete */
37702ac6454SAndrew Thompson 		}
37802ac6454SAndrew Thompson 	}
37902ac6454SAndrew Thompson 	/* verify the packet byte count */
38002ac6454SAndrew Thompson 	if (count > td->remainder) {
38102ac6454SAndrew Thompson 		/* invalid USB packet */
38202ac6454SAndrew Thompson 		td->error = 1;
38302ac6454SAndrew Thompson 		return (0);		/* we are complete */
38402ac6454SAndrew Thompson 	}
38502ac6454SAndrew Thompson 	while (count > 0) {
38602ac6454SAndrew Thompson 		usb2_get_page(td->pc, td->offset, &buf_res);
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 		/* get correct length */
38902ac6454SAndrew Thompson 		if (buf_res.length > count) {
39002ac6454SAndrew Thompson 			buf_res.length = count;
39102ac6454SAndrew Thompson 		}
39202ac6454SAndrew Thompson 		/* receive data */
39302ac6454SAndrew Thompson 		ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
39402ac6454SAndrew Thompson 		    buf_res.buffer, buf_res.length);
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 		/* update counters */
39702ac6454SAndrew Thompson 		count -= buf_res.length;
39802ac6454SAndrew Thompson 		td->offset += buf_res.length;
39902ac6454SAndrew Thompson 		td->remainder -= buf_res.length;
40002ac6454SAndrew Thompson 	}
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson 	/* clear OUT packet interrupt */
40302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_RXOUTI ^ 0xFF);
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 	/* release FIFO bank */
40602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_FIFOCON ^ 0xFF);
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson 	/* check if we are complete */
40902ac6454SAndrew Thompson 	if ((td->remainder == 0) || got_short) {
41002ac6454SAndrew Thompson 		if (td->short_pkt) {
41102ac6454SAndrew Thompson 			/* we are complete */
41202ac6454SAndrew Thompson 			return (0);
41302ac6454SAndrew Thompson 		}
41402ac6454SAndrew Thompson 		/* else need to receive a zero length packet */
41502ac6454SAndrew Thompson 	}
41602ac6454SAndrew Thompson 	if (--to) {
41702ac6454SAndrew Thompson 		goto repeat;
41802ac6454SAndrew Thompson 	}
41902ac6454SAndrew Thompson not_complete:
42002ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or OUT packet */
42102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
42202ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_RXOUTE);
42302ac6454SAndrew Thompson 	return (1);			/* not complete */
42402ac6454SAndrew Thompson }
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson static uint8_t
42702ac6454SAndrew Thompson atmegadci_data_tx(struct atmegadci_td *td)
42802ac6454SAndrew Thompson {
42902ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
43002ac6454SAndrew Thompson 	struct usb2_page_search buf_res;
43102ac6454SAndrew Thompson 	uint16_t count;
43202ac6454SAndrew Thompson 	uint8_t to;
43302ac6454SAndrew Thompson 	uint8_t temp;
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	to = 3;				/* don't loop forever! */
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	/* get pointer to softc */
43802ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	/* select endpoint number */
44102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson repeat:
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson 	/* check endpoint status */
44602ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
45102ac6454SAndrew Thompson 		/*
45202ac6454SAndrew Thompson 	         * The current transfer was aborted
45302ac6454SAndrew Thompson 	         * by the USB Host
45402ac6454SAndrew Thompson 	         */
45502ac6454SAndrew Thompson 		td->error = 1;
45602ac6454SAndrew Thompson 		return (0);		/* complete */
45702ac6454SAndrew Thompson 	}
45802ac6454SAndrew Thompson 	if (!(temp & (ATMEGA_UEINTX_FIFOCON |
45902ac6454SAndrew Thompson 	    ATMEGA_UEINTX_TXINI))) {
46002ac6454SAndrew Thompson 		/* cannot write any data */
46102ac6454SAndrew Thompson 		goto not_complete;
46202ac6454SAndrew Thompson 	}
46302ac6454SAndrew Thompson 	count = td->max_packet_size;
46402ac6454SAndrew Thompson 	if (td->remainder < count) {
46502ac6454SAndrew Thompson 		/* we have a short packet */
46602ac6454SAndrew Thompson 		td->short_pkt = 1;
46702ac6454SAndrew Thompson 		count = td->remainder;
46802ac6454SAndrew Thompson 	}
46902ac6454SAndrew Thompson 	while (count > 0) {
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 		usb2_get_page(td->pc, td->offset, &buf_res);
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 		/* get correct length */
47402ac6454SAndrew Thompson 		if (buf_res.length > count) {
47502ac6454SAndrew Thompson 			buf_res.length = count;
47602ac6454SAndrew Thompson 		}
47702ac6454SAndrew Thompson 		/* transmit data */
47802ac6454SAndrew Thompson 		ATMEGA_WRITE_MULTI_1(sc, ATMEGA_UEDATX,
47902ac6454SAndrew Thompson 		    buf_res.buffer, buf_res.length);
48002ac6454SAndrew Thompson 
48102ac6454SAndrew Thompson 		/* update counters */
48202ac6454SAndrew Thompson 		count -= buf_res.length;
48302ac6454SAndrew Thompson 		td->offset += buf_res.length;
48402ac6454SAndrew Thompson 		td->remainder -= buf_res.length;
48502ac6454SAndrew Thompson 	}
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	/* clear IN packet interrupt */
48802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_TXINI);
48902ac6454SAndrew Thompson 
49002ac6454SAndrew Thompson 	/* allocate FIFO bank */
49102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_FIFOCON);
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	/* check remainder */
49402ac6454SAndrew Thompson 	if (td->remainder == 0) {
49502ac6454SAndrew Thompson 		if (td->short_pkt) {
49602ac6454SAndrew Thompson 			return (0);	/* complete */
49702ac6454SAndrew Thompson 		}
49802ac6454SAndrew Thompson 		/* else we need to transmit a short packet */
49902ac6454SAndrew Thompson 	}
50002ac6454SAndrew Thompson 	if (--to) {
50102ac6454SAndrew Thompson 		goto repeat;
50202ac6454SAndrew Thompson 	}
50302ac6454SAndrew Thompson not_complete:
50402ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or free IN packet */
50502ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
50602ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
50702ac6454SAndrew Thompson 	return (1);			/* not complete */
50802ac6454SAndrew Thompson }
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson static uint8_t
51102ac6454SAndrew Thompson atmegadci_data_tx_sync(struct atmegadci_td *td)
51202ac6454SAndrew Thompson {
51302ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
51402ac6454SAndrew Thompson 	uint8_t temp;
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson 	/* get pointer to softc */
51702ac6454SAndrew Thompson 	sc = ATMEGA_PC2SC(td->pc);
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 	/* select endpoint number */
52002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson 	/* check endpoint status */
52302ac6454SAndrew Thompson 	temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson 	DPRINTFN(5, "temp=0x%02x\n", temp);
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 	if (temp & ATMEGA_UEINTX_RXSTPI) {
52802ac6454SAndrew Thompson 		DPRINTFN(5, "faking complete\n");
52902ac6454SAndrew Thompson 		/* Race condition */
53002ac6454SAndrew Thompson 		return (0);		/* complete */
53102ac6454SAndrew Thompson 	}
53202ac6454SAndrew Thompson 	/*
53302ac6454SAndrew Thompson 	 * The control endpoint has only got one bank, so if that bank
53402ac6454SAndrew Thompson 	 * is free the packet has been transferred!
53502ac6454SAndrew Thompson 	 */
53602ac6454SAndrew Thompson 	if (!(temp & (ATMEGA_UEINTX_FIFOCON |
53702ac6454SAndrew Thompson 	    ATMEGA_UEINTX_TXINI))) {
53802ac6454SAndrew Thompson 		/* cannot write any data */
53902ac6454SAndrew Thompson 		goto not_complete;
54002ac6454SAndrew Thompson 	}
54102ac6454SAndrew Thompson 	if (sc->sc_dv_addr != 0xFF) {
54202ac6454SAndrew Thompson 		/* set new address */
54302ac6454SAndrew Thompson 		atmegadci_set_address(sc, sc->sc_dv_addr);
54402ac6454SAndrew Thompson 	}
54502ac6454SAndrew Thompson 	return (0);			/* complete */
54602ac6454SAndrew Thompson 
54702ac6454SAndrew Thompson not_complete:
54802ac6454SAndrew Thompson 	/* we only want to know if there is a SETUP packet or free IN packet */
54902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
55002ac6454SAndrew Thompson 	    ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
55102ac6454SAndrew Thompson 	return (1);			/* not complete */
55202ac6454SAndrew Thompson }
55302ac6454SAndrew Thompson 
55402ac6454SAndrew Thompson static uint8_t
55502ac6454SAndrew Thompson atmegadci_xfer_do_fifo(struct usb2_xfer *xfer)
55602ac6454SAndrew Thompson {
55702ac6454SAndrew Thompson 	struct atmegadci_td *td;
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
56202ac6454SAndrew Thompson 	while (1) {
56302ac6454SAndrew Thompson 		if ((td->func) (td)) {
56402ac6454SAndrew Thompson 			/* operation in progress */
56502ac6454SAndrew Thompson 			break;
56602ac6454SAndrew Thompson 		}
56702ac6454SAndrew Thompson 		if (((void *)td) == xfer->td_transfer_last) {
56802ac6454SAndrew Thompson 			goto done;
56902ac6454SAndrew Thompson 		}
57002ac6454SAndrew Thompson 		if (td->error) {
57102ac6454SAndrew Thompson 			goto done;
57202ac6454SAndrew Thompson 		} else if (td->remainder > 0) {
57302ac6454SAndrew Thompson 			/*
57402ac6454SAndrew Thompson 			 * We had a short transfer. If there is no alternate
57502ac6454SAndrew Thompson 			 * next, stop processing !
57602ac6454SAndrew Thompson 			 */
57702ac6454SAndrew Thompson 			if (!td->alt_next) {
57802ac6454SAndrew Thompson 				goto done;
57902ac6454SAndrew Thompson 			}
58002ac6454SAndrew Thompson 		}
58102ac6454SAndrew Thompson 		/*
58202ac6454SAndrew Thompson 		 * Fetch the next transfer descriptor and transfer
58302ac6454SAndrew Thompson 		 * some flags to the next transfer descriptor
58402ac6454SAndrew Thompson 		 */
58502ac6454SAndrew Thompson 		td = td->obj_next;
58602ac6454SAndrew Thompson 		xfer->td_transfer_cache = td;
58702ac6454SAndrew Thompson 	}
58802ac6454SAndrew Thompson 	return (1);			/* not complete */
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson done:
59102ac6454SAndrew Thompson 	/* compute all actual lengths */
59202ac6454SAndrew Thompson 
59302ac6454SAndrew Thompson 	atmegadci_standard_done(xfer);
59402ac6454SAndrew Thompson 	return (0);			/* complete */
59502ac6454SAndrew Thompson }
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson static void
59802ac6454SAndrew Thompson atmegadci_interrupt_poll(struct atmegadci_softc *sc)
59902ac6454SAndrew Thompson {
60002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson repeat:
60302ac6454SAndrew Thompson 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
60402ac6454SAndrew Thompson 		if (!atmegadci_xfer_do_fifo(xfer)) {
60502ac6454SAndrew Thompson 			/* queue has been modified */
60602ac6454SAndrew Thompson 			goto repeat;
60702ac6454SAndrew Thompson 		}
60802ac6454SAndrew Thompson 	}
60902ac6454SAndrew Thompson }
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson static void
61202ac6454SAndrew Thompson atmegadci_vbus_interrupt(struct atmegadci_softc *sc, uint8_t is_on)
61302ac6454SAndrew Thompson {
61402ac6454SAndrew Thompson 	DPRINTFN(5, "vbus = %u\n", is_on);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	if (is_on) {
61702ac6454SAndrew Thompson 		if (!sc->sc_flags.status_vbus) {
61802ac6454SAndrew Thompson 			sc->sc_flags.status_vbus = 1;
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 			usb2_sw_transfer(&sc->sc_root_intr,
62302ac6454SAndrew Thompson 			    &atmegadci_root_intr_done);
62402ac6454SAndrew Thompson 		}
62502ac6454SAndrew Thompson 	} else {
62602ac6454SAndrew Thompson 		if (sc->sc_flags.status_vbus) {
62702ac6454SAndrew Thompson 			sc->sc_flags.status_vbus = 0;
62802ac6454SAndrew Thompson 			sc->sc_flags.status_bus_reset = 0;
62902ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 0;
63002ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 0;
63102ac6454SAndrew Thompson 			sc->sc_flags.change_connect = 1;
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 			usb2_sw_transfer(&sc->sc_root_intr,
63602ac6454SAndrew Thompson 			    &atmegadci_root_intr_done);
63702ac6454SAndrew Thompson 		}
63802ac6454SAndrew Thompson 	}
63902ac6454SAndrew Thompson }
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson void
64202ac6454SAndrew Thompson atmegadci_interrupt(struct atmegadci_softc *sc)
64302ac6454SAndrew Thompson {
64402ac6454SAndrew Thompson 	uint8_t status;
64502ac6454SAndrew Thompson 
64602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	/* read interrupt status */
64902ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_UDINT);
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	/* clear all set interrupts */
65202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDINT, ~status);
65302ac6454SAndrew Thompson 
654d59dbd0aSAndrew Thompson 	DPRINTFN(14, "UDINT=0x%02x\n", status);
655d59dbd0aSAndrew Thompson 
65602ac6454SAndrew Thompson 	/* check for any bus state change interrupts */
65702ac6454SAndrew Thompson 	if (status & ATMEGA_UDINT_EORSTI) {
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson 		DPRINTFN(5, "end of reset\n");
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 		/* set correct state */
66202ac6454SAndrew Thompson 		sc->sc_flags.status_bus_reset = 1;
66302ac6454SAndrew Thompson 		sc->sc_flags.status_suspend = 0;
66402ac6454SAndrew Thompson 		sc->sc_flags.change_suspend = 0;
66502ac6454SAndrew Thompson 		sc->sc_flags.change_connect = 1;
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 		/* disable resume interrupt */
66802ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
66902ac6454SAndrew Thompson 		    ATMEGA_UDINT_SUSPE |
67002ac6454SAndrew Thompson 		    ATMEGA_UDINT_EORSTE);
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 		/* complete root HUB interrupt endpoint */
67302ac6454SAndrew Thompson 		usb2_sw_transfer(&sc->sc_root_intr,
67402ac6454SAndrew Thompson 		    &atmegadci_root_intr_done);
67502ac6454SAndrew Thompson 	}
67602ac6454SAndrew Thompson 	/*
67702ac6454SAndrew Thompson 	 * If resume and suspend is set at the same time we interpret
67802ac6454SAndrew Thompson 	 * that like RESUME. Resume is set when there is at least 3
67902ac6454SAndrew Thompson 	 * milliseconds of inactivity on the USB BUS.
68002ac6454SAndrew Thompson 	 */
68102ac6454SAndrew Thompson 	if (status & ATMEGA_UDINT_EORSMI) {
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson 		DPRINTFN(5, "resume interrupt\n");
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson 		if (sc->sc_flags.status_suspend) {
68602ac6454SAndrew Thompson 			/* update status bits */
68702ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 0;
68802ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 1;
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson 			/* disable resume interrupt */
69102ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
69202ac6454SAndrew Thompson 			    ATMEGA_UDINT_SUSPE |
69302ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSTE);
69402ac6454SAndrew Thompson 
69502ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
69602ac6454SAndrew Thompson 			usb2_sw_transfer(&sc->sc_root_intr,
69702ac6454SAndrew Thompson 			    &atmegadci_root_intr_done);
69802ac6454SAndrew Thompson 		}
69902ac6454SAndrew Thompson 	} else if (status & ATMEGA_UDINT_SUSPI) {
70002ac6454SAndrew Thompson 
70102ac6454SAndrew Thompson 		DPRINTFN(5, "suspend interrupt\n");
70202ac6454SAndrew Thompson 
70302ac6454SAndrew Thompson 		if (!sc->sc_flags.status_suspend) {
70402ac6454SAndrew Thompson 			/* update status bits */
70502ac6454SAndrew Thompson 			sc->sc_flags.status_suspend = 1;
70602ac6454SAndrew Thompson 			sc->sc_flags.change_suspend = 1;
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 			/* disable suspend interrupt */
70902ac6454SAndrew Thompson 			ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
71002ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSMI |
71102ac6454SAndrew Thompson 			    ATMEGA_UDINT_EORSTE);
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 			/* complete root HUB interrupt endpoint */
71402ac6454SAndrew Thompson 			usb2_sw_transfer(&sc->sc_root_intr,
71502ac6454SAndrew Thompson 			    &atmegadci_root_intr_done);
71602ac6454SAndrew Thompson 		}
71702ac6454SAndrew Thompson 	}
71802ac6454SAndrew Thompson 	/* check VBUS */
71902ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_USBINT);
72002ac6454SAndrew Thompson 
72102ac6454SAndrew Thompson 	/* clear all set interrupts */
72202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_USBINT, ~status);
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson 	if (status & ATMEGA_USBINT_VBUSTI) {
72502ac6454SAndrew Thompson 		uint8_t temp;
72602ac6454SAndrew Thompson 
727d59dbd0aSAndrew Thompson 		DPRINTFN(5, "USBINT=0x%02x\n", status);
728d59dbd0aSAndrew Thompson 
72902ac6454SAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
73002ac6454SAndrew Thompson 		atmegadci_vbus_interrupt(sc, temp & ATMEGA_USBSTA_VBUS);
73102ac6454SAndrew Thompson 	}
73202ac6454SAndrew Thompson 	/* check for any endpoint interrupts */
73302ac6454SAndrew Thompson 	status = ATMEGA_READ_1(sc, ATMEGA_UEINT);
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	/* clear all set interrupts */
73602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UEINT, ~status);
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson 	if (status) {
73902ac6454SAndrew Thompson 
740d59dbd0aSAndrew Thompson 		DPRINTFN(5, "real endpoint interrupt UEINT=0x%02x\n", status);
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson 		atmegadci_interrupt_poll(sc);
74302ac6454SAndrew Thompson 	}
74402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
74502ac6454SAndrew Thompson }
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson static void
74802ac6454SAndrew Thompson atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp *temp)
74902ac6454SAndrew Thompson {
75002ac6454SAndrew Thompson 	struct atmegadci_td *td;
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson 	/* get current Transfer Descriptor */
75302ac6454SAndrew Thompson 	td = temp->td_next;
75402ac6454SAndrew Thompson 	temp->td = td;
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson 	/* prepare for next TD */
75702ac6454SAndrew Thompson 	temp->td_next = td->obj_next;
75802ac6454SAndrew Thompson 
75902ac6454SAndrew Thompson 	/* fill out the Transfer Descriptor */
76002ac6454SAndrew Thompson 	td->func = temp->func;
76102ac6454SAndrew Thompson 	td->pc = temp->pc;
76202ac6454SAndrew Thompson 	td->offset = temp->offset;
76302ac6454SAndrew Thompson 	td->remainder = temp->len;
76402ac6454SAndrew Thompson 	td->error = 0;
76502ac6454SAndrew Thompson 	td->did_stall = 0;
76602ac6454SAndrew Thompson 	td->short_pkt = temp->short_pkt;
76702ac6454SAndrew Thompson 	td->alt_next = temp->setup_alt_next;
76802ac6454SAndrew Thompson }
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson static void
77102ac6454SAndrew Thompson atmegadci_setup_standard_chain(struct usb2_xfer *xfer)
77202ac6454SAndrew Thompson {
77302ac6454SAndrew Thompson 	struct atmegadci_std_temp temp;
77402ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
77502ac6454SAndrew Thompson 	struct atmegadci_td *td;
77602ac6454SAndrew Thompson 	uint32_t x;
77702ac6454SAndrew Thompson 	uint8_t ep_no;
77802ac6454SAndrew Thompson 	uint8_t need_sync;
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
78102ac6454SAndrew Thompson 	    xfer->address, UE_GET_ADDR(xfer->endpoint),
78202ac6454SAndrew Thompson 	    xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	temp.max_frame_size = xfer->max_frame_size;
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson 	td = xfer->td_start[0];
78702ac6454SAndrew Thompson 	xfer->td_transfer_first = td;
78802ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	/* setup temp */
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 	temp.td = NULL;
79302ac6454SAndrew Thompson 	temp.td_next = xfer->td_start[0];
79402ac6454SAndrew Thompson 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
79502ac6454SAndrew Thompson 	temp.offset = 0;
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(xfer->xroot->bus);
79802ac6454SAndrew Thompson 	ep_no = (xfer->endpoint & UE_ADDR);
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 	/* check if we should prepend a setup message */
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
80302ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
80402ac6454SAndrew Thompson 
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;
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
81102ac6454SAndrew Thompson 		}
81202ac6454SAndrew Thompson 		x = 1;
81302ac6454SAndrew Thompson 	} else {
81402ac6454SAndrew Thompson 		x = 0;
81502ac6454SAndrew Thompson 	}
81602ac6454SAndrew Thompson 
81702ac6454SAndrew Thompson 	if (x != xfer->nframes) {
81802ac6454SAndrew Thompson 		if (xfer->endpoint & UE_DIR_IN) {
81902ac6454SAndrew Thompson 			temp.func = &atmegadci_data_tx;
82002ac6454SAndrew Thompson 			need_sync = 1;
82102ac6454SAndrew Thompson 		} else {
82202ac6454SAndrew Thompson 			temp.func = &atmegadci_data_rx;
82302ac6454SAndrew Thompson 			need_sync = 0;
82402ac6454SAndrew Thompson 		}
82502ac6454SAndrew Thompson 
82602ac6454SAndrew Thompson 		/* setup "pc" pointer */
82702ac6454SAndrew Thompson 		temp.pc = xfer->frbuffers + x;
82802ac6454SAndrew Thompson 	} else {
82902ac6454SAndrew Thompson 		need_sync = 0;
83002ac6454SAndrew Thompson 	}
83102ac6454SAndrew Thompson 	while (x != xfer->nframes) {
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 		/* DATA0 / DATA1 message */
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 		temp.len = xfer->frlengths[x];
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 		x++;
83802ac6454SAndrew Thompson 
83902ac6454SAndrew Thompson 		if (x == xfer->nframes) {
84002ac6454SAndrew Thompson 			temp.setup_alt_next = 0;
84102ac6454SAndrew Thompson 		}
84202ac6454SAndrew Thompson 		if (temp.len == 0) {
84302ac6454SAndrew Thompson 
84402ac6454SAndrew Thompson 			/* make sure that we send an USB packet */
84502ac6454SAndrew Thompson 
84602ac6454SAndrew Thompson 			temp.short_pkt = 0;
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 		} else {
84902ac6454SAndrew Thompson 
85002ac6454SAndrew Thompson 			/* regular data transfer */
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson 			temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
85302ac6454SAndrew Thompson 		}
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson 		atmegadci_setup_standard_chain_sub(&temp);
85602ac6454SAndrew Thompson 
85702ac6454SAndrew Thompson 		if (xfer->flags_int.isochronous_xfr) {
85802ac6454SAndrew Thompson 			temp.offset += temp.len;
85902ac6454SAndrew Thompson 		} else {
86002ac6454SAndrew Thompson 			/* get next Page Cache pointer */
86102ac6454SAndrew Thompson 			temp.pc = xfer->frbuffers + x;
86202ac6454SAndrew Thompson 		}
86302ac6454SAndrew Thompson 	}
86402ac6454SAndrew Thompson 
86502ac6454SAndrew Thompson 	/* always setup a valid "pc" pointer for status and sync */
86602ac6454SAndrew Thompson 	temp.pc = xfer->frbuffers + 0;
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson 	/* check if we need to sync */
86902ac6454SAndrew Thompson 	if (need_sync && xfer->flags_int.control_xfr) {
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 		/* we need a SYNC point after TX */
87202ac6454SAndrew Thompson 		temp.func = &atmegadci_data_tx_sync;
87302ac6454SAndrew Thompson 		temp.len = 0;
87402ac6454SAndrew Thompson 		temp.short_pkt = 0;
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 		atmegadci_setup_standard_chain_sub(&temp);
87702ac6454SAndrew Thompson 	}
87802ac6454SAndrew Thompson 	/* check if we should append a status stage */
87902ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
88002ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson 		/*
88302ac6454SAndrew Thompson 		 * Send a DATA1 message and invert the current
88402ac6454SAndrew Thompson 		 * endpoint direction.
88502ac6454SAndrew Thompson 		 */
88602ac6454SAndrew Thompson 		if (xfer->endpoint & UE_DIR_IN) {
88702ac6454SAndrew Thompson 			temp.func = &atmegadci_data_rx;
88802ac6454SAndrew Thompson 			need_sync = 0;
88902ac6454SAndrew Thompson 		} else {
89002ac6454SAndrew Thompson 			temp.func = &atmegadci_data_tx;
89102ac6454SAndrew Thompson 			need_sync = 1;
89202ac6454SAndrew Thompson 		}
89302ac6454SAndrew Thompson 		temp.len = 0;
89402ac6454SAndrew Thompson 		temp.short_pkt = 0;
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 		atmegadci_setup_standard_chain_sub(&temp);
89702ac6454SAndrew Thompson 		if (need_sync) {
89802ac6454SAndrew Thompson 			/* we need a SYNC point after TX */
89902ac6454SAndrew Thompson 			temp.func = &atmegadci_data_tx_sync;
90002ac6454SAndrew Thompson 			temp.len = 0;
90102ac6454SAndrew Thompson 			temp.short_pkt = 0;
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 			atmegadci_setup_standard_chain_sub(&temp);
90402ac6454SAndrew Thompson 		}
90502ac6454SAndrew Thompson 	}
90602ac6454SAndrew Thompson 	/* must have at least one frame! */
90702ac6454SAndrew Thompson 	td = temp.td;
90802ac6454SAndrew Thompson 	xfer->td_transfer_last = td;
90902ac6454SAndrew Thompson }
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson static void
91202ac6454SAndrew Thompson atmegadci_timeout(void *arg)
91302ac6454SAndrew Thompson {
91402ac6454SAndrew Thompson 	struct usb2_xfer *xfer = arg;
91502ac6454SAndrew Thompson 
91602ac6454SAndrew Thompson 	DPRINTF("xfer=%p\n", xfer);
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
91902ac6454SAndrew Thompson 
92002ac6454SAndrew Thompson 	/* transfer is transferred */
92102ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_TIMEOUT);
92202ac6454SAndrew Thompson }
92302ac6454SAndrew Thompson 
92402ac6454SAndrew Thompson static void
92502ac6454SAndrew Thompson atmegadci_start_standard_chain(struct usb2_xfer *xfer)
92602ac6454SAndrew Thompson {
92702ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson 	/* poll one time - will turn on interrupts */
93002ac6454SAndrew Thompson 	if (atmegadci_xfer_do_fifo(xfer)) {
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 		/* put transfer on interrupt queue */
93302ac6454SAndrew Thompson 		usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson 		/* start timeout, if any */
93602ac6454SAndrew Thompson 		if (xfer->timeout != 0) {
93702ac6454SAndrew Thompson 			usb2_transfer_timeout_ms(xfer,
93802ac6454SAndrew Thompson 			    &atmegadci_timeout, xfer->timeout);
93902ac6454SAndrew Thompson 		}
94002ac6454SAndrew Thompson 	}
94102ac6454SAndrew Thompson }
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson static void
94402ac6454SAndrew Thompson atmegadci_root_intr_done(struct usb2_xfer *xfer,
94502ac6454SAndrew Thompson     struct usb2_sw_transfer *std)
94602ac6454SAndrew Thompson {
94702ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
95202ac6454SAndrew Thompson 
95302ac6454SAndrew Thompson 	if (std->state != USB_SW_TR_PRE_DATA) {
95402ac6454SAndrew Thompson 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
95502ac6454SAndrew Thompson 			/* transfer transferred */
95602ac6454SAndrew Thompson 			atmegadci_device_done(xfer, std->err);
95702ac6454SAndrew Thompson 		}
95802ac6454SAndrew Thompson 		goto done;
95902ac6454SAndrew Thompson 	}
96002ac6454SAndrew Thompson 	/* setup buffer */
96102ac6454SAndrew Thompson 	std->ptr = sc->sc_hub_idata;
96202ac6454SAndrew Thompson 	std->len = sizeof(sc->sc_hub_idata);
96302ac6454SAndrew Thompson 
96402ac6454SAndrew Thompson 	/* set port bit */
96502ac6454SAndrew Thompson 	sc->sc_hub_idata[0] = 0x02;	/* we only have one port */
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson done:
96802ac6454SAndrew Thompson 	return;
96902ac6454SAndrew Thompson }
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson static usb2_error_t
97202ac6454SAndrew Thompson atmegadci_standard_done_sub(struct usb2_xfer *xfer)
97302ac6454SAndrew Thompson {
97402ac6454SAndrew Thompson 	struct atmegadci_td *td;
97502ac6454SAndrew Thompson 	uint32_t len;
97602ac6454SAndrew Thompson 	uint8_t error;
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 	DPRINTFN(9, "\n");
97902ac6454SAndrew Thompson 
98002ac6454SAndrew Thompson 	td = xfer->td_transfer_cache;
98102ac6454SAndrew Thompson 
98202ac6454SAndrew Thompson 	do {
98302ac6454SAndrew Thompson 		len = td->remainder;
98402ac6454SAndrew Thompson 
98502ac6454SAndrew Thompson 		if (xfer->aframes != xfer->nframes) {
98602ac6454SAndrew Thompson 			/*
98702ac6454SAndrew Thompson 		         * Verify the length and subtract
98802ac6454SAndrew Thompson 		         * the remainder from "frlengths[]":
98902ac6454SAndrew Thompson 		         */
99002ac6454SAndrew Thompson 			if (len > xfer->frlengths[xfer->aframes]) {
99102ac6454SAndrew Thompson 				td->error = 1;
99202ac6454SAndrew Thompson 			} else {
99302ac6454SAndrew Thompson 				xfer->frlengths[xfer->aframes] -= len;
99402ac6454SAndrew Thompson 			}
99502ac6454SAndrew Thompson 		}
99602ac6454SAndrew Thompson 		/* Check for transfer error */
99702ac6454SAndrew Thompson 		if (td->error) {
99802ac6454SAndrew Thompson 			/* the transfer is finished */
99902ac6454SAndrew Thompson 			error = 1;
100002ac6454SAndrew Thompson 			td = NULL;
100102ac6454SAndrew Thompson 			break;
100202ac6454SAndrew Thompson 		}
100302ac6454SAndrew Thompson 		/* Check for short transfer */
100402ac6454SAndrew Thompson 		if (len > 0) {
100502ac6454SAndrew Thompson 			if (xfer->flags_int.short_frames_ok) {
100602ac6454SAndrew Thompson 				/* follow alt next */
100702ac6454SAndrew Thompson 				if (td->alt_next) {
100802ac6454SAndrew Thompson 					td = td->obj_next;
100902ac6454SAndrew Thompson 				} else {
101002ac6454SAndrew Thompson 					td = NULL;
101102ac6454SAndrew Thompson 				}
101202ac6454SAndrew Thompson 			} else {
101302ac6454SAndrew Thompson 				/* the transfer is finished */
101402ac6454SAndrew Thompson 				td = NULL;
101502ac6454SAndrew Thompson 			}
101602ac6454SAndrew Thompson 			error = 0;
101702ac6454SAndrew Thompson 			break;
101802ac6454SAndrew Thompson 		}
101902ac6454SAndrew Thompson 		td = td->obj_next;
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 		/* this USB frame is complete */
102202ac6454SAndrew Thompson 		error = 0;
102302ac6454SAndrew Thompson 		break;
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 	} while (0);
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	/* update transfer cache */
102802ac6454SAndrew Thompson 
102902ac6454SAndrew Thompson 	xfer->td_transfer_cache = td;
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson 	return (error ?
103202ac6454SAndrew Thompson 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
103302ac6454SAndrew Thompson }
103402ac6454SAndrew Thompson 
103502ac6454SAndrew Thompson static void
103602ac6454SAndrew Thompson atmegadci_standard_done(struct usb2_xfer *xfer)
103702ac6454SAndrew Thompson {
103802ac6454SAndrew Thompson 	usb2_error_t err = 0;
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
104102ac6454SAndrew Thompson 	    xfer, xfer->pipe);
104202ac6454SAndrew Thompson 
104302ac6454SAndrew Thompson 	/* reset scanner */
104402ac6454SAndrew Thompson 
104502ac6454SAndrew Thompson 	xfer->td_transfer_cache = xfer->td_transfer_first;
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
104802ac6454SAndrew Thompson 
104902ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
105002ac6454SAndrew Thompson 
105102ac6454SAndrew Thompson 			err = atmegadci_standard_done_sub(xfer);
105202ac6454SAndrew Thompson 		}
105302ac6454SAndrew Thompson 		xfer->aframes = 1;
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
105602ac6454SAndrew Thompson 			goto done;
105702ac6454SAndrew Thompson 		}
105802ac6454SAndrew Thompson 	}
105902ac6454SAndrew Thompson 	while (xfer->aframes != xfer->nframes) {
106002ac6454SAndrew Thompson 
106102ac6454SAndrew Thompson 		err = atmegadci_standard_done_sub(xfer);
106202ac6454SAndrew Thompson 		xfer->aframes++;
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson 		if (xfer->td_transfer_cache == NULL) {
106502ac6454SAndrew Thompson 			goto done;
106602ac6454SAndrew Thompson 		}
106702ac6454SAndrew Thompson 	}
106802ac6454SAndrew Thompson 
106902ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr &&
107002ac6454SAndrew Thompson 	    !xfer->flags_int.control_act) {
107102ac6454SAndrew Thompson 
107202ac6454SAndrew Thompson 		err = atmegadci_standard_done_sub(xfer);
107302ac6454SAndrew Thompson 	}
107402ac6454SAndrew Thompson done:
107502ac6454SAndrew Thompson 	atmegadci_device_done(xfer, err);
107602ac6454SAndrew Thompson }
107702ac6454SAndrew Thompson 
107802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
107902ac6454SAndrew Thompson  *	atmegadci_device_done
108002ac6454SAndrew Thompson  *
108102ac6454SAndrew Thompson  * NOTE: this function can be called more than one time on the
108202ac6454SAndrew Thompson  * same USB transfer!
108302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
108402ac6454SAndrew Thompson static void
108502ac6454SAndrew Thompson atmegadci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
108602ac6454SAndrew Thompson {
108702ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
108802ac6454SAndrew Thompson 	uint8_t ep_no;
108902ac6454SAndrew Thompson 
109002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
109102ac6454SAndrew Thompson 
1092d59dbd0aSAndrew Thompson 	DPRINTFN(9, "xfer=%p, pipe=%p, error=%d\n",
109302ac6454SAndrew Thompson 	    xfer, xfer->pipe, error);
109402ac6454SAndrew Thompson 
109502ac6454SAndrew Thompson 	if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
109602ac6454SAndrew Thompson 		ep_no = (xfer->endpoint & UE_ADDR);
109702ac6454SAndrew Thompson 
109802ac6454SAndrew Thompson 		/* select endpoint number */
109902ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
110002ac6454SAndrew Thompson 
110102ac6454SAndrew Thompson 		/* disable endpoint interrupt */
110202ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson 		DPRINTFN(15, "disabled interrupts!\n");
110502ac6454SAndrew Thompson 	}
110602ac6454SAndrew Thompson 	/* dequeue transfer and start next transfer */
110702ac6454SAndrew Thompson 	usb2_transfer_done(xfer, error);
110802ac6454SAndrew Thompson }
110902ac6454SAndrew Thompson 
111002ac6454SAndrew Thompson static void
111102ac6454SAndrew Thompson atmegadci_set_stall(struct usb2_device *udev, struct usb2_xfer *xfer,
111202ac6454SAndrew Thompson     struct usb2_pipe *pipe)
111302ac6454SAndrew Thompson {
111402ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
111502ac6454SAndrew Thompson 	uint8_t ep_no;
111602ac6454SAndrew Thompson 
111702ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
111802ac6454SAndrew Thompson 
111902ac6454SAndrew Thompson 	DPRINTFN(5, "pipe=%p\n", pipe);
112002ac6454SAndrew Thompson 
112102ac6454SAndrew Thompson 	if (xfer) {
112202ac6454SAndrew Thompson 		/* cancel any ongoing transfers */
112302ac6454SAndrew Thompson 		atmegadci_device_done(xfer, USB_ERR_STALLED);
112402ac6454SAndrew Thompson 	}
112502ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(udev->bus);
112602ac6454SAndrew Thompson 	/* get endpoint number */
112702ac6454SAndrew Thompson 	ep_no = (pipe->edesc->bEndpointAddress & UE_ADDR);
112802ac6454SAndrew Thompson 	/* select endpoint number */
112902ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
113002ac6454SAndrew Thompson 	/* set stall */
113102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
113202ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
113302ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQ);
113402ac6454SAndrew Thompson }
113502ac6454SAndrew Thompson 
113602ac6454SAndrew Thompson static void
113702ac6454SAndrew Thompson atmegadci_clear_stall_sub(struct atmegadci_softc *sc, uint8_t ep_no,
113802ac6454SAndrew Thompson     uint8_t ep_type, uint8_t ep_dir)
113902ac6454SAndrew Thompson {
114002ac6454SAndrew Thompson 	uint8_t temp;
114102ac6454SAndrew Thompson 
114202ac6454SAndrew Thompson 	if (ep_type == UE_CONTROL) {
114302ac6454SAndrew Thompson 		/* clearing stall is not needed */
114402ac6454SAndrew Thompson 		return;
114502ac6454SAndrew Thompson 	}
114602ac6454SAndrew Thompson 	/* select endpoint number */
114702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
114802ac6454SAndrew Thompson 
114902ac6454SAndrew Thompson 	/* set endpoint reset */
115002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(ep_no));
115102ac6454SAndrew Thompson 
115202ac6454SAndrew Thompson 	/* clear endpoint reset */
115302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
115402ac6454SAndrew Thompson 
115502ac6454SAndrew Thompson 	/* set stall */
115602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
115702ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
115802ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQ);
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson 	/* reset data toggle */
116102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
116202ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
116302ac6454SAndrew Thompson 	    ATMEGA_UECONX_RSTDT);
116402ac6454SAndrew Thompson 
116502ac6454SAndrew Thompson 	/* clear stall */
116602ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
116702ac6454SAndrew Thompson 	    ATMEGA_UECONX_EPEN |
116802ac6454SAndrew Thompson 	    ATMEGA_UECONX_STALLRQC);
116902ac6454SAndrew Thompson 
1170d59dbd0aSAndrew Thompson 	do {
117102ac6454SAndrew Thompson 		temp = 0;
117202ac6454SAndrew Thompson 		if (ep_type == UE_BULK) {
117302ac6454SAndrew Thompson 			temp |= ATMEGA_UECFG0X_EPTYPE2;
117402ac6454SAndrew Thompson 		} else if (ep_type == UE_INTERRUPT) {
117502ac6454SAndrew Thompson 			temp |= ATMEGA_UECFG0X_EPTYPE3;
117602ac6454SAndrew Thompson 		} else {
117702ac6454SAndrew Thompson 			temp |= ATMEGA_UECFG0X_EPTYPE1;
117802ac6454SAndrew Thompson 		}
117902ac6454SAndrew Thompson 		if (ep_dir & UE_DIR_IN) {
118002ac6454SAndrew Thompson 			temp |= ATMEGA_UECFG0X_EPDIR;
118102ac6454SAndrew Thompson 		}
118202ac6454SAndrew Thompson 		/* two banks, 64-bytes wMaxPacket */
118302ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, temp);
118402ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
118502ac6454SAndrew Thompson 		    ATMEGA_UECFG1X_ALLOC |
118602ac6454SAndrew Thompson 		    ATMEGA_UECFG1X_EPBK1 |
1187d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPSIZE(3));
118802ac6454SAndrew Thompson 
118902ac6454SAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
119002ac6454SAndrew Thompson 		if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
119102ac6454SAndrew Thompson 			DPRINTFN(0, "Chip rejected configuration\n");
119202ac6454SAndrew Thompson 		}
1193d59dbd0aSAndrew Thompson 	} while (0);
119402ac6454SAndrew Thompson }
119502ac6454SAndrew Thompson 
119602ac6454SAndrew Thompson static void
119702ac6454SAndrew Thompson atmegadci_clear_stall(struct usb2_device *udev, struct usb2_pipe *pipe)
119802ac6454SAndrew Thompson {
119902ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
120002ac6454SAndrew Thompson 	struct usb2_endpoint_descriptor *ed;
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 	DPRINTFN(5, "pipe=%p\n", pipe);
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
120502ac6454SAndrew Thompson 
120602ac6454SAndrew Thompson 	/* check mode */
120702ac6454SAndrew Thompson 	if (udev->flags.usb2_mode != USB_MODE_DEVICE) {
120802ac6454SAndrew Thompson 		/* not supported */
120902ac6454SAndrew Thompson 		return;
121002ac6454SAndrew Thompson 	}
121102ac6454SAndrew Thompson 	/* get softc */
121202ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(udev->bus);
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson 	/* get endpoint descriptor */
121502ac6454SAndrew Thompson 	ed = pipe->edesc;
121602ac6454SAndrew Thompson 
121702ac6454SAndrew Thompson 	/* reset endpoint */
121802ac6454SAndrew Thompson 	atmegadci_clear_stall_sub(sc,
121902ac6454SAndrew Thompson 	    (ed->bEndpointAddress & UE_ADDR),
122002ac6454SAndrew Thompson 	    (ed->bmAttributes & UE_XFERTYPE),
122102ac6454SAndrew Thompson 	    (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
122202ac6454SAndrew Thompson }
122302ac6454SAndrew Thompson 
122402ac6454SAndrew Thompson usb2_error_t
122502ac6454SAndrew Thompson atmegadci_init(struct atmegadci_softc *sc)
122602ac6454SAndrew Thompson {
122702ac6454SAndrew Thompson 	uint8_t n;
122802ac6454SAndrew Thompson 
122902ac6454SAndrew Thompson 	DPRINTF("start\n");
123002ac6454SAndrew Thompson 
123102ac6454SAndrew Thompson 	/* set up the bus structure */
123202ac6454SAndrew Thompson 	sc->sc_bus.usbrev = USB_REV_1_1;
123302ac6454SAndrew Thompson 	sc->sc_bus.methods = &atmegadci_bus_methods;
123402ac6454SAndrew Thompson 
123502ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
1236d59dbd0aSAndrew Thompson #if 0
1237d59dbd0aSAndrew Thompson 	/* XXX TODO - currently done by boot strap */
123802ac6454SAndrew Thompson 
123902ac6454SAndrew Thompson 	/* enable USB PAD regulator */
124002ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UHWCON,
1241d59dbd0aSAndrew Thompson 	    ATMEGA_UHWCON_UVREGE | ATMEGA_UHWCON_UIMOD);
1242d59dbd0aSAndrew Thompson #endif
124302ac6454SAndrew Thompson 	/* turn on clocks */
124402ac6454SAndrew Thompson 	(sc->sc_clocks_on) (&sc->sc_bus);
124502ac6454SAndrew Thompson 
1246d59dbd0aSAndrew Thompson 	/* make sure device is re-enumerated */
1247d59dbd0aSAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
1248d59dbd0aSAndrew Thompson 
124902ac6454SAndrew Thompson 	/* wait a little for things to stabilise */
1250d59dbd0aSAndrew Thompson 	usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
125102ac6454SAndrew Thompson 
125202ac6454SAndrew Thompson 	/* enable interrupts */
125302ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
125402ac6454SAndrew Thompson 	    ATMEGA_UDINT_SUSPE |
125502ac6454SAndrew Thompson 	    ATMEGA_UDINT_EORSTE);
125602ac6454SAndrew Thompson 
125702ac6454SAndrew Thompson 	/* reset all endpoints */
125802ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST,
125902ac6454SAndrew Thompson 	    (1 << ATMEGA_EP_MAX) - 1);
126002ac6454SAndrew Thompson 
126102ac6454SAndrew Thompson 	/* disable reset */
126202ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
126302ac6454SAndrew Thompson 
126402ac6454SAndrew Thompson 	/* disable all endpoints */
1265d59dbd0aSAndrew Thompson 	for (n = 0; n != ATMEGA_EP_MAX; n++) {
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 		/* select endpoint */
126802ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, n);
126902ac6454SAndrew Thompson 
127002ac6454SAndrew Thompson 		/* disable endpoint interrupt */
127102ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
127202ac6454SAndrew Thompson 
127302ac6454SAndrew Thompson 		/* disable endpoint */
127402ac6454SAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 0);
127502ac6454SAndrew Thompson 	}
127602ac6454SAndrew Thompson 
127702ac6454SAndrew Thompson 	/* turn off clocks */
127802ac6454SAndrew Thompson 
127902ac6454SAndrew Thompson 	atmegadci_clocks_off(sc);
128002ac6454SAndrew Thompson 
1281d59dbd0aSAndrew Thompson 	/* read initial VBUS state */
1282d59dbd0aSAndrew Thompson 
1283d59dbd0aSAndrew Thompson 	n = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
1284d59dbd0aSAndrew Thompson 	atmegadci_vbus_interrupt(sc, n & ATMEGA_USBSTA_VBUS);
1285d59dbd0aSAndrew Thompson 
128602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
128702ac6454SAndrew Thompson 
128802ac6454SAndrew Thompson 	/* catch any lost interrupts */
128902ac6454SAndrew Thompson 
129002ac6454SAndrew Thompson 	atmegadci_do_poll(&sc->sc_bus);
129102ac6454SAndrew Thompson 
129202ac6454SAndrew Thompson 	return (0);			/* success */
129302ac6454SAndrew Thompson }
129402ac6454SAndrew Thompson 
129502ac6454SAndrew Thompson void
129602ac6454SAndrew Thompson atmegadci_uninit(struct atmegadci_softc *sc)
129702ac6454SAndrew Thompson {
129802ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
129902ac6454SAndrew Thompson 
130002ac6454SAndrew Thompson 	/* turn on clocks */
130102ac6454SAndrew Thompson 	(sc->sc_clocks_on) (&sc->sc_bus);
130202ac6454SAndrew Thompson 
130302ac6454SAndrew Thompson 	/* disable interrupts */
130402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 0);
130502ac6454SAndrew Thompson 
130602ac6454SAndrew Thompson 	/* reset all endpoints */
130702ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST,
130802ac6454SAndrew Thompson 	    (1 << ATMEGA_EP_MAX) - 1);
130902ac6454SAndrew Thompson 
131002ac6454SAndrew Thompson 	/* disable reset */
131102ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
131202ac6454SAndrew Thompson 
131302ac6454SAndrew Thompson 	sc->sc_flags.port_powered = 0;
131402ac6454SAndrew Thompson 	sc->sc_flags.status_vbus = 0;
131502ac6454SAndrew Thompson 	sc->sc_flags.status_bus_reset = 0;
131602ac6454SAndrew Thompson 	sc->sc_flags.status_suspend = 0;
131702ac6454SAndrew Thompson 	sc->sc_flags.change_suspend = 0;
131802ac6454SAndrew Thompson 	sc->sc_flags.change_connect = 1;
131902ac6454SAndrew Thompson 
132002ac6454SAndrew Thompson 	atmegadci_pull_down(sc);
132102ac6454SAndrew Thompson 	atmegadci_clocks_off(sc);
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 	/* disable USB PAD regulator */
132402ac6454SAndrew Thompson 	ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 0);
132502ac6454SAndrew Thompson 
132602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
132702ac6454SAndrew Thompson }
132802ac6454SAndrew Thompson 
132902ac6454SAndrew Thompson void
133002ac6454SAndrew Thompson atmegadci_suspend(struct atmegadci_softc *sc)
133102ac6454SAndrew Thompson {
133202ac6454SAndrew Thompson 	return;
133302ac6454SAndrew Thompson }
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson void
133602ac6454SAndrew Thompson atmegadci_resume(struct atmegadci_softc *sc)
133702ac6454SAndrew Thompson {
133802ac6454SAndrew Thompson 	return;
133902ac6454SAndrew Thompson }
134002ac6454SAndrew Thompson 
134102ac6454SAndrew Thompson static void
134202ac6454SAndrew Thompson atmegadci_do_poll(struct usb2_bus *bus)
134302ac6454SAndrew Thompson {
134402ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
134502ac6454SAndrew Thompson 
134602ac6454SAndrew Thompson 	USB_BUS_LOCK(&sc->sc_bus);
134702ac6454SAndrew Thompson 	atmegadci_interrupt_poll(sc);
134802ac6454SAndrew Thompson 	atmegadci_root_ctrl_poll(sc);
134902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(&sc->sc_bus);
135002ac6454SAndrew Thompson }
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
135302ac6454SAndrew Thompson  * at91dci bulk support
135402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
135502ac6454SAndrew Thompson static void
135602ac6454SAndrew Thompson atmegadci_device_bulk_open(struct usb2_xfer *xfer)
135702ac6454SAndrew Thompson {
135802ac6454SAndrew Thompson 	return;
135902ac6454SAndrew Thompson }
136002ac6454SAndrew Thompson 
136102ac6454SAndrew Thompson static void
136202ac6454SAndrew Thompson atmegadci_device_bulk_close(struct usb2_xfer *xfer)
136302ac6454SAndrew Thompson {
136402ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
136502ac6454SAndrew Thompson }
136602ac6454SAndrew Thompson 
136702ac6454SAndrew Thompson static void
136802ac6454SAndrew Thompson atmegadci_device_bulk_enter(struct usb2_xfer *xfer)
136902ac6454SAndrew Thompson {
137002ac6454SAndrew Thompson 	return;
137102ac6454SAndrew Thompson }
137202ac6454SAndrew Thompson 
137302ac6454SAndrew Thompson static void
137402ac6454SAndrew Thompson atmegadci_device_bulk_start(struct usb2_xfer *xfer)
137502ac6454SAndrew Thompson {
137602ac6454SAndrew Thompson 	/* setup TDs */
137702ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
137802ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
137902ac6454SAndrew Thompson }
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_bulk_methods =
138202ac6454SAndrew Thompson {
138302ac6454SAndrew Thompson 	.open = atmegadci_device_bulk_open,
138402ac6454SAndrew Thompson 	.close = atmegadci_device_bulk_close,
138502ac6454SAndrew Thompson 	.enter = atmegadci_device_bulk_enter,
138602ac6454SAndrew Thompson 	.start = atmegadci_device_bulk_start,
138702ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
138802ac6454SAndrew Thompson 	.start_is_cancelable = 1,
138902ac6454SAndrew Thompson };
139002ac6454SAndrew Thompson 
139102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
139202ac6454SAndrew Thompson  * at91dci control support
139302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
139402ac6454SAndrew Thompson static void
139502ac6454SAndrew Thompson atmegadci_device_ctrl_open(struct usb2_xfer *xfer)
139602ac6454SAndrew Thompson {
139702ac6454SAndrew Thompson 	return;
139802ac6454SAndrew Thompson }
139902ac6454SAndrew Thompson 
140002ac6454SAndrew Thompson static void
140102ac6454SAndrew Thompson atmegadci_device_ctrl_close(struct usb2_xfer *xfer)
140202ac6454SAndrew Thompson {
140302ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
140402ac6454SAndrew Thompson }
140502ac6454SAndrew Thompson 
140602ac6454SAndrew Thompson static void
140702ac6454SAndrew Thompson atmegadci_device_ctrl_enter(struct usb2_xfer *xfer)
140802ac6454SAndrew Thompson {
140902ac6454SAndrew Thompson 	return;
141002ac6454SAndrew Thompson }
141102ac6454SAndrew Thompson 
141202ac6454SAndrew Thompson static void
141302ac6454SAndrew Thompson atmegadci_device_ctrl_start(struct usb2_xfer *xfer)
141402ac6454SAndrew Thompson {
141502ac6454SAndrew Thompson 	/* setup TDs */
141602ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
141702ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
141802ac6454SAndrew Thompson }
141902ac6454SAndrew Thompson 
142002ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_ctrl_methods =
142102ac6454SAndrew Thompson {
142202ac6454SAndrew Thompson 	.open = atmegadci_device_ctrl_open,
142302ac6454SAndrew Thompson 	.close = atmegadci_device_ctrl_close,
142402ac6454SAndrew Thompson 	.enter = atmegadci_device_ctrl_enter,
142502ac6454SAndrew Thompson 	.start = atmegadci_device_ctrl_start,
142602ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
142702ac6454SAndrew Thompson 	.start_is_cancelable = 1,
142802ac6454SAndrew Thompson };
142902ac6454SAndrew Thompson 
143002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
143102ac6454SAndrew Thompson  * at91dci interrupt support
143202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
143302ac6454SAndrew Thompson static void
143402ac6454SAndrew Thompson atmegadci_device_intr_open(struct usb2_xfer *xfer)
143502ac6454SAndrew Thompson {
143602ac6454SAndrew Thompson 	return;
143702ac6454SAndrew Thompson }
143802ac6454SAndrew Thompson 
143902ac6454SAndrew Thompson static void
144002ac6454SAndrew Thompson atmegadci_device_intr_close(struct usb2_xfer *xfer)
144102ac6454SAndrew Thompson {
144202ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
144302ac6454SAndrew Thompson }
144402ac6454SAndrew Thompson 
144502ac6454SAndrew Thompson static void
144602ac6454SAndrew Thompson atmegadci_device_intr_enter(struct usb2_xfer *xfer)
144702ac6454SAndrew Thompson {
144802ac6454SAndrew Thompson 	return;
144902ac6454SAndrew Thompson }
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson static void
145202ac6454SAndrew Thompson atmegadci_device_intr_start(struct usb2_xfer *xfer)
145302ac6454SAndrew Thompson {
145402ac6454SAndrew Thompson 	/* setup TDs */
145502ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
145602ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
145702ac6454SAndrew Thompson }
145802ac6454SAndrew Thompson 
145902ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_intr_methods =
146002ac6454SAndrew Thompson {
146102ac6454SAndrew Thompson 	.open = atmegadci_device_intr_open,
146202ac6454SAndrew Thompson 	.close = atmegadci_device_intr_close,
146302ac6454SAndrew Thompson 	.enter = atmegadci_device_intr_enter,
146402ac6454SAndrew Thompson 	.start = atmegadci_device_intr_start,
146502ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
146602ac6454SAndrew Thompson 	.start_is_cancelable = 1,
146702ac6454SAndrew Thompson };
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
147002ac6454SAndrew Thompson  * at91dci full speed isochronous support
147102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
147202ac6454SAndrew Thompson static void
147302ac6454SAndrew Thompson atmegadci_device_isoc_fs_open(struct usb2_xfer *xfer)
147402ac6454SAndrew Thompson {
147502ac6454SAndrew Thompson 	return;
147602ac6454SAndrew Thompson }
147702ac6454SAndrew Thompson 
147802ac6454SAndrew Thompson static void
147902ac6454SAndrew Thompson atmegadci_device_isoc_fs_close(struct usb2_xfer *xfer)
148002ac6454SAndrew Thompson {
148102ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
148202ac6454SAndrew Thompson }
148302ac6454SAndrew Thompson 
148402ac6454SAndrew Thompson static void
148502ac6454SAndrew Thompson atmegadci_device_isoc_fs_enter(struct usb2_xfer *xfer)
148602ac6454SAndrew Thompson {
148702ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
148802ac6454SAndrew Thompson 	uint32_t temp;
148902ac6454SAndrew Thompson 	uint32_t nframes;
149002ac6454SAndrew Thompson 
149102ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
149202ac6454SAndrew Thompson 	    xfer, xfer->pipe->isoc_next, xfer->nframes);
149302ac6454SAndrew Thompson 
149402ac6454SAndrew Thompson 	/* get the current frame index */
149502ac6454SAndrew Thompson 
149602ac6454SAndrew Thompson 	nframes =
149702ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UDFNUMH) << 8) |
149802ac6454SAndrew Thompson 	    (ATMEGA_READ_1(sc, ATMEGA_UDFNUML));
149902ac6454SAndrew Thompson 
150002ac6454SAndrew Thompson 	nframes &= ATMEGA_FRAME_MASK;
150102ac6454SAndrew Thompson 
150202ac6454SAndrew Thompson 	/*
150302ac6454SAndrew Thompson 	 * check if the frame index is within the window where the frames
150402ac6454SAndrew Thompson 	 * will be inserted
150502ac6454SAndrew Thompson 	 */
150602ac6454SAndrew Thompson 	temp = (nframes - xfer->pipe->isoc_next) & ATMEGA_FRAME_MASK;
150702ac6454SAndrew Thompson 
150802ac6454SAndrew Thompson 	if ((xfer->pipe->is_synced == 0) ||
150902ac6454SAndrew Thompson 	    (temp < xfer->nframes)) {
151002ac6454SAndrew Thompson 		/*
151102ac6454SAndrew Thompson 		 * If there is data underflow or the pipe queue is
151202ac6454SAndrew Thompson 		 * empty we schedule the transfer a few frames ahead
151302ac6454SAndrew Thompson 		 * of the current frame position. Else two isochronous
151402ac6454SAndrew Thompson 		 * transfers might overlap.
151502ac6454SAndrew Thompson 		 */
151602ac6454SAndrew Thompson 		xfer->pipe->isoc_next = (nframes + 3) & ATMEGA_FRAME_MASK;
151702ac6454SAndrew Thompson 		xfer->pipe->is_synced = 1;
151802ac6454SAndrew Thompson 		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
151902ac6454SAndrew Thompson 	}
152002ac6454SAndrew Thompson 	/*
152102ac6454SAndrew Thompson 	 * compute how many milliseconds the insertion is ahead of the
152202ac6454SAndrew Thompson 	 * current frame position:
152302ac6454SAndrew Thompson 	 */
152402ac6454SAndrew Thompson 	temp = (xfer->pipe->isoc_next - nframes) & ATMEGA_FRAME_MASK;
152502ac6454SAndrew Thompson 
152602ac6454SAndrew Thompson 	/*
152702ac6454SAndrew Thompson 	 * pre-compute when the isochronous transfer will be finished:
152802ac6454SAndrew Thompson 	 */
152902ac6454SAndrew Thompson 	xfer->isoc_time_complete =
153002ac6454SAndrew Thompson 	    usb2_isoc_time_expand(&sc->sc_bus, nframes) + temp +
153102ac6454SAndrew Thompson 	    xfer->nframes;
153202ac6454SAndrew Thompson 
153302ac6454SAndrew Thompson 	/* compute frame number for next insertion */
153402ac6454SAndrew Thompson 	xfer->pipe->isoc_next += xfer->nframes;
153502ac6454SAndrew Thompson 
153602ac6454SAndrew Thompson 	/* setup TDs */
153702ac6454SAndrew Thompson 	atmegadci_setup_standard_chain(xfer);
153802ac6454SAndrew Thompson }
153902ac6454SAndrew Thompson 
154002ac6454SAndrew Thompson static void
154102ac6454SAndrew Thompson atmegadci_device_isoc_fs_start(struct usb2_xfer *xfer)
154202ac6454SAndrew Thompson {
154302ac6454SAndrew Thompson 	/* start TD chain */
154402ac6454SAndrew Thompson 	atmegadci_start_standard_chain(xfer);
154502ac6454SAndrew Thompson }
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_device_isoc_fs_methods =
154802ac6454SAndrew Thompson {
154902ac6454SAndrew Thompson 	.open = atmegadci_device_isoc_fs_open,
155002ac6454SAndrew Thompson 	.close = atmegadci_device_isoc_fs_close,
155102ac6454SAndrew Thompson 	.enter = atmegadci_device_isoc_fs_enter,
155202ac6454SAndrew Thompson 	.start = atmegadci_device_isoc_fs_start,
155302ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
155402ac6454SAndrew Thompson 	.start_is_cancelable = 1,
155502ac6454SAndrew Thompson };
155602ac6454SAndrew Thompson 
155702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
155802ac6454SAndrew Thompson  * at91dci root control support
155902ac6454SAndrew Thompson  *------------------------------------------------------------------------*
156002ac6454SAndrew Thompson  * simulate a hardware HUB by handling
156102ac6454SAndrew Thompson  * all the necessary requests
156202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
156302ac6454SAndrew Thompson 
156402ac6454SAndrew Thompson static void
156502ac6454SAndrew Thompson atmegadci_root_ctrl_open(struct usb2_xfer *xfer)
156602ac6454SAndrew Thompson {
156702ac6454SAndrew Thompson 	return;
156802ac6454SAndrew Thompson }
156902ac6454SAndrew Thompson 
157002ac6454SAndrew Thompson static void
157102ac6454SAndrew Thompson atmegadci_root_ctrl_close(struct usb2_xfer *xfer)
157202ac6454SAndrew Thompson {
157302ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
157402ac6454SAndrew Thompson 
157502ac6454SAndrew Thompson 	if (sc->sc_root_ctrl.xfer == xfer) {
157602ac6454SAndrew Thompson 		sc->sc_root_ctrl.xfer = NULL;
157702ac6454SAndrew Thompson 	}
157802ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
157902ac6454SAndrew Thompson }
158002ac6454SAndrew Thompson 
158102ac6454SAndrew Thompson /*
158202ac6454SAndrew Thompson  * USB descriptors for the virtual Root HUB:
158302ac6454SAndrew Thompson  */
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson static const struct usb2_device_descriptor atmegadci_devd = {
158602ac6454SAndrew Thompson 	.bLength = sizeof(struct usb2_device_descriptor),
158702ac6454SAndrew Thompson 	.bDescriptorType = UDESC_DEVICE,
158802ac6454SAndrew Thompson 	.bcdUSB = {0x00, 0x02},
158902ac6454SAndrew Thompson 	.bDeviceClass = UDCLASS_HUB,
159002ac6454SAndrew Thompson 	.bDeviceSubClass = UDSUBCLASS_HUB,
159102ac6454SAndrew Thompson 	.bDeviceProtocol = UDPROTO_HSHUBSTT,
159202ac6454SAndrew Thompson 	.bMaxPacketSize = 64,
159302ac6454SAndrew Thompson 	.bcdDevice = {0x00, 0x01},
159402ac6454SAndrew Thompson 	.iManufacturer = 1,
159502ac6454SAndrew Thompson 	.iProduct = 2,
159602ac6454SAndrew Thompson 	.bNumConfigurations = 1,
159702ac6454SAndrew Thompson };
159802ac6454SAndrew Thompson 
159902ac6454SAndrew Thompson static const struct usb2_device_qualifier atmegadci_odevd = {
160002ac6454SAndrew Thompson 	.bLength = sizeof(struct usb2_device_qualifier),
160102ac6454SAndrew Thompson 	.bDescriptorType = UDESC_DEVICE_QUALIFIER,
160202ac6454SAndrew Thompson 	.bcdUSB = {0x00, 0x02},
160302ac6454SAndrew Thompson 	.bDeviceClass = UDCLASS_HUB,
160402ac6454SAndrew Thompson 	.bDeviceSubClass = UDSUBCLASS_HUB,
160502ac6454SAndrew Thompson 	.bDeviceProtocol = UDPROTO_FSHUB,
160602ac6454SAndrew Thompson 	.bMaxPacketSize0 = 0,
160702ac6454SAndrew Thompson 	.bNumConfigurations = 0,
160802ac6454SAndrew Thompson };
160902ac6454SAndrew Thompson 
161002ac6454SAndrew Thompson static const struct atmegadci_config_desc atmegadci_confd = {
161102ac6454SAndrew Thompson 	.confd = {
161202ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_config_descriptor),
161302ac6454SAndrew Thompson 		.bDescriptorType = UDESC_CONFIG,
161402ac6454SAndrew Thompson 		.wTotalLength[0] = sizeof(atmegadci_confd),
161502ac6454SAndrew Thompson 		.bNumInterface = 1,
161602ac6454SAndrew Thompson 		.bConfigurationValue = 1,
161702ac6454SAndrew Thompson 		.iConfiguration = 0,
161802ac6454SAndrew Thompson 		.bmAttributes = UC_SELF_POWERED,
161902ac6454SAndrew Thompson 		.bMaxPower = 0,
162002ac6454SAndrew Thompson 	},
162102ac6454SAndrew Thompson 	.ifcd = {
162202ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_interface_descriptor),
162302ac6454SAndrew Thompson 		.bDescriptorType = UDESC_INTERFACE,
162402ac6454SAndrew Thompson 		.bNumEndpoints = 1,
162502ac6454SAndrew Thompson 		.bInterfaceClass = UICLASS_HUB,
162602ac6454SAndrew Thompson 		.bInterfaceSubClass = UISUBCLASS_HUB,
162702ac6454SAndrew Thompson 		.bInterfaceProtocol = UIPROTO_HSHUBSTT,
162802ac6454SAndrew Thompson 	},
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 	.endpd = {
163102ac6454SAndrew Thompson 		.bLength = sizeof(struct usb2_endpoint_descriptor),
163202ac6454SAndrew Thompson 		.bDescriptorType = UDESC_ENDPOINT,
163302ac6454SAndrew Thompson 		.bEndpointAddress = (UE_DIR_IN | ATMEGA_INTR_ENDPT),
163402ac6454SAndrew Thompson 		.bmAttributes = UE_INTERRUPT,
163502ac6454SAndrew Thompson 		.wMaxPacketSize[0] = 8,
163602ac6454SAndrew Thompson 		.bInterval = 255,
163702ac6454SAndrew Thompson 	},
163802ac6454SAndrew Thompson };
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson static const struct usb2_hub_descriptor_min atmegadci_hubd = {
164102ac6454SAndrew Thompson 	.bDescLength = sizeof(atmegadci_hubd),
164202ac6454SAndrew Thompson 	.bDescriptorType = UDESC_HUB,
164302ac6454SAndrew Thompson 	.bNbrPorts = 1,
164402ac6454SAndrew Thompson 	.wHubCharacteristics[0] =
164502ac6454SAndrew Thompson 	(UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) & 0xFF,
164602ac6454SAndrew Thompson 	.wHubCharacteristics[1] =
164702ac6454SAndrew Thompson 	(UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) >> 8,
164802ac6454SAndrew Thompson 	.bPwrOn2PwrGood = 50,
164902ac6454SAndrew Thompson 	.bHubContrCurrent = 0,
165002ac6454SAndrew Thompson 	.DeviceRemovable = {0},		/* port is removable */
165102ac6454SAndrew Thompson };
165202ac6454SAndrew Thompson 
165302ac6454SAndrew Thompson #define	STRING_LANG \
165402ac6454SAndrew Thompson   0x09, 0x04,				/* American English */
165502ac6454SAndrew Thompson 
165602ac6454SAndrew Thompson #define	STRING_VENDOR \
165702ac6454SAndrew Thompson   'A', 0, 'T', 0, 'M', 0, 'E', 0, 'G', 0, 'A', 0
165802ac6454SAndrew Thompson 
165902ac6454SAndrew Thompson #define	STRING_PRODUCT \
166002ac6454SAndrew Thompson   'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \
166102ac6454SAndrew Thompson   'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \
166202ac6454SAndrew Thompson   'U', 0, 'B', 0,
166302ac6454SAndrew Thompson 
166402ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_LANG, atmegadci_langtab);
166502ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_VENDOR, atmegadci_vendor);
166602ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_PRODUCT, atmegadci_product);
166702ac6454SAndrew Thompson 
166802ac6454SAndrew Thompson static void
166902ac6454SAndrew Thompson atmegadci_root_ctrl_enter(struct usb2_xfer *xfer)
167002ac6454SAndrew Thompson {
167102ac6454SAndrew Thompson 	return;
167202ac6454SAndrew Thompson }
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson static void
167502ac6454SAndrew Thompson atmegadci_root_ctrl_start(struct usb2_xfer *xfer)
167602ac6454SAndrew Thompson {
167702ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
167802ac6454SAndrew Thompson 
167902ac6454SAndrew Thompson 	sc->sc_root_ctrl.xfer = xfer;
168002ac6454SAndrew Thompson 
168102ac6454SAndrew Thompson 	usb2_bus_roothub_exec(xfer->xroot->bus);
168202ac6454SAndrew Thompson }
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson static void
168502ac6454SAndrew Thompson atmegadci_root_ctrl_task(struct usb2_bus *bus)
168602ac6454SAndrew Thompson {
168702ac6454SAndrew Thompson 	atmegadci_root_ctrl_poll(ATMEGA_BUS2SC(bus));
168802ac6454SAndrew Thompson }
168902ac6454SAndrew Thompson 
169002ac6454SAndrew Thompson static void
169102ac6454SAndrew Thompson atmegadci_root_ctrl_done(struct usb2_xfer *xfer,
169202ac6454SAndrew Thompson     struct usb2_sw_transfer *std)
169302ac6454SAndrew Thompson {
169402ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
169502ac6454SAndrew Thompson 	uint16_t value;
169602ac6454SAndrew Thompson 	uint16_t index;
1697d59dbd0aSAndrew Thompson 	uint8_t temp;
169802ac6454SAndrew Thompson 
169902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
170002ac6454SAndrew Thompson 
170102ac6454SAndrew Thompson 	if (std->state != USB_SW_TR_SETUP) {
170202ac6454SAndrew Thompson 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
170302ac6454SAndrew Thompson 			/* transfer transferred */
170402ac6454SAndrew Thompson 			atmegadci_device_done(xfer, std->err);
170502ac6454SAndrew Thompson 		}
170602ac6454SAndrew Thompson 		goto done;
170702ac6454SAndrew Thompson 	}
170802ac6454SAndrew Thompson 	/* buffer reset */
170902ac6454SAndrew Thompson 	std->ptr = USB_ADD_BYTES(&sc->sc_hub_temp, 0);
171002ac6454SAndrew Thompson 	std->len = 0;
171102ac6454SAndrew Thompson 
171202ac6454SAndrew Thompson 	value = UGETW(std->req.wValue);
171302ac6454SAndrew Thompson 	index = UGETW(std->req.wIndex);
171402ac6454SAndrew Thompson 
171502ac6454SAndrew Thompson 	/* demultiplex the control request */
171602ac6454SAndrew Thompson 
171702ac6454SAndrew Thompson 	switch (std->req.bmRequestType) {
171802ac6454SAndrew Thompson 	case UT_READ_DEVICE:
171902ac6454SAndrew Thompson 		switch (std->req.bRequest) {
172002ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
172102ac6454SAndrew Thompson 			goto tr_handle_get_descriptor;
172202ac6454SAndrew Thompson 		case UR_GET_CONFIG:
172302ac6454SAndrew Thompson 			goto tr_handle_get_config;
172402ac6454SAndrew Thompson 		case UR_GET_STATUS:
172502ac6454SAndrew Thompson 			goto tr_handle_get_status;
172602ac6454SAndrew Thompson 		default:
172702ac6454SAndrew Thompson 			goto tr_stalled;
172802ac6454SAndrew Thompson 		}
172902ac6454SAndrew Thompson 		break;
173002ac6454SAndrew Thompson 
173102ac6454SAndrew Thompson 	case UT_WRITE_DEVICE:
173202ac6454SAndrew Thompson 		switch (std->req.bRequest) {
173302ac6454SAndrew Thompson 		case UR_SET_ADDRESS:
173402ac6454SAndrew Thompson 			goto tr_handle_set_address;
173502ac6454SAndrew Thompson 		case UR_SET_CONFIG:
173602ac6454SAndrew Thompson 			goto tr_handle_set_config;
173702ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
173802ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
173902ac6454SAndrew Thompson 		case UR_SET_DESCRIPTOR:
174002ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
174102ac6454SAndrew Thompson 		case UR_SET_FEATURE:
174202ac6454SAndrew Thompson 		default:
174302ac6454SAndrew Thompson 			goto tr_stalled;
174402ac6454SAndrew Thompson 		}
174502ac6454SAndrew Thompson 		break;
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 	case UT_WRITE_ENDPOINT:
174802ac6454SAndrew Thompson 		switch (std->req.bRequest) {
174902ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
175002ac6454SAndrew Thompson 			switch (UGETW(std->req.wValue)) {
175102ac6454SAndrew Thompson 			case UF_ENDPOINT_HALT:
175202ac6454SAndrew Thompson 				goto tr_handle_clear_halt;
175302ac6454SAndrew Thompson 			case UF_DEVICE_REMOTE_WAKEUP:
175402ac6454SAndrew Thompson 				goto tr_handle_clear_wakeup;
175502ac6454SAndrew Thompson 			default:
175602ac6454SAndrew Thompson 				goto tr_stalled;
175702ac6454SAndrew Thompson 			}
175802ac6454SAndrew Thompson 			break;
175902ac6454SAndrew Thompson 		case UR_SET_FEATURE:
176002ac6454SAndrew Thompson 			switch (UGETW(std->req.wValue)) {
176102ac6454SAndrew Thompson 			case UF_ENDPOINT_HALT:
176202ac6454SAndrew Thompson 				goto tr_handle_set_halt;
176302ac6454SAndrew Thompson 			case UF_DEVICE_REMOTE_WAKEUP:
176402ac6454SAndrew Thompson 				goto tr_handle_set_wakeup;
176502ac6454SAndrew Thompson 			default:
176602ac6454SAndrew Thompson 				goto tr_stalled;
176702ac6454SAndrew Thompson 			}
176802ac6454SAndrew Thompson 			break;
176902ac6454SAndrew Thompson 		case UR_SYNCH_FRAME:
177002ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
177102ac6454SAndrew Thompson 		default:
177202ac6454SAndrew Thompson 			goto tr_stalled;
177302ac6454SAndrew Thompson 		}
177402ac6454SAndrew Thompson 		break;
177502ac6454SAndrew Thompson 
177602ac6454SAndrew Thompson 	case UT_READ_ENDPOINT:
177702ac6454SAndrew Thompson 		switch (std->req.bRequest) {
177802ac6454SAndrew Thompson 		case UR_GET_STATUS:
177902ac6454SAndrew Thompson 			goto tr_handle_get_ep_status;
178002ac6454SAndrew Thompson 		default:
178102ac6454SAndrew Thompson 			goto tr_stalled;
178202ac6454SAndrew Thompson 		}
178302ac6454SAndrew Thompson 		break;
178402ac6454SAndrew Thompson 
178502ac6454SAndrew Thompson 	case UT_WRITE_INTERFACE:
178602ac6454SAndrew Thompson 		switch (std->req.bRequest) {
178702ac6454SAndrew Thompson 		case UR_SET_INTERFACE:
178802ac6454SAndrew Thompson 			goto tr_handle_set_interface;
178902ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
179002ac6454SAndrew Thompson 			goto tr_valid;	/* nop */
179102ac6454SAndrew Thompson 		case UR_SET_FEATURE:
179202ac6454SAndrew Thompson 		default:
179302ac6454SAndrew Thompson 			goto tr_stalled;
179402ac6454SAndrew Thompson 		}
179502ac6454SAndrew Thompson 		break;
179602ac6454SAndrew Thompson 
179702ac6454SAndrew Thompson 	case UT_READ_INTERFACE:
179802ac6454SAndrew Thompson 		switch (std->req.bRequest) {
179902ac6454SAndrew Thompson 		case UR_GET_INTERFACE:
180002ac6454SAndrew Thompson 			goto tr_handle_get_interface;
180102ac6454SAndrew Thompson 		case UR_GET_STATUS:
180202ac6454SAndrew Thompson 			goto tr_handle_get_iface_status;
180302ac6454SAndrew Thompson 		default:
180402ac6454SAndrew Thompson 			goto tr_stalled;
180502ac6454SAndrew Thompson 		}
180602ac6454SAndrew Thompson 		break;
180702ac6454SAndrew Thompson 
180802ac6454SAndrew Thompson 	case UT_WRITE_CLASS_INTERFACE:
180902ac6454SAndrew Thompson 	case UT_WRITE_VENDOR_INTERFACE:
181002ac6454SAndrew Thompson 		/* XXX forward */
181102ac6454SAndrew Thompson 		break;
181202ac6454SAndrew Thompson 
181302ac6454SAndrew Thompson 	case UT_READ_CLASS_INTERFACE:
181402ac6454SAndrew Thompson 	case UT_READ_VENDOR_INTERFACE:
181502ac6454SAndrew Thompson 		/* XXX forward */
181602ac6454SAndrew Thompson 		break;
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson 	case UT_WRITE_CLASS_DEVICE:
181902ac6454SAndrew Thompson 		switch (std->req.bRequest) {
182002ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
182102ac6454SAndrew Thompson 			goto tr_valid;
182202ac6454SAndrew Thompson 		case UR_SET_DESCRIPTOR:
182302ac6454SAndrew Thompson 		case UR_SET_FEATURE:
182402ac6454SAndrew Thompson 			break;
182502ac6454SAndrew Thompson 		default:
182602ac6454SAndrew Thompson 			goto tr_stalled;
182702ac6454SAndrew Thompson 		}
182802ac6454SAndrew Thompson 		break;
182902ac6454SAndrew Thompson 
183002ac6454SAndrew Thompson 	case UT_WRITE_CLASS_OTHER:
183102ac6454SAndrew Thompson 		switch (std->req.bRequest) {
183202ac6454SAndrew Thompson 		case UR_CLEAR_FEATURE:
183302ac6454SAndrew Thompson 			goto tr_handle_clear_port_feature;
183402ac6454SAndrew Thompson 		case UR_SET_FEATURE:
183502ac6454SAndrew Thompson 			goto tr_handle_set_port_feature;
183602ac6454SAndrew Thompson 		case UR_CLEAR_TT_BUFFER:
183702ac6454SAndrew Thompson 		case UR_RESET_TT:
183802ac6454SAndrew Thompson 		case UR_STOP_TT:
183902ac6454SAndrew Thompson 			goto tr_valid;
184002ac6454SAndrew Thompson 
184102ac6454SAndrew Thompson 		default:
184202ac6454SAndrew Thompson 			goto tr_stalled;
184302ac6454SAndrew Thompson 		}
184402ac6454SAndrew Thompson 		break;
184502ac6454SAndrew Thompson 
184602ac6454SAndrew Thompson 	case UT_READ_CLASS_OTHER:
184702ac6454SAndrew Thompson 		switch (std->req.bRequest) {
184802ac6454SAndrew Thompson 		case UR_GET_TT_STATE:
184902ac6454SAndrew Thompson 			goto tr_handle_get_tt_state;
185002ac6454SAndrew Thompson 		case UR_GET_STATUS:
185102ac6454SAndrew Thompson 			goto tr_handle_get_port_status;
185202ac6454SAndrew Thompson 		default:
185302ac6454SAndrew Thompson 			goto tr_stalled;
185402ac6454SAndrew Thompson 		}
185502ac6454SAndrew Thompson 		break;
185602ac6454SAndrew Thompson 
185702ac6454SAndrew Thompson 	case UT_READ_CLASS_DEVICE:
185802ac6454SAndrew Thompson 		switch (std->req.bRequest) {
185902ac6454SAndrew Thompson 		case UR_GET_DESCRIPTOR:
186002ac6454SAndrew Thompson 			goto tr_handle_get_class_descriptor;
186102ac6454SAndrew Thompson 		case UR_GET_STATUS:
186202ac6454SAndrew Thompson 			goto tr_handle_get_class_status;
186302ac6454SAndrew Thompson 
186402ac6454SAndrew Thompson 		default:
186502ac6454SAndrew Thompson 			goto tr_stalled;
186602ac6454SAndrew Thompson 		}
186702ac6454SAndrew Thompson 		break;
186802ac6454SAndrew Thompson 	default:
186902ac6454SAndrew Thompson 		goto tr_stalled;
187002ac6454SAndrew Thompson 	}
187102ac6454SAndrew Thompson 	goto tr_valid;
187202ac6454SAndrew Thompson 
187302ac6454SAndrew Thompson tr_handle_get_descriptor:
187402ac6454SAndrew Thompson 	switch (value >> 8) {
187502ac6454SAndrew Thompson 	case UDESC_DEVICE:
187602ac6454SAndrew Thompson 		if (value & 0xff) {
187702ac6454SAndrew Thompson 			goto tr_stalled;
187802ac6454SAndrew Thompson 		}
187902ac6454SAndrew Thompson 		std->len = sizeof(atmegadci_devd);
188002ac6454SAndrew Thompson 		std->ptr = USB_ADD_BYTES(&atmegadci_devd, 0);
188102ac6454SAndrew Thompson 		goto tr_valid;
188202ac6454SAndrew Thompson 	case UDESC_CONFIG:
188302ac6454SAndrew Thompson 		if (value & 0xff) {
188402ac6454SAndrew Thompson 			goto tr_stalled;
188502ac6454SAndrew Thompson 		}
188602ac6454SAndrew Thompson 		std->len = sizeof(atmegadci_confd);
188702ac6454SAndrew Thompson 		std->ptr = USB_ADD_BYTES(&atmegadci_confd, 0);
188802ac6454SAndrew Thompson 		goto tr_valid;
188902ac6454SAndrew Thompson 	case UDESC_STRING:
189002ac6454SAndrew Thompson 		switch (value & 0xff) {
189102ac6454SAndrew Thompson 		case 0:		/* Language table */
189202ac6454SAndrew Thompson 			std->len = sizeof(atmegadci_langtab);
189302ac6454SAndrew Thompson 			std->ptr = USB_ADD_BYTES(&atmegadci_langtab, 0);
189402ac6454SAndrew Thompson 			goto tr_valid;
189502ac6454SAndrew Thompson 
189602ac6454SAndrew Thompson 		case 1:		/* Vendor */
189702ac6454SAndrew Thompson 			std->len = sizeof(atmegadci_vendor);
189802ac6454SAndrew Thompson 			std->ptr = USB_ADD_BYTES(&atmegadci_vendor, 0);
189902ac6454SAndrew Thompson 			goto tr_valid;
190002ac6454SAndrew Thompson 
190102ac6454SAndrew Thompson 		case 2:		/* Product */
190202ac6454SAndrew Thompson 			std->len = sizeof(atmegadci_product);
190302ac6454SAndrew Thompson 			std->ptr = USB_ADD_BYTES(&atmegadci_product, 0);
190402ac6454SAndrew Thompson 			goto tr_valid;
190502ac6454SAndrew Thompson 		default:
190602ac6454SAndrew Thompson 			break;
190702ac6454SAndrew Thompson 		}
190802ac6454SAndrew Thompson 		break;
190902ac6454SAndrew Thompson 	default:
191002ac6454SAndrew Thompson 		goto tr_stalled;
191102ac6454SAndrew Thompson 	}
191202ac6454SAndrew Thompson 	goto tr_stalled;
191302ac6454SAndrew Thompson 
191402ac6454SAndrew Thompson tr_handle_get_config:
191502ac6454SAndrew Thompson 	std->len = 1;
191602ac6454SAndrew Thompson 	sc->sc_hub_temp.wValue[0] = sc->sc_conf;
191702ac6454SAndrew Thompson 	goto tr_valid;
191802ac6454SAndrew Thompson 
191902ac6454SAndrew Thompson tr_handle_get_status:
192002ac6454SAndrew Thompson 	std->len = 2;
192102ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
192202ac6454SAndrew Thompson 	goto tr_valid;
192302ac6454SAndrew Thompson 
192402ac6454SAndrew Thompson tr_handle_set_address:
192502ac6454SAndrew Thompson 	if (value & 0xFF00) {
192602ac6454SAndrew Thompson 		goto tr_stalled;
192702ac6454SAndrew Thompson 	}
192802ac6454SAndrew Thompson 	sc->sc_rt_addr = value;
192902ac6454SAndrew Thompson 	goto tr_valid;
193002ac6454SAndrew Thompson 
193102ac6454SAndrew Thompson tr_handle_set_config:
193202ac6454SAndrew Thompson 	if (value >= 2) {
193302ac6454SAndrew Thompson 		goto tr_stalled;
193402ac6454SAndrew Thompson 	}
193502ac6454SAndrew Thompson 	sc->sc_conf = value;
193602ac6454SAndrew Thompson 	goto tr_valid;
193702ac6454SAndrew Thompson 
193802ac6454SAndrew Thompson tr_handle_get_interface:
193902ac6454SAndrew Thompson 	std->len = 1;
194002ac6454SAndrew Thompson 	sc->sc_hub_temp.wValue[0] = 0;
194102ac6454SAndrew Thompson 	goto tr_valid;
194202ac6454SAndrew Thompson 
194302ac6454SAndrew Thompson tr_handle_get_tt_state:
194402ac6454SAndrew Thompson tr_handle_get_class_status:
194502ac6454SAndrew Thompson tr_handle_get_iface_status:
194602ac6454SAndrew Thompson tr_handle_get_ep_status:
194702ac6454SAndrew Thompson 	std->len = 2;
194802ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.wValue, 0);
194902ac6454SAndrew Thompson 	goto tr_valid;
195002ac6454SAndrew Thompson 
195102ac6454SAndrew Thompson tr_handle_set_halt:
195202ac6454SAndrew Thompson tr_handle_set_interface:
195302ac6454SAndrew Thompson tr_handle_set_wakeup:
195402ac6454SAndrew Thompson tr_handle_clear_wakeup:
195502ac6454SAndrew Thompson tr_handle_clear_halt:
195602ac6454SAndrew Thompson 	goto tr_valid;
195702ac6454SAndrew Thompson 
195802ac6454SAndrew Thompson tr_handle_clear_port_feature:
195902ac6454SAndrew Thompson 	if (index != 1) {
196002ac6454SAndrew Thompson 		goto tr_stalled;
196102ac6454SAndrew Thompson 	}
196202ac6454SAndrew Thompson 	DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
196302ac6454SAndrew Thompson 
196402ac6454SAndrew Thompson 	switch (value) {
196502ac6454SAndrew Thompson 	case UHF_PORT_SUSPEND:
196602ac6454SAndrew Thompson 		atmegadci_wakeup_peer(xfer);
196702ac6454SAndrew Thompson 		break;
196802ac6454SAndrew Thompson 
196902ac6454SAndrew Thompson 	case UHF_PORT_ENABLE:
197002ac6454SAndrew Thompson 		sc->sc_flags.port_enabled = 0;
197102ac6454SAndrew Thompson 		break;
197202ac6454SAndrew Thompson 
197302ac6454SAndrew Thompson 	case UHF_PORT_TEST:
197402ac6454SAndrew Thompson 	case UHF_PORT_INDICATOR:
197502ac6454SAndrew Thompson 	case UHF_C_PORT_ENABLE:
197602ac6454SAndrew Thompson 	case UHF_C_PORT_OVER_CURRENT:
197702ac6454SAndrew Thompson 	case UHF_C_PORT_RESET:
197802ac6454SAndrew Thompson 		/* nops */
197902ac6454SAndrew Thompson 		break;
198002ac6454SAndrew Thompson 	case UHF_PORT_POWER:
198102ac6454SAndrew Thompson 		sc->sc_flags.port_powered = 0;
198202ac6454SAndrew Thompson 		atmegadci_pull_down(sc);
198302ac6454SAndrew Thompson 		atmegadci_clocks_off(sc);
198402ac6454SAndrew Thompson 		break;
198502ac6454SAndrew Thompson 	case UHF_C_PORT_CONNECTION:
1986d59dbd0aSAndrew Thompson 		/* clear connect change flag */
198702ac6454SAndrew Thompson 		sc->sc_flags.change_connect = 0;
1988d59dbd0aSAndrew Thompson 
1989d59dbd0aSAndrew Thompson 		/* configure the control endpoint */
1990d59dbd0aSAndrew Thompson 
1991d59dbd0aSAndrew Thompson 		/* select endpoint number */
1992d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UENUM, 0);
1993d59dbd0aSAndrew Thompson 
1994d59dbd0aSAndrew Thompson 		/* set endpoint reset */
1995d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(0));
1996d59dbd0aSAndrew Thompson 
1997d59dbd0aSAndrew Thompson 		/* clear endpoint reset */
1998d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1999d59dbd0aSAndrew Thompson 
2000d59dbd0aSAndrew Thompson 		/* enable and stall endpoint */
2001d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
2002d59dbd0aSAndrew Thompson 		    ATMEGA_UECONX_EPEN |
2003d59dbd0aSAndrew Thompson 		    ATMEGA_UECONX_STALLRQ);
2004d59dbd0aSAndrew Thompson 
2005d59dbd0aSAndrew Thompson 		/* one bank, 64-bytes wMaxPacket */
2006d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X,
2007d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG0X_EPTYPE0);
2008d59dbd0aSAndrew Thompson 		ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
2009d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_ALLOC |
2010d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPBK0 |
2011d59dbd0aSAndrew Thompson 		    ATMEGA_UECFG1X_EPSIZE(3));
2012d59dbd0aSAndrew Thompson 
2013d59dbd0aSAndrew Thompson 		/* check valid config */
2014d59dbd0aSAndrew Thompson 		temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
2015d59dbd0aSAndrew Thompson 		if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
2016d59dbd0aSAndrew Thompson 			DPRINTFN(0, "Chip rejected EP0 configuration\n");
2017d59dbd0aSAndrew Thompson 		}
201802ac6454SAndrew Thompson 		break;
201902ac6454SAndrew Thompson 	case UHF_C_PORT_SUSPEND:
202002ac6454SAndrew Thompson 		sc->sc_flags.change_suspend = 0;
202102ac6454SAndrew Thompson 		break;
202202ac6454SAndrew Thompson 	default:
202302ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
202402ac6454SAndrew Thompson 		goto done;
202502ac6454SAndrew Thompson 	}
202602ac6454SAndrew Thompson 	goto tr_valid;
202702ac6454SAndrew Thompson 
202802ac6454SAndrew Thompson tr_handle_set_port_feature:
202902ac6454SAndrew Thompson 	if (index != 1) {
203002ac6454SAndrew Thompson 		goto tr_stalled;
203102ac6454SAndrew Thompson 	}
203202ac6454SAndrew Thompson 	DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
203302ac6454SAndrew Thompson 
203402ac6454SAndrew Thompson 	switch (value) {
203502ac6454SAndrew Thompson 	case UHF_PORT_ENABLE:
203602ac6454SAndrew Thompson 		sc->sc_flags.port_enabled = 1;
203702ac6454SAndrew Thompson 		break;
203802ac6454SAndrew Thompson 	case UHF_PORT_SUSPEND:
203902ac6454SAndrew Thompson 	case UHF_PORT_RESET:
204002ac6454SAndrew Thompson 	case UHF_PORT_TEST:
204102ac6454SAndrew Thompson 	case UHF_PORT_INDICATOR:
204202ac6454SAndrew Thompson 		/* nops */
204302ac6454SAndrew Thompson 		break;
204402ac6454SAndrew Thompson 	case UHF_PORT_POWER:
204502ac6454SAndrew Thompson 		sc->sc_flags.port_powered = 1;
204602ac6454SAndrew Thompson 		break;
204702ac6454SAndrew Thompson 	default:
204802ac6454SAndrew Thompson 		std->err = USB_ERR_IOERROR;
204902ac6454SAndrew Thompson 		goto done;
205002ac6454SAndrew Thompson 	}
205102ac6454SAndrew Thompson 	goto tr_valid;
205202ac6454SAndrew Thompson 
205302ac6454SAndrew Thompson tr_handle_get_port_status:
205402ac6454SAndrew Thompson 
205502ac6454SAndrew Thompson 	DPRINTFN(9, "UR_GET_PORT_STATUS\n");
205602ac6454SAndrew Thompson 
205702ac6454SAndrew Thompson 	if (index != 1) {
205802ac6454SAndrew Thompson 		goto tr_stalled;
205902ac6454SAndrew Thompson 	}
206002ac6454SAndrew Thompson 	if (sc->sc_flags.status_vbus) {
206102ac6454SAndrew Thompson 		atmegadci_clocks_on(sc);
206202ac6454SAndrew Thompson 		atmegadci_pull_up(sc);
206302ac6454SAndrew Thompson 	} else {
206402ac6454SAndrew Thompson 		atmegadci_pull_down(sc);
206502ac6454SAndrew Thompson 		atmegadci_clocks_off(sc);
206602ac6454SAndrew Thompson 	}
206702ac6454SAndrew Thompson 
206802ac6454SAndrew Thompson 	/* Select FULL-speed and Device Side Mode */
206902ac6454SAndrew Thompson 
207002ac6454SAndrew Thompson 	value = UPS_PORT_MODE_DEVICE;
207102ac6454SAndrew Thompson 
207202ac6454SAndrew Thompson 	if (sc->sc_flags.port_powered) {
207302ac6454SAndrew Thompson 		value |= UPS_PORT_POWER;
207402ac6454SAndrew Thompson 	}
207502ac6454SAndrew Thompson 	if (sc->sc_flags.port_enabled) {
207602ac6454SAndrew Thompson 		value |= UPS_PORT_ENABLED;
207702ac6454SAndrew Thompson 	}
207802ac6454SAndrew Thompson 	if (sc->sc_flags.status_vbus &&
207902ac6454SAndrew Thompson 	    sc->sc_flags.status_bus_reset) {
208002ac6454SAndrew Thompson 		value |= UPS_CURRENT_CONNECT_STATUS;
208102ac6454SAndrew Thompson 	}
208202ac6454SAndrew Thompson 	if (sc->sc_flags.status_suspend) {
208302ac6454SAndrew Thompson 		value |= UPS_SUSPEND;
208402ac6454SAndrew Thompson 	}
208502ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.ps.wPortStatus, value);
208602ac6454SAndrew Thompson 
208702ac6454SAndrew Thompson 	value = 0;
208802ac6454SAndrew Thompson 
208902ac6454SAndrew Thompson 	if (sc->sc_flags.change_connect) {
209002ac6454SAndrew Thompson 		value |= UPS_C_CONNECT_STATUS;
209102ac6454SAndrew Thompson 	}
209202ac6454SAndrew Thompson 	if (sc->sc_flags.change_suspend) {
209302ac6454SAndrew Thompson 		value |= UPS_C_SUSPEND;
209402ac6454SAndrew Thompson 	}
209502ac6454SAndrew Thompson 	USETW(sc->sc_hub_temp.ps.wPortChange, value);
209602ac6454SAndrew Thompson 	std->len = sizeof(sc->sc_hub_temp.ps);
209702ac6454SAndrew Thompson 	goto tr_valid;
209802ac6454SAndrew Thompson 
209902ac6454SAndrew Thompson tr_handle_get_class_descriptor:
210002ac6454SAndrew Thompson 	if (value & 0xFF) {
210102ac6454SAndrew Thompson 		goto tr_stalled;
210202ac6454SAndrew Thompson 	}
210302ac6454SAndrew Thompson 	std->ptr = USB_ADD_BYTES(&atmegadci_hubd, 0);
210402ac6454SAndrew Thompson 	std->len = sizeof(atmegadci_hubd);
210502ac6454SAndrew Thompson 	goto tr_valid;
210602ac6454SAndrew Thompson 
210702ac6454SAndrew Thompson tr_stalled:
210802ac6454SAndrew Thompson 	std->err = USB_ERR_STALLED;
210902ac6454SAndrew Thompson tr_valid:
211002ac6454SAndrew Thompson done:
211102ac6454SAndrew Thompson 	return;
211202ac6454SAndrew Thompson }
211302ac6454SAndrew Thompson 
211402ac6454SAndrew Thompson static void
211502ac6454SAndrew Thompson atmegadci_root_ctrl_poll(struct atmegadci_softc *sc)
211602ac6454SAndrew Thompson {
211702ac6454SAndrew Thompson 	usb2_sw_transfer(&sc->sc_root_ctrl,
211802ac6454SAndrew Thompson 	    &atmegadci_root_ctrl_done);
211902ac6454SAndrew Thompson }
212002ac6454SAndrew Thompson 
212102ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_root_ctrl_methods =
212202ac6454SAndrew Thompson {
212302ac6454SAndrew Thompson 	.open = atmegadci_root_ctrl_open,
212402ac6454SAndrew Thompson 	.close = atmegadci_root_ctrl_close,
212502ac6454SAndrew Thompson 	.enter = atmegadci_root_ctrl_enter,
212602ac6454SAndrew Thompson 	.start = atmegadci_root_ctrl_start,
212702ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
212802ac6454SAndrew Thompson 	.start_is_cancelable = 0,
212902ac6454SAndrew Thompson };
213002ac6454SAndrew Thompson 
213102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
213202ac6454SAndrew Thompson  * at91dci root interrupt support
213302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
213402ac6454SAndrew Thompson static void
213502ac6454SAndrew Thompson atmegadci_root_intr_open(struct usb2_xfer *xfer)
213602ac6454SAndrew Thompson {
213702ac6454SAndrew Thompson 	return;
213802ac6454SAndrew Thompson }
213902ac6454SAndrew Thompson 
214002ac6454SAndrew Thompson static void
214102ac6454SAndrew Thompson atmegadci_root_intr_close(struct usb2_xfer *xfer)
214202ac6454SAndrew Thompson {
214302ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
214402ac6454SAndrew Thompson 
214502ac6454SAndrew Thompson 	if (sc->sc_root_intr.xfer == xfer) {
214602ac6454SAndrew Thompson 		sc->sc_root_intr.xfer = NULL;
214702ac6454SAndrew Thompson 	}
214802ac6454SAndrew Thompson 	atmegadci_device_done(xfer, USB_ERR_CANCELLED);
214902ac6454SAndrew Thompson }
215002ac6454SAndrew Thompson 
215102ac6454SAndrew Thompson static void
215202ac6454SAndrew Thompson atmegadci_root_intr_enter(struct usb2_xfer *xfer)
215302ac6454SAndrew Thompson {
215402ac6454SAndrew Thompson 	return;
215502ac6454SAndrew Thompson }
215602ac6454SAndrew Thompson 
215702ac6454SAndrew Thompson static void
215802ac6454SAndrew Thompson atmegadci_root_intr_start(struct usb2_xfer *xfer)
215902ac6454SAndrew Thompson {
216002ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
216102ac6454SAndrew Thompson 
216202ac6454SAndrew Thompson 	sc->sc_root_intr.xfer = xfer;
216302ac6454SAndrew Thompson }
216402ac6454SAndrew Thompson 
216502ac6454SAndrew Thompson struct usb2_pipe_methods atmegadci_root_intr_methods =
216602ac6454SAndrew Thompson {
216702ac6454SAndrew Thompson 	.open = atmegadci_root_intr_open,
216802ac6454SAndrew Thompson 	.close = atmegadci_root_intr_close,
216902ac6454SAndrew Thompson 	.enter = atmegadci_root_intr_enter,
217002ac6454SAndrew Thompson 	.start = atmegadci_root_intr_start,
217102ac6454SAndrew Thompson 	.enter_is_cancelable = 1,
217202ac6454SAndrew Thompson 	.start_is_cancelable = 1,
217302ac6454SAndrew Thompson };
217402ac6454SAndrew Thompson 
217502ac6454SAndrew Thompson static void
217602ac6454SAndrew Thompson atmegadci_xfer_setup(struct usb2_setup_params *parm)
217702ac6454SAndrew Thompson {
217802ac6454SAndrew Thompson 	const struct usb2_hw_ep_profile *pf;
217902ac6454SAndrew Thompson 	struct atmegadci_softc *sc;
218002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
218102ac6454SAndrew Thompson 	void *last_obj;
218202ac6454SAndrew Thompson 	uint32_t ntd;
218302ac6454SAndrew Thompson 	uint32_t n;
218402ac6454SAndrew Thompson 	uint8_t ep_no;
218502ac6454SAndrew Thompson 
218602ac6454SAndrew Thompson 	sc = ATMEGA_BUS2SC(parm->udev->bus);
218702ac6454SAndrew Thompson 	xfer = parm->curr_xfer;
218802ac6454SAndrew Thompson 
218902ac6454SAndrew Thompson 	/*
219002ac6454SAndrew Thompson 	 * NOTE: This driver does not use any of the parameters that
219102ac6454SAndrew Thompson 	 * are computed from the following values. Just set some
219202ac6454SAndrew Thompson 	 * reasonable dummies:
219302ac6454SAndrew Thompson 	 */
219402ac6454SAndrew Thompson 	parm->hc_max_packet_size = 0x500;
219502ac6454SAndrew Thompson 	parm->hc_max_packet_count = 1;
219602ac6454SAndrew Thompson 	parm->hc_max_frame_size = 0x500;
219702ac6454SAndrew Thompson 
219802ac6454SAndrew Thompson 	usb2_transfer_setup_sub(parm);
219902ac6454SAndrew Thompson 
220002ac6454SAndrew Thompson 	/*
220102ac6454SAndrew Thompson 	 * compute maximum number of TDs
220202ac6454SAndrew Thompson 	 */
220302ac6454SAndrew Thompson 	if (parm->methods == &atmegadci_device_ctrl_methods) {
220402ac6454SAndrew Thompson 
220502ac6454SAndrew Thompson 		ntd = xfer->nframes + 1 /* STATUS */ + 1	/* SYNC 1 */
220602ac6454SAndrew Thompson 		    + 1 /* SYNC 2 */ ;
220702ac6454SAndrew Thompson 
220802ac6454SAndrew Thompson 	} else if (parm->methods == &atmegadci_device_bulk_methods) {
220902ac6454SAndrew Thompson 
221002ac6454SAndrew Thompson 		ntd = xfer->nframes + 1 /* SYNC */ ;
221102ac6454SAndrew Thompson 
221202ac6454SAndrew Thompson 	} else if (parm->methods == &atmegadci_device_intr_methods) {
221302ac6454SAndrew Thompson 
221402ac6454SAndrew Thompson 		ntd = xfer->nframes + 1 /* SYNC */ ;
221502ac6454SAndrew Thompson 
221602ac6454SAndrew Thompson 	} else if (parm->methods == &atmegadci_device_isoc_fs_methods) {
221702ac6454SAndrew Thompson 
221802ac6454SAndrew Thompson 		ntd = xfer->nframes + 1 /* SYNC */ ;
221902ac6454SAndrew Thompson 
222002ac6454SAndrew Thompson 	} else {
222102ac6454SAndrew Thompson 
222202ac6454SAndrew Thompson 		ntd = 0;
222302ac6454SAndrew Thompson 	}
222402ac6454SAndrew Thompson 
222502ac6454SAndrew Thompson 	/*
222602ac6454SAndrew Thompson 	 * check if "usb2_transfer_setup_sub" set an error
222702ac6454SAndrew Thompson 	 */
222802ac6454SAndrew Thompson 	if (parm->err) {
222902ac6454SAndrew Thompson 		return;
223002ac6454SAndrew Thompson 	}
223102ac6454SAndrew Thompson 	/*
223202ac6454SAndrew Thompson 	 * allocate transfer descriptors
223302ac6454SAndrew Thompson 	 */
223402ac6454SAndrew Thompson 	last_obj = NULL;
223502ac6454SAndrew Thompson 
223602ac6454SAndrew Thompson 	/*
223702ac6454SAndrew Thompson 	 * get profile stuff
223802ac6454SAndrew Thompson 	 */
223902ac6454SAndrew Thompson 	if (ntd) {
224002ac6454SAndrew Thompson 
224102ac6454SAndrew Thompson 		ep_no = xfer->endpoint & UE_ADDR;
224202ac6454SAndrew Thompson 		atmegadci_get_hw_ep_profile(parm->udev, &pf, ep_no);
224302ac6454SAndrew Thompson 
224402ac6454SAndrew Thompson 		if (pf == NULL) {
224502ac6454SAndrew Thompson 			/* should not happen */
224602ac6454SAndrew Thompson 			parm->err = USB_ERR_INVAL;
224702ac6454SAndrew Thompson 			return;
224802ac6454SAndrew Thompson 		}
224902ac6454SAndrew Thompson 	} else {
225002ac6454SAndrew Thompson 		ep_no = 0;
225102ac6454SAndrew Thompson 		pf = NULL;
225202ac6454SAndrew Thompson 	}
225302ac6454SAndrew Thompson 
225402ac6454SAndrew Thompson 	/* align data */
225502ac6454SAndrew Thompson 	parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
225602ac6454SAndrew Thompson 
225702ac6454SAndrew Thompson 	for (n = 0; n != ntd; n++) {
225802ac6454SAndrew Thompson 
225902ac6454SAndrew Thompson 		struct atmegadci_td *td;
226002ac6454SAndrew Thompson 
226102ac6454SAndrew Thompson 		if (parm->buf) {
226202ac6454SAndrew Thompson 
226302ac6454SAndrew Thompson 			td = USB_ADD_BYTES(parm->buf, parm->size[0]);
226402ac6454SAndrew Thompson 
226502ac6454SAndrew Thompson 			/* init TD */
226602ac6454SAndrew Thompson 			td->max_packet_size = xfer->max_packet_size;
226702ac6454SAndrew Thompson 			td->ep_no = ep_no;
226802ac6454SAndrew Thompson 			if (pf->support_multi_buffer) {
226902ac6454SAndrew Thompson 				td->support_multi_buffer = 1;
227002ac6454SAndrew Thompson 			}
227102ac6454SAndrew Thompson 			td->obj_next = last_obj;
227202ac6454SAndrew Thompson 
227302ac6454SAndrew Thompson 			last_obj = td;
227402ac6454SAndrew Thompson 		}
227502ac6454SAndrew Thompson 		parm->size[0] += sizeof(*td);
227602ac6454SAndrew Thompson 	}
227702ac6454SAndrew Thompson 
227802ac6454SAndrew Thompson 	xfer->td_start[0] = last_obj;
227902ac6454SAndrew Thompson }
228002ac6454SAndrew Thompson 
228102ac6454SAndrew Thompson static void
228202ac6454SAndrew Thompson atmegadci_xfer_unsetup(struct usb2_xfer *xfer)
228302ac6454SAndrew Thompson {
228402ac6454SAndrew Thompson 	return;
228502ac6454SAndrew Thompson }
228602ac6454SAndrew Thompson 
228702ac6454SAndrew Thompson static void
228802ac6454SAndrew Thompson atmegadci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
228902ac6454SAndrew Thompson     struct usb2_pipe *pipe)
229002ac6454SAndrew Thompson {
229102ac6454SAndrew Thompson 	struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
229202ac6454SAndrew Thompson 
2293d59dbd0aSAndrew Thompson 	DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
229402ac6454SAndrew Thompson 	    pipe, udev->address,
229502ac6454SAndrew Thompson 	    edesc->bEndpointAddress, udev->flags.usb2_mode,
2296d59dbd0aSAndrew Thompson 	    sc->sc_rt_addr, udev->device_index);
229702ac6454SAndrew Thompson 
229802ac6454SAndrew Thompson 	if (udev->device_index == sc->sc_rt_addr) {
229902ac6454SAndrew Thompson 
230002ac6454SAndrew Thompson 		if (udev->flags.usb2_mode != USB_MODE_HOST) {
230102ac6454SAndrew Thompson 			/* not supported */
230202ac6454SAndrew Thompson 			return;
230302ac6454SAndrew Thompson 		}
230402ac6454SAndrew Thompson 		switch (edesc->bEndpointAddress) {
230502ac6454SAndrew Thompson 		case USB_CONTROL_ENDPOINT:
230602ac6454SAndrew Thompson 			pipe->methods = &atmegadci_root_ctrl_methods;
230702ac6454SAndrew Thompson 			break;
230802ac6454SAndrew Thompson 		case UE_DIR_IN | ATMEGA_INTR_ENDPT:
230902ac6454SAndrew Thompson 			pipe->methods = &atmegadci_root_intr_methods;
231002ac6454SAndrew Thompson 			break;
231102ac6454SAndrew Thompson 		default:
231202ac6454SAndrew Thompson 			/* do nothing */
231302ac6454SAndrew Thompson 			break;
231402ac6454SAndrew Thompson 		}
231502ac6454SAndrew Thompson 	} else {
231602ac6454SAndrew Thompson 
231702ac6454SAndrew Thompson 		if (udev->flags.usb2_mode != USB_MODE_DEVICE) {
231802ac6454SAndrew Thompson 			/* not supported */
231902ac6454SAndrew Thompson 			return;
232002ac6454SAndrew Thompson 		}
232102ac6454SAndrew Thompson 		if (udev->speed != USB_SPEED_FULL) {
232202ac6454SAndrew Thompson 			/* not supported */
232302ac6454SAndrew Thompson 			return;
232402ac6454SAndrew Thompson 		}
232502ac6454SAndrew Thompson 		switch (edesc->bmAttributes & UE_XFERTYPE) {
232602ac6454SAndrew Thompson 		case UE_CONTROL:
232702ac6454SAndrew Thompson 			pipe->methods = &atmegadci_device_ctrl_methods;
232802ac6454SAndrew Thompson 			break;
232902ac6454SAndrew Thompson 		case UE_INTERRUPT:
233002ac6454SAndrew Thompson 			pipe->methods = &atmegadci_device_intr_methods;
233102ac6454SAndrew Thompson 			break;
233202ac6454SAndrew Thompson 		case UE_ISOCHRONOUS:
233302ac6454SAndrew Thompson 			pipe->methods = &atmegadci_device_isoc_fs_methods;
233402ac6454SAndrew Thompson 			break;
233502ac6454SAndrew Thompson 		case UE_BULK:
233602ac6454SAndrew Thompson 			pipe->methods = &atmegadci_device_bulk_methods;
233702ac6454SAndrew Thompson 			break;
233802ac6454SAndrew Thompson 		default:
233902ac6454SAndrew Thompson 			/* do nothing */
234002ac6454SAndrew Thompson 			break;
234102ac6454SAndrew Thompson 		}
234202ac6454SAndrew Thompson 	}
234302ac6454SAndrew Thompson }
234402ac6454SAndrew Thompson 
234502ac6454SAndrew Thompson struct usb2_bus_methods atmegadci_bus_methods =
234602ac6454SAndrew Thompson {
234702ac6454SAndrew Thompson 	.pipe_init = &atmegadci_pipe_init,
234802ac6454SAndrew Thompson 	.xfer_setup = &atmegadci_xfer_setup,
234902ac6454SAndrew Thompson 	.xfer_unsetup = &atmegadci_xfer_unsetup,
235002ac6454SAndrew Thompson 	.get_hw_ep_profile = &atmegadci_get_hw_ep_profile,
235102ac6454SAndrew Thompson 	.set_stall = &atmegadci_set_stall,
235202ac6454SAndrew Thompson 	.clear_stall = &atmegadci_clear_stall,
235302ac6454SAndrew Thompson 	.roothub_exec = &atmegadci_root_ctrl_task,
235402ac6454SAndrew Thompson };
2355