static status_t usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length) { device_lun *lun = (device_lun *)cookie; disk_device *device = lun->device; mutex_lock(&device->lock); if (device->removed) { mutex_unlock(&device->lock); return B_DEV_NOT_READY; } status_t result = B_DEV_INVALID_IOCTL; switch (op) { case B_GET_DEVICE_SIZE: { if (lun->media_changed) { result = usb_disk_update_capacity(lun); if (result != B_OK) break; } size_t size = lun->block_size * lun->block_count; result = user_memcpy(buffer, &size, sizeof(size)); break; } case B_GET_MEDIA_STATUS: { *(status_t *)buffer = usb_disk_test_unit_ready(lun); TRACE("B_GET_MEDIA_STATUS: 0x%08lx\n", *(status_t *)buffer); result = B_OK; break; } case B_GET_GEOMETRY: { if (lun->media_changed) { result = usb_disk_update_capacity(lun); if (result != B_OK) break; } device_geometry *geometry = (device_geometry *)buffer; geometry->bytes_per_sector = lun->block_size; geometry->cylinder_count = lun->block_count; geometry->sectors_per_track = geometry->head_count = 1; geometry->device_type = lun->device_type; geometry->removable = lun->removable; geometry->read_only = lun->write_protected; geometry->write_once = (lun->device_type == B_WORM); TRACE("B_GET_GEOMETRY: %ld sectors at %ld bytes per sector\n", geometry->cylinder_count, geometry->bytes_per_sector); result = B_OK; break; } case B_FLUSH_DRIVE_CACHE: TRACE("B_FLUSH_DRIVE_CACHE\n"); result = usb_disk_synchronize(lun, true); break; case B_EJECT_DEVICE: { uint8 commandBlock[12]; memset(commandBlock, 0, sizeof(commandBlock)); commandBlock[0] = SCSI_START_STOP_UNIT_6; commandBlock[1] = lun->logical_unit_number << 5; commandBlock[4] = 2; result = usb_disk_operation(lun, commandBlock, NULL, NULL, false); break; } case B_LOAD_MEDIA: { uint8 commandBlock[12]; memset(commandBlock, 0, sizeof(commandBlock)); commandBlock[0] = SCSI_START_STOP_UNIT_6; commandBlock[1] = lun->logical_unit_number << 5; commandBlock[4] = 3; result = usb_disk_operation(lun, commandBlock, NULL, NULL, false); break; } #if HAIKU_TARGET_PLATFORM_HAIKU case B_GET_ICON: // We don't support this legacy ioctl anymore, but the two other // icon ioctls below instead. break; case B_GET_ICON_NAME: result = user_strlcpy((char *)buffer, "devices/drive-removable-media-usb", B_FILE_NAME_LENGTH); break; case B_GET_VECTOR_ICON: { if (length != sizeof(device_icon)) { result = B_BAD_VALUE; break; } device_icon iconData; if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK) { result = B_BAD_ADDRESS; break; } if (iconData.icon_size >= (int32)sizeof(kDeviceIcon)) { if (user_memcpy(iconData.icon_data, kDeviceIcon, sizeof(kDeviceIcon)) != B_OK) { result = B_BAD_ADDRESS; break; } } iconData.icon_size = sizeof(kDeviceIcon); result = user_memcpy(buffer, &iconData, sizeof(device_icon)); break; } case B_GET_DEVICE_NAME: { size_t nameLength = sizeof(lun->vendor_name) + sizeof(lun->product_name) + sizeof(lun->product_revision) + 3; char name[nameLength]; snprintf(name, nameLength, "%.8s %.16s %.4s", lun->vendor_name, lun->product_name, lun->product_revision); normalize_name(name, nameLength); result = user_strlcpy((char *)buffer, name, length); if (result > 0) result = B_OK; TRACE_ALWAYS("got device name: \"%s\" = %s\n", name, strerror(result)); break; } #endif default: TRACE_ALWAYS("unhandled ioctl %" B_PRIu32 "\n", op); break; } mutex_unlock(&device->lock); return result; }
static status_t usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length) { device_lun *lun = (device_lun *)cookie; disk_device *device = lun->device; mutex_lock(&device->lock); if (device->removed) { mutex_unlock(&device->lock); return B_DEV_NOT_READY; } status_t result = B_DEV_INVALID_IOCTL; switch (op) { case B_GET_MEDIA_STATUS: { *(status_t *)buffer = usb_disk_test_unit_ready(lun); TRACE("B_GET_MEDIA_STATUS: 0x%08lx\n", *(status_t *)buffer); result = B_OK; break; } case B_GET_GEOMETRY: { if (lun->media_changed) { result = usb_disk_update_capacity(lun); if (result != B_OK) break; } device_geometry *geometry = (device_geometry *)buffer; geometry->bytes_per_sector = lun->block_size; geometry->cylinder_count = lun->block_count; geometry->sectors_per_track = geometry->head_count = 1; geometry->device_type = lun->device_type; geometry->removable = lun->removable; geometry->read_only = lun->write_protected; geometry->write_once = (lun->device_type == B_WORM); TRACE("B_GET_GEOMETRY: %ld sectors at %ld bytes per sector\n", geometry->cylinder_count, geometry->bytes_per_sector); result = B_OK; break; } case B_FLUSH_DRIVE_CACHE: TRACE("B_FLUSH_DRIVE_CACHE\n"); result = usb_disk_synchronize(lun, true); break; case B_EJECT_DEVICE: result = usb_disk_operation(lun, SCSI_START_STOP_UNIT_6, 6, 0, 2, NULL, NULL, false); break; case B_LOAD_MEDIA: result = usb_disk_operation(lun, SCSI_START_STOP_UNIT_6, 6, 0, 3, NULL, NULL, false); break; #if HAIKU_TARGET_PLATFORM_HAIKU case B_GET_ICON: // We don't support this legacy ioctl anymore, but the two other // icon ioctls below instead. break; case B_GET_ICON_NAME: result = user_strlcpy((char *)buffer, "devices/drive-removable-media-usb", B_FILE_NAME_LENGTH); break; case B_GET_VECTOR_ICON: { if (length != sizeof(device_icon)) { result = B_BAD_VALUE; break; } device_icon iconData; if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK) { result = B_BAD_ADDRESS; break; } if (iconData.icon_size >= (int32)sizeof(kDeviceIcon)) { if (user_memcpy(iconData.icon_data, kDeviceIcon, sizeof(kDeviceIcon)) != B_OK) { result = B_BAD_ADDRESS; break; } } iconData.icon_size = sizeof(kDeviceIcon); result = user_memcpy(buffer, &iconData, sizeof(device_icon)); break; } #endif default: TRACE_ALWAYS("unhandled ioctl %ld\n", op); break; } mutex_unlock(&device->lock); return result; }
static status_t usb_disk_ioctl(void *cookie, uint32 op, void *buffer, size_t length) { device_lun *lun = (device_lun *)cookie; disk_device *device = lun->device; mutex_lock(&device->lock); if (device->removed) { mutex_unlock(&device->lock); return B_DEV_NOT_READY; } status_t result = B_DEV_INVALID_IOCTL; switch (op) { case B_GET_MEDIA_STATUS: { err_act action = err_act_ok; for (uint32 tries = 0; tries < 3; tries++) { status_t ready = usb_disk_test_unit_ready(lun, &action); if (ready == B_OK || ready == B_DEV_NO_MEDIA || (action != err_act_retry && action != err_act_many_retries)) { *(status_t *)buffer = ready; break; } snooze(500000); } TRACE("B_GET_MEDIA_STATUS: 0x%08" B_PRIx32 "\n", *(status_t *)buffer); result = B_OK; break; } case B_GET_GEOMETRY: { if (lun->media_changed) { result = usb_disk_update_capacity(lun); if (result != B_OK) break; } device_geometry geometry; devfs_compute_geometry_size(&geometry, lun->block_count, lun->block_size); geometry.device_type = lun->device_type; geometry.removable = lun->removable; geometry.read_only = lun->write_protected; geometry.write_once = lun->device_type == B_WORM; TRACE("B_GET_GEOMETRY: %" B_PRId32 " sectors at %" B_PRId32 " bytes per sector\n", geometry.cylinder_count, geometry.bytes_per_sector); result = user_memcpy(buffer, &geometry, sizeof(device_geometry)); break; } case B_FLUSH_DRIVE_CACHE: TRACE("B_FLUSH_DRIVE_CACHE\n"); result = usb_disk_synchronize(lun, true); break; case B_EJECT_DEVICE: result = usb_disk_operation(lun, SCSI_START_STOP_UNIT_6, 6, 0, 2, NULL, NULL, false); break; case B_LOAD_MEDIA: result = usb_disk_operation(lun, SCSI_START_STOP_UNIT_6, 6, 0, 3, NULL, NULL, false); break; #if HAIKU_TARGET_PLATFORM_HAIKU case B_GET_ICON: // We don't support this legacy ioctl anymore, but the two other // icon ioctls below instead. break; case B_GET_ICON_NAME: result = user_strlcpy((char *)buffer, "devices/drive-removable-media-usb", B_FILE_NAME_LENGTH); break; case B_GET_VECTOR_ICON: { if (length != sizeof(device_icon)) { result = B_BAD_VALUE; break; } device_icon iconData; if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK) { result = B_BAD_ADDRESS; break; } if (iconData.icon_size >= (int32)sizeof(kDeviceIcon)) { if (user_memcpy(iconData.icon_data, kDeviceIcon, sizeof(kDeviceIcon)) != B_OK) { result = B_BAD_ADDRESS; break; } } iconData.icon_size = sizeof(kDeviceIcon); result = user_memcpy(buffer, &iconData, sizeof(device_icon)); break; } case B_GET_DEVICE_NAME: { size_t nameLength = sizeof(lun->vendor_name) + sizeof(lun->product_name) + sizeof(lun->product_revision) + 3; char name[nameLength]; snprintf(name, nameLength, "%.8s %.16s %.4s", lun->vendor_name, lun->product_name, lun->product_revision); normalize_name(name, nameLength); result = user_strlcpy((char *)buffer, name, length); if (result > 0) result = B_OK; TRACE_ALWAYS("got device name \"%s\": %s\n", name, strerror(result)); break; } #endif default: TRACE_ALWAYS("unhandled ioctl %" B_PRId32 "\n", op); break; } mutex_unlock(&device->lock); return result; }