1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _U_BUS_H_
6 #define _U_BUS_H_
7 
8 #include "aos/list.h"
9 
10 #include <drivers/spinlock.h>
11 #include <drivers/char/u_driver.h>
12 
13 /**
14  * struct u_bus_private - bus private information
15  * should only be accessed by char_core
16  * @dev_list: device list belongs to current bus
17  * @drv_list: driver list belongs to current bus
18  * @bus: pointer to the bus struct this u_bus_private_t belongs to
19  *
20  * */
21 typedef struct u_bus_private {
22 	spinlock_t lock;
23 	struct dlist_s dev_list;
24 	struct dlist_s drv_list;
25 	struct dlist_s bus_node;
26 	struct u_bus *bus;
27 } u_bus_private_t;
28 
29 /**
30  * struct u_bus - bus struct, virtual or hardware bus
31  *
32  * @name: bus name
33  * @drv_list: driver list belongs to this bus
34  * @match:
35  * @probe:
36  * @remove:
37  * @shutdown:
38  * @online:
39  * @offline:
40  * @suspend:
41  * @resume:
42  * @on_ping:
43  * @pm:
44  *
45  * */
46 typedef struct u_bus {
47 	char *name;
48 	struct u_bus_private *p;
49 
50 	// for legacy drivers
51 	int (*init)(struct u_device *_dev);
52 	int (*deinit)(struct u_device *_dev);
53 	int (*pm)(struct u_device *dev, u_pm_ops_t state);
54 	int (*match)(struct u_device *dev, struct u_driver *drv);
55 
56 	// for linux-like drivers
57 	int (*probe)(struct u_device *dev);
58 	int (*remove)(struct u_device *dev);
59 	void (*shutdown)(struct u_device *dev);
60 	int (*online)(struct u_device *dev);
61 	int (*offline)(struct u_device *dev);
62 	int (*suspend)(struct u_device *dev, u_pm_ops_t state);
63 	int (*resume)(struct u_device *dev);
64 
65 	// TODO: when receive IPC ping message call on_ping, shall move this to u_driver_private_t?
66 	int (*on_ping)(struct u_device *dev);
67 
68 } u_bus_t;
69 
70 /**
71  * init global bus list
72  * */
73 int u_bus_init(void);
74 
75 /**
76  * register bus to system
77  * @drv: the bus to register
78  *
79  * */
80 int u_bus_register(struct u_bus *bus);
81 
82 /**
83  * unregister bus from system
84  * @drv: the bus to be removed
85  *
86  * */
87 int u_bus_unregister(struct u_bus *bus);
88 
89 
90 /**
91 * connect dev into target bus(dev->bus)
92 * low level operations, driver is not aware of this operation
93 *
94 * @param dev - pointer to target device
95 * @return always returns 0
96 */
97 int u_bus_add_device(struct u_device *dev);
98 
99 /**
100  * enumerate driver list and try to init the device with the drivers
101  *
102  * @param dev - pointer to the deviice to be initialized
103  *
104  * @return 0 if device initialization success; negative if device initialization fails
105  */
106 int u_bus_try_init_device(struct u_device *dev);
107 
108 /**
109  * add a driver into bus's driver list
110  *
111  * @param drv - the driver to be added
112  * @return always return 0
113  */
114 int u_bus_add_driver(struct u_driver *drv);
115 
116 /**
117  * get the bus with specified name
118  * @name: the name of target bus
119  *
120  * */
121 u_bus_t *u_bus_get_by_name(char *name);
122 
123 /**
124  * disconnect device with its driver and delete it from target bus(dev->bus)
125  *
126  * @param dev - pointer to target device
127  * @return always return 0
128  */
129 int u_bus_del_device(struct u_device *dev);
130 
131 /**
132  * delete a driver from bus's driver list
133  *
134  * @param drv - pointer to the driver to be deleted
135  * @return always return 0
136  */
137 int u_bus_del_driver(struct u_driver *drv);
138 
139 /**
140  * attach a driver into bus's driver list,
141  * try to init free devices(not drived by any driver) if device is matchd
142  *
143  * @drv: the driver to be added
144  * @return always return 0
145  */
146 int u_bus_attach_driver(struct u_driver *drv);
147 
148 /**
149  * deattch a driver into bus's driver list
150  *
151  * @param drv - pointer to target driver to be deatched
152  * @return always return 0
153  */
154 int u_bus_detach_driver(struct u_driver *drv);
155 
156 #if 0
157 struct u_device *u_device_find_by_devid(dev_t devid);
158 #endif
159 
160 #endif //_U_BUS_H_
161