usb_windows.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <windows.h>
  17. #include <winerror.h>
  18. #include <errno.h>
  19. #include <usb100.h>
  20. #include <adb_api.h>
  21. #include <stdio.h>
  22. #include "sysdeps.h"
  23. #define TRACE_TAG TRACE_USB
  24. #include "adb.h"
  25. /** Structure usb_handle describes our connection to the usb device via
  26. AdbWinApi.dll. This structure is returned from usb_open() routine and
  27. is expected in each subsequent call that is accessing the device.
  28. */
  29. struct usb_handle {
  30. /// Previous entry in the list of opened usb handles
  31. usb_handle *prev;
  32. /// Next entry in the list of opened usb handles
  33. usb_handle *next;
  34. /// Handle to USB interface
  35. ADBAPIHANDLE adb_interface;
  36. /// Handle to USB read pipe (endpoint)
  37. ADBAPIHANDLE adb_read_pipe;
  38. /// Handle to USB write pipe (endpoint)
  39. ADBAPIHANDLE adb_write_pipe;
  40. /// Interface name
  41. char* interface_name;
  42. /// Mask for determining when to use zero length packets
  43. unsigned zero_mask;
  44. };
  45. /// Class ID assigned to the device by androidusb.sys
  46. static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
  47. /// List of opened usb handles
  48. static usb_handle handle_list = {
  49. .prev = &handle_list,
  50. .next = &handle_list,
  51. };
  52. /// Locker for the list of opened usb handles
  53. ADB_MUTEX_DEFINE( usb_lock );
  54. /// Checks if there is opened usb handle in handle_list for this device.
  55. int known_device(const char* dev_name);
  56. /// Checks if there is opened usb handle in handle_list for this device.
  57. /// usb_lock mutex must be held before calling this routine.
  58. int known_device_locked(const char* dev_name);
  59. /// Registers opened usb handle (adds it to handle_list).
  60. int register_new_device(usb_handle* handle);
  61. /// Checks if interface (device) matches certain criteria
  62. int recognized_device(usb_handle* handle);
  63. /// Enumerates present and available interfaces (devices), opens new ones and
  64. /// registers usb transport for them.
  65. void find_devices();
  66. /// Entry point for thread that polls (every second) for new usb interfaces.
  67. /// This routine calls find_devices in infinite loop.
  68. void* device_poll_thread(void* unused);
  69. /// Initializes this module
  70. void usb_init();
  71. /// Cleans up this module
  72. void usb_cleanup();
  73. /// Opens usb interface (device) by interface (device) name.
  74. usb_handle* do_usb_open(const wchar_t* interface_name);
  75. /// Writes data to the opened usb handle
  76. int usb_write(usb_handle* handle, const void* data, int len);
  77. /// Reads data using the opened usb handle
  78. int usb_read(usb_handle *handle, void* data, int len);
  79. /// Cleans up opened usb handle
  80. void usb_cleanup_handle(usb_handle* handle);
  81. /// Cleans up (but don't close) opened usb handle
  82. void usb_kick(usb_handle* handle);
  83. /// Closes opened usb handle
  84. int usb_close(usb_handle* handle);
  85. /// Gets interface (device) name for an opened usb handle
  86. const char *usb_name(usb_handle* handle);
  87. int known_device_locked(const char* dev_name) {
  88. usb_handle* usb;
  89. if (NULL != dev_name) {
  90. // Iterate through the list looking for the name match.
  91. for(usb = handle_list.next; usb != &handle_list; usb = usb->next) {
  92. // In Windows names are not case sensetive!
  93. if((NULL != usb->interface_name) &&
  94. (0 == stricmp(usb->interface_name, dev_name))) {
  95. return 1;
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. int known_device(const char* dev_name) {
  102. int ret = 0;
  103. if (NULL != dev_name) {
  104. adb_mutex_lock(&usb_lock);
  105. ret = known_device_locked(dev_name);
  106. adb_mutex_unlock(&usb_lock);
  107. }
  108. return ret;
  109. }
  110. int register_new_device(usb_handle* handle) {
  111. if (NULL == handle)
  112. return 0;
  113. adb_mutex_lock(&usb_lock);
  114. // Check if device is already in the list
  115. if (known_device_locked(handle->interface_name)) {
  116. adb_mutex_unlock(&usb_lock);
  117. return 0;
  118. }
  119. // Not in the list. Add this handle to the list.
  120. handle->next = &handle_list;
  121. handle->prev = handle_list.prev;
  122. handle->prev->next = handle;
  123. handle->next->prev = handle;
  124. adb_mutex_unlock(&usb_lock);
  125. return 1;
  126. }
  127. void* device_poll_thread(void* unused) {
  128. D("Created device thread\n");
  129. while(1) {
  130. find_devices();
  131. adb_sleep_ms(1000);
  132. }
  133. return NULL;
  134. }
  135. void usb_init() {
  136. adb_thread_t tid;
  137. if(adb_thread_create(&tid, device_poll_thread, NULL)) {
  138. fatal_errno("cannot create input thread");
  139. }
  140. }
  141. void usb_cleanup() {
  142. }
  143. usb_handle* do_usb_open(const wchar_t* interface_name) {
  144. // Allocate our handle
  145. usb_handle* ret = (usb_handle*)malloc(sizeof(usb_handle));
  146. if (NULL == ret)
  147. return NULL;
  148. // Set linkers back to the handle
  149. ret->next = ret;
  150. ret->prev = ret;
  151. // Create interface.
  152. ret->adb_interface = AdbCreateInterfaceByName(interface_name);
  153. if (NULL == ret->adb_interface) {
  154. free(ret);
  155. errno = GetLastError();
  156. return NULL;
  157. }
  158. // Open read pipe (endpoint)
  159. ret->adb_read_pipe =
  160. AdbOpenDefaultBulkReadEndpoint(ret->adb_interface,
  161. AdbOpenAccessTypeReadWrite,
  162. AdbOpenSharingModeReadWrite);
  163. if (NULL != ret->adb_read_pipe) {
  164. // Open write pipe (endpoint)
  165. ret->adb_write_pipe =
  166. AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface,
  167. AdbOpenAccessTypeReadWrite,
  168. AdbOpenSharingModeReadWrite);
  169. if (NULL != ret->adb_write_pipe) {
  170. // Save interface name
  171. unsigned long name_len = 0;
  172. // First get expected name length
  173. AdbGetInterfaceName(ret->adb_interface,
  174. NULL,
  175. &name_len,
  176. true);
  177. if (0 != name_len) {
  178. ret->interface_name = (char*)malloc(name_len);
  179. if (NULL != ret->interface_name) {
  180. // Now save the name
  181. if (AdbGetInterfaceName(ret->adb_interface,
  182. ret->interface_name,
  183. &name_len,
  184. true)) {
  185. // We're done at this point
  186. return ret;
  187. }
  188. } else {
  189. SetLastError(ERROR_OUTOFMEMORY);
  190. }
  191. }
  192. }
  193. }
  194. // Something went wrong.
  195. int saved_errno = GetLastError();
  196. usb_cleanup_handle(ret);
  197. free(ret);
  198. SetLastError(saved_errno);
  199. return NULL;
  200. }
  201. int usb_write(usb_handle* handle, const void* data, int len) {
  202. unsigned long time_out = 5000;
  203. unsigned long written = 0;
  204. int ret;
  205. D("usb_write %d\n", len);
  206. if (NULL != handle) {
  207. // Perform write
  208. ret = AdbWriteEndpointSync(handle->adb_write_pipe,
  209. (void*)data,
  210. (unsigned long)len,
  211. &written,
  212. time_out);
  213. int saved_errno = GetLastError();
  214. if (ret) {
  215. // Make sure that we've written what we were asked to write
  216. D("usb_write got: %ld, expected: %d\n", written, len);
  217. if (written == (unsigned long)len) {
  218. if(handle->zero_mask && (len & handle->zero_mask) == 0) {
  219. // Send a zero length packet
  220. AdbWriteEndpointSync(handle->adb_write_pipe,
  221. (void*)data,
  222. 0,
  223. &written,
  224. time_out);
  225. }
  226. return 0;
  227. }
  228. } else {
  229. // assume ERROR_INVALID_HANDLE indicates we are disconnected
  230. if (saved_errno == ERROR_INVALID_HANDLE)
  231. usb_kick(handle);
  232. }
  233. errno = saved_errno;
  234. } else {
  235. D("usb_write NULL handle\n");
  236. SetLastError(ERROR_INVALID_HANDLE);
  237. }
  238. D("usb_write failed: %d\n", errno);
  239. return -1;
  240. }
  241. int usb_read(usb_handle *handle, void* data, int len) {
  242. unsigned long time_out = 0;
  243. unsigned long read = 0;
  244. int ret;
  245. D("usb_read %d\n", len);
  246. if (NULL != handle) {
  247. while (len > 0) {
  248. int xfer = (len > 4096) ? 4096 : len;
  249. ret = AdbReadEndpointSync(handle->adb_read_pipe,
  250. (void*)data,
  251. (unsigned long)xfer,
  252. &read,
  253. time_out);
  254. int saved_errno = GetLastError();
  255. D("usb_write got: %ld, expected: %d, errno: %d\n", read, xfer, saved_errno);
  256. if (ret) {
  257. data += read;
  258. len -= read;
  259. if (len == 0)
  260. return 0;
  261. } else {
  262. // assume ERROR_INVALID_HANDLE indicates we are disconnected
  263. if (saved_errno == ERROR_INVALID_HANDLE)
  264. usb_kick(handle);
  265. break;
  266. }
  267. errno = saved_errno;
  268. }
  269. } else {
  270. D("usb_read NULL handle\n");
  271. SetLastError(ERROR_INVALID_HANDLE);
  272. }
  273. D("usb_read failed: %d\n", errno);
  274. return -1;
  275. }
  276. void usb_cleanup_handle(usb_handle* handle) {
  277. if (NULL != handle) {
  278. if (NULL != handle->interface_name)
  279. free(handle->interface_name);
  280. if (NULL != handle->adb_write_pipe)
  281. AdbCloseHandle(handle->adb_write_pipe);
  282. if (NULL != handle->adb_read_pipe)
  283. AdbCloseHandle(handle->adb_read_pipe);
  284. if (NULL != handle->adb_interface)
  285. AdbCloseHandle(handle->adb_interface);
  286. handle->interface_name = NULL;
  287. handle->adb_write_pipe = NULL;
  288. handle->adb_read_pipe = NULL;
  289. handle->adb_interface = NULL;
  290. }
  291. }
  292. void usb_kick(usb_handle* handle) {
  293. if (NULL != handle) {
  294. adb_mutex_lock(&usb_lock);
  295. usb_cleanup_handle(handle);
  296. adb_mutex_unlock(&usb_lock);
  297. } else {
  298. SetLastError(ERROR_INVALID_HANDLE);
  299. errno = ERROR_INVALID_HANDLE;
  300. }
  301. }
  302. int usb_close(usb_handle* handle) {
  303. D("usb_close\n");
  304. if (NULL != handle) {
  305. // Remove handle from the list
  306. adb_mutex_lock(&usb_lock);
  307. if ((handle->next != handle) && (handle->prev != handle)) {
  308. handle->next->prev = handle->prev;
  309. handle->prev->next = handle->next;
  310. handle->prev = handle;
  311. handle->next = handle;
  312. }
  313. adb_mutex_unlock(&usb_lock);
  314. // Cleanup handle
  315. usb_cleanup_handle(handle);
  316. free(handle);
  317. }
  318. return 0;
  319. }
  320. const char *usb_name(usb_handle* handle) {
  321. if (NULL == handle) {
  322. SetLastError(ERROR_INVALID_HANDLE);
  323. errno = ERROR_INVALID_HANDLE;
  324. return NULL;
  325. }
  326. return (const char*)handle->interface_name;
  327. }
  328. int recognized_device(usb_handle* handle) {
  329. if (NULL == handle)
  330. return 0;
  331. // Check vendor and product id first
  332. USB_DEVICE_DESCRIPTOR device_desc;
  333. if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
  334. &device_desc)) {
  335. return 0;
  336. }
  337. // Then check interface properties
  338. USB_INTERFACE_DESCRIPTOR interf_desc;
  339. if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
  340. &interf_desc)) {
  341. return 0;
  342. }
  343. // Must have two endpoints
  344. if (2 != interf_desc.bNumEndpoints) {
  345. return 0;
  346. }
  347. if (is_adb_interface(device_desc.idVendor, device_desc.idProduct,
  348. interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) {
  349. if(interf_desc.bInterfaceProtocol == 0x01) {
  350. AdbEndpointInformation endpoint_info;
  351. // assuming zero is a valid bulk endpoint ID
  352. if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) {
  353. handle->zero_mask = endpoint_info.max_packet_size - 1;
  354. }
  355. }
  356. return 1;
  357. }
  358. return 0;
  359. }
  360. void find_devices() {
  361. usb_handle* handle = NULL;
  362. char entry_buffer[2048];
  363. char interf_name[2048];
  364. AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
  365. unsigned long entry_buffer_size = sizeof(entry_buffer);
  366. char* copy_name;
  367. // Enumerate all present and active interfaces.
  368. ADBAPIHANDLE enum_handle =
  369. AdbEnumInterfaces(usb_class_id, true, true, true);
  370. if (NULL == enum_handle)
  371. return;
  372. while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) {
  373. // TODO: FIXME - temp hack converting wchar_t into char.
  374. // It would be better to change AdbNextInterface so it will return
  375. // interface name as single char string.
  376. const wchar_t* wchar_name = next_interface->device_name;
  377. for(copy_name = interf_name;
  378. L'\0' != *wchar_name;
  379. wchar_name++, copy_name++) {
  380. *copy_name = (char)(*wchar_name);
  381. }
  382. *copy_name = '\0';
  383. // Lets see if we already have this device in the list
  384. if (!known_device(interf_name)) {
  385. // This seems to be a new device. Open it!
  386. handle = do_usb_open(next_interface->device_name);
  387. if (NULL != handle) {
  388. // Lets see if this interface (device) belongs to us
  389. if (recognized_device(handle)) {
  390. D("adding a new device %s\n", interf_name);
  391. char serial_number[512];
  392. unsigned long serial_number_len = sizeof(serial_number);
  393. if (AdbGetSerialNumber(handle->adb_interface,
  394. serial_number,
  395. &serial_number_len,
  396. true)) {
  397. // Lets make sure that we don't duplicate this device
  398. if (register_new_device(handle)) {
  399. register_usb_transport(handle, serial_number, NULL, 1);
  400. } else {
  401. D("register_new_device failed for %s\n", interf_name);
  402. usb_cleanup_handle(handle);
  403. free(handle);
  404. }
  405. } else {
  406. D("cannot get serial number\n");
  407. usb_cleanup_handle(handle);
  408. free(handle);
  409. }
  410. } else {
  411. usb_cleanup_handle(handle);
  412. free(handle);
  413. }
  414. }
  415. }
  416. entry_buffer_size = sizeof(entry_buffer);
  417. }
  418. AdbCloseHandle(enum_handle);
  419. }