Ejemplo n.º 1
0
bool match_serial_number(struct usb_dev_handle* usbdev, void* custom, unsigned int len)
{
  bool ret;
  char* buffer = (char*)malloc(len);
  usb_get_string_simple(usbdev, usb_device(usbdev)->descriptor.iSerialNumber,
      buffer, len);
  ret = strncmp(buffer, (char*)custom, len) == 0;
  free(buffer);
  return ret;
}
Ejemplo n.º 2
0
static int device_type(usb_dev_handle *lvr_winusb){
	struct usb_device *dev;
	int i;
	dev = usb_device(lvr_winusb);
	for(i =0;i < SUPPORTED_DEVICES;i++){
		if (dev->descriptor.idVendor == vendor_id[i] && 
			dev->descriptor.idProduct == product_id[i] ) {
			return i;
		}
	}
	return -1;
}
Ejemplo n.º 3
0
const char *get_playlist_extension(PTP_USB *ptp_usb)
{
  struct usb_device *dev;
  static char creative_pl_extension[] = ".zpl";
  static char default_pl_extension[] = ".pla";

  dev = usb_device(ptp_usb->handle);
  if (dev->descriptor.idVendor == 0x041e) {
    return creative_pl_extension;
  }
  return default_pl_extension;
}
Ejemplo n.º 4
0
void USBManager::slotConnect()
{
    if (!mDeviceState)
    {

        usb_dev_handle *lDeviceHandle = NULL;
        usb_bus *lDeviceBus = NULL;

        struct usb_device *lCurrentDevice;
        for (lDeviceBus = usb_get_busses(); lDeviceBus != NULL && lDeviceHandle == NULL;
             lDeviceBus = lDeviceBus->next)
        {
            for (lCurrentDevice = lDeviceBus->devices; lCurrentDevice != NULL && lDeviceHandle == NULL;
                 lCurrentDevice = lCurrentDevice->next)
            {
                if (lCurrentDevice->descriptor.idVendor == mVID && lCurrentDevice->descriptor.idProduct == mPID)
                {
                    mUSBDevice = lCurrentDevice;
                    lDeviceHandle = usb_open(lCurrentDevice);
                    qDebug() << "lDeviceHandle: " << lDeviceHandle;
                }
//                qDebug() << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
//                qDebug() << "Pid: " << QString("%1").arg(lCurrentDevice->descriptor.idProduct, 0, 16);
//                qDebug() << "Vid: " << QString("%1").arg(lCurrentDevice->descriptor.idVendor, 0, 16);
//                qDebug() << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
            }
        }
        if (lDeviceHandle)
        {
            delete mDeviceHandle;
            mDeviceHandle = lDeviceHandle;
        }

        mDeviceState = (mDeviceHandle);
    }
    else
    {
            int lResult = usb_claim_interface(mDeviceHandle, usb_device(mDeviceHandle)->config->interface->altsetting->iInterface);
            if (lResult > 0)
            {
                usb_reset(mDeviceHandle);
                qDebug() << "reset";
            }
            else
            {
//                qDebug() << metaObject()->className() << " device is already opened!";
            }
    }
    slotDebugLogger();
}
Ejemplo n.º 5
0
/* CAUTION: be careful when modifying the output format of this function,
 * since it's used to produce sub-drivers "stub" using
 * scripts/subdriver/path-to-subdriver.sh
 */
