transport_usb.c 3.3 KB

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