11#pragma once
22
3+ // USB标准定义
4+ #define USB_DIR_OUT 0
5+ #define USB_DIR_IN 0x80
6+
7+ #define USB_TYPE_STANDARD (0x00 << 5)
8+ #define USB_TYPE_CLASS (0x01 << 5)
9+ #define USB_TYPE_VENDOR (0x02 << 5)
10+
11+ #define USB_RECIP_DEVICE 0x00
12+ #define USB_RECIP_INTERFACE 0x01
13+ #define USB_RECIP_ENDPOINT 0x02
14+
15+ // 描述符类型
16+ #define USB_DT_DEVICE 0x01
17+ #define USB_DT_CONFIG 0x02
18+ #define USB_DT_STRING 0x03
19+ #define USB_DT_INTERFACE 0x04
20+ #define USB_DT_ENDPOINT 0x05
21+
22+ // 端点类型
23+ #define USB_ENDPOINT_XFER_CONTROL 0
24+ #define USB_ENDPOINT_XFER_ISOC 1
25+ #define USB_ENDPOINT_XFER_BULK 2
26+ #define USB_ENDPOINT_XFER_INT 3
27+
28+ // USB标准请求
29+ #define USB_REQ_GET_STATUS 0x00
30+ #define USB_REQ_CLEAR_FEATURE 0x01
31+ #define USB_REQ_SET_FEATURE 0x03
32+ #define USB_REQ_SET_ADDRESS 0x05
33+ #define USB_REQ_GET_DESCRIPTOR 0x06
34+ #define USB_REQ_SET_DESCRIPTOR 0x07
35+ #define USB_REQ_GET_CONFIGURATION 0x08
36+ #define USB_REQ_SET_CONFIGURATION 0x09
37+ #define USB_REQ_GET_INTERFACE 0x0A
38+ #define USB_REQ_SET_INTERFACE 0x0B
39+
40+ // 设备速度
41+ #define USB_SPEED_UNKNOWN 0
42+ #define USB_SPEED_LOW 1
43+ #define USB_SPEED_FULL 2
44+ #define USB_SPEED_HIGH 3
45+ #define USB_SPEED_SUPER 4
46+
47+ // USB设备状态
48+ #define USB_STATE_NOTATTACHED 0
49+ #define USB_STATE_ATTACHED 1
50+ #define USB_STATE_POWERED 2
51+ #define USB_STATE_DEFAULT 3
52+ #define USB_STATE_ADDRESS 4
53+ #define USB_STATE_CONFIGURED 5
54+ #define USB_STATE_SUSPENDED 6
55+
356#include "cp_kernel.h"
457
558typedef errno_t (* netdev_send_t )(void * dev , void * data , uint32_t len );
@@ -64,6 +117,167 @@ typedef struct netdev {
64117 netdev_send_t send ;
65118 netdev_recv_t recv ;
66119} netdev_t ;
120+ // USB标准设备请求
121+ typedef struct {
122+ uint8_t bmRequestType ;
123+ uint8_t bRequest ;
124+ uint16_t wValue ;
125+ uint16_t wIndex ;
126+ uint16_t wLength ;
127+ } __attribute__((packed )) usb_device_request_t ;
128+
129+ // USB设备描述符
130+ typedef struct {
131+ uint8_t bLength ;
132+ uint8_t bDescriptorType ;
133+ uint16_t bcdUSB ;
134+ uint8_t bDeviceClass ;
135+ uint8_t bDeviceSubClass ;
136+ uint8_t bDeviceProtocol ;
137+ uint8_t bMaxPacketSize0 ;
138+ uint16_t idVendor ;
139+ uint16_t idProduct ;
140+ uint16_t bcdDevice ;
141+ uint8_t iManufacturer ;
142+ uint8_t iProduct ;
143+ uint8_t iSerialNumber ;
144+ uint8_t bNumConfigurations ;
145+ } __attribute__((packed )) usb_device_descriptor_t ;
146+
147+ // USB配置描述符
148+ typedef struct {
149+ uint8_t bLength ;
150+ uint8_t bDescriptorType ;
151+ uint16_t wTotalLength ;
152+ uint8_t bNumInterfaces ;
153+ uint8_t bConfigurationValue ;
154+ uint8_t iConfiguration ;
155+ uint8_t bmAttributes ;
156+ uint8_t bMaxPower ;
157+ } __attribute__((packed )) usb_config_descriptor_t ;
158+
159+ // USB接口描述符
160+ typedef struct {
161+ uint8_t bLength ;
162+ uint8_t bDescriptorType ;
163+ uint8_t bInterfaceNumber ;
164+ uint8_t bAlternateSetting ;
165+ uint8_t bNumEndpoints ;
166+ uint8_t bInterfaceClass ;
167+ uint8_t bInterfaceSubClass ;
168+ uint8_t bInterfaceProtocol ;
169+ uint8_t iInterface ;
170+ } __attribute__((packed )) usb_interface_descriptor_t ;
171+
172+ // USB端点描述符
173+ typedef struct {
174+ uint8_t bLength ;
175+ uint8_t bDescriptorType ;
176+ uint8_t bEndpointAddress ;
177+ uint8_t bmAttributes ;
178+ uint16_t wMaxPacketSize ;
179+ uint8_t bInterval ;
180+ } __attribute__((packed )) usb_endpoint_descriptor_t ;
181+
182+ // 前向声明
183+ typedef struct usb_device usb_device_t ;
184+ typedef struct usb_endpoint usb_endpoint_t ;
185+ typedef struct usb_transfer usb_transfer_t ;
186+ typedef struct usb_hcd usb_hcd_t ;
187+
188+ // USB传输完成回调
189+ typedef void (* usb_transfer_callback_t )(usb_transfer_t * transfer );
190+
191+ // USB传输结构
192+ struct usb_transfer {
193+ usb_device_t * device ;
194+ usb_endpoint_t * endpoint ;
195+
196+ void * buffer ;
197+ uint32_t length ;
198+ uint32_t actual_length ;
199+
200+ int status ;
201+ void * hcd_private ; // HCD私有数据
202+
203+ usb_transfer_callback_t callback ;
204+ void * user_data ;
205+
206+ struct usb_transfer * next ;
207+ };
208+
209+ // USB端点结构
210+ struct usb_endpoint {
211+ uint8_t address ;
212+ uint8_t attributes ;
213+ uint16_t max_packet_size ;
214+ uint8_t interval ;
215+
216+ usb_device_t * device ;
217+ void * hcd_private ;
218+ };
219+
220+ // USB设备结构
221+ struct usb_device {
222+ uint8_t port ;
223+
224+ uint8_t address ;
225+ uint8_t speed ;
226+ uint8_t state ;
227+
228+ usb_device_descriptor_t descriptor ;
229+ usb_config_descriptor_t * config_descriptor ;
230+
231+ uint8_t class ;
232+ uint8_t subclass ;
233+
234+ usb_endpoint_t endpoints [32 ]; // 最多16 IN + 16 OUT
235+
236+ usb_hcd_t * hcd ;
237+ void * hcd_private ; // HCD私有数据
238+
239+ void * private_data ;
240+
241+ struct usb_device * next ;
242+ };
243+
244+ // USB主机控制器驱动操作
245+ typedef struct {
246+ int (* init )(usb_hcd_t * hcd );
247+ int (* shutdown )(usb_hcd_t * hcd );
248+
249+ int (* reset_port )(usb_hcd_t * hcd , uint8_t port );
250+ int (* enable_slot )(usb_hcd_t * hcd , usb_device_t * device );
251+ int (* disable_slot )(usb_hcd_t * hcd , usb_device_t * device );
252+
253+ int (* address_device )(usb_hcd_t * hcd , usb_device_t * device , uint8_t address );
254+ int (* configure_endpoint )(usb_hcd_t * hcd , usb_endpoint_t * endpoint );
255+
256+ int (* control_transfer )(usb_hcd_t * hcd , usb_transfer_t * transfer , usb_device_request_t * setup );
257+ int (* bulk_transfer )(usb_hcd_t * hcd , usb_transfer_t * transfer );
258+ int (* interrupt_transfer )(usb_hcd_t * hcd , usb_transfer_t * transfer );
259+ } usb_hcd_ops_t ;
260+
261+ // USB主机控制器驱动
262+ struct usb_hcd {
263+ const char * name ;
264+ usb_hcd_ops_t * ops ;
265+
266+ void * regs ; // 寄存器基址
267+ void * private_data ;
268+
269+ usb_device_t * devices ;
270+ uint8_t num_ports ;
271+
272+ struct usb_hcd * next ;
273+ };
274+
275+ typedef struct usb_driver {
276+ uint8_t class ;
277+ uint8_t subclass ;
278+ int (* probe )(usb_device_t * usbdev );
279+ int (* remove )(usb_device_t * usbdev );
280+ } usb_driver_t ;
67281
68282void regist_netdev (void * desc , uint8_t * mac , uint32_t mtu , netdev_send_t send , netdev_recv_t recv );
69283errno_t netdev_send (netdev_t * dev , void * data , uint32_t len );
@@ -76,3 +290,14 @@ size_t device_write(size_t lba, size_t number, const void *buffer, int drive);
76290
77291pci_device_t * pci_find_vid_did (uint16_t vendor_id , uint16_t device_id );
78292pci_device_t * pci_find_class (uint32_t class_code );
293+
294+ void keyboard_tmp_writer (const uint8_t * data );
295+ uint8_t keyboard_scancode (uint8_t scancode , uint8_t scancode_1 , uint8_t scancode_2 );
296+
297+ void register_usb_driver (usb_driver_t * driver );
298+ int usb_control_transfer (usb_device_t * device , usb_device_request_t * setup , void * data ,
299+ uint32_t length , usb_transfer_callback_t callback , void * user_data );
300+ int usb_interrupt_transfer (usb_device_t * device , uint8_t endpoint , void * data , uint32_t length ,
301+ usb_transfer_callback_t callback , void * user_data );
302+ usb_hcd_t * usb_register_hcd (const char * name , usb_hcd_ops_t * ops , void * regs , void * data );
303+ void usb_unregister_hcd (usb_hcd_t * hcd );
0 commit comments