| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter |
| 4 |
* |
| 5 |
* Copyright (C) 2013-2025 PEAK System-Technik GmbH |
| 6 |
* Author: Stéphane Grosjean <[email protected]> |
| 7 |
*/ |
| 8 |
#include <linux/ethtool.h> |
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/netdevice.h> |
| 11 |
#include <linux/usb.h> |
| 12 |
|
| 13 |
#include <linux/can.h> |
| 14 |
#include <linux/can/dev.h> |
| 15 |
#include <linux/can/error.h> |
| 16 |
#include <linux/can/dev/peak_canfd.h> |
| 17 |
|
| 18 |
#include "pcan_usb_core.h" |
| 19 |
#include "pcan_usb_pro.h" |
| 20 |
|
| 21 |
#define PCAN_USBPROFD_CHANNEL_COUNT 2 |
| 22 |
#define PCAN_USBFD_CHANNEL_COUNT 1 |
| 23 |
|
| 24 |
/* PCAN-USB Pro FD adapter internal clock (Hz) */ |
| 25 |
#define PCAN_UFD_CRYSTAL_HZ 80000000 |
| 26 |
|
| 27 |
#define PCAN_UFD_CMD_BUFFER_SIZE 512 |
| 28 |
#define PCAN_UFD_LOSPD_PKT_SIZE 64 |
| 29 |
|
| 30 |
/* PCAN-USB Pro FD command timeout (ms.) */ |
| 31 |
#define PCAN_UFD_CMD_TIMEOUT_MS 1000 |
| 32 |
|
| 33 |
/* PCAN-USB Pro FD rx/tx buffers size */ |
| 34 |
#define PCAN_UFD_RX_BUFFER_SIZE 2048 |
| 35 |
#define PCAN_UFD_TX_BUFFER_SIZE 512 |
| 36 |
|
| 37 |
/* struct pcan_ufd_fw_info::type */ |
| 38 |
#define PCAN_USBFD_TYPE_STD 1 |
| 39 |
#define PCAN_USBFD_TYPE_EXT 2 /* includes EP numbers */ |
| 40 |
|
| 41 |
/* read some versions info from the hw device */ |
| 42 |
struct __packed pcan_ufd_fw_info { |
| 43 |
__le16 size_of; /* sizeof this */ |
| 44 |
__le16 type; /* type of this structure */ |
| 45 |
u8 hw_type; /* Type of hardware (HW_TYPE_xxx) */ |
| 46 |
u8 bl_version[3]; /* Bootloader version */ |
| 47 |
u8 hw_version; /* Hardware version (PCB) */ |
| 48 |
u8 fw_version[3]; /* Firmware version */ |
| 49 |
__le32 dev_id[2]; /* "device id" per CAN */ |
| 50 |
__le32 ser_no; /* S/N */ |
| 51 |
__le32 flags; /* special functions */ |
| 52 |
|
| 53 |
/* extended data when type >= PCAN_USBFD_TYPE_EXT */ |
| 54 |
u8 cmd_out_ep; /* ep for cmd */ |
| 55 |
u8 cmd_in_ep; /* ep for replies */ |
| 56 |
u8 data_out_ep[2]; /* ep for CANx TX */ |
| 57 |
u8 data_in_ep; /* ep for CAN RX */ |
| 58 |
u8 dummy[3]; |
| 59 |
}; |
| 60 |
|
| 61 |
/* handle device specific info used by the netdevices */ |
| 62 |
struct pcan_usb_fd_if { |
| 63 |
struct peak_usb_device *dev[PCAN_USB_MAX_CHANNEL]; |
| 64 |
struct pcan_ufd_fw_info fw_info; |
| 65 |
struct peak_time_ref time_ref; |
| 66 |
int cm_ignore_count; |
| 67 |
int dev_opened_count; |
| 68 |
}; |
| 69 |
|
| 70 |
/* device information */ |
| 71 |
struct pcan_usb_fd_device { |
| 72 |
struct peak_usb_device dev; |
| 73 |
struct can_berr_counter bec; |
| 74 |
struct pcan_usb_fd_if *usb_if; |
| 75 |
u8 *cmd_buffer_addr; |
| 76 |
}; |
| 77 |
|
| 78 |
/* Extended USB commands (non uCAN commands) */ |
| 79 |
|
| 80 |
/* Clock Modes command */ |
| 81 |
#define PCAN_UFD_CMD_CLK_SET 0x80 |
| 82 |
|
| 83 |
#define PCAN_UFD_CLK_80MHZ 0x0 |
| 84 |
#define PCAN_UFD_CLK_60MHZ 0x1 |
| 85 |
#define PCAN_UFD_CLK_40MHZ 0x2 |
| 86 |
#define PCAN_UFD_CLK_30MHZ 0x3 |
| 87 |
#define PCAN_UFD_CLK_24MHZ 0x4 |
| 88 |
#define PCAN_UFD_CLK_20MHZ 0x5 |
| 89 |
#define PCAN_UFD_CLK_DEF PCAN_UFD_CLK_80MHZ |
| 90 |
|
| 91 |
struct __packed pcan_ufd_clock { |
| 92 |
__le16 opcode_channel; |
| 93 |
|
| 94 |
u8 mode; |
| 95 |
u8 unused[5]; |
| 96 |
}; |
| 97 |
|
| 98 |
/* LED control command */ |
| 99 |
#define PCAN_UFD_CMD_LED_SET 0x86 |
| 100 |
|
| 101 |
#define PCAN_UFD_LED_DEV 0x00 |
| 102 |
#define PCAN_UFD_LED_FAST 0x01 |
| 103 |
#define PCAN_UFD_LED_SLOW 0x02 |
| 104 |
#define PCAN_UFD_LED_ON 0x03 |
| 105 |
#define PCAN_UFD_LED_OFF 0x04 |
| 106 |
#define PCAN_UFD_LED_DEF PCAN_UFD_LED_DEV |
| 107 |
|
| 108 |
struct __packed pcan_ufd_led { |
| 109 |
__le16 opcode_channel; |
| 110 |
|
| 111 |
u8 mode; |
| 112 |
u8 unused[5]; |
| 113 |
}; |
| 114 |
|
| 115 |
/* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */ |
| 116 |
#define PCAN_UFD_FLTEXT_CALIBRATION 0x8000 |
| 117 |
|
| 118 |
struct __packed pcan_ufd_options { |
| 119 |
__le16 opcode_channel; |
| 120 |
|
| 121 |
__le16 ucan_mask; |
| 122 |
u16 unused; |
| 123 |
__le16 usb_mask; |
| 124 |
}; |
| 125 |
|
| 126 |
/* Extended usage of uCAN messages for PCAN-USB Pro FD */ |
| 127 |
#define PCAN_UFD_MSG_CALIBRATION 0x100 |
| 128 |
|
| 129 |
struct __packed pcan_ufd_ts_msg { |
| 130 |
__le16 size; |
| 131 |
__le16 type; |
| 132 |
__le32 ts_low; |
| 133 |
__le32 ts_high; |
| 134 |
__le16 usb_frame_index; |
| 135 |
u16 unused; |
| 136 |
}; |
| 137 |
|
| 138 |
#define PCAN_UFD_MSG_OVERRUN 0x101 |
| 139 |
|
| 140 |
#define PCAN_UFD_OVMSG_CHANNEL(o) ((o)->channel & 0xf) |
| 141 |
|
| 142 |
struct __packed pcan_ufd_ovr_msg { |
| 143 |
__le16 size; |
| 144 |
__le16 type; |
| 145 |
__le32 ts_low; |
| 146 |
__le32 ts_high; |
| 147 |
u8 channel; |
| 148 |
u8 unused[3]; |
| 149 |
}; |
| 150 |
|
| 151 |
#define PCAN_UFD_CMD_DEVID_SET 0x81 |
| 152 |
|
| 153 |
struct __packed pcan_ufd_device_id { |
| 154 |
__le16 opcode_channel; |
| 155 |
|
| 156 |
u16 unused; |
| 157 |
__le32 device_id; |
| 158 |
}; |
| 159 |
|
| 160 |
static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om) |
| 161 |
{ |
| 162 |
return om->channel & 0xf; |
| 163 |
} |
| 164 |
|
| 165 |
/* Clock mode frequency values */ |
| 166 |
static const u32 pcan_usb_fd_clk_freq[6] = { |
| 167 |
[PCAN_UFD_CLK_80MHZ] = 80000000, |
| 168 |
[PCAN_UFD_CLK_60MHZ] = 60000000, |
| 169 |
[PCAN_UFD_CLK_40MHZ] = 40000000, |
| 170 |
[PCAN_UFD_CLK_30MHZ] = 30000000, |
| 171 |
[PCAN_UFD_CLK_24MHZ] = 24000000, |
| 172 |
[PCAN_UFD_CLK_20MHZ] = 20000000 |
| 173 |
}; |
| 174 |
|
| 175 |
/* return a device USB interface */ |
| 176 |
static inline |
| 177 |
struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev) |
| 178 |
{ |
| 179 |
struct pcan_usb_fd_device *pdev = |
| 180 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 181 |
return pdev->usb_if; |
| 182 |
} |
| 183 |
|
| 184 |
/* return a device USB commands buffer */ |
| 185 |
static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev) |
| 186 |
{ |
| 187 |
struct pcan_usb_fd_device *pdev = |
| 188 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 189 |
return pdev->cmd_buffer_addr; |
| 190 |
} |
| 191 |
|
| 192 |
/* send PCAN-USB Pro FD commands synchronously */ |
| 193 |
static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail) |
| 194 |
{ |
| 195 |
struct pcan_usb_fd_device *pdev = |
| 196 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 197 |
struct pcan_ufd_fw_info *fw_info = &pdev->usb_if->fw_info; |
| 198 |
void *cmd_head = pcan_usb_fd_cmd_buffer(dev); |
| 199 |
int err = 0; |
| 200 |
u8 *packet_ptr; |
| 201 |
int packet_len; |
| 202 |
ptrdiff_t cmd_len; |
| 203 |
|
| 204 |
/* usb device unregistered? */ |
| 205 |
if (!(dev->state & PCAN_USB_STATE_CONNECTED)) |
| 206 |
return 0; |
| 207 |
|
| 208 |
/* if a packet is not filled completely by commands, the command list |
| 209 |
* is terminated with an "end of collection" record. |
| 210 |
*/ |
| 211 |
cmd_len = cmd_tail - cmd_head; |
| 212 |
if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) { |
| 213 |
memset(cmd_tail, 0xff, sizeof(u64)); |
| 214 |
cmd_len += sizeof(u64); |
| 215 |
} |
| 216 |
|
| 217 |
packet_ptr = cmd_head; |
| 218 |
packet_len = cmd_len; |
| 219 |
|
| 220 |
/* firmware is not able to re-assemble 512 bytes buffer in full-speed */ |
| 221 |
if (unlikely(dev->udev->speed != USB_SPEED_HIGH)) |
| 222 |
packet_len = min(packet_len, PCAN_UFD_LOSPD_PKT_SIZE); |
| 223 |
|
| 224 |
do { |
| 225 |
err = usb_bulk_msg(dev->udev, |
| 226 |
usb_sndbulkpipe(dev->udev, |
| 227 |
fw_info->cmd_out_ep), |
| 228 |
packet_ptr, packet_len, |
| 229 |
NULL, PCAN_UFD_CMD_TIMEOUT_MS); |
| 230 |
if (err) { |
| 231 |
netdev_err(dev->netdev, |
| 232 |
"sending command failure: %d\n", err); |
| 233 |
break; |
| 234 |
} |
| 235 |
|
| 236 |
packet_ptr += packet_len; |
| 237 |
cmd_len -= packet_len; |
| 238 |
|
| 239 |
if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE) |
| 240 |
packet_len = cmd_len; |
| 241 |
|
| 242 |
} while (packet_len > 0); |
| 243 |
|
| 244 |
return err; |
| 245 |
} |
| 246 |
|
| 247 |
static int pcan_usb_fd_read_fwinfo(struct peak_usb_device *dev, |
| 248 |
struct pcan_ufd_fw_info *fw_info) |
| 249 |
{ |
| 250 |
return pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO, |
| 251 |
PCAN_USBPRO_INFO_FW, |
| 252 |
fw_info, |
| 253 |
sizeof(*fw_info)); |
| 254 |
} |
| 255 |
|
| 256 |
/* build the commands list in the given buffer, to enter operational mode */ |
| 257 |
static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf) |
| 258 |
{ |
| 259 |
struct pucan_wr_err_cnt *prc; |
| 260 |
struct pucan_command *cmd; |
| 261 |
u8 *pc = buf; |
| 262 |
|
| 263 |
/* 1st, reset error counters: */ |
| 264 |
prc = (struct pucan_wr_err_cnt *)pc; |
| 265 |
prc->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 266 |
PUCAN_CMD_WR_ERR_CNT); |
| 267 |
|
| 268 |
/* select both counters */ |
| 269 |
prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE); |
| 270 |
|
| 271 |
/* and reset their values */ |
| 272 |
prc->tx_counter = 0; |
| 273 |
prc->rx_counter = 0; |
| 274 |
|
| 275 |
/* moves the pointer forward */ |
| 276 |
pc += sizeof(struct pucan_wr_err_cnt); |
| 277 |
|
| 278 |
/* add command to switch from ISO to non-ISO mode, if fw allows it */ |
| 279 |
if (dev->can.ctrlmode_supported & CAN_CTRLMODE_FD_NON_ISO) { |
| 280 |
struct pucan_options *puo = (struct pucan_options *)pc; |
| 281 |
|
| 282 |
puo->opcode_channel = |
| 283 |
(dev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) ? |
| 284 |
pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 285 |
PUCAN_CMD_CLR_DIS_OPTION) : |
| 286 |
pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 287 |
PUCAN_CMD_SET_EN_OPTION); |
| 288 |
|
| 289 |
puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO); |
| 290 |
|
| 291 |
/* to be sure that no other extended bits will be taken into |
| 292 |
* account |
| 293 |
*/ |
| 294 |
puo->unused = 0; |
| 295 |
|
| 296 |
/* moves the pointer forward */ |
| 297 |
pc += sizeof(struct pucan_options); |
| 298 |
} |
| 299 |
|
| 300 |
/* next, go back to operational mode */ |
| 301 |
cmd = (struct pucan_command *)pc; |
| 302 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 303 |
(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ? |
| 304 |
PUCAN_CMD_LISTEN_ONLY_MODE : |
| 305 |
PUCAN_CMD_NORMAL_MODE); |
| 306 |
pc += sizeof(struct pucan_command); |
| 307 |
|
| 308 |
return pc - buf; |
| 309 |
} |
| 310 |
|
| 311 |
/* set CAN bus on/off */ |
| 312 |
static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff) |
| 313 |
{ |
| 314 |
u8 *pc = pcan_usb_fd_cmd_buffer(dev); |
| 315 |
int l; |
| 316 |
|
| 317 |
if (onoff) { |
| 318 |
/* build the cmds list to enter operational mode */ |
| 319 |
l = pcan_usb_fd_build_restart_cmd(dev, pc); |
| 320 |
} else { |
| 321 |
struct pucan_command *cmd = (struct pucan_command *)pc; |
| 322 |
|
| 323 |
/* build cmd to go back to reset mode */ |
| 324 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 325 |
PUCAN_CMD_RESET_MODE); |
| 326 |
l = sizeof(struct pucan_command); |
| 327 |
} |
| 328 |
|
| 329 |
/* send the command */ |
| 330 |
return pcan_usb_fd_send_cmd(dev, pc + l); |
| 331 |
} |
| 332 |
|
| 333 |
/* set filtering masks: |
| 334 |
* |
| 335 |
* idx in range [0..63] selects a row #idx, all rows otherwise |
| 336 |
* mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s) |
| 337 |
* |
| 338 |
* Each bit of this 64 x 32 bits array defines a CANID value: |
| 339 |
* |
| 340 |
* bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while |
| 341 |
* bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded. |
| 342 |
*/ |
| 343 |
static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx, |
| 344 |
u32 mask) |
| 345 |
{ |
| 346 |
struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 347 |
int i, n; |
| 348 |
|
| 349 |
/* select all rows when idx is out of range [0..63] */ |
| 350 |
if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) { |
| 351 |
n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS; |
| 352 |
idx = 0; |
| 353 |
|
| 354 |
/* select the row (and only the row) otherwise */ |
| 355 |
} else { |
| 356 |
n = idx + 1; |
| 357 |
} |
| 358 |
|
| 359 |
for (i = idx; i < n; i++, cmd++) { |
| 360 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 361 |
PUCAN_CMD_FILTER_STD); |
| 362 |
cmd->idx = cpu_to_le16(i); |
| 363 |
cmd->mask = cpu_to_le32(mask); |
| 364 |
} |
| 365 |
|
| 366 |
/* send the command */ |
| 367 |
return pcan_usb_fd_send_cmd(dev, cmd); |
| 368 |
} |
| 369 |
|
| 370 |
/* set/unset options |
| 371 |
* |
| 372 |
* onoff set(1)/unset(0) options |
| 373 |
* mask each bit defines a kind of options to set/unset |
| 374 |
*/ |
| 375 |
static int pcan_usb_fd_set_options(struct peak_usb_device *dev, |
| 376 |
bool onoff, u16 ucan_mask, u16 usb_mask) |
| 377 |
{ |
| 378 |
struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 379 |
|
| 380 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 381 |
(onoff) ? PUCAN_CMD_SET_EN_OPTION : |
| 382 |
PUCAN_CMD_CLR_DIS_OPTION); |
| 383 |
|
| 384 |
cmd->ucan_mask = cpu_to_le16(ucan_mask); |
| 385 |
cmd->usb_mask = cpu_to_le16(usb_mask); |
| 386 |
|
| 387 |
/* send the command */ |
| 388 |
return pcan_usb_fd_send_cmd(dev, ++cmd); |
| 389 |
} |
| 390 |
|
| 391 |
/* setup LED control */ |
| 392 |
static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode) |
| 393 |
{ |
| 394 |
struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 395 |
|
| 396 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 397 |
PCAN_UFD_CMD_LED_SET); |
| 398 |
cmd->mode = led_mode; |
| 399 |
|
| 400 |
/* send the command */ |
| 401 |
return pcan_usb_fd_send_cmd(dev, ++cmd); |
| 402 |
} |
| 403 |
|
| 404 |
/* set CAN clock domain */ |
| 405 |
static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev, |
| 406 |
u8 clk_mode) |
| 407 |
{ |
| 408 |
struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 409 |
|
| 410 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 411 |
PCAN_UFD_CMD_CLK_SET); |
| 412 |
cmd->mode = clk_mode; |
| 413 |
|
| 414 |
/* send the command */ |
| 415 |
return pcan_usb_fd_send_cmd(dev, ++cmd); |
| 416 |
} |
| 417 |
|
| 418 |
/* set bittiming for CAN and CAN-FD header */ |
| 419 |
static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev, |
| 420 |
struct can_bittiming *bt) |
| 421 |
{ |
| 422 |
struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 423 |
|
| 424 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 425 |
PUCAN_CMD_TIMING_SLOW); |
| 426 |
cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1, |
| 427 |
dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES); |
| 428 |
|
| 429 |
cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1); |
| 430 |
cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1); |
| 431 |
cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1)); |
| 432 |
|
| 433 |
cmd->ewl = 96; /* default */ |
| 434 |
|
| 435 |
/* send the command */ |
| 436 |
return pcan_usb_fd_send_cmd(dev, ++cmd); |
| 437 |
} |
| 438 |
|
| 439 |
/* set CAN-FD bittiming for data */ |
| 440 |
static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev, |
| 441 |
struct can_bittiming *bt) |
| 442 |
{ |
| 443 |
struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 444 |
|
| 445 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 446 |
PUCAN_CMD_TIMING_FAST); |
| 447 |
cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1); |
| 448 |
cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1); |
| 449 |
cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1); |
| 450 |
cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1)); |
| 451 |
|
| 452 |
/* send the command */ |
| 453 |
return pcan_usb_fd_send_cmd(dev, ++cmd); |
| 454 |
} |
| 455 |
|
| 456 |
/* read user CAN channel id from device */ |
| 457 |
static int pcan_usb_fd_get_can_channel_id(struct peak_usb_device *dev, |
| 458 |
u32 *can_ch_id) |
| 459 |
{ |
| 460 |
int err; |
| 461 |
struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev); |
| 462 |
|
| 463 |
err = pcan_usb_fd_read_fwinfo(dev, &usb_if->fw_info); |
| 464 |
if (err) |
| 465 |
return err; |
| 466 |
|
| 467 |
*can_ch_id = le32_to_cpu(usb_if->fw_info.dev_id[dev->ctrl_idx]); |
| 468 |
return err; |
| 469 |
} |
| 470 |
|
| 471 |
/* set a new CAN channel id in the flash memory of the device */ |
| 472 |
static int pcan_usb_fd_set_can_channel_id(struct peak_usb_device *dev, u32 can_ch_id) |
| 473 |
{ |
| 474 |
struct pcan_ufd_device_id *cmd = pcan_usb_fd_cmd_buffer(dev); |
| 475 |
|
| 476 |
cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, |
| 477 |
PCAN_UFD_CMD_DEVID_SET); |
| 478 |
cmd->device_id = cpu_to_le32(can_ch_id); |
| 479 |
|
| 480 |
/* send the command */ |
| 481 |
return pcan_usb_fd_send_cmd(dev, ++cmd); |
| 482 |
} |
| 483 |
|
| 484 |
/* handle restart but in asynchronously way |
| 485 |
* (uses PCAN-USB Pro code to complete asynchronous request) |
| 486 |
*/ |
| 487 |
static int pcan_usb_fd_restart_async(struct peak_usb_device *dev, |
| 488 |
struct urb *urb, u8 *buf) |
| 489 |
{ |
| 490 |
struct pcan_usb_fd_device *pdev = |
| 491 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 492 |
struct pcan_ufd_fw_info *fw_info = &pdev->usb_if->fw_info; |
| 493 |
u8 *pc = buf; |
| 494 |
|
| 495 |
/* build the entire cmds list in the provided buffer, to go back into |
| 496 |
* operational mode. |
| 497 |
*/ |
| 498 |
pc += pcan_usb_fd_build_restart_cmd(dev, pc); |
| 499 |
|
| 500 |
/* add EOC */ |
| 501 |
memset(pc, 0xff, sizeof(struct pucan_command)); |
| 502 |
pc += sizeof(struct pucan_command); |
| 503 |
|
| 504 |
/* complete the URB */ |
| 505 |
usb_fill_bulk_urb(urb, dev->udev, |
| 506 |
usb_sndbulkpipe(dev->udev, fw_info->cmd_out_ep), |
| 507 |
buf, pc - buf, |
| 508 |
pcan_usb_pro_restart_complete, dev); |
| 509 |
|
| 510 |
/* and submit it. */ |
| 511 |
return usb_submit_urb(urb, GFP_ATOMIC); |
| 512 |
} |
| 513 |
|
| 514 |
static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded) |
| 515 |
{ |
| 516 |
struct pcan_usb_fd_device *pdev = |
| 517 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 518 |
|
| 519 |
pdev->cmd_buffer_addr[0] = 0; |
| 520 |
pdev->cmd_buffer_addr[1] = !!loaded; |
| 521 |
|
| 522 |
return pcan_usb_pro_send_req(dev, |
| 523 |
PCAN_USBPRO_REQ_FCT, |
| 524 |
PCAN_USBPRO_FCT_DRVLD, |
| 525 |
pdev->cmd_buffer_addr, |
| 526 |
PCAN_USBPRO_FCT_DRVLD_REQ_LEN); |
| 527 |
} |
| 528 |
|
| 529 |
static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if, |
| 530 |
struct pucan_msg *rx_msg) |
| 531 |
{ |
| 532 |
struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg; |
| 533 |
struct peak_usb_device *dev; |
| 534 |
struct net_device *netdev; |
| 535 |
struct canfd_frame *cfd; |
| 536 |
struct sk_buff *skb; |
| 537 |
const u16 rx_msg_flags = le16_to_cpu(rm->flags); |
| 538 |
|
| 539 |
if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev)) |
| 540 |
return -ENOMEM; |
| 541 |
|
| 542 |
dev = usb_if->dev[pucan_msg_get_channel(rm)]; |
| 543 |
netdev = dev->netdev; |
| 544 |
|
| 545 |
if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) { |
| 546 |
/* CANFD frame case */ |
| 547 |
skb = alloc_canfd_skb(netdev, &cfd); |
| 548 |
if (!skb) |
| 549 |
return -ENOMEM; |
| 550 |
|
| 551 |
if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH) |
| 552 |
cfd->flags |= CANFD_BRS; |
| 553 |
|
| 554 |
if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND) |
| 555 |
cfd->flags |= CANFD_ESI; |
| 556 |
|
| 557 |
cfd->len = can_fd_dlc2len(pucan_msg_get_dlc(rm)); |
| 558 |
} else { |
| 559 |
/* CAN 2.0 frame case */ |
| 560 |
skb = alloc_can_skb(netdev, (struct can_frame **)&cfd); |
| 561 |
if (!skb) |
| 562 |
return -ENOMEM; |
| 563 |
|
| 564 |
can_frame_set_cc_len((struct can_frame *)cfd, |
| 565 |
pucan_msg_get_dlc(rm), |
| 566 |
dev->can.ctrlmode); |
| 567 |
} |
| 568 |
|
| 569 |
cfd->can_id = le32_to_cpu(rm->can_id); |
| 570 |
|
| 571 |
if (rx_msg_flags & PUCAN_MSG_EXT_ID) |
| 572 |
cfd->can_id |= CAN_EFF_FLAG; |
| 573 |
|
| 574 |
if (rx_msg_flags & PUCAN_MSG_RTR) { |
| 575 |
cfd->can_id |= CAN_RTR_FLAG; |
| 576 |
} else { |
| 577 |
memcpy(cfd->data, rm->d, cfd->len); |
| 578 |
netdev->stats.rx_bytes += cfd->len; |
| 579 |
} |
| 580 |
netdev->stats.rx_packets++; |
| 581 |
|
| 582 |
peak_usb_netif_rx_64(skb, le32_to_cpu(rm->ts_low), |
| 583 |
le32_to_cpu(rm->ts_high)); |
| 584 |
|
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
/* handle uCAN status message */ |
| 589 |
static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if, |
| 590 |
struct pucan_msg *rx_msg) |
| 591 |
{ |
| 592 |
struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg; |
| 593 |
struct pcan_usb_fd_device *pdev; |
| 594 |
enum can_state new_state = CAN_STATE_ERROR_ACTIVE; |
| 595 |
enum can_state rx_state, tx_state; |
| 596 |
struct peak_usb_device *dev; |
| 597 |
struct net_device *netdev; |
| 598 |
struct can_frame *cf; |
| 599 |
struct sk_buff *skb; |
| 600 |
|
| 601 |
if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev)) |
| 602 |
return -ENOMEM; |
| 603 |
|
| 604 |
dev = usb_if->dev[pucan_stmsg_get_channel(sm)]; |
| 605 |
pdev = container_of(dev, struct pcan_usb_fd_device, dev); |
| 606 |
netdev = dev->netdev; |
| 607 |
|
| 608 |
/* nothing should be sent while in BUS_OFF state */ |
| 609 |
if (dev->can.state == CAN_STATE_BUS_OFF) |
| 610 |
return 0; |
| 611 |
|
| 612 |
if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) { |
| 613 |
new_state = CAN_STATE_BUS_OFF; |
| 614 |
} else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) { |
| 615 |
new_state = CAN_STATE_ERROR_PASSIVE; |
| 616 |
} else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) { |
| 617 |
new_state = CAN_STATE_ERROR_WARNING; |
| 618 |
} else { |
| 619 |
/* back to (or still in) ERROR_ACTIVE state */ |
| 620 |
new_state = CAN_STATE_ERROR_ACTIVE; |
| 621 |
pdev->bec.txerr = 0; |
| 622 |
pdev->bec.rxerr = 0; |
| 623 |
} |
| 624 |
|
| 625 |
/* state hasn't changed */ |
| 626 |
if (new_state == dev->can.state) |
| 627 |
return 0; |
| 628 |
|
| 629 |
/* handle bus state change */ |
| 630 |
tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0; |
| 631 |
rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0; |
| 632 |
|
| 633 |
/* allocate an skb to store the error frame */ |
| 634 |
skb = alloc_can_err_skb(netdev, &cf); |
| 635 |
can_change_state(netdev, cf, tx_state, rx_state); |
| 636 |
|
| 637 |
/* things must be done even in case of OOM */ |
| 638 |
if (new_state == CAN_STATE_BUS_OFF) |
| 639 |
can_bus_off(netdev); |
| 640 |
|
| 641 |
if (!skb) |
| 642 |
return -ENOMEM; |
| 643 |
|
| 644 |
peak_usb_netif_rx_64(skb, le32_to_cpu(sm->ts_low), |
| 645 |
le32_to_cpu(sm->ts_high)); |
| 646 |
|
| 647 |
return 0; |
| 648 |
} |
| 649 |
|
| 650 |
/* handle uCAN error message */ |
| 651 |
static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if, |
| 652 |
struct pucan_msg *rx_msg) |
| 653 |
{ |
| 654 |
struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg; |
| 655 |
struct pcan_usb_fd_device *pdev; |
| 656 |
struct peak_usb_device *dev; |
| 657 |
|
| 658 |
if (pucan_ermsg_get_channel(er) >= ARRAY_SIZE(usb_if->dev)) |
| 659 |
return -EINVAL; |
| 660 |
|
| 661 |
dev = usb_if->dev[pucan_ermsg_get_channel(er)]; |
| 662 |
pdev = container_of(dev, struct pcan_usb_fd_device, dev); |
| 663 |
|
| 664 |
/* keep a trace of tx and rx error counters for later use */ |
| 665 |
pdev->bec.txerr = er->tx_err_cnt; |
| 666 |
pdev->bec.rxerr = er->rx_err_cnt; |
| 667 |
|
| 668 |
return 0; |
| 669 |
} |
| 670 |
|
| 671 |
/* handle uCAN overrun message */ |
| 672 |
static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if, |
| 673 |
struct pucan_msg *rx_msg) |
| 674 |
{ |
| 675 |
struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg; |
| 676 |
struct peak_usb_device *dev; |
| 677 |
struct net_device *netdev; |
| 678 |
struct can_frame *cf; |
| 679 |
struct sk_buff *skb; |
| 680 |
|
| 681 |
if (pufd_omsg_get_channel(ov) >= ARRAY_SIZE(usb_if->dev)) |
| 682 |
return -EINVAL; |
| 683 |
|
| 684 |
dev = usb_if->dev[pufd_omsg_get_channel(ov)]; |
| 685 |
netdev = dev->netdev; |
| 686 |
|
| 687 |
/* allocate an skb to store the error frame */ |
| 688 |
skb = alloc_can_err_skb(netdev, &cf); |
| 689 |
if (!skb) |
| 690 |
return -ENOMEM; |
| 691 |
|
| 692 |
cf->can_id |= CAN_ERR_CRTL; |
| 693 |
cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW; |
| 694 |
|
| 695 |
peak_usb_netif_rx_64(skb, le32_to_cpu(ov->ts_low), |
| 696 |
le32_to_cpu(ov->ts_high)); |
| 697 |
|
| 698 |
netdev->stats.rx_over_errors++; |
| 699 |
netdev->stats.rx_errors++; |
| 700 |
|
| 701 |
return 0; |
| 702 |
} |
| 703 |
|
| 704 |
/* handle USB calibration message */ |
| 705 |
static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if, |
| 706 |
struct pucan_msg *rx_msg) |
| 707 |
{ |
| 708 |
struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg; |
| 709 |
|
| 710 |
/* should wait until clock is stabilized */ |
| 711 |
if (usb_if->cm_ignore_count > 0) |
| 712 |
usb_if->cm_ignore_count--; |
| 713 |
else |
| 714 |
peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low)); |
| 715 |
} |
| 716 |
|
| 717 |
/* callback for bulk IN urb */ |
| 718 |
static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb) |
| 719 |
{ |
| 720 |
struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev); |
| 721 |
struct net_device *netdev = dev->netdev; |
| 722 |
struct pucan_msg *rx_msg; |
| 723 |
u8 *msg_ptr, *msg_end; |
| 724 |
int err = 0; |
| 725 |
|
| 726 |
/* loop reading all the records from the incoming message */ |
| 727 |
msg_ptr = urb->transfer_buffer; |
| 728 |
msg_end = urb->transfer_buffer + urb->actual_length; |
| 729 |
for (; msg_ptr < msg_end;) { |
| 730 |
u16 rx_msg_type, rx_msg_size; |
| 731 |
|
| 732 |
rx_msg = (struct pucan_msg *)msg_ptr; |
| 733 |
if (!rx_msg->size) { |
| 734 |
/* null packet found: end of list */ |
| 735 |
break; |
| 736 |
} |
| 737 |
|
| 738 |
rx_msg_size = le16_to_cpu(rx_msg->size); |
| 739 |
rx_msg_type = le16_to_cpu(rx_msg->type); |
| 740 |
|
| 741 |
/* check if the record goes out of current packet */ |
| 742 |
if (msg_ptr + rx_msg_size > msg_end) { |
| 743 |
netdev_err(netdev, |
| 744 |
"got frag rec: should inc usb rx buf sze\n"); |
| 745 |
err = -EBADMSG; |
| 746 |
break; |
| 747 |
} |
| 748 |
|
| 749 |
switch (rx_msg_type) { |
| 750 |
case PUCAN_MSG_CAN_RX: |
| 751 |
err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg); |
| 752 |
if (err < 0) |
| 753 |
goto fail; |
| 754 |
break; |
| 755 |
|
| 756 |
case PCAN_UFD_MSG_CALIBRATION: |
| 757 |
pcan_usb_fd_decode_ts(usb_if, rx_msg); |
| 758 |
break; |
| 759 |
|
| 760 |
case PUCAN_MSG_ERROR: |
| 761 |
err = pcan_usb_fd_decode_error(usb_if, rx_msg); |
| 762 |
if (err < 0) |
| 763 |
goto fail; |
| 764 |
break; |
| 765 |
|
| 766 |
case PUCAN_MSG_STATUS: |
| 767 |
err = pcan_usb_fd_decode_status(usb_if, rx_msg); |
| 768 |
if (err < 0) |
| 769 |
goto fail; |
| 770 |
break; |
| 771 |
|
| 772 |
case PCAN_UFD_MSG_OVERRUN: |
| 773 |
err = pcan_usb_fd_decode_overrun(usb_if, rx_msg); |
| 774 |
if (err < 0) |
| 775 |
goto fail; |
| 776 |
break; |
| 777 |
|
| 778 |
default: |
| 779 |
netdev_err(netdev, |
| 780 |
"unhandled msg type 0x%02x (%d): ignored\n", |
| 781 |
rx_msg_type, rx_msg_type); |
| 782 |
break; |
| 783 |
} |
| 784 |
|
| 785 |
msg_ptr += rx_msg_size; |
| 786 |
} |
| 787 |
|
| 788 |
fail: |
| 789 |
if (err) |
| 790 |
pcan_dump_mem("received msg", |
| 791 |
urb->transfer_buffer, urb->actual_length); |
| 792 |
return err; |
| 793 |
} |
| 794 |
|
| 795 |
/* CAN/CANFD frames encoding callback */ |
| 796 |
static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev, |
| 797 |
struct sk_buff *skb, u8 *obuf, size_t *size) |
| 798 |
{ |
| 799 |
struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf; |
| 800 |
struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
| 801 |
u16 tx_msg_size, tx_msg_flags; |
| 802 |
u8 dlc; |
| 803 |
|
| 804 |
if (cfd->len > CANFD_MAX_DLEN) |
| 805 |
return -EINVAL; |
| 806 |
|
| 807 |
tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4); |
| 808 |
tx_msg->size = cpu_to_le16(tx_msg_size); |
| 809 |
tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX); |
| 810 |
|
| 811 |
tx_msg_flags = 0; |
| 812 |
if (cfd->can_id & CAN_EFF_FLAG) { |
| 813 |
tx_msg_flags |= PUCAN_MSG_EXT_ID; |
| 814 |
tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK); |
| 815 |
} else { |
| 816 |
tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK); |
| 817 |
} |
| 818 |
|
| 819 |
if (can_is_canfd_skb(skb)) { |
| 820 |
/* considering a CANFD frame */ |
| 821 |
dlc = can_fd_len2dlc(cfd->len); |
| 822 |
|
| 823 |
tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN; |
| 824 |
|
| 825 |
if (cfd->flags & CANFD_BRS) |
| 826 |
tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH; |
| 827 |
|
| 828 |
if (cfd->flags & CANFD_ESI) |
| 829 |
tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND; |
| 830 |
} else { |
| 831 |
/* CAND 2.0 frames */ |
| 832 |
dlc = can_get_cc_dlc((struct can_frame *)cfd, |
| 833 |
dev->can.ctrlmode); |
| 834 |
|
| 835 |
if (cfd->can_id & CAN_RTR_FLAG) |
| 836 |
tx_msg_flags |= PUCAN_MSG_RTR; |
| 837 |
} |
| 838 |
|
| 839 |
/* Single-Shot frame */ |
| 840 |
if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) |
| 841 |
tx_msg_flags |= PUCAN_MSG_SINGLE_SHOT; |
| 842 |
|
| 843 |
tx_msg->flags = cpu_to_le16(tx_msg_flags); |
| 844 |
tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, dlc); |
| 845 |
memcpy(tx_msg->d, cfd->data, cfd->len); |
| 846 |
|
| 847 |
/* add null size message to tag the end (messages are 32-bits aligned) |
| 848 |
*/ |
| 849 |
tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size); |
| 850 |
|
| 851 |
tx_msg->size = 0; |
| 852 |
|
| 853 |
/* set the whole size of the USB packet to send */ |
| 854 |
*size = tx_msg_size + sizeof(u32); |
| 855 |
|
| 856 |
return 0; |
| 857 |
} |
| 858 |
|
| 859 |
/* start the interface (last chance before set bus on) */ |
| 860 |
static int pcan_usb_fd_start(struct peak_usb_device *dev) |
| 861 |
{ |
| 862 |
struct pcan_usb_fd_device *pdev = |
| 863 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 864 |
int err; |
| 865 |
|
| 866 |
/* set filter mode: all acceptance */ |
| 867 |
err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff); |
| 868 |
if (err) |
| 869 |
return err; |
| 870 |
|
| 871 |
/* opening first device: */ |
| 872 |
if (pdev->usb_if->dev_opened_count == 0) { |
| 873 |
/* reset time_ref */ |
| 874 |
peak_usb_init_time_ref(&pdev->usb_if->time_ref, |
| 875 |
&pcan_usb_pro_fd); |
| 876 |
|
| 877 |
/* enable USB calibration messages */ |
| 878 |
err = pcan_usb_fd_set_options(dev, 1, |
| 879 |
PUCAN_OPTION_ERROR, |
| 880 |
PCAN_UFD_FLTEXT_CALIBRATION); |
| 881 |
} |
| 882 |
|
| 883 |
pdev->usb_if->dev_opened_count++; |
| 884 |
|
| 885 |
/* reset cached error counters */ |
| 886 |
pdev->bec.txerr = 0; |
| 887 |
pdev->bec.rxerr = 0; |
| 888 |
|
| 889 |
return err; |
| 890 |
} |
| 891 |
|
| 892 |
/* socket callback used to copy berr counters values received through USB */ |
| 893 |
static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev, |
| 894 |
struct can_berr_counter *bec) |
| 895 |
{ |
| 896 |
struct peak_usb_device *dev = netdev_priv(netdev); |
| 897 |
struct pcan_usb_fd_device *pdev = |
| 898 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 899 |
|
| 900 |
*bec = pdev->bec; |
| 901 |
|
| 902 |
/* must return 0 */ |
| 903 |
return 0; |
| 904 |
} |
| 905 |
|
| 906 |
/* probe function for all PCAN-USB FD family usb interfaces */ |
| 907 |
static int pcan_usb_fd_probe(struct usb_interface *intf) |
| 908 |
{ |
| 909 |
struct usb_host_interface *iface_desc = &intf->altsetting[0]; |
| 910 |
|
| 911 |
/* CAN interface is always interface #0 */ |
| 912 |
return iface_desc->desc.bInterfaceNumber; |
| 913 |
} |
| 914 |
|
| 915 |
/* stop interface (last chance before set bus off) */ |
| 916 |
static int pcan_usb_fd_stop(struct peak_usb_device *dev) |
| 917 |
{ |
| 918 |
struct pcan_usb_fd_device *pdev = |
| 919 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 920 |
|
| 921 |
/* turn off special msgs for that interface if no other dev opened */ |
| 922 |
if (pdev->usb_if->dev_opened_count == 1) |
| 923 |
pcan_usb_fd_set_options(dev, 0, |
| 924 |
PUCAN_OPTION_ERROR, |
| 925 |
PCAN_UFD_FLTEXT_CALIBRATION); |
| 926 |
pdev->usb_if->dev_opened_count--; |
| 927 |
|
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
/* called when probing, to initialize a device object */ |
| 932 |
static int pcan_usb_fd_init(struct peak_usb_device *dev) |
| 933 |
{ |
| 934 |
struct pcan_usb_fd_device *pdev = |
| 935 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 936 |
struct pcan_ufd_fw_info *fw_info; |
| 937 |
int i, err = -ENOMEM; |
| 938 |
|
| 939 |
/* do this for 1st channel only */ |
| 940 |
if (!dev->prev_siblings) { |
| 941 |
/* allocate netdevices common structure attached to first one */ |
| 942 |
pdev->usb_if = kzalloc_obj(*pdev->usb_if); |
| 943 |
if (!pdev->usb_if) |
| 944 |
goto err_out; |
| 945 |
|
| 946 |
/* allocate command buffer once for all for the interface */ |
| 947 |
pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE, |
| 948 |
GFP_KERNEL); |
| 949 |
if (!pdev->cmd_buffer_addr) |
| 950 |
goto err_out_1; |
| 951 |
|
| 952 |
/* number of ts msgs to ignore before taking one into account */ |
| 953 |
pdev->usb_if->cm_ignore_count = 5; |
| 954 |
|
| 955 |
fw_info = &pdev->usb_if->fw_info; |
| 956 |
|
| 957 |
err = pcan_usb_fd_read_fwinfo(dev, fw_info); |
| 958 |
if (err) { |
| 959 |
dev_err(dev->netdev->dev.parent, |
| 960 |
"unable to read %s firmware info (err %d)\n", |
| 961 |
dev->adapter->name, err); |
| 962 |
goto err_out_2; |
| 963 |
} |
| 964 |
|
| 965 |
/* explicit use of dev_xxx() instead of netdev_xxx() here: |
| 966 |
* information displayed are related to the device itself, not |
| 967 |
* to the canx (channel) device. |
| 968 |
*/ |
| 969 |
dev_info(dev->netdev->dev.parent, |
| 970 |
"PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n", |
| 971 |
dev->adapter->name, fw_info->hw_version, |
| 972 |
fw_info->fw_version[0], |
| 973 |
fw_info->fw_version[1], |
| 974 |
fw_info->fw_version[2], |
| 975 |
dev->adapter->ctrl_count); |
| 976 |
|
| 977 |
/* check for ability to switch between ISO/non-ISO modes */ |
| 978 |
if (fw_info->fw_version[0] >= 2) { |
| 979 |
/* firmware >= 2.x supports ISO/non-ISO switching */ |
| 980 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO; |
| 981 |
} else { |
| 982 |
/* firmware < 2.x only supports fixed(!) non-ISO */ |
| 983 |
dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO; |
| 984 |
} |
| 985 |
|
| 986 |
/* if vendor rsp type is greater than or equal to 2, then it |
| 987 |
* contains EP numbers to use for cmds pipes. If not, then |
| 988 |
* default EP should be used. |
| 989 |
*/ |
| 990 |
if (le16_to_cpu(fw_info->type) < PCAN_USBFD_TYPE_EXT) { |
| 991 |
fw_info->cmd_out_ep = PCAN_USBPRO_EP_CMDOUT; |
| 992 |
fw_info->cmd_in_ep = PCAN_USBPRO_EP_CMDIN; |
| 993 |
} |
| 994 |
|
| 995 |
/* tell the hardware the can driver is running */ |
| 996 |
err = pcan_usb_fd_drv_loaded(dev, 1); |
| 997 |
if (err) { |
| 998 |
dev_err(dev->netdev->dev.parent, |
| 999 |
"unable to tell %s driver is loaded (err %d)\n", |
| 1000 |
dev->adapter->name, err); |
| 1001 |
goto err_out_2; |
| 1002 |
} |
| 1003 |
} else { |
| 1004 |
/* otherwise, simply copy previous sibling's values */ |
| 1005 |
struct pcan_usb_fd_device *ppdev = |
| 1006 |
container_of(dev->prev_siblings, |
| 1007 |
struct pcan_usb_fd_device, dev); |
| 1008 |
|
| 1009 |
pdev->usb_if = ppdev->usb_if; |
| 1010 |
pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr; |
| 1011 |
|
| 1012 |
/* do a copy of the ctrlmode[_supported] too */ |
| 1013 |
dev->can.ctrlmode = ppdev->dev.can.ctrlmode; |
| 1014 |
dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported; |
| 1015 |
|
| 1016 |
fw_info = &pdev->usb_if->fw_info; |
| 1017 |
} |
| 1018 |
|
| 1019 |
pdev->usb_if->dev[dev->ctrl_idx] = dev; |
| 1020 |
dev->can_channel_id = |
| 1021 |
le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]); |
| 1022 |
|
| 1023 |
/* if vendor rsp type is greater than or equal to 2, then it contains EP |
| 1024 |
* numbers to use for data pipes. If not, then statically defined EP are |
| 1025 |
* used (see peak_usb_create_dev()). |
| 1026 |
*/ |
| 1027 |
if (le16_to_cpu(fw_info->type) >= PCAN_USBFD_TYPE_EXT) { |
| 1028 |
dev->ep_msg_in = fw_info->data_in_ep; |
| 1029 |
dev->ep_msg_out = fw_info->data_out_ep[dev->ctrl_idx]; |
| 1030 |
} |
| 1031 |
|
| 1032 |
/* set clock domain */ |
| 1033 |
for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++) |
| 1034 |
if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i]) |
| 1035 |
break; |
| 1036 |
|
| 1037 |
if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) { |
| 1038 |
dev_warn(dev->netdev->dev.parent, |
| 1039 |
"incompatible clock frequencies\n"); |
| 1040 |
err = -EINVAL; |
| 1041 |
goto err_out_2; |
| 1042 |
} |
| 1043 |
|
| 1044 |
pcan_usb_fd_set_clock_domain(dev, i); |
| 1045 |
|
| 1046 |
/* set LED in default state (end of init phase) */ |
| 1047 |
pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF); |
| 1048 |
|
| 1049 |
return 0; |
| 1050 |
|
| 1051 |
err_out_2: |
| 1052 |
kfree(pdev->cmd_buffer_addr); |
| 1053 |
err_out_1: |
| 1054 |
kfree(pdev->usb_if); |
| 1055 |
err_out: |
| 1056 |
return err; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* called when driver module is being unloaded */ |
| 1060 |
static void pcan_usb_fd_exit(struct peak_usb_device *dev) |
| 1061 |
{ |
| 1062 |
struct pcan_usb_fd_device *pdev = |
| 1063 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 1064 |
|
| 1065 |
/* when rmmod called before unplug and if down, should reset things |
| 1066 |
* before leaving |
| 1067 |
*/ |
| 1068 |
if (dev->can.state != CAN_STATE_STOPPED) { |
| 1069 |
/* set bus off on the corresponding channel */ |
| 1070 |
pcan_usb_fd_set_bus(dev, 0); |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* switch off corresponding CAN LEDs */ |
| 1074 |
pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF); |
| 1075 |
|
| 1076 |
/* if channel #0 (only) */ |
| 1077 |
if (dev->ctrl_idx == 0) { |
| 1078 |
/* turn off calibration message if any device were opened */ |
| 1079 |
if (pdev->usb_if->dev_opened_count > 0) |
| 1080 |
pcan_usb_fd_set_options(dev, 0, |
| 1081 |
PUCAN_OPTION_ERROR, |
| 1082 |
PCAN_UFD_FLTEXT_CALIBRATION); |
| 1083 |
|
| 1084 |
/* tell USB adapter that the driver is being unloaded */ |
| 1085 |
pcan_usb_fd_drv_loaded(dev, 0); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
/* called when the USB adapter is unplugged */ |
| 1090 |
static void pcan_usb_fd_free(struct peak_usb_device *dev) |
| 1091 |
{ |
| 1092 |
/* last device: can free shared objects now */ |
| 1093 |
if (!dev->prev_siblings && !dev->next_siblings) { |
| 1094 |
struct pcan_usb_fd_device *pdev = |
| 1095 |
container_of(dev, struct pcan_usb_fd_device, dev); |
| 1096 |
|
| 1097 |
/* free commands buffer */ |
| 1098 |
kfree(pdev->cmd_buffer_addr); |
| 1099 |
|
| 1100 |
/* free usb interface object */ |
| 1101 |
kfree(pdev->usb_if); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
/* blink LED's */ |
| 1106 |
static int pcan_usb_fd_set_phys_id(struct net_device *netdev, |
| 1107 |
enum ethtool_phys_id_state state) |
| 1108 |
{ |
| 1109 |
struct peak_usb_device *dev = netdev_priv(netdev); |
| 1110 |
int err = 0; |
| 1111 |
|
| 1112 |
switch (state) { |
| 1113 |
case ETHTOOL_ID_ACTIVE: |
| 1114 |
err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_FAST); |
| 1115 |
break; |
| 1116 |
case ETHTOOL_ID_INACTIVE: |
| 1117 |
err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF); |
| 1118 |
break; |
| 1119 |
default: |
| 1120 |
break; |
| 1121 |
} |
| 1122 |
|
| 1123 |
return err; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static const struct ethtool_ops pcan_usb_fd_ethtool_ops = { |
| 1127 |
.set_phys_id = pcan_usb_fd_set_phys_id, |
| 1128 |
.get_ts_info = pcan_get_ts_info, |
| 1129 |
.get_eeprom_len = peak_usb_get_eeprom_len, |
| 1130 |
.get_eeprom = peak_usb_get_eeprom, |
| 1131 |
.set_eeprom = peak_usb_set_eeprom, |
| 1132 |
}; |
| 1133 |
|
| 1134 |
/* describes the PCAN-USB FD adapter */ |
| 1135 |
static const struct can_bittiming_const pcan_usb_fd_const = { |
| 1136 |
.name = "pcan_usb_fd", |
| 1137 |
.tseg1_min = 1, |
| 1138 |
.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), |
| 1139 |
.tseg2_min = 1, |
| 1140 |
.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), |
| 1141 |
.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), |
| 1142 |
.brp_min = 1, |
| 1143 |
.brp_max = (1 << PUCAN_TSLOW_BRP_BITS), |
| 1144 |
.brp_inc = 1, |
| 1145 |
}; |
| 1146 |
|
| 1147 |
static const struct can_bittiming_const pcan_usb_fd_data_const = { |
| 1148 |
.name = "pcan_usb_fd", |
| 1149 |
.tseg1_min = 1, |
| 1150 |
.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), |
| 1151 |
.tseg2_min = 1, |
| 1152 |
.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), |
| 1153 |
.sjw_max = (1 << PUCAN_TFAST_SJW_BITS), |
| 1154 |
.brp_min = 1, |
| 1155 |
.brp_max = (1 << PUCAN_TFAST_BRP_BITS), |
| 1156 |
.brp_inc = 1, |
| 1157 |
}; |
| 1158 |
|
| 1159 |
const struct peak_usb_adapter pcan_usb_fd = { |
| 1160 |
.name = "PCAN-USB FD", |
| 1161 |
.device_id = PCAN_USBFD_PRODUCT_ID, |
| 1162 |
.ctrl_count = PCAN_USBFD_CHANNEL_COUNT, |
| 1163 |
.ctrlmode_supported = CAN_CTRLMODE_FD | |
| 1164 |
CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | |
| 1165 |
CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, |
| 1166 |
.clock = { |
| 1167 |
.freq = PCAN_UFD_CRYSTAL_HZ, |
| 1168 |
}, |
| 1169 |
.bittiming_const = &pcan_usb_fd_const, |
| 1170 |
.data_bittiming_const = &pcan_usb_fd_data_const, |
| 1171 |
|
| 1172 |
/* size of device private data */ |
| 1173 |
.sizeof_dev_private = sizeof(struct pcan_usb_fd_device), |
| 1174 |
|
| 1175 |
.ethtool_ops = &pcan_usb_fd_ethtool_ops, |
| 1176 |
|
| 1177 |
/* timestamps usage */ |
| 1178 |
.ts_used_bits = 32, |
| 1179 |
.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ |
| 1180 |
.us_per_ts_shift = 0, |
| 1181 |
|
| 1182 |
/* give here messages in/out endpoints */ |
| 1183 |
.ep_msg_in = PCAN_USBPRO_EP_MSGIN, |
| 1184 |
.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0}, |
| 1185 |
|
| 1186 |
/* size of rx/tx usb buffers */ |
| 1187 |
.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, |
| 1188 |
.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, |
| 1189 |
|
| 1190 |
/* device callbacks */ |
| 1191 |
.intf_probe = pcan_usb_fd_probe, |
| 1192 |
.dev_init = pcan_usb_fd_init, |
| 1193 |
|
| 1194 |
.dev_exit = pcan_usb_fd_exit, |
| 1195 |
.dev_free = pcan_usb_fd_free, |
| 1196 |
.dev_set_bus = pcan_usb_fd_set_bus, |
| 1197 |
.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, |
| 1198 |
.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, |
| 1199 |
.dev_get_can_channel_id = pcan_usb_fd_get_can_channel_id, |
| 1200 |
.dev_set_can_channel_id = pcan_usb_fd_set_can_channel_id, |
| 1201 |
.dev_decode_buf = pcan_usb_fd_decode_buf, |
| 1202 |
.dev_start = pcan_usb_fd_start, |
| 1203 |
.dev_stop = pcan_usb_fd_stop, |
| 1204 |
.dev_restart_async = pcan_usb_fd_restart_async, |
| 1205 |
.dev_encode_msg = pcan_usb_fd_encode_msg, |
| 1206 |
|
| 1207 |
.do_get_berr_counter = pcan_usb_fd_get_berr_counter, |
| 1208 |
}; |
| 1209 |
|
| 1210 |
/* describes the PCAN-CHIP USB */ |
| 1211 |
static const struct can_bittiming_const pcan_usb_chip_const = { |
| 1212 |
.name = "pcan_chip_usb", |
| 1213 |
.tseg1_min = 1, |
| 1214 |
.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), |
| 1215 |
.tseg2_min = 1, |
| 1216 |
.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), |
| 1217 |
.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), |
| 1218 |
.brp_min = 1, |
| 1219 |
.brp_max = (1 << PUCAN_TSLOW_BRP_BITS), |
| 1220 |
.brp_inc = 1, |
| 1221 |
}; |
| 1222 |
|
| 1223 |
static const struct can_bittiming_const pcan_usb_chip_data_const = { |
| 1224 |
.name = "pcan_chip_usb", |
| 1225 |
.tseg1_min = 1, |
| 1226 |
.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), |
| 1227 |
.tseg2_min = 1, |
| 1228 |
.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), |
| 1229 |
.sjw_max = (1 << PUCAN_TFAST_SJW_BITS), |
| 1230 |
.brp_min = 1, |
| 1231 |
.brp_max = (1 << PUCAN_TFAST_BRP_BITS), |
| 1232 |
.brp_inc = 1, |
| 1233 |
}; |
| 1234 |
|
| 1235 |
const struct peak_usb_adapter pcan_usb_chip = { |
| 1236 |
.name = "PCAN-Chip USB", |
| 1237 |
.device_id = PCAN_USBCHIP_PRODUCT_ID, |
| 1238 |
.ctrl_count = PCAN_USBFD_CHANNEL_COUNT, |
| 1239 |
.ctrlmode_supported = CAN_CTRLMODE_FD | |
| 1240 |
CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | |
| 1241 |
CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, |
| 1242 |
.clock = { |
| 1243 |
.freq = PCAN_UFD_CRYSTAL_HZ, |
| 1244 |
}, |
| 1245 |
.bittiming_const = &pcan_usb_chip_const, |
| 1246 |
.data_bittiming_const = &pcan_usb_chip_data_const, |
| 1247 |
|
| 1248 |
/* size of device private data */ |
| 1249 |
.sizeof_dev_private = sizeof(struct pcan_usb_fd_device), |
| 1250 |
|
| 1251 |
.ethtool_ops = &pcan_usb_fd_ethtool_ops, |
| 1252 |
|
| 1253 |
/* timestamps usage */ |
| 1254 |
.ts_used_bits = 32, |
| 1255 |
.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ |
| 1256 |
.us_per_ts_shift = 0, |
| 1257 |
|
| 1258 |
/* give here messages in/out endpoints */ |
| 1259 |
.ep_msg_in = PCAN_USBPRO_EP_MSGIN, |
| 1260 |
.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0}, |
| 1261 |
|
| 1262 |
/* size of rx/tx usb buffers */ |
| 1263 |
.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, |
| 1264 |
.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, |
| 1265 |
|
| 1266 |
/* device callbacks */ |
| 1267 |
.intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ |
| 1268 |
.dev_init = pcan_usb_fd_init, |
| 1269 |
|
| 1270 |
.dev_exit = pcan_usb_fd_exit, |
| 1271 |
.dev_free = pcan_usb_fd_free, |
| 1272 |
.dev_set_bus = pcan_usb_fd_set_bus, |
| 1273 |
.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, |
| 1274 |
.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, |
| 1275 |
.dev_get_can_channel_id = pcan_usb_fd_get_can_channel_id, |
| 1276 |
.dev_set_can_channel_id = pcan_usb_fd_set_can_channel_id, |
| 1277 |
.dev_decode_buf = pcan_usb_fd_decode_buf, |
| 1278 |
.dev_start = pcan_usb_fd_start, |
| 1279 |
.dev_stop = pcan_usb_fd_stop, |
| 1280 |
.dev_restart_async = pcan_usb_fd_restart_async, |
| 1281 |
.dev_encode_msg = pcan_usb_fd_encode_msg, |
| 1282 |
|
| 1283 |
.do_get_berr_counter = pcan_usb_fd_get_berr_counter, |
| 1284 |
}; |
| 1285 |
|
| 1286 |
/* describes the PCAN-USB Pro FD adapter */ |
| 1287 |
static const struct can_bittiming_const pcan_usb_pro_fd_const = { |
| 1288 |
.name = "pcan_usb_pro_fd", |
| 1289 |
.tseg1_min = 1, |
| 1290 |
.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), |
| 1291 |
.tseg2_min = 1, |
| 1292 |
.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), |
| 1293 |
.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), |
| 1294 |
.brp_min = 1, |
| 1295 |
.brp_max = (1 << PUCAN_TSLOW_BRP_BITS), |
| 1296 |
.brp_inc = 1, |
| 1297 |
}; |
| 1298 |
|
| 1299 |
static const struct can_bittiming_const pcan_usb_pro_fd_data_const = { |
| 1300 |
.name = "pcan_usb_pro_fd", |
| 1301 |
.tseg1_min = 1, |
| 1302 |
.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), |
| 1303 |
.tseg2_min = 1, |
| 1304 |
.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), |
| 1305 |
.sjw_max = (1 << PUCAN_TFAST_SJW_BITS), |
| 1306 |
.brp_min = 1, |
| 1307 |
.brp_max = (1 << PUCAN_TFAST_BRP_BITS), |
| 1308 |
.brp_inc = 1, |
| 1309 |
}; |
| 1310 |
|
| 1311 |
const struct peak_usb_adapter pcan_usb_pro_fd = { |
| 1312 |
.name = "PCAN-USB Pro FD", |
| 1313 |
.device_id = PCAN_USBPROFD_PRODUCT_ID, |
| 1314 |
.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT, |
| 1315 |
.ctrlmode_supported = CAN_CTRLMODE_FD | |
| 1316 |
CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | |
| 1317 |
CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, |
| 1318 |
.clock = { |
| 1319 |
.freq = PCAN_UFD_CRYSTAL_HZ, |
| 1320 |
}, |
| 1321 |
.bittiming_const = &pcan_usb_pro_fd_const, |
| 1322 |
.data_bittiming_const = &pcan_usb_pro_fd_data_const, |
| 1323 |
|
| 1324 |
/* size of device private data */ |
| 1325 |
.sizeof_dev_private = sizeof(struct pcan_usb_fd_device), |
| 1326 |
|
| 1327 |
.ethtool_ops = &pcan_usb_fd_ethtool_ops, |
| 1328 |
|
| 1329 |
/* timestamps usage */ |
| 1330 |
.ts_used_bits = 32, |
| 1331 |
.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ |
| 1332 |
.us_per_ts_shift = 0, |
| 1333 |
|
| 1334 |
/* give here messages in/out endpoints */ |
| 1335 |
.ep_msg_in = PCAN_USBPRO_EP_MSGIN, |
| 1336 |
.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1}, |
| 1337 |
|
| 1338 |
/* size of rx/tx usb buffers */ |
| 1339 |
.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, |
| 1340 |
.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, |
| 1341 |
|
| 1342 |
/* device callbacks */ |
| 1343 |
.intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ |
| 1344 |
.dev_init = pcan_usb_fd_init, |
| 1345 |
|
| 1346 |
.dev_exit = pcan_usb_fd_exit, |
| 1347 |
.dev_free = pcan_usb_fd_free, |
| 1348 |
.dev_set_bus = pcan_usb_fd_set_bus, |
| 1349 |
.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, |
| 1350 |
.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, |
| 1351 |
.dev_get_can_channel_id = pcan_usb_fd_get_can_channel_id, |
| 1352 |
.dev_set_can_channel_id = pcan_usb_fd_set_can_channel_id, |
| 1353 |
.dev_decode_buf = pcan_usb_fd_decode_buf, |
| 1354 |
.dev_start = pcan_usb_fd_start, |
| 1355 |
.dev_stop = pcan_usb_fd_stop, |
| 1356 |
.dev_restart_async = pcan_usb_fd_restart_async, |
| 1357 |
.dev_encode_msg = pcan_usb_fd_encode_msg, |
| 1358 |
|
| 1359 |
.do_get_berr_counter = pcan_usb_fd_get_berr_counter, |
| 1360 |
}; |
| 1361 |
|
| 1362 |
/* describes the PCAN-USB X6 adapter */ |
| 1363 |
static const struct can_bittiming_const pcan_usb_x6_const = { |
| 1364 |
.name = "pcan_usb_x6", |
| 1365 |
.tseg1_min = 1, |
| 1366 |
.tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), |
| 1367 |
.tseg2_min = 1, |
| 1368 |
.tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), |
| 1369 |
.sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), |
| 1370 |
.brp_min = 1, |
| 1371 |
.brp_max = (1 << PUCAN_TSLOW_BRP_BITS), |
| 1372 |
.brp_inc = 1, |
| 1373 |
}; |
| 1374 |
|
| 1375 |
static const struct can_bittiming_const pcan_usb_x6_data_const = { |
| 1376 |
.name = "pcan_usb_x6", |
| 1377 |
.tseg1_min = 1, |
| 1378 |
.tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), |
| 1379 |
.tseg2_min = 1, |
| 1380 |
.tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), |
| 1381 |
.sjw_max = (1 << PUCAN_TFAST_SJW_BITS), |
| 1382 |
.brp_min = 1, |
| 1383 |
.brp_max = (1 << PUCAN_TFAST_BRP_BITS), |
| 1384 |
.brp_inc = 1, |
| 1385 |
}; |
| 1386 |
|
| 1387 |
const struct peak_usb_adapter pcan_usb_x6 = { |
| 1388 |
.name = "PCAN-USB X6", |
| 1389 |
.device_id = PCAN_USBX6_PRODUCT_ID, |
| 1390 |
.ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT, |
| 1391 |
.ctrlmode_supported = CAN_CTRLMODE_FD | |
| 1392 |
CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | |
| 1393 |
CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, |
| 1394 |
.clock = { |
| 1395 |
.freq = PCAN_UFD_CRYSTAL_HZ, |
| 1396 |
}, |
| 1397 |
.bittiming_const = &pcan_usb_x6_const, |
| 1398 |
.data_bittiming_const = &pcan_usb_x6_data_const, |
| 1399 |
|
| 1400 |
/* size of device private data */ |
| 1401 |
.sizeof_dev_private = sizeof(struct pcan_usb_fd_device), |
| 1402 |
|
| 1403 |
.ethtool_ops = &pcan_usb_fd_ethtool_ops, |
| 1404 |
|
| 1405 |
/* timestamps usage */ |
| 1406 |
.ts_used_bits = 32, |
| 1407 |
.us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ |
| 1408 |
.us_per_ts_shift = 0, |
| 1409 |
|
| 1410 |
/* give here messages in/out endpoints */ |
| 1411 |
.ep_msg_in = PCAN_USBPRO_EP_MSGIN, |
| 1412 |
.ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1}, |
| 1413 |
|
| 1414 |
/* size of rx/tx usb buffers */ |
| 1415 |
.rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, |
| 1416 |
.tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, |
| 1417 |
|
| 1418 |
/* device callbacks */ |
| 1419 |
.intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ |
| 1420 |
.dev_init = pcan_usb_fd_init, |
| 1421 |
|
| 1422 |
.dev_exit = pcan_usb_fd_exit, |
| 1423 |
.dev_free = pcan_usb_fd_free, |
| 1424 |
.dev_set_bus = pcan_usb_fd_set_bus, |
| 1425 |
.dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, |
| 1426 |
.dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, |
| 1427 |
.dev_get_can_channel_id = pcan_usb_fd_get_can_channel_id, |
| 1428 |
.dev_set_can_channel_id = pcan_usb_fd_set_can_channel_id, |
| 1429 |
.dev_decode_buf = pcan_usb_fd_decode_buf, |
| 1430 |
.dev_start = pcan_usb_fd_start, |
| 1431 |
.dev_stop = pcan_usb_fd_stop, |
| 1432 |
.dev_restart_async = pcan_usb_fd_restart_async, |
| 1433 |
.dev_encode_msg = pcan_usb_fd_encode_msg, |
| 1434 |
|
| 1435 |
.do_get_berr_counter = pcan_usb_fd_get_berr_counter, |
| 1436 |
}; |