Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

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