linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hid2hci: Remove usb_find_busses and usb_find_devices calls
@ 2009-07-30 17:07 Mario Limonciello
  2009-07-31 17:54 ` [PATCH] hid2hci: Remove usb_find_busses and usb_find_devices Kay Sievers
  0 siblings, 1 reply; 2+ messages in thread
From: Mario Limonciello @ 2009-07-30 17:07 UTC (permalink / raw)
  To: linux-hotplug


[-- Attachment #1.1: Type: text/plain, Size: 483 bytes --]

Hi:

As a follow up to the thread titled "[PATCH 1/2] hid2hci: iterate libusb
devices twice", I've redone that patch to remove the hacks that were
added to work around the bug that has a kernel solution.

This patch removes the calls to usb_find_busses and usb_find_devices and
instead stuffs the necessary bits into a single usb_device so that the
other libusb calls work later on.

Thanks,
-- 
Mario Limonciello
*Dell | Linux Engineering*
mario_limonciello@dell.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: iterate.diff --]
[-- Type: text/x-patch; name="iterate.diff", Size: 2550 bytes --]

=== modified file 'extras/hid2hci/hid2hci.c'
--- extras/hid2hci/hid2hci.c	2009-07-24 16:06:22 +0000
+++ extras/hid2hci/hid2hci.c	2009-07-30 17:00:07 +0000
@@ -150,44 +150,46 @@
 	return err;
 }
 
-/*
- * The braindead libusb needs to scan and open all devices, just to
- * to find the device we already have. This needs to be fixed in libusb
- * or it will be ripped out and we carry our own code.
- */
 static struct usb_device *usb_device_open_from_udev(struct udev_device *usb_dev)
 {
 	struct usb_bus *bus;
+	struct usb_device *dev;
 	const char *str;
-	int busnum;
-	int devnum;
+	char num[4];
+
+	usb_init();
+
+	bus = malloc(sizeof(*bus));
+	dev = malloc(sizeof(*dev));
+
+	if (!bus || !dev)
+		return NULL;
+
+	memset((void *)bus, 0, sizeof(*bus));
+	memset((void *)dev, 0, sizeof(*dev));
 
 	str = udev_device_get_sysattr_value(usb_dev, "busnum");
 	if (str == NULL)
-		return NULL;
-	busnum = strtol(str, NULL, 0);
+		goto err;
+	snprintf(num, sizeof(num), "%03i",(int)strtol(str,NULL,0));
+	strncpy(bus->dirname, num, sizeof(bus->dirname) - 1);
+	bus->dirname[sizeof(bus->dirname) - 1] = 0;
+	dev->bus = bus;
 
 	str = udev_device_get_sysattr_value(usb_dev, "devnum");
 	if (str == NULL)
-		return NULL;
-	devnum = strtol(str, NULL, 0);
-
-	usb_init();
-	usb_find_busses();
-	usb_find_devices();
-
-	for (bus = usb_get_busses(); bus; bus = bus->next) {
-		struct usb_device *dev;
-
-		if (strtol(bus->dirname, NULL, 10) != busnum)
-			continue;
-
-		for (dev = bus->devices; dev; dev = dev->next) {
-			if (dev->devnum == devnum)
-				return dev;
-		}
-	}
-
+		goto err;
+	dev->devnum = strtol(str, NULL, 0);
+
+	snprintf(num, sizeof(num), "%03i",dev->devnum);
+	strncpy(dev->filename, num, sizeof(dev->filename) - 1);
+	dev->filename[sizeof(dev->filename) - 1] = 0;
+
+	return dev;
+
+err:
+	free(dev);
+	free(bus);
 	return NULL;
 }
 
@@ -205,7 +207,10 @@
 		dev = usb_device_open_from_udev(udev_dev);
 		if (dev == NULL)
 			return NULL;
-		return usb_open(dev);
+		handle = usb_open(dev);
+		free(dev->bus);
+		free(dev);
+		return handle;
 	}
 
 	/* find matching sibling of the current usb_device, they share the same hub */
@@ -247,8 +252,11 @@
 				continue;
 			/* only look at the first matching device */
 			dev = usb_device_open_from_udev(udev_parent);
-			if (dev != NULL)
+			if (dev != NULL) {
 				handle = usb_open(dev);
+				free(dev->bus);
+				free(dev);
+			}
 			udev_device_unref(udev_device);
 			break;
 		}


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] hid2hci: Remove usb_find_busses and usb_find_devices
  2009-07-30 17:07 [PATCH] hid2hci: Remove usb_find_busses and usb_find_devices calls Mario Limonciello
@ 2009-07-31 17:54 ` Kay Sievers
  0 siblings, 0 replies; 2+ messages in thread
