1 /**
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _U_DEVICE_H_
6 #define _U_DEVICE_H_
7 
8 #include <stdio.h>
9 
10 #include "aos/list.h"
11 #include "aos/vfs.h"
12 #include <drivers/spinlock.h>
13 #include <drivers/ddkc_log.h>
14 
15 #define LOG_TAG "UDEV"
16 #include <ulog/ulog.h>
17 
18 struct u_device;
19 struct u_device_private;
20 
21 
22 /* valid device id: [0, 9999), this should be very enough */
23 #define MAX_DEV_ID_DIGS 4
24 #define MAX_DEV_ID_NUM 9999
25 
26 /**
27  * struct u_device_private - device private information
28  * should only be accessed by char_core
29  * @bus_node: node in bus list
30  * @driver_node: node in driver list
31  *
32  * */
33 typedef struct u_device_private {
34 	spinlock_t lock;
35 	struct dlist_s bus_node;
36 	struct dlist_s drv_node;
37 	struct dlist_s parent_node; /* node in parent's child list with head of child_head*/
38 	struct dlist_s child_head;  /* child list's head */
39 	struct u_device *dev;
40 	char *name;
41 } u_device_private_t;
42 
43 /**
44  * struct u_device - device structure
45  * @parent: device's parent
46  * @drv: the driver by which this device is driven
47  * @bus: the bus to which this device is on
48  * @p: driver private information, device driver and bus list information
49  * */
50 typedef struct u_device {
51 	struct u_device* parent;
52 	struct u_driver *drv;
53 	struct u_bus *bus;
54 	char *name;
55 	unsigned int id;
56 	void *user_data;
57 	void *driver_data;
58 	unsigned int dev_id;
59 	u_device_private_t *p;
60 } u_device_t;
61 
62 /*
63  * all device node must be registered with u_device_node_create
64  * */
65 typedef struct u_dev_node {
66 	//TODO: should add lock for device node list operation
67 	struct dlist_s node;
68 	struct u_dev_node *parent;
69 	int dev_id;
70 	//TODO: shall dev be added for device node?
71 	struct u_device *dev;
72 	char *path_name;
73 	void *drv_data;
74 	struct file_operations *fops;
75 	struct file_ops legacy_fops;
76 	char name[0];
77 } u_dev_node_t;
78 
79 
80 typedef struct u_device_info_ipc {
81 	unsigned int id;
82 	char name[32];
83 } u_device_info_ipc_t;
84 
85 /**
86  * add device into bus device list
87  * conditions to check in u_device_add:
88  * 1. dev is not NULL
89  * 2. dev->bus is assigned correctly
90  * 3. dev->p is allocated and initialized
91  *
92  * @param dev - pointer to target device
93  * @return 0 for success; netative for failure
94  */
95 int u_device_add(struct u_device *dev);
96 
97 /**
98  * initialize u_device's private info
99  *
100  * @param dev - pointer to target device
101  * @return 0 for success; netative for failure
102  */
103 int u_device_initialize(struct u_device *dev);
104 
105 /**
106  * register a device into system
107  *
108  * @param dev - pointer to target device
109  * @return 0 for success; netative for failure
110  */
111 int u_device_register(struct u_device *dev);
112 
113 /**
114  * check whether a device is registered or not
115  * @param dev - pointer to the device to be checked
116  *
117  * @return 0 for success; netative for failure
118  */
119 int u_device_is_registered(struct u_device *dev);
120 
121 /**
122  * unregister device from system
123  * @param dev - pointer to target device to be unregistered
124  * @return 0 for success; negative for failure
125  */
126 int u_device_unregister(struct u_device *dev);
127 
128 /**
129  * delete device from system
130  *
131  * @param dev - pointer to device to be deleted
132  * @return 0 for success; negative for failure
133  */
134 int u_device_del(struct u_device *dev);
135 
136 /**
137  * dump device's information, name, id, parent, bus, driver, drv_data included
138  * @param dev - pointer to target device
139  * @return 0 for success; netative for failure
140  */
141 int u_device_dump(struct u_device *dev);
142 
143 int u_device_root_node_init(void);
144 struct u_dev_node * u_device_node_create(struct u_dev_node *parent, unsigned int dev_id, void *drv_data, char *name);
145 #if 0
146 struct u_dev_node * u_device_node_find_by_devid(dev_t devid);
147 #endif
148 struct u_dev_node * u_device_node_find_by_pathname(char *pathname);
149 struct u_dev_node * u_device_node_find_by_dev(struct u_device *u_dev);
150 struct u_dev_node * u_device_node_find_by_nodename(char *node_name);
151 int u_device_node_delete(unsigned int dev_id);
152 
153 #endif //_U_DEVICE_H_
154