cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

usb_8dev.c (23985B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * CAN driver for "8 devices" USB2CAN converter
      4 *
      5 * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at)
      6 *
      7 * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c
      8 * and drivers/net/can/usb/esd_usb2.c
      9 *
     10 * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de)
     11 * for testing and fixing this driver. Also many thanks to "8 devices",
     12 * who were very cooperative and answered my questions.
     13 */
     14
     15#include <linux/signal.h>
     16#include <linux/slab.h>
     17#include <linux/module.h>
     18#include <linux/netdevice.h>
     19#include <linux/usb.h>
     20
     21#include <linux/can.h>
     22#include <linux/can/dev.h>
     23#include <linux/can/error.h>
     24
     25/* driver constants */
     26#define MAX_RX_URBS			20
     27#define MAX_TX_URBS			20
     28#define RX_BUFFER_SIZE			64
     29
     30/* vendor and product id */
     31#define USB_8DEV_VENDOR_ID		0x0483
     32#define USB_8DEV_PRODUCT_ID		0x1234
     33
     34/* endpoints */
     35enum usb_8dev_endpoint {
     36	USB_8DEV_ENDP_DATA_RX = 1,
     37	USB_8DEV_ENDP_DATA_TX,
     38	USB_8DEV_ENDP_CMD_RX,
     39	USB_8DEV_ENDP_CMD_TX
     40};
     41
     42/* device CAN clock */
     43#define USB_8DEV_ABP_CLOCK		32000000
     44
     45/* setup flags */
     46#define USB_8DEV_SILENT			0x01
     47#define USB_8DEV_LOOPBACK		0x02
     48#define USB_8DEV_DISABLE_AUTO_RESTRANS	0x04
     49#define USB_8DEV_STATUS_FRAME		0x08
     50
     51/* commands */
     52enum usb_8dev_cmd {
     53	USB_8DEV_RESET = 1,
     54	USB_8DEV_OPEN,
     55	USB_8DEV_CLOSE,
     56	USB_8DEV_SET_SPEED,
     57	USB_8DEV_SET_MASK_FILTER,
     58	USB_8DEV_GET_STATUS,
     59	USB_8DEV_GET_STATISTICS,
     60	USB_8DEV_GET_SERIAL,
     61	USB_8DEV_GET_SOFTW_VER,
     62	USB_8DEV_GET_HARDW_VER,
     63	USB_8DEV_RESET_TIMESTAMP,
     64	USB_8DEV_GET_SOFTW_HARDW_VER
     65};
     66
     67/* command options */
     68#define USB_8DEV_BAUD_MANUAL		0x09
     69#define USB_8DEV_CMD_START		0x11
     70#define USB_8DEV_CMD_END		0x22
     71
     72#define USB_8DEV_CMD_SUCCESS		0
     73#define USB_8DEV_CMD_ERROR		255
     74
     75#define USB_8DEV_CMD_TIMEOUT		1000
     76
     77/* frames */
     78#define USB_8DEV_DATA_START		0x55
     79#define USB_8DEV_DATA_END		0xAA
     80
     81#define USB_8DEV_TYPE_CAN_FRAME		0
     82#define USB_8DEV_TYPE_ERROR_FRAME	3
     83
     84#define USB_8DEV_EXTID			0x01
     85#define USB_8DEV_RTR			0x02
     86#define USB_8DEV_ERR_FLAG		0x04
     87
     88/* status */
     89#define USB_8DEV_STATUSMSG_OK		0x00  /* Normal condition. */
     90#define USB_8DEV_STATUSMSG_OVERRUN	0x01  /* Overrun occurred when sending */
     91#define USB_8DEV_STATUSMSG_BUSLIGHT	0x02  /* Error counter has reached 96 */
     92#define USB_8DEV_STATUSMSG_BUSHEAVY	0x03  /* Error count. has reached 128 */
     93#define USB_8DEV_STATUSMSG_BUSOFF	0x04  /* Device is in BUSOFF */
     94#define USB_8DEV_STATUSMSG_STUFF	0x20  /* Stuff Error */
     95#define USB_8DEV_STATUSMSG_FORM		0x21  /* Form Error */
     96#define USB_8DEV_STATUSMSG_ACK		0x23  /* Ack Error */
     97#define USB_8DEV_STATUSMSG_BIT0		0x24  /* Bit1 Error */
     98#define USB_8DEV_STATUSMSG_BIT1		0x25  /* Bit0 Error */
     99#define USB_8DEV_STATUSMSG_CRC		0x27  /* CRC Error */
    100
    101#define USB_8DEV_RP_MASK		0x7F  /* Mask for Receive Error Bit */
    102
    103
    104/* table of devices that work with this driver */
    105static const struct usb_device_id usb_8dev_table[] = {
    106	{ USB_DEVICE(USB_8DEV_VENDOR_ID, USB_8DEV_PRODUCT_ID) },
    107	{ }					/* Terminating entry */
    108};
    109
    110MODULE_DEVICE_TABLE(usb, usb_8dev_table);
    111
    112struct usb_8dev_tx_urb_context {
    113	struct usb_8dev_priv *priv;
    114
    115	u32 echo_index;
    116};
    117
    118/* Structure to hold all of our device specific stuff */
    119struct usb_8dev_priv {
    120	struct can_priv can; /* must be the first member */
    121
    122	struct usb_device *udev;
    123	struct net_device *netdev;
    124
    125	atomic_t active_tx_urbs;
    126	struct usb_anchor tx_submitted;
    127	struct usb_8dev_tx_urb_context tx_contexts[MAX_TX_URBS];
    128
    129	struct usb_anchor rx_submitted;
    130
    131	struct can_berr_counter bec;
    132
    133	u8 *cmd_msg_buffer;
    134
    135	struct mutex usb_8dev_cmd_lock;
    136	void *rxbuf[MAX_RX_URBS];
    137	dma_addr_t rxbuf_dma[MAX_RX_URBS];
    138};
    139
    140/* tx frame */
    141struct __packed usb_8dev_tx_msg {
    142	u8 begin;
    143	u8 flags;	/* RTR and EXT_ID flag */
    144	__be32 id;	/* upper 3 bits not used */
    145	u8 dlc;		/* data length code 0-8 bytes */
    146	u8 data[8];	/* 64-bit data */
    147	u8 end;
    148};
    149
    150/* rx frame */
    151struct __packed usb_8dev_rx_msg {
    152	u8 begin;
    153	u8 type;		/* frame type */
    154	u8 flags;		/* RTR and EXT_ID flag */
    155	__be32 id;		/* upper 3 bits not used */
    156	u8 dlc;			/* data length code 0-8 bytes */
    157	u8 data[8];		/* 64-bit data */
    158	__be32 timestamp;	/* 32-bit timestamp */
    159	u8 end;
    160};
    161
    162/* command frame */
    163struct __packed usb_8dev_cmd_msg {
    164	u8 begin;
    165	u8 channel;	/* unknown - always 0 */
    166	u8 command;	/* command to execute */
    167	u8 opt1;	/* optional parameter / return value */
    168	u8 opt2;	/* optional parameter 2 */
    169	u8 data[10];	/* optional parameter and data */
    170	u8 end;
    171};
    172
    173static int usb_8dev_send_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size)
    174{
    175	int actual_length;
    176
    177	return usb_bulk_msg(priv->udev,
    178			    usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_TX),
    179			    msg, size, &actual_length, USB_8DEV_CMD_TIMEOUT);
    180}
    181
    182static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size,
    183				int *actual_length)
    184{
    185	return usb_bulk_msg(priv->udev,
    186			    usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_RX),
    187			    msg, size, actual_length, USB_8DEV_CMD_TIMEOUT);
    188}
    189
    190/* Send command to device and receive result.
    191 * Command was successful when opt1 = 0.
    192 */
    193static int usb_8dev_send_cmd(struct usb_8dev_priv *priv,
    194			     struct usb_8dev_cmd_msg *out,
    195			     struct usb_8dev_cmd_msg *in)
    196{
    197	int err;
    198	int num_bytes_read;
    199	struct net_device *netdev;
    200
    201	netdev = priv->netdev;
    202
    203	out->begin = USB_8DEV_CMD_START;
    204	out->end = USB_8DEV_CMD_END;
    205
    206	mutex_lock(&priv->usb_8dev_cmd_lock);
    207
    208	memcpy(priv->cmd_msg_buffer, out,
    209		sizeof(struct usb_8dev_cmd_msg));
    210
    211	err = usb_8dev_send_cmd_msg(priv, priv->cmd_msg_buffer,
    212				    sizeof(struct usb_8dev_cmd_msg));
    213	if (err < 0) {
    214		netdev_err(netdev, "sending command message failed\n");
    215		goto failed;
    216	}
    217
    218	err = usb_8dev_wait_cmd_msg(priv, priv->cmd_msg_buffer,
    219				    sizeof(struct usb_8dev_cmd_msg),
    220				    &num_bytes_read);
    221	if (err < 0) {
    222		netdev_err(netdev, "no command message answer\n");
    223		goto failed;
    224	}
    225
    226	memcpy(in, priv->cmd_msg_buffer, sizeof(struct usb_8dev_cmd_msg));
    227
    228	if (in->begin != USB_8DEV_CMD_START || in->end != USB_8DEV_CMD_END ||
    229			num_bytes_read != 16 || in->opt1 != 0)
    230		err = -EPROTO;
    231
    232failed:
    233	mutex_unlock(&priv->usb_8dev_cmd_lock);
    234	return err;
    235}
    236
    237/* Send open command to device */
    238static int usb_8dev_cmd_open(struct usb_8dev_priv *priv)
    239{
    240	struct can_bittiming *bt = &priv->can.bittiming;
    241	struct usb_8dev_cmd_msg outmsg;
    242	struct usb_8dev_cmd_msg inmsg;
    243	u32 ctrlmode = priv->can.ctrlmode;
    244	u32 flags = USB_8DEV_STATUS_FRAME;
    245	__be32 beflags;
    246	__be16 bebrp;
    247
    248	memset(&outmsg, 0, sizeof(outmsg));
    249	outmsg.command = USB_8DEV_OPEN;
    250	outmsg.opt1 = USB_8DEV_BAUD_MANUAL;
    251	outmsg.data[0] = bt->prop_seg + bt->phase_seg1;
    252	outmsg.data[1] = bt->phase_seg2;
    253	outmsg.data[2] = bt->sjw;
    254
    255	/* BRP */
    256	bebrp = cpu_to_be16((u16)bt->brp);
    257	memcpy(&outmsg.data[3], &bebrp, sizeof(bebrp));
    258
    259	/* flags */
    260	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
    261		flags |= USB_8DEV_LOOPBACK;
    262	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
    263		flags |= USB_8DEV_SILENT;
    264	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
    265		flags |= USB_8DEV_DISABLE_AUTO_RESTRANS;
    266
    267	beflags = cpu_to_be32(flags);
    268	memcpy(&outmsg.data[5], &beflags, sizeof(beflags));
    269
    270	return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
    271}
    272
    273/* Send close command to device */
    274static int usb_8dev_cmd_close(struct usb_8dev_priv *priv)
    275{
    276	struct usb_8dev_cmd_msg inmsg;
    277	struct usb_8dev_cmd_msg outmsg = {
    278		.channel = 0,
    279		.command = USB_8DEV_CLOSE,
    280		.opt1 = 0,
    281		.opt2 = 0
    282	};
    283
    284	return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
    285}
    286
    287/* Get firmware and hardware version */
    288static int usb_8dev_cmd_version(struct usb_8dev_priv *priv, u32 *res)
    289{
    290	struct usb_8dev_cmd_msg	inmsg;
    291	struct usb_8dev_cmd_msg	outmsg = {
    292		.channel = 0,
    293		.command = USB_8DEV_GET_SOFTW_HARDW_VER,
    294		.opt1 = 0,
    295		.opt2 = 0
    296	};
    297
    298	int err = usb_8dev_send_cmd(priv, &outmsg, &inmsg);
    299	if (err)
    300		return err;
    301
    302	*res = be32_to_cpup((__be32 *)inmsg.data);
    303
    304	return err;
    305}
    306
    307/* Set network device mode
    308 *
    309 * Maybe we should leave this function empty, because the device
    310 * set mode variable with open command.
    311 */
    312static int usb_8dev_set_mode(struct net_device *netdev, enum can_mode mode)
    313{
    314	struct usb_8dev_priv *priv = netdev_priv(netdev);
    315	int err = 0;
    316
    317	switch (mode) {
    318	case CAN_MODE_START:
    319		err = usb_8dev_cmd_open(priv);
    320		if (err)
    321			netdev_warn(netdev, "couldn't start device");
    322		break;
    323
    324	default:
    325		return -EOPNOTSUPP;
    326	}
    327
    328	return err;
    329}
    330
    331/* Read error/status frames */
    332static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv,
    333				struct usb_8dev_rx_msg *msg)
    334{
    335	struct can_frame *cf;
    336	struct sk_buff *skb;
    337	struct net_device_stats *stats = &priv->netdev->stats;
    338
    339	/* Error message:
    340	 * byte 0: Status
    341	 * byte 1: bit   7: Receive Passive
    342	 * byte 1: bit 0-6: Receive Error Counter
    343	 * byte 2: Transmit Error Counter
    344	 * byte 3: Always 0 (maybe reserved for future use)
    345	 */
    346
    347	u8 state = msg->data[0];
    348	u8 rxerr = msg->data[1] & USB_8DEV_RP_MASK;
    349	u8 txerr = msg->data[2];
    350	int rx_errors = 0;
    351	int tx_errors = 0;
    352
    353	skb = alloc_can_err_skb(priv->netdev, &cf);
    354	if (!skb)
    355		return;
    356
    357	switch (state) {
    358	case USB_8DEV_STATUSMSG_OK:
    359		priv->can.state = CAN_STATE_ERROR_ACTIVE;
    360		cf->can_id |= CAN_ERR_PROT;
    361		cf->data[2] = CAN_ERR_PROT_ACTIVE;
    362		break;
    363	case USB_8DEV_STATUSMSG_BUSOFF:
    364		priv->can.state = CAN_STATE_BUS_OFF;
    365		cf->can_id |= CAN_ERR_BUSOFF;
    366		priv->can.can_stats.bus_off++;
    367		can_bus_off(priv->netdev);
    368		break;
    369	case USB_8DEV_STATUSMSG_OVERRUN:
    370	case USB_8DEV_STATUSMSG_BUSLIGHT:
    371	case USB_8DEV_STATUSMSG_BUSHEAVY:
    372		cf->can_id |= CAN_ERR_CRTL;
    373		break;
    374	default:
    375		priv->can.state = CAN_STATE_ERROR_WARNING;
    376		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
    377		priv->can.can_stats.bus_error++;
    378		break;
    379	}
    380
    381	switch (state) {
    382	case USB_8DEV_STATUSMSG_OK:
    383	case USB_8DEV_STATUSMSG_BUSOFF:
    384		break;
    385	case USB_8DEV_STATUSMSG_ACK:
    386		cf->can_id |= CAN_ERR_ACK;
    387		tx_errors = 1;
    388		break;
    389	case USB_8DEV_STATUSMSG_CRC:
    390		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
    391		rx_errors = 1;
    392		break;
    393	case USB_8DEV_STATUSMSG_BIT0:
    394		cf->data[2] |= CAN_ERR_PROT_BIT0;
    395		tx_errors = 1;
    396		break;
    397	case USB_8DEV_STATUSMSG_BIT1:
    398		cf->data[2] |= CAN_ERR_PROT_BIT1;
    399		tx_errors = 1;
    400		break;
    401	case USB_8DEV_STATUSMSG_FORM:
    402		cf->data[2] |= CAN_ERR_PROT_FORM;
    403		rx_errors = 1;
    404		break;
    405	case USB_8DEV_STATUSMSG_STUFF:
    406		cf->data[2] |= CAN_ERR_PROT_STUFF;
    407		rx_errors = 1;
    408		break;
    409	case USB_8DEV_STATUSMSG_OVERRUN:
    410		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
    411		stats->rx_over_errors++;
    412		rx_errors = 1;
    413		break;
    414	case USB_8DEV_STATUSMSG_BUSLIGHT:
    415		priv->can.state = CAN_STATE_ERROR_WARNING;
    416		cf->data[1] = (txerr > rxerr) ?
    417			CAN_ERR_CRTL_TX_WARNING :
    418			CAN_ERR_CRTL_RX_WARNING;
    419		priv->can.can_stats.error_warning++;
    420		break;
    421	case USB_8DEV_STATUSMSG_BUSHEAVY:
    422		priv->can.state = CAN_STATE_ERROR_PASSIVE;
    423		cf->data[1] = (txerr > rxerr) ?
    424			CAN_ERR_CRTL_TX_PASSIVE :
    425			CAN_ERR_CRTL_RX_PASSIVE;
    426		priv->can.can_stats.error_passive++;
    427		break;
    428	default:
    429		netdev_warn(priv->netdev,
    430			    "Unknown status/error message (%d)\n", state);
    431		break;
    432	}
    433
    434	if (tx_errors) {
    435		cf->data[2] |= CAN_ERR_PROT_TX;
    436		stats->tx_errors++;
    437	}
    438
    439	if (rx_errors)
    440		stats->rx_errors++;
    441
    442	cf->data[6] = txerr;
    443	cf->data[7] = rxerr;
    444
    445	priv->bec.txerr = txerr;
    446	priv->bec.rxerr = rxerr;
    447
    448	netif_rx(skb);
    449}
    450
    451/* Read data and status frames */
    452static void usb_8dev_rx_can_msg(struct usb_8dev_priv *priv,
    453				struct usb_8dev_rx_msg *msg)
    454{
    455	struct can_frame *cf;
    456	struct sk_buff *skb;
    457	struct net_device_stats *stats = &priv->netdev->stats;
    458
    459	if (msg->type == USB_8DEV_TYPE_ERROR_FRAME &&
    460		   msg->flags == USB_8DEV_ERR_FLAG) {
    461		usb_8dev_rx_err_msg(priv, msg);
    462	} else if (msg->type == USB_8DEV_TYPE_CAN_FRAME) {
    463		skb = alloc_can_skb(priv->netdev, &cf);
    464		if (!skb)
    465			return;
    466
    467		cf->can_id = be32_to_cpu(msg->id);
    468		can_frame_set_cc_len(cf, msg->dlc & 0xF, priv->can.ctrlmode);
    469
    470		if (msg->flags & USB_8DEV_EXTID)
    471			cf->can_id |= CAN_EFF_FLAG;
    472
    473		if (msg->flags & USB_8DEV_RTR) {
    474			cf->can_id |= CAN_RTR_FLAG;
    475		} else {
    476			memcpy(cf->data, msg->data, cf->len);
    477			stats->rx_bytes += cf->len;
    478		}
    479		stats->rx_packets++;
    480
    481		netif_rx(skb);
    482	} else {
    483		netdev_warn(priv->netdev, "frame type %d unknown",
    484			 msg->type);
    485	}
    486
    487}
    488
    489/* Callback for reading data from device
    490 *
    491 * Check urb status, call read function and resubmit urb read operation.
    492 */
    493static void usb_8dev_read_bulk_callback(struct urb *urb)
    494{
    495	struct usb_8dev_priv *priv = urb->context;
    496	struct net_device *netdev;
    497	int retval;
    498	int pos = 0;
    499
    500	netdev = priv->netdev;
    501
    502	if (!netif_device_present(netdev))
    503		return;
    504
    505	switch (urb->status) {
    506	case 0: /* success */
    507		break;
    508
    509	case -ENOENT:
    510	case -EPIPE:
    511	case -EPROTO:
    512	case -ESHUTDOWN:
    513		return;
    514
    515	default:
    516		netdev_info(netdev, "Rx URB aborted (%d)\n",
    517			 urb->status);
    518		goto resubmit_urb;
    519	}
    520
    521	while (pos < urb->actual_length) {
    522		struct usb_8dev_rx_msg *msg;
    523
    524		if (pos + sizeof(struct usb_8dev_rx_msg) > urb->actual_length) {
    525			netdev_err(priv->netdev, "format error\n");
    526			break;
    527		}
    528
    529		msg = (struct usb_8dev_rx_msg *)(urb->transfer_buffer + pos);
    530		usb_8dev_rx_can_msg(priv, msg);
    531
    532		pos += sizeof(struct usb_8dev_rx_msg);
    533	}
    534
    535resubmit_urb:
    536	usb_fill_bulk_urb(urb, priv->udev,
    537			  usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX),
    538			  urb->transfer_buffer, RX_BUFFER_SIZE,
    539			  usb_8dev_read_bulk_callback, priv);
    540
    541	retval = usb_submit_urb(urb, GFP_ATOMIC);
    542
    543	if (retval == -ENODEV)
    544		netif_device_detach(netdev);
    545	else if (retval)
    546		netdev_err(netdev,
    547			"failed resubmitting read bulk urb: %d\n", retval);
    548}
    549
    550/* Callback handler for write operations
    551 *
    552 * Free allocated buffers, check transmit status and
    553 * calculate statistic.
    554 */
    555static void usb_8dev_write_bulk_callback(struct urb *urb)
    556{
    557	struct usb_8dev_tx_urb_context *context = urb->context;
    558	struct usb_8dev_priv *priv;
    559	struct net_device *netdev;
    560
    561	BUG_ON(!context);
    562
    563	priv = context->priv;
    564	netdev = priv->netdev;
    565
    566	/* free up our allocated buffer */
    567	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
    568			  urb->transfer_buffer, urb->transfer_dma);
    569
    570	atomic_dec(&priv->active_tx_urbs);
    571
    572	if (!netif_device_present(netdev))
    573		return;
    574
    575	if (urb->status)
    576		netdev_info(netdev, "Tx URB aborted (%d)\n",
    577			 urb->status);
    578
    579	netdev->stats.tx_packets++;
    580	netdev->stats.tx_bytes += can_get_echo_skb(netdev, context->echo_index, NULL);
    581
    582	/* Release context */
    583	context->echo_index = MAX_TX_URBS;
    584
    585	netif_wake_queue(netdev);
    586}
    587
    588/* Send data to device */
    589static netdev_tx_t usb_8dev_start_xmit(struct sk_buff *skb,
    590				      struct net_device *netdev)
    591{
    592	struct usb_8dev_priv *priv = netdev_priv(netdev);
    593	struct net_device_stats *stats = &netdev->stats;
    594	struct can_frame *cf = (struct can_frame *) skb->data;
    595	struct usb_8dev_tx_msg *msg;
    596	struct urb *urb;
    597	struct usb_8dev_tx_urb_context *context = NULL;
    598	u8 *buf;
    599	int i, err;
    600	size_t size = sizeof(struct usb_8dev_tx_msg);
    601
    602	if (can_dropped_invalid_skb(netdev, skb))
    603		return NETDEV_TX_OK;
    604
    605	/* create a URB, and a buffer for it, and copy the data to the URB */
    606	urb = usb_alloc_urb(0, GFP_ATOMIC);
    607	if (!urb)
    608		goto nomem;
    609
    610	buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC,
    611				 &urb->transfer_dma);
    612	if (!buf) {
    613		netdev_err(netdev, "No memory left for USB buffer\n");
    614		goto nomembuf;
    615	}
    616
    617	memset(buf, 0, size);
    618
    619	msg = (struct usb_8dev_tx_msg *)buf;
    620	msg->begin = USB_8DEV_DATA_START;
    621	msg->flags = 0x00;
    622
    623	if (cf->can_id & CAN_RTR_FLAG)
    624		msg->flags |= USB_8DEV_RTR;
    625
    626	if (cf->can_id & CAN_EFF_FLAG)
    627		msg->flags |= USB_8DEV_EXTID;
    628
    629	msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK);
    630	msg->dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
    631	memcpy(msg->data, cf->data, cf->len);
    632	msg->end = USB_8DEV_DATA_END;
    633
    634	for (i = 0; i < MAX_TX_URBS; i++) {
    635		if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
    636			context = &priv->tx_contexts[i];
    637			break;
    638		}
    639	}
    640
    641	/* May never happen! When this happens we'd more URBs in flight as
    642	 * allowed (MAX_TX_URBS).
    643	 */
    644	if (!context)
    645		goto nofreecontext;
    646
    647	context->priv = priv;
    648	context->echo_index = i;
    649
    650	usb_fill_bulk_urb(urb, priv->udev,
    651			  usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_TX),
    652			  buf, size, usb_8dev_write_bulk_callback, context);
    653	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
    654	usb_anchor_urb(urb, &priv->tx_submitted);
    655
    656	can_put_echo_skb(skb, netdev, context->echo_index, 0);
    657
    658	atomic_inc(&priv->active_tx_urbs);
    659
    660	err = usb_submit_urb(urb, GFP_ATOMIC);
    661	if (unlikely(err)) {
    662		can_free_echo_skb(netdev, context->echo_index, NULL);
    663
    664		usb_unanchor_urb(urb);
    665		usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
    666
    667		atomic_dec(&priv->active_tx_urbs);
    668
    669		if (err == -ENODEV)
    670			netif_device_detach(netdev);
    671		else
    672			netdev_warn(netdev, "failed tx_urb %d\n", err);
    673		stats->tx_dropped++;
    674	} else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
    675		/* Slow down tx path */
    676		netif_stop_queue(netdev);
    677
    678	/* Release our reference to this URB, the USB core will eventually free
    679	 * it entirely.
    680	 */
    681	usb_free_urb(urb);
    682
    683	return NETDEV_TX_OK;
    684
    685nofreecontext:
    686	usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
    687	usb_free_urb(urb);
    688
    689	netdev_warn(netdev, "couldn't find free context");
    690
    691	return NETDEV_TX_BUSY;
    692
    693nomembuf:
    694	usb_free_urb(urb);
    695
    696nomem:
    697	dev_kfree_skb(skb);
    698	stats->tx_dropped++;
    699
    700	return NETDEV_TX_OK;
    701}
    702
    703static int usb_8dev_get_berr_counter(const struct net_device *netdev,
    704				     struct can_berr_counter *bec)
    705{
    706	struct usb_8dev_priv *priv = netdev_priv(netdev);
    707
    708	bec->txerr = priv->bec.txerr;
    709	bec->rxerr = priv->bec.rxerr;
    710
    711	return 0;
    712}
    713
    714/* Start USB device */
    715static int usb_8dev_start(struct usb_8dev_priv *priv)
    716{
    717	struct net_device *netdev = priv->netdev;
    718	int err, i;
    719
    720	for (i = 0; i < MAX_RX_URBS; i++) {
    721		struct urb *urb = NULL;
    722		u8 *buf;
    723		dma_addr_t buf_dma;
    724
    725		/* create a URB, and a buffer for it */
    726		urb = usb_alloc_urb(0, GFP_KERNEL);
    727		if (!urb) {
    728			err = -ENOMEM;
    729			break;
    730		}
    731
    732		buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
    733					 &buf_dma);
    734		if (!buf) {
    735			netdev_err(netdev, "No memory left for USB buffer\n");
    736			usb_free_urb(urb);
    737			err = -ENOMEM;
    738			break;
    739		}
    740
    741		urb->transfer_dma = buf_dma;
    742
    743		usb_fill_bulk_urb(urb, priv->udev,
    744				  usb_rcvbulkpipe(priv->udev,
    745						  USB_8DEV_ENDP_DATA_RX),
    746				  buf, RX_BUFFER_SIZE,
    747				  usb_8dev_read_bulk_callback, priv);
    748		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
    749		usb_anchor_urb(urb, &priv->rx_submitted);
    750
    751		err = usb_submit_urb(urb, GFP_KERNEL);
    752		if (err) {
    753			usb_unanchor_urb(urb);
    754			usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf,
    755					  urb->transfer_dma);
    756			usb_free_urb(urb);
    757			break;
    758		}
    759
    760		priv->rxbuf[i] = buf;
    761		priv->rxbuf_dma[i] = buf_dma;
    762
    763		/* Drop reference, USB core will take care of freeing it */
    764		usb_free_urb(urb);
    765	}
    766
    767	/* Did we submit any URBs */
    768	if (i == 0) {
    769		netdev_warn(netdev, "couldn't setup read URBs\n");
    770		return err;
    771	}
    772
    773	/* Warn if we've couldn't transmit all the URBs */
    774	if (i < MAX_RX_URBS)
    775		netdev_warn(netdev, "rx performance may be slow\n");
    776
    777	err = usb_8dev_cmd_open(priv);
    778	if (err)
    779		goto failed;
    780
    781	priv->can.state = CAN_STATE_ERROR_ACTIVE;
    782
    783	return 0;
    784
    785failed:
    786	if (err == -ENODEV)
    787		netif_device_detach(priv->netdev);
    788
    789	netdev_warn(netdev, "couldn't submit control: %d\n", err);
    790
    791	return err;
    792}
    793
    794/* Open USB device */
    795static int usb_8dev_open(struct net_device *netdev)
    796{
    797	struct usb_8dev_priv *priv = netdev_priv(netdev);
    798	int err;
    799
    800	/* common open */
    801	err = open_candev(netdev);
    802	if (err)
    803		return err;
    804
    805	/* finally start device */
    806	err = usb_8dev_start(priv);
    807	if (err) {
    808		if (err == -ENODEV)
    809			netif_device_detach(priv->netdev);
    810
    811		netdev_warn(netdev, "couldn't start device: %d\n",
    812			 err);
    813
    814		close_candev(netdev);
    815
    816		return err;
    817	}
    818
    819	netif_start_queue(netdev);
    820
    821	return 0;
    822}
    823
    824static void unlink_all_urbs(struct usb_8dev_priv *priv)
    825{
    826	int i;
    827
    828	usb_kill_anchored_urbs(&priv->rx_submitted);
    829
    830	for (i = 0; i < MAX_RX_URBS; ++i)
    831		usb_free_coherent(priv->udev, RX_BUFFER_SIZE,
    832				  priv->rxbuf[i], priv->rxbuf_dma[i]);
    833
    834	usb_kill_anchored_urbs(&priv->tx_submitted);
    835	atomic_set(&priv->active_tx_urbs, 0);
    836
    837	for (i = 0; i < MAX_TX_URBS; i++)
    838		priv->tx_contexts[i].echo_index = MAX_TX_URBS;
    839}
    840
    841/* Close USB device */
    842static int usb_8dev_close(struct net_device *netdev)
    843{
    844	struct usb_8dev_priv *priv = netdev_priv(netdev);
    845	int err = 0;
    846
    847	/* Send CLOSE command to CAN controller */
    848	err = usb_8dev_cmd_close(priv);
    849	if (err)
    850		netdev_warn(netdev, "couldn't stop device");
    851
    852	priv->can.state = CAN_STATE_STOPPED;
    853
    854	netif_stop_queue(netdev);
    855
    856	/* Stop polling */
    857	unlink_all_urbs(priv);
    858
    859	close_candev(netdev);
    860
    861	return err;
    862}
    863
    864static const struct net_device_ops usb_8dev_netdev_ops = {
    865	.ndo_open = usb_8dev_open,
    866	.ndo_stop = usb_8dev_close,
    867	.ndo_start_xmit = usb_8dev_start_xmit,
    868	.ndo_change_mtu = can_change_mtu,
    869};
    870
    871static const struct can_bittiming_const usb_8dev_bittiming_const = {
    872	.name = "usb_8dev",
    873	.tseg1_min = 1,
    874	.tseg1_max = 16,
    875	.tseg2_min = 1,
    876	.tseg2_max = 8,
    877	.sjw_max = 4,
    878	.brp_min = 1,
    879	.brp_max = 1024,
    880	.brp_inc = 1,
    881};
    882
    883/* Probe USB device
    884 *
    885 * Check device and firmware.
    886 * Set supported modes and bittiming constants.
    887 * Allocate some memory.
    888 */
    889static int usb_8dev_probe(struct usb_interface *intf,
    890			 const struct usb_device_id *id)
    891{
    892	struct net_device *netdev;
    893	struct usb_8dev_priv *priv;
    894	int i, err = -ENOMEM;
    895	u32 version;
    896	char buf[18];
    897	struct usb_device *usbdev = interface_to_usbdev(intf);
    898
    899	/* product id looks strange, better we also check iProduct string */
    900	if (usb_string(usbdev, usbdev->descriptor.iProduct, buf,
    901		       sizeof(buf)) > 0 && strcmp(buf, "USB2CAN converter")) {
    902		dev_info(&usbdev->dev, "ignoring: not an USB2CAN converter\n");
    903		return -ENODEV;
    904	}
    905
    906	netdev = alloc_candev(sizeof(struct usb_8dev_priv), MAX_TX_URBS);
    907	if (!netdev) {
    908		dev_err(&intf->dev, "Couldn't alloc candev\n");
    909		return -ENOMEM;
    910	}
    911
    912	priv = netdev_priv(netdev);
    913
    914	priv->udev = usbdev;
    915	priv->netdev = netdev;
    916
    917	priv->can.state = CAN_STATE_STOPPED;
    918	priv->can.clock.freq = USB_8DEV_ABP_CLOCK;
    919	priv->can.bittiming_const = &usb_8dev_bittiming_const;
    920	priv->can.do_set_mode = usb_8dev_set_mode;
    921	priv->can.do_get_berr_counter = usb_8dev_get_berr_counter;
    922	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
    923				      CAN_CTRLMODE_LISTENONLY |
    924				      CAN_CTRLMODE_ONE_SHOT |
    925				      CAN_CTRLMODE_CC_LEN8_DLC;
    926
    927	netdev->netdev_ops = &usb_8dev_netdev_ops;
    928
    929	netdev->flags |= IFF_ECHO; /* we support local echo */
    930
    931	init_usb_anchor(&priv->rx_submitted);
    932
    933	init_usb_anchor(&priv->tx_submitted);
    934	atomic_set(&priv->active_tx_urbs, 0);
    935
    936	for (i = 0; i < MAX_TX_URBS; i++)
    937		priv->tx_contexts[i].echo_index = MAX_TX_URBS;
    938
    939	priv->cmd_msg_buffer = devm_kzalloc(&intf->dev, sizeof(struct usb_8dev_cmd_msg),
    940					    GFP_KERNEL);
    941	if (!priv->cmd_msg_buffer)
    942		goto cleanup_candev;
    943
    944	usb_set_intfdata(intf, priv);
    945
    946	SET_NETDEV_DEV(netdev, &intf->dev);
    947
    948	mutex_init(&priv->usb_8dev_cmd_lock);
    949
    950	err = register_candev(netdev);
    951	if (err) {
    952		netdev_err(netdev,
    953			"couldn't register CAN device: %d\n", err);
    954		goto cleanup_candev;
    955	}
    956
    957	err = usb_8dev_cmd_version(priv, &version);
    958	if (err) {
    959		netdev_err(netdev, "can't get firmware version\n");
    960		goto cleanup_unregister_candev;
    961	} else {
    962		netdev_info(netdev,
    963			 "firmware: %d.%d, hardware: %d.%d\n",
    964			 (version>>24) & 0xff, (version>>16) & 0xff,
    965			 (version>>8) & 0xff, version & 0xff);
    966	}
    967
    968	return 0;
    969
    970cleanup_unregister_candev:
    971	unregister_netdev(priv->netdev);
    972
    973cleanup_candev:
    974	free_candev(netdev);
    975
    976	return err;
    977
    978}
    979
    980/* Called by the usb core when driver is unloaded or device is removed */
    981static void usb_8dev_disconnect(struct usb_interface *intf)
    982{
    983	struct usb_8dev_priv *priv = usb_get_intfdata(intf);
    984
    985	usb_set_intfdata(intf, NULL);
    986
    987	if (priv) {
    988		netdev_info(priv->netdev, "device disconnected\n");
    989
    990		unregister_netdev(priv->netdev);
    991		unlink_all_urbs(priv);
    992		free_candev(priv->netdev);
    993	}
    994
    995}
    996
    997static struct usb_driver usb_8dev_driver = {
    998	.name =		"usb_8dev",
    999	.probe =	usb_8dev_probe,
   1000	.disconnect =	usb_8dev_disconnect,
   1001	.id_table =	usb_8dev_table,
   1002};
   1003
   1004module_usb_driver(usb_8dev_driver);
   1005
   1006MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
   1007MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
   1008MODULE_LICENSE("GPL v2");