Moved force claim into device class.

This commit is contained in:
Klaus Reimer 2011-02-05 15:08:03 +01:00 committed by k
parent d80266929c
commit a4ec2a9d68
2 changed files with 17 additions and 21 deletions

View File

@ -6,9 +6,11 @@
package de.ailis.usb4java.jsr80;
import static de.ailis.usb4java.USB.USB_DT_STRING;
import static de.ailis.usb4java.USB.libusb_has_detach_kernel_driver_np;
import static de.ailis.usb4java.USB.usb_claim_interface;
import static de.ailis.usb4java.USB.usb_close;
import static de.ailis.usb4java.USB.usb_control_msg;
import static de.ailis.usb4java.USB.usb_detach_kernel_driver_np;
import static de.ailis.usb4java.USB.usb_get_descriptor;
import static de.ailis.usb4java.USB.usb_get_string;
import static de.ailis.usb4java.USB.usb_open;
@ -375,13 +377,16 @@ abstract class AbstractDevice implements UsbDevice
*
* @param number
* The number of the interface to claim.
* @param force
* If claim should be forces if possible.
* @throws UsbException
* When interface could not be claimed.
* @throws UsbClaimException
* When an interface is already claimed.
*/
final void claimInterface(final byte number) throws UsbClaimException,
final void claimInterface(final byte number, final boolean force)
throws UsbClaimException,
UsbException
{
if (this.claimedInterfaceNumber != null)
@ -390,6 +395,13 @@ abstract class AbstractDevice implements UsbDevice
USBLock.acquire();
try
{
// Detach existing driver from the device if requested and
// libusb supports it.
if (force && libusb_has_detach_kernel_driver_np())
{
usb_detach_kernel_driver_np(open(), number);
}
final int result = usb_claim_interface(open(), number & 0xff);
if (result < 0) throw new UsbException(usb_strerror());
this.claimedInterfaceNumber = number;
@ -568,7 +580,8 @@ abstract class AbstractDevice implements UsbDevice
@Override
public final void syncSubmit(final UsbControlIrp irp) throws UsbException
{
if( (irp.bRequest() == UsbConst.REQUEST_SET_CONFIGURATION) && (irp.bmRequestType() ==0) )
if ((irp.bRequest() == UsbConst.REQUEST_SET_CONFIGURATION)
&& (irp.bmRequestType() == 0))
{
final int result2 = usb_set_configuration(open(), irp.wValue());
if (result2 < 0) throw new UsbException(usb_strerror());

View File

@ -5,9 +5,6 @@
package de.ailis.usb4java.jsr80;
import static de.ailis.usb4java.USB.libusb_has_detach_kernel_driver_np;
import static de.ailis.usb4java.USB.usb_detach_kernel_driver_np;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
@ -51,9 +48,6 @@ public final class UsbInterfaceImpl implements UsbInterface
/** The endpoints. */
private final List<UsbEndpoint> endpoints;
/** The USB device. */
private final AbstractDevice device;
/**
* Constructor.
@ -72,7 +66,6 @@ public final class UsbInterfaceImpl implements UsbInterface
{
this.configuration = configuration;
this.descriptor = new UsbInterfaceDescriptorImpl(lowLevelDescriptor);
this.device = device;
final List<UsbEndpoint> endpoints = new ArrayList<UsbEndpoint>();
for (final USB_Endpoint_Descriptor desc : lowLevelDescriptor
@ -125,8 +118,6 @@ public final class UsbInterfaceImpl implements UsbInterface
/**
* @see UsbInterface#claim(UsbInterfacePolicy)
*
* TODO Policy is ignored
*/
@Override
@ -138,18 +129,10 @@ public final class UsbInterfaceImpl implements UsbInterface
USBLock.acquire();
try
{
// Detach existing driver from the device if requested and
// libusb supports it.
if (policy != null && policy.forceClaim(this)
&& libusb_has_detach_kernel_driver_np())
{
usb_detach_kernel_driver_np(this.device.open(),
this.descriptor.bInterfaceNumber());
}
device.setActiveUsbConfigurationNumber(this.configuration
.getUsbConfigurationDescriptor().bConfigurationValue());
device.claimInterface(this.descriptor.bInterfaceNumber());
device.claimInterface(this.descriptor.bInterfaceNumber(),
policy != null && policy.forceClaim(this));
this.configuration.setUsbInterface(
this.descriptor.bInterfaceNumber(), this);
}