On 07/09/2018 02:01 PM, Florian Ferg wrote: > From: Florian Ferg <flfe@xxxxxxxxxxxxxxx> > > This patch adds the driver for the IXXAT USB-to-CAN interfaces. > There is an adapter for the older communication layer cl1 and another > adapter for the newer communication layer cl2. > > Signed-off-by: Florian Ferg <flfe@xxxxxxxxxxxxxxx> Please fix the following sparse errors: > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:49:19: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:49:19: expected restricted __le32 [usertype] size > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:49:19: got unsigned int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:50:19: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:50:19: expected restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:50:19: got int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:51:21: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:51:21: expected restricted __le16 [usertype] socket > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:51:21: got int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:54:23: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:54:23: expected restricted __le32 [usertype] res_size > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:54:23: got unsigned int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:56:19: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:56:19: expected restricted __le32 [usertype] code > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:56:19: got unsigned int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:171:46: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:171:46: expected unsigned short const [unsigned] [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:171:46: got restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:175:46: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:175:46: expected unsigned short const [unsigned] [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:175:46: got restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:180:34: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:180:34: expected restricted __le16 [usertype] bus_ctrl_count > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:180:34: got unsigned short [unsigned] [usertype] <noident> > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:181:33: warning: restricted __le16 degrades to integer > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:184:45: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:184:45: expected restricted __le16 <noident> > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:184:45: got unsigned short [unsigned] [usertype] type > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:210:46: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:210:46: expected unsigned short const [unsigned] [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:210:46: got restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:214:46: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:214:46: expected unsigned short const [unsigned] [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:214:46: got restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:227:47: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:227:47: expected restricted __le32 [usertype] device_fpga_version > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:227:47: got unsigned int const [unsigned] [usertype] fpgav > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:228:42: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:228:42: expected restricted __le16 [usertype] device_version > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:228:42: got unsigned short const [unsigned] [usertype] devv > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:324:46: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:324:46: expected unsigned short const [unsigned] [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:324:46: got restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:328:46: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:328:46: expected unsigned short const [unsigned] [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:328:46: got restricted __le16 [usertype] port > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:641:63: warning: incorrect type in argument 2 (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:641:63: expected unsigned int [unsigned] [usertype] ts > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:641:63: got restricted __le32 [usertype] time > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:748:41: warning: missing braces around initializer > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:752:33: warning: invalid assignment: |= > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:752:33: left side has type restricted __le32 > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:752:33: right side has type int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:755:33: warning: invalid assignment: |= > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:755:33: left side has type restricted __le32 > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:755:33: right side has type int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:756:34: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:756:34: expected restricted __le32 [usertype] msg_id > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:756:34: got unsigned int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:758:34: warning: incorrect type in assignment (different base types) > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:758:34: expected restricted __le32 [usertype] msg_id > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:758:34: got unsigned int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:762:33: warning: invalid assignment: |= > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:762:33: left side has type restricted __le32 > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:762:33: right side has type int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:765:41: warning: invalid assignment: |= > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:765:41: left side has type restricted __le32 > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:765:41: right side has type int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:767:33: warning: invalid assignment: |= > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:767:33: left side has type restricted __le32 > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:767:33: right side has type int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:769:33: warning: invalid assignment: |= > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:769:33: left side has type restricted __le32 > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:769:33: right side has type int > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:1245:33: warning: restricted __le16 degrades to integer > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c:1246:30: warning: restricted __le16 degrades to integer > --- > [v4] > Using "if (err)" instead of "if (ret < 0)" if returncode is never greater > than zero. > Using __le32 when appropriate. > Removed unnecessary preinitialisation of variables. > Defined a command structure which contains the request and the response > block of a device command. > Using a helper function to initialize device commands. > Using ktime_get_real() instead of do_gettimeofday(). > Preinitialize variables in the declaration when appropriate. > Removed unnecessary type casts. > Using a union for CL1/CL2 can messages. > Removed unnecessary restart task. > Fixed cleanup code in ixxat_usb_create_dev(). > > [v3] > Moved can message handling to ixxat_usb_core.c > > [v2] > Fixed some style issues. > Using ktime API for timestamps. > Reworked the driver to be only one module with adapters for cl1 / cl2. > CAN-IDM100 device will not be released. The driver handles CANIDM-101 now. > Added error passive recognition. (Even though current devices don't > signal it. Future devices probably will.) > > drivers/net/can/usb/Kconfig | 17 + > drivers/net/can/usb/Makefile | 1 + > drivers/net/can/usb/ixxat_usb/Makefile | 2 + > drivers/net/can/usb/ixxat_usb/ixxat_usb_cl1.c | 146 +++ > drivers/net/can/usb/ixxat_usb/ixxat_usb_cl2.c | 263 +++++ > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c | 1268 ++++++++++++++++++++++++ > drivers/net/can/usb/ixxat_usb/ixxat_usb_core.h | 531 ++++++++++ > 7 files changed, 2228 insertions(+) > create mode 100644 drivers/net/can/usb/ixxat_usb/Makefile > create mode 100644 drivers/net/can/usb/ixxat_usb/ixxat_usb_cl1.c > create mode 100644 drivers/net/can/usb/ixxat_usb/ixxat_usb_cl2.c > create mode 100644 drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c > create mode 100644 drivers/net/can/usb/ixxat_usb/ixxat_usb_core.h > > diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig > index c36f4bd..ca21974 100644 > --- a/drivers/net/can/usb/Kconfig > +++ b/drivers/net/can/usb/Kconfig > @@ -89,4 +89,21 @@ config CAN_MCBA_USB > This driver supports the CAN BUS Analyzer interface > from Microchip (http://www.microchip.com/development-tools/). > > +config CAN_IXXAT_USB > + tristate "IXXAT USB-to-CAN interfaces" > + ---help--- > + This driver adds support for IXXAT USB-to-CAN devices. > + > + The driver provides support for the following devices: > + - IXXAT USB-to-CAN compact > + - IXXAT USB-to-CAN embedded > + - IXXAT USB-to-CAN professional > + - IXXAT USB-to-CAN automotive > + - IXXAT USB-to-CAN FD compact > + - IXXAT USB-to-CAN FD professional > + - IXXAT USB-to-CAN FD automotive > + - IXXAT USB-to-CAN FD MiniPCIe > + - IXXAT USB-to-CAR > + - IXXAT CAN-IDM101 > + > endmenu > diff --git a/drivers/net/can/usb/Makefile b/drivers/net/can/usb/Makefile > index 49ac7b9..9d63b8d 100644 > --- a/drivers/net/can/usb/Makefile > +++ b/drivers/net/can/usb/Makefile > @@ -10,3 +10,4 @@ obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o > obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ > obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o > obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o > +obj-$(CONFIG_CAN_IXXAT_USB) += ixxat_usb/ > diff --git a/drivers/net/can/usb/ixxat_usb/Makefile b/drivers/net/can/usb/ixxat_usb/Makefile > new file mode 100644 > index 0000000..125d270 > --- /dev/null > +++ b/drivers/net/can/usb/ixxat_usb/Makefile > @@ -0,0 +1,2 @@ > +obj-$(CONFIG_CAN_IXXAT_USB) += ixxat_usb2can.o > +ixxat_usb2can-y = ixxat_usb_cl1.o ixxat_usb_cl2.o ixxat_usb_core.o > diff --git a/drivers/net/can/usb/ixxat_usb/ixxat_usb_cl1.c b/drivers/net/can/usb/ixxat_usb/ixxat_usb_cl1.c > new file mode 100644 > index 0000000..b6f4d11 > --- /dev/null > +++ b/drivers/net/can/usb/ixxat_usb/ixxat_usb_cl1.c > @@ -0,0 +1,146 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +/* CAN driver adapter for IXXAT USB-to-CAN CL1 > + * > + * Copyright (C) 2018 HMS Industrial Networks <socketcan@xxxxxxxxxxxxxxx> > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms of the GNU General Public License as published > + * by the Free Software Foundation; version 2 of the License. > + * > + * This program is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + */ > + > +#include <linux/kernel.h> > +#include <linux/can/dev.h> > +#include <linux/usb.h> > + > +#include "ixxat_usb_core.h" > + > +#define IXXAT_USB_CLOCK 8000000 > + > +#define IXXAT_USB_BUFFER_SIZE_RX 512 > +#define IXXAT_USB_BUFFER_SIZE_TX 256 > + > +#define IXXAT_USB_MODES (CAN_CTRLMODE_3_SAMPLES | \ > + CAN_CTRLMODE_BERR_REPORTING | \ > + CAN_CTRLMODE_LISTENONLY) \ > + > +#define IXXAT_USB_BTMODE_TSM_CL1 0x80 > + > +/* bittiming parameters */ > +#define IXXAT_USB2CAN_TSEG1_MIN 1 > +#define IXXAT_USB2CAN_TSEG1_MAX 16 > +#define IXXAT_USB2CAN_TSEG2_MIN 1 > +#define IXXAT_USB2CAN_TSEG2_MAX 8 > +#define IXXAT_USB2CAN_SJW_MAX 4 > +#define IXXAT_USB2CAN_BRP_MIN 1 > +#define IXXAT_USB2CAN_BRP_MAX 64 > +#define IXXAT_USB2CAN_BRP_INC 1 > + > +/* USB endpoint mapping for CL1 */ > +#define IXXAT_USB2CAN_EP1_IN (1 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP2_IN (2 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP3_IN (3 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP4_IN (4 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP5_IN (5 | USB_DIR_IN) > + > +#define IXXAT_USB2CAN_EP1_OUT (1 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP2_OUT (2 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP3_OUT (3 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP4_OUT (4 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP5_OUT (5 | USB_DIR_OUT) > + > +#define IXXAT_USB_CAN_CMD_INIT 0x325 > + > +static const struct can_bittiming_const usb2can_bt = { > + .name = IXXAT_USB_CTRL_NAME, > + .tseg1_min = IXXAT_USB2CAN_TSEG1_MIN, > + .tseg1_max = IXXAT_USB2CAN_TSEG1_MAX, > + .tseg2_min = IXXAT_USB2CAN_TSEG2_MIN, > + .tseg2_max = IXXAT_USB2CAN_TSEG2_MAX, > + .sjw_max = IXXAT_USB2CAN_SJW_MAX, > + .brp_min = IXXAT_USB2CAN_BRP_MIN, > + .brp_max = IXXAT_USB2CAN_BRP_MAX, > + .brp_inc = IXXAT_USB2CAN_BRP_INC, > +}; > + > +static int ixxat_usb_init_ctrl(struct ixxat_usb_device *dev) > +{ > + const struct can_bittiming *bt = &dev->can.bittiming; > + const u16 port = dev->ctrl_index; > + int ret; > + u8 opmode = IXXAT_USB_OPMODE_EXTENDED | IXXAT_USB_OPMODE_STANDARD; > + > + /* Bittiming calculation from kernel > + * (see can-utils/can-calc-bit-timing.c) > + */ I think that comment is not needed. We are actually _in_ the kernel and the struct is defined and documented in include/uapi/linux/can/netlink.h > + u8 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); > + u8 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | > + (((bt->phase_seg2 - 1) & 0x7) << 4); > + > + struct ixxat_usb_init_cl1_cmd *cmd; > + const u32 rcv_size = sizeof(cmd->res); > + const u32 snd_size = sizeof(*cmd); Please move all variable declarations to the beginning of the function. > + > + cmd = kmalloc(snd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) > + btr1 |= IXXAT_USB_BTMODE_TSM_CL1; > + if (dev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) > + opmode |= IXXAT_USB_OPMODE_ERRFRAME; > + if (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) > + opmode |= IXXAT_USB_OPMODE_LISTONLY; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.size = cpu_to_le32(snd_size - rcv_size); > + cmd->req.code = cpu_to_le32(IXXAT_USB_CAN_CMD_INIT); > + cmd->req.port = cpu_to_le16(port); > + cmd->mode = opmode; > + cmd->btr0 = btr0; > + cmd->btr1 = btr1; > + > + ret = ixxat_usb_snd_cmd(dev->udev, port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev->udev, port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +const struct ixxat_usb_adapter usb2can_cl1 = { > + .clock = IXXAT_USB_CLOCK, > + .bt = &usb2can_bt, > + .btd = NULL, not needed... > + .modes = IXXAT_USB_MODES, > + .buffer_size_rx = IXXAT_USB_BUFFER_SIZE_RX, > + .buffer_size_tx = IXXAT_USB_BUFFER_SIZE_TX, > + .ep_msg_in = { > + IXXAT_USB2CAN_EP1_IN, > + IXXAT_USB2CAN_EP2_IN, > + IXXAT_USB2CAN_EP3_IN, > + IXXAT_USB2CAN_EP4_IN, > + IXXAT_USB2CAN_EP5_IN > + }, > + .ep_msg_out = { > + IXXAT_USB2CAN_EP1_OUT, > + IXXAT_USB2CAN_EP2_OUT, > + IXXAT_USB2CAN_EP3_OUT, > + IXXAT_USB2CAN_EP4_OUT, > + IXXAT_USB2CAN_EP5_OUT > + }, > + .ep_offs = 0, not needed... but you might keep them for documentation purposes. > + .init_ctrl = ixxat_usb_init_ctrl > +}; > diff --git a/drivers/net/can/usb/ixxat_usb/ixxat_usb_cl2.c b/drivers/net/can/usb/ixxat_usb/ixxat_usb_cl2.c > new file mode 100644 > index 0000000..9b89878 > --- /dev/null > +++ b/drivers/net/can/usb/ixxat_usb/ixxat_usb_cl2.c > @@ -0,0 +1,263 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +/* CAN driver adapter for IXXAT USB-to-CAN CL2 > + * > + * Copyright (C) 2018 HMS Industrial Networks <socketcan@xxxxxxxxxxxxxxx> > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms of the GNU General Public License as published > + * by the Free Software Foundation; version 2 of the License. > + * > + * This program is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + */ > + > +#include <linux/kernel.h> > +#include <linux/can/dev.h> > +#include <linux/usb.h> > + > +#include "ixxat_usb_core.h" > + > +#define IXXAT_USB_CLOCK 80000000 > + > +#define IXXAT_USB_BUFFER_SIZE_RX 512 > +#define IXXAT_USB_BUFFER_SIZE_TX 512 > + > +#define IXXAT_USB_MODES (CAN_CTRLMODE_3_SAMPLES | \ > + CAN_CTRLMODE_LISTENONLY | \ > + CAN_CTRLMODE_BERR_REPORTING | \ > + CAN_CTRLMODE_FD | \ > + CAN_CTRLMODE_FD_NON_ISO) > + > +/* bittiming parameters CL2 */ > +#define IXXAT_USB2CAN_TSEG1_MIN 1 > +#define IXXAT_USB2CAN_TSEG1_MAX 256 > +#define IXXAT_USB2CAN_TSEG2_MIN 1 > +#define IXXAT_USB2CAN_TSEG2_MAX 256 > +#define IXXAT_USB2CAN_SJW_MAX 128 > +#define IXXAT_USB2CAN_BRP_MIN 2 > +#define IXXAT_USB2CAN_BRP_MAX 513 > +#define IXXAT_USB2CAN_BRP_INC 1 > + > +#define IXXAT_USB2CAN_TSEG1_MIN_DATA 1 > +#define IXXAT_USB2CAN_TSEG1_MAX_DATA 256 > +#define IXXAT_USB2CAN_TSEG2_MIN_DATA 1 > +#define IXXAT_USB2CAN_TSEG2_MAX_DATA 256 > +#define IXXAT_USB2CAN_SJW_MAX_DATA 128 > +#define IXXAT_USB2CAN_BRP_MIN_DATA 2 > +#define IXXAT_USB2CAN_BRP_MAX_DATA 513 > +#define IXXAT_USB2CAN_BRP_INC_DATA 1 > + > +/* bittiming parameters CAN IDM */ > +#define IXXAT_CANIDM_TSEG1_MIN 1 > +#define IXXAT_CANIDM_TSEG1_MAX 256 > +#define IXXAT_CANIDM_TSEG2_MIN 1 > +#define IXXAT_CANIDM_TSEG2_MAX 128 > +#define IXXAT_CANIDM_SJW_MAX 128 > +#define IXXAT_CANIDM_BRP_MIN 1 > +#define IXXAT_CANIDM_BRP_MAX 512 > +#define IXXAT_CANIDM_BRP_INC 1 > + > +#define IXXAT_CANIDM_TSEG1_MIN_DATA 1 > +#define IXXAT_CANIDM_TSEG1_MAX_DATA 32 > +#define IXXAT_CANIDM_TSEG2_MIN_DATA 1 > +#define IXXAT_CANIDM_TSEG2_MAX_DATA 16 > +#define IXXAT_CANIDM_SJW_MAX_DATA 8 > +#define IXXAT_CANIDM_BRP_MIN_DATA 1 > +#define IXXAT_CANIDM_BRP_MAX_DATA 32 > +#define IXXAT_CANIDM_BRP_INC_DATA 1 > + > +/* USB endpoint mapping for CL2 */ > +#define IXXAT_USB2CAN_EP1_IN (1 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP2_IN (2 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP3_IN (3 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP4_IN (4 | USB_DIR_IN) > +#define IXXAT_USB2CAN_EP5_IN (5 | USB_DIR_IN) > + > +#define IXXAT_USB2CAN_EP1_OUT (1 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP2_OUT (2 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP3_OUT (3 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP4_OUT (4 | USB_DIR_OUT) > +#define IXXAT_USB2CAN_EP5_OUT (5 | USB_DIR_OUT) > + > +/* USB endpoint mapping for CAN IDM */ > +#define IXXAT_CANIDM_EP1_IN (2 | USB_DIR_IN) > +#define IXXAT_CANIDM_EP2_IN (4 | USB_DIR_IN) > +#define IXXAT_CANIDM_EP3_IN (6 | USB_DIR_IN) > +#define IXXAT_CANIDM_EP4_IN (8 | USB_DIR_IN) > +#define IXXAT_CANIDM_EP5_IN (10 | USB_DIR_IN) > + > +#define IXXAT_CANIDM_EP1_OUT (1 | USB_DIR_OUT) > +#define IXXAT_CANIDM_EP2_OUT (3 | USB_DIR_OUT) > +#define IXXAT_CANIDM_EP3_OUT (5 | USB_DIR_OUT) > +#define IXXAT_CANIDM_EP4_OUT (7 | USB_DIR_OUT) > +#define IXXAT_CANIDM_EP5_OUT (9 | USB_DIR_OUT) > + > +#define IXXAT_USB_CAN_CMD_INIT 0x337 > + > +static const struct can_bittiming_const usb2can_bt = { > + .name = IXXAT_USB_CTRL_NAME, > + .tseg1_min = IXXAT_USB2CAN_TSEG1_MIN, > + .tseg1_max = IXXAT_USB2CAN_TSEG1_MAX, > + .tseg2_min = IXXAT_USB2CAN_TSEG2_MIN, > + .tseg2_max = IXXAT_USB2CAN_TSEG2_MAX, > + .sjw_max = IXXAT_USB2CAN_SJW_MAX, > + .brp_min = IXXAT_USB2CAN_BRP_MIN, > + .brp_max = IXXAT_USB2CAN_BRP_MAX, > + .brp_inc = IXXAT_USB2CAN_BRP_INC, > +}; > + > +static const struct can_bittiming_const usb2can_btd = { > + .name = IXXAT_USB_CTRL_NAME, > + .tseg1_min = IXXAT_USB2CAN_TSEG1_MIN_DATA, > + .tseg1_max = IXXAT_USB2CAN_TSEG1_MAX_DATA, > + .tseg2_min = IXXAT_USB2CAN_TSEG2_MIN_DATA, > + .tseg2_max = IXXAT_USB2CAN_TSEG2_MAX_DATA, > + .sjw_max = IXXAT_USB2CAN_SJW_MAX_DATA, > + .brp_min = IXXAT_USB2CAN_BRP_MIN_DATA, > + .brp_max = IXXAT_USB2CAN_BRP_MAX_DATA, > + .brp_inc = IXXAT_USB2CAN_BRP_INC_DATA, > +}; > + > +static const struct can_bittiming_const canidm_bt = { > + .name = IXXAT_USB_CTRL_NAME, > + .tseg1_min = IXXAT_CANIDM_TSEG1_MIN, > + .tseg1_max = IXXAT_CANIDM_TSEG1_MAX, > + .tseg2_min = IXXAT_CANIDM_TSEG2_MIN, > + .tseg2_max = IXXAT_CANIDM_TSEG2_MAX, > + .sjw_max = IXXAT_CANIDM_SJW_MAX, > + .brp_min = IXXAT_CANIDM_BRP_MIN, > + .brp_max = IXXAT_CANIDM_BRP_MAX, > + .brp_inc = IXXAT_CANIDM_BRP_INC > +}; > + > +static const struct can_bittiming_const canidm_btd = { > + .name = IXXAT_USB_CTRL_NAME, > + .tseg1_min = IXXAT_CANIDM_TSEG1_MIN_DATA, > + .tseg1_max = IXXAT_CANIDM_TSEG1_MAX_DATA, > + .tseg2_min = IXXAT_CANIDM_TSEG2_MIN_DATA, > + .tseg2_max = IXXAT_CANIDM_TSEG2_MAX_DATA, > + .sjw_max = IXXAT_CANIDM_SJW_MAX_DATA, > + .brp_min = IXXAT_CANIDM_BRP_MIN_DATA, > + .brp_max = IXXAT_CANIDM_BRP_MAX_DATA, > + .brp_inc = IXXAT_CANIDM_BRP_INC_DATA > +}; > + > +static int ixxat_usb_init_ctrl(struct ixxat_usb_device *dev) > +{ > + const struct can_bittiming *bt = &dev->can.bittiming; > + const struct can_bittiming *btd = &dev->can.data_bittiming; > + const u16 port = dev->ctrl_index; > + int ret; > + u32 btmode = IXXAT_USB_BTMODE_NAT; > + u8 exmode = 0; > + u8 opmode = IXXAT_USB_OPMODE_EXTENDED | IXXAT_USB_OPMODE_STANDARD; > + > + struct ixxat_usb_init_cl2_cmd *cmd; > + const u32 rcv_size = sizeof(cmd->res); > + const u32 snd_size = sizeof(*cmd); same here > + > + cmd = kmalloc(snd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) > + btmode = IXXAT_USB_BTMODE_TSM; > + if (dev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) > + opmode |= IXXAT_USB_OPMODE_ERRFRAME; > + if (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) > + opmode |= IXXAT_USB_OPMODE_LISTONLY; > + if ((CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO) & dev->can.ctrlmode) > + exmode |= IXXAT_USB_EXMODE_EXTDATA | IXXAT_USB_EXMODE_FASTDATA; > + if (!(CAN_CTRLMODE_FD_NON_ISO & dev->can.ctrlmode) && exmode) > + exmode |= IXXAT_USB_EXMODE_ISOFD; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.size = cpu_to_le32(snd_size - rcv_size); > + cmd->req.code = cpu_to_le32(IXXAT_USB_CAN_CMD_INIT); > + cmd->req.port = cpu_to_le16(port); > + cmd->opmode = opmode; > + cmd->exmode = exmode; > + cmd->sdr.mode = cpu_to_le32(btmode); > + cmd->sdr.bps = cpu_to_le32(bt->brp); > + cmd->sdr.ts1 = cpu_to_le16(bt->prop_seg + bt->phase_seg1); > + cmd->sdr.ts2 = cpu_to_le16(bt->phase_seg2); > + cmd->sdr.sjw = cpu_to_le16(bt->sjw); > + cmd->sdr.tdo = 0; > + > + if (exmode) { > + cmd->fdr.mode = cpu_to_le32(btmode); > + cmd->fdr.bps = cpu_to_le32(btd->brp); > + cmd->fdr.ts1 = cpu_to_le16(btd->prop_seg + btd->phase_seg1); > + cmd->fdr.ts2 = cpu_to_le16(btd->phase_seg2); > + cmd->fdr.sjw = cpu_to_le16(btd->sjw); > + cmd->fdr.tdo = cpu_to_le16(btd->brp * (btd->phase_seg1 + 1 + > + btd->prop_seg)); > + } > + > + ret = ixxat_usb_snd_cmd(dev->udev, port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev->udev, port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +const struct ixxat_usb_adapter usb2can_cl2 = { > + .clock = IXXAT_USB_CLOCK, > + .bt = &usb2can_bt, > + .btd = &usb2can_btd, > + .modes = IXXAT_USB_MODES, > + .buffer_size_rx = IXXAT_USB_BUFFER_SIZE_RX, > + .buffer_size_tx = IXXAT_USB_BUFFER_SIZE_TX, > + .ep_msg_in = { > + IXXAT_USB2CAN_EP1_IN, > + IXXAT_USB2CAN_EP2_IN, > + IXXAT_USB2CAN_EP3_IN, > + IXXAT_USB2CAN_EP4_IN, > + IXXAT_USB2CAN_EP5_IN > + }, > + .ep_msg_out = { > + IXXAT_USB2CAN_EP1_OUT, > + IXXAT_USB2CAN_EP2_OUT, > + IXXAT_USB2CAN_EP3_OUT, > + IXXAT_USB2CAN_EP4_OUT, > + IXXAT_USB2CAN_EP5_OUT > + }, > + .ep_offs = 1, > + .init_ctrl = ixxat_usb_init_ctrl > +}; > + > +const struct ixxat_usb_adapter can_idm = { > + .clock = IXXAT_USB_CLOCK, > + .bt = &canidm_bt, > + .btd = &canidm_btd, > + .modes = IXXAT_USB_MODES, > + .buffer_size_rx = IXXAT_USB_BUFFER_SIZE_RX, > + .buffer_size_tx = IXXAT_USB_BUFFER_SIZE_TX, > + .ep_msg_in = { > + IXXAT_CANIDM_EP1_IN, > + IXXAT_CANIDM_EP2_IN, > + IXXAT_CANIDM_EP3_IN, > + IXXAT_CANIDM_EP4_IN, > + IXXAT_CANIDM_EP5_IN > + }, > + .ep_msg_out = { > + IXXAT_CANIDM_EP1_OUT, > + IXXAT_CANIDM_EP2_OUT, > + IXXAT_CANIDM_EP3_OUT, > + IXXAT_CANIDM_EP4_OUT, > + IXXAT_CANIDM_EP5_OUT > + }, > + .ep_offs = 0, > + .init_ctrl = ixxat_usb_init_ctrl > +}; > diff --git a/drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c b/drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c > new file mode 100644 > index 0000000..cbbb348 > --- /dev/null > +++ b/drivers/net/can/usb/ixxat_usb/ixxat_usb_core.c > @@ -0,0 +1,1268 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +/* CAN driver for IXXAT USB-to-CAN > + * > + * Copyright (C) 2018 HMS Industrial Networks <socketcan@xxxxxxxxxxxxxxx> > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms of the GNU General Public License as published > + * by the Free Software Foundation; version 2 of the License. > + * > + * This program is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + */ > + > +#include <linux/module.h> > +#include <linux/kernel.h> > +#include <linux/can/dev.h> > +#include <linux/kthread.h> > +#include <linux/usb.h> > + > +#include "ixxat_usb_core.h" > + > +MODULE_AUTHOR("Marcel Schmidt <socketcan@xxxxxxxxxxxxxxx>"); > +MODULE_DESCRIPTION("CAN driver for IXXAT USB-to-CAN FD adapters"); > +MODULE_LICENSE("GPL v2"); > + > +/* Table of devices that work with this driver */ > +static const struct usb_device_id ixxat_usb_table[] = { > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_COMPACT_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_EMBEDDED_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_PROFESSIONAL_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_AUTOMOTIVE_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_FD_COMPACT_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_FD_PROFESSIONAL_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_FD_AUTOMOTIVE_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAN_FD_PCIE_MINI_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, USB2CAR_PRODUCT_ID) }, > + { USB_DEVICE(IXXAT_USB_VENDOR_ID, CAN_IDM101_PRODUCT_ID) }, > + { } /* Terminating entry */ > +}; > + > +MODULE_DEVICE_TABLE(usb, ixxat_usb_table); > + > +void ixxat_usb_setup_cmd(struct ixxat_usb_dal_req *req, > + struct ixxat_usb_dal_res *res) > +{ > + req->size = sizeof(*req); > + req->port = 0xffff; > + req->socket = 0xffff; > + req->code = 0; > + > + res->res_size = sizeof(*res); > + res->ret_size = 0; > + res->code = 0xffffffff; > +} > + > +int ixxat_usb_snd_cmd(struct usb_device *dev, const u16 port, void *buf, > + const u16 size) > +{ > + const int to = msecs_to_jiffies(IXXAT_USB_MSG_TIMEOUT); > + const u8 rq = 0xff; > + const u8 rt = USB_TYPE_VENDOR | USB_DIR_OUT; > + int i; > + int ret = 0; > + int scp = usb_sndctrlpipe(dev, 0); > + > + for (i = 0; i < IXXAT_USB_MAX_COM_REQ; ++i) { > + ret = usb_control_msg(dev, scp, rq, rt, port, 0, buf, size, to); > + if (ret < 0) > + msleep(IXXAT_USB_MSG_CYCLE); > + else > + break; > + } > + > + if (ret < 0) > + dev_info(&dev->dev, "Error %d: TX command failure\n", ret); > + > + return ret; > +} > + > +int ixxat_usb_rcv_cmd(struct usb_device *dev, const u16 port, void *buf, > + const u16 size) > +{ > + const int to = msecs_to_jiffies(IXXAT_USB_MSG_TIMEOUT); > + const u8 rq = 0xff; > + const u8 rt = USB_TYPE_VENDOR | USB_DIR_IN; > + int i; > + int pos = 0; > + int rcp = usb_rcvctrlpipe(dev, 0); > + int ret = 0; > + > + for (i = 0; i < IXXAT_USB_MAX_COM_REQ; ++i) { > + void *rb = buf + pos; > + const int rs = size - pos; > + > + ret = usb_control_msg(dev, rcp, rq, rt, port, 0, rb, rs, to); > + if (ret < 0) { > + msleep(IXXAT_USB_MSG_CYCLE); > + continue; > + } > + > + pos += ret; > + if (pos < size) > + msleep(IXXAT_USB_MSG_CYCLE); > + else > + break; > + } > + > + if (pos != size) > + ret = -EBADMSG; > + > + if (ret < 0) > + dev_info(&dev->dev, "Error %d: RX command failure\n", ret); > + > + return ret; > +} > + > +static void ixxat_usb_update_ts_now(struct ixxat_usb_device *dev, u32 ts_now) > +{ > + u32 *ts_dev = &dev->time_ref.ts_dev_0; > + ktime_t *kt_host = &dev->time_ref.kt_host_0; > + u64 timebase = (u64)0x00000000FFFFFFFF - (u64)(*ts_dev) + (u64)ts_now; > + > + *kt_host = ktime_add_us(*kt_host, timebase); > + *ts_dev = ts_now; > +} > + > +static void ixxat_usb_get_ts_tv(struct ixxat_usb_device *dev, u32 ts, > + ktime_t *k_time) > +{ > + ktime_t tmp_time = dev->time_ref.kt_host_0; > + > + if (ts < dev->time_ref.ts_dev_last) > + ixxat_usb_update_ts_now(dev, ts); > + > + dev->time_ref.ts_dev_last = ts; > + tmp_time = ktime_add_us(tmp_time, ts - dev->time_ref.ts_dev_0); > + > + if (k_time) > + *k_time = tmp_time; > +} > + > +static void ixxat_usb_set_ts_now(struct ixxat_usb_device *dev, u32 ts_now) > +{ > + dev->time_ref.ts_dev_0 = ts_now; > + dev->time_ref.kt_host_0 = ktime_get_real(); > + dev->time_ref.ts_dev_last = ts_now; > +} > + > +static int ixxat_usb_get_dev_caps(struct usb_device *dev, > + struct ixxat_dev_caps *dev_caps) > +{ > + int i; > + int ret; > + struct ixxat_usb_caps_cmd *cmd; > + const u32 cmd_size = sizeof(*cmd); > + const u32 req_size = sizeof(cmd->req); > + const u32 rcv_size = cmd_size - req_size; > + const u32 snd_size = req_size + sizeof(cmd->res); > + > + cmd = kmalloc(cmd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.code = cpu_to_le32(IXXAT_USB_BRD_CMD_GET_DEVCAPS); > + cmd->res.res_size = cpu_to_le32(rcv_size); > + > + ret = ixxat_usb_snd_cmd(dev, cmd->req.port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev, cmd->req.port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = 0; > + dev_caps->bus_ctrl_count = le16_to_cpu(cmd->caps.bus_ctrl_count); > + for (i = 0; i < dev_caps->bus_ctrl_count; ++i) { > + u16 type = le16_to_cpu(cmd->caps.bus_ctrl_types[i]); > + > + dev_caps->bus_ctrl_types[i] = type; > + } > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +static int ixxat_usb_get_dev_info(struct usb_device *dev, > + struct ixxat_dev_info *dev_info) > +{ > + int ret; > + struct ixxat_usb_info_cmd *cmd; > + const u32 cmd_size = sizeof(*cmd); > + const u32 req_size = sizeof(cmd->req); > + const u32 rcv_size = cmd_size - req_size; > + const u32 snd_size = req_size + sizeof(cmd->res); > + > + cmd = kmalloc(cmd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.code = cpu_to_le32(IXXAT_USB_BRD_CMD_GET_DEVINFO); > + cmd->res.res_size = cpu_to_le32(rcv_size); > + > + ret = ixxat_usb_snd_cmd(dev, cmd->req.port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev, cmd->req.port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + if (dev_info) { > + const size_t id_size = sizeof(cmd->info.device_id); > + const size_t nm_size = sizeof(cmd->info.device_name); > + const u32 fpgav = le32_to_cpu(cmd->info.device_fpga_version); > + const u16 devv = le16_to_cpu(cmd->info.device_version); > + > + memcpy(dev_info->device_id, &cmd->info.device_id, id_size); > + memcpy(dev_info->device_name, &cmd->info.device_name, nm_size); > + dev_info->device_fpga_version = fpgav; > + dev_info->device_version = devv; > + } > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +static int ixxat_usb_start_ctrl(struct ixxat_usb_device *dev, u32 *time_ref) > +{ > + const u16 port = dev->ctrl_index; > + int ret; > + struct ixxat_usb_start_cmd *cmd; > + const u32 cmd_size = sizeof(*cmd); > + const u32 req_size = sizeof(cmd->req); > + const u32 rcv_size = cmd_size - req_size; > + const u32 snd_size = req_size + sizeof(cmd->res); > + > + cmd = kmalloc(cmd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.code = cpu_to_le32(IXXAT_USB_CAN_CMD_START); > + cmd->req.port = cpu_to_le16(port); > + cmd->res.res_size = cpu_to_le32(rcv_size); > + cmd->time = 0; > + > + ret = ixxat_usb_snd_cmd(dev->udev, port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev->udev, port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + if (time_ref) > + *time_ref = le32_to_cpu(cmd->time); > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +static int ixxat_usb_stop_ctrl(struct ixxat_usb_device *dev) > +{ > + const u16 port = dev->ctrl_index; > + int ret; > + struct ixxat_usb_stop_cmd *cmd; > + const u32 rcv_size = sizeof(cmd->res); > + const u32 snd_size = sizeof(*cmd); > + > + cmd = kmalloc(snd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.size = cpu_to_le32(snd_size - rcv_size); > + cmd->req.code = cpu_to_le32(IXXAT_USB_CAN_CMD_STOP); > + cmd->req.port = cpu_to_le16(port); > + cmd->action = cpu_to_le32(IXXAT_USB_STOP_ACTION_CLEARALL); > + > + ret = ixxat_usb_snd_cmd(dev->udev, port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev->udev, port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + if (!ret) > + dev->can.state = CAN_STATE_STOPPED; > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +static int ixxat_usb_power_ctrl(struct usb_device *dev, u8 mode) > +{ > + int ret; > + struct ixxat_usb_power_cmd *cmd; > + const u32 rcv_size = sizeof(cmd->res); > + const u32 snd_size = sizeof(*cmd); > + > + cmd = kmalloc(snd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.size = cpu_to_le32(snd_size - rcv_size); > + cmd->req.code = cpu_to_le32(IXXAT_USB_BRD_CMD_POWER); > + cmd->mode = mode; > + > + ret = ixxat_usb_snd_cmd(dev, cmd->req.port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev, cmd->req.port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +static int ixxat_usb_reset_ctrl(struct ixxat_usb_device *dev) > +{ > + const u16 port = dev->ctrl_index; > + int ret; > + struct ixxat_usb_dal_cmd *cmd; > + const u32 snd_size = sizeof(*cmd); > + const u32 rcv_size = sizeof(cmd->res); > + > + cmd = kmalloc(snd_size, GFP_KERNEL); > + if (!cmd) > + return -ENOMEM; > + > + ixxat_usb_setup_cmd(&cmd->req, &cmd->res); > + cmd->req.code = cpu_to_le32(IXXAT_USB_CAN_CMD_RESET); > + cmd->req.port = cpu_to_le16(port); > + > + ret = ixxat_usb_snd_cmd(dev->udev, port, cmd, snd_size); > + if (ret < 0) > + goto fail; > + > + ret = ixxat_usb_rcv_cmd(dev->udev, port, &cmd->res, rcv_size); > + if (ret < 0) > + goto fail; > + > + ret = le32_to_cpu(cmd->res.code); > + > +fail: > + kfree(cmd); > + return ret; > +} > + > +static void ixxat_usb_unlink_all_urbs(struct ixxat_usb_device *dev) > +{ > + usb_kill_anchored_urbs(&dev->rx_submitted); > + usb_kill_anchored_urbs(&dev->tx_submitted); > + atomic_set(&dev->active_tx_urbs, 0); > +} > + > +static int ixxat_usb_set_mode(struct net_device *netdev, enum can_mode mode) > +{ > + struct ixxat_usb_device *dev = netdev_priv(netdev); > + u32 time_ref; > + > + switch (mode) { > + case CAN_MODE_START: > + ixxat_usb_stop_ctrl(dev); > + ixxat_usb_start_ctrl(dev, &time_ref); > + break; > + case CAN_MODE_STOP: > + case CAN_MODE_SLEEP: > + default: > + return -EOPNOTSUPP; > + } > + > + return 0; > +} > + > +static int ixxat_usb_get_berr_counter(const struct net_device *netdev, > + struct can_berr_counter *bec) > +{ > + struct ixxat_usb_device *dev = netdev_priv(netdev); > + > + *bec = dev->bec; > + return 0; > +} > + > +static int ixxat_usb_handle_canmsg(struct ixxat_usb_device *dev, > + struct ixxat_can_msg *rx) > +{ > + struct net_device *netdev = dev->netdev; > + struct canfd_frame *cf; > + struct sk_buff *skb; > + const u32 flags = le32_to_cpu(rx->base.flags); > + const u8 dlc = IXXAT_USB_DECODE_DLC(flags); > + > + if (flags & IXXAT_USB_FDMSG_FLAGS_EDL) > + skb = alloc_canfd_skb(netdev, &cf); > + else > + skb = alloc_can_skb(netdev, (struct can_frame **)&cf); > + > + if (!skb) > + return -ENOMEM; > + > + if (flags & IXXAT_USB_FDMSG_FLAGS_EDL) { > + if (flags & IXXAT_USB_FDMSG_FLAGS_FDR) > + cf->flags |= CANFD_BRS; > + > + if (flags & IXXAT_USB_FDMSG_FLAGS_ESI) > + cf->flags |= CANFD_ESI; > + > + cf->len = can_dlc2len(get_canfd_dlc(dlc)); > + } else { > + cf->len = get_can_dlc(dlc); > + } > + > + if (flags & IXXAT_USB_MSG_FLAGS_OVR) { > + netdev->stats.rx_over_errors++; > + netdev->stats.rx_errors++; > + netdev_err(netdev, "Error: Message overflow\n"); > + } > + > + cf->can_id = le32_to_cpu(rx->base.msg_id); > + > + if (flags & IXXAT_USB_MSG_FLAGS_EXT) > + cf->can_id |= CAN_EFF_FLAG; > + > + if (flags & IXXAT_USB_MSG_FLAGS_RTR) { > + cf->can_id |= CAN_RTR_FLAG; > + } else { > + if (dev->adapter == &usb2can_cl1) > + memcpy(cf->data, rx->cl1.data, cf->len); > + else > + memcpy(cf->data, rx->cl2.data, cf->len); > + } > + > + ixxat_usb_get_ts_tv(dev, le32_to_cpu(rx->base.time), &skb->tstamp); > + > + netdev->stats.rx_packets++; > + netdev->stats.rx_bytes += cf->len; > + netif_rx(skb); > + > + return 0; > +} > + > +static int ixxat_usb_handle_status(struct ixxat_usb_device *dev, > + struct ixxat_can_msg *rx) > +{ > + struct net_device *netdev = dev->netdev; > + struct can_frame *can_frame; > + struct sk_buff *skb = alloc_can_err_skb(netdev, &can_frame); > + enum can_state new_state = CAN_STATE_ERROR_ACTIVE; > + u32 raw_status; > + > + if (dev->adapter == &usb2can_cl1) > + raw_status = le32_to_cpu(*(__le32 *)(rx->cl1.data)); > + else > + raw_status = le32_to_cpu(*(__le32 *)(rx->cl2.data)); > + > + if (!skb) > + return -ENOMEM; > + > + if (raw_status == IXXAT_USB_CAN_STATUS_OK) { > + dev->can.state = CAN_STATE_ERROR_ACTIVE; > + can_frame->can_id |= CAN_ERR_CRTL; > + can_frame->data[1] |= CAN_ERR_CRTL_ACTIVE; > + } else if (raw_status & IXXAT_USB_CAN_STATUS_BUSOFF) { > + can_frame->can_id |= CAN_ERR_BUSOFF; > + dev->can.can_stats.bus_off++; > + new_state = CAN_STATE_BUS_OFF; > + can_bus_off(netdev); > + } else { > + if (raw_status & IXXAT_USB_CAN_STATUS_ERRLIM) { > + can_frame->can_id |= CAN_ERR_CRTL; > + can_frame->data[1] |= CAN_ERR_CRTL_TX_WARNING; > + can_frame->data[1] |= CAN_ERR_CRTL_RX_WARNING; > + dev->can.can_stats.error_warning++; > + new_state = CAN_STATE_ERROR_WARNING; > + } > + > + if (raw_status & IXXAT_USB_CAN_STATUS_ERR_PAS) { > + can_frame->can_id |= CAN_ERR_CRTL; > + can_frame->data[1] |= CAN_ERR_CRTL_TX_PASSIVE; > + can_frame->data[1] |= CAN_ERR_CRTL_RX_PASSIVE; > + dev->can.can_stats.error_passive++; > + new_state = CAN_STATE_ERROR_PASSIVE; > + } > + > + if (raw_status & IXXAT_USB_CAN_STATUS_OVERRUN) { > + can_frame->can_id |= CAN_ERR_CRTL; > + can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW; > + new_state = CAN_STATE_MAX; > + } > + } > + > + if (new_state == CAN_STATE_ERROR_ACTIVE) { > + dev->bec.txerr = 0; > + dev->bec.rxerr = 0; > + } > + > + if (new_state != CAN_STATE_MAX) > + dev->can.state = new_state; > + > + netdev->stats.rx_packets++; > + netdev->stats.rx_bytes += can_frame->can_dlc; > + netif_rx(skb); > + > + return 0; > +} > + > +static int ixxat_usb_handle_error(struct ixxat_usb_device *dev, > + struct ixxat_can_msg *rx) > +{ > + struct net_device *netdev = dev->netdev; > + struct can_frame *can_frame; > + struct sk_buff *skb = alloc_can_err_skb(netdev, &can_frame); > + u8 raw_error; > + > + if (dev->adapter == &usb2can_cl1) { > + raw_error = rx->cl1.data[0]; > + dev->bec.rxerr = rx->cl1.data[3]; > + dev->bec.txerr = rx->cl1.data[4]; > + } else { > + raw_error = rx->cl2.data[0]; > + dev->bec.rxerr = rx->cl2.data[3]; > + dev->bec.txerr = rx->cl2.data[4]; > + } > + > + if (dev->can.state == CAN_STATE_BUS_OFF) > + return 0; > + > + if (!skb) > + return -ENOMEM; > + > + switch (raw_error) { > + case IXXAT_USB_CAN_ERROR_ACK: > + can_frame->can_id |= CAN_ERR_ACK; > + netdev->stats.tx_errors++; > + break; > + case IXXAT_USB_CAN_ERROR_BIT: > + can_frame->can_id |= CAN_ERR_PROT; > + can_frame->data[2] |= CAN_ERR_PROT_BIT; > + netdev->stats.rx_errors++; > + break; > + case IXXAT_USB_CAN_ERROR_CRC: > + can_frame->can_id |= CAN_ERR_PROT; > + can_frame->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; > + netdev->stats.rx_errors++; > + break; > + case IXXAT_USB_CAN_ERROR_FORM: > + can_frame->can_id |= CAN_ERR_PROT; > + can_frame->data[2] |= CAN_ERR_PROT_FORM; > + netdev->stats.rx_errors++; > + break; > + case IXXAT_USB_CAN_ERROR_STUFF: > + can_frame->can_id |= CAN_ERR_PROT; > + can_frame->data[2] |= CAN_ERR_PROT_STUFF; > + netdev->stats.rx_errors++; > + break; > + default: > + can_frame->can_id |= CAN_ERR_PROT; > + can_frame->data[2] |= CAN_ERR_PROT_UNSPEC; > + netdev->stats.rx_errors++; > + break; > + } > + > + netdev->stats.rx_packets++; > + netdev->stats.rx_bytes += can_frame->can_dlc; > + netif_rx(skb); > + > + return 0; > +} > + > +static int ixxat_usb_decode_buf(struct urb *urb) > +{ > + struct ixxat_usb_device *dev = urb->context; > + struct net_device *netdev = dev->netdev; > + struct ixxat_can_msg *can_msg; > + int err = 0; > + u32 msg_end = urb->actual_length; > + u32 read_size = 0; > + u8 *data = urb->transfer_buffer; > + > + while (msg_end > 0) { > + u8 msg_type; > + > + can_msg = (struct ixxat_can_msg *)&data[read_size]; > + > + if (!can_msg || !can_msg->base.size) { > + err = -ENOTSUPP; > + netdev_err(netdev, "Error %d: Unsupported usb msg\n", > + err); > + break; > + } > + > + if ((read_size + can_msg->base.size + 1) > urb->actual_length) { You trust here, that base.size doesn't lie. You don't ensure that read_size + sizeof(struct struct ixxat_can_msg) [1] <= urb->actual_length. [1] It's more complicated, as not all of sizeof(struct struct ixxat_can_msg) is accessed, depeding on the msg_type and adapter version. Please don't consider dating coming from outside trustworthy, especially when it comes to length :) We don't want to access kernel memory outside of the rx'ed USB message. > + err = -EBADMSG; > + netdev_err(netdev, > + "Error %d: Usb rx-buffer size unknown\n", > + err); > + break; > + } > + > + msg_type = le32_to_cpu(can_msg->base.flags); > + msg_type &= IXXAT_USB_MSG_FLAGS_TYPE; > + > + switch (msg_type) { > + case IXXAT_USB_CAN_DATA: > + err = ixxat_usb_handle_canmsg(dev, can_msg); > + if (err) > + goto fail; > + break; > + case IXXAT_USB_CAN_STATUS: > + err = ixxat_usb_handle_status(dev, can_msg); > + if (err) > + goto fail; > + break; > + case IXXAT_USB_CAN_ERROR: > + err = ixxat_usb_handle_error(dev, can_msg); > + if (err) > + goto fail; > + break; > + case IXXAT_USB_CAN_TIMEOVR: > + ixxat_usb_get_ts_tv(dev, can_msg->base.time, NULL); > + break; > + case IXXAT_USB_CAN_INFO: > + case IXXAT_USB_CAN_WAKEUP: > + case IXXAT_USB_CAN_TIMERST: > + break; > + default: > + netdev_err(netdev, > + "Unhandled rec type 0x%02x (%d): ignored\n", > + msg_type, msg_type); > + break; > + } > + > + read_size += (can_msg->base.size + 1); > + msg_end -= (can_msg->base.size + 1); > + } > + > +fail: > + if (err) > + netdev_err(netdev, "Error %d: Buffer decoding failed\n", err); > + > + return err; > +} > + > +static void ixxat_usb_read_bulk(struct urb *urb) > +{ > + struct ixxat_usb_device *dev = urb->context; > + const struct ixxat_usb_adapter *adapter = dev->adapter; > + struct net_device *netdev = dev->netdev; > + struct usb_device *udev = dev->udev; > + int err; > + > + if (!netif_device_present(netdev)) > + return; > + > + switch (urb->status) { > + case 0: /* success */ > + break; > + case -EPROTO: > + case -EILSEQ: > + case -ENOENT: > + case -ECONNRESET: > + case -ESHUTDOWN: > + return; > + default: > + netdev_err(netdev, "Rx urb aborted /(%d)\n", urb->status); > + goto resubmit_urb; > + } > + > + if (urb->actual_length > 0) > + if (dev->state & IXXAT_USB_STATE_STARTED) > + err = ixxat_usb_decode_buf(urb); > + > +resubmit_urb: > + usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, dev->ep_msg_in), > + urb->transfer_buffer, adapter->buffer_size_rx, > + ixxat_usb_read_bulk, dev); > + > + usb_anchor_urb(urb, &dev->rx_submitted); > + err = usb_submit_urb(urb, GFP_ATOMIC); > + if (!err) > + return; > + > + usb_unanchor_urb(urb); > + > + if (err == -ENODEV) > + netif_device_detach(netdev); > + else > + netdev_err(netdev, > + "Error %d: Failed to resubmit read bulk urb\n", err); > +} > + > +static void ixxat_usb_write_bulk(struct urb *urb) > +{ > + struct ixxat_tx_urb_context *context = urb->context; > + struct ixxat_usb_device *dev; > + struct net_device *netdev; > + > + if (WARN_ON(!context)) > + return; > + > + dev = context->dev; > + netdev = dev->netdev; > + atomic_dec(&dev->active_tx_urbs); > + > + if (!netif_device_present(netdev)) > + return; > + > + if (!urb->status) { > + netdev->stats.tx_packets += context->count; > + netdev->stats.tx_bytes += context->dlc; > + } else { > + netdev_err(netdev, "Error %d: Tx urb aborted\n", urb->status); > + } > + > + can_get_echo_skb(netdev, context->echo_index); > + context->echo_index = IXXAT_USB_MAX_TX_URBS; > + > + if (!urb->status) > + netif_wake_queue(netdev); > +} > + > +static void ixxat_usb_encode_msg(struct ixxat_usb_device *dev, > + struct sk_buff *skb, u8 *obuf, > + size_t *size) why don't you return the size, but use a pointer here? > +{ > + struct canfd_frame *cf = (struct canfd_frame *)skb->data; > + struct ixxat_can_msg can_msg = {0}; > + struct ixxat_can_msg_base *msg_base = &can_msg.base; > + > + if (cf->can_id & CAN_RTR_FLAG) > + msg_base->flags |= IXXAT_USB_MSG_FLAGS_RTR; > + > + if (cf->can_id & CAN_EFF_FLAG) { > + msg_base->flags |= IXXAT_USB_MSG_FLAGS_EXT; > + msg_base->msg_id = cf->can_id & CAN_EFF_MASK; > + } else { > + msg_base->msg_id = cf->can_id & CAN_SFF_MASK; > + } > + > + if (can_is_canfd_skb(skb)) { > + msg_base->flags |= IXXAT_USB_FDMSG_FLAGS_EDL; > + > + if (!(cf->can_id & CAN_RTR_FLAG) && (cf->flags & CANFD_BRS)) > + msg_base->flags |= IXXAT_USB_FDMSG_FLAGS_FDR; > + > + msg_base->flags |= IXXAT_USB_ENCODE_DLC(can_len2dlc(cf->len)); > + } else { > + msg_base->flags |= IXXAT_USB_ENCODE_DLC(cf->len); > + } Don't access flags and msg_id directly, they are __le32... > + > + le32_to_cpus(&msg_base->flags); > + le32_to_cpus(&msg_base->msg_id); ...and don't swap them later. Use u32 variables on the stack for flags and msg_id and assign then using cpu_to_le32 > + > + msg_base->size = (u8)(sizeof(*msg_base) - 1); > + if (dev->adapter == &usb2can_cl1) { > + msg_base->size += (u8)(sizeof(can_msg.cl1) - CAN_MAX_DLEN); > + msg_base->size += cf->len; > + memcpy(can_msg.cl1.data, cf->data, cf->len); > + } else { > + msg_base->size += (u8)(sizeof(can_msg.cl2) - CANFD_MAX_DLEN); > + msg_base->size += cf->len; > + memcpy(can_msg.cl2.data, cf->data, cf->len); > + } > + > + *size = msg_base->size + 1; > + memcpy(obuf, &can_msg, *size); > + skb->data_len = *size; > +} > + > +static netdev_tx_t ixxat_usb_start_xmit(struct sk_buff *skb, > + struct net_device *netdev) > +{ > + struct ixxat_usb_device *dev = netdev_priv(netdev); > + struct ixxat_tx_urb_context *context = NULL; > + struct net_device_stats *stats = &netdev->stats; > + struct canfd_frame *cf = (struct canfd_frame *)skb->data; > + struct urb *urb; > + u8 *obuf; > + int i; > + int err; > + size_t size = dev->adapter->buffer_size_tx; > + > + if (can_dropped_invalid_skb(netdev, skb)) > + return NETDEV_TX_OK; > + > + for (i = 0; i < IXXAT_USB_MAX_TX_URBS; i++) { > + if (dev->tx_contexts[i].echo_index == IXXAT_USB_MAX_TX_URBS) { > + context = dev->tx_contexts + i; > + break; > + } > + } > + > + if (WARN_ON_ONCE(!context)) > + return NETDEV_TX_BUSY; > + > + urb = context->urb; > + obuf = urb->transfer_buffer; > + > + ixxat_usb_encode_msg(dev, skb, obuf, &size); > + > + context->echo_index = i; > + context->dlc = cf->len; > + context->count = 1; > + > + urb->transfer_buffer_length = size; > + usb_anchor_urb(urb, &dev->tx_submitted); > + can_put_echo_skb(skb, netdev, context->echo_index); > + atomic_inc(&dev->active_tx_urbs); > + > + err = usb_submit_urb(urb, GFP_ATOMIC); > + if (unlikely(err)) { > + can_free_echo_skb(netdev, context->echo_index); > + usb_unanchor_urb(urb); > + atomic_dec(&dev->active_tx_urbs); > + > + context->echo_index = IXXAT_USB_MAX_TX_URBS; > + > + if (err == -ENODEV) { > + netif_device_detach(netdev); > + } else { > + stats->tx_dropped++; > + netdev_err(netdev, > + "Error %d: Submitting tx-urb failed\n", err); > + } > + } else { > + if (atomic_read(&dev->active_tx_urbs) >= IXXAT_USB_MAX_TX_URBS) > + netif_stop_queue(netdev); > + } > + > + return NETDEV_TX_OK; > +} > + > +static int ixxat_usb_setup_rx_urbs(struct ixxat_usb_device *dev) > +{ > + int i; > + int err = 0; > + const struct ixxat_usb_adapter *adapter = dev->adapter; > + struct net_device *netdev = dev->netdev; > + struct usb_device *udev = dev->udev; > + > + for (i = 0; i < IXXAT_USB_MAX_RX_URBS; i++) { > + struct urb *urb; > + u8 *buf; > + > + urb = usb_alloc_urb(0, GFP_KERNEL); > + if (!urb) { > + err = -ENOMEM; > + netdev_err(netdev, "Error %d: No memory for URBs\n", > + err); > + break; > + } > + > + buf = kmalloc(adapter->buffer_size_rx, GFP_KERNEL); > + if (!buf) { > + usb_free_urb(urb); > + err = -ENOMEM; > + netdev_err(netdev, > + "Error %d: No memory for USB-buffer\n", err); > + break; > + } > + > + usb_fill_bulk_urb(urb, udev, > + usb_rcvbulkpipe(udev, dev->ep_msg_in), buf, > + adapter->buffer_size_rx, ixxat_usb_read_bulk, > + dev); > + > + urb->transfer_flags |= URB_FREE_BUFFER; > + usb_anchor_urb(urb, &dev->rx_submitted); > + > + err = usb_submit_urb(urb, GFP_KERNEL); > + if (err) { > + usb_unanchor_urb(urb); > + kfree(buf); > + usb_free_urb(urb); > + > + if (err == -ENODEV) > + netif_device_detach(netdev); > + > + break; > + } > + > + usb_free_urb(urb); > + } > + > + if (i == 0) > + netdev_err(netdev, "Error: Couldn't setup any rx-URBs\n"); > + > + return err; > +} > + > +static int ixxat_usb_setup_tx_urbs(struct ixxat_usb_device *dev) > +{ > + int i; > + int ret = 0; > + const struct ixxat_usb_adapter *adapter = dev->adapter; > + struct net_device *netdev = dev->netdev; > + struct usb_device *udev = dev->udev; > + > + for (i = 0; i < IXXAT_USB_MAX_TX_URBS; i++) { > + struct ixxat_tx_urb_context *context; > + struct urb *urb = NULL; > + u8 *buf = NULL; > + > + urb = usb_alloc_urb(0, GFP_KERNEL); > + if (!urb) { > + ret = -ENOMEM; > + netdev_err(netdev, "Error %d: No memory for URBs\n", > + ret); > + break; > + } > + > + buf = kmalloc(adapter->buffer_size_tx, GFP_KERNEL); > + if (!buf) { > + usb_free_urb(urb); > + ret = -ENOMEM; > + netdev_err(netdev, > + "Error %d: No memory for USB-buffer\n", ret); > + break; > + } > + > + context = dev->tx_contexts + i; > + context->dev = dev; > + context->urb = urb; > + > + usb_fill_bulk_urb(urb, udev, > + usb_sndbulkpipe(udev, dev->ep_msg_out), buf, > + adapter->buffer_size_tx, ixxat_usb_write_bulk, > + context); > + > + urb->transfer_flags |= URB_FREE_BUFFER; > + } > + > + if (i == 0) { > + netdev_err(netdev, "Error: Couldn't setup any tx-URBs\n"); > + usb_kill_anchored_urbs(&dev->rx_submitted); > + } > + > + return ret; > +} > + > +static void ixxat_usb_disconnect(struct usb_interface *intf) > +{ > + struct ixxat_usb_device *dev; > + struct ixxat_usb_device *prev_dev; > + > + /* unregister the given device and all previous devices */ > + for (dev = usb_get_intfdata(intf); dev; dev = prev_dev) { > + prev_dev = dev->prev_dev; > + unregister_netdev(dev->netdev); > + free_candev(dev->netdev); > + } > + > + usb_set_intfdata(intf, NULL); > +} > + > +static int ixxat_usb_start(struct ixxat_usb_device *dev) > +{ > + int err; > + int i; > + u32 time_ref = 0; > + const struct ixxat_usb_adapter *adapter = dev->adapter; > + > + err = ixxat_usb_setup_rx_urbs(dev); > + if (err) > + return err; > + > + err = ixxat_usb_setup_tx_urbs(dev); > + if (err) > + return err; > + > + /* Try to reset the controller, in case it is already initialized > + * from a previous unclean shutdown > + */ > + ixxat_usb_reset_ctrl(dev); > + > + if (adapter->init_ctrl) { > + err = adapter->init_ctrl(dev); > + if (err) > + goto fail; > + } > + > + if (!dev->ctrl_open) { > + err = ixxat_usb_start_ctrl(dev, &time_ref); > + if (err) > + goto fail; > + > + ixxat_usb_set_ts_now(dev, time_ref); > + } > + > + dev->ctrl_open = 1; > + > + dev->bec.txerr = 0; > + dev->bec.rxerr = 0; > + > + dev->state |= IXXAT_USB_STATE_STARTED; > + dev->can.state = CAN_STATE_ERROR_ACTIVE; > + > + return 0; > + > +fail: > + if (err == -ENODEV) > + netif_device_detach(dev->netdev); > + > + netdev_err(dev->netdev, "Error %d: Couldn't submit control\n", err); > + > + for (i = 0; i < IXXAT_USB_MAX_TX_URBS; i++) { > + usb_free_urb(dev->tx_contexts[i].urb); > + dev->tx_contexts[i].urb = NULL; > + } > + > + return err; > +} > + > +static int ixxat_usb_open(struct net_device *netdev) > +{ > + struct ixxat_usb_device *dev = netdev_priv(netdev); > + int err; > + > + /* common open */ > + err = open_candev(netdev); > + if (err) > + return err; > + > + /* finally start device */ > + err = ixxat_usb_start(dev); > + if (err) { > + netdev_err(netdev, "Error %d: Couldn't start device.\n", err); > + close_candev(netdev); > + return err; > + } > + > + netif_start_queue(netdev); > + > + return 0; > +} > + > +static int ixxat_usb_stop(struct net_device *netdev) > +{ > + int err = 0; > + struct ixxat_usb_device *dev = netdev_priv(netdev); > + > + netif_stop_queue(netdev); > + ixxat_usb_unlink_all_urbs(dev); > + > + if (dev->ctrl_open) { > + err = ixxat_usb_stop_ctrl(dev); > + if (err) > + netdev_warn(netdev, "Error %d: Cannot stop device\n", > + err); > + } > + > + dev->ctrl_open = 0; > + close_candev(netdev); > + dev->can.state = CAN_STATE_STOPPED; > + > + return 0; > +} > + > +static const struct net_device_ops ixxat_usb_netdev_ops = { > + .ndo_open = ixxat_usb_open, > + .ndo_stop = ixxat_usb_stop, > + .ndo_start_xmit = ixxat_usb_start_xmit > +}; > + > +static const struct ixxat_usb_adapter *ixxat_usb_get_adapter(const u16 id) > +{ > + switch (id) { > + case USB2CAN_COMPACT_PRODUCT_ID: > + case USB2CAN_EMBEDDED_PRODUCT_ID: > + case USB2CAN_PROFESSIONAL_PRODUCT_ID: > + case USB2CAN_AUTOMOTIVE_PRODUCT_ID: > + return &usb2can_cl1; > + case USB2CAN_FD_COMPACT_PRODUCT_ID: > + case USB2CAN_FD_PROFESSIONAL_PRODUCT_ID: > + case USB2CAN_FD_AUTOMOTIVE_PRODUCT_ID: > + case USB2CAN_FD_PCIE_MINI_PRODUCT_ID: > + case USB2CAR_PRODUCT_ID: > + return &usb2can_cl2; > + case CAN_IDM101_PRODUCT_ID: > + return &can_idm; > + default: > + return NULL; > + } > +} > + > +static int ixxat_usb_create_dev(struct usb_interface *intf, > + const struct ixxat_usb_adapter *adapter, > + u16 ctrl_index) > +{ > + struct usb_device *udev = interface_to_usbdev(intf); > + struct ixxat_usb_device *dev; > + struct net_device *netdev; > + int err; > + int i; > + > + netdev = alloc_candev(sizeof(*dev), IXXAT_USB_MAX_TX_URBS); > + if (!netdev) { > + dev_err(&intf->dev, "Cannot allocate candev\n"); > + return -ENOMEM; > + } > + > + dev = netdev_priv(netdev); > + > + dev->ctrl_open = 0; > + dev->udev = udev; > + dev->netdev = netdev; > + dev->adapter = adapter; > + dev->ctrl_index = ctrl_index; > + dev->state = IXXAT_USB_STATE_CONNECTED; > + > + i = ctrl_index + adapter->ep_offs; > + dev->ep_msg_in = adapter->ep_msg_in[i]; > + dev->ep_msg_out = adapter->ep_msg_out[i]; > + > + dev->can.clock.freq = adapter->clock; > + dev->can.bittiming_const = adapter->bt; > + dev->can.data_bittiming_const = adapter->btd; > + > + dev->can.do_set_mode = ixxat_usb_set_mode; > + dev->can.do_get_berr_counter = ixxat_usb_get_berr_counter; > + > + dev->can.ctrlmode_supported = adapter->modes; > + > + netdev->netdev_ops = &ixxat_usb_netdev_ops; > + > + netdev->flags |= IFF_ECHO; > + > + init_usb_anchor(&dev->rx_submitted); > + init_usb_anchor(&dev->tx_submitted); > + > + atomic_set(&dev->active_tx_urbs, 0); > + > + for (i = 0; i < IXXAT_USB_MAX_TX_URBS; i++) > + dev->tx_contexts[i].echo_index = IXXAT_USB_MAX_TX_URBS; > + > + dev->prev_dev = usb_get_intfdata(intf); > + usb_set_intfdata(intf, dev); > + > + SET_NETDEV_DEV(netdev, &intf->dev); > + err = register_candev(netdev); > + if (err) { > + dev_err(&intf->dev, "Error %d: Failed to register can device\n", > + err); > + goto free_candev; > + } > + > + if (dev->prev_dev) > + (dev->prev_dev)->next_dev = dev; > + > + err = ixxat_usb_get_dev_info(udev, &dev->dev_info); > + if (err) { > + dev_err(&intf->dev, > + "Error %d: Failed to get device information\n", err); > + goto unreg_candev; > + } > + > + netdev_info(netdev, "%s: Connected Channel %u (device %s)\n", > + dev->dev_info.device_name, ctrl_index, > + dev->dev_info.device_id); > + > + return 0; > + > +unreg_candev: > + unregister_candev(netdev); > +free_candev: > + usb_set_intfdata(intf, dev->prev_dev); > + free_candev(netdev); > + return err; > +} > + > +static int ixxat_usb_probe(struct usb_interface *intf, > + const struct usb_device_id *id) > +{ > + struct usb_device *udev = interface_to_usbdev(intf); > + struct usb_host_interface *host_intf = intf->altsetting; > + const struct ixxat_usb_adapter *adapter; > + struct ixxat_dev_caps dev_caps; > + u16 i; > + int err; > + > + usb_reset_configuration(udev); > + > + adapter = ixxat_usb_get_adapter(id->idProduct); > + if (!adapter) { > + dev_err(&intf->dev, "%s: Unknown device id %d\n", > + IXXAT_USB_DRIVER_NAME, id->idProduct); > + return -ENODEV; > + } > + > + for (i = 0; i < host_intf->desc.bNumEndpoints; i++) { > + const u8 epaddr = host_intf->endpoint[i].desc.bEndpointAddress; > + int match; > + u8 j; > + > + /* Check if usb-endpoint address matches known usb-endpoints */ > + for (j = 0; j < IXXAT_USB_MAX_CHANNEL; j++) { > + u8 ep_msg_in = adapter->ep_msg_in[j]; > + u8 ep_msg_out = adapter->ep_msg_in[j]; > + > + if (epaddr == ep_msg_in || epaddr == ep_msg_out) { > + match = 1; > + break; > + } > + } > + > + if (!match) > + return -ENODEV; > + } > + > + err = ixxat_usb_power_ctrl(udev, IXXAT_USB_POWER_WAKEUP); > + if (err) > + return err; > + > + msleep(IXXAT_USB_POWER_WAKEUP_TIME); > + > + err = ixxat_usb_get_dev_caps(udev, &dev_caps); > + if (err) { > + dev_err(&intf->dev, > + "Error %d: Failed to get device capabilities\n", err); > + return err; > + } > + > + err = -ENODEV; > + for (i = 0; i < dev_caps.bus_ctrl_count; i++) { > + u8 bustype = IXXAT_USB_BUS_TYPE(dev_caps.bus_ctrl_types[i]); > + > + if (bustype == IXXAT_USB_BUS_CAN) > + err = ixxat_usb_create_dev(intf, adapter, i); > + > + if (err) { > + /* deregister already created devices */ > + ixxat_usb_disconnect(intf); > + return err; > + } > + } > + > + return err; > +} > + > +static struct usb_driver ixxat_usb_driver = { > + .name = IXXAT_USB_DRIVER_NAME, > + .probe = ixxat_usb_probe, > + .disconnect = ixxat_usb_disconnect, > + .id_table = ixxat_usb_table, > +}; > + > +module_usb_driver(ixxat_usb_driver); > diff --git a/drivers/net/can/usb/ixxat_usb/ixxat_usb_core.h b/drivers/net/can/usb/ixxat_usb/ixxat_usb_core.h > new file mode 100644 > index 0000000..dee87cd > --- /dev/null > +++ b/drivers/net/can/usb/ixxat_usb/ixxat_usb_core.h > @@ -0,0 +1,531 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +/* CAN driver base for IXXAT USB-to-CAN > + * > + * Copyright (C) 2018 HMS Industrial Networks <socketcan@xxxxxxxxxxxxxxx> > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms of the GNU General Public License as published > + * by the Free Software Foundation; version 2 of the License. > + * > + * This program is distributed in the hope that it will be useful, but > + * WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + */ > + > +#ifndef IXXAT_USB_CORE_H > +#define IXXAT_USB_CORE_H > + > +#define IXXAT_USB_DRIVER_NAME "ixxat_usb2can" > +#define IXXAT_USB_CTRL_NAME "ixxat_usb" consider using one space one after the macro name, same for the *.c files. > + > +#define IXXAT_USB_VENDOR_ID 0x08d8 > + > +/* supported device ids: CL1 */ > +#define USB2CAN_COMPACT_PRODUCT_ID 0x0008 > +#define USB2CAN_EMBEDDED_PRODUCT_ID 0x0009 > +#define USB2CAN_PROFESSIONAL_PRODUCT_ID 0x000A > +#define USB2CAN_AUTOMOTIVE_PRODUCT_ID 0x000B > + > +/* supported device ids: CL2 */ > +#define USB2CAN_FD_COMPACT_PRODUCT_ID 0x0014 > +#define USB2CAN_FD_PROFESSIONAL_PRODUCT_ID 0x0016 > +#define USB2CAN_FD_AUTOMOTIVE_PRODUCT_ID 0x0017 > +#define USB2CAN_FD_PCIE_MINI_PRODUCT_ID 0x001B > +#define USB2CAR_PRODUCT_ID 0x001C > +#define CAN_IDM101_PRODUCT_ID 0xFF12 > + > +#define IXXAT_USB_BUS_CAN 1 > + > +#define IXXAT_USB_BUS_TYPE(type) ((u8)(((type) >> 8) & 0x00FF)) > + > +#define IXXAT_USB_STATE_CONNECTED 0x00000001 > +#define IXXAT_USB_STATE_STARTED 0x00000002 consider using BIT() > + > +#define IXXAT_USB_MAX_CHANNEL 5 > +#define IXXAT_USB_MAX_TYPES 32 > +#define IXXAT_USB_MAX_RX_URBS 4 > +#define IXXAT_USB_MAX_TX_URBS 10 > +#define IXXAT_USB_MAX_COM_REQ 10 > + > +#define IXXAT_USB_MSG_TIMEOUT 50 > +#define IXXAT_USB_MSG_CYCLE 20 > + > +#define IXXAT_USB_POWER_WAKEUP 0 > +#define IXXAT_USB_POWER_WAKEUP_TIME 500 > + > +//#define IXXAT_USB_CMD_BUFFER_SIZE 256 > + > +#define IXXAT_USB_OPMODE_STANDARD 0x01 > +#define IXXAT_USB_OPMODE_EXTENDED 0x02 > +#define IXXAT_USB_OPMODE_ERRFRAME 0x04 > +#define IXXAT_USB_OPMODE_LISTONLY 0x08 > + > +#define IXXAT_USB_EXMODE_EXTDATA 0x01 > +#define IXXAT_USB_EXMODE_FASTDATA 0x02 > +#define IXXAT_USB_EXMODE_ISOFD 0x04 > + > +#define IXXAT_USB_BTMODE_NAT 0x00000001 > +#define IXXAT_USB_BTMODE_TSM 0x00000002 same here > + > +#define IXXAT_USB_STOP_ACTION_CLEARALL 3 > + > +#define IXXAT_RESTART_TASK_CYCLE_TIME 20 > + > +#define IXXAT_USB_CAN_DATA 0x00 > +#define IXXAT_USB_CAN_INFO 0x01 > +#define IXXAT_USB_CAN_ERROR 0x02 > +#define IXXAT_USB_CAN_STATUS 0x03 > +#define IXXAT_USB_CAN_WAKEUP 0x04 > +#define IXXAT_USB_CAN_TIMEOVR 0x05 > +#define IXXAT_USB_CAN_TIMERST 0x06 > + > +#define IXXAT_USB_CAN_STATUS_OK 0x00000000 > +#define IXXAT_USB_CAN_STATUS_OVERRUN 0x00000002 > +#define IXXAT_USB_CAN_STATUS_ERRLIM 0x00000004 > +#define IXXAT_USB_CAN_STATUS_BUSOFF 0x00000008 > +#define IXXAT_USB_CAN_STATUS_ERR_PAS 0x00002000 > + > +#define IXXAT_USB_CAN_ERROR_STUFF 1 > +#define IXXAT_USB_CAN_ERROR_FORM 2 > +#define IXXAT_USB_CAN_ERROR_ACK 3 > +#define IXXAT_USB_CAN_ERROR_BIT 4 > +#define IXXAT_USB_CAN_ERROR_CRC 6 > + > +#define IXXAT_USB_MSG_FLAGS_TYPE 0x000000FF > +#define IXXAT_USB_MSG_FLAGS_DLC 0x000F0000 > +#define IXXAT_USB_MSG_FLAGS_OVR 0x00100000 > +#define IXXAT_USB_MSG_FLAGS_RTR 0x00400000 > +#define IXXAT_USB_MSG_FLAGS_EXT 0x00800000 > + > +#define IXXAT_USB_DECODE_DLC(flags) (((flags) & IXXAT_USB_MSG_FLAGS_DLC) >> 16) > +#define IXXAT_USB_ENCODE_DLC(len) (((len) << 16) & IXXAT_USB_MSG_FLAGS_DLC) > + > +#define IXXAT_USB_FDMSG_FLAGS_EDL 0x00000400 > +#define IXXAT_USB_FDMSG_FLAGS_FDR 0x00000800 > +#define IXXAT_USB_FDMSG_FLAGS_ESI 0x00001000 > + > +#define IXXAT_USB_CAN_CMD_START 0x326 > +#define IXXAT_USB_CAN_CMD_STOP 0x327 > +#define IXXAT_USB_CAN_CMD_RESET 0x328 > + > +#define IXXAT_USB_BRD_CMD_GET_DEVCAPS 0x401 > +#define IXXAT_USB_BRD_CMD_GET_DEVINFO 0x402 > +#define IXXAT_USB_BRD_CMD_POWER 0x421 > + > +/** > + * struct ixxat_can_msg_base - IXXAT CAN message base (CL1/CL2) > + * @size: Message size (this field excluded) > + * @time: Message timestamp > + * @msg_id: Message ID > + * @flags: Message flags > + * > + * Contains the common fields of an IXXAT CAN message on both CL1 and CL2 > + * devices > + */ > +struct ixxat_can_msg_base { > + u8 size; > + __le32 time; > + __le32 msg_id; > + __le32 flags; > +} __packed; > + > +/** > + * struct ixxat_can_msg_cl1 - IXXAT CAN message (CL1) > + * @data: Message data (standard CAN frame) > + * > + * Contains the fields of an IXXAT CAN message on CL1 devices > + */ > +struct ixxat_can_msg_cl1 { > + u8 data[CAN_MAX_DLEN]; > +} __packed; > + > +/** > + * struct ixxat_can_msg_cl2 - IXXAT CAN message (CL2) > + * @client_id: Client ID > + * @data: Message data (CAN FD frame) > + * > + * Contains the fields of an IXXAT CAN message on CL2 devices > + */ > +struct ixxat_can_msg_cl2 { > + __le32 client_id; > + u8 data[CANFD_MAX_DLEN]; > +} __packed; > + > +/** > + * struct ixxat_can_msg - IXXAT CAN message > + * @base: Base message > + * @cl1: Cl1 message > + * @cl2: Cl2 message > + * > + * Contains an IXXAT CAN message > + */ > +struct ixxat_can_msg { > + struct ixxat_can_msg_base base; > + union { > + struct ixxat_can_msg_cl1 cl1; > + struct ixxat_can_msg_cl2 cl2; > + }; > +} __packed; > + > +/** > + * struct ixxat_dev_caps - Device capabilities > + * @bus_ctrl_count: Stores the bus controller counter > + * @bus_ctrl_types: Stores the bus controller types > + * > + * Contains the device capabilities > + */ > +struct ixxat_dev_caps { > + __le16 bus_ctrl_count; > + __le16 bus_ctrl_types[IXXAT_USB_MAX_TYPES]; > +} __packed; > + > +/** > + * struct ixxat_canbtp Bittiming parameters (CL2) > + * @mode: Operation mode > + * @bps: Bits per second > + * @ts1: First time segment > + * @ts2: Second time segment > + * @sjw: Synchronization jump width > + * @tdo: Transmitter delay offset > + * > + * Bittiming parameters of a CL2 initialization request > + */ > +struct ixxat_canbtp { > + __le32 mode; > + __le32 bps; > + __le16 ts1; > + __le16 ts2; > + __le16 sjw; > + __le16 tdo; > +} __packed; > + > +/** > + * struct ixxat_dev_info IXXAT usb device information > + * @device_name: Name of the device > + * @device_id: Device identification ( unique device id) > + * @device_version: Device version ( 0, 1, ...) > + * @device_fpga_version: Version of FPGA design > + * > + * Contains device information of IXXAT USB devices > + */ > +struct ixxat_dev_info { > + char device_name[16]; > + char device_id[16]; > + __le16 device_version; > + __le32 device_fpga_version; > +} __packed; > + > +/** > + * struct ixxat_time_ref Time reference > + * @kt_host_0: Latest time on the host > + * @ts_dev_0: Latest time stamp on the device > + * @ts_dev_last: Last device time stamp > + * > + * Contains time references of the device and the host > + */ > +struct ixxat_time_ref { > + ktime_t kt_host_0; > + u32 ts_dev_0; > + u32 ts_dev_last; > +}; > + > +/** > + * struct ixxat_tx_urb_context URB content for transmission > + * @dev: IXXAT USB device > + * echo_index: Echo index > + * @dlc: Data length code > + * @count: Counter > + * @urb: USB request block > + * > + * Contains content for USB request block transmissions > + */ > +struct ixxat_tx_urb_context { > + struct ixxat_usb_device *dev; > + u32 echo_index; > + u8 dlc; > + u8 count; > + struct urb *urb; > +}; > + > +/** > + * struct ixxat_usb_device IXXAT USB device > + * @can: CAN common private data > + * @adapter: USB network descriptor > + * @udev: USB device > + * @netdev: Net_device > + * @active_tx_urbs: Active tx urbs > + * @tx_submitted: Submitted tx usb anchor > + * @tx_contexts: Buffer for tx contexts > + * @rx_submitted: Submitted rx usb anchor > + * @state: Device state > + * @ctrl_open: Controller opened flag > + * @ctrl_index: Controller index > + * @ep_msg_in: USB endpoint for incoming messages > + * @ep_msg_out: USB endpoint for outgoing messages > + * @prev_dev: Previous opened device > + * @next_dev: Next opened device in list > + * @time_ref: Time reference > + * @dev_info: Device information > + * @bec: CAN error counter > + * > + * IXXAT USB-to-CAN device > + */ > +struct ixxat_usb_device { > + struct can_priv can; > + const struct ixxat_usb_adapter *adapter; > + struct usb_device *udev; > + struct net_device *netdev; > + > + atomic_t active_tx_urbs; > + struct usb_anchor tx_submitted; > + struct ixxat_tx_urb_context tx_contexts[IXXAT_USB_MAX_TX_URBS]; > + struct usb_anchor rx_submitted; > + > + u32 state; > + > + u32 ctrl_open; Why do you need two variables holding state "ctrl_open" and "state"? > + u16 ctrl_index; > + > + u8 ep_msg_in; > + u8 ep_msg_out; > + > + struct ixxat_usb_device *prev_dev; > + struct ixxat_usb_device *next_dev; > + > + struct ixxat_time_ref time_ref; > + struct ixxat_dev_info dev_info; > + > + struct can_berr_counter bec; > +}; > + > +/** > + * struct ixxat_usb_dal_req IXXAT device request block > + * @size: Request size > + * @port: Request port > + * @socket: Request socket > + * @code: Request code > + * > + * IXXAT device request block > + */ > +struct ixxat_usb_dal_req { > + __le32 size; > + __le16 port; > + __le16 socket; > + __le32 code; > +} __packed; > + > +/** > + * struct ixxat_usb_dal_res IXXAT device response block > + * @res_size: Expected response size > + * @ret_size: Actual response size > + * @code: Return code > + * > + * IXXAT device response block > + */ > +struct ixxat_usb_dal_res { > + __le32 res_size; > + __le32 ret_size; > + __le32 code; > +} __packed; > + > +/** > + * struct ixxat_usb_dal_cmd IXXAT device command > + * @req: Request block > + * @req: Response block > + * > + * IXXAT device command > + */ > +struct ixxat_usb_dal_cmd { > + struct ixxat_usb_dal_req req; > + struct ixxat_usb_dal_res res; > +} __packed; > + > +/** > + * struct ixxat_usb_caps_cmd Device capabilities command > + * @req: Request block > + * @res: Response block > + * @caps: Device capabilities > + * > + * Can be sent to a device to request its capabilities > + */ > +struct ixxat_usb_caps_cmd { > + struct ixxat_usb_dal_req req; > + struct ixxat_usb_dal_res res; > + struct ixxat_dev_caps caps; > +} __packed; > + > +/** > + * struct ixxat_usb_init_cl1_cmd Initialization command (CL1) > + * @req: Request block > + * @mode: Operation mode > + * @btr0: Bittiming register 0 > + * @btr1: Bittiming register 1 > + * @padding: 1 byte padding > + * @res: Response block > + * > + * Can be sent to a CL1 device to initialize it > + */ > +struct ixxat_usb_init_cl1_cmd { > + struct ixxat_usb_dal_req req; > + u8 mode; > + u8 btr0; > + u8 btr1; > + u8 padding; > + struct ixxat_usb_dal_res res; > +} __packed; > + > +/** > + * struct ixxat_usb_init_cl2_cmd Initialization command (CL2) > + * @req: Request block > + * @opmode: Operation mode > + * @exmode: Extended mode > + * @sdr: Stadard bittiming parameters > + * @fdr: Fast data bittiming parameters > + * @_padding: 2 bytes padding > + * @res: Response block > + * > + * Can be sent to a CL2 device to initialize it > + */ > +struct ixxat_usb_init_cl2_cmd { > + struct ixxat_usb_dal_req req; > + u8 opmode; > + u8 exmode; > + struct ixxat_canbtp sdr; > + struct ixxat_canbtp fdr; > + __le16 _padding; > + struct ixxat_usb_dal_res res; > +} __packed; > + > +/** > + * struct ixxat_usb_start_cmd Controller start command > + * @req: Request block > + * @res: Response block > + * @time: Timestamp > + * > + * Can be sent to a device to start its controller > + */ > +struct ixxat_usb_start_cmd { > + struct ixxat_usb_dal_req req; > + struct ixxat_usb_dal_res res; > + __le32 time; > +} __packed; > + > +/** > + * struct ixxat_usb_stop_cmd Controller stop command > + * @req: Request block > + * @action: Stop action > + * @res: Response block > + * > + * Can be sent to a device to start its controller > + */ > +struct ixxat_usb_stop_cmd { > + struct ixxat_usb_dal_req req; > + __le32 action; > + struct ixxat_usb_dal_res res; > +} __packed; > + > +/** > + * struct ixxat_usb_power_cmd Power command > + * @req: Request block > + * @mode: Power mode > + * @_padding1: 1 byte padding > + * @_padding2: 2 byte padding > + * @res: Response block > + * > + * Can be sent to a device to set its power mode > + */ > +struct ixxat_usb_power_cmd { > + struct ixxat_usb_dal_req req; > + u8 mode; > + u8 _padding1; > + __le16 _padding2; > + struct ixxat_usb_dal_res res; > +} __packed; > + > +/** > + * struct ixxat_usb_info_cmd Device information command > + * @req: Request block > + * @res: Response block > + * @info: Device information > + * > + * Can be sent to a device to request its device information > + */ > +struct ixxat_usb_info_cmd { > + struct ixxat_usb_dal_req req; > + struct ixxat_usb_dal_res res; > + struct ixxat_dev_info info; > +} __packed; > + > +/** > + * struct ixxat_usb_adapter IXXAT USB device adapter > + * @clock: Clock frequency > + * @bt: Bittiming constants > + * @btd: Data bittiming constants > + * @modes: Supported modes > + * @buffer_size_rx: Buffer size for receiving > + * @buffer_size_tx: Buffer size for transfer > + * @ep_msg_in: USB endpoint buffer for incoming messages > + * @ep_msg_out: USB endpoint buffer for outgoing messages > + * @ep_offs: Endpoint offset (device depended) > + * > + * Device Adapter for IXXAT USB devices > + */ > +struct ixxat_usb_adapter { > + const u32 clock; > + const struct can_bittiming_const *bt; > + const struct can_bittiming_const *btd; > + const u32 modes; > + const u16 buffer_size_rx; > + const u16 buffer_size_tx; > + const u8 ep_msg_in[IXXAT_USB_MAX_CHANNEL]; > + const u8 ep_msg_out[IXXAT_USB_MAX_CHANNEL]; > + const u8 ep_offs; > + int (*init_ctrl)(struct ixxat_usb_device *dev); > +}; > + > +extern const struct ixxat_usb_adapter usb2can_cl1; > +extern const struct ixxat_usb_adapter usb2can_cl2; > +extern const struct ixxat_usb_adapter can_idm; > + > +/** > + * ixxat_usb_setup_cmd() - Setup a device command > + * @req: Request block > + * @res: Response block > + * > + * This function sets the default values in the request and the response block > + * of a device command > + */ > +void ixxat_usb_setup_cmd(struct ixxat_usb_dal_req *req, > + struct ixxat_usb_dal_res *res); > + > +/** > + * ixxat_usb_send_cmd() - Send a command to the device > + * @dev: USB device > + * @port: Command port > + * @buf: Command buffer > + * @size: Command size > + * > + * This function sends a specific command to the device > + * > + * Return: Negative error code or bytes transferred on success > + */ > +int ixxat_usb_snd_cmd(struct usb_device *dev, const u16 port, void *buf, > + const u16 size); > + > +/** > + * ixxat_usb_rcv_cmd() - Receive a command from the device > + * @dev: USB device > + * @port: Command port > + * @buf: Command buffer > + * @size: Command size > + * > + * This function receives a response to a device request > + * > + * Return: Negative error code or bytes transferred on success > + */ > +int ixxat_usb_rcv_cmd(struct usb_device *dev, const u16 port, void *buf, > + const u16 size); > + > +#endif /* IXXAT_USB_CORE_H */ > Marc -- Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
Attachment:
signature.asc
Description: OpenPGP digital signature