From: Kay Sievers @ 2009-07-31 17:54 UTC (permalink / raw)
  To: linux-hotplug

[-- Attachment #1: Type: text/plain, Size: 956 bytes --]

On Thu, Jul 30, 2009 at 13:07, Mario
Limonciello<mario_limonciello@dell.com> wrote:
> As a follow up to the thread titled "[PATCH 1/2] hid2hci: iterate libusb
> devices twice", I've redone that patch to remove the hacks that were
> added to work around the bug that has a kernel solution.
>
> This patch removes the calls to usb_find_busses and usb_find_devices and
> instead stuffs the necessary bits into a single usb_device so that the
> other libusb calls work later on.

I first want to try to get libusb or maybe libusb1 fixed properly
before we go that road. Adding Daniel to Cc:.

That problem will re-appear again for other users, and if possible, it
should be solved where it is caused. For now we are just "slow" and
let libusb do very silly things if used from udev -- but it works.

If there will be no fix, we are prepared to do something like you
sent, or to drop libusb entirely and just use something like the
attached thing.

Thanks,
Kay

[-- Attachment #2: usb.c --]
[-- Type: text/x-csrc, Size: 2092 bytes --]

/*
 * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details:
 */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <linux/usb/ch9.h>

#include "usb.h"

struct usb_dev_handle {
	int fd;
	int intf;
};

struct usb_dev_handle *usb_open_from_devnode(const char *filename)
{
	struct usb_dev_handle *handle;

	handle = calloc(1, sizeof(struct usb_dev_handle));
	if (handle == NULL)
		return NULL;

	handle->fd = open(filename, O_RDWR);
	if (handle->fd < 0) {
		free(handle);
		return NULL;
	}
	return handle;
}

int usb_close(struct usb_dev_handle *dev)
{
	if (dev == NULL)
		return 0;
	close(dev->fd);
	free(dev);
	return 0;
}

int usb_claim_interface(struct usb_dev_handle *dev, int interface)
{
	int ret;

	ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
	if (ret < 0)
		return ret;
	dev->intf = interface;
	return 0;
}

int usb_control_msg(struct usb_dev_handle *dev, int requesttype, int request,
		    int value, int idx, char *bytes, int size, int timeout)
{
	struct usbdevfs_ctrltransfer ctrl = {
		.bRequestType = requesttype,
		.bRequest = request,
		.wValue = value,
		.wIndex = idx,
		.wLength = size,
		.data = bytes,
		.timeout = timeout,
	};
	return ioctl(dev->fd, USBDEVFS_CONTROL, &ctrl);
}

int usb_detach_kernel_driver_np(struct usb_dev_handle *dev, int interface)
{
	struct usbdevfs_ioctl command = {
		.ifno = interface,
		.ioctl_code = USBDEVFS_DISCONNECT,
	};
	return ioctl(dev->fd, USBDEVFS_IOCTL, &command);
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-07-31 17:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-30 17:07 [PATCH] hid2hci: Remove usb_find_busses and usb_find_devices calls Mario Limonciello
2009-07-31 17:54 ` [PATCH] hid2hci: Remove usb_find_busses and usb_find_devices Kay Sievers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).