xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/usbhost/drvBus.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 
79 //#undef DEBUG
80 
81 #include <MsCommon.h>
82 #include  "include/drvPorts.h"
83 #include  "include/drvConfig.h"
84 #include  "include/drvCompiler.h"
85 
86 #include "drvBase.h"
87 
88 
device_bind_driver(struct device_s * dev)89 void device_bind_driver(struct device_s * dev)
90 {
91 	pr_debug("bound device '%s' to driver '%s'\n",
92 				dev->bus_id,dev->driver->name);
93 	list_add_tail(&dev->driver_list,&dev->driver->devices);
94 }
95 
96 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
bus_match(struct device_s * dev,struct device_driver * drv)97 static int bus_match(struct device_s * dev, struct device_driver * drv)
98 {
99 	int error = -ENODEV;
100 
101 	// Call BUS provided match function to match device ID witch specific comparing method
102 	if (dev->bus->match(dev,drv)) {
103 		dev->driver = drv;
104 		// Match device ==> call driver's probe function
105 		if (drv->probe) {
106 			if ((error = drv->probe(dev))) {
107 				dev->driver = NULL;
108 				return error;
109 			}
110 		}
111 		device_bind_driver(dev);
112 		error = 0;
113 	}
114 
115 	return error;
116 }
117 
118 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
device_attach(struct device_s * dev)119 static int device_attach(struct device_s * dev)
120 {
121 	struct bus_type * bus = dev->bus;
122 	struct list_head * entry;
123 	struct device_driver  * drv;
124 	struct list_head *_mtr;
125 
126 	if (dev->driver) {
127 		device_bind_driver(dev);
128 		return 1;
129 	}
130 
131 	if (bus->match) {
132 		list_for_each(entry,&bus->drivers_list) {
133 			_mtr = entry ;
134 			drv = (struct device_driver *)((char *)_mtr - (char *)offsetof(struct device_driver, bus_list) );
135 			if (!bus_match(dev,drv))
136 				return 1;
137 		}
138 	}
139 
140   return 0;
141 }
142 
143 
144 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
driver_attach(struct device_driver * drv)145 void driver_attach(struct device_driver * drv)
146 {
147 	struct bus_type * bus = drv->bus;
148 	struct list_head * entry;
149 	struct device_s * dev;
150 	const struct list_head *_mtr;
151 
152 	if (!bus->match)
153 		return;
154 
155 	list_for_each(entry,&bus->devices_list) {
156 		_mtr = entry ;
157 		dev = (struct device_s *)((char *)_mtr - (char *)offsetof(struct device_s, bus_list) );
158 		if (!dev->driver) {
159 			bus_match(dev,drv);
160 		}
161 	}
162 }
163 
164 
165 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
device_release_driver(struct device_s * dev)166 void device_release_driver(struct device_s * dev)
167 {
168 	struct device_driver * drv = dev->driver;
169 
170 	if (drv) {
171 		list_del_init(&dev->driver_list);
172 		if (drv->remove)
173 			drv->remove(dev);
174 		dev->driver = NULL;
175 	}
176 }
177 
178 
179 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
driver_detach(struct device_driver * drv)180 static void driver_detach(struct device_driver * drv)
181 {
182 	struct list_head * entry, * next;
183 	struct device_s *dev;
184 	struct list_head *_mptr;
185 
186 	list_for_each_safe(entry,next,&drv->devices) {
187 		// struct device * dev = container_of(entry,struct device,driver_list);
188 		_mptr = entry;
189 		dev = (struct device_s *)( (char *)_mptr - (char *)offsetof(struct device_s, driver_list) );
190 		device_release_driver(dev);
191 	}
192 
193 }
194 
195 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
bus_add_device(struct device_s * dev)196 int bus_add_device(struct device_s * dev)
197 {
198 	int error = 0;
199 
200 	if (dev->bus) {
201 		pr_debug("bus %s: add device %s\n",dev->bus->name,dev->bus_id);
202 		list_add_tail(&dev->bus_list,&dev->bus->devices_list);
203 		device_attach(dev);
204 	}
205 	return error;
206 }
207 
208 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
bus_remove_device(struct device_s * dev)209 void bus_remove_device(struct device_s * dev)
210 {
211 	if (dev->bus) {
212 		pr_debug("bus %s: remove device %s\n",dev->bus->name,dev->bus_id);
213 		device_release_driver(dev);
214 		list_del_init(&dev->bus_list);
215 	}
216 }
217 
218 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
bus_add_driver(struct device_driver * drv)219 int bus_add_driver(struct device_driver * drv)
220 {
221 	struct bus_type * bus = drv->bus;
222 	int error = 0;
223 
224 	if (bus) {
225 		pr_debug("bus %s: add driver %s\n",bus->name,drv->name);
226 
227 		// Add driver to bus driver lsit
228 		list_add_tail(&drv->bus_list,&bus->drivers_list);
229 		driver_attach(drv);
230 	}
231 	return error;
232 }
233 
234 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
bus_remove_driver(struct device_driver * drv)235 void bus_remove_driver(struct device_driver * drv)
236 {
237 	if (drv->bus) {
238 		pr_debug("bus %s: remove driver %s\n",drv->bus->name,drv->name);
239 		driver_detach(drv);
240 		list_del_init(&drv->bus_list);
241 	}
242 }
243 
244 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
bus_register(struct bus_type * bus)245 int bus_register(struct bus_type * bus)
246 {
247 	pr_debug("bus type '%s' registered\n",bus->name);
248 	return 0;
249 }
250 
251 
252 
bus_unregister(struct bus_type * bus)253 void bus_unregister(struct bus_type * bus)
254 {
255 	pr_debug("bus %s: unregistering\n",bus->name);
256 }
257 
258