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
17#include "usb_vendors.h"
18
19#include <stdio.h>
20#include <stdlib.h>
21
22#ifdef _WIN32
23#  ifndef WIN32_LEAN_AND_MEAN
24#    define WIN32_LEAN_AND_MEAN
25#  endif
26#  include "windows.h"
27#  include "shlobj.h"
28#else
29#  include <unistd.h>
30#  include <sys/stat.h>
31#endif
32
33#include "sysdeps.h"
34#include "adb.h"
35
36#define ANDROID_PATH            ".android"
37#define ANDROID_ADB_INI         "adb_usb.ini"
38
39#define TRACE_TAG               TRACE_USB
40
41/* Keep the list below sorted alphabetically by #define name */
42// Acer's USB Vendor ID
43#define VENDOR_ID_ACER          0x0502
44// Allwinner's USB Vendor ID
45#define VENDOR_ID_ALLWINNER     0x1F3A
46// Amlogic's USB Vendor ID
47#define VENDOR_ID_AMLOGIC       0x1b8e
48// AnyDATA's USB Vendor ID
49#define VENDOR_ID_ANYDATA       0x16D5
50// Archos's USB Vendor ID
51#define VENDOR_ID_ARCHOS        0x0E79
52// Asus's USB Vendor ID
53#define VENDOR_ID_ASUS          0x0b05
54// BYD's USB Vendor ID
55#define VENDOR_ID_BYD           0x1D91
56// Compal's USB Vendor ID
57#define VENDOR_ID_COMPAL        0x04B7
58// Compalcomm's USB Vendor ID
59#define VENDOR_ID_COMPALCOMM    0x1219
60// Dell's USB Vendor ID
61#define VENDOR_ID_DELL          0x413c
62// ECS's USB Vendor ID
63#define VENDOR_ID_ECS           0x03fc
64// EMERGING_TECH's USB Vendor ID
65#define VENDOR_ID_EMERGING_TECH 0x297F
66// Emerson's USB Vendor ID
67#define VENDOR_ID_EMERSON       0x2207
68// Foxconn's USB Vendor ID
69#define VENDOR_ID_FOXCONN       0x0489
70// Fujitsu's USB Vendor ID
71#define VENDOR_ID_FUJITSU       0x04C5
72// Funai's USB Vendor ID
73#define VENDOR_ID_FUNAI         0x0F1C
74// Garmin-Asus's USB Vendor ID
75#define VENDOR_ID_GARMIN_ASUS   0x091E
76// Gigabyte's USB Vendor ID
77#define VENDOR_ID_GIGABYTE      0x0414
78// Gigaset's USB Vendor ID
79#define VENDOR_ID_GIGASET       0x1E85
80// GIONEE's USB Vendor ID
81#define VENDOR_ID_GIONEE        0x271D
82// Google's USB Vendor ID
83#define VENDOR_ID_GOOGLE        0x18d1
84// Haier's USB Vendor ID
85#define VENDOR_ID_HAIER         0x201E
86// Harris's USB Vendor ID
87#define VENDOR_ID_HARRIS        0x19A5
88// Hisense's USB Vendor ID
89#define VENDOR_ID_HISENSE       0x109b
90// Honeywell's USB Vendor ID
91#define VENDOR_ID_HONEYWELL     0x0c2e
92// HP's USB Vendor ID
93#define VENDOR_ID_HP            0x03f0
94// HTC's USB Vendor ID
95#define VENDOR_ID_HTC           0x0bb4
96// Huawei's USB Vendor ID
97#define VENDOR_ID_HUAWEI        0x12D1
98// INQ Mobile's USB Vendor ID
99#define VENDOR_ID_INQ_MOBILE    0x2314
100// Intel's USB Vendor ID
101#define VENDOR_ID_INTEL         0x8087
102// Intermec's USB Vendor ID
103#define VENDOR_ID_INTERMEC      0x067e
104// IRiver's USB Vendor ID
105#define VENDOR_ID_IRIVER        0x2420
106// K-Touch's USB Vendor ID
107#define VENDOR_ID_K_TOUCH       0x24E3
108// KT Tech's USB Vendor ID
109#define VENDOR_ID_KT_TECH       0x2116
110// Kobo's USB Vendor ID
111#define VENDOR_ID_KOBO          0x2237
112// Kyocera's USB Vendor ID
113#define VENDOR_ID_KYOCERA       0x0482
114// Lab126's USB Vendor ID
115#define VENDOR_ID_LAB126        0x1949
116// Lenovo's USB Vendor ID
117#define VENDOR_ID_LENOVO        0x17EF
118// LenovoMobile's USB Vendor ID
119#define VENDOR_ID_LENOVOMOBILE  0x2006
120// LG's USB Vendor ID
121#define VENDOR_ID_LGE           0x1004
122// Lumigon's USB Vendor ID
123#define VENDOR_ID_LUMIGON       0x25E3
124// Motorola's USB Vendor ID
125#define VENDOR_ID_MOTOROLA      0x22b8
126// MSI's USB Vendor ID
127#define VENDOR_ID_MSI           0x0DB0
128// MTK's USB Vendor ID
129#define VENDOR_ID_MTK           0x0e8d
130// NEC's USB Vendor ID
131#define VENDOR_ID_NEC           0x0409
132// B&N Nook's USB Vendor ID
133#define VENDOR_ID_NOOK          0x2080
134// Nvidia's USB Vendor ID
135#define VENDOR_ID_NVIDIA        0x0955
136// OPPO's USB Vendor ID
137#define VENDOR_ID_OPPO          0x22D9
138// On-The-Go-Video's USB Vendor ID
139#define VENDOR_ID_OTGV          0x2257
140// OUYA's USB Vendor ID
141#define VENDOR_ID_OUYA          0x2836
142// Pantech's USB Vendor ID
143#define VENDOR_ID_PANTECH       0x10A9
144// Pegatron's USB Vendor ID
145#define VENDOR_ID_PEGATRON      0x1D4D
146// Philips's USB Vendor ID
147#define VENDOR_ID_PHILIPS       0x0471
148// Panasonic Mobile Communication's USB Vendor ID
149#define VENDOR_ID_PMC           0x04DA
150// Positivo's USB Vendor ID
151#define VENDOR_ID_POSITIVO      0x1662
152// Prestigio's USB Vendor ID
153#define VENDOR_ID_PRESTIGIO     0x29e4
154// Qisda's USB Vendor ID
155#define VENDOR_ID_QISDA         0x1D45
156// Qualcomm's USB Vendor ID
157#define VENDOR_ID_QUALCOMM      0x05c6
158// Quanta's USB Vendor ID
159#define VENDOR_ID_QUANTA        0x0408
160// Rockchip's USB Vendor ID
161#define VENDOR_ID_ROCKCHIP      0x2207
162// Samsung's USB Vendor ID
163#define VENDOR_ID_SAMSUNG       0x04e8
164// Sharp's USB Vendor ID
165#define VENDOR_ID_SHARP         0x04dd
166// SK Telesys's USB Vendor ID
167#define VENDOR_ID_SK_TELESYS    0x1F53
168// Smartisan's USB Vendor ID
169#define VENDOR_ID_SMARTISAN     0x29a9
170// Sony's USB Vendor ID
171#define VENDOR_ID_SONY          0x054C
172// Sony Ericsson's USB Vendor ID
173#define VENDOR_ID_SONY_ERICSSON 0x0FCE
174// T & A Mobile Phones' USB Vendor ID
175#define VENDOR_ID_T_AND_A       0x1BBB
176// TechFaith's USB Vendor ID
177#define VENDOR_ID_TECHFAITH     0x1d09
178// Teleepoch's USB Vendor ID
179#define VENDOR_ID_TELEEPOCH     0x2340
180// Texas Instruments's USB Vendor ID
181#define VENDOR_ID_TI            0x0451
182// Toshiba's USB Vendor ID
183#define VENDOR_ID_TOSHIBA       0x0930
184// Unowhy's USB Vendor ID
185#define VENDOR_ID_UNOWHY        0x2A49
186// Vizio's USB Vendor ID
187#define VENDOR_ID_VIZIO         0xE040
188// Wacom's USB Vendor ID
189#define VENDOR_ID_WACOM         0x0531
190// Xiaomi's USB Vendor ID
191#define VENDOR_ID_XIAOMI        0x2717
192// YotaDevices's USB Vendor ID
193#define VENDOR_ID_YOTADEVICES   0x2916
194// Yulong Coolpad's USB Vendor ID
195#define VENDOR_ID_YULONG_COOLPAD 0x1EBF
196// ZTE's USB Vendor ID
197#define VENDOR_ID_ZTE           0x19D2
198/* Keep the list above sorted alphabetically by #define name */
199
200/** built-in vendor list */
201/* Keep the list below sorted alphabetically */
202int builtInVendorIds[] = {
203    VENDOR_ID_ACER,
204    VENDOR_ID_ALLWINNER,
205    VENDOR_ID_AMLOGIC,
206    VENDOR_ID_ANYDATA,
207    VENDOR_ID_ARCHOS,
208    VENDOR_ID_ASUS,
209    VENDOR_ID_BYD,
210    VENDOR_ID_COMPAL,
211    VENDOR_ID_COMPALCOMM,
212    VENDOR_ID_DELL,
213    VENDOR_ID_ECS,
214    VENDOR_ID_EMERGING_TECH,
215    VENDOR_ID_EMERSON,
216    VENDOR_ID_FOXCONN,
217    VENDOR_ID_FUJITSU,
218    VENDOR_ID_FUNAI,
219    VENDOR_ID_GARMIN_ASUS,
220    VENDOR_ID_GIGABYTE,
221    VENDOR_ID_GIGASET,
222    VENDOR_ID_GIONEE,
223    VENDOR_ID_GOOGLE,
224    VENDOR_ID_HAIER,
225    VENDOR_ID_HARRIS,
226    VENDOR_ID_HISENSE,
227    VENDOR_ID_HONEYWELL,
228    VENDOR_ID_HP,
229    VENDOR_ID_HTC,
230    VENDOR_ID_HUAWEI,
231    VENDOR_ID_INQ_MOBILE,
232    VENDOR_ID_INTEL,
233    VENDOR_ID_INTERMEC,
234    VENDOR_ID_IRIVER,
235    VENDOR_ID_KOBO,
236    VENDOR_ID_K_TOUCH,
237    VENDOR_ID_KT_TECH,
238    VENDOR_ID_KYOCERA,
239    VENDOR_ID_LAB126,
240    VENDOR_ID_LENOVO,
241    VENDOR_ID_LENOVOMOBILE,
242    VENDOR_ID_LGE,
243    VENDOR_ID_LUMIGON,
244    VENDOR_ID_MOTOROLA,
245    VENDOR_ID_MSI,
246    VENDOR_ID_MTK,
247    VENDOR_ID_NEC,
248    VENDOR_ID_NOOK,
249    VENDOR_ID_NVIDIA,
250    VENDOR_ID_OPPO,
251    VENDOR_ID_OTGV,
252    VENDOR_ID_OUYA,
253    VENDOR_ID_PANTECH,
254    VENDOR_ID_PEGATRON,
255    VENDOR_ID_PHILIPS,
256    VENDOR_ID_PMC,
257    VENDOR_ID_POSITIVO,
258    VENDOR_ID_PRESTIGIO,
259    VENDOR_ID_QISDA,
260    VENDOR_ID_QUALCOMM,
261    VENDOR_ID_QUANTA,
262    VENDOR_ID_ROCKCHIP,
263    VENDOR_ID_SAMSUNG,
264    VENDOR_ID_SHARP,
265    VENDOR_ID_SK_TELESYS,
266    VENDOR_ID_SMARTISAN,
267    VENDOR_ID_SONY,
268    VENDOR_ID_SONY_ERICSSON,
269    VENDOR_ID_T_AND_A,
270    VENDOR_ID_TECHFAITH,
271    VENDOR_ID_TELEEPOCH,
272    VENDOR_ID_TI,
273    VENDOR_ID_TOSHIBA,
274    VENDOR_ID_UNOWHY,
275    VENDOR_ID_VIZIO,
276    VENDOR_ID_WACOM,
277    VENDOR_ID_XIAOMI,
278    VENDOR_ID_YOTADEVICES,
279    VENDOR_ID_YULONG_COOLPAD,
280    VENDOR_ID_ZTE,
281};
282/* Keep the list above sorted alphabetically */
283
284#define BUILT_IN_VENDOR_COUNT    (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
285
286/* max number of supported vendor ids (built-in + 3rd party). increase as needed */
287#define VENDOR_COUNT_MAX         128
288
289int vendorIds[VENDOR_COUNT_MAX];
290unsigned vendorIdCount = 0;
291
292int get_adb_usb_ini(char* buff, size_t len);
293
294void usb_vendors_init(void)
295{
296    if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
297        fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
298        exit(2);
299    }
300
301    /* add the built-in vendors at the beginning of the array */
302    memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
303
304    /* default array size is the number of built-in vendors */
305    vendorIdCount = BUILT_IN_VENDOR_COUNT;
306
307    if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
308        return;
309
310    char temp[PATH_MAX];
311    if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
312        FILE * f = fopen(temp, "rt");
313
314        if (f != NULL) {
315            /* The vendor id file is pretty basic. 1 vendor id per line.
316               Lines starting with # are comments */
317            while (fgets(temp, sizeof(temp), f) != NULL) {
318                if (temp[0] == '#')
319                    continue;
320
321                long value = strtol(temp, NULL, 0);
322                if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
323                    fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
324                    exit(2);
325                }
326
327                vendorIds[vendorIdCount++] = (int)value;
328
329                /* make sure we don't go beyond the array */
330                if (vendorIdCount == VENDOR_COUNT_MAX) {
331                    break;
332                }
333            }
334            fclose(f);
335        }
336    }
337}
338
339/* Utils methods */
340
341/* builds the path to the adb vendor id file. returns 0 if success */
342int build_path(char* buff, size_t len, const char* format, const char* home)
343{
344    if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= (signed)len) {
345        return 1;
346    }
347
348    return 0;
349}
350
351/* fills buff with the path to the adb vendor id file. returns 0 if success */
352int get_adb_usb_ini(char* buff, size_t len)
353{
354#ifdef _WIN32
355    const char* home = getenv("ANDROID_SDK_HOME");
356    if (home != NULL) {
357        return build_path(buff, len, "%s\\%s\\%s", home);
358    } else {
359        char path[MAX_PATH];
360        SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
361        return build_path(buff, len, "%s\\%s\\%s", path);
362    }
363#else
364    const char* home = getenv("HOME");
365    if (home == NULL)
366        home = "/tmp";
367
368    return build_path(buff, len, "%s/%s/%s", home);
369#endif
370}
371