void HIDDumpTree(hid_dev_handle_t udev, usage_tables_t *utab)
{
	int	i;
#ifndef SHUT_MODE
	/* extract the VendorId for further testing */
	int vendorID = usb_device((struct usb_dev_handle *)udev)->descriptor.idVendor;
#endif

	/* Do not go further if we already know nothing will be displayed.
	 * Some reports take a while before they timeout, so if these are
	 * not used in the driver, they should only be fetched when we're
	 * in debug mode
	 */
	if (nut_debug_level < 1) {
		return;
	}

	for (i = 0; i < pDesc->nitems; i++)
	{
		double		value;
		HIDData_t	*pData = &pDesc->item[i];

		/* skip reports 254/255 for Eaton / MGE / Dell due to special handling needs */
#ifdef SHUT_MODE
		if ((pData->ReportID == 254) || (pData->ReportID == 255)) {
			continue;
		}
#else
		if ((vendorID == 0x0463) || (vendorID == 0x047c)) {
			if ((pData->ReportID == 254) || (pData->ReportID == 255)) {
				continue;
			}
		}
#endif

		/* Get data value */
		if (HIDGetDataValue(udev, pData, &value, MAX_TS) == 1) {
			upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i, Value: %g",
				HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size, value);
			continue;
		}

		upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i",
			HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size);
	}

	fflush(stdout);
}
Ejemplo n.º 6
0
void
usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
{
	struct usb_device *dev;
	struct libusb20_device *pdev;
	uint8_t *ptr;
	int error;
	uint32_t size;
	uint16_t len;
	uint8_t x;

	if (udev == NULL) {
		/* be NULL safe */
		return;
	}
	dev = usb_device(udev);
	pdev = (void *)udev;

	if (dev->descriptor.bNumConfigurations == 0) {
		/* invalid device */
		return;
	}
	size = dev->descriptor.bNumConfigurations *
	    sizeof(struct usb_config_descriptor);

	dev->config = malloc(size);
	if (dev->config == NULL) {
		/* out of memory */
		return;
	}
	memset(dev->config, 0, size);

	for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {

		error = (pdev->methods->get_config_desc_full) (
		    pdev, &ptr, &len, x);

		if (error) {
			usb_destroy_configuration(dev);
			return;
		}
		usb_parse_configuration(dev->config + x, ptr);

		/* free config buffer */
		free(ptr);
	}
	return;
}
Ejemplo n.º 7
0
bool device_iterator (struct usb_dev_handle const* usbdev, void* custom, unsigned int len)
{
    bool ret = false;
    int i;
    char current_dev_path[10];
    const struct usb_device *device = usb_device((struct usb_dev_handle *)usbdev);

    /* only here to prevent the unused warning */
    /* TODO remove */
    len = *((unsigned long*)custom);

    /* Obtain the device's full path */
    //sprintf(current_dev_path, "%s/%s", usbdev->bus->dirname, usbdev->device->filename);
    sprintf(current_dev_path, "%s/%s", device->bus->dirname, device->filename);

    /* Check if we already saw this dev */
    for ( i = 0 ; ( hid_id[i] != NULL ) ; i++ )
    {
        if (!strcmp(hid_id[i], current_dev_path ) )
            break;
    }

    /* Append device to the list if needed */
    if (hid_id[i] == NULL)
    {
        hid_id[i] = (char *) malloc (strlen(device->filename) + strlen(device->bus->dirname) );
        sprintf(hid_id[i], "%s/%s", device->bus->dirname, device->filename);
    }
    else /* device already seen */
    {
        return false;
    }

    /* Filter non HID device */
    if ( (device->descriptor.bDeviceClass == 0) /* Class defined at interface level */
            && device->config
            && device->config->interface->altsetting->bInterfaceClass == USB_CLASS_HID)
        ret = true;
    else
        ret = false;

    return ret;
}
Ejemplo n.º 8
0
int
usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
{
	struct usb_device *dev;
	int err;
	uint8_t i;

	/*
	 * Need to translate from "bConfigurationValue" to
	 * configuration index:
	 */

	if (bConfigurationValue == 0) {
		/* unconfigure */
		i = 255;
	} else {
		/* lookup configuration index */
		dev = usb_device(udev);

		/* check if the configuration array is not there */
		if (dev->config == NULL) {
			return (-1);
		}
		for (i = 0;; i++) {
			if (i == dev->descriptor.bNumConfigurations) {
				/* "bConfigurationValue" not found */
				return (-1);
			}
			if ((dev->config + i)->bConfigurationValue ==
			    bConfigurationValue) {
				break;
			}
		}
	}

	err = libusb20_dev_set_config_index((void *)udev, i);

	if (err)
		return (-1);

	return (0);
}
Ejemplo n.º 9
0
gint
aifp_check_and_init_device(void) {

        usb_init();

        dh = ifp_find_device();
        if (dh == NULL) {
                message_dialog(_("Error"), options.playlist_is_embedded ? GTK_WIDGET(main_window) : GTK_WIDGET(playlist_window),
                               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL, _("No suitable iRiver iFP device found.\n"
                               "Perhaps it is unplugged or turned off."));
                return -1;
        }

        dev = usb_device(dh);

        if (usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber)) {

                message_dialog(_("Error"), options.playlist_is_embedded ? GTK_WIDGET(main_window) : GTK_WIDGET(playlist_window),
                               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL, _("Device is busy.\n(Aqualung was unable to claim its interface.)"));

                if (ifp_release_device(dh)) {
                        fprintf(stderr, "ifp_device.c: aifp_check_and_init_device(): release_device failed\n");
                }

                return -1;
        }

        if (ifp_init(&ifpdev, dh)) {

                message_dialog(_("Error"), options.playlist_is_embedded ? GTK_WIDGET(main_window) : GTK_WIDGET(playlist_window),
                               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL, _("Device is not responding.\nTry jiggling the handle."));

                usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);

                if (ifp_release_device(dh)) {
                        fprintf(stderr, "ifp_device.c: aifp_check_and_init_device(): release_device failed\n");
                }
                return -1;
        }
        return 0;
}
Ejemplo n.º 10
0
void list_devices() {
    usb_dev_handle *handle = 0;
    struct usb_device *dev = 0;
    struct usb_device_descriptor desc;
    memset(&desc, 0, sizeof(desc));
    char descString[255];
    memset(&descString, 0, sizeof(descString));
    int numWheels = sizeof(wheels)/sizeof(wheelstruct);

    int numFound = 0;
    int i = 0;
    wheelstruct w = wheels[i];
    for (i = 0; i < numWheels; i++) {
        w = wheels[i];
        printf("Scanning for \"%s\": ", w.name);
        handle = usb_open_device_with_vid_pid(NULL, VID_LOGITECH, w.native_pid);
        if (handle != 0) {
            dev = usb_device(handle);
            if (dev != 0) {
                int ret = usb_get_string_simple(handle, desc.iProduct, descString, 255);
                if (ret == 0) {
                    numFound++;
                    printf("\t\tFound \"%s\", release number %x, %04x:%04x (bus %d, device %d)",
                           descString, desc.bcdDevice, desc.idVendor, desc.idProduct,
                           dev->bus->location, dev->devnum);
                } else {
                    perror("Get device descriptor");
                }
            } else {
                perror ("Get device");
            }
            usb_close(handle);
        }
        printf("\n");
    }
    printf("Found %d devices.\n", numFound);
}
Ejemplo n.º 11
0
void Context::set_usb_device(struct usb_dev_handle *dev)
{
    ftdi_set_usbdev(d->ftdi, dev);
    d->dev = usb_device(dev);
}
Ejemplo n.º 12
0
/*----------------------------------------------------------------------------*/
void usb_audio_massstorage(void)
{
    device_check();
    if (pc_connect)
    {
        input_number_en = 0;
        vol_change_en=1;
        //sd_speed_init(0, 100);
        main_menu = MENU_USBDEV_MAIN;
        disp_port(MENU_USBDEV_MAIN);
        usb_config(USB_DEVICE_CFG);
        write_protect_set(0);                           //不使用写保护功能,如果需要,请根据卡座写保护的状态改变配置
        SYSTEM_CLK_DIV1();
        init_usb_device_hardware();
#if((PLL_IN_HPRC == INPUT_OSC_SELECT) && USE_SOF_PLL)
        {
            u8 flag;
            flag = correct_hprc();
            if(1 == flag)			 ///<限定时间内,校准通过,保存矫正值
            {
                write_hprc_cnt(MEM_PLLCON0, memery_hprc_cnt);
//			deg_string("secuss\n");
            }
            else if(0 == flag)		  ///<校准时USB线拔出
            {
                work_mode = MUSIC_MODE;
                IE1 &= ~BIT(1);
                P3PU &= ~BIT(6);
//			deg_string("unsecuss\n");
                return;
            }
            else if(2 == flag)		 ///<限定时间内,校准失效,可继续相应功能
            {
//			work_mode = MUSIC_MODE;
//			IE1 &= ~BIT(1);
//        	P3PU &= ~BIT(6);
//			return;
            }
        }
#endif
        key_table_sel(0);
        flush_all_msg();
#if ((USB_DEVICE_CFG & USB_SPEAKER) == USB_SPEAKER)
        set_eq(eq_mode);
#endif
        alm_request_cnt = 0;
        set_max_vol(MAX_ANOLOG_VOL,MAX_DIGITAL_VOL);///设置最大音量
        usb_device();
        recording_end();
        dac_mute_control(0,1);		 ///<退出PC后,关掉DAC MUTE。
#if ((USB_DEVICE_CFG & USB_SPEAKER) == USB_SPEAKER)
        main_vol_set(0, CHANGE_VOL_NO_MEM);
#endif
        IE1 &= ~BIT(1);
        P3PU &= ~BIT(6);
    }
    else
    {
        work_mode++;
        Set_Curr_Func(work_mode);
    }
}
Ejemplo n.º 13
0
int
usbReadDeviceDescriptor (UsbDevice *device) {
  UsbDeviceExtension *devx = device->extension;
  memcpy(&device->descriptor, &usb_device(devx->handle)->descriptor, UsbDescriptorSize_Device);
  return 1;
}
Ejemplo n.º 14
0
usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
{
  struct usb_bus *busses, *bus;
  usb_dev_handle *handle = NULL;
  struct prox_unit units[50];
  int iUnit = 0;

  usb_find_busses();
  usb_find_devices();

  busses = usb_get_busses();

  for (bus = busses; bus; bus = bus->next) {
    struct usb_device *dev;
    
    for (dev = bus->devices; dev; dev = dev->next) {
      struct usb_device_descriptor *desc = &(dev->descriptor);

      if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) {
        handle = usb_open(dev);
        if (!handle) {
          if (verbose)
            fprintf(stderr, "open fabiled: %s!\n", usb_strerror());
          //return NULL;
          continue;
        }
        *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;

        struct prox_unit unit = {handle, {0}};
        usb_get_string_simple(handle, desc->iSerialNumber, unit.serial_number, sizeof(unit.serial_number));
        units[iUnit++] = unit;

        //return handle;
      }
    }
  }

  if (iUnit > 0) {
    int iSelection = 0;

    fprintf(stdout, "\nConnected units:\n");

    for (int i = 0; i < iUnit; i++) {
      struct usb_device * dev = usb_device(units[i].handle);
      fprintf(stdout, "\t%d. SN: %s [%s/%s]\n", i+1, units[i].serial_number, dev->bus->dirname, dev->filename);
    }
    if (iUnit > 1) {
      while (iSelection < 1 || iSelection > iUnit) {
        fprintf(stdout, "Which unit do you want to connect to? ");
        fscanf(stdin, "%d", &iSelection);
        }
      }
    else
      iSelection = 1;
    iSelection --;

    for (int i = 0; i < iUnit; i++) {
      if (iSelection == i) continue;
      usb_close(units[i].handle);
      units[i].handle = NULL;
    }

    return units[iSelection].handle;
  }

  return NULL;
}
Ejemplo n.º 15
0
void dealWithDevice(usb_dev_handle *udev)
{
	int err=1,i;
	pthread_t input_thread, output_thread;
	void *thread_retval;

	/* sometime other processes may be probing the LabPro just when we try to claim it, so try a few times */
	for(i=0, err=1; i<3 && err; i++) {	
#ifdef DEBUG
		fprintf(stderr, "trying to claim interface\n");
		fflush(0);
#endif
		err=usb_claim_interface(udev, 0);
		usleep(20000); /* wait 20 ms for safety */
		if(err) sleep(1);
	}
	if (err) {
		fprintf(stderr, "error claiming interface: %s\n", usb_strerror());
		return;
	}
	
#ifdef DEBUG
		fprintf(stderr, "trying to configure interface\n");
		fflush(0);
#endif
	if (!usb_device(udev)->config) { /* sometimes the Labpro returns no descriptors, configure it anyway! */
#ifdef DEBUG
		fprintf(stderr, "no descriptors... doing default configure\n");
		fflush(0);
#endif
			err=usb_set_configuration(udev, 1); /* configure interface */
	} else {
		err=usb_set_configuration(udev, usb_device(udev)->config[0].bConfigurationValue); /* configure interface */
	}
	
	if (err) {
		fprintf(stderr, "error configuring interface: %s\n", usb_strerror());
		return;
	}
	usleep(20000); /* wait 20 ms for safety */
#ifdef DEBUG
		fprintf(stderr, "done configuring... trying to reclaim\n");
		fflush(0);
#endif

	/* sometime other processes may be probing the LabPro just when we try to claim it, so try a few times */
	for(i=0, err=1; i<3 && err; i++) {	
#ifdef DEBUG
		fprintf(stderr, "trying to claim interface\n");
		fflush(0);
#endif
		err=usb_claim_interface(udev, 0);
		usleep(20000); /* wait 20 ms for safety */
		if(err) sleep(1);
	}
	if (err) {
		fprintf(stderr, "error claiming interface: %s\n", usb_strerror());
		return;
	}

#ifdef DEBUG
	fprintf(stderr, "USB device apparently fully prepared to handle data\n");
	fflush(0);
#endif
	
	err=pthread_create(&input_thread, 0, (void *)pass_input, udev);
	if(!err) err=pthread_create(&output_thread, 0, (void *)pass_output, udev);
	
	if(!err) {
		err=pthread_join(input_thread, &thread_retval);
		while(reader_running) { 
			usb_clear_halt(global_intf, USB_ENDPOINT_IN | 2); /* terminate eternal read operation */
			/* usb_resetep(global_intf, USB_ENDPOINT_IN | 2); */ /* terminate eternal read operation */
			usb_clear_halt(global_intf, USB_ENDPOINT_OUT | 2); /* terminate eternal read operation */
			/* usb_resetep(global_intf, USB_ENDPOINT_OUT | 2); */ /* terminate eternal read operation */
			sleep(1);
		}
		err=pthread_join(output_thread, &thread_retval);
	}
	usb_reset(udev);
	usb_release_interface(udev,0);
	
}
Ejemplo n.º 16
0
int rcpod_HasAcceleratedI2C(rcpod_dev* rcpod) {
  /* We always have accelerated I2C if the firmware is version 1.20 or later. */
  return usb_device(rcpod->usbdevh)->descriptor.bcdDevice >= 0x0120;
}
Ejemplo n.º 17
0
/* find_endpoints */
static int novacom_usb_find_endpoints(usb_dev_handle *handle, int eps[2], int *iface)
{
	int i;
	int rc;
	struct usb_device *dev = usb_device(handle);

#if USB_SCAN_TRACE
	log_printf(LOG_SPEW, "find novacom endpoints: handle %p device %p\n", handle, dev);
#endif

	if(dev == NULL || dev->config == NULL) {
		return -1;
	}
	struct usb_interface *interface = dev->config->interface;

	for (i = 0; i < dev->config->bNumInterfaces; i++) {
#if USB_SCAN_TRACE
		log_printf(LOG_SPEW, "interfacenum %d\n", i);
#endif

		int a;
		for (a = 0; a < interface->num_altsetting; a++) {

#if USB_SCAN_TRACE
			log_printf(LOG_SPEW, "altsetting %d\n", a);

			log_printf(LOG_SPEW, "class %d subclass %d protocol %d endpoints %d\n",
				interface[i].altsetting[a].bInterfaceClass,
				interface[i].altsetting[a].bInterfaceSubClass,
				interface[i].altsetting[a].bInterfaceProtocol,
				interface[i].altsetting[a].bNumEndpoints);
#endif

			// see if it's a blank interface with two or three bulk endpoints, probably us
			//
			// XXX be smarter about it
			if (interface[i].altsetting[a].bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
				// match against our subclass/protocol id
				(interface[i].altsetting[a].bInterfaceSubClass == 0x47 &&
				interface[i].altsetting[a].bInterfaceProtocol == 0x11) ) {

				// match the two bulk endpoints we care about
				eps[0] = eps[1] = 0;
				if (interface[i].altsetting[a].endpoint[0].bEndpointAddress & 0x80)
					eps[0] = interface[i].altsetting[a].endpoint[0].bEndpointAddress;
				if ((interface[i].altsetting[a].endpoint[1].bEndpointAddress & 0x80) == 0)
					eps[1] = interface[i].altsetting[a].endpoint[1].bEndpointAddress;

				if (eps[0] == 0 || eps[1] == 0) {
					log_printf(LOG_ERROR, "failed to find acceptable endpoints\n");
					continue;
				}

#if 0
				// set the config
				rc = usb_set_configuration(handle, dev->config->bConfigurationValue);
				if (rc) {
					log_printf(LOG_ERROR, "failed to set config %d\n", dev->config->bConfigurationValue);
					return -1;
				}
#endif

				// claim this interface
				rc = usb_claim_interface(handle, i);
				if (rc) {
					//log_printf(LOG_ERROR, "failed to claim interface %d, errno %d\n", i, errno);
					return -1;
				} else if(iface) {
					*iface = i;
				}

#if 0
				// set the alternate interface
				if (a != 0) {
					rc = usb_set_altinterface(handle, a);
					if (rc) {
						log_printf(LOG_ERROR, "failed to set altinterface %d\n", a);
						return -1;
					}
				}
#endif
			
				return 0;
			}
		}
	}

	return -1;
}