Skip to contentSkip to conversation

"[PATCH] Add support for PEAK-System GmbH CAN USB adapters (v2) "

PATCH [email protected] 4 participants 9 messages Dec 16, 2011 – Dec 21, 2011 CAN network drivers

lore
Share & tools
lore
#0 lore raw

From 1cee3be3875f27a2ee3942b00d611450f4369325 Mon Sep 17 00:00:00 2001
From: Stephane Grosjean <[email protected]>
Date: Fri, 16 Dec 2011 11:11:37 +0100
Subject: [PATCH] Add support for PEAK-System GmbH CAN USB adapters (v2)

v2 includes:
change dev_xxx() into netdev_xxx() macros
add missing include linux/module.h
pre-allocate urbs and buffers for tx path at _open() and free them at _close()
rather than into _start_xmit()
remove some unused code and (boring) #ifdef/#endif
use "menuconfig" in Kconfig rather than "config" entry

---
 drivers/net/can/usb/Kconfig         |   20 +
 drivers/net/can/usb/Makefile        |   12 +
 drivers/net/can/usb/pcan_usb.c      |  654 +++++++++++++++++++
 drivers/net/can/usb/pcan_usb_core.c |  895 ++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.c  | 1207 +++++++++++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.h  |  468 ++++++++++++++
 drivers/net/can/usb/peak_usb.h      |  148 +++++
 7 files changed, 3404 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/can/usb/pcan_usb.c
 create mode 100644 drivers/net/can/usb/pcan_usb_core.c
 create mode 100644 drivers/net/can/usb/pcan_usb_pro.c
 create mode 100644 drivers/net/can/usb/pcan_usb_pro.h
 create mode 100644 drivers/net/can/usb/peak_usb.h
drivers/net/can/usb/Kconfig +20 -0
index 0452549..cb5f91c 100644
@@ -13,4 +13,24 @@ config CAN_ESD_USB2
           This driver supports the CAN-USB/2 interface
           from esd electronic system design gmbh (http://www.esd.eu).
 
+menuconfig CAN_PEAK_USB
+	tristate "PEAK-System USB adapters"
+	---help---
+	  This driver is for PCAN USB interfaces from PEAK-System 
+	  (http://www.peak-system.com).
+
+config CAN_PCAN_USB
+	tristate "PEAK PCAN-USB adapter"
+	depends on CAN_PEAK_USB
+	---help---
+	  This driver is for the one channel PCAN-USB interface
+	  from PEAK-System (http://www.peak-system.com).
+
+config CAN_PCAN_USB_PRO
+	tristate "PEAK PCAN-USB Pro adapter"
+	depends on CAN_PEAK_USB
+	---help---
+	  This driver is for the two channels PCAN-USB Pro interface
+	  from PEAK-System (http://www.peak-system.com).
+
 endmenu
drivers/net/can/usb/Makefile +12 -0
index fce3cf1..a6f1cf6 100644
@@ -5,4 +5,16 @@
 obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
 obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o
 
+obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
+peak_usb-y = pcan_usb_core.o
+
+ifneq ($(CONFIG_CAN_PCAN_USB),)
+peak_usb-y += pcan_usb.o
+endif
+
+ifneq ($(CONFIG_CAN_PCAN_USB_PRO),)
+peak_usb-y += pcan_usb_pro.o
+endif
+
+ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
 ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/usb/pcan_usb.c new file +654 -0

654 lines not on this page · raw message

drivers/net/can/usb/pcan_usb_core.c new file +895 -0

895 lines not on this page · raw message

drivers/net/can/usb/pcan_usb_pro.c new file +1207 -0

1,207 lines not on this page · raw message

drivers/net/can/usb/pcan_usb_pro.h new file +468 -0

204 lines not on this page · raw message

drivers/net/can/usb/peak_usb.h new file +148 -0

148 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.1

Part of the diff is not shown here. View the raw message.

#1 lore raw
Quoted 4 lines · On 12/16/2011 11:19 AM, [email protected] wrote:
Quoted 1 line

From 1cee3be3875f27a2ee3942b00d611450f4369325 Mon Sep 17 00:00:00 2001

From: Stephane Grosjean <[email protected]>
Date: Fri, 16 Dec 2011 11:11:37 +0100
Subject: [PATCH] Add support for PEAK-System GmbH CAN USB adapters (v2)

Please be more verbose in the commit message, e.g. what adapters does it support.

Quoted 7 lines

v2 includes:
change dev_xxx() into netdev_xxx() macros
add missing include linux/module.h
pre-allocate urbs and buffers for tx path at _open() and free them at _close()
rather than into _start_xmit()
remove some unused code and (boring) #ifdef/#endif
use "menuconfig" in Kconfig rather than "config" entry

Please put the "v2 includes" below "---" as it should not be part of the commit message.

Quoted 8 lines
---
 drivers/net/can/usb/Kconfig         |   20 +
 drivers/net/can/usb/Makefile        |   12 +
 drivers/net/can/usb/pcan_usb.c      |  654 +++++++++++++++++++
 drivers/net/can/usb/pcan_usb_core.c |  895 ++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.c  | 1207 +++++++++++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.h  |  468 ++++++++++++++
 drivers/net/can/usb/peak_usb.h      |  148 +++++

More than three files for this driver(s) ==> use a separate sub-directory?

Would it make sense to split this patch in

core.patch
adaper1.patch,
adaper2.patch?

Unfortunately, checkpatch.pl does report many issues, e.g., lines too long, white space issues, ... It's also unusual to use very long variable and macro names. Please shorten, also to avoid long lines. Furthermore I realized switch statements with many cases. Wouldn't a jump table be more efficient?

Thanks,

Wolfgang.

#2 lore raw
In reply to [email protected]
Quoted 4 lines · On 12/16/2011 11:19 AM, [email protected] wrote:

From 1cee3be3875f27a2ee3942b00d611450f4369325 Mon Sep 17 00:00:00 2001
From: Stephane Grosjean <[email protected]>
Date: Fri, 16 Dec 2011 11:11:37 +0100
Subject: [PATCH] Add support for PEAK-System GmbH CAN USB adapters (v2)

The driver has some unusal coding style, please use checkpatch and do sparse checking (compile the drivers with C=2). Make use of C99 initialisers. Use foo->bar not &foo->bar[0] to get the pointer for the first array element

more comments inline.

Marc

Quoted 63 lines

v2 includes:
change dev_xxx() into netdev_xxx() macros
add missing include linux/module.h
pre-allocate urbs and buffers for tx path at _open() and free them at _close()
rather than into _start_xmit()
remove some unused code and (boring) #ifdef/#endif
use "menuconfig" in Kconfig rather than "config" entry

---
 drivers/net/can/usb/Kconfig         |   20 +
 drivers/net/can/usb/Makefile        |   12 +
 drivers/net/can/usb/pcan_usb.c      |  654 +++++++++++++++++++
 drivers/net/can/usb/pcan_usb_core.c |  895 ++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.c  | 1207 +++++++++++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.h  |  468 ++++++++++++++
 drivers/net/can/usb/peak_usb.h      |  148 +++++
 7 files changed, 3404 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/can/usb/pcan_usb.c
 create mode 100644 drivers/net/can/usb/pcan_usb_core.c
 create mode 100644 drivers/net/can/usb/pcan_usb_pro.c
 create mode 100644 drivers/net/can/usb/pcan_usb_pro.h
 create mode 100644 drivers/net/can/usb/peak_usb.h
drivers/net/can/usb/Kconfig +20 -0
index 0452549..cb5f91c 100644
@@ -13,4 +13,24 @@ config CAN_ESD_USB2
           This driver supports the CAN-USB/2 interface
           from esd electronic system design gmbh (http://www.esd.eu).
 
+menuconfig CAN_PEAK_USB
+	tristate "PEAK-System USB adapters"
+	---help---
+	  This driver is for PCAN USB interfaces from PEAK-System 
+	  (http://www.peak-system.com).
+
+config CAN_PCAN_USB
+	tristate "PEAK PCAN-USB adapter"
+	depends on CAN_PEAK_USB
+	---help---
+	  This driver is for the one channel PCAN-USB interface
+	  from PEAK-System (http://www.peak-system.com).
+
+config CAN_PCAN_USB_PRO
+	tristate "PEAK PCAN-USB Pro adapter"
+	depends on CAN_PEAK_USB
+	---help---
+	  This driver is for the two channels PCAN-USB Pro interface
+	  from PEAK-System (http://www.peak-system.com).
+
 endmenu
drivers/net/can/usb/Makefile +3 -0
index fce3cf1..a6f1cf6 100644
@@ -5,4 +5,16 @@
 obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
 obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o
 
+obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
+peak_usb-y = pcan_usb_core.o
+

drivers/net/can/usb/Makefile line 20

Quoted 10 lines
drivers/net/can/usb/Makefile +9 -0
+ifneq ($(CONFIG_CAN_PCAN_USB),)
+peak_usb-y += pcan_usb.o
+endif
+
+ifneq ($(CONFIG_CAN_PCAN_USB_PRO),)
+peak_usb-y += pcan_usb_pro.o
+endif
+
+ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
 ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG

^^^^^^^^^^

one ccflag should be enough :)

Quoted 13 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
new file mode 100644
index 0000000..ec46da6
@@ -0,0 +1,654 @@
+/*
+ * CAN driver for PEAK System PCAN-USB adapter
+ *
+ * Copyright (C) 2011-2012 PEAK-System GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published

drivers/net/can/usb/pcan_usb.c line 17

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

please remove the address

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+ */
+#include <linux/netdevice.h>
+#include <linux/usb.h>
+#include <linux/module.h>
+
+#include <linux/can.h>
+#include <linux/can/dev.h>
+#include <linux/can/error.h>

drivers/net/can/usb/pcan_usb.c line 35

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+
+#include "peak_usb.h"
+
+MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
+
+/* PCAN-USB Endpoints */
+#define PCAN_USB_EP_CMDOUT   1
+#define PCAN_USB_EP_CMDIN    (PCAN_USB_EP_CMDOUT|USB_DIR_IN)
+#define PCAN_USB_EP_MSGOUT   2
+#define PCAN_USB_EP_MSGIN    (PCAN_USB_EP_MSGOUT|USB_DIR_IN)
                                                  ^^^

please add spaces around the |

drivers/net/can/usb/pcan_usb.c line 41

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+
+/* PCAN-USB parameter command */
+#define PCAN_USB_PARAMETER_LEN   14
+struct __packed pcan_usb_parameter {
+	u8 function;
+	u8	number;
         ^^^

please use one space

Quoted 33 lines
drivers/net/can/usb/pcan_usb.c new file +33 -0
+	u8 parameters[PCAN_USB_PARAMETER_LEN];
+};
+
+/* PCAN-USB command timeout (ms.) */
+#define PCAN_USB_COMMAND_TIMEOUT 1000
+
+/* PCAN-USB rx/tx buffers size */
+#define PCAN_USB_RX_BUFFER_SIZE  64
+#define PCAN_USB_TX_BUFFER_SIZE  64
+
+#define PCAN_USB_MSG_HEADER_LEN  2 /* Packet type information (1xbyte) 
+                                    + count of messages (1xbyte) */
+
+/* PCAN-USB adapter internal clock (MHz) */
+#define PCAN_USB_CRYSTAL_HZ      16000000
+
+/* PCAN-USB USB message record status/len field */
+#define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
+#define PCAN_USB_STATUSLEN_INTERNAL  (1 << 6)
+#define PCAN_USB_STATUSLEN_EXT_ID    (1 << 5)
+#define PCAN_USB_STATUSLEN_RTR       (1 << 4)
+#define PCAN_USB_STATUSLEN_DLC       (0xf)
+
+/* PCAN-USB error flags */
+#define PCAN_USB_ERROR_TXFULL    0x01
+#define PCAN_USB_ERROR_RXQOVR    0x02
+#define PCAN_USB_ERROR_BUS_LIGHT 0x04
+#define PCAN_USB_ERROR_BUS_HEAVY 0x08
+#define PCAN_USB_ERROR_BUS_OFF   0x10
+#define PCAN_USB_ERROR_RXQEMPTY  0x20
+#define PCAN_USB_ERROR_QOVR      0x40
+#define PCAN_USB_ERROR_TXQFULL   0x80
+

drivers/net/can/usb/pcan_usb.c line 84

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+/* SJA1000 modes */
+#define SJA1000_MODE_NORMAL 0x00
+#define SJA1000_MODE_INIT   0x01
+
+/* tick duration = 42.666 us => 
+ * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
+ *  accuracy = 10^-7
+ */
+#define PCAN_USB_TS_DIV_SHIFTER        20
+#define PCAN_USB_TS_US_PER_TICK        44739243

If you want to align the #defines please use tab between symbol and value

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+
+struct pcan_usb {
+	struct peak_usb_device pcandev; /* must be the first member */
+	struct peak_time_ref time_ref;
+};
+
+/*
+ * Send the given PCAN-USB command synchronously
+ */
+static int pcan_usb_send_command(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)

drivers/net/can/usb/pcan_usb.c line 104

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+{
+	int actual_length;
+	struct pcan_usb_parameter cmd;
+	int err;
+
+	/* usb device unregistered? */
+	if (!(dev->state & PCAN_USB_STATE_CONNECTED)) return 0;
+
+	cmd.function = f;
+	cmd.number = n;

you can use C99 initialisers instead:

	struct pcan_usb_parameter cmd = {
		.function = f,
		.number = n,
	};

then parameters in automatically set with 0x0....

drivers/net/can/usb/pcan_usb.c line 106

Quoted 2 lines
drivers/net/can/usb/pcan_usb.c new file +2 -0
+
+	if (p != NULL)

if (p)

drivers/net/can/usb/pcan_usb.c line 107

Quoted 1 line
drivers/net/can/usb/pcan_usb.c new file +1 -0
+		memcpy(&cmd.parameters[0], p, PCAN_USB_PARAMETER_LEN);

I'd write memcpy(cmd.parameters, p, ARRAY_SIZE(cmd.parameters))

drivers/net/can/usb/pcan_usb.c line 109

Quoted 2 lines
drivers/net/can/usb/pcan_usb.c new file +2 -0
+	else
+		memset(&cmd.parameters[0], '\0', PCAN_USB_PARAMETER_LEN);

...and you can remove the memset here.

Quoted 11 lines
drivers/net/can/usb/pcan_usb.c new file +11 -0
+
+	err = usb_bulk_msg(dev->udev, 
+	                   usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
+	                   &cmd, sizeof(struct pcan_usb_parameter),
+	                   &actual_length, PCAN_USB_COMMAND_TIMEOUT);
+	if (err) 
+		netdev_err(dev->netdev, 
+		           "sending command function=0x%x number=0x%x failure: %d\n",
+		           f, n, err);
+
+	return err;

drivers/net/can/usb/pcan_usb.c line 130

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+}
+
+static int pcan_usb_wait_response(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
+{
+	int actual_length;
+	struct pcan_usb_parameter cmd;
+	int err;
+
+	/* usb device unregistered? */
+	if (!(dev->state & PCAN_USB_STATE_CONNECTED)) return 0;

please put the return in a seperate line.

drivers/net/can/usb/pcan_usb.c line 136

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+
+	/* first, send command */
+	err = pcan_usb_send_command(dev, f, n, NULL);
+	if (err) {
+		return err;
+	}

please remove the { }

drivers/net/can/usb/pcan_usb.c line 109

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+
+	cmd.function = f;
+	cmd.number = n;
+	memset(&cmd.parameters[0], '\0', PCAN_USB_PARAMETER_LEN);

please use C99 initialisers

drivers/net/can/usb/pcan_usb.c line 152

Quoted 12 lines
drivers/net/can/usb/pcan_usb.c new file +12 -0
+
+	/* then, wait for the response */
+	mdelay(5);
+	err = usb_bulk_msg(dev->udev, 
+	                   usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
+	                   &cmd, sizeof(struct pcan_usb_parameter),
+	                   &actual_length, PCAN_USB_COMMAND_TIMEOUT);
+	if (err)
+		netdev_err(dev->netdev, 
+		           "waiting response function=0x%x number=0x%x failure: %d\n",
+		           f, n, err);
+	else if (p) memcpy(p, &cmd.parameters[0], PCAN_USB_PARAMETER_LEN);

please put the memcpy in a seperate line. I'd use cmd.parameters instead of &cmd.parameters[0], please use ARRAY_SIZE.

drivers/net/can/usb/pcan_usb.c line 162

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+
+	return err;
+}
+
+static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+
+	args[0] = 0;
+	args[1] = mode;

the array can be initializes in C99 style, too:

	u8 args[PCAN_USB_PARAMETER_LEN] = {
		[0] = 0,
		[1] = mode,
	};

The [0] can be skipped, by you might want to include it for documentation purpose.

Please add a newline in before return

drivers/net/can/usb/pcan_usb.c line 171

Quoted 9 lines
drivers/net/can/usb/pcan_usb.c new file +9 -0
+	return pcan_usb_send_command(dev, 9, 2, args);
+}
+
+static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+
+	args[0] = onoff ? 1 : 0;
+	return pcan_usb_send_command(dev, 3, 2, args);

please use/add C99 initialisers + newline, too

drivers/net/can/usb/pcan_usb.c line 179

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+}
+
+static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+
+	args[0] = onoff ? 1 : 0;
+	return pcan_usb_send_command(dev, 3, 3, args);

dito

drivers/net/can/usb/pcan_usb.c line 187

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+}
+
+static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+
+	args[0] = onoff ? 1 : 0;
+	return pcan_usb_send_command(dev, 10, 2, args);

dito

I see some magic numbers for the command here, does it make sense to define an enum for them?

drivers/net/can/usb/pcan_usb.c line 192

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+}
+
+/*
+ * This routine was stolen from drivers/net/can/sja1000/sja1000.c
+ */

Nice for crediting this, but IMHO no need to write it into the code.

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+static int pcan_usb_set_bittiming(struct peak_usb_device *dev, struct can_bittiming *bt)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+	u8 btr0, btr1;
+

drivers/net/can/usb/pcan_usb.c line 207

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+	btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
+	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) \
+	     | (((bt->phase_seg2 - 1) & 0x7) << 4);
+	if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+		btr1 |= 0x80;
+
+	args[0] = btr1;
+	args[1] = btr0;
+
+	printk("btr0=0x%02x btr1=0x%02x", btr0, btr1);

Please use netdev_LEVEL() instead of printk

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+
+	return pcan_usb_send_command(dev, 1, 2, args);
+}
+
+static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)

drivers/net/can/usb/pcan_usb.c line 222

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+{
+	int err;
+
+	err = pcan_usb_set_bus(dev, onoff);
+	if (err) 
+		return err;
+
+	if (!onoff) {
+		err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
+	}

please remove the { }

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+
+	return err;
+}
+
+static int pcan_usb_get_serial_number(struct peak_usb_device *dev, u32 *serial_number)

drivers/net/can/usb/pcan_usb.c line 237

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+	int err;
+
+	err = pcan_usb_wait_response(dev, 6, 1, args);
+	if (err) 
+		netdev_err(dev->netdev, "getting serial number failure: %d\n", err);
+	else {
+		if (serial_number) memcpy(serial_number, &args[0], 4);
+	}

please remove the { }, no command after the if, please.

Hmmm that serial number to u32 memcpy looks fishy, I smell endianess problem. Can you test your driver on an big endian machine, like powerpc?

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+
+	return err;
+}
+

