drivers/net/can/usb/etas_es58x/es58x_devlink.c

Source file repositories/reference/linux-study-clean/drivers/net/can/usb/etas_es58x/es58x_devlink.c

File Facts

System
Linux kernel
Corpus path
drivers/net/can/usb/etas_es58x/es58x_devlink.c
Extension
.c
Size
7926 bytes
Lines
261
Domain
Driver Families
Bucket
drivers/net
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0

/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
 *
 * File es58x_devlink.c: report the product information using devlink.
 *
 * Copyright (c) 2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
 */

#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/usb.h>
#include <net/devlink.h>

#include "es58x_core.h"

/* USB descriptor index containing the product information string. */
#define ES58X_PROD_INFO_IDX 6

/**
 * es58x_parse_sw_version() - Extract boot loader or firmware version.
 * @es58x_dev: ES58X device.
 * @prod_info: USB custom string returned by the device.
 * @prefix: Select which information should be parsed. Set it to "FW"
 *	to parse the firmware version or to "BL" to parse the
 *	bootloader version.
 *
 * The @prod_info string contains the firmware and the bootloader
 * version number all prefixed by a magic string and concatenated with
 * other numbers. Depending on the device, the firmware (bootloader)
 * format is either "FW_Vxx.xx.xx" ("BL_Vxx.xx.xx") or "FW:xx.xx.xx"
 * ("BL:xx.xx.xx") where 'x' represents a digit. @prod_info must
 * contains the common part of those prefixes: "FW" or "BL".
 *
 * Parse @prod_info and store the version number in
 * &es58x_dev.firmware_version or &es58x_dev.bootloader_version
 * according to @prefix value.
 *
 * Return: zero on success, -EINVAL if @prefix contains an invalid
 *	value and -EBADMSG if @prod_info could not be parsed.
 */
static int es58x_parse_sw_version(struct es58x_device *es58x_dev,
				  const char *prod_info, const char *prefix)
{
	struct es58x_sw_version *version;
	int major, minor, revision;

	if (!strcmp(prefix, "FW"))
		version = &es58x_dev->firmware_version;
	else if (!strcmp(prefix, "BL"))
		version = &es58x_dev->bootloader_version;
	else
		return -EINVAL;

	/* Go to prefix */
	prod_info = strstr(prod_info, prefix);
	if (!prod_info)
		return -EBADMSG;
	/* Go to beginning of the version number */
	while (!isdigit(*prod_info)) {
		prod_info++;
		if (!*prod_info)
			return -EBADMSG;
	}

	if (sscanf(prod_info, "%2u.%2u.%2u", &major, &minor, &revision) != 3)
		return -EBADMSG;

	version->major = major;
	version->minor = minor;
	version->revision = revision;

	return 0;
}

/**
 * es58x_parse_hw_rev() - Extract hardware revision number.
 * @es58x_dev: ES58X device.
 * @prod_info: USB custom string returned by the device.
 *
 * @prod_info contains the hardware revision prefixed by a magic
 * string and conquenated together with other numbers. Depending on
 * the device, the hardware revision format is either
 * "HW_VER:axxx/xxx" or "HR:axxx/xxx" where 'a' represents a letter
 * and 'x' a digit.
 *
 * Parse @prod_info and store the hardware revision number in
 * &es58x_dev.hardware_revision.
 *
 * Return: zero on success, -EBADMSG if @prod_info could not be

Annotation

Implementation Notes