xref: /freebsd/sys/dev/usb/usb_controller.h (revision 02ac6454880b59bbc5f3f74dffaffa90b30adc8b)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
2702ac6454SAndrew Thompson #ifndef _USB2_CONTROLLER_H_
2802ac6454SAndrew Thompson #define	_USB2_CONTROLLER_H_
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /* defines */
3102ac6454SAndrew Thompson 
3202ac6454SAndrew Thompson #define	USB_BUS_DMA_TAG_MAX 8
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson /* structure prototypes */
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson struct usb2_bus;
3702ac6454SAndrew Thompson struct usb2_page;
3802ac6454SAndrew Thompson struct usb2_pipe;
3902ac6454SAndrew Thompson struct usb2_page_cache;
4002ac6454SAndrew Thompson struct usb2_setup_params;
4102ac6454SAndrew Thompson struct usb2_hw_ep_profile;
4202ac6454SAndrew Thompson struct usb2_fs_isoc_schedule;
4302ac6454SAndrew Thompson struct usb2_config_descriptor;
4402ac6454SAndrew Thompson struct usb2_endpoint_descriptor;
4502ac6454SAndrew Thompson 
4602ac6454SAndrew Thompson /* typedefs */
4702ac6454SAndrew Thompson 
4802ac6454SAndrew Thompson typedef void (usb2_bus_mem_sub_cb_t)(struct usb2_bus *bus, struct usb2_page_cache *pc, struct usb2_page *pg, uint32_t size, uint32_t align);
4902ac6454SAndrew Thompson typedef void (usb2_bus_mem_cb_t)(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *scb);
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson /*
5202ac6454SAndrew Thompson  * The following structure is used to define all the USB BUS
5302ac6454SAndrew Thompson  * callbacks.
5402ac6454SAndrew Thompson  */
5502ac6454SAndrew Thompson struct usb2_bus_methods {
5602ac6454SAndrew Thompson 
5702ac6454SAndrew Thompson 	/* USB Device and Host mode - Mandatory */
5802ac6454SAndrew Thompson 
5902ac6454SAndrew Thompson 	void    (*pipe_init) (struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe);
6002ac6454SAndrew Thompson 	void    (*do_poll) (struct usb2_bus *);
6102ac6454SAndrew Thompson 	void    (*xfer_setup) (struct usb2_setup_params *parm);
6202ac6454SAndrew Thompson 	void    (*xfer_unsetup) (struct usb2_xfer *xfer);
6302ac6454SAndrew Thompson 	void    (*get_dma_delay) (struct usb2_bus *, uint32_t *pdelay);
6402ac6454SAndrew Thompson 	void    (*device_suspend) (struct usb2_device *udev);
6502ac6454SAndrew Thompson 	void    (*device_resume) (struct usb2_device *udev);
6602ac6454SAndrew Thompson 	void    (*set_hw_power) (struct usb2_bus *bus);
6702ac6454SAndrew Thompson 	/*
6802ac6454SAndrew Thompson 	 * The following flag is set if one or more control transfers are
6902ac6454SAndrew Thompson 	 * active:
7002ac6454SAndrew Thompson 	 */
7102ac6454SAndrew Thompson #define	USB_HW_POWER_CONTROL	0x01
7202ac6454SAndrew Thompson 	/*
7302ac6454SAndrew Thompson 	 * The following flag is set if one or more bulk transfers are
7402ac6454SAndrew Thompson 	 * active:
7502ac6454SAndrew Thompson 	 */
7602ac6454SAndrew Thompson #define	USB_HW_POWER_BULK	0x02
7702ac6454SAndrew Thompson 	/*
7802ac6454SAndrew Thompson 	 * The following flag is set if one or more interrupt transfers are
7902ac6454SAndrew Thompson 	 * active:
8002ac6454SAndrew Thompson 	 */
8102ac6454SAndrew Thompson #define	USB_HW_POWER_INTERRUPT	0x04
8202ac6454SAndrew Thompson 	/*
8302ac6454SAndrew Thompson 	 * The following flag is set if one or more isochronous transfers
8402ac6454SAndrew Thompson 	 * are active:
8502ac6454SAndrew Thompson 	 */
8602ac6454SAndrew Thompson #define	USB_HW_POWER_ISOC	0x08
8702ac6454SAndrew Thompson 	/*
8802ac6454SAndrew Thompson 	 * The following flag is set if one or more non-root-HUB devices
8902ac6454SAndrew Thompson 	 * are present on the given USB bus:
9002ac6454SAndrew Thompson 	 */
9102ac6454SAndrew Thompson #define	USB_HW_POWER_NON_ROOT_HUB 0x10
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson 	/* USB Device mode only - Mandatory */
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson 	void    (*get_hw_ep_profile) (struct usb2_device *udev, const struct usb2_hw_ep_profile **ppf, uint8_t ep_addr);
9602ac6454SAndrew Thompson 	void    (*set_stall) (struct usb2_device *udev, struct usb2_xfer *xfer, struct usb2_pipe *pipe);
9702ac6454SAndrew Thompson 	void    (*clear_stall) (struct usb2_device *udev, struct usb2_pipe *pipe);
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson 	/* USB Device and Host mode - Optional */
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson 	void	(*roothub_exec) (struct usb2_bus *);
10202ac6454SAndrew Thompson };
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson /*
10502ac6454SAndrew Thompson  * The following structure is used to define all the USB pipe
10602ac6454SAndrew Thompson  * callbacks.
10702ac6454SAndrew Thompson  */
10802ac6454SAndrew Thompson struct usb2_pipe_methods {
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson 	/* Mandatory USB Device and Host mode callbacks: */
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson 	void    (*open) (struct usb2_xfer *xfer);
11302ac6454SAndrew Thompson 	void    (*close) (struct usb2_xfer *xfer);
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson 	void    (*enter) (struct usb2_xfer *xfer);
11602ac6454SAndrew Thompson 	void    (*start) (struct usb2_xfer *xfer);
11702ac6454SAndrew Thompson 
11802ac6454SAndrew Thompson 	/* Optional */
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson 	void   *info;
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson 	/* Flags */
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson 	uint8_t	enter_is_cancelable:1;
12502ac6454SAndrew Thompson 	uint8_t	start_is_cancelable:1;
12602ac6454SAndrew Thompson };
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson /*
12902ac6454SAndrew Thompson  * The following structure keeps information about what a hardware USB
13002ac6454SAndrew Thompson  * endpoint supports.
13102ac6454SAndrew Thompson  */
13202ac6454SAndrew Thompson struct usb2_hw_ep_profile {
13302ac6454SAndrew Thompson 	uint16_t max_in_frame_size;	/* IN-token direction */
13402ac6454SAndrew Thompson 	uint16_t max_out_frame_size;	/* OUT-token direction */
13502ac6454SAndrew Thompson 	uint8_t	is_simplex:1;
13602ac6454SAndrew Thompson 	uint8_t	support_multi_buffer:1;
13702ac6454SAndrew Thompson 	uint8_t	support_bulk:1;
13802ac6454SAndrew Thompson 	uint8_t	support_control:1;
13902ac6454SAndrew Thompson 	uint8_t	support_interrupt:1;
14002ac6454SAndrew Thompson 	uint8_t	support_isochronous:1;
14102ac6454SAndrew Thompson 	uint8_t	support_in:1;		/* IN-token is supported */
14202ac6454SAndrew Thompson 	uint8_t	support_out:1;		/* OUT-token is supported */
14302ac6454SAndrew Thompson };
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson /*
14602ac6454SAndrew Thompson  * The following structure is used when trying to allocate hardware
14702ac6454SAndrew Thompson  * endpoints for an USB configuration in USB device side mode.
14802ac6454SAndrew Thompson  */
14902ac6454SAndrew Thompson struct usb2_hw_ep_scratch_sub {
15002ac6454SAndrew Thompson 	const struct usb2_hw_ep_profile *pf;
15102ac6454SAndrew Thompson 	uint16_t max_frame_size;
15202ac6454SAndrew Thompson 	uint8_t	hw_endpoint_out;
15302ac6454SAndrew Thompson 	uint8_t	hw_endpoint_in;
15402ac6454SAndrew Thompson 	uint8_t	needs_ep_type;
15502ac6454SAndrew Thompson 	uint8_t	needs_in:1;
15602ac6454SAndrew Thompson 	uint8_t	needs_out:1;
15702ac6454SAndrew Thompson };
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson /*
16002ac6454SAndrew Thompson  * The following structure is used when trying to allocate hardware
16102ac6454SAndrew Thompson  * endpoints for an USB configuration in USB device side mode.
16202ac6454SAndrew Thompson  */
16302ac6454SAndrew Thompson struct usb2_hw_ep_scratch {
16402ac6454SAndrew Thompson 	struct usb2_hw_ep_scratch_sub ep[USB_EP_MAX];
16502ac6454SAndrew Thompson 	struct usb2_hw_ep_scratch_sub *ep_max;
16602ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
16702ac6454SAndrew Thompson 	struct usb2_device *udev;
16802ac6454SAndrew Thompson 	struct usb2_bus_methods *methods;
16902ac6454SAndrew Thompson 	uint8_t	bmOutAlloc[(USB_EP_MAX + 15) / 16];
17002ac6454SAndrew Thompson 	uint8_t	bmInAlloc[(USB_EP_MAX + 15) / 16];
17102ac6454SAndrew Thompson };
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson /*
17402ac6454SAndrew Thompson  * The following structure is used when generating USB descriptors
17502ac6454SAndrew Thompson  * from USB templates.
17602ac6454SAndrew Thompson  */
17702ac6454SAndrew Thompson struct usb2_temp_setup {
17802ac6454SAndrew Thompson 	void   *buf;
17902ac6454SAndrew Thompson 	uint32_t size;
18002ac6454SAndrew Thompson 	uint8_t	usb2_speed;
18102ac6454SAndrew Thompson 	uint8_t	self_powered;
18202ac6454SAndrew Thompson 	uint8_t	bNumEndpoints;
18302ac6454SAndrew Thompson 	uint8_t	bInterfaceNumber;
18402ac6454SAndrew Thompson 	uint8_t	bAlternateSetting;
18502ac6454SAndrew Thompson 	uint8_t	bConfigurationValue;
18602ac6454SAndrew Thompson 	usb2_error_t err;
18702ac6454SAndrew Thompson };
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson /* prototypes */
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson void	usb2_bus_mem_flush_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb);
19202ac6454SAndrew Thompson uint8_t	usb2_bus_mem_alloc_all(struct usb2_bus *bus, bus_dma_tag_t dmat, usb2_bus_mem_cb_t *cb);
19302ac6454SAndrew Thompson void	usb2_bus_mem_free_all(struct usb2_bus *bus, usb2_bus_mem_cb_t *cb);
19402ac6454SAndrew Thompson void	usb2_bus_roothub_exec(struct usb2_bus *bus);
19502ac6454SAndrew Thompson uint16_t usb2_isoc_time_expand(struct usb2_bus *bus, uint16_t isoc_time_curr);
19602ac6454SAndrew Thompson uint16_t usb2_fs_isoc_schedule_isoc_time_expand(struct usb2_device *udev, struct usb2_fs_isoc_schedule **pp_start, struct usb2_fs_isoc_schedule **pp_end, uint16_t isoc_time);
19702ac6454SAndrew Thompson uint8_t	usb2_fs_isoc_schedule_alloc(struct usb2_fs_isoc_schedule *fss, uint8_t *pstart, uint16_t len);
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson #endif					/* _USB2_CONTROLLER_H_ */
200