Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * CAN driver for PEAK System PCAN-USB Pro adapter
   4 * Derived from the PCAN project file driver/src/pcan_usbpro.c
   5 *
   6 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
   7 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
 
 
 
 
 
 
 
 
 
   8 */
   9#include <linux/ethtool.h>
  10#include <linux/module.h>
  11#include <linux/netdevice.h>
  12#include <linux/usb.h>
 
  13
  14#include <linux/can.h>
  15#include <linux/can/dev.h>
  16#include <linux/can/error.h>
  17
  18#include "pcan_usb_core.h"
  19#include "pcan_usb_pro.h"
  20
 
 
  21#define PCAN_USBPRO_CHANNEL_COUNT	2
  22
  23/* PCAN-USB Pro adapter internal clock (MHz) */
  24#define PCAN_USBPRO_CRYSTAL_HZ		56000000
  25
  26/* PCAN-USB Pro command timeout (ms.) */
  27#define PCAN_USBPRO_COMMAND_TIMEOUT	1000
  28
  29/* PCAN-USB Pro rx/tx buffers size */
  30#define PCAN_USBPRO_RX_BUFFER_SIZE	1024
  31#define PCAN_USBPRO_TX_BUFFER_SIZE	64
  32
  33#define PCAN_USBPRO_MSG_HEADER_LEN	4
  34
  35/* some commands responses need to be re-submitted */
  36#define PCAN_USBPRO_RSP_SUBMIT_MAX	2
  37
  38#define PCAN_USBPRO_RTR			0x01
  39#define PCAN_USBPRO_EXT			0x02
  40#define PCAN_USBPRO_SS			0x08
  41
  42#define PCAN_USBPRO_CMD_BUFFER_SIZE	512
  43
  44/* handle device specific info used by the netdevices */
  45struct pcan_usb_pro_interface {
  46	struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
  47	struct peak_time_ref time_ref;
  48	int cm_ignore_count;
  49	int dev_opened_count;
  50};
  51
  52/* device information */
  53struct pcan_usb_pro_device {
  54	struct peak_usb_device dev;
  55	struct pcan_usb_pro_interface *usb_if;
  56	u32 cached_ccbt;
  57};
  58
  59/* internal structure used to handle messages sent to bulk urb */
  60struct pcan_usb_pro_msg {
  61	u8 *rec_ptr;
  62	int rec_buffer_size;
  63	int rec_buffer_len;
  64	union {
  65		__le16 *rec_cnt_rd;
  66		__le32 *rec_cnt;
  67		u8 *rec_buffer;
  68	} u;
  69};
  70
  71/* records sizes table indexed on message id. (8-bits value) */
  72static u16 pcan_usb_pro_sizeof_rec[256] = {
  73	[PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
  74	[PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
  75	[PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
  76	[PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
  77	[PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
  78	[PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
  79	[PCAN_USBPRO_SETDEVID] = sizeof(struct pcan_usb_pro_devid),
  80	[PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
  81	[PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
  82	[PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
  83	[PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  84	[PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  85	[PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
  86	[PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
  87	[PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
  88	[PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
  89	[PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
  90};
  91
  92/*
  93 * initialize PCAN-USB Pro message data structure
  94 */
  95static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
  96			 int buffer_size)
  97{
  98	if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
  99		return NULL;
 100
 101	pm->u.rec_buffer = (u8 *)buffer_addr;
 102	pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
 103	pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
 104
 105	return pm->rec_ptr;
 106}
 107
 108static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
 109			       void *buffer_addr, int buffer_size)
 110{
 111	u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
 112
 113	if (pr) {
 114		pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
 115		*pm->u.rec_cnt = 0;
 116	}
 117	return pr;
 118}
 119
 120/*
 121 * add one record to a message being built
 122 */
 123static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, int id, ...)
 124{
 125	int len, i;
 126	u8 *pc;
 127	va_list ap;
 128
 129	va_start(ap, id);
 130
 131	pc = pm->rec_ptr + 1;
 132
 133	i = 0;
 134	switch (id) {
 135	case PCAN_USBPRO_TXMSG8:
 136		i += 4;
 137		fallthrough;
 138	case PCAN_USBPRO_TXMSG4:
 139		i += 4;
 140		fallthrough;
 141	case PCAN_USBPRO_TXMSG0:
 142		*pc++ = va_arg(ap, int);
 143		*pc++ = va_arg(ap, int);
 144		*pc++ = va_arg(ap, int);
 145		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 146		pc += 4;
 147		memcpy(pc, va_arg(ap, int *), i);
 148		pc += i;
 149		break;
 150
 151	case PCAN_USBPRO_SETBTR:
 152	case PCAN_USBPRO_GETDEVID:
 153	case PCAN_USBPRO_SETDEVID:
 154		*pc++ = va_arg(ap, int);
 155		pc += 2;
 156		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 157		pc += 4;
 158		break;
 159
 160	case PCAN_USBPRO_SETFILTR:
 161	case PCAN_USBPRO_SETBUSACT:
 162	case PCAN_USBPRO_SETSILENT:
 163		*pc++ = va_arg(ap, int);
 164		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 165		pc += 2;
 166		break;
 167
 168	case PCAN_USBPRO_SETLED:
 169		*pc++ = va_arg(ap, int);
 170		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 171		pc += 2;
 172		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 173		pc += 4;
 174		break;
 175
 176	case PCAN_USBPRO_SETTS:
 177		pc++;
 178		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 179		pc += 2;
 180		break;
 181
 182	default:
 183		pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
 184			PCAN_USB_DRIVER_NAME, __func__, id, id);
 185		pc--;
 186		break;
 187	}
 188
 189	len = pc - pm->rec_ptr;
 190	if (len > 0) {
 191		le32_add_cpu(pm->u.rec_cnt, 1);
 192		*pm->rec_ptr = id;
 193
 194		pm->rec_ptr = pc;
 195		pm->rec_buffer_len += len;
 196	}
 197
 198	va_end(ap);
 199
 200	return len;
 201}
 202
 203/*
 204 * send PCAN-USB Pro command synchronously
 205 */
 206static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
 207				 struct pcan_usb_pro_msg *pum)
 208{
 209	int actual_length;
 210	int err;
 211
 212	/* usb device unregistered? */
 213	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 214		return 0;
 215
 216	err = usb_bulk_msg(dev->udev,
 217		usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 218		pum->u.rec_buffer, pum->rec_buffer_len,
 219		&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
 220	if (err)
 221		netdev_err(dev->netdev, "sending command failure: %d\n", err);
 222
 223	return err;
 224}
 225
 226/*
 227 * wait for PCAN-USB Pro command response
 228 */
 229static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
 230				 struct pcan_usb_pro_msg *pum)
 231{
 232	u8 req_data_type, req_channel;
 233	int actual_length;
 234	int i, err = 0;
 235
 236	/* usb device unregistered? */
 237	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 238		return 0;
 239
 240	req_data_type = pum->u.rec_buffer[4];
 241	req_channel = pum->u.rec_buffer[5];
 242
 243	*pum->u.rec_cnt = 0;
 244	for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
 245		struct pcan_usb_pro_msg rsp;
 246		union pcan_usb_pro_rec *pr;
 247		u32 r, rec_cnt;
 248		u16 rec_len;
 249		u8 *pc;
 250
 251		err = usb_bulk_msg(dev->udev,
 252			usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
 253			pum->u.rec_buffer, pum->rec_buffer_len,
 254			&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
 255		if (err) {
 256			netdev_err(dev->netdev, "waiting rsp error %d\n", err);
 257			break;
 258		}
 259
 260		if (actual_length == 0)
 261			continue;
 262
 263		err = -EBADMSG;
 264		if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
 265			netdev_err(dev->netdev,
 266				   "got abnormal too small rsp (len=%d)\n",
 267				   actual_length);
 268			break;
 269		}
 270
 271		pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
 272			actual_length);
 273
 274		rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
 275
 276		/* loop on records stored into message */
 277		for (r = 0; r < rec_cnt; r++) {
 278			pr = (union pcan_usb_pro_rec *)pc;
 279			rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
 280			if (!rec_len) {
 281				netdev_err(dev->netdev,
 282					   "got unprocessed record in msg\n");
 283				pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
 284					      actual_length);
 285				break;
 286			}
 287
 288			/* check if response corresponds to request */
 289			if (pr->data_type != req_data_type)
 290				netdev_err(dev->netdev,
 291					   "got unwanted rsp %xh: ignored\n",
 292					   pr->data_type);
 293
 294			/* check if channel in response corresponds too */
 295			else if ((req_channel != 0xff) &&
 296				(pr->bus_act.channel != req_channel))
 297				netdev_err(dev->netdev,
 298					"got rsp %xh but on chan%u: ignored\n",
 299					req_data_type, pr->bus_act.channel);
 300
 301			/* got the response */
 302			else
 303				return 0;
 304
 305			/* otherwise, go on with next record in message */
 306			pc += rec_len;
 307		}
 308	}
 309
 310	return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
 311}
 312
 313int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
 314			  int req_value, void *req_addr, int req_size)
 315{
 316	int err;
 317	u8 req_type;
 318	unsigned int p;
 319
 320	/* usb device unregistered? */
 321	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 322		return 0;
 323
 324	req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
 325
 326	switch (req_id) {
 327	case PCAN_USBPRO_REQ_FCT:
 328		p = usb_sndctrlpipe(dev->udev, 0);
 329		break;
 330
 331	default:
 332		p = usb_rcvctrlpipe(dev->udev, 0);
 333		req_type |= USB_DIR_IN;
 334		memset(req_addr, '\0', req_size);
 335		break;
 336	}
 337
 338	err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
 339			      req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
 340	if (err < 0) {
 341		netdev_info(dev->netdev,
 342			    "unable to request usb[type=%d value=%d] err=%d\n",
 343			    req_id, req_value, err);
 344		return err;
 345	}
 346
 347	return 0;
 348}
 349
 350static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
 351{
 352	struct pcan_usb_pro_msg um;
 353
 354	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 355	pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
 356
 357	return pcan_usb_pro_send_cmd(dev, &um);
 358}
 359
 360static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
 361{
 362	struct pcan_usb_pro_device *pdev =
 363			container_of(dev, struct pcan_usb_pro_device, dev);
 364	struct pcan_usb_pro_msg um;
 365
 366	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 367	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
 368
 369	/* cache the CCBT value to reuse it before next buson */
 370	pdev->cached_ccbt = ccbt;
 371
 372	return pcan_usb_pro_send_cmd(dev, &um);
 373}
 374
 375static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
 376{
 377	struct pcan_usb_pro_msg um;
 378
 379	/* if bus=on, be sure the bitrate being set before! */
 380	if (onoff) {
 381		struct pcan_usb_pro_device *pdev =
 382			     container_of(dev, struct pcan_usb_pro_device, dev);
 383
 384		pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
 385	}
 386
 387	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 388	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
 389
 390	return pcan_usb_pro_send_cmd(dev, &um);
 391}
 392
 393static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
 394{
 395	struct pcan_usb_pro_msg um;
 396
 397	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 398	pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
 399
 400	return pcan_usb_pro_send_cmd(dev, &um);
 401}
 402
 403static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
 404{
 405	struct pcan_usb_pro_msg um;
 406
 407	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 408	pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
 409
 410	return pcan_usb_pro_send_cmd(dev, &um);
 411}
 412
 413static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
 414				u32 timeout)
 415{
 416	struct pcan_usb_pro_msg um;
 417
 418	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 419	pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
 420
 421	return pcan_usb_pro_send_cmd(dev, &um);
 422}
 423
 424static int pcan_usb_pro_get_can_channel_id(struct peak_usb_device *dev,
 425					   u32 *can_ch_id)
 426{
 427	struct pcan_usb_pro_devid *pdn;
 428	struct pcan_usb_pro_msg um;
 429	int err;
 430	u8 *pc;
 431
 432	pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 433	pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
 434
 435	err =  pcan_usb_pro_send_cmd(dev, &um);
 436	if (err)
 437		return err;
 438
 439	err = pcan_usb_pro_wait_rsp(dev, &um);
 440	if (err)
 441		return err;
 442
 443	pdn = (struct pcan_usb_pro_devid *)pc;
 444	*can_ch_id = le32_to_cpu(pdn->dev_num);
 
 445
 446	return err;
 447}
 448
 449static int pcan_usb_pro_set_can_channel_id(struct peak_usb_device *dev,
 450					   u32 can_ch_id)
 451{
 452	struct pcan_usb_pro_msg um;
 453
 454	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 455	pcan_msg_add_rec(&um, PCAN_USBPRO_SETDEVID, dev->ctrl_idx,
 456			 can_ch_id);
 457
 458	return pcan_usb_pro_send_cmd(dev, &um);
 459}
 460
 461static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
 462				      struct can_bittiming *bt)
 463{
 464	u32 ccbt;
 465
 466	ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
 467	ccbt |= (bt->sjw - 1) << 24;
 468	ccbt |= (bt->phase_seg2 - 1) << 20;
 469	ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
 470	ccbt |= bt->brp - 1;
 471
 472	netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
 473
 474	return pcan_usb_pro_set_bitrate(dev, ccbt);
 475}
 476
 477void pcan_usb_pro_restart_complete(struct urb *urb)
 478{
 479	/* can delete usb resources */
 480	peak_usb_async_complete(urb);
 481
 482	/* notify candev and netdev */
 483	peak_usb_restart_complete(urb->context);
 484}
 485
 486/*
 487 * handle restart but in asynchronously way
 488 */
 489static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
 490				      struct urb *urb, u8 *buf)
 491{
 492	struct pcan_usb_pro_msg um;
 493
 494	pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
 495	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
 496
 497	usb_fill_bulk_urb(urb, dev->udev,
 498			usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 499			buf, PCAN_USB_MAX_CMD_LEN,
 500			pcan_usb_pro_restart_complete, dev);
 501
 502	return usb_submit_urb(urb, GFP_ATOMIC);
 503}
 504
 505static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
 506{
 507	u8 *buffer;
 508	int err;
 509
 510	buffer = kzalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
 511	if (!buffer)
 512		return -ENOMEM;
 513
 514	buffer[0] = 0;
 515	buffer[1] = !!loaded;
 516
 517	err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
 518				    PCAN_USBPRO_FCT_DRVLD, buffer,
 519				    PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
 520	kfree(buffer);
 521
 522	return err;
 523}
 524
 525static inline
 526struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
 527{
 528	struct pcan_usb_pro_device *pdev =
 529			container_of(dev, struct pcan_usb_pro_device, dev);
 530	return pdev->usb_if;
 531}
 532
 533static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
 534				      struct pcan_usb_pro_rxmsg *rx)
 535{
 536	const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
 537	struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
 538	struct net_device *netdev = dev->netdev;
 539	struct can_frame *can_frame;
 540	struct sk_buff *skb;
 
 541	struct skb_shared_hwtstamps *hwts;
 542
 543	skb = alloc_can_skb(netdev, &can_frame);
 544	if (!skb)
 545		return -ENOMEM;
 546
 547	can_frame->can_id = le32_to_cpu(rx->id);
 548	can_frame->len = rx->len & 0x0f;
 549
 550	if (rx->flags & PCAN_USBPRO_EXT)
 551		can_frame->can_id |= CAN_EFF_FLAG;
 552
 553	if (rx->flags & PCAN_USBPRO_RTR) {
 554		can_frame->can_id |= CAN_RTR_FLAG;
 555	} else {
 556		memcpy(can_frame->data, rx->data, can_frame->len);
 557
 558		netdev->stats.rx_bytes += can_frame->len;
 559	}
 560	netdev->stats.rx_packets++;
 561
 
 562	hwts = skb_hwtstamps(skb);
 563	peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(rx->ts32),
 564			     &hwts->hwtstamp);
 565
 
 
 566	netif_rx(skb);
 567
 568	return 0;
 569}
 570
 571static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
 572				     struct pcan_usb_pro_rxstatus *er)
 573{
 574	const u16 raw_status = le16_to_cpu(er->status);
 575	const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
 576	struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
 577	struct net_device *netdev = dev->netdev;
 578	struct can_frame *can_frame;
 579	enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
 580	u8 err_mask = 0;
 581	struct sk_buff *skb;
 
 582	struct skb_shared_hwtstamps *hwts;
 583
 584	/* nothing should be sent while in BUS_OFF state */
 585	if (dev->can.state == CAN_STATE_BUS_OFF)
 586		return 0;
 587
 588	if (!raw_status) {
 589		/* no error bit (back to active state) */
 590		dev->can.state = CAN_STATE_ERROR_ACTIVE;
 591		return 0;
 592	}
 593
 594	if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
 595			  PCAN_USBPRO_STATUS_QOVERRUN)) {
 596		/* trick to bypass next comparison and process other errors */
 597		new_state = CAN_STATE_MAX;
 598	}
 599
 600	if (raw_status & PCAN_USBPRO_STATUS_BUS) {
 601		new_state = CAN_STATE_BUS_OFF;
 602	} else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
 603		u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
 604		u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
 605
 606		if (rx_err_cnt > 127)
 607			err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
 608		else if (rx_err_cnt > 96)
 609			err_mask |= CAN_ERR_CRTL_RX_WARNING;
 610
 611		if (tx_err_cnt > 127)
 612			err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
 613		else if (tx_err_cnt > 96)
 614			err_mask |= CAN_ERR_CRTL_TX_WARNING;
 615
 616		if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
 617				CAN_ERR_CRTL_TX_WARNING))
 618			new_state = CAN_STATE_ERROR_WARNING;
 619		else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
 620				     CAN_ERR_CRTL_TX_PASSIVE))
 621			new_state = CAN_STATE_ERROR_PASSIVE;
 622	}
 623
 624	/* donot post any error if current state didn't change */
 625	if (dev->can.state == new_state)
 626		return 0;
 627
 628	/* allocate an skb to store the error frame */
 629	skb = alloc_can_err_skb(netdev, &can_frame);
 630	if (!skb)
 631		return -ENOMEM;
 632
 633	switch (new_state) {
 634	case CAN_STATE_BUS_OFF:
 635		can_frame->can_id |= CAN_ERR_BUSOFF;
 636		dev->can.can_stats.bus_off++;
 637		can_bus_off(netdev);
 638		break;
 639
 640	case CAN_STATE_ERROR_PASSIVE:
 641		can_frame->can_id |= CAN_ERR_CRTL;
 642		can_frame->data[1] |= err_mask;
 643		dev->can.can_stats.error_passive++;
 644		break;
 645
 646	case CAN_STATE_ERROR_WARNING:
 647		can_frame->can_id |= CAN_ERR_CRTL;
 648		can_frame->data[1] |= err_mask;
 649		dev->can.can_stats.error_warning++;
 650		break;
 651
 652	case CAN_STATE_ERROR_ACTIVE:
 653		break;
 654
 655	default:
 656		/* CAN_STATE_MAX (trick to handle other errors) */
 657		if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
 658			can_frame->can_id |= CAN_ERR_PROT;
 659			can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
 660			netdev->stats.rx_over_errors++;
 661			netdev->stats.rx_errors++;
 662		}
 663
 664		if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
 665			can_frame->can_id |= CAN_ERR_CRTL;
 666			can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
 667			netdev->stats.rx_over_errors++;
 668			netdev->stats.rx_errors++;
 669		}
 670
 671		new_state = CAN_STATE_ERROR_ACTIVE;
 672		break;
 673	}
 674
 675	dev->can.state = new_state;
 676
 
 677	hwts = skb_hwtstamps(skb);
 678	peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(er->ts32), &hwts->hwtstamp);
 
 
 679	netif_rx(skb);
 680
 681	return 0;
 682}
 683
 684static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
 685				   struct pcan_usb_pro_rxts *ts)
 686{
 687	/* should wait until clock is stabilized */
 688	if (usb_if->cm_ignore_count > 0)
 689		usb_if->cm_ignore_count--;
 690	else
 691		peak_usb_set_ts_now(&usb_if->time_ref,
 692				    le32_to_cpu(ts->ts64[1]));
 693}
 694
 695/*
 696 * callback for bulk IN urb
 697 */
 698static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
 699{
 700	struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
 701	struct net_device *netdev = dev->netdev;
 702	struct pcan_usb_pro_msg usb_msg;
 703	u8 *rec_ptr, *msg_end;
 704	u16 rec_cnt;
 705	int err = 0;
 706
 707	rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
 708					urb->actual_length);
 709	if (!rec_ptr) {
 710		netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
 711		return -EINVAL;
 712	}
 713
 714	/* loop reading all the records from the incoming message */
 715	msg_end = urb->transfer_buffer + urb->actual_length;
 716	rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
 717	for (; rec_cnt > 0; rec_cnt--) {
 718		union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
 719		u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
 720
 721		if (!sizeof_rec) {
 722			netdev_err(netdev,
 723				   "got unsupported rec in usb msg:\n");
 724			err = -ENOTSUPP;
 725			break;
 726		}
 727
 728		/* check if the record goes out of current packet */
 729		if (rec_ptr + sizeof_rec > msg_end) {
 730			netdev_err(netdev,
 731				"got frag rec: should inc usb rx buf size\n");
 732			err = -EBADMSG;
 733			break;
 734		}
 735
 736		switch (pr->data_type) {
 737		case PCAN_USBPRO_RXMSG8:
 738		case PCAN_USBPRO_RXMSG4:
 739		case PCAN_USBPRO_RXMSG0:
 740		case PCAN_USBPRO_RXRTR:
 741			err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
 742			if (err < 0)
 743				goto fail;
 744			break;
 745
 746		case PCAN_USBPRO_RXSTATUS:
 747			err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
 748			if (err < 0)
 749				goto fail;
 750			break;
 751
 752		case PCAN_USBPRO_RXTS:
 753			pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
 754			break;
 755
 756		default:
 757			netdev_err(netdev,
 758				   "unhandled rec type 0x%02x (%d): ignored\n",
 759				   pr->data_type, pr->data_type);
 760			break;
 761		}
 762
 763		rec_ptr += sizeof_rec;
 764	}
 765
 766fail:
 767	if (err)
 768		pcan_dump_mem("received msg",
 769			      urb->transfer_buffer, urb->actual_length);
 770
 771	return err;
 772}
 773
 774static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
 775				   struct sk_buff *skb, u8 *obuf, size_t *size)
 776{
 777	struct can_frame *cf = (struct can_frame *)skb->data;
 778	u8 data_type, len, flags;
 779	struct pcan_usb_pro_msg usb_msg;
 780
 781	pcan_msg_init_empty(&usb_msg, obuf, *size);
 782
 783	if ((cf->can_id & CAN_RTR_FLAG) || (cf->len == 0))
 784		data_type = PCAN_USBPRO_TXMSG0;
 785	else if (cf->len <= 4)
 786		data_type = PCAN_USBPRO_TXMSG4;
 787	else
 788		data_type = PCAN_USBPRO_TXMSG8;
 789
 790	len = (dev->ctrl_idx << 4) | (cf->len & 0x0f);
 791
 792	flags = 0;
 793	if (cf->can_id & CAN_EFF_FLAG)
 794		flags |= PCAN_USBPRO_EXT;
 795	if (cf->can_id & CAN_RTR_FLAG)
 796		flags |= PCAN_USBPRO_RTR;
 797
 798	/* Single-Shot frame */
 799	if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
 800		flags |= PCAN_USBPRO_SS;
 801
 802	pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
 803			 cf->data);
 804
 805	*size = usb_msg.rec_buffer_len;
 806
 807	return 0;
 808}
 809
 810static int pcan_usb_pro_start(struct peak_usb_device *dev)
 811{
 812	struct pcan_usb_pro_device *pdev =
 813			container_of(dev, struct pcan_usb_pro_device, dev);
 814	int err;
 815
 816	err = pcan_usb_pro_set_silent(dev,
 817				dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
 818	if (err)
 819		return err;
 820
 821	/* filter mode: 0-> All OFF; 1->bypass */
 822	err = pcan_usb_pro_set_filter(dev, 1);
 823	if (err)
 824		return err;
 825
 826	/* opening first device: */
 827	if (pdev->usb_if->dev_opened_count == 0) {
 828		/* reset time_ref */
 829		peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
 830
 831		/* ask device to send ts messages */
 832		err = pcan_usb_pro_set_ts(dev, 1);
 833	}
 834
 835	pdev->usb_if->dev_opened_count++;
 836
 837	return err;
 838}
 839
 840/*
 841 * stop interface
 842 * (last chance before set bus off)
 843 */
 844static int pcan_usb_pro_stop(struct peak_usb_device *dev)
 845{
 846	struct pcan_usb_pro_device *pdev =
 847			container_of(dev, struct pcan_usb_pro_device, dev);
 848
 849	/* turn off ts msgs for that interface if no other dev opened */
 850	if (pdev->usb_if->dev_opened_count == 1)
 851		pcan_usb_pro_set_ts(dev, 0);
 852
 853	pdev->usb_if->dev_opened_count--;
 854
 855	return 0;
 856}
 857
 858/*
 859 * called when probing to initialize a device object.
 860 */
 861static int pcan_usb_pro_init(struct peak_usb_device *dev)
 862{
 863	struct pcan_usb_pro_device *pdev =
 864			container_of(dev, struct pcan_usb_pro_device, dev);
 865	struct pcan_usb_pro_interface *usb_if = NULL;
 866	struct pcan_usb_pro_fwinfo *fi = NULL;
 867	struct pcan_usb_pro_blinfo *bi = NULL;
 868	int err;
 869
 870	/* do this for 1st channel only */
 871	if (!dev->prev_siblings) {
 872		/* allocate netdevices common structure attached to first one */
 873		usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
 874				 GFP_KERNEL);
 875		fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
 876		bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
 877		if (!usb_if || !fi || !bi) {
 878			err = -ENOMEM;
 879			goto err_out;
 880		}
 881
 882		/* number of ts msgs to ignore before taking one into account */
 883		usb_if->cm_ignore_count = 5;
 884
 885		/*
 886		 * explicit use of dev_xxx() instead of netdev_xxx() here:
 887		 * information displayed are related to the device itself, not
 888		 * to the canx netdevices.
 889		 */
 890		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 891					    PCAN_USBPRO_INFO_FW,
 892					    fi, sizeof(*fi));
 893		if (err) {
 894			dev_err(dev->netdev->dev.parent,
 895				"unable to read %s firmware info (err %d)\n",
 896				pcan_usb_pro.name, err);
 897			goto err_out;
 898		}
 899
 900		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 901					    PCAN_USBPRO_INFO_BL,
 902					    bi, sizeof(*bi));
 903		if (err) {
 904			dev_err(dev->netdev->dev.parent,
 905				"unable to read %s bootloader info (err %d)\n",
 906				pcan_usb_pro.name, err);
 907			goto err_out;
 908		}
 909
 910		/* tell the device the can driver is running */
 911		err = pcan_usb_pro_drv_loaded(dev, 1);
 912		if (err)
 913			goto err_out;
 914
 915		dev_info(dev->netdev->dev.parent,
 916		     "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
 917		     pcan_usb_pro.name,
 918		     bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
 919		     pcan_usb_pro.ctrl_count);
 920	} else {
 921		usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
 922	}
 923
 924	pdev->usb_if = usb_if;
 925	usb_if->dev[dev->ctrl_idx] = dev;
 926
 927	/* set LED in default state (end of init phase) */
 928	pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
 929
 930	kfree(bi);
 931	kfree(fi);
 932
 933	return 0;
 934
 935 err_out:
 936	kfree(bi);
 937	kfree(fi);
 938	kfree(usb_if);
 939
 940	return err;
 941}
 942
 943static void pcan_usb_pro_exit(struct peak_usb_device *dev)
 944{
 945	struct pcan_usb_pro_device *pdev =
 946			container_of(dev, struct pcan_usb_pro_device, dev);
 947
 948	/*
 949	 * when rmmod called before unplug and if down, should reset things
 950	 * before leaving
 951	 */
 952	if (dev->can.state != CAN_STATE_STOPPED) {
 953		/* set bus off on the corresponding channel */
 954		pcan_usb_pro_set_bus(dev, 0);
 955	}
 956
 957	/* if channel #0 (only) */
 958	if (dev->ctrl_idx == 0) {
 959		/* turn off calibration message if any device were opened */
 960		if (pdev->usb_if->dev_opened_count > 0)
 961			pcan_usb_pro_set_ts(dev, 0);
 962
 963		/* tell the PCAN-USB Pro device the driver is being unloaded */
 964		pcan_usb_pro_drv_loaded(dev, 0);
 965	}
 966}
 967
 968/*
 969 * called when PCAN-USB Pro adapter is unplugged
 970 */
 971static void pcan_usb_pro_free(struct peak_usb_device *dev)
 972{
 973	/* last device: can free pcan_usb_pro_interface object now */
 974	if (!dev->prev_siblings && !dev->next_siblings)
 975		kfree(pcan_usb_pro_dev_if(dev));
 976}
 977
 978/*
 979 * probe function for new PCAN-USB Pro usb interface
 980 */
 981int pcan_usb_pro_probe(struct usb_interface *intf)
 982{
 983	struct usb_host_interface *if_desc;
 984	int i;
 985
 986	if_desc = intf->altsetting;
 987
 988	/* check interface endpoint addresses */
 989	for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
 990		struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
 991
 992		/*
 993		 * below is the list of valid ep addresses. Any other ep address
 994		 * is considered as not-CAN interface address => no dev created
 995		 */
 996		switch (ep->bEndpointAddress) {
 997		case PCAN_USBPRO_EP_CMDOUT:
 998		case PCAN_USBPRO_EP_CMDIN:
 999		case PCAN_USBPRO_EP_MSGOUT_0:
1000		case PCAN_USBPRO_EP_MSGOUT_1:
1001		case PCAN_USBPRO_EP_MSGIN:
1002		case PCAN_USBPRO_EP_UNUSED:
1003			break;
1004		default:
1005			return -ENODEV;
1006		}
1007	}
1008
1009	return 0;
1010}
1011
1012static int pcan_usb_pro_set_phys_id(struct net_device *netdev,
1013				    enum ethtool_phys_id_state state)
1014{
1015	struct peak_usb_device *dev = netdev_priv(netdev);
1016	int err = 0;
1017
1018	switch (state) {
1019	case ETHTOOL_ID_ACTIVE:
1020		/* fast blinking forever */
1021		err = pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_BLINK_FAST,
1022					   0xffffffff);
1023		break;
1024
1025	case ETHTOOL_ID_INACTIVE:
1026		/* restore LED default */
1027		err = pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
1028		break;
1029
1030	default:
1031		break;
1032	}
1033
1034	return err;
1035}
1036
1037static const struct ethtool_ops pcan_usb_pro_ethtool_ops = {
1038	.set_phys_id = pcan_usb_pro_set_phys_id,
1039	.get_ts_info = pcan_get_ts_info,
1040	.get_eeprom_len	= peak_usb_get_eeprom_len,
1041	.get_eeprom = peak_usb_get_eeprom,
1042	.set_eeprom = peak_usb_set_eeprom,
1043};
1044
1045/*
1046 * describe the PCAN-USB Pro adapter
1047 */
1048static const struct can_bittiming_const pcan_usb_pro_const = {
1049	.name = "pcan_usb_pro",
1050	.tseg1_min = 1,
1051	.tseg1_max = 16,
1052	.tseg2_min = 1,
1053	.tseg2_max = 8,
1054	.sjw_max = 4,
1055	.brp_min = 1,
1056	.brp_max = 1024,
1057	.brp_inc = 1,
1058};
1059
1060const struct peak_usb_adapter pcan_usb_pro = {
1061	.name = "PCAN-USB Pro",
1062	.device_id = PCAN_USBPRO_PRODUCT_ID,
1063	.ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1064	.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
1065			      CAN_CTRLMODE_ONE_SHOT,
1066	.clock = {
1067		.freq = PCAN_USBPRO_CRYSTAL_HZ,
1068	},
1069	.bittiming_const = &pcan_usb_pro_const,
1070
1071	/* size of device private data */
1072	.sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1073
1074	.ethtool_ops = &pcan_usb_pro_ethtool_ops,
1075
1076	/* timestamps usage */
1077	.ts_used_bits = 32,
 
1078	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1079	.us_per_ts_shift = 0,
1080
1081	/* give here messages in/out endpoints */
1082	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1083	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1084
1085	/* size of rx/tx usb buffers */
1086	.rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1087	.tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1088
1089	/* device callbacks */
1090	.intf_probe = pcan_usb_pro_probe,
1091	.dev_init = pcan_usb_pro_init,
1092	.dev_exit = pcan_usb_pro_exit,
1093	.dev_free = pcan_usb_pro_free,
1094	.dev_set_bus = pcan_usb_pro_set_bus,
1095	.dev_set_bittiming = pcan_usb_pro_set_bittiming,
1096	.dev_get_can_channel_id = pcan_usb_pro_get_can_channel_id,
1097	.dev_set_can_channel_id = pcan_usb_pro_set_can_channel_id,
1098	.dev_decode_buf = pcan_usb_pro_decode_buf,
1099	.dev_encode_msg = pcan_usb_pro_encode_msg,
1100	.dev_start = pcan_usb_pro_start,
1101	.dev_stop = pcan_usb_pro_stop,
1102	.dev_restart_async = pcan_usb_pro_restart_async,
1103};
v4.6
 
   1/*
   2 * CAN driver for PEAK System PCAN-USB Pro adapter
   3 * Derived from the PCAN project file driver/src/pcan_usbpro.c
   4 *
   5 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
   6 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License as published
  10 * by the Free Software Foundation; version 2 of the License.
  11 *
  12 * This program is distributed in the hope that it will be useful, but
  13 * WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * General Public License for more details.
  16 */
 
 
  17#include <linux/netdevice.h>
  18#include <linux/usb.h>
  19#include <linux/module.h>
  20
  21#include <linux/can.h>
  22#include <linux/can/dev.h>
  23#include <linux/can/error.h>
  24
  25#include "pcan_usb_core.h"
  26#include "pcan_usb_pro.h"
  27
  28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter");
  29
  30#define PCAN_USBPRO_CHANNEL_COUNT	2
  31
  32/* PCAN-USB Pro adapter internal clock (MHz) */
  33#define PCAN_USBPRO_CRYSTAL_HZ		56000000
  34
  35/* PCAN-USB Pro command timeout (ms.) */
  36#define PCAN_USBPRO_COMMAND_TIMEOUT	1000
  37
  38/* PCAN-USB Pro rx/tx buffers size */
  39#define PCAN_USBPRO_RX_BUFFER_SIZE	1024
  40#define PCAN_USBPRO_TX_BUFFER_SIZE	64
  41
  42#define PCAN_USBPRO_MSG_HEADER_LEN	4
  43
  44/* some commands responses need to be re-submitted */
  45#define PCAN_USBPRO_RSP_SUBMIT_MAX	2
  46
  47#define PCAN_USBPRO_RTR			0x01
  48#define PCAN_USBPRO_EXT			0x02
 
  49
  50#define PCAN_USBPRO_CMD_BUFFER_SIZE	512
  51
  52/* handle device specific info used by the netdevices */
  53struct pcan_usb_pro_interface {
  54	struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
  55	struct peak_time_ref time_ref;
  56	int cm_ignore_count;
  57	int dev_opened_count;
  58};
  59
  60/* device information */
  61struct pcan_usb_pro_device {
  62	struct peak_usb_device dev;
  63	struct pcan_usb_pro_interface *usb_if;
  64	u32 cached_ccbt;
  65};
  66
  67/* internal structure used to handle messages sent to bulk urb */
  68struct pcan_usb_pro_msg {
  69	u8 *rec_ptr;
  70	int rec_buffer_size;
  71	int rec_buffer_len;
  72	union {
  73		__le16 *rec_cnt_rd;
  74		__le32 *rec_cnt;
  75		u8 *rec_buffer;
  76	} u;
  77};
  78
  79/* records sizes table indexed on message id. (8-bits value) */
  80static u16 pcan_usb_pro_sizeof_rec[256] = {
  81	[PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
  82	[PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
  83	[PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
  84	[PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
  85	[PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
  86	[PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
 
  87	[PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
  88	[PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
  89	[PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
  90	[PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  91	[PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  92	[PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
  93	[PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
  94	[PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
  95	[PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
  96	[PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
  97};
  98
  99/*
 100 * initialize PCAN-USB Pro message data structure
 101 */
 102static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
 103			 int buffer_size)
 104{
 105	if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
 106		return NULL;
 107
 108	pm->u.rec_buffer = (u8 *)buffer_addr;
 109	pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
 110	pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
 111
 112	return pm->rec_ptr;
 113}
 114
 115static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
 116			       void *buffer_addr, int buffer_size)
 117{
 118	u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
 119
 120	if (pr) {
 121		pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
 122		*pm->u.rec_cnt = 0;
 123	}
 124	return pr;
 125}
 126
 127/*
 128 * add one record to a message being built
 129 */
 130static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...)
 131{
 132	int len, i;
 133	u8 *pc;
 134	va_list ap;
 135
 136	va_start(ap, id);
 137
 138	pc = pm->rec_ptr + 1;
 139
 140	i = 0;
 141	switch (id) {
 142	case PCAN_USBPRO_TXMSG8:
 143		i += 4;
 
 144	case PCAN_USBPRO_TXMSG4:
 145		i += 4;
 
 146	case PCAN_USBPRO_TXMSG0:
 147		*pc++ = va_arg(ap, int);
 148		*pc++ = va_arg(ap, int);
 149		*pc++ = va_arg(ap, int);
 150		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 151		pc += 4;
 152		memcpy(pc, va_arg(ap, int *), i);
 153		pc += i;
 154		break;
 155
 156	case PCAN_USBPRO_SETBTR:
 157	case PCAN_USBPRO_GETDEVID:
 
 158		*pc++ = va_arg(ap, int);
 159		pc += 2;
 160		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 161		pc += 4;
 162		break;
 163
 164	case PCAN_USBPRO_SETFILTR:
 165	case PCAN_USBPRO_SETBUSACT:
 166	case PCAN_USBPRO_SETSILENT:
 167		*pc++ = va_arg(ap, int);
 168		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 169		pc += 2;
 170		break;
 171
 172	case PCAN_USBPRO_SETLED:
 173		*pc++ = va_arg(ap, int);
 174		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 175		pc += 2;
 176		*(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 177		pc += 4;
 178		break;
 179
 180	case PCAN_USBPRO_SETTS:
 181		pc++;
 182		*(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 183		pc += 2;
 184		break;
 185
 186	default:
 187		pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
 188			PCAN_USB_DRIVER_NAME, __func__, id, id);
 189		pc--;
 190		break;
 191	}
 192
 193	len = pc - pm->rec_ptr;
 194	if (len > 0) {
 195		*pm->u.rec_cnt = cpu_to_le32(le32_to_cpu(*pm->u.rec_cnt) + 1);
 196		*pm->rec_ptr = id;
 197
 198		pm->rec_ptr = pc;
 199		pm->rec_buffer_len += len;
 200	}
 201
 202	va_end(ap);
 203
 204	return len;
 205}
 206
 207/*
 208 * send PCAN-USB Pro command synchronously
 209 */
 210static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
 211				 struct pcan_usb_pro_msg *pum)
 212{
 213	int actual_length;
 214	int err;
 215
 216	/* usb device unregistered? */
 217	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 218		return 0;
 219
 220	err = usb_bulk_msg(dev->udev,
 221		usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 222		pum->u.rec_buffer, pum->rec_buffer_len,
 223		&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
 224	if (err)
 225		netdev_err(dev->netdev, "sending command failure: %d\n", err);
 226
 227	return err;
 228}
 229
 230/*
 231 * wait for PCAN-USB Pro command response
 232 */
 233static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
 234				 struct pcan_usb_pro_msg *pum)
 235{
 236	u8 req_data_type, req_channel;
 237	int actual_length;
 238	int i, err = 0;
 239
 240	/* usb device unregistered? */
 241	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 242		return 0;
 243
 244	req_data_type = pum->u.rec_buffer[4];
 245	req_channel = pum->u.rec_buffer[5];
 246
 247	*pum->u.rec_cnt = 0;
 248	for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
 249		struct pcan_usb_pro_msg rsp;
 250		union pcan_usb_pro_rec *pr;
 251		u32 r, rec_cnt;
 252		u16 rec_len;
 253		u8 *pc;
 254
 255		err = usb_bulk_msg(dev->udev,
 256			usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
 257			pum->u.rec_buffer, pum->rec_buffer_len,
 258			&actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
 259		if (err) {
 260			netdev_err(dev->netdev, "waiting rsp error %d\n", err);
 261			break;
 262		}
 263
 264		if (actual_length == 0)
 265			continue;
 266
 267		err = -EBADMSG;
 268		if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
 269			netdev_err(dev->netdev,
 270				   "got abnormal too small rsp (len=%d)\n",
 271				   actual_length);
 272			break;
 273		}
 274
 275		pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
 276			actual_length);
 277
 278		rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
 279
 280		/* loop on records stored into message */
 281		for (r = 0; r < rec_cnt; r++) {
 282			pr = (union pcan_usb_pro_rec *)pc;
 283			rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
 284			if (!rec_len) {
 285				netdev_err(dev->netdev,
 286					   "got unprocessed record in msg\n");
 287				pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
 288					      actual_length);
 289				break;
 290			}
 291
 292			/* check if response corresponds to request */
 293			if (pr->data_type != req_data_type)
 294				netdev_err(dev->netdev,
 295					   "got unwanted rsp %xh: ignored\n",
 296					   pr->data_type);
 297
 298			/* check if channel in response corresponds too */
 299			else if ((req_channel != 0xff) && \
 300				(pr->bus_act.channel != req_channel))
 301				netdev_err(dev->netdev,
 302					"got rsp %xh but on chan%u: ignored\n",
 303					req_data_type, pr->bus_act.channel);
 304
 305			/* got the response */
 306			else
 307				return 0;
 308
 309			/* otherwise, go on with next record in message */
 310			pc += rec_len;
 311		}
 312	}
 313
 314	return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
 315}
 316
 317int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
 318			  int req_value, void *req_addr, int req_size)
 319{
 320	int err;
 321	u8 req_type;
 322	unsigned int p;
 323
 324	/* usb device unregistered? */
 325	if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 326		return 0;
 327
 328	req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
 329
 330	switch (req_id) {
 331	case PCAN_USBPRO_REQ_FCT:
 332		p = usb_sndctrlpipe(dev->udev, 0);
 333		break;
 334
 335	default:
 336		p = usb_rcvctrlpipe(dev->udev, 0);
 337		req_type |= USB_DIR_IN;
 338		memset(req_addr, '\0', req_size);
 339		break;
 340	}
 341
 342	err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
 343			      req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
 344	if (err < 0) {
 345		netdev_info(dev->netdev,
 346			    "unable to request usb[type=%d value=%d] err=%d\n",
 347			    req_id, req_value, err);
 348		return err;
 349	}
 350
 351	return 0;
 352}
 353
 354static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
 355{
 356	struct pcan_usb_pro_msg um;
 357
 358	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 359	pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
 360
 361	return pcan_usb_pro_send_cmd(dev, &um);
 362}
 363
 364static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
 365{
 366	struct pcan_usb_pro_device *pdev =
 367			container_of(dev, struct pcan_usb_pro_device, dev);
 368	struct pcan_usb_pro_msg um;
 369
 370	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 371	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
 372
 373	/* cache the CCBT value to reuse it before next buson */
 374	pdev->cached_ccbt = ccbt;
 375
 376	return pcan_usb_pro_send_cmd(dev, &um);
 377}
 378
 379static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
 380{
 381	struct pcan_usb_pro_msg um;
 382
 383	/* if bus=on, be sure the bitrate being set before! */
 384	if (onoff) {
 385		struct pcan_usb_pro_device *pdev =
 386			     container_of(dev, struct pcan_usb_pro_device, dev);
 387
 388		pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
 389	}
 390
 391	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 392	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
 393
 394	return pcan_usb_pro_send_cmd(dev, &um);
 395}
 396
 397static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
 398{
 399	struct pcan_usb_pro_msg um;
 400
 401	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 402	pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
 403
 404	return pcan_usb_pro_send_cmd(dev, &um);
 405}
 406
 407static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
 408{
 409	struct pcan_usb_pro_msg um;
 410
 411	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 412	pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
 413
 414	return pcan_usb_pro_send_cmd(dev, &um);
 415}
 416
 417static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
 418				u32 timeout)
 419{
 420	struct pcan_usb_pro_msg um;
 421
 422	pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 423	pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
 424
 425	return pcan_usb_pro_send_cmd(dev, &um);
 426}
 427
 428static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
 429				      u32 *device_id)
 430{
 431	struct pcan_usb_pro_devid *pdn;
 432	struct pcan_usb_pro_msg um;
 433	int err;
 434	u8 *pc;
 435
 436	pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 437	pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
 438
 439	err =  pcan_usb_pro_send_cmd(dev, &um);
 440	if (err)
 441		return err;
 442
 443	err = pcan_usb_pro_wait_rsp(dev, &um);
 444	if (err)
 445		return err;
 446
 447	pdn = (struct pcan_usb_pro_devid *)pc;
 448	if (device_id)
 449		*device_id = le32_to_cpu(pdn->serial_num);
 450
 451	return err;
 452}
 453
 
 
 
 
 
 
 
 
 
 
 
 
 454static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
 455				      struct can_bittiming *bt)
 456{
 457	u32 ccbt;
 458
 459	ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
 460	ccbt |= (bt->sjw - 1) << 24;
 461	ccbt |= (bt->phase_seg2 - 1) << 20;
 462	ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
 463	ccbt |= bt->brp - 1;
 464
 465	netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
 466
 467	return pcan_usb_pro_set_bitrate(dev, ccbt);
 468}
 469
 470void pcan_usb_pro_restart_complete(struct urb *urb)
 471{
 472	/* can delete usb resources */
 473	peak_usb_async_complete(urb);
 474
 475	/* notify candev and netdev */
 476	peak_usb_restart_complete(urb->context);
 477}
 478
 479/*
 480 * handle restart but in asynchronously way
 481 */
 482static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
 483				      struct urb *urb, u8 *buf)
 484{
 485	struct pcan_usb_pro_msg um;
 486
 487	pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
 488	pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
 489
 490	usb_fill_bulk_urb(urb, dev->udev,
 491			usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 492			buf, PCAN_USB_MAX_CMD_LEN,
 493			pcan_usb_pro_restart_complete, dev);
 494
 495	return usb_submit_urb(urb, GFP_ATOMIC);
 496}
 497
 498static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
 499{
 500	u8 *buffer;
 501	int err;
 502
 503	buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
 504	if (!buffer)
 505		return -ENOMEM;
 506
 507	buffer[0] = 0;
 508	buffer[1] = !!loaded;
 509
 510	err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
 511				    PCAN_USBPRO_FCT_DRVLD, buffer,
 512				    PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
 513	kfree(buffer);
 514
 515	return err;
 516}
 517
 518static inline
 519struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
 520{
 521	struct pcan_usb_pro_device *pdev =
 522			container_of(dev, struct pcan_usb_pro_device, dev);
 523	return pdev->usb_if;
 524}
 525
 526static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
 527				      struct pcan_usb_pro_rxmsg *rx)
 528{
 529	const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
 530	struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
 531	struct net_device *netdev = dev->netdev;
 532	struct can_frame *can_frame;
 533	struct sk_buff *skb;
 534	struct timeval tv;
 535	struct skb_shared_hwtstamps *hwts;
 536
 537	skb = alloc_can_skb(netdev, &can_frame);
 538	if (!skb)
 539		return -ENOMEM;
 540
 541	can_frame->can_id = le32_to_cpu(rx->id);
 542	can_frame->can_dlc = rx->len & 0x0f;
 543
 544	if (rx->flags & PCAN_USBPRO_EXT)
 545		can_frame->can_id |= CAN_EFF_FLAG;
 546
 547	if (rx->flags & PCAN_USBPRO_RTR)
 548		can_frame->can_id |= CAN_RTR_FLAG;
 549	else
 550		memcpy(can_frame->data, rx->data, can_frame->can_dlc);
 
 
 
 
 551
 552	peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
 553	hwts = skb_hwtstamps(skb);
 554	hwts->hwtstamp = timeval_to_ktime(tv);
 
 555
 556	netdev->stats.rx_packets++;
 557	netdev->stats.rx_bytes += can_frame->can_dlc;
 558	netif_rx(skb);
 559
 560	return 0;
 561}
 562
 563static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
 564				     struct pcan_usb_pro_rxstatus *er)
 565{
 566	const u16 raw_status = le16_to_cpu(er->status);
 567	const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
 568	struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
 569	struct net_device *netdev = dev->netdev;
 570	struct can_frame *can_frame;
 571	enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
 572	u8 err_mask = 0;
 573	struct sk_buff *skb;
 574	struct timeval tv;
 575	struct skb_shared_hwtstamps *hwts;
 576
 577	/* nothing should be sent while in BUS_OFF state */
 578	if (dev->can.state == CAN_STATE_BUS_OFF)
 579		return 0;
 580
 581	if (!raw_status) {
 582		/* no error bit (back to active state) */
 583		dev->can.state = CAN_STATE_ERROR_ACTIVE;
 584		return 0;
 585	}
 586
 587	if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
 588			  PCAN_USBPRO_STATUS_QOVERRUN)) {
 589		/* trick to bypass next comparison and process other errors */
 590		new_state = CAN_STATE_MAX;
 591	}
 592
 593	if (raw_status & PCAN_USBPRO_STATUS_BUS) {
 594		new_state = CAN_STATE_BUS_OFF;
 595	} else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
 596		u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
 597		u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
 598
 599		if (rx_err_cnt > 127)
 600			err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
 601		else if (rx_err_cnt > 96)
 602			err_mask |= CAN_ERR_CRTL_RX_WARNING;
 603
 604		if (tx_err_cnt > 127)
 605			err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
 606		else if (tx_err_cnt > 96)
 607			err_mask |= CAN_ERR_CRTL_TX_WARNING;
 608
 609		if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
 610				CAN_ERR_CRTL_TX_WARNING))
 611			new_state = CAN_STATE_ERROR_WARNING;
 612		else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
 613				     CAN_ERR_CRTL_TX_PASSIVE))
 614			new_state = CAN_STATE_ERROR_PASSIVE;
 615	}
 616
 617	/* donot post any error if current state didn't change */
 618	if (dev->can.state == new_state)
 619		return 0;
 620
 621	/* allocate an skb to store the error frame */
 622	skb = alloc_can_err_skb(netdev, &can_frame);
 623	if (!skb)
 624		return -ENOMEM;
 625
 626	switch (new_state) {
 627	case CAN_STATE_BUS_OFF:
 628		can_frame->can_id |= CAN_ERR_BUSOFF;
 629		dev->can.can_stats.bus_off++;
 630		can_bus_off(netdev);
 631		break;
 632
 633	case CAN_STATE_ERROR_PASSIVE:
 634		can_frame->can_id |= CAN_ERR_CRTL;
 635		can_frame->data[1] |= err_mask;
 636		dev->can.can_stats.error_passive++;
 637		break;
 638
 639	case CAN_STATE_ERROR_WARNING:
 640		can_frame->can_id |= CAN_ERR_CRTL;
 641		can_frame->data[1] |= err_mask;
 642		dev->can.can_stats.error_warning++;
 643		break;
 644
 645	case CAN_STATE_ERROR_ACTIVE:
 646		break;
 647
 648	default:
 649		/* CAN_STATE_MAX (trick to handle other errors) */
 650		if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
 651			can_frame->can_id |= CAN_ERR_PROT;
 652			can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
 653			netdev->stats.rx_over_errors++;
 654			netdev->stats.rx_errors++;
 655		}
 656
 657		if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
 658			can_frame->can_id |= CAN_ERR_CRTL;
 659			can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
 660			netdev->stats.rx_over_errors++;
 661			netdev->stats.rx_errors++;
 662		}
 663
 664		new_state = CAN_STATE_ERROR_ACTIVE;
 665		break;
 666	}
 667
 668	dev->can.state = new_state;
 669
 670	peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
 671	hwts = skb_hwtstamps(skb);
 672	hwts->hwtstamp = timeval_to_ktime(tv);
 673	netdev->stats.rx_packets++;
 674	netdev->stats.rx_bytes += can_frame->can_dlc;
 675	netif_rx(skb);
 676
 677	return 0;
 678}
 679
 680static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
 681				   struct pcan_usb_pro_rxts *ts)
 682{
 683	/* should wait until clock is stabilized */
 684	if (usb_if->cm_ignore_count > 0)
 685		usb_if->cm_ignore_count--;
 686	else
 687		peak_usb_set_ts_now(&usb_if->time_ref,
 688				    le32_to_cpu(ts->ts64[1]));
 689}
 690
 691/*
 692 * callback for bulk IN urb
 693 */
 694static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
 695{
 696	struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
 697	struct net_device *netdev = dev->netdev;
 698	struct pcan_usb_pro_msg usb_msg;
 699	u8 *rec_ptr, *msg_end;
 700	u16 rec_cnt;
 701	int err = 0;
 702
 703	rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
 704					urb->actual_length);
 705	if (!rec_ptr) {
 706		netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
 707		return -EINVAL;
 708	}
 709
 710	/* loop reading all the records from the incoming message */
 711	msg_end = urb->transfer_buffer + urb->actual_length;
 712	rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
 713	for (; rec_cnt > 0; rec_cnt--) {
 714		union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
 715		u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
 716
 717		if (!sizeof_rec) {
 718			netdev_err(netdev,
 719				   "got unsupported rec in usb msg:\n");
 720			err = -ENOTSUPP;
 721			break;
 722		}
 723
 724		/* check if the record goes out of current packet */
 725		if (rec_ptr + sizeof_rec > msg_end) {
 726			netdev_err(netdev,
 727				"got frag rec: should inc usb rx buf size\n");
 728			err = -EBADMSG;
 729			break;
 730		}
 731
 732		switch (pr->data_type) {
 733		case PCAN_USBPRO_RXMSG8:
 734		case PCAN_USBPRO_RXMSG4:
 735		case PCAN_USBPRO_RXMSG0:
 736		case PCAN_USBPRO_RXRTR:
 737			err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
 738			if (err < 0)
 739				goto fail;
 740			break;
 741
 742		case PCAN_USBPRO_RXSTATUS:
 743			err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
 744			if (err < 0)
 745				goto fail;
 746			break;
 747
 748		case PCAN_USBPRO_RXTS:
 749			pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
 750			break;
 751
 752		default:
 753			netdev_err(netdev,
 754				   "unhandled rec type 0x%02x (%d): ignored\n",
 755				   pr->data_type, pr->data_type);
 756			break;
 757		}
 758
 759		rec_ptr += sizeof_rec;
 760	}
 761
 762fail:
 763	if (err)
 764		pcan_dump_mem("received msg",
 765			      urb->transfer_buffer, urb->actual_length);
 766
 767	return err;
 768}
 769
 770static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
 771				   struct sk_buff *skb, u8 *obuf, size_t *size)
 772{
 773	struct can_frame *cf = (struct can_frame *)skb->data;
 774	u8 data_type, len, flags;
 775	struct pcan_usb_pro_msg usb_msg;
 776
 777	pcan_msg_init_empty(&usb_msg, obuf, *size);
 778
 779	if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
 780		data_type = PCAN_USBPRO_TXMSG0;
 781	else if (cf->can_dlc <= 4)
 782		data_type = PCAN_USBPRO_TXMSG4;
 783	else
 784		data_type = PCAN_USBPRO_TXMSG8;
 785
 786	len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
 787
 788	flags = 0;
 789	if (cf->can_id & CAN_EFF_FLAG)
 790		flags |= 0x02;
 791	if (cf->can_id & CAN_RTR_FLAG)
 792		flags |= 0x01;
 
 
 
 
 793
 794	pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
 795			 cf->data);
 796
 797	*size = usb_msg.rec_buffer_len;
 798
 799	return 0;
 800}
 801
 802static int pcan_usb_pro_start(struct peak_usb_device *dev)
 803{
 804	struct pcan_usb_pro_device *pdev =
 805			container_of(dev, struct pcan_usb_pro_device, dev);
 806	int err;
 807
 808	err = pcan_usb_pro_set_silent(dev,
 809				dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
 810	if (err)
 811		return err;
 812
 813	/* filter mode: 0-> All OFF; 1->bypass */
 814	err = pcan_usb_pro_set_filter(dev, 1);
 815	if (err)
 816		return err;
 817
 818	/* opening first device: */
 819	if (pdev->usb_if->dev_opened_count == 0) {
 820		/* reset time_ref */
 821		peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
 822
 823		/* ask device to send ts messages */
 824		err = pcan_usb_pro_set_ts(dev, 1);
 825	}
 826
 827	pdev->usb_if->dev_opened_count++;
 828
 829	return err;
 830}
 831
 832/*
 833 * stop interface
 834 * (last chance before set bus off)
 835 */
 836static int pcan_usb_pro_stop(struct peak_usb_device *dev)
 837{
 838	struct pcan_usb_pro_device *pdev =
 839			container_of(dev, struct pcan_usb_pro_device, dev);
 840
 841	/* turn off ts msgs for that interface if no other dev opened */
 842	if (pdev->usb_if->dev_opened_count == 1)
 843		pcan_usb_pro_set_ts(dev, 0);
 844
 845	pdev->usb_if->dev_opened_count--;
 846
 847	return 0;
 848}
 849
 850/*
 851 * called when probing to initialize a device object.
 852 */
 853static int pcan_usb_pro_init(struct peak_usb_device *dev)
 854{
 855	struct pcan_usb_pro_device *pdev =
 856			container_of(dev, struct pcan_usb_pro_device, dev);
 857	struct pcan_usb_pro_interface *usb_if = NULL;
 858	struct pcan_usb_pro_fwinfo *fi = NULL;
 859	struct pcan_usb_pro_blinfo *bi = NULL;
 860	int err;
 861
 862	/* do this for 1st channel only */
 863	if (!dev->prev_siblings) {
 864		/* allocate netdevices common structure attached to first one */
 865		usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
 866				 GFP_KERNEL);
 867		fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
 868		bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
 869		if (!usb_if || !fi || !bi) {
 870			err = -ENOMEM;
 871			goto err_out;
 872		}
 873
 874		/* number of ts msgs to ignore before taking one into account */
 875		usb_if->cm_ignore_count = 5;
 876
 877		/*
 878		 * explicit use of dev_xxx() instead of netdev_xxx() here:
 879		 * information displayed are related to the device itself, not
 880		 * to the canx netdevices.
 881		 */
 882		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 883					    PCAN_USBPRO_INFO_FW,
 884					    fi, sizeof(*fi));
 885		if (err) {
 886			dev_err(dev->netdev->dev.parent,
 887				"unable to read %s firmware info (err %d)\n",
 888				pcan_usb_pro.name, err);
 889			goto err_out;
 890		}
 891
 892		err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 893					    PCAN_USBPRO_INFO_BL,
 894					    bi, sizeof(*bi));
 895		if (err) {
 896			dev_err(dev->netdev->dev.parent,
 897				"unable to read %s bootloader info (err %d)\n",
 898				pcan_usb_pro.name, err);
 899			goto err_out;
 900		}
 901
 902		/* tell the device the can driver is running */
 903		err = pcan_usb_pro_drv_loaded(dev, 1);
 904		if (err)
 905			goto err_out;
 906
 907		dev_info(dev->netdev->dev.parent,
 908		     "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
 909		     pcan_usb_pro.name,
 910		     bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
 911		     pcan_usb_pro.ctrl_count);
 912	} else {
 913		usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
 914	}
 915
 916	pdev->usb_if = usb_if;
 917	usb_if->dev[dev->ctrl_idx] = dev;
 918
 919	/* set LED in default state (end of init phase) */
 920	pcan_usb_pro_set_led(dev, 0, 1);
 921
 922	kfree(bi);
 923	kfree(fi);
 924
 925	return 0;
 926
 927 err_out:
 928	kfree(bi);
 929	kfree(fi);
 930	kfree(usb_if);
 931
 932	return err;
 933}
 934
 935static void pcan_usb_pro_exit(struct peak_usb_device *dev)
 936{
 937	struct pcan_usb_pro_device *pdev =
 938			container_of(dev, struct pcan_usb_pro_device, dev);
 939
 940	/*
 941	 * when rmmod called before unplug and if down, should reset things
 942	 * before leaving
 943	 */
 944	if (dev->can.state != CAN_STATE_STOPPED) {
 945		/* set bus off on the corresponding channel */
 946		pcan_usb_pro_set_bus(dev, 0);
 947	}
 948
 949	/* if channel #0 (only) */
 950	if (dev->ctrl_idx == 0) {
 951		/* turn off calibration message if any device were opened */
 952		if (pdev->usb_if->dev_opened_count > 0)
 953			pcan_usb_pro_set_ts(dev, 0);
 954
 955		/* tell the PCAN-USB Pro device the driver is being unloaded */
 956		pcan_usb_pro_drv_loaded(dev, 0);
 957	}
 958}
 959
 960/*
 961 * called when PCAN-USB Pro adapter is unplugged
 962 */
 963static void pcan_usb_pro_free(struct peak_usb_device *dev)
 964{
 965	/* last device: can free pcan_usb_pro_interface object now */
 966	if (!dev->prev_siblings && !dev->next_siblings)
 967		kfree(pcan_usb_pro_dev_if(dev));
 968}
 969
 970/*
 971 * probe function for new PCAN-USB Pro usb interface
 972 */
 973int pcan_usb_pro_probe(struct usb_interface *intf)
 974{
 975	struct usb_host_interface *if_desc;
 976	int i;
 977
 978	if_desc = intf->altsetting;
 979
 980	/* check interface endpoint addresses */
 981	for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
 982		struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
 983
 984		/*
 985		 * below is the list of valid ep addreses. Any other ep address
 986		 * is considered as not-CAN interface address => no dev created
 987		 */
 988		switch (ep->bEndpointAddress) {
 989		case PCAN_USBPRO_EP_CMDOUT:
 990		case PCAN_USBPRO_EP_CMDIN:
 991		case PCAN_USBPRO_EP_MSGOUT_0:
 992		case PCAN_USBPRO_EP_MSGOUT_1:
 993		case PCAN_USBPRO_EP_MSGIN:
 994		case PCAN_USBPRO_EP_UNUSED:
 995			break;
 996		default:
 997			return -ENODEV;
 998		}
 999	}
