From nobody Mon Apr 18 18:57:15 2022 X-Original-To: freebsd-usb@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4641F11D175C for ; Mon, 18 Apr 2022 18:57:25 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [IPv6:2a01:4f8:c17:6c4b::2]) (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 4Khx504HhNz3v7d for ; Mon, 18 Apr 2022 18:57:24 +0000 (UTC) (envelope-from hps@selasky.org) Received: from [10.36.2.165] (unknown [176.74.213.87]) (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 4A416260509; Mon, 18 Apr 2022 20:57:17 +0200 (CEST) Message-ID: Date: Mon, 18 Apr 2022 20:57:15 +0200 List-Id: FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-usb List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-usb@freebsd.org X-BeenThere: freebsd-usb@freebsd.org MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: Trouble loading firmware to USB device Content-Language: en-US To: Farhan Khan , freebsd-usb@freebsd.org References: From: Hans Petter Selasky In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4Khx504HhNz3v7d X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of hps@selasky.org designates 2a01:4f8:c17:6c4b::2 as permitted sender) smtp.mailfrom=hps@selasky.org X-Spamd-Result: default: False [-3.27 / 15.00]; ARC_NA(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:c]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[selasky.org]; NEURAL_HAM_MEDIUM(-0.97)[-0.970]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; RCPT_COUNT_TWO(0.00)[2]; MLMMJ_DEST(0.00)[freebsd-usb]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:2a01:4f8::/32, country:DE]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[] X-ThisMailContainsUnwantedMimeParts: N On 4/13/22 00:10, Farhan Khan wrote: > Hi all, > > I am trying to load the copy the OpenBSD athn code. Currently I am > trying to load the firmware of the device but running an issue where > the interrupt RX callback is getting an USB_ERR_CANCELLED event. > > Based on my reading of the OpenBSD code, it appears that the OpenBSD > driver will load the firmware in chunks, then submit a NULL (size 0) > usbd_do_request to indicate completion of the firmware. At this, the > driver will tsleep() for 1 second. If it breaks with a return value of > 0, the driver will continue successfully. My understanding is that this > should trigger an Rx Interrupt, which should run the wakup() that the > tsleep() is looking for. > > However, in my case the USB_ST_SETUP condition is run, but it seems to > block indefinitely. Meanwhile, the tsleep() expires, returns a > EWOULDBLOCK and eventually runs the detach handler. As a result, the > blocking callback receives a USB_ST_ERROR with the value > USB_ERR_CANCELLED. > > What might be causing the blocking of the callback? Am I loading the > firmware incorrectly? > > My firmware code is here: > https://github.com/khanzf/freebsd/blob/ar9271-check/sys/dev/athn/usb/if_athn_usb.c#L934 > The interrupt handler, athn_usb_intr, is line 2405. > > The OpenBSD equivalent of the firmware loader is this: > https://github.com/openbsd/src/blob/master/sys/dev/usb/if_athn_usb.c#L642 > The interrupt handler is also athn_usb_intr online 1874. > > Any assistance would be great, been stuck for a few weeks :/ > Hi, error = tsleep(&usc->wait_msg_id, 0, "athnfw", 5); This means wait 5 ticks which is typically 5ms before timing out, which is probably too short! This code is wrong for FreeBSD. The lock should cover the whole section and you should use msleep instead. ATHN_LOCK(sc); error = usbd_do_request(usc->sc_udev, &sc->sc_mtx, &req, NULL); if (error == 0 && usc->wait_msg_id != 0) { printf("Error is %d\n", error); error = msleep(&usc->wait_msg_id, 0, "athnfw", hz); /* wait 1 second at most */ if (error) { ATHN_UNLOCK(sc); printf("Exiting condition %d\n", error); return error; } } ATHN_UNLOCK(sc); --HPS