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