1000
1001	return 0;
1002}
1003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1004/*
1005 * describe the PCAN-USB Pro adapter
1006 */
1007static const struct can_bittiming_const pcan_usb_pro_const = {
1008	.name = "pcan_usb_pro",
1009	.tseg1_min = 1,
1010	.tseg1_max = 16,
1011	.tseg2_min = 1,
1012	.tseg2_max = 8,
1013	.sjw_max = 4,
1014	.brp_min = 1,
1015	.brp_max = 1024,
1016	.brp_inc = 1,
1017};
1018
1019const struct peak_usb_adapter pcan_usb_pro = {
1020	.name = "PCAN-USB Pro",
1021	.device_id = PCAN_USBPRO_PRODUCT_ID,
1022	.ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1023	.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
 
1024	.clock = {
1025		.freq = PCAN_USBPRO_CRYSTAL_HZ,
1026	},
1027	.bittiming_const = &pcan_usb_pro_const,
1028
1029	/* size of device private data */
1030	.sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1031
 
 
1032	/* timestamps usage */
1033	.ts_used_bits = 32,
1034	.ts_period = 1000000, /* calibration period in ts. */
1035	.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1036	.us_per_ts_shift = 0,
1037
1038	/* give here messages in/out endpoints */
1039	.ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1040	.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1041
1042	/* size of rx/tx usb buffers */
1043	.rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1044	.tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1045
1046	/* device callbacks */
1047	.intf_probe = pcan_usb_pro_probe,
1048	.dev_init = pcan_usb_pro_init,
1049	.dev_exit = pcan_usb_pro_exit,
1050	.dev_free = pcan_usb_pro_free,
1051	.dev_set_bus = pcan_usb_pro_set_bus,
1052	.dev_set_bittiming = pcan_usb_pro_set_bittiming,
1053	.dev_get_device_id = pcan_usb_pro_get_device_id,
 
1054	.dev_decode_buf = pcan_usb_pro_decode_buf,
1055	.dev_encode_msg = pcan_usb_pro_encode_msg,
1056	.dev_start = pcan_usb_pro_start,
1057	.dev_stop = pcan_usb_pro_stop,
1058	.dev_restart_async = pcan_usb_pro_restart_async,
1059};