usb.h (3643B)
1 /* USB Host Controller Driver */ 2 #ifndef USB_H 3 #define USB_H 4 5 #include <stdint.h> 6 7 /* USB Request Types */ 8 #define USB_REQ_GET_STATUS 0x00 9 #define USB_REQ_CLEAR_FEATURE 0x01 10 #define USB_REQ_SET_FEATURE 0x03 11 #define USB_REQ_SET_ADDRESS 0x05 12 #define USB_REQ_GET_DESCRIPTOR 0x06 13 #define USB_REQ_SET_DESCRIPTOR 0x07 14 #define USB_REQ_GET_CONFIGURATION 0x08 15 #define USB_REQ_SET_CONFIGURATION 0x09 16 #define USB_REQ_GET_INTERFACE 0x0A 17 #define USB_REQ_SET_INTERFACE 0x0B 18 19 /* USB Descriptor Types */ 20 #define USB_DESC_DEVICE 0x01 21 #define USB_DESC_CONFIGURATION 0x02 22 #define USB_DESC_STRING 0x03 23 #define USB_DESC_INTERFACE 0x04 24 #define USB_DESC_ENDPOINT 0x05 25 #define USB_DESC_HID 0x21 26 #define USB_DESC_REPORT 0x22 27 28 /* USB HID Request Types */ 29 #define USB_HID_REQ_GET_REPORT 0x01 30 #define USB_HID_REQ_GET_IDLE 0x02 31 #define USB_HID_REQ_GET_PROTOCOL 0x03 32 #define USB_HID_REQ_SET_REPORT 0x09 33 #define USB_HID_REQ_SET_IDLE 0x0A 34 #define USB_HID_REQ_SET_PROTOCOL 0x0B 35 36 /* USB Device Request */ 37 typedef struct { 38 uint8_t bmRequestType; 39 uint8_t bRequest; 40 uint16_t wValue; 41 uint16_t wIndex; 42 uint16_t wLength; 43 } __attribute__((packed)) usb_device_request_t; 44 45 /* USB Device Descriptor */ 46 typedef struct { 47 uint8_t bLength; 48 uint8_t bDescriptorType; 49 uint16_t bcdUSB; 50 uint8_t bDeviceClass; 51 uint8_t bDeviceSubClass; 52 uint8_t bDeviceProtocol; 53 uint8_t bMaxPacketSize0; 54 uint16_t idVendor; 55 uint16_t idProduct; 56 uint16_t bcdDevice; 57 uint8_t iManufacturer; 58 uint8_t iProduct; 59 uint8_t iSerialNumber; 60 uint8_t bNumConfigurations; 61 } __attribute__((packed)) usb_device_descriptor_t; 62 63 /* USB Configuration Descriptor */ 64 typedef struct { 65 uint8_t bLength; 66 uint8_t bDescriptorType; 67 uint16_t wTotalLength; 68 uint8_t bNumInterfaces; 69 uint8_t bConfigurationValue; 70 uint8_t iConfiguration; 71 uint8_t bmAttributes; 72 uint8_t bMaxPower; 73 } __attribute__((packed)) usb_config_descriptor_t; 74 75 /* USB Interface Descriptor */ 76 typedef struct { 77 uint8_t bLength; 78 uint8_t bDescriptorType; 79 uint8_t bInterfaceNumber; 80 uint8_t bAlternateSetting; 81 uint8_t bNumEndpoints; 82 uint8_t bInterfaceClass; 83 uint8_t bInterfaceSubClass; 84 uint8_t bInterfaceProtocol; 85 uint8_t iInterface; 86 } __attribute__((packed)) usb_interface_descriptor_t; 87 88 /* USB Endpoint Descriptor */ 89 typedef struct { 90 uint8_t bLength; 91 uint8_t bDescriptorType; 92 uint8_t bEndpointAddress; 93 uint8_t bmAttributes; 94 uint16_t wMaxPacketSize; 95 uint8_t bInterval; 96 } __attribute__((packed)) usb_endpoint_descriptor_t; 97 98 /* USB HID Descriptor */ 99 typedef struct { 100 uint8_t bLength; 101 uint8_t bDescriptorType; 102 uint16_t bcdHID; 103 uint8_t bCountryCode; 104 uint8_t bNumDescriptors; 105 uint8_t bDescriptorType2; 106 uint16_t wDescriptorLength; 107 } __attribute__((packed)) usb_hid_descriptor_t; 108 109 /* USB Mouse Boot Protocol Report */ 110 typedef struct { 111 uint8_t buttons; /* Bit 0: Left, Bit 1: Right, Bit 2: Middle */ 112 int8_t x; /* X movement */ 113 int8_t y; /* Y movement */ 114 } __attribute__((packed)) usb_mouse_report_t; 115 116 /* USB Mouse State */ 117 typedef struct { 118 int x; 119 int y; 120 uint8_t buttons; 121 uint8_t initialized; 122 } usb_mouse_state_t; 123 124 /* Function prototypes */ 125 int usb_init(void); 126 int usb_enumerate_devices(void); 127 int usb_mouse_init(void); 128 void usb_mouse_poll(void); 129 void usb_mouse_get_state(usb_mouse_state_t* state); 130 131 /* Lua bindings */ 132 #ifdef lua_h 133 int lua_usb_mouse_poll(lua_State* L); 134 int lua_usb_mouse_get_state(lua_State* L); 135 #endif 136 137 #endif /* USB_H */