drivers/net/can/usb/pcan_usb.c line 251

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+static int pcan_usb_get_device_number(struct peak_usb_device *dev, u32 *device_number)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+	int err;
+
+	err = pcan_usb_wait_response(dev, 4, 1, args);
+	if (err) 
+		netdev_err(dev->netdev, "getting device number failure: %d\n", err);
+	else {
+		if (device_number) *device_number = args[0];

please remove the { }, no command in the same line as the "if"

Quoted 25 lines
drivers/net/can/usb/pcan_usb.c new file +25 -0
+	}
+
+	return err;
+}
+
+static void pcan_usb_update_time_word(struct pcan_usb *pdev, u16 ts16, u8 s)
+{
+
+	if (s == 0)
+		peak_usb_set_timestamp_now(&pdev->time_ref, ts16);
+	else 
+		peak_usb_update_timestamp_now(&pdev->time_ref, ts16);
+}
+
+/*
+ * callback for bulk IN urb
+ */
+static int pcan_usb_decode_msg(struct peak_usb_device *dev, struct urb *urb)
+{
+	struct pcan_usb *pdev = (struct pcan_usb *)dev;
+	struct net_device *netdev = dev->netdev;
+	int err = 0;
+
+	if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
+		struct net_device_stats *stats = &netdev->stats;

drivers/net/can/usb/pcan_usb.c line 286

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+
+		u8 *ibuf = urb->transfer_buffer;
+		const u8 msg_count = ibuf[1];
+		int frm_count = 0;
+		u8 i;
+
+		ibuf += PCAN_USB_MSG_HEADER_LEN;
+
+		for (i = 0; i < msg_count && !err; i++) {
+

please remove that extra blame line

drivers/net/can/usb/pcan_usb.c line 290

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+			struct sk_buff *skb;
+			struct can_frame *cf;
+			struct timeval tv;
+			u16 tmp16, ts16=0, dts=0, prev_ts8=0;

please add spaces around =

drivers/net/can/usb/pcan_usb.c line 294

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+			u8 sl = *ibuf++;
+			u8 rec_len = (sl & PCAN_USB_STATUSLEN_DLC);
+
+			/* handle error frames here */

I suggest to put the error handling in a sub function. If you have

Quoted 21 lines
drivers/net/can/usb/pcan_usb.c new file +21 -0
+			if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
+				u8 f = *ibuf++;
+				u8 n = *ibuf++;
+
+				if (sl & PCAN_USB_STATUSLEN_TIMESTAMP) {
+					/* only the first packet supplies a word timestamp */
+					if (i == 0) {
+						memcpy(&tmp16, ibuf, 2);
+						ibuf += 2;
+						ts16 = le16_to_cpu(tmp16);
+						dts = 0;
+					}
+					else {
+						u8 ts8 = *ibuf++;
+
+						if (dts) {
+							dts &= 0xff00;
+							if (ts8 < prev_ts8)
+								dts += 0x100;
+						}
+

drivers/net/can/usb/pcan_usb.c line 325

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+						dts |= ts8;
+						prev_ts8 = ts8;
+
+					}
+				}
+
+				switch (f) {
+
+				case 1:
+

please reove these two empty lines. I think you should define an enum for the functions.

drivers/net/can/usb/pcan_usb.c line 328

Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+					/* allocate an skb to store the error frame */
+					skb = alloc_can_err_skb(netdev, &cf);
+					if (skb == NULL) {

!skb

please talk to Wolfgang for the error handling, he is the expert now :)

Quoted 52 lines
drivers/net/can/usb/pcan_usb.c new file +52 -0
+						err = -ENOMEM;
+						break;
+					}
+
+					if (n & (PCAN_USB_ERROR_RXQOVR|PCAN_USB_ERROR_QOVR)) {
+						cf->can_id |= CAN_ERR_CRTL;
+						cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
+						stats->rx_over_errors++;
+						stats->rx_errors++;
+					}
+					if (n & PCAN_USB_ERROR_BUS_OFF) {
+						cf->can_id |= CAN_ERR_BUSOFF;
+						can_bus_off(netdev);
+					}
+					if (n & PCAN_USB_ERROR_BUS_HEAVY) {
+						cf->can_id |= CAN_ERR_CRTL;
+						cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
+						dev->can.can_stats.error_passive++;
+					}
+					if (n & PCAN_USB_ERROR_BUS_LIGHT) {
+						cf->can_id |= CAN_ERR_CRTL;
+						cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
+						dev->can.can_stats.error_warning++;
+					}
+
+					if (cf->can_id != CAN_ERR_FLAG) {
+						if (sl & PCAN_USB_STATUSLEN_TIMESTAMP) {
+							peak_usb_get_timestamp_tv(&pdev->time_ref, ts16+dts, &tv);
+							skb->tstamp = timeval_to_ktime(tv);
+						}
+						netif_rx(skb);
+						stats->rx_packets++;
+					}
+
+					/* v3: sometimes the telegram carries 3 additional data */
+					/* without note in ucStatusLen. */
+					ibuf += rec_len;
+					break;
+
+				case 2:
+					/* analog values (ignored) */
+					ibuf += 2;
+					break;
+
+				case 3:
+					/* bus load (ignored) */
+					ibuf++;
+					break;
+
+				case 4:
+					/* only timestamp */
+					memcpy(&tmp16, ibuf, 2);

drivers/net/can/usb/pcan_usb.c line 390

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+					ibuf += 2;
+					ts16 = le16_to_cpu(tmp16);
+					pcan_usb_update_time_word(pdev, ts16, i);
+					break;
+
+				case 5:
+					/* error frame/bus event */
+					if (n & PCAN_USB_ERROR_TXQFULL) {
+						netdev_info(netdev, "device Tx queue full)\n");
+					}

remove { }

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+					ibuf += rec_len;
+					break;
+
+				case 10:

drivers/net/can/usb/pcan_usb.c line 404

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+					/* future... */
+					break;
+
+				default:
+					netdev_err(netdev, "unexpected function %u\n", f);
+					break;
+				}
+			}
+
+			/* handle normal can frames here */

please use an extra fucntion for normal frames

