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
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <sysdeps.h>
22
23#define  TRACE_TAG  TRACE_TRANSPORT
24#include "adb.h"
25
26#if ADB_HOST
27#include "usb_vendors.h"
28#endif
29
30#ifdef HAVE_BIG_ENDIAN
31#define H4(x)	(((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
32static inline void fix_endians(apacket *p)
33{
34    p->msg.command     = H4(p->msg.command);
35    p->msg.arg0        = H4(p->msg.arg0);
36    p->msg.arg1        = H4(p->msg.arg1);
37    p->msg.data_length = H4(p->msg.data_length);
38    p->msg.data_check  = H4(p->msg.data_check);
39    p->msg.magic       = H4(p->msg.magic);
40}
41unsigned host_to_le32(unsigned n)
42{
43    return H4(n);
44}
45#else
46#define fix_endians(p) do {} while (0)
47unsigned host_to_le32(unsigned n)
48{
49    return n;
50}
51#endif
52
53static int remote_read(apacket *p, atransport *t)
54{
55    if(usb_read(t->usb, &p->msg, sizeof(amessage))){
56        D("remote usb: read terminated (message)\n");
57        return -1;
58    }
59
60    fix_endians(p);
61
62    if(check_header(p)) {
63        D("remote usb: check_header failed\n");
64        return -1;
65    }
66
67    if(p->msg.data_length) {
68        if(usb_read(t->usb, p->data, p->msg.data_length)){
69            D("remote usb: terminated (data)\n");
70            return -1;
71        }
72    }
73
74    if(check_data(p)) {
75        D("remote usb: check_data failed\n");
76        return -1;
77    }
78
79    return 0;
80}
81
82static int remote_write(apacket *p, atransport *t)
83{
84    unsigned size = p->msg.data_length;
85
86    fix_endians(p);
87
88    if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
89        D("remote usb: 1 - write terminated\n");
90        return -1;
91    }
92    if(p->msg.data_length == 0) return 0;
93    if(usb_write(t->usb, &p->data, size)) {
94        D("remote usb: 2 - write terminated\n");
95        return -1;
96    }
97
98    return 0;
99}
100
101static void remote_close(atransport *t)
102{
103    usb_close(t->usb);
104    t->usb = 0;
105}
106
107static void remote_kick(atransport *t)
108{
109    usb_kick(t->usb);
110}
111
112void init_usb_transport(atransport *t, usb_handle *h, int state)
113{
114    D("transport: usb\n");
115    t->close = remote_close;
116    t->kick = remote_kick;
117    t->read_from_remote = remote_read;
118    t->write_to_remote = remote_write;
119    t->sync_token = 1;
120    t->connection_state = state;
121    t->type = kTransportUsb;
122    t->usb = h;
123
124#if ADB_HOST
125    HOST = 1;
126#else
127    HOST = 0;
128#endif
129}
130
131#if ADB_HOST
132int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
133{
134    unsigned i;
135    for (i = 0; i < vendorIdCount; i++) {
136        if (vid == vendorIds[i]) {
137            if (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS &&
138                    usb_protocol == ADB_PROTOCOL) {
139                return 1;
140            }
141
142            return 0;
143        }
144    }
145
146    return 0;
147}
148#endif
149