refactoring usb module from windows 50/11250/1
authoryoonki.park <yoonki.park@samsung.com>
Wed, 23 Oct 2013 07:43:00 +0000 (16:43 +0900)
committeryoonki.park <yoonki.park@samsung.com>
Wed, 23 Oct 2013 07:43:00 +0000 (16:43 +0900)
Change-Id: I44d4ec072a7166f3f9347f2d6cab4b0effad6163
Signed-off-by: yoonki.park <yoonki.park@samsung.com>
src/common_modules.h
src/usb_windows.c

index 2049134f7ad04b0f215bc58d99462fe0c91dcd0e..a1af8a928c05b7dbcde884f3e854c0d51ef59671 100644 (file)
@@ -38,7 +38,7 @@
 #define A_VERSION 0x0100000
 #define SDB_VERSION_MAJOR   2       // increments upon significant architectural changes or the achievement of important milestones
 #define SDB_VERSION_MINOR   2       // progress is made within a major version
-#define SDB_VERSION_PATCH   15      // increments for small sets of changes
+#define SDB_VERSION_PATCH   17      // increments for small sets of changes
 #define SDB_VERSION_MAX_LENGTH  128
 
 extern MAP hex_map;
index c35a80b15421f7a23da7625b3b3a7731dd17d139..aed769eaa40517a9583804f6438d1614c0ac6f40 100644 (file)
 #include "ddk/usb100.h"
 #include "ddk/usbioctl.h"
 
