rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 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_USB_H |
| 24 | #define __DEV_USB_H |
| 25 | |
| 26 | #include <sys/types.h> |
| 27 | #include <compiler.h> |
| 28 | |
| 29 | /* top level initialization for usb client, abstracts away the interfaces */ |
| 30 | typedef struct { |
| 31 | void *desc; |
| 32 | size_t len; |
| 33 | uint flags; |
| 34 | } usb_descriptor __ALIGNED(2); |
| 35 | |
| 36 | #define USB_DESC_FLAG_STATIC (0x1) |
| 37 | |
| 38 | #define USB_DESC_STATIC(x) { .desc = (void *)(x), .len = sizeof(x), .flags = USB_DESC_FLAG_STATIC } |
| 39 | |
| 40 | typedef struct { |
| 41 | usb_descriptor string; |
| 42 | uint8_t id; |
| 43 | } usb_string; |
| 44 | |
| 45 | /* complete usb config struct, passed in to usb_setup() */ |
| 46 | typedef struct { |
| 47 | struct usb_descriptor_speed { |
| 48 | usb_descriptor device; |
| 49 | usb_descriptor device_qual; |
| 50 | usb_descriptor config; |
| 51 | } lowspeed, highspeed; |
| 52 | usb_descriptor langid; |
| 53 | } usb_config; |
| 54 | |
| 55 | /* external code needs to set up the usb stack via the following calls */ |
| 56 | status_t usb_setup(usb_config *config); |
| 57 | |
| 58 | /* apped new interface descriptors to the existing config if desired */ |
| 59 | status_t usb_append_interface_highspeed(const uint8_t *int_descr, size_t len); |
| 60 | status_t usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len); |
| 61 | |
| 62 | status_t usb_add_string(const char *string, uint8_t id); |
| 63 | |
| 64 | status_t usb_start(void); |
| 65 | status_t usb_stop(void); |
| 66 | |
| 67 | /* callbacks from usbc and usb layers */ |
| 68 | typedef enum { |
| 69 | USB_CB_RESET, |
| 70 | USB_CB_SUSPEND, |
| 71 | USB_CB_RESUME, |
| 72 | USB_CB_DISCONNECT, |
| 73 | USB_CB_ONLINE, |
| 74 | USB_CB_OFFLINE, |
| 75 | USB_CB_SETUP_MSG, |
| 76 | } usb_callback_op_t; |
| 77 | |
| 78 | /* setup arg is valid during CB_SETUP_MSG */ |
| 79 | union usb_callback_args { |
| 80 | const struct usb_setup *setup; |
| 81 | }; |
| 82 | |
| 83 | typedef status_t (*usb_callback_t)(void *cookie, usb_callback_op_t op, const union usb_callback_args *args); |
| 84 | |
| 85 | /* callback api the usbc driver uses */ |
| 86 | status_t usbc_callback(usb_callback_op_t op, const union usb_callback_args *args); |
| 87 | |
| 88 | /* callback api that anyone can register for */ |
| 89 | status_t usb_register_callback(usb_callback_t, void *cookie); |
| 90 | |
| 91 | #endif |
| 92 | |