From owner-freebsd-usb@freebsd.org Mon Jan 29 19:13:03 2018 Return-Path: Delivered-To: freebsd-usb@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E7F25ED8B07 for ; Mon, 29 Jan 2018 19:13:02 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mailman.ysv.freebsd.org (mailman.ysv.freebsd.org [IPv6:2001:1900:2254:206a::50:5]) by mx1.freebsd.org (Postfix) with ESMTP id 6AB3B8185D for ; Mon, 29 Jan 2018 19:13:02 +0000 (UTC) (envelope-from hps@selasky.org) Received: by mailman.ysv.freebsd.org (Postfix) id 2A055ED8B03; Mon, 29 Jan 2018 19:13:02 +0000 (UTC) Delivered-To: usb@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 078B7ED8AFB for ; Mon, 29 Jan 2018 19:13:02 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [IPv6:2a01:4f8:c17:6c4b::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 771C481859; Mon, 29 Jan 2018 19:13:01 +0000 (UTC) (envelope-from hps@selasky.org) Received: from hps2016.home.selasky.org (unknown [62.141.128.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id 327C326018F; Mon, 29 Jan 2018 20:13:00 +0100 (CET) Subject: Re: Feedback for portable libusb code To: =?UTF-8?Q?Romain_Tarti=c3=a8re?= , usb@freebsd.org References: <20180129184445.GB7783@blogreen.org> From: Hans Petter Selasky Message-ID: <9128056d-c916-597e-f866-48cf6051ee0e@selasky.org> Date: Mon, 29 Jan 2018 20:10:06 +0100 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <20180129184445.GB7783@blogreen.org> Content-Type: multipart/mixed; boundary="------------10B0C664C0A9F56F53F827C7" Content-Language: en-US X-BeenThere: freebsd-usb@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: FreeBSD support for USB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Jan 2018 19:13:03 -0000 This is a multi-part message in MIME format. --------------10B0C664C0A9F56F53F827C7 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 01/29/18 19:44, Romain Tartière wrote: > Hello! > > FreeBSD's libusb allows userland programs to access USB devices in a way > similar to GNU/Linux with it's libusb. > > However, there are some differences between these implementations. I > recently diagnosed an issue in sigrok, provided a patch, but the > maintainer is having some questions and I am not sure about the best way > to address them. > > If someone with more USB insight could give a look to this GitHub issue, > that would be awesome ! > > Basically, the usb_get_port_path() calls libusb_open() before (and > libusb_close() after) libusb_get_port_numbers() because this is required > on FreeBSD: > https://github.com/sigrokproject/libsigrok/blob/a9010323ddf4e479663e871386c05db05ea3522e/src/usb.c#L478-L491 > > However, calling libusb_open() twice fails… > > The issue is here: > https://github.com/sigrokproject/libsigrok/pull/6 Hi, Does the attached patch for FreeBSD's libusb solve your issue? --HPS --------------10B0C664C0A9F56F53F827C7 Content-Type: text/x-patch; name="libusb.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="libusb.diff" Index: libusb20_int.h =================================================================== --- libusb20_int.h (revision 328435) +++ libusb20_int.h (working copy) @@ -237,8 +237,12 @@ uint8_t is_opened; uint8_t parent_address; uint8_t parent_port; + uint8_t port_level; char usb_desc[96]; + +#define LIBUSB20_DEVICE_PORT_PATH_MAX 32 + uint8_t port_path[LIBUSB20_DEVICE_PORT_PATH_MAX]; }; extern const struct libusb20_backend_methods libusb20_ugen20_backend; Index: libusb20_ugen20.c =================================================================== --- libusb20_ugen20.c (revision 328435) +++ libusb20_ugen20.c (working copy) @@ -136,6 +136,7 @@ const char *tmp = id; struct usb_device_descriptor ddesc; struct usb_device_info devinfo; + struct usb_device_port_path udpp; uint32_t plugtime; char buf[64]; int f; @@ -219,6 +220,21 @@ pdev->device_address, devinfo.udi_vendor, devinfo.udi_product, pdev->bus_number); + /* get parent port path */ + + if (ioctl(f, IOUSB(USB_GET_DEV_PORT_PATH), &udpp)) { + error = LIBUSB20_ERROR_OTHER; + goto done; + } + + if (udpp.udp_port_level > LIBUSB20_DEVICE_PORT_PATH_MAX) { + error = LIBUSB20_ERROR_OVERFLOW; + goto done; + } + + memcpy(pdev->port_path, udpp.udp_port_no, udpp.udp_port_level); + pdev->port_level = udpp.udp_port_level; + error = 0; done: close(f); @@ -653,17 +669,13 @@ static int ugen20_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize) { - struct usb_device_port_path udpp; - if (ioctl(pdev->file_ctrl, IOUSB(USB_GET_DEV_PORT_PATH), &udpp)) - return (LIBUSB20_ERROR_OTHER); - - if (udpp.udp_port_level > bufsize) + if (pdev->port_level > bufsize) return (LIBUSB20_ERROR_OVERFLOW); - memcpy(buf, udpp.udp_port_no, udpp.udp_port_level); + memcpy(buf, pdev->port_path, pdev->port_level); - return (udpp.udp_port_level); /* success */ + return (pdev->port_level); /* success */ } static int --------------10B0C664C0A9F56F53F827C7--