-#define DLL_SYMBOL_DECLARE(api, ret, name, args) \
-   typedef ret (api * __dll_##name##_t)args; \
-   static __dll_##name##_t name
-
-#define LOAD_FUNCPTR(name)                                    \
-  do {                                                        \
-  HMODULE h = GetModuleHandle("winusb.dll");                  \
-  if(h == NULL)                                               \
-    h = LoadLibrary("winusb.dll");                            \
-  if(h == NULL)                                               \
-    break;                                                    \
-  if((name = (__dll_##name##_t)GetProcAddress(h, #name)))     \
-    break;                                                    \
-  if((name = (__dll_##name##_t)GetProcAddress(h, #name "A"))) \
-    break;                                                    \
-  if((name = (__dll_##name##_t)GetProcAddress(h, #name "W"))) \
-    break;                                                    \
-  } while(0)
-
 // The USBD_PIPE_TYPE enumerator indicates the type of pipe.
 typedef enum _USBD_PIPE_TYPE { 
   UsbdPipeTypeControl      = 0, //Indicates that the pipe is a control pipe.
@@ -103,25 +84,45 @@ static LIST_NODE* handle_list = NULL;
 
 SDB_MUTEX_DEFINE( usb_lock );
 
-// winusb.dll entrypoints
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_Initialize, (HANDLE, PWINUSB_INTERFACE_HANDLE));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_Free, (WINUSB_INTERFACE_HANDLE));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_GetDescriptor, (WINUSB_INTERFACE_HANDLE, UCHAR, UCHAR, USHORT, PUCHAR, ULONG, PULONG));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_QueryInterfaceSettings, (WINUSB_INTERFACE_HANDLE, UCHAR, PUSB_INTERFACE_DESCRIPTOR));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_SetCurrentAlternateSetting, (WINUSB_INTERFACE_HANDLE, UCHAR));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_GetCurrentAlternateSetting, (WINUSB_INTERFACE_HANDLE, PUCHAR));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_QueryPipe, (WINUSB_INTERFACE_HANDLE, UCHAR, UCHAR, PWINUSB_PIPE_INFORMATION));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_SetPipePolicy, (WINUSB_INTERFACE_HANDLE, UCHAR, ULONG, ULONG, PVOID));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_GetPipePolicy, (WINUSB_INTERFACE_HANDLE, UCHAR, ULONG, PULONG, PVOID));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_ReadPipe, (WINUSB_INTERFACE_HANDLE, UCHAR, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_WritePipe, (WINUSB_INTERFACE_HANDLE, UCHAR, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_ControlTransfer, (WINUSB_INTERFACE_HANDLE, WINUSB_SETUP_PACKET, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_ResetPipe, (WINUSB_INTERFACE_HANDLE, UCHAR));
-DLL_SYMBOL_DECLARE(WINAPI, BOOL, WinUsb_GetOverlappedResult, (WINUSB_INTERFACE_HANDLE, LPOVERLAPPED, LPDWORD, BOOL));
+HMODULE g_h = NULL;
+
+#define TYPEDEF_SYMBOL(name, args)      \
+   typedef BOOL (WINAPI * _##name##_)args;  \
+   static _##name##_ name
+
+#define LOAD_FUNCPTR(name)                                    \
+  do {                                                        \
+  if((name = (_##name##_)GetProcAddress(g_h, #name)))         \
+    break;                                                    \
+  } while(0)
+
+// declare all symbols exported by winusb dll
+TYPEDEF_SYMBOL(WinUsb_Initialize, (HANDLE, PWINUSB_INTERFACE_HANDLE));
+TYPEDEF_SYMBOL(WinUsb_Free, (WINUSB_INTERFACE_HANDLE));
+TYPEDEF_SYMBOL(WinUsb_GetDescriptor, (WINUSB_INTERFACE_HANDLE, UCHAR, UCHAR, USHORT, PUCHAR, ULONG, PULONG));
+TYPEDEF_SYMBOL(WinUsb_QueryInterfaceSettings, (WINUSB_INTERFACE_HANDLE, UCHAR, PUSB_INTERFACE_DESCRIPTOR));
+TYPEDEF_SYMBOL(WinUsb_SetCurrentAlternateSetting, (WINUSB_INTERFACE_HANDLE, UCHAR));
+TYPEDEF_SYMBOL(WinUsb_GetCurrentAlternateSetting, (WINUSB_INTERFACE_HANDLE, PUCHAR));
+TYPEDEF_SYMBOL(WinUsb_QueryPipe, (WINUSB_INTERFACE_HANDLE, UCHAR, UCHAR, PWINUSB_PIPE_INFORMATION));
+TYPEDEF_SYMBOL(WinUsb_SetPipePolicy, (WINUSB_INTERFACE_HANDLE, UCHAR, ULONG, ULONG, PVOID));
+TYPEDEF_SYMBOL(WinUsb_GetPipePolicy, (WINUSB_INTERFACE_HANDLE, UCHAR, ULONG, PULONG, PVOID));
+TYPEDEF_SYMBOL(WinUsb_ReadPipe, (WINUSB_INTERFACE_HANDLE, UCHAR, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
+TYPEDEF_SYMBOL(WinUsb_WritePipe, (WINUSB_INTERFACE_HANDLE, UCHAR, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
+TYPEDEF_SYMBOL(WinUsb_ControlTransfer, (WINUSB_INTERFACE_HANDLE, WINUSB_SETUP_PACKET, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
+TYPEDEF_SYMBOL(WinUsb_ResetPipe, (WINUSB_INTERFACE_HANDLE, UCHAR));
+TYPEDEF_SYMBOL(WinUsb_GetOverlappedResult, (WINUSB_INTERFACE_HANDLE, LPOVERLAPPED, LPDWORD, BOOL));
 
 void win_usb_init(void) {
     // Initialize DLL functions
-    if (!WinUsb_Initialize) {
+    if (g_h == NULL) {
+        g_h = GetModuleHandle("winusb.dll");
+        if(g_h == NULL) {
+            g_h = LoadLibrary("winusb.dll");
+        }
+        if(g_h == NULL) {
+            LOG_FATAL("cannot load winusb dll\n");
+        }
+
         LOAD_FUNCPTR(WinUsb_Initialize);
         LOAD_FUNCPTR(WinUsb_Free);
         LOAD_FUNCPTR(WinUsb_GetDescriptor);
@@ -146,13 +147,13 @@ int is_device_registered(const char *node_path)
 
     LIST_NODE* cur_ptr = handle_list;
     while(cur_ptr != NULL) {
-       usb_handle* usb = cur_ptr->data;
-       cur_ptr = cur_ptr->next_ptr;
+        usb_handle* usb = cur_ptr->data;
+        cur_ptr = cur_ptr->next_ptr;
 
-       if(!strcmp(usb->unique_node_path, node_path)) {
-               r = 1;
-               break;
-       }
+        if(!strcmp(usb->unique_node_path, node_path)) {
+            r = 1;
+            break;
+        }
     }
     sdb_mutex_unlock(&usb_lock, "usb is_device_registered");
     return r;
@@ -169,7 +170,7 @@ usb_handle *usb_open(const char *device_path) {
             FILE_FLAG_OVERLAPPED, NULL);
 
     if (INVALID_HANDLE_VALUE == hnd) {
-        D("fail to create device file: %s due to: %d\n", device_path, GetLastError());
+        LOG_DEBUG("fail to create device file: %s due to: %d\n", device_path, GetLastError());
        return NULL;
     }
 
@@ -201,7 +202,7 @@ usb_handle *usb_open(const char *device_path) {
     }
 
     if (2 != usb_interface_descriptor.bNumEndpoints) {
-        D("the number of endpoint should be two\n");
+        LOG_DEBUG("the number of endpoint should be two\n");
         return NULL;
     }
 
@@ -226,12 +227,12 @@ usb_handle *usb_open(const char *device_path) {
         // only interested in bulk type
         if (UsbdPipeTypeBulk == pipe_info.PipeType) {
             if (USB_ENDPOINT_DIRECTION_IN(pipe_info.PipeId)) {
-                D("builk in endpoint index: %d, id: %04x\n", endpoint_index, pipe_info.PipeId);
+                LOG_DEBUG("builk in endpoint index: %d, id: %04x\n", endpoint_index, pipe_info.PipeId);
                 usb->end_point[0] = pipe_info.PipeId;
             }
 
             if (USB_ENDPOINT_DIRECTION_OUT(pipe_info.PipeId)) {
-                D("builk out endpoint index: %d, id: %04x\n", endpoint_index, pipe_info.PipeId);
+                LOG_DEBUG("builk out endpoint index: %d, id: %04x\n", endpoint_index, pipe_info.PipeId);
                 usb->end_point[1] = pipe_info.PipeId;
             }
 
@@ -298,7 +299,7 @@ int usb_find_devices(GUID deviceClassID) {
     // Get information about all the installed devices for the specified device interface class.
     HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&deviceClassID, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
     if (hDeviceInfo == INVALID_HANDLE_VALUE) {
-        D("fail to find any device: %d\n", GetLastError());
+        LOG_DEBUG("fail to find any device: %d\n", GetLastError());
         return 0;
     }
 
@@ -320,7 +321,7 @@ int usb_find_devices(GUID deviceClassID) {
         //Check for some other error
         if (!bResult)
         {
-            D("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
+            LOG_DEBUG("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
             break;
         }
 
@@ -340,11 +341,11 @@ int usb_find_devices(GUID deviceClassID) {
                 detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(requiredLength);
 
                 if (detailData == NULL) {
-                    D("fail to allocate memory\n");
+                    LOG_DEBUG("fail to allocate memory\n");
                     break;
                 }
             } else {
-                D("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
+                LOG_DEBUG("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
                 break;
             }
         }
@@ -357,7 +358,7 @@ int usb_find_devices(GUID deviceClassID) {
 
         //Check for some other error
         if (!bResult) {
-            D("fail to setdup get device interface detail: %d\n", GetLastError());
+            LOG_DEBUG("fail to setdup get device interface detail: %d\n", GetLastError());
             if (detailData != NULL) {
                 free(detailData);
             }
@@ -376,16 +377,16 @@ int usb_find_devices(GUID deviceClassID) {
             if (hnd != NULL) {
                 char serial[256];
                 if (get_serial_number(hnd, serial, sizeof(serial)) > 0) {
-                    D("register usb for: %s\n", serial);
+                    LOG_DEBUG("register usb for: %s\n", serial);
                     if (register_device(hnd)) {
                         register_usb_transport(hnd, serial);
                     } else {
-                        D("fail to register usb\n");
+                        LOG_DEBUG("fail to register usb\n");
                         win_usb_close(hnd);
                         free(hnd);
                     }
                 } else {
-                    D("fail to get usb serial name: kick? close?\n");
+                    LOG_DEBUG("fail to get usb serial name: kick? close?\n");
                     win_usb_close(hnd);
                     free(hnd);
                 }
@@ -399,7 +400,7 @@ int usb_find_devices(GUID deviceClassID) {
 }
 
 void* device_poll_thread(void* sleep_msec) {
-    D("Created device thread\n");
+    LOG_DEBUG("Created device thread\n");
 
     int  mseconds = (int) sleep_msec;
     while (1) {
@@ -420,7 +421,11 @@ void sdb_usb_init() {
 }
 
 void sdb_usb_cleanup() {
-    D("TODO: not imple yet\n");
+    LOG_DEBUG("+all clean resources+\n");
+
+    if (g_h != NULL) {
+        FreeLibrary(g_h);
+    }
 }
 
 int usb_bulk_transfer(usb_handle* handle, BOOL is_read, const void *data, unsigned long length, unsigned long *actual_length, unsigned long timeout) {
@@ -433,13 +438,13 @@ int usb_bulk_transfer(usb_handle* handle, BOOL is_read, const void *data, unsign
         endpoint = handle->end_point[1];
     }
     if (handle->fd == NULL) {
-        D("invalid handle\n");
+        LOG_DEBUG("invalid handle\n");
         return 0;
     }
 
     // do not complete within the specified time-out interval
     if (!WinUsb_SetPipePolicy(handle->fd, endpoint, PIPE_TRANSFER_TIMEOUT, sizeof(tmp), &tmp)) {
-        D("fail to set timeout\n");
+        LOG_DEBUG("fail to set timeout\n");
         SetLastError(ERROR_SEM_TIMEOUT);
         return 0;
     }
@@ -463,7 +468,7 @@ int usb_bulk_transfer(usb_handle* handle, BOOL is_read, const void *data, unsign
         if (NULL != overlapped.hEvent){
             CloseHandle(overlapped.hEvent);
         }
-        D("pipe error: (%ld/ld) error:%d\n", length, transferred, GetLastError());
+        LOG_DEBUG("pipe error: (%ld/ld) error:%d\n", length, transferred, GetLastError());
         return 0;
     }
 
@@ -486,12 +491,12 @@ int sdb_usb_write(usb_handle* handle, const void* data, int len) {
     unsigned long written = 0;
     int ret;
 
-    D("+sdb_usb_write %d\n", len);
+    LOG_DEBUG("+sdb_usb_write %d\n", len);
 
     if (NULL != handle) {
         ret = usb_bulk_transfer(handle, FALSE, (void*)data, (unsigned long)len, &written, time_out);
         int saved_errno = GetLastError();
-        D("sdb_usb_write got(ret:%d): %ld, expected: %d, errno: %d\n",ret, written, len, saved_errno);
+        LOG_DEBUG("sdb_usb_write got(ret:%d): %ld, expected: %d, errno: %d\n",ret, written, len, saved_errno);
 
         if (ret) {
             if (written == (unsigned long) len) {
@@ -509,11 +514,11 @@ int sdb_usb_write(usb_handle* handle, const void* data, int len) {
         }
         errno = saved_errno;
     } else {
-        D("usb_write NULL handle\n");
+        LOG_DEBUG("usb_write NULL handle\n");
         SetLastError(ERROR_INVALID_HANDLE);
     }
 
-    D("-sdb_usb_write failed: %d\n", errno);
+    LOG_DEBUG("-sdb_usb_write failed: %d\n", errno);
 
     return -1;
 }
@@ -522,13 +527,13 @@ int sdb_usb_read(usb_handle *handle, void* data, int len) {
     unsigned long n = 0;
     int ret;
 
-    D("+sdb_usb_read %d\n", len);
+    LOG_DEBUG("+sdb_usb_read %d\n", len);
     if (NULL != handle) {
         while (len > 0) {
             int xfer = (len > 4096) ? 4096 : len;
             ret = usb_bulk_transfer(handle, TRUE, (void*)data, (unsigned long)xfer, &n, (unsigned long)0);
             int saved_errno = GetLastError();
-            D("sdb_usb_read got(ret:%d): %ld, expected: %d, errno: %d\n", ret, n, xfer, saved_errno);
+            LOG_DEBUG("sdb_usb_read got(ret:%d): %ld, expected: %d, errno: %d\n", ret, n, xfer, saved_errno);
 
             if (ret) {
                 data += n;
@@ -546,17 +551,17 @@ int sdb_usb_read(usb_handle *handle, void* data, int len) {
             errno = saved_errno;
         }
     } else {
-        D("sdb_usb_read NULL handle\n");
+        LOG_DEBUG("sdb_usb_read NULL handle\n");
         SetLastError(ERROR_INVALID_HANDLE);
     }
 
-    D("-sdb_usb_read failed: %d\n", errno);
+    LOG_DEBUG("-sdb_usb_read failed: %d\n", errno);
 
     return -1;
 }
 
 int win_usb_close(usb_handle *handle) {
-    D("+usb win_usb_close\n");
+    LOG_DEBUG("+usb win_usb_close\n");
     if (NULL != handle) {
         if (NULL != handle->fd) {
             WinUsb_Free(handle->fd);
@@ -568,28 +573,28 @@ int win_usb_close(usb_handle *handle) {
         }
         handle = NULL;
     }
-    D("-usb win_usb_close\n");
+    LOG_DEBUG("-usb win_usb_close\n");
     return 0;
 }
 
 void sdb_usb_kick(usb_handle* handle) {
-    D("+sdb_usb_kick: %p\n", handle);
+    LOG_DEBUG("+sdb_usb_kick: %p\n", handle);
     // called from transport remote kick if already registered
     // so only clean for win usb handle resources
     sdb_mutex_lock(&usb_lock, "usb sdb_usb_kick");
     win_usb_close(handle);
     sdb_mutex_unlock(&usb_lock , "usb sdb_usb_kick");
-    D("-sdb_usb_kick: %p\n", handle);
+    LOG_DEBUG("-sdb_usb_kick: %p\n", handle);
 }
 
 int sdb_usb_close(usb_handle* handle) {
-    D("+sdb_usb_close: %p\n", handle);
+    LOG_DEBUG("+sdb_usb_close: %p\n", handle);
     if (NULL != handle) {
         sdb_mutex_lock(&usb_lock, "usb sdb_usb_close");
         remove_node(&handle_list, handle->node, NULL);
         sdb_mutex_unlock(&usb_lock, "usb sdb_usb_close");
     }
-    D("-sdb_usb_close: %p\n", handle);
+    LOG_DEBUG("-sdb_usb_close: %p\n", handle);
     return 0;
 }