Quoted 22 lines
drivers/net/can/usb/pcan_usb.c new file +22 -0
+			else {
+
+				u8 d = 0;
+
+				skb = alloc_can_skb(netdev, &cf);
+				if (skb == NULL) {
+					err = -ENOMEM;
+					break;
+				}
+
+				if (sl & PCAN_USB_STATUSLEN_EXT_ID) {
+					u32 tmp32;
+					memcpy(&tmp32, ibuf, 4);
+					ibuf += 4;
+					cf->can_id = le32_to_cpu(tmp32 >> 3) | CAN_EFF_FLAG;
+				}
+				else {
+					memcpy(&tmp16, ibuf, 2);
+					ibuf += 2;
+					cf->can_id = le16_to_cpu(tmp16 >> 5);
+				}
+

drivers/net/can/usb/pcan_usb.c line 436

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+				cf->can_dlc = get_can_dlc(rec_len);
+
+				/* first data packet timestamp is a word */
+				if (!frm_count++) {
+					memcpy(&tmp16, ibuf, 2);
+					ibuf += 2;
+					ts16 = le16_to_cpu(tmp16);
+					dts = 0;
+				}
+				else {

the preferred coding style is
} else {

drivers/net/can/usb/pcan_usb.c line 317

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+					u8 ts8 = *ibuf++;
+
+					if (dts) {
+						dts &= 0xff00;
+						if (ts8 < prev_ts8)
+							dts += 0x100;
+					}
+
+					dts |= ts8;
+					prev_ts8 = ts8;

I think I've seen this code before, please add a function for it.

drivers/net/can/usb/pcan_usb.c line 454

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+
+				}
+
+				/* read data */
+				if (sl & PCAN_USB_STATUSLEN_RTR) {
+					cf->can_id |= CAN_RTR_FLAG;
+				}
+				else {

} else { - or better get remove the { }

drivers/net/can/usb/pcan_usb.c line 461

Quoted 7 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
+					for (; d < rec_len; d++)
+						cf->data[d] = *ibuf++;
+				}
+
+				for (; d < 8; d++) {
+					cf->data[d] = 0;
+				}

memset?

drivers/net/can/usb/pcan_usb.c line 471

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+
+				peak_usb_get_timestamp_tv(&pdev->time_ref, ts16+dts, &tv);
+				skb->tstamp = timeval_to_ktime(tv);
+				netif_rx(skb);
+
+				stats->rx_packets++;
+				stats->rx_bytes += cf->can_dlc;
+			}
+
+			if ((ibuf - (u8 *)urb->transfer_buffer) > urb->transfer_buffer_length) {

What does this check do? You should do bounds checking of ibuf before accessing it.

drivers/net/can/usb/pcan_usb.c line 481

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+				netdev_err(netdev, "usb message format error\n");
+				err = -EINVAL;
+				break;
+			}
+		}
+	}
+	else if (urb->actual_length > 0) {
+		netdev_err(netdev, "usb message length error (%u)\n", urb->actual_length);
+		err = -EINVAL;
+	}

I'd move the error checking to the beginning of the function. Return early if you detect an error, so that you can get rid of (at least) one indention level.

Quoted 14 lines
drivers/net/can/usb/pcan_usb.c new file +14 -0
+
+	return err;
+}
+
+static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb, u8 *obuf, size_t *size)
+{
+	struct net_device *netdev = dev->netdev;
+	struct net_device_stats *stats = &netdev->stats;
+	struct can_frame *cf = (struct can_frame *)skb->data;
+	u8 *pc;
+
+	obuf[0] = 2;
+	obuf[1] = 1;
+

drivers/net/can/usb/pcan_usb.c line 505

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+	pc = &obuf[PCAN_USB_MSG_HEADER_LEN];
+
+	/* status/len byte */
+	*pc = cf->can_dlc;
+	if (cf->can_id & CAN_RTR_FLAG) 
+		*pc |= PCAN_USB_STATUSLEN_RTR;
+	
+	/* can id */
+	if (cf->can_id & CAN_EFF_FLAG) {
+		u32 tmp32 = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
                ^^^ should be __le32 then

drivers/net/can/usb/pcan_usb.c line 511

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+		tmp32 <<= 3;
+		*pc |= PCAN_USB_STATUSLEN_EXT_ID;
+		memcpy(++pc, &tmp32, 4);
+		pc += 4;
+	}
+	else {

} else {

drivers/net/can/usb/pcan_usb.c line 512

Quoted 1 line
drivers/net/can/usb/pcan_usb.c new file +1 -0
+		u16 tmp16 = (u16 )cpu_to_le32(cf->can_id & CAN_ERR_MASK);
                            ^^^^^^^^^^^^^^^^^

why convert to le32 and then cast to u16?

drivers/net/can/usb/pcan_usb.c line 519

Quoted 7 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
+		tmp16 <<= 5;
+		memcpy(++pc, &tmp16, 2);
+		pc += 2;
+	}
+
+	/* can data */
+	if ((cf->can_id & CAN_RTR_FLAG) == 0) {

if (cf->can_id & CAN_RTR_FLAG)

drivers/net/can/usb/pcan_usb.c line 520

Quoted 1 line
drivers/net/can/usb/pcan_usb.c new file +1 -0
+		memcpy(pc, &cf->data[0], cf->can_dlc);
		memcpy(pc, cf->data, cf->can_dlc);
Quoted 124 lines
drivers/net/can/usb/pcan_usb.c new file +124 -0
+		pc += cf->can_dlc;
+	}
+
+	/* Hmm... pcan driver sets the count of messages sent in the last byte... */
+	obuf[(*size)-1] = (u8 )(stats->tx_packets & 0xff);
+
+	return 0;
+}
+
+/*
+ * Start interface
+ */
+static int pcan_usb_start(struct peak_usb_device *dev)
+{
+	struct pcan_usb *pdev = (struct pcan_usb *)dev; 
+	int err;
+
+	/* number of bits used in timestamps read from adapter struct */
+	peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
+
+	/* If revision greater than 3, can put silent mode on/off*/
+	if (dev->device_rev > 3) {
+	
+		err = pcan_usb_set_silent(dev, 
+		                          (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY));
+		if (err)
+			goto failed;
+	}
+
+	err = pcan_usb_set_ext_vcc(dev, 0);
+	if (err)
+		goto failed;
+
+	return 0;
+
+failed:
+
+	return err;
+}
+
+static int pcan_usb_init(struct peak_usb_device *dev)
+{
+	u32 serial_number;
+	int err;
+
+	err = pcan_usb_get_serial_number(dev, &serial_number);
+	if (!err)
+		netdev_info(dev->netdev, "serial %08X\n", serial_number);
+
+	return err;
+}
+
+/*
+ * probe function for new PCAN-USB usb interface
+ */
+static int pcan_usb_probe(struct usb_interface *intf)
+{
+	struct usb_host_interface *iface_desc;
+	int i;
+
+	/* check endpoint addresses (numbers) and associated max data length */
+	/* (only from setting 0) */
+	iface_desc = &intf->altsetting[0];
+
+	/* check interface endpoint addresses */
+	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
+		struct usb_endpoint_descriptor *endpoint = &iface_desc->endpoint[i].desc;
+
+		/* Below is the list of valid ep addreses. All other ep address */
+		/* is considered as not-CAN interface address => no dev created */
+		switch (endpoint->bEndpointAddress) {
+		case PCAN_USB_EP_CMDOUT:
+		case PCAN_USB_EP_CMDIN:
+		case PCAN_USB_EP_MSGOUT:
+		case PCAN_USB_EP_MSGIN:
+			break;
+		default:
+			return -ENODEV;
+		}
+	}
+ 
+	return 0;
+}
+
+/* 
+ * Describe the PCAN-USB adapter 
+ */
+struct peak_usb_adapter pcan_usb = {
+	.name = "PCAN-USB",
+	.device_id = PCAN_USB_PRODUCT_ID,
+	.ctrl_count = 1,
+	.clock = {
+		.freq = PCAN_USB_CRYSTAL_HZ/2,
+	},
+	.bittiming_const = {
+		.name = "pcan_usb",
+		.tseg1_min = 1,
+		.tseg1_max = 16,
+		.tseg2_min = 1,
+		.tseg2_max = 8,
+		.sjw_max = 4,
+		.brp_min = 1,
+		.brp_max = 64,
+		.brp_inc = 1,
+	},
+
+	/* size of device private data */
+	.sizeof_dev_private = sizeof(struct pcan_usb),
+	
+	/* timestamps usage */
+	.ts_used_bits = 16,
+	.ts_period = 24575, /* calibration period in ts. */
+	.us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us = (ts * scale) >> shift */
+	.us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER,
+
+	/* give here commands/messages in/out endpoints */
+	.ep_msg_in = PCAN_USB_EP_MSGIN,
+	.ep_msg_out = {PCAN_USB_EP_MSGOUT},
+
+	/* size of rx/tx usb buffers */
+	.rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
+	.tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
+
+	/* device callbacks */

drivers/net/can/usb/pcan_usb.c line 654

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+	.intf_probe = pcan_usb_probe,
+	.device_init = pcan_usb_init,
+	.device_set_bus = pcan_usb_write_mode,
+	.device_set_bittiming = pcan_usb_set_bittiming,
+	.device_get_device_number = pcan_usb_get_device_number,
+	.device_decode_msg = pcan_usb_decode_msg,
+	.device_encode_msg = pcan_usb_encode_msg,
+	.device_start = pcan_usb_start,
+};
+

that's it for now

Marc

Signature · 4 lines
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   |
#3 lore raw

Hi Marc,

Thanks for your review. Please, find my answers and comments below...

Quoted 9 lines · Marc Kleine-Budde
Quoted 4 lines · On 12/16/2011 11:19 AM, [email protected] wrote:

From 1cee3be3875f27a2ee3942b00d611450f4369325 Mon Sep 17 00:00:00 2001
From: Stephane Grosjean<[email protected]>
Date: Fri, 16 Dec 2011 11:11:37 +0100
Subject: [PATCH] Add support for PEAK-System GmbH CAN USB adapters (v2)

The driver has some unusal coding style, please use checkpatch and do sparse checking (compile the drivers with C=2). Make use of C99 initialisers. Use foo->bar not&foo->bar[0] to get the pointer for the first array element

Ok, I did use the C99 style and removed my &foo->bar[0] too, everywhere in the driver.

Quoted 7 lines
drivers/net/can/usb/Makefile +2 -0
+
+ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
  ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG

^^^^^^^^^^

one ccflag should be enough :)

I think so. :-)

Quoted 4 lines

+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. please remove the address

So I changed the above 3 lines into the two below:

+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc..

Is it ok?

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+#define PCAN_USB_EP_CMDIN    (PCAN_USB_EP_CMDOUT|USB_DIR_IN)
+#define PCAN_USB_EP_MSGOUT   2
+#define PCAN_USB_EP_MSGIN    (PCAN_USB_EP_MSGOUT|USB_DIR_IN)
                                                   ^^^

please add spaces around the |

Ok.

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+#define PCAN_USB_PARAMETER_LEN   14
+struct __packed pcan_usb_parameter {
+	u8 function;
+	u8	number;
          ^^^

please use one space

Ok.

Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +2 -0
+#define PCAN_USB_TS_DIV_SHIFTER        20
+#define PCAN_USB_TS_US_PER_TICK        44739243

If you want to align the #defines please use tab between symbol and value

So I put ONE tab in between the symbol and the value, everywhere in the driver (IMHO, using tab(s) here too much relies on editor's tab size. Putting white spaces here fixes that... but it's my humble opinion only).

Quoted 11 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+
+	cmd.function = f;
+	cmd.number = n;

you can use C99 initialisers instead:

	struct pcan_usb_parameter cmd = {
		.function = f,
		.number = n,
	};

then parameters in automatically set with 0x0....

Ok.

Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	if (p != NULL)

if (p)

Ok.

Quoted 2 lines

drivers/net/can/usb/pcan_usb.c line 107

Quoted 1 line
drivers/net/can/usb/pcan_usb.c new file +1 -0
+		memcpy(&cmd.parameters[0], p, PCAN_USB_PARAMETER_LEN);

I'd write memcpy(cmd.parameters, p, ARRAY_SIZE(cmd.parameters))

So did I...

Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	else
+		memset(&cmd.parameters[0], '\0', PCAN_USB_PARAMETER_LEN);

...and you can remove the memset here.

Ok.

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+
+	/* usb device unregistered? */
+	if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;

please put the return in a seperate line.

Ok (done everywhere in the driver too).

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+	err = pcan_usb_send_command(dev, f, n, NULL);
+	if (err) {
+		return err;
+	}

please remove the { }

Ok (done everywhere in the driver too).

Quoted 10 lines

the array can be initializes in C99 style, too:

	u8 args[PCAN_USB_PARAMETER_LEN] = {
		[0] = 0,
		[1] = mode,
	};

The [0] can be skipped, by you might want to include it for documentation purpose.

No, I don't... So I removed the [0] = 0 too...

Quoted 1 line

Please add a newline in before return

Ok

Quoted 4 lines

dito

I see some magic numbers for the command here, does it make sense to define an enum for them?

TODO...

Quoted 6 lines

drivers/net/can/usb/pcan_usb.c line 192

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+}
+
+/*
+ * This routine was stolen from drivers/net/can/sja1000/sja1000.c
+ */

