1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "usb_ddk_pnp_loader.h"
10 #include <unistd.h>
11 
12 #include "device_resource_if.h"
13 #include "hcs_tree_if.h"
14 #include "hdf_base.h"
15 #include "hdf_cstring.h"
16 #include "hdf_device_node.h"
17 #include "hdf_device_object.h"
18 #include "hdf_log.h"
19 #include "hdf_dlist.h"
20 #include "hdf_sbuf.h"
21 #include "osal_file.h"
22 #include "osal_mem.h"
23 #include "osal_time.h"
24 #include "securec.h"
25 #include "usb_pnp_manager.h"
26 #ifndef __LITEOS__
27 #include "usb_wrapper.h"
28 #endif
29 
30 #define HDF_LOG_TAG USB_DDK_PNP_LOADER
31 
32 #define USB_DDK_PNP_CLASS_VENDOR_SPEC 0xFF
33 
34 static struct DListHead g_usbPnpDeviceTableListHead;
35 static struct UsbPnpMatchIdTable **g_usbPnpMatchIdTable = NULL;
36 
UsbDdkPnpLoaderBufCreate(const char * moduleName,const char * serviceName,const char * deviceMatchAttr,struct UsbPnpNotifyServiceInfo serviceInfo)37 static struct HdfSBuf *UsbDdkPnpLoaderBufCreate(const char *moduleName, const char *serviceName,
38     const char *deviceMatchAttr, struct UsbPnpNotifyServiceInfo serviceInfo)
39 {
40     struct HdfSBuf *pnpData = NULL;
41 
42     pnpData = HdfSbufObtainDefaultSize();
43     if (pnpData == NULL) {
44         HDF_LOGE("%s: HdfSbufTypedObtain pnpData fail", __func__);
45         return NULL;
46     }
47 
48     if (!UsbPnpManagerWriteModuleName(pnpData, moduleName)) {
49         HDF_LOGE("%s: write moduleName failed!", __func__);
50         goto OUT;
51     }
52 
53     if (!HdfSbufWriteString(pnpData, serviceName)) {
54         HDF_LOGE("%s: write service name failed!", __func__);
55         goto OUT;
56     }
57 
58     if (!HdfSbufWriteString(pnpData, deviceMatchAttr)) {
59         HDF_LOGE("%s: write deviceMatchAttr failed!", __func__);
60         goto OUT;
61     }
62 
63     if (!HdfSbufWriteBuffer(pnpData, (const void *)(&serviceInfo), serviceInfo.length)) {
64         HDF_LOGE("%s: write privateData failed!", __func__);
65         goto OUT;
66     }
67 
68     return pnpData;
69 
70 OUT:
71     HdfSbufRecycle(pnpData);
72 
73     return NULL;
74 }
75 
UsbDdkPnpLoaderMatchDevice(const struct UsbPnpNotifyMatchInfoTable * dev,const struct UsbPnpMatchIdTable * id)76 static bool UsbDdkPnpLoaderMatchDevice(
77     const struct UsbPnpNotifyMatchInfoTable *dev, const struct UsbPnpMatchIdTable *id)
78 {
79     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_VENDOR) && (id->vendorId != dev->deviceInfo.vendorId)) {
80         return false;
81     }
82 
83     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_PRODUCT) && (id->productId != dev->deviceInfo.productId)) {
84         return false;
85     }
86 
87     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_LOW) && (id->bcdDeviceLow > dev->deviceInfo.bcdDeviceLow)) {
88         return false;
89     }
90 
91     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_HIGH) && (id->bcdDeviceHigh < dev->deviceInfo.bcdDeviceHigh)) {
92         return false;
93     }
94 
95     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_CLASS) && (id->deviceClass != dev->deviceInfo.deviceClass)) {
96         return false;
97     }
98 
99     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_SUBCLASS) && (id->deviceSubClass != dev->deviceInfo.deviceSubClass)) {
100         return false;
101     }
102 
103     if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_PROTOCOL) && (id->deviceProtocol != dev->deviceInfo.deviceProtocol)) {
104         return false;
105     }
106 
107     return true;
108 }
109 
110 // Need optimize this device object array later, should bind device object to usb device
111 #define REGISTER_DEV_MAX 16
112 static struct HdfDeviceObject *g_resistedDevice[REGISTER_DEV_MAX] = {0};
SaveRegistedDevice(struct HdfDeviceObject * dev)113 static int32_t SaveRegistedDevice(struct HdfDeviceObject *dev)
114 {
115     for (size_t i = 0; i < REGISTER_DEV_MAX; i++) {
116         if (g_resistedDevice[i] == NULL) {
117             g_resistedDevice[i] = dev;
118             return HDF_SUCCESS;
119         }
120     }
121 
122     return HDF_FAILURE;
123 }
124 
GetRegistedDevice(const char * serviceName)125 static struct HdfDeviceObject *GetRegistedDevice(const char *serviceName)
126 {
127     struct HdfDeviceObject *dev = NULL;
128     for (size_t i = 0; i < REGISTER_DEV_MAX; i++) {
129         if (g_resistedDevice[i] == NULL) {
130             continue;
131         }
132         if (!strcmp(HdfDeviceGetServiceName(g_resistedDevice[i]), serviceName)) {
133             dev = g_resistedDevice[i];
134             g_resistedDevice[i] = NULL;
135             break;
136         }
137     }
138 
139     return dev;
140 }
141 
UsbPnpManagerRegisterDevice(struct UsbPnpManagerDeviceInfo * managerInfo)142 static int32_t UsbPnpManagerRegisterDevice(struct UsbPnpManagerDeviceInfo *managerInfo)
143 {
144     int32_t ret = HDF_FAILURE;
145     struct HdfDeviceObject *devObj = HdfDeviceObjectAlloc(managerInfo->usbPnpManager, managerInfo->moduleName);
146     if (devObj == NULL) {
147         HDF_LOGE("%s: failed to alloc device object", __func__);
148         return HDF_DEV_ERR_NO_DEVICE;
149     }
150 
151     struct HdfDeviceNode *devNode = CONTAINER_OF(devObj, struct HdfDeviceNode, deviceObject);
152     if (devNode == NULL) {
153         HDF_LOGE("%s: failed to alloc device node object", __func__);
154         HdfDeviceObjectRelease(devObj);
155         return HDF_DEV_ERR_NO_DEVICE;
156     }
157     devNode->policy = SERVICE_POLICY_CAPACITY;
158     devNode->servName = HdfStringCopy(managerInfo->serviceName);
159     if (devNode->servName == NULL) {
160         HdfDeviceObjectRelease(devObj);
161         return HDF_DEV_ERR_NO_MEMORY;
162     }
163 
164     devObj->priv = (void *)managerInfo->privateData;
165     ret = HdfDeviceObjectRegister(devObj);
166     if (ret != HDF_SUCCESS) {
167         HDF_LOGE("%s: failed to register device %s", __func__, managerInfo->serviceName);
168         HdfDeviceObjectRelease(devObj);
169         return ret;
170     }
171     // need optimize this code to remove SaveRegistedDevice function later
172     return SaveRegistedDevice(devObj);
173 }
174 
UsbPnpManagerUnregisterDevice(struct UsbPnpManagerDeviceInfo * managerInfo)175 static int32_t UsbPnpManagerUnregisterDevice(struct UsbPnpManagerDeviceInfo *managerInfo)
176 {
177     // need optimize this code to remove GetRegistedDevice function later
178     struct HdfDeviceObject *devObj = GetRegistedDevice(managerInfo->serviceName);
179     if (devObj == NULL) {
180         return HDF_DEV_ERR_NO_DEVICE;
181     }
182     HdfDeviceObjectRelease(devObj);
183     return HDF_SUCCESS;
184 }
185 
UsbPnpManagerRegisterOrUnregisterDevice(struct UsbPnpManagerDeviceInfo * managerInfo)186 int32_t UsbPnpManagerRegisterOrUnregisterDevice(struct UsbPnpManagerDeviceInfo *managerInfo)
187 {
188     if (managerInfo == NULL) {
189         return HDF_ERR_INVALID_PARAM;
190     }
191 
192     return managerInfo->isReg ? UsbPnpManagerRegisterDevice(managerInfo) : UsbPnpManagerUnregisterDevice(managerInfo);
193 }
194 
UsbDdkPnpLoaderMatchHandle(const struct UsbPnpNotifyMatchInfoTable * dev,int8_t index,struct UsbPnpMatchIdTable * id,bool flag)195 static void UsbDdkPnpLoaderMatchHandle(
196     const struct UsbPnpNotifyMatchInfoTable *dev, int8_t index, struct UsbPnpMatchIdTable *id, bool flag)
197 {
198     if ((!id->pnpMatchFlag) && flag) {
199         if (!(id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_CLASS)) {
200             id->interfaceClass[id->interfaceClassLength++] = dev->interfaceInfo[index].interfaceClass;
201         }
202         if (!(id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_SUBCLASS)) {
203             id->interfaceSubClass[id->interfaceSubClassLength++] = dev->interfaceInfo[index].interfaceSubClass;
204         }
205         if (!(id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_PROTOCOL)) {
206             id->interfaceProtocol[id->interfaceProtocolLength++] = dev->interfaceInfo[index].interfaceProtocol;
207         }
208         if (!(id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER)) {
209             id->interfaceNumber[id->interfaceLength++] = dev->interfaceInfo[index].interfaceNumber;
210         }
211     }
212 }
213 
UsbDdkPnpLoaderMatchFlagFirst(struct UsbPnpMatchIdTable * id)214 static bool UsbDdkPnpLoaderMatchFlagFirst(struct UsbPnpMatchIdTable *id)
215 {
216     int32_t i;
217     bool ret = true;
218 
219     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_CLASS) {
220         for (i = 0; i < id->interfaceClassLength; i++) {
221             if (((id->interfaceClassMask >> (uint32_t)i) & 0x01) == 0) {
222                 break;
223             }
224         }
225         if (i < id->interfaceClassLength) {
226             ret = false;
227             goto OUT;
228         }
229     }
230 
231     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_SUBCLASS) {
232         for (i = 0; i < id->interfaceSubClassLength; i++) {
233             if (((id->interfaceSubClassMask >> (uint32_t)i) & 0x01) == 0) {
234                 break;
235             }
236         }
237         if (i < id->interfaceSubClassLength) {
238             ret = false;
239             goto OUT;
240         }
241     }
242 
243     ret = true;
244 
245 OUT:
246     return ret;
247 }
248 
UsbDdkPnpLoaderMatchFlag(const struct UsbPnpNotifyMatchInfoTable * dev,int8_t index,struct UsbPnpMatchIdTable * id,bool flag)249 static bool UsbDdkPnpLoaderMatchFlag(
250     const struct UsbPnpNotifyMatchInfoTable *dev, int8_t index, struct UsbPnpMatchIdTable *id, bool flag)
251 {
252     int32_t i;
253     bool ret = true;
254 
255     if (!UsbDdkPnpLoaderMatchFlagFirst(id)) {
256         ret = false;
257         goto OUT;
258     }
259 
260     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_PROTOCOL) {
261         for (i = 0; i < id->interfaceProtocolLength; i++) {
262             if (((id->interfaceProtocolMask >> (uint32_t)i) & 0x01) == 0) {
263                 break;
264             }
265         }
266         if (i < id->interfaceProtocolLength) {
267             ret = false;
268             goto OUT;
269         }
270     }
271 
272     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER) {
273         for (i = 0; i < id->interfaceLength; i++) {
274             if (((id->interfaceMask >> (uint32_t)i) & 0x01) == 0) {
275                 break;
276             }
277         }
278         if (i < id->interfaceLength) {
279             ret = false;
280             goto OUT;
281         }
282     }
283 
284     ret = true;
285 
286 OUT:
287     UsbDdkPnpLoaderMatchHandle(dev, index, id, flag);
288 
289     return ret;
290 }
291 
UsbDdkPnpLoaderMatchInterface(const struct UsbPnpNotifyMatchInfoTable * dev,int8_t index,struct UsbPnpMatchIdTable * id)292 static bool UsbDdkPnpLoaderMatchInterface(
293     const struct UsbPnpNotifyMatchInfoTable *dev, int8_t index, struct UsbPnpMatchIdTable *id)
294 {
295     int32_t i;
296     bool maskFlag = true;
297 
298     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_CLASS) {
299         for (i = 0; i < id->interfaceClassLength; i++) {
300             if (id->interfaceClass[i] == dev->interfaceInfo[index].interfaceClass) {
301                 id->interfaceClassMask |= (1 << (uint32_t)i);
302                 break;
303             }
304         }
305 
306         if (i >= id->interfaceClassLength) {
307             maskFlag = false;
308         }
309     }
310 
311     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_SUBCLASS) {
312         for (i = 0; i < id->interfaceSubClassLength; i++) {
313             if (id->interfaceSubClass[i] == dev->interfaceInfo[index].interfaceSubClass) {
314                 id->interfaceSubClassMask |= (1 << (uint32_t)i);
315                 break;
316             }
317         }
318 
319         if (i >= id->interfaceSubClassLength) {
320             maskFlag = false;
321         }
322     }
323 
324     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_PROTOCOL) {
325         for (i = 0; i < id->interfaceProtocolLength; i++) {
326             if (id->interfaceProtocol[i] == dev->interfaceInfo[index].interfaceProtocol) {
327                 id->interfaceProtocolMask |= (1 << (uint32_t)i);
328                 break;
329             }
330         }
331 
332         if (i >= id->interfaceProtocolLength) {
333             maskFlag = false;
334         }
335     }
336 
337     if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER) {
338         for (i = 0; i < id->interfaceLength; i++) {
339             if (id->interfaceNumber[i] == dev->interfaceInfo[index].interfaceNumber) {
340                 id->interfaceMask |= (1 << (uint32_t)i);
341                 break;
342             }
343         }
344 
345         if (i >= id->interfaceLength) {
346             maskFlag = false;
347         }
348     }
349 
350     return maskFlag;
351 }
352 
UsbDdkPnpLoaderMatchOneIdIntf(const struct UsbPnpNotifyMatchInfoTable * dev,int8_t index,struct UsbPnpMatchIdTable * id)353 static bool UsbDdkPnpLoaderMatchOneIdIntf(
354     const struct UsbPnpNotifyMatchInfoTable *dev, int8_t index, struct UsbPnpMatchIdTable *id)
355 {
356     bool maskFlag = true;
357 
358     if (dev->deviceInfo.deviceClass == USB_DDK_PNP_CLASS_VENDOR_SPEC &&
359         !(id->matchFlag & USB_PNP_NOTIFY_MATCH_VENDOR) &&
360         (id->matchFlag &
361             (USB_PNP_NOTIFY_MATCH_INT_CLASS | USB_PNP_NOTIFY_MATCH_INT_SUBCLASS | USB_PNP_NOTIFY_MATCH_INT_PROTOCOL |
362                 USB_PNP_NOTIFY_MATCH_INT_NUMBER))) {
363         return false;
364     }
365 
366     maskFlag = UsbDdkPnpLoaderMatchInterface(dev, index, id);
367     if (!UsbDdkPnpLoaderMatchFlag(dev, index, id, maskFlag)) {
368         return false;
369     }
370 
371     if (!id->pnpMatchFlag) {
372         id->pnpMatchFlag = true;
373     } else {
374         return false;
375     }
376 
377     return true;
378 }
379 
UsbDdkPnpLoaderParseIdInfClass(const struct DeviceResourceNode * node,const struct DeviceResourceIface * devResIface,struct UsbPnpMatchIdTable * table)380 static int32_t UsbDdkPnpLoaderParseIdInfClass(const struct DeviceResourceNode *node,
381     const struct DeviceResourceIface *devResIface, struct UsbPnpMatchIdTable *table)
382 {
383     table->interfaceClassMask = 0;
384     table->interfaceClassLength = devResIface->GetElemNum(node, "interfaceClass");
385     if (table->interfaceClassLength <= 0) {
386         HDF_LOGE("%s: read interfaceClass length=%d fail!", __func__, table->interfaceClassLength);
387         return HDF_FAILURE;
388     }
389     if (devResIface->GetUint8Array(node, "interfaceClass", table->interfaceClass, table->interfaceClassLength, 0) !=
390         HDF_SUCCESS) {
391         HDF_LOGE("%s: read interfaceClass fail!", __func__);
392         return HDF_FAILURE;
393     }
394     if (!(table->matchFlag & USB_PNP_NOTIFY_MATCH_INT_CLASS)) {
395         table->interfaceClassLength = 0;
396     }
397 
398     table->interfaceSubClassMask = 0;
399     table->interfaceSubClassLength = devResIface->GetElemNum(node, "interfaceSubClass");
400     if (table->interfaceSubClassLength <= 0) {
401         HDF_LOGE("%s: read interfaceSubClass length=%d fail!", __func__, table->interfaceSubClassLength);
402         return HDF_FAILURE;
403     }
404     if (devResIface->GetUint8Array(
405         node, "interfaceSubClass", table->interfaceSubClass, table->interfaceSubClassLength, 0) != HDF_SUCCESS) {
406         HDF_LOGE("%s: read interfaceSubClass fail!", __func__);
407         return HDF_FAILURE;
408     }
409     if (!(table->matchFlag & USB_PNP_NOTIFY_MATCH_INT_SUBCLASS)) {
410         table->interfaceSubClassLength = 0;
411     }
412 
413     return HDF_SUCCESS;
414 }
415 
UsbDdkPnpLoaderParseIdInferface(const struct DeviceResourceNode * node,const struct DeviceResourceIface * devResIface,struct UsbPnpMatchIdTable * table)416 static int32_t UsbDdkPnpLoaderParseIdInferface(const struct DeviceResourceNode *node,
417     const struct DeviceResourceIface *devResIface, struct UsbPnpMatchIdTable *table)
418 {
419     if (UsbDdkPnpLoaderParseIdInfClass(node, devResIface, table) != HDF_SUCCESS) {
420         return HDF_FAILURE;
421     }
422 
423     table->interfaceProtocolMask = 0;
424     table->interfaceProtocolLength = devResIface->GetElemNum(node, "interfaceProtocol");
425     if (table->interfaceProtocolLength <= 0) {
426         HDF_LOGE("%s: read interfaceProtocol length=%d fail!", __func__, table->interfaceProtocolLength);
427         return HDF_FAILURE;
428     }
429     if (devResIface->GetUint8Array(
430         node, "interfaceProtocol", table->interfaceProtocol, table->interfaceProtocolLength, 0) != HDF_SUCCESS) {
431         HDF_LOGE("%s: read interfaceProtocol fail!", __func__);
432         return HDF_FAILURE;
433     }
434     if (!(table->matchFlag & USB_PNP_NOTIFY_MATCH_INT_PROTOCOL)) {
435         table->interfaceProtocolLength = 0;
436     }
437 
438     table->interfaceMask = 0;
439     table->interfaceLength = devResIface->GetElemNum(node, "interfaceNumber");
440     if (table->interfaceLength <= 0) {
441         HDF_LOGE("%s: read interfaceNumber length=%d fail!", __func__, table->interfaceLength);
442         return HDF_FAILURE;
443     }
444     if (devResIface->GetUint8Array(node, "interfaceNumber", table->interfaceNumber, table->interfaceLength, 0) !=
445         HDF_SUCCESS) {
446         HDF_LOGE("%s: read interfaceNumber fail!", __func__);
447         return HDF_FAILURE;
448     }
449     if (!(table->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER)) {
450         table->interfaceLength = 0;
451     }
452 
453     return HDF_SUCCESS;
454 }
455 
UsbDdkPnpLoaderParseIdDevice(const struct DeviceResourceNode * node,const struct DeviceResourceIface * devResIface,struct UsbPnpMatchIdTable * table)456 static int32_t UsbDdkPnpLoaderParseIdDevice(const struct DeviceResourceNode *node,
457     const struct DeviceResourceIface *devResIface, struct UsbPnpMatchIdTable *table)
458 {
459     if (devResIface->GetUint16(node, "vendorId", &table->vendorId, 0) != HDF_SUCCESS) {
460         HDF_LOGE("%s: read vendorId fail!", __func__);
461         return HDF_FAILURE;
462     }
463 
464     if (devResIface->GetUint16(node, "productId", &table->productId, 0) != HDF_SUCCESS) {
465         HDF_LOGE("%s: read productId fail!", __func__);
466         return HDF_FAILURE;
467     }
468 
469     if (devResIface->GetUint16(node, "bcdDeviceLow", &table->bcdDeviceLow, 0) != HDF_SUCCESS) {
470         HDF_LOGE("%s: read bcdDeviceLow fail!", __func__);
471         return HDF_FAILURE;
472     }
473 
474     if (devResIface->GetUint16(node, "bcdDeviceHigh", &table->bcdDeviceHigh, 0) != HDF_SUCCESS) {
475         HDF_LOGE("%s: read bcdDeviceHigh fail!", __func__);
476         return HDF_FAILURE;
477     }
478 
479     if (devResIface->GetUint8(node, "deviceClass", &table->deviceClass, 0) != HDF_SUCCESS) {
480         HDF_LOGE("%s: read deviceClass fail!", __func__);
481         return HDF_FAILURE;
482     }
483 
484     if (devResIface->GetUint8(node, "deviceSubClass", &table->deviceSubClass, 0) != HDF_SUCCESS) {
485         HDF_LOGE("%s: read deviceSubClass fail!", __func__);
486         return HDF_FAILURE;
487     }
488 
489     if (devResIface->GetUint8(node, "deviceProtocol", &table->deviceProtocol, 0) != HDF_SUCCESS) {
490         HDF_LOGE("%s: read deviceProtocol fail!", __func__);
491         return HDF_FAILURE;
492     }
493 
494     return HDF_SUCCESS;
495 }
496 
UsbDdkPnpLoaderParseIdTable(const struct DeviceResourceNode * node,const struct DeviceResourceIface * devResIface,struct UsbPnpMatchIdTable * table)497 static int32_t UsbDdkPnpLoaderParseIdTable(const struct DeviceResourceNode *node,
498     const struct DeviceResourceIface *devResIface, struct UsbPnpMatchIdTable *table)
499 {
500     if (node == NULL || table == NULL || devResIface == NULL) {
501         HDF_LOGE("%s: node or table or devResIface is NULL!", __func__);
502         return HDF_FAILURE;
503     }
504 
505     if (devResIface->GetString(node, "moduleName", &table->moduleName, "") != HDF_SUCCESS) {
506         HDF_LOGE("%s: read moduleName fail!", __func__);
507         return HDF_FAILURE;
508     }
509 
510     if (devResIface->GetString(node, "serviceName", &table->serviceName, "") != HDF_SUCCESS) {
511         HDF_LOGE("%s: read serviceName fail!", __func__);
512         return HDF_FAILURE;
513     }
514 
515     if (devResIface->GetString(node, "deviceMatchAttr", &table->deviceMatchAttr, "") != HDF_SUCCESS) {
516         HDF_LOGE("%s: read deviceMatchAttr fail!", __func__);
517         return HDF_FAILURE;
518     }
519 
520     if (devResIface->GetUint8(node, "length", &table->length, 0) != HDF_SUCCESS) {
521         HDF_LOGE("%s: read length fail!", __func__);
522         return HDF_FAILURE;
523     }
524 
525     if (devResIface->GetUint16(node, "matchFlag", &table->matchFlag, 0) != HDF_SUCCESS) {
526         HDF_LOGE("%s: read matchFlag fail!", __func__);
527         return HDF_FAILURE;
528     }
529 
530     if (UsbDdkPnpLoaderParseIdDevice(node, devResIface, table) != HDF_SUCCESS) {
531         return HDF_FAILURE;
532     }
533 
534     return UsbDdkPnpLoaderParseIdInferface(node, devResIface, table);
535 }
536 
UsbDdkPnpLoaderParseTableList(const struct DeviceResourceNode * node,int32_t idTabCount,const struct DeviceResourceIface * devResIface)537 static struct UsbPnpMatchIdTable **UsbDdkPnpLoaderParseTableList(
538     const struct DeviceResourceNode *node, int32_t idTabCount, const struct DeviceResourceIface *devResIface)
539 {
540     int32_t ret;
541     int32_t count;
542     const char *idTableName = NULL;
543     struct UsbPnpMatchIdTable **idTable = NULL;
544     const struct DeviceResourceNode *tableNode = NULL;
545 
546     idTable = (struct UsbPnpMatchIdTable **)OsalMemCalloc((idTabCount + 1) * sizeof(struct UsbPnpMatchIdTable *));
547     if (idTable == NULL) {
548         HDF_LOGE("%s: OsalMemCalloc failure!", __func__);
549         return NULL;
550     }
551     idTable[idTabCount] = NULL;
552     for (count = 0; count < idTabCount; count++) {
553         idTable[count] = (struct UsbPnpMatchIdTable *)OsalMemCalloc(sizeof(struct UsbPnpMatchIdTable));
554         if (idTable[count] == NULL) {
555             HDF_LOGE("%s: OsalMemCalloc failure!", __func__);
556             goto OUT;
557         }
558         ret = devResIface->GetStringArrayElem(node, "idTableList", count, &idTableName, NULL);
559         if (ret != HDF_SUCCESS) {
560             goto OUT;
561         }
562         tableNode = devResIface->GetChildNode(node, idTableName);
563         if (tableNode == NULL) {
564             HDF_LOGE("%s: tableNode is NULL!", __func__);
565             goto OUT;
566         }
567         if (UsbDdkPnpLoaderParseIdTable(tableNode, devResIface, idTable[count]) != HDF_SUCCESS) {
568             HDF_LOGE("%s: UsbDdkPnpLoaderParseIdTable failure!", __func__);
569             goto OUT;
570         }
571     }
572 
573     return idTable;
574 
575 OUT:
576     while ((--count) >= 0) {
577         OsalMemFree(idTable[count]);
578     }
579     OsalMemFree(idTable);
580 
581     return NULL;
582 }
583 
UsbDdkPnpLoaderParseTable(const struct DeviceResourceNode * node)584 static struct UsbPnpMatchIdTable **UsbDdkPnpLoaderParseTable(const struct DeviceResourceNode *node)
585 {
586     struct DeviceResourceIface *devResIface = NULL;
587     int32_t idTabCount;
588 
589     if (node == NULL) {
590         HDF_LOGE("%s: node is NULL!", __func__);
591         return NULL;
592     }
593 
594     devResIface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
595     if (devResIface == NULL) {
596         HDF_LOGE("%s: devResIface is NULL!", __func__);
597         return NULL;
598     }
599     idTabCount = devResIface->GetElemNum(node, "idTableList");
600     if (idTabCount <= 0) {
601         HDF_LOGE("%s: idTableList not found!", __func__);
602         return NULL;
603     }
604 
605     return UsbDdkPnpLoaderParseTableList(node, idTabCount, devResIface);
606 }
607 
UsbDdkPnpLoaderParseDeviceId(const struct DeviceResourceNode * node)608 static struct UsbPnpMatchIdTable **UsbDdkPnpLoaderParseDeviceId(const struct DeviceResourceNode *node)
609 {
610     const char *deviceIdName = NULL;
611     struct DeviceResourceIface *devResIface = NULL;
612     const struct DeviceResourceNode *deviceIdNode = NULL;
613 
614     if (node == NULL) {
615         HDF_LOGE("%s: node is NULL!", __func__);
616         return NULL;
617     }
618 
619     devResIface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
620     if (devResIface == NULL) {
621         HDF_LOGE("%s: devResIface is NULL!", __func__);
622         return NULL;
623     }
624 
625     if (devResIface->GetString(node, "usb_pnp_device_id", &deviceIdName, NULL) != HDF_SUCCESS) {
626         HDF_LOGE("%s: get usb_pnp_device_id name failure!", __func__);
627         return NULL;
628     }
629 
630     deviceIdNode = devResIface->GetChildNode(node, deviceIdName);
631     if (deviceIdNode == NULL) {
632         HDF_LOGE("%s: deviceIdNode is NULL!", __func__);
633         return NULL;
634     }
635 
636     return UsbDdkPnpLoaderParseTable(deviceIdNode);
637 }
638 
UsbDdkPnpLoaderPnpMatch(void)639 static struct UsbPnpMatchIdTable **UsbDdkPnpLoaderPnpMatch(void)
640 {
641     struct DeviceResourceIface *devResInstance = NULL;
642     const struct DeviceResourceNode *rootNode = NULL;
643     const struct DeviceResourceNode *usbPnpNode = NULL;
644 
645     devResInstance = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
646     if (devResInstance == NULL) {
647         HDF_LOGE("%s: devResInstance is NULL!", __func__);
648         return NULL;
649     }
650 
651     rootNode = devResInstance->GetRootNode();
652     if (rootNode == NULL) {
653         HDF_LOGE("%s: devResNode is NULL!", __func__);
654         return NULL;
655     }
656 
657     usbPnpNode = devResInstance->GetNodeByMatchAttr(rootNode, "usb_pnp_match");
658     if (usbPnpNode == NULL) {
659         HDF_LOGE("%s: usbPnpNode is NULL!", __func__);
660         return NULL;
661     }
662 
663     return UsbDdkPnpLoaderParseDeviceId(usbPnpNode);
664 }
665 
UsbDdkPnpLoaderDispatchPnpDevice(struct HdfDeviceObject * usbPnpManagerDevice,struct HdfSBuf * data,bool isReg)666 static int32_t UsbDdkPnpLoaderDispatchPnpDevice(
667     struct HdfDeviceObject *usbPnpManagerDevice, struct HdfSBuf *data, bool isReg)
668 {
669     uint32_t infoSize = 0;
670     struct UsbPnpNotifyServiceInfo *privateData = NULL;
671     struct UsbPnpManagerDeviceInfo managerInfo;
672 
673     const char *moduleName = HdfSbufReadString(data);
674     if (moduleName == NULL) {
675         return HDF_ERR_INVALID_PARAM;
676     }
677     const char *serviceName = HdfSbufReadString(data);
678     if (serviceName == NULL) {
679         return HDF_ERR_INVALID_PARAM;
680     }
681 
682     const char *deviceMatchAttr = HdfSbufReadString(data);
683     if (deviceMatchAttr == NULL) {
684         return HDF_ERR_INVALID_PARAM;
685     }
686 
687     if (!HdfSbufReadBuffer(data, (const void **)(&privateData), &infoSize)) {
688         HDF_LOGW("%s: HdfSbufReadBuffer privateData error!", __func__);
689         privateData = NULL;
690     }
691 
692     managerInfo.usbPnpManager = usbPnpManagerDevice;
693     managerInfo.moduleName = moduleName;
694     managerInfo.serviceName = serviceName;
695     managerInfo.deviceMatchAttr = deviceMatchAttr;
696     managerInfo.privateData = privateData;
697     managerInfo.isReg = isReg;
698 
699     return UsbPnpManagerRegisterOrUnregisterDevice(&managerInfo);
700 }
701 
UsbDdkPnpLoaderDeviceListAdd(const struct UsbPnpNotifyMatchInfoTable * info,const struct UsbPnpMatchIdTable * idTable)702 static int32_t UsbDdkPnpLoaderDeviceListAdd(
703     const struct UsbPnpNotifyMatchInfoTable *info, const struct UsbPnpMatchIdTable *idTable)
704 {
705     int32_t ret;
706     unsigned char *ptr = NULL;
707     struct UsbPnpDeviceListTable *deviceTableListTemp = NULL;
708 
709     ptr = OsalMemAlloc(sizeof(struct UsbPnpDeviceListTable));
710     if (ptr == NULL) {
711         ret = HDF_ERR_MALLOC_FAIL;
712         HDF_LOGE("%s:%d OsalMemAlloc faile, ret=%d ", __func__, __LINE__, ret);
713     } else {
714         deviceTableListTemp = (struct UsbPnpDeviceListTable *)ptr;
715 
716         DListHeadInit(&deviceTableListTemp->list);
717         deviceTableListTemp->moduleName = idTable->moduleName;
718         deviceTableListTemp->serviceName = idTable->serviceName;
719         deviceTableListTemp->deviceMatchAttr = idTable->deviceMatchAttr;
720         deviceTableListTemp->status = USB_PNP_ADD_STATUS;
721         deviceTableListTemp->usbDevAddr = info->usbDevAddr;
722         deviceTableListTemp->devNum = info->devNum;
723         deviceTableListTemp->busNum = info->busNum;
724         deviceTableListTemp->interfaceLength = idTable->interfaceLength;
725         ret = memcpy_s(deviceTableListTemp->interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, idTable->interfaceNumber,
726             USB_PNP_INFO_MAX_INTERFACES);
727         if (ret != HDF_SUCCESS) {
728             HDF_LOGE("%{public}s:%{public}d memcpy_s failed", __func__, __LINE__);
729             return ret;
730         }
731 
732         DListInsertTail(&deviceTableListTemp->list, &g_usbPnpDeviceTableListHead);
733 
734         ret = HDF_SUCCESS;
735     }
736 
737     return ret;
738 }
739 
UsbDdkPnpLoaderAddInterface(const struct UsbPnpNotifyMatchInfoTable * info,const struct UsbPnpMatchIdTable * idTable)740 static struct UsbPnpDeviceListTable *UsbDdkPnpLoaderAddInterface(
741     const struct UsbPnpNotifyMatchInfoTable *info, const struct UsbPnpMatchIdTable *idTable)
742 {
743     if (DListIsEmpty(&g_usbPnpDeviceTableListHead) == true) {
744         HDF_LOGE("%s:%d g_usbPnpDeviceTableListHead is empty. ", __func__, __LINE__);
745         return NULL;
746     }
747 
748     struct UsbPnpDeviceListTable *deviceListTablePos = NULL;
749     struct UsbPnpDeviceListTable *deviceListTableTemp = NULL;
750     DLIST_FOR_EACH_ENTRY_SAFE(
751         deviceListTablePos, deviceListTableTemp, &g_usbPnpDeviceTableListHead, struct UsbPnpDeviceListTable, list) {
752         if ((strcmp(deviceListTablePos->moduleName, idTable->moduleName) == 0) &&
753             (strcmp(deviceListTablePos->serviceName, idTable->serviceName) == 0) &&
754             (strcmp(deviceListTablePos->deviceMatchAttr, idTable->deviceMatchAttr) == 0) &&
755             (deviceListTablePos->usbDevAddr == info->usbDevAddr) && (deviceListTablePos->devNum == info->devNum) &&
756             (deviceListTablePos->busNum == info->busNum)) {
757             return deviceListTablePos;
758         }
759     }
760 
761     HDF_LOGE("%s:%d interface=%d-%d-%d to be add but not exist. ", __func__, __LINE__, info->devNum, info->busNum,
762         info->numInfos);
763 
764     return NULL;
765 }
766 
UsbDdkPnpLoaderrAddPnpDevice(struct HdfDeviceObject * usbPnpManagerDevice,const struct UsbPnpNotifyMatchInfoTable * infoTable,const struct UsbPnpMatchIdTable * idTable,uint32_t cmdId)767 static int32_t UsbDdkPnpLoaderrAddPnpDevice(struct HdfDeviceObject *usbPnpManagerDevice,
768     const struct UsbPnpNotifyMatchInfoTable *infoTable, const struct UsbPnpMatchIdTable *idTable, uint32_t cmdId)
769 {
770     struct UsbPnpDeviceListTable *deviceListTable = UsbDdkPnpLoaderAddInterface(infoTable, idTable);
771     if ((deviceListTable != NULL) && (deviceListTable->status != USB_PNP_REMOVE_STATUS)) {
772         return HDF_SUCCESS;
773     }
774 
775     struct UsbPnpNotifyServiceInfo serviceInfo;
776     serviceInfo.length =
777         sizeof(struct UsbPnpNotifyServiceInfo) - (USB_PNP_INFO_MAX_INTERFACES - idTable->interfaceLength);
778     serviceInfo.devNum = infoTable->devNum;
779     serviceInfo.busNum = infoTable->busNum;
780     serviceInfo.interfaceLength = idTable->interfaceLength;
781     int32_t ret = memcpy_s(serviceInfo.interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, idTable->interfaceNumber,
782         USB_PNP_INFO_MAX_INTERFACES);
783     if (ret != HDF_SUCCESS) {
784         HDF_LOGE("%{public}s:%{public}d memcpy_s failed", __func__, __LINE__);
785         return ret;
786     }
787 
788     struct HdfSBuf *pnpData =
789         UsbDdkPnpLoaderBufCreate(idTable->moduleName, idTable->serviceName, idTable->deviceMatchAttr, serviceInfo);
790     if (pnpData == NULL) {
791         ret = HDF_FAILURE;
792         HDF_LOGE("%s: UsbDdkPnpLoaderBufCreate failed", __func__);
793         goto ERROR;
794     }
795 
796     ret = UsbDdkPnpLoaderDispatchPnpDevice(usbPnpManagerDevice, pnpData, true);
797     if (ret != HDF_SUCCESS) {
798         HDF_LOGE("%s: handle failed, cmdId is %d, ret=%d", __func__, cmdId, ret);
799     } else {
800         if (cmdId == USB_PNP_NOTIFY_ADD_INTERFACE) {
801             if (deviceListTable == NULL) {
802                 ret = HDF_ERR_INVALID_OBJECT;
803                 HDF_LOGE("%s:%d UsbDdkPnpLoaderAddInterface failed", __func__, __LINE__);
804                 goto ERROR;
805             }
806             deviceListTable->status = USB_PNP_ADD_STATUS;
807         } else {
808             ret = UsbDdkPnpLoaderDeviceListAdd(infoTable, idTable);
809             if (ret != HDF_SUCCESS) {
810                 HDF_LOGE("%s:%d UsbDdkPnpLoaderDeviceListAdd failed", __func__, __LINE__);
811                 goto ERROR;
812             }
813         }
814     }
815 ERROR:
816     HdfSbufRecycle(pnpData);
817     return ret;
818 }
819 
UsbDdkPnpLoaderAddDevice(uint32_t cmdId,uint8_t index,struct HdfDeviceObject * usbPnpManagerDevice,const struct UsbPnpNotifyMatchInfoTable * infoTable,struct UsbPnpMatchIdTable ** matchIdTable)820 static bool UsbDdkPnpLoaderAddDevice(uint32_t cmdId, uint8_t index, struct HdfDeviceObject *usbPnpManagerDevice,
821     const struct UsbPnpNotifyMatchInfoTable *infoTable, struct UsbPnpMatchIdTable **matchIdTable)
822 {
823     int32_t ret;
824     struct UsbPnpMatchIdTable *idTable = NULL;
825     int32_t tableCount;
826     bool match = false;
827     for (tableCount = 0, idTable = matchIdTable[0]; idTable != NULL; idTable = matchIdTable[++tableCount]) {
828         if (!UsbDdkPnpLoaderMatchDevice(infoTable, idTable)) {
829             continue;
830         }
831 
832         if (!UsbDdkPnpLoaderMatchOneIdIntf(infoTable, index, idTable)) {
833             continue;
834         }
835 
836         HDF_LOGD("%s:%d matchDevice end, index=%d tableCount=%d is match \
837             moduleName=%s, serviceName=%s",
838             __func__, __LINE__, index, tableCount, idTable->moduleName, idTable->serviceName);
839         match = true;
840         ret = UsbDdkPnpLoaderrAddPnpDevice(usbPnpManagerDevice, infoTable, idTable, cmdId);
841         if (ret != HDF_SUCCESS) {
842             continue;
843         }
844     }
845     return match;
846 }
847 
UsbDdkPnpLoaderRemoveHandle(struct HdfDeviceObject * usbPnpManager,struct UsbPnpDeviceListTable * deviceListTablePos)848 static int32_t UsbDdkPnpLoaderRemoveHandle(
849     struct HdfDeviceObject *usbPnpManager, struct UsbPnpDeviceListTable *deviceListTablePos)
850 {
851     struct UsbPnpNotifyServiceInfo serviceInfo;
852     struct HdfSBuf *pnpData = NULL;
853     int32_t ret = HDF_SUCCESS;
854 
855     serviceInfo.length = (uint32_t)(sizeof(struct UsbPnpNotifyServiceInfo) -
856         (USB_PNP_INFO_MAX_INTERFACES - deviceListTablePos->interfaceLength));
857     serviceInfo.devNum = deviceListTablePos->devNum;
858     serviceInfo.busNum = deviceListTablePos->busNum;
859     serviceInfo.interfaceLength = deviceListTablePos->interfaceLength;
860     ret = memcpy_s(serviceInfo.interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, deviceListTablePos->interfaceNumber,
861         USB_PNP_INFO_MAX_INTERFACES);
862     if (ret != HDF_SUCCESS) {
863         HDF_LOGE("%{public}s:%{public}d memcpy_s failed", __func__, __LINE__);
864         return ret;
865     }
866 
867     pnpData = UsbDdkPnpLoaderBufCreate(deviceListTablePos->moduleName, deviceListTablePos->serviceName,
868         deviceListTablePos->deviceMatchAttr, serviceInfo);
869     if (pnpData == NULL) {
870         HDF_LOGE("%s: UsbDdkPnpLoaderBufCreate faile", __func__);
871         return HDF_FAILURE;
872     }
873 
874     if (deviceListTablePos->status != USB_PNP_REMOVE_STATUS) {
875         ret = UsbDdkPnpLoaderDispatchPnpDevice(usbPnpManager, pnpData, false);
876         if (ret != HDF_SUCCESS) {
877             HDF_LOGE("%s:%d UsbDdkPnpLoaderDispatchPnpDevice faile ret=%d", __func__, __LINE__, ret);
878             goto ERROR;
879         }
880         deviceListTablePos->status = USB_PNP_REMOVE_STATUS;
881     }
882 
883 ERROR:
884     HdfSbufRecycle(pnpData);
885     return ret;
886 }
887 
UsbDdkPnpLoaderRemoveDevice(struct HdfDeviceObject * usbPnpManager,struct UsbPnpRemoveInfo removeInfo,uint32_t cmdId)888 static int32_t UsbDdkPnpLoaderRemoveDevice(
889     struct HdfDeviceObject *usbPnpManager, struct UsbPnpRemoveInfo removeInfo, uint32_t cmdId)
890 {
891     int32_t ret = HDF_SUCCESS;
892     struct UsbPnpDeviceListTable *deviceListTablePos = NULL;
893     struct UsbPnpDeviceListTable *deviceListTableTemp = NULL;
894     bool findFlag = false;
895     int32_t i;
896 
897     if (DListIsEmpty(&g_usbPnpDeviceTableListHead) == true) {
898         HDF_LOGE("%s:%d g_usbPnpDeviceTableListHead is empty. ", __func__, __LINE__);
899         return HDF_SUCCESS;
900     }
901 
902     DLIST_FOR_EACH_ENTRY_SAFE(
903         deviceListTablePos, deviceListTableTemp, &g_usbPnpDeviceTableListHead, struct UsbPnpDeviceListTable, list) {
904         if (deviceListTablePos->usbDevAddr == removeInfo.usbDevAddr) {
905             if (removeInfo.removeType == USB_PNP_NOTIFY_REMOVE_INTERFACE_NUM) {
906                 for (i = 0; i < deviceListTablePos->interfaceLength; i++) {
907                     if (deviceListTablePos->interfaceNumber[i] == removeInfo.interfaceNum) {
908                         break;
909                     }
910                 }
911 
912                 if (i >= deviceListTablePos->interfaceLength) {
913                     continue;
914                 }
915             }
916             findFlag = true;
917 
918             ret = UsbDdkPnpLoaderRemoveHandle(usbPnpManager, deviceListTablePos);
919             if (ret != HDF_SUCCESS) {
920                 break;
921             }
922 
923             if (cmdId != USB_PNP_NOTIFY_REMOVE_INTERFACE) {
924                 DListRemove(&deviceListTablePos->list);
925                 OsalMemFree(deviceListTablePos);
926                 deviceListTablePos = NULL;
927             }
928         }
929     }
930 
931     if (findFlag == false) {
932         HDF_LOGE("%s:%d removeType=%d to be remove but not exist.", __func__, __LINE__, removeInfo.removeType);
933         ret = HDF_FAILURE;
934     }
935 
936     return ret;
937 }
938 
UsbDdkPnpLoaderDevice(struct HdfDeviceObject * usbPnpManagerDevice,const struct UsbPnpNotifyMatchInfoTable * infoTable,uint32_t id)939 static int32_t UsbDdkPnpLoaderDevice(
940     struct HdfDeviceObject *usbPnpManagerDevice, const struct UsbPnpNotifyMatchInfoTable *infoTable, uint32_t id)
941 {
942     int8_t i;
943     int32_t tableCount;
944     struct UsbPnpMatchIdTable *idTable = NULL;
945 
946     if ((infoTable == NULL) || (g_usbPnpMatchIdTable == NULL) || (g_usbPnpMatchIdTable[0] == NULL)) {
947         HDF_LOGE("%s:%d infoTable or super or g_usbPnpMatchIdTable is NULL!", __func__, __LINE__);
948         return HDF_ERR_INVALID_PARAM;
949     }
950     bool match = false;
951     for (i = 0; i < infoTable->numInfos; i++) {
952         if (UsbDdkPnpLoaderAddDevice(id, i, usbPnpManagerDevice, infoTable, g_usbPnpMatchIdTable)) {
953             match = true;
954         }
955     }
956     if (!match) {
957 #ifndef __LITEOS__
958         UsbDDKDriverMatchFailEvent(infoTable);
959 #endif
960     }
961 
962     for (tableCount = 0, idTable = g_usbPnpMatchIdTable[0]; idTable != NULL;
963          idTable = g_usbPnpMatchIdTable[++tableCount]) {
964         idTable->interfaceClassMask = 0;
965         if (!(idTable->matchFlag & USB_PNP_NOTIFY_MATCH_INT_CLASS)) {
966             idTable->interfaceClassLength = 0;
967         }
968         idTable->interfaceSubClassMask = 0;
969         if (!(idTable->matchFlag & USB_PNP_NOTIFY_MATCH_INT_SUBCLASS)) {
970             idTable->interfaceSubClassLength = 0;
971         }
972         idTable->interfaceProtocolMask = 0;
973         if (!(idTable->matchFlag & USB_PNP_NOTIFY_MATCH_INT_PROTOCOL)) {
974             idTable->interfaceProtocolLength = 0;
975         }
976         idTable->interfaceMask = 0;
977         if (!(idTable->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER)) {
978             idTable->interfaceLength = 0;
979         }
980         idTable->pnpMatchFlag = false;
981     }
982 
983     return HDF_SUCCESS;
984 }
985 
UsbDdkPnpLoaderEventReceived(void * usbPnpManagerPtr,uint32_t id,struct HdfSBuf * data)986 int32_t UsbDdkPnpLoaderEventReceived(void *usbPnpManagerPtr, uint32_t id, struct HdfSBuf *data)
987 {
988     int32_t ret;
989     bool flag = false;
990     uint32_t infoSize;
991     struct UsbPnpNotifyMatchInfoTable *infoTable = NULL;
992     struct UsbPnpRemoveInfo removeInfo;
993     struct HdfDeviceObject *usbPnpManagerDevice = (struct HdfDeviceObject *)usbPnpManagerPtr;
994 
995     switch (id) {
996         case USB_PNP_NOTIFY_ADD_INTERFACE:
997         case USB_PNP_NOTIFY_ADD_DEVICE:
998         case USB_PNP_NOTIFY_REPORT_INTERFACE:
999 #if USB_PNP_NOTIFY_TEST_MODE == true
1000         case USB_PNP_NOTIFY_ADD_TEST:
1001 #endif
1002             flag = HdfSbufReadBuffer(data, (const void **)(&infoTable), &infoSize);
1003             if ((!flag) || (infoTable == NULL)) {
1004                 ret = HDF_ERR_INVALID_PARAM;
1005                 HDF_LOGE("%s: fail to read infoTable in event data, flag = %d", __func__, flag);
1006                 return ret;
1007             }
1008             ret = UsbDdkPnpLoaderDevice(usbPnpManagerDevice, infoTable, id);
1009             break;
1010         case USB_PNP_NOTIFY_REMOVE_INTERFACE:
1011         case USB_PNP_NOTIFY_REMOVE_DEVICE:
1012 #if USB_PNP_NOTIFY_TEST_MODE == true
1013         case USB_PNP_NOTIFY_REMOVE_TEST:
1014 #endif
1015             flag = HdfSbufReadBuffer(data, (const void **)(&infoTable), &infoSize);
1016             if ((!flag) || (infoTable == NULL)) {
1017                 ret = HDF_ERR_INVALID_PARAM;
1018                 HDF_LOGE("%s: fail to read infoTable in event data, flag = %d", __func__, flag);
1019                 return ret;
1020             }
1021             removeInfo.removeType = infoTable->removeType;
1022             removeInfo.usbDevAddr = infoTable->usbDevAddr;
1023             removeInfo.devNum = infoTable->devNum;
1024             removeInfo.busNum = infoTable->busNum;
1025             removeInfo.interfaceNum = infoTable->interfaceInfo[0].interfaceNumber;
1026             ret = UsbDdkPnpLoaderRemoveDevice(usbPnpManagerDevice, removeInfo, id);
1027             break;
1028         default:
1029             HDF_LOGI("%{public}s: not interested in %{public}d", __func__, id);
1030             ret = HDF_SUCCESS;
1031             break;
1032     }
1033 
1034     HDF_LOGD("%s:%d ret=%d DONE", __func__, __LINE__, ret);
1035 
1036     return ret;
1037 }
1038 
UsbDdkPnpLoaderEventHandle(void)1039 int32_t UsbDdkPnpLoaderEventHandle(void)
1040 {
1041     static bool firstInitFlag = true;
1042     if (firstInitFlag == true) {
1043         firstInitFlag = false;
1044         DListHeadInit(&g_usbPnpDeviceTableListHead);
1045     }
1046 
1047     g_usbPnpMatchIdTable = UsbDdkPnpLoaderPnpMatch();
1048     if ((g_usbPnpMatchIdTable == NULL) || (g_usbPnpMatchIdTable[0] == NULL)) {
1049         HDF_LOGE("%s: g_usbPnpMatchIdTable or g_usbPnpMatchIdTable[0] is NULL!", __func__);
1050         return HDF_ERR_INVALID_PARAM;
1051     }
1052 
1053     return HDF_SUCCESS;
1054 }
1055