blob: f566190c12ca14ad866b38010091f3e78570a208 [file] [log] [blame]
Travis Geiselbrecht2c691e82008-09-04 02:41:01 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef __DEV_USBC_H
24#define __DEV_USBC_H
25
Travis Geiselbrecht89fcb142008-10-10 03:18:10 -070026#include <sys/types.h>
27#include <debug.h>
28#include <hw/usb.h>
29
Travis Geiselbrecht2c691e82008-09-04 02:41:01 -070030void usbc_init(void);
31
Travis Geiselbrecht89fcb142008-10-10 03:18:10 -070032typedef uint ep_t;
33
34typedef enum {
35 IN = 0,
36 OUT
37} ep_dir_t;
38
39typedef enum {
40 CB_RESET,
41 CB_SUSPEND,
42 CB_RESUME,
43 CB_DISCONNECT,
44 CB_ONLINE,
45 CB_OFFLINE,
46 CB_SETUP_MSG,
47
48 /* endpoint transfer stuff */
49 CB_EP_RXCOMPLETE,
50 CB_EP_TXCOMPLETE,
51 CB_EP_TRANSFER_CANCELLED,
52} usbc_callback_op_t;
53
54typedef struct {
55 void *buf;
56 size_t buflen;
57 uint bufpos;
58 int result;
59 void *extra; // extra pointer to store whatever you want
60} usbc_transfer;
61
62enum {
63 USB_TRANSFER_RESULT_OK = 0,
64 USB_TRANSFER_RESULT_ERR = -1,
65 USB_TRANSFER_RESULT_CANCELLED = -2,
66};
67
68typedef int (*ep_callback)(ep_t endpoint, usbc_callback_op_t op, usbc_transfer *transfer);
69
70void usbc_setup_endpoint(ep_t ep, ep_dir_t dir, bool active, ep_callback callback, uint width, uint blocksize);
71int usbc_queue_rx(ep_t ep, usbc_transfer *transfer);
72int usbc_queue_tx(ep_t ep, usbc_transfer *transfer);
73
74/* setup arg is valid during CB_SETUP_MSG */
75union usb_callback_args {
76 const struct usb_setup *setup;
77};
78
79typedef int (*usb_callback)(usbc_callback_op_t op, const union usb_callback_args *args);
80
81int usbc_set_callback(usb_callback);
82int usbc_set_active(bool active);
83
84/* called back from within a callback to handle setup responses */
85void usbc_ep0_ack(void);
86void usbc_ep0_stall(void);
87void usbc_ep0_send(const void *buf, size_t len, size_t maxlen);
88void usbc_ep0_recv(void *buf, size_t len, ep_callback);
89
90bool usbc_is_highspeed(void);
91
92static inline void usbc_dump_transfer(const usbc_transfer *t)
93{
94 printf("usb transfer %p: buf %p, buflen %zd, bufpos %u, result %d\n", t, t->buf, t->buflen, t->bufpos, t->result);
95}
96
Travis Geiselbrecht2c691e82008-09-04 02:41:01 -070097#endif
98