Nice for crediting this, but IMHO no need to write it into the code.

Ok, removed now (FYI it's not so nice to steal something which was already stolen ;-)

Quoted 8 lines
Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+static int pcan_usb_set_bittiming(struct peak_usb_device *dev, struct can_bittiming *bt)
+{
+	u8 args[PCAN_USB_PARAMETER_LEN];
+	u8 btr0, btr1;
+
+	btr0 = ((bt->brp - 1)&  0x3f) | (((bt->sjw - 1)&  0x3)<<  6);
+	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1)&  0xf) \
+	     | (((bt->phase_seg2 - 1)&  0x7)<<  4);
Quoted 8 lines

drivers/net/can/usb/pcan_usb.c line 207

Quoted 7 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
+	if (dev->can.ctrlmode&  CAN_CTRLMODE_3_SAMPLES)
+		btr1 |= 0x80;
+
+	args[0] = btr1;
+	args[1] = btr0;
+
+	printk("btr0=0x%02x btr1=0x%02x", btr0, btr1);

Please use netdev_LEVEL() instead of printk

I talked about that in my previous e-mail to Sebastian: these printk()s are called in sequence: this one (for example) ISNOT the first one but occurs next to a netdev_info() macro (please see function "peak_usb_set_bittiming()" in "pcan_usb_core.c") ... According to that, if you think that the occurrence of some kernel event(s) in between the two messages (which could printk() msgs too) is to be taken into account, I'll think about a new way of putting bitrate info.

Quoted 11 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
+
+	err = pcan_usb_wait_response(dev, 6, 1, args);
+	if (err)
+		netdev_err(dev->netdev, "getting serial number failure: %d\n", err);
+	else {
+		if (serial_number) memcpy(serial_number,&args[0], 4);
+	}

please remove the { }, no command after the if, please.

Hmmm that serial number to u32 memcpy looks fishy, I smell endianess problem. Can you test your driver on an big endian machine, like powerpc?

Ok no need to test, you're right (not very proud about that ;-)

Quoted 5 lines

drivers/net/can/usb/pcan_usb.c line 294

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+			u8 sl = *ibuf++;
+			u8 rec_len = (sl&  PCAN_USB_STATUSLEN_DLC);
+
+			/* handle error frames here */

I suggest to put the error handling in a sub function. If you have

Hmmm... I also thought about that, but the amount of contextual variables to pass as arguments is quite large (i, ibuf, dts...): for that kind of hw, for example, timestamp coding depends on its place (rank) in the message. TODO

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+				switch (f) {
+
+				case 1:
+

please reove these two empty lines. I think you should define an enum for the functions.

Ok.

Quoted 6 lines

drivers/net/can/usb/pcan_usb.c line 328

Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+					/* allocate an skb to store the error frame */
+					skb = alloc_can_err_skb(netdev,&cf);
+					if (skb == NULL) {

!skb

please talk to Wolfgang for the error handling, he is the expert now :)

???
What should I talk about? Is the way errors are handling wrong?

Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +2 -0
+
+			/* handle normal can frames here */

please use an extra fucntion for normal frames

Same answer than error handling (see above): not so easy nor so obvious...

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+				}
+				else {

the preferred coding style is
} else {

Ok (tried to change the whole driver files)

Quoted 11 lines

drivers/net/can/usb/pcan_usb.c line 317

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+					u8 ts8 = *ibuf++;
+
+					if (dts) {
+						dts&= 0xff00;
+						if (ts8<  prev_ts8)
+							dts += 0x100;
+					}
+
+					dts |= ts8;
+					prev_ts8 = ts8;

I think I've seen this code before, please add a function for it.

Ok, this is done now.

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+				else {

} else { - or better get remove the { }

drivers/net/can/usb/pcan_usb.c line 457

Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+					for (; d<  rec_len; d++)
+						cf->data[d] = *ibuf++;
+				}

Ok ({} removed)

Quoted 5 lines

drivers/net/can/usb/pcan_usb.c line 461

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+
+				for (; d<  8; d++) {
+					cf->data[d] = 0;
+				}

memset?

memset(cf->data+d, '\0', 8 - d); /* and not memset(&cf->data[d],...;-), even if I personally think that this last syntax is more explicit */

Quoted 4 lines
Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+
+				peak_usb_get_timestamp_tv(&pdev->time_ref, ts16+dts,&tv);
+				skb->tstamp = timeval_to_ktime(tv);
+				netif_rx(skb);
Quoted 8 lines

drivers/net/can/usb/pcan_usb.c

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+
+				stats->rx_packets++;
+				stats->rx_bytes += cf->can_dlc;
+			}
+
+			if ((ibuf - (u8 *)urb->transfer_buffer)>  urb->transfer_buffer_length) {

What does this check do? You should do bounds checking of ibuf before accessing it.

Yes you're right... My problem is that the size of the records (in the message) to decode is difficult to predict (see timestamp management for example). Well "difficult" is not the good word, but I thought it would cost too much code for checking next ibuf each time.. But ok, I'll fix that too..

Quoted 5 lines
Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+				netdev_err(netdev, "usb message format error\n");
+				err = -EINVAL;
+				break;
+			}
+		}
Quoted 8 lines

drivers/net/can/usb/pcan_usb.c line 481

Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+	}
+	else if (urb->actual_length>  0) {
+		netdev_err(netdev, "usb message length error (%u)\n", urb->actual_length);
+		err = -EINVAL;
+	}

I'd move the error checking to the beginning of the function. Return early if you detect an error, so that you can get rid of (at least) one indention level.

I agree with you but for the indentation argument only: I think current code enables to generally do only a single test (actual_length > PCAN_USB_MSG_HEADER_LEN) than the one you're proposing. Moreover, even if I know that gcc does some quite incredible optimizations sometimes, I always prefer defining strict local variables (I mean, defining variable only in the block where it is used). But once again, if the linux kernel coding style strictly requests it (or if any other arg regarding optimization comes from the list), I don't see any problem to move these tests where you propose.

Quoted 8 lines
Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+
+	return err;
+}
+
+static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb, u8 *obuf, size_t *size)
+{
+	struct net_device *netdev = dev->netdev;
+	struct net_device_stats *stats =&netdev->stats;
Quoted 8 lines

drivers/net/can/usb/pcan_usb.c line 497

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+	struct can_frame *cf = (struct can_frame *)skb->data;
+	u8 *pc;
+
+	obuf[0] = 2;
+	obuf[1] = 1;
+
+	pc =&obuf[PCAN_USB_MSG_HEADER_LEN];
+

I think I also have to change the above $obuf[] into:

pc = obuf + PCAN_USB_MSG_HEADER_LEN;

right?

Quoted 9 lines

drivers/net/can/usb/pcan_usb.c

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+	/* status/len byte */
+	*pc = cf->can_dlc;
+	if (cf->can_id&  CAN_RTR_FLAG)
+		*pc |= PCAN_USB_STATUSLEN_RTR;
+	
+	/* can id */
+	if (cf->can_id&  CAN_EFF_FLAG) {
+		u32 tmp32 = cpu_to_le32(cf->can_id&  CAN_ERR_MASK);
                 ^^^ should be __le32 then

Ok.

Quoted 7 lines

drivers/net/can/usb/pcan_usb.c line 511

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+		tmp32<<= 3;
+		*pc |= PCAN_USB_STATUSLEN_EXT_ID;
+		memcpy(++pc,&tmp32, 4);
+		pc += 4;
+	}
+	else {

} else {

Ok.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		u16 tmp16 = (u16 )cpu_to_le32(cf->can_id&  CAN_ERR_MASK);
                             ^^^^^^^^^^^^^^^^^

why convert to le32 and then cast to u16?

...because can_id is u32 and I only want to keep the lowest 16 bits . Is there another way to do that?

Quoted 8 lines

drivers/net/can/usb/pcan_usb.c

Quoted 7 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
+		tmp16<<= 5;
+		memcpy(++pc,&tmp16, 2);
+		pc += 2;
+	}
+
+	/* can data */
+	if ((cf->can_id&  CAN_RTR_FLAG) == 0) {

if (cf->can_id& CAN_RTR_FLAG)

Ok, but only if you wanted to say:

if (!(cf->can_id& CAN_RTR_FLAG)) {

;-)

Quoted 2 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		memcpy(pc,&cf->data[0], cf->can_dlc);
		memcpy(pc, cf->data, cf->can_dlc);

Ok.

drivers/net/can/usb/pcan_usb.c line 584

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+	/* check endpoint addresses (numbers) and associated max data length */
+	/* (only from setting 0) */
+	iface_desc =&intf->altsetting[0];
+

What about the above &altsetting[0] ? Should it be also changed into:

iface_desc = intf->altsetting;

?

Quoted 5 lines

that's it for now

Marc

Many thanks for all of your work, Marc.

Stéphane

#4 lore raw

Hallo Stéphane,

Quoted 3 lines · Grosjean Stephane

Hi Marc,

Thanks for your review. Please, find my answers and comments below...

...

Quoted 5 lines · Marc Kleine-Budde
Quoted 1 line

please talk to Wolfgang for the error handling, he is the expert now :)

???
What should I talk about? Is the way errors are handling wrong?

Please fix your coding style first, then I will have a closer look.

Wolfgang.

#5 lore raw
In reply to Grosjean Stephane
Quoted 8 lines · Grosjean Stephane

I think so. :-)

Quoted 6 lines

+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. please remove the address

Quoted 6 lines

So I changed the above 3 lines into the two below:

+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc..

Is it ok?

Just remove completely.

Quoted 14 lines
Quoted 5 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+#define PCAN_USB_EP_CMDIN    (PCAN_USB_EP_CMDOUT|USB_DIR_IN)
+#define PCAN_USB_EP_MSGOUT   2
+#define PCAN_USB_EP_MSGIN    (PCAN_USB_EP_MSGOUT|USB_DIR_IN)
                                                   ^^^

please add spaces around the |

Ok.

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+#define PCAN_USB_PARAMETER_LEN   14
+struct __packed pcan_usb_parameter {
+    u8 function;
+    u8    number;
          ^^^

please use one space

Ok.

Quoted 6 lines
Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +2 -0
+#define PCAN_USB_TS_DIV_SHIFTER        20
+#define PCAN_USB_TS_US_PER_TICK        44739243

If you want to align the #defines please use tab between symbol and value

So I put ONE tab in between the symbol and the value, everywhere in the driver (IMHO, using tab(s) here too much relies on editor's tab size. Putting white spaces here fixes that... but it's my humble opinion only).

For kernel code a tab is 8 spaces, see Documentation/CodingStyle

Quoted 6 lines

Chapter 1: Indentation

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

[...]

Quoted 10 lines
Quoted 2 lines
Quoted 1 line

+ printk("btr0=0x%02x btr1=0x%02x", btr0, btr1);

Please use netdev_LEVEL() instead of printk

I talked about that in my previous e-mail to Sebastian: these printk()s are called in sequence: this one (for example) ISNOT the first one but occurs next to a netdev_info() macro (please see function "peak_usb_set_bittiming()" in "pcan_usb_core.c") ... According to that, if you think that the occurrence of some kernel event(s) in between the two messages (which could printk() msgs too) is to be taken into account, I'll think about a new way of putting bitrate info.

+1, You should always end prints with "\n", continuation of print should be avoided.

Quoted 17 lines
Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+
+    err = pcan_usb_wait_response(dev, 6, 1, args);
+    if (err)
+        netdev_err(dev->netdev, "getting serial number failure:

%d\n", err);

drivers/net/can/usb/pcan_usb.c new file +3 -0
+    else {
+        if (serial_number) memcpy(serial_number,&args[0], 4);
+    }

please remove the { }, no command after the if, please.

Quoted 2 lines

Hmmm that serial number to u32 memcpy looks fishy, I smell endianess problem. Can you test your driver on an big endian machine, like powerpc?

Ok no need to test, you're right (not very proud about that ;-)

Quoted 4 lines
Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+            u8 sl = *ibuf++;
+            u8 rec_len = (sl&  PCAN_USB_STATUSLEN_DLC);
+
+            /* handle error frames here */
Quoted 6 lines
Quoted 1 line

I suggest to put the error handling in a sub function. If you have

Hmmm... I also thought about that, but the amount of contextual variables to pass as arguments is quite large (i, ibuf, dts...): for that kind of hw, for example, timestamp coding depends on its place (rank) in the message. TODO

You can put all these arguments into a struct and just pass the pointer to the struct.

Quoted 10 lines
Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+                switch (f) {
+
+                case 1:
+

please reove these two empty lines. I think you should define an enum for the functions.

Ok.

Quoted 3 lines
Quoted 3 lines
drivers/net/can/usb/pcan_usb.c new file +3 -0
+                    /* allocate an skb to store the error frame */
+                    skb = alloc_can_err_skb(netdev,&cf);
+                    if (skb == NULL) {
Quoted 6 lines
Quoted 3 lines

!skb

please talk to Wolfgang for the error handling, he is the expert now :)

???
What should I talk about? Is the way errors are handling wrong?

I haven't looked at it, but Wolfgang has really twisted his mind around error handling, so he's the expert now.

[...]

Quoted 8 lines
Quoted 5 lines

drivers/net/can/usb/pcan_usb.c line 461

Quoted 4 lines
drivers/net/can/usb/pcan_usb.c new file +4 -0
+
+                for (; d<  8; d++) {
+                    cf->data[d] = 0;
+                }

memset?

memset(cf->data+d, '\0', 8 - d); /* and not memset(&cf->data[d],...;-), even if I personally think that this last syntax is more explicit */

or even:

memset(cf->data, 0x0, sizeof(cf-data) - d);

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +2 -0
+
+                peak_usb_get_timestamp_tv(&pdev->time_ref,

ts16+dts,&tv);

drivers/net/can/usb/pcan_usb.c new file +3 -0
+                skb->tstamp = timeval_to_ktime(tv);
+                netif_rx(skb);
+
Quoted 8 lines
Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+                stats->rx_packets++;
+                stats->rx_bytes += cf->can_dlc;
+            }
+
+            if ((ibuf - (u8 *)urb->transfer_buffer)> 

urb->transfer_buffer_length) {

What does this check do? You should do bounds checking of ibuf before accessing it.

Quoted 5 lines

Yes you're right... My problem is that the size of the records (in the message) to decode is difficult to predict (see timestamp management for example). Well "difficult" is not the good word, but I thought it would cost too much code for checking next ibuf each time.. But ok, I'll fix that too..

You know these security guys? They'll modify your mouse so that the kernel would load the can driver, then send specially prepared messages and then boom, root exploit.

Quoted 45 lines
Quoted 11 lines

Review

Quoted 11 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+                netdev_err(netdev, "usb message format error\n");
+                err = -EINVAL;
+                break;
+            }
+        }
+    }
+    else if (urb->actual_length>  0) {
+        netdev_err(netdev, "usb message length error (%u)\n",

urb->actual_length);

(quoted diff) +2 -0
+        err = -EINVAL;
+    }
Quoted 3 lines

