Skip to content

Commit ef3e614

Browse files
增加 usb 驱动, 更新内核模块接口, device新增卸载设备接口.
内核模块新增任务子系统接口. Co-authored-by: suhuajun-github <115517663+suhuajun-github@users.noreply.github.com>
1 parent d58127a commit ef3e614

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4404
-1644
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ jobs:
8888
xmake build e1000
8989
xmake build extfs
9090
xmake build nvme
91+
xmake build hid
92+
xmake build xhci
9193
xmake build iso
9294
xmake build img
9395

assets/initramfs.img

3 KB
Binary file not shown.

assets/limine.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ wallpaper: boot():/background.jpg
1111
module_path: boot():/extfs.km
1212
module_path: boot():/e1000.km
1313
module_path: boot():/nvme.km
14+
module_path: boot():/hid.km
15+
module_path: boot():/xhci.km
1416
module_path: boot():/initramfs.img
15-
kernel_cmdline: console=default root=/dev/sata0p1 mem=default
17+
kernel_cmdline: console=tty root=/dev/sata0p1 mem=default
1618
kaslr: no
1719

1820
/CoolPotOS-CP_Kernel-i386

assets/rootfs.tar.xz

2.19 MB
Binary file not shown.

module/all_include/cp_kernel.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ typedef size_t usize;
1010
typedef ssize_t isize;
1111
typedef int wchar_t;
1212

13+
#define ABS(x) ((x) > 0 ? (x) : -(x))
14+
#define MAX(x, y) ((x > y) ? (x) : (y))
15+
#define MIN(x, y) ((x < y) ? (x) : (y))
16+
1317
typedef int pid_t;
1418
typedef int errno_t;
1519

@@ -32,3 +36,11 @@ int strcmp(const char *s1, const char *s2);
3236
int strncmp(const char *s1, const char *s2, size_t n);
3337
int sprintf(char *buf, char const *fmt, ...);
3438
int snprintf(char *buf, int count, const char *fmt, ...);
39+
40+
static inline void arch_enable_interrupt() {
41+
__asm__ volatile("sti");
42+
}
43+
44+
static inline void arch_disable_interrupt() {
45+
__asm__ volatile("cli");
46+
}

module/all_include/driver_subsystem.h

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
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

558
typedef 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

68282
void regist_netdev(void *desc, uint8_t *mac, uint32_t mtu, netdev_send_t send, netdev_recv_t recv);
69283
errno_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

77291
pci_device_t *pci_find_vid_did(uint16_t vendor_id, uint16_t device_id);
78292
pci_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);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#define MAX_NICE 19
4+
#define MIN_NICE -20
5+
#define NICE_WIDTH (MAX_NICE - MIN_NICE + 1)
6+
7+
#define MAX_RT_PRIO 100
8+
#define MAX_DL_PRIO 0
9+
10+
#define MAX_PRIO (MAX_RT_PRIO + NICE_WIDTH)
11+
#define DEFAULT_PRIO (MAX_RT_PRIO + NICE_WIDTH / 2)
12+
13+
#define NICE_TO_PRIO(nice) ((nice) + DEFAULT_PRIO)
14+
#define PRIO_TO_NICE(prio) ((prio) - DEFAULT_PRIO)
15+
16+
#define IDLE_PRIORITY NICE_TO_PRIO(20)
17+
#define NORMAL_PRIORITY NICE_TO_PRIO(0)
18+
#define KTHREAD_PRIORITY NICE_TO_PRIO(-5)
19+
20+
#include "cp_kernel.h"
21+
22+
pid_t task_create(const char *name, void (*entry)(uint64_t), uint64_t arg, uint64_t priority);
23+
uint64_t task_exit(int64_t code);
24+
void scheduler_yield();

module/extfs/ext.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ extern char *vfs_get_fullpath(vfs_node_t);
1515
int ext_mount(const char *src, vfs_node_t node) {
1616
spin_lock(rwlock);
1717

18-
if (is_virtual_fs(src)) return -1;
18+
if (is_virtual_fs(src)) {
19+
spin_unlock(rwlock);
20+
return -1;
21+
}
1922

2023
ext4_device_register(vfs_dev_get(), src);
2124

0 commit comments

Comments
 (0)