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