/*
* CAN driver for esd CAN-USB/2
*
* Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/init.h>
#include <linux/signal.h>
#include <linux/slab.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>
MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 interfaces");
MODULE_LICENSE("GPL v2");
/* Define these values to match your devices */
#define USB_ESDGMBH_VENDOR_ID 0x0ab4
#define USB_CANUSB2_PRODUCT_ID 0x0010
#define ESD_USB2_CAN_CLOCK 60000000
#define ESD_USB2_MAX_NETS 2
/* USB2 commands */
#define CMD_VERSION 1 /* also used for VERSION_REPLY */
#define CMD_CAN_RX 2 /* device to host only */
#define CMD_CAN_TX 3 /* also used for TX_DONE */
#define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
#define CMD_TS 5 /* also used for TS_REPLY */
#define CMD_IDADD 6 /* also used for IDADD_REPLY */
/* esd CAN message flags - dlc field */
#define ESD_RTR 0x10
/* esd CAN message flags - id field */
#define ESD_EXTID 0x20000000
#define ESD_EVENT 0x40000000
#define ESD_IDMASK 0x1fffffff
/* esd CAN event ids used by this driver */
#define ESD_EV_CAN_ERROR_EXT 2
/* baudrate message flags */
#define ESD_USB2_UBR 0x80000000
#define ESD_USB2_LOM 0x40000000
#define ESD_USB2_NO_BAUDRATE 0x7fffffff
#define ESD_USB2_TSEG1_MIN 1
#define ESD_USB2_TSEG1_MAX 16
#define ESD_USB2_TSEG1_SHIFT 16
#define ESD_USB2_TSEG2_MIN 1
#define ESD_USB2_TSEG2_MAX 8
#define ESD_USB2_TSEG2_SHIFT 20
#define ESD_USB2_SJW_MAX 4
#define ESD_USB2_SJW_SHIFT 14
#define ESD_USB2_BRP_MIN 1
#define ESD_USB2_BRP_MAX 1024
#define ESD_USB2_BRP_INC 1
#define ESD_USB2_3_SAMPLES 0x00800000
/* esd IDADD message */
#define ESD_ID_ENABLE 0x80
#define ESD_MAX_ID_SEGMENT 64
/* SJA1000 ECC register (emulated by usb2 firmware) */
#define SJA1000_ECC_SEG 0x1F
#define SJA1000_ECC_DIR 0x20
#define SJA1000_ECC_ERR 0x06
#define SJA1000_ECC_BIT 0x00
#define SJA1000_ECC_FORM 0x40
#define SJA1000_ECC_STUFF 0x80
#define SJA1000_ECC_MASK 0xc0
/* esd bus state event codes */
#define ESD_BUSSTATE_MASK 0xc0
#define ESD_BUSSTATE_WARN 0x40
#define ESD_BUSSTATE_ERRPASSIVE 0x80
#define ESD_BUSSTATE_BUSOFF 0xc0
#define RX_BUFFER_SIZE 1024
#define MAX_RX_URBS 4
#define MAX_TX_URBS 16 /* must be power of 2 */
struct header_msg {
u8 len; /* len is always the total message length in 32bit words */
u8 cmd;
u8 rsvd[2];
};
struct version_msg {
u8 len;
u8 cmd;
u8 rsvd;
u8 flags;
__le32 drv_version;
};
struct version_reply_msg {
u8 len;
u8 cmd;
u8 nets;
u8 features;
__le32 version;
u8 name[16];
__le32 rsvd;
__le32 ts;
};
struct rx_msg {
u8 len;
u8 cmd;
u8 net;
u8 dlc;
__le32 ts;
__le32 id; /* upper 3 bits contain flags */
u8 data[8];
};
struct tx_msg {
u8 len;
u8 cmd;
u8 net;
u8 dlc;
__le32 hnd;
__le32 id; /* upper 3 bits contain flags */
u8 data[8];
};
struct tx_done_msg {
u8 len;
u8 cmd;
u8 net;
u8 status;
__le32 hnd;
__le32 ts;
};
struct id_filter_msg {
u8 len;
u8 cmd;
u8 net;
u8 option;
__le32 mask[ESD_MAX_ID_SEGMENT + 1];
};
struct set_baudrate_msg {
u8 len;
u8 cmd;
u8 net;
u8 rsvd;
__le32 baud;
};
/* Main message type used between library and application */
struct __attribute__ ((packed)) esd_usb2_msg {
union {
struct header_msg hdr;
struct version_msg version;
struct version_reply_msg version_reply;
struct rx_msg rx;
struct tx_msg tx;
struct tx_done_msg txdone;
struct set_baudrate_msg setbaud;
struct id_filter_msg filter;
} msg;
};
static struct usb_device_id esd_usb2_table[] = {
{USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
{}
};
MODULE_DEVICE_TABLE(usb, esd_usb2_table);
struct esd_usb2_net_priv;
struct esd_tx_urb_context {
struct esd_usb2_net_priv *priv;
u32 echo_index;
int dlc;
};
struct esd_usb2 {
struct usb_device *udev;
struct esd_usb2_net_priv *nets