Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 1 | /* |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 17 | #define LOG_TAG "UsbHostManagerJNI" |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include "jni.h" |
| 21 | #include "JNIHelp.h" |
| 22 | #include "android_runtime/AndroidRuntime.h" |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 23 | #include "android_runtime/Log.h" |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 24 | |
| 25 | #include <usbhost/usbhost.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <asm/byteorder.h> |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <sys/ioctl.h> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 33 | |
| 34 | namespace android |
| 35 | { |
| 36 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 37 | static struct parcel_file_descriptor_offsets_t |
| 38 | { |
| 39 | jclass mClass; |
| 40 | jmethodID mConstructor; |
| 41 | } gParcelFileDescriptorOffsets; |
| 42 | |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 43 | static jmethodID method_beginUsbDeviceAdded; |
| 44 | static jmethodID method_addUsbConfiguration; |
| 45 | static jmethodID method_addUsbInterface; |
| 46 | static jmethodID method_addUsbEndpoint; |
| 47 | static jmethodID method_endUsbDeviceAdded; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 48 | static jmethodID method_usbDeviceRemoved; |
| 49 | |
| 50 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 51 | if (env->ExceptionCheck()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 52 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 53 | LOGE_EX(env); |
| 54 | env->ExceptionClear(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static int usb_device_added(const char *devname, void* client_data) { |
| 59 | struct usb_descriptor_header* desc; |
| 60 | struct usb_descriptor_iter iter; |
| 61 | |
| 62 | struct usb_device *device = usb_device_open(devname); |
| 63 | if (!device) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 64 | ALOGE("usb_device_open failed\n"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 69 | jobject thiz = (jobject)client_data; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 70 | const usb_device_descriptor* deviceDesc = usb_device_get_device_descriptor(device); |
| 71 | |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 72 | char *manufacturer = usb_device_get_manufacturer_name(device); |
| 73 | char *product = usb_device_get_product_name(device); |
| 74 | char *serial = usb_device_get_serial(device); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 75 | |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 76 | jstring deviceName = env->NewStringUTF(devname); |
Mike Lockwood | 9ee5e7e | 2014-09-08 16:15:21 -0700 | [diff] [blame] | 77 | jstring manufacturerName = AndroidRuntime::NewStringLatin1(env, manufacturer); |
| 78 | jstring productName = AndroidRuntime::NewStringLatin1(env, product); |
| 79 | jstring serialNumber = AndroidRuntime::NewStringLatin1(env, serial); |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 80 | |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 81 | jboolean result = env->CallBooleanMethod(thiz, method_beginUsbDeviceAdded, |
| 82 | deviceName, usb_device_get_vendor_id(device), usb_device_get_product_id(device), |
| 83 | deviceDesc->bDeviceClass, deviceDesc->bDeviceSubClass, deviceDesc->bDeviceProtocol, |
| 84 | manufacturerName, productName, serialNumber); |
| 85 | |
Robin Cutshaw | 575ca85 | 2012-05-01 19:45:25 -0400 | [diff] [blame] | 86 | env->DeleteLocalRef(serialNumber); |
| 87 | env->DeleteLocalRef(productName); |
| 88 | env->DeleteLocalRef(manufacturerName); |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 89 | env->DeleteLocalRef(deviceName); |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 90 | free(manufacturer); |
| 91 | free(product); |
| 92 | free(serial); |
| 93 | |
| 94 | if (!result) goto fail; |
| 95 | |
| 96 | usb_descriptor_iter_init(device, &iter); |
| 97 | |
| 98 | while ((desc = usb_descriptor_iter_next(&iter)) != NULL) { |
| 99 | if (desc->bDescriptorType == USB_DT_CONFIG) { |
| 100 | struct usb_config_descriptor *config = (struct usb_config_descriptor *)desc; |
| 101 | char *name = usb_device_get_string(device, config->iConfiguration); |
Mike Lockwood | 9ee5e7e | 2014-09-08 16:15:21 -0700 | [diff] [blame] | 102 | jstring configName = AndroidRuntime::NewStringLatin1(env, name); |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 103 | |
| 104 | env->CallVoidMethod(thiz, method_addUsbConfiguration, |
| 105 | config->bConfigurationValue, configName, config->bmAttributes, |
| 106 | config->bMaxPower); |
| 107 | |
| 108 | env->DeleteLocalRef(configName); |
| 109 | free(name); |
| 110 | } else if (desc->bDescriptorType == USB_DT_INTERFACE) { |
| 111 | struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc; |
| 112 | char *name = usb_device_get_string(device, interface->iInterface); |
Mike Lockwood | 9ee5e7e | 2014-09-08 16:15:21 -0700 | [diff] [blame] | 113 | jstring interfaceName = AndroidRuntime::NewStringLatin1(env, name); |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 114 | |
| 115 | env->CallVoidMethod(thiz, method_addUsbInterface, |
| 116 | interface->bInterfaceNumber, interfaceName, interface->bAlternateSetting, |
| 117 | interface->bInterfaceClass, interface->bInterfaceSubClass, |
| 118 | interface->bInterfaceProtocol); |
| 119 | |
| 120 | env->DeleteLocalRef(interfaceName); |
| 121 | free(name); |
| 122 | } else if (desc->bDescriptorType == USB_DT_ENDPOINT) { |
| 123 | struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)desc; |
| 124 | |
| 125 | env->CallVoidMethod(thiz, method_addUsbEndpoint, |
| 126 | endpoint->bEndpointAddress, endpoint->bmAttributes, |
| 127 | __le16_to_cpu(endpoint->wMaxPacketSize), endpoint->bInterval); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | env->CallVoidMethod(thiz, method_endUsbDeviceAdded); |
| 132 | |
| 133 | fail: |
| 134 | usb_device_close(device); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 135 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int usb_device_removed(const char *devname, void* client_data) { |
| 141 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 142 | jobject thiz = (jobject)client_data; |
| 143 | |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 144 | jstring deviceName = env->NewStringUTF(devname); |
Mike Lockwood | b3174c4 | 2011-06-09 14:48:11 -0400 | [diff] [blame] | 145 | env->CallVoidMethod(thiz, method_usbDeviceRemoved, deviceName); |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 146 | env->DeleteLocalRef(deviceName); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 147 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Andreas Gampe | 184e3ed | 2014-09-29 15:04:06 -0700 | [diff] [blame^] | 151 | static void android_server_UsbHostManager_monitorUsbHostBus(JNIEnv* /* env */, jobject thiz) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 152 | { |
| 153 | struct usb_host_context* context = usb_host_init(); |
| 154 | if (!context) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 155 | ALOGE("usb_host_init failed"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | // this will never return so it is safe to pass thiz directly |
| 159 | usb_host_run(context, usb_device_added, usb_device_removed, NULL, (void *)thiz); |
| 160 | } |
| 161 | |
Andreas Gampe | 184e3ed | 2014-09-29 15:04:06 -0700 | [diff] [blame^] | 162 | static jobject android_server_UsbHostManager_openDevice(JNIEnv *env, jobject /* thiz */, |
| 163 | jstring deviceName) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 164 | { |
| 165 | const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL); |
| 166 | struct usb_device* device = usb_device_open(deviceNameStr); |
| 167 | env->ReleaseStringUTFChars(deviceName, deviceNameStr); |
| 168 | |
| 169 | if (!device) |
| 170 | return NULL; |
| 171 | |
| 172 | int fd = usb_device_get_fd(device); |
Henrik Baard | 9bd36ef | 2013-07-09 15:14:23 +0200 | [diff] [blame] | 173 | if (fd < 0) { |
| 174 | usb_device_close(device); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 175 | return NULL; |
Henrik Baard | 9bd36ef | 2013-07-09 15:14:23 +0200 | [diff] [blame] | 176 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 177 | int newFD = dup(fd); |
| 178 | usb_device_close(device); |
| 179 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 180 | jobject fileDescriptor = jniCreateFileDescriptor(env, newFD); |
| 181 | if (fileDescriptor == NULL) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | return env->NewObject(gParcelFileDescriptorOffsets.mClass, |
| 185 | gParcelFileDescriptorOffsets.mConstructor, fileDescriptor); |
| 186 | } |
| 187 | |
| 188 | static JNINativeMethod method_table[] = { |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 189 | { "monitorUsbHostBus", "()V", (void*)android_server_UsbHostManager_monitorUsbHostBus }, |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 190 | { "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 191 | (void*)android_server_UsbHostManager_openDevice }, |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 192 | }; |
| 193 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 194 | int register_android_server_UsbHostManager(JNIEnv *env) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 195 | { |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 196 | jclass clazz = env->FindClass("com/android/server/usb/UsbHostManager"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 197 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 198 | ALOGE("Can't find com/android/server/usb/UsbHostManager"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 199 | return -1; |
| 200 | } |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 201 | method_beginUsbDeviceAdded = env->GetMethodID(clazz, "beginUsbDeviceAdded", |
| 202 | "(Ljava/lang/String;IIIIILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z"); |
| 203 | if (method_beginUsbDeviceAdded == NULL) { |
| 204 | ALOGE("Can't find beginUsbDeviceAdded"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 205 | return -1; |
| 206 | } |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 207 | method_addUsbConfiguration = env->GetMethodID(clazz, "addUsbConfiguration", |
| 208 | "(ILjava/lang/String;II)V"); |
| 209 | if (method_addUsbConfiguration == NULL) { |
| 210 | ALOGE("Can't find addUsbConfiguration"); |
| 211 | return -1; |
| 212 | } |
| 213 | method_addUsbInterface = env->GetMethodID(clazz, "addUsbInterface", |
| 214 | "(ILjava/lang/String;IIII)V"); |
| 215 | if (method_addUsbInterface == NULL) { |
| 216 | ALOGE("Can't find addUsbInterface"); |
| 217 | return -1; |
| 218 | } |
| 219 | method_addUsbEndpoint = env->GetMethodID(clazz, "addUsbEndpoint", "(IIII)V"); |
| 220 | if (method_addUsbEndpoint == NULL) { |
| 221 | ALOGE("Can't find addUsbEndpoint"); |
| 222 | return -1; |
| 223 | } |
| 224 | method_endUsbDeviceAdded = env->GetMethodID(clazz, "endUsbDeviceAdded", "()V"); |
| 225 | if (method_endUsbDeviceAdded == NULL) { |
| 226 | ALOGE("Can't find endUsbDeviceAdded"); |
| 227 | return -1; |
| 228 | } |
| 229 | method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved", |
| 230 | "(Ljava/lang/String;)V"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 231 | if (method_usbDeviceRemoved == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 232 | ALOGE("Can't find usbDeviceRemoved"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 236 | clazz = env->FindClass("android/os/ParcelFileDescriptor"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 237 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor"); |
| 238 | gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
Mike Lockwood | 7531aa2 | 2014-01-13 10:31:01 -0800 | [diff] [blame] | 239 | gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", |
| 240 | "(Ljava/io/FileDescriptor;)V"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 241 | LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL, |
| 242 | "Unable to find constructor for android.os.ParcelFileDescriptor"); |
| 243 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 244 | return jniRegisterNativeMethods(env, "com/android/server/usb/UsbHostManager", |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 245 | method_table, NELEM(method_table)); |
| 246 | } |
| 247 | |
| 248 | }; |