From owner-freebsd-hackers@freebsd.org Thu Jul 16 21:22:05 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 6257A36E3B9 for ; Thu, 16 Jul 2020 21:22:05 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [88.99.82.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4B76dm3Zvpz473y for ; Thu, 16 Jul 2020 21:22:04 +0000 (UTC) (envelope-from hps@selasky.org) Received: from hps2020.home.selasky.org (unknown [178.17.145.105]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id 5E4D52603CC; Thu, 16 Jul 2020 23:21:57 +0200 (CEST) Subject: Re: Question on structure of USB (specifically USB Serial) stack To: "Brian Mcgovern (bmcgover)" , "freebsd-hackers@freebsd.org" References: From: Hans Petter Selasky Message-ID: Date: Thu, 16 Jul 2020 23:21:33 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4B76dm3Zvpz473y X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of hps@selasky.org designates 88.99.82.50 as permitted sender) smtp.mailfrom=hps@selasky.org X-Spamd-Result: default: False [-2.44 / 15.00]; TO_DN_EQ_ADDR_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+a:mail.turbocat.net]; NEURAL_HAM_LONG(-1.03)[-1.029]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[selasky.org]; ARC_NA(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_HAM_SHORT(-0.13)[-0.128]; RCPT_COUNT_TWO(0.00)[2]; NEURAL_HAM_MEDIUM(-0.99)[-0.986]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:88.99.0.0/16, country:DE]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[] 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: Thu, 16 Jul 2020 21:22:05 -0000 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. --HPS