GS_USB
I forgot to set a subject for this email last time i sent it :/ It probably never made it out of anyone’s spam filters. Here it is again
Hello All,
Whats the protocol for getting a driver into the mainline kernel? I've reached a working and fairly stable state with the CAN adapter I'm working on, and would like to get its driver mainlined.
Attached is a patch.
Thanks,
Max Schneider.
From 2e4c4df9aae119279eb0913db0b7bc1d1025879a Mon Sep 17 00:00:00 2001
From: Maximilian Schneider <[email protected]>
Date: Wed, 2 Oct 2013 18:48:26 +0000
Subject: [PATCH] GS_USB CAN Driver.
--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1121 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1130 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.c
drivers/net/can/usb/Kconfig +8 -0
@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenu
drivers/net/can/usb/Makefile +1 -0
@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/usb/gs_usb.c new file +1121 -0
1,121 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Marc Kleine-Budde
maintainerQuoted 7 lines · Max S.
I forgot to set a subject for this email last time i sent it :/ It probably never made it out of anyone’s spam filters. Here it is again
Hello All,
Whats the protocol for getting a driver into the mainline kernel?
Posting the driver, inline not attached, is preferred. Nagging the maintainers after some days if they are not responding :)
Quoted 2 lines
I've reached a working and fairly stable state with the CAN adapter I'm working on, and would like to get its driver mainlined.
I'm currently working on the CAN subsystem, too. I think find some time during the weekend to look at your code.
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 |
Quoted 7 lines · Max S.
I forgot to set a subject for this email last time i sent it :/ It probably never made it out of anyone’s spam filters. Here it is again
Hello All,
Whats the protocol for getting a driver into the mainline kernel?
Simply post it here "inline" on the mailing list after you have done the usual checkpatch.pl validation.
Quoted 4 lines
I've reached a working and fairly stable state with the CAN adapter I'm working on, and would like to get its driver mainlined.
Attached is a patch.
I inlined it here for review:
Quoted 4 lines
Quoted 1 line
From
2e4c4df9aae119279eb0913db0b7bc1d1025879aMon Sep 17 00:00:00 2001From: Maximilian Schneider <[email protected]>
Date: Wed, 2 Oct 2013 18:48:26 +0000
Subject: [PATCH]GS_USBCAN Driver.
A few more words here about the device and this driver are welcome.
Quoted 176 lines
Signed-off-by Maximilian Schneider <[email protected]>--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1121 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1130 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.cdrivers/net/can/usb/Kconfig +8
-0@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenudrivers/net/can/usb/Makefile +1
-0@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUGdrivers/net/can/usb/gs_usb.c new file +133
-0@@ -0,0 +1,1121 @@ +/* CAN driver for Geschwister Schneider USB/CAN devices. +* +* Copyright (C) 2013 Geschwister Schneider Technologie-, +* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). +* +* Many thanks to all socketcan devs! +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published +* by the Free Software Foundation; version 2 of the License. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +*/ + +#include <linux/init.h> +#include <linux/signal.h> +#include <linux/module.h> +#include <linux/netdevice.h> +#include <linux/usb.h> + +#include <linux/can.h> +#include <linux/can/dev.h> +#include <linux/can/error.h> + +/* Device specific constants */ +#define USB_GSUSB_1_VENDOR_ID 0x1d50 +#define USB_GSUSB_1_PRODUCT_ID 0x606f + +#define GSUSB_ENDPOINT_IN 1 +#define GSUSB_ENDPOINT_OUT 2 + +/* Device specific constants */ +enum gs_usb_breq { +GS_USB_BREQ_HOST_FORMAT =0 , +GS_USB_BREQ_BITTIMING , +GS_USB_BREQ_MODE , +GS_USB_BREQ_BERR , +GS_USB_BREQ_BT_CONST , +GS_USB_BREQ_DEVICE_CONFIG +}; + +enum gs_can_mode { +GS_CAN_MODE_RESET =0 , +GS_CAN_MODE_START +}; + +enum gs_can_state { +GS_CAN_STATE_ERROR_ACTIVE =0 , +GS_CAN_STATE_ERROR_WARNING , +GS_CAN_STATE_ERROR_PASSIVE , +GS_CAN_STATE_BUS_OFF , +GS_CAN_STATE_STOPPED , +GS_CAN_STATE_SLEEPING +}; + +/* data types passed between host and device */ +struct __packed gs_host_config { +u32 byte_order; +}; + +struct __packed gs_device_config { +u8 reserved1; +u8 reserved2; +u8 reserved3; +u8 icount; +u32 sw_version; +u32 hw_version; +}; + +#define GS_CAN_MODE_NORMAL 0 +#define GS_CAN_MODE_LISTEN_ONLY (1<<0) +#define GS_CAN_MODE_LOOP_BACK (1<<1) +#define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_MODE_ONE_SHOT (1<<3) + +struct __packed gs_device_mode { +u32 mode; +u32 flags; +}; + +struct __packed gs_device_state { +u32 state; +u32 rxerr; +u32 txerr; +}; + +struct __packed gs_device_bittiming { +u32 prop_seg; +u32 phase_seg1; +u32 phase_seg2; +u32 sjw; +u32 brp; +}; + +#define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) +#define GS_CAN_FEATURE_LOOP_BACK (1<<1) +#define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_FEATURE_ONE_SHOT (1<<3) + +struct __packed gs_device_bt_const { +u32 feature; +u32 fclk_can; +u32 tseg1_min; +u32 tseg1_max; +u32 tseg2_min; +u32 tseg2_max; +u32 sjw_max; +u32 brp_min; +u32 brp_max; +u32 brp_inc; +}; + +#define GS_CAN_FLAG_OVERFLOW 1 + +struct __packed gs_host_frame { +u32 echo_id; +u32 can_id; + +u8 can_dlc; +u8 channel; +u8 flags; +u8 reserved; + +u8 data[8 ]; +}; + +#define GS_RX_BUFFER_SIZE sizeof(struct __packed gs_host_frame) + +/* Special address description flags for the CAN_ID */ +#define GS_EFF_FLAG 0x80000000U/* EFF/SFF is set in the MSB */
drivers/net/can/usb/gs_usb.c line 143
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+#define GS_RTR_FLAG 0x40000000U/* remote transmission request */ +#define GS_ERR_FLAG 0x20000000U/* error frame */ + +/* Valid bits in CAN ID for frame formats */ +#define GS_SFF_MASK 0x000007FFU/* standard frame format (SFF) */ +#define GS_EFF_MASK 0x1FFFFFFFU/* extended frame format (EFF) */ +#define GS_ERR_MASK 0x1FFFFFFFU/* omit EFF, RTR, ERR flags */ + +/* FIXME: this macro is only a placeholder for a better function */ +#define CONVERT_FLAGS (x) x
Please fix (== remove?).
Quoted 17 lines
drivers/net/can/usb/gs_usb.c new file +17
-0+/* Only send a max of MAX_TX_URBS frames per channel to the device at a time. */ +#define MAX_TX_URBS 10 +/* Only launch a max of MAX_RX_URBS usb requests at a time. */ +#define MAX_RX_URBS 30 +/* Maximum number of interfaces the driver supports per device. +* Current hardware only supports 2 interfaces. The future may vary. +*/ +#define MAX_INTF 2 + +struct gs_tx_context { +struct gs_can *dev; +unsigned int echo_id; +}; + +struct gs_can { +struct can_priv can;/* must be the first member */ +
drivers/net/can/usb/gs_usb.c line 171
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+struct gs_usb *parent; + +struct net_device *netdev; +struct usb_device *udev; +struct usb_interface *iface; + +struct can_bittiming_const bt_const; +unsigned int channel;/* channel number */ + +spinlock_t tx_ctx_lock;
Please briefly describe what the lock is used for.
Quoted 31 lines
drivers/net/can/usb/gs_usb.c new file +31
-0+struct gs_tx_context tx_context[MAX_TX_URBS ]; + +struct usb_anchor tx_submitted; +atomic_t active_tx_urbs; +}; + +/* usb interface struct */ +struct gs_usb { +struct gs_can *canch[MAX_INTF ]; + +struct usb_anchor rx_submitted; + +atomic_t active_channels; +}; + +/* 'allocate' a tx context. +* returns a valid tx context or NULL if there is no space. +*/ +static struct gs_tx_context *gs_alloc_tx_context (struct gs_can *dev) +{ +int i =0 ; +unsigned long flags; +spin_lock_irqsave (&dev->tx_ctx_lock, flags); + +for (; i <MAX_TX_URBS ; i++) { +if (dev->tx_context[i].echo_id ==MAX_TX_URBS ) { + dev->tx_context[i].echo_id = i; +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return &dev->tx_context[i]; + } + }
drivers/net/can/usb/gs_usb.c line 212
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return NULL ; +} + +/* releases a tx context +*/ +static void gs_free_tx_context (struct gs_tx_context *txc) +{ +unsigned long flags;
Empty line please.
drivers/net/can/usb/gs_usb.c line 224
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+spin_lock_irqsave (&txc->dev->tx_ctx_lock, flags); + txc->echo_id =MAX_TX_URBS ; +spin_unlock_irqrestore (&txc->dev->tx_ctx_lock, flags); +} + +/* Get a tx context by id. +*/ +static struct gs_tx_context *gs_get_tx_context ( +struct gs_can *dev, +unsigned int id) +{ +unsigned long flags;
Ditto
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+spin_lock_irqsave (&dev->tx_ctx_lock, flags); +if (id <MAX_TX_URBS ) { +if (dev->tx_context[id].echo_id == id) { +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return &dev->tx_context[id]; + } + }
drivers/net/can/usb/gs_usb.c line 241
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return NULL ; +} + +/* Get a tx contexts id. +*/ +static unsigned int gs_tx_context_id (struct gs_tx_context *txc) +{ +unsigned int id; +unsigned long flags;
Ditto.
drivers/net/can/usb/gs_usb.c line 248
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+spin_lock_irqsave (&txc->dev->tx_ctx_lock, flags); + id = txc->echo_id; +spin_unlock_irqrestore (&txc->dev->tx_ctx_lock, flags); +return id; +} + +#define BUFFER_SIZE sizeof(struct gs_host_frame)
I think sizeof is clear enough.
Quoted 40 lines
drivers/net/can/usb/gs_usb.c new file +40
-0+ +static int gs_cmd_reset (struct gs_usb *gsusb,struct gs_can *gsdev) +{ +struct gs_device_mode *dm; +struct usb_interface *intf = gsdev->iface; +int rc; + + dm =kzalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ; + + dm->mode =GS_CAN_MODE_RESET ; + + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_MODE , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + gsdev->channel, +0 , + dm, +sizeof (*dm), +1000 ); + +return rc; +} + +/**************************** USB CALLBACKS **************************/ +static void gs_usb_recieve_bulk_callback (struct urb *urb) +{ +struct gs_usb *usbcan = urb->context; +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct net_device_stats *stats; +struct gs_host_frame *hf = urb->transfer_buffer; +struct gs_tx_context *txc; + +struct can_frame *cf; +struct sk_buff *skb; +
drivers/net/can/usb/gs_usb.c line 298
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+BUG_ON (!usbcan); + +switch (urb->status) { +case 0 :/* success */ +break ; +case -ENOENT : +case -ESHUTDOWN : +return ; +default : +/* printk(KERN_INFO "Rx URB aborted (%d)\n", urb->status); */
Please remove. You have too much debug lines and printouts.
drivers/net/can/usb/gs_usb.c line 305
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+/* do not resubmit aborted urbs. eg: when device goes down */ +return ; + } + +/* device reports out of range channel id */ +if (hf->channel >=MAX_INTF ) +/* printk(KERN_INFO "%d] strange channel #\n", hf->channel); */
Ditto.
Quoted 19 lines
drivers/net/can/usb/gs_usb.c new file +19
-0+ goto resubmit_urb; + +/* device reports bad channel id */ + dev = usbcan->canch[hf->channel]; +BUG_ON (!dev); + + netdev = dev->netdev; +BUG_ON (!netdev); + +if (!netif_device_present (netdev)) +return ; + + stats = &dev->netdev->stats; + +if (hf->echo_id ==-1 ) {/* normal rx */ + skb =alloc_can_skb (dev->netdev, &cf); +if (skb ==NULL ) +return ; +
drivers/net/can/usb/gs_usb.c line 334
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ cf->can_id =CONVERT_FLAGS (hf->can_id); +/* is it necessary to use get_can_dlc? */ + cf->can_dlc =get_can_dlc (hf->can_dlc); +memcpy (cf->data, hf->data,8 ); + +netif_rx (skb); + + netdev->stats.rx_packets++; + netdev->stats.rx_bytes += hf->can_dlc; + }else {/* echo_id = hf->echo_id */
s/=/==/
drivers/net/can/usb/gs_usb.c line 347
Quoted 13 lines
drivers/net/can/usb/gs_usb.c new file +13
-0+if (hf->echo_id >=MAX_TX_URBS ) { +netdev_info (netdev,"BAD echo %d\n" , hf->echo_id); + goto resubmit_urb; + } + + netdev->stats.tx_packets++; + netdev->stats.tx_bytes += hf->can_dlc; + + txc =gs_get_tx_context (dev, hf->echo_id); + +/* bad devices send bad echo_ids. */ +if (!txc) { +netdev_info (netdev,"bad echo %d\n" , hf->echo_id);
Isn't this an error or a warning, at least?
drivers/net/can/usb/gs_usb.c line 359
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+ goto resubmit_urb; + } + +can_get_echo_skb (netdev,gs_tx_context_id (txc)); + +gs_free_tx_context (txc); + +netif_wake_queue (netdev); + } + +if (hf->flags &GS_CAN_FLAG_OVERFLOW ) +netdev_info (netdev,"overflow\n" );
Is this a CAN message overflow? If yes please generate an error message like for the SJA1000:
http://lxr.free-electrons.com/source/drivers/net/can/sja1000/sja1000.c#L391
Quoted 35 lines
drivers/net/can/usb/gs_usb.c new file +35
-0+ + +resubmit_urb: +/* note: no urb refill... works for me */ + + rc =usb_submit_urb (urb,GFP_ATOMIC ); + +/* USB failure take down all interfaces */ +if (rc == -ENODEV ) { +for (rc =0 ; rc <MAX_INTF ; rc++) { +if (usbcan->canch[rc]) +netif_device_detach (usbcan->canch[rc]->netdev); + } + } +} + +static int gs_usb_set_bittiming (struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct can_bittiming *bt = &dev->can.bittiming; +struct usb_interface *intf = dev->iface; +int rc; +struct gs_device_bittiming *dbt; + + dbt =kmalloc (sizeof (*dbt),GFP_KERNEL ); +if (!dbt) +return -ENOMEM ; + + dbt->prop_seg = bt->prop_seg; + dbt->phase_seg1 = bt->phase_seg1; + dbt->phase_seg2 = bt->phase_seg2; + dbt->sjw = bt->sjw; + dbt->brp = bt->brp; + + rc = usb_control_msg(interface_to_usbdev(intf),
drivers/net/can/usb/gs_usb.c line 404
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_BITTIMING , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dbt, +sizeof (*dbt), + 1000); +if (rc <0 ) +dev_warn (netdev->dev.parent,"Couldn't set bittimings %d" , rc);
netdev_err?
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+kfree (dbt); + +return rc; +} + +static int gs_usb_set_mode (struct net_device *netdev,enum can_mode mode) +{
drivers/net/can/usb/gs_usb.c line 422
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+if (mode ==CAN_MODE_START ) { +netif_wake_queue (netdev); +return 0 ; + } + +return -EOPNOTSUPP ; +} + +static int gs_usb_get_state (const struct net_device *netdev, +enum can_state *state)
Please use some indention here and in other similar places.
Quoted 52 lines
drivers/net/can/usb/gs_usb.c new file +52
-0+{ + +struct gs_can *dev =netdev_priv (netdev); +struct usb_interface *intf = dev->iface; +int rc; +struct gs_device_state *dstate; + +dev_warn (netdev->dev.parent,"CAN state" ); + + dstate =kmalloc (sizeof (*dstate),GFP_KERNEL ); +if (!dstate) +return -ENOMEM ; + + rc =usb_control_msg (interface_to_usbdev (intf), +usb_rcvctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_BERR , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dstate, +sizeof (*dstate), +1000 ); + +if (rc <0 ) { +dev_warn (netdev->dev.parent,"Couldn't get state %d" , rc); +kfree (dstate); +return rc; + } + +switch (dstate->state) { +/* RX/TX error count < 96 */ +case GS_CAN_STATE_ERROR_ACTIVE : + *state =CAN_STATE_ERROR_ACTIVE ; +break ; +/* RX/TX error count < 128 */ +case GS_CAN_STATE_ERROR_WARNING : + *state =CAN_STATE_ERROR_WARNING ; +break ; +/* RX/TX error count < 256 */ +case GS_CAN_STATE_ERROR_PASSIVE : + *state =CAN_STATE_ERROR_PASSIVE ; +break ; +/* RX/TX error count >= 256 */ +case GS_CAN_STATE_BUS_OFF : + *state =CAN_STATE_BUS_OFF ; +break ; +/* Device is stopped */ +case GS_CAN_STATE_STOPPED : + *state =CAN_STATE_STOPPED ; +break ; +/* Device is sleeping */ +case GS_CAN_STATE_SLEEPING :
drivers/net/can/usb/gs_usb.c line 484
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ *state =CAN_STATE_SLEEPING ; +break ; +default : +kfree (dstate); +return -ENOTSUPP ; + } + +kfree (dstate); +return 0 ; +}
This callback is normally only needed if the state is not updated by the driver via asynchronous events (interrupts).
drivers/net/can/usb/gs_usb.c line 487
Quoted 2 lines
drivers/net/can/usb/gs_usb.c new file +2
-0+static int gs_usb_get_berr_counter (const struct net_device *netdev, +struct can_berr_counter *bec)
Indention.
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+{ +struct gs_can *dev =netdev_priv (netdev); +struct usb_interface *intf = dev->iface; +int rc; +struct gs_device_state *dstate; + + dstate =kmalloc (sizeof (*dstate),GFP_KERNEL ); +if (!dstate) +return -ENOMEM ; + + rc = usb_control_msg(interface_to_usbdev(intf), + usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
drivers/net/can/usb/gs_usb.c line 509
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+GS_USB_BREQ_BERR , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dstate, +sizeof (*dstate), + 1000); +if (rc <0 ) { +dev_warn (netdev->dev.parent,"Couldn't get error counters %d" , + rc);
dev_err or better netdev_err? Please use the netdev_* function whenever possible, not only here.
Quoted 14 lines
drivers/net/can/usb/gs_usb.c new file +14
-0+kfree (dstate); +return rc; + } + + bec->txerr = dstate->txerr; + bec->rxerr = dstate->rxerr; + +kfree (dstate); +return 0 ; +} + +static void gs_usb_xmit_callback (struct urb *urb) +{ +struct gs_tx_context *txc = urb->context;
drivers/net/can/usb/gs_usb.c line 533
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+struct gs_can *dev; +struct net_device *netdev; + +BUG_ON (!txc); + + dev = txc->dev; +BUG_ON (!dev); + + netdev = dev->netdev; +BUG_ON (!netdev);
Too much sanity checks! Please reduce the number of BUG_ONs here and in other places.
drivers/net/can/usb/gs_usb.c line 537
Quoted 3 lines
drivers/net/can/usb/gs_usb.c new file +3
-0+if (urb->status) { +dev_info (netdev->dev.parent,"%d] xmit err %d\n" , + dev->channel, txc->echo_id);
netdev_err?
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ } + +usb_free_coherent (urb->dev, + urb->transfer_buffer_length, + urb->transfer_buffer, + urb->transfer_dma); + +atomic_dec (&dev->active_tx_urbs);
drivers/net/can/usb/gs_usb.c line 555
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +if (!netif_device_present (netdev)) +return ; + +if (netif_queue_stopped (netdev)) +netif_wake_queue (netdev); +} + +static netdev_tx_t gs_can_start_xmit (struct sk_buff *skb, +struct net_device *netdev)
Indention!
drivers/net/can/usb/gs_usb.c line 567
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+{ +struct gs_can *dev =netdev_priv (netdev); +struct net_device_stats *stats = &dev->netdev->stats; +struct urb *urb; +struct gs_host_frame *hf; +struct can_frame *cf; +int rc; +unsigned int idx; +struct gs_tx_context *txc; + +if (dev->can.ctrlmode&CAN_CTRLMODE_LISTENONLY ) +return NETDEV_TX_BUSY ;
Hm, it's better not to start the TX queue in that case. But that's a problem other driver do have as well.
Quoted 14 lines
drivers/net/can/usb/gs_usb.c new file +14
-0+ +if (can_dropped_invalid_skb (netdev, skb)) +return NETDEV_TX_OK ; + +/* find an empty context to keep track of transmission */ + txc =gs_alloc_tx_context (dev); +if (!txc) +return NETDEV_TX_BUSY ; + +/* create a URB, and a buffer for it */ + urb =usb_alloc_urb (0 ,GFP_ATOMIC ); +if (!urb) { +gs_free_tx_context (txc); +dev_err (netdev->dev.parent,"No memory left for URBs\n" );
drivers/net/can/usb/gs_usb.c line 591
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ goto nomem; + } + + hf =usb_alloc_coherent (dev->udev,sizeof (*hf),GFP_ATOMIC , + &urb->transfer_dma); +if (!hf) { +gs_free_tx_context (txc); +dev_err (netdev->dev.parent,"No memory left for USB buffer\n" ); +usb_free_urb (urb); + goto nomem;
It usual to use labels for cleanup to avoid code duplication, e.g. "goto free_urb;".
drivers/net/can/usb/gs_usb.c line 598
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+ } + + cf = (struct can_frame *)skb->data; + + idx =gs_tx_context_id (txc); + +if (idx >=MAX_TX_URBS ) {
Can that happen?
drivers/net/can/usb/gs_usb.c line 604
Quoted 6 lines
drivers/net/can/usb/gs_usb.c new file +6
-0+dev_err (netdev->dev.parent,"Invalid tx context %d\n" , idx); +usb_free_urb (urb); +usb_free_coherent (dev->udev, +sizeof (*hf), + hf, + urb->transfer_dma);
Label?
Quoted 17 lines
drivers/net/can/usb/gs_usb.c new file +17
-0+ goto nomem; + } + + hf->echo_id = idx; + hf->channel = dev->channel; + + hf->can_id =CONVERT_FLAGS (cf->can_id); + hf->can_dlc = cf->can_dlc; +memcpy (hf->data, cf->data,8 ); + +usb_fill_bulk_urb (urb, dev->udev, +usb_sndbulkpipe (dev->udev,GSUSB_ENDPOINT_OUT ), + hf, +sizeof (*hf), + gs_usb_xmit_callback, + txc); +
drivers/net/can/usb/gs_usb.c line 631
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; +usb_anchor_urb (urb, &dev->tx_submitted); + +can_put_echo_skb (skb, netdev, idx); + +atomic_inc (&dev->active_tx_urbs); + + rc =usb_submit_urb (urb,GFP_ATOMIC ); +if (unlikely (rc)) {/* usb send failed */ +dev_err (netdev->dev.parent,"usb_submit failed %d\n" , rc);
netdev_err!
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+can_free_echo_skb (netdev,gs_tx_context_id (txc)); +gs_free_tx_context (txc); + +usb_unanchor_urb (urb); + usb_free_coherent(dev->udev,
drivers/net/can/usb/gs_usb.c line 647
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+sizeof (*hf), + hf, + urb->transfer_dma); + +atomic_dec (&dev->active_tx_urbs); + +if (rc == -ENODEV ) { +netif_device_detach (netdev); + }else { +dev_warn (netdev->dev.parent,"Failed tx_urb %d\n" , rc);
dev_err? Does it happen?
Quoted 26 lines
drivers/net/can/usb/gs_usb.c new file +26
-0+ stats->tx_dropped++; + } + }else { + netdev->trans_start = jiffies; + +/* Slow down tx path */ +if (atomic_read (&dev->active_tx_urbs) >=MAX_TX_URBS ) +netif_stop_queue (netdev); + } + +/* let usb core take care of this urb */ +usb_free_urb (urb); + +return NETDEV_TX_OK ; + +nomem: +dev_kfree_skb (skb); + stats->tx_dropped++; +return NETDEV_TX_OK ; +} + +static int gs_can_open (struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct gs_usb *parent = dev->parent; +int rc, i;
drivers/net/can/usb/gs_usb.c line 684
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+struct gs_device_mode *dm; +u32 ctrlmode; + +/* common open */ + rc =open_candev (netdev); +if (rc) +return rc; + +if (atomic_add_return (1 , &parent->active_channels) ==1 ) { +dev_info (netdev->dev.parent,"Allocating rx URBs\n" );
You are using to much debug messages, which are not really useful for the user.
drivers/net/can/usb/gs_usb.c line 693
Quoted 9 lines
drivers/net/can/usb/gs_usb.c new file +9
-0+for (i =0 ; i <MAX_RX_URBS ; i++) { +struct urb *urb =NULL ; +u8 *buf =NULL ; + +/* alloc rx urb */ + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) { +dev_err (netdev->dev.parent, +"No memory left for URBs\n" );
close_candev() is missing. Please use labels for cleanup.
Quoted 39 lines
drivers/net/can/usb/gs_usb.c new file +39
-0+return -ENOMEM ; + } + +/* alloc rx buffer */ + buf =usb_alloc_coherent (dev->udev, +BUFFER_SIZE , +GFP_KERNEL , + &urb->transfer_dma); +if (!buf) { +dev_err (netdev->dev.parent, +"No memory left for USB buffer\n" ); +usb_free_urb (urb); +return -ENOMEM ; + } + + +/* fill, anchor, and submit rx urb */ +usb_fill_bulk_urb (urb, + dev->udev, +usb_rcvbulkpipe (dev->udev, +GSUSB_ENDPOINT_IN ), + buf, +BUFFER_SIZE , + gs_usb_recieve_bulk_callback, + parent + ); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +usb_anchor_urb (urb, &parent->rx_submitted); + + rc =usb_submit_urb (urb,GFP_KERNEL ); +if (rc) { +if (rc == -ENODEV ) +netif_device_detach (dev->netdev); + +usb_unanchor_urb (urb); +break ; + } +
drivers/net/can/usb/gs_usb.c line 742
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+/* Drop reference, +* USB core will take care of freeing it +*/ +usb_free_urb (urb); + } + } + + dm =kmalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ;
Cleanup?
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ +/* flags */ + ctrlmode = dev->can.ctrlmode; + dm->flags =0 ; + +if (ctrlmode &CAN_CTRLMODE_LOOPBACK ) + dm->flags |=GS_CAN_MODE_LOOP_BACK ; +
drivers/net/can/usb/gs_usb.c line 760
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+else if (ctrlmode &CAN_CTRLMODE_LISTENONLY ) + dm->flags |=GS_CAN_MODE_LISTEN_ONLY ; + + +/* Controller is not allowed to retry TX +* this mode is unavailable on atmels uc3c hardware +*/ +if (ctrlmode &CAN_CTRLMODE_ONE_SHOT ) { + dm->flags |=GS_CAN_MODE_ONE_SHOT ; +dev_warn (netdev->dev.parent,"GS_USB_ONE_SHOT\n" );
Why a warning? I think you can remove this message.
drivers/net/can/usb/gs_usb.c line 765
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+ } + +if (ctrlmode &CAN_CTRLMODE_3_SAMPLES ) { + dm->flags |=GS_CAN_MODE_TRIPLE_SAMPLE ; +dev_warn (netdev->dev.parent,"GS_USB_TRIPLE_SAMPLE\n" );
Ditto.
Quoted 9 lines
drivers/net/can/usb/gs_usb.c new file +9
-0+ } + + +/* finally start device */ + dm->mode =GS_CAN_MODE_START ; + rc = usb_control_msg(interface_to_usbdev(dev->iface), + usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0), + GS_USB_BREQ_MODE, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
drivers/net/can/usb/gs_usb.c line 784
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ dev->channel, +0 , + dm, +sizeof (*dm), + 1000); + +kfree (dm); + +if (rc <0 ) { +dev_warn (netdev->dev.parent,"Couldn't start device %d\n" , rc);
netdev_err?
drivers/net/can/usb/gs_usb.c line 791
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+return rc; + } + +/* check mode... */ + dm =kmalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ;
No need to re-allocate "dm".
drivers/net/can/usb/gs_usb.c line 805
Quoted 13 lines
drivers/net/can/usb/gs_usb.c new file +13
-0+ rc =usb_control_msg (interface_to_usbdev (dev->iface), +usb_rcvctrlpipe (interface_to_usbdev (dev->iface),0 ), +GS_USB_BREQ_MODE , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dm, +sizeof (*dm), +1000 ); + +if (rc <0 ) { +dev_warn (netdev->dev.parent,"Couldn't get device mode %d\n" , + rc);
netdev_err?
drivers/net/can/usb/gs_usb.c line 806
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+kfree (dm); +return rc; + } + +kfree (dm);
Again, please use labels to simplify your cleanup code.
drivers/net/can/usb/gs_usb.c line 816
Quoted 6 lines
drivers/net/can/usb/gs_usb.c new file +6
-0+ + dev->can.state =CAN_STATE_ERROR_ACTIVE ; + +netif_start_queue (netdev); + +netdev_info (netdev,"CAN open\n" );
You are too verbose. Please remove.
drivers/net/can/usb/gs_usb.c line 820
Quoted 4 lines
drivers/net/can/usb/gs_usb.c new file +4
-0+return 0 ; +} + +/* think: ip link set canx down */
What do you mean?
drivers/net/can/usb/gs_usb.c line 832
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+static int gs_can_close (struct net_device *netdev) +{ +int rc; +struct gs_can *dev =netdev_priv (netdev); +struct gs_usb *parent = dev->parent; + +netif_stop_queue (netdev); + +/* Stop polling */ +if (atomic_dec_and_test (&parent->active_channels)) { +dev_info (netdev->dev.parent, +"Freeing rx usb memory\n" );
You are too verbose. Please remove.
drivers/net/can/usb/gs_usb.c line 839
Quoted 6 lines
drivers/net/can/usb/gs_usb.c new file +6
-0+usb_kill_anchored_urbs (&parent->rx_submitted); + } + + rc =gs_cmd_reset (parent, dev); +if (rc <0 ) +dev_warn (netdev->dev.parent,"Couldn't shutdown device %d" , rc);
netdev_err?
drivers/net/can/usb/gs_usb.c line 843
Quoted 3 lines
drivers/net/can/usb/gs_usb.c new file +3
-0+close_candev (netdev); + +netdev_info (netdev,"CAN close\n" );
You are too verbose. Please remove.
drivers/net/can/usb/gs_usb.c line 856
Quoted 13 lines
drivers/net/can/usb/gs_usb.c new file +13
-0+return 0 ; +} + +/******************************** USB ********************************/ + +static const struct net_device_ops gs_usb_netdev_ops = { + .ndo_open = gs_can_open, + .ndo_stop = gs_can_close, + .ndo_start_xmit = gs_can_start_xmit, +}; + +static int gs_make_candev (struct gs_can **out,unsigned int channel, +struct usb_interface *intf)
Indention!
Quoted 16 lines
drivers/net/can/usb/gs_usb.c new file +16
-0+{ +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct gs_device_bt_const *bt_const; + + bt_const =kmalloc (sizeof (*bt_const),GFP_KERNEL ); +if (!bt_const) +return -ENOMEM ; + +/* fetch bit timing constants */ + rc = usb_control_msg(interface_to_usbdev(intf), + usb_rcvctrlpipe(interface_to_usbdev(intf), 0), + GS_USB_BREQ_BT_CONST, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel,
drivers/net/can/usb/gs_usb.c line 882
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+0 , + bt_const, +sizeof (*bt_const), + 1000); + +if (rc <0 ) { +dev_err (&intf->dev, +"Couldn't get bit timing const for channel %d\n" , + rc); +kfree (bt_const);
labels for cleanup!
drivers/net/can/usb/gs_usb.c line 886
Quoted 4 lines
drivers/net/can/usb/gs_usb.c new file +4
-0+return rc; + } + +dev_info (&intf->dev,"fclock: %d\n" , bt_const->fclk_can);
Please remove. The use can query the information with "ip -d link show".
Quoted 15 lines
drivers/net/can/usb/gs_usb.c new file +15
-0+ +/* create netdev */ + netdev =alloc_candev (sizeof (struct gs_can ),MAX_TX_URBS ); +if (!netdev) { +dev_err (&intf->dev,"Couldn't alloc candev\n" ); +kfree (bt_const); +return -ENOMEM ; + } + + dev =netdev_priv (netdev); + + netdev->netdev_ops = &gs_usb_netdev_ops; + + netdev->flags |=IFF_ECHO ;/* we support full roundtrip echo */ +
drivers/net/can/usb/gs_usb.c line 911
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+/* dev settup */ +strcpy (dev->bt_const.name,"gs_usb" ); + dev->bt_const.tseg1_min = bt_const->tseg1_min; + dev->bt_const.tseg1_max = bt_const->tseg1_max; + dev->bt_const.tseg2_min = bt_const->tseg2_min; + dev->bt_const.tseg2_max = bt_const->tseg2_max; + dev->bt_const.sjw_max = bt_const->sjw_max; + dev->bt_const.brp_min = bt_const->brp_min; + dev->bt_const.brp_max = bt_const->brp_max; + dev->bt_const.brp_inc = bt_const->brp_inc;
memcpy() or "dev->bt_const = bt_const->sjw_max;"?
Quoted 51 lines
drivers/net/can/usb/gs_usb.c new file +51
-0+ + dev->udev =interface_to_usbdev (intf); + dev->iface = intf; + dev->netdev = netdev; + dev->channel = channel; + +init_usb_anchor (&dev->tx_submitted); +atomic_set (&dev->active_tx_urbs,0 ); +spin_lock_init (&dev->tx_ctx_lock); +for (rc =0 ; rc <MAX_TX_URBS ; rc++) { + dev->tx_context[rc].dev = dev; + dev->tx_context[rc].echo_id =MAX_TX_URBS ; + } + +/* can settup */ + dev->can.state =CAN_STATE_STOPPED ; + dev->can.clock.freq = bt_const->fclk_can; + dev->can.bittiming_const = &dev->bt_const; + dev->can.do_set_bittiming = gs_usb_set_bittiming; + dev->can.do_set_mode = gs_usb_set_mode; + dev->can.do_get_state = gs_usb_get_state; + dev->can.do_get_berr_counter = gs_usb_get_berr_counter; + + dev->can.ctrlmode_supported =0 ; + +if (bt_const->feature &GS_CAN_FEATURE_LISTEN_ONLY ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LISTENONLY ; + +if (bt_const->feature &GS_CAN_FEATURE_LOOP_BACK ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LOOPBACK ; + +if (bt_const->feature &GS_CAN_FEATURE_TRIPLE_SAMPLE ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_3_SAMPLES ; + +if (bt_const->feature &GS_CAN_FEATURE_ONE_SHOT ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_ONE_SHOT ; + +kfree (bt_const); + +SET_NETDEV_DEV (netdev, &intf->dev); + + rc =register_candev (dev->netdev); +if (rc) { +free_candev (dev->netdev); +dev_err (&intf->dev,"Couldn't register candev\n" ); +return rc; + } + + *out = dev; +return 0 ; +}
drivers/net/can/usb/gs_usb.c line 972
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +static void gs_destroy_candev (struct gs_can *dev) +{ +unregister_candev (dev->netdev); +free_candev (dev->netdev); +usb_kill_anchored_urbs (&dev->tx_submitted); +} + +static int gs_usb_probe (struct usb_interface *intf, +const struct usb_device_id *id)
Indention!
Quoted 41 lines
drivers/net/can/usb/gs_usb.c new file +41
-0+{ +struct gs_usb *dev; +int rc = -ENOMEM ; +unsigned int icount, i; +struct gs_host_config *hconf; +struct gs_device_config *dconf; + + hconf =kmalloc (sizeof (*hconf),GFP_KERNEL ); +if (!hconf) +return -ENOMEM ; + + hconf->byte_order =0x0000beef ; + + +/* send host config */ + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_HOST_FORMAT , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , +1 , + intf->altsetting[0 ].desc.bInterfaceNumber, + hconf, +sizeof (*hconf), +1000 ); + +kfree (hconf); + +if (rc <0 ) { +dev_err (&intf->dev,"Couldn't send data format: %d\n" , + rc); +return rc; + } + + + dconf =kmalloc (sizeof (*dconf),GFP_KERNEL ); +if (!dconf) +return -ENOMEM ; + +/* read device config */ + rc = usb_control_msg(interface_to_usbdev(intf), + usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
drivers/net/can/usb/gs_usb.c line 1023
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+GS_USB_BREQ_DEVICE_CONFIG , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , +1 , + intf->altsetting[0 ].desc.bInterfaceNumber, + dconf, +sizeof (*dconf), + 1000); +if (rc <0 ) { +dev_err (&intf->dev,"Couldn't recieve device config: %d\n" , + rc);
kfree(dconf); ?
Quoted 27 lines
drivers/net/can/usb/gs_usb.c new file +27
-0+return rc; + } + + icount = dconf->icount+1 ; +kfree (dconf); + +dev_info (&intf->dev,"Configuring for %d interfaces\n" , icount); + +if (icount >MAX_INTF ) { +dev_err (&intf->dev, +"Driver cannot handle more that %d Can interfaces\n" , +MAX_INTF ); +return -EINVAL ; + } + + dev =kzalloc (sizeof (*dev),GFP_KERNEL ); +init_usb_anchor (&dev->rx_submitted); + +atomic_set (&dev->active_channels,0 ); + +usb_set_intfdata (intf, dev); + +for (i =0 ; i < icount; i++) { + rc =gs_make_candev (&dev->canch[i], i, intf); +if (rc) { +/* on failure destroy previously created candevs */ + icount = i;
drivers/net/can/usb/gs_usb.c line 1060
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+for (i =0 ; i < icount; i++) { +gs_destroy_candev (dev->canch[i]); + dev->canch[icount] =NULL ; + } +return rc; + } + dev->canch[i]->parent = dev; + } + +dev_info (&intf->dev,"Probe\n" );
One dev_info per probe is fine but it should provide more information.
drivers/net/can/usb/gs_usb.c line 1071
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +11
-0+return 0 ; +} + +static void gs_usb_disconnect (struct usb_interface *intf) +{ +unsigned i; +struct gs_usb *dev =usb_get_intfdata (intf); +usb_set_intfdata (intf,NULL ); + +if (!dev) { +dev_info (&intf->dev,"Disconnect (nodata)\n" );
dev_err?
Quoted 52 lines
drivers/net/can/usb/gs_usb.c new file +50
-0+return ; + } + +for (i =0 ; i <MAX_INTF ; i++) { +struct gs_can *can = dev->canch[i]; + +if (!can) +continue ; + +gs_destroy_candev (can); + } + +usb_kill_anchored_urbs (&dev->rx_submitted); + +dev_info (&intf->dev,"Disconnect\n" ); +} + +MODULE_DEVICE_TABLE (usb, gs_usb_table); + +static struct usb_device_id gs_usb_table[] = { + {USB_DEVICE (USB_GSUSB_1_VENDOR_ID ,USB_GSUSB_1_PRODUCT_ID )}, + {}/* Terminating entry */ +}; + +static struct usb_driver gs_usb_driver = { + .name ="gs_usb" , + .probe = gs_usb_probe, + .disconnect = gs_usb_disconnect, + .id_table = gs_usb_table +}; + +static int __init gs_usb_init (void ) +{ +return usb_register (&gs_usb_driver); +} + +static void __exit gs_usb_exit(void) +{ +usb_deregister (&gs_usb_driver); +} + +module_init (gs_usb_init); +module_exit (gs_usb_exit); + +MODULE_AUTHOR ("Maximilian Schneider <[email protected]>" ); +MODULE_DESCRIPTION ( +"Socket CAN device driver for Geschwister Schneider Technologie-, " +"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces." ); +MODULE_LICENSE ("GPL v2" ); +Signature · 1 line
1.7.10.4
Wolfgang.
Hello Wolfgang,
Here is the cleaned up patch incorporating your suggestions.
I am using "git commit --amend" to compile changes into a single patch. Is this the correct method?
From 98ba2435f65df418f64c05a38ff4c013df98fdb4 Mon Sep 17 00:00:00 2001
From: Maximilian Schneider <[email protected]>
Date: Mon, 7 Oct 2013 14:02:53 +0000
Subject: [PATCH] GS_USB CAN driver
--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1118 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1127 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.c
drivers/net/can/usb/Kconfig +8 -0
@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenu
drivers/net/can/usb/Makefile +1 -0
@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/usb/gs_usb.c new file +8 -0
@@ -0,0 +1,1118 @@ +/* CAN driver for Geschwister Schneider USB/CAN devices. +* +* Copyright (C) 2013 Geschwister Schneider Technologie-, +* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). +* +* Many thanks to all socketcan devs! +* +* This program is free software; you can redistribute it and/or modify
it
+ * under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#include <linux/init.h>
+#include <linux/signal.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/usb.h>
+
+#include <linux/can.h>
+#include <linux/can/dev.h>
+#include <linux/can/error.h>
+
+/* Device specific constants */
+#define USB_GSUSB_1_VENDOR_ID 0x1d50
+#define USB_GSUSB_1_PRODUCT_ID 0x606f
+
+#define GSUSB_ENDPOINT_IN 1
+#define GSUSB_ENDPOINT_OUT 2
+
+/* Device specific constants */
+enum gs_usb_breq {
+ GS_USB_BREQ_HOST_FORMAT = 0,
+ GS_USB_BREQ_BITTIMING,
+ GS_USB_BREQ_MODE,
+ GS_USB_BREQ_BERR,
+ GS_USB_BREQ_BT_CONST,
+ GS_USB_BREQ_DEVICE_CONFIG
+};
+
+enum gs_can_mode {
+ GS_CAN_MODE_RESET = 0,
+ GS_CAN_MODE_START
+};
+
+enum gs_can_state {
+ GS_CAN_STATE_ERROR_ACTIVE = 0,
+ GS_CAN_STATE_ERROR_WARNING,
+ GS_CAN_STATE_ERROR_PASSIVE,
+ GS_CAN_STATE_BUS_OFF,
+ GS_CAN_STATE_STOPPED,
+ GS_CAN_STATE_SLEEPING
+};
+
+/* data types passed between host and device */
+struct __packed gs_host_config {
+ u32 byte_order;
+};
+
+struct __packed gs_device_config {
+ u8 reserved1;
+ u8 reserved2;
+ u8 reserved3;
+ u8 icount;
+ u32 sw_version;
+ u32 hw_version;
+};
+
+#define GS_CAN_MODE_NORMAL 0 +#define GS_CAN_MODE_LISTEN_ONLY (1<<0) +#define GS_CAN_MODE_LOOP_BACK (1<<1) +#define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_MODE_ONE_SHOT (1<<3)
+
+struct __packed gs_device_mode {
+ u32 mode;
+ u32 flags;
+};
+
+struct __packed gs_device_state {
+ u32 state;
+ u32 rxerr;
+ u32 txerr;
+};
+
+struct __packed gs_device_bittiming {
+ u32 prop_seg;
+ u32 phase_seg1;
+ u32 phase_seg2;
+ u32 sjw;
+ u32 brp;
+};
+
+#define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) +#define GS_CAN_FEATURE_LOOP_BACK (1<<1) +#define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_FEATURE_ONE_SHOT (1<<3)
+
+struct __packed gs_device_bt_const {
+ u32 feature;
+ u32 fclk_can;
+ u32 tseg1_min;
+ u32 tseg1_max;
+ u32 tseg2_min;
+ u32 tseg2_max;
+ u32 sjw_max;
+ u32 brp_min;
+ u32 brp_max;
+ u32 brp_inc;
+};
+
+#define GS_CAN_FLAG_OVERFLOW 1
+
+struct __packed gs_host_frame {
+ u32 echo_id;
+ u32 can_id;
+
+ u8 can_dlc;
+ u8 channel;
+ u8 flags;
+ u8 reserved;
+
+ u8 data[8];
+};
+
+/* Special address description flags for the CAN_ID */ +#define GS_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ +#define GS_RTR_FLAG 0x40000000U /* remote transmission request */ +#define GS_ERR_FLAG 0x20000000U /* error frame */
+
+/* Valid bits in CAN ID for frame formats */
+#define GS_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ +#define GS_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ +#define GS_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */ +
+/* Only send a max of MAX_TX_URBS frames per channel to the device at a time. */
+#define MAX_TX_URBS 10
+/* Only launch a max of MAX_RX_URBS usb requests at a time. */ +#define MAX_RX_URBS 30
+/* Maximum number of interfaces the driver supports per device.
+ * Current hardware only supports 2 interfaces. The future may vary.
+ */
+#define MAX_INTF 2
+
+struct gs_tx_context {
+ struct gs_can *dev;
+ unsigned int echo_id;
+};
+
+struct gs_can {
+ struct can_priv can; /* must be the first member */
+
+ struct gs_usb *parent;
+
+ struct net_device *netdev;
+ struct usb_device *udev;
+ struct usb_interface *iface;
+
+ struct can_bittiming_const bt_const;
+ unsigned int channel; /* channel number */
+
+ /* This lock prevents a race condition
+ * between xmit and recieve.
+ */
+ spinlock_t tx_ctx_lock;
+ struct gs_tx_context tx_context[MAX_TX_URBS];
+
+ struct usb_anchor tx_submitted;
+ atomic_t active_tx_urbs;
+};
+
+/* usb interface struct */
+struct gs_usb {
+ struct gs_can *canch[MAX_INTF];
+
+ struct usb_anchor rx_submitted;
+
+ atomic_t active_channels;
+};
+
+/* 'allocate' a tx context.
+ * returns a valid tx context or NULL if there is no space.
+ */
+static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev) +{
+ int i = 0;
+
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->tx_ctx_lock, flags);
+
+ for (; i < MAX_TX_URBS; i++) {
+ if (dev->tx_context[i].echo_id == MAX_TX_URBS) {
+ dev->tx_context[i].echo_id = i;
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return &dev->tx_context[i];
+ }
+ }
+
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return NULL;
+}
+
+/* releases a tx context
+ */
+static void gs_free_tx_context(struct gs_tx_context *txc) +{
+ unsigned long flags;
+
+ spin_lock_irqsave(&txc->dev->tx_ctx_lock, flags);
+ txc->echo_id = MAX_TX_URBS;
+ spin_unlock_irqrestore(&txc->dev->tx_ctx_lock, flags);
+}
+
+/* Get a tx context by id.
+ */
+static struct gs_tx_context *gs_get_tx_context(
+ struct gs_can *dev,
+ unsigned int id)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->tx_ctx_lock, flags);
+ if (id < MAX_TX_URBS) {
+ if (dev->tx_context[id].echo_id == id) {
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return &dev->tx_context[id];
+ }
+ }
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return NULL;
+}
+
+/* Get a tx contexts id.
+ */
+static unsigned int gs_tx_context_id(struct gs_tx_context *txc) +{
+ unsigned int id;
+
+ unsigned long flags;
+
+ spin_lock_irqsave(&txc->dev->tx_ctx_lock, flags);
+ id = txc->echo_id;
+ spin_unlock_irqrestore(&txc->dev->tx_ctx_lock, flags);
+ return id;
+}
+
+static int gs_cmd_reset(struct gs_usb *gsusb, struct gs_can *gsdev) +{
+ struct gs_device_mode *dm;
+ struct usb_interface *intf = gsdev->iface;
+ int rc;
+
+ dm = kzalloc(sizeof(*dm), GFP_KERNEL);
+ if (!dm)
+ return -ENOMEM;
+
+ dm->mode = GS_CAN_MODE_RESET;
+
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_sndctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_MODE,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ gsdev->channel,
+ 0,
+ dm,
+ sizeof(*dm),
+ 1000);
+
+ return rc;
+}
+
+/**************************** USB CALLBACKS **************************/ +static void gs_usb_recieve_bulk_callback(struct urb *urb) +{
+ struct gs_usb *usbcan = urb->context;
+ struct gs_can *dev;
+ struct net_device *netdev;
+ int rc;
+ struct net_device_stats *stats;
+ struct gs_host_frame *hf = urb->transfer_buffer;
+ struct gs_tx_context *txc;
+
+ struct can_frame *cf;
+ struct sk_buff *skb;
+
+ BUG_ON(!usbcan);
+
+ switch (urb->status) {
+ case 0: /* success */
+ break;
+ case -ENOENT:
+ case -ESHUTDOWN:
+ return;
+ default:
+ /* do not resubmit aborted urbs. eg: when device goes down */
+ return;
+ }
+
+ /* device reports out of range channel id */
+ if (hf->channel >= MAX_INTF)
+ goto resubmit_urb;
+
+ /* device reports bad channel id */
+ dev = usbcan->canch[hf->channel];
+
+ netdev = dev->netdev;
+
+ if (!netif_device_present(netdev))
+ return;
+
+ stats = &dev->netdev->stats;
+
+ if (hf->echo_id == -1) { /* normal rx */
+ skb = alloc_can_skb(dev->netdev, &cf);
+ if (skb == NULL)
+ return;
+
+ cf->can_id = hf->can_id;
+ /* is it necessary to use get_can_dlc? */
+ cf->can_dlc = get_can_dlc(hf->can_dlc);
+ memcpy(cf->data, hf->data, 8);
+
+ netif_rx(skb);
+
+ netdev->stats.rx_packets++;
+ netdev->stats.rx_bytes += hf->can_dlc;
+ } else { /* echo_id == hf->echo_id */
+ if (hf->echo_id >= MAX_TX_URBS) {
+ netdev_err(netdev,
+ "Device sent an out of range echo id:%d\n",
+ hf->echo_id);
+ goto resubmit_urb;
+ }
+
+ netdev->stats.tx_packets++;
+ netdev->stats.tx_bytes += hf->can_dlc;
+
+ txc = gs_get_tx_context(dev, hf->echo_id);
+
+ /* bad devices send bad echo_ids. */
+ if (!txc) {
+ netdev_err(netdev,
+ "Device sent a bad echo id:%d\n",
+ hf->echo_id);
+ goto resubmit_urb;
+ }
+
+ can_get_echo_skb(netdev, gs_tx_context_id(txc));
+
+ gs_free_tx_context(txc);
+
+ netif_wake_queue(netdev);
+ }
+
+ if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
+ skb = alloc_can_err_skb(netdev, &cf);
+ if (skb == NULL)
+ goto resubmit_urb;
+
+ cf->can_id |= CAN_ERR_CRTL;
+ cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
+ stats->rx_over_errors++;
+ stats->rx_errors++;
+ netif_rx(skb);
+ }
+
+
+resubmit_urb:
+ /* note: no urb refill... works for me */
+
+ rc = usb_submit_urb(urb, GFP_ATOMIC);
+
+ /* USB failure take down all interfaces */
+ if (rc == -ENODEV) {
+ for (rc = 0; rc < MAX_INTF; rc++) {
+ if (usbcan->canch[rc])
+ netif_device_detach(usbcan->canch[rc]->netdev);
+ }
+ }
+}
+
+static int gs_usb_set_bittiming(struct net_device *netdev) +{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct can_bittiming *bt = &dev->can.bittiming;
+ struct usb_interface *intf = dev->iface;
+ int rc;
+ struct gs_device_bittiming *dbt;
+
+ dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
+ if (!dbt)
+ return -ENOMEM;
+
+ dbt->prop_seg = bt->prop_seg;
+ dbt->phase_seg1 = bt->phase_seg1;
+ dbt->phase_seg2 = bt->phase_seg2;
+ dbt->sjw = bt->sjw;
+ dbt->brp = bt->brp;
+
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_sndctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_BITTIMING,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dbt,
+ sizeof(*dbt),
+ 1000);
+ if (rc < 0)
+ dev_err(netdev->dev.parent, "Couldn't set bittimings %d", rc);
+
+ kfree(dbt);
+
+ return rc;
+}
+
+static int gs_usb_set_mode(struct net_device *netdev, enum can_mode mode)
+{
+ if (mode == CAN_MODE_START) {
+ netif_wake_queue(netdev);
+ return 0;
+ }
+
+ return -EOPNOTSUPP;
+}
+
+static int gs_usb_get_state(const struct net_device *netdev,
+ enum can_state *state)
+{
+
+ struct gs_can *dev = netdev_priv(netdev);
+ struct usb_interface *intf = dev->iface;
+ int rc;
+ struct gs_device_state *dstate;
+
+ dev_warn(netdev->dev.parent, "CAN state");
+
+ dstate = kmalloc(sizeof(*dstate), GFP_KERNEL);
+ if (!dstate)
+ return -ENOMEM;
+
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_BERR,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dstate,
+ sizeof(*dstate),
+ 1000);
+
+ if (rc < 0) {
+ dev_warn(netdev->dev.parent, "Couldn't get state %d", rc);
+ kfree(dstate);
+ return rc;
+ }
+
+ switch (dstate->state) {
+ /* RX/TX error count < 96 */
+ case GS_CAN_STATE_ERROR_ACTIVE:
+ *state = CAN_STATE_ERROR_ACTIVE;
+ break;
+ /* RX/TX error count < 128 */
+ case GS_CAN_STATE_ERROR_WARNING:
+ *state = CAN_STATE_ERROR_WARNING;
+ break;
+ /* RX/TX error count < 256 */
+ case GS_CAN_STATE_ERROR_PASSIVE:
+ *state = CAN_STATE_ERROR_PASSIVE;
+ break;
+ /* RX/TX error count >= 256 */
+ case GS_CAN_STATE_BUS_OFF:
+ *state = CAN_STATE_BUS_OFF;
+ break;
+ /* Device is stopped */
+ case GS_CAN_STATE_STOPPED:
+ *state = CAN_STATE_STOPPED;
+ break;
+ /* Device is sleeping */
+ case GS_CAN_STATE_SLEEPING:
+ *state = CAN_STATE_SLEEPING;
+ break;
+ default:
+ kfree(dstate);
+ return -ENOTSUPP;
+ }
+
+ kfree(dstate);
+ return 0;
+}
+
+static int gs_usb_get_berr_counter(const struct net_device *netdev,
+ struct can_berr_counter *bec)
+{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct usb_interface *intf = dev->iface;
+ int rc;
+ struct gs_device_state *dstate;
+
+ dstate = kmalloc(sizeof(*dstate), GFP_KERNEL);
+ if (!dstate)
+ return -ENOMEM;
+
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_BERR,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dstate,
+ sizeof(*dstate),
+ 1000);
+ if (rc < 0) {
+ dev_warn(netdev->dev.parent, "Couldn't get error counters %d",
+ rc);
+ kfree(dstate);
+ return rc;
+ }
+
+ bec->txerr = dstate->txerr;
+ bec->rxerr = dstate->rxerr;
+
+ kfree(dstate);
+ return 0;
+}
+
+static void gs_usb_xmit_callback(struct urb *urb)
+{
+ struct gs_tx_context *txc = urb->context;
+ struct gs_can *dev;
+ struct net_device *netdev;
+
+ dev = txc->dev;
+
+ netdev = dev->netdev;
+
+ if (urb->status) {
+ netdev_info(netdev, "%d] xmit err %d\n",
+ dev->channel, txc->echo_id);
+ }
+
+ usb_free_coherent(urb->dev,
+ urb->transfer_buffer_length,
+ urb->transfer_buffer,
+ urb->transfer_dma);
+
+ atomic_dec(&dev->active_tx_urbs);
+
+ if (!netif_device_present(netdev))
+ return;
+
+ if (netif_queue_stopped(netdev))
+ netif_wake_queue(netdev);
+}
+
+static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
+ struct net_device *netdev)
+{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct net_device_stats *stats = &dev->netdev->stats;
+ struct urb *urb;
+ struct gs_host_frame *hf;
+ struct can_frame *cf;
+ int rc;
+ unsigned int idx;
+ struct gs_tx_context *txc;
+
+ if (dev->can.ctrlmode&CAN_CTRLMODE_LISTENONLY)
+ return NETDEV_TX_BUSY;
+
+ if (can_dropped_invalid_skb(netdev, skb))
+ return NETDEV_TX_OK;
+
+ /* find an empty context to keep track of transmission */
+ txc = gs_alloc_tx_context(dev);
+ if (!txc)
+ return NETDEV_TX_BUSY;
+
+ /* create a URB, and a buffer for it */
+ urb = usb_alloc_urb(0, GFP_ATOMIC);
+ if (!urb) {
+ netdev_err(netdev, "No memory left for URBs\n");
+ goto nomem_urb;
+ }
+
+ hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
+ &urb->transfer_dma);
+ if (!hf) {
+ netdev_err(netdev, "No memory left for USB buffer\n");
+ goto nomem_hf;
+ }
+
+ cf = (struct can_frame *)skb->data;
+
+ idx = gs_tx_context_id(txc);
+
+ if (idx >= MAX_TX_URBS) {
+ netdev_err(netdev, "Invalid tx context %d\n", idx);
+ goto badidx;
+ }
+
+ hf->echo_id = idx;
+ hf->channel = dev->channel;
+
+ hf->can_id = cf->can_id;
+ hf->can_dlc = cf->can_dlc;
+ memcpy(hf->data, cf->data, 8);
+
+ usb_fill_bulk_urb(urb, dev->udev,
+ usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
+ hf,
+ sizeof(*hf),
+ gs_usb_xmit_callback,
+ txc);
+
+ urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+ usb_anchor_urb(urb, &dev->tx_submitted);
+
+ can_put_echo_skb(skb, netdev, idx);
+
+ atomic_inc(&dev->active_tx_urbs);
+
+ rc = usb_submit_urb(urb, GFP_ATOMIC);
+ if (unlikely(rc)) { /* usb send failed */
+ atomic_dec(&dev->active_tx_urbs);
+
+ netdev_err(netdev, "usb_submit failed %d\n", rc);
+
+ can_free_echo_skb(netdev, gs_tx_context_id(txc));
+ gs_free_tx_context(txc);
+
+ usb_unanchor_urb(urb);
+ usb_free_coherent(dev->udev,
+ sizeof(*hf),
+ hf,
+ urb->transfer_dma);
+
+
+
+ if (rc == -ENODEV) {
+ netif_device_detach(netdev);
+ } else {
+ netdev_err(netdev, "Failed tx_urb %d\n", rc);
+
+ stats->tx_dropped++;
+ }
+ } else {
+ netdev->trans_start = jiffies;
+
+ /* Slow down tx path */
+ if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS)
+ netif_stop_queue(netdev);
+ }
+
+ /* let usb core take care of this urb */
+ usb_free_urb(urb);
+
+ return NETDEV_TX_OK;
+
+badidx:
+ usb_free_coherent(dev->udev,
+ sizeof(*hf),
+ hf,
+ urb->transfer_dma);
+nomem_hf:
+ usb_free_urb(urb);
+
+nomem_urb:
+ gs_free_tx_context(txc);
+ dev_kfree_skb(skb);
+ stats->tx_dropped++;
+ return NETDEV_TX_OK;
+}
+
+static int gs_can_open(struct net_device *netdev)
+{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct gs_usb *parent = dev->parent;
+ int rc, i;
+ struct gs_device_mode *dm;
+ u32 ctrlmode;
+
+ /* common open */
+ rc = open_candev(netdev);
+ if (rc)
+ return rc;
+
+ if (atomic_add_return(1, &parent->active_channels) == 1) {
+ for (i = 0; i < MAX_RX_URBS; i++) {
+ struct urb *urb = NULL;
+ u8 *buf = NULL;
+
+ /* alloc rx urb */
+ urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb) {
+ netdev_err(netdev,
+ "No memory left for URBs\n");
+ return -ENOMEM;
+ }
+
+ /* alloc rx buffer */
+ buf = usb_alloc_coherent(dev->udev,
+ sizeof(struct gs_host_frame),
+ GFP_KERNEL,
+ &urb->transfer_dma);
+ if (!buf) {
+ netdev_err(netdev,
+ "No memory left for USB buffer\n");
+ usb_free_urb(urb);
+ return -ENOMEM;
+ }
+
+
+ /* fill, anchor, and submit rx urb */
+ usb_fill_bulk_urb(urb,
+ dev->udev,
+ usb_rcvbulkpipe(dev->udev,
+ GSUSB_ENDPOINT_IN),
+ buf,
+ sizeof(struct gs_host_frame),
+ gs_usb_recieve_bulk_callback,
+ parent
+ );
+ urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+
+ usb_anchor_urb(urb, &parent->rx_submitted);
+
+ rc = usb_submit_urb(urb, GFP_KERNEL);
+ if (rc) {
+ if (rc == -ENODEV)
+ netif_device_detach(dev->netdev);
+
+ usb_unanchor_urb(urb);
+ break;
+ }
+
+ /* Drop reference,
+ * USB core will take care of freeing it
+ */
+ usb_free_urb(urb);
+ }
+ }
+
+ dm = kmalloc(sizeof(*dm), GFP_KERNEL);
+ if (!dm)
+ return -ENOMEM;
+
+ /* flags */
+ ctrlmode = dev->can.ctrlmode;
+ dm->flags = 0;
+
+ if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
+ dm->flags |= GS_CAN_MODE_LOOP_BACK;
+
+ else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
+ dm->flags |= GS_CAN_MODE_LISTEN_ONLY;
+
+
+ /* Controller is not allowed to retry TX
+ * this mode is unavailable on atmels uc3c hardware
+ */
+ if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
+ dm->flags |= GS_CAN_MODE_ONE_SHOT;
+
+ if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+ dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
+
+ /* finally start device */
+ dm->mode = GS_CAN_MODE_START;
+ rc = usb_control_msg(interface_to_usbdev(dev->iface),
+ usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
+ GS_USB_BREQ_MODE,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dm,
+ sizeof(*dm),
+ 1000);
+
+ if (rc < 0) {
+ netdev_err(netdev, "Couldn't start device %d\n", rc);
+ kfree(dm);
+ return rc;
+ }
+
+ /* check mode... */
+ if (!dm)
+ return -ENOMEM;
+
+ rc = usb_control_msg(interface_to_usbdev(dev->iface),
+ usb_rcvctrlpipe(interface_to_usbdev(dev->iface), 0),
+ GS_USB_BREQ_MODE,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dm,
+ sizeof(*dm),
+ 1000);
+
+ if (rc < 0) {
+ netdev_err(netdev, "Couldn't get device mode %d\n",
+ rc);
+ kfree(dm);
+ return rc;
+ }
+
+ kfree(dm);
+
+ dev->can.state = CAN_STATE_ERROR_ACTIVE;
+
+ netif_start_queue(netdev);
+
+ netdev_info(netdev, "CAN open\n");
+ return 0;
+}
+
+static int gs_can_close(struct net_device *netdev)
+{
+ int rc;
+ struct gs_can *dev = netdev_priv(netdev);
+ struct gs_usb *parent = dev->parent;
+
+ netif_stop_queue(netdev);
+
+ /* Stop polling */
+ if (atomic_dec_and_test(&parent->active_channels))
+ usb_kill_anchored_urbs(&parent->rx_submitted);
+
+ rc = gs_cmd_reset(parent, dev);
+
+ if (rc < 0)
+ netdev_warn(netdev, "Couldn't shutdown device %d", rc);
+
+ close_candev(netdev);
+
+ return 0;
+}
+
+/******************************** USB ********************************/ +
+static const struct net_device_ops gs_usb_netdev_ops = {
+ .ndo_open = gs_can_open,
+ .ndo_stop = gs_can_close,
+ .ndo_start_xmit = gs_can_start_xmit,
+};
+
+static int gs_make_candev(struct gs_can **out, unsigned int channel,
+ struct usb_interface *intf)
+{
+ struct gs_can *dev;
+ struct net_device *netdev;
+ int rc;
+ struct gs_device_bt_const *bt_const;
+
+ bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
+ if (!bt_const)
+ return -ENOMEM;
+
+ /* fetch bit timing constants */
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_BT_CONST,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ channel,
+ 0,
+ bt_const,
+ sizeof(*bt_const),
+ 1000);
+
+ if (rc < 0) {
+ dev_err(&intf->dev,
+ "Couldn't get bit timing const for channel %d\n",
+ rc);
+ kfree(bt_const);
+ return rc;
+ }
+
+ /* create netdev */
+ netdev = alloc_candev(sizeof(struct gs_can), MAX_TX_URBS);
+ if (!netdev) {
+ dev_err(&intf->dev, "Couldn't alloc candev\n");
+ kfree(bt_const);
+ return -ENOMEM;
+ }
+
+ dev = netdev_priv(netdev);
+
+ netdev->netdev_ops = &gs_usb_netdev_ops;
+
+ netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
+
+ /* dev settup */
+ strcpy(dev->bt_const.name, "gs_usb");
+ dev->bt_const.tseg1_min = bt_const->tseg1_min;
+ dev->bt_const.tseg1_max = bt_const->tseg1_max;
+ dev->bt_const.tseg2_min = bt_const->tseg2_min;
+ dev->bt_const.tseg2_max = bt_const->tseg2_max;
+ dev->bt_const.sjw_max = bt_const->sjw_max;
+ dev->bt_const.brp_min = bt_const->brp_min;
+ dev->bt_const.brp_max = bt_const->brp_max;
+ dev->bt_const.brp_inc = bt_const->brp_inc;
+
+ dev->udev = interface_to_usbdev(intf);
+ dev->iface = intf;
+ dev->netdev = netdev;
+ dev->channel = channel;
+
+ init_usb_anchor(&dev->tx_submitted);
+ atomic_set(&dev->active_tx_urbs, 0);
+ spin_lock_init(&dev->tx_ctx_lock);
+ for (rc = 0; rc < MAX_TX_URBS; rc++) {
+ dev->tx_context[rc].dev = dev;
+ dev->tx_context[rc].echo_id = MAX_TX_URBS;
+ }
+
+ /* can settup */
+ dev->can.state = CAN_STATE_STOPPED;
+ dev->can.clock.freq = bt_const->fclk_can;
+ dev->can.bittiming_const = &dev->bt_const;
+ dev->can.do_set_bittiming = gs_usb_set_bittiming;
+ dev->can.do_set_mode = gs_usb_set_mode;
+ dev->can.do_get_state = gs_usb_get_state;
+ dev->can.do_get_berr_counter = gs_usb_get_berr_counter;
+
+ dev->can.ctrlmode_supported = 0;
+
+ if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
+
+ if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
+
+ if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
+
+ if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
+
+ kfree(bt_const);
+
+ SET_NETDEV_DEV(netdev, &intf->dev);
+
+ rc = register_candev(dev->netdev);
+ if (rc) {
+ free_candev(dev->netdev);
+ dev_err(&intf->dev, "Couldn't register candev\n");
+ return rc;
+ }
+
+ *out = dev;
+ return 0;
+}
+
+static void gs_destroy_candev(struct gs_can *dev)
+{
+ unregister_candev(dev->netdev);
+ free_candev(dev->netdev);
+ usb_kill_anchored_urbs(&dev->tx_submitted);
+}
+
+static int gs_usb_probe(struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ struct gs_usb *dev;
+ int rc = -ENOMEM;
+ unsigned int icount, i;
+ struct gs_host_config *hconf;
+ struct gs_device_config *dconf;
+
+ hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
+ if (!hconf)
+ return -ENOMEM;
+
+ hconf->byte_order = 0x0000beef;
+
+
+ /* send host config */
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_sndctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_HOST_FORMAT,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ 1,
+ intf->altsetting[0].desc.bInterfaceNumber,
+ hconf,
+ sizeof(*hconf),
+ 1000);
+
+ kfree(hconf);
+
+ if (rc < 0) {
+ dev_err(&intf->dev, "Couldn't send data format: %d\n",
+ rc);
+ return rc;
+ }
+
+
+ dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
+ if (!dconf)
+ return -ENOMEM;
+
+ /* read device config */
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_DEVICE_CONFIG,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ 1,
+ intf->altsetting[0].desc.bInterfaceNumber,
+ dconf,
+ sizeof(*dconf),
+ 1000);
+ if (rc < 0) {
+ dev_err(&intf->dev, "Couldn't recieve device config: %d\n",
+ rc);
+
+ kfree(dconf);
+
+ return rc;
+ }
+
+ icount = dconf->icount+1;
+
+ kfree(dconf);
+
+ dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
+
+ if (icount > MAX_INTF) {
+ dev_err(&intf->dev,
+ "Driver cannot handle more that %d Can interfaces\n",
+ MAX_INTF);
+ return -EINVAL;
+ }
+
+ dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+ init_usb_anchor(&dev->rx_submitted);
+
+ atomic_set(&dev->active_channels, 0);
+
+ usb_set_intfdata(intf, dev);
+
+ for (i = 0; i < icount; i++) {
+ rc = gs_make_candev(&dev->canch[i], i, intf);
+ if (rc) {
+ /* on failure destroy previously created candevs */
+ icount = i;
+ for (i = 0; i < icount; i++) {
+ gs_destroy_candev(dev->canch[i]);
+ dev->canch[icount] = NULL;
+ }
+ return rc;
+ }
+ dev->canch[i]->parent = dev;
+ }
+
+ dev_info(&intf->dev, "Probe\n");
+ return 0;
+}
+
+static void gs_usb_disconnect(struct usb_interface *intf) +{
+ unsigned i;
+ struct gs_usb *dev = usb_get_intfdata(intf);
+ usb_set_intfdata(intf, NULL);
+
+ if (!dev) {
+ dev_err(&intf->dev, "Disconnect (nodata)\n");
+ return;
+ }
+
+ for (i = 0; i < MAX_INTF; i++) {
+ struct gs_can *can = dev->canch[i];
+
+ if (!can)
+ continue;
+
+ gs_destroy_candev(can);
+ }
+
+ usb_kill_anchored_urbs(&dev->rx_submitted);
+}
+
+MODULE_DEVICE_TABLE(usb, gs_usb_table);
+
+static struct usb_device_id gs_usb_table[] = {
+ {USB_DEVICE(USB_GSUSB_1_VENDOR_ID, USB_GSUSB_1_PRODUCT_ID)},
+ {} /* Terminating entry */
+};
+
+static struct usb_driver gs_usb_driver = {
+ .name = "gs_usb",
+ .probe = gs_usb_probe,
+ .disconnect = gs_usb_disconnect,
+ .id_table = gs_usb_table
+};
+
+static int __init gs_usb_init(void)
+{
+ return usb_register(&gs_usb_driver);
+}
+
+static void __exit gs_usb_exit(void)
+{
+ usb_deregister(&gs_usb_driver);
+}
+
+module_init(gs_usb_init);
+module_exit(gs_usb_exit);
+
+MODULE_AUTHOR("Maximilian Schneider <[email protected]>"); +MODULE_DESCRIPTION(
+"Socket CAN device driver for Geschwister Schneider Technologie-, " +"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces."); +MODULE_LICENSE("GPL v2");
+
Signature · 1 line
1.7.10.4
On Mon, 07 Oct 2013 14:22:00 +0000, "Max S." <[email protected]>
Quoted 11 lines · wrote:
Hello Wolfgang,
Here is the cleaned up patch incorporating your suggestions.
I am using "git commit --amend" to compile changes into a single patch.
Is this the correct method?
I normally use "git rebase -i <first-commit-id>"? It's very
powerful and allows to reorder, squash, re-word commits and
much more.
Will not find time for the review today...
The CAN controller does not do any asynchronous error reporting,
right? This will limit the usability of this device, especially
CAN applications relying on that feature will not work. Any chance
to add asynchronous error reporting to the firmware?
Wolfgang.
Quoted 1 line · Wolfgang Grandegger
Will not find time for the review today...
no problem.
Quoted 4 lines
The CAN controller does not do any asynchronous error reporting, right? This will limit the usability of this device, especially CAN applications relying on that feature will not work. Any chance to add asynchronous error reporting to the firmware?
Only CAN_ERR_CRTL_RX_OVERFLOW is currently reported (possible when host is too slow).CAN_ERR_CRTL_TX_OVERFLOW is impossible due to the driver design (tx context + echo).
Which errors are most important to the applications you mentioned?
The hardware does not allow for the sensing of the CAN_ERR_TRX_* errors. I can sense some CAN_ERR_PROT_* errors, as well as all CAN_ERR_CRTL_* errors.
In firmware it is possible to queue error/status frames much like normal frames. So they would show up in the receive callback... Thats what you meant by asynchronous right?
regards,
Max Schneider.
Quoted 10 lines · Max S.
Quoted 1 line · Wolfgang Grandegger
Will not find time for the review today...
no problem.
Quoted 4 lines
The CAN controller does not do any asynchronous error reporting, right? This will limit the usability of this device, especially CAN applications relying on that feature will not work. Any chance to add asynchronous error reporting to the firmware?
Quoted 6 lines
Only
CAN_ERR_CRTL_RX_OVERFLOWis currently reported (possible when host is too slow).CAN_ERR_CRTL_TX_OVERFLOWis impossible due to the driver design (tx context + echo).Which errors are most important to the applications you mentioned?
CAN error state changes are sufficient, including bus-off. Bus-off recovery initiated by the software would be nice as well. Reporting individual bus errors is less important.
Quoted 7 lines
The hardware does not allow for the sensing of the CAN_ERR_TRX_* errors. I can sense some CAN_ERR_PROT_* errors, as well as all CAN_ERR_CRTL_* errors.
In firmware it is possible to queue error/status frames much like normal frames. So they would show up in the receive callback... Thats what you meant by asynchronous right?
Yep, an application can then read and handle these error messages. Here is the output of "candump -candump -td -e any,0:0,#FFFFFFFF" showing error state changes until bus-off and recovery with an SJA1000:
(000.202225) can0 5F [8] 07 B4 4F 67 3A 90 D9 E4
(000.201285) can0 20000204 [8] 00 08 00 00 00 00 78 00 ERRORFRAME
controller-problem{tx-error-warning}
error-counter-tx-rx{{120}{0}}
(000.003249) can0 20000240 [8] 00 00 00 00 00 00 00 00 ERRORFRAME
bus-off
(005.015531) can0 20000100 [8] 00 00 00 00 00 00 7F 00 ERRORFRAME
restarted-after-bus-off
error-counter-tx-rx{{127}{0}}We don't have yet support for decreasing the error state, e.g.:
(000.000133) can0 20000200 [8] 00 40 00 00 00 00 00 00 ERRORFRAME
state-change{back-to-error-active}Wolfgang.
Any progress on reviewing the GS_USB driver code?
I have added asynchronous state update. Here is the latest patch.
From 346f223bb3a8f4435f93cdfbc5e06db2cbf7179d Mon Sep 17 00:00:00 2001
From: Maximilian Schneider <[email protected]>
Date: Sun, 3 Nov 2013 16:57:06 +0000
Subject: [PATCH] Added support for the GS_USB CAN devices.
--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1037 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1046 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.c
drivers/net/can/usb/Kconfig +8 -0
@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenu
drivers/net/can/usb/Makefile +1 -0
@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/usb/gs_usb.c new file +8 -0
@@ -0,0 +1,1037 @@ +/* CAN driver for Geschwister Schneider USB/CAN devices. +* +* Copyright (C) 2013 Geschwister Schneider Technologie-, +* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). +* +* Many thanks to all socketcan devs! +* +* This program is free software; you can redistribute it and/or modify
it
+ * under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#include <linux/init.h>
+#include <linux/signal.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/usb.h>
+
+#include <linux/can.h>
+#include <linux/can/dev.h>
+#include <linux/can/error.h>
+
+/* Device specific constants */
+#define USB_GSUSB_1_VENDOR_ID 0x1d50
+#define USB_GSUSB_1_PRODUCT_ID 0x606f
+
+#define GSUSB_ENDPOINT_IN 1
+#define GSUSB_ENDPOINT_OUT 2
+
+/* Device specific constants */
+enum gs_usb_breq {
+ GS_USB_BREQ_HOST_FORMAT = 0,
+ GS_USB_BREQ_BITTIMING,
+ GS_USB_BREQ_MODE,
+ GS_USB_BREQ_BERR,
+ GS_USB_BREQ_BT_CONST,
+ GS_USB_BREQ_DEVICE_CONFIG
+};
+
+enum gs_can_mode {
+ GS_CAN_MODE_RESET = 0,
+ GS_CAN_MODE_START
+};
+
+enum gs_can_state {
+ GS_CAN_STATE_ERROR_ACTIVE = 0,
+ GS_CAN_STATE_ERROR_WARNING,
+ GS_CAN_STATE_ERROR_PASSIVE,
+ GS_CAN_STATE_BUS_OFF,
+ GS_CAN_STATE_STOPPED,
+ GS_CAN_STATE_SLEEPING
+};
+
+/* data types passed between host and device */
+struct __packed gs_host_config {
+ u32 byte_order;
+};
+
+struct __packed gs_device_config {
+ u8 reserved1;
+ u8 reserved2;
+ u8 reserved3;
+ u8 icount;
+ u32 sw_version;
+ u32 hw_version;
+};
+
+#define GS_CAN_MODE_NORMAL 0 +#define GS_CAN_MODE_LISTEN_ONLY (1<<0) +#define GS_CAN_MODE_LOOP_BACK (1<<1) +#define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_MODE_ONE_SHOT (1<<3)
+
+struct __packed gs_device_mode {
+ u32 mode;
+ u32 flags;
+};
+
+struct __packed gs_device_state {
+ u32 state;
+ u32 rxerr;
+ u32 txerr;
+};
+
+struct __packed gs_device_bittiming {
+ u32 prop_seg;
+ u32 phase_seg1;
+ u32 phase_seg2;
+ u32 sjw;
+ u32 brp;
+};
+
+#define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) +#define GS_CAN_FEATURE_LOOP_BACK (1<<1) +#define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_FEATURE_ONE_SHOT (1<<3)
+
+struct __packed gs_device_bt_const {
+ u32 feature;
+ u32 fclk_can;
+ u32 tseg1_min;
+ u32 tseg1_max;
+ u32 tseg2_min;
+ u32 tseg2_max;
+ u32 sjw_max;
+ u32 brp_min;
+ u32 brp_max;
+ u32 brp_inc;
+};
+
+#define GS_CAN_FLAG_OVERFLOW 1
+
+struct __packed gs_host_frame {
+ u32 echo_id;
+ u32 can_id;
+
+ u8 can_dlc;
+ u8 channel;
+ u8 flags;
+ u8 reserved;
+
+ u8 data[8];
+};
+
+/* Special address description flags for the CAN_ID */ +#define GS_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ +#define GS_RTR_FLAG 0x40000000U /* remote transmission request */ +#define GS_ERR_FLAG 0x20000000U /* error frame */
+
+/* Valid bits in CAN ID for frame formats */
+#define GS_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ +#define GS_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ +#define GS_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */ +
+/* Only send a max of MAX_TX_URBS frames per channel to the device at a time. */
+#define MAX_TX_URBS 10
+/* Only launch a max of MAX_RX_URBS usb requests at a time. */ +#define MAX_RX_URBS 30
+/* Maximum number of interfaces the driver supports per device.
+ * Current hardware only supports 2 interfaces. The future may vary.
+ */
+#define MAX_INTF 2
+
+struct gs_tx_context {
+ struct gs_can *dev;
+ unsigned int echo_id;
+};
+
+struct gs_can {
+ struct can_priv can; /* must be the first member */
+
+ struct gs_usb *parent;
+
+ struct net_device *netdev;
+ struct usb_device *udev;
+ struct usb_interface *iface;
+
+ struct can_bittiming_const bt_const;
+ unsigned int channel; /* channel number */
+
+ /* This lock prevents a race condition
+ * between xmit and recieve.
+ */
+ spinlock_t tx_ctx_lock;
+ struct gs_tx_context tx_context[MAX_TX_URBS];
+
+ struct usb_anchor tx_submitted;
+ atomic_t active_tx_urbs;
+};
+
+/* usb interface struct */
+struct gs_usb {
+ struct gs_can *canch[MAX_INTF];
+
+ struct usb_anchor rx_submitted;
+
+ atomic_t active_channels;
+};
+
+/* 'allocate' a tx context.
+ * returns a valid tx context or NULL if there is no space.
+ */
+static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev) +{
+ int i = 0;
+
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->tx_ctx_lock, flags);
+
+ for (; i < MAX_TX_URBS; i++) {
+ if (dev->tx_context[i].echo_id == MAX_TX_URBS) {
+ dev->tx_context[i].echo_id = i;
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return &dev->tx_context[i];
+ }
+ }
+
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return NULL;
+}
+
+/* releases a tx context
+ */
+static void gs_free_tx_context(struct gs_tx_context *txc) +{
+ unsigned long flags;
+
+ spin_lock_irqsave(&txc->dev->tx_ctx_lock, flags);
+ txc->echo_id = MAX_TX_URBS;
+ spin_unlock_irqrestore(&txc->dev->tx_ctx_lock, flags);
+}
+
+/* Get a tx context by id.
+ */
+static struct gs_tx_context *gs_get_tx_context(
+ struct gs_can *dev,
+ unsigned int id)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->tx_ctx_lock, flags);
+ if (id < MAX_TX_URBS) {
+ if (dev->tx_context[id].echo_id == id) {
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return &dev->tx_context[id];
+ }
+ }
+ spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
+ return NULL;
+}
+
+/* Get a tx contexts id.
+ */
+static unsigned int gs_tx_context_id(struct gs_tx_context *txc) +{
+ unsigned int id;
+
+ unsigned long flags;
+
+ spin_lock_irqsave(&txc->dev->tx_ctx_lock, flags);
+ id = txc->echo_id;
+ spin_unlock_irqrestore(&txc->dev->tx_ctx_lock, flags);
+ return id;
+}
+
+static int gs_cmd_reset(struct gs_usb *gsusb, struct gs_can *gsdev) +{
+ struct gs_device_mode *dm;
+ struct usb_interface *intf = gsdev->iface;
+ int rc;
+
+ dm = kzalloc(sizeof(*dm), GFP_KERNEL);
+ if (!dm)
+ return -ENOMEM;
+
+ dm->mode = GS_CAN_MODE_RESET;
+
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_sndctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_MODE,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ gsdev->channel,
+ 0,
+ dm,
+ sizeof(*dm),
+ 1000);
+
+ return rc;
+}
+
+static void gs_update_state(struct gs_can *dev, struct can_frame *cf) +{
+ if (cf->can_id & CAN_ERR_BUSOFF) {
+ dev->can.state = CAN_STATE_BUS_OFF;
+ } else if (cf->can_id & CAN_ERR_CRTL) {
+ dev->can.state = CAN_STATE_ERROR_ACTIVE;
+
+ if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
+ (cf->data[1] & CAN_ERR_CRTL_RX_WARNING))
+ dev->can.state = CAN_STATE_ERROR_WARNING;
+
+ if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
+ (cf->data[1]&CAN_ERR_CRTL_RX_PASSIVE))
+ dev->can.state = CAN_STATE_ERROR_PASSIVE;
+ }
+}
+
+/**************************** USB CALLBACKS **************************/ +static void gs_usb_recieve_bulk_callback(struct urb *urb) +{
+ struct gs_usb *usbcan = urb->context;
+ struct gs_can *dev;
+ struct net_device *netdev;
+ int rc;
+ struct net_device_stats *stats;
+ struct gs_host_frame *hf = urb->transfer_buffer;
+ struct gs_tx_context *txc;
+
+ struct can_frame *cf;
+ struct sk_buff *skb;
+
+ BUG_ON(!usbcan);
+
+ switch (urb->status) {
+ case 0: /* success */
+ break;
+ case -ENOENT:
+ case -ESHUTDOWN:
+ return;
+ default:
+ /* do not resubmit aborted urbs. eg: when device goes down */
+ return;
+ }
+
+ /* device reports out of range channel id */
+ if (hf->channel >= MAX_INTF)
+ goto resubmit_urb;
+
+ /* device reports bad channel id */
+ dev = usbcan->canch[hf->channel];
+
+ netdev = dev->netdev;
+
+ if (!netif_device_present(netdev))
+ return;
+
+ stats = &dev->netdev->stats;
+
+ if (hf->echo_id == -1) { /* normal rx */
+ skb = alloc_can_skb(dev->netdev, &cf);
+ if (skb == NULL)
+ return;
+
+ cf->can_id = hf->can_id;
+ /* is it necessary to use get_can_dlc? */
+ cf->can_dlc = get_can_dlc(hf->can_dlc);
+ memcpy(cf->data, hf->data, 8);
+
+ /* ERROR frames tell us information about the controler */
+ if (hf->can_id & GS_ERR_FLAG)
+ gs_update_state(dev, cf);
+
+ netif_rx(skb);
+
+ netdev->stats.rx_packets++;
+ netdev->stats.rx_bytes += hf->can_dlc;
+ } else { /* echo_id == hf->echo_id */
+ if (hf->echo_id >= MAX_TX_URBS) {
+ netdev_err(netdev,
+ "Device sent an out of range echo id:%d\n",
+ hf->echo_id);
+ goto resubmit_urb;
+ }
+
+ netdev->stats.tx_packets++;
+ netdev->stats.tx_bytes += hf->can_dlc;
+
+ txc = gs_get_tx_context(dev, hf->echo_id);
+
+ /* bad devices send bad echo_ids. */
+ if (!txc) {
+ netdev_err(netdev,
+ "Device sent a bad echo id:%d\n",
+ hf->echo_id);
+ goto resubmit_urb;
+ }
+
+ can_get_echo_skb(netdev, gs_tx_context_id(txc));
+
+ gs_free_tx_context(txc);
+
+ netif_wake_queue(netdev);
+ }
+
+ if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
+ skb = alloc_can_err_skb(netdev, &cf);
+ if (skb == NULL)
+ goto resubmit_urb;
+
+ cf->can_id |= CAN_ERR_CRTL;
+ cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
+ stats->rx_over_errors++;
+ stats->rx_errors++;
+ netif_rx(skb);
+ }
+
+
+resubmit_urb:
+ /* note: no urb refill... works for me */
+
+ rc = usb_submit_urb(urb, GFP_ATOMIC);
+
+ /* USB failure take down all interfaces */
+ if (rc == -ENODEV) {
+ for (rc = 0; rc < MAX_INTF; rc++) {
+ if (usbcan->canch[rc])
+ netif_device_detach(usbcan->canch[rc]->netdev);
+ }
+ }
+}
+
+static int gs_usb_set_bittiming(struct net_device *netdev) +{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct can_bittiming *bt = &dev->can.bittiming;
+ struct usb_interface *intf = dev->iface;
+ int rc;
+ struct gs_device_bittiming *dbt;
+
+ dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
+ if (!dbt)
+ return -ENOMEM;
+
+ dbt->prop_seg = bt->prop_seg;
+ dbt->phase_seg1 = bt->phase_seg1;
+ dbt->phase_seg2 = bt->phase_seg2;
+ dbt->sjw = bt->sjw;
+ dbt->brp = bt->brp;
+
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_sndctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_BITTIMING,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dbt,
+ sizeof(*dbt),
+ 1000);
+ if (rc < 0)
+ dev_err(netdev->dev.parent, "Couldn't set bittimings %d", rc);
+
+ kfree(dbt);
+
+ return rc;
+}
+
+static int gs_usb_set_mode(struct net_device *netdev, enum can_mode mode)
+{
+ if (mode == CAN_MODE_START) {
+ netif_wake_queue(netdev);
+ return 0;
+ }
+
+ return -EOPNOTSUPP;
+}
+
+static void gs_usb_xmit_callback(struct urb *urb)
+{
+ struct gs_tx_context *txc = urb->context;
+ struct gs_can *dev;
+ struct net_device *netdev;
+
+ dev = txc->dev;
+
+ netdev = dev->netdev;
+
+ if (urb->status) {
+ netdev_info(netdev, "%d] xmit err %d\n",
+ dev->channel, txc->echo_id);
+ }
+
+ usb_free_coherent(urb->dev,
+ urb->transfer_buffer_length,
+ urb->transfer_buffer,
+ urb->transfer_dma);
+
+ atomic_dec(&dev->active_tx_urbs);
+
+ if (!netif_device_present(netdev))
+ return;
+
+ if (netif_queue_stopped(netdev))
+ netif_wake_queue(netdev);
+}
+
+static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
+ struct net_device *netdev)
+{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct net_device_stats *stats = &dev->netdev->stats;
+ struct urb *urb;
+ struct gs_host_frame *hf;
+ struct can_frame *cf;
+ int rc;
+ unsigned int idx;
+ struct gs_tx_context *txc;
+
+ if (dev->can.ctrlmode&CAN_CTRLMODE_LISTENONLY)
+ return NETDEV_TX_BUSY;
+
+ if (can_dropped_invalid_skb(netdev, skb))
+ return NETDEV_TX_OK;
+
+ /* find an empty context to keep track of transmission */
+ txc = gs_alloc_tx_context(dev);
+ if (!txc)
+ return NETDEV_TX_BUSY;
+
+ /* create a URB, and a buffer for it */
+ urb = usb_alloc_urb(0, GFP_ATOMIC);
+ if (!urb) {
+ netdev_err(netdev, "No memory left for URBs\n");
+ goto nomem_urb;
+ }
+
+ hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
+ &urb->transfer_dma);
+ if (!hf) {
+ netdev_err(netdev, "No memory left for USB buffer\n");
+ goto nomem_hf;
+ }
+
+ cf = (struct can_frame *)skb->data;
+
+ idx = gs_tx_context_id(txc);
+
+ if (idx >= MAX_TX_URBS) {
+ netdev_err(netdev, "Invalid tx context %d\n", idx);
+ goto badidx;
+ }
+
+ hf->echo_id = idx;
+ hf->channel = dev->channel;
+
+ hf->can_id = cf->can_id;
+ hf->can_dlc = cf->can_dlc;
+ memcpy(hf->data, cf->data, 8);
+
+ usb_fill_bulk_urb(urb, dev->udev,
+ usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
+ hf,
+ sizeof(*hf),
+ gs_usb_xmit_callback,
+ txc);
+
+ urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+ usb_anchor_urb(urb, &dev->tx_submitted);
+
+ can_put_echo_skb(skb, netdev, idx);
+
+ atomic_inc(&dev->active_tx_urbs);
+
+ rc = usb_submit_urb(urb, GFP_ATOMIC);
+ if (unlikely(rc)) { /* usb send failed */
+ atomic_dec(&dev->active_tx_urbs);
+
+ netdev_err(netdev, "usb_submit failed %d\n", rc);
+
+ can_free_echo_skb(netdev, gs_tx_context_id(txc));
+ gs_free_tx_context(txc);
+
+ usb_unanchor_urb(urb);
+ usb_free_coherent(dev->udev,
+ sizeof(*hf),
+ hf,
+ urb->transfer_dma);
+
+
+
+ if (rc == -ENODEV) {
+ netif_device_detach(netdev);
+ } else {
+ netdev_err(netdev, "Failed tx_urb %d\n", rc);
+
+ stats->tx_dropped++;
+ }
+ } else {
+ netdev->trans_start = jiffies;
+
+ /* Slow down tx path */
+ if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS)
+ netif_stop_queue(netdev);
+ }
+
+ /* let usb core take care of this urb */
+ usb_free_urb(urb);
+
+ return NETDEV_TX_OK;
+
+badidx:
+ usb_free_coherent(dev->udev,
+ sizeof(*hf),
+ hf,
+ urb->transfer_dma);
+nomem_hf:
+ usb_free_urb(urb);
+
+nomem_urb:
+ gs_free_tx_context(txc);
+ dev_kfree_skb(skb);
+ stats->tx_dropped++;
+ return NETDEV_TX_OK;
+}
+
+static int gs_can_open(struct net_device *netdev)
+{
+ struct gs_can *dev = netdev_priv(netdev);
+ struct gs_usb *parent = dev->parent;
+ int rc, i;
+ struct gs_device_mode *dm;
+ u32 ctrlmode;
+
+ /* common open */
+ rc = open_candev(netdev);
+ if (rc)
+ return rc;
+
+ if (atomic_add_return(1, &parent->active_channels) == 1) {
+ for (i = 0; i < MAX_RX_URBS; i++) {
+ struct urb *urb = NULL;
+ u8 *buf = NULL;
+
+ /* alloc rx urb */
+ urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb) {
+ netdev_err(netdev,
+ "No memory left for URBs\n");
+ return -ENOMEM;
+ }
+
+ /* alloc rx buffer */
+ buf = usb_alloc_coherent(dev->udev,
+ sizeof(struct gs_host_frame),
+ GFP_KERNEL,
+ &urb->transfer_dma);
+ if (!buf) {
+ netdev_err(netdev,
+ "No memory left for USB buffer\n");
+ usb_free_urb(urb);
+ return -ENOMEM;
+ }
+
+
+ /* fill, anchor, and submit rx urb */
+ usb_fill_bulk_urb(urb,
+ dev->udev,
+ usb_rcvbulkpipe(dev->udev,
+ GSUSB_ENDPOINT_IN),
+ buf,
+ sizeof(struct gs_host_frame),
+ gs_usb_recieve_bulk_callback,
+ parent
+ );
+ urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+
+ usb_anchor_urb(urb, &parent->rx_submitted);
+
+ rc = usb_submit_urb(urb, GFP_KERNEL);
+ if (rc) {
+ if (rc == -ENODEV)
+ netif_device_detach(dev->netdev);
+
+ usb_unanchor_urb(urb);
+ break;
+ }
+
+ /* Drop reference,
+ * USB core will take care of freeing it
+ */
+ usb_free_urb(urb);
+ }
+ }
+
+ dm = kmalloc(sizeof(*dm), GFP_KERNEL);
+ if (!dm)
+ return -ENOMEM;
+
+ /* flags */
+ ctrlmode = dev->can.ctrlmode;
+ dm->flags = 0;
+
+ if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
+ dm->flags |= GS_CAN_MODE_LOOP_BACK;
+
+ else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
+ dm->flags |= GS_CAN_MODE_LISTEN_ONLY;
+
+
+ /* Controller is not allowed to retry TX
+ * this mode is unavailable on atmels uc3c hardware
+ */
+ if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
+ dm->flags |= GS_CAN_MODE_ONE_SHOT;
+
+ if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+ dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
+
+ /* finally start device */
+ dm->mode = GS_CAN_MODE_START;
+ rc = usb_control_msg(interface_to_usbdev(dev->iface),
+ usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
+ GS_USB_BREQ_MODE,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dm,
+ sizeof(*dm),
+ 1000);
+
+ if (rc < 0) {
+ netdev_err(netdev, "Couldn't start device %d\n", rc);
+ kfree(dm);
+ return rc;
+ }
+
+ /* check mode... */
+ if (!dm)
+ return -ENOMEM;
+
+ rc = usb_control_msg(interface_to_usbdev(dev->iface),
+ usb_rcvctrlpipe(interface_to_usbdev(dev->iface), 0),
+ GS_USB_BREQ_MODE,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ dev->channel,
+ 0,
+ dm,
+ sizeof(*dm),
+ 1000);
+
+ if (rc < 0) {
+ netdev_err(netdev, "Couldn't get device mode %d\n",
+ rc);
+ kfree(dm);
+ return rc;
+ }
+
+ kfree(dm);
+
+ dev->can.state = CAN_STATE_ERROR_ACTIVE;
+
+ netif_start_queue(netdev);
+
+ netdev_info(netdev, "CAN open\n");
+ return 0;
+}
+
+static int gs_can_close(struct net_device *netdev)
+{
+ int rc;
+ struct gs_can *dev = netdev_priv(netdev);
+ struct gs_usb *parent = dev->parent;
+
+ netif_stop_queue(netdev);
+
+ /* Stop polling */
+ if (atomic_dec_and_test(&parent->active_channels))
+ usb_kill_anchored_urbs(&parent->rx_submitted);
+
+ rc = gs_cmd_reset(parent, dev);
+
+ if (rc < 0)
+ netdev_warn(netdev, "Couldn't shutdown device %d", rc);
+
+ close_candev(netdev);
+
+ return 0;
+}
+
+/******************************** USB ********************************/ +
+static const struct net_device_ops gs_usb_netdev_ops = {
+ .ndo_open = gs_can_open,
+ .ndo_stop = gs_can_close,
+ .ndo_start_xmit = gs_can_start_xmit,
+};
+
+static int gs_make_candev(struct gs_can **out, unsigned int channel,
+ struct usb_interface *intf)
+{
+ struct gs_can *dev;
+ struct net_device *netdev;
+ int rc;
+ struct gs_device_bt_const *bt_const;
+
+ bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
+ if (!bt_const)
+ return -ENOMEM;
+
+ /* fetch bit timing constants */
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_BT_CONST,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ channel,
+ 0,
+ bt_const,
+ sizeof(*bt_const),
+ 1000);
+
+ if (rc < 0) {
+ dev_err(&intf->dev,
+ "Couldn't get bit timing const for channel %d\n",
+ rc);
+ kfree(bt_const);
+ return rc;
+ }
+
+ /* create netdev */
+ netdev = alloc_candev(sizeof(struct gs_can), MAX_TX_URBS);
+ if (!netdev) {
+ dev_err(&intf->dev, "Couldn't alloc candev\n");
+ kfree(bt_const);
+ return -ENOMEM;
+ }
+
+ dev = netdev_priv(netdev);
+
+ netdev->netdev_ops = &gs_usb_netdev_ops;
+
+ netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
+
+ /* dev settup */
+ strcpy(dev->bt_const.name, "gs_usb");
+ dev->bt_const.tseg1_min = bt_const->tseg1_min;
+ dev->bt_const.tseg1_max = bt_const->tseg1_max;
+ dev->bt_const.tseg2_min = bt_const->tseg2_min;
+ dev->bt_const.tseg2_max = bt_const->tseg2_max;
+ dev->bt_const.sjw_max = bt_const->sjw_max;
+ dev->bt_const.brp_min = bt_const->brp_min;
+ dev->bt_const.brp_max = bt_const->brp_max;
+ dev->bt_const.brp_inc = bt_const->brp_inc;
+
+ dev->udev = interface_to_usbdev(intf);
+ dev->iface = intf;
+ dev->netdev = netdev;
+ dev->channel = channel;
+
+ init_usb_anchor(&dev->tx_submitted);
+ atomic_set(&dev->active_tx_urbs, 0);
+ spin_lock_init(&dev->tx_ctx_lock);
+ for (rc = 0; rc < MAX_TX_URBS; rc++) {
+ dev->tx_context[rc].dev = dev;
+ dev->tx_context[rc].echo_id = MAX_TX_URBS;
+ }
+
+ /* can settup */
+ dev->can.state = CAN_STATE_STOPPED;
+ dev->can.clock.freq = bt_const->fclk_can;
+ dev->can.bittiming_const = &dev->bt_const;
+ dev->can.do_set_bittiming = gs_usb_set_bittiming;
+ dev->can.do_set_mode = gs_usb_set_mode;
+
+ dev->can.ctrlmode_supported = 0;
+
+ if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
+
+ if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
+
+ if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
+
+ if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT)
+ dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
+
+ kfree(bt_const);
+
+ SET_NETDEV_DEV(netdev, &intf->dev);
+
+ rc = register_candev(dev->netdev);
+ if (rc) {
+ free_candev(dev->netdev);
+ dev_err(&intf->dev, "Couldn't register candev\n");
+ return rc;
+ }
+
+ *out = dev;
+ return 0;
+}
+
+static void gs_destroy_candev(struct gs_can *dev)
+{
+ unregister_candev(dev->netdev);
+ free_candev(dev->netdev);
+ usb_kill_anchored_urbs(&dev->tx_submitted);
+}
+
+static int gs_usb_probe(struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ struct gs_usb *dev;
+ int rc = -ENOMEM;
+ unsigned int icount, i;
+ struct gs_host_config *hconf;
+ struct gs_device_config *dconf;
+
+ hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
+ if (!hconf)
+ return -ENOMEM;
+
+ hconf->byte_order = 0x0000beef;
+
+
+ /* send host config */
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_sndctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_HOST_FORMAT,
+ USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ 1,
+ intf->altsetting[0].desc.bInterfaceNumber,
+ hconf,
+ sizeof(*hconf),
+ 1000);
+
+ kfree(hconf);
+
+ if (rc < 0) {
+ dev_err(&intf->dev, "Couldn't send data format: %d\n",
+ rc);
+ return rc;
+ }
+
+
+ dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
+ if (!dconf)
+ return -ENOMEM;
+
+ /* read device config */
+ rc = usb_control_msg(interface_to_usbdev(intf),
+ usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
+ GS_USB_BREQ_DEVICE_CONFIG,
+ USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
+ 1,
+ intf->altsetting[0].desc.bInterfaceNumber,
+ dconf,
+ sizeof(*dconf),
+ 1000);
+ if (rc < 0) {
+ dev_err(&intf->dev, "Couldn't recieve device config: %d\n",
+ rc);
+
+ kfree(dconf);
+
+ return rc;
+ }
+
+ icount = dconf->icount+1;
+
+ kfree(dconf);
+
+ dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
+
+ if (icount > MAX_INTF) {
+ dev_err(&intf->dev,
+ "Driver cannot handle more that %d Can interfaces\n",
+ MAX_INTF);
+ return -EINVAL;
+ }
+
+ dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+ init_usb_anchor(&dev->rx_submitted);
+
+ atomic_set(&dev->active_channels, 0);
+
+ usb_set_intfdata(intf, dev);
+
+ for (i = 0; i < icount; i++) {
+ rc = gs_make_candev(&dev->canch[i], i, intf);
+ if (rc) {
+ /* on failure destroy previously created candevs */
+ icount = i;
+ for (i = 0; i < icount; i++) {
+ gs_destroy_candev(dev->canch[i]);
+ dev->canch[icount] = NULL;
+ }
+ return rc;
+ }
+ dev->canch[i]->parent = dev;
+ }
+
+ dev_info(&intf->dev, "Probe\n");
+ return 0;
+}
+
+static void gs_usb_disconnect(struct usb_interface *intf) +{
+ unsigned i;
+ struct gs_usb *dev = usb_get_intfdata(intf);
+ usb_set_intfdata(intf, NULL);
+
+ if (!dev) {
+ dev_err(&intf->dev, "Disconnect (nodata)\n");
+ return;
+ }
+
+ for (i = 0; i < MAX_INTF; i++) {
+ struct gs_can *can = dev->canch[i];
+
+ if (!can)
+ continue;
+
+ gs_destroy_candev(can);
+ }
+
+ usb_kill_anchored_urbs(&dev->rx_submitted);
+}
+
+MODULE_DEVICE_TABLE(usb, gs_usb_table);
+
+static struct usb_device_id gs_usb_table[] = {
+ {USB_DEVICE(USB_GSUSB_1_VENDOR_ID, USB_GSUSB_1_PRODUCT_ID)},
+ {} /* Terminating entry */
+};
+
+static struct usb_driver gs_usb_driver = {
+ .name = "gs_usb",
+ .probe = gs_usb_probe,
+ .disconnect = gs_usb_disconnect,
+ .id_table = gs_usb_table
+};
+
+static int __init gs_usb_init(void)
+{
+ return usb_register(&gs_usb_driver);
+}
+
+static void __exit gs_usb_exit(void)
+{
+ usb_deregister(&gs_usb_driver);
+}
+
+module_init(gs_usb_init);
+module_exit(gs_usb_exit);
+
+MODULE_AUTHOR("Maximilian Schneider <[email protected]>"); +MODULE_DESCRIPTION(
+"Socket CAN device driver for Geschwister Schneider Technologie-, " +"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces."); +MODULE_LICENSE("GPL v2");
+
Signature · 1 line
1.7.10.4
Quoted 3 lines · Max S.
Any progress on reviewing the
GS_USBdriver code?I have added asynchronous state update. Here is the latest patch.
Great, actually I was waiting for support of the asynchronous state changes... Will have a closer look later this week.
Wolfgang,
Hi Max,
Quoted 9 lines · Max S.
Any progress on reviewing the
GS_USBdriver code?I have added asynchronous state update. Here is the latest patch.
Quoted 1 line
From
346f223bb3a8f4435f93cdfbc5e06db2cbf7179dMon Sep 17 00:00:00 2001From: Maximilian Schneider <[email protected]>
Date: Sun, 3 Nov 2013 16:57:06 +0000
Subject: [PATCH] Added support for theGS_USBCAN devices.
Please add some more lines here about the driver. Also, the patch below is line wrapped. Make sure that your mail client does not mangle white space.
Quoted 96 lines
Signed-off-by Maximilian Schneider <[email protected]>--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1037 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1046 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.cdrivers/net/can/usb/Kconfig +8
-0@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenudrivers/net/can/usb/Makefile +1
-0@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUGdrivers/net/can/usb/gs_usb.c new file +8
-0@@ -0,0 +1,1037 @@ +/* CAN driver for Geschwister Schneider USB/CAN devices. +* +* Copyright (C) 2013 Geschwister Schneider Technologie-, +* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). +* +* Many thanks to all socketcan devs! +* +* 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.
+ */drivers/net/can/usb/gs_usb.c new file +36
-0+ +#include <linux/init.h> +#include <linux/signal.h> +#include <linux/module.h> +#include <linux/netdevice.h> +#include <linux/usb.h> + +#include <linux/can.h> +#include <linux/can/dev.h> +#include <linux/can/error.h> + +/* Device specific constants */ +#define USB_GSUSB_1_VENDOR_ID 0x1d50 +#define USB_GSUSB_1_PRODUCT_ID 0x606f + +#define GSUSB_ENDPOINT_IN 1 +#define GSUSB_ENDPOINT_OUT 2 + +/* Device specific constants */ +enum gs_usb_breq { +GS_USB_BREQ_HOST_FORMAT =0 , +GS_USB_BREQ_BITTIMING , +GS_USB_BREQ_MODE , +GS_USB_BREQ_BERR , +GS_USB_BREQ_BT_CONST , +GS_USB_BREQ_DEVICE_CONFIG +}; + +enum gs_can_mode { +GS_CAN_MODE_RESET =0 , +GS_CAN_MODE_START +}; + +enum gs_can_state { +GS_CAN_STATE_ERROR_ACTIVE =0 , +GS_CAN_STATE_ERROR_WARNING ,
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+GS_CAN_STATE_ERROR_PASSIVE , +GS_CAN_STATE_BUS_OFF , +GS_CAN_STATE_STOPPED , + GS_CAN_STATE_SLEEPING +}; + +/* data types passed between host and device */ +struct __packed gs_host_config { +u32 byte_order; +};
It's more common to add __packed after the closing bracket "}".
Quoted 66 lines
drivers/net/can/usb/gs_usb.c new file +66
-0+ +struct __packed gs_device_config { +u8 reserved1; +u8 reserved2; +u8 reserved3; +u8 icount; +u32 sw_version; +u32 hw_version; +}; + +#define GS_CAN_MODE_NORMAL 0 +#define GS_CAN_MODE_LISTEN_ONLY (1<<0) +#define GS_CAN_MODE_LOOP_BACK (1<<1) +#define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_MODE_ONE_SHOT (1<<3) + +struct __packed gs_device_mode { +u32 mode; +u32 flags; +}; + +struct __packed gs_device_state { +u32 state; +u32 rxerr; +u32 txerr; +}; + +struct __packed gs_device_bittiming { +u32 prop_seg; +u32 phase_seg1; +u32 phase_seg2; +u32 sjw; +u32 brp; +}; + +#define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) +#define GS_CAN_FEATURE_LOOP_BACK (1<<1) +#define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_FEATURE_ONE_SHOT (1<<3) + +struct __packed gs_device_bt_const { +u32 feature; +u32 fclk_can; +u32 tseg1_min; +u32 tseg1_max; +u32 tseg2_min; +u32 tseg2_max; +u32 sjw_max; +u32 brp_min; +u32 brp_max; +u32 brp_inc; +}; + +#define GS_CAN_FLAG_OVERFLOW 1 + +struct __packed gs_host_frame { +u32 echo_id; +u32 can_id; + +u8 can_dlc; +u8 channel; +u8 flags; +u8 reserved; + +u8 data[8 ]; +};
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +/* Special address description flags for the CAN_ID */ +#define GS_EFF_FLAG 0x80000000U/* EFF/SFF is set in the MSB */ +#define GS_RTR_FLAG 0x40000000U/* remote transmission request */ +#define GS_ERR_FLAG 0x20000000U/* error frame */ + +/* Valid bits in CAN ID for frame formats */ +#define GS_SFF_MASK 0x000007FFU/* standard frame format (SFF) */ +#define GS_EFF_MASK 0x1FFFFFFFU/* extended frame format (EFF) */ +#define GS_ERR_MASK 0x1FFFFFFFU/* omit EFF, RTR, ERR flags */
You redefine the CAN flags and mask here but in many places you silentely assume that they are identical, e.g. via:
cf->can_id = hf->can_id;
Not sure how to handle this transprently. I think these defines will never change. Either use the CAN flags and mask directly (like you already do for the error frames) or properly remap/copy all flags and values. Other opinions?
drivers/net/can/usb/gs_usb.c
Quoted 9 lines
+/* Only send a max of
MAX_TX_URBSframes per channel to the device at a time. */drivers/net/can/usb/gs_usb.c new file +7
-0+#define MAX_TX_URBS 10 +/* Only launch a max of MAX_RX_URBS usb requests at a time. */ +#define MAX_RX_URBS 30 +/* Maximum number of interfaces the driver supports per device. +* Current hardware only supports 2 interfaces. The future may vary. +*/ +#define MAX_INTF 2
Please use a prefix here as well.
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +11
-0+ +struct gs_tx_context { +struct gs_can *dev; +unsigned int echo_id; +}; + +struct gs_can { +struct can_priv can;/* must be the first member */ + +struct gs_usb *parent; +
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+struct net_device *netdev; +struct usb_device *udev; +struct usb_interface *iface; + +struct can_bittiming_const bt_const; +unsigned int channel;/* channel number */ + +/* This lock prevents a race condition +* between xmit and recieve. +*/
Does fit on one line?
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+spinlock_t tx_ctx_lock; +struct gs_tx_context tx_context[MAX_TX_URBS ]; + +struct usb_anchor tx_submitted; +atomic_t active_tx_urbs;
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+}; + +/* usb interface struct */ +struct gs_usb { +struct gs_can *canch[MAX_INTF ]; + +struct usb_anchor rx_submitted; + +atomic_t active_channels; +};
Looks better without empty lines.
drivers/net/can/usb/gs_usb.c
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ +/* 'allocate' a tx context. +* returns a valid tx context or NULL if there is no space. +*/ +static struct gs_tx_context *gs_alloc_tx_context (struct gs_can *dev) +{ +int i =0 ; +
Remove empty line please.
Quoted 15 lines
drivers/net/can/usb/gs_usb.c new file +15
-0+unsigned long flags; + +spin_lock_irqsave (&dev->tx_ctx_lock, flags); + +for (; i <MAX_TX_URBS ; i++) { +if (dev->tx_context[i].echo_id ==MAX_TX_URBS ) { + dev->tx_context[i].echo_id = i; +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return &dev->tx_context[i]; + } + } + +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return NULL ; +}
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +/* releases a tx context +*/ +static void gs_free_tx_context (struct gs_tx_context *txc) +{ +unsigned long flags; + +spin_lock_irqsave (&txc->dev->tx_ctx_lock, flags); + txc->echo_id =MAX_TX_URBS ; +spin_unlock_irqrestore (&txc->dev->tx_ctx_lock, flags);
Such assignments re done with one instruction. No need to make it event more atomic.
drivers/net/can/usb/gs_usb.c
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+} + +/* Get a tx context by id. +*/ +static struct gs_tx_context *gs_get_tx_context ( +struct gs_can *dev, +unsigned int id)
Please use a common style, also for the continuation lines.
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+{ +unsigned long flags; + +spin_lock_irqsave (&dev->tx_ctx_lock, flags); +if (id <MAX_TX_URBS ) { +if (dev->tx_context[id].echo_id == id) { +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return &dev->tx_context[id]; + } + }
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return NULL ; +} + +/* Get a tx contexts id. +*/ +static unsigned int gs_tx_context_id (struct gs_tx_context *txc) +{ +unsigned int id; +
Remove empty line please.
drivers/net/can/usb/gs_usb.c
Quoted 6 lines
drivers/net/can/usb/gs_usb.c new file +6
-0+unsigned long flags; + +spin_lock_irqsave (&txc->dev->tx_ctx_lock, flags); + id = txc->echo_id; +spin_unlock_irqrestore (&txc->dev->tx_ctx_lock, flags); +return id;
See my comments above.
Quoted 21 lines
drivers/net/can/usb/gs_usb.c new file +21
-0+} + +static int gs_cmd_reset (struct gs_usb *gsusb,struct gs_can *gsdev) +{ +struct gs_device_mode *dm; +struct usb_interface *intf = gsdev->iface; +int rc; + + dm =kzalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ; + + dm->mode =GS_CAN_MODE_RESET ; + + rc = usb_control_msg(interface_to_usbdev(intf), + usb_sndctrlpipe(interface_to_usbdev(intf), 0), + GS_USB_BREQ_MODE, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + gsdev->channel, + 0, + dm,
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+sizeof (*dm), + 1000); + +return rc; +} + +static void gs_update_state (struct gs_can *dev,struct can_frame *cf) +{ +if (cf->can_id &CAN_ERR_BUSOFF ) { + dev->can.state =CAN_STATE_BUS_OFF ;
In case of bus-off. What does the hardware do? How does it recover? Is recovery triggered by software possible? Is it possible to support manual recovery?
drivers/net/can/usb/gs_usb.c
Quoted 2 lines
drivers/net/can/usb/gs_usb.c new file +2
-0+ }else if (cf->can_id &CAN_ERR_CRTL ) { + dev->can.state =CAN_STATE_ERROR_ACTIVE ;
This means back to error active, right?
drivers/net/can/usb/gs_usb.c
Quoted 2 lines
drivers/net/can/usb/gs_usb.c new file +2
-0+if ((cf->data[1 ] &CAN_ERR_CRTL_TX_WARNING ) || + (cf->data[1 ] &CAN_ERR_CRTL_RX_WARNING ))
Here you assume that the mask and flags do match the CAN_* definitions. See my comment above.
drivers/net/can/usb/gs_usb.c
Quoted 4 lines
drivers/net/can/usb/gs_usb.c new file +4
-0+ dev->can.state =CAN_STATE_ERROR_WARNING ; + +if ((cf->data[1 ] &CAN_ERR_CRTL_TX_PASSIVE ) || + (cf->data[1 ]&CAN_ERR_CRTL_RX_PASSIVE ))
s/&/ & /
drivers/net/can/usb/gs_usb.c
Quoted 2 lines
drivers/net/can/usb/gs_usb.c new file +2
-0+ dev->can.state =CAN_STATE_ERROR_PASSIVE ; + }
Also please update the stats accordingly:
can_stats.bus_off++; can_stats.error_warning++ can_stats.error_passive++
Apart from that, the state handling is rather basic but totaly sufficient. Would be nice if you could show the state handling and bus-off recovery by reporting the output of "$ candump -td -e any,0:0,#FFFFFFFF" while sending messages to the device ...
1. without cable connected (entering error passive)
2. with short-circuited CAN high and low (entering bus-off)
drivers/net/can/usb/gs_usb.c
Quoted 3 lines
drivers/net/can/usb/gs_usb.c new file +3
-0+} + +/**************************** USB CALLBACKS **************************/
Please respect the linux coding style, also for comments.
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+static void gs_usb_recieve_bulk_callback (struct urb *urb) +{ +struct gs_usb *usbcan = urb->context; +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct net_device_stats *stats; +struct gs_host_frame *hf = urb->transfer_buffer; +struct gs_tx_context *txc; +
Remove empty line please.
struct can_frame *cf;
Quoted 25 lines
drivers/net/can/usb/gs_usb.c new file +25
-0+struct sk_buff *skb; + +BUG_ON (!usbcan); + +switch (urb->status) { +case 0 :/* success */ +break ; +case -ENOENT : +case -ESHUTDOWN : +return ; +default : +/* do not resubmit aborted urbs. eg: when device goes down */ +return ; + } + +/* device reports out of range channel id */ +if (hf->channel >=MAX_INTF ) + goto resubmit_urb; + +/* device reports bad channel id */ + dev = usbcan->canch[hf->channel]; + + netdev = dev->netdev; + +if (!netif_device_present (netdev))
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+return ; + + stats = &dev->netdev->stats; + +if (hf->echo_id ==-1 ) {/* normal rx */ + skb =alloc_can_skb (dev->netdev, &cf); +if (skb ==NULL ) +return ; + + cf->can_id = hf->can_id;
What about endianess? I think the driver should care.
drivers/net/can/usb/gs_usb.c
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+/* is it necessary to use get_can_dlc? */ + cf->can_dlc =get_can_dlc (hf->can_dlc); +memcpy (cf->data, hf->data,8 ); + +/* ERROR frames tell us information about the controler */
s/controler/controller/
drivers/net/can/usb/gs_usb.c
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +11
-0+if (hf->can_id &GS_ERR_FLAG ) +gs_update_state (dev, cf); + +netif_rx (skb); + + netdev->stats.rx_packets++; + netdev->stats.rx_bytes += hf->can_dlc; + }else {/* echo_id == hf->echo_id */ +if (hf->echo_id >=MAX_TX_URBS ) { + netdev_err(netdev, + "Device sent an out of range echo id:%d\n",
s/:/ /
Quoted 4 lines
drivers/net/can/usb/gs_usb.c new file +4
-0+ hf->echo_id); + goto resubmit_urb; + } +
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ netdev->stats.tx_packets++; + netdev->stats.tx_bytes += hf->can_dlc; + + txc =gs_get_tx_context (dev, hf->echo_id); + +/* bad devices send bad echo_ids. */ +if (!txc) { +netdev_err (netdev, +"Device sent a bad echo id:%d\n" , + hf->echo_id);
Ditto
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+ goto resubmit_urb; + } + +can_get_echo_skb (netdev,gs_tx_context_id (txc)); + +gs_free_tx_context (txc); + +netif_wake_queue (netdev); + } + +if (hf->flags &GS_CAN_FLAG_OVERFLOW ) { + skb =alloc_can_err_skb (netdev, &cf);
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+if (skb ==NULL ) + goto resubmit_urb; + + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] =CAN_ERR_CRTL_RX_OVERFLOW ; + stats->rx_over_errors++; + stats->rx_errors++; +netif_rx (skb); + } +
Only one empty line.
Quoted 52 lines
drivers/net/can/usb/gs_usb.c new file +51
-0+ +resubmit_urb: +/* note: no urb refill... works for me */ + + rc =usb_submit_urb (urb,GFP_ATOMIC ); + +/* USB failure take down all interfaces */ +if (rc == -ENODEV ) { +for (rc =0 ; rc <MAX_INTF ; rc++) { +if (usbcan->canch[rc]) +netif_device_detach (usbcan->canch[rc]->netdev); + } + } +} + +static int gs_usb_set_bittiming (struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct can_bittiming *bt = &dev->can.bittiming; +struct usb_interface *intf = dev->iface; +int rc; +struct gs_device_bittiming *dbt; + + dbt =kmalloc (sizeof (*dbt),GFP_KERNEL ); +if (!dbt) +return -ENOMEM ; + + dbt->prop_seg = bt->prop_seg; + dbt->phase_seg1 = bt->phase_seg1; + dbt->phase_seg2 = bt->phase_seg2; + dbt->sjw = bt->sjw; + dbt->brp = bt->brp; + + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_BITTIMING , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dbt, +sizeof (*dbt), +1000 ); +if (rc <0 ) +dev_err (netdev->dev.parent,"Couldn't set bittimings %d" , rc); + +kfree (dbt); + +return rc; +} + +static int gs_usb_set_mode(struct net_device *netdev, enum can_modemode)
drivers/net/can/usb/gs_usb.c
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+{ +if (mode ==CAN_MODE_START ) { +netif_wake_queue (netdev); +return 0 ; + }
This is usually used for bus off recovery by the software. If it's not possible, you should drop this callback.
drivers/net/can/usb/gs_usb.c
Quoted 13 lines
drivers/net/can/usb/gs_usb.c new file +13
-0+ +return -EOPNOTSUPP ; +} + +static void gs_usb_xmit_callback (struct urb *urb) +{ +struct gs_tx_context *txc = urb->context; +struct gs_can *dev; +struct net_device *netdev; + + dev = txc->dev; + + netdev = dev->netdev;
You can do the assignment directly in the function header, like for gs_tx_context.
Quoted 23 lines
drivers/net/can/usb/gs_usb.c new file +23
-0+ +if (urb->status) { +netdev_info (netdev,"%d] xmit err %d\n" , + dev->channel, txc->echo_id); + } + +usb_free_coherent (urb->dev, + urb->transfer_buffer_length, + urb->transfer_buffer, + urb->transfer_dma); + +atomic_dec (&dev->active_tx_urbs); + +if (!netif_device_present (netdev)) +return ; + +if (netif_queue_stopped (netdev)) +netif_wake_queue (netdev); +} + +static netdev_tx_t gs_can_start_xmit (struct sk_buff *skb, +struct net_device *netdev) +{
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+struct gs_can *dev =netdev_priv (netdev); +struct net_device_stats *stats = &dev->netdev->stats; +struct urb *urb; +struct gs_host_frame *hf; +struct can_frame *cf; +int rc; +unsigned int idx; +struct gs_tx_context *txc; + +if (dev->can.ctrlmode&CAN_CTRLMODE_LISTENONLY )
s/&/ & /
Quoted 15 lines
drivers/net/can/usb/gs_usb.c new file +15
-0+return NETDEV_TX_BUSY ; + +if (can_dropped_invalid_skb (netdev, skb)) +return NETDEV_TX_OK ; + +/* find an empty context to keep track of transmission */ + txc =gs_alloc_tx_context (dev); +if (!txc) +return NETDEV_TX_BUSY ; + +/* create a URB, and a buffer for it */ + urb =usb_alloc_urb (0 ,GFP_ATOMIC ); +if (!urb) { +netdev_err (netdev,"No memory left for URBs\n" ); + goto nomem_urb;
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ } + + hf =usb_alloc_coherent (dev->udev,sizeof (*hf),GFP_ATOMIC , + &urb->transfer_dma); +if (!hf) { +netdev_err (netdev,"No memory left for USB buffer\n" ); + goto nomem_hf; + } + + cf = (struct can_frame *)skb->data;
Could be moved down ...
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ + idx =gs_tx_context_id (txc); + +if (idx >=MAX_TX_URBS ) { +netdev_err (netdev,"Invalid tx context %d\n" , idx); + goto badidx; + } + + hf->echo_id = idx; + hf->channel = dev->channel;
... here.
drivers/net/can/usb/gs_usb.c
Quoted 1 line
drivers/net/can/usb/gs_usb.c new file +1
-0+ hf->can_id = cf->can_id;
Endianess?
Quoted 23 lines
drivers/net/can/usb/gs_usb.c new file +23
-0+ hf->can_dlc = cf->can_dlc; +memcpy (hf->data, cf->data,8 ); + +usb_fill_bulk_urb (urb, dev->udev, +usb_sndbulkpipe (dev->udev,GSUSB_ENDPOINT_OUT ), + hf, +sizeof (*hf), + gs_usb_xmit_callback, + txc); + + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; +usb_anchor_urb (urb, &dev->tx_submitted); + +can_put_echo_skb (skb, netdev, idx); + +atomic_inc (&dev->active_tx_urbs); + + rc =usb_submit_urb (urb,GFP_ATOMIC ); +if (unlikely (rc)) {/* usb send failed */ +atomic_dec (&dev->active_tx_urbs); + +netdev_err (netdev,"usb_submit failed %d\n" , rc); +
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+can_free_echo_skb (netdev,gs_tx_context_id (txc)); +gs_free_tx_context (txc); + +usb_unanchor_urb (urb); +usb_free_coherent (dev->udev, +sizeof (*hf), + hf, + urb->transfer_dma); + +
Only one empty line please.
drivers/net/can/usb/gs_usb.c
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+if (rc == -ENODEV ) { +netif_device_detach (netdev); + }else { +netdev_err (netdev,"Failed tx_urb %d\n" , rc); +
Looks better without empty line.
Quoted 58 lines
drivers/net/can/usb/gs_usb.c new file +58
-0+ stats->tx_dropped++; + } + }else { + netdev->trans_start = jiffies; + +/* Slow down tx path */ +if (atomic_read (&dev->active_tx_urbs) >=MAX_TX_URBS ) +netif_stop_queue (netdev); + } + +/* let usb core take care of this urb */ +usb_free_urb (urb); + +return NETDEV_TX_OK ; + +badidx: +usb_free_coherent (dev->udev, +sizeof (*hf), + hf, + urb->transfer_dma); +nomem_hf: +usb_free_urb (urb); + +nomem_urb: +gs_free_tx_context (txc); +dev_kfree_skb (skb); + stats->tx_dropped++; +return NETDEV_TX_OK ; +} + +static int gs_can_open (struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct gs_usb *parent = dev->parent; +int rc, i; +struct gs_device_mode *dm; +u32 ctrlmode; + +/* common open */ + rc =open_candev (netdev); +if (rc) +return rc; + +if (atomic_add_return (1 , &parent->active_channels) ==1 ) { +for (i =0 ; i <MAX_RX_URBS ; i++) { +struct urb *urb =NULL ; +u8 *buf =NULL ; + +/* alloc rx urb */ + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) { +netdev_err (netdev, +"No memory left for URBs\n" ); +return -ENOMEM ; + } + +/* alloc rx buffer */ + buf = usb_alloc_coherent(dev->udev,
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+sizeof (struct gs_host_frame ), +GFP_KERNEL , + &urb->transfer_dma); +if (!buf) { +netdev_err (netdev, +"No memory left for USB buffer\n" ); +usb_free_urb (urb); +return -ENOMEM ; + } +
Only one empty line.
Quoted 36 lines
drivers/net/can/usb/gs_usb.c new file +36
-0+ +/* fill, anchor, and submit rx urb */ +usb_fill_bulk_urb (urb, + dev->udev, +usb_rcvbulkpipe (dev->udev, +GSUSB_ENDPOINT_IN ), + buf, +sizeof (struct gs_host_frame ), + gs_usb_recieve_bulk_callback, + parent + ); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +usb_anchor_urb (urb, &parent->rx_submitted); + + rc =usb_submit_urb (urb,GFP_KERNEL ); +if (rc) { +if (rc == -ENODEV ) +netif_device_detach (dev->netdev); + +usb_unanchor_urb (urb); +break ; + } + +/* Drop reference, +* USB core will take care of freeing it +*/ +usb_free_urb (urb); + } + } + + dm =kmalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ; + +/* flags */
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ ctrlmode = dev->can.ctrlmode; + dm->flags =0 ; + +if (ctrlmode &CAN_CTRLMODE_LOOPBACK ) + dm->flags |=GS_CAN_MODE_LOOP_BACK ; + +else if (ctrlmode &CAN_CTRLMODE_LISTENONLY ) + dm->flags |=GS_CAN_MODE_LISTEN_ONLY ; + +
Ditto
drivers/net/can/usb/gs_usb.c
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+/* Controller is not allowed to retry TX +* this mode is unavailable on atmels uc3c hardware +*/ +if (ctrlmode &CAN_CTRLMODE_ONE_SHOT ) + dm->flags |=GS_CAN_MODE_ONE_SHOT ;
Have you tested oneshot mode.
Quoted 7 lines
drivers/net/can/usb/gs_usb.c new file +7
-0+if (ctrlmode &CAN_CTRLMODE_3_SAMPLES ) + dm->flags |=GS_CAN_MODE_TRIPLE_SAMPLE ; + +/* finally start device */ + dm->mode =GS_CAN_MODE_START ; + rc = usb_control_msg(interface_to_usbdev(dev->iface), + usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+GS_USB_BREQ_MODE , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dm, +sizeof (*dm), + 1000); + +if (rc <0 ) { +netdev_err (netdev,"Couldn't start device %d\n" , rc);
s/%d/ (err=%d)/ ?
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +11
-0+kfree (dm); +return rc; + } + +/* check mode... */ +if (!dm) +return -ENOMEM ; + + rc = usb_control_msg(interface_to_usbdev(dev->iface), + usb_rcvctrlpipe(interface_to_usbdev(dev->iface), 0), + GS_USB_BREQ_MODE,
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dm, +sizeof (*dm), + 1000); + +if (rc <0 ) { +netdev_err (netdev,"Couldn't get device mode %d\n" , + rc);
Ditto.
drivers/net/can/usb/gs_usb.c
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +11
-0+kfree (dm); +return rc; + } + +kfree (dm); + + dev->can.state =CAN_STATE_ERROR_ACTIVE ; + +netif_start_queue (netdev); + +netdev_info (netdev,"CAN open\n" );
Please remove.
Quoted 9 lines
drivers/net/can/usb/gs_usb.c new file +9
-0+return 0 ; +} + +static int gs_can_close (struct net_device *netdev) +{ +int rc; +struct gs_can *dev =netdev_priv (netdev); +struct gs_usb *parent = dev->parent; +
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+netif_stop_queue (netdev); + +/* Stop polling */ +if (atomic_dec_and_test (&parent->active_channels)) +usb_kill_anchored_urbs (&parent->rx_submitted); + + rc =gs_cmd_reset (parent, dev); + +if (rc <0 ) +netdev_warn (netdev,"Couldn't shutdown device %d" , rc);
device %d is confusing. See above.
Quoted 30 lines
drivers/net/can/usb/gs_usb.c new file +30
-0+ +close_candev (netdev); + +return 0 ; +} + +/******************************** USB ********************************/ + +static const struct net_device_ops gs_usb_netdev_ops = { + .ndo_open = gs_can_open, + .ndo_stop = gs_can_close, + .ndo_start_xmit = gs_can_start_xmit, +}; + +static int gs_make_candev (struct gs_can **out,unsigned int channel, +struct usb_interface *intf) +{ +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct gs_device_bt_const *bt_const; + + bt_const =kmalloc (sizeof (*bt_const),GFP_KERNEL ); +if (!bt_const) +return -ENOMEM ; + +/* fetch bit timing constants */ + rc = usb_control_msg(interface_to_usbdev(intf), + usb_rcvctrlpipe(interface_to_usbdev(intf), 0), + GS_USB_BREQ_BT_CONST,
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + channel, +0 , + bt_const, +sizeof (*bt_const), + 1000); + +if (rc <0 ) { + dev_err(&intf->dev, + "Couldn't get bit timing const for channel %d\n",
Ditto
Quoted 66 lines
drivers/net/can/usb/gs_usb.c new file +66
-0+ rc); +kfree (bt_const); +return rc; + } + +/* create netdev */ + netdev =alloc_candev (sizeof (struct gs_can ),MAX_TX_URBS ); +if (!netdev) { +dev_err (&intf->dev,"Couldn't alloc candev\n" ); +kfree (bt_const); +return -ENOMEM ; + } + + dev =netdev_priv (netdev); + + netdev->netdev_ops = &gs_usb_netdev_ops; + + netdev->flags |=IFF_ECHO ;/* we support full roundtrip echo */ + +/* dev settup */ +strcpy (dev->bt_const.name,"gs_usb" ); + dev->bt_const.tseg1_min = bt_const->tseg1_min; + dev->bt_const.tseg1_max = bt_const->tseg1_max; + dev->bt_const.tseg2_min = bt_const->tseg2_min; + dev->bt_const.tseg2_max = bt_const->tseg2_max; + dev->bt_const.sjw_max = bt_const->sjw_max; + dev->bt_const.brp_min = bt_const->brp_min; + dev->bt_const.brp_max = bt_const->brp_max; + dev->bt_const.brp_inc = bt_const->brp_inc; + + dev->udev =interface_to_usbdev (intf); + dev->iface = intf; + dev->netdev = netdev; + dev->channel = channel; + +init_usb_anchor (&dev->tx_submitted); +atomic_set (&dev->active_tx_urbs,0 ); +spin_lock_init (&dev->tx_ctx_lock); +for (rc =0 ; rc <MAX_TX_URBS ; rc++) { + dev->tx_context[rc].dev = dev; + dev->tx_context[rc].echo_id =MAX_TX_URBS ; + } + +/* can settup */ + dev->can.state =CAN_STATE_STOPPED ; + dev->can.clock.freq = bt_const->fclk_can; + dev->can.bittiming_const = &dev->bt_const; + dev->can.do_set_bittiming = gs_usb_set_bittiming; + dev->can.do_set_mode = gs_usb_set_mode; + + dev->can.ctrlmode_supported =0 ; + +if (bt_const->feature &GS_CAN_FEATURE_LISTEN_ONLY ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LISTENONLY ; + +if (bt_const->feature &GS_CAN_FEATURE_LOOP_BACK ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LOOPBACK ; + +if (bt_const->feature &GS_CAN_FEATURE_TRIPLE_SAMPLE ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_3_SAMPLES ; + +if (bt_const->feature &GS_CAN_FEATURE_ONE_SHOT ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_ONE_SHOT ; + +kfree (bt_const); +
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+SET_NETDEV_DEV (netdev, &intf->dev); + + rc =register_candev (dev->netdev); +if (rc) { +free_candev (dev->netdev); +dev_err (&intf->dev,"Couldn't register candev\n" ); +return rc; + } + + *out = dev;
Why not returning "dev" directly. You can pack errors into the pointer with ERR_PTR(rc).
Quoted 15 lines
drivers/net/can/usb/gs_usb.c new file +15
-0+return 0 ; +} + +static void gs_destroy_candev (struct gs_can *dev) +{ +unregister_candev (dev->netdev); +free_candev (dev->netdev); +usb_kill_anchored_urbs (&dev->tx_submitted); +} + +static int gs_usb_probe (struct usb_interface *intf, +const struct usb_device_id *id) +{ +struct gs_usb *dev; +int rc = -ENOMEM ;
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+unsigned int icount, i; +struct gs_host_config *hconf; +struct gs_device_config *dconf; + + hconf =kmalloc (sizeof (*hconf),GFP_KERNEL ); +if (!hconf) +return -ENOMEM ; + + hconf->byte_order =0x0000beef ; +
Only one empty line please.
Quoted 43 lines
drivers/net/can/usb/gs_usb.c new file +43
-0+ +/* send host config */ + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_HOST_FORMAT , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , +1 , + intf->altsetting[0 ].desc.bInterfaceNumber, + hconf, +sizeof (*hconf), +1000 ); + +kfree (hconf); + +if (rc <0 ) { +dev_err (&intf->dev,"Couldn't send data format: %d\n" , + rc); +return rc; + } + + + dconf =kmalloc (sizeof (*dconf),GFP_KERNEL ); +if (!dconf) +return -ENOMEM ; + +/* read device config */ + rc =usb_control_msg (interface_to_usbdev (intf), +usb_rcvctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_DEVICE_CONFIG , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , +1 , + intf->altsetting[0 ].desc.bInterfaceNumber, + dconf, +sizeof (*dconf), +1000 ); +if (rc <0 ) { +dev_err (&intf->dev,"Couldn't recieve device config: %d\n" , + rc); + +kfree (dconf); + +return rc; + }
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ + icount = dconf->icount+1 ; + +kfree (dconf); + +dev_info (&intf->dev,"Configuring for %d interfaces\n" , icount); + +if (icount >MAX_INTF ) { + dev_err(&intf->dev, + "Driver cannot handle more that %d Can interfaces\n",
s/Can/CAN/ ?
Quoted 75 lines
drivers/net/can/usb/gs_usb.c new file +75
-0+ MAX_INTF); +return -EINVAL ; + } + + dev =kzalloc (sizeof (*dev),GFP_KERNEL ); +init_usb_anchor (&dev->rx_submitted); + +atomic_set (&dev->active_channels,0 ); + +usb_set_intfdata (intf, dev); + +for (i =0 ; i < icount; i++) { + rc =gs_make_candev (&dev->canch[i], i, intf); +if (rc) { +/* on failure destroy previously created candevs */ + icount = i; +for (i =0 ; i < icount; i++) { +gs_destroy_candev (dev->canch[i]); + dev->canch[icount] =NULL ; + } +return rc; + } + dev->canch[i]->parent = dev; + } + +dev_info (&intf->dev,"Probe\n" ); +return 0 ; +} + +static void gs_usb_disconnect (struct usb_interface *intf) +{ +unsigned i; +struct gs_usb *dev =usb_get_intfdata (intf); +usb_set_intfdata (intf,NULL ); + +if (!dev) { +dev_err (&intf->dev,"Disconnect (nodata)\n" ); +return ; + } + +for (i =0 ; i <MAX_INTF ; i++) { +struct gs_can *can = dev->canch[i]; + +if (!can) +continue ; + +gs_destroy_candev (can); + } + +usb_kill_anchored_urbs (&dev->rx_submitted); +} + +MODULE_DEVICE_TABLE (usb, gs_usb_table); + +static struct usb_device_id gs_usb_table[] = { + {USB_DEVICE (USB_GSUSB_1_VENDOR_ID ,USB_GSUSB_1_PRODUCT_ID )}, + {}/* Terminating entry */ +}; + +static struct usb_driver gs_usb_driver = { + .name ="gs_usb" , + .probe = gs_usb_probe, + .disconnect = gs_usb_disconnect, + .id_table = gs_usb_table +}; + +static int __init gs_usb_init (void ) +{ +return usb_register (&gs_usb_driver); +} + +static void __exit gs_usb_exit(void) +{ +usb_deregister (&gs_usb_driver); +}
drivers/net/can/usb/gs_usb.c
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +module_init (gs_usb_init); +module_exit (gs_usb_exit); + +MODULE_AUTHOR ("Maximilian Schneider <[email protected]>" ); +MODULE_DESCRIPTION ( +"Socket CAN device driver for Geschwister Schneider Technologie-, " +"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces." ); +MODULE_LICENSE ("GPL v2" ); +
Thanks for your patience.
Wolfgang.
Hi Wolfgang,
Quoted 9 lines · Wolfgang Grandegger
You redefine the CAN flags and mask here but in many places you silentely assume that they are identical, e.g. via:
cf->can_id = hf->can_id;Not sure how to handle this transprently. I think these defines will never change. Either use the CAN flags and mask directly (like you already do for the error frames) or properly remap/copy all flags and values. Other opinions?
I have removed the GS_CAN_* flags. They are the same as those in the Linux can headers. The idea was not to have to repackage everything. But instead get by with some copies. like cf->can_id = hf->can_id; Byte order is handled by the device.
Quoted 3 lines
In case of bus-off. What does the hardware do? How does it recover? Is recovery triggered by software possible? Is it possible to support manual recovery?
Manual recovery is not supported. The node can leave bus off state if it detects 128 occurrences of 11 consecutive recessive bits on the bus. Maybe I can trick the controller out of bus off by doing a software reset. That's probably not going to be a clean solution though.
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ } else if (cf->can_id & CAN_ERR_CRTL) { + dev->can.state = CAN_STATE_ERROR_ACTIVE;This means back to error active, right?
Yes, if none of the following conditions apply.
I guess i could use an if else if else instead of conditionally overwriting the state...
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +6
-0+ if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || + (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) + dev->can.state = CAN_STATE_ERROR_WARNING; + + if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || + (cf->data[1]&CAN_ERR_CRTL_RX_PASSIVE))s/&/ & /
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ dev->can.state = CAN_STATE_ERROR_PASSIVE; + }Also please update the stats accordingly:
These are simple counts for how often the conditions occurred? They count when the respective state is entered? When a node recovers from bus off does that count as entering error passive?
Quoted 5 lines
can_stats.bus_off++; can_stats.error_warning++ can_stats.error_passive++
Quoted 7 lines
Apart from that, the state handling is rather basic but totaly sufficient. Would be nice if you could show the state handling and bus-off recovery by reporting the output of "$ candump -td -e any,0:0,#FFFFFFFF" while sending messages to the device ...
1. without cable connected (entering error passive)
2. with short-circuited CAN high and low (entering bus-off)
$ sudo ip link set can0 up type can bitrate 250000 $ cansequence can0 -p $ candump -td -e can0,0:0,#FFFFFFFF 1>can.log
# CAN node transmitting normally
(000.000000) can0 002 [1] 00
...
(000.000006) can0 002 [1] 68
# CAN node is disconnected from bus
(000.040973) can0 20000004 [8] 00 20 00 00 00 00 80 00
ERRORFRAME
controller-problem{tx-error-passive}
error-counter-tx-rx{{128}{0}} (007.437011) can0 002 [1] 69
...
(000.000991) can0 002 [1] B3
# CAN node is reconnected to bus
(000.000008) can0 20000004 [8] 00 00 00 00 00 00 00 00
ERRORFRAME
controller-problem{} (000.000004) can0 002 [1] B4
...
(000.000003) can0 002 [1] D3
# CAN node is disconnected from bus
(000.062975) can0 20000004 [8] 00 20 00 00 00 00 80 00
ERRORFRAME
controller-problem{tx-error-passive}
error-counter-tx-rx{{128}{0}}
# short added between CAN-High to CAN-Low
(005.593023) can0 20000040 [8] 00 00 00 00 00 00 00 0E
ERRORFRAME
bus-off
error-counter-tx-rx{{0}{14}}
(000.260982) can0 20000100 [8] 00 00 00 00 00 00 E8 00
ERRORFRAME
restarted-after-bus-off
error-counter-tx-rx{{232}{0}}
(000.526024) can0 20000040 [8] 00 00 00 00 00 00 00 7D
ERRORFRAME
bus-off
error-counter-tx-rx{{0}{125}}
(001.834994) can0 20000100 [8] 00 00 00 00 00 00 F0 00
ERRORFRAME
restarted-after-bus-off
error-counter-tx-rx{{240}{0}}
(000.523992) can0 20000040 [8] 00 00 00 00 00 00 00 80
ERRORFRAME
bus-off
error-counter-tx-rx{{0}{128}}
(001.835013) can0 20000100 [8] 00 00 00 00 00 00 F8 00
ERRORFRAME
restarted-after-bus-off
error-counter-tx-rx{{248}{0}}
(000.524981) can0 20000004 [8] 00 00 00 00 00 00 28 00
ERRORFRAME
controller-problem{}
error-counter-tx-rx{{40}{0}}
(000.086006) can0 20000040 [8] 00 00 00 00 00 00 00 7C
ERRORFRAME
bus-off
error-counter-tx-rx{{0}{124}}
# The short between CAN-High and CAN-Low is removed
(000.525000) can0 20000100 [8] 00 00 00 00 00 00 80 00
ERRORFRAME
restarted-after-bus-off
error-counter-tx-rx{{128}{0}}
(002.702998) can0 002 [1] D4
...
(000.000003) can0 002 [1] ED
# CAN node is reconnected to bus
(000.000007) can0 20000004 [8] 00 08 00 00 00 00 6E 00
ERRORFRAME
controller-problem{tx-error-warning}
error-counter-tx-rx{{110}{0}}
(000.000003) can0 002 [1] EE
...
(000.000990) can0 002 [1] 60
(000.000008) can0 20000004 [8] 00 00 00 00 00 00 00 00
ERRORFRAME
controller-problem{}
(000.000004) can0 002 [1] 61
(000.000004) can0 002 [1] 62
...Thank you for your time,
Max Schneider.
Hi Max,
On Mon, 11 Nov 2013 02:10:35 +0000, "Max S." <[email protected]>
Quoted 28 lines · wrote:
Hi Wolfgang,
Quoted 10 lines · Wolfgang Grandegger
You redefine the CAN flags and mask here but in many places you
silentely assume that they are identical, e.g. via:
cf->can_id = hf->can_id;Quoted 7 lines
Not sure how to handle this transprently. I think these defines will
never change. Either use the CAN flags and mask directly (like you
already do for the error frames) or properly remap/copy all flags and
values. Other opinions?
Quoted 7 lines
I have removed the GS_CAN_* flags. They are the same as those in the
Linux can headers. The idea was not to have to repackage everything. But
instead get by with some copies. like
cf->can_id=hf->can_id; Byteorder is handled by the device.
OK, Please add a few lines of documentation explaining that. Would be nice
to have a global version number for the current definitions and
declarations.
Quoted 8 lines
Quoted 5 lines
In case of bus-off. What does the hardware do? How does it recover? Is
recovery triggered by software possible? Is it possible to support
manual recovery?
Quoted 7 lines
Manual recovery is not supported. The node can leave bus off state if
it detects 128 occurrences of 11 consecutive recessive bits on the bus.
Maybe I can trick the controller out of bus off by doing a software
reset. That's probably not going to be a clean solution though.
Would be nice if manual recovery is supported but it's not a requirement.
Maybe you could make the delay before restarting bus-off configurable
using the "restart-ms" argument.
For further information have a look to:
http://lxr.free-electrons.com/source/Documentation/networking/can.txt#L891
Quoted 44 lines
Quoted 11 lines
Review
Quoted 5 lines
(quoted diff) +2
-0+ } else if (cf->can_id & CAN_ERR_CRTL) { + dev->can.state = CAN_STATE_ERROR_ACTIVE;This means back to error active, right?
Yes, if none of the following conditions apply.
I guess i could use an if else if else instead of conditionally
overwriting the state...
Quoted 25 lines
Review
Quoted 13 lines
(quoted diff) +6
-0+ if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || + (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) + dev->can.state = CAN_STATE_ERROR_WARNING; + + if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || + (cf->data[1]&CAN_ERR_CRTL_RX_PASSIVE))s/&/ & /
Quoted 3 lines
(quoted diff) +2
-0+ dev->can.state = CAN_STATE_ERROR_PASSIVE; + }
Quoted 7 lines
Quoted 1 line
Also please update the stats accordingly:
These are simple counts for how often the conditions occurred? They
count when the respective state is entered? When a node recovers from
bus off does that count as entering error passive?
Yes, if it's reported, but normally not. Currently we mainly support
increasing CAN error states active->warning->passive->bus-off.
Quoted 7 lines
Quoted 7 lines
can_stats.bus_off++; can_stats.error_warning++ can_stats.error_passive++
I forgot "can_stats.restarts".
Quoted 36 lines
Quoted 8 lines
Apart from that, the state handling is rather basic but totaly
sufficient. Would be nice if you could show the state handling
and bus-off recovery by reporting the output of "$ candump -td -e
Quoted 7 lines
any,0:0,#FFFFFFFF" while sending messages to the device ...
1. without cable connected (entering error passive)
2. with short-circuited CAN high and low (entering bus-off)
$ sudo ip link set can0 up type can bitrate 250000$ cansequence can0 -p$ candump -td -e can0,0:0,#FFFFFFFF 1>can.log# CAN node transmitting normally
(000.000000) can0 002 [1] 00
...
(000.000006) can0 002 [1] 68
# CAN node is disconnected from bus
Quoted 7 lines
(000.040973) can0 20000004 [8] 00 20 00 00 00 00 80 00
ERRORFRAME
controller-problem{tx-error-passive} error-counter-tx-rx{{128}{0}}
OK, error warning is obviously not reported (also not required).
Quoted 6 lines
(007.437011) can0 002 [1] 69
...
(000.000991) can0 002 [1] B3
Quoted 7 lines
# CAN node is reconnected to bus
(000.000008) can0 20000004 [8] 00 00 00 00 00 00 00 00
ERRORFRAME
controller-problem{}
This is obviously the back to error active. Could you use
cf->can_id |= CAN_ERR_PROT;
cf->data[2] = CAN_ERR_PROT_ACTIVE;as for the at91_can (or flexcan, c_can, usb_8dev.c). See:
http://lxr.free-electrons.com/source/drivers/net/can/at91_can.c#L962
Well, back to error active is not an error but that's how we handle
it currently.
Quoted 18 lines
(000.000004) can0 002 [1] B4
...
(000.000003) can0 002 [1] D3
# CAN node is disconnected from bus
(000.062975) can0 20000004 [8] 00 20 00 00 00 00 80 00
ERRORFRAME
controller-problem{tx-error-passive} error-counter-tx-rx{{128}{0}}# short added between CAN-High to CAN-Low
Quoted 7 lines
(005.593023) can0 20000040 [8] 00 00 00 00 00 00 00 0E
ERRORFRAME
bus-off error-counter-tx-rx{{0}{14}}
The error counter do not match. Also error warning and passive
are not reported.
Quoted 7 lines
(000.260982) can0 20000100 [8] 00 00 00 00 00 00 E8 00
ERRORFRAME
restarted-after-bus-off error-counter-tx-rx{{232}{0}}
Bus-off recovery is restarted after 26ms. Is this value configurable.
See also my comments above.
Quoted 78 lines
(000.526024) can0 20000040 [8] 00 00 00 00 00 00 00 7D
ERRORFRAME
bus-off error-counter-tx-rx{{0}{125}}(001.834994) can0 20000100 [8] 00 00 00 00 00 00 F0 00
ERRORFRAME
restarted-after-bus-off error-counter-tx-rx{{240}{0}}(000.523992) can0 20000040 [8] 00 00 00 00 00 00 00 80
ERRORFRAME
bus-off error-counter-tx-rx{{0}{128}}(001.835013) can0 20000100 [8] 00 00 00 00 00 00 F8 00
ERRORFRAME
restarted-after-bus-off error-counter-tx-rx{{248}{0}}(000.524981) can0 20000004 [8] 00 00 00 00 00 00 28 00
ERRORFRAME
controller-problem{} error-counter-tx-rx{{40}{0}}(000.086006) can0 20000040 [8] 00 00 00 00 00 00 00 7C
ERRORFRAME
bus-off error-counter-tx-rx{{0}{124}}# The short between CAN-High and CAN-Low is removed
(000.525000) can0 20000100 [8] 00 00 00 00 00 00 80 00
ERRORFRAME
restarted-after-bus-off error-counter-tx-rx{{128}{0}}(002.702998) can0 002 [1] D4
...
(000.000003) can0 002 [1] ED
# CAN node is reconnected to bus
(000.000007) can0 20000004 [8] 00 08 00 00 00 00 6E 00
ERRORFRAME
controller-problem{tx-error-warning} error-counter-tx-rx{{110}{0}}(000.000003) can0 002 [1] EE
...
Quoted 7 lines
(000.000990) can0 002 [1] 60
(000.000008) can0 20000004 [8] 00 00 00 00 00 00 00 00
ERRORFRAME
controller-problem{}
Back to error active. See my comments above. Overall, it does
not look too bad.
Wolfgang
Quoted 19 lines
Quoted 8 lines
Manual recovery is not supported. The node can leave bus off state if
it detects 128 occurrences of 11 consecutive recessive bits on the bus.
Maybe I can trick the controller out of bus off by doing a software
reset. That's probably not going to be a clean solution though.
Would be nice if manual recovery is supported but it's not a requirement.
Maybe you could make the delay before restarting bus-off configurable
using the "restart-ms" argument.
Quoted 7 lines
For further information have a look to:
http://lxr.free-electrons.com/source/Documentation/networking/can.txt#L891
Yes.
Usually the CAN controller sits in BUS_OFF until it detects correct CAN frames _or_ until the controller is re-initialized.
The CAN controller should not recover itself as this might be wrong depending on the use-case.
Therefore the manual "restart" can be initiated by some user application or it can be handled inside the driver e.g. 200ms after a BUS_OFF detection by the "restart-ms" option.
Regards,
Oliver
A message that is not in the archive (sent off-list or not yet received): 1384199350.3483.20.camel@blackbox
Quoted 11 lines · Max S.
Hmm. Seems I missed something.
Quoted 7 lines
Yes.
Quoted 8 lines
Quoted 2 lines
Usually the CAN controller sits in
BUS_OFFuntil it detects correct CAN frames _or_ until the controller is re-initialized.From the CAN2.0A spec chapter 7
12. An node which is ’bus off’ is permitted to become ’error active’ (no longer ’bus off’) with its error counters both set to 0 after 128 occurrence of 11 consecutive ’recessive’ bits have been monitored on the bus.
Hm - I assume this does not mean 128*11 bits but 128 occurences of 11 consecutive ’recessive’ bits with at least one dominant bit in between. But I'm not sure about that.
Quoted 10 lines
So: When the controller goes into bus off because of a short on can high to low. It will inevitably observe recessive bits. and consequently come back out of bus off. It will then attempt to resend and hit bus off again. rinse & repeat.
Why is it necessary to be able to get the controller out of bus off manually?
Think about a misconfigured bitrate.
The application should decide if it's better to stay calm (in BUS_OFF) or whether it should try to re-initialize the CAN controller and start again.
Quoted 22 lines
I was able to improve the firmware to make status updates faster. The following is the current state: @250K bitrate
(000.000011) can0 20000040 [8] 00 00 00 00 00 00 00 01 ERRORFRAMEbus-off error-counter-tx-rx{{0}{1}} (000.005980) can0 20000100 [8] 00 00 00 00 00 00 00 80 ERRORFRAME restarted-after-bus-off error-counter-tx-rx{{0}{128}} (000.000013) can0 20000008 [8] 00 00 40 00 00 00 00 00 ERRORFRAME protocol-violation{{back-to-error-active}{}} (000.000006) can0 20000004 [8] 00 08 00 00 00 00 60 00 ERRORFRAME controller-problem{tx-error-warning} error-counter-tx-rx{{96}{0}} (000.000008) can0 20000004 [8] 00 20 00 00 00 00 80 00 ERRORFRAME controller-problem{tx-error-passive} error-counter-tx-rx{{128}{0}} (000.002014) can0 20000040 [8] 00 00 00 00 00 00 00 01 ERRORFRAME bus-off error-counter-tx-rx{{0}{1}}Quoted 1 line
Quoted 4 lines
Quoted 2 lines
The CAN controller should not recover itself as this might be wrong depending on the use-case.
Is recovery (getting out of bus off) not part of the CAN spec?
As discussed above the controller get's out of the BUS_OFF state when receiving correct CAN traffic.
Or by kicking the controller by restarting it :-)
Quoted 6 lines
Quoted 6 lines
Therefore the manual "restart" can be initiated by some user application or it can be handled inside the driver e.g. 200ms after a
BUS_OFFdetection by the "restart-ms" option.
Regards,
Oliver
Hello all,
I've been doing some more testing and came across some strange behavior. Frames appear to be out of order.
I have both chanels of the GS_USB device and a PEAKUSB adapter on a bus, as well as another node (to ack frames).
I have modified the GS_USB firmware to PANIC when txed frames are out of sequence.
tx interrupt now looks something like this
static uint8_t count = 0;
void tx_channel_0_interrupt(void){
PANIC(txframe.data[0] != count);
count++;The GS_SUB device never panics, however cansequence -r on the peak device gives me messages like:
received wrong sequence count. expected: 216, got: 220 received wrong sequence count. expected: 224, got: 216 received wrong sequence count. expected: 220, got: 224 received wrong sequence count. expected: 227, got: 229 received wrong sequence count. expected: 233, got: 227 received wrong sequence count. expected: 229, got: 233
notice that no frames are lost, just rearranged.
I can force frames to be out of order by restarting cansequence, so i know my monitor code is working.
I have been trying to reproduce this error using libusb in userspace, but have not had any success.
Any thoughts on this?
Regards,
Max Schneider.
OK.
Here's the latest progress.
no line wrap this time :)
From 74b99fe4e949a53d05ac35dc413e3ec3f9e352f6 Mon Sep 17 00:00:00 2001
From: Maximilian Schneider <[email protected]>
Date: Sun, 3 Nov 2013 16:57:06 +0000
Subject: [PATCH] Added support for the GS_USB CAN devices. The Geschwister
Schneider Family of devices are galvanically isolated
USB2.0 to CAN2.0A/B adapters. Currently two form factors
are available, a tethered dongle in a rugged enclosure, and mini-pci-e card.
--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1005 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1014 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.c
drivers/net/can/usb/Kconfig +8 -0
@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenu
drivers/net/can/usb/Makefile +1 -0
@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
drivers/net/can/usb/gs_usb.c new file +1005 -0
1,005 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Hey,
Can someone give me a heads up on the GS_USB driver?
Whats the next step? Is someone looking at it?
Regards,
Max Schneider.
Quoted 3 lines · Max S.
Can someone give me a heads up on the
GS_USBdriver?Whats the next step? Is someone looking at it?
Hello Max,
AFAIK Wolfgang asked for some comments and I'm still not sure about the BUS_OFF behaviour.
Additionally it is usual to have an available hardware when a driver get's into mainline. I tried to click the "Add To Cart" button at
http://schneidersoft.net/node/2 but nothing happens :-(
As long as there's no available HW there's IMHO no pressure for mainline.
Regards,
Oliver
Quoted 6 lines · Oliver Hartkopp
Quoted 3 lines · Max S.
Can someone give me a heads up on the
GS_USBdriver?Whats the next step? Is someone looking at it?
Quoted 8 lines
Hello Max,
AFAIK Wolfgang asked for some comments and I'm still not sure about the
BUS_OFFbehaviour.Additionally it is usual to have an available hardware when a driver get's into mainline. I tried to click the "Add To Cart" button at
http://schneidersoft.net/node/2 but nothing happens :-(
Oops. Now there is something :-)
Did you activate it today?
Quoted 11 lines
As long as there's no available HW there's IMHO no pressure for mainline.
Regards,
Oliver--
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
Quoted 13 lines · Oliver Hartkopp
Quoted 11 lines · Oliver Hartkopp
Quoted 3 lines · Max S.
Can someone give me a heads up on the
GS_USBdriver?Whats the next step? Is someone looking at it?
Hello Max,
AFAIK Wolfgang asked for some comments and I'm still not sure about the
BUS_OFFbehaviour.
Quoted 7 lines
Quoted 3 lines
Additionally it is usual to have an available hardware when a driver get's into mainline. I tried to click the "Add To Cart" button at
http://schneidersoft.net/node/2 but nothing happens :-(Oops. Now there is something :-)
Did you activate it today?
Nope Its been up for a while. It requires javascript, maybe you had it turned off?
Quoted 16 lines
Quoted 4 lines
As long as there's no available HW there's IMHO no pressure for mainline.
Quoted 8 lines
Regards,
Oliver--
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--
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
Hi Max,
Quoted 5 lines · Max S.
Hey,
Can someone give me a heads up on the
GS_USBdriver?Whats the next step? Is someone looking at it?
I will have a final look tomorrow or over the week-end. The driver was already in a good shape. Then it's up to Marc...
Anyway, next time please send the patch as standalone mail with the subject "[PATCH vX] can: gs_usb: ..." incrementing X for each new version. It's also common to briefly describe what has been changed below the "---" line. A rather good example is here:
http://marc.info/?l=linux-can&m=138377346111229&w=2
Then it's less probable that your latest patch will be realized.
Wolfgang.
Quoted 12 lines · Wolfgang Grandegger
Hi Max,
Quoted 5 lines · Max S.
Hey,
Can someone give me a heads up on the
GS_USBdriver?Whats the next step? Is someone looking at it?
I will have a final look tomorrow or over the week-end. The driver was already in a good shape. Then it's up to Marc...
Quoted 8 lines
Anyway, next time please send the patch as standalone mail with the subject "[PATCH vX] can:
gs_usb: ..." incrementing X for each new version. It's also common to briefly describe what has been changed below the "---" line. A rather good example is here:http://marc.info/?l=linux-can&m=138377346111229&w=2
Then it's less probable that your latest patch will be realized.
I mean "s/less/more/" or "s/will be/will not be/"
Wolfgang
Quoted 6 lines · Max S.
OK.
Here's the latest progress.
no line wrap this time :)
Quoted 8 lines
Quoted 1 line
From
74b99fe4e949a53d05ac35dc413e3ec3f9e352f6Mon Sep 17 00:00:00 2001From: Maximilian Schneider <[email protected]>
Date: Sun, 3 Nov 2013 16:57:06 +0000
Subject: [PATCH] Added support for theGS_USBCAN devices. The Geschwister
Schneider Family of devices are galvanically isolated
USB2.0 to CAN2.0A/B adapters. Currently two form factors
are available, a tethered dongle in a rugged enclosure, and mini-pci-e card.
To avoid such long subjects, please read "man git commit":
"Though not required, it’s a good idea to begin the commit message with
a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. Tools that turn commits into email, for example, use the first line on the
Subject: line and the rest of the commit in the body."
Quoted 162 lines
Signed-off-by Maximilian Schneider <[email protected]>--- drivers/net/can/usb/Kconfig | 8 + drivers/net/can/usb/Makefile | 1 + drivers/net/can/usb/gs_usb.c | 1005 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1014 insertions(+) create mode 100644 drivers/net/can/usb/gs_usb.cdrivers/net/can/usb/Kconfig +8
-0@@ -54,4 +54,12 @@ config CAN_8DEV_USB This driver supports the USB2CAN interface from 8 devices (http://www.8devices.com). +config CAN_GS_USB + tristate "Geschwister Schneider UG interfaces" + ---help--- + This driver supports the Geschwister Schneider USB/CAN devices. + If unsure choose N, + choose Y for built in support, + M to compile as module (module will be named: gs_usb). + endmenudrivers/net/can/usb/Makefile +1
-0@@ -7,5 +7,6 @@ obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o +obj-$(CONFIG_CAN_GS_USB) += gs_usb.o ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUGdrivers/net/can/usb/gs_usb.c new file +120
-0@@ -0,0 +1,1005 @@ +/* CAN driver for Geschwister Schneider USB/CAN devices. +* +* Copyright (C) 2013 Geschwister Schneider Technologie-, +* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). +* +* Many thanks to all socketcan devs! +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published +* by the Free Software Foundation; version 2 of the License. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +*/ + +#include <linux/init.h> +#include <linux/signal.h> +#include <linux/module.h> +#include <linux/netdevice.h> +#include <linux/usb.h> + +#include <linux/can.h> +#include <linux/can/dev.h> +#include <linux/can/error.h> + +/* Device specific constants */ +#define USB_GSUSB_1_VENDOR_ID 0x1d50 +#define USB_GSUSB_1_PRODUCT_ID 0x606f + +#define GSUSB_ENDPOINT_IN 1 +#define GSUSB_ENDPOINT_OUT 2 + +/* Device specific constants */ +enum gs_usb_breq { +GS_USB_BREQ_HOST_FORMAT =0 , +GS_USB_BREQ_BITTIMING , +GS_USB_BREQ_MODE , +GS_USB_BREQ_BERR , +GS_USB_BREQ_BT_CONST , +GS_USB_BREQ_DEVICE_CONFIG +}; + +enum gs_can_mode { +/* reset a channel. turns it off */ +GS_CAN_MODE_RESET =0 , +/* starts a channel */ +GS_CAN_MODE_START +}; + +enum gs_can_state { +GS_CAN_STATE_ERROR_ACTIVE =0 , +GS_CAN_STATE_ERROR_WARNING , +GS_CAN_STATE_ERROR_PASSIVE , +GS_CAN_STATE_BUS_OFF , +GS_CAN_STATE_STOPPED , +GS_CAN_STATE_SLEEPING +}; + +/* data types passed between host and device */ +struct gs_host_config { +u32 byte_order; +} __packed; + +struct gs_device_config { +u8 reserved1; +u8 reserved2; +u8 reserved3; +u8 icount; +u32 sw_version; +u32 hw_version; +} __packed; + +#define GS_CAN_MODE_NORMAL 0 +#define GS_CAN_MODE_LISTEN_ONLY (1<<0) +#define GS_CAN_MODE_LOOP_BACK (1<<1) +#define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_MODE_ONE_SHOT (1<<3) + +struct gs_device_mode { +u32 mode; +u32 flags; +} __packed; + +struct gs_device_state { +u32 state; +u32 rxerr; +u32 txerr; +} __packed; + +struct gs_device_bittiming { +u32 prop_seg; +u32 phase_seg1; +u32 phase_seg2; +u32 sjw; +u32 brp; +} __packed; + +#define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) +#define GS_CAN_FEATURE_LOOP_BACK (1<<1) +#define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) +#define GS_CAN_FEATURE_ONE_SHOT (1<<3) + +struct gs_device_bt_const { +u32 feature; +u32 fclk_can; +u32 tseg1_min; +u32 tseg1_max; +u32 tseg2_min; +u32 tseg2_max; +u32 sjw_max; +u32 brp_min; +u32 brp_max; +u32 brp_inc; +} __packed; + +#define GS_CAN_FLAG_OVERFLOW 1 + +struct gs_host_frame {
drivers/net/can/usb/gs_usb.c line 130
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+u32 echo_id; +u32 can_id; + +u8 can_dlc; +u8 channel; +u8 flags; +u8 reserved; + +u8 data[8 ]; +} __packed;
Could you please add some comment on the *firmware* here or in the file header, especially that it uses exactly the same flags and mask as for CAN-Linux for can_id, flags, error flags etc. and therefore no mapping is necessary. Otherwise it's magic how CAN frames are used and created.
Quoted 65 lines
drivers/net/can/usb/gs_usb.c new file +65
-0+ +/* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */ +#define GS_MAX_TX_URBS 10 +/* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */ +#define GS_MAX_RX_URBS 30 +/* Maximum number of interfaces the driver supports per device. +* Current hardware only supports 2 interfaces. The future may vary. +*/ +#define GS_MAX_INTF 2 + +struct gs_tx_context { +struct gs_can *dev; +unsigned int echo_id; +}; + +struct gs_can { +struct can_priv can;/* must be the first member */ + +struct gs_usb *parent; + +struct net_device *netdev; +struct usb_device *udev; +struct usb_interface *iface; + +struct can_bittiming_const bt_const; +unsigned int channel;/* channel number */ + +/* This lock prevents a race condition between xmit and recieve. */ +spinlock_t tx_ctx_lock; +struct gs_tx_context tx_context[GS_MAX_TX_URBS ]; + +struct usb_anchor tx_submitted; +atomic_t active_tx_urbs; +}; + +/* usb interface struct */ +struct gs_usb { +struct gs_can *canch[GS_MAX_INTF ]; +struct usb_anchor rx_submitted; +atomic_t active_channels; +struct usb_device *udev; +}; + +/* 'allocate' a tx context. +* returns a valid tx context or NULL if there is no space. +*/ +static struct gs_tx_context *gs_alloc_tx_context (struct gs_can *dev) +{ +int i =0 ; +unsigned long flags; + +spin_lock_irqsave (&dev->tx_ctx_lock, flags); + +for (; i <GS_MAX_TX_URBS ; i++) { +if (dev->tx_context[i].echo_id ==GS_MAX_TX_URBS ) { + dev->tx_context[i].echo_id = i; +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return &dev->tx_context[i]; + } + } + +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return NULL ; +} +
drivers/net/can/usb/gs_usb.c line 205
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+/* releases a tx context +*/ +static void gs_free_tx_context (struct gs_tx_context *txc) +{ +unsigned long flags; + +spin_lock_irqsave (&txc->dev->tx_ctx_lock, flags); + txc->echo_id =GS_MAX_TX_URBS ; +spin_unlock_irqrestore (&txc->dev->tx_ctx_lock, flags); +}
You do not need a lock to assign a variable. It's done in one instruction. I think I already commented on that.
drivers/net/can/usb/gs_usb.c line 211
Quoted 5 lines
drivers/net/can/usb/gs_usb.c new file +5
-0+/* Get a tx context by id. +*/ +static struct gs_tx_context *gs_get_tx_context ( +struct gs_can *dev, +unsigned int id)
Still time for some nitpicking. Please start adding arguments on the first line.
Quoted 12 lines
drivers/net/can/usb/gs_usb.c new file +12
-0+{ +unsigned long flags; + +spin_lock_irqsave (&dev->tx_ctx_lock, flags); +if (id <GS_MAX_TX_URBS ) { +if (dev->tx_context[id].echo_id == id) { +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return &dev->tx_context[id]; + } + } +spin_unlock_irqrestore (&dev->tx_ctx_lock, flags); +return NULL ;
drivers/net/can/usb/gs_usb.c line 233
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+} + +/* Get a tx contexts id. +*/ +static unsigned int gs_tx_context_id (struct gs_tx_context *txc) +{ +unsigned int id; + id = txc->echo_id; +return id; +}
What is this helper function good for? Also it's shorter to just
return txc->echo_id;
Quoted 23 lines
drivers/net/can/usb/gs_usb.c new file +23
-0+ +static int gs_cmd_reset (struct gs_usb *gsusb,struct gs_can *gsdev) +{ +struct gs_device_mode *dm; +struct usb_interface *intf = gsdev->iface; +int rc; + + dm =kzalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ; + + dm->mode =GS_CAN_MODE_RESET ; + + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_MODE , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + gsdev->channel, +0 , + dm, +sizeof (*dm), +1000 ); +
drivers/net/can/usb/gs_usb.c line 266
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+return rc; +} + +static void gs_update_state (struct gs_can *dev,struct can_frame *cf) +{ +struct can_device_stats *can_stats = &dev->can.can_stats; + +if (cf->can_id &CAN_ERR_RESTARTED ) { + dev->can.state =CAN_STATE_ERROR_ACTIVE ; + can_stats->restarts++;
Hm, is error active here correct ....
Quoted 4 lines
drivers/net/can/usb/gs_usb.c new file +4
-0+ }else if (cf->can_id &CAN_ERR_BUSOFF ) { + dev->can.state =CAN_STATE_BUS_OFF ; + can_stats->bus_off++; + }else if (cf->can_id &CAN_ERR_CRTL ) {
drivers/net/can/usb/gs_usb.c line 280
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+if ((cf->data[1 ] &CAN_ERR_CRTL_TX_WARNING ) || + (cf->data[1 ] &CAN_ERR_CRTL_RX_WARNING )) { + dev->can.state =CAN_STATE_ERROR_WARNING ; + can_stats->error_warning++; + }else if ((cf->data[1 ] &CAN_ERR_CRTL_TX_PASSIVE ) || + (cf->data[1 ] &CAN_ERR_CRTL_RX_PASSIVE )) { + dev->can.state =CAN_STATE_ERROR_PASSIVE ; + can_stats->error_passive++; + }else { + dev->can.state =CAN_STATE_ERROR_ACTIVE ;
... or here. Normally the back to error active, when recovered from bus-off, does come some time later. Anyway, please add for back to error active:
cf->can_id |= CAN_ERR_PROT;
cf->data[2] = CAN_ERR_PROT_ACTIVE;Or does the firmware already sets these bits. And did you re-check the error messages when going error passive, bus-off, restarted, back to error active. Do they look good now?
Quoted 36 lines
drivers/net/can/usb/gs_usb.c new file +36
-0+ } + } +} + +static void gs_usb_recieve_bulk_callback (struct urb *urb) +{ +struct gs_usb *usbcan = urb->context; +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct net_device_stats *stats; +struct gs_host_frame *hf = urb->transfer_buffer; +struct gs_tx_context *txc; +struct can_frame *cf; +struct sk_buff *skb; + +BUG_ON (!usbcan); + +switch (urb->status) { +case 0 :/* success */ +break ; +case -ENOENT : +case -ESHUTDOWN : +return ; +default : +/* do not resubmit aborted urbs. eg: when device goes down */ +return ; + } + +/* device reports out of range channel id */ +if (hf->channel >=GS_MAX_INTF ) + goto resubmit_urb; + +/* device reports bad channel id */ + dev = usbcan->canch[hf->channel]; +
drivers/net/can/usb/gs_usb.c line 326
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ netdev = dev->netdev; + stats = &netdev->stats; + +if (!netif_device_present (netdev)) +return ; + +if (hf->echo_id ==-1 ) {/* normal rx */ + skb =alloc_can_skb (dev->netdev, &cf); +if (skb ==NULL ) +return ;
if (!skb)
as you do for all other checks of that type.
Quoted 35 lines
drivers/net/can/usb/gs_usb.c new file +35
-0+ + cf->can_id = hf->can_id; +/* is it necessary to use get_can_dlc? */ + cf->can_dlc =get_can_dlc (hf->can_dlc); +memcpy (cf->data, hf->data,8 ); + +/* ERROR frames tell us information about the controller */ +if (hf->can_id &CAN_ERR_FLAG ) +gs_update_state (dev, cf); + +netif_rx (skb); + + netdev->stats.rx_packets++; + netdev->stats.rx_bytes += hf->can_dlc; + }else {/* echo_id == hf->echo_id */ +if (hf->echo_id >=GS_MAX_TX_URBS ) { +netdev_err (netdev, +"Device sent an out of range echo id %d\n" , + hf->echo_id); + goto resubmit_urb; + } + + netdev->stats.tx_packets++; + netdev->stats.tx_bytes += hf->can_dlc; + + txc =gs_get_tx_context (dev, hf->echo_id); + +/* bad devices send bad echo_ids. */ +if (!txc) { +netdev_err (netdev, +"Device sent a bad echo id %d\n" , + hf->echo_id); + goto resubmit_urb; + } +
drivers/net/can/usb/gs_usb.c line 371
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+can_get_echo_skb (netdev,gs_tx_context_id (txc)); + +gs_free_tx_context (txc); + +netif_wake_queue (netdev); + } + +if (hf->flags &GS_CAN_FLAG_OVERFLOW ) { + skb =alloc_can_err_skb (netdev, &cf); +if (skb ==NULL )
Ditto.
Quoted 9 lines
drivers/net/can/usb/gs_usb.c new file +9
-0+ goto resubmit_urb; + + cf->can_id |=CAN_ERR_CRTL ; + cf->data[1 ] =CAN_ERR_CRTL_RX_OVERFLOW ; + stats->rx_over_errors++; + stats->rx_errors++; +netif_rx (skb); + } +
drivers/net/can/usb/gs_usb.c line 390
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+resubmit_urb: +/* note: no urb refill... works for me */ +usb_fill_bulk_urb (urb, + usbcan->udev, +usb_rcvbulkpipe (usbcan->udev,GSUSB_ENDPOINT_IN ), + hf, +sizeof (struct gs_host_frame ), + gs_usb_recieve_bulk_callback, + usbcan + );
Please move the ");" to the upper line as you do for the other function calls as well.
Quoted 114 lines
drivers/net/can/usb/gs_usb.c new file +114
-0+ + rc =usb_submit_urb (urb,GFP_ATOMIC ); + +/* USB failure take down all interfaces */ +if (rc == -ENODEV ) { +for (rc =0 ; rc <GS_MAX_INTF ; rc++) { +if (usbcan->canch[rc]) +netif_device_detach (usbcan->canch[rc]->netdev); + } + } +} + +static int gs_usb_set_bittiming (struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct can_bittiming *bt = &dev->can.bittiming; +struct usb_interface *intf = dev->iface; +int rc; +struct gs_device_bittiming *dbt; + + dbt =kmalloc (sizeof (*dbt),GFP_KERNEL ); +if (!dbt) +return -ENOMEM ; + + dbt->prop_seg = bt->prop_seg; + dbt->phase_seg1 = bt->phase_seg1; + dbt->phase_seg2 = bt->phase_seg2; + dbt->sjw = bt->sjw; + dbt->brp = bt->brp; + + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_BITTIMING , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dbt, +sizeof (*dbt), +1000 ); +if (rc <0 ) +dev_err (netdev->dev.parent,"Couldn't set bittimings %d" , rc); + +kfree (dbt); + +return rc; +} + +static void gs_usb_xmit_callback (struct urb *urb) +{ +struct gs_tx_context *txc = urb->context; +struct gs_can *dev = txc->dev; +struct net_device *netdev = dev->netdev; + +if (urb->status) { +netdev_info (netdev,"%d] xmit err %d\n" , + dev->channel, txc->echo_id); + } + +usb_free_coherent (urb->dev, + urb->transfer_buffer_length, + urb->transfer_buffer, + urb->transfer_dma); + +atomic_dec (&dev->active_tx_urbs); + +if (!netif_device_present (netdev)) +return ; + +if (netif_queue_stopped (netdev)) +netif_wake_queue (netdev); +} + +static netdev_tx_t gs_can_start_xmit (struct sk_buff *skb, +struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct net_device_stats *stats = &dev->netdev->stats; +struct urb *urb; +struct gs_host_frame *hf; +struct can_frame *cf; +int rc; +unsigned int idx; +struct gs_tx_context *txc; + +if (dev->can.ctrlmode &CAN_CTRLMODE_LISTENONLY ) +return NETDEV_TX_BUSY ; + +if (can_dropped_invalid_skb (netdev, skb)) +return NETDEV_TX_OK ; + +/* find an empty context to keep track of transmission */ + txc =gs_alloc_tx_context (dev); +if (!txc) +return NETDEV_TX_BUSY ; + +/* create a URB, and a buffer for it */ + urb =usb_alloc_urb (0 ,GFP_ATOMIC ); +if (!urb) { +netdev_err (netdev,"No memory left for URBs\n" ); + goto nomem_urb; + } + + hf =usb_alloc_coherent (dev->udev,sizeof (*hf),GFP_ATOMIC , + &urb->transfer_dma); +if (!hf) { +netdev_err (netdev,"No memory left for USB buffer\n" ); + goto nomem_hf; + } + + idx =gs_tx_context_id (txc); + +if (idx >=GS_MAX_TX_URBS ) { +netdev_err (netdev,"Invalid tx context %d\n" , idx); + goto badidx;
drivers/net/can/usb/gs_usb.c line 514
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ } + + hf->echo_id = idx; + hf->channel = dev->channel; + + cf = (struct can_frame *)skb->data; + + hf->can_id = cf->can_id; + hf->can_dlc = cf->can_dlc; +memcpy (hf->data, cf->data,8 );
memcpy(hf->data, cf->data, cf->can_dlc); !
Quoted 184 lines
drivers/net/can/usb/gs_usb.c new file +184
-0+ +usb_fill_bulk_urb (urb, dev->udev, +usb_sndbulkpipe (dev->udev,GSUSB_ENDPOINT_OUT ), + hf, +sizeof (*hf), + gs_usb_xmit_callback, + txc); + + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; +usb_anchor_urb (urb, &dev->tx_submitted); + +can_put_echo_skb (skb, netdev, idx); + +atomic_inc (&dev->active_tx_urbs); + + rc =usb_submit_urb (urb,GFP_ATOMIC ); +if (unlikely (rc)) {/* usb send failed */ +atomic_dec (&dev->active_tx_urbs); + +netdev_err (netdev,"usb_submit failed %d\n" , rc); + +can_free_echo_skb (netdev,gs_tx_context_id (txc)); +gs_free_tx_context (txc); + +usb_unanchor_urb (urb); +usb_free_coherent (dev->udev, +sizeof (*hf), + hf, + urb->transfer_dma); + + +if (rc == -ENODEV ) { +netif_device_detach (netdev); + }else { +netdev_err (netdev,"Failed tx_urb %d\n" , rc); + stats->tx_dropped++; + } + }else { + netdev->trans_start = jiffies; + +/* Slow down tx path */ +if (atomic_read (&dev->active_tx_urbs) >=GS_MAX_TX_URBS ) +netif_stop_queue (netdev); + } + +/* let usb core take care of this urb */ +usb_free_urb (urb); + +return NETDEV_TX_OK ; + +badidx: +usb_free_coherent (dev->udev, +sizeof (*hf), + hf, + urb->transfer_dma); +nomem_hf: +usb_free_urb (urb); + +nomem_urb: +gs_free_tx_context (txc); +dev_kfree_skb (skb); + stats->tx_dropped++; +return NETDEV_TX_OK ; +} + +static int gs_can_open (struct net_device *netdev) +{ +struct gs_can *dev =netdev_priv (netdev); +struct gs_usb *parent = dev->parent; +int rc, i; +struct gs_device_mode *dm; +u32 ctrlmode; + + rc =open_candev (netdev); +if (rc) +return rc; + +if (atomic_add_return (1 , &parent->active_channels) ==1 ) { +for (i =0 ; i <GS_MAX_RX_URBS ; i++) { +struct urb *urb =NULL ; +u8 *buf =NULL ; + +/* alloc rx urb */ + urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!urb) { +netdev_err (netdev, +"No memory left for URBs\n" ); +return -ENOMEM ; + } + +/* alloc rx buffer */ + buf =usb_alloc_coherent (dev->udev, +sizeof (struct gs_host_frame ), +GFP_KERNEL , + &urb->transfer_dma); +if (!buf) { +netdev_err (netdev, +"No memory left for USB buffer\n" ); +usb_free_urb (urb); +return -ENOMEM ; + } + +/* fill, anchor, and submit rx urb */ +usb_fill_bulk_urb (urb, + dev->udev, +usb_rcvbulkpipe (dev->udev, +GSUSB_ENDPOINT_IN ), + buf, +sizeof (struct gs_host_frame ), + gs_usb_recieve_bulk_callback, + parent + ); + urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +usb_anchor_urb (urb, &parent->rx_submitted); + + rc =usb_submit_urb (urb,GFP_KERNEL ); +if (rc) { +if (rc == -ENODEV ) +netif_device_detach (dev->netdev); + +usb_unanchor_urb (urb); +break ; + } + +/* Drop reference, +* USB core will take care of freeing it +*/ +usb_free_urb (urb); + } + } + + dm =kmalloc (sizeof (*dm),GFP_KERNEL ); +if (!dm) +return -ENOMEM ; + +/* flags */ + ctrlmode = dev->can.ctrlmode; + dm->flags =0 ; + +if (ctrlmode &CAN_CTRLMODE_LOOPBACK ) + dm->flags |=GS_CAN_MODE_LOOP_BACK ; +else if (ctrlmode &CAN_CTRLMODE_LISTENONLY ) + dm->flags |=GS_CAN_MODE_LISTEN_ONLY ; + +/* Controller is not allowed to retry TX +* this mode is unavailable on atmels uc3c hardware +*/ +if (ctrlmode &CAN_CTRLMODE_ONE_SHOT ) + dm->flags |=GS_CAN_MODE_ONE_SHOT ; + +if (ctrlmode &CAN_CTRLMODE_3_SAMPLES ) + dm->flags |=GS_CAN_MODE_TRIPLE_SAMPLE ; + +/* finally start device */ + dm->mode =GS_CAN_MODE_START ; + rc =usb_control_msg (interface_to_usbdev (dev->iface), +usb_sndctrlpipe (interface_to_usbdev (dev->iface),0 ), +GS_USB_BREQ_MODE , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dm, +sizeof (*dm), +1000 ); + +if (rc <0 ) { +netdev_err (netdev,"Couldn't start device (err=%d)\n" , rc); +kfree (dm); +return rc; + } + + rc =usb_control_msg (interface_to_usbdev (dev->iface), +usb_rcvctrlpipe (interface_to_usbdev (dev->iface),0 ), +GS_USB_BREQ_MODE , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , + dev->channel, +0 , + dm, +sizeof (*dm), +1000 ); + +if (rc <0 ) { + netdev_err(netdev, "Couldn't get device mode (err=%d)\n",
drivers/net/can/usb/gs_usb.c line 708
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ rc); +kfree (dm); +return rc; + } + +kfree (dm); + + dev->can.state =CAN_STATE_ERROR_ACTIVE ; + +netif_start_queue (netdev);
if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) netif_start_queue(netdev);
That way, the xmit callback will never be called. I think we should add these lines for all other drivers as well.
Quoted 35 lines
drivers/net/can/usb/gs_usb.c new file +35
-0+ +return 0 ; +} + +static int gs_can_close (struct net_device *netdev) +{ +int rc; +struct gs_can *dev =netdev_priv (netdev); +struct gs_usb *parent = dev->parent; + +netif_stop_queue (netdev); + +/* Stop polling */ +if (atomic_dec_and_test (&parent->active_channels)) +usb_kill_anchored_urbs (&parent->rx_submitted); + + rc =gs_cmd_reset (parent, dev); + +if (rc <0 ) +netdev_warn (netdev,"Couldn't shutdown device (err=%d)" , rc); + +close_candev (netdev); + +return 0 ; +} + +/******************************** USB ********************************/ + +static const struct net_device_ops gs_usb_netdev_ops = { + .ndo_open = gs_can_open, + .ndo_stop = gs_can_close, + .ndo_start_xmit = gs_can_start_xmit, +}; + +static struct gs_can *gs_make_candev(unsigned int channel,
drivers/net/can/usb/gs_usb.c line 753
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ struct usb_interface *intf) +{ +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct gs_device_bt_const *bt_const; + + bt_const =kmalloc (sizeof (*bt_const),GFP_KERNEL ); +if (!bt_const) +return ERR_PTR (-ENOMEM );
This function does not return an error code, therefore just:
return -ENOMEM;
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ +/* fetch bit timing constants */ + rc = usb_control_msg(interface_to_usbdev(intf), + usb_rcvctrlpipe(interface_to_usbdev(intf), 0), + GS_USB_BREQ_BT_CONST, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + 0,
drivers/net/can/usb/gs_usb.c line 771
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ bt_const, +sizeof (*bt_const), + 1000); + +if (rc <0 ) { +dev_err (&intf->dev, +"Couldn't get bit timing const for channel (err=%d)\n" , + rc); +kfree (bt_const); +return ERR_PTR (rc);
return rc; !
Puh, at a closer look there are quite some issues with returning a proper return code.
drivers/net/can/usb/gs_usb.c line 779
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ } + +/* create netdev */ + netdev =alloc_candev (sizeof (struct gs_can ),GS_MAX_TX_URBS ); +if (!netdev) { +dev_err (&intf->dev,"Couldn't alloc candev\n" ); +kfree (bt_const); +return ERR_PTR (-ENOMEM );
This function does not return an error code, therefore just:
return -ENOMEM;
Quoted 51 lines
drivers/net/can/usb/gs_usb.c new file +51
-0+ } + + dev =netdev_priv (netdev); + + netdev->netdev_ops = &gs_usb_netdev_ops; + + netdev->flags |=IFF_ECHO ;/* we support full roundtrip echo */ + +/* dev settup */ +strcpy (dev->bt_const.name,"gs_usb" ); + dev->bt_const.tseg1_min = bt_const->tseg1_min; + dev->bt_const.tseg1_max = bt_const->tseg1_max; + dev->bt_const.tseg2_min = bt_const->tseg2_min; + dev->bt_const.tseg2_max = bt_const->tseg2_max; + dev->bt_const.sjw_max = bt_const->sjw_max; + dev->bt_const.brp_min = bt_const->brp_min; + dev->bt_const.brp_max = bt_const->brp_max; + dev->bt_const.brp_inc = bt_const->brp_inc; + + dev->udev =interface_to_usbdev (intf); + dev->iface = intf; + dev->netdev = netdev; + dev->channel = channel; + +init_usb_anchor (&dev->tx_submitted); +atomic_set (&dev->active_tx_urbs,0 ); +spin_lock_init (&dev->tx_ctx_lock); +for (rc =0 ; rc <GS_MAX_TX_URBS ; rc++) { + dev->tx_context[rc].dev = dev; + dev->tx_context[rc].echo_id =GS_MAX_TX_URBS ; + } + +/* can settup */ + dev->can.state =CAN_STATE_STOPPED ; + dev->can.clock.freq = bt_const->fclk_can; + dev->can.bittiming_const = &dev->bt_const; + dev->can.do_set_bittiming = gs_usb_set_bittiming; + + dev->can.ctrlmode_supported =0 ; + +if (bt_const->feature &GS_CAN_FEATURE_LISTEN_ONLY ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LISTENONLY ; + +if (bt_const->feature &GS_CAN_FEATURE_LOOP_BACK ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LOOPBACK ; + +if (bt_const->feature &GS_CAN_FEATURE_TRIPLE_SAMPLE ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_3_SAMPLES ; + +if (bt_const->feature &GS_CAN_FEATURE_ONE_SHOT ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_ONE_SHOT ;
drivers/net/can/usb/gs_usb.c line 840
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +kfree (bt_const); + +SET_NETDEV_DEV (netdev, &intf->dev); + + rc =register_candev (dev->netdev); +if (rc) { +free_candev (dev->netdev); +dev_err (&intf->dev,"Couldn't register candev\n" ); +return ERR_PTR (rc);
return rc; !!!
Quoted 61 lines
drivers/net/can/usb/gs_usb.c new file +61
-0+ } + +return dev; +} + +static void gs_destroy_candev (struct gs_can *dev) +{ +unregister_candev (dev->netdev); +free_candev (dev->netdev); +usb_kill_anchored_urbs (&dev->tx_submitted); +} + +static int gs_usb_probe (struct usb_interface *intf, +const struct usb_device_id *id) +{ +struct gs_usb *dev; +int rc = -ENOMEM ; +unsigned int icount, i; +struct gs_host_config *hconf; +struct gs_device_config *dconf; + + hconf =kmalloc (sizeof (*hconf),GFP_KERNEL ); +if (!hconf) +return -ENOMEM ; + + hconf->byte_order =0x0000beef ; + +/* send host config */ + rc =usb_control_msg (interface_to_usbdev (intf), +usb_sndctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_HOST_FORMAT , +USB_DIR_OUT |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , +1 , + intf->altsetting[0 ].desc.bInterfaceNumber, + hconf, +sizeof (*hconf), +1000 ); + +kfree (hconf); + +if (rc <0 ) { +dev_err (&intf->dev,"Couldn't send data format: %d\n" , + rc); +return rc; + } + + + dconf =kmalloc (sizeof (*dconf),GFP_KERNEL ); +if (!dconf) +return -ENOMEM ; + +/* read device config */ + rc =usb_control_msg (interface_to_usbdev (intf), +usb_rcvctrlpipe (interface_to_usbdev (intf),0 ), +GS_USB_BREQ_DEVICE_CONFIG , +USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_INTERFACE , +1 , + intf->altsetting[0 ].desc.bInterfaceNumber, + dconf, +sizeof (*dconf), +1000 );
drivers/net/can/usb/gs_usb.c line 911
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+if (rc <0 ) { +dev_err (&intf->dev,"Couldn't recieve device config: %d\n" , + rc); + +kfree (dconf); + +return rc; + } + + icount = dconf->icount+1 ;
s/+/ + /
Quoted 25 lines
drivers/net/can/usb/gs_usb.c new file +25
-0+ +kfree (dconf); + +dev_info (&intf->dev,"Configuring for %d interfaces\n" , icount); + +if (icount >GS_MAX_INTF ) { +dev_err (&intf->dev, +"Driver cannot handle more that %d CAN interfaces\n" , +GS_MAX_INTF ); +return -EINVAL ; + } + + dev =kzalloc (sizeof (*dev),GFP_KERNEL ); +init_usb_anchor (&dev->rx_submitted); + +atomic_set (&dev->active_channels,0 ); + +usb_set_intfdata (intf, dev); + dev->udev =interface_to_usbdev (intf); + +for (i =0 ; i < icount; i++) { + dev->canch[i] =gs_make_candev (i, intf); +if (IS_ERR_OR_NULL (dev->canch[i])) { +/* on failure destroy previously created candevs */ + icount = i;
drivers/net/can/usb/gs_usb.c line 946
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+for (i =0 ; i < icount; i++) { +gs_destroy_candev (dev->canch[i]); + dev->canch[icount] =NULL ; + } +return rc; + } + dev->canch[i]->parent = dev; + } + +dev_info (&intf->dev,"Probe\n" );
Please remove. You may want to add an info line at the end of gs_make_candev() to report firmware version or other information useful for the *normal* user.
Quoted 28 lines
drivers/net/can/usb/gs_usb.c new file +28
-0+return 0 ; +} + +static void gs_usb_disconnect (struct usb_interface *intf) +{ +unsigned i; +struct gs_usb *dev =usb_get_intfdata (intf); +usb_set_intfdata (intf,NULL ); + +if (!dev) { +dev_err (&intf->dev,"Disconnect (nodata)\n" ); +return ; + } + +for (i =0 ; i <GS_MAX_INTF ; i++) { +struct gs_can *can = dev->canch[i]; + +if (!can) +continue ; + +gs_destroy_candev (can); + } + +usb_kill_anchored_urbs (&dev->rx_submitted); +} + +MODULE_DEVICE_TABLE (usb, gs_usb_table); +
drivers/net/can/usb/gs_usb.c line 984
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+static struct usb_device_id gs_usb_table[] = { + {USB_DEVICE (USB_GSUSB_1_VENDOR_ID ,USB_GSUSB_1_PRODUCT_ID )}, + {}/* Terminating entry */ +}; + +static struct usb_driver gs_usb_driver = { + .name ="gs_usb" , + .probe = gs_usb_probe, + .disconnect = gs_usb_disconnect, + .id_table = gs_usb_table
Please add a "," here as well.
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +11
-0+}; + +static int __init gs_usb_init (void ) +{ +return usb_register (&gs_usb_driver); +} + +static void __exit gs_usb_exit(void) +{ +usb_deregister (&gs_usb_driver); +}
drivers/net/can/usb/gs_usb.c line 1005
Quoted 10 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +module_init (gs_usb_init); +module_exit (gs_usb_exit); + +MODULE_AUTHOR ("Maximilian Schneider <[email protected]>" ); +MODULE_DESCRIPTION ( +"Socket CAN device driver for Geschwister Schneider Technologie-, " +"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces." ); +MODULE_LICENSE ("GPL v2" ); +
Wolfgang.
Marc Kleine-Budde
maintainerHello Wolfgang,
thanks for reviewing the driver.
Quoted 2 lines · Wolfgang Grandegger
Quoted 2 lines
+static struct
gs_can*gs_make_candev(unsigned int channel,drivers/net/can/usb/gs_usb.c line 744
Quoted 1 line
drivers/net/can/usb/gs_usb.c new file +1
-0+ struct usb_interface *intf)
This function returns a struct gs_can pointer...
Quoted 11 lines
Quoted 9 lines
drivers/net/can/usb/gs_usb.c line 753
Quoted 9 lines
drivers/net/can/usb/gs_usb.c new file +9
-0+{ +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct gs_device_bt_const *bt_const; + + bt_const =kmalloc (sizeof (*bt_const),GFP_KERNEL ); +if (!bt_const) +return ERR_PTR (-ENOMEM );This function does not return an error code, therefore just:
Quoted 1 line
return -ENOMEM;
....so ERR_PTR(-ENOMEM) is correct. The error value "-ENOMEM" has to be converted to a pointer and this is what ERR_PTR does. The same is true for the rest of this function.
Quoted 99 lines
Quoted 11 lines
Quoted 11 lines
drivers/net/can/usb/gs_usb.c new file +10
-0+ +/* fetch bit timing constants */ + rc = usb_control_msg(interface_to_usbdev(intf), + usb_rcvctrlpipe(interface_to_usbdev(intf), 0), + GS_USB_BREQ_BT_CONST, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + 0, + bt_const, + sizeof(*bt_const),Quoted 8 lines
drivers/net/can/usb/gs_usb.c line 771
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ 1000); + +if (rc <0 ) { +dev_err (&intf->dev, +"Couldn't get bit timing const for channel (err=%d)\n" , + rc); +kfree (bt_const); +return ERR_PTR (rc);return rc; !
Puh, at a closer look there are quite some issues with returning a proper return code.
Quoted 8 lines
drivers/net/can/usb/gs_usb.c line 779
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ } + +/* create netdev */ + netdev =alloc_candev (sizeof (struct gs_can ),GS_MAX_TX_URBS ); +if (!netdev) { +dev_err (&intf->dev,"Couldn't alloc candev\n" ); +kfree (bt_const); +return ERR_PTR (-ENOMEM );This function does not return an error code, therefore just:
return -ENOMEM;Quoted 53 lines
Quoted 53 lines
drivers/net/can/usb/gs_usb.c new file +53
-0+ } + + dev =netdev_priv (netdev); + + netdev->netdev_ops = &gs_usb_netdev_ops; + + netdev->flags |=IFF_ECHO ;/* we support full roundtrip echo */ + +/* dev settup */ +strcpy (dev->bt_const.name,"gs_usb" ); + dev->bt_const.tseg1_min = bt_const->tseg1_min; + dev->bt_const.tseg1_max = bt_const->tseg1_max; + dev->bt_const.tseg2_min = bt_const->tseg2_min; + dev->bt_const.tseg2_max = bt_const->tseg2_max; + dev->bt_const.sjw_max = bt_const->sjw_max; + dev->bt_const.brp_min = bt_const->brp_min; + dev->bt_const.brp_max = bt_const->brp_max; + dev->bt_const.brp_inc = bt_const->brp_inc; + + dev->udev =interface_to_usbdev (intf); + dev->iface = intf; + dev->netdev = netdev; + dev->channel = channel; + +init_usb_anchor (&dev->tx_submitted); +atomic_set (&dev->active_tx_urbs,0 ); +spin_lock_init (&dev->tx_ctx_lock); +for (rc =0 ; rc <GS_MAX_TX_URBS ; rc++) { + dev->tx_context[rc].dev = dev; + dev->tx_context[rc].echo_id =GS_MAX_TX_URBS ; + } + +/* can settup */ + dev->can.state =CAN_STATE_STOPPED ; + dev->can.clock.freq = bt_const->fclk_can; + dev->can.bittiming_const = &dev->bt_const; + dev->can.do_set_bittiming = gs_usb_set_bittiming; + + dev->can.ctrlmode_supported =0 ; + +if (bt_const->feature &GS_CAN_FEATURE_LISTEN_ONLY ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LISTENONLY ; + +if (bt_const->feature &GS_CAN_FEATURE_LOOP_BACK ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_LOOPBACK ; + +if (bt_const->feature &GS_CAN_FEATURE_TRIPLE_SAMPLE ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_3_SAMPLES ; + +if (bt_const->feature &GS_CAN_FEATURE_ONE_SHOT ) + dev->can.ctrlmode_supported |=CAN_CTRLMODE_ONE_SHOT ; + +kfree (bt_const);Quoted 8 lines
drivers/net/can/usb/gs_usb.c line 840
Quoted 8 lines
drivers/net/can/usb/gs_usb.c new file +8
-0+ +SET_NETDEV_DEV (netdev, &intf->dev); + + rc =register_candev (dev->netdev); +if (rc) { +free_candev (dev->netdev); +dev_err (&intf->dev,"Couldn't register candev\n" ); +return ERR_PTR (rc);return rc; !!!
Quoted 4 lines
Quoted 4 lines
drivers/net/can/usb/gs_usb.c line 844
Quoted 4 lines
drivers/net/can/usb/gs_usb.c new file +4
-0+ } + +return dev; +}
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 |
Quoted 21 lines · Marc Kleine-Budde
Hello Wolfgang,
thanks for reviewing the driver.
Quoted 2 lines · Wolfgang Grandegger
Quoted 2 lines
+static struct
gs_can*gs_make_candev(unsigned int channel,drivers/net/can/usb/gs_usb.c line 744
Quoted 1 line
drivers/net/can/usb/gs_usb.c new file +1
-0+ struct usb_interface *intf)This function returns a struct
gs_canpointer...Quoted 11 lines
Quoted 9 lines
drivers/net/can/usb/gs_usb.c line 753
Quoted 9 lines
drivers/net/can/usb/gs_usb.c new file +9
-0+{ +struct gs_can *dev; +struct net_device *netdev; +int rc; +struct gs_device_bt_const *bt_const; + + bt_const =kmalloc (sizeof (*bt_const),GFP_KERNEL ); +if (!bt_const) +return ERR_PTR (-ENOMEM );This function does not return an error code, therefore just:
Quoted 5 lines
Quoted 1 line
return -ENOMEM;....so
ERR_PTR(-ENOMEM)is correct. The error value "-ENOMEM" has to be converted to a pointer and this is whatERR_PTRdoes. The same is true for the rest of this function.
Of course. Obviously I got confused as well.
Wolfgang.