From owner-freebsd-hackers@freebsd.org Fri Jul 17 00:28:53 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5156A372AFB for ; Fri, 17 Jul 2020 00:28:53 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (gate2.funkthat.com [208.87.223.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "gate2.funkthat.com", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B7BnJ17jCz4JvY; Fri, 17 Jul 2020 00:28:51 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (localhost [127.0.0.1]) by gold.funkthat.com (8.15.2/8.15.2) with ESMTPS id 06H0IYiw024359 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 16 Jul 2020 17:18:34 -0700 (PDT) (envelope-from jmg@gold.funkthat.com) Received: (from jmg@localhost) by gold.funkthat.com (8.15.2/8.15.2/Submit) id 06H0IY5h024358; Thu, 16 Jul 2020 17:18:34 -0700 (PDT) (envelope-from jmg) Date: Thu, 16 Jul 2020 17:18:34 -0700 From: John-Mark Gurney To: "Brian Mcgovern (bmcgover)" Cc: Ian Lepore , Hans Petter Selasky , "freebsd-hackers@freebsd.org" Subject: Re: Question on structure of USB (specifically USB Serial) stack Message-ID: <20200717001834.GU4213@funkthat.com> Mail-Followup-To: "Brian Mcgovern (bmcgover)" , Ian Lepore , Hans Petter Selasky , "freebsd-hackers@freebsd.org" References: <82c1e5cede57ced764bfc3a33c75cc0b36a67660.camel@freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: FreeBSD 11.3-STABLE amd64 X-PGP-Fingerprint: D87A 235F FB71 1F3F 55B7 ED9B D5FF 5A51 C0AC 3D65 X-Files: The truth is out there X-URL: https://www.funkthat.com/ X-Resume: https://www.funkthat.com/~jmg/resume.html X-TipJar: bitcoin:13Qmb6AeTgQecazTWph4XasEsP7nGRbAPE X-to-the-FBI-CIA-and-NSA: HI! HOW YA DOIN? can i haz chizburger? User-Agent: Mutt/1.6.1 (2016-04-27) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (gold.funkthat.com [127.0.0.1]); Thu, 16 Jul 2020 17:18:34 -0700 (PDT) X-Rspamd-Queue-Id: 4B7BnJ17jCz4JvY X-Spamd-Bar: + Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of jmg@gold.funkthat.com has no SPF policy when checking 208.87.223.18) smtp.mailfrom=jmg@gold.funkthat.com X-Spamd-Result: default: False [1.87 / 15.00]; TO_DN_EQ_ADDR_SOME(0.00)[]; ARC_NA(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; TO_DN_SOME(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[funkthat.com]; AUTH_NA(1.00)[]; NEURAL_SPAM_MEDIUM(0.67)[0.673]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_HAM_SHORT(-0.06)[-0.059]; NEURAL_SPAM_LONG(0.05)[0.055]; R_SPF_NA(0.00)[no SPF record]; FORGED_SENDER(0.30)[jmg@funkthat.com,jmg@gold.funkthat.com]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:32354, ipnet:208.87.216.0/21, country:US]; FROM_NEQ_ENVFROM(0.00)[jmg@funkthat.com,jmg@gold.funkthat.com]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jul 2020 00:28:53 -0000 FreeBSD Hackers wrote this message on Thu, Jul 16, 2020 at 23:18 +0000: > Ian Lepore said: > > This seems to me to be one of those situations where answering the > exact question that was asked isn't the right thing to do. Instead it > needs to be answered with another question: What it is you're really > trying to accomplish, and why do you think doing this crazy-unsafe > casting is the only way to get it done? > > I get its crazy unsafe for the non-specific case. That is why I'm asking the question - to see if there is a good way to get the info. > > What I'm hoping to do is pin some USB devices with FTDI interfaces to more consistent name, similar to /dev/usb, based on the tree of struct usb_device entries and their respective addresses. What I see with the cuaUXX formatting is that if one gets rebooted or someone hits the power cord, it'll bounce fast enough that it'll come back with a different name. As a result, even though we can get a consistent layout on power up, if you come back a week or two later, everything is out of order. I was hoping that if I could at least get an alias that mapped to the port the device was in, that would be sufficient to find the "right one", regardless of what the cuaUX names were doing. > > The reason for picking this particular section of the code is that it appears I can get most of the work done locally and have similar behavior with all of the devices that share ucom. However, if what I'm seeing as a hurdle is really a wall, I'm open to better ideas. Look here: https://reviews.freebsd.org/D21886 I've added a script, linked here: https://www.funkthat.com/~jmg/FreeBSD/usbserialsn There are two modes, if the device has a serial number, that will be used. If the device has an empty serial number, it's usb bus and port number path will be used. > Sent: Thursday, July 16, 2020 6:32 PM > To: Hans Petter Selasky ; Brian Mcgovern (bmcgover) ; freebsd-hackers@freebsd.org > Subject: Re: Question on structure of USB (specifically USB Serial) stack > > On Thu, 2020-07-16 at 23:21 +0200, Hans Petter Selasky wrote: > > On 2020-07-16 22:20, Brian Mcgovern (bmcgover) via freebsd-hackers > > wrote: > > > All, > > > I'm doing some playing with the ucom code, and I'm down to a > > > link in the data that I'm just not confident I've parsed the code > > > correctly. In sys/dev/usb/serial/usb_serial.c, specifically in > > > ucom_attach_tty, I'm trying to get a reference to the usb_device > > > structure for the device used elsewhere in the USB code. The > > > specific devices I'm working with are USB->RJ 45 cables with a > > > built in FTDI chip, in case this matters > > > > > > It appears that the ucom_super_softc and ucom_softc structures > > > are available. From looking around the code, it appears the > > > sc_parent field of ucom_softc is pointing back to the uftdi_softc > > > structure in uftdi.c (for the uftdi case), so the path would be > > > ucom_softc->sc_parent->sc_udev, but my concern is going through the > > > void *, as it appears each of the devices that use ucom as the base > > > have sc_udev in a different part of their structure, meaning I'm > > > likely going to crash the system if I plan on it being an FTDI > > > device, and its not. Is there a callback or a canonical mechanism > > > for accessing this part of the structure given a starting point of > > > the ucom_softc? Alternatively, are there any well defined > > > attributes I can use to figure out what that void* is pointing to, > > > or at least conditionalizing the code so I can dereference it > > > correctly? > > > > Hi, > > > > Usually using void pointers this way works fine, as long as you are > > careful. There is also something called __containerof() which can be > > used to get the pointer to a structure based on the pointer to a > > structure inside that structure, thinking of the struct ucom_softc, > > which is type-safe. > > > > > > You still can't just blindly cast ucom_softc->sc_parent back to a > struct uftdi_softc in ucom_tty_attach() because it will crash and burn > if any non-ftdi serial device is attached. Using __container_of() > doesn't change that in any way -- you must already know for certain > that the type pointed to by the pointer is the type you name in the > container_of() invocation. > > This seems to me to be one of those situations where answering the > exact question that was asked isn't the right thing to do. Instead it > needs to be answered with another question: What it is you're really > trying to accomplish, and why do you think doing this crazy-unsafe > casting is the only way to get it done? -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not."