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