I'd move the error checking to the beginning of the function. Return early if you detect an error, so that you can get rid of (at least) one indention level.

I agree with you but for the indentation argument only: I think current code enables to generally do only a single test (actual_length > PCAN_USB_MSG_HEADER_LEN) than the one you're proposing. Moreover, even if I know that gcc does some quite incredible optimizations sometimes, I always prefer defining strict local variables (I mean, defining variable only in the block where it is used). But once again, if the linux kernel coding style strictly requests it (or if any other arg regarding optimization comes from the list), I don't see any problem to move these tests where you propose.

Quoted 9 lines
Quoted 9 lines
drivers/net/can/usb/pcan_usb.c new file +5 -0
+
+    return err;
+}
+
+static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct

sk_buff *skb, u8 *obuf, size_t *size)

drivers/net/can/usb/pcan_usb.c new file +3 -0
+{
+    struct net_device *netdev = dev->netdev;
+    struct net_device_stats *stats =&netdev->stats;
Quoted 8 lines

drivers/net/can/usb/pcan_usb.c line 497

Quoted 8 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+    struct can_frame *cf = (struct can_frame *)skb->data;
+    u8 *pc;
+
+    obuf[0] = 2;
+    obuf[1] = 1;
+
+    pc =&obuf[PCAN_USB_MSG_HEADER_LEN];
+

I think I also have to change the above $obuf[] into:

Quoted 3 lines

pc = obuf + PCAN_USB_MSG_HEADER_LEN;

right?

yes, would be consistent.

Quoted 17 lines
Quoted 10 lines

drivers/net/can/usb/pcan_usb.c

Quoted 9 lines
drivers/net/can/usb/pcan_usb.c new file +8 -0
+    /* status/len byte */
+    *pc = cf->can_dlc;
+    if (cf->can_id&  CAN_RTR_FLAG)
+        *pc |= PCAN_USB_STATUSLEN_RTR;
+   
+    /* can id */
+    if (cf->can_id&  CAN_EFF_FLAG) {
+        u32 tmp32 = cpu_to_le32(cf->can_id&  CAN_ERR_MASK);
                 ^^^ should be __le32 then

Ok.

Quoted 6 lines
Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+        tmp32<<= 3;
+        *pc |= PCAN_USB_STATUSLEN_EXT_ID;
+        memcpy(++pc,&tmp32, 4);
+        pc += 4;
+    }
+    else {
Quoted 7 lines
Quoted 1 line

} else {

Ok.

Quoted 3 lines
Quoted 1 line

+ u16 tmp16 = (u16 )cpu_to_le32(cf->can_id& CAN_ERR_MASK);

                             ^^^^^^^^^^^^^^^^^

why convert to le32 and then cast to u16?

...because can_id is u32 and I only want to keep the lowest 16 bits . Is there another way to do that?

there's no need for an explicit cast

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/net/can/usb/pcan_usb.c new file +7 -0
+        tmp16<<= 5;
+        memcpy(++pc,&tmp16, 2);
+        pc += 2;
+    }
+
+    /* can data */
+    if ((cf->can_id&  CAN_RTR_FLAG) == 0) {
Quoted 6 lines
Quoted 1 line

if (cf->can_id& CAN_RTR_FLAG)

Ok, but only if you wanted to say:

if (!(cf->can_id& CAN_RTR_FLAG)) {

;-)

Opps, yes :)

Quoted 10 lines
Quoted 3 lines
Quoted 2 lines

+ memcpy(pc,&cf->data[0], cf->can_dlc);

        memcpy(pc, cf->data, cf->can_dlc);

Ok.

drivers/net/can/usb/pcan_usb.c line 584

Quoted 5 lines

+ /* check endpoint addresses (numbers) and associated max data
length */

drivers/net/can/usb/pcan_usb.c new file +3 -0
+    /* (only from setting 0) */
+    iface_desc =&intf->altsetting[0];
+
Quoted 3 lines

What about the above &altsetting[0] ? Should it be also changed into:

iface_desc = intf->altsetting;

Yes

Marc

Signature · 4 lines
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   |
#6 lore raw
In reply to [email protected]

Hi,

some annotations also from me. ;-) Since Marc already did the coding style nitpicking I've tried to focus on logical stuff. I hope it is helpful.

Cheers,
Sebastian

Am 16.12.2011 11:19, schrieb [email protected]:

