File: adb_winusb_interface.cpp

package info (click to toggle)
android-platform-development 7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 30,092 kB
  • sloc: ansic: 161,291; java: 15,681; cpp: 7,721; xml: 6,419; python: 5,456; sh: 1,748; lisp: 261; ruby: 183; asm: 132; perl: 88; makefile: 22
file content (340 lines) | stat: -rwxr-xr-x 11,611 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** \file
  This file consists of implementation of class AdbWinUsbInterfaceObject
  that encapsulates an interface on our USB device that is accessible
  via WinUsb API.
*/

#include "stdafx.h"
#include "adb_winusb_interface.h"
#include "adb_winusb_endpoint_object.h"

AdbWinUsbInterfaceObject::AdbWinUsbInterfaceObject(const wchar_t* interf_name)
    : AdbInterfaceObject(interf_name),
      usb_device_handle_(INVALID_HANDLE_VALUE),
      winusb_handle_(NULL),
      interface_number_(0xFF),
      def_read_endpoint_(0xFF),
      read_endpoint_id_(0xFF),
      def_write_endpoint_(0xFF),
      write_endpoint_id_(0xFF) {
}

AdbWinUsbInterfaceObject::~AdbWinUsbInterfaceObject() {
  ATLASSERT(NULL == winusb_handle_);
  ATLASSERT(INVALID_HANDLE_VALUE == usb_device_handle_);
}

LONG AdbWinUsbInterfaceObject::Release() {
  ATLASSERT(ref_count_ > 0);
  LONG ret = InterlockedDecrement(&ref_count_);
  ATLASSERT(ret >= 0);
  if (0 == ret) {
    LastReferenceReleased();
    delete this;
  }
  return ret;
}

ADBAPIHANDLE AdbWinUsbInterfaceObject::CreateHandle() {
  // Open USB device for this inteface Note that WinUsb API
  // requires the handle to be opened for overlapped I/O.
  usb_device_handle_ = CreateFile(interface_name().c_str(),
                                  GENERIC_READ | GENERIC_WRITE,
                                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                                  NULL, OPEN_EXISTING,
                                  FILE_FLAG_OVERLAPPED, NULL);
  if (INVALID_HANDLE_VALUE == usb_device_handle_)
    return NULL;

  // Initialize WinUSB API for this interface
  if (!WinUsb_Initialize(usb_device_handle_, &winusb_handle_))
    return NULL;

  // Cache current interface number that will be used in
  // WinUsb_Xxx calls performed on this interface.
  if (!WinUsb_GetCurrentAlternateSetting(winusb_handle(), &interface_number_))
    return false;

  // Cache interface properties
  unsigned long bytes_written;

  // Cache USB device descriptor
  if (!WinUsb_GetDescriptor(winusb_handle(), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0,
                            reinterpret_cast<PUCHAR>(&usb_device_descriptor_),
                            sizeof(usb_device_descriptor_), &bytes_written)) {
    return false;
  }

  // Cache USB configuration descriptor
  if (!WinUsb_GetDescriptor(winusb_handle(), USB_CONFIGURATION_DESCRIPTOR_TYPE,
                            0, 0,
                            reinterpret_cast<PUCHAR>(&usb_config_descriptor_),
                            sizeof(usb_config_descriptor_), &bytes_written)) {
    return false;
  }

  // Cache USB interface descriptor
  if (!WinUsb_QueryInterfaceSettings(winusb_handle(), interface_number(),
                                     &usb_interface_descriptor_)) {
    return false;
  }

  // Save indexes and IDs for bulk read / write endpoints. We will use them to
  // convert ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX and
  // ADB_QUERY_BULK_READ_ENDPOINT_INDEX into actual endpoint indexes and IDs.
  for (UCHAR endpoint = 0; endpoint < usb_interface_descriptor_.bNumEndpoints;
       endpoint++) {
    // Get endpoint information
    WINUSB_PIPE_INFORMATION pipe_info;
    if (!WinUsb_QueryPipe(winusb_handle(), interface_number(), endpoint,
                          &pipe_info)) {
      return false;
    }

    if (UsbdPipeTypeBulk == pipe_info.PipeType) {
      // This is a bulk endpoint. Cache its index and ID.
      if (0 != (pipe_info.PipeId & USB_ENDPOINT_DIRECTION_MASK)) {
        // Use this endpoint as default bulk read endpoint
        ATLASSERT(0xFF == def_read_endpoint_);
        def_read_endpoint_ = endpoint;
        read_endpoint_id_ = pipe_info.PipeId;
      } else {
        // Use this endpoint as default bulk write endpoint
        ATLASSERT(0xFF == def_write_endpoint_);
        def_write_endpoint_ = endpoint;
        write_endpoint_id_ = pipe_info.PipeId;
      }
    }
  }

  return AdbInterfaceObject::CreateHandle();
}

