<commit_before>/*
 * lib/usbopen.c - Common USB device lookup and open code
 *
 * Written 2008-2011 by <NAME>
 * Copyright 2008-2011 <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */



#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <usb.h>

#include "usbopen.h"


#define	USB_PATH_ROOT	"/sys/bus/usb/devices/"
#define	USB_BUS_LEAF	"/busnum"
#define	USB_DEV_LEAF	"/devnum"

static uint16_t vendor = 0;
static uint16_t product = 0;
static const struct usb_device *restricted_path = NULL;
static int initialized = 0;


static void initialize(void)
{

	if (initialized)
		return;
	initialized = 1;

	usb_init();
	usb_find_busses();
	usb_find_devices();
}


void usb_rescan(void)
{
	initialized = 0;
}


usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product)
{
	const struct usb_bus *bus;
	struct usb_device *dev;
	usb_dev_handle *handle;
#ifdef DO_FULL_USB_BUREAUCRACY
	int res;
#endif

	initialize();

	for (bus = usb_get_busses(); bus; bus = bus->next)
		for (dev = bus->devices; dev; dev = dev->next) {
			if (restricted_path && restricted_path != dev)
				continue;
			if (dev->descriptor.idVendor !=
			    (vendor ? vendor : default_vendor))
				continue;
			if (dev->descriptor.idProduct !=
			    (product ? product : default_product))
				continue;
			handle = usb_open(dev);
#ifdef DO_FULL_USB_BUREAUCRACY
			if (!handle)
				return NULL;
			res = usb_set_configuration(handle, 1);
			if (res < 0) {
				fprintf(stderr, "usb_set_configuration: %d\n",
				    res);
				return NULL;
			}
			res = usb_claim_interface(handle, 0);
			if (res < 0) {
				fprintf(stderr, "usb_claim_interface: %d\n",
				    res);
				return NULL;
			}
			res = usb_set_altinterface(handle, 0);
			if (res < 0) {
				fprintf(stderr, "usb_set_altinterface: %d\n",
				    res);
				return NULL;
			}
#endif
			return handle;
		}
	return NULL;
}


static void bad_id(const char *id)
{
	fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id);
	exit(1);
}


void parse_usb_id(const char *id)
{
	unsigned long tmp;
	char *end;

	tmp = strtoul(id, &end, 16);
	if (*end != ':')
		bad_id(id);
	if (tmp > 0xffff)
		bad_id(id);
	vendor = tmp;
	tmp = strtoul(end+1, &end, 16);
	if (*end)
		bad_id(id);
	if (tmp > 0xffff)
		bad_id(id);
	product = tmp;
}


static void restrict_usb_dev(int bus_num, int dev_num)
{
	const struct usb_bus *bus;
	const struct usb_device *dev;

	initialize();

	for (bus = usb_busses; bus; bus = bus->next) {
		if (atoi(bus->dirname) != bus_num)
			continue;
		for (dev = bus->devices; dev; dev = dev->next)
			if (dev->devnum == dev_num) {
				restricted_path = dev;
				return;
			}
	}
	fprintf(stderr, "no device %d/%d\n", bus_num, dev_num);
	exit(1);
}


static void restrict_usb_by_dev(const char *path)<commit_msg>tools/lib/usbopen.c: make vendor and/or product optional<commit_after>/*
 * lib/usbopen.c - Common USB device lookup and open code
 *
 * Written 2008-2011 by <NAME>
 * Copyright 2008-2011 <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */



#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <usb.h>

#include "usbopen.h"


#define	USB_PATH_ROOT	"/sys/bus/usb/devices/"
#define	USB_BUS_LEAF	"/busnum"
#define	USB_DEV_LEAF	"/devnum"

static uint16_t vendor = 0;
static uint16_t product = 0;
static const struct usb_device *restricted_path = NULL;
static int initialized = 0;


static void initialize(void)
{

	if (initialized)
		return;
	initialized = 1;

	usb_init();
	usb_find_busses();
	usb_find_devices();
}


void usb_rescan(void)
{
	initialized = 0;
}


usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product)
{
	const struct usb_bus *bus;
	struct usb_device *dev;
	usb_dev_handle *handle;
#ifdef DO_FULL_USB_BUREAUCRACY
	int res;
#endif

	initialize();

	if (!vendor)
		vendor = default_vendor;
	if (!product)
		product = default_product;

	for (bus = usb_get_busses(); bus; bus = bus->next)
		for (dev = bus->devices; dev; dev = dev->next) {
			if (restricted_path && restricted_path != dev)
				continue;
			if (vendor && dev->descriptor.idVendor != vendor)
				continue;
			if (product && dev->descriptor.idProduct != product)
				continue;
			handle = usb_open(dev);
#ifdef DO_FULL_USB_BUREAUCRACY
			if (!handle)
				return NULL;
			res = usb_set_configuration(handle, 1);
			if (res < 0) {
				fprintf(stderr, "usb_set_configuration: %d\n",
				    res);
				return NULL;
			}
			res = usb_claim_interface(handle, 0);
			if (res < 0) {
				fprintf(stderr, "usb_claim_interface: %d\n",
				    res);
				return NULL;
			}
			res = usb_set_altinterface(handle, 0);
			if (res < 0) {
				fprintf(stderr, "usb_set_altinterface: %d\n",
				    res);
				return NULL;
			}
#endif
			return handle;
		}
	return NULL;
}


static void bad_id(const char *id)
{
	fprintf(stderr, "\"%s\" is not a valid [vendor]:[product] ID\n", id);
	exit(1);
}


void parse_usb_id(const char *id)
{
	unsigned long tmp;
	char *end;

	if (*id == ':') {
		vendor = 0;
		end = (char *) id; /* ugly */
	} else {
		tmp = strtoul(id, &end, 16);
		if (*end != ':')
			bad_id(id);
		if (tmp > 0xffff)
			bad_id(id);
		vendor = tmp;
	}
	if (!*end)
		bad_id(id);
	if (!end[1])
		product = 0;
	else {
		tmp = strtoul(end+1, &end, 16);
		if (*end)
			bad_id(id);
		if (tmp > 0xffff)
			bad_id(id);
		product = tmp;
	}
}


static void restrict_usb_dev(int bus_num, int dev_num)
{
	const struct usb_bus *bus;
	const struct usb_device *dev;

	initialize();

	for (bus = usb_busses; bus; bus = bus->next) {
		if (atoi(bus->dirname) != bus_num)
			continue;
		for (dev = bus->devices; dev; dev = dev->next)
			if (dev->devnum == dev_num) {
				restricted_path = dev;
				return;
			}
	}
	fprintf(stderr, "no device %d/%d\n", bus_num, dev_num);
	exit(1);
}


static void restrict_usb_by_dev(const char *path)
{'fraction_non_alphanumeric': 0.1365946632782719, 'fraction_numerical': 0.0147712833545108, 'mean_word_length': 3.566352429296592, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2088099', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2350, 'n_words': 647}