From nobody Mon Nov 09 18:36:55 2026 X-Original-To: dev-commits-src-all@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 4jNdmS16pJz70H1G for ; Mon, 09 Nov 2026 18:36:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jNdmS0731z4HWM for ; Mon, 09 Nov 2026 18:36:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794249416; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=X3C/eVl6nL++ZOsQzZXVdsjRAc97XmsydMvisgrCb0w=; b=AGRO4L82QGL1UinvLzCaInN8h503dH8J3zqTZnKvNibF3A4AwiZfGPphXHK1Ni9v4GqnY9 EDDxqoQGda8vUniLZ6vvgTB4GEpsp9pqx3FoxfdczbASBQ5Kxi6fYVVmrv1kfCKBJdeycV qOEKdmuv0p9kaK7AeH37hfVjrY3El7t2sg73kszu6avAt0+aZxMgJP12EO25j9WVMvPWAy qEWE3XdC1/8AL6gNh54Nu5s+9otLhTeEUh0yahBe2WXFWzR9iuHjZ/Ar9o4KN3/5IWZvR/ +E5kPtiockM+urDFBNvwWKLLfaXMjirfcyQxXCkp+tNtxw417Ug2l3lNmBJGQQ== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794249416; b=M2qrP2rIgKwTHh0FauFfXmdt5Y3YyqOM22d8tgHvlBFp5Sx0DEBKbSZx0o412G/qIW3IFI YfEHzViEZ/IE2qb7XwoH/5WVJ4OsOOhiS3K5y3a2Zp1VIJiRwNC1OMdbMCoK+l8CrgcWjB hkocCQNqRjrx+5QWWIdtbR090MVRh1BSu6Bw3ZTMmQ7HnHQo2IJpqAoIY/nna5LkN0pbHq l4M/CyAuVCEFHzo53wg0ogKma1dijlDIhitn8h3V0fsT0yoF6MgJJzLBlTCwhSGhXHq/bv h5ijBwxmt4iaLmnUODZz1LH6+Kc0O9O4zXPhiGzqrJzMLpV8iixlXnSy2ttHaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794249416; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=X3C/eVl6nL++ZOsQzZXVdsjRAc97XmsydMvisgrCb0w=; b=ocDab++2Zeb47yKkUHIas9MDES6kUZVT5SAgKtWHb0EwXPgS0ELy5t8xsb97wt6thn3VH3 +6ACUY/VJvpNxYCgOvjROq3otAGnlGSDdS6VcO/0LAGwkFuVVLgQLdYNPPProj12JUhJ/f BmrcDgUlzRDmyNmnkR1z0YwcOAI1SYCEX75Ozytl9RQkKrg8T4hHUYYzDtjao8zu1/k4Jc GYsnxWdkFLw5XLyx4redEs/4hAx+1SiYM03Il5SIsZE76AwnlQeqMYr8Zet+u9onCe3Brj SDph3eKA5TjlhQgsoQ+jo2vlCzTypIY9nZ5Rcc5C9K2aHkBcdYt7USbUOpfVrA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jNdmR6vCVzt0X for ; Mon, 09 Nov 2026 18:36:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 9b50 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 09 Nov 2026 18:36:55 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Abdelkader Boudih Subject: git: 0b545e5a9281 - main - asmc: split the PIO backend into asmcpio.c List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: seuros X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0b545e5a9281919117786d57e02876e8031dfc94 Auto-Submitted: auto-generated Date: Mon, 09 Nov 2026 18:36:55 +0000 Message-Id: <6af212c7.9b50.10b42327@gitrepo.freebsd.org> The branch main has been updated by seuros: URL: https://cgit.FreeBSD.org/src/commit/?id=0b545e5a9281919117786d57e02876e8031dfc94 commit 0b545e5a9281919117786d57e02876e8031dfc94 Author: Abdelkader Boudih AuthorDate: 2026-11-09 18:35:21 +0000 Commit: Abdelkader Boudih CommitDate: 2026-11-09 18:35:21 +0000 asmc: split the PIO backend into asmcpio.c Moved the inline PIO transactions out of the key transport routines, which now only dispatch on sc_is_mmio, matching asmcmmio.c. getbyindex and getinfo stay fused: PIO needs both halves under one hold of sc_mtx. No functional change. MFC After: 1 Week Approved-by: ngie (co-mentor) Reviewed by: ngie Differential Revision: https://reviews.freebsd.org/D61296 --- sys/conf/files.amd64 | 1 + sys/dev/asmc/asmc.c | 281 +--------------------------------------- sys/dev/asmc/asmc_pio.c | 324 ++++++++++++++++++++++++++++++++++++++++++++++ sys/dev/asmc/asmc_pio.h | 28 ++++ sys/modules/asmc/Makefile | 2 +- 5 files changed, 361 insertions(+), 275 deletions(-) diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64 index 0e8d9f72c60e..887c619abc51 100644 --- a/sys/conf/files.amd64 +++ b/sys/conf/files.amd64 @@ -118,6 +118,7 @@ dev/apple_bce/apple_bce_queue.c optional apple_bce pci dev/apple_bce/apple_bce_vhci.c optional apple_bce pci usb dev/asmc/asmc.c optional asmc isa dev/asmc/asmc_debug.c optional asmc isa asmc_debug +dev/asmc/asmc_pio.c optional asmc isa dev/asmc/asmcmmio.c optional asmc isa dev/axgbe/if_axgbe_pci.c optional axp dev/axgbe/xgbe-desc.c optional axp diff --git a/sys/dev/asmc/asmc.c b/sys/dev/asmc/asmc.c index cdbfbd647ae3..e2872dac1c3d 100644 --- a/sys/dev/asmc/asmc.c +++ b/sys/dev/asmc/asmc.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -87,19 +88,6 @@ static int asmc_backlight_get_info(device_t dev, struct backlight_info *info); * SMC functions. */ static int asmc_init(device_t dev); -static int asmc_command(device_t dev, uint8_t command); -static int asmc_wait(device_t dev, uint8_t val); -static int asmc_wait_ack(device_t dev, uint8_t val, int amount); -static int asmc_pio_send(device_t dev, const uint8_t *buf, int len, - uint8_t status); -static int asmc_pio_recv(device_t dev, uint8_t *buf, int len, - uint8_t status); -static int asmc_pio_getbyindex_locked(device_t dev, int index, - uint8_t *key); -static int asmc_pio_getinfo_locked(device_t dev, const uint8_t *key, - uint8_t *info); -static void asmc_keyinfo_decode(const uint8_t *info, uint8_t *len, - char *type); static int asmc_fan_count(device_t dev); static int asmc_fan_getvalue(device_t dev, const char *key, int fan); static int asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed); @@ -1120,227 +1108,26 @@ asmc_detect_capabilities(device_t dev) sc->sc_has_safespeed, sc->sc_has_alsl); } -/* - * We need to make sure that the SMC acks the byte sent. - * Just wait up to (amount * 10) ms. - */ -static int -asmc_wait_ack(device_t dev, uint8_t val, int amount) -{ - struct asmc_softc *sc = device_get_softc(dev); - u_int i; - - val = val & ASMC_STATUS_MASK; - - for (i = 0; i < amount; i++) { - if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val) - return (0); - DELAY(10); - } - - return (1); -} - -/* - * We need to make sure that the SMC acks the byte sent. - * Just wait up to 100 ms. - */ -static int -asmc_wait(device_t dev, uint8_t val) -{ -#ifdef ASMC_DEBUG - struct asmc_softc *sc; -#endif - - if (asmc_wait_ack(dev, val, 1000) == 0) - return (0); - -#ifdef ASMC_DEBUG - sc = device_get_softc(dev); - - device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, - val & ASMC_STATUS_MASK, ASMC_CMDPORT_READ(sc)); -#endif - return (1); -} - -/* - * Send the given command, retrying up to 10 times if - * the acknowledgement fails. - */ -static int -asmc_command(device_t dev, uint8_t command) -{ - int i; - struct asmc_softc *sc = device_get_softc(dev); - - for (i = 0; i < 10; i++) { - ASMC_CMDPORT_WRITE(sc, command); - if (asmc_wait_ack(dev, 0x0c, 100) == 0) { - return (0); - } - } - -#ifdef ASMC_DEBUG - device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command, - ASMC_CMDPORT_READ(sc)); -#endif - return (1); -} - -/* Push len bytes out the data port, awaiting status after each. */ -static int -asmc_pio_send(device_t dev, const uint8_t *buf, int len, uint8_t status) -{ - struct asmc_softc *sc = device_get_softc(dev); - - for (int i = 0; i < len; i++) { - ASMC_DATAPORT_WRITE(sc, buf[i]); - if (asmc_wait(dev, status)) - return (1); - } - - return (0); -} - -/* Await status then pull each of len bytes from the data port. */ -static int -asmc_pio_recv(device_t dev, uint8_t *buf, int len, uint8_t status) -{ - struct asmc_softc *sc = device_get_softc(dev); - - for (int i = 0; i < len; i++) { - if (asmc_wait(dev, status)) - return (1); - buf[i] = ASMC_DATAPORT_READ(sc); - } - - return (0); -} - -/* Fetch the name of the index'th key; the caller holds sc_mtx. */ -static int -asmc_pio_getbyindex_locked(device_t dev, int index, uint8_t *key) -{ - struct asmc_softc *sc = device_get_softc(dev); - uint8_t index_buf[ASMC_KEYLEN]; - - index_buf[0] = (index >> 24) & 0xff; - index_buf[1] = (index >> 16) & 0xff; - index_buf[2] = (index >> 8) & 0xff; - index_buf[3] = index & 0xff; - - if (asmc_command(dev, ASMC_CMDGETBYINDEX)) - return (1); - - if (asmc_pio_send(dev, index_buf, ASMC_KEYLEN, ASMC_STATUS_AWAIT_DATA)) - return (1); - - ASMC_DATAPORT_WRITE(sc, ASMC_KEYLEN); - - return (asmc_pio_recv(dev, key, ASMC_KEYLEN, ASMC_STATUS_DATA_READY)); -} - -/* Fetch the raw key info response; the caller holds sc_mtx. */ -static int -asmc_pio_getinfo_locked(device_t dev, const uint8_t *key, uint8_t *info) -{ - struct asmc_softc *sc = device_get_softc(dev); - - if (asmc_command(dev, ASMC_CMDGETINFO)) - return (1); - - if (asmc_pio_send(dev, key, ASMC_KEYLEN, ASMC_STATUS_AWAIT_DATA)) - return (1); - - ASMC_DATAPORT_WRITE(sc, ASMC_KEYINFO_RESPLEN); - - return (asmc_pio_recv(dev, info, ASMC_KEYINFO_RESPLEN, - ASMC_STATUS_DATA_READY)); -} - -/* Split a key info response; type needs ASMC_TYPELEN + 1 bytes. */ -static void -asmc_keyinfo_decode(const uint8_t *info, uint8_t *len, char *type) -{ - if (len != NULL) - *len = info[0]; - if (type != NULL) { - memcpy(type, &info[1], ASMC_TYPELEN); - type[ASMC_TYPELEN] = '\0'; - } -} - int asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len) { struct asmc_softc *sc = device_get_softc(dev); - int error = 1, try = 0; if (sc->sc_is_mmio) return (asmc_mmio_key_read(dev, key, buf, len)); - mtx_lock_spin(&sc->sc_mtx); - -begin: - if (asmc_command(dev, ASMC_CMDREAD)) - goto out; - - if (asmc_pio_send(dev, (const uint8_t *)key, ASMC_KEYLEN, - ASMC_STATUS_AWAIT_DATA)) - goto out; - - ASMC_DATAPORT_WRITE(sc, len); - - if (asmc_pio_recv(dev, buf, len, ASMC_STATUS_DATA_READY)) - goto out; - - error = 0; -out: - if (error) { - if (++try < 10) - goto begin; - device_printf(dev, "%s for key %s failed %d times, giving up\n", - __func__, key, try); - } - - mtx_unlock_spin(&sc->sc_mtx); - - return (error); + return (asmc_pio_key_read(dev, key, buf, len)); } - -/* - * Get key info (length and type) from SMC using command 0x13. - * If len is non-NULL, stores the key's value length. - * If type is non-NULL, stores the 4-char type string (must be at least 5 bytes). - */ int asmc_key_getinfo(device_t dev, const char *key, uint8_t *len, char *type) { struct asmc_softc *sc = device_get_softc(dev); - uint8_t info[ASMC_KEYINFO_RESPLEN]; - int error = -1, try = 0; if (sc->sc_is_mmio) return (asmc_mmio_key_getinfo(dev, key, len, type)); - mtx_lock_spin(&sc->sc_mtx); - - do { - if (asmc_pio_getinfo_locked(dev, (const uint8_t *)key, - info) == 0) { - error = 0; - break; - } - } while (++try < ASMC_MAXRETRIES); - - mtx_unlock_spin(&sc->sc_mtx); - - if (error == 0) - asmc_keyinfo_decode(info, len, type); - - return (error); + return (asmc_pio_key_getinfo(dev, key, len, type)); } /* SMC sensor type table: type string to fixed-point divisor. */ @@ -1660,9 +1447,7 @@ asmc_key_dump_by_index(device_t dev, int index, char *key_out, char *type_out, uint8_t *len_out) { struct asmc_softc *sc = device_get_softc(dev); - uint8_t key_buf[ASMC_KEYLEN]; - uint8_t info_buf[ASMC_KEYINFO_RESPLEN]; - int error = ENXIO, try = 0; + int error; if (sc->sc_is_mmio) { error = asmc_mmio_key_getbyindex(dev, index, key_out); @@ -1672,26 +1457,8 @@ asmc_key_dump_by_index(device_t dev, int index, char *key_out, type_out)); } - mtx_lock_spin(&sc->sc_mtx); - - do { - if (asmc_pio_getbyindex_locked(dev, index, key_buf) == 0 && - asmc_pio_getinfo_locked(dev, key_buf, info_buf) == 0) { - error = 0; - break; - } - } while (++try < ASMC_MAXRETRIES); - - mtx_unlock_spin(&sc->sc_mtx); - - if (error != 0) - return (error); - - memcpy(key_out, key_buf, ASMC_KEYLEN); - key_out[ASMC_KEYLEN] = '\0'; - asmc_keyinfo_decode(info_buf, len_out, type_out); - - return (0); + return (asmc_pio_key_dump_by_index(dev, index, key_out, type_out, + len_out)); } /* @@ -1729,45 +1496,11 @@ int asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len) { struct asmc_softc *sc = device_get_softc(dev); - int i, error = -1, try = 0; if (sc->sc_is_mmio) return (asmc_mmio_key_write(dev, key, buf, len)); - mtx_lock_spin(&sc->sc_mtx); - -begin: - ASMC_DPRINTF(("cmd port: cmd write\n")); - if (asmc_command(dev, ASMC_CMDWRITE)) - goto out; - - ASMC_DPRINTF(("data port: key\n")); - if (asmc_pio_send(dev, (const uint8_t *)key, ASMC_KEYLEN, - ASMC_STATUS_AWAIT_DATA)) - goto out; - - ASMC_DPRINTF(("data port: length\n")); - ASMC_DATAPORT_WRITE(sc, len); - - ASMC_DPRINTF(("data port: buffer\n")); - for (i = 0; i < len; i++) { - if (asmc_wait(dev, ASMC_STATUS_AWAIT_DATA)) - goto out; - ASMC_DATAPORT_WRITE(sc, buf[i]); - } - - error = 0; -out: - if (error) { - if (++try < 10) - goto begin; - device_printf(dev, "%s for key %s failed %d times, giving up\n", - __func__, key, try); - } - - mtx_unlock_spin(&sc->sc_mtx); - - return (error); + return (asmc_pio_key_write(dev, key, buf, len)); } /* diff --git a/sys/dev/asmc/asmc_pio.c b/sys/dev/asmc/asmc_pio.c new file mode 100644 index 000000000000..75ff8dbd3522 --- /dev/null +++ b/sys/dev/asmc/asmc_pio.c @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2026 Abdelkader Boudih + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * PIO backend for Apple SMC (pre-T2 Macs). + * + * The SMC is reached through three I/O ports: a data port, a command port + * carrying the status nibble, and an interrupt source port. Every + * transaction is a command byte, a key name, a length, and then the value, + * with a status handshake between each step. + */ + +#include "opt_asmc.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +/* + * We need to make sure that the SMC acks the byte sent. + * Just wait up to (amount * 10) ms. + */ +static int +asmc_wait_ack(device_t dev, uint8_t val, int amount) +{ + struct asmc_softc *sc = device_get_softc(dev); + u_int i; + + val = val & ASMC_STATUS_MASK; + + for (i = 0; i < amount; i++) { + if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val) + return (0); + DELAY(10); + } + + return (1); +} + +/* + * We need to make sure that the SMC acks the byte sent. + * Just wait up to 100 ms. + */ +static int +asmc_wait(device_t dev, uint8_t val) +{ +#ifdef ASMC_DEBUG + struct asmc_softc *sc; +#endif + + if (asmc_wait_ack(dev, val, 1000) == 0) + return (0); + +#ifdef ASMC_DEBUG + sc = device_get_softc(dev); + + device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, + val & ASMC_STATUS_MASK, ASMC_CMDPORT_READ(sc)); +#endif + return (1); +} + +/* + * Send the given command, retrying up to 10 times if + * the acknowledgement fails. + */ +static int +asmc_command(device_t dev, uint8_t command) +{ + int i; + struct asmc_softc *sc = device_get_softc(dev); + + for (i = 0; i < 10; i++) { + ASMC_CMDPORT_WRITE(sc, command); + if (asmc_wait_ack(dev, 0x0c, 100) == 0) { + return (0); + } + } + +#ifdef ASMC_DEBUG + device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command, + ASMC_CMDPORT_READ(sc)); +#endif + return (1); +} + +/* Push len bytes out the data port, awaiting status after each. */ +static int +asmc_pio_send(device_t dev, const uint8_t *buf, int len, uint8_t status) +{ + struct asmc_softc *sc = device_get_softc(dev); + + for (int i = 0; i < len; i++) { + ASMC_DATAPORT_WRITE(sc, buf[i]); + if (asmc_wait(dev, status)) + return (1); + } + + return (0); +} + +/* Await status then pull each of len bytes from the data port. */ +static int +asmc_pio_recv(device_t dev, uint8_t *buf, int len, uint8_t status) +{ + struct asmc_softc *sc = device_get_softc(dev); + + for (int i = 0; i < len; i++) { + if (asmc_wait(dev, status)) + return (1); + buf[i] = ASMC_DATAPORT_READ(sc); + } + + return (0); +} + +/* Fetch the name of the index'th key; the caller holds sc_mtx. */ +static int +asmc_pio_getbyindex_locked(device_t dev, int index, uint8_t *key) +{ + struct asmc_softc *sc = device_get_softc(dev); + uint8_t index_buf[ASMC_KEYLEN]; + + index_buf[0] = (index >> 24) & 0xff; + index_buf[1] = (index >> 16) & 0xff; + index_buf[2] = (index >> 8) & 0xff; + index_buf[3] = index & 0xff; + + if (asmc_command(dev, ASMC_CMDGETBYINDEX)) + return (1); + + if (asmc_pio_send(dev, index_buf, ASMC_KEYLEN, ASMC_STATUS_AWAIT_DATA)) + return (1); + + ASMC_DATAPORT_WRITE(sc, ASMC_KEYLEN); + + return (asmc_pio_recv(dev, key, ASMC_KEYLEN, ASMC_STATUS_DATA_READY)); +} + +/* Fetch the raw key info response; the caller holds sc_mtx. */ +static int +asmc_pio_getinfo_locked(device_t dev, const uint8_t *key, uint8_t *info) +{ + struct asmc_softc *sc = device_get_softc(dev); + + if (asmc_command(dev, ASMC_CMDGETINFO)) + return (1); + + if (asmc_pio_send(dev, key, ASMC_KEYLEN, ASMC_STATUS_AWAIT_DATA)) + return (1); + + ASMC_DATAPORT_WRITE(sc, ASMC_KEYINFO_RESPLEN); + + return (asmc_pio_recv(dev, info, ASMC_KEYINFO_RESPLEN, + ASMC_STATUS_DATA_READY)); +} + +/* Split a key info response; type needs ASMC_TYPELEN + 1 bytes. */ +static void +asmc_keyinfo_decode(const uint8_t *info, uint8_t *len, char *type) +{ + if (len != NULL) + *len = info[0]; + if (type != NULL) { + memcpy(type, &info[1], ASMC_TYPELEN); + type[ASMC_TYPELEN] = '\0'; + } +} + +int +asmc_pio_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len) +{ + struct asmc_softc *sc = device_get_softc(dev); + int error = 1, try = 0; + + mtx_lock_spin(&sc->sc_mtx); + +begin: + if (asmc_command(dev, ASMC_CMDREAD)) + goto out; + + if (asmc_pio_send(dev, (const uint8_t *)key, ASMC_KEYLEN, + ASMC_STATUS_AWAIT_DATA)) + goto out; + + ASMC_DATAPORT_WRITE(sc, len); + + if (asmc_pio_recv(dev, buf, len, ASMC_STATUS_DATA_READY)) + goto out; + + error = 0; +out: + if (error) { + if (++try < 10) + goto begin; + device_printf(dev, "%s for key %s failed %d times, giving up\n", + __func__, key, try); + } + + mtx_unlock_spin(&sc->sc_mtx); + + return (error); +} + +/* + * Get key info (length and type) from SMC using command 0x13. + * If len is non-NULL, stores the key's value length. + * If type is non-NULL, stores the 4-char type string (must be at least 5 bytes). + */ +int +asmc_pio_key_getinfo(device_t dev, const char *key, uint8_t *len, char *type) +{ + struct asmc_softc *sc = device_get_softc(dev); + uint8_t info[ASMC_KEYINFO_RESPLEN]; + int error = -1, try = 0; + + mtx_lock_spin(&sc->sc_mtx); + + do { + if (asmc_pio_getinfo_locked(dev, (const uint8_t *)key, + info) == 0) { + error = 0; + break; + } + } while (++try < ASMC_MAXRETRIES); + + mtx_unlock_spin(&sc->sc_mtx); + + if (error == 0) + asmc_keyinfo_decode(info, len, type); + + return (error); +} + +/* + * Helper function to get key info by index (for sensor detection). + */ +int +asmc_pio_key_dump_by_index(device_t dev, int index, char *key_out, + char *type_out, uint8_t *len_out) +{ + struct asmc_softc *sc = device_get_softc(dev); + uint8_t key_buf[ASMC_KEYLEN]; + uint8_t info_buf[ASMC_KEYINFO_RESPLEN]; + int error = ENXIO, try = 0; + + mtx_lock_spin(&sc->sc_mtx); + + do { + if (asmc_pio_getbyindex_locked(dev, index, key_buf) == 0 && + asmc_pio_getinfo_locked(dev, key_buf, info_buf) == 0) { + error = 0; + break; + } + } while (++try < ASMC_MAXRETRIES); + + mtx_unlock_spin(&sc->sc_mtx); + + if (error != 0) + return (error); + + memcpy(key_out, key_buf, ASMC_KEYLEN); + key_out[ASMC_KEYLEN] = '\0'; + asmc_keyinfo_decode(info_buf, len_out, type_out); + + return (0); +} + +int +asmc_pio_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len) +{ + struct asmc_softc *sc = device_get_softc(dev); + int i, error = -1, try = 0; + + mtx_lock_spin(&sc->sc_mtx); + +begin: + ASMC_DPRINTF(("cmd port: cmd write\n")); + if (asmc_command(dev, ASMC_CMDWRITE)) + goto out; + + ASMC_DPRINTF(("data port: key\n")); + if (asmc_pio_send(dev, (const uint8_t *)key, ASMC_KEYLEN, + ASMC_STATUS_AWAIT_DATA)) + goto out; + + ASMC_DPRINTF(("data port: length\n")); + ASMC_DATAPORT_WRITE(sc, len); + + ASMC_DPRINTF(("data port: buffer\n")); + for (i = 0; i < len; i++) { + if (asmc_wait(dev, ASMC_STATUS_AWAIT_DATA)) + goto out; + ASMC_DATAPORT_WRITE(sc, buf[i]); + } + + error = 0; +out: + if (error) { + if (++try < 10) + goto begin; + device_printf(dev, "%s for key %s failed %d times, giving up\n", + __func__, key, try); + } + + mtx_unlock_spin(&sc->sc_mtx); + + return (error); +} diff --git a/sys/dev/asmc/asmc_pio.h b/sys/dev/asmc/asmc_pio.h new file mode 100644 index 000000000000..857066da6141 --- /dev/null +++ b/sys/dev/asmc/asmc_pio.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2026 Abdelkader Boudih + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef _DEV_ASMC_ASMC_PIO_H_ +#define _DEV_ASMC_ASMC_PIO_H_ + +struct asmc_softc; + +/* + * PIO backend functions. + * + * These mirror the MMIO entry points, except that getbyindex and getinfo are + * one call here: the PIO protocol needs both halves inside a single hold of + * sc_mtx, so the key name cannot be returned separately. + */ +int asmc_pio_key_read(device_t dev, const char *key, + uint8_t *buf, uint8_t len); +int asmc_pio_key_write(device_t dev, const char *key, + uint8_t *buf, uint8_t len); +int asmc_pio_key_getinfo(device_t dev, const char *key, + uint8_t *len, char *type); +int asmc_pio_key_dump_by_index(device_t dev, int index, char *key, + char *type, uint8_t *len); + +#endif /* _DEV_ASMC_ASMC_PIO_H_ */ diff --git a/sys/modules/asmc/Makefile b/sys/modules/asmc/Makefile index f29c3b3a245c..f18f23c88966 100644 --- a/sys/modules/asmc/Makefile +++ b/sys/modules/asmc/Makefile @@ -1,7 +1,7 @@ .PATH: ${SRCTOP}/sys/dev/asmc KMOD= asmc -SRCS= asmc.c asmcmmio.c opt_acpi.h opt_asmc.h +SRCS= asmc.c asmc_pio.c asmcmmio.c opt_acpi.h opt_asmc.h SRCS+= acpi_if.h backlight_if.h bus_if.h device_if.h .if defined(ASMC_DEBUG)