From 759f40f5cf3b7f2f36d28406ffc86935f112adce Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sun, 1 Feb 2026 18:44:45 -0800 Subject: [PATCH 1/6] rm -rf drivers/spi/; kernel driver isn't needed --- drivers/spi/.gitignore | 7 - drivers/spi/Makefile | 14 - drivers/spi/load.sh | 27 -- drivers/spi/patch | 33 -- drivers/spi/pull-src.sh | 12 - drivers/spi/spi_panda.h | 160 ------- drivers/spi/spidev_panda.c | 891 ------------------------------------- 7 files changed, 1144 deletions(-) delete mode 100644 drivers/spi/.gitignore delete mode 100644 drivers/spi/Makefile delete mode 100755 drivers/spi/load.sh delete mode 100644 drivers/spi/patch delete mode 100755 drivers/spi/pull-src.sh delete mode 100644 drivers/spi/spi_panda.h delete mode 100644 drivers/spi/spidev_panda.c diff --git a/drivers/spi/.gitignore b/drivers/spi/.gitignore deleted file mode 100644 index 49dc09d8..00000000 --- a/drivers/spi/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -spidev.c -*.ko -*.cmd -*.mod -*.symvers -*.order -*.mod.c diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile deleted file mode 100644 index 9a2a0abf..00000000 --- a/drivers/spi/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -obj-m += spidev_panda.o - -KDIR := /lib/modules/$(shell uname -r)/build -PWD := $(shell pwd) - -# GCC9 bug, apply kernel patch instead? -# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0b999ae3614d09d97a1575936bcee884f912b10e -ccflags-y := -Wno-missing-attributes - -default: - $(MAKE) -C $(KDIR) M=$(PWD) modules - -clean: - $(MAKE) -C $(KDIR) M=$(PWD) clean diff --git a/drivers/spi/load.sh b/drivers/spi/load.sh deleted file mode 100755 index 910ac872..00000000 --- a/drivers/spi/load.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env bash -set -e - -DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" -cd $DIR - -make -j8 - -sudo su -c "echo spi0.0 > /sys/bus/spi/drivers/spidev/unbind" || true - -sudo dmesg -C - -#sudo rmmod -f spidev_panda -sudo rmmod spidev_panda || true -sudo insmod spidev_panda.ko - -sudo su -c "echo 'file $DIR/spidev_panda.c +p' > /sys/kernel/debug/dynamic_debug/control" -sudo su -c "echo 'file $DIR/spi_panda.h +p' > /sys/kernel/debug/dynamic_debug/control" - -sudo lsmod - -echo "loaded" -ls -la /dev/spi* -sudo chmod 666 /dev/spi* -ipython -c "from panda import Panda; print(Panda.list())" -KERN=1 ipython -c "from panda import Panda; print(Panda.list())" -dmesg diff --git a/drivers/spi/patch b/drivers/spi/patch deleted file mode 100644 index a1463034..00000000 --- a/drivers/spi/patch +++ /dev/null @@ -1,33 +0,0 @@ -53c53,54 -< #define SPIDEV_MAJOR 153 /* assigned */ ---- -> int SPIDEV_MAJOR = 0; -> //#define SPIDEV_MAJOR 153 /* assigned */ -354a356,358 -> -> #include "spi_panda.h" -> -413,414c417,419 -< retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0, -< (__u8 __user *)arg); ---- -> retval = panda_transfer(spidev, spi, arg); -> //retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0, -> // (__u8 __user *)arg); -697,698d701 -< { .compatible = "rohm,dh2228fv" }, -< { .compatible = "lineartechnology,ltc2488" }, -831c834 -< .name = "spidev", ---- -> .name = "spidev_panda", -856c859 -< status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops); ---- -> status = register_chrdev(0, "spi", &spidev_fops); -860c863,865 -< spidev_class = class_create(THIS_MODULE, "spidev"); ---- -> SPIDEV_MAJOR = status; -> -> spidev_class = class_create(THIS_MODULE, "spidev_panda"); diff --git a/drivers/spi/pull-src.sh b/drivers/spi/pull-src.sh deleted file mode 100755 index 64dac9bc..00000000 --- a/drivers/spi/pull-src.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash -set -e - -DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" -cd $DIR - -rm -f spidev.c -wget https://raw.githubusercontent.com/commaai/agnos-kernel-sdm845/master/drivers/spi/spidev.c - -# diff spidev.c spidev_panda.c > patch -# git diff --no-index spidev.c spidev_panda.c -patch -o spidev_panda.c spidev.c -i patch diff --git a/drivers/spi/spi_panda.h b/drivers/spi/spi_panda.h deleted file mode 100644 index c7da681f..00000000 --- a/drivers/spi/spi_panda.h +++ /dev/null @@ -1,160 +0,0 @@ -#include -#include -#include - -#define SPI_SYNC 0x5AU -#define SPI_HACK 0x79U -#define SPI_DACK 0x85U -#define SPI_NACK 0x1FU -#define SPI_CHECKSUM_START 0xABU - -struct __attribute__((packed)) spi_header { - u8 sync; - u8 endpoint; - uint16_t tx_len; - uint16_t max_rx_len; -}; - -struct spi_panda_transfer { - __u64 rx_buf; - __u64 tx_buf; - __u32 tx_length; - __u32 rx_length_max; - __u32 timeout; - __u8 endpoint; - __u8 expect_disconnect; -}; - -static u8 panda_calc_checksum(u8 *buf, u16 length) { - int i; - u8 checksum = SPI_CHECKSUM_START; - for (i = 0U; i < length; i++) { - checksum ^= buf[i]; - } - return checksum; -} - -static long panda_wait_for_ack(struct spidev_data *spidev, u8 ack_val, u8 length) { - int i; - int ret; - for (i = 0; i < 1000; i++) { - ret = spidev_sync_read(spidev, length); - if (ret < 0) { - return ret; - } - - if (spidev->rx_buffer[0] == ack_val) { - return 0; - } else if (spidev->rx_buffer[0] == SPI_NACK) { - return -2; - } - if (i > 20) usleep_range(10, 20); - } - return -1; -} - -static long panda_transfer_raw(struct spidev_data *spidev, struct spi_device *spi, unsigned long arg) { - u16 rx_len; - long retval = -1; - struct spi_header header; - struct spi_panda_transfer pt; - - struct spi_transfer t = { - .len = 0, - .tx_buf = spidev->tx_buffer, - .rx_buf = spidev->rx_buffer, - .speed_hz = spidev->spi->max_speed_hz, - }; - - struct spi_message m; - spi_message_init(&m); - spi_message_add_tail(&t, &m); - - // read struct from user - if (!access_ok(VERIFY_WRITE, arg, sizeof(pt))) { - return -1; - } - if (copy_from_user(&pt, (void __user *)arg, sizeof(pt))) { - return -1; - } - dev_dbg(&spi->dev, "ep: %d, tx len: %d\n", pt.endpoint, pt.tx_length); - - // send header - header.sync = 0x5a; - header.endpoint = pt.endpoint; - header.tx_len = pt.tx_length; - header.max_rx_len = pt.rx_length_max; - memcpy(spidev->tx_buffer, &header, sizeof(header)); - spidev->tx_buffer[sizeof(header)] = panda_calc_checksum(spidev->tx_buffer, sizeof(header)); - - t.len = sizeof(header) + 1; - retval = spidev_sync(spidev, &m); - if (retval < 0) { - dev_dbg(&spi->dev, "spi xfer failed %ld\n", retval); - return retval; - } - - // wait for ACK - retval = panda_wait_for_ack(spidev, SPI_HACK, 1); - if (retval < 0) { - dev_dbg(&spi->dev, "no header ack %ld\n", retval); - return retval; - } - - // send data - dev_dbg(&spi->dev, "sending data\n"); - retval = copy_from_user(spidev->tx_buffer, (const u8 __user *)(uintptr_t)pt.tx_buf, pt.tx_length); - spidev->tx_buffer[pt.tx_length] = panda_calc_checksum(spidev->tx_buffer, pt.tx_length); - t.len = pt.tx_length + 1; - retval = spidev_sync(spidev, &m); - - if (pt.expect_disconnect) { - return 0; - } - - // wait for ACK - retval = panda_wait_for_ack(spidev, SPI_DACK, 3); - if (retval < 0) { - dev_dbg(&spi->dev, "no data ack\n"); - return retval; - } - - // get response - t.rx_buf = spidev->rx_buffer + 3; - rx_len = (spidev->rx_buffer[2] << 8) | (spidev->rx_buffer[1]); - dev_dbg(&spi->dev, "rx len %u\n", rx_len); - if (rx_len > pt.rx_length_max) { - dev_dbg(&spi->dev, "RX len greater than max\n"); - return -1; - } - - // do the read - t.len = rx_len + 1; - retval = spidev_sync(spidev, &m); - if (retval < 0) { - dev_dbg(&spi->dev, "spi xfer failed %ld\n", retval); - return retval; - } - if (panda_calc_checksum(spidev->rx_buffer, 3 + rx_len + 1) != 0) { - dev_dbg(&spi->dev, "bad checksum\n"); - return -1; - } - - retval = copy_to_user((u8 __user *)(uintptr_t)pt.rx_buf, spidev->rx_buffer + 3, rx_len); - - return rx_len; -} - -static long panda_transfer(struct spidev_data *spidev, struct spi_device *spi, unsigned long arg) { - int i; - int ret; - dev_dbg(&spi->dev, "=== XFER start ===\n"); - for (i = 0; i < 20; i++) { - ret = panda_transfer_raw(spidev, spi, arg); - if (ret >= 0) { - break; - } - } - dev_dbg(&spi->dev, "took %d tries\n", i+1); - return ret; -} diff --git a/drivers/spi/spidev_panda.c b/drivers/spi/spidev_panda.c deleted file mode 100644 index f21fe338..00000000 --- a/drivers/spi/spidev_panda.c +++ /dev/null @@ -1,891 +0,0 @@ -/* - * Simple synchronous userspace interface to SPI devices - * - * Copyright (C) 2006 SWAPP - * Andrea Paterniani - * Copyright (C) 2007 David Brownell (simplification, cleanup) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - - -/* - * This supports access to SPI devices using normal userspace I/O calls. - * Note that while traditional UNIX/POSIX I/O semantics are half duplex, - * and often mask message boundaries, full SPI support requires full duplex - * transfers. There are several kinds of internal message boundaries to - * handle chipselect management and other protocol options. - * - * SPI has a character major number assigned. We allocate minor numbers - * dynamically using a bitmask. You must use hotplug tools, such as udev - * (or mdev with busybox) to create and destroy the /dev/spidevB.C device - * nodes, since there is no fixed association of minor numbers with any - * particular SPI bus or device. - */ -int SPIDEV_MAJOR = 0; -//#define SPIDEV_MAJOR 153 /* assigned */ -#define N_SPI_MINORS 32 /* ... up to 256 */ - -static DECLARE_BITMAP(minors, N_SPI_MINORS); - - -/* Bit masks for spi_device.mode management. Note that incorrect - * settings for some settings can cause *lots* of trouble for other - * devices on a shared bus: - * - * - CS_HIGH ... this device will be active when it shouldn't be - * - 3WIRE ... when active, it won't behave as it should - * - NO_CS ... there will be no explicit message boundaries; this - * is completely incompatible with the shared bus model - * - READY ... transfers may proceed when they shouldn't. - * - * REVISIT should changing those flags be privileged? - */ -#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \ - | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \ - | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \ - | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD) - -struct spidev_data { - dev_t devt; - spinlock_t spi_lock; - struct spi_device *spi; - struct list_head device_entry; - - /* TX/RX buffers are NULL unless this device is open (users > 0) */ - struct mutex buf_lock; - unsigned users; - u8 *tx_buffer; - u8 *rx_buffer; - u32 speed_hz; -}; - -static LIST_HEAD(device_list); -static DEFINE_MUTEX(device_list_lock); - -static unsigned bufsiz = 4096; -module_param(bufsiz, uint, S_IRUGO); -MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message"); - -/*-------------------------------------------------------------------------*/ - -static ssize_t -spidev_sync(struct spidev_data *spidev, struct spi_message *message) -{ - DECLARE_COMPLETION_ONSTACK(done); - int status; - struct spi_device *spi; - - spin_lock_irq(&spidev->spi_lock); - spi = spidev->spi; - spin_unlock_irq(&spidev->spi_lock); - - if (spi == NULL) - status = -ESHUTDOWN; - else - status = spi_sync(spi, message); - - if (status == 0) - status = message->actual_length; - - return status; -} - -static inline ssize_t -spidev_sync_write(struct spidev_data *spidev, size_t len) -{ - struct spi_transfer t = { - .tx_buf = spidev->tx_buffer, - .len = len, - .speed_hz = spidev->speed_hz, - }; - struct spi_message m; - - spi_message_init(&m); - spi_message_add_tail(&t, &m); - return spidev_sync(spidev, &m); -} - -static inline ssize_t -spidev_sync_read(struct spidev_data *spidev, size_t len) -{ - struct spi_transfer t = { - .rx_buf = spidev->rx_buffer, - .len = len, - .speed_hz = spidev->speed_hz, - }; - struct spi_message m; - - spi_message_init(&m); - spi_message_add_tail(&t, &m); - return spidev_sync(spidev, &m); -} - -/*-------------------------------------------------------------------------*/ - -/* Read-only message with current device setup */ -static ssize_t -spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) -{ - struct spidev_data *spidev; - ssize_t status = 0; - - /* chipselect only toggles at start or end of operation */ - if (count > bufsiz) - return -EMSGSIZE; - - spidev = filp->private_data; - - mutex_lock(&spidev->buf_lock); - status = spidev_sync_read(spidev, count); - if (status > 0) { - unsigned long missing; - - missing = copy_to_user(buf, spidev->rx_buffer, status); - if (missing == status) - status = -EFAULT; - else - status = status - missing; - } - mutex_unlock(&spidev->buf_lock); - - return status; -} - -/* Write-only message with current device setup */ -static ssize_t -spidev_write(struct file *filp, const char __user *buf, - size_t count, loff_t *f_pos) -{ - struct spidev_data *spidev; - ssize_t status = 0; - unsigned long missing; - - /* chipselect only toggles at start or end of operation */ - if (count > bufsiz) - return -EMSGSIZE; - - spidev = filp->private_data; - - mutex_lock(&spidev->buf_lock); - missing = copy_from_user(spidev->tx_buffer, buf, count); - if (missing == 0) - status = spidev_sync_write(spidev, count); - else - status = -EFAULT; - mutex_unlock(&spidev->buf_lock); - - return status; -} - -static int spidev_message(struct spidev_data *spidev, - struct spi_ioc_transfer *u_xfers, unsigned n_xfers) -{ - struct spi_message msg; - struct spi_transfer *k_xfers; - struct spi_transfer *k_tmp; - struct spi_ioc_transfer *u_tmp; - unsigned n, total, tx_total, rx_total; - u8 *tx_buf, *rx_buf; - int status = -EFAULT; - - spi_message_init(&msg); - k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL); - if (k_xfers == NULL) - return -ENOMEM; - - /* Construct spi_message, copying any tx data to bounce buffer. - * We walk the array of user-provided transfers, using each one - * to initialize a kernel version of the same transfer. - */ - tx_buf = spidev->tx_buffer; - rx_buf = spidev->rx_buffer; - total = 0; - tx_total = 0; - rx_total = 0; - for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; - n; - n--, k_tmp++, u_tmp++) { - k_tmp->len = u_tmp->len; - - total += k_tmp->len; - /* Since the function returns the total length of transfers - * on success, restrict the total to positive int values to - * avoid the return value looking like an error. Also check - * each transfer length to avoid arithmetic overflow. - */ - if (total > INT_MAX || k_tmp->len > INT_MAX) { - status = -EMSGSIZE; - goto done; - } - - if (u_tmp->rx_buf) { - /* this transfer needs space in RX bounce buffer */ - rx_total += k_tmp->len; - if (rx_total > bufsiz) { - status = -EMSGSIZE; - goto done; - } - k_tmp->rx_buf = rx_buf; - if (!access_ok(VERIFY_WRITE, (u8 __user *) - (uintptr_t) u_tmp->rx_buf, - u_tmp->len)) - goto done; - rx_buf += k_tmp->len; - } - if (u_tmp->tx_buf) { - /* this transfer needs space in TX bounce buffer */ - tx_total += k_tmp->len; - if (tx_total > bufsiz) { - status = -EMSGSIZE; - goto done; - } - k_tmp->tx_buf = tx_buf; - if (copy_from_user(tx_buf, (const u8 __user *) - (uintptr_t) u_tmp->tx_buf, - u_tmp->len)) - goto done; - tx_buf += k_tmp->len; - } - - k_tmp->cs_change = !!u_tmp->cs_change; - k_tmp->tx_nbits = u_tmp->tx_nbits; - k_tmp->rx_nbits = u_tmp->rx_nbits; - k_tmp->bits_per_word = u_tmp->bits_per_word; - k_tmp->delay_usecs = u_tmp->delay_usecs; - k_tmp->speed_hz = u_tmp->speed_hz; - if (!k_tmp->speed_hz) - k_tmp->speed_hz = spidev->speed_hz; -#ifdef VERBOSE - dev_dbg(&spidev->spi->dev, - " xfer len %u %s%s%s%dbits %u usec %uHz\n", - u_tmp->len, - u_tmp->rx_buf ? "rx " : "", - u_tmp->tx_buf ? "tx " : "", - u_tmp->cs_change ? "cs " : "", - u_tmp->bits_per_word ? : spidev->spi->bits_per_word, - u_tmp->delay_usecs, - u_tmp->speed_hz ? : spidev->spi->max_speed_hz); -#endif - spi_message_add_tail(k_tmp, &msg); - } - - status = spidev_sync(spidev, &msg); - if (status < 0) - goto done; - - /* copy any rx data out of bounce buffer */ - rx_buf = spidev->rx_buffer; - for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) { - if (u_tmp->rx_buf) { - if (__copy_to_user((u8 __user *) - (uintptr_t) u_tmp->rx_buf, rx_buf, - u_tmp->len)) { - status = -EFAULT; - goto done; - } - rx_buf += u_tmp->len; - } - } - status = total; - -done: - kfree(k_xfers); - return status; -} - -static struct spi_ioc_transfer * -spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc, - unsigned *n_ioc) -{ - struct spi_ioc_transfer *ioc; - u32 tmp; - - /* Check type, command number and direction */ - if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC - || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0)) - || _IOC_DIR(cmd) != _IOC_WRITE) - return ERR_PTR(-ENOTTY); - - tmp = _IOC_SIZE(cmd); - if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) - return ERR_PTR(-EINVAL); - *n_ioc = tmp / sizeof(struct spi_ioc_transfer); - if (*n_ioc == 0) - return NULL; - - /* copy into scratch area */ - ioc = kmalloc(tmp, GFP_KERNEL); - if (!ioc) - return ERR_PTR(-ENOMEM); - if (__copy_from_user(ioc, u_ioc, tmp)) { - kfree(ioc); - return ERR_PTR(-EFAULT); - } - return ioc; -} - - -#include "spi_panda.h" - -static long -spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) -{ - int err = 0; - int retval = 0; - struct spidev_data *spidev; - struct spi_device *spi; - u32 tmp; - unsigned n_ioc; - struct spi_ioc_transfer *ioc; - - /* Check type and command number */ - if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC) - return -ENOTTY; - - /* Check access direction once here; don't repeat below. - * IOC_DIR is from the user perspective, while access_ok is - * from the kernel perspective; so they look reversed. - */ - if (_IOC_DIR(cmd) & _IOC_READ) - err = !access_ok(VERIFY_WRITE, - (void __user *)arg, _IOC_SIZE(cmd)); - if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE) - err = !access_ok(VERIFY_READ, - (void __user *)arg, _IOC_SIZE(cmd)); - if (err) - return -EFAULT; - - /* guard against device removal before, or while, - * we issue this ioctl. - */ - spidev = filp->private_data; - spin_lock_irq(&spidev->spi_lock); - spi = spi_dev_get(spidev->spi); - spin_unlock_irq(&spidev->spi_lock); - - if (spi == NULL) - return -ESHUTDOWN; - - /* use the buffer lock here for triple duty: - * - prevent I/O (from us) so calling spi_setup() is safe; - * - prevent concurrent SPI_IOC_WR_* from morphing - * data fields while SPI_IOC_RD_* reads them; - * - SPI_IOC_MESSAGE needs the buffer locked "normally". - */ - mutex_lock(&spidev->buf_lock); - - switch (cmd) { - /* read requests */ - case SPI_IOC_RD_MODE: - retval = __put_user(spi->mode & SPI_MODE_MASK, - (__u8 __user *)arg); - break; - case SPI_IOC_RD_MODE32: - retval = __put_user(spi->mode & SPI_MODE_MASK, - (__u32 __user *)arg); - break; - case SPI_IOC_RD_LSB_FIRST: - retval = panda_transfer(spidev, spi, arg); - //retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0, - // (__u8 __user *)arg); - break; - case SPI_IOC_RD_BITS_PER_WORD: - retval = __put_user(spi->bits_per_word, (__u8 __user *)arg); - break; - case SPI_IOC_RD_MAX_SPEED_HZ: - retval = __put_user(spidev->speed_hz, (__u32 __user *)arg); - break; - - /* write requests */ - case SPI_IOC_WR_MODE: - case SPI_IOC_WR_MODE32: - if (cmd == SPI_IOC_WR_MODE) - retval = __get_user(tmp, (u8 __user *)arg); - else - retval = __get_user(tmp, (u32 __user *)arg); - if (retval == 0) { - u32 save = spi->mode; - - if (tmp & ~SPI_MODE_MASK) { - retval = -EINVAL; - break; - } - - tmp |= spi->mode & ~SPI_MODE_MASK; - spi->mode = (u16)tmp; - retval = spi_setup(spi); - if (retval < 0) - spi->mode = save; - else - dev_dbg(&spi->dev, "spi mode %x\n", tmp); - } - break; - case SPI_IOC_WR_LSB_FIRST: - retval = __get_user(tmp, (__u8 __user *)arg); - if (retval == 0) { - u32 save = spi->mode; - - if (tmp) - spi->mode |= SPI_LSB_FIRST; - else - spi->mode &= ~SPI_LSB_FIRST; - retval = spi_setup(spi); - if (retval < 0) - spi->mode = save; - else - dev_dbg(&spi->dev, "%csb first\n", - tmp ? 'l' : 'm'); - } - break; - case SPI_IOC_WR_BITS_PER_WORD: - retval = __get_user(tmp, (__u8 __user *)arg); - if (retval == 0) { - u8 save = spi->bits_per_word; - - spi->bits_per_word = tmp; - retval = spi_setup(spi); - if (retval < 0) - spi->bits_per_word = save; - else - dev_dbg(&spi->dev, "%d bits per word\n", tmp); - } - break; - case SPI_IOC_WR_MAX_SPEED_HZ: - retval = __get_user(tmp, (__u32 __user *)arg); - if (retval == 0) { - u32 save = spi->max_speed_hz; - - spi->max_speed_hz = tmp; - retval = spi_setup(spi); - if (retval >= 0) - spidev->speed_hz = tmp; - else - dev_dbg(&spi->dev, "%d Hz (max)\n", tmp); - spi->max_speed_hz = save; - } - break; - - default: - /* segmented and/or full-duplex I/O request */ - /* Check message and copy into scratch area */ - ioc = spidev_get_ioc_message(cmd, - (struct spi_ioc_transfer __user *)arg, &n_ioc); - if (IS_ERR(ioc)) { - retval = PTR_ERR(ioc); - break; - } - if (!ioc) - break; /* n_ioc is also 0 */ - - /* translate to spi_message, execute */ - retval = spidev_message(spidev, ioc, n_ioc); - kfree(ioc); - break; - } - - mutex_unlock(&spidev->buf_lock); - spi_dev_put(spi); - return retval; -} - -#ifdef CONFIG_COMPAT -static long -spidev_compat_ioc_message(struct file *filp, unsigned int cmd, - unsigned long arg) -{ - struct spi_ioc_transfer __user *u_ioc; - int retval = 0; - struct spidev_data *spidev; - struct spi_device *spi; - unsigned n_ioc, n; - struct spi_ioc_transfer *ioc; - - u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg); - if (!access_ok(VERIFY_READ, u_ioc, _IOC_SIZE(cmd))) - return -EFAULT; - - /* guard against device removal before, or while, - * we issue this ioctl. - */ - spidev = filp->private_data; - spin_lock_irq(&spidev->spi_lock); - spi = spi_dev_get(spidev->spi); - spin_unlock_irq(&spidev->spi_lock); - - if (spi == NULL) - return -ESHUTDOWN; - - /* SPI_IOC_MESSAGE needs the buffer locked "normally" */ - mutex_lock(&spidev->buf_lock); - - /* Check message and copy into scratch area */ - ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc); - if (IS_ERR(ioc)) { - retval = PTR_ERR(ioc); - goto done; - } - if (!ioc) - goto done; /* n_ioc is also 0 */ - - /* Convert buffer pointers */ - for (n = 0; n < n_ioc; n++) { - ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf); - ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf); - } - - /* translate to spi_message, execute */ - retval = spidev_message(spidev, ioc, n_ioc); - kfree(ioc); - -done: - mutex_unlock(&spidev->buf_lock); - spi_dev_put(spi); - return retval; -} - -static long -spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) -{ - if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC - && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0)) - && _IOC_DIR(cmd) == _IOC_WRITE) - return spidev_compat_ioc_message(filp, cmd, arg); - - return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); -} -#else -#define spidev_compat_ioctl NULL -#endif /* CONFIG_COMPAT */ - -static int spidev_open(struct inode *inode, struct file *filp) -{ - struct spidev_data *spidev; - int status = -ENXIO; - - mutex_lock(&device_list_lock); - - list_for_each_entry(spidev, &device_list, device_entry) { - if (spidev->devt == inode->i_rdev) { - status = 0; - break; - } - } - - if (status) { - pr_debug("spidev: nothing for minor %d\n", iminor(inode)); - goto err_find_dev; - } - - if (!spidev->tx_buffer) { - spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL); - if (!spidev->tx_buffer) { - dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); - status = -ENOMEM; - goto err_find_dev; - } - } - - if (!spidev->rx_buffer) { - spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL); - if (!spidev->rx_buffer) { - dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); - status = -ENOMEM; - goto err_alloc_rx_buf; - } - } - - spidev->users++; - filp->private_data = spidev; - nonseekable_open(inode, filp); - - mutex_unlock(&device_list_lock); - return 0; - -err_alloc_rx_buf: - kfree(spidev->tx_buffer); - spidev->tx_buffer = NULL; -err_find_dev: - mutex_unlock(&device_list_lock); - return status; -} - -static int spidev_release(struct inode *inode, struct file *filp) -{ - struct spidev_data *spidev; - - mutex_lock(&device_list_lock); - spidev = filp->private_data; - filp->private_data = NULL; - - /* last close? */ - spidev->users--; - if (!spidev->users) { - int dofree; - - kfree(spidev->tx_buffer); - spidev->tx_buffer = NULL; - - kfree(spidev->rx_buffer); - spidev->rx_buffer = NULL; - - spin_lock_irq(&spidev->spi_lock); - if (spidev->spi) - spidev->speed_hz = spidev->spi->max_speed_hz; - - /* ... after we unbound from the underlying device? */ - dofree = (spidev->spi == NULL); - spin_unlock_irq(&spidev->spi_lock); - - if (dofree) - kfree(spidev); - } - mutex_unlock(&device_list_lock); - - return 0; -} - -static const struct file_operations spidev_fops = { - .owner = THIS_MODULE, - /* REVISIT switch to aio primitives, so that userspace - * gets more complete API coverage. It'll simplify things - * too, except for the locking. - */ - .write = spidev_write, - .read = spidev_read, - .unlocked_ioctl = spidev_ioctl, - .compat_ioctl = spidev_compat_ioctl, - .open = spidev_open, - .release = spidev_release, - .llseek = no_llseek, -}; - -/*-------------------------------------------------------------------------*/ - -/* The main reason to have this class is to make mdev/udev create the - * /dev/spidevB.C character device nodes exposing our userspace API. - * It also simplifies memory management. - */ - -static struct class *spidev_class; - -#ifdef CONFIG_OF -static const struct of_device_id spidev_dt_ids[] = { - { .compatible = "commaai,panda" }, - {}, -}; -MODULE_DEVICE_TABLE(of, spidev_dt_ids); -#endif - -#ifdef CONFIG_ACPI - -/* Dummy SPI devices not to be used in production systems */ -#define SPIDEV_ACPI_DUMMY 1 - -static const struct acpi_device_id spidev_acpi_ids[] = { - /* - * The ACPI SPT000* devices are only meant for development and - * testing. Systems used in production should have a proper ACPI - * description of the connected peripheral and they should also use - * a proper driver instead of poking directly to the SPI bus. - */ - { "SPT0001", SPIDEV_ACPI_DUMMY }, - { "SPT0002", SPIDEV_ACPI_DUMMY }, - { "SPT0003", SPIDEV_ACPI_DUMMY }, - {}, -}; -MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids); - -static void spidev_probe_acpi(struct spi_device *spi) -{ - const struct acpi_device_id *id; - - if (!has_acpi_companion(&spi->dev)) - return; - - id = acpi_match_device(spidev_acpi_ids, &spi->dev); - if (WARN_ON(!id)) - return; - - if (id->driver_data == SPIDEV_ACPI_DUMMY) - dev_warn(&spi->dev, "do not use this driver in production systems!\n"); -} -#else -static inline void spidev_probe_acpi(struct spi_device *spi) {} -#endif - -/*-------------------------------------------------------------------------*/ - -static int spidev_probe(struct spi_device *spi) -{ - struct spidev_data *spidev; - int status; - unsigned long minor; - - /* - * spidev should never be referenced in DT without a specific - * compatible string, it is a Linux implementation thing - * rather than a description of the hardware. - */ - if (spi->dev.of_node && !of_match_device(spidev_dt_ids, &spi->dev)) { - dev_err(&spi->dev, "buggy DT: spidev listed directly in DT\n"); - WARN_ON(spi->dev.of_node && - !of_match_device(spidev_dt_ids, &spi->dev)); - } - - spidev_probe_acpi(spi); - - /* Allocate driver data */ - spidev = kzalloc(sizeof(*spidev), GFP_KERNEL); - if (!spidev) - return -ENOMEM; - - /* Initialize the driver data */ - spidev->spi = spi; - spin_lock_init(&spidev->spi_lock); - mutex_init(&spidev->buf_lock); - - INIT_LIST_HEAD(&spidev->device_entry); - - /* If we can allocate a minor number, hook up this device. - * Reusing minors is fine so long as udev or mdev is working. - */ - mutex_lock(&device_list_lock); - minor = find_first_zero_bit(minors, N_SPI_MINORS); - if (minor < N_SPI_MINORS) { - struct device *dev; - - spidev->devt = MKDEV(SPIDEV_MAJOR, minor); - dev = device_create(spidev_class, &spi->dev, spidev->devt, - spidev, "spidev%d.%d", - spi->master->bus_num, spi->chip_select); - status = PTR_ERR_OR_ZERO(dev); - } else { - dev_dbg(&spi->dev, "no minor number available!\n"); - status = -ENODEV; - } - if (status == 0) { - set_bit(minor, minors); - list_add(&spidev->device_entry, &device_list); - } - mutex_unlock(&device_list_lock); - - spidev->speed_hz = spi->max_speed_hz; - - if (status == 0) - spi_set_drvdata(spi, spidev); - else - kfree(spidev); - - return status; -} - -static int spidev_remove(struct spi_device *spi) -{ - struct spidev_data *spidev = spi_get_drvdata(spi); - - /* make sure ops on existing fds can abort cleanly */ - spin_lock_irq(&spidev->spi_lock); - spidev->spi = NULL; - spin_unlock_irq(&spidev->spi_lock); - - /* prevent new opens */ - mutex_lock(&device_list_lock); - list_del(&spidev->device_entry); - device_destroy(spidev_class, spidev->devt); - clear_bit(MINOR(spidev->devt), minors); - if (spidev->users == 0) - kfree(spidev); - mutex_unlock(&device_list_lock); - - return 0; -} - -static struct spi_driver spidev_spi_driver = { - .driver = { - .name = "spidev_panda", - .of_match_table = of_match_ptr(spidev_dt_ids), - .acpi_match_table = ACPI_PTR(spidev_acpi_ids), - }, - .probe = spidev_probe, - .remove = spidev_remove, - - /* NOTE: suspend/resume methods are not necessary here. - * We don't do anything except pass the requests to/from - * the underlying controller. The refrigerator handles - * most issues; the controller driver handles the rest. - */ -}; - -/*-------------------------------------------------------------------------*/ - -static int __init spidev_init(void) -{ - int status; - - /* Claim our 256 reserved device numbers. Then register a class - * that will key udev/mdev to add/remove /dev nodes. Last, register - * the driver which manages those device numbers. - */ - BUILD_BUG_ON(N_SPI_MINORS > 256); - status = register_chrdev(0, "spi", &spidev_fops); - if (status < 0) - return status; - - SPIDEV_MAJOR = status; - - spidev_class = class_create(THIS_MODULE, "spidev_panda"); - if (IS_ERR(spidev_class)) { - unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); - return PTR_ERR(spidev_class); - } - - status = spi_register_driver(&spidev_spi_driver); - if (status < 0) { - class_destroy(spidev_class); - unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); - } - return status; -} -module_init(spidev_init); - -static void __exit spidev_exit(void) -{ - spi_unregister_driver(&spidev_spi_driver); - class_destroy(spidev_class); - unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); -} -module_exit(spidev_exit); - -MODULE_AUTHOR("Andrea Paterniani, "); -MODULE_DESCRIPTION("User mode SPI device interface"); -MODULE_LICENSE("GPL"); -MODULE_ALIAS("spi:spidev"); From ae769db8b6b6cc0ac4adfa892c76269256d3e272 Mon Sep 17 00:00:00 2001 From: commaci-public <60409688+commaci-public@users.noreply.github.com> Date: Mon, 2 Feb 2026 07:37:23 -0800 Subject: [PATCH 2/6] [bot] Update cppcheck to 2.19.1 (#2254) * [bot] Update cppcheck to 2.19.1 * update coverage table --------- Co-authored-by: Vehicle Researcher Co-authored-by: Adeeb Shihadeh --- tests/misra/coverage_table | 2 +- tests/misra/install.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/misra/coverage_table b/tests/misra/coverage_table index 0395aba0..8753003f 100644 --- a/tests/misra/coverage_table +++ b/tests/misra/coverage_table @@ -9,7 +9,7 @@ 2.6 X (Cppcheck) 2.7 X (Addon) 3.1 X (Addon) -3.2 X (Addon) +3.2 4.1 X (Addon) 4.2 X (Addon) 5.1 X (Addon) diff --git a/tests/misra/install.sh b/tests/misra/install.sh index 9c8fd385..c787033e 100755 --- a/tests/misra/install.sh +++ b/tests/misra/install.sh @@ -10,7 +10,7 @@ fi cd $CPPCHECK_DIR -VERS="2.16.0" +VERS="2.19.1" git fetch --all --tags --force git checkout $VERS From 259645f427c3abbcb0bb36747685792f3c3937eb Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 4 Feb 2026 12:50:32 -0800 Subject: [PATCH 3/6] Revert "[bot] Update cppcheck to 2.19.1 (#2254)" This reverts commit ae769db8b6b6cc0ac4adfa892c76269256d3e272. --- tests/misra/coverage_table | 2 +- tests/misra/install.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/misra/coverage_table b/tests/misra/coverage_table index 8753003f..0395aba0 100644 --- a/tests/misra/coverage_table +++ b/tests/misra/coverage_table @@ -9,7 +9,7 @@ 2.6 X (Cppcheck) 2.7 X (Addon) 3.1 X (Addon) -3.2 +3.2 X (Addon) 4.1 X (Addon) 4.2 X (Addon) 5.1 X (Addon) diff --git a/tests/misra/install.sh b/tests/misra/install.sh index c787033e..9c8fd385 100755 --- a/tests/misra/install.sh +++ b/tests/misra/install.sh @@ -10,7 +10,7 @@ fi cd $CPPCHECK_DIR -VERS="2.19.1" +VERS="2.16.0" git fetch --all --tags --force git checkout $VERS From 8922b48a300bd3b77fb640aa128bb03c6f43733c Mon Sep 17 00:00:00 2001 From: Andi Radulescu Date: Mon, 9 Feb 2026 19:36:32 +0200 Subject: [PATCH 4/6] remove mcu_type (#2324) * remove mcu_type from Panda, it's redundant since all supported devices are H7 * revert disable automatic CAN-FD switching H7 check * revert original can no longer be flashed error * assert, assert, assert * normal reset is fine --- board/jungle/__init__.py | 10 ++-------- python/__init__.py | 30 +++++++++++------------------- tests/hitl/1_program.py | 14 ++++++-------- tests/hitl/2_health.py | 3 --- tests/hitl/reset_jungles.py | 4 +--- 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/board/jungle/__init__.py b/board/jungle/__init__.py index dfb7d98c..e9e7e96b 100644 --- a/board/jungle/__init__.py +++ b/board/jungle/__init__.py @@ -52,7 +52,7 @@ class PandaJungle(Panda): def flash(self, fn=None, code=None, reconnect=True): if not fn: - fn = os.path.join(FW_PATH, self._mcu_type.config.app_fn.replace("panda", "panda_jungle")) + fn = os.path.join(FW_PATH, McuType.H7.config.app_fn.replace("panda", "panda_jungle")) super().flash(fn=fn, code=code, reconnect=reconnect) def recover(self, timeout: int | None = 60, reset: bool = True) -> bool: @@ -73,15 +73,9 @@ class PandaJungle(Panda): self.flash() return True - def get_mcu_type(self) -> McuType: - hw_type = self.get_type() - if hw_type in PandaJungle.H7_DEVICES: - return McuType.H7 - raise ValueError(f"unknown HW type: {hw_type}") - def up_to_date(self, fn=None) -> bool: if fn is None: - fn = os.path.join(FW_PATH, self.get_mcu_type().config.app_fn.replace("panda", "panda_jungle")) + fn = os.path.join(FW_PATH, McuType.H7.config.app_fn.replace("panda", "panda_jungle")) return super().up_to_date(fn=fn) # ******************* health ******************* diff --git a/python/__init__.py b/python/__init__.py index 95d47d66..11f9ac65 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -145,7 +145,6 @@ class Panda: else: self._connect_serial = serial - # connect and set mcu type self.connect(claim) def _cli_select_panda(self): @@ -202,13 +201,9 @@ class Panda: self._serial = serial self._connect_serial = serial self._handle_open = True - self._mcu_type = self.get_mcu_type() self.health_version, self.can_version, self.can_health_version = self.get_packets_versions() logger.debug("connected") - hw_type = self.get_type() - assert hw_type in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" - # disable openpilot's heartbeat checks if self._disable_checks: self.set_heartbeat_disabled() @@ -335,6 +330,9 @@ class Panda: return [] def reset(self, enter_bootstub=False, enter_bootloader=False, reconnect=True): + if enter_bootstub or enter_bootloader: + assert (hw_type := self.get_type()) in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" + # no response is expected since it resets right away timeout = 5000 if isinstance(self._handle, PandaSpiHandle) else 15000 try: @@ -414,16 +412,14 @@ class Panda: pass def flash(self, fn=None, code=None, reconnect=True): + assert (hw_type := self.get_type()) in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" + if self.up_to_date(fn=fn): logger.info("flash: already up to date") return - hw_type = self.get_type() - if hw_type not in self.SUPPORTED_DEVICES: - raise RuntimeError(f"HW type {hw_type.hex()} is deprecated and can no longer be flashed.") - if not fn: - fn = os.path.join(FW_PATH, self._mcu_type.config.app_fn) + fn = os.path.join(FW_PATH, McuType.H7.config.app_fn) assert os.path.isfile(fn) logger.debug("flash: main version is %s", self.get_version()) if not self.bootstub: @@ -438,13 +434,15 @@ class Panda: logger.debug("flash: bootstub version is %s", self.get_version()) # do flash - Panda.flash_static(self._handle, code, mcu_type=self._mcu_type) + Panda.flash_static(self._handle, code, mcu_type=McuType.H7) # reconnect if reconnect: self.reconnect() def recover(self, timeout: int | None = 60, reset: bool = True) -> bool: + assert (hw_type := self.get_type()) in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" + dfu_serial = self.get_dfu_serial() if reset: @@ -489,7 +487,7 @@ class Panda: def up_to_date(self, fn=None) -> bool: current = self.get_signature() if fn is None: - fn = os.path.join(FW_PATH, self.get_mcu_type().config.app_fn) + fn = os.path.join(FW_PATH, McuType.H7.config.app_fn) expected = Panda.get_signature_from_firmware(fn) return (current == expected) @@ -601,12 +599,6 @@ class Panda: else: return (0, 0, 0) - def get_mcu_type(self) -> McuType: - hw_type = self.get_type() - if hw_type in Panda.H7_DEVICES: - return McuType.H7 - raise ValueError(f"unknown HW type: {hw_type}") - def is_internal(self): return self.get_type() in Panda.INTERNAL_DEVICES @@ -627,7 +619,7 @@ class Panda: return self._serial def get_dfu_serial(self): - return PandaDFU.st_serial_to_dfu_serial(self._serial, self._mcu_type) + return PandaDFU.st_serial_to_dfu_serial(self._serial, McuType.H7) def get_uid(self): """ diff --git a/tests/hitl/1_program.py b/tests/hitl/1_program.py index 09c8b867..bf20e82d 100644 --- a/tests/hitl/1_program.py +++ b/tests/hitl/1_program.py @@ -11,7 +11,6 @@ def check_signature(p): def test_dfu(p): - app_mcu_type = p.get_mcu_type() dfu_serial = p.get_dfu_serial() p.reset(enter_bootstub=True) @@ -19,7 +18,7 @@ def test_dfu(p): assert Panda.wait_for_dfu(dfu_serial, timeout=19), "failed to enter DFU" dfu = PandaDFU(dfu_serial) - assert dfu.get_mcu_type() == app_mcu_type + assert dfu.get_mcu_type() == McuType.H7 assert dfu_serial in PandaDFU.list() @@ -38,9 +37,9 @@ def test_known_bootstub(p): McuType.H7: ["bootstub.panda_h7.bin"], } - for kb in known_bootstubs[p.get_mcu_type()]: - app_ids = (p.get_mcu_type(), p.get_usb_serial()) - assert None not in app_ids + for kb in known_bootstubs[McuType.H7]: + app_serial = p.get_usb_serial() + assert app_serial is not None p.reset(enter_bootstub=True) p.reset(enter_bootloader=True) @@ -57,10 +56,9 @@ def test_known_bootstub(p): p.connect(claim=False, wait=True) - # check for MCU or serial mismatch + # check for serial mismatch with Panda(p._serial, claim=False) as np: - bootstub_ids = (np.get_mcu_type(), np.get_usb_serial()) - assert app_ids == bootstub_ids + assert np.get_usb_serial() == app_serial # ensure we can flash app and it jumps to app p.flash() diff --git a/tests/hitl/2_health.py b/tests/hitl/2_health.py index 8fac3e96..f1784b0a 100644 --- a/tests/hitl/2_health.py +++ b/tests/hitl/2_health.py @@ -17,8 +17,6 @@ def test_hw_type(p): """ hw_type = p.get_type() - mcu_type = p.get_mcu_type() - assert mcu_type is not None app_uid = p.get_uid() usb_serial = p.get_usb_serial() @@ -30,7 +28,6 @@ def test_hw_type(p): with Panda(p.get_usb_serial()) as pp: assert pp.bootstub assert pp.get_type() == hw_type, "Bootstub and app hw type mismatch" - assert pp.get_mcu_type() == mcu_type, "Bootstub and app MCU type mismatch" assert pp.get_uid() == app_uid def test_heartbeat(p, panda_jungle): diff --git a/tests/hitl/reset_jungles.py b/tests/hitl/reset_jungles.py index 09bd04ad..97702d80 100755 --- a/tests/hitl/reset_jungles.py +++ b/tests/hitl/reset_jungles.py @@ -15,7 +15,6 @@ def recover(s): def flash(s): with PandaJungle(s) as p: p.flash() - return p.get_mcu_type() # Reset + flash all CI hardware to get it into a consistent state # * port 1: jungles-under-test @@ -42,5 +41,4 @@ if __name__ == "__main__": for s in SERIALS: assert PandaJungle.wait_for_panda(s, timeout=10) assert set(PandaJungle.list()) >= SERIALS - mcu_types = list(exc.map(flash, SERIALS, timeout=20)) - assert set(mcu_types) == {McuType.H7, } + list(exc.map(flash, SERIALS, timeout=20)) From fbd4f60d8265fac4c7050102c369d399173dbd82 Mon Sep 17 00:00:00 2001 From: Andi Radulescu Date: Mon, 9 Feb 2026 22:53:35 +0200 Subject: [PATCH 5/6] fix usb connect on macos (#2326) fix claim on macos --- python/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/__init__.py b/python/__init__.py index 11f9ac65..b3c93e5a 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -274,7 +274,7 @@ class Panda: handle = device.open() if sys.platform not in ("win32", "cygwin", "msys", "darwin"): handle.setAutoDetachKernelDriver(True) - if claim: + if claim or sys.platform == "darwin": handle.claimInterface(0) # handle.setInterfaceAltSetting(0, 0) # Issue in USB stack From b99d79692409514d296d85f367897fa2f86daaa5 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Tue, 10 Feb 2026 20:31:44 -0800 Subject: [PATCH 6/6] Revert "remove mcu_type (#2324)" This reverts commit 8922b48a300bd3b77fb640aa128bb03c6f43733c. --- board/jungle/__init__.py | 10 ++++++++-- python/__init__.py | 30 +++++++++++++++++++----------- tests/hitl/1_program.py | 14 ++++++++------ tests/hitl/2_health.py | 3 +++ tests/hitl/reset_jungles.py | 4 +++- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/board/jungle/__init__.py b/board/jungle/__init__.py index e9e7e96b..dfb7d98c 100644 --- a/board/jungle/__init__.py +++ b/board/jungle/__init__.py @@ -52,7 +52,7 @@ class PandaJungle(Panda): def flash(self, fn=None, code=None, reconnect=True): if not fn: - fn = os.path.join(FW_PATH, McuType.H7.config.app_fn.replace("panda", "panda_jungle")) + fn = os.path.join(FW_PATH, self._mcu_type.config.app_fn.replace("panda", "panda_jungle")) super().flash(fn=fn, code=code, reconnect=reconnect) def recover(self, timeout: int | None = 60, reset: bool = True) -> bool: @@ -73,9 +73,15 @@ class PandaJungle(Panda): self.flash() return True + def get_mcu_type(self) -> McuType: + hw_type = self.get_type() + if hw_type in PandaJungle.H7_DEVICES: + return McuType.H7 + raise ValueError(f"unknown HW type: {hw_type}") + def up_to_date(self, fn=None) -> bool: if fn is None: - fn = os.path.join(FW_PATH, McuType.H7.config.app_fn.replace("panda", "panda_jungle")) + fn = os.path.join(FW_PATH, self.get_mcu_type().config.app_fn.replace("panda", "panda_jungle")) return super().up_to_date(fn=fn) # ******************* health ******************* diff --git a/python/__init__.py b/python/__init__.py index b3c93e5a..48960a40 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -145,6 +145,7 @@ class Panda: else: self._connect_serial = serial + # connect and set mcu type self.connect(claim) def _cli_select_panda(self): @@ -201,9 +202,13 @@ class Panda: self._serial = serial self._connect_serial = serial self._handle_open = True + self._mcu_type = self.get_mcu_type() self.health_version, self.can_version, self.can_health_version = self.get_packets_versions() logger.debug("connected") + hw_type = self.get_type() + assert hw_type in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" + # disable openpilot's heartbeat checks if self._disable_checks: self.set_heartbeat_disabled() @@ -330,9 +335,6 @@ class Panda: return [] def reset(self, enter_bootstub=False, enter_bootloader=False, reconnect=True): - if enter_bootstub or enter_bootloader: - assert (hw_type := self.get_type()) in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" - # no response is expected since it resets right away timeout = 5000 if isinstance(self._handle, PandaSpiHandle) else 15000 try: @@ -412,14 +414,16 @@ class Panda: pass def flash(self, fn=None, code=None, reconnect=True): - assert (hw_type := self.get_type()) in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" - if self.up_to_date(fn=fn): logger.info("flash: already up to date") return + hw_type = self.get_type() + if hw_type not in self.SUPPORTED_DEVICES: + raise RuntimeError(f"HW type {hw_type.hex()} is deprecated and can no longer be flashed.") + if not fn: - fn = os.path.join(FW_PATH, McuType.H7.config.app_fn) + fn = os.path.join(FW_PATH, self._mcu_type.config.app_fn) assert os.path.isfile(fn) logger.debug("flash: main version is %s", self.get_version()) if not self.bootstub: @@ -434,15 +438,13 @@ class Panda: logger.debug("flash: bootstub version is %s", self.get_version()) # do flash - Panda.flash_static(self._handle, code, mcu_type=McuType.H7) + Panda.flash_static(self._handle, code, mcu_type=self._mcu_type) # reconnect if reconnect: self.reconnect() def recover(self, timeout: int | None = 60, reset: bool = True) -> bool: - assert (hw_type := self.get_type()) in self.SUPPORTED_DEVICES, f"Unknown HW: {hw_type}" - dfu_serial = self.get_dfu_serial() if reset: @@ -487,7 +489,7 @@ class Panda: def up_to_date(self, fn=None) -> bool: current = self.get_signature() if fn is None: - fn = os.path.join(FW_PATH, McuType.H7.config.app_fn) + fn = os.path.join(FW_PATH, self.get_mcu_type().config.app_fn) expected = Panda.get_signature_from_firmware(fn) return (current == expected) @@ -599,6 +601,12 @@ class Panda: else: return (0, 0, 0) + def get_mcu_type(self) -> McuType: + hw_type = self.get_type() + if hw_type in Panda.H7_DEVICES: + return McuType.H7 + raise ValueError(f"unknown HW type: {hw_type}") + def is_internal(self): return self.get_type() in Panda.INTERNAL_DEVICES @@ -619,7 +627,7 @@ class Panda: return self._serial def get_dfu_serial(self): - return PandaDFU.st_serial_to_dfu_serial(self._serial, McuType.H7) + return PandaDFU.st_serial_to_dfu_serial(self._serial, self._mcu_type) def get_uid(self): """ diff --git a/tests/hitl/1_program.py b/tests/hitl/1_program.py index bf20e82d..09c8b867 100644 --- a/tests/hitl/1_program.py +++ b/tests/hitl/1_program.py @@ -11,6 +11,7 @@ def check_signature(p): def test_dfu(p): + app_mcu_type = p.get_mcu_type() dfu_serial = p.get_dfu_serial() p.reset(enter_bootstub=True) @@ -18,7 +19,7 @@ def test_dfu(p): assert Panda.wait_for_dfu(dfu_serial, timeout=19), "failed to enter DFU" dfu = PandaDFU(dfu_serial) - assert dfu.get_mcu_type() == McuType.H7 + assert dfu.get_mcu_type() == app_mcu_type assert dfu_serial in PandaDFU.list() @@ -37,9 +38,9 @@ def test_known_bootstub(p): McuType.H7: ["bootstub.panda_h7.bin"], } - for kb in known_bootstubs[McuType.H7]: - app_serial = p.get_usb_serial() - assert app_serial is not None + for kb in known_bootstubs[p.get_mcu_type()]: + app_ids = (p.get_mcu_type(), p.get_usb_serial()) + assert None not in app_ids p.reset(enter_bootstub=True) p.reset(enter_bootloader=True) @@ -56,9 +57,10 @@ def test_known_bootstub(p): p.connect(claim=False, wait=True) - # check for serial mismatch + # check for MCU or serial mismatch with Panda(p._serial, claim=False) as np: - assert np.get_usb_serial() == app_serial + bootstub_ids = (np.get_mcu_type(), np.get_usb_serial()) + assert app_ids == bootstub_ids # ensure we can flash app and it jumps to app p.flash() diff --git a/tests/hitl/2_health.py b/tests/hitl/2_health.py index f1784b0a..8fac3e96 100644 --- a/tests/hitl/2_health.py +++ b/tests/hitl/2_health.py @@ -17,6 +17,8 @@ def test_hw_type(p): """ hw_type = p.get_type() + mcu_type = p.get_mcu_type() + assert mcu_type is not None app_uid = p.get_uid() usb_serial = p.get_usb_serial() @@ -28,6 +30,7 @@ def test_hw_type(p): with Panda(p.get_usb_serial()) as pp: assert pp.bootstub assert pp.get_type() == hw_type, "Bootstub and app hw type mismatch" + assert pp.get_mcu_type() == mcu_type, "Bootstub and app MCU type mismatch" assert pp.get_uid() == app_uid def test_heartbeat(p, panda_jungle): diff --git a/tests/hitl/reset_jungles.py b/tests/hitl/reset_jungles.py index 97702d80..09bd04ad 100755 --- a/tests/hitl/reset_jungles.py +++ b/tests/hitl/reset_jungles.py @@ -15,6 +15,7 @@ def recover(s): def flash(s): with PandaJungle(s) as p: p.flash() + return p.get_mcu_type() # Reset + flash all CI hardware to get it into a consistent state # * port 1: jungles-under-test @@ -41,4 +42,5 @@ if __name__ == "__main__": for s in SERIALS: assert PandaJungle.wait_for_panda(s, timeout=10) assert set(PandaJungle.list()) >= SERIALS - list(exc.map(flash, SERIALS, timeout=20)) + mcu_types = list(exc.map(flash, SERIALS, timeout=20)) + assert set(mcu_types) == {McuType.H7, }