usb_vendors.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2009 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 "usb_vendors.h"
  17. #include <stdio.h>
  18. #ifdef _WIN32
  19. # define WIN32_LEAN_AND_MEAN
  20. # include "windows.h"
  21. # include "shlobj.h"
  22. #else
  23. # include <unistd.h>
  24. # include <sys/stat.h>
  25. #endif
  26. #include "sysdeps.h"
  27. #include "adb.h"
  28. #define ANDROID_PATH ".android"
  29. #define ANDROID_ADB_INI "adb_usb.ini"
  30. #define TRACE_TAG TRACE_USB
  31. // Google's USB Vendor ID
  32. #define VENDOR_ID_GOOGLE 0x18d1
  33. // Intel's USB Vendor ID
  34. #define VENDOR_ID_INTEL 0x8087
  35. // HTC's USB Vendor ID
  36. #define VENDOR_ID_HTC 0x0bb4
  37. // Samsung's USB Vendor ID
  38. #define VENDOR_ID_SAMSUNG 0x04e8
  39. // Motorola's USB Vendor ID
  40. #define VENDOR_ID_MOTOROLA 0x22b8
  41. // LG's USB Vendor ID
  42. #define VENDOR_ID_LGE 0x1004
  43. // Huawei's USB Vendor ID
  44. #define VENDOR_ID_HUAWEI 0x12D1
  45. // Acer's USB Vendor ID
  46. #define VENDOR_ID_ACER 0x0502
  47. // Sony Ericsson's USB Vendor ID
  48. #define VENDOR_ID_SONY_ERICSSON 0x0FCE
  49. // Foxconn's USB Vendor ID
  50. #define VENDOR_ID_FOXCONN 0x0489
  51. // Dell's USB Vendor ID
  52. #define VENDOR_ID_DELL 0x413c
  53. // Nvidia's USB Vendor ID
  54. #define VENDOR_ID_NVIDIA 0x0955
  55. // Garmin-Asus's USB Vendor ID
  56. #define VENDOR_ID_GARMIN_ASUS 0x091E
  57. // Sharp's USB Vendor ID
  58. #define VENDOR_ID_SHARP 0x04dd
  59. // ZTE's USB Vendor ID
  60. #define VENDOR_ID_ZTE 0x19D2
  61. // Kyocera's USB Vendor ID
  62. #define VENDOR_ID_KYOCERA 0x0482
  63. // Pantech's USB Vendor ID
  64. #define VENDOR_ID_PANTECH 0x10A9
  65. // Qualcomm's USB Vendor ID
  66. #define VENDOR_ID_QUALCOMM 0x05c6
  67. // On-The-Go-Video's USB Vendor ID
  68. #define VENDOR_ID_OTGV 0x2257
  69. // NEC's USB Vendor ID
  70. #define VENDOR_ID_NEC 0x0409
  71. // Panasonic Mobile Communication's USB Vendor ID
  72. #define VENDOR_ID_PMC 0x04DA
  73. // Toshiba's USB Vendor ID
  74. #define VENDOR_ID_TOSHIBA 0x0930
  75. // SK Telesys's USB Vendor ID
  76. #define VENDOR_ID_SK_TELESYS 0x1F53
  77. // KT Tech's USB Vendor ID
  78. #define VENDOR_ID_KT_TECH 0x2116
  79. // Asus's USB Vendor ID
  80. #define VENDOR_ID_ASUS 0x0b05
  81. // Philips's USB Vendor ID
  82. #define VENDOR_ID_PHILIPS 0x0471
  83. // Texas Instruments's USB Vendor ID
  84. #define VENDOR_ID_TI 0x0451
  85. // Funai's USB Vendor ID
  86. #define VENDOR_ID_FUNAI 0x0F1C
  87. // Gigabyte's USB Vendor ID
  88. #define VENDOR_ID_GIGABYTE 0x0414
  89. // IRiver's USB Vendor ID
  90. #define VENDOR_ID_IRIVER 0x2420
  91. // Compal's USB Vendor ID
  92. #define VENDOR_ID_COMPAL 0x1219
  93. // T & A Mobile Phones' USB Vendor ID
  94. #define VENDOR_ID_T_AND_A 0x1BBB
  95. // LenovoMobile's USB Vendor ID
  96. #define VENDOR_ID_LENOVOMOBILE 0x2006
  97. // Lenovo's USB Vendor ID
  98. #define VENDOR_ID_LENOVO 0x17EF
  99. // Vizio's USB Vendor ID
  100. #define VENDOR_ID_VIZIO 0xE040
  101. // K-Touch's USB Vendor ID
  102. #define VENDOR_ID_K_TOUCH 0x24E3
  103. // Pegatron's USB Vendor ID
  104. #define VENDOR_ID_PEGATRON 0x1D4D
  105. // Archos's USB Vendor ID
  106. #define VENDOR_ID_ARCHOS 0x0E79
  107. // Positivo's USB Vendor ID
  108. #define VENDOR_ID_POSITIVO 0x1662
  109. // Fujitsu's USB Vendor ID
  110. #define VENDOR_ID_FUJITSU 0x04C5
  111. // Lumigon's USB Vendor ID
  112. #define VENDOR_ID_LUMIGON 0x25E3
  113. // Quanta's USB Vendor ID
  114. #define VENDOR_ID_QUANTA 0x0408
  115. // INQ Mobile's USB Vendor ID
  116. #define VENDOR_ID_INQ_MOBILE 0x2314
  117. // Sony's USB Vendor ID
  118. #define VENDOR_ID_SONY 0x054C
  119. // Lab126's USB Vendor ID
  120. #define VENDOR_ID_LAB126 0x1949
  121. // Yulong Coolpad's USB Vendor ID
  122. #define VENDOR_ID_YULONG_COOLPAD 0x1EBF
  123. // Kobo's USB Vendor ID
  124. #define VENDOR_ID_KOBO 0x2237
  125. // Teleepoch's USB Vendor ID
  126. #define VENDOR_ID_TELEEPOCH 0x2340
  127. /** built-in vendor list */
  128. int builtInVendorIds[] = {
  129. VENDOR_ID_GOOGLE,
  130. VENDOR_ID_INTEL,
  131. VENDOR_ID_HTC,
  132. VENDOR_ID_SAMSUNG,
  133. VENDOR_ID_MOTOROLA,
  134. VENDOR_ID_LGE,
  135. VENDOR_ID_HUAWEI,
  136. VENDOR_ID_ACER,
  137. VENDOR_ID_SONY_ERICSSON,
  138. VENDOR_ID_FOXCONN,
  139. VENDOR_ID_DELL,
  140. VENDOR_ID_NVIDIA,
  141. VENDOR_ID_GARMIN_ASUS,
  142. VENDOR_ID_SHARP,
  143. VENDOR_ID_ZTE,
  144. VENDOR_ID_KYOCERA,
  145. VENDOR_ID_PANTECH,
  146. VENDOR_ID_QUALCOMM,
  147. VENDOR_ID_OTGV,
  148. VENDOR_ID_NEC,
  149. VENDOR_ID_PMC,
  150. VENDOR_ID_TOSHIBA,
  151. VENDOR_ID_SK_TELESYS,
  152. VENDOR_ID_KT_TECH,
  153. VENDOR_ID_ASUS,
  154. VENDOR_ID_PHILIPS,
  155. VENDOR_ID_TI,
  156. VENDOR_ID_FUNAI,
  157. VENDOR_ID_GIGABYTE,
  158. VENDOR_ID_IRIVER,
  159. VENDOR_ID_COMPAL,
  160. VENDOR_ID_T_AND_A,
  161. VENDOR_ID_LENOVOMOBILE,
  162. VENDOR_ID_LENOVO,
  163. VENDOR_ID_VIZIO,
  164. VENDOR_ID_K_TOUCH,
  165. VENDOR_ID_PEGATRON,
  166. VENDOR_ID_ARCHOS,
  167. VENDOR_ID_POSITIVO,
  168. VENDOR_ID_FUJITSU,
  169. VENDOR_ID_LUMIGON,
  170. VENDOR_ID_QUANTA,
  171. VENDOR_ID_INQ_MOBILE,
  172. VENDOR_ID_SONY,
  173. VENDOR_ID_LAB126,
  174. VENDOR_ID_YULONG_COOLPAD,
  175. VENDOR_ID_KOBO,
  176. VENDOR_ID_TELEEPOCH,
  177. };
  178. #define BUILT_IN_VENDOR_COUNT (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
  179. /* max number of supported vendor ids (built-in + 3rd party). increase as needed */
  180. #define VENDOR_COUNT_MAX 128
  181. int vendorIds[VENDOR_COUNT_MAX];
  182. unsigned vendorIdCount = 0;
  183. int get_adb_usb_ini(char* buff, size_t len);
  184. void usb_vendors_init(void)
  185. {
  186. if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
  187. fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
  188. exit(2);
  189. }
  190. /* add the built-in vendors at the beginning of the array */
  191. memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
  192. /* default array size is the number of built-in vendors */
  193. vendorIdCount = BUILT_IN_VENDOR_COUNT;
  194. if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
  195. return;
  196. char temp[PATH_MAX];
  197. if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
  198. FILE * f = fopen(temp, "rt");
  199. if (f != NULL) {
  200. /* The vendor id file is pretty basic. 1 vendor id per line.
  201. Lines starting with # are comments */
  202. while (fgets(temp, sizeof(temp), f) != NULL) {
  203. if (temp[0] == '#')
  204. continue;
  205. long value = strtol(temp, NULL, 0);
  206. if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
  207. fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
  208. exit(2);
  209. }
  210. vendorIds[vendorIdCount++] = (int)value;
  211. /* make sure we don't go beyond the array */
  212. if (vendorIdCount == VENDOR_COUNT_MAX) {
  213. break;
  214. }
  215. }
  216. fclose(f);
  217. }
  218. }
  219. }
  220. /* Utils methods */
  221. /* builds the path to the adb vendor id file. returns 0 if success */
  222. int build_path(char* buff, size_t len, const char* format, const char* home)
  223. {
  224. if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= (signed)len) {
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. /* fills buff with the path to the adb vendor id file. returns 0 if success */
  230. int get_adb_usb_ini(char* buff, size_t len)
  231. {
  232. #ifdef _WIN32
  233. const char* home = getenv("ANDROID_SDK_HOME");
  234. if (home != NULL) {
  235. return build_path(buff, len, "%s\\%s\\%s", home);
  236. } else {
  237. char path[MAX_PATH];
  238. SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
  239. return build_path(buff, len, "%s\\%s\\%s", path);
  240. }
  241. #else
  242. const char* home = getenv("HOME");
  243. if (home == NULL)
  244. home = "/tmp";
  245. return build_path(buff, len, "%s/%s/%s", home);
  246. #endif
  247. }