Quoted 11 lines
drivers/net/can/usb/pcan_usb.c new file +11 -0
+static int pcan_usb_wait_response(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
+{
+	int actual_length;
+	struct pcan_usb_parameter cmd;
+	int err;
+
+	/* usb device unregistered? */
+	if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;
+
+	/* first, send command */
+	err = pcan_usb_send_command(dev, f, n, NULL);

drivers/net/can/usb/pcan_usb.c line 143

Quoted 10 lines
drivers/net/can/usb/pcan_usb.c new file +10 -0
+	if (err) {
+		return err;
+	}
+
+	cmd.function = f;
+	cmd.number = n;
+	memset(&cmd.parameters[0], '\0', PCAN_USB_PARAMETER_LEN);
+
+	/* then, wait for the response */
+	mdelay(5);

A delay to wait for a response? What happens if the device takes a little bit longer. This looks to be not really robust.

Quoted 46 lines
drivers/net/can/usb/pcan_usb_core.c new file +46 -0
+	err = usb_bulk_msg(dev->udev,
+	                   usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
+	&cmd, sizeof(struct pcan_usb_parameter),
+	&actual_length, PCAN_USB_COMMAND_TIMEOUT);
+	if (err)
+		netdev_err(dev->netdev,
+		           "waiting response function=0x%x number=0x%x failure: %d\n",
+		           f, n, err);
+	else if (p) memcpy(p,&cmd.parameters[0], PCAN_USB_PARAMETER_LEN);
+
+	return err;
+}
+
+
+/*
+ * Start interface
+ */
+static int peak_usb_start(struct peak_usb_device *dev)
+{
+	struct net_device *netdev = dev->netdev;
+	int err, i;
+
+	for (i = 0; i<  PCAN_USB_MAX_RX_URBS; i++) {
+		struct urb *urb = NULL;
+		u8 *buf = NULL;
+
+		/* create a URB, and a buffer for it, to receive usb messages */
+		urb = usb_alloc_urb(0, GFP_KERNEL);
+		if (!urb) {
+			netdev_err(netdev, "No memory left for URBs\n");
+			return -ENOMEM;
+		}
+
+		buf = usb_alloc_coherent(dev->udev, dev->adapter->rx_buffer_size,
+		                         GFP_KERNEL,&urb->transfer_dma);
+		if (!buf) {
+			netdev_err(netdev, "No memory left for USB buffer\n");
+			usb_free_urb(urb);
+			return -ENOMEM;
+		}
+
+		usb_fill_bulk_urb(urb, dev->udev,
+		                  usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
+		                  buf, dev->adapter->rx_buffer_size,
+		                  peak_usb_read_bulk_callback, dev);
+		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

drivers/net/can/usb/pcan_usb_core.c line 352

Quoted 10 lines
drivers/net/can/usb/pcan_usb_core.c new file +10 -0
+		usb_anchor_urb(urb,&dev->rx_submitted);
+
+		err = usb_submit_urb(urb, GFP_KERNEL);
+		if (err) {
+			if (err == -ENODEV)
+				netif_device_detach(dev->netdev);
+
+			usb_unanchor_urb(urb);
+			usb_free_coherent(dev->udev, dev->adapter->rx_buffer_size, buf,
+					  urb->transfer_dma);

How about the URB?

Quoted 18 lines
drivers/net/can/usb/pcan_usb_core.c new file +18 -0
+			break;
+		}
+
+		/* Drop reference, USB core will take care of freeing it */
+		usb_free_urb(urb);
+	}
+
+	/* Did we submit any URBs */
+	if (i == 0) {
+		netdev_warn(netdev, "couldn't setup read URBs\n");
+		return err;
+	}
+
+	/* Warn if we've couldn't transmit all the URBs */
+	if (i<  PCAN_USB_MAX_RX_URBS)
+		netdev_warn(netdev, "rx performance may be slow\n");
+
+	/* Pre-alloc tx buffers and corresponding urbs */

drivers/net/can/usb/pcan_usb_core.c line 380

Quoted 10 lines
drivers/net/can/usb/pcan_usb_core.c new file +10 -0
+	for (i = 0; i<  PCAN_USB_MAX_TX_URBS; i++) {
+		 struct peak_tx_urb_context *context;
+		struct urb *urb = NULL;
+		u8 *buf = NULL;
+
+		/* create a URB, and a buffer for it, to trsmit usb messages */
+		urb = usb_alloc_urb(0, GFP_KERNEL);
+		if (!urb) {
+			netdev_err(netdev, "No memory left for URBs\n");
+			return -ENOMEM;

What happens with the URBs and buffers allocated before?

drivers/net/can/usb/pcan_usb_core.c line 388

Quoted 8 lines
drivers/net/can/usb/pcan_usb_core.c new file +8 -0
+		}
+
+		buf = usb_alloc_coherent(dev->udev, dev->adapter->tx_buffer_size,
+		                         GFP_KERNEL,&urb->transfer_dma);
+		if (!buf) {
+			netdev_err(netdev, "No memory left for USB buffer\n");
+			usb_free_urb(urb);
+			return -ENOMEM;

Dito.

Quoted 5 lines
drivers/net/can/usb/pcan_usb_core.c new file +5 -0
+		}
+
+		context =&dev->tx_contexts[i];
+		context->dev = dev;
+		context->urb = urb;

drivers/net/can/usb/pcan_usb_core.c

Quoted 10 lines
drivers/net/can/usb/pcan_usb_core.c new file +10 -0
+
+		usb_fill_bulk_urb(urb, dev->udev,
+		                  usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
+		                  buf, dev->adapter->tx_buffer_size,
+		                  peak_usb_write_bulk_callback, context);
+		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+	}
+
+	/* Warn if we've couldn't transmit all the URBs */
+	if (i<  PCAN_USB_MAX_TX_URBS)

This will never happen since the loop above will always return from this function in error case (return -ENOMEM).

Quoted 34 lines
drivers/net/can/usb/pcan_usb_core.c new file +34 -0
+		netdev_warn(netdev, "tx performance may be slow\n");
+
+	if (dev->adapter->device_start) {
+		err = dev->adapter->device_start(dev);
+		if (err)
+			goto failed;
+	}
+
+	/* can set bus on now */
+	if (dev->adapter->device_set_bus) {
+		err = dev->adapter->device_set_bus(dev, 1);
+		if (err)
+			goto failed;
+	}
+
+	dev->can.state = CAN_STATE_ERROR_ACTIVE;
+
+	return 0;
+
+failed:
+	if (err == -ENODEV)
+		netif_device_detach(dev->netdev);
+
+	netdev_warn(netdev, "couldn't submit control: %d\n", err);
+
+	return err;
+}
+
+
+static int peak_usb_ndo_open(struct net_device *netdev)
+{
+	struct peak_usb_device *dev = netdev_priv(netdev);
+	int err;
+

drivers/net/can/usb/pcan_usb_core.c line 526

Quoted 10 lines
drivers/net/can/usb/pcan_usb_core.c new file +10 -0
+	/* common open */
+	err = open_candev(netdev);
+	if (err)
+		return err;
+
+	/* finally start device */
+	err = peak_usb_start(dev);
+	if (err) {
+		if (err == -ENODEV)
+			netif_device_detach(dev->netdev);

This is already done in peak_usb_start() on -ENODEV.

Quoted 19 lines
drivers/net/can/usb/pcan_usb_core.c new file +19 -0
+
+		netdev_warn(netdev, "couldn't start device: %d\n", err);
+
+		close_candev(netdev);
+
+		return err;
+	}
+
+	dev->open_time = jiffies;
+
+	netif_start_queue(netdev);
+
+	return 0;
+}
+
+static int peak_usb_set_bittiming(struct net_device *netdev)
+{
+	struct peak_usb_device *dev = netdev_priv(netdev);
+	struct can_bittiming *bt =&dev->can.bittiming;

drivers/net/can/usb/pcan_usb_core.c line 615

Quoted 10 lines
drivers/net/can/usb/pcan_usb_core.c new file +10 -0
+	int err;
+
+	netdev_info(netdev, "set bitrate %u Kbps [sam=%d, phase_seg2=%d phase_seg1=%d prop_seg=%d sjw=%d brp=%d] ",
+	            bt->bitrate, bt->sample_point, bt->phase_seg2, bt->phase_seg1, bt->prop_seg, bt->sjw, bt->brp);
+
+	if (dev->adapter->device_set_bittiming)
+		err = dev->adapter->device_set_bittiming(dev, bt);
+
+	else {
+		printk("not supported");

Use e.g. dev_warn or whatever.

drivers/net/can/usb/pcan_usb_core.c line 623

Quoted 8 lines
drivers/net/can/usb/pcan_usb_core.c new file +8 -0
+		err = 0;
+	}
+
+	if (err)
+		printk(" failure (err %d)", err);
+	printk("\n");
+
+	return err;

I would generally recommend to fix up the whole function. Dedicated err variable looks a bit too much cause it is only used to print error value for dev_set_bittiming.

Quoted 11 lines
drivers/net/can/usb/pcan_usb_pro.c new file +11 -0
+}
+
+static int pcan_usb_pro_wait_response(struct peak_usb_device *dev,
+                                      struct pcan_usb_pro_msg_t *pum)
+{
+	u8 req_data_type, req_channel;
+	int actual_length;
+	int i, err = 0;
+
+	/* usb device unregistered? */
+	if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;

drivers/net/can/usb/pcan_usb_pro.c line 439

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.c new file +10 -0
+
+	/* first, keep in memory the request type and the eventual channel */
+	/* to filter received message */
+	req_data_type = pum->u.rec_buffer[4];
+	req_channel = pum->u.rec_buffer[5];
+
+	/* then, clear number of records to be sure to receive a real response */
+	/* from the device */
+	*pum->u.rec_counter = 0;
+	mdelay(5);

I wonder if this delay is really necessary as usb_bulk_msg is waits a finite time to finish.

Quoted 32 lines
drivers/net/can/usb/pcan_usb_pro.c new file +32 -0
+	for (i = 0; !err&&  i<  PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
+		struct pcan_usb_pro_msg_t rsp;
+		u32 r, rec_counter;
+		u8 *pc;
+
+		err = usb_bulk_msg(dev->udev,
+		                   usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
+		&pum->u.rec_buffer[0], pum->rec_buffer_len,
+		&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
+		if (err) {
+			netdev_err(dev->netdev, "waiting response failure: %d\n", err);
+			break;
+		}
+
+		if (actual_length == 0) {
+			continue;
+		}
+
+		if (actual_length<  PCAN_USB_PRO_MSG_HEADER_LEN) {
+			netdev_err(dev->netdev, "got abnormal too small response (len=%d)\n",
+			           actual_length);
+			err = -EBADMSG;
+			break;
+		}
+	
+		pc = pcan_usb_pro_msg_init(&rsp,&pum->u.rec_buffer[0], actual_length);
+
+		rec_counter = le32_to_cpu(*rsp.u.rec_counter);
+
+		for (r = 0; r<  rec_counter; r++) {
+			union pcan_usb_pro_rec_t *pr = (union pcan_usb_pro_rec_t *)pc;
+			int rec_size = pcan_usb_pro_sizeof_rec(pr->data_type);

drivers/net/can/usb/pcan_usb_pro.c line 482

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.c new file +10 -0
+			if (rec_size<= 0) {
+				netdev_err(dev->netdev, "got unprocessed record in msg\n");
+				dump_mem("received response msg",
+				&pum->u.rec_buffer[0], actual_length);
+				err = -EBADMSG;
+				break;
+			}
+
+			/* check if the response corresponds to the request */
+			if (pr->data_type != req_data_type) {

Empty statement.

drivers/net/can/usb/pcan_usb_pro.c

Quoted 4 lines
drivers/net/can/usb/pcan_usb_pro.c new file +4 -0
+			}
+
+			/* check if the channel in the response corresponds to the request */
+			else if ((req_channel != 0xff)&&  (pr->bus_activity.channel != req_channel)) {

Dito.

drivers/net/can/usb/pcan_usb_pro.c line 490

Quoted 4 lines
drivers/net/can/usb/pcan_usb_pro.c new file +4 -0
+			}
+
+			/* got the response */
+			else return 0;

Optimize the if/else if/else statement to also ge rid of the empty statements.

drivers/net/can/usb/pcan_usb_pro.c

Quoted 6 lines
drivers/net/can/usb/pcan_usb_pro.c new file +6 -0
+
+			/* otherwise, go on with next record in message */
+			pc += rec_size;	
+		}
+
+		if (r>= rec_counter) {

Empty statement. Remove it.

Quoted 19 lines
drivers/net/can/usb/pcan_usb_pro.c new file +19 -0
+		}
+	}
+
+	return (i>= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
+}
+
+static int pcan_usb_pro_request(struct peak_usb_device *dev, int req_id, int req_value, void *req_addr, int req_size)
+{
+	int err;
+	u8 req_type;
+	unsigned int p;
+
+	/* usb device unregistered? */
+	if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;
+
+	memset(req_addr, '\0', req_size);
+
+	req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
+	switch (req_id) {

drivers/net/can/usb/pcan_usb_pro.c line 525

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.c new file +10 -0
+	case USB_VENDOR_REQUEST_FKT:
+		/* Host to device */
+		p = usb_sndctrlpipe(dev->udev, 0);
+		break;
+
+	case USB_VENDOR_REQUEST_INFO:
+	default:
+		/* Device to host */
+		p = usb_rcvctrlpipe(dev->udev, 0);
+		req_type |= USB_DIR_IN;

Missing break.

Quoted 49 lines
(quoted diff) +49 -0
+	}
+
+	err = usb_control_msg(dev->udev,
+	                      p,
+	                      req_id,
+	                      req_type,
+	                      req_value,
+	                      0, req_addr, req_size,
+	                      2*USB_CTRL_GET_TIMEOUT);
+	if (err<  0)
+		netdev_info(dev->netdev,
+		            "unable to request usb[type=%d value=%d] err=%d\n",
+		            req_id, req_value, err);
+	else {
+		//dump_mem("request content", req_addr, req_size);
+		err = 0;
+	}
+
+	return err;
+}
+
+
+/*
+ * called when driver module is being unloaded:
+ *
+ * rmmod when plugged  =>  module_exit =>  device_exit, then
+ *                                       usb_deregister(), then
+ *                                       device_stop, then
+ *                        module_disconnect =>  device_free
+ * rmmod but unplugged =>  module_exit
+ * unplug              =>  module_disconnect =>  device_free
+ *
+ * (last change to send something on usb)
+ */
+static void pcan_usb_pro_exit(struct peak_usb_device *dev)
+{
+	struct pcan_usb_pro_device *pdev = (struct pcan_usb_pro_device *)dev;
+
+	/* when rmmod called before unplug and ifconfig down, MUST reset things */
+	/* before leaving */
+	if (dev->can.state != CAN_STATE_STOPPED) {
+
+		/* set bus off on the corresponding channel */
+		pcan_usb_pro_set_bus(dev, 0);
+	}
+
+	/* if channel #0 (only) =>  tell PCAN-USB-PRO the can driver is being */
+	/* unloaded */
+	if (dev->ctrl_index == 0) {

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+		/* turn off calibration message if any device were opened */
+		if (pdev->usb_if->dev_opened_count>  0)
+			pcan_usb_pro_set_calibration_msgs(dev, 0);
+
+		/* tell the PCAN-USB-PRO device that drive is being unloaded */
+		pcan_usb_pro_driver_loaded(dev, 0, 0);
+
+		/* this device needs also to be resetted when driver is unloaded */
+		/* TODO hangs when rmmod */

Then fix it. ;-)

Quoted 5 lines
drivers/net/can/usb/pcan_usb_pro.c new file +5 -0
+		//if (dev->udev)
+		//	usb_reset_device(dev->udev);
+	}
+}
+
#7 lore raw
Quoted 2 lines · Le Sam, 12/17/2011 02:17 PM, @{sender } a écrit:

Hi,

Hi,

Quoted 1 line

some annotations also from me. ;-)

Many thanks for these.

Quoted 6 lines

Since Marc already did the coding
style nitpicking I've tried to focus on logical stuff. I hope it is helpful.

Cheers,
Sebastian

My responses and explanations below. I'm working on a new version which should take into account a lot (all?) of your comments.

Quoted 16 lines

Am 16.12.2011 11:19, schrieb [email protected]:

Quoted 15 lines
drivers/net/can/usb/pcan_usb.c new file +15 -0
+static int pcan_usb_wait_response(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
+{
+    int actual_length;
+    struct pcan_usb_parameter cmd;
+    int err;
+
+    /* usb device unregistered? */
+    if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;
+
+    /* first, send command */
+    err = pcan_usb_send_command(dev, f, n, NULL);
+    if (err) {
+        return err;
+    }
+
Quoted 9 lines

drivers/net/can/usb/pcan_usb.c line 143

Quoted 6 lines
drivers/net/can/usb/pcan_usb.c new file +6 -0
+    cmd.function = f;
+    cmd.number = n;
+    memset(&cmd.parameters[0], '\0', PCAN_USB_PARAMETER_LEN);
+
+    /* then, wait for the response */
+    mdelay(5);

A delay to wait for a response? What happens if the device takes a little bit longer. This looks to be not really robust.

Historical reason I mean... Seems working without the mdelay(), so I'll remove it.

Quoted 49 lines
Quoted 49 lines
drivers/net/can/usb/pcan_usb_core.c new file +49 -0
+    err = usb_bulk_msg(dev->udev,
+                       usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
+    &cmd, sizeof(struct pcan_usb_parameter),
+    &actual_length, PCAN_USB_COMMAND_TIMEOUT);
+    if (err)
+        netdev_err(dev->netdev,
+                   "waiting response function=0x%x number=0x%x failure: %d\n",
+                   f, n, err);
+    else if (p) memcpy(p,&cmd.parameters[0], PCAN_USB_PARAMETER_LEN);
+
+    return err;
+}
+
+
+/*
+ * Start interface
+ */
+static int peak_usb_start(struct peak_usb_device *dev)
+{
+    struct net_device *netdev = dev->netdev;
+    int err, i;
+
+    for (i = 0; i<  PCAN_USB_MAX_RX_URBS; i++) {
+        struct urb *urb = NULL;
+        u8 *buf = NULL;
+
+        /* create a URB, and a buffer for it, to receive usb messages */
+        urb = usb_alloc_urb(0, GFP_KERNEL);
+        if (!urb) {
+            netdev_err(netdev, "No memory left for URBs\n");
+            return -ENOMEM;
+        }
+
+        buf = usb_alloc_coherent(dev->udev, dev->adapter->rx_buffer_size,
+                                 GFP_KERNEL,&urb->transfer_dma);
+        if (!buf) {
+            netdev_err(netdev, "No memory left for USB buffer\n");
+            usb_free_urb(urb);
+            return -ENOMEM;
+        }
+
+        usb_fill_bulk_urb(urb, dev->udev,
+                          usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
+                          buf, dev->adapter->rx_buffer_size,
+                          peak_usb_read_bulk_callback, dev);
+        urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+        usb_anchor_urb(urb,&dev->rx_submitted);
+
+        err = usb_submit_urb(urb, GFP_KERNEL);
Quoted 8 lines

drivers/net/can/usb/pcan_usb_core.c line 352

Quoted 7 lines
drivers/net/can/usb/pcan_usb_core.c new file +7 -0
+        if (err) {
+            if (err == -ENODEV)
+                netif_device_detach(dev->netdev);
+
+            usb_unanchor_urb(urb);
+            usb_free_coherent(dev->udev, dev->adapter->rx_buffer_size, buf,
+                      urb->transfer_dma);

How about the URB?

Seems you're right, "usb_free_urb()" is missing here... (Please have a look to ems_usb.c too)

Quoted 21 lines
Quoted 21 lines
drivers/net/can/usb/pcan_usb_core.c new file +21 -0
+            break;
+        }
+
+        /* Drop reference, USB core will take care of freeing it */
+        usb_free_urb(urb);
+    }
+
+    /* Did we submit any URBs */
+    if (i == 0) {
+        netdev_warn(netdev, "couldn't setup read URBs\n");
+        return err;
+    }
+
+    /* Warn if we've couldn't transmit all the URBs */
+    if (i<  PCAN_USB_MAX_RX_URBS)
+        netdev_warn(netdev, "rx performance may be slow\n");
+
+    /* Pre-alloc tx buffers and corresponding urbs */
+    for (i = 0; i<  PCAN_USB_MAX_TX_URBS; i++) {
+         struct peak_tx_urb_context *context;
+        struct urb *urb = NULL;
Quoted 8 lines

drivers/net/can/usb/pcan_usb_core.c line 380

Quoted 7 lines
drivers/net/can/usb/pcan_usb_core.c new file +7 -0
+        u8 *buf = NULL;
+
+        /* create a URB, and a buffer for it, to trsmit usb messages */
+        urb = usb_alloc_urb(0, GFP_KERNEL);
+        if (!urb) {
+            netdev_err(netdev, "No memory left for URBs\n");
+            return -ENOMEM;

What happens with the URBs and buffers allocated before?

I changed that in the two _RX/_TX loops: if urb or buffer allocation fails, break the loop after warning into logs. Next, check loop variable against 0 and PCAN_USB_MAX_xX_URBS: returns error if 0, warns about slow path but continue if lower than its max.

Quoted 9 lines

drivers/net/can/usb/pcan_usb_core.c line 388

Quoted 8 lines
drivers/net/can/usb/pcan_usb_core.c new file +8 -0
+        }
+
+        buf = usb_alloc_coherent(dev->udev, dev->adapter->tx_buffer_size,
+                                 GFP_KERNEL,&urb->transfer_dma);
+        if (!buf) {
+            netdev_err(netdev, "No memory left for USB buffer\n");
+            usb_free_urb(urb);
+            return -ENOMEM;

Dito.

Ok.

Quoted 9 lines
Quoted 9 lines
drivers/net/can/usb/pcan_usb_core.c new file +9 -0
+        }
+
+        context =&dev->tx_contexts[i];
+        context->dev = dev;
+        context->urb = urb;
+
+        usb_fill_bulk_urb(urb, dev->udev,
+                          usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
+                          buf, dev->adapter->tx_buffer_size,
Quoted 9 lines

drivers/net/can/usb/pcan_usb_core.c

Quoted 6 lines
drivers/net/can/usb/pcan_usb_core.c new file +6 -0
+                          peak_usb_write_bulk_callback, context);
+        urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+    }
+
+    /* Warn if we've couldn't transmit all the URBs */
+    if (i<  PCAN_USB_MAX_TX_URBS)

This will never happen since the loop above will always return from this function in error case (return -ENOMEM).

Ok, now this could happen ;-)

Quoted 37 lines
Quoted 37 lines
drivers/net/can/usb/pcan_usb_core.c new file +37 -0
+        netdev_warn(netdev, "tx performance may be slow\n");
+
+    if (dev->adapter->device_start) {
+        err = dev->adapter->device_start(dev);
+        if (err)
+            goto failed;
+    }
+
+    /* can set bus on now */
+    if (dev->adapter->device_set_bus) {
+        err = dev->adapter->device_set_bus(dev, 1);
+        if (err)
+            goto failed;
+    }
+
+    dev->can.state = CAN_STATE_ERROR_ACTIVE;
+
+    return 0;
+
+failed:
+    if (err == -ENODEV)
+        netif_device_detach(dev->netdev);
+
+    netdev_warn(netdev, "couldn't submit control: %d\n", err);
+
+    return err;
+}
+
+
+static int peak_usb_ndo_open(struct net_device *netdev)
+{
+    struct peak_usb_device *dev = netdev_priv(netdev);
+    int err;
+
+    /* common open */
+    err = open_candev(netdev);
+    if (err)
Quoted 8 lines

drivers/net/can/usb/pcan_usb_core.c line 526

Quoted 7 lines
drivers/net/can/usb/pcan_usb_core.c new file +7 -0
+        return err;
+
+    /* finally start device */
+    err = peak_usb_start(dev);
+    if (err) {
+        if (err == -ENODEV)
+            netif_device_detach(dev->netdev);

This is already done in peak_usb_start() on -ENODEV.

Ok (Great!) That's right! Think about changing ems_usb.c too.

Quoted 22 lines
Quoted 22 lines
drivers/net/can/usb/pcan_usb_core.c new file +22 -0
+
+        netdev_warn(netdev, "couldn't start device: %d\n", err);
+
+        close_candev(netdev);
+
+        return err;
+    }
+
+    dev->open_time = jiffies;
+
+    netif_start_queue(netdev);
+
+    return 0;
+}
+
+static int peak_usb_set_bittiming(struct net_device *netdev)
+{
+    struct peak_usb_device *dev = netdev_priv(netdev);
+    struct can_bittiming *bt =&dev->can.bittiming;
+    int err;
+
+    netdev_info(netdev, "set bitrate %u Kbps [sam=%d, phase_seg2=%d phase_seg1=%d prop_seg=%d sjw=%d brp=%d] ",
Quoted 8 lines

drivers/net/can/usb/pcan_usb_core.c line 615

Quoted 7 lines
drivers/net/can/usb/pcan_usb_core.c new file +7 -0
+                bt->bitrate, bt->sample_point, bt->phase_seg2, bt->phase_seg1, bt->prop_seg, bt->sjw, bt->brp);
+
+    if (dev->adapter->device_set_bittiming)
+        err = dev->adapter->device_set_bittiming(dev, bt);
+
+    else {
+        printk("not supported");

Use e.g. dev_warn or whatever.

Not sure: this "printk()" occurs next to above "netdev_info()" which format string doesn't end with any trailing '\n'

Quoted 11 lines

drivers/net/can/usb/pcan_usb_core.c line 623

Quoted 8 lines
drivers/net/can/usb/pcan_usb_core.c new file +8 -0
+        err = 0;
+    }
+
+    if (err)
+        printk(" failure (err %d)", err);
+    printk("\n");
+
+    return err;

I would generally recommend to fix up the whole function. Dedicated err variable looks a bit too much cause it is only used to print error value for dev_set_bittiming.

... because of nested printk() (see pcan_usb_set_bittiming() and pcan_usb_pro_set_bittiming() functions in their resp. source files), I'm obliged to follow the sequence of that code, except if having a local variable for the handling of dev_set_bittiming() status is worse than several calls to printk("\n")...

Quoted 15 lines
Quoted 15 lines
drivers/net/can/usb/pcan_usb_pro.c new file +15 -0
+}
+
+static int pcan_usb_pro_wait_response(struct peak_usb_device *dev,
+                                      struct pcan_usb_pro_msg_t *pum)
+{
+    u8 req_data_type, req_channel;
+    int actual_length;
+    int i, err = 0;
+
+    /* usb device unregistered? */
+    if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;
+
+    /* first, keep in memory the request type and the eventual channel */
+    /* to filter received message */
+    req_data_type = pum->u.rec_buffer[4];
Quoted 9 lines

drivers/net/can/usb/pcan_usb_pro.c line 439

Quoted 6 lines
drivers/net/can/usb/pcan_usb_pro.c new file +6 -0
+    req_channel = pum->u.rec_buffer[5];
+
+    /* then, clear number of records to be sure to receive a real response */
+    /* from the device */
+    *pum->u.rec_counter = 0;
+    mdelay(5);

I wonder if this delay is really necessary as usb_bulk_msg is waits a finite time to finish.

I removed it, then checked that it wasn't, thanks!

Quoted 47 lines
Quoted 32 lines
drivers/net/can/usb/pcan_usb_pro.c new file +32 -0
+    for (i = 0; !err&&  i<  PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
+        struct pcan_usb_pro_msg_t rsp;
+        u32 r, rec_counter;
+        u8 *pc;
+
+        err = usb_bulk_msg(dev->udev,
+                           usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
+        &pum->u.rec_buffer[0], pum->rec_buffer_len,
+        &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
+        if (err) {
+            netdev_err(dev->netdev, "waiting response failure: %d\n", err);
+            break;
+        }
+
+        if (actual_length == 0) {
+            continue;
+        }
+
+        if (actual_length<  PCAN_USB_PRO_MSG_HEADER_LEN) {
+            netdev_err(dev->netdev, "got abnormal too small response (len=%d)\n",
+                       actual_length);
+            err = -EBADMSG;
+            break;
+        }
+    
+        pc = pcan_usb_pro_msg_init(&rsp,&pum->u.rec_buffer[0], actual_length);
+
+        rec_counter = le32_to_cpu(*rsp.u.rec_counter);
+
+        for (r = 0; r<  rec_counter; r++) {
+            union pcan_usb_pro_rec_t *pr = (union pcan_usb_pro_rec_t *)pc;
+            int rec_size = pcan_usb_pro_sizeof_rec(pr->data_type);

drivers/net/can/usb/pcan_usb_pro.c line 482

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.c new file +10 -0
+            if (rec_size<= 0) {
+                netdev_err(dev->netdev, "got unprocessed record in msg\n");
+                dump_mem("received response msg",
+                &pum->u.rec_buffer[0], actual_length);
+                err = -EBADMSG;
+                break;
+            }
+
+            /* check if the response corresponds to the request */
+            if (pr->data_type != req_data_type) {

Empty statement.

drivers/net/can/usb/pcan_usb_pro.c

Quoted 4 lines
drivers/net/can/usb/pcan_usb_pro.c new file +4 -0
+            }
+
+            /* check if the channel in the response corresponds to the request */
+            else if ((req_channel != 0xff)&&  (pr->bus_activity.channel != req_channel)) {
Quoted 7 lines

Dito.

drivers/net/can/usb/pcan_usb_pro.c line 490

Quoted 4 lines
drivers/net/can/usb/pcan_usb_pro.c new file +4 -0
+            }
+
+            /* got the response */
+            else return 0;

Optimize the if/else if/else statement to also ge rid of the empty statements.

Sorry for that... These empty statements come from an unifdef pass which removes some DEBUG #ifdef/#endif... Now, the if statement has been included into the block or #ifdef/#endif have been removed.

Quoted 7 lines

drivers/net/can/usb/pcan_usb_pro.c

Quoted 6 lines
drivers/net/can/usb/pcan_usb_pro.c new file +6 -0
+
+            /* otherwise, go on with next record in message */
+            pc += rec_size;    
+        }
+
+        if (r>= rec_counter) {

Empty statement. Remove it.

See above.

Quoted 22 lines
Quoted 22 lines
drivers/net/can/usb/pcan_usb_pro.c new file +22 -0
+        }
+    }
+
+    return (i>= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
+}
+
+static int pcan_usb_pro_request(struct peak_usb_device *dev, int req_id, int req_value, void *req_addr, int req_size)
+{
+    int err;
+    u8 req_type;
+    unsigned int p;
+
+    /* usb device unregistered? */
+    if (!(dev->state&  PCAN_USB_STATE_CONNECTED)) return 0;
+
+    memset(req_addr, '\0', req_size);
+
+    req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
+    switch (req_id) {
+    case USB_VENDOR_REQUEST_FKT:
+        /* Host to device */
+        p = usb_sndctrlpipe(dev->udev, 0);
Quoted 8 lines

drivers/net/can/usb/pcan_usb_pro.c line 525

Quoted 7 lines
drivers/net/can/usb/pcan_usb_pro.c new file +7 -0
+        break;
+
+    case USB_VENDOR_REQUEST_INFO:
+    default:
+        /* Device to host */
+        p = usb_rcvctrlpipe(dev->udev, 0);
+        req_type |= USB_DIR_IN;

Missing break.

Ok.

Quoted 52 lines
Quoted 52 lines
drivers/net/can/usb/pcan_usb_pro.c new file +52 -0
+    }
+
+    err = usb_control_msg(dev->udev,
+                          p,
+                          req_id,
+                          req_type,
+                          req_value,
+                          0, req_addr, req_size,
+                          2*USB_CTRL_GET_TIMEOUT);
+    if (err<  0)
+        netdev_info(dev->netdev,
+                    "unable to request usb[type=%d value=%d] err=%d\n",
+                    req_id, req_value, err);
+    else {
+        //dump_mem("request content", req_addr, req_size);
+        err = 0;
+    }
+
+    return err;
+}
+
+
+/*
+ * called when driver module is being unloaded:
+ *
+ * rmmod when plugged  =>  module_exit =>  device_exit, then
+ *                                       usb_deregister(), then
+ *                                       device_stop, then
+ *                        module_disconnect =>  device_free
+ * rmmod but unplugged =>  module_exit
+ * unplug              =>  module_disconnect =>  device_free
+ *
+ * (last change to send something on usb)
+ */
+static void pcan_usb_pro_exit(struct peak_usb_device *dev)
+{
+    struct pcan_usb_pro_device *pdev = (struct pcan_usb_pro_device *)dev;
+
+    /* when rmmod called before unplug and ifconfig down, MUST reset things */
+    /* before leaving */
+    if (dev->can.state != CAN_STATE_STOPPED) {
+
+        /* set bus off on the corresponding channel */
+        pcan_usb_pro_set_bus(dev, 0);
+    }
+
+    /* if channel #0 (only) =>  tell PCAN-USB-PRO the can driver is being */
+    /* unloaded */
+    if (dev->ctrl_index == 0) {
+
+        /* turn off calibration message if any device were opened */
+        if (pdev->usb_if->dev_opened_count>  0)
Quoted 8 lines

drivers/net/can/usb/pcan_usb_pro.c line 1101

Quoted 7 lines
drivers/net/can/usb/pcan_usb_pro.c new file +7 -0
+            pcan_usb_pro_set_calibration_msgs(dev, 0);
+
+        /* tell the PCAN-USB-PRO device that drive is being unloaded */
+        pcan_usb_pro_driver_loaded(dev, 0, 0);
+
+        /* this device needs also to be resetted when driver is unloaded */
+        /* TODO hangs when rmmod */

Then fix it. ;-)

... or remove the comment ;-))

So, comment is in fact to be removed since the issue didn't exist at all.

Quoted 10 lines

drivers/net/can/usb/pcan_usb_pro.c line 1106

Quoted 5 lines
drivers/net/can/usb/pcan_usb_pro.c new file +5 -0
+        //if (dev->udev)
+        //    usb_reset_device(dev->udev);
+    }
+}
+