bool AdbWinUsbInterfaceObject::CloseHandle() {
  if (NULL != winusb_handle_) {
    WinUsb_Free(winusb_handle_);
    winusb_handle_ = NULL;
  }
  if (INVALID_HANDLE_VALUE != usb_device_handle_) {
    ::CloseHandle(usb_device_handle_);
    usb_device_handle_ = INVALID_HANDLE_VALUE;
  }

  return AdbInterfaceObject::CloseHandle();
}

bool AdbWinUsbInterfaceObject::GetSerialNumber(void* buffer,
                                               unsigned long* buffer_char_size,
                                               bool ansi) {
  if (!IsOpened()) {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }

  if (NULL == buffer_char_size) {
    SetLastError(ERROR_INVALID_PARAMETER);
    return false;
  }

  // Calculate serial number string size. Note that WinUsb_GetDescriptor
  // API will not return number of bytes needed to store serial number
  // string. So we will have to start with a reasonably large preallocated
  // buffer and then loop through WinUsb_GetDescriptor calls, doubling up
  // string buffer size every time ERROR_INSUFFICIENT_BUFFER is returned.
  union {
    // Preallocate reasonably sized buffer on the stack.
    char small_buffer[64];
    USB_STRING_DESCRIPTOR initial_ser_num;
  };
  USB_STRING_DESCRIPTOR* ser_num = &initial_ser_num;
  // Buffer byte size
  unsigned long ser_num_size = sizeof(small_buffer);
  // After successful call to WinUsb_GetDescriptor will contain serial
  // number descriptor size.
  unsigned long bytes_written;
  while (!WinUsb_GetDescriptor(winusb_handle(), USB_STRING_DESCRIPTOR_TYPE,
                               usb_device_descriptor_.iSerialNumber,
                               0x0409, // English (US)
                               reinterpret_cast<PUCHAR>(ser_num),
                               ser_num_size, &bytes_written)) {
    // Any error other than ERROR_INSUFFICIENT_BUFFER is terminal here.
    if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) {
      if (ser_num != &initial_ser_num)
        delete[] reinterpret_cast<char*>(ser_num);
      return false;
    }

    // Double up buffer size and reallocate string buffer
    ser_num_size *= 2;
    if (ser_num != &initial_ser_num)
      delete[] reinterpret_cast<char*>(ser_num);
    try {
      ser_num =
          reinterpret_cast<USB_STRING_DESCRIPTOR*>(new char[ser_num_size]);
    } catch (...) {
      SetLastError(ERROR_OUTOFMEMORY);
      return false;
    }
  }

  // Serial number string length
  unsigned long str_len = (ser_num->bLength -
                           FIELD_OFFSET(USB_STRING_DESCRIPTOR, bString)) /
                          sizeof(wchar_t);

  // Lets see if requested buffer is big enough to fit the string
  if ((NULL == buffer) || (*buffer_char_size < (str_len + 1))) {
    // Requested buffer is too small.
    if (ser_num != &initial_ser_num)
      delete[] reinterpret_cast<char*>(ser_num);
    *buffer_char_size = str_len + 1;
    SetLastError(ERROR_INSUFFICIENT_BUFFER);
    return false;
  }

  bool ret = true;
  if (ansi) {
    // We need to convert name from wide char to ansi string
    if (0 != WideCharToMultiByte(CP_ACP, 0, ser_num->bString,
                                 static_cast<int>(str_len),
                                 reinterpret_cast<PSTR>(buffer),
                                 static_cast<int>(*buffer_char_size),
                                 NULL, NULL)) {
      // Zero-terminate output string.
      reinterpret_cast<char*>(buffer)[str_len] = '\0';
    } else {
      ret = false;
    }
  } else {
    // For wide char output just copy string buffer,
    // and zero-terminate output string.
    CopyMemory(buffer, ser_num->bString, bytes_written);
    reinterpret_cast<wchar_t*>(buffer)[str_len] = L'\0';
  }

  if (ser_num != &initial_ser_num)
    delete[] reinterpret_cast<char*>(ser_num);

  return ret;
}

