Add Nuvoton NCT6694 MFD drivers
Subsystems (10)
v3 cover Add Nuvoton NCT6694 MFD drivers
This patch series introduces support for Nuvoton NCT6694, a peripheral expander based on USB interface. It models the chip as an MFD driver (1/7), GPIO driver(2/7), I2C Adapter driver(3/7), CANfd driver(4/7), WDT driver(5/7), HWMON driver(6/7), and RTC driver(7/7).
The MFD driver implements USB device functionality to issue custom-define USB bulk pipe packets for NCT6694. Each child device can use the USB functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.
The following introduces the custom-define USB transactions:
nct6694_read_msg - Send bulk-out pipe to write request packet
Receive bulk-in pipe to read response packet Receive bulk-in pipe to read data packet
nct6694_write_msg - Send bulk-out pipe to write request packet
Send bulk-out pipe to write data packet Receive bulk-in pipe to read response packet Receive bulk-in pipe to read data packet
Changes since version 2:
- Add MODULE_ALIAS() for each child driver
- Modify gpio line names be a local variable in gpio-nct6694.c
- Drop unnecessary platform_get_drvdata() in gpio-nct6694.c
- Rename each command in nct6694_canfd.c
- Modify each function name consistently in nct6694_canfd.c
- Modify the pretimeout validation procedure in nct6694_wdt.c
- Fix warnings in nct6694-hwmon.c
Changes since version 1:
- Implement IRQ domain to handle IRQ demux in nct6694.c
- Modify USB_DEVICE to USB_DEVICE_AND_INTERFACE_INFO API in nct6694.c
- Add each driver's command structure
- Fix USB functions in nct6694.c
- Fix platform driver registration in each child driver
- Sort each driver's header files alphabetically
- Drop unnecessary header in gpio-nct6694.c
- Add gpio line names in gpio-nct6694.c
- Fix errors and warnings in nct6694_canfd.c
- Fix TX-flow control in nct6694_canfd.c
- Fix warnings in nct6694_wdt.c
- Drop unnecessary logs in nct6694_wdt.c
- Modify start() function to setup device in nct6694_wdt.c
- Add voltage sensors functionality in nct6694-hwmon.c
- Add temperature sensors functionality in nct6694-hwmon.c
- Fix overwrite error return values in nct6694-hwmon.c
- Add write value limitation for each write() function in nct6694-hwmon.c
- Drop unnecessary logs in rtc-nct6694.c
- Fix overwrite error return values in rtc-nct6694.c
- Modify to use dev_err_probe API in rtc-nct6694.c
Ming Yu (7):
mfd: Add core driver for Nuvoton NCT6694
gpio: Add Nuvoton NCT6694 GPIO support
i2c: Add Nuvoton NCT6694 I2C support
can: Add Nuvoton NCT6694 CAN support
watchdog: Add Nuvoton NCT6694 WDT support
hwmon: Add Nuvoton NCT6694 HWMON support
rtc: Add Nuvoton NCT6694 RTC support
MAINTAINERS | 13 + drivers/gpio/Kconfig | 12 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-nct6694.c | 438 +++++++++++++++ drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/nct6694-hwmon.c | 768 ++++++++++++++++++++++++++ drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-nct6694.c | 153 +++++ drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 2 + drivers/mfd/nct6694.c | 382 +++++++++++++ drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/nct6694_canfd.c | 920 +++++++++++++++++++++++++++++++ drivers/rtc/Kconfig | 10 + drivers/rtc/Makefile | 1 + drivers/rtc/rtc-nct6694.c | 264 +++++++++ drivers/watchdog/Kconfig | 11 + drivers/watchdog/Makefile | 1 + drivers/watchdog/nct6694_wdt.c | 277 ++++++++++ include/linux/mfd/nct6694.h | 139 +++++ 23 files changed, 3435 insertions(+) create mode 100644 drivers/gpio/gpio-nct6694.c create mode 100644 drivers/hwmon/nct6694-hwmon.c create mode 100644 drivers/i2c/busses/i2c-nct6694.c create mode 100644 drivers/mfd/nct6694.c create mode 100644 drivers/net/can/nct6694_canfd.c create mode 100644 drivers/rtc/rtc-nct6694.c create mode 100644 drivers/watchdog/nct6694_wdt.c create mode 100644 include/linux/mfd/nct6694.h
Signature · 1 line
2.34.1
PATCH v3 1/7 mfd: Add core driver for Nuvoton NCT6694
The Nuvoton NCT6694 is a peripheral expander with 16 GPIO chips, 6 I2C controllers, 2 CANfd controllers, 2 Watchdog timers, ADC, PWM, and RTC.
This driver implements USB device functionality and shares the chip's peripherals as a child device.
Each child device can use the USB functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.
--- MAINTAINERS | 7 + drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 2 + drivers/mfd/nct6694.c | 382 ++++++++++++++++++++++++++++++++++++ include/linux/mfd/nct6694.h | 139 +++++++++++++ 5 files changed, 540 insertions(+) create mode 100644 drivers/mfd/nct6694.c create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +7 -0
@@ -16541,6 +16541,13 @@ F: drivers/nubus/ F: include/linux/nubus.h F: include/uapi/linux/nubus.h +NUVOTON NCT6694 MFD DRIVER +M: Ming Yu <[email protected]> +L: [email protected] +S: Supported +F: drivers/mfd/nct6694.c +F: include/linux/mfd/nct6694.h + NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER M: Antonino Daplas <[email protected]> L: [email protected]
drivers/mfd/Kconfig +10 -0
@@ -558,6 +558,16 @@ config MFD_MX25_TSADC i.MX25 processors. They consist of a conversion queue for general purpose ADC and a queue for Touchscreens. +config MFD_NCT6694 + tristate "Nuvoton NCT6694 support" + select MFD_CORE + depends on USB + help + This adds support for Nuvoton USB device NCT6694 sharing peripherals + This includes the USB devcie driver and core APIs. + Additional drivers must be enabled in order to use the functionality + of the device. + config MFD_HI6421_PMIC tristate "HiSilicon Hi6421 PMU/Codec IC" depends on OF
drivers/mfd/Makefile +2 -0
@@ -117,6 +117,8 @@ obj-$(CONFIG_TWL6040_CORE) += twl6040.o obj-$(CONFIG_MFD_MX25_TSADC) += fsl-imx25-tsadc.o +obj-$(CONFIG_MFD_NCT6694) += nct6694.o + obj-$(CONFIG_MFD_MC13XXX) += mc13xxx-core.o obj-$(CONFIG_MFD_MC13XXX_SPI) += mc13xxx-spi.o obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o
drivers/mfd/nct6694.c new file +382 -0
382 lines not on this page · raw message
include/linux/mfd/nct6694.h new file +139 -0
139 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Quoted 167 lines · Ming Yu
The Nuvoton NCT6694 is a peripheral expander with 16 GPIO chips, 6 I2C controllers, 2 CANfd controllers, 2 Watchdog timers, ADC, PWM, and RTC.
This driver implements USB device functionality and shares the chip's peripherals as a child device.
Each child device can use the USB functions
nct6694_read_msg()andnct6694_write_msg()to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.Signed-off-by Ming Yu <[email protected]>--- MAINTAINERS | 7 + drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 2 + drivers/mfd/nct6694.c | 382 ++++++++++++++++++++++++++++++++++++ include/linux/mfd/nct6694.h | 139 +++++++++++++ 5 files changed, 540 insertions(+)create mode 100644
drivers/mfd/nct6694.c
create mode 100644include/linux/mfd/nct6694.hMAINTAINERS +7
-0@@ -16541,6 +16541,13 @@ F: drivers/nubus/ F: include/linux/nubus.h F: include/uapi/linux/nubus.h +NUVOTON NCT6694 MFD DRIVER +M: Ming Yu <[email protected]> +L: [email protected] +S: Supported +F: drivers/mfd/nct6694.c +F: include/linux/mfd/nct6694.h + NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER M: Antonino Daplas <[email protected]> L: [email protected]drivers/mfd/Kconfig +10
-0@@ -558,6 +558,16 @@ config MFD_MX25_TSADC i.MX25 processors. They consist of a conversion queue for general purpose ADC and a queue for Touchscreens. +config MFD_NCT6694 + tristate "Nuvoton NCT6694 support" + select MFD_CORE + depends on USB + help + This adds support for Nuvoton USB device NCT6694 sharing peripherals + This includes the USB devcie driver and core APIs. + Additional drivers must be enabled in order to use the functionality + of the device. + config MFD_HI6421_PMIC tristate "HiSilicon Hi6421 PMU/Codec IC" depends on OFdrivers/mfd/Makefile +2
-0@@ -117,6 +117,8 @@ obj-$(CONFIG_TWL6040_CORE) += twl6040.o obj-$(CONFIG_MFD_MX25_TSADC) += fsl-imx25-tsadc.o +obj-$(CONFIG_MFD_NCT6694) += nct6694.o + obj-$(CONFIG_MFD_MC13XXX) += mc13xxx-core.o obj-$(CONFIG_MFD_MC13XXX_SPI) += mc13xxx-spi.o obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.odrivers/mfd/nct6694.c new file +86
-0@@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Nuvoton NCT6694 MFD driver based on USB interface. +* +* Copyright (C) 2024 Nuvoton Technology Corp. +*/ + +#include <linux/interrupt.h> +#include <linux/irq.h> +#include <linux/irqdomain.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/usb.h> + +#define MFD_DEV_SIMPLE (_name) \ +{ \ + .name = NCT6694_DEV_##_name, \ +} \ + +#define MFD_DEV_WITH_ID (_name, _id) \ +{ \ + .name = NCT6694_DEV_##_name, \ + .id = _id, \ +} + +/* MFD device resources */ +static const struct mfd_cell nct6694_dev[] = { +MFD_DEV_WITH_ID (GPIO ,0x0 ), +MFD_DEV_WITH_ID (GPIO ,0x1 ), +MFD_DEV_WITH_ID (GPIO ,0x2 ), +MFD_DEV_WITH_ID (GPIO ,0x3 ), +MFD_DEV_WITH_ID (GPIO ,0x4 ), +MFD_DEV_WITH_ID (GPIO ,0x5 ), +MFD_DEV_WITH_ID (GPIO ,0x6 ), +MFD_DEV_WITH_ID (GPIO ,0x7 ), +MFD_DEV_WITH_ID (GPIO ,0x8 ), +MFD_DEV_WITH_ID (GPIO ,0x9 ), +MFD_DEV_WITH_ID (GPIO ,0xA ), +MFD_DEV_WITH_ID (GPIO ,0xB ), +MFD_DEV_WITH_ID (GPIO ,0xC ), +MFD_DEV_WITH_ID (GPIO ,0xD ), +MFD_DEV_WITH_ID (GPIO ,0xE ), +MFD_DEV_WITH_ID (GPIO ,0xF ), + +MFD_DEV_WITH_ID (I2C ,0x0 ), +MFD_DEV_WITH_ID (I2C ,0x1 ), +MFD_DEV_WITH_ID (I2C ,0x2 ), +MFD_DEV_WITH_ID (I2C ,0x3 ), +MFD_DEV_WITH_ID (I2C ,0x4 ), +MFD_DEV_WITH_ID (I2C ,0x5 ), + +MFD_DEV_WITH_ID (CAN ,0x0 ), +MFD_DEV_WITH_ID (CAN ,0x1 ), + +MFD_DEV_WITH_ID (WDT ,0x0 ), +MFD_DEV_WITH_ID (WDT ,0x1 ), + +MFD_DEV_SIMPLE (HWMON ), +MFD_DEV_SIMPLE (RTC ), +}; + +static int nct6694_response_err_handling (struct nct6694 *nct6694, +unsigned char err_status) +{ +struct device *dev = &nct6694->udev->dev; + +switch (err_status) { +case NCT6694_NO_ERROR : +return err_status; +case NCT6694_NOT_SUPPORT_ERROR : +dev_dbg (dev,"%s: Command is not support!\n" , __func__); +break ; +case NCT6694_NO_RESPONSE_ERROR : +dev_dbg (dev,"%s: Command is no response!\n" , __func__); +break ; +case NCT6694_TIMEOUT_ERROR : +dev_dbg (dev,"%s: Command is timeout!\n" , __func__); +break ; +case NCT6694_PENDING : +dev_dbg (dev,"%s: Command is pending!\n" , __func__); +break ; +default : +return -EINVAL ;
drivers/mfd/nct6694.c line 96
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+ } + +return -EIO ; +} + +int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header;
RCT violation
Quoted 29 lines
drivers/mfd/nct6694.c new file +29
-0+struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock); + +/* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_GET ; + cmd_header->len = length; + + ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), + cmd_header,NCT6694_CMD_PACKET_SZ , &tx_len, + nct6694->timeout); +if (ret) +return ret; + +/* Receive response packet from USB device */ + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + response_header,NCT6694_CMD_PACKET_SZ , &rx_len, + nct6694->timeout); +if (ret) +return ret; + + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + buf,NCT6694_MAX_PACKET_SZ , &rx_len, nct6694->timeout); +if (ret) +return ret;
drivers/mfd/nct6694.c line 135
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+ +return nct6694_response_err_handling (nct6694, response_header->sts); +} +EXPORT_SYMBOL (nct6694_read_msg); + +int nct6694_write_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header;
Same as above
Quoted 34 lines
drivers/mfd/nct6694.c new file +34
-0+struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock); + +/* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_SET ; + cmd_header->len = length; + + ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), + cmd_header,NCT6694_CMD_PACKET_SZ , &tx_len, + nct6694->timeout); +if (ret) +return ret; + + ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), + buf, length, &tx_len, nct6694->timeout); +if (ret) +return ret; + +/* Receive response packet from USB device */ + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + response_header,NCT6694_CMD_PACKET_SZ , &rx_len, + nct6694->timeout); +if (ret) +return ret; + + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + buf,NCT6694_MAX_PACKET_SZ , &rx_len, nct6694->timeout); +if (ret) +return ret;
drivers/mfd/nct6694.c line 179
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+ +return nct6694_response_err_handling (nct6694, response_header->sts); +} +EXPORT_SYMBOL (nct6694_write_msg); + +static void usb_int_callback (struct urb *urb) +{ +struct nct6694 *nct6694 = urb->context; +struct device *dev = &nct6694->udev->dev; +unsigned int *int_status = urb->transfer_buffer;
Again RCT violation
Quoted 39 lines
drivers/mfd/nct6694.c new file +39
-0+int ret; + +switch (urb->status) { +case 0 : +break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +return ; +default : + goto resubmit; + } + +while (*int_status) { +int irq =__ffs (*int_status); + +if (*int_status & (1 << irq)) +generic_handle_irq_safe (irq_find_mapping (nct6694->domain, irq)); + + *int_status &= ~(1 << irq); + } + +resubmit: + ret =usb_submit_urb (urb,GFP_ATOMIC ); +if (ret) +dev_dbg (dev,"%s: Failed to resubmit urb, status %d" , + __func__, ret); +} + +static void nct6694_irq_lock (struct irq_data *data) +{ +struct nct6694 *nct6694 =irq_data_get_irq_chip_data (data); + +mutex_lock (&nct6694->irq_lock); +} + +static void nct6694_irq_sync_unlock (struct irq_data *data) +{ +struct nct6694 *nct6694 =irq_data_get_irq_chip_data (data);
drivers/mfd/nct6694.c line 228
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+ +mutex_unlock (&nct6694->irq_lock); +} + +static void nct6694_irq_enable (struct irq_data *data) +{ +struct nct6694 *nct6694 =irq_data_get_irq_chip_data (data); +unsigned int irq = data->hwirq; + + nct6694->irq_enable |= (1 << irq);
I wonder if this is necessary to create local variable irq if this is used only once. Maybe instead:
nct6694->irq_enable |= (1 << data->hwirq);
Not big thing though
Quoted 43 lines
drivers/mfd/nct6694.c new file +43
-0+} + +static void nct6694_irq_disable (struct irq_data *data) +{ +struct nct6694 *nct6694 =irq_data_get_irq_chip_data (data); +unsigned int irq = data->hwirq; + + nct6694->irq_enable &= ~(1 << irq); +} + +static struct irq_chip nct6694_irq_chip = { + .name ="nct6694-irq" , + .flags =IRQCHIP_SKIP_SET_WAKE , + .irq_bus_lock = nct6694_irq_lock, + .irq_bus_sync_unlock = nct6694_irq_sync_unlock, + .irq_enable = nct6694_irq_enable, + .irq_disable = nct6694_irq_disable, +}; + +static int nct6694_irq_domain_map (struct irq_domain *d,unsigned int irq, +irq_hw_number_t hw) +{ +struct nct6694 *nct6694 = d->host_data; + +irq_set_chip_data (irq, nct6694); +irq_set_chip_and_handler (irq, &nct6694_irq_chip, handle_simple_irq); + +return 0 ; +} + +static void nct6694_irq_domain_unmap (struct irq_domain *d,unsigned int irq) +{ +irq_set_chip_and_handler (irq,NULL ,NULL ); +irq_set_chip_data (irq,NULL ); +} + +static const struct irq_domain_ops nct6694_irq_domain_ops = { + .map = nct6694_irq_domain_map, + .unmap = nct6694_irq_domain_unmap, +}; + +static int nct6694_usb_probe (struct usb_interface *iface, +const struct usb_device_id *id)
drivers/mfd/nct6694.c line 281
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+{ +struct usb_device *udev =interface_to_usbdev (iface); +struct device *dev = &udev->dev; +struct usb_host_interface *interface; +struct usb_endpoint_descriptor *int_endpoint; +struct nct6694 *nct6694; +struct nct6694_cmd_header *cmd_header; +struct nct6694_response_header *response_header; +int pipe, maxp; +int ret;
Please follow RCT rule
Quoted 245 lines
drivers/mfd/nct6694.c new file +100
-0+ + interface = iface->cur_altsetting; + + int_endpoint = &interface->endpoint[0 ].desc; +if (!usb_endpoint_is_int_in (int_endpoint)) +return -ENODEV ; + + nct6694 =devm_kzalloc (dev,sizeof (*nct6694),GFP_KERNEL ); +if (!nct6694) +return -ENOMEM ; + + pipe =usb_rcvintpipe (udev,NCT6694_INT_IN_EP ); + maxp =usb_maxpacket (udev, pipe); + + cmd_header =devm_kzalloc (dev,sizeof (*cmd_header), +GFP_KERNEL ); +if (!cmd_header) +return -ENOMEM ; + + response_header =devm_kzalloc (dev,sizeof (*response_header), +GFP_KERNEL ); +if (!response_header) +return -ENOMEM ; + + nct6694->int_buffer =devm_kcalloc (dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL ); +if (!nct6694->int_buffer) +return -ENOMEM ; + + nct6694->int_in_urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!nct6694->int_in_urb) + return -ENOMEM;> + + nct6694->domain =irq_domain_add_simple (NULL ,NCT6694_NR_IRQS ,0 , + &nct6694_irq_domain_ops, + nct6694); +if (!nct6694->domain) +return -ENODEV ; + + nct6694->udev = udev; + nct6694->timeout =NCT6694_URB_TIMEOUT ;/* Wait until urb complete */ + nct6694->cmd_header = cmd_header; + nct6694->response_header = response_header; + +mutex_init (&nct6694->access_lock); +mutex_init (&nct6694->irq_lock); + +usb_fill_int_urb (nct6694->int_in_urb, udev, pipe, + nct6694->int_buffer, maxp, usb_int_callback, + nct6694, int_endpoint->bInterval); + ret =usb_submit_urb (nct6694->int_in_urb,GFP_KERNEL ); +if (ret) + goto err_urb; + +dev_set_drvdata (dev, nct6694); +usb_set_intfdata (iface, nct6694); + + ret =mfd_add_hotplug_devices (dev, nct6694_dev,ARRAY_SIZE (nct6694_dev)); +if (ret) + goto err_mfd; + +dev_info (dev,"Probed device: (%04X:%04X)\n" , id->idVendor, id->idProduct); +return 0 ; + +err_mfd: +usb_kill_urb (nct6694->int_in_urb); +err_urb: +usb_free_urb (nct6694->int_in_urb); +return dev_err_probe (dev, ret,"Probe failed\n" ); +} + +static void nct6694_usb_disconnect (struct usb_interface *iface) +{ +struct usb_device *udev =interface_to_usbdev (iface); +struct nct6694 *nct6694 =usb_get_intfdata (iface); + +mfd_remove_devices (&udev->dev); +usb_kill_urb (nct6694->int_in_urb); +usb_free_urb (nct6694->int_in_urb); +} + +static const struct usb_device_id nct6694_ids[] = { + {USB_DEVICE_AND_INTERFACE_INFO (NCT6694_VENDOR_ID , +NCT6694_PRODUCT_ID , +0xFF ,0x00 ,0x00 )}, + {} +}; +MODULE_DEVICE_TABLE (usb, nct6694_ids); + +static struct usb_driver nct6694_usb_driver = { + .name ="nct6694" , + .id_table = nct6694_ids, + .probe = nct6694_usb_probe, + .disconnect = nct6694_usb_disconnect, +}; + +module_usb_driver (nct6694_usb_driver); + +MODULE_DESCRIPTION ("USB-MFD driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" );include/linux/mfd/nct6694.h new file +139
-0@@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Nuvoton NCT6694 USB transaction and data structure. +* +* Copyright (C) 2024 Nuvoton Technology Corp. +*/ + +#ifndef __MFD_NCT6694_H +#define __MFD_NCT6694_H + +#define NCT6694_DEV_GPIO "nct6694-gpio" +#define NCT6694_DEV_I2C "nct6694-i2c" +#define NCT6694_DEV_CAN "nct6694-can" +#define NCT6694_DEV_WDT "nct6694-wdt" +#define NCT6694_DEV_HWMON "nct6694-hwmon" +#define NCT6694_DEV_RTC "nct6694-rtc" + +#define NCT6694_VENDOR_ID 0x0416 +#define NCT6694_PRODUCT_ID 0x200B +#define NCT6694_INT_IN_EP 0x81 +#define NCT6694_BULK_IN_EP 0x02 +#define NCT6694_BULK_OUT_EP 0x03 +#define NCT6694_MAX_PACKET_SZ 0x200 + +#define NCT6694_CMD_PACKET_SZ 0x8 +#define NCT6694_HCTRL_SET 0x40 +#define NCT6694_HCTRL_GET 0x80 + +#define NCT6694_URB_TIMEOUT 1000 + +enum nct6694_irq_id { +NCT6694_IRQ_GPIO0 =0 , +NCT6694_IRQ_GPIO1 , +NCT6694_IRQ_GPIO2 , +NCT6694_IRQ_GPIO3 , +NCT6694_IRQ_GPIO4 , +NCT6694_IRQ_GPIO5 , +NCT6694_IRQ_GPIO6 , +NCT6694_IRQ_GPIO7 , +NCT6694_IRQ_GPIO8 , +NCT6694_IRQ_GPIO9 , +NCT6694_IRQ_GPIOA , +NCT6694_IRQ_GPIOB , +NCT6694_IRQ_GPIOC , +NCT6694_IRQ_GPIOD , +NCT6694_IRQ_GPIOE , +NCT6694_IRQ_GPIOF , +NCT6694_IRQ_CAN1 , +NCT6694_IRQ_CAN2 , +NCT6694_IRQ_RTC , +NCT6694_NR_IRQS , +}; + +enum nct6694_response_err_status { +NCT6694_NO_ERROR =0 , +NCT6694_FORMAT_ERROR , +NCT6694_RESERVED1 , +NCT6694_RESERVED2 , +NCT6694_NOT_SUPPORT_ERROR , +NCT6694_NO_RESPONSE_ERROR , +NCT6694_TIMEOUT_ERROR , +NCT6694_PENDING , +}; + +struct nct6694 { +struct usb_device *udev; +struct urb *int_in_urb; +struct irq_domain *domain; +struct nct6694_cmd_header *cmd_header; +struct nct6694_response_header *response_header; +struct mutex access_lock; +struct mutex irq_lock; +unsigned char *int_buffer; +unsigned int irq_enable; +/* time in msec to wait for the urb to the complete */ +long timeout; +}; + +struct nct6694_cmd_header { +unsigned char rsv1; +unsigned char mod; +unsigned char cmd; +unsigned char sel; +unsigned char hctrl; +unsigned char rsv2; +unsigned short len; +} __packed; + +struct nct6694_response_header { +unsigned char sequence_id; +unsigned char sts; +unsigned int rsv; +unsigned short len; +} __packed; + +/* +* nct6694_read_msg - Receive data from NCT6694 USB device +* +* @nct6694 - Nuvoton NCT6694 structure +* @mod - Module byte +* @offset - Offset byte or (Select byte | Command byte) +* @length - Length byte +* @buf - Read data from rx buffer +* +* USB Transaction format: +* +* OUT |RSV|MOD|CMD|SEL|HCTL|RSV|LEN_L|LEN_H| +* OUT |SEQ|STS|RSV|RSV|RSV|RSV||LEN_L|LEN_H| +* IN |-------D------A------D------A-------| +* IN ...... +* IN |-------D------A------D------A-------| +*/ +int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf); + +/* +* nct6694_read_msg - Transmit data to NCT6694 USB device +* +* @nct6694 - Nuvoton NCT6694 structure +* @mod - Module byte +* @offset - Offset byte or (Select byte | Command byte) +* @length - Length byte +* @buf - Write data to tx buffer +* +* USB Transaction format: +* +* OUT |RSV|MOD|CMD|SEL|HCTL|RSV|LEN_L|LEN_H| +* OUT |-------D------A------D------A-------| +* OUT ...... +* OUT |-------D------A------D------A-------| +* IN |SEQ|STS|RSV|RSV|RSV|RSV||LEN_L|LEN_H| +* IN |-------D------A------D------A-------| +* IN ...... +* IN |-------D------A------D------A-------| +*/ +int nct6694_write_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf); + +#endif
Jakub Kicinski
maintainerRe: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Quoted 7 lines · On Tue, 10 Dec 2024 11:57:41 +0100 Mateusz Polchlopek wrote:
drivers/mfd/nct6694.c line 96
Quoted 5 lines
drivers/mfd/nct6694.c new file +5
-0+int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header;RCT violation
This code is not under net not drivers/net
As a general rule please focus on functional review, formatting and process issues are harder to judge unless you read all of the mailing list traffic.
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Quoted 9 lines · Jakub Kicinski
Quoted 7 lines · On Tue, 10 Dec 2024 11:57:41 +0100 Mateusz Polchlopek wrote:
drivers/mfd/nct6694.c line 96
Quoted 5 lines
drivers/mfd/nct6694.c new file +5
-0+int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header;RCT violation
Quoted 4 lines
This code is not under net not
drivers/net
As a general rule please focus on functional review, formatting and process issues are harder to judge unless you read all of the mailing list traffic.
Okay, my bad. Thanks Kuba for explanation and I will focus on code next time
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
In reply to Ming YuQuoted 28 lines · Ming Yu
drivers/mfd/nct6694.c new file +28
-0+ nct6694->int_buffer =devm_kcalloc (dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL ); +if (!nct6694->int_buffer) +return -ENOMEM ; + + nct6694->int_in_urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!nct6694->int_in_urb) +return -ENOMEM ; + + nct6694->domain =irq_domain_add_simple (NULL ,NCT6694_NR_IRQS ,0 , + &nct6694_irq_domain_ops, + nct6694); +if (!nct6694->domain) +return -ENODEV ; + + nct6694->udev = udev; + nct6694->timeout =NCT6694_URB_TIMEOUT ;/* Wait until urb complete */ + nct6694->cmd_header = cmd_header; + nct6694->response_header = response_header; + +mutex_init (&nct6694->access_lock); +mutex_init (&nct6694->irq_lock); + +usb_fill_int_urb (nct6694->int_in_urb, udev, pipe, + nct6694->int_buffer, maxp, usb_int_callback, + nct6694, int_endpoint->bInterval); + ret =usb_submit_urb (nct6694->int_in_urb,GFP_KERNEL ); +if (ret)
drivers/mfd/nct6694.c line 343
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+ goto err_urb; + +dev_set_drvdata (dev, nct6694); +usb_set_intfdata (iface, nct6694); + + ret =mfd_add_hotplug_devices (dev, nct6694_dev,ARRAY_SIZE (nct6694_dev)); +if (ret) + goto err_mfd; + +dev_info (dev,"Probed device: (%04X:%04X)\n" , id->idVendor, id->idProduct);
Drop. Duplicating existing messages and interfaces. Your driver is supposed to be silent on success.
drivers/mfd/nct6694.c line 350
Quoted 7 lines
drivers/mfd/nct6694.c new file +7
-0+return 0 ; + +err_mfd: +usb_kill_urb (nct6694->int_in_urb); +err_urb: +usb_free_urb (nct6694->int_in_urb); +return dev_err_probe (dev, ret,"Probe failed\n" );
No, this should go to individual call causing errors so this will be informative. Above is not informative at all and kernel already reports this, so drop.
drivers/mfd/nct6694.c line 356
Quoted 6 lines
drivers/mfd/nct6694.c new file +6
-0+} + +static void nct6694_usb_disconnect (struct usb_interface *iface) +{ +struct usb_device *udev =interface_to_usbdev (iface); +struct nct6694 *nct6694 =usb_get_intfdata (iface);
Best regards,
Krzysztof
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Dear Krzysztof,
Thank you for your comments,
Krzysztof Kozlowski <[email protected]> 於 2024年12月10日 週二 下午10:38寫道:
Quoted 14 lines
drivers/mfd/nct6694.c line 343
Quoted 10 lines
drivers/mfd/nct6694.c new file +9
-0+ +dev_set_drvdata (dev, nct6694); +usb_set_intfdata (iface, nct6694); + + ret =mfd_add_hotplug_devices (dev, nct6694_dev,ARRAY_SIZE (nct6694_dev)); +if (ret) + goto err_mfd; + +dev_info (dev,"Probed device: (%04X:%04X)\n" , id->idVendor, id->idProduct);Drop. Duplicating existing messages and interfaces. Your driver is supposed to be silent on success.
Okay, I will drop it in v4.
Quoted 12 lines
drivers/mfd/nct6694.c line 350
Quoted 7 lines
drivers/mfd/nct6694.c new file +7
-0+return 0 ; + +err_mfd: +usb_kill_urb (nct6694->int_in_urb); +err_urb: +usb_free_urb (nct6694->int_in_urb); +return dev_err_probe (dev, ret,"Probe failed\n" );No, this should go to individual call causing errors so this will be informative. Above is not informative at all and kernel already reports this, so drop.
Okay, I will drop it in v4.
Quoted 7 lines
drivers/mfd/nct6694.c line 356
Quoted 6 lines
drivers/mfd/nct6694.c new file +6
-0+} + +static void nct6694_usb_disconnect (struct usb_interface *iface) +{ +struct usb_device *udev =interface_to_usbdev (iface); +struct nct6694 *nct6694 =usb_get_intfdata (iface);
Best regards,
Ming
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
In reply to Ming YuLe 10/12/2024 à 11:45, Ming Yu a écrit :
Quoted 11 lines
The Nuvoton NCT6694 is a peripheral expander with 16 GPIO chips, 6 I2C controllers, 2 CANfd controllers, 2 Watchdog timers, ADC, PWM, and RTC.
This driver implements USB device functionality and shares the chip's peripherals as a child device.
Each child device can use the USB functions
nct6694_read_msg()andnct6694_write_msg()to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.
...
drivers/mfd/nct6694.c line 100
Quoted 9 lines
drivers/mfd/nct6694.c new file +9
-0+int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header; +struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock);
Nitpick: This could be moved a few lines below, should it still comply with your coding style.
Quoted 29 lines
drivers/mfd/nct6694.c new file +29
-0+ +/* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_GET ; + cmd_header->len = length; + + ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), + cmd_header,NCT6694_CMD_PACKET_SZ , &tx_len, + nct6694->timeout); +if (ret) +return ret; + +/* Receive response packet from USB device */ + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + response_header,NCT6694_CMD_PACKET_SZ , &rx_len, + nct6694->timeout); +if (ret) +return ret; + + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + buf,NCT6694_MAX_PACKET_SZ , &rx_len, nct6694->timeout); +if (ret) +return ret; + +return nct6694_response_err_handling (nct6694, response_header->sts); +} +EXPORT_SYMBOL (nct6694_read_msg);
drivers/mfd/nct6694.c line 139
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+ +int nct6694_write_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header; +struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock);
Nitpick: This could be moved a few lines below, should it still comply with your coding style.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + /* Send command packet to USB device */
Nitpick: double space before */
Quoted 5 lines
drivers/mfd/nct6694.c new file +5
-0+ cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_SET ; + cmd_header->len = length;
...
Quoted 1 line
+static struct
irq_chipnct6694_irq_chip= {
const?
Quoted 7 lines
drivers/mfd/nct6694.c new file +7
-0+ .name = "nct6694-irq", + .flags = IRQCHIP_SKIP_SET_WAKE, + .irq_bus_lock = nct6694_irq_lock, + .irq_bus_sync_unlock = nct6694_irq_sync_unlock, + .irq_enable = nct6694_irq_enable, + .irq_disable = nct6694_irq_disable, +};
...
Quoted 28 lines
drivers/mfd/nct6694.c new file +28
-0+static int nct6694_usb_probe (struct usb_interface *iface, +const struct usb_device_id *id) +{ +struct usb_device *udev =interface_to_usbdev (iface); +struct device *dev = &udev->dev; +struct usb_host_interface *interface; +struct usb_endpoint_descriptor *int_endpoint; +struct nct6694 *nct6694; +struct nct6694_cmd_header *cmd_header; +struct nct6694_response_header *response_header; +int pipe, maxp; +int ret; + + interface = iface->cur_altsetting; + + int_endpoint = &interface->endpoint[0 ].desc; +if (!usb_endpoint_is_int_in (int_endpoint)) +return -ENODEV ; + + nct6694 =devm_kzalloc (dev,sizeof (*nct6694),GFP_KERNEL ); +if (!nct6694) +return -ENOMEM ; + + pipe =usb_rcvintpipe (udev,NCT6694_INT_IN_EP ); + maxp =usb_maxpacket (udev, pipe); + + cmd_header =devm_kzalloc (dev,sizeof (*cmd_header), +GFP_KERNEL );
drivers/mfd/nct6694.c line 307
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+if (!cmd_header) +return -ENOMEM ; + + response_header =devm_kzalloc (dev,sizeof (*response_header), +GFP_KERNEL ); +if (!response_header) +return -ENOMEM ; + + nct6694->int_buffer =devm_kcalloc (dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
Why for cmd_header and response_header we use a temp variable, while here we update directly nct6694->int_buffer?
It would save a few LoC do remove this temp var.
drivers/mfd/nct6694.c line 313
Quoted 6 lines
drivers/mfd/nct6694.c new file +6
-0+if (!nct6694->int_buffer) +return -ENOMEM ; + + nct6694->int_in_urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!nct6694->int_in_urb) +return -ENOMEM ;
...
CJ
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Dear Christophe,
Thank you for your comments,
Christophe JAILLET <[email protected]> 於 2024年12月12日 週四 上午12:44寫道:
Quoted 14 lines
drivers/mfd/nct6694.c line 100
Quoted 10 lines
drivers/mfd/nct6694.c new file +9
-0+int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header; +struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock);Nitpick: This could be moved a few lines below, should it still comply with your coding style.
I think the lock should be placed here to prevent the cmd_header from being overwritten by another caller.
Could you share your perspective on this?
Quoted 34 lines
Quoted 34 lines
drivers/mfd/nct6694.c new file +34
-0+ +/* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_GET ; + cmd_header->len = length; + + ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), + cmd_header,NCT6694_CMD_PACKET_SZ , &tx_len, + nct6694->timeout); +if (ret) +return ret; + +/* Receive response packet from USB device */ + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + response_header,NCT6694_CMD_PACKET_SZ , &rx_len, + nct6694->timeout); +if (ret) +return ret; + + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), + buf,NCT6694_MAX_PACKET_SZ , &rx_len, nct6694->timeout); +if (ret) +return ret; + +return nct6694_response_err_handling (nct6694, response_header->sts); +} +EXPORT_SYMBOL (nct6694_read_msg); + +int nct6694_write_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header;
Quoted 9 lines
drivers/mfd/nct6694.c line 100
Quoted 5 lines
drivers/mfd/nct6694.c new file +5
-0+struct nct6694_response_header *response_header = nct6694->response_header; +struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock);Nitpick: This could be moved a few lines below, should it still comply with your coding style.
I think the lock should be placed here to prevent the cmd_header from being overwritten by another caller.
Could you share your perspective on this?
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + /* Send command packet to USB device */Nitpick: double space before */
Fix it in v4.
Quoted 6 lines
drivers/mfd/nct6694.c line 146
Quoted 5 lines
drivers/mfd/nct6694.c new file +5
-0+ cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_SET ; + cmd_header->len = length;
Quoted 6 lines
...
Quoted 1 line
+static struct
irq_chipnct6694_irq_chip= {const?
Fix it in v4.
Quoted 52 lines
Quoted 7 lines
drivers/mfd/nct6694.c new file +7
-0+ .name = "nct6694-irq", + .flags = IRQCHIP_SKIP_SET_WAKE, + .irq_bus_lock = nct6694_irq_lock, + .irq_bus_sync_unlock = nct6694_irq_sync_unlock, + .irq_enable = nct6694_irq_enable, + .irq_disable = nct6694_irq_disable, +};...
Quoted 28 lines
drivers/mfd/nct6694.c new file +28
-0+static int nct6694_usb_probe (struct usb_interface *iface, +const struct usb_device_id *id) +{ +struct usb_device *udev =interface_to_usbdev (iface); +struct device *dev = &udev->dev; +struct usb_host_interface *interface; +struct usb_endpoint_descriptor *int_endpoint; +struct nct6694 *nct6694; +struct nct6694_cmd_header *cmd_header; +struct nct6694_response_header *response_header; +int pipe, maxp; +int ret; + + interface = iface->cur_altsetting; + + int_endpoint = &interface->endpoint[0 ].desc; +if (!usb_endpoint_is_int_in (int_endpoint)) +return -ENODEV ; + + nct6694 =devm_kzalloc (dev,sizeof (*nct6694),GFP_KERNEL ); +if (!nct6694) +return -ENOMEM ; + + pipe =usb_rcvintpipe (udev,NCT6694_INT_IN_EP ); + maxp =usb_maxpacket (udev, pipe); + + cmd_header =devm_kzalloc (dev,sizeof (*cmd_header), +GFP_KERNEL );drivers/mfd/nct6694.c line 307
Quoted 10 lines
drivers/mfd/nct6694.c new file +10
-0+if (!cmd_header) +return -ENOMEM ; + + response_header =devm_kzalloc (dev,sizeof (*response_header), +GFP_KERNEL ); +if (!response_header) +return -ENOMEM ; + + nct6694->int_buffer =devm_kcalloc (dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );Why for
cmd_headerandresponse_headerwe use a temp variable, while here we update directlynct6694->int_buffer?
Quoted 2 lines
It would save a few LoC do remove this temp var.
Fix it in v4.
Quoted 9 lines
Quoted 6 lines
drivers/mfd/nct6694.c new file +6
-0+if (!nct6694->int_buffer) +return -ENOMEM ; + + nct6694->int_in_urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!nct6694->int_in_urb) +return -ENOMEM ;...
Best regards,
Ming
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Le 12/12/2024 à 08:01, Ming Yu a écrit :
Quoted 20 lines
Dear Christophe,
Thank you for your comments,
Christophe JAILLET <[email protected]> 於 2024年12月12日 週四 上午12:44寫道:
Quoted 14 lines
drivers/mfd/nct6694.c line 100
Quoted 10 lines
drivers/mfd/nct6694.c new file +9
-0+int nct6694_read_msg (struct nct6694 *nct6694,u8 mod,u16 offset, +u16 length,void *buf) +{ +struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; +struct nct6694_response_header *response_header = nct6694->response_header; +struct usb_device *udev = nct6694->udev; +int tx_len, rx_len, ret; + +guard (mutex)(&nct6694->access_lock);Nitpick: This could be moved a few lines below, should it still comply with your coding style.
Quoted 3 lines
I think the lock should be placed here to prevent the
cmd_headerfrom being overwritten by another caller.
Could you share your perspective on this?
You are right, I misread the code :(
(I though cmd_header was a local structure)
Quoted 8 lines
Quoted 8 lines
drivers/mfd/nct6694.c line 107
Quoted 8 lines
drivers/mfd/nct6694.c new file +7
-0+ +/* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset &0xFF ; + cmd_header->sel = (offset >>8 ) &0xFF ; + cmd_header->hctrl =NCT6694_HCTRL_GET ; + cmd_header->len = length;
CJ
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
In reply to Ming YuHi Ming,
thanks for your patch!
Quoted 13 lines · Ming Yu
The Nuvoton NCT6694 is a peripheral expander with 16 GPIO chips, 6 I2C controllers, 2 CANfd controllers, 2 Watchdog timers, ADC, PWM, and RTC.
This driver implements USB device functionality and shares the chip's peripherals as a child device.
Each child device can use the USB functions
nct6694_read_msg()andnct6694_write_msg()to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.Signed-off-by Ming Yu <[email protected]>
(...)
drivers/mfd/nct6694.c line 199
Quoted 7 lines
drivers/mfd/nct6694.c new file +7
-0+while (*int_status) { +int irq =__ffs (*int_status); + +if (*int_status & (1 << irq)) +generic_handle_irq_safe (irq_find_mapping (nct6694->domain, irq)); + + *int_status &= ~(1 << irq);
What about doing what you do in the GPIO driver and #include <linux/bits.h>
And search and replace "(1 << irq)" with BIT(irq)?
PS the main reason we do this is because
int a = (1 << 31);
becomes a negative number on 32bit machines, and can lead to confusing side effects. BIT() always work on unsigned.
Yours,
Linus Walleij
Re: [PATCH v3 1/7] mfd: Add core driver for Nuvoton NCT6694
Dear Linus,
Thank you for your reply,
Linus Walleij <[email protected]> 於 2024年12月20日 週五 下午8:45寫道:
Quoted 5 lines
Hi Ming,
thanks for your patch!
...
Quoted 13 lines
drivers/mfd/nct6694.c line 199
Quoted 7 lines
drivers/mfd/nct6694.c new file +7
-0+while (*int_status) { +int irq =__ffs (*int_status); + +if (*int_status & (1 << irq)) +generic_handle_irq_safe (irq_find_mapping (nct6694->domain, irq)); + + *int_status &= ~(1 << irq);What about doing what you do in the GPIO driver and #include <
linux/bits.h>And search and replace "(1 << irq)" with BIT(irq)?
Quoted 8 lines
PS the main reason we do this is because
int a = (1 << 31);
becomes a negative number on 32bit machines, and can lead to confusing side effects.
BIT()always work on unsigned.
Understood! I will make the modifications in the v4.
Best regards,
Ming
PATCH v3 2/7 gpio: Add Nuvoton NCT6694 GPIO support
In reply to Ming YuThis driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.
--- MAINTAINERS | 1 + drivers/gpio/Kconfig | 12 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-nct6694.c | 438 ++++++++++++++++++++++++++++++++++++ 4 files changed, 452 insertions(+) create mode 100644 drivers/gpio/gpio-nct6694.c
MAINTAINERS +1 -0
@@ -16545,6 +16545,7 @@ NUVOTON NCT6694 MFD DRIVER M: Ming Yu <[email protected]> L: [email protected] S: Supported +F: drivers/gpio/gpio-nct6694.c F: drivers/mfd/nct6694.c F: include/linux/mfd/nct6694.h
drivers/gpio/Kconfig +12 -0
@@ -1460,6 +1460,18 @@ config GPIO_MAX77650 GPIO driver for MAX77650/77651 PMIC from Maxim Semiconductor. These chips have a single pin that can be configured as GPIO. +config GPIO_NCT6694 + tristate "Nuvoton NCT6694 GPIO controller support" + depends on MFD_NCT6694 + select GENERIC_IRQ_CHIP + select GPIOLIB_IRQCHIP + help + This driver supports 8 GPIO pins per bank that can all be interrupt + sources. + + This driver can also be built as a module. If so, the module will be + called gpio-nct6694. + config GPIO_PALMAS bool "TI PALMAS series PMICs GPIO" depends on MFD_PALMAS
drivers/gpio/Makefile +1 -0
@@ -123,6 +123,7 @@ obj-$(CONFIG_GPIO_MXC) += gpio-mxc.o obj-$(CONFIG_GPIO_MXS) += gpio-mxs.o obj-$(CONFIG_GPIO_NOMADIK) += gpio-nomadik.o obj-$(CONFIG_GPIO_NPCM_SGPIO) += gpio-npcm-sgpio.o +obj-$(CONFIG_GPIO_NCT6694) += gpio-nct6694.o obj-$(CONFIG_GPIO_OCTEON) += gpio-octeon.o obj-$(CONFIG_GPIO_OMAP) += gpio-omap.o obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o
drivers/gpio/gpio-nct6694.c new file +438 -0
438 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v3 2/7] gpio: Add Nuvoton NCT6694 GPIO support
Quoted 6 lines · Ming Yu
This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>---
Looks much better now. Please address one more issue I just noticed.
drivers/gpio/gpio-nct6694.c line 401
Quoted 2 lines
drivers/gpio/gpio-nct6694.c new file +2
-0+ +mutex_init (&data->irq_lock);
This is never destroyed. Please use devm_mutex_init() preferably to not add remove(). Also, the other mutex doesn't seem to be initialized at all.
Bart
Re: [PATCH v3 2/7] gpio: Add Nuvoton NCT6694 GPIO support
Dear Bartosz,
Thank you for your comments,
Bartosz Golaszewski <[email protected]> 於 2024年12月10日 週二 下午8:46寫道:
Quoted 10 lines
Looks much better now. Please address one more issue I just noticed.
drivers/gpio/gpio-nct6694.c line 401
Quoted 2 lines
drivers/gpio/gpio-nct6694.c new file +2
-0+ +mutex_init (&data->irq_lock);This is never destroyed. Please use
devm_mutex_init()preferably to not addremove(). Also, the other mutex doesn't seem to be initialized at all.
Understood! I will address the issue and include the missing part in the next patch.
For other drivers that use mutex_init() without destroying it should also be changed to devm_mutex_init(), right?
Best regards,
Ming
Re: [PATCH v3 2/7] gpio: Add Nuvoton NCT6694 GPIO support
In reply to Ming YuHi Ming,
thanks for your patch!
Some nits below:
Quoted 4 lines · Ming Yu
This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>
(...)
drivers/gpio/gpio-nct6694.c line 13
Quoted 6 lines
drivers/gpio/gpio-nct6694.c new file +6
-0+#include <linux/gpio/driver.h> +#include <linux/interrupt.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/platform_device.h>
#include <linux/bits.h>
is missing, include it explicitly.
Quoted 1 line
+ return !(BIT(offset) &
data->xmit_buf);
Here you use the BIT() macro from <linux/bits.h>
Quoted 4 lines
drivers/gpio/gpio-nct6694.c new file +4
-0+static int nct6694_direction_input (struct gpio_chip *gpio,unsigned int offset) +{ +struct nct6694_gpio_data *data =gpiochip_get_data (gpio); +int ret;
drivers/gpio/gpio-nct6694.c line 78
Quoted 10 lines
drivers/gpio/gpio-nct6694.c new file +10
-0+ +guard (mutex)(&data->lock); + + ret =nct6694_read_msg (data->nct6694,NCT6694_GPIO_MOD , +NCT6694_GPO_DIR + data->group, +NCT6694_GPIO_LEN , &data->xmit_buf); +if (ret <0 ) +return ret; + + data->xmit_buf &= ~(1 << offset);
data->xmit_buf &= ~BIT(offset);
Quoted 6 lines
drivers/gpio/gpio-nct6694.c new file +6
-0+static int nct6694_direction_output (struct gpio_chip *gpio, +unsigned int offset,int val) +{ +struct nct6694_gpio_data *data =gpiochip_get_data (gpio); +int ret; +
drivers/gpio/gpio-nct6694.c line 100
Quoted 10 lines
drivers/gpio/gpio-nct6694.c new file +10
-0+guard (mutex)(&data->lock); + +/* Set direction to output */ + ret =nct6694_read_msg (data->nct6694,NCT6694_GPIO_MOD , +NCT6694_GPO_DIR + data->group, +NCT6694_GPIO_LEN , &data->xmit_buf); +if (ret <0 ) +return ret; + + data->xmit_buf |= (1 << offset);
data->xmit_buf |= BIT(offset);
drivers/gpio/gpio-nct6694.c line 117
Quoted 4 lines
drivers/gpio/gpio-nct6694.c new file +4
-0+if (val) + data->xmit_buf |= (1 << offset); +else + data->xmit_buf &= ~(1 << offset);
Same
Quoted 3 lines
drivers/gpio/gpio-nct6694.c new file +3
-0+static void nct6694_set_value (struct gpio_chip *gpio,unsigned int offset, +int val) +{
(...)
drivers/gpio/gpio-nct6694.c line 117
Quoted 4 lines
drivers/gpio/gpio-nct6694.c new file +4
-0+if (val) + data->xmit_buf |= (1 << offset); +else + data->xmit_buf &= ~(1 << offset);
Same
Quoted 9 lines
drivers/gpio/gpio-nct6694.c new file +9
-0+static irqreturn_t nct6694_irq_handler (int irq,void *priv) +{ +struct nct6694_gpio_data *data = priv; +unsigned char status; + +guard (mutex)(&data->lock); + + nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_STS + data->group,
drivers/gpio/gpio-nct6694.c line 245
Quoted 10 lines
drivers/gpio/gpio-nct6694.c new file +10
-0+ NCT6694_GPIO_LEN, &data->xmit_buf); + + status = data->xmit_buf; + +while (status) { +int bit =__ffs (status); + + data->xmit_buf =BIT (bit); +handle_nested_irq (irq_find_mapping (data->gpio.irq.domain, bit)); + status &= ~(1 << bit);
Same
Just use BIT() consistently please.
Yours,
Linus Walleij
Re: [PATCH v3 2/7] gpio: Add Nuvoton NCT6694 GPIO support
Dear Linus,
Thank you for your comments,
Linus Walleij <[email protected]> 於 2024年12月20日 週五 下午8:42寫道:
Quoted 24 lines
Hi Ming,
thanks for your patch!
Some nits below:
Quoted 4 lines · Ming Yu
This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>(...)
drivers/gpio/gpio-nct6694.c line 13
Quoted 6 lines
drivers/gpio/gpio-nct6694.c new file +6
-0+#include <linux/gpio/driver.h> +#include <linux/interrupt.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/platform_device.h> #include <
linux/bits.h>
is missing, include it explicitly.
Quoted 4 lines
Quoted 1 line
+ return !(BIT(offset) &
data->xmit_buf);Here you use the
BIT()macro from <linux/bits.h>
Understood. I will add the header in the next patch.
Quoted 8 lines
Quoted 8 lines
drivers/gpio/gpio-nct6694.c new file +8
-0+static int nct6694_direction_input (struct gpio_chip *gpio,unsigned int offset) +{ +struct nct6694_gpio_data *data =gpiochip_get_data (gpio); +int ret; + +guard (mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD,
Quoted 9 lines
drivers/gpio/gpio-nct6694.c line 78
Quoted 6 lines
drivers/gpio/gpio-nct6694.c new file +6
-0+NCT6694_GPO_DIR + data->group, + NCT6694_GPIO_LEN, &data->xmit_buf); +if (ret <0 ) +return ret; + + data->xmit_buf &= ~(1 << offset);
data->xmit_buf&= ~BIT(offset);
Okay! Fix it in the v4.
Quoted 10 lines
Quoted 10 lines
drivers/gpio/gpio-nct6694.c new file +10
-0+static int nct6694_direction_output (struct gpio_chip *gpio, +unsigned int offset,int val) +{ +struct nct6694_gpio_data *data =gpiochip_get_data (gpio); +int ret; + +guard (mutex)(&data->lock); + +/* Set direction to output */ + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD,
Quoted 9 lines
drivers/gpio/gpio-nct6694.c line 100
Quoted 6 lines
drivers/gpio/gpio-nct6694.c new file +6
-0+NCT6694_GPO_DIR + data->group, + NCT6694_GPIO_LEN, &data->xmit_buf); +if (ret <0 ) +return ret; + + data->xmit_buf |= (1 << offset);
data->xmit_buf|= BIT(offset);
Okay! Fix it in the v4.
Quoted 7 lines
drivers/gpio/gpio-nct6694.c line 117
Quoted 4 lines
drivers/gpio/gpio-nct6694.c new file +4
-0+if (val) + data->xmit_buf |= (1 << offset); +else + data->xmit_buf &= ~(1 << offset);Same
Okay! Fix it in the v4.
Quoted 11 lines
Quoted 3 lines
drivers/gpio/gpio-nct6694.c new file +3
-0+static void nct6694_set_value (struct gpio_chip *gpio,unsigned int offset, +int val) +{(...)
drivers/gpio/gpio-nct6694.c line 117
Quoted 4 lines
drivers/gpio/gpio-nct6694.c new file +4
-0+if (val) + data->xmit_buf |= (1 << offset); +else + data->xmit_buf &= ~(1 << offset);Same
Okay! Fix it in the v4.
Quoted 22 lines
Quoted 9 lines
drivers/gpio/gpio-nct6694.c new file +9
-0+static irqreturn_t nct6694_irq_handler (int irq,void *priv) +{ +struct nct6694_gpio_data *data = priv; +unsigned char status; + +guard (mutex)(&data->lock); + + nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_STS + data->group,drivers/gpio/gpio-nct6694.c line 245
Quoted 10 lines
drivers/gpio/gpio-nct6694.c new file +10
-0+ NCT6694_GPIO_LEN, &data->xmit_buf); + + status = data->xmit_buf; + +while (status) { +int bit =__ffs (status); + + data->xmit_buf =BIT (bit); +handle_nested_irq (irq_find_mapping (data->gpio.irq.domain, bit)); + status &= ~(1 << bit);Same
Quoted 2 lines
Just use
BIT()consistently please.
Okay! Fix it in the v4.
Best regards,
Ming
PATCH v3 3/7 i2c: Add Nuvoton NCT6694 I2C support
In reply to Ming YuThis driver supports I2C adapter functionality for NCT6694 MFD device based on USB interface, each I2C controller use default baudrate(100K).
--- MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 ++ drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-nct6694.c | 153 +++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 drivers/i2c/busses/i2c-nct6694.c
MAINTAINERS +1 -0
@@ -16546,6 +16546,7 @@ M: Ming Yu <[email protected]> L: [email protected] S: Supported F: drivers/gpio/gpio-nct6694.c +F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: include/linux/mfd/nct6694.h
drivers/i2c/busses/Kconfig +10 -0
@@ -1325,6 +1325,16 @@ config I2C_LJCA This driver can also be built as a module. If so, the module will be called i2c-ljca. +config I2C_NCT6694 + tristate "Nuvoton NCT6694 I2C adapter support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB to I2C interface. + + This driver can also be built as a module. If so, the module will + be called i2c-nct6694. + config I2C_CP2615 tristate "Silicon Labs CP2615 USB sound card and I2C adapter" depends on USB
drivers/i2c/busses/Makefile +1 -0
@@ -135,6 +135,7 @@ obj-$(CONFIG_I2C_GXP) += i2c-gxp.o obj-$(CONFIG_I2C_DIOLAN_U2C) += i2c-diolan-u2c.o obj-$(CONFIG_I2C_DLN2) += i2c-dln2.o obj-$(CONFIG_I2C_LJCA) += i2c-ljca.o +obj-$(CONFIG_I2C_NCT6694) += i2c-nct6694.o obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o obj-$(CONFIG_I2C_PARPORT) += i2c-parport.o obj-$(CONFIG_I2C_PCI1XXXX) += i2c-mchp-pci1xxxx.o
drivers/i2c/busses/i2c-nct6694.c new file +153 -0
153 lines not on this page · raw message
Signature · 1 line
2.34.1
Andi Shyti
maintainerRe: [PATCH v3 3/7] i2c: Add Nuvoton NCT6694 I2C support
Hi Ming,
Quoted 5 lines
drivers/i2c/busses/i2c-nct6694.c new file +5
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* Nuvoton NCT6694 I2C adapter driver based on USB interface. +* +* Copyright (C) 2024 Nuvoton Technology Corp.
drivers/i2c/busses/i2c-nct6694.c line 15
Quoted 10 lines
drivers/i2c/busses/i2c-nct6694.c new file +10
-0+*/ + +#include <linux/i2c.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/platform_device.h> + +/* Host interface */
What does it mean "Host interface"?
drivers/i2c/busses/i2c-nct6694.c line 19
Quoted 4 lines
drivers/i2c/busses/i2c-nct6694.c new file +4
-0+#define NCT6694_I2C_MOD 0x03 + +/* Message Channel*/ +/* Command 00h */
This comments are meaningless, either make them clearer or remove them.
Quoted 1 line
+#define
NCT6694_I2C_CMD0_OFFSET0x0000/* OFFSET = SEL|CMD */
I find this comment quite meaningless. Can you please make it clearer?
Quoted 17 lines
drivers/i2c/busses/i2c-nct6694.c new file +17
-0+#define NCT6694_I2C_CMD0_LEN 0x90 + +enum i2c_baudrate { +I2C_BR_25K =0 , +I2C_BR_50K , +I2C_BR_100K , +I2C_BR_200K , +I2C_BR_400K , +I2C_BR_800K , +I2C_BR_1M +}; + +struct __packed nct6694_i2c_cmd0 { +u8 port; +u8 br; +u8 addr; +u8 w_cnt;
drivers/i2c/busses/i2c-nct6694.c line 47
Quoted 10 lines
drivers/i2c/busses/i2c-nct6694.c new file +10
-0+u8 r_cnt; +u8 rsv[11 ]; +u8 write_data[0x40 ]; +u8 read_data[0x40 ]; +}; + +struct nct6694_i2c_data { +struct nct6694 *nct6694; +struct i2c_adapter adapter; +unsigned char *xmit_buf;
why isn't this a nct6694_i2c_cmd0 type?
Quoted 18 lines
drivers/i2c/busses/i2c-nct6694.c new file +18
-0+unsigned char port; +unsigned char br; +}; + +static int nct6694_xfer (struct i2c_adapter *adap,struct i2c_msg *msgs,int num) +{ +struct nct6694_i2c_data *data = adap->algo_data; +struct nct6694_i2c_cmd0 *cmd = (struct nct6694_i2c_cmd0 *)data->xmit_buf; +int ret, i; + +for (i =0 ; i < num ; i++) { +struct i2c_msg *msg_temp = &msgs[i]; + +memset (data->xmit_buf,0 ,sizeof (struct nct6694_i2c_cmd0 )); + +if (msg_temp->len >64 ) +return -EPROTO ; + cmd->port = data->port;
drivers/i2c/busses/i2c-nct6694.c line 75
Quoted 10 lines
drivers/i2c/busses/i2c-nct6694.c new file +10
-0+ cmd->br = data->br; + cmd->addr =i2c_8bit_addr_from_msg (msg_temp); +if (msg_temp->flags &I2C_M_RD ) { + cmd->r_cnt = msg_temp->len; + ret =nct6694_write_msg (data->nct6694,NCT6694_I2C_MOD , +NCT6694_I2C_CMD0_OFFSET , +NCT6694_I2C_CMD0_LEN , + cmd); +if (ret <0 ) +return 0 ;
why not return ret?
Quoted 10 lines
drivers/i2c/busses/i2c-nct6694.c new file +10
-0+ +memcpy (msg_temp->buf, cmd->read_data, msg_temp->len); + }else { + cmd->w_cnt = msg_temp->len; +memcpy (cmd->write_data, msg_temp->buf, msg_temp->len); + ret =nct6694_write_msg (data->nct6694,NCT6694_I2C_MOD , +NCT6694_I2C_CMD0_OFFSET , +NCT6694_I2C_CMD0_LEN , + cmd); +if (ret <0 )
drivers/i2c/busses/i2c-nct6694.c line 95
Quoted 10 lines
drivers/i2c/busses/i2c-nct6694.c new file +10
-0+return 0 ; + } + } + +return num; +} + +static u32 nct6694_func (struct i2c_adapter *adapter) +{ +return (I2C_FUNC_I2C |I2C_FUNC_SMBUS_EMUL );
parenthesis are not needed.
Quoted 1 line
+}
...
drivers/i2c/busses/i2c-nct6694.c line 148
Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c new file +9
-0+static struct platform_driver nct6694_i2c_driver = { + .driver = { + .name ="nct6694-i2c" , + }, + .probe = nct6694_i2c_probe, + .remove = nct6694_i2c_remove, +}; + +module_platform_driver (nct6694_i2c_driver);
what I meant in v1 is to try using module_auxiliary_driver(). Check, e.g., i2c-ljca.c or i2c-keba.c.
Andi
Quoted 7 lines
drivers/i2c/busses/i2c-nct6694.c new file +4
-0+MODULE_DESCRIPTION ("USB-I2C adapter driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-i2c" );Signature · 1 line
2.34.1
Re: [PATCH v3 3/7] i2c: Add Nuvoton NCT6694 I2C support
Dear Andi,
Thank you for your comments,
Andi Shyti <[email protected]> 於 2024年12月26日 週四 上午8:43寫道:
Quoted 20 lines
drivers/i2c/busses/i2c-nct6694.c line 15
Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c new file +8
-0+#include <linux/i2c.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/platform_device.h> + +/* Host interface */ What does it mean "Host interface"?
drivers/i2c/busses/i2c-nct6694.c line 19
Quoted 4 lines
drivers/i2c/busses/i2c-nct6694.c new file +4
-0+#define NCT6694_I2C_MOD 0x03 + +/* Message Channel*/ +/* Command 00h */ This comments are meaningless, either make them clearer or remove them.
Quoted 5 lines
Quoted 1 line
+#define
NCT6694_I2C_CMD0_OFFSET0x0000/* OFFSET = SEL|CMD */I find this comment quite meaningless. Can you please make it clearer?
I have already updated these structures and comments following suggestions from other reviewers, and I plan to include the changes in the next patch submission.
Quoted 21 lines
Quoted 21 lines
drivers/i2c/busses/i2c-nct6694.c new file +21
-0+#define NCT6694_I2C_CMD0_LEN 0x90 + +enum i2c_baudrate { +I2C_BR_25K =0 , +I2C_BR_50K , +I2C_BR_100K , +I2C_BR_200K , +I2C_BR_400K , +I2C_BR_800K , +I2C_BR_1M +}; + +struct __packed nct6694_i2c_cmd0 { +u8 port; +u8 br; +u8 addr; +u8 w_cnt; +u8 r_cnt; +u8 rsv[11 ]; +u8 write_data[0x40 ]; +u8 read_data[0x40 ];
Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c line 47
Quoted 6 lines
drivers/i2c/busses/i2c-nct6694.c new file +6
-0+}; + +struct nct6694_i2c_data { +struct nct6694 *nct6694; +struct i2c_adapter adapter; +unsigned char *xmit_buf;why isn't this a
nct6694_i2c_cmd0type?
Fix it in v4.
Quoted 22 lines
Quoted 22 lines
drivers/i2c/busses/i2c-nct6694.c new file +22
-0+unsigned char port; +unsigned char br; +}; + +static int nct6694_xfer (struct i2c_adapter *adap,struct i2c_msg *msgs,int num) +{ +struct nct6694_i2c_data *data = adap->algo_data; +struct nct6694_i2c_cmd0 *cmd = (struct nct6694_i2c_cmd0 *)data->xmit_buf; +int ret, i; + +for (i =0 ; i < num ; i++) { +struct i2c_msg *msg_temp = &msgs[i]; + +memset (data->xmit_buf,0 ,sizeof (struct nct6694_i2c_cmd0 )); + +if (msg_temp->len >64 ) +return -EPROTO ; + cmd->port = data->port; + cmd->br = data->br; + cmd->addr =i2c_8bit_addr_from_msg (msg_temp); +if (msg_temp->flags &I2C_M_RD ) { + cmd->r_cnt = msg_temp->len;
Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c line 75
Quoted 6 lines
drivers/i2c/busses/i2c-nct6694.c new file +6
-0+ ret =nct6694_write_msg (data->nct6694,NCT6694_I2C_MOD , +NCT6694_I2C_CMD0_OFFSET , +NCT6694_I2C_CMD0_LEN , + cmd); +if (ret <0 ) +return 0 ;why not return ret?
Fix it in v4.
Quoted 14 lines
Quoted 14 lines
drivers/i2c/busses/i2c-nct6694.c new file +14
-0+ +memcpy (msg_temp->buf, cmd->read_data, msg_temp->len); + }else { + cmd->w_cnt = msg_temp->len; +memcpy (cmd->write_data, msg_temp->buf, msg_temp->len); + ret =nct6694_write_msg (data->nct6694,NCT6694_I2C_MOD , +NCT6694_I2C_CMD0_OFFSET , +NCT6694_I2C_CMD0_LEN , + cmd); +if (ret <0 ) +return 0 ; + } + } +
Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c line 95
Quoted 6 lines
drivers/i2c/busses/i2c-nct6694.c new file +6
-0+return num; +} + +static u32 nct6694_func (struct i2c_adapter *adapter) +{ +return (I2C_FUNC_I2C |I2C_FUNC_SMBUS_EMUL );parenthesis are not needed.
Fix it in v4.
Quoted 4 lines
Quoted 1 line
+}
...
Quoted 13 lines
drivers/i2c/busses/i2c-nct6694.c line 148
Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c new file +9
-0+static struct platform_driver nct6694_i2c_driver = { + .driver = { + .name ="nct6694-i2c" , + }, + .probe = nct6694_i2c_probe, + .remove = nct6694_i2c_remove, +}; + +module_platform_driver (nct6694_i2c_driver);what I meant in v1 is to try using
module_auxiliary_driver(). Check, e.g.,i2c-ljca.cori2c-keba.c.
I think the NCT6694 is an MCU-based device, and the current implementation is as an MFD driver. Are you suggesting it should instead be implemented as an auxiliary device driver? If so, would that mean all related drivers need to be revised accordingly?
Best regards,
Ming
Andi Shyti
maintainerRe: [PATCH v3 3/7] i2c: Add Nuvoton NCT6694 I2C support
Hi Ming,
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/i2c/busses/i2c-nct6694.c new file +4
-0+static struct platform_driver nct6694_i2c_driver = { + .driver = { + .name ="nct6694-i2c" , + },Quoted 8 lines
drivers/i2c/busses/i2c-nct6694.c line 148
Quoted 5 lines
drivers/i2c/busses/i2c-nct6694.c new file +5
-0+ .probe = nct6694_i2c_probe, + .remove = nct6694_i2c_remove, +}; + +module_platform_driver (nct6694_i2c_driver);what I meant in v1 is to try using
module_auxiliary_driver(). Check, e.g.,i2c-ljca.cori2c-keba.c.
Quoted 4 lines
I think the NCT6694 is an MCU-based device, and the current implementation is as an MFD driver. Are you suggesting it should instead be implemented as an auxiliary device driver? If so, would that mean all related drivers need to be revised accordingly?
No worries, module_platform_driver() is also fine.
Andi
PATCH v3 4/7 can: Add Nuvoton NCT6694 CAN support
In reply to Ming YuThis driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface.
--- MAINTAINERS | 1 + drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/nct6694_canfd.c | 920 ++++++++++++++++++++++++++++++++ 4 files changed, 932 insertions(+) create mode 100644 drivers/net/can/nct6694_canfd.c
MAINTAINERS +1 -0
@@ -16548,6 +16548,7 @@ S: Supported F: drivers/gpio/gpio-nct6694.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c +F: drivers/net/can/nct6694_canfd.c F: include/linux/mfd/nct6694.h NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/net/can/Kconfig +10 -0
@@ -200,6 +200,16 @@ config CAN_SUN4I To compile this driver as a module, choose M here: the module will be called sun4i_can. +config CAN_NCT6694 + tristate "Nuvoton NCT6694 Socket CANfd support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to socket CANfd controller. + + This driver can also be built as a module. If so, the module will + be called nct6694_canfd. + config CAN_TI_HECC depends on ARM tristate "TI High End CAN Controller"
drivers/net/can/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_CAN_JANZ_ICAN3) += janz-ican3.o obj-$(CONFIG_CAN_KVASER_PCIEFD) += kvaser_pciefd.o obj-$(CONFIG_CAN_MSCAN) += mscan/ obj-$(CONFIG_CAN_M_CAN) += m_can/ +obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o obj-$(CONFIG_CAN_PEAK_PCIEFD) += peak_canfd/ obj-$(CONFIG_CAN_SJA1000) += sja1000/ obj-$(CONFIG_CAN_SUN4I) += sun4i_can.o
drivers/net/can/nct6694_canfd.c new file +920 -0
920 lines not on this page · raw message
Signature · 1 line
2.34.1
Marc Kleine-Budde
maintainerRe: [PATCH v3 4/7] can: Add Nuvoton NCT6694 CAN support
Quoted 2 lines · Ming Yu
This driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface.
Please use the rx-offload helper, otherwise the CAN frames might be revived out of order.
Quoted 121 lines
Signed-off-by Ming Yu <[email protected]>--- MAINTAINERS | 1 + drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/nct6694_canfd.c | 920 ++++++++++++++++++++++++++++++++ 4 files changed, 932 insertions(+) create mode 100644 drivers/net/can/nct6694_canfd.cMAINTAINERS +1
-0@@ -16548,6 +16548,7 @@ S: Supported F: drivers/gpio/gpio-nct6694.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c +F: drivers/net/can/nct6694_canfd.c F: include/linux/mfd/nct6694.h NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVERdrivers/net/can/Kconfig +10
-0@@ -200,6 +200,16 @@ config CAN_SUN4I To compile this driver as a module, choose M here: the module will be called sun4i_can. +config CAN_NCT6694 + tristate "Nuvoton NCT6694 Socket CANfd support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to socket CANfd controller. + + This driver can also be built as a module. If so, the module will + be called nct6694_canfd. + config CAN_TI_HECC depends on ARM tristate "TI High End CAN Controller"drivers/net/can/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_CAN_JANZ_ICAN3) += janz-ican3.o obj-$(CONFIG_CAN_KVASER_PCIEFD) += kvaser_pciefd.o obj-$(CONFIG_CAN_MSCAN) += mscan/ obj-$(CONFIG_CAN_M_CAN) += m_can/ +obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o obj-$(CONFIG_CAN_PEAK_PCIEFD) += peak_canfd/ obj-$(CONFIG_CAN_SJA1000) += sja1000/ obj-$(CONFIG_CAN_SUN4I) += sun4i_can.odrivers/net/can/nct6694_canfd.c new file +60
-0@@ -0,0 +1,920 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Nuvoton NCT6694 Socket CANfd driver based on USB interface. +* +* Copyright (C) 2024 Nuvoton Technology Corp. +*/ + +#include <linux/can/dev.h> +#include <linux/ethtool.h> +#include <linux/irqdomain.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/netdevice.h> +#include <linux/platform_device.h> + +#define DRVNAME "nct6694-can" + +/* Host interface */ +#define NCT6694_CAN_MOD 0x05 + +/* Message Channel*/ +/* Command 00h */ +#define NCT6694_CAN_CMD0_OFFSET (idx) (idx ? 0x0100 : 0x0000) +#define NCT6694_CAN_CTRL1_MON BIT(0) +#define NCT6694_CAN_CTRL1_NISO BIT(1) +#define NCT6694_CAN_CTRL1_LBCK BIT(2) + +/* Command 01h */ +#define NCT6694_CAN_CMD1_OFFSET 0x0001 + +/* Command 02h */ +#define NCT6694_CAN_CMD2_OFFSET (idx, mask) \ + ({ typeof(mask) mask_ = (mask); \ + idx ? ((0x80 | (mask_ & 0xFF)) << 8 | 0x02) : \ + ((0x00 | (mask_ & 0xFF)) << 8 | 0x02); }) + +#define NCT6694_CAN_EVENT_ERR BIT(0) +#define NCT6694_CAN_EVENT_STATUS BIT(1) +#define NCT6694_CAN_EVENT_TX_EVT BIT(2) +#define NCT6694_CAN_EVENT_RX_EVT BIT(3) +#define NCT6694_CAN_EVENT_REC BIT(4) +#define NCT6694_CAN_EVENT_TEC BIT(5) +#define NCT6694_CAN_EVT_TX_FIFO_EMPTY BIT(7)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_LOST BIT(5)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_HALF_FULL BIT(6)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_IN BIT(7) + +/* Command 10h */ +#define NCT6694_CAN_CMD10_OFFSET (buf_cnt) \ + (((buf_cnt) & 0xFF) << 8 | 0x10) +#define NCT6694_CAN_TAG_CAN0 0xC0 +#define NCT6694_CAN_TAG_CAN1 0xC1 +#define NCT6694_CAN_FLAG_EFF BIT(0) +#define NCT6694_CAN_FLAG_RTR BIT(1) +#define NCT6694_CAN_FLAG_FD BIT(2) +#define NCT6694_CAN_FLAG_BRS BIT(3) +#define NCT6694_CAN_FLAG_ERR BIT(4) +
drivers/net/can/nct6694_canfd.c line 70
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+/* Command 11h */ +#define NCT6694_CAN_CMD11_OFFSET (idx, buf_cnt) \ + ({ typeof(buf_cnt) buf_cnt_ = (buf_cnt); \ + idx ? ((0x80 | (buf_cnt_ & 0xFF)) << 8 | 0x11) : \ + ((0x00 | (buf_cnt_ & 0xFF)) << 8 | 0x11); }) + +#define NCT6694_CAN_RX_QUOTA 64 + +enum nct6694_event_err { +NCT6694_CAN_EVT_NO_ERROR ,
^^^ add _ERR_
drivers/net/can/nct6694_canfd.c line 81
Quoted 11 lines
drivers/net/can/nct6694_canfd.c new file +11
-0+NCT6694_CAN_EVT_CRC_ERROR , +NCT6694_CAN_EVT_STUFF_ERROR , +NCT6694_CAN_EVT_ACK_ERROR , +NCT6694_CAN_EVT_FORM_ERROR , +NCT6694_CAN_EVT_BIT_ERROR , +NCT6694_CAN_EVT_TIMEOUT_ERROR , + NCT6694_CAN_EVT_UNKNOWN_ERROR, +}; + +enum nct6694_event_status { + NCT6694_CAN_EVT_ERROR_ACTIVE,
^^^ add _STATUS_
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+NCT6694_CAN_EVT_ERROR_PASSIVE , +NCT6694_CAN_EVT_BUS_OFF , + NCT6694_CAN_EVT_WARNING, +}; + +struct __packed nct6694_can_setting { +__le32 nbr; +__le32 dbr; +u8 active; +u8 reserved[3 ];
drivers/net/can/nct6694_canfd.c line 101
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+__le16 ctrl1; +__le16 ctrl2; +__le32 nbtp; +__le32 dbtp; +}; + +struct __packed nct6694_can_information { +u8 tx_fifo_cnt; +u8 rx_fifo_cnt; +__le16 reserved;
u8 reserved[2];
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+__le32 can_clk; +}; + +struct __packed nct6694_can_event { +u8 err1; +u8 status1; +u8 tx_evt1; +u8 rx_evt1; +u8 rec1;
drivers/net/can/nct6694_canfd.c line 120
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+u8 tec1; +u8 reserved1[2 ]; +u8 err2; +u8 status2; +u8 tx_evt2; +u8 rx_evt2; +u8 rec2; +u8 tec2; +u8 reserved2[2 ]; +};
Create an extra struct that only describes a channel
struct __packed nct6694_can_event_channel {
u8 err;
u8 status;
u8 tx_evt;
u8 rx_evt;
u8 rec;
u8 tec;
u8 reserved[2];
}and put an array of 2 into struct __packed nct6694_can_event.
drivers/net/can/nct6694_canfd.c line 129
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+ +struct __packed nct6694_can_xmit { +u8 tag; +u8 flag; +u8 reserved; +u8 dlc; +__le32 id; +u8 data[64 ]; +u8 msg_buf[72 ];
Why is the message so long? What's in the msg_buf?
drivers/net/can/nct6694_canfd.c line 136
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+}; + +struct nct6694_can_priv { +struct can_priv can;/* must be the first member */ +struct net_device *ndev; +struct nct6694 *nct6694; +struct mutex lock;
What does lock protect?
drivers/net/can/nct6694_canfd.c line 140
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+struct sk_buff *tx_skb; +struct workqueue_struct *wq; +struct work_struct tx_work; +unsigned char *tx_buf;
void *
drivers/net/can/nct6694_canfd.c line 141
Quoted 1 line
drivers/net/can/nct6694_canfd.c new file +1
-0+unsigned char *rx_buf;
void *
drivers/net/can/nct6694_canfd.c line 143
Quoted 2 lines
drivers/net/can/nct6694_canfd.c new file +2
-0+unsigned char can_idx; +bool tx_busy;
IMHO it makes no sense to have tx_skb and tx_busy
Quoted 86 lines
drivers/net/can/nct6694_canfd.c new file +86
-0+}; + +static const struct can_bittiming_const nct6694_can_bittiming_nominal_const = { + .name =DRVNAME , + .tseg1_min =2 , + .tseg1_max =256 , + .tseg2_min =2 , + .tseg2_max =128 , + .sjw_max =128 , + .brp_min =1 , + .brp_max =511 , + .brp_inc =1 , +}; + +static const struct can_bittiming_const nct6694_can_bittiming_data_const = { + .name =DRVNAME , + .tseg1_min =1 , + .tseg1_max =32 , + .tseg2_min =1 , + .tseg2_max =16 , + .sjw_max =16 , + .brp_min =1 , + .brp_max =31 , + .brp_inc =1 , +}; + +static void nct6694_can_clean (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +if (priv->tx_skb || priv->tx_busy) + ndev->stats.tx_errors++; +dev_kfree_skb (priv->tx_skb); +if (priv->tx_busy) +can_free_echo_skb (priv->ndev,0 ,NULL ); + priv->tx_skb =NULL ; + priv->tx_busy = false; +} + +static int nct6694_can_get_berr_counter (const struct net_device *ndev, +struct can_berr_counter *bec) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +u8 mask =NCT6694_CAN_EVENT_REC |NCT6694_CAN_EVENT_TEC ; +int ret; + +guard (mutex)(&priv->lock); + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (priv->can_idx, mask), +sizeof (struct nct6694_can_event ), + evt); +if (ret <0 ) +return ret; + + bec->rxerr = priv->can_idx ? evt->rec2 : evt->rec1; + bec->txerr = priv->can_idx ? evt->tec2 : evt->tec1; + +return 0 ; +} + +static int nct6694_can_handle_lost_msg (struct net_device *ndev) +{ +struct net_device_stats *stats = &ndev->stats; +struct sk_buff *skb; +struct can_frame *frame; + +netdev_err (ndev,"RX FIFO overflow, message(s) lost.\n" ); + + stats->rx_errors++; + stats->rx_over_errors++; + + skb =alloc_can_err_skb (ndev, &frame); +if (unlikely (!skb)) +return 0 ; + + frame->can_id |=CAN_ERR_CRTL ; + frame->data[1 ] =CAN_ERR_CRTL_RX_OVERFLOW ; + +netif_receive_skb (skb); + +return 1 ; +} + +static void nct6694_can_read_fifo (struct net_device *ndev)
drivers/net/can/nct6694_canfd.c line 239
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->rx_buf; +struct net_device_stats *stats = &ndev->stats; +struct canfd_frame *cf; +struct sk_buff *skb; +int can_idx = priv->can_idx; +u32 id; +int ret; +u8 fd_format =0 ;
bool - no need to init
Quoted 12 lines
drivers/net/can/nct6694_canfd.c new file +12
-0+ +guard (mutex)(&priv->lock); + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD11_OFFSET (can_idx,1 ), +sizeof (struct nct6694_can_xmit ), xmit); +if (ret <0 ) +return ; + +/* Check type of frame and create skb */ + fd_format = xmit->flag &NCT6694_CAN_FLAG_FD ; +if (fd_format)
drivers/net/can/nct6694_canfd.c line 261
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ skb =alloc_canfd_skb (ndev, &cf); + else + skb =alloc_can_skb (ndev, (struct can_frame **)&cf); + +if (!skb) { + stats->rx_dropped++; +return ; + } + + cf->len = xmit->dlc;
what does xmit->dlc contain? The DLC or the length?
drivers/net/can/nct6694_canfd.c line 270
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+ +/* Get ID and set flag by its type(Standard ID format or Ext ID format) */ + id =le32_to_cpu (xmit->id); +if (xmit->flag &NCT6694_CAN_FLAG_EFF ) { +/* +* In case the Extended ID frame is received, the standard +* and extended part of the ID are swapped in the register, +* so swap them back to obtain the correct ID. +*/
You comment doesn't match the code.
Quoted 247 lines
drivers/net/can/nct6694_canfd.c new file +247
-0+ id |=CAN_EFF_FLAG ; + } + + cf->can_id = id; + +/* Set ESI flag */ +if (xmit->flag &NCT6694_CAN_FLAG_ERR ) { + cf->flags |=CANFD_ESI ; +netdev_dbg (ndev,"ESI Error\n" ); + } + +/* Set RTR and BRS */ +if (!fd_format && (xmit->flag &NCT6694_CAN_FLAG_RTR )) { + cf->can_id |=CAN_RTR_FLAG ; + }else { +if (xmit->flag &NCT6694_CAN_FLAG_BRS ) + cf->flags |=CANFD_BRS ; + +memcpy (cf->data, xmit->data, cf->len); + + stats->rx_bytes += cf->len; + } + + stats->rx_packets++; + +netif_receive_skb (skb); +} + +static int nct6694_can_do_rx_poll (struct net_device *ndev,int quota) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx; +u32 pkts =0 ; +u8 mask_rx =NCT6694_CAN_EVENT_RX_EVT ; +u8 rx_evt; + +for (;;) { + scoped_guard(mutex, &priv->lock) { +nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, mask_rx), +sizeof (struct nct6694_can_event ), evt); + + rx_evt = can_idx ? evt->rx_evt2 : evt->rx_evt1; + } + +if (rx_evt &NCT6694_CAN_EVT_RX_DATA_LOST ) +nct6694_can_handle_lost_msg (ndev); + +/* No data */ +if ((rx_evt &NCT6694_CAN_EVT_RX_DATA_IN ) ==0 ) +break ; + +if (quota <=0 ) +break ; + +nct6694_can_read_fifo (ndev); + quota--; + pkts++; + } + +return pkts; +} + +static int nct6694_can_handle_lec_err (struct net_device *ndev,u8 bus_err) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; +struct can_frame *cf; +struct sk_buff *skb; + +if (bus_err ==NCT6694_CAN_EVT_NO_ERROR ) +return 0 ; + + priv->can.can_stats.bus_error++; + stats->rx_errors++; + +/* Propagate the error condition to the CAN stack. */ + skb =alloc_can_err_skb (ndev, &cf); + +if (unlikely (!skb)) +return 0 ; + +/* Read the error counter register and check for new errors. */ + cf->can_id |=CAN_ERR_PROT |CAN_ERR_BUSERROR ; + +switch (bus_err) { +case NCT6694_CAN_EVT_CRC_ERROR : + cf->data[3 ] =CAN_ERR_PROT_LOC_CRC_SEQ ; +break ; + +case NCT6694_CAN_EVT_STUFF_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_STUFF ; +break ; + +case NCT6694_CAN_EVT_ACK_ERROR : + cf->data[3 ] =CAN_ERR_PROT_LOC_ACK ; +break ; + +case NCT6694_CAN_EVT_FORM_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_FORM ; +break ; + +case NCT6694_CAN_EVT_BIT_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_BIT | +CAN_ERR_PROT_BIT0 | +CAN_ERR_PROT_BIT1 ; +break ; + +case NCT6694_CAN_EVT_TIMEOUT_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_UNSPEC ; +break ; + +case NCT6694_CAN_EVT_UNKNOWN_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_UNSPEC ; +/* +* It means 'unspecified'(the value is '0'). +* But it is not sure if it's ok to send an error package +* without specific error bit. +*/ +break ; + +default : +break ; + } + +/* Reset the error counter, ack the IRQ and re-enable the counter. */ + stats->rx_packets++; + stats->rx_bytes += cf->can_dlc; +netif_receive_skb (skb); + +return 1 ; +} + +static int nct6694_can_handle_state_change (struct net_device *ndev, +enum can_state new_state) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; +struct can_frame *cf; +struct sk_buff *skb; +struct can_berr_counter bec; + +switch (new_state) { +case CAN_STATE_ERROR_ACTIVE : + priv->can.can_stats.error_warning++; + priv->can.state =CAN_STATE_ERROR_ACTIVE ; +break ; +case CAN_STATE_ERROR_WARNING : + priv->can.can_stats.error_warning++; + priv->can.state =CAN_STATE_ERROR_WARNING ; +break ; +case CAN_STATE_ERROR_PASSIVE : + priv->can.can_stats.error_passive++; + priv->can.state =CAN_STATE_ERROR_PASSIVE ; +break ; +case CAN_STATE_BUS_OFF : + priv->can.state =CAN_STATE_BUS_OFF ; + priv->can.can_stats.bus_off++; +can_bus_off (ndev); +break ; +default : +break ; + } + +/* propagate the error condition to the CAN stack */ + skb =alloc_can_err_skb (ndev, &cf); +if (unlikely (!skb)) +return 0 ; + +nct6694_can_get_berr_counter (ndev, &bec); + +switch (new_state) { +case CAN_STATE_ERROR_WARNING : +/* error warning state */ + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] = (bec.txerr > bec.rxerr) ?CAN_ERR_CRTL_TX_WARNING : +CAN_ERR_CRTL_RX_WARNING ; + cf->data[6 ] = bec.txerr; + cf->data[7 ] = bec.rxerr; +break ; +case CAN_STATE_ERROR_PASSIVE : +/* error passive state */ + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] |=CAN_ERR_CRTL_RX_PASSIVE ; +if (bec.txerr >127 ) + cf->data[1 ] |=CAN_ERR_CRTL_TX_PASSIVE ; + cf->data[6 ] = bec.txerr; + cf->data[7 ] = bec.rxerr; +break ; +case CAN_STATE_BUS_OFF : +/* bus-off state */ + cf->can_id |=CAN_ERR_BUSOFF ; +break ; +default : +break ; + } + + stats->rx_packets++; + stats->rx_bytes += cf->can_dlc; +netif_receive_skb (skb); + +return 1 ; +} + +static int nct6694_can_handle_state_errors (struct net_device *ndev, +unsigned char can_status) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +int work_done =0 ; + +if (can_status ==NCT6694_CAN_EVT_ERROR_ACTIVE && + priv->can.state !=CAN_STATE_ERROR_ACTIVE ) { +netdev_dbg (ndev,"Error, entered active state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_ERROR_ACTIVE ); + } + +if (can_status ==NCT6694_CAN_EVT_WARNING && + priv->can.state !=CAN_STATE_ERROR_WARNING ) { +netdev_dbg (ndev,"Error, entered warning state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_ERROR_WARNING ); + } + +if (can_status ==NCT6694_CAN_EVT_ERROR_PASSIVE && + priv->can.state !=CAN_STATE_ERROR_PASSIVE ) { +netdev_dbg (ndev,"Error, entered passive state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_ERROR_PASSIVE ); + } + +if (can_status ==NCT6694_CAN_EVT_BUS_OFF && + priv->can.state !=CAN_STATE_BUS_OFF ) { +netdev_dbg (ndev,"Error, entered bus-off state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_BUS_OFF ); + } + +return work_done; +} + +static int nct6694_can_poll (struct net_device *ndev,int quota) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx;
drivers/net/can/nct6694_canfd.c line 527
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+int work_done =0 , ret; +u8 evt_mask =NCT6694_CAN_EVENT_ERR |NCT6694_CAN_EVENT_STATUS ; +u8 bus_err, can_status; + + scoped_guard(mutex, &priv->lock) { + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, evt_mask), +sizeof (struct nct6694_can_event ), evt); +if (ret <0 ) +return IRQ_NONE ;
propagate the error
Quoted 177 lines
drivers/net/can/nct6694_canfd.c new file +177
-0+ +if (can_idx) { + bus_err = evt->err2; + can_status = evt->status2; + }else { + bus_err = evt->err1; + can_status = evt->status1; + } + } + +/* Handle bus state changes */ + work_done +=nct6694_can_handle_state_errors (ndev, can_status); + +/* Handle lec errors on the bus */ +if (priv->can.ctrlmode &CAN_CTRLMODE_BERR_REPORTING ) + work_done +=nct6694_can_handle_lec_err (ndev, bus_err); + +/* Handle RX events */ + work_done +=nct6694_can_do_rx_poll (ndev, quota - work_done); +return work_done; +} + +static void nct6694_can_tx_irq (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; + +guard (mutex)(&priv->lock); + stats->tx_bytes +=can_get_echo_skb (ndev,0 ,NULL ); + stats->tx_packets++; + priv->tx_busy = false; +netif_wake_queue (ndev); +} + +static irqreturn_t nct6694_can_irq (int irq,void *data) +{ +struct net_device *ndev = data; +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx; +int ret; +u8 mask_sts =NCT6694_CAN_EVENT_TX_EVT ; +u8 tx_evt; + + scoped_guard(mutex, &priv->lock) { + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, mask_sts), +sizeof (struct nct6694_can_event ), evt); +if (ret <0 ) +return IRQ_NONE ; + + tx_evt = can_idx ? evt->tx_evt2 : evt->tx_evt1; + } + +if (tx_evt) { +nct6694_can_tx_irq (ndev); + }else { + ret =nct6694_can_poll (ndev,NCT6694_CAN_RX_QUOTA ); +if (!ret) +return IRQ_NONE ; + } + +return IRQ_HANDLED ; +} + +static int nct6694_can_start (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_setting *setting = (struct nct6694_can_setting *)priv->tx_buf; +const struct can_bittiming *n_bt = &priv->can.bittiming; +const struct can_bittiming *d_bt = &priv->can.data_bittiming; +int ret; + +guard (mutex)(&priv->lock); + +memset (priv->tx_buf,0 ,sizeof (struct nct6694_can_setting )); + setting->nbr =cpu_to_le32 (n_bt->bitrate); + setting->dbr =cpu_to_le32 (d_bt->bitrate); + +if (priv->can.ctrlmode &CAN_CTRLMODE_LISTENONLY ) + setting->ctrl1 |=cpu_to_le16 (NCT6694_CAN_CTRL1_MON ); + +if ((priv->can.ctrlmode &CAN_CTRLMODE_FD ) && + priv->can.ctrlmode &CAN_CTRLMODE_FD_NON_ISO ) + setting->ctrl1 |=cpu_to_le16 (NCT6694_CAN_CTRL1_NISO ); + +if (priv->can.ctrlmode &CAN_CTRLMODE_LOOPBACK ) + setting->ctrl1 |=cpu_to_le16 (NCT6694_CAN_CTRL1_LBCK ); + + ret =nct6694_write_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD0_OFFSET (priv->can_idx), +sizeof (struct nct6694_can_setting ), setting); +if (ret <0 ) +return ret; + + priv->can.state =CAN_STATE_ERROR_ACTIVE ; + +return 0 ; +} + +static int nct6694_can_stop (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +netif_stop_queue (ndev); +free_irq (ndev->irq, ndev); +destroy_workqueue (priv->wq); + priv->wq =NULL ; +nct6694_can_clean (ndev); + priv->can.state =CAN_STATE_STOPPED ; +close_candev (ndev); + +return 0 ; +} + +static int nct6694_can_set_mode (struct net_device *ndev,enum can_mode mode) +{ +switch (mode) { +case CAN_MODE_START : +nct6694_can_clean (ndev); +nct6694_can_start (ndev); +netif_wake_queue (ndev); +break ; +default : +return -EOPNOTSUPP ; + } + +return 0 ; +} + +static int nct6694_can_open (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +int ret; + + ret =open_candev (ndev); +if (ret) +return ret; + + ret =request_threaded_irq (ndev->irq,NULL , + nct6694_can_irq,IRQF_ONESHOT , +"nct6694_can" , ndev); +if (ret) { +netdev_err (ndev,"Failed to request IRQ\n" ); + goto close_candev; + } + + priv->wq =alloc_ordered_workqueue ("%s-nct6694_wq" , +WQ_FREEZABLE |WQ_MEM_RECLAIM , + ndev->name); +if (!priv->wq) { + ret = -ENOMEM ; + goto free_irq; + } + + priv->tx_skb =NULL ; + priv->tx_busy = false; + + ret =nct6694_can_start (ndev); +if (ret) + goto destroy_wq; + +netif_start_queue (ndev); + +return 0 ; + +destroy_wq: +destroy_workqueue (priv->wq); +free_irq: +free_irq (ndev->irq, ndev); +close_candev: +close_candev (ndev); +return ret; +} + +static netdev_tx_t nct6694_can_start_xmit (struct sk_buff *skb, +struct net_device *ndev)
drivers/net/can/nct6694_canfd.c line 714
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +if (priv->tx_skb || priv->tx_busy) { +netdev_err (ndev,"hard_xmit called while tx busy\n" ); +return NETDEV_TX_BUSY ; + } + +if (can_dev_dropped_skb (ndev, skb)) +return NETDEV_TX_OK ;
please drop first
Quoted 18 lines
drivers/net/can/nct6694_canfd.c new file +18
-0+ +netif_stop_queue (ndev); + priv->tx_skb = skb; +queue_work (priv->wq, &priv->tx_work); + +return NETDEV_TX_OK ; +} + +static void nct6694_can_tx (struct net_device *ndev,struct canfd_frame *cf) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->tx_buf; +u32 txid =0 ; + +memset (xmit,0 ,sizeof (struct nct6694_can_xmit )); + +if (priv->can_idx ==0 ) + xmit->tag = NCT6694_CAN_TAG_CAN0<;
drivers/net/can/nct6694_canfd.c line 742
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+else + xmit->tag =NCT6694_CAN_TAG_CAN1 ; + +if (cf->can_id &CAN_EFF_FLAG ) { + txid = cf->can_id &CAN_EFF_MASK ; +/* +* In case the Extended ID frame is transmitted, the +* standard and extended part of the ID are swapped +* in the register, so swap them back to send the +* correct ID.
You comment doesn't match the code.
drivers/net/can/nct6694_canfd.c line 752
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+*/ + xmit->flag |=NCT6694_CAN_FLAG_EFF ; + }else { + txid = cf->can_id &CAN_SFF_MASK ; + } + + xmit->id =cpu_to_le32 (txid); + xmit->dlc = cf->len; + + if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
No need to check ctrlmode
drivers/net/can/nct6694_canfd.c line 760
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ can_is_canfd_skb(priv->tx_skb)) { + xmit->flag |=NCT6694_CAN_FLAG_FD ; +if (cf->flags &CANFD_BRS ) + xmit->flag |=NCT6694_CAN_FLAG_BRS ; + } + +if (cf->can_id &CAN_RTR_FLAG ) + xmit->flag |=NCT6694_CAN_FLAG_RTR ;
you can move this into the !can_is_canfd_skb branch of the if
Quoted 60 lines
drivers/net/can/nct6694_canfd.c new file +60
-0+ +memcpy (xmit->data, cf->data, cf->len); + +nct6694_write_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD10_OFFSET (1 ), +sizeof (struct nct6694_can_xmit ), + xmit); +} + +static void nct6694_can_tx_work (struct work_struct *work) +{ +struct nct6694_can_priv *priv =container_of (work, + struct nct6694_can_priv, + tx_work); +struct net_device *ndev = priv->ndev; +struct canfd_frame *cf; + +guard (mutex)(&priv->lock); + +if (priv->tx_skb) { +if (priv->can.state ==CAN_STATE_BUS_OFF ) { +nct6694_can_clean (ndev); + }else { + cf = (struct canfd_frame *)priv->tx_skb->data; +nct6694_can_tx (ndev, cf); + priv->tx_busy = true; +can_put_echo_skb (priv->tx_skb, ndev,0 ,0 ); + priv->tx_skb =NULL ; + } + } +} + +static const struct net_device_ops nct6694_can_netdev_ops = { + .ndo_open = nct6694_can_open, + .ndo_stop = nct6694_can_stop, + .ndo_start_xmit = nct6694_can_start_xmit, + .ndo_change_mtu = can_change_mtu, +}; + +static const struct ethtool_ops nct6694_can_ethtool_ops = { + .get_ts_info = ethtool_op_get_ts_info, +}; + +static int nct6694_can_get_clock (struct nct6694_can_priv *priv) +{ +struct nct6694_can_information *info = (struct nct6694_can_information *)priv->rx_buf; +int ret; + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD1_OFFSET , +sizeof (struct nct6694_can_information ), + info); +if (ret) +return ret; + +return le32_to_cpu (info->can_clk); +} + +static int nct6694_can_probe (struct platform_device *pdev) +{
drivers/net/can/nct6694_canfd.c line 830
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+const struct mfd_cell *cell =mfd_get_cell (pdev); +struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +struct nct6694_can_priv *priv; +struct net_device *ndev; +int ret, irq, can_clk; + + irq =irq_create_mapping (nct6694->domain, +NCT6694_IRQ_CAN1 + cell->id); +if (!irq) +return -EINVAL ;
propagate error value
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ + ndev =alloc_candev (sizeof (struct nct6694_can_priv ),1 ); +if (!ndev) +return -ENOMEM ; + + ndev->irq = irq;
drivers/net/can/nct6694_canfd.c line 846
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ ndev->flags |=IFF_ECHO ; + ndev->netdev_ops = &nct6694_can_netdev_ops; + ndev->ethtool_ops = &nct6694_can_ethtool_ops; + + priv =netdev_priv (ndev); + priv->nct6694 = nct6694; + priv->ndev = ndev; + + priv->tx_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
devm_kzalloc()
drivers/net/can/nct6694_canfd.c line 853
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+if (!priv->tx_buf) { + ret = -ENOMEM ; + goto free_candev; + } + + priv->rx_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
devm_kzalloc()
drivers/net/can/nct6694_canfd.c line 861
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+if (!priv->rx_buf) { + ret = -ENOMEM ; + goto free_candev; + } + + can_clk =nct6694_can_get_clock (priv); +if (can_clk <0 ) { + ret = -EIO ;
propagate the error value, don't overwrite it
move the dev_err_probe() here.
Quoted 24 lines
drivers/net/can/nct6694_canfd.c new file +24
-0+ goto free_candev; + } + +mutex_init (&priv->lock); +INIT_WORK (&priv->tx_work, nct6694_can_tx_work); + + priv->can_idx = cell->id; + priv->can.state =CAN_STATE_STOPPED ; + priv->can.clock.freq = can_clk; + priv->can.bittiming_const = &nct6694_can_bittiming_nominal_const; + priv->can.data_bittiming_const = &nct6694_can_bittiming_data_const; + priv->can.do_set_mode = nct6694_can_set_mode; + priv->can.do_get_berr_counter = nct6694_can_get_berr_counter; + + priv->can.ctrlmode =CAN_CTRLMODE_FD ; + + priv->can.ctrlmode_supported =CAN_CTRLMODE_LOOPBACK | +CAN_CTRLMODE_LISTENONLY | +CAN_CTRLMODE_FD | +CAN_CTRLMODE_FD_NON_ISO | +CAN_CTRLMODE_BERR_REPORTING ; + +platform_set_drvdata (pdev, priv); +SET_NETDEV_DEV (priv->ndev, &pdev->dev);
drivers/net/can/nct6694_canfd.c line 895
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ + ret =register_candev (priv->ndev); +if (ret) + goto free_candev; + +return 0 ; + +free_candev: +free_candev (ndev); +return dev_err_probe (&pdev->dev, ret,"Probe failed\n" );
Move the dev_err_probe() with an appropriate error message to where the error occurs. If malloc fails, the kernel already prints for you, so here it's only nct6694_can_get_clock() only.
Quoted 30 lines
drivers/net/can/nct6694_canfd.c new file +25
-0+} + +static void nct6694_can_remove (struct platform_device *pdev) +{ +struct nct6694_can_priv *priv =platform_get_drvdata (pdev); + +cancel_work_sync (&priv->tx_work); +unregister_candev (priv->ndev); +free_candev (priv->ndev); +} + +static struct platform_driver nct6694_can_driver = { + .driver = { + .name =DRVNAME , + }, + .probe = nct6694_can_probe, + .remove = nct6694_can_remove, +}; + +module_platform_driver (nct6694_can_driver); + +MODULE_DESCRIPTION ("USB-CAN FD driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-can" );Signature · 1 line
2.34.1
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung Nürnberg | Phone: +49-5121-206917-129 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Re: [PATCH v3 4/7] can: Add Nuvoton NCT6694 CAN support
Quoted 84 lines · Marc Kleine-Budde
Quoted 2 lines · Ming Yu
This driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface.
Please use the rx-offload helper, otherwise the CAN frames might be revived out of order.
Quoted 77 lines
Signed-off-by Ming Yu <[email protected]>--- MAINTAINERS | 1 + drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/nct6694_canfd.c | 920 ++++++++++++++++++++++++++++++++ 4 files changed, 932 insertions(+) create mode 100644 drivers/net/can/nct6694_canfd.cMAINTAINERS +1
-0@@ -16548,6 +16548,7 @@ S: Supported F: drivers/gpio/gpio-nct6694.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c +F: drivers/net/can/nct6694_canfd.c F: include/linux/mfd/nct6694.h NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVERdrivers/net/can/Kconfig +10
-0@@ -200,6 +200,16 @@ config CAN_SUN4I To compile this driver as a module, choose M here: the module will be called sun4i_can. +config CAN_NCT6694 + tristate "Nuvoton NCT6694 Socket CANfd support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to socket CANfd controller. + + This driver can also be built as a module. If so, the module will + be called nct6694_canfd. + config CAN_TI_HECC depends on ARM tristate "TI High End CAN Controller"drivers/net/can/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_CAN_JANZ_ICAN3) += janz-ican3.o obj-$(CONFIG_CAN_KVASER_PCIEFD) += kvaser_pciefd.o obj-$(CONFIG_CAN_MSCAN) += mscan/ obj-$(CONFIG_CAN_M_CAN) += m_can/ +obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o obj-$(CONFIG_CAN_PEAK_PCIEFD) += peak_canfd/ obj-$(CONFIG_CAN_SJA1000) += sja1000/ obj-$(CONFIG_CAN_SUN4I) += sun4i_can.odrivers/net/can/nct6694_canfd.c new file +16
-0@@ -0,0 +1,920 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Nuvoton NCT6694 Socket CANfd driver based on USB interface. +* +* Copyright (C) 2024 Nuvoton Technology Corp. +*/ + +#include <linux/can/dev.h> +#include <linux/ethtool.h> +#include <linux/irqdomain.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/netdevice.h> +#include <linux/platform_device.h>
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 24
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ +#define DRVNAME "nct6694-can" + +/* Host interface */ +#define NCT6694_CAN_MOD 0x05 + +/* Message Channel*/ +/* Command 00h */
Instead of this comment, explain what the command 00h does.
Also better to give a memorable name instead of CMD0. For example:CMD_TX, CMD_RX…
If possible, make it match the structure names.
Quoted 33 lines
Quoted 33 lines
drivers/net/can/nct6694_canfd.c new file +33
-0+#define NCT6694_CAN_CMD0_OFFSET (idx) (idx ? 0x0100 : 0x0000) +#define NCT6694_CAN_CTRL1_MON BIT(0) +#define NCT6694_CAN_CTRL1_NISO BIT(1) +#define NCT6694_CAN_CTRL1_LBCK BIT(2) + +/* Command 01h */ +#define NCT6694_CAN_CMD1_OFFSET 0x0001 + +/* Command 02h */ +#define NCT6694_CAN_CMD2_OFFSET (idx, mask) \ + ({ typeof(mask) mask_ = (mask); \ + idx ? ((0x80 | (mask_ & 0xFF)) << 8 | 0x02) : \ + ((0x00 | (mask_ & 0xFF)) << 8 | 0x02); }) + +#define NCT6694_CAN_EVENT_ERR BIT(0) +#define NCT6694_CAN_EVENT_STATUS BIT(1) +#define NCT6694_CAN_EVENT_TX_EVT BIT(2) +#define NCT6694_CAN_EVENT_RX_EVT BIT(3) +#define NCT6694_CAN_EVENT_REC BIT(4) +#define NCT6694_CAN_EVENT_TEC BIT(5) +#define NCT6694_CAN_EVT_TX_FIFO_EMPTY BIT(7)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_LOST BIT(5)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_HALF_FULL BIT(6)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_IN BIT(7) + +/* Command 10h */ +#define NCT6694_CAN_CMD10_OFFSET (buf_cnt) \ + (((buf_cnt) & 0xFF) << 8 | 0x10) +#define NCT6694_CAN_TAG_CAN0 0xC0 +#define NCT6694_CAN_TAG_CAN1 0xC1 +#define NCT6694_CAN_FLAG_EFF BIT(0) +#define NCT6694_CAN_FLAG_RTR BIT(1) +#define NCT6694_CAN_FLAG_FD BIT(2)
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 65
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+#define NCT6694_CAN_FLAG_BRS BIT(3) +#define NCT6694_CAN_FLAG_ERR BIT(4) + +/* Command 11h */ +#define NCT6694_CAN_CMD11_OFFSET (idx, buf_cnt) \ + ({ typeof(buf_cnt) buf_cnt_ = (buf_cnt); \ + idx ? ((0x80 | (buf_cnt_ & 0xFF)) << 8 | 0x11) : \ + ((0x00 | (buf_cnt_ & 0xFF)) << 8 | 0x11); })
Simplify this. Do something like:
#define NCT6694_CAN_CMD11_OFFSET(idx, buf_cnt) \
(idx ? 0x80 : 0x00) | \
(buf_cnt & 0xFF)) << 8 | 0x11) \(apply this also to NCT6694_CAN_CMD2_OFFSET())
Quoted 70 lines
drivers/net/can/nct6694_canfd.c line 70
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+#define NCT6694_CAN_RX_QUOTA 64 + +enum nct6694_event_err { +NCT6694_CAN_EVT_NO_ERROR ,^^^ add _ERR_drivers/net/can/nct6694_canfd.c line 81
Quoted 11 lines
drivers/net/can/nct6694_canfd.c new file +11
-0+NCT6694_CAN_EVT_CRC_ERROR , +NCT6694_CAN_EVT_STUFF_ERROR , +NCT6694_CAN_EVT_ACK_ERROR , +NCT6694_CAN_EVT_FORM_ERROR , +NCT6694_CAN_EVT_BIT_ERROR , +NCT6694_CAN_EVT_TIMEOUT_ERROR , + NCT6694_CAN_EVT_UNKNOWN_ERROR, +}; + +enum nct6694_event_status { + NCT6694_CAN_EVT_ERROR_ACTIVE,^^^ add _STATUS_Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+NCT6694_CAN_EVT_ERROR_PASSIVE , +NCT6694_CAN_EVT_BUS_OFF , + NCT6694_CAN_EVT_WARNING, +}; + +struct __packed nct6694_can_setting { +__le32 nbr; +__le32 dbr; +u8 active; +u8 reserved[3 ];drivers/net/can/nct6694_canfd.c line 101
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+__le16 ctrl1; +__le16 ctrl2; +__le32 nbtp; +__le32 dbtp; +}; + +struct __packed nct6694_can_information { +u8 tx_fifo_cnt; +u8 rx_fifo_cnt; +__le16 reserved;u8 reserved[2];
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+__le32 can_clk; +}; + +struct __packed nct6694_can_event { +u8 err1; +u8 status1; +u8 tx_evt1; +u8 rx_evt1; +u8 rec1;drivers/net/can/nct6694_canfd.c line 120
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+u8 tec1; +u8 reserved1[2 ]; +u8 err2; +u8 status2; +u8 tx_evt2; +u8 rx_evt2; +u8 rec2; +u8 tec2; +u8 reserved2[2 ]; +};Create an extra struct that only describes a channel
struct __packed nct6694_can_event_channel { u8 err; u8 status; u8 tx_evt; u8 rx_evt; u8 rec; u8 tec; u8 reserved[2]; }
Quoted 4 lines
and put an array of 2 into struct
__packednct6694_can_event.drivers/net/can/nct6694_canfd.c line 121
Quoted 2 lines
drivers/net/can/nct6694_canfd.c new file +2
-0+ +struct __packed nct6694_can_xmit {
Is this struct for both TX and RX? If so, name it something like
struct nct6694_can_frame
The term xmit is only used for transmission, not for reception.
Quoted 10 lines
drivers/net/can/nct6694_canfd.c line 129
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+u8 tag; +u8 flag; +u8 reserved; +u8 dlc; +__le32 id; +u8 data[64 ]; +u8 msg_buf[72 ];Why is the message so long? What's in the
msg_buf?
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 136
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+}; + +struct nct6694_can_priv { +struct can_priv can;/* must be the first member */ +struct net_device *ndev; +struct nct6694 *nct6694; +struct mutex lock;What does lock protect?
+1
mutexes are good if you want to keep the lock for a long time.
For short period, spinlock are more performant:
spinlock_t lock;
Quoted 7 lines
drivers/net/can/nct6694_canfd.c line 140
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+struct sk_buff *tx_skb; +struct workqueue_struct *wq; +struct work_struct tx_work; +unsigned char *tx_buf;void *
drivers/net/can/nct6694_canfd.c line 141
Quoted 1 line
drivers/net/can/nct6694_canfd.c new file +1
-0+unsigned char *rx_buf;void *
Rather than void*, tx_buf and rx_buf can be replaced by an union:
union nct6694_can_rx {
struct nct6694_can_event event;
struct nct6694_can_xmit xmit;
struct nct6694_can_information info;
};(same for nct6694_can_tx)
Then in struct nct6694_can_priv, you will just have:
union nct6694_can_tx tx;
union nct6694_can_rx rx;
With this,
NCT6694_MAX_PACKET_SZ
can most likely be replaced by sizeof(union nct6694_can_rx) or sizeof(union nct6694_can_tx).
Quoted 118 lines
drivers/net/can/nct6694_canfd.c line 143
Quoted 2 lines
drivers/net/can/nct6694_canfd.c new file +2
-0+unsigned char can_idx; +bool tx_busy;IMHO it makes no sense to have
tx_skbandtx_busyQuoted 86 lines
drivers/net/can/nct6694_canfd.c new file +86
-0+}; + +static const struct can_bittiming_const nct6694_can_bittiming_nominal_const = { + .name =DRVNAME , + .tseg1_min =2 , + .tseg1_max =256 , + .tseg2_min =2 , + .tseg2_max =128 , + .sjw_max =128 , + .brp_min =1 , + .brp_max =511 , + .brp_inc =1 , +}; + +static const struct can_bittiming_const nct6694_can_bittiming_data_const = { + .name =DRVNAME , + .tseg1_min =1 , + .tseg1_max =32 , + .tseg2_min =1 , + .tseg2_max =16 , + .sjw_max =16 , + .brp_min =1 , + .brp_max =31 , + .brp_inc =1 , +}; + +static void nct6694_can_clean (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +if (priv->tx_skb || priv->tx_busy) + ndev->stats.tx_errors++; +dev_kfree_skb (priv->tx_skb); +if (priv->tx_busy) +can_free_echo_skb (priv->ndev,0 ,NULL ); + priv->tx_skb =NULL ; + priv->tx_busy = false; +} + +static int nct6694_can_get_berr_counter (const struct net_device *ndev, +struct can_berr_counter *bec) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +u8 mask =NCT6694_CAN_EVENT_REC |NCT6694_CAN_EVENT_TEC ; +int ret; + +guard (mutex)(&priv->lock); + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (priv->can_idx, mask), +sizeof (struct nct6694_can_event ), + evt); +if (ret <0 ) +return ret; + + bec->rxerr = priv->can_idx ? evt->rec2 : evt->rec1; + bec->txerr = priv->can_idx ? evt->tec2 : evt->tec1; + +return 0 ; +} + +static int nct6694_can_handle_lost_msg (struct net_device *ndev) +{ +struct net_device_stats *stats = &ndev->stats; +struct sk_buff *skb; +struct can_frame *frame; + +netdev_err (ndev,"RX FIFO overflow, message(s) lost.\n" ); + + stats->rx_errors++; + stats->rx_over_errors++; + + skb =alloc_can_err_skb (ndev, &frame); +if (unlikely (!skb)) +return 0 ; + + frame->can_id |=CAN_ERR_CRTL ; + frame->data[1 ] =CAN_ERR_CRTL_RX_OVERFLOW ; + +netif_receive_skb (skb); + +return 1 ; +} + +static void nct6694_can_read_fifo (struct net_device *ndev)drivers/net/can/nct6694_canfd.c line 239
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->rx_buf; +struct net_device_stats *stats = &ndev->stats; +struct canfd_frame *cf; +struct sk_buff *skb; +int can_idx = priv->can_idx; +u32 id; +int ret; +u8 fd_format =0 ;bool - no need to init
Quoted 16 lines
drivers/net/can/nct6694_canfd.c new file +16
-0+ +guard (mutex)(&priv->lock); + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD11_OFFSET (can_idx,1 ), +sizeof (struct nct6694_can_xmit ), xmit); +if (ret <0 ) +return ; + +/* Check type of frame and create skb */ + fd_format = xmit->flag &NCT6694_CAN_FLAG_FD ; +if (fd_format) + skb =alloc_canfd_skb (ndev, &cf); +else + skb =alloc_can_skb (ndev, (struct can_frame **)&cf); +
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 261
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+if (!skb) { + stats->rx_dropped++; +return ; + } + + cf->len = xmit->dlc;what does
xmit->dlccontain? The DLC or the length?
+1
Also, do not trust the device data. Even if SPI attacks are less common, make sure to sanitize this length.
cf->len = canfd_sanitize_len(xmit->dlc);
Or
cf->len = canfd_sanitize_len(xmit->dlc);
if xmit->dlc is in fact a DLC.
Quoted 86 lines
drivers/net/can/nct6694_canfd.c line 270
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+ +/* Get ID and set flag by its type(Standard ID format or Ext ID format) */ + id =le32_to_cpu (xmit->id); +if (xmit->flag &NCT6694_CAN_FLAG_EFF ) { +/* +* In case the Extended ID frame is received, the standard +* and extended part of the ID are swapped in the register, +* so swap them back to obtain the correct ID. +*/ You comment doesn't match the code.
Quoted 74 lines
drivers/net/can/nct6694_canfd.c new file +74
-0+ id |=CAN_EFF_FLAG ; + } + + cf->can_id = id; + +/* Set ESI flag */ +if (xmit->flag &NCT6694_CAN_FLAG_ERR ) { + cf->flags |=CANFD_ESI ; +netdev_dbg (ndev,"ESI Error\n" ); + } + +/* Set RTR and BRS */ +if (!fd_format && (xmit->flag &NCT6694_CAN_FLAG_RTR )) { + cf->can_id |=CAN_RTR_FLAG ; + }else { +if (xmit->flag &NCT6694_CAN_FLAG_BRS ) + cf->flags |=CANFD_BRS ; + +memcpy (cf->data, xmit->data, cf->len); + + stats->rx_bytes += cf->len; + } + + stats->rx_packets++; + +netif_receive_skb (skb); +} + +static int nct6694_can_do_rx_poll (struct net_device *ndev,int quota) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx; +u32 pkts =0 ; +u8 mask_rx =NCT6694_CAN_EVENT_RX_EVT ; +u8 rx_evt; + +for (;;) { + scoped_guard(mutex, &priv->lock) { +nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, mask_rx), +sizeof (struct nct6694_can_event ), evt); + + rx_evt = can_idx ? evt->rx_evt2 : evt->rx_evt1; + } + +if (rx_evt &NCT6694_CAN_EVT_RX_DATA_LOST ) +nct6694_can_handle_lost_msg (ndev); + +/* No data */ +if ((rx_evt &NCT6694_CAN_EVT_RX_DATA_IN ) ==0 ) +break ; + +if (quota <=0 ) +break ; + +nct6694_can_read_fifo (ndev); + quota--; + pkts++; + } + +return pkts; +} + +static int nct6694_can_handle_lec_err (struct net_device *ndev,u8 bus_err) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; +struct can_frame *cf; +struct sk_buff *skb; + +if (bus_err ==NCT6694_CAN_EVT_NO_ERROR ) +return 0 ; +
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 352
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ priv->can.can_stats.bus_error++; + stats->rx_errors++; + +/* Propagate the error condition to the CAN stack. */ + skb =alloc_can_err_skb (ndev, &cf); + +if (unlikely (!skb)) +return 0 ;
Do not exit if the memory allocation fails. Instead, do the error handling to increase the statistics.
Look at what other CAN drivers are doing.
Quoted 38 lines
Quoted 38 lines
drivers/net/can/nct6694_canfd.c new file +38
-0+/* Read the error counter register and check for new errors. */ + cf->can_id |=CAN_ERR_PROT |CAN_ERR_BUSERROR ; + +switch (bus_err) { +case NCT6694_CAN_EVT_CRC_ERROR : + cf->data[3 ] =CAN_ERR_PROT_LOC_CRC_SEQ ; +break ; + +case NCT6694_CAN_EVT_STUFF_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_STUFF ; +break ; + +case NCT6694_CAN_EVT_ACK_ERROR : + cf->data[3 ] =CAN_ERR_PROT_LOC_ACK ; +break ; + +case NCT6694_CAN_EVT_FORM_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_FORM ; +break ; + +case NCT6694_CAN_EVT_BIT_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_BIT | +CAN_ERR_PROT_BIT0 | +CAN_ERR_PROT_BIT1 ; +break ; + +case NCT6694_CAN_EVT_TIMEOUT_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_UNSPEC ; +break ; + +case NCT6694_CAN_EVT_UNKNOWN_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_UNSPEC ; +/* +* It means 'unspecified'(the value is '0'). +* But it is not sure if it's ok to send an error package +* without specific error bit. +*/ +break ;
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 399
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ +default : +break ; + } + +/* Reset the error counter, ack the IRQ and re-enable the counter. */ + stats->rx_packets++; + stats->rx_bytes += cf->can_dlc;
The CAN error frames are not regular packets. Do not increase the RX stats. Instead, increase the error stats.
Quoted 32 lines
Quoted 32 lines
drivers/net/can/nct6694_canfd.c new file +32
-0+netif_receive_skb (skb); + +return 1 ; +} + +static int nct6694_can_handle_state_change (struct net_device *ndev, +enum can_state new_state) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; +struct can_frame *cf; +struct sk_buff *skb; +struct can_berr_counter bec; + +switch (new_state) { +case CAN_STATE_ERROR_ACTIVE : + priv->can.can_stats.error_warning++; + priv->can.state =CAN_STATE_ERROR_ACTIVE ; +break ; +case CAN_STATE_ERROR_WARNING : + priv->can.can_stats.error_warning++; + priv->can.state =CAN_STATE_ERROR_WARNING ; +break ; +case CAN_STATE_ERROR_PASSIVE : + priv->can.can_stats.error_passive++; + priv->can.state =CAN_STATE_ERROR_PASSIVE ; +break ; +case CAN_STATE_BUS_OFF : + priv->can.state =CAN_STATE_BUS_OFF ; + priv->can.can_stats.bus_off++; +can_bus_off (ndev); +break ;
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 439
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+default : +break ; + } + +/* propagate the error condition to the CAN stack */ + skb =alloc_can_err_skb (ndev, &cf); +if (unlikely (!skb)) +return 0 ;
Same as above: handle the statistics even if the allocation fails.
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 448
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+nct6694_can_get_berr_counter (ndev, &bec); + +switch (new_state) { +case CAN_STATE_ERROR_WARNING : +/* error warning state */ + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] = (bec.txerr > bec.rxerr) ?CAN_ERR_CRTL_TX_WARNING : +CAN_ERR_CRTL_RX_WARNING ;
Prefer an if/else here instead of the ternary operator. It is more readable.
Quoted 14 lines
Quoted 14 lines
drivers/net/can/nct6694_canfd.c new file +14
-0+ cf->data[6 ] = bec.txerr; + cf->data[7 ] = bec.rxerr; +break ; +case CAN_STATE_ERROR_PASSIVE : +/* error passive state */ + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] |=CAN_ERR_CRTL_RX_PASSIVE ; +if (bec.txerr >127 ) + cf->data[1 ] |=CAN_ERR_CRTL_TX_PASSIVE ; + cf->data[6 ] = bec.txerr; + cf->data[7 ] = bec.rxerr; +break ; +case CAN_STATE_BUS_OFF : +/* bus-off state */
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 470
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ cf->can_id |=CAN_ERR_BUSOFF ; +break ; +default : +break ; + } + + stats->rx_packets++; + stats->rx_bytes += cf->can_dlc;
Do not increase the RX stats when you receive a CAN event.
Quoted 128 lines
Quoted 47 lines
drivers/net/can/nct6694_canfd.c new file +47
-0+netif_receive_skb (skb); + +return 1 ; +} + +static int nct6694_can_handle_state_errors (struct net_device *ndev, +unsigned char can_status) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +int work_done =0 ; + +if (can_status ==NCT6694_CAN_EVT_ERROR_ACTIVE && + priv->can.state !=CAN_STATE_ERROR_ACTIVE ) { +netdev_dbg (ndev,"Error, entered active state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_ERROR_ACTIVE ); + } + +if (can_status ==NCT6694_CAN_EVT_WARNING && + priv->can.state !=CAN_STATE_ERROR_WARNING ) { +netdev_dbg (ndev,"Error, entered warning state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_ERROR_WARNING ); + } + +if (can_status ==NCT6694_CAN_EVT_ERROR_PASSIVE && + priv->can.state !=CAN_STATE_ERROR_PASSIVE ) { +netdev_dbg (ndev,"Error, entered passive state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_ERROR_PASSIVE ); + } + +if (can_status ==NCT6694_CAN_EVT_BUS_OFF && + priv->can.state !=CAN_STATE_BUS_OFF ) { +netdev_dbg (ndev,"Error, entered bus-off state\n" ); + work_done +=nct6694_can_handle_state_change (ndev, +CAN_STATE_BUS_OFF ); + } + +return work_done; +} + +static int nct6694_can_poll (struct net_device *ndev,int quota) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx;drivers/net/can/nct6694_canfd.c line 527
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+int work_done =0 , ret; +u8 evt_mask =NCT6694_CAN_EVENT_ERR |NCT6694_CAN_EVENT_STATUS ; +u8 bus_err, can_status; + + scoped_guard(mutex, &priv->lock) { + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, evt_mask), +sizeof (struct nct6694_can_event ), evt); +if (ret <0 ) +return IRQ_NONE ;propagate the error
Quoted 68 lines
drivers/net/can/nct6694_canfd.c new file +68
-0+ +if (can_idx) { + bus_err = evt->err2; + can_status = evt->status2; + }else { + bus_err = evt->err1; + can_status = evt->status1; + } + } + +/* Handle bus state changes */ + work_done +=nct6694_can_handle_state_errors (ndev, can_status); + +/* Handle lec errors on the bus */ +if (priv->can.ctrlmode &CAN_CTRLMODE_BERR_REPORTING ) + work_done +=nct6694_can_handle_lec_err (ndev, bus_err); + +/* Handle RX events */ + work_done +=nct6694_can_do_rx_poll (ndev, quota - work_done); +return work_done; +} + +static void nct6694_can_tx_irq (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; + +guard (mutex)(&priv->lock); + stats->tx_bytes +=can_get_echo_skb (ndev,0 ,NULL ); + stats->tx_packets++; + priv->tx_busy = false; +netif_wake_queue (ndev); +} + +static irqreturn_t nct6694_can_irq (int irq,void *data) +{ +struct net_device *ndev = data; +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx; +int ret; +u8 mask_sts =NCT6694_CAN_EVENT_TX_EVT ; +u8 tx_evt; + + scoped_guard(mutex, &priv->lock) { + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, mask_sts), +sizeof (struct nct6694_can_event ), evt); +if (ret <0 ) +return IRQ_NONE ; + + tx_evt = can_idx ? evt->tx_evt2 : evt->tx_evt1; + } + +if (tx_evt) { +nct6694_can_tx_irq (ndev); + }else { + ret =nct6694_can_poll (ndev,NCT6694_CAN_RX_QUOTA ); +if (!ret) +return IRQ_NONE ; + } + +return IRQ_HANDLED ; +} + +static int nct6694_can_start (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev);
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 603
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+struct nct6694_can_setting *setting = (struct nct6694_can_setting *)priv->tx_buf; +const struct can_bittiming *n_bt = &priv->can.bittiming; +const struct can_bittiming *d_bt = &priv->can.data_bittiming; +int ret; + +guard (mutex)(&priv->lock); + +memset (priv->tx_buf,0 ,sizeof (struct nct6694_can_setting ));
When you memset(), use this pattern:
memset(setting, 0, sizeof(*setting));
Apply this throughout your driver.
Quoted 338 lines
Quoted 101 lines
drivers/net/can/nct6694_canfd.c new file +101
-0+ setting->nbr =cpu_to_le32 (n_bt->bitrate); + setting->dbr =cpu_to_le32 (d_bt->bitrate); + +if (priv->can.ctrlmode &CAN_CTRLMODE_LISTENONLY ) + setting->ctrl1 |=cpu_to_le16 (NCT6694_CAN_CTRL1_MON ); + +if ((priv->can.ctrlmode &CAN_CTRLMODE_FD ) && + priv->can.ctrlmode &CAN_CTRLMODE_FD_NON_ISO ) + setting->ctrl1 |=cpu_to_le16 (NCT6694_CAN_CTRL1_NISO ); + +if (priv->can.ctrlmode &CAN_CTRLMODE_LOOPBACK ) + setting->ctrl1 |=cpu_to_le16 (NCT6694_CAN_CTRL1_LBCK ); + + ret =nct6694_write_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD0_OFFSET (priv->can_idx), +sizeof (struct nct6694_can_setting ), setting); +if (ret <0 ) +return ret; + + priv->can.state =CAN_STATE_ERROR_ACTIVE ; + +return 0 ; +} + +static int nct6694_can_stop (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +netif_stop_queue (ndev); +free_irq (ndev->irq, ndev); +destroy_workqueue (priv->wq); + priv->wq =NULL ; +nct6694_can_clean (ndev); + priv->can.state =CAN_STATE_STOPPED ; +close_candev (ndev); + +return 0 ; +} + +static int nct6694_can_set_mode (struct net_device *ndev,enum can_mode mode) +{ +switch (mode) { +case CAN_MODE_START : +nct6694_can_clean (ndev); +nct6694_can_start (ndev); +netif_wake_queue (ndev); +break ; +default : +return -EOPNOTSUPP ; + } + +return 0 ; +} + +static int nct6694_can_open (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +int ret; + + ret =open_candev (ndev); +if (ret) +return ret; + + ret =request_threaded_irq (ndev->irq,NULL , + nct6694_can_irq,IRQF_ONESHOT , +"nct6694_can" , ndev); +if (ret) { +netdev_err (ndev,"Failed to request IRQ\n" ); + goto close_candev; + } + + priv->wq =alloc_ordered_workqueue ("%s-nct6694_wq" , +WQ_FREEZABLE |WQ_MEM_RECLAIM , + ndev->name); +if (!priv->wq) { + ret = -ENOMEM ; + goto free_irq; + } + + priv->tx_skb =NULL ; + priv->tx_busy = false; + + ret =nct6694_can_start (ndev); +if (ret) + goto destroy_wq; + +netif_start_queue (ndev); + +return 0 ; + +destroy_wq: +destroy_workqueue (priv->wq); +free_irq: +free_irq (ndev->irq, ndev); +close_candev: +close_candev (ndev); +return ret; +} + +static netdev_tx_t nct6694_can_start_xmit (struct sk_buff *skb, +struct net_device *ndev)drivers/net/can/nct6694_canfd.c line 714
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +if (priv->tx_skb || priv->tx_busy) { +netdev_err (ndev,"hard_xmit called while tx busy\n" ); +return NETDEV_TX_BUSY ; + } + +if (can_dev_dropped_skb (ndev, skb)) +return NETDEV_TX_OK ;please drop first
Quoted 18 lines
drivers/net/can/nct6694_canfd.c new file +18
-0+ +netif_stop_queue (ndev); + priv->tx_skb = skb; +queue_work (priv->wq, &priv->tx_work); + +return NETDEV_TX_OK ; +} + +static void nct6694_can_tx (struct net_device *ndev,struct canfd_frame *cf) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->tx_buf; +u32 txid =0 ; + +memset (xmit,0 ,sizeof (struct nct6694_can_xmit )); + +if (priv->can_idx ==0 ) + xmit->tag = NCT6694_CAN_TAG_CAN0<;drivers/net/can/nct6694_canfd.c line 742
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+else + xmit->tag =NCT6694_CAN_TAG_CAN1 ; + +if (cf->can_id &CAN_EFF_FLAG ) { + txid = cf->can_id &CAN_EFF_MASK ; +/* +* In case the Extended ID frame is transmitted, the +* standard and extended part of the ID are swapped +* in the register, so swap them back to send the +* correct ID. You comment doesn't match the code.
drivers/net/can/nct6694_canfd.c line 752
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+*/ + xmit->flag |=NCT6694_CAN_FLAG_EFF ; + }else { + txid = cf->can_id &CAN_SFF_MASK ; + } + + xmit->id =cpu_to_le32 (txid); + xmit->dlc = cf->len; + + if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&No need to check ctrlmode
drivers/net/can/nct6694_canfd.c line 760
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ can_is_canfd_skb(priv->tx_skb)) { + xmit->flag |=NCT6694_CAN_FLAG_FD ; +if (cf->flags &CANFD_BRS ) + xmit->flag |=NCT6694_CAN_FLAG_BRS ; + } + +if (cf->can_id &CAN_RTR_FLAG ) + xmit->flag |=NCT6694_CAN_FLAG_RTR ;you can move this into the !can_is_canfd_skb branch of the if
Quoted 60 lines
drivers/net/can/nct6694_canfd.c new file +60
-0+ +memcpy (xmit->data, cf->data, cf->len); + +nct6694_write_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD10_OFFSET (1 ), +sizeof (struct nct6694_can_xmit ), + xmit); +} + +static void nct6694_can_tx_work (struct work_struct *work) +{ +struct nct6694_can_priv *priv =container_of (work, + struct nct6694_can_priv, + tx_work); +struct net_device *ndev = priv->ndev; +struct canfd_frame *cf; + +guard (mutex)(&priv->lock); + +if (priv->tx_skb) { +if (priv->can.state ==CAN_STATE_BUS_OFF ) { +nct6694_can_clean (ndev); + }else { + cf = (struct canfd_frame *)priv->tx_skb->data; +nct6694_can_tx (ndev, cf); + priv->tx_busy = true; +can_put_echo_skb (priv->tx_skb, ndev,0 ,0 ); + priv->tx_skb =NULL ; + } + } +} + +static const struct net_device_ops nct6694_can_netdev_ops = { + .ndo_open = nct6694_can_open, + .ndo_stop = nct6694_can_stop, + .ndo_start_xmit = nct6694_can_start_xmit, + .ndo_change_mtu = can_change_mtu, +}; + +static const struct ethtool_ops nct6694_can_ethtool_ops = { + .get_ts_info = ethtool_op_get_ts_info, +}; + +static int nct6694_can_get_clock (struct nct6694_can_priv *priv) +{ +struct nct6694_can_information *info = (struct nct6694_can_information *)priv->rx_buf; +int ret; + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD1_OFFSET , +sizeof (struct nct6694_can_information ), + info); +if (ret) +return ret; + +return le32_to_cpu (info->can_clk); +} + +static int nct6694_can_probe (struct platform_device *pdev) +{drivers/net/can/nct6694_canfd.c line 830
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+const struct mfd_cell *cell =mfd_get_cell (pdev); +struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +struct nct6694_can_priv *priv; +struct net_device *ndev; +int ret, irq, can_clk; + + irq =irq_create_mapping (nct6694->domain, +NCT6694_IRQ_CAN1 + cell->id); +if (!irq) +return -EINVAL ;propagate error value
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ + ndev =alloc_candev (sizeof (struct nct6694_can_priv ),1 ); +if (!ndev) +return -ENOMEM ; + + ndev->irq = irq;drivers/net/can/nct6694_canfd.c line 846
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ ndev->flags |=IFF_ECHO ; + ndev->netdev_ops = &nct6694_can_netdev_ops; + ndev->ethtool_ops = &nct6694_can_ethtool_ops; + + priv =netdev_priv (ndev); + priv->nct6694 = nct6694; + priv->ndev = ndev; + + priv->tx_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
devm_kzalloc()drivers/net/can/nct6694_canfd.c line 853
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+if (!priv->tx_buf) { + ret = -ENOMEM ; + goto free_candev; + } + + priv->rx_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
devm_kzalloc()drivers/net/can/nct6694_canfd.c line 861
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+if (!priv->rx_buf) { + ret = -ENOMEM ; + goto free_candev; + } + + can_clk =nct6694_can_get_clock (priv); +if (can_clk <0 ) { + ret = -EIO ;propagate the error value, don't overwrite it
move the
dev_err_probe()here.Quoted 24 lines
drivers/net/can/nct6694_canfd.c new file +24
-0+ goto free_candev; + } + +mutex_init (&priv->lock); +INIT_WORK (&priv->tx_work, nct6694_can_tx_work); + + priv->can_idx = cell->id; + priv->can.state =CAN_STATE_STOPPED ; + priv->can.clock.freq = can_clk; + priv->can.bittiming_const = &nct6694_can_bittiming_nominal_const; + priv->can.data_bittiming_const = &nct6694_can_bittiming_data_const; + priv->can.do_set_mode = nct6694_can_set_mode; + priv->can.do_get_berr_counter = nct6694_can_get_berr_counter; + + priv->can.ctrlmode =CAN_CTRLMODE_FD ; + + priv->can.ctrlmode_supported =CAN_CTRLMODE_LOOPBACK | +CAN_CTRLMODE_LISTENONLY | +CAN_CTRLMODE_FD | +CAN_CTRLMODE_FD_NON_ISO | +CAN_CTRLMODE_BERR_REPORTING ; + +platform_set_drvdata (pdev, priv); +SET_NETDEV_DEV (priv->ndev, &pdev->dev);drivers/net/can/nct6694_canfd.c line 895
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ + ret =register_candev (priv->ndev); +if (ret) + goto free_candev; + +return 0 ; + +free_candev: +free_candev (ndev); +return dev_err_probe (&pdev->dev, ret,"Probe failed\n" );Move the
dev_err_probe()with an appropriate error message to where the error occurs. If malloc fails, the kernel already prints for you, so here it's onlynct6694_can_get_clock()only.Quoted 17 lines
drivers/net/can/nct6694_canfd.c new file +17
-0+} + +static void nct6694_can_remove (struct platform_device *pdev) +{ +struct nct6694_can_priv *priv =platform_get_drvdata (pdev); + +cancel_work_sync (&priv->tx_work); +unregister_candev (priv->ndev); +free_candev (priv->ndev); +} + +static struct platform_driver nct6694_can_driver = { + .driver = { + .name =DRVNAME , + }, + .probe = nct6694_can_probe, + .remove = nct6694_can_remove,
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 920
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+}; + +module_platform_driver (nct6694_can_driver); + +MODULE_DESCRIPTION ("USB-CAN FD driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-can" );
Yours sincerely,
Vincent Mailhol
Re: [PATCH v3 4/7] can: Add Nuvoton NCT6694 CAN support
Dear Vincent,
Thank you for your comments,
Vincent Mailhol <[email protected]> 於 2024年12月11日 週三 下午11:25寫道:
Quoted 1 line
...
Quoted 8 lines
Quoted 5 lines
drivers/net/can/nct6694_canfd.c line 24
Quoted 5 lines
drivers/net/can/nct6694_canfd.c new file +5
-0+/* Host interface */ +#define NCT6694_CAN_MOD 0x05 + +/* Message Channel*/ +/* Command 00h */ Instead of this comment, explain what the command 00h does.
Quoted 5 lines
Also better to give a memorable name instead of CMD0. For example:
CMD_TX, CMD_RX…If possible, make it match the structure names.
Okay, I'll make the modifications in the next patch.
Quoted 44 lines
Quoted 33 lines
Quoted 33 lines
drivers/net/can/nct6694_canfd.c new file +33
-0+#define NCT6694_CAN_CMD0_OFFSET (idx) (idx ? 0x0100 : 0x0000) +#define NCT6694_CAN_CTRL1_MON BIT(0) +#define NCT6694_CAN_CTRL1_NISO BIT(1) +#define NCT6694_CAN_CTRL1_LBCK BIT(2) + +/* Command 01h */ +#define NCT6694_CAN_CMD1_OFFSET 0x0001 + +/* Command 02h */ +#define NCT6694_CAN_CMD2_OFFSET (idx, mask) \ + ({ typeof(mask) mask_ = (mask); \ + idx ? ((0x80 | (mask_ & 0xFF)) << 8 | 0x02) : \ + ((0x00 | (mask_ & 0xFF)) << 8 | 0x02); }) + +#define NCT6694_CAN_EVENT_ERR BIT(0) +#define NCT6694_CAN_EVENT_STATUS BIT(1) +#define NCT6694_CAN_EVENT_TX_EVT BIT(2) +#define NCT6694_CAN_EVENT_RX_EVT BIT(3) +#define NCT6694_CAN_EVENT_REC BIT(4) +#define NCT6694_CAN_EVENT_TEC BIT(5) +#define NCT6694_CAN_EVT_TX_FIFO_EMPTY BIT(7)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_LOST BIT(5)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_HALF_FULL BIT(6)/* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_IN BIT(7) + +/* Command 10h */ +#define NCT6694_CAN_CMD10_OFFSET (buf_cnt) \ + (((buf_cnt) & 0xFF) << 8 | 0x10) +#define NCT6694_CAN_TAG_CAN0 0xC0 +#define NCT6694_CAN_TAG_CAN1 0xC1 +#define NCT6694_CAN_FLAG_EFF BIT(0) +#define NCT6694_CAN_FLAG_RTR BIT(1) +#define NCT6694_CAN_FLAG_FD BIT(2)Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 65
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+#define NCT6694_CAN_FLAG_BRS BIT(3) +#define NCT6694_CAN_FLAG_ERR BIT(4) + +/* Command 11h */ +#define NCT6694_CAN_CMD11_OFFSET (idx, buf_cnt) \ + ({ typeof(buf_cnt) buf_cnt_ = (buf_cnt); \ + idx ? ((0x80 | (buf_cnt_ & 0xFF)) << 8 | 0x11) : \ + ((0x00 | (buf_cnt_ & 0xFF)) << 8 | 0x11); })Simplify this. Do something like:
Quoted 6 lines
#define NCT6694_CAN_CMD11_OFFSET(idx, buf_cnt) \ (idx ? 0x80 : 0x00) | \ (buf_cnt & 0xFF)) << 8 | 0x11) \(apply this also to
NCT6694_CAN_CMD2_OFFSET())
Understood! I will fix these in v4.
Quoted 72 lines
Quoted 72 lines
drivers/net/can/nct6694_canfd.c line 70
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+#define NCT6694_CAN_RX_QUOTA 64 + +enum nct6694_event_err { +NCT6694_CAN_EVT_NO_ERROR ,^^^ add _ERR_drivers/net/can/nct6694_canfd.c line 81
Quoted 11 lines
drivers/net/can/nct6694_canfd.c new file +11
-0+NCT6694_CAN_EVT_CRC_ERROR , +NCT6694_CAN_EVT_STUFF_ERROR , +NCT6694_CAN_EVT_ACK_ERROR , +NCT6694_CAN_EVT_FORM_ERROR , +NCT6694_CAN_EVT_BIT_ERROR , +NCT6694_CAN_EVT_TIMEOUT_ERROR , + NCT6694_CAN_EVT_UNKNOWN_ERROR, +}; + +enum nct6694_event_status { + NCT6694_CAN_EVT_ERROR_ACTIVE,^^^ add _STATUS_Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+NCT6694_CAN_EVT_ERROR_PASSIVE , +NCT6694_CAN_EVT_BUS_OFF , + NCT6694_CAN_EVT_WARNING, +}; + +struct __packed nct6694_can_setting { +__le32 nbr; +__le32 dbr; +u8 active; +u8 reserved[3 ];drivers/net/can/nct6694_canfd.c line 101
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+__le16 ctrl1; +__le16 ctrl2; +__le32 nbtp; +__le32 dbtp; +}; + +struct __packed nct6694_can_information { +u8 tx_fifo_cnt; +u8 rx_fifo_cnt; +__le16 reserved;u8 reserved[2];
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+__le32 can_clk; +}; + +struct __packed nct6694_can_event { +u8 err1; +u8 status1; +u8 tx_evt1; +u8 rx_evt1; +u8 rec1;drivers/net/can/nct6694_canfd.c line 120
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+u8 tec1; +u8 reserved1[2 ]; +u8 err2; +u8 status2; +u8 tx_evt2; +u8 rx_evt2; +u8 rec2; +u8 tec2; +u8 reserved2[2 ]; +};Create an extra struct that only describes a channel
struct __packed nct6694_can_event_channel { u8 err; u8 status; u8 tx_evt; u8 rx_evt; u8 rec; u8 tec; u8 reserved[2]; }and put an array of 2 into struct
__packednct6694_can_event.
Quoted 9 lines
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +struct __packed nct6694_can_xmit {Is this struct for both TX and RX? If so, name it something like
struct
nct6694_can_frameThe term xmit is only used for transmission, not for reception.
Okay, I'll make the modifications in the next patch.
Quoted 20 lines
Quoted 10 lines
drivers/net/can/nct6694_canfd.c line 129
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+u8 tag; +u8 flag; +u8 reserved; +u8 dlc; +__le32 id; +u8 data[64 ]; +u8 msg_buf[72 ];Why is the message so long? What's in the
msg_buf?Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 136
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+}; + +struct nct6694_can_priv { +struct can_priv can;/* must be the first member */ +struct net_device *ndev; +struct nct6694 *nct6694; +struct mutex lock;What does lock protect?
Quoted 8 lines
+1
mutexes are good if you want to keep the lock for a long time.
For short period, spinlock are more performant:
spinlock_t lock;
The lock ensures that data accessed by nct6694_read_msg() and nct6694_write_msg() is not overwritten.
Since nct6694_read_msg() and nct6694_write_msg() use usb_bulk_msg(), which may cause the process to sleep, spinlock is not used.
Quoted 23 lines
Quoted 7 lines
drivers/net/can/nct6694_canfd.c line 140
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+struct sk_buff *tx_skb; +struct workqueue_struct *wq; +struct work_struct tx_work; +unsigned char *tx_buf;void *
Quoted 1 line
+ unsigned char
*rx_buf;void *
Rather than void*,
tx_bufandrx_bufcan be replaced by an union:union nct6694_can_rx { struct nct6694_can_event event; struct nct6694_can_xmit xmit; struct nct6694_can_information info; };(same for
nct6694_can_tx)Then in struct
nct6694_can_priv, you will just have:union
nct6694_can_txtx;
unionnct6694_can_rxrx;
Quoted 7 lines
With this,
NCT6694_MAX_PACKET_SZcan most likely be replaced by
sizeof(union nct6694_can_rx)orsizeof(union nct6694_can_tx).
Okay, I'll make the modifications in the next patch.
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
drivers/net/can/nct6694_canfd.c new file +2
-0+unsigned char can_idx; +bool tx_busy;
...
Quoted 42 lines
Quoted 28 lines
drivers/net/can/nct6694_canfd.c line 239
Quoted 11 lines
drivers/net/can/nct6694_canfd.c new file +11
-0+static void nct6694_can_read_fifo (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->rx_buf; +struct net_device_stats *stats = &ndev->stats; +struct canfd_frame *cf; +struct sk_buff *skb; +int can_idx = priv->can_idx; +u32 id; +int ret; +u8 fd_format =0 ;bool - no need to init
Quoted 16 lines
drivers/net/can/nct6694_canfd.c new file +16
-0+ +guard (mutex)(&priv->lock); + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD11_OFFSET (can_idx,1 ), +sizeof (struct nct6694_can_xmit ), xmit); +if (ret <0 ) +return ; + +/* Check type of frame and create skb */ + fd_format = xmit->flag &NCT6694_CAN_FLAG_FD ; +if (fd_format) + skb =alloc_canfd_skb (ndev, &cf); +else + skb =alloc_can_skb (ndev, (struct can_frame **)&cf); +Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 261
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+if (!skb) { + stats->rx_dropped++; +return ; + } + + cf->len = xmit->dlc;what does
xmit->dlccontain? The DLC or the length?+1
Also, do not trust the device data. Even if SPI attacks are less common, make sure to sanitize this length.
Quoted 8 lines
cf->len = canfd_sanitize_len(xmit->dlc);Or
cf->len = canfd_sanitize_len(xmit->dlc);if
xmit->dlcis in fact a DLC.
Excuse me, the xmit->dlc is actual data length.
Does it need to be fixed?
Quoted 1 line
Quoted 1 line
Quoted 1 line
+
...
Quoted 22 lines
Quoted 10 lines
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+static int nct6694_can_handle_lec_err (struct net_device *ndev,u8 bus_err) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; +struct can_frame *cf; +struct sk_buff *skb; + +if (bus_err ==NCT6694_CAN_EVT_NO_ERROR ) +return 0 ; +Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 352
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ priv->can.can_stats.bus_error++; + stats->rx_errors++; + +/* Propagate the error condition to the CAN stack. */ + skb =alloc_can_err_skb (ndev, &cf); + +if (unlikely (!skb)) +return 0 ;Do not exit if the memory allocation fails. Instead, do the error handling to increase the statistics.
Quoted 2 lines
Look at what other CAN drivers are doing.
Okay, I'll make the modifications in the next patch.
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
drivers/net/can/nct6694_canfd.c new file +41
-0+/* Read the error counter register and check for new errors. */ + cf->can_id |=CAN_ERR_PROT |CAN_ERR_BUSERROR ; + +switch (bus_err) { +case NCT6694_CAN_EVT_CRC_ERROR : + cf->data[3 ] =CAN_ERR_PROT_LOC_CRC_SEQ ; +break ; + +case NCT6694_CAN_EVT_STUFF_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_STUFF ; +break ; + +case NCT6694_CAN_EVT_ACK_ERROR : + cf->data[3 ] =CAN_ERR_PROT_LOC_ACK ; +break ; + +case NCT6694_CAN_EVT_FORM_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_FORM ; +break ; + +case NCT6694_CAN_EVT_BIT_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_BIT | +CAN_ERR_PROT_BIT0 | +CAN_ERR_PROT_BIT1 ; +break ; + +case NCT6694_CAN_EVT_TIMEOUT_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_UNSPEC ; +break ; + +case NCT6694_CAN_EVT_UNKNOWN_ERROR : + cf->data[2 ] |=CAN_ERR_PROT_UNSPEC ; +/* +* It means 'unspecified'(the value is '0'). +* But it is not sure if it's ok to send an error package +* without specific error bit. +*/ +break ; + +default : +break ;
Quoted 9 lines
Quoted 5 lines
drivers/net/can/nct6694_canfd.c line 399
Quoted 5 lines
drivers/net/can/nct6694_canfd.c new file +5
-0+ } + +/* Reset the error counter, ack the IRQ and re-enable the counter. */ + stats->rx_packets++; + stats->rx_bytes += cf->can_dlc;The CAN error frames are not regular packets. Do not increase the RX stats. Instead, increase the error stats.
Okay, I'll make the modifications in the next patch.
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
drivers/net/can/nct6694_canfd.c new file +34
-0+netif_receive_skb (skb); + +return 1 ; +} + +static int nct6694_can_handle_state_change (struct net_device *ndev, +enum can_state new_state) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct net_device_stats *stats = &ndev->stats; +struct can_frame *cf; +struct sk_buff *skb; +struct can_berr_counter bec; + +switch (new_state) { +case CAN_STATE_ERROR_ACTIVE : + priv->can.can_stats.error_warning++; + priv->can.state =CAN_STATE_ERROR_ACTIVE ; +break ; +case CAN_STATE_ERROR_WARNING : + priv->can.can_stats.error_warning++; + priv->can.state =CAN_STATE_ERROR_WARNING ; +break ; +case CAN_STATE_ERROR_PASSIVE : + priv->can.can_stats.error_passive++; + priv->can.state =CAN_STATE_ERROR_PASSIVE ; +break ; +case CAN_STATE_BUS_OFF : + priv->can.state =CAN_STATE_BUS_OFF ; + priv->can.can_stats.bus_off++; +can_bus_off (ndev); +break ; +default : +break ;
Quoted 9 lines
Quoted 6 lines
drivers/net/can/nct6694_canfd.c line 439
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ } + +/* propagate the error condition to the CAN stack */ + skb =alloc_can_err_skb (ndev, &cf); +if (unlikely (!skb)) +return 0 ;Same as above: handle the statistics even if the allocation fails.
Okay, I'll make the modifications in the next patch.
Quoted 11 lines
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 448
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+nct6694_can_get_berr_counter (ndev, &bec); + +switch (new_state) { +case CAN_STATE_ERROR_WARNING : +/* error warning state */ + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] = (bec.txerr > bec.rxerr) ?CAN_ERR_CRTL_TX_WARNING : +CAN_ERR_CRTL_RX_WARNING ;Prefer an if/else here instead of the ternary operator. It is more readable.
Fix it in v4.
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
drivers/net/can/nct6694_canfd.c new file +14
-0+ cf->data[6 ] = bec.txerr; + cf->data[7 ] = bec.rxerr; +break ; +case CAN_STATE_ERROR_PASSIVE : +/* error passive state */ + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] |=CAN_ERR_CRTL_RX_PASSIVE ; +if (bec.txerr >127 ) + cf->data[1 ] |=CAN_ERR_CRTL_TX_PASSIVE ; + cf->data[6 ] = bec.txerr; + cf->data[7 ] = bec.rxerr; +break ; +case CAN_STATE_BUS_OFF : +/* bus-off state */ Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 470
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ cf->can_id |=CAN_ERR_BUSOFF ; +break ; +default : +break ; + } + + stats->rx_packets++; + stats->rx_bytes += cf->can_dlc;
Quoted 2 lines
Do not increase the RX stats when you receive a CAN event.
Okay, I'll make the modifications in the next patch.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+netif_receive_skb (skb); + +return 1 ; +}
...
Quoted 14 lines
Quoted 11 lines
drivers/net/can/nct6694_canfd.c line 603
Quoted 11 lines
drivers/net/can/nct6694_canfd.c new file +11
-0+static int nct6694_can_start (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_setting *setting = (struct nct6694_can_setting *)priv->tx_buf; +const struct can_bittiming *n_bt = &priv->can.bittiming; +const struct can_bittiming *d_bt = &priv->can.data_bittiming; +int ret; + +guard (mutex)(&priv->lock); + +memset (priv->tx_buf,0 ,sizeof (struct nct6694_can_setting ));When you
memset(), use this pattern:
Quoted 4 lines
memset(setting, 0, sizeof(*setting));Apply this throughout your driver.
Okay, I'll make the modifications in the next patch.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/net/can/nct6694_canfd.c new file +3
-0+ setting->nbr =cpu_to_le32 (n_bt->bitrate); + setting->dbr =cpu_to_le32 (d_bt->bitrate); +
...
Quoted 10 lines
Quoted 6 lines
drivers/net/can/nct6694_canfd.c line 920
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+module_platform_driver (nct6694_can_driver); + +MODULE_DESCRIPTION ("USB-CAN FD driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-can" );Yours sincerely,
Vincent Mailhol
Best regards,
Ming
Re: [PATCH v3 4/7] can: Add Nuvoton NCT6694 CAN support
Quoted 5 lines · Ming Yu
Dear Vincent,
Thank you for your comments,
Vincent Mailhol <[email protected]> 於 2024年12月11日 週三 下午11:25寫道:
(...)
Quoted 13 lines
Quoted 13 lines
Quoted 3 lines
Quoted 1 line
+
cf->len=xmit->dlc;what does
xmit->dlccontain? The DLC or the length?+1
Also, do not trust the device data. Even if SPI attacks are less common, make sure to sanitize this length.
cf->len = canfd_sanitize_len(xmit->dlc);Or
Quoted 7 lines
Quoted 4 lines
cf->len = canfd_sanitize_len(xmit->dlc);if
xmit->dlcis in fact a DLC.Excuse me, the
xmit->dlcis actual data length.
Does it need to be fixed?
Yes, name is xmit->len. DLC has a different meaning.
Yours sincerely,
Vincent Mailhol
Re: [PATCH v3 4/7] can: Add Nuvoton NCT6694 CAN support
In reply to Marc Kleine-BuddeDear Marc,
Thank you for your comments,
Marc Kleine-Budde <[email protected]> 於 2024年12月11日 週三 下午5:59寫道:
Quoted 8 lines · Ming Yu
Quoted 2 lines
This driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface.
Please use the rx-offload helper, otherwise the CAN frames might be revived out of order.
Understood. I'll make the modifications in the next patch.
Quoted 1 line
Quoted 1 line
...
Quoted 30 lines
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+ +struct __packed nct6694_can_event { +u8 err1; +u8 status1; +u8 tx_evt1; +u8 rx_evt1; +u8 rec1;drivers/net/can/nct6694_canfd.c line 120
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+u8 tec1; +u8 reserved1[2 ]; +u8 err2; +u8 status2; +u8 tx_evt2; +u8 rx_evt2; +u8 rec2; +u8 tec2; +u8 reserved2[2 ]; +};Create an extra struct that only describes a channel
struct __packed nct6694_can_event_channel { u8 err; u8 status; u8 tx_evt; u8 rx_evt; u8 rec; u8 tec; u8 reserved[2]; }
Quoted 2 lines
and put an array of 2 into struct
__packednct6694_can_event.
Fix it in v4.
Quoted 12 lines
drivers/net/can/nct6694_canfd.c line 129
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+ +struct __packed nct6694_can_xmit { +u8 tag; +u8 flag; +u8 reserved; +u8 dlc; +__le32 id; +u8 data[64 ]; +u8 msg_buf[72 ];Why is the message so long? What's in the
msg_buf?
It's deprecated, I'll drop it in v4.
Quoted 10 lines
drivers/net/can/nct6694_canfd.c line 136
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+}; + +struct nct6694_can_priv { +struct can_priv can;/* must be the first member */ +struct net_device *ndev; +struct nct6694 *nct6694; +struct mutex lock;What does lock protect?
The lock is used to protect tx_buf and rx_buf for each CAN device.
Quoted 4 lines
drivers/net/can/nct6694_canfd.c line 140
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+struct sk_buff *tx_skb; +struct workqueue_struct *wq; +struct work_struct tx_work; +unsigned char *tx_buf;
Quoted 8 lines
void *
Quoted 1 line
+ unsigned char
*rx_buf;void *
drivers/net/can/nct6694_canfd.c line 143
Quoted 2 lines
drivers/net/can/nct6694_canfd.c new file +2
-0+unsigned char can_idx; +bool tx_busy;IMHO it makes no sense to have
tx_skbandtx_busy
Okay! I will revisit these to evaluate whether they are still necessary.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+}; +
...
Quoted 4 lines
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+static void nct6694_can_read_fifo (struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->rx_buf;
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 239
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+struct net_device_stats *stats = &ndev->stats; +struct canfd_frame *cf; +struct sk_buff *skb; +int can_idx = priv->can_idx; +u32 id; +int ret; +u8 fd_format =0 ;bool - no need to init
Fix it in v4.
Quoted 16 lines
Quoted 16 lines
drivers/net/can/nct6694_canfd.c new file +16
-0+ +guard (mutex)(&priv->lock); + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD11_OFFSET (can_idx,1 ), +sizeof (struct nct6694_can_xmit ), xmit); +if (ret <0 ) +return ; + +/* Check type of frame and create skb */ + fd_format = xmit->flag &NCT6694_CAN_FLAG_FD ; +if (fd_format) + skb =alloc_canfd_skb (ndev, &cf); +else + skb =alloc_can_skb (ndev, (struct can_frame **)&cf); +
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 261
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+if (!skb) { + stats->rx_dropped++; +return ; + } + + cf->len = xmit->dlc;what does
xmit->dlccontain? The DLC or the length?
It reflects the actual data length.
Quoted 10 lines
drivers/net/can/nct6694_canfd.c line 270
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+ +/* Get ID and set flag by its type(Standard ID format or Ext ID format) */ + id =le32_to_cpu (xmit->id); +if (xmit->flag &NCT6694_CAN_FLAG_EFF ) { +/* +* In case the Extended ID frame is received, the standard +* and extended part of the ID are swapped in the register, +* so swap them back to obtain the correct ID. +*/
Quoted 2 lines
You comment doesn't match the code.
Fix it in v4.
Quoted 27 lines
Quoted 27 lines
drivers/net/can/nct6694_canfd.c new file +27
-0+ id |=CAN_EFF_FLAG ; + } + + cf->can_id = id; + +/* Set ESI flag */ +if (xmit->flag &NCT6694_CAN_FLAG_ERR ) { + cf->flags |=CANFD_ESI ; +netdev_dbg (ndev,"ESI Error\n" ); + } + +/* Set RTR and BRS */ +if (!fd_format && (xmit->flag &NCT6694_CAN_FLAG_RTR )) { + cf->can_id |=CAN_RTR_FLAG ; + }else { +if (xmit->flag &NCT6694_CAN_FLAG_BRS ) + cf->flags |=CANFD_BRS ; + +memcpy (cf->data, xmit->data, cf->len); + + stats->rx_bytes += cf->len; + } + + stats->rx_packets++; + +netif_receive_skb (skb); +}
...
Quoted 9 lines
Quoted 9 lines
drivers/net/can/nct6694_canfd.c new file +9
-0+static int nct6694_can_poll (struct net_device *ndev,int quota) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_event *evt = (struct nct6694_can_event *)priv->rx_buf; +int can_idx = priv->can_idx; +int work_done =0 , ret; +u8 evt_mask =NCT6694_CAN_EVENT_ERR |NCT6694_CAN_EVENT_STATUS ; +u8 bus_err, can_status; +
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 527
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ scoped_guard(mutex, &priv->lock) { + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD2_OFFSET (can_idx, evt_mask), +sizeof (struct nct6694_can_event ), evt); +if (ret <0 ) +return IRQ_NONE ;propagate the error
Fix it in v4.
Quoted 10 lines
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ +if (can_idx) { + bus_err = evt->err2; + can_status = evt->status2; + }else { + bus_err = evt->err1; + can_status = evt->status1; + } + } +
...
Quoted 6 lines
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+static netdev_tx_t nct6694_can_start_xmit (struct sk_buff *skb, +struct net_device *ndev) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); + +if (priv->tx_skb || priv->tx_busy) {
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 714
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+netdev_err (ndev,"hard_xmit called while tx busy\n" ); +return NETDEV_TX_BUSY ; + } + +if (can_dev_dropped_skb (ndev, skb)) +return NETDEV_TX_OK ;please drop first
Fix it in v4.
Quoted 22 lines
Quoted 22 lines
drivers/net/can/nct6694_canfd.c new file +22
-0+ +netif_stop_queue (ndev); + priv->tx_skb = skb; +queue_work (priv->wq, &priv->tx_work); + +return NETDEV_TX_OK ; +} + +static void nct6694_can_tx (struct net_device *ndev,struct canfd_frame *cf) +{ +struct nct6694_can_priv *priv =netdev_priv (ndev); +struct nct6694_can_xmit *xmit = (struct nct6694_can_xmit *)priv->tx_buf; +u32 txid =0 ; + +memset (xmit,0 ,sizeof (struct nct6694_can_xmit )); + +if (priv->can_idx ==0 ) + xmit->tag = NCT6694_CAN_TAG_CAN0<; +else + xmit->tag =NCT6694_CAN_TAG_CAN1 ; + +if (cf->can_id &CAN_EFF_FLAG ) {
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 742
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ txid = cf->can_id &CAN_EFF_MASK ; +/* +* In case the Extended ID frame is transmitted, the +* standard and extended part of the ID are swapped +* in the register, so swap them back to send the +* correct ID. You comment doesn't match the code.
Fix it in v4.
Quoted 4 lines
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+*/ + xmit->flag |=NCT6694_CAN_FLAG_EFF ; + }else { + txid = cf->can_id &CAN_SFF_MASK ;
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 752
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ } + + xmit->id =cpu_to_le32 (txid); + xmit->dlc = cf->len; + + if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&No need to check ctrlmode
Fix it in v4.
Quoted 11 lines
drivers/net/can/nct6694_canfd.c line 760
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+ can_is_canfd_skb(priv->tx_skb)) { + xmit->flag |=NCT6694_CAN_FLAG_FD ; +if (cf->flags &CANFD_BRS ) + xmit->flag |=NCT6694_CAN_FLAG_BRS ; + } + +if (cf->can_id &CAN_RTR_FLAG ) + xmit->flag |=NCT6694_CAN_FLAG_RTR ;you can move this into the !can_is_canfd_skb branch of the if
Fix it in v4.
Quoted 64 lines
Quoted 64 lines
drivers/net/can/nct6694_canfd.c new file +64
-0+ +memcpy (xmit->data, cf->data, cf->len); + +nct6694_write_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD10_OFFSET (1 ), +sizeof (struct nct6694_can_xmit ), + xmit); +} + +static void nct6694_can_tx_work (struct work_struct *work) +{ +struct nct6694_can_priv *priv =container_of (work, + struct nct6694_can_priv, + tx_work); +struct net_device *ndev = priv->ndev; +struct canfd_frame *cf; + +guard (mutex)(&priv->lock); + +if (priv->tx_skb) { +if (priv->can.state ==CAN_STATE_BUS_OFF ) { +nct6694_can_clean (ndev); + }else { + cf = (struct canfd_frame *)priv->tx_skb->data; +nct6694_can_tx (ndev, cf); + priv->tx_busy = true; +can_put_echo_skb (priv->tx_skb, ndev,0 ,0 ); + priv->tx_skb =NULL ; + } + } +} + +static const struct net_device_ops nct6694_can_netdev_ops = { + .ndo_open = nct6694_can_open, + .ndo_stop = nct6694_can_stop, + .ndo_start_xmit = nct6694_can_start_xmit, + .ndo_change_mtu = can_change_mtu, +}; + +static const struct ethtool_ops nct6694_can_ethtool_ops = { + .get_ts_info = ethtool_op_get_ts_info, +}; + +static int nct6694_can_get_clock (struct nct6694_can_priv *priv) +{ +struct nct6694_can_information *info = (struct nct6694_can_information *)priv->rx_buf; +int ret; + + ret =nct6694_read_msg (priv->nct6694,NCT6694_CAN_MOD , +NCT6694_CAN_CMD1_OFFSET , +sizeof (struct nct6694_can_information ), + info); +if (ret) +return ret; + +return le32_to_cpu (info->can_clk); +} + +static int nct6694_can_probe (struct platform_device *pdev) +{ +const struct mfd_cell *cell =mfd_get_cell (pdev); +struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +struct nct6694_can_priv *priv; +struct net_device *ndev;
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 830
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+int ret, irq, can_clk; + + irq =irq_create_mapping (nct6694->domain, +NCT6694_IRQ_CAN1 + cell->id); +if (!irq) +return -EINVAL ;propagate error value
Fix it in v4.
Quoted 18 lines
Quoted 6 lines
drivers/net/can/nct6694_canfd.c new file +6
-0+ + ndev =alloc_candev (sizeof (struct nct6694_can_priv ),1 ); +if (!ndev) +return -ENOMEM ; + + ndev->irq = irq;drivers/net/can/nct6694_canfd.c line 846
Quoted 10 lines
drivers/net/can/nct6694_canfd.c new file +10
-0+ ndev->flags |=IFF_ECHO ; + ndev->netdev_ops = &nct6694_can_netdev_ops; + ndev->ethtool_ops = &nct6694_can_ethtool_ops; + + priv =netdev_priv (ndev); + priv->nct6694 = nct6694; + priv->ndev = ndev; + + priv->tx_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
devm_kzalloc()
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 853
Quoted 7 lines
drivers/net/can/nct6694_canfd.c new file +7
-0+if (!priv->tx_buf) { + ret = -ENOMEM ; + goto free_candev; + } + + priv->rx_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
devm_kzalloc()
Fix it in v4.
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 861
Quoted 8 lines
drivers/net/can/nct6694_canfd.c new file +8
-0+if (!priv->rx_buf) { + ret = -ENOMEM ; + goto free_candev; + } + + can_clk =nct6694_can_get_clock (priv); +if (can_clk <0 ) { + ret = -EIO ;
Quoted 4 lines
propagate the error value, don't overwrite it
move the
dev_err_probe()here.
Fix it in v4.
Quoted 30 lines
Quoted 30 lines
drivers/net/can/nct6694_canfd.c new file +30
-0+ goto free_candev; + } + +mutex_init (&priv->lock); +INIT_WORK (&priv->tx_work, nct6694_can_tx_work); + + priv->can_idx = cell->id; + priv->can.state =CAN_STATE_STOPPED ; + priv->can.clock.freq = can_clk; + priv->can.bittiming_const = &nct6694_can_bittiming_nominal_const; + priv->can.data_bittiming_const = &nct6694_can_bittiming_data_const; + priv->can.do_set_mode = nct6694_can_set_mode; + priv->can.do_get_berr_counter = nct6694_can_get_berr_counter; + + priv->can.ctrlmode =CAN_CTRLMODE_FD ; + + priv->can.ctrlmode_supported =CAN_CTRLMODE_LOOPBACK | +CAN_CTRLMODE_LISTENONLY | +CAN_CTRLMODE_FD | +CAN_CTRLMODE_FD_NON_ISO | +CAN_CTRLMODE_BERR_REPORTING ; + +platform_set_drvdata (pdev, priv); +SET_NETDEV_DEV (priv->ndev, &pdev->dev); + + ret =register_candev (priv->ndev); +if (ret) + goto free_candev; + +return 0 ;
Quoted 9 lines
drivers/net/can/nct6694_canfd.c line 895
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+ +free_candev: +free_candev (ndev); +return dev_err_probe (&pdev->dev, ret,"Probe failed\n" );Move the
dev_err_probe()with an appropriate error message to where the error occurs. If malloc fails, the kernel already prints for you, so here it's onlynct6694_can_get_clock()only.
Understood. I'll make the modifications in the next patch.
Quoted 34 lines
Quoted 26 lines
drivers/net/can/nct6694_canfd.c new file +25
-1+} + +static void nct6694_can_remove (struct platform_device *pdev) +{ +struct nct6694_can_priv *priv =platform_get_drvdata (pdev); + +cancel_work_sync (&priv->tx_work); +unregister_candev (priv->ndev); +free_candev (priv->ndev); +} + +static struct platform_driver nct6694_can_driver = { + .driver = { + .name =DRVNAME , + }, + .probe = nct6694_can_probe, + .remove = nct6694_can_remove, +}; + +module_platform_driver (nct6694_can_driver); + +MODULE_DESCRIPTION ("USB-CAN FD driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-can" ); --Quoted 4 lines
2.34.1
regards,
Marc
Quoted 5 lines
--
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de |Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Best regards,
Ming
Re: [PATCH v3 4/7] can: Add Nuvoton NCT6694 CAN support
Hi Marc,
Quoted 21 lines
Quoted 8 lines
drivers/net/can/nct6694_canfd.c line 136
Quoted 5 lines
drivers/net/can/nct6694_canfd.c new file +5
-0+struct nct6694_can_priv { +struct can_priv can;/* must be the first member */ +struct net_device *ndev; +struct nct6694 *nct6694; +struct mutex lock;What does lock protect?
The lock is used to protect
tx_bufandrx_buffor each CAN device.Quoted 10 lines
drivers/net/can/nct6694_canfd.c line 140
Quoted 4 lines
drivers/net/can/nct6694_canfd.c new file +4
-0+struct sk_buff *tx_skb; +struct workqueue_struct *wq; +struct work_struct tx_work; +unsigned char *tx_buf;void *
Quoted 1 line
+ unsigned char
*rx_buf;void *
Quoted 2 lines
drivers/net/can/nct6694_canfd.c new file +2
-0+unsigned char can_idx; +bool tx_busy;
Quoted 7 lines
Quoted 2 lines
IMHO it makes no sense to have
tx_skbandtx_busyOkay! I will revisit these to evaluate whether they are still necessary.
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+}; +
I think there needs to be a tx_skb to record the skb passed by start_xmit(), otherwise it can't handle the can_frame in tx_work. If this is not necessary, could you please explain?
In addition, the tx flow is based on the implementation in
https://elixir.bootlin.com/linux/v6.12.6/source/drivers/net/can/spi/mcp251x.c
Thanks,
Ming
PATCH v3 5/7 watchdog: Add Nuvoton NCT6694 WDT support
In reply to Ming YuThis driver supports Watchdog timer functionality for NCT6694 MFD device based on USB interface.
--- MAINTAINERS | 1 + drivers/watchdog/Kconfig | 11 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/nct6694_wdt.c | 277 +++++++++++++++++++++++++++++++++ 4 files changed, 290 insertions(+) create mode 100644 drivers/watchdog/nct6694_wdt.c
MAINTAINERS +1 -0
@@ -16549,6 +16549,7 @@ F: drivers/gpio/gpio-nct6694.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/nct6694_canfd.c +F: drivers/watchdog/nct6694_wdt.c F: include/linux/mfd/nct6694.h NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/watchdog/Kconfig +11 -0
@@ -739,6 +739,17 @@ config MAX77620_WATCHDOG MAX77620 chips. To compile this driver as a module, choose M here: the module will be called max77620_wdt. +config NCT6694_WATCHDOG + tristate "Nuvoton NCT6694 watchdog support" + depends on MFD_NCT6694 + select WATCHDOG_CORE + help + Say Y here to support Nuvoton NCT6694 watchdog timer + functionality. + + This driver can also be built as a module. If so, the module + will be called nct6694_wdt. + config IMX2_WDT tristate "IMX2+ Watchdog" depends on ARCH_MXC || ARCH_LAYERSCAPE || COMPILE_TEST
drivers/watchdog/Makefile +1 -0
@@ -232,6 +232,7 @@ obj-$(CONFIG_WM831X_WATCHDOG) += wm831x_wdt.o obj-$(CONFIG_WM8350_WATCHDOG) += wm8350_wdt.o obj-$(CONFIG_MAX63XX_WATCHDOG) += max63xx_wdt.o obj-$(CONFIG_MAX77620_WATCHDOG) += max77620_wdt.o +obj-$(CONFIG_NCT6694_WATCHDOG) += nct6694_wdt.o obj-$(CONFIG_ZIIRAVE_WATCHDOG) += ziirave_wdt.o obj-$(CONFIG_SOFT_WATCHDOG) += softdog.o obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o
drivers/watchdog/nct6694_wdt.c new file +277 -0
277 lines not on this page · raw message
Signature · 1 line
2.34.1
Guenter Roeck
maintainerRe: [PATCH v3 5/7] watchdog: Add Nuvoton NCT6694 WDT support
Quoted 5 lines · Ming Yu
This driver supports Watchdog timer functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>---
...
Quoted 2 lines
drivers/watchdog/nct6694_wdt.c new file +2
-0+static int nct6694_wdt_probe (struct platform_device *pdev) +{
...
Quoted 7 lines
drivers/watchdog/nct6694_wdt.c new file +7
-0+ wdev->timeout = timeout; + wdev->pretimeout = pretimeout; +if (timeout < pretimeout) { +dev_warn (data->dev,"pretimeout < timeout. Setting to zero\n" ); + wdev->pretimeout =0 ; + } +
drivers/watchdog/nct6694_wdt.c line 258
Quoted 10 lines
drivers/watchdog/nct6694_wdt.c new file +10
-0+ wdev->min_timeout =1 ; + wdev->max_timeout =255 ; + +mutex_init (&data->lock); + +platform_set_drvdata (pdev, data); + +/* Register watchdog timer device to WDT framework */ +watchdog_set_drvdata (&data->wdev, data); +watchdog_init_timeout (&data->wdev, timeout, dev);
This is pointless since timeout is pre-initialized with a value != 0. That means a value provided through devicetree will never be used unless the user sets timeout=0 as module parameter. But then the above check for pretimeout is useless.
Guenter
Re: [PATCH v3 5/7] watchdog: Add Nuvoton NCT6694 WDT support
Dear Guenter,
Thank you for your comments,
Guenter Roeck <[email protected]> 於 2024年12月10日 週二 下午11:22寫道:
Quoted 18 lines
Quoted 3 lines
drivers/watchdog/nct6694_wdt.c new file +2
-0+static int nct6694_wdt_probe (struct platform_device *pdev) +{...
Quoted 14 lines
drivers/watchdog/nct6694_wdt.c new file +14
-0+ wdev->timeout = timeout; + wdev->pretimeout = pretimeout; +if (timeout < pretimeout) { +dev_warn (data->dev,"pretimeout < timeout. Setting to zero\n" ); + wdev->pretimeout =0 ; + } + + wdev->min_timeout =1 ; + wdev->max_timeout =255 ; + +mutex_init (&data->lock); + +platform_set_drvdata (pdev, data); +
Quoted 9 lines
drivers/watchdog/nct6694_wdt.c line 258
Quoted 3 lines
drivers/watchdog/nct6694_wdt.c new file +3
-0+/* Register watchdog timer device to WDT framework */ +watchdog_set_drvdata (&data->wdev, data); +watchdog_init_timeout (&data->wdev, timeout, dev);This is pointless since timeout is pre-initialized with a value != 0. That means a value provided through devicetree will never be used unless the user sets timeout=0 as module parameter. But then the above check for pretimeout is useless.
Understood! I will drop it in v4.
Best regards,
Ming
PATCH v3 6/7 hwmon: Add Nuvoton NCT6694 HWMON support
In reply to Ming YuThis driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface.
--- MAINTAINERS | 1 + drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/nct6694-hwmon.c | 768 ++++++++++++++++++++++++++++++++++ 4 files changed, 780 insertions(+) create mode 100644 drivers/hwmon/nct6694-hwmon.c
MAINTAINERS +1 -0
@@ -16546,6 +16546,7 @@ M: Ming Yu <[email protected]> L: [email protected] S: Supported F: drivers/gpio/gpio-nct6694.c +F: drivers/hwmon/nct6694-hwmon.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/nct6694_canfd.c
drivers/hwmon/Kconfig +10 -0
@@ -1636,6 +1636,16 @@ config SENSORS_NCT6683 This driver can also be built as a module. If so, the module will be called nct6683. +config SENSORS_NCT6694 + tristate "Nuvoton NCT6694 Hardware Monitor support" + depends on MFD_NCT6694 + help + Say Y here to support Nuvoton NCT6694 hardware monitoring + functionality. + + This driver can also be built as a module. If so, the module + will be called nct6694-hwmon. + config SENSORS_NCT6775_CORE tristate select REGMAP
drivers/hwmon/Makefile +1 -0
@@ -168,6 +168,7 @@ obj-$(CONFIG_SENSORS_MLXREG_FAN) += mlxreg-fan.o obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o obj-$(CONFIG_SENSORS_MR75203) += mr75203.o obj-$(CONFIG_SENSORS_NCT6683) += nct6683.o +obj-$(CONFIG_SENSORS_NCT6694) += nct6694-hwmon.o obj-$(CONFIG_SENSORS_NCT6775_CORE) += nct6775-core.o nct6775-objs := nct6775-platform.o obj-$(CONFIG_SENSORS_NCT6775) += nct6775.o
drivers/hwmon/nct6694-hwmon.c new file +768 -0
768 lines not on this page · raw message
Signature · 1 line
2.34.1
Guenter Roeck
maintainerRe: [PATCH v3 6/7] hwmon: Add Nuvoton NCT6694 HWMON support
Quoted 4 lines · Ming Yu
This driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>
checkpatch:
WARNING: From:/Signed-off-by: email address mismatch: 'From: Ming Yu <[email protected]>' != 'Signed-off-by: Ming Yu <[email protected]>'
Quoted 123 lines
--- MAINTAINERS | 1 + drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/nct6694-hwmon.c | 768 ++++++++++++++++++++++++++++++++++ 4 files changed, 780 insertions(+) create mode 100644 drivers/hwmon/nct6694-hwmon.cMAINTAINERS +1
-0@@ -16546,6 +16546,7 @@ M: Ming Yu <[email protected]> L: [email protected] S: Supported F: drivers/gpio/gpio-nct6694.c +F: drivers/hwmon/nct6694-hwmon.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/nct6694_canfd.cdrivers/hwmon/Kconfig +10
-0@@ -1636,6 +1636,16 @@ config SENSORS_NCT6683 This driver can also be built as a module. If so, the module will be called nct6683. +config SENSORS_NCT6694 + tristate "Nuvoton NCT6694 Hardware Monitor support" + depends on MFD_NCT6694 + help + Say Y here to support Nuvoton NCT6694 hardware monitoring + functionality. + + This driver can also be built as a module. If so, the module + will be called nct6694-hwmon. + config SENSORS_NCT6775_CORE tristate select REGMAPdrivers/hwmon/Makefile +1
-0@@ -168,6 +168,7 @@ obj-$(CONFIG_SENSORS_MLXREG_FAN) += mlxreg-fan.o obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o obj-$(CONFIG_SENSORS_MR75203) += mr75203.o obj-$(CONFIG_SENSORS_NCT6683) += nct6683.o +obj-$(CONFIG_SENSORS_NCT6694) += nct6694-hwmon.o obj-$(CONFIG_SENSORS_NCT6775_CORE) += nct6775-core.o nct6775-objs := nct6775-platform.o obj-$(CONFIG_SENSORS_NCT6775) += nct6775.odrivers/hwmon/nct6694-hwmon.c new file +64
-0@@ -0,0 +1,768 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Nuvoton NCT6694 HWMON driver based on USB interface. +* +* Copyright (C) 2024 Nuvoton Technology Corp. +*/ + +#include <linux/bitfield.h> +#include <linux/hwmon.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/slab.h> + +/* Host interface */ +#define NCT6694_RPT_MOD 0xFF +#define NCT6694_HWMON_MOD 0x00 +#define NCT6694_PWM_MOD 0x01 + +/* Report Channel */ +#define NCT6694_VIN_IDX (x) (0x00 + (x)) +#define NCT6694_TIN_IDX (x) \ + ({ typeof(x) (_x) = (x); \ + ((_x) < 10) ? (0x10 + ((_x) * 2)) : \ + (0x30 + (((_x) - 10) * 2)); }) +#define NCT6694_FIN_IDX (x) (0x50 + ((x) * 2)) +#define NCT6694_PWM_IDX (x) (0x70 + (x)) +#define NCT6694_VIN_STS (x) (0x68 + (x)) +#define NCT6694_TIN_STS (x) (0x6A + (x)) +#define NCT6694_FIN_STS (x) (0x6E + (x)) + +/* Message Channel*/ +/* HWMON Command */ +/* Command 00h */ +#define NCT6694_HWMON_CMD0_LEN 0x40 +#define NCT6694_HWMON_CMD0_OFFSET 0x0000/* OFFSET = SEL|CMD */ +#define NCT6694_VIN_EN (x) (0x00 + (x)) +#define NCT6694_TIN_EN (x) (0x02 + (x)) +#define NCT6694_FIN_EN (x) (0x04 + (x)) +#define NCT6694_PWM_EN (x) (0x06 + (x)) +#define NCT6694_PWM_FREQ_IDX (x) (0x30 + (x)) +/* Command 02h */ +#define NCT6694_HWMON_CMD2_LEN 0x90 +#define NCT6694_HWMON_CMD2_OFFSET 0x0002/* OFFSET = SEL|CMD */ +#define NCT6694_SMI_CTRL_IDX 0x00 +#define NCT6694_VIN_HL (x) (0x10 + ((x) * 2)) +#define NCT6694_VIN_LL (x) (0x11 + ((x) * 2)) +#define NCT6694_TIN_HYST (x) (0x30 + ((x) * 2)) +#define NCT6694_TIN_HL (x) (0x31 + ((x) * 2)) +#define NCT6694_FIN_HL (x) (0x70 + ((x) * 2)) +#define NCT6694_FIN_LL (x) (0x71 + ((x) * 2)) +/* PWM Command */ +#define NCT6694_PWM_CMD1_LEN 0x18 +#define NCT6694_PWM_CMD1_OFFSET 0x0001 +#define NCT6694_MAL_VAL (x) (0x02 + (x)) + +#define NCT6694_FREQ_FROM_REG (reg) ((reg) * 25000 / 255) +#define NCT6694_FREQ_TO_REG (val) \ + (DIV_ROUND_CLOSEST(clamp_val((val), 100, 25000) * 255, 25000)) + +#define NCT6694_LSB_REG_MASK GENMASK(7, 5) +#define NCT6694_TIN_HYST_MASK GENMASK(7, 5)
drivers/hwmon/nct6694-hwmon.c line 74
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+ +static inline long in_from_reg (u8 reg) +{ +return reg *16 ; +} + +static inline u8 in_to_reg (long val) +{ +if (val <=0 ) +return 0 ;
This is pointless since the calling code already clamps the value to [0, 2032].
drivers/hwmon/nct6694-hwmon.c line 75
Quoted 1 line
drivers/hwmon/nct6694-hwmon.c new file +1
-0+return val /16 ;
DIV_ROUND_CLOSEST() ?
drivers/hwmon/nct6694-hwmon.c line 80
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+} + +static inline long temp_from_reg (u8 reg) +{ +return reg *1000 ;
This always returns a positive value, even though the temperature is supposed to be signed. More on that below.
drivers/hwmon/nct6694-hwmon.c line 85
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+} + +static inline u8 temp_to_reg (long val) +{ +return val /1000 ;
DIV_ROUND_CLOSEST() ?
Quoted 95 lines
drivers/hwmon/nct6694-hwmon.c new file +95
-0+} + +struct nct6694_hwmon_data { +struct nct6694 *nct6694; +struct mutex lock; +unsigned char *xmit_buf; +unsigned char hwmon_en[NCT6694_HWMON_CMD0_LEN ]; +}; + +#define NCT6694_HWMON_IN_CONFIG (HWMON_I_INPUT | HWMON_I_ENABLE | \ + HWMON_I_MAX | HWMON_I_MIN | \ + HWMON_I_ALARM) +#define NCT6694_HWMON_TEMP_CONFIG (HWMON_T_INPUT | HWMON_T_ENABLE | \ + HWMON_T_MAX | HWMON_T_MAX_HYST | \ + HWMON_T_MAX_ALARM) +#define NCT6694_HWMON_FAN_CONFIG (HWMON_F_INPUT | HWMON_F_ENABLE | \ + HWMON_F_MIN | HWMON_F_MIN_ALARM) +#define NCT6694_HWMON_PWM_CONFIG (HWMON_PWM_INPUT | HWMON_PWM_ENABLE | \ + HWMON_PWM_FREQ) +static const struct hwmon_channel_info *nct6694_info[] = { +HWMON_CHANNEL_INFO (in, +NCT6694_HWMON_IN_CONFIG ,/* VIN0 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN1 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN2 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN3 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN5 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN6 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN7 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN14 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN15 */ +NCT6694_HWMON_IN_CONFIG ,/* VIN16 */ +NCT6694_HWMON_IN_CONFIG ,/* VBAT */ +NCT6694_HWMON_IN_CONFIG ,/* VSB */ +NCT6694_HWMON_IN_CONFIG ,/* AVSB */ +NCT6694_HWMON_IN_CONFIG ,/* VCC */ +NCT6694_HWMON_IN_CONFIG ,/* VHIF */ +NCT6694_HWMON_IN_CONFIG ),/* VTT */ + +HWMON_CHANNEL_INFO (temp, +NCT6694_HWMON_TEMP_CONFIG ,/* THR1 */ +NCT6694_HWMON_TEMP_CONFIG ,/* THR2 */ +NCT6694_HWMON_TEMP_CONFIG ,/* THR14 */ +NCT6694_HWMON_TEMP_CONFIG ,/* THR15 */ +NCT6694_HWMON_TEMP_CONFIG ,/* THR16 */ +NCT6694_HWMON_TEMP_CONFIG ,/* TDP0 */ +NCT6694_HWMON_TEMP_CONFIG ,/* TDP1 */ +NCT6694_HWMON_TEMP_CONFIG ,/* TDP2 */ +NCT6694_HWMON_TEMP_CONFIG ,/* TDP3 */ +NCT6694_HWMON_TEMP_CONFIG ,/* TDP4 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN0 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN1 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN2 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN3 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN4 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN5 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN6 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN7 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN8 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN9 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN10 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN11 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN12 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN13 */ +NCT6694_HWMON_TEMP_CONFIG ,/* DTIN14 */ +NCT6694_HWMON_TEMP_CONFIG ),/* DTIN15 */ + +HWMON_CHANNEL_INFO (fan, +NCT6694_HWMON_FAN_CONFIG ,/* FIN0 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN1 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN2 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN3 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN4 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN5 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN6 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN7 */ +NCT6694_HWMON_FAN_CONFIG ,/* FIN8 */ +NCT6694_HWMON_FAN_CONFIG ),/* FIN9 */ + +HWMON_CHANNEL_INFO (pwm, +NCT6694_HWMON_PWM_CONFIG ,/* PWM0 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM1 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM2 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM3 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM4 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM5 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM6 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM7 */ +NCT6694_HWMON_PWM_CONFIG ,/* PWM8 */ +NCT6694_HWMON_PWM_CONFIG ),/* PWM9 */ +NULL +}; + +static int nct6694_in_read (struct device *dev,u32 attr,int channel, +long *val) +{
drivers/hwmon/nct6694-hwmon.c line 190
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char vin_en; +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_in_enable: + vin_en = data->hwmon_en[NCT6694_VIN_EN (channel /8 )]; + *val = !!(vin_en &BIT (channel %8 )) ?1 :0 ;
Drop "? 1 : 0"
drivers/hwmon/nct6694-hwmon.c line 196
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+ +return 0 ; +case hwmon_in_input: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_VIN_IDX (channel),1 , + data->xmit_buf);
I am curious: Since the received data length is not returned, and the expected minimum length is not passed to nct6694_read_msg(), the driver has no means to check if the received data actually includes the expected values at the expected index. That doesn't matter here, but some of the returned data is located far into the buffer. Is it indeed not necessary for the driver to check if the received data is actually as long as expected ?
Quoted 47 lines
drivers/hwmon/nct6694-hwmon.c new file +47
-0+if (ret) +return ret; + + *val =in_from_reg (data->xmit_buf[0 ]); + +return 0 ; +case hwmon_in_max: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + *val =in_from_reg (data->xmit_buf[NCT6694_VIN_HL (channel)]); + +return 0 ; +case hwmon_in_min: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + *val =in_from_reg (data->xmit_buf[NCT6694_VIN_LL (channel)]); + +return 0 ; +case hwmon_in_alarm: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_VIN_STS (channel /8 ),1 , + data->xmit_buf); +if (ret) +return ret; + + *val = !!(data->xmit_buf[0 ] &BIT (channel %8 )); + +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_temp_read (struct device *dev,u32 attr,int channel, +long *val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev);
drivers/hwmon/nct6694-hwmon.c line 253
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+unsigned char temp_en, temp_hyst; +int ret, int_part, frac_part; +signed char temp_max; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_temp_enable: + temp_en = data->hwmon_en[NCT6694_TIN_EN (channel /8 )]; + *val = !!(temp_en &BIT (channel %8 )) ?1 :0 ;
Drop "? 1 : 0"
Quoted 17 lines
drivers/hwmon/nct6694-hwmon.c new file +17
-0+ +return 0 ; +case hwmon_temp_input: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_TIN_IDX (channel),2 , + data->xmit_buf); +if (ret) +return ret; + + int_part =sign_extend32 (data->xmit_buf[0 ],7 ); + frac_part =FIELD_GET (NCT6694_LSB_REG_MASK , data->xmit_buf[1 ]); +if (int_part <0 ) + *val = (int_part +1 ) *1000 - (8 - frac_part) *125 ; +else + *val = int_part *1000 + frac_part *125 ; + +return 0 ;
drivers/hwmon/nct6694-hwmon.c line 280
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+case hwmon_temp_max: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + *val =temp_from_reg (data->xmit_buf[NCT6694_TIN_HL (channel)]); +
If the value in NCT6694_TIN_HL() is signed, this will return a large positive value instead.
drivers/hwmon/nct6694-hwmon.c line 293
Quoted 13 lines
drivers/hwmon/nct6694-hwmon.c new file +13
-0+return 0 ; +case hwmon_temp_max_hyst: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + temp_max = (signed char )data->xmit_buf[NCT6694_TIN_HL (channel)]; + temp_hyst =FIELD_GET (NCT6694_TIN_HYST_MASK , + data->xmit_buf[NCT6694_TIN_HYST (channel)]); +if (temp_max <0 )
This suggests that the temperature limit is signed, suggesting in turn that temp_from_reg() is wrong.
Quoted 23 lines
drivers/hwmon/nct6694-hwmon.c new file +23
-0+ *val =temp_from_reg (temp_max + temp_hyst); + else + *val =temp_from_reg (temp_max - temp_hyst); + +return 0 ; +case hwmon_temp_max_alarm: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_TIN_STS (channel /8 ),1 , + data->xmit_buf); +if (ret) +return ret; + + *val = !!(data->xmit_buf[0 ] &BIT (channel %8 )); + +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_fan_read (struct device *dev,u32 attr,int channel, +long *val) +{
drivers/hwmon/nct6694-hwmon.c line 326
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char fanin_en; +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_fan_enable: + fanin_en = data->hwmon_en[NCT6694_FIN_EN (channel /8 )]; + *val = !!(fanin_en &BIT (channel %8 )) ?1 :0 ;
Drop "? 1 : 0"
drivers/hwmon/nct6694-hwmon.c line 337
Quoted 11 lines
drivers/hwmon/nct6694-hwmon.c new file +11
-0+ +return 0 ; +case hwmon_fan_input: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_FIN_IDX (channel),2 , + data->xmit_buf); +if (ret) +return ret; + + *val = (data->xmit_buf[1 ] | + (data->xmit_buf[0 ] <<8 )) &0xFFFF ;
The "& 0xffff" is pointless.
drivers/hwmon/nct6694-hwmon.c line 349
Quoted 12 lines
drivers/hwmon/nct6694-hwmon.c new file +12
-0+ +return 0 ; +case hwmon_fan_min: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + *val = (data->xmit_buf[NCT6694_FIN_LL (channel)] | + data->xmit_buf[NCT6694_FIN_HL (channel)] <<8 ) &0xFFFF ;
Same here.
Quoted 20 lines
drivers/hwmon/nct6694-hwmon.c new file +20
-0+ +return 0 ; +case hwmon_fan_min_alarm: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_FIN_STS (channel /8 ), +1 , data->xmit_buf); +if (ret) +return ret; + + *val = !!(data->xmit_buf[0 ] &BIT (channel %8 )); + +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_pwm_read (struct device *dev,u32 attr,int channel, +long *val) +{
drivers/hwmon/nct6694-hwmon.c line 379
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char pwm_en; +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_pwm_enable: + pwm_en = data->hwmon_en[NCT6694_PWM_EN (channel /8 )]; + *val = !!(pwm_en &BIT (channel %8 )) ?1 :0 ;
Drop "? 1 : 0".
Quoted 114 lines
drivers/hwmon/nct6694-hwmon.c new file +114
-0+ +return 0 ; +case hwmon_pwm_input: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_PWM_IDX (channel), +1 , data->xmit_buf); +if (ret) +return ret; + + *val = data->xmit_buf[0 ]; + +return 0 ; +case hwmon_pwm_freq: + *val =NCT6694_FREQ_FROM_REG (data->hwmon_en[NCT6694_PWM_FREQ_IDX (channel)]); + +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_enable_channel (struct device *dev,u8 reg, +int channel,long val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); + +if (val ==0 ) + data->hwmon_en[reg] &= ~BIT (channel %8 ); +else if (val ==1 ) + data->hwmon_en[reg] |=BIT (channel %8 ); +else +return -EINVAL ; + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD0_OFFSET , +NCT6694_HWMON_CMD0_LEN , + data->hwmon_en); +} + +static int nct6694_in_write (struct device *dev,u32 attr,int channel, +long val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_in_enable: +return nct6694_enable_channel (dev,NCT6694_VIN_EN (channel /8 ), + channel, val); +case hwmon_in_max: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + val =clamp_val (val,0 ,2032 ); + data->xmit_buf[NCT6694_VIN_HL (channel)] =in_to_reg (val); + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +case hwmon_in_min: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + val =clamp_val (val,0 ,2032 ); + data->xmit_buf[NCT6694_VIN_LL (channel)] =in_to_reg (val); + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_temp_write (struct device *dev,u32 attr,int channel, +long val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +signed char temp_max, temp_hyst; +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_temp_enable: +return nct6694_enable_channel (dev,NCT6694_TIN_EN (channel /8 ), + channel, val); +case hwmon_temp_max: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + val =clamp_val (val,-127000 ,127000 ); + data->xmit_buf[NCT6694_TIN_HL (channel)] =temp_to_reg (val); + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf);
drivers/hwmon/nct6694-hwmon.c line 503
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+case hwmon_temp_max_hyst: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); + + val =clamp_val (val,-127000 ,127000 ); + temp_max = (signed char )data->xmit_buf[NCT6694_TIN_HL (channel)]; + temp_hyst = (temp_max <0 ) ? (temp_max + val /1000 ) : + (temp_max - val /1000 );
DIV_ROUND_CLOSEST() ?
Quoted 183 lines
drivers/hwmon/nct6694-hwmon.c new file +183
-0+ temp_hyst =clamp_val (temp_hyst,0 ,7 ); + data->xmit_buf[NCT6694_TIN_HYST (channel)] = + (data->xmit_buf[NCT6694_TIN_HYST (channel)] & ~NCT6694_TIN_HYST_MASK ) | +FIELD_PREP (NCT6694_TIN_HYST_MASK , temp_hyst); + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_fan_write (struct device *dev,u32 attr,int channel, +long val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_fan_enable: +return nct6694_enable_channel (dev,NCT6694_FIN_EN (channel /8 ), + channel, val); +case hwmon_fan_min: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + val =clamp_val (val,1 ,65535 ); + data->xmit_buf[NCT6694_FIN_HL (channel)] = (u8)((val >>8 ) &0xFF ); + data->xmit_buf[NCT6694_FIN_LL (channel)] = (u8)(val &0xFF ); + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_pwm_write (struct device *dev,u32 attr,int channel, +long val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +int ret; + +guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_pwm_enable: +return nct6694_enable_channel (dev,NCT6694_PWM_EN (channel /8 ), + channel, val); +case hwmon_pwm_input: +if (val <0 || val >255 ) +return -EINVAL ; + + ret =nct6694_read_msg (data->nct6694,NCT6694_PWM_MOD , +NCT6694_PWM_CMD1_OFFSET , +NCT6694_PWM_CMD1_LEN , + data->xmit_buf); +if (ret) +return ret; + + data->xmit_buf[NCT6694_MAL_VAL (channel)] = val; + +return nct6694_write_msg (data->nct6694,NCT6694_PWM_MOD , +NCT6694_PWM_CMD1_OFFSET , +NCT6694_PWM_CMD1_LEN , + data->xmit_buf); +case hwmon_pwm_freq: + data->hwmon_en[NCT6694_PWM_FREQ_IDX (channel)] =NCT6694_FREQ_TO_REG (val); + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD0_OFFSET , +NCT6694_HWMON_CMD0_LEN , + data->hwmon_en); +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +switch (type) { +case hwmon_in:/* in mV */ +return nct6694_in_read (dev, attr, channel, val); +case hwmon_temp:/* in mC */ +return nct6694_temp_read (dev, attr, channel, val); +case hwmon_fan:/* in RPM */ +return nct6694_fan_read (dev, attr, channel, val); +case hwmon_pwm:/* in value 0~255 */ +return nct6694_pwm_read (dev, attr, channel, val); +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +switch (type) { +case hwmon_in: +return nct6694_in_write (dev, attr, channel, val); +case hwmon_temp: +return nct6694_temp_write (dev, attr, channel, val); +case hwmon_fan: +return nct6694_fan_write (dev, attr, channel, val); +case hwmon_pwm: +return nct6694_pwm_write (dev, attr, channel, val); +default : +return -EOPNOTSUPP ; + } +} + +static umode_t nct6694_is_visible (const void *data, +enum hwmon_sensor_types type, +u32 attr,int channel) +{ +switch (type) { +case hwmon_in: +switch (attr) { +case hwmon_in_enable: +case hwmon_in_max: +case hwmon_in_min: +return 0644 ; +case hwmon_in_alarm: +case hwmon_in_input: +return 0444 ; +default : +return 0 ; + } +case hwmon_temp: +switch (attr) { +case hwmon_temp_enable: +case hwmon_temp_max: +case hwmon_temp_max_hyst: +return 0644 ; +case hwmon_temp_input: +case hwmon_temp_max_alarm: +return 0444 ; +default : +return 0 ; + } +case hwmon_fan: +switch (attr) { +case hwmon_fan_enable: +case hwmon_fan_min: +return 0644 ; +case hwmon_fan_input: +case hwmon_fan_min_alarm: +return 0444 ; +default : +return 0 ; + } +case hwmon_pwm: +switch (attr) { +case hwmon_pwm_enable: +case hwmon_pwm_freq: +case hwmon_pwm_input: +return 0644 ; +default : +return 0 ; + } +default : +return 0 ; + } +} + +static const struct hwmon_ops nct6694_hwmon_ops = { + .is_visible = nct6694_is_visible, + .read = nct6694_read, + .write = nct6694_write, +}; + +static const struct hwmon_chip_info nct6694_chip_info = {
drivers/hwmon/nct6694-hwmon.c line 696
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+ .ops = &nct6694_hwmon_ops, + .info = nct6694_info, +}; + +static int nct6694_hwmon_init (struct nct6694_hwmon_data *data) +{ +int ret; + +guard (mutex)(&data->lock); +
Not significant, but this is unnecessary because the function is only called once from probe.
Quoted 30 lines
drivers/hwmon/nct6694-hwmon.c new file +30
-0+/* +* Record each Hardware Monitor Channel enable status +* and PWM frequency register +*/ + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD0_OFFSET , +NCT6694_HWMON_CMD0_LEN , + data->hwmon_en); +if (ret) +return ret; + +/* Set Fan input Real Time alarm mode */ + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + data->xmit_buf[NCT6694_SMI_CTRL_IDX ] =0x02 ; + +return nct6694_write_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +} + +static int nct6694_hwmon_probe (struct platform_device *pdev) +{ +struct nct6694_hwmon_data *data;
drivers/hwmon/nct6694-hwmon.c line 736
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +struct device *hwmon_dev; +int ret; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->xmit_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );
Wondering ... why not just devm_kzalloc(..,. NCT6694_MAX_PACKET_SZ, ...) ? sizeof(unsigned char) is always 1, after all.
drivers/hwmon/nct6694-hwmon.c line 742
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+if (!data->xmit_buf) +return -ENOMEM ; + + data->nct6694 = nct6694; +mutex_init (&data->lock); +platform_set_drvdata (pdev, data);
This is unnecessary unless I am missing something.
Quoted 30 lines
drivers/hwmon/nct6694-hwmon.c new file +26
-0+ + ret =nct6694_hwmon_init (data); +if (ret) +return ret; + +/* Register hwmon device to HWMON framework */ + hwmon_dev =devm_hwmon_device_register_with_info (&pdev->dev, +"nct6694" , data, + &nct6694_chip_info, +NULL ); +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +static struct platform_driver nct6694_hwmon_driver = { + .driver = { + .name ="nct6694-hwmon" , + }, + .probe = nct6694_hwmon_probe, +}; + +module_platform_driver (nct6694_hwmon_driver); + +MODULE_DESCRIPTION ("USB-HWMON driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-hwmon" );Signature · 1 line
2.34.1
Re: [PATCH v3 6/7] hwmon: Add Nuvoton NCT6694 HWMON support
Dear Guenter,
Thank you for your comments,
Guenter Roeck <[email protected]> 於 2024年12月10日 週二 下午11:58寫道:
Quoted 11 lines · Ming Yu
Quoted 4 lines
This driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>checkpatch:
WARNING: From:/Signed-off-by: email address mismatch: 'From: Ming Yu <[email protected]>' != 'Signed-off-by: Ming Yu <[email protected]>'
Fix it in v4.
...
Quoted 12 lines
drivers/hwmon/nct6694-hwmon.c line 74
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c new file +9
-0+static inline long in_from_reg (u8 reg) +{ +return reg *16 ; +} + +static inline u8 in_to_reg (long val) +{ +if (val <=0 ) +return 0 ;This is pointless since the calling code already clamps the value to [0, 2032].
Drop it in v4.
Quoted 4 lines
Quoted 1 line
+ return val / 16;
DIV_ROUND_CLOSEST()?
Fix it in v4.
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 80
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+} + +static inline long temp_from_reg (u8 reg) +{ +return reg *1000 ;This always returns a positive value, even though the temperature is supposed to be signed. More on that below.
I will modify the return value to (sign_extend32(reg, 7) * 1000) in v4.
Quoted 8 lines
drivers/hwmon/nct6694-hwmon.c line 85
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+} + +static inline u8 temp_to_reg (long val) +{ +return val /1000 ;
DIV_ROUND_CLOSEST()?
Fix it in v4.
Quoted 1 line
Quoted 1 line
+}
...
Quoted 7 lines
Quoted 7 lines
drivers/hwmon/nct6694-hwmon.c new file +7
-0+static int nct6694_in_read (struct device *dev,u32 attr,int channel, +long *val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char vin_en; +int ret; +
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 190
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_in_enable: + vin_en = data->hwmon_en[NCT6694_VIN_EN (channel /8 )]; + *val = !!(vin_en &BIT (channel %8 )) ?1 :0 ;Drop "? 1 : 0"
Drop it in v4.
Quoted 7 lines
drivers/hwmon/nct6694-hwmon.c line 196
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+ +return 0 ; +case hwmon_in_input: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_VIN_IDX (channel),1 , + data->xmit_buf);
Quoted 8 lines
I am curious: Since the received data length is not returned, and the expected minimum length is not passed to
nct6694_read_msg(), the driver has no means to check if the received data actually includes the expected values at the expected index. That doesn't matter here, but some of the returned data is located far into the buffer. Is it indeed not necessary for the driver to check if the received data is actually as long as expected ?
The fourth parameter is expected length of nct6694_read_msg(), which in this case is 1.
I will add the data length validation code for handling the response_header in both nct6694_read_msg() and nct6694_write_msg() in the next patch.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+ if (ret) + return ret;
...
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/nct6694-hwmon.c new file +8
-0+static int nct6694_temp_read (struct device *dev,u32 attr,int channel, +long *val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char temp_en, temp_hyst; +int ret, int_part, frac_part; +signed char temp_max; +
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 253
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_temp_enable: + temp_en = data->hwmon_en[NCT6694_TIN_EN (channel /8 )]; + *val = !!(temp_en &BIT (channel %8 )) ?1 :0 ;Drop "? 1 : 0"
Drop it in v4.
Quoted 22 lines
Quoted 22 lines
drivers/hwmon/nct6694-hwmon.c new file +22
-0+ +return 0 ; +case hwmon_temp_input: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_TIN_IDX (channel),2 , + data->xmit_buf); +if (ret) +return ret; + + int_part =sign_extend32 (data->xmit_buf[0 ],7 ); + frac_part =FIELD_GET (NCT6694_LSB_REG_MASK , data->xmit_buf[1 ]); +if (int_part <0 ) + *val = (int_part +1 ) *1000 - (8 - frac_part) *125 ; +else + *val = int_part *1000 + frac_part *125 ; + +return 0 ; +case hwmon_temp_max: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf);
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 280
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+if (ret) +return ret; + + *val =temp_from_reg (data->xmit_buf[NCT6694_TIN_HL (channel)]); +If the value in
NCT6694_TIN_HL()is signed, this will return a large positive value instead.
Understood! I will fix it in v4.
Quoted 14 lines
drivers/hwmon/nct6694-hwmon.c line 293
Quoted 13 lines
drivers/hwmon/nct6694-hwmon.c new file +13
-0+return 0 ; +case hwmon_temp_max_hyst: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); +if (ret) +return ret; + + temp_max = (signed char )data->xmit_buf[NCT6694_TIN_HL (channel)]; + temp_hyst =FIELD_GET (NCT6694_TIN_HYST_MASK , + data->xmit_buf[NCT6694_TIN_HYST (channel)]); +if (temp_max <0 )
Quoted 3 lines
This suggests that the temperature limit is signed, suggesting in turn that
temp_from_reg()is wrong.
Understood! I will fix it in v4.
Quoted 27 lines
Quoted 27 lines
drivers/hwmon/nct6694-hwmon.c new file +27
-0+ *val =temp_from_reg (temp_max + temp_hyst); + else + *val =temp_from_reg (temp_max - temp_hyst); + +return 0 ; +case hwmon_temp_max_alarm: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_TIN_STS (channel /8 ),1 , + data->xmit_buf); +if (ret) +return ret; + + *val = !!(data->xmit_buf[0 ] &BIT (channel %8 )); + +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_fan_read (struct device *dev,u32 attr,int channel, +long *val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char fanin_en; +int ret; +
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 326
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_fan_enable: + fanin_en = data->hwmon_en[NCT6694_FIN_EN (channel /8 )]; + *val = !!(fanin_en &BIT (channel %8 )) ?1 :0 ;Drop "? 1 : 0"
Drop it in v4.
Quoted 5 lines
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+ +return 0 ; +case hwmon_fan_input: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_FIN_IDX(channel), 2,
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 337
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+ data->xmit_buf); +if (ret) +return ret; + + *val = (data->xmit_buf[1 ] | + (data->xmit_buf[0 ] <<8 )) &0xFFFF ;The "&
0xffff" is pointless.
Fix it in v4.
Quoted 6 lines
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+ +return 0 ; +case hwmon_fan_min: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CMD2_OFFSET, + NCT6694_HWMON_CMD2_LEN,
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 349
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+ data->xmit_buf); +if (ret) +return ret; + + *val = (data->xmit_buf[NCT6694_FIN_LL (channel)] | + data->xmit_buf[NCT6694_FIN_HL (channel)] <<8 ) &0xFFFF ;Same here.
Fix it in v4.
Quoted 24 lines
Quoted 24 lines
drivers/hwmon/nct6694-hwmon.c new file +24
-0+ +return 0 ; +case hwmon_fan_min_alarm: + ret =nct6694_read_msg (data->nct6694,NCT6694_RPT_MOD , +NCT6694_FIN_STS (channel /8 ), +1 , data->xmit_buf); +if (ret) +return ret; + + *val = !!(data->xmit_buf[0 ] &BIT (channel %8 )); + +return 0 ; +default : +return -EOPNOTSUPP ; + } +} + +static int nct6694_pwm_read (struct device *dev,u32 attr,int channel, +long *val) +{ +struct nct6694_hwmon_data *data =dev_get_drvdata (dev); +unsigned char pwm_en; +int ret; +
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 379
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+guard (mutex)(&data->lock); + +switch (attr) { +case hwmon_pwm_enable: + pwm_en = data->hwmon_en[NCT6694_PWM_EN (channel /8 )]; + *val = !!(pwm_en &BIT (channel %8 )) ?1 :0 ;Drop "? 1 : 0".
Drop it in v4.
Quoted 1 line
Quoted 1 line
+
...
Quoted 11 lines
drivers/hwmon/nct6694-hwmon.c line 503
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+case hwmon_temp_max_hyst: + ret =nct6694_read_msg (data->nct6694,NCT6694_HWMON_MOD , +NCT6694_HWMON_CMD2_OFFSET , +NCT6694_HWMON_CMD2_LEN , + data->xmit_buf); + + val =clamp_val (val,-127000 ,127000 ); + temp_max = (signed char )data->xmit_buf[NCT6694_TIN_HL (channel)]; + temp_hyst = (temp_max <0 ) ? (temp_max + val /1000 ) : + (temp_max - val /1000 );
Quoted 2 lines
DIV_ROUND_CLOSEST()?
Fix it in v4.
Quoted 5 lines
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+ temp_hyst =clamp_val (temp_hyst,0 ,7 ); + data->xmit_buf[NCT6694_TIN_HYST (channel)] = + (data->xmit_buf[NCT6694_TIN_HYST (channel)] & ~NCT6694_TIN_HYST_MASK ) | +FIELD_PREP (NCT6694_TIN_HYST_MASK , temp_hyst); +
...
Quoted 11 lines
drivers/hwmon/nct6694-hwmon.c line 696
Quoted 7 lines
drivers/hwmon/nct6694-hwmon.c new file +7
-0+ +static int nct6694_hwmon_init (struct nct6694_hwmon_data *data) +{ +int ret; + +guard (mutex)(&data->lock); +Not significant, but this is unnecessary because the function is only called once from probe.
Understood! I will drop it in the next patch.
Quoted 1 line
Quoted 1 line
+ /*
...
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/nct6694-hwmon.c new file +8
-0+static int nct6694_hwmon_probe (struct platform_device *pdev) +{ +struct nct6694_hwmon_data *data; +struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +struct device *hwmon_dev; +int ret; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL );
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 736
Quoted 5 lines
drivers/hwmon/nct6694-hwmon.c new file +5
-0+if (!data) +return -ENOMEM ; + + data->xmit_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL );Wondering ... why not just devm_kzalloc(..,.
NCT6694_MAX_PACKET_SZ, ...) ? sizeof(unsigned char) is always 1, after all.
Okay, I'll fix it.
Quoted 9 lines
drivers/hwmon/nct6694-hwmon.c line 742
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+if (!data->xmit_buf) +return -ENOMEM ; + + data->nct6694 = nct6694; +mutex_init (&data->lock); +platform_set_drvdata (pdev, data);This is unnecessary unless I am missing something.
Drop it in v4.
Quoted 22 lines
Quoted 22 lines
drivers/hwmon/nct6694-hwmon.c new file +22
-0+ + ret =nct6694_hwmon_init (data); +if (ret) +return ret; + +/* Register hwmon device to HWMON framework */ + hwmon_dev =devm_hwmon_device_register_with_info (&pdev->dev, +"nct6694" , data, + &nct6694_chip_info, +NULL ); +return PTR_ERR_OR_ZERO (hwmon_dev); +} + +static struct platform_driver nct6694_hwmon_driver = { + .driver = { + .name ="nct6694-hwmon" , + }, + .probe = nct6694_hwmon_probe, +}; + +module_platform_driver (nct6694_hwmon_driver); +
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/nct6694-hwmon.c new file +4
-1+MODULE_DESCRIPTION ("USB-HWMON driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-hwmon" ); --2.34.1
Best regards,
Ming
Re: [PATCH v3 6/7] hwmon: Add Nuvoton NCT6694 HWMON support
In reply to Ming YuQuoted 146 lines · Ming Yu
This driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface.
Signed-off-by Ming Yu <[email protected]>--- MAINTAINERS | 1 + drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/nct6694-hwmon.c | 768 ++++++++++++++++++++++++++++++++++ 4 files changed, 780 insertions(+) create mode 100644 drivers/hwmon/nct6694-hwmon.cMAINTAINERS +1
-0@@ -16546,6 +16546,7 @@ M: Ming Yu <[email protected]> L: [email protected] S: Supported F: drivers/gpio/gpio-nct6694.c +F: drivers/hwmon/nct6694-hwmon.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/nct6694_canfd.cdrivers/hwmon/Kconfig +10
-0@@ -1636,6 +1636,16 @@ config SENSORS_NCT6683 This driver can also be built as a module. If so, the module will be called nct6683. +config SENSORS_NCT6694 + tristate "Nuvoton NCT6694 Hardware Monitor support" + depends on MFD_NCT6694 + help + Say Y here to support Nuvoton NCT6694 hardware monitoring + functionality. + + This driver can also be built as a module. If so, the module + will be called nct6694-hwmon. + config SENSORS_NCT6775_CORE tristate select REGMAPdrivers/hwmon/Makefile +1
-0@@ -168,6 +168,7 @@ obj-$(CONFIG_SENSORS_MLXREG_FAN) += mlxreg-fan.o obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o obj-$(CONFIG_SENSORS_MR75203) += mr75203.o obj-$(CONFIG_SENSORS_NCT6683) += nct6683.o +obj-$(CONFIG_SENSORS_NCT6694) += nct6694-hwmon.o obj-$(CONFIG_SENSORS_NCT6775_CORE) += nct6775-core.o nct6775-objs := nct6775-platform.o obj-$(CONFIG_SENSORS_NCT6775) += nct6775.odrivers/hwmon/nct6694-hwmon.c new file +83
-0@@ -0,0 +1,768 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Nuvoton NCT6694 HWMON driver based on USB interface. +* +* Copyright (C) 2024 Nuvoton Technology Corp. +*/ + +#include <linux/bitfield.h> +#include <linux/hwmon.h> +#include <linux/kernel.h> +#include <linux/mfd/core.h> +#include <linux/mfd/nct6694.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/slab.h> + +/* Host interface */ +#define NCT6694_RPT_MOD 0xFF +#define NCT6694_HWMON_MOD 0x00 +#define NCT6694_PWM_MOD 0x01 + +/* Report Channel */ +#define NCT6694_VIN_IDX (x) (0x00 + (x)) +#define NCT6694_TIN_IDX (x) \ + ({ typeof(x) (_x) = (x); \ + ((_x) < 10) ? (0x10 + ((_x) * 2)) : \ + (0x30 + (((_x) - 10) * 2)); }) +#define NCT6694_FIN_IDX (x) (0x50 + ((x) * 2)) +#define NCT6694_PWM_IDX (x) (0x70 + (x)) +#define NCT6694_VIN_STS (x) (0x68 + (x)) +#define NCT6694_TIN_STS (x) (0x6A + (x)) +#define NCT6694_FIN_STS (x) (0x6E + (x)) + +/* Message Channel*/ +/* HWMON Command */ +/* Command 00h */ +#define NCT6694_HWMON_CMD0_LEN 0x40 +#define NCT6694_HWMON_CMD0_OFFSET 0x0000/* OFFSET = SEL|CMD */ +#define NCT6694_VIN_EN (x) (0x00 + (x)) +#define NCT6694_TIN_EN (x) (0x02 + (x)) +#define NCT6694_FIN_EN (x) (0x04 + (x)) +#define NCT6694_PWM_EN (x) (0x06 + (x)) +#define NCT6694_PWM_FREQ_IDX (x) (0x30 + (x)) +/* Command 02h */ +#define NCT6694_HWMON_CMD2_LEN 0x90 +#define NCT6694_HWMON_CMD2_OFFSET 0x0002/* OFFSET = SEL|CMD */ +#define NCT6694_SMI_CTRL_IDX 0x00 +#define NCT6694_VIN_HL (x) (0x10 + ((x) * 2)) +#define NCT6694_VIN_LL (x) (0x11 + ((x) * 2)) +#define NCT6694_TIN_HYST (x) (0x30 + ((x) * 2)) +#define NCT6694_TIN_HL (x) (0x31 + ((x) * 2)) +#define NCT6694_FIN_HL (x) (0x70 + ((x) * 2)) +#define NCT6694_FIN_LL (x) (0x71 + ((x) * 2)) +/* PWM Command */ +#define NCT6694_PWM_CMD1_LEN 0x18 +#define NCT6694_PWM_CMD1_OFFSET 0x0001 +#define NCT6694_MAL_VAL (x) (0x02 + (x)) + +#define NCT6694_FREQ_FROM_REG (reg) ((reg) * 25000 / 255) +#define NCT6694_FREQ_TO_REG (val) \ + (DIV_ROUND_CLOSEST(clamp_val((val), 100, 25000) * 255, 25000)) + +#define NCT6694_LSB_REG_MASK GENMASK(7, 5) +#define NCT6694_TIN_HYST_MASK GENMASK(7, 5) + +static inline long in_from_reg (u8 reg) +{ +return reg *16 ; +} + +static inline u8 in_to_reg (long val) +{ +if (val <=0 ) +return 0 ; +return val /16 ; +} + +static inline long temp_from_reg (u8 reg) +{ +return reg *1000 ; +} + +static inline u8 temp_to_reg (long val)
drivers/hwmon/nct6694-hwmon.c line 93
Quoted 10 lines
drivers/hwmon/nct6694-hwmon.c new file +10
-0+{ +return val /1000 ; +} + +struct nct6694_hwmon_data { +struct nct6694 *nct6694; +struct mutex lock; +unsigned char *xmit_buf; +unsigned char hwmon_en[NCT6694_HWMON_CMD0_LEN ]; +};
A global comment on this series: do not declare your buffers as some opaque unsigned char arrays. Instead, make it a structure (or an array of structures if needed) using the little endian types for the different fields.
You already applied this change to the CAN driver after I made a comment, please do the same throughout the series.
The same applies with any other comments made by anyone else: do not only apply to the patch where the comment is made, but apply it broadly to the series.
Thank you.
Yours sincerely,
Vincent Mailhol
Re: [PATCH v3 6/7] hwmon: Add Nuvoton NCT6694 HWMON support
Dear Vincent,
Thank you for your comments,
I understand, I will make the modifications for these drivers,
Thanks,
Ming
Vincent Mailhol <[email protected]> 於 2024年12月13日 週五 上午12:10寫道:
Quoted 1 line
...
Quoted 24 lines
drivers/hwmon/nct6694-hwmon.c line 93
Quoted 6 lines
drivers/hwmon/nct6694-hwmon.c new file +6
-0+struct nct6694_hwmon_data { +struct nct6694 *nct6694; +struct mutex lock; +unsigned char *xmit_buf; +unsigned char hwmon_en[NCT6694_HWMON_CMD0_LEN ]; +};A global comment on this series: do not declare your buffers as some opaque unsigned char arrays. Instead, make it a structure (or an array of structures if needed) using the little endian types for the different fields.
You already applied this change to the CAN driver after I made a comment, please do the same throughout the series.
The same applies with any other comments made by anyone else: do not only apply to the patch where the comment is made, but apply it broadly to the series.
Thank you.
Yours sincerely,
Vincent Mailhol
PATCH v3 7/7 rtc: Add Nuvoton NCT6694 RTC support
In reply to Ming YuThis driver supports RTC functionality for NCT6694 MFD device based on USB interface.
--- MAINTAINERS | 1 + drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-nct6694.c | 264 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 drivers/rtc/rtc-nct6694.c
MAINTAINERS +1 -0
@@ -16550,6 +16550,7 @@ F: drivers/hwmon/nct6694-hwmon.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/nct6694_canfd.c +F: drivers/rtc/rtc-nct6694.c F: drivers/watchdog/nct6694_wdt.c F: include/linux/mfd/nct6694.h
drivers/rtc/Kconfig +10 -0
@@ -406,6 +406,16 @@ config RTC_DRV_NCT3018Y This driver can also be built as a module, if so, the module will be called "rtc-nct3018y". +config RTC_DRV_NCT6694 + tristate "Nuvoton NCT6694 RTC support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to RTC. + + This driver can also be built as a module. If so, the module will + be called rtc-nct6694. + config RTC_DRV_RK808 tristate "Rockchip RK805/RK808/RK809/RK817/RK818 RTC" depends on MFD_RK8XX
drivers/rtc/Makefile +1 -0
@@ -116,6 +116,7 @@ obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.o obj-$(CONFIG_RTC_DRV_MXC_V2) += rtc-mxc_v2.o obj-$(CONFIG_RTC_DRV_GAMECUBE) += rtc-gamecube.o obj-$(CONFIG_RTC_DRV_NCT3018Y) += rtc-nct3018y.o +obj-$(CONFIG_RTC_DRV_NCT6694) += rtc-nct6694.o obj-$(CONFIG_RTC_DRV_NTXEC) += rtc-ntxec.o obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o obj-$(CONFIG_RTC_DRV_OPAL) += rtc-opal.o
drivers/rtc/rtc-nct6694.c new file +264 -0
264 lines not on this page · raw message
Signature · 1 line
2.34.1
Alexandre Belloni
maintainerRe: [PATCH v3 7/7] rtc: Add Nuvoton NCT6694 RTC support
Quoted 19 lines · Ming Yu
drivers/rtc/rtc-nct6694.c new file +19
-0+static int nct6694_rtc_probe (struct platform_device *pdev) +{ +struct nct6694_rtc_data *data; +struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +int ret, irq; + + irq =irq_create_mapping (nct6694->domain,NCT6694_IRQ_RTC ); +if (!irq) +return -EINVAL ; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->xmit_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL ); +if (!data->xmit_buf) +return -ENOMEM ; +
drivers/rtc/rtc-nct6694.c line 236
Quoted 10 lines
drivers/rtc/rtc-nct6694.c new file +10
-0+ data->rtc =devm_rtc_allocate_device (&pdev->dev); +if (IS_ERR (data->rtc)) +return PTR_ERR (data->rtc); + + data->nct6694 = nct6694; + data->rtc->ops = &nct6694_rtc_ops; + data->rtc->range_min =RTC_TIMESTAMP_BEGIN_2000 ; + data->rtc->range_max =RTC_TIMESTAMP_END_2099 ; + +mutex_init (&data->lock);
You should use rtc_lock/rtc_unlock instead of having your own lock. The core will take and release the lock appropriately before calling the rtc_ops so you only have to do it in the irq handler.
drivers/rtc/rtc-nct6694.c line 238
Quoted 2 lines
drivers/rtc/rtc-nct6694.c new file +2
-0+ +device_set_wakeup_capable (&pdev->dev,1 );
This will cause a memory leak later on, see the discussion here:
https://lore.kernel.org/linux-rtc/[email protected]/T/#mf51fdce6036efa3ea12fe75bd5126d4ac0c6813e
Quoted 29 lines
drivers/rtc/rtc-nct6694.c new file +26
-0+ +platform_set_drvdata (pdev, data); + + ret =devm_request_threaded_irq (&pdev->dev, irq,NULL , + nct6694_irq,IRQF_ONESHOT , +"nct6694-rtc" , data); +if (ret <0 ) +return dev_err_probe (&pdev->dev, ret,"Failed to request irq\n" ); + +/* Register rtc device to RTC framework */ +return devm_rtc_register_device (data->rtc); +} + +static struct platform_driver nct6694_rtc_driver = { + .driver = { + .name ="nct6694-rtc" , + }, + .probe = nct6694_rtc_probe, +}; + +module_platform_driver (nct6694_rtc_driver); + +MODULE_DESCRIPTION ("USB-RTC driver for NCT6694" ); +MODULE_AUTHOR ("Ming Yu <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:nct6694-rtc" );Signature · 1 line
2.34.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH v3 7/7] rtc: Add Nuvoton NCT6694 RTC support
Dear Alexandre,
Thank you for your comments,
Alexandre Belloni <[email protected]> 於 2024年12月16日 週一 下午6:42寫道:
Quoted 27 lines · Ming Yu
Quoted 25 lines
drivers/rtc/rtc-nct6694.c new file +25
-0+static int nct6694_rtc_probe (struct platform_device *pdev) +{ +struct nct6694_rtc_data *data; +struct nct6694 *nct6694 =dev_get_drvdata (pdev->dev.parent); +int ret, irq; + + irq =irq_create_mapping (nct6694->domain,NCT6694_IRQ_RTC ); +if (!irq) +return -EINVAL ; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->xmit_buf =devm_kcalloc (&pdev->dev,NCT6694_MAX_PACKET_SZ , +sizeof (unsigned char ),GFP_KERNEL ); +if (!data->xmit_buf) +return -ENOMEM ; + + data->rtc =devm_rtc_allocate_device (&pdev->dev); +if (IS_ERR (data->rtc)) +return PTR_ERR (data->rtc); + + data->nct6694 = nct6694; + data->rtc->ops = &nct6694_rtc_ops;
Quoted 9 lines
drivers/rtc/rtc-nct6694.c line 236
Quoted 4 lines
drivers/rtc/rtc-nct6694.c new file +4
-0+ data->rtc->range_min =RTC_TIMESTAMP_BEGIN_2000 ; + data->rtc->range_max =RTC_TIMESTAMP_END_2099 ; + +mutex_init (&data->lock);You should use rtc_lock/rtc_unlock instead of having your own lock. The core will take and release the lock appropriately before calling the
rtc_opsso you only have to do it in the irq handler.
Understood. I will make the modifications in the next patch.
Quoted 7 lines
drivers/rtc/rtc-nct6694.c line 238
Quoted 2 lines
drivers/rtc/rtc-nct6694.c new file +2
-0+ +device_set_wakeup_capable (&pdev->dev,1 );This will cause a memory leak later on, see the discussion here:
https://lore.kernel.org/linux-rtc/[email protected]/T/#mf51fdce6036efa3ea12fe75bd5126d4ac0c6813e
Okay! I will drop it and add device_init_wakeup() in the next patch.
Best regards,
Ming