--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
PEAK-System Technik GmbH, Otto-Roehm-Strasse 69, D-64293 Darmstadt
Geschaeftsleitung: A.Gach/U.Wilhelm, HRB 9183 Darmstadt, Ust.IdNr.:DE 202 220 078 St.Nr.:007/241/13586 FA Darmstadt, WEE-Reg.-Nr.: DE39305391
Tel.:+49 (0)6151 8173-20 - Fax:+49 (0)6151 8173-29
E-mail: [email protected] - http://www.peak-system.com

#8 lore raw
In reply to [email protected]

More comments on the coding style...

Quoted 27 lines · On 12/16/2011 11:19 AM, [email protected] wrote:
Quoted 1 line

From 1cee3be3875f27a2ee3942b00d611450f4369325 Mon Sep 17 00:00:00 2001

From: Stephane Grosjean <[email protected]>
Date: Fri, 16 Dec 2011 11:11:37 +0100
Subject: [PATCH] Add support for PEAK-System GmbH CAN USB adapters (v2)

v2 includes:
change dev_xxx() into netdev_xxx() macros
add missing include linux/module.h
pre-allocate urbs and buffers for tx path at _open() and free them at _close()
rather than into _start_xmit()
remove some unused code and (boring) #ifdef/#endif
use "menuconfig" in Kconfig rather than "config" entry