bool AdbWinUsbInterfaceObject::GetEndpointInformation(
    UCHAR endpoint_index,
    AdbEndpointInformation* info) {
  if (!IsOpened()) {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }

  if (NULL == info) {
    SetLastError(ERROR_INVALID_PARAMETER);
    return false;
  }

  // Get actual endpoint index for predefined read / write endpoints.
  if (ADB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) {
    endpoint_index = def_read_endpoint_;
  } else if (ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) {
    endpoint_index = def_write_endpoint_;
  }

  // Query endpoint information
  WINUSB_PIPE_INFORMATION pipe_info;
  if (!WinUsb_QueryPipe(winusb_handle(), interface_number(), endpoint_index,
                        &pipe_info)) {
    return false;
  }

  // Save endpoint information into output.
  info->max_packet_size = pipe_info.MaximumPacketSize;
  info->max_transfer_size = 0xFFFFFFFF;
  info->endpoint_address = pipe_info.PipeId;
  info->polling_interval = pipe_info.Interval;
  info->setting_index = interface_number();
  switch (pipe_info.PipeType) {
    case UsbdPipeTypeControl:
      info->endpoint_type = AdbEndpointTypeControl;
      break;

    case UsbdPipeTypeIsochronous:
      info->endpoint_type = AdbEndpointTypeIsochronous;
      break;

    case UsbdPipeTypeBulk:
      info->endpoint_type = AdbEndpointTypeBulk;
      break;

    case UsbdPipeTypeInterrupt:
      info->endpoint_type = AdbEndpointTypeInterrupt;
      break;

    default:
      info->endpoint_type = AdbEndpointTypeInvalid;
      break;
  }

  return true;
}

ADBAPIHANDLE AdbWinUsbInterfaceObject::OpenEndpoint(
    UCHAR endpoint_index,
    AdbOpenAccessType access_type,
    AdbOpenSharingMode sharing_mode) {
  // Convert index into id
  UCHAR endpoint_id;

  if ((ADB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) ||
      (def_read_endpoint_ == endpoint_index)) {
    endpoint_id = read_endpoint_id_;
    endpoint_index = def_read_endpoint_;
  } else if ((ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) ||
             (def_write_endpoint_ == endpoint_index)) {
    endpoint_id = write_endpoint_id_;
    endpoint_index = def_write_endpoint_;
  } else {
    SetLastError(ERROR_INVALID_PARAMETER);
    return false;
  }

  return OpenEndpoint(endpoint_id, endpoint_index);
}

ADBAPIHANDLE AdbWinUsbInterfaceObject::OpenEndpoint(UCHAR endpoint_id,
                                                    UCHAR endpoint_index) {
  if (!IsOpened()) {
    SetLastError(ERROR_INVALID_HANDLE);
    return false;
  }

  AdbEndpointObject* adb_endpoint = NULL;
  
  try {
    adb_endpoint =
        new AdbWinUsbEndpointObject(this, endpoint_id, endpoint_index);
  } catch (...) {
    SetLastError(ERROR_OUTOFMEMORY);
    return NULL;
  }

  ADBAPIHANDLE ret = adb_endpoint->CreateHandle();

  adb_endpoint->Release();

  return ret;
}