---
 drivers/net/can/usb/Kconfig         |   20 +
 drivers/net/can/usb/Makefile        |   12 +
 drivers/net/can/usb/pcan_usb.c      |  654 +++++++++++++++++++
 drivers/net/can/usb/pcan_usb_core.c |  895 ++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.c  | 1207 +++++++++++++++++++++++++++++++++++
 drivers/net/can/usb/pcan_usb_pro.h  |  468 ++++++++++++++
 drivers/net/can/usb/peak_usb.h      |  148 +++++
 7 files changed, 3404 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/can/usb/pcan_usb.c
 create mode 100644 drivers/net/can/usb/pcan_usb_core.c
 create mode 100644 drivers/net/can/usb/pcan_usb_pro.c
 create mode 100644 drivers/net/can/usb/pcan_usb_pro.h
 create mode 100644 drivers/net/can/usb/peak_usb.h

....

Quoted 21 lines

+++ b/drivers/net/can/usb/pcan_usb_pro.h

drivers/net/can/usb/pcan_usb_pro.h new file +19 -0
@@ -0,0 +1,468 @@
+/*
+ * CAN driver for PEAK System PCAN-USB-PRO adapter
+ *
+ * Copyright (C) 2011-2012 PEAK-System GmbH
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef __pcan_usb_pro_h__

drivers/net/can/usb/pcan_usb_pro.h line 29

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.h new file +10 -0
+#define __pcan_usb_pro_h__
+
+/* 
+ * USB Vendor Request Data Types
+ */
+
+/* Vendor (other) request */
+#define USB_VENDOR_REQUEST_INFO                   0
+#define USB_VENDOR_REQUEST_ZERO                   1
+#define USB_VENDOR_REQUEST_FKT                    2

Looks like emums to me, here and in many other places (because used in switch statements).

drivers/net/can/usb/pcan_usb_pro.h line 34

Quoted 4 lines
drivers/net/can/usb/pcan_usb_pro.h new file +4 -0
+/* Vendor Request wValue for XXX_INFO */
+#define USB_VENDOR_REQUEST_wVALUE_INFO_BOOTLOADER  0
+#define USB_VENDOR_REQUEST_wVALUE_INFO_FIRMWARE    1
+#define USB_VENDOR_REQUEST_wVALUE_INFO_uC_CHIPID   2

Please use upper case.

http://lxr.linux.no/#linux+v3.1.5/Documentation/CodingStyle#L590

drivers/net/can/usb/pcan_usb_pro.h line 39

Quoted 5 lines
drivers/net/can/usb/pcan_usb_pro.h new file +5 -0
+#define USB_VENDOR_REQUEST_wVALUE_INFO_USB_CHIPID  3
+#define USB_VENDOR_REQUEST_wVALUE_INFO_DEVICENR	   4	
+#define USB_VENDOR_REQUEST_wVALUE_INFO_CPLD        5
+#define USB_VENDOR_REQUEST_wVALUE_INFO_MODE			6	
+#define USB_VENDOR_REQUEST_wVALUE_INFO_TIMEMODE		7

Please, no hungarian notation in Linux code please. See:

http://lxr.linux.no/#linux+v3.1.5/Documentation/CodingStyle#L252

drivers/net/can/usb/pcan_usb_pro.h line 50

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.h new file +10 -0
+/* Vendor Request wValue for XXX_ZERO */
+/* Value is Endpoint Number 0-7 */
+
+/* Vendor Request wValue for XXX_FKT */
+#define USB_VENDOR_REQUEST_wVALUE_SETFKT_BOOT       0 // set Bootloader Mode
+#define USB_VENDOR_REQUEST_wVALUE_SETFKT_DEBUG_CAN  1 // not used
+#define USB_VENDOR_REQUEST_wVALUE_SETFKT_DEBUG_LIN  2 // not used
+#define USB_VENDOR_REQUEST_wVALUE_SETFKT_DEBUG1     3 // not used
+#define USB_VENDOR_REQUEST_wVALUE_SETFKT_DEBUG2     4 // not used
+#define USB_VENDOR_REQUEST_wVALUE_SETFKT_INTERFACE_DRIVER_LOADED 5

Ditto...

Quoted 33 lines
drivers/net/can/usb/pcan_usb_pro.h new file +33 -0
+/* ctrl_type value */
+#define INTERN_FIRMWARE_INFO_STRUCT_TYPE 	0x11223322
+#define EXT_FIRMWARE_INFO_STRUCT_TYPE 		0x11223344
+
+#define BOOTLOADER_INFO_STRUCT_TYPE 		0x11112222
+#define uC_CHIPID_STRUCT_TYPE 				0
+#define USB_CHIPID_STRUCT_TYPE 				0
+#define DEVICE_NR_STRUCT_TYPE 				0x3738393A
+#define CPLD_INFO_STRUCT_TYPE 				0x1A1A2277
+
+/* USB_VENDOR_REQUEST_wVALUE_INFO_BOOTLOADER vendor request record type */
+struct __packed pcan_usb_pro_bootloader_info_t
+{
+	u32 ctrl_type;
+	u8  version[4];		//[0] -> main [1]-> sub [2]-> debug
+	u8  day;
+	u8  month;
+	u8  year;
+	u8  dummy;
+	u32 serial_num_high;
+	u32 serial_num_low;
+	u32 hw_type;
+	u32 hw_rev;
+};
+
+struct __packed pcan_usb_pro_crc_block_t
+{
+	u32 address;
+	u32 len;
+	u32 crc;
+};
+
+/* USB_VENDOR_REQUEST_wVALUE_INFO_FIRMWARE vendor request record type */

drivers/net/can/usb/pcan_usb_pro.h line 94

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.h new file +10 -0
+struct __packed pcan_usb_pro_ext_firmware_info_t
+{
+	u32 ctrl_type;
+	u8	 version[4];		//[0] -> main [1]-> sub [2]-> debug
+	u8  day;
+	u8  month;
+	u8  year;
+	u8  dummy;
+	u32 fw_type;
+};

Could you explain the rational behind the prefix USB_VENDOR_REQUEST_wVALUE_INFO. Why does the struct use a different prefix. I think this is some remainder from old Windoze code using hungarian notation!?

drivers/net/can/usb/pcan_usb_pro.h line 101

Quoted 6 lines
drivers/net/can/usb/pcan_usb_pro.h new file +6 -0
+/* USB_VENDOR_REQUEST_wVALUE_INFO_uC_CHIPID vendor request record type */
+struct __packed pcan_usb_pro_uc_chipid_t
+{
+	u32 ctrl_type;
+	u32 chip_id;
+};

Ditto here and in many other places.

Quoted 32 lines
drivers/net/can/usb/pcan_usb_pro.h new file +32 -0
+/* 
+ * USB Command Record types 
+ */
+
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_RX_8                0x80
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_RX_4                0x81
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_RX_0                0x82
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_RTR_RX              0x83
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_STATUS_ERROR_RX     0x84
+#define DATA_TYPE_USB2CAN_STRUCT_CALIBRATION_TIMESTAMP_RX   0x85
+#define DATA_TYPE_USB2CAN_STRUCT_BUSLAST_RX                 0x86
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_TX_8                0x41
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_TX_4                0x42
+#define DATA_TYPE_USB2CAN_STRUCT_CANMSG_TX_0                0x43
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_GETBAUDRATE            0x01 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETBAUDRATE            0x02
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_GETCANBUSACTIVATE      0x03
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETCANBUSACTIVATE      0x04
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETSILENTMODE          0x05 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETDEVICENR            0x06 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETWARNINGLIMIT        0x07 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETLOOKUP_EXPLICIT     0x08 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETLOOKUP_GROUP        0x09 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETFILTERMODE          0x0a 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETRESET_MODE          0x0b 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETERRORFRAME          0x0c 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_GETCANBUS_ERROR_STATUS 0x0D 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETREGISTER            0x0e 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_GETREGISTER            0x0f 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETGET_CALIBRATION_MSG 0x10 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETGET_BUSLAST_MSG     0x11 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_GETDEVICENR            0x12 

drivers/net/can/usb/pcan_usb_pro.h line 181

Quoted 10 lines
drivers/net/can/usb/pcan_usb_pro.h new file +10 -0
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SETSTRING              0x13 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_GETSTRING              0x14 
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_STRING                 0x15
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SAVEEEPROM             0x16
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_USB_IN_PACKET_DELAY    0x17
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_TIMESTAMP_PARAM        0x18
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_ERROR_GEN_ID           0x19
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_ERROR_GEN_NOW          0x1A
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SET_SOFTFILER          0x1B
+#define DATA_TYPE_USB2CAN_STRUCT_FKT_SET_CANLED             0x1C

Ditto, why is the prefix "DATA_TYPE_USB2CAN_STRUCT_FKT"? Please use logical and consistent prefixes. I will make the code more readable.

Wolfgang.