aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/can
diff options
context:
space:
mode:
authorStephane Grosjean <s.grosjean@peak-system.com>2015-01-23 05:31:25 -0500
committerMarc Kleine-Budde <mkl@pengutronix.de>2015-01-28 08:47:06 -0500
commit0a25e1f4f18566b750ebd3ae995af64e23111e63 (patch)
treea0921092990874e482a05ecd29ce5b66bcd82c3c /drivers/net/can
parentfaa233d902bb97ac034fa2580e6e0934ccf98cf3 (diff)
can: peak_usb: add support for PEAK new CANFD USB adapters
Add support for the following new PEAK-System technik CANFD USB adapters: PCAN-USB FD single CANFD channel USB adapter PCAN-USB Pro FD dual CANFD channels USB adapter Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Acked-by: Andri Yngvason <andri.yngvason@marel.com> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Diffstat (limited to 'drivers/net/can')
-rw-r--r--drivers/net/can/usb/Kconfig14
-rw-r--r--drivers/net/can/usb/peak_usb/Makefile2
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_ucan.h222
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.c4
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.h4
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_fd.c1095
6 files changed, 1337 insertions, 4 deletions
diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig
index f6f55004fe2b..bcb272f6c68a 100644
--- a/drivers/net/can/usb/Kconfig
+++ b/drivers/net/can/usb/Kconfig
@@ -59,10 +59,18 @@ config CAN_KVASER_USB
59 module will be called kvaser_usb. 59 module will be called kvaser_usb.
60 60
61config CAN_PEAK_USB 61config CAN_PEAK_USB
62 tristate "PEAK PCAN-USB/USB Pro interfaces" 62 tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
63 ---help--- 63 ---help---
64 This driver supports the PCAN-USB and PCAN-USB Pro adapters 64 This driver supports the PEAK-System Technik USB adapters that enable
65 from PEAK-System Technik (http://www.peak-system.com). 65 access to the CAN bus, with repect to the CAN 2.0b and/or CAN-FD
66 standards, that is:
67
68 PCAN-USB single CAN 2.0b channel USB adapter
69 PCAN-USB Pro dual CAN 2.0b channels USB adapter
70 PCAN-USB FD single CAN-FD channel USB adapter
71 PCAN-USB Pro FD dual CAN-FD channels USB adapter
72
73 (see also http://www.peak-system.com).
66 74
67config CAN_8DEV_USB 75config CAN_8DEV_USB
68 tristate "8 devices USB2CAN interface" 76 tristate "8 devices USB2CAN interface"
diff --git a/drivers/net/can/usb/peak_usb/Makefile b/drivers/net/can/usb/peak_usb/Makefile
index 1aefbc88d643..1839e9ca62e7 100644
--- a/drivers/net/can/usb/peak_usb/Makefile
+++ b/drivers/net/can/usb/peak_usb/Makefile
@@ -1,2 +1,2 @@
1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o 1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
2peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o 2peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o pcan_usb_fd.o
diff --git a/drivers/net/can/usb/peak_usb/pcan_ucan.h b/drivers/net/can/usb/peak_usb/pcan_ucan.h
new file mode 100644
index 000000000000..1ba7c25002e1
--- /dev/null
+++ b/drivers/net/can/usb/peak_usb/pcan_ucan.h
@@ -0,0 +1,222 @@
1/*
2 * CAN driver for PEAK System micro-CAN based adapters
3 *
4 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
5 * Copyright (C) 2011-2013 Stephane Grosjean <s.grosjean@peak-system.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16#ifndef PUCAN_H
17#define PUCAN_H
18
19/* uCAN commands opcodes list (low-order 10 bits) */
20#define PUCAN_CMD_NOP 0x000
21#define PUCAN_CMD_RESET_MODE 0x001
22#define PUCAN_CMD_NORMAL_MODE 0x002
23#define PUCAN_CMD_LISTEN_ONLY_MODE 0x003
24#define PUCAN_CMD_TIMING_SLOW 0x004
25#define PUCAN_CMD_TIMING_FAST 0x005
26#define PUCAN_CMD_FILTER_STD 0x008
27#define PUCAN_CMD_TX_ABORT 0x009
28#define PUCAN_CMD_WR_ERR_CNT 0x00a
29#define PUCAN_CMD_RX_FRAME_ENABLE 0x00b
30#define PUCAN_CMD_RX_FRAME_DISABLE 0x00c
31#define PUCAN_CMD_END_OF_COLLECTION 0x3ff
32
33/* uCAN received messages list */
34#define PUCAN_MSG_CAN_RX 0x0001
35#define PUCAN_MSG_ERROR 0x0002
36#define PUCAN_MSG_STATUS 0x0003
37#define PUCAN_MSG_BUSLOAD 0x0004
38#define PUCAN_MSG_CAN_TX 0x1000
39
40/* uCAN command common header */
41struct __packed pucan_command {
42 __le16 opcode_channel;
43 u16 args[3];
44};
45
46/* uCAN TIMING_SLOW command fields */
47#define PUCAN_TSLOW_SJW_T(s, t) (((s) & 0xf) | ((!!(t)) << 7))
48#define PUCAN_TSLOW_TSEG2(t) ((t) & 0xf)
49#define PUCAN_TSLOW_TSEG1(t) ((t) & 0x3f)
50#define PUCAN_TSLOW_BRP(b) ((b) & 0x3ff)
51
52struct __packed pucan_timing_slow {
53 __le16 opcode_channel;
54
55 u8 ewl; /* Error Warning limit */
56 u8 sjw_t; /* Sync Jump Width + Triple sampling */
57 u8 tseg2; /* Timing SEGment 2 */
58 u8 tseg1; /* Timing SEGment 1 */
59
60 __le16 brp; /* BaudRate Prescaler */
61};
62
63/* uCAN TIMING_FAST command fields */
64#define PUCAN_TFAST_SJW(s) ((s) & 0x3)
65#define PUCAN_TFAST_TSEG2(t) ((t) & 0x7)
66#define PUCAN_TFAST_TSEG1(t) ((t) & 0xf)
67#define PUCAN_TFAST_BRP(b) ((b) & 0x3ff)
68
69struct __packed pucan_timing_fast {
70 __le16 opcode_channel;
71
72 u8 unused;
73 u8 sjw; /* Sync Jump Width */
74 u8 tseg2; /* Timing SEGment 2 */
75 u8 tseg1; /* Timing SEGment 1 */
76
77 __le16 brp; /* BaudRate Prescaler */
78};
79
80/* uCAN FILTER_STD command fields */
81#define PUCAN_FLTSTD_ROW_IDX_BITS 6
82
83struct __packed pucan_filter_std {
84 __le16 opcode_channel;
85
86 __le16 idx;
87 __le32 mask; /* CAN-ID bitmask in idx range */
88};
89
90/* uCAN WR_ERR_CNT command fields */
91#define PUCAN_WRERRCNT_TE 0x4000 /* Tx error cntr write Enable */
92#define PUCAN_WRERRCNT_RE 0x8000 /* Rx error cntr write Enable */
93
94struct __packed pucan_wr_err_cnt {
95 __le16 opcode_channel;
96
97 __le16 sel_mask;
98 u8 tx_counter; /* Tx error counter new value */
99 u8 rx_counter; /* Rx error counter new value */
100
101 u16 unused;
102};
103
104/* uCAN RX_FRAME_ENABLE command fields */
105#define PUCAN_FLTEXT_ERROR 0x0001
106#define PUCAN_FLTEXT_BUSLOAD 0x0002
107
108struct __packed pucan_filter_ext {
109 __le16 opcode_channel;
110
111 __le16 ext_mask;
112 u32 unused;
113};
114
115/* uCAN received messages global format */
116struct __packed pucan_msg {
117 __le16 size;
118 __le16 type;
119 __le32 ts_low;
120 __le32 ts_high;
121};
122
123/* uCAN flags for CAN/CANFD messages */
124#define PUCAN_MSG_SELF_RECEIVE 0x80
125#define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */
126#define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */
127#define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */
128#define PUCAN_MSG_SINGLE_SHOT 0x08
129#define PUCAN_MSG_LOOPED_BACK 0x04
130#define PUCAN_MSG_EXT_ID 0x02
131#define PUCAN_MSG_RTR 0x01
132
133struct __packed pucan_rx_msg {
134 __le16 size;
135 __le16 type;
136 __le32 ts_low;
137 __le32 ts_high;
138 __le32 tag_low;
139 __le32 tag_high;
140 u8 channel_dlc;
141 u8 client;
142 __le16 flags;
143 __le32 can_id;
144 u8 d[0];
145};
146
147/* uCAN error types */
148#define PUCAN_ERMSG_BIT_ERROR 0
149#define PUCAN_ERMSG_FORM_ERROR 1
150#define PUCAN_ERMSG_STUFF_ERROR 2
151#define PUCAN_ERMSG_OTHER_ERROR 3
152#define PUCAN_ERMSG_ERR_CNT_DEC 4
153
154struct __packed pucan_error_msg {
155 __le16 size;
156 __le16 type;
157 __le32 ts_low;
158 __le32 ts_high;
159 u8 channel_type_d;
160 u8 code_g;
161 u8 tx_err_cnt;
162 u8 rx_err_cnt;
163};
164
165#define PUCAN_BUS_PASSIVE 0x20
166#define PUCAN_BUS_WARNING 0x40
167#define PUCAN_BUS_BUSOFF 0x80
168
169struct __packed pucan_status_msg {
170 __le16 size;
171 __le16 type;
172 __le32 ts_low;
173 __le32 ts_high;
174 u8 channel_p_w_b;
175 u8 unused[3];
176};
177
178/* uCAN transmitted message format */
179#define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4))
180
181struct __packed pucan_tx_msg {
182 __le16 size;
183 __le16 type;
184 __le32 tag_low;
185 __le32 tag_high;
186 u8 channel_dlc;
187 u8 client;
188 __le16 flags;
189 __le32 can_id;
190 u8 d[0];
191};
192
193/* build the cmd opcode_channel field with respect to the correct endianness */
194static inline __le16 pucan_cmd_opcode_channel(struct peak_usb_device *dev,
195 int opcode)
196{
197 return cpu_to_le16(((dev->ctrl_idx) << 12) | ((opcode) & 0x3ff));
198}
199
200/* return the channel number part from any received message channel_dlc field */
201static inline int pucan_msg_get_channel(struct pucan_rx_msg *rm)
202{
203 return rm->channel_dlc & 0xf;
204}
205
206/* return the dlc value from any received message channel_dlc field */
207static inline int pucan_msg_get_dlc(struct pucan_rx_msg *rm)
208{
209 return rm->channel_dlc >> 4;
210}
211
212static inline int pucan_ermsg_get_channel(struct pucan_error_msg *em)
213{
214 return em->channel_type_d & 0x0f;
215}
216
217static inline int pucan_stmsg_get_channel(struct pucan_status_msg *sm)
218{
219 return sm->channel_p_w_b & 0x0f;
220}
221
222#endif
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index 7587337a3b42..7921cff93a63 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -37,6 +37,8 @@ MODULE_LICENSE("GPL v2");
37static struct usb_device_id peak_usb_table[] = { 37static struct usb_device_id peak_usb_table[] = {
38 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)}, 38 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)},
39 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)}, 39 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)},
40 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)},
41 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)},
40 {} /* Terminating entry */ 42 {} /* Terminating entry */
41}; 43};
42 44
@@ -46,6 +48,8 @@ MODULE_DEVICE_TABLE(usb, peak_usb_table);
46static const struct peak_usb_adapter *const peak_usb_adapters_list[] = { 48static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
47 &pcan_usb, 49 &pcan_usb,
48 &pcan_usb_pro, 50 &pcan_usb_pro,
51 &pcan_usb_fd,
52 &pcan_usb_pro_fd,
49}; 53};
50 54
51/* 55/*
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.h b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
index 0e76b84fb786..9e624f05ad4d 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
@@ -25,6 +25,8 @@
25/* supported device ids. */ 25/* supported device ids. */
26#define PCAN_USB_PRODUCT_ID 0x000c 26#define PCAN_USB_PRODUCT_ID 0x000c
27#define PCAN_USBPRO_PRODUCT_ID 0x000d 27#define PCAN_USBPRO_PRODUCT_ID 0x000d
28#define PCAN_USBPROFD_PRODUCT_ID 0x0011
29#define PCAN_USBFD_PRODUCT_ID 0x0012
28 30
29#define PCAN_USB_DRIVER_NAME "peak_usb" 31#define PCAN_USB_DRIVER_NAME "peak_usb"
30 32
@@ -86,6 +88,8 @@ struct peak_usb_adapter {
86 88
87extern const struct peak_usb_adapter pcan_usb; 89extern const struct peak_usb_adapter pcan_usb;
88extern const struct peak_usb_adapter pcan_usb_pro; 90extern const struct peak_usb_adapter pcan_usb_pro;
91extern const struct peak_usb_adapter pcan_usb_fd;
92extern const struct peak_usb_adapter pcan_usb_pro_fd;
89 93
90struct peak_time_ref { 94struct peak_time_ref {
91 struct timeval tv_host_0, tv_host; 95 struct timeval tv_host_0, tv_host;
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
new file mode 100644
index 000000000000..962c3f027383
--- /dev/null
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
@@ -0,0 +1,1095 @@
1/*
2 * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter
3 *
4 * Copyright (C) 2013-2014 Stephane Grosjean <s.grosjean@peak-system.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 */
15#include <linux/netdevice.h>
16#include <linux/usb.h>
17#include <linux/module.h>
18
19#include <linux/can.h>
20#include <linux/can/dev.h>
21#include <linux/can/error.h>
22
23#include "pcan_usb_core.h"
24#include "pcan_usb_pro.h"
25#include "pcan_ucan.h"
26
27MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro FD adapter");
29
30#define PCAN_USBPROFD_CHANNEL_COUNT 2
31#define PCAN_USBFD_CHANNEL_COUNT 1
32
33/* PCAN-USB Pro FD adapter internal clock (Hz) */
34#define PCAN_UFD_CRYSTAL_HZ 80000000
35
36#define PCAN_UFD_CMD_BUFFER_SIZE 512
37#define PCAN_UFD_LOSPD_PKT_SIZE 64
38
39/* PCAN-USB Pro FD command timeout (ms.) */
40#define PCAN_UFD_CMD_TIMEOUT_MS 1000
41
42/* PCAN-USB Pro FD rx/tx buffers size */
43#define PCAN_UFD_RX_BUFFER_SIZE 2048
44#define PCAN_UFD_TX_BUFFER_SIZE 512
45
46/* read some versions info from the hw devcie */
47struct __packed pcan_ufd_fw_info {
48 __le16 size_of; /* sizeof this */
49 __le16 type; /* type of this structure */
50 u8 hw_type; /* Type of hardware (HW_TYPE_xxx) */
51 u8 bl_version[3]; /* Bootloader version */
52 u8 hw_version; /* Hardware version (PCB) */
53 u8 fw_version[3]; /* Firmware version */
54 __le32 dev_id[2]; /* "device id" per CAN */
55 __le32 ser_no; /* S/N */
56 __le32 flags; /* special functions */
57};
58
59/* handle device specific info used by the netdevices */
60struct pcan_usb_fd_if {
61 struct peak_usb_device *dev[PCAN_USB_MAX_CHANNEL];
62 struct pcan_ufd_fw_info fw_info;
63 struct peak_time_ref time_ref;
64 int cm_ignore_count;
65 int dev_opened_count;
66};
67
68/* device information */
69struct pcan_usb_fd_device {
70 struct peak_usb_device dev;
71 struct can_berr_counter bec;
72 struct pcan_usb_fd_if *usb_if;
73 u8 *cmd_buffer_addr;
74};
75
76/* Extended USB commands (non uCAN commands) */
77
78/* Clock Modes command */
79#define PCAN_UFD_CMD_CLK_SET 0x80
80
81#define PCAN_UFD_CLK_80MHZ 0x0
82#define PCAN_UFD_CLK_60MHZ 0x1
83#define PCAN_UFD_CLK_40MHZ 0x2
84#define PCAN_UFD_CLK_30MHZ 0x3
85#define PCAN_UFD_CLK_24MHZ 0x4
86#define PCAN_UFD_CLK_20MHZ 0x5
87#define PCAN_UFD_CLK_DEF PCAN_UFD_CLK_80MHZ
88
89struct __packed pcan_ufd_clock {
90 __le16 opcode_channel;
91
92 u8 mode;
93 u8 unused[5];
94};
95
96/* LED control command */
97#define PCAN_UFD_CMD_LED_SET 0x86
98
99#define PCAN_UFD_LED_DEV 0x00
100#define PCAN_UFD_LED_FAST 0x01
101#define PCAN_UFD_LED_SLOW 0x02
102#define PCAN_UFD_LED_ON 0x03
103#define PCAN_UFD_LED_OFF 0x04
104#define PCAN_UFD_LED_DEF PCAN_UFD_LED_DEV
105
106struct __packed pcan_ufd_led {
107 __le16 opcode_channel;
108
109 u8 mode;
110 u8 unused[5];
111};
112
113/* Extended usage of uCAN commands CMD_RX_FRAME_xxxABLE for PCAN-USB Pro FD */
114#define PCAN_UFD_FLTEXT_CALIBRATION 0x8000
115
116struct __packed pcan_ufd_filter_ext {
117 __le16 opcode_channel;
118
119 __le16 ext_mask;
120 u16 unused;
121 __le16 usb_mask;
122};
123
124/* Extended usage of uCAN messages for PCAN-USB Pro FD */
125#define PCAN_UFD_MSG_CALIBRATION 0x100
126
127struct __packed pcan_ufd_ts_msg {
128 __le16 size;
129 __le16 type;
130 __le32 ts_low;
131 __le32 ts_high;
132 __le16 usb_frame_index;
133 u16 unused;
134};
135
136#define PCAN_UFD_MSG_OVERRUN 0x101
137
138#define PCAN_UFD_OVMSG_CHANNEL(o) ((o)->channel & 0xf)
139
140struct __packed pcan_ufd_ovr_msg {
141 __le16 size;
142 __le16 type;
143 __le32 ts_low;
144 __le32 ts_high;
145 u8 channel;
146 u8 unused[3];
147};
148
149static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
150{
151 return om->channel & 0xf;
152}
153
154/* Clock mode frequency values */
155static const u32 pcan_usb_fd_clk_freq[6] = {
156 [PCAN_UFD_CLK_80MHZ] = 80000000,
157 [PCAN_UFD_CLK_60MHZ] = 60000000,
158 [PCAN_UFD_CLK_40MHZ] = 40000000,
159 [PCAN_UFD_CLK_30MHZ] = 30000000,
160 [PCAN_UFD_CLK_24MHZ] = 24000000,
161 [PCAN_UFD_CLK_20MHZ] = 20000000
162};
163
164/* return a device USB interface */
165static inline
166struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev)
167{
168 struct pcan_usb_fd_device *pdev =
169 container_of(dev, struct pcan_usb_fd_device, dev);
170 return pdev->usb_if;
171}
172
173/* return a device USB commands buffer */
174static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev)
175{
176 struct pcan_usb_fd_device *pdev =
177 container_of(dev, struct pcan_usb_fd_device, dev);
178 return pdev->cmd_buffer_addr;
179}
180
181/* send PCAN-USB Pro FD commands synchronously */
182static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail)
183{
184 void *cmd_head = pcan_usb_fd_cmd_buffer(dev);
185 int err;
186 u8 *packet_ptr;
187 int i, n = 1, packet_len;
188 ptrdiff_t cmd_len;
189
190 /* usb device unregistered? */
191 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
192 return 0;
193
194 /* if a packet is not filled completely by commands, the command list
195 * is terminated with an "end of collection" record.
196 */
197 cmd_len = cmd_tail - cmd_head;
198 if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) {
199 memset(cmd_tail, 0xff, sizeof(u64));
200 cmd_len += sizeof(u64);
201 }
202
203 packet_ptr = cmd_head;
204
205 /* firmware is not able to re-assemble 512 bytes buffer in full-speed */
206 if ((dev->udev->speed != USB_SPEED_HIGH) &&
207 (cmd_len > PCAN_UFD_LOSPD_PKT_SIZE)) {
208 packet_len = PCAN_UFD_LOSPD_PKT_SIZE;
209 n += cmd_len / packet_len;
210 } else {
211 packet_len = cmd_len;
212 }
213
214 for (i = 0; i < n; i++) {
215 err = usb_bulk_msg(dev->udev,
216 usb_sndbulkpipe(dev->udev,
217 PCAN_USBPRO_EP_CMDOUT),
218 packet_ptr, packet_len,
219 NULL, PCAN_UFD_CMD_TIMEOUT_MS);
220 if (err) {
221 netdev_err(dev->netdev,
222 "sending command failure: %d\n", err);
223 break;
224 }
225
226 packet_ptr += packet_len;
227 }
228
229 return err;
230}
231
232/* build the commands list in the given buffer, to enter operational mode */
233static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
234{
235 struct pucan_wr_err_cnt *prc;
236 struct pucan_command *cmd;
237 u8 *pc = buf;
238
239 /* 1st, reset error counters: */
240 prc = (struct pucan_wr_err_cnt *)pc;
241 prc->opcode_channel = pucan_cmd_opcode_channel(dev,
242 PUCAN_CMD_WR_ERR_CNT);
243
244 /* select both counters */
245 prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
246
247 /* and reset their values */
248 prc->tx_counter = 0;
249 prc->rx_counter = 0;
250
251 /* moves the pointer forward */
252 pc += sizeof(struct pucan_wr_err_cnt);
253
254 /* next, go back to operational mode */
255 cmd = (struct pucan_command *)pc;
256 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
257 (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ?
258 PUCAN_CMD_LISTEN_ONLY_MODE :
259 PUCAN_CMD_NORMAL_MODE);
260 pc += sizeof(struct pucan_command);
261
262 return pc - buf;
263}
264
265/* set CAN bus on/off */
266static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
267{
268 u8 *pc = pcan_usb_fd_cmd_buffer(dev);
269 int l;
270
271 if (onoff) {
272 /* build the cmds list to enter operational mode */
273 l = pcan_usb_fd_build_restart_cmd(dev, pc);
274 } else {
275 struct pucan_command *cmd = (struct pucan_command *)pc;
276
277 /* build cmd to go back to reset mode */
278 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
279 PUCAN_CMD_RESET_MODE);
280 l = sizeof(struct pucan_command);
281 }
282
283 /* send the command */
284 return pcan_usb_fd_send_cmd(dev, pc + l);
285}
286
287/* set filtering masks:
288 *
289 * idx in range [0..63] selects a row #idx, all rows otherwise
290 * mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s)
291 *
292 * Each bit of this 64 x 32 bits array defines a CANID value:
293 *
294 * bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while
295 * bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded.
296 */
297static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
298 u32 mask)
299{
300 struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
301 int i, n;
302
303 /* select all rows when idx is out of range [0..63] */
304 if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) {
305 n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS;
306 idx = 0;
307
308 /* select the row (and only the row) otherwise */
309 } else {
310 n = idx + 1;
311 }
312
313 for (i = idx; i < n; i++, cmd++) {
314 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
315 PUCAN_CMD_FILTER_STD);
316 cmd->idx = cpu_to_le16(i);
317 cmd->mask = cpu_to_le32(mask);
318 }
319
320 /* send the command */
321 return pcan_usb_fd_send_cmd(dev, cmd);
322}
323
324/* set/unset notifications filter:
325 *
326 * onoff sets(1)/unset(0) notifications
327 * mask each bit defines a kind of notification to set/unset
328 */
329static int pcan_usb_fd_set_filter_ext(struct peak_usb_device *dev,
330 bool onoff, u16 ext_mask, u16 usb_mask)
331{
332 struct pcan_ufd_filter_ext *cmd = pcan_usb_fd_cmd_buffer(dev);
333
334 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
335 (onoff) ? PUCAN_CMD_RX_FRAME_ENABLE :
336 PUCAN_CMD_RX_FRAME_DISABLE);
337
338 cmd->ext_mask = cpu_to_le16(ext_mask);
339 cmd->usb_mask = cpu_to_le16(usb_mask);
340
341 /* send the command */
342 return pcan_usb_fd_send_cmd(dev, ++cmd);
343}
344
345/* setup LED control */
346static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
347{
348 struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
349
350 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
351 PCAN_UFD_CMD_LED_SET);
352 cmd->mode = led_mode;
353
354 /* send the command */
355 return pcan_usb_fd_send_cmd(dev, ++cmd);
356}
357
358/* set CAN clock domain */
359static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
360 u8 clk_mode)
361{
362 struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
363
364 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
365 PCAN_UFD_CMD_CLK_SET);
366 cmd->mode = clk_mode;
367
368 /* send the command */
369 return pcan_usb_fd_send_cmd(dev, ++cmd);
370}
371
372/* set bittiming for CAN and CAN-FD header */
373static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
374 struct can_bittiming *bt)
375{
376 struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
377
378 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
379 PUCAN_CMD_TIMING_SLOW);
380 cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1,
381 dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
382
383 cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1);
384 cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
385 cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1));
386
387 cmd->ewl = 96; /* default */
388
389 /* send the command */
390 return pcan_usb_fd_send_cmd(dev, ++cmd);
391}
392
393/* set CAN-FD bittiming for data */
394static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
395 struct can_bittiming *bt)
396{
397 struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
398
399 cmd->opcode_channel = pucan_cmd_opcode_channel(dev,
400 PUCAN_CMD_TIMING_FAST);
401 cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1);
402 cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1);
403 cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
404 cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1));
405
406 /* send the command */
407 return pcan_usb_fd_send_cmd(dev, ++cmd);
408}
409
410/* handle restart but in asynchronously way
411 * (uses PCAN-USB Pro code to complete asynchronous request)
412 */
413static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
414 struct urb *urb, u8 *buf)
415{
416 u8 *pc = buf;
417
418 /* build the entire cmds list in the provided buffer, to go back into
419 * operational mode.
420 */
421 pc += pcan_usb_fd_build_restart_cmd(dev, pc);
422
423 /* add EOC */
424 memset(pc, 0xff, sizeof(struct pucan_command));
425 pc += sizeof(struct pucan_command);
426
427 /* complete the URB */
428 usb_fill_bulk_urb(urb, dev->udev,
429 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
430 buf, pc - buf,
431 pcan_usb_pro_restart_complete, dev);
432
433 /* and submit it. */
434 return usb_submit_urb(urb, GFP_ATOMIC);
435}
436
437static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
438{
439 struct pcan_usb_fd_device *pdev =
440 container_of(dev, struct pcan_usb_fd_device, dev);
441
442 pdev->cmd_buffer_addr[0] = 0;
443 pdev->cmd_buffer_addr[1] = !!loaded;
444
445 return pcan_usb_pro_send_req(dev,
446 PCAN_USBPRO_REQ_FCT,
447 PCAN_USBPRO_FCT_DRVLD,
448 pdev->cmd_buffer_addr,
449 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
450}
451
452static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
453 struct pucan_msg *rx_msg)
454{
455 struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
456 struct peak_usb_device *dev = usb_if->dev[pucan_msg_get_channel(rm)];
457 struct net_device *netdev = dev->netdev;
458 struct canfd_frame *cfd;
459 struct sk_buff *skb;
460 const u16 rx_msg_flags = le16_to_cpu(rm->flags);
461
462 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
463 /* CANFD frame case */
464 skb = alloc_canfd_skb(netdev, &cfd);
465 if (!skb)
466 return -ENOMEM;
467
468 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
469 cfd->flags |= CANFD_BRS;
470
471 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
472 cfd->flags |= CANFD_ESI;
473
474 cfd->len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(rm)));
475 } else {
476 /* CAN 2.0 frame case */
477 skb = alloc_can_skb(netdev, (struct can_frame **)&cfd);
478 if (!skb)
479 return -ENOMEM;
480
481 cfd->len = get_can_dlc(pucan_msg_get_dlc(rm));
482 }
483
484 cfd->can_id = le32_to_cpu(rm->can_id);
485
486 if (rx_msg_flags & PUCAN_MSG_EXT_ID)
487 cfd->can_id |= CAN_EFF_FLAG;
488
489 if (rx_msg_flags & PUCAN_MSG_RTR)
490 cfd->can_id |= CAN_RTR_FLAG;
491 else
492 memcpy(cfd->data, rm->d, cfd->len);
493
494 peak_usb_netif_rx(skb, &usb_if->time_ref,
495 le32_to_cpu(rm->ts_low), le32_to_cpu(rm->ts_high));
496
497 netdev->stats.rx_packets++;
498 netdev->stats.rx_bytes += cfd->len;
499
500 return 0;
501}
502
503/* handle uCAN status message */
504static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
505 struct pucan_msg *rx_msg)
506{
507 struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
508 struct peak_usb_device *dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
509 struct pcan_usb_fd_device *pdev =
510 container_of(dev, struct pcan_usb_fd_device, dev);
511 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
512 enum can_state rx_state, tx_state;
513 struct net_device *netdev = dev->netdev;
514 struct can_frame *cf;
515 struct sk_buff *skb;
516
517 /* nothing should be sent while in BUS_OFF state */
518 if (dev->can.state == CAN_STATE_BUS_OFF)
519 return 0;
520
521 if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
522 new_state = CAN_STATE_BUS_OFF;
523 } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
524 new_state = CAN_STATE_ERROR_PASSIVE;
525 } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
526 new_state = CAN_STATE_ERROR_WARNING;
527 } else {
528 /* no error bit (so, no error skb, back to active state) */
529 dev->can.state = CAN_STATE_ERROR_ACTIVE;
530 pdev->bec.txerr = 0;
531 pdev->bec.rxerr = 0;
532 return 0;
533 }
534
535 /* state hasn't changed */
536 if (new_state == dev->can.state)
537 return 0;
538
539 /* handle bus state change */
540 tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
541 rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
542
543 /* allocate an skb to store the error frame */
544 skb = alloc_can_err_skb(netdev, &cf);
545 if (skb)
546 can_change_state(netdev, cf, tx_state, rx_state);
547
548 /* things must be done even in case of OOM */
549 if (new_state == CAN_STATE_BUS_OFF)
550 can_bus_off(netdev);
551
552 if (!skb)
553 return -ENOMEM;
554
555 peak_usb_netif_rx(skb, &usb_if->time_ref,
556 le32_to_cpu(sm->ts_low), le32_to_cpu(sm->ts_high));
557
558 netdev->stats.rx_packets++;
559 netdev->stats.rx_bytes += cf->can_dlc;
560
561 return 0;
562}
563
564/* handle uCAN error message */
565static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
566 struct pucan_msg *rx_msg)
567{
568 struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
569 struct peak_usb_device *dev = usb_if->dev[pucan_ermsg_get_channel(er)];
570 struct pcan_usb_fd_device *pdev =
571 container_of(dev, struct pcan_usb_fd_device, dev);
572
573 /* keep a trace of tx and rx error counters for later use */
574 pdev->bec.txerr = er->tx_err_cnt;
575 pdev->bec.rxerr = er->rx_err_cnt;
576
577 return 0;
578}
579
580/* handle uCAN overrun message */
581static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
582 struct pucan_msg *rx_msg)
583{
584 struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
585 struct peak_usb_device *dev = usb_if->dev[pufd_omsg_get_channel(ov)];
586 struct net_device *netdev = dev->netdev;
587 struct can_frame *cf;
588 struct sk_buff *skb;
589
590 /* allocate an skb to store the error frame */
591 skb = alloc_can_err_skb(netdev, &cf);
592 if (!skb)
593 return -ENOMEM;
594
595 cf->can_id |= CAN_ERR_CRTL;
596 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
597
598 peak_usb_netif_rx(skb, &usb_if->time_ref,
599 le32_to_cpu(ov->ts_low), le32_to_cpu(ov->ts_high));
600
601 netdev->stats.rx_over_errors++;
602 netdev->stats.rx_errors++;
603
604 return 0;
605}
606
607/* handle USB calibration message */
608static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
609 struct pucan_msg *rx_msg)
610{
611 struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
612
613 /* should wait until clock is stabilized */
614 if (usb_if->cm_ignore_count > 0)
615 usb_if->cm_ignore_count--;
616 else
617 peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
618}
619
620/* callback for bulk IN urb */
621static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
622{
623 struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
624 struct net_device *netdev = dev->netdev;
625 struct pucan_msg *rx_msg;
626 u8 *msg_ptr, *msg_end;
627 int err = 0;
628
629 /* loop reading all the records from the incoming message */
630 msg_ptr = urb->transfer_buffer;
631 msg_end = urb->transfer_buffer + urb->actual_length;
632 for (; msg_ptr < msg_end;) {
633 u16 rx_msg_type, rx_msg_size;
634
635 rx_msg = (struct pucan_msg *)msg_ptr;
636 if (!rx_msg->size) {
637 /* null packet found: end of list */
638 break;
639 }
640
641 rx_msg_size = le16_to_cpu(rx_msg->size);
642 rx_msg_type = le16_to_cpu(rx_msg->type);
643
644 /* check if the record goes out of current packet */
645 if (msg_ptr + rx_msg_size > msg_end) {
646 netdev_err(netdev,
647 "got frag rec: should inc usb rx buf sze\n");
648 err = -EBADMSG;
649 break;
650 }
651
652 switch (rx_msg_type) {
653 case PUCAN_MSG_CAN_RX:
654 err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
655 if (err < 0)
656 goto fail;
657 break;
658
659 case PCAN_UFD_MSG_CALIBRATION:
660 pcan_usb_fd_decode_ts(usb_if, rx_msg);
661 break;
662
663 case PUCAN_MSG_ERROR:
664 err = pcan_usb_fd_decode_error(usb_if, rx_msg);
665 if (err < 0)
666 goto fail;
667 break;
668
669 case PUCAN_MSG_STATUS:
670 err = pcan_usb_fd_decode_status(usb_if, rx_msg);
671 if (err < 0)
672 goto fail;
673 break;
674
675 case PCAN_UFD_MSG_OVERRUN:
676 err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
677 if (err < 0)
678 goto fail;
679 break;
680
681 default:
682 netdev_err(netdev,
683 "unhandled msg type 0x%02x (%d): ignored\n",
684 rx_msg_type, rx_msg_type);
685 break;
686 }
687
688 msg_ptr += rx_msg_size;
689 }
690
691fail:
692 if (err)
693 pcan_dump_mem("received msg",
694 urb->transfer_buffer, urb->actual_length);
695 return err;
696}
697
698/* CAN/CANFD frames encoding callback */
699static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
700 struct sk_buff *skb, u8 *obuf, size_t *size)
701{
702 struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
703 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
704 u16 tx_msg_size, tx_msg_flags;
705 u8 can_dlc;
706
707 tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
708 tx_msg->size = cpu_to_le16(tx_msg_size);
709 tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
710
711 tx_msg_flags = 0;
712 if (cfd->can_id & CAN_EFF_FLAG) {
713 tx_msg_flags |= PUCAN_MSG_EXT_ID;
714 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
715 } else {
716 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
717 }
718
719 if (can_is_canfd_skb(skb)) {
720 /* considering a CANFD frame */
721 can_dlc = can_len2dlc(cfd->len);
722
723 tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
724
725 if (cfd->flags & CANFD_BRS)
726 tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
727
728 if (cfd->flags & CANFD_ESI)
729 tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
730 } else {
731 /* CAND 2.0 frames */
732 can_dlc = cfd->len;
733
734 if (cfd->can_id & CAN_RTR_FLAG)
735 tx_msg_flags |= PUCAN_MSG_RTR;
736 }
737
738 tx_msg->flags = cpu_to_le16(tx_msg_flags);
739 tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, can_dlc);
740 memcpy(tx_msg->d, cfd->data, cfd->len);
741
742 /* add null size message to tag the end (messages are 32-bits aligned)
743 */
744 tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
745
746 tx_msg->size = 0;
747
748 /* set the whole size of the USB packet to send */
749 *size = tx_msg_size + sizeof(u32);
750
751 return 0;
752}
753
754/* start the interface (last chance before set bus on) */
755static int pcan_usb_fd_start(struct peak_usb_device *dev)
756{
757 struct pcan_usb_fd_device *pdev =
758 container_of(dev, struct pcan_usb_fd_device, dev);
759 int err;
760
761 /* set filter mode: all acceptance */
762 err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
763 if (err)
764 return err;
765
766 /* opening first device: */
767 if (pdev->usb_if->dev_opened_count == 0) {
768 /* reset time_ref */
769 peak_usb_init_time_ref(&pdev->usb_if->time_ref,
770 &pcan_usb_pro_fd);
771
772 /* enable USB calibration messages */
773 err = pcan_usb_fd_set_filter_ext(dev, 1,
774 PUCAN_FLTEXT_ERROR,
775 PCAN_UFD_FLTEXT_CALIBRATION);
776 }
777
778 pdev->usb_if->dev_opened_count++;
779
780 /* reset cached error counters */
781 pdev->bec.txerr = 0;
782 pdev->bec.rxerr = 0;
783
784 return err;
785}
786
787/* socket callback used to copy berr counters values receieved through USB */
788static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
789 struct can_berr_counter *bec)
790{
791 struct peak_usb_device *dev = netdev_priv(netdev);
792 struct pcan_usb_fd_device *pdev =
793 container_of(dev, struct pcan_usb_fd_device, dev);
794
795 *bec = pdev->bec;
796
797 /* must return 0 */
798 return 0;
799}
800
801/* stop interface (last chance before set bus off) */
802static int pcan_usb_fd_stop(struct peak_usb_device *dev)
803{
804 struct pcan_usb_fd_device *pdev =
805 container_of(dev, struct pcan_usb_fd_device, dev);
806
807 /* turn off special msgs for that interface if no other dev opened */
808 if (pdev->usb_if->dev_opened_count == 1)
809 pcan_usb_fd_set_filter_ext(dev, 0,
810 PUCAN_FLTEXT_ERROR,
811 PCAN_UFD_FLTEXT_CALIBRATION);
812 pdev->usb_if->dev_opened_count--;
813
814 return 0;
815}
816
817/* called when probing, to initialize a device object */
818static int pcan_usb_fd_init(struct peak_usb_device *dev)
819{
820 struct pcan_usb_fd_device *pdev =
821 container_of(dev, struct pcan_usb_fd_device, dev);
822 int i, err = -ENOMEM;
823
824 /* do this for 1st channel only */
825 if (!dev->prev_siblings) {
826 /* allocate netdevices common structure attached to first one */
827 pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
828 if (!pdev->usb_if)
829 goto err_out;
830
831 /* allocate command buffer once for all for the interface */
832 pdev->cmd_buffer_addr = kmalloc(PCAN_UFD_CMD_BUFFER_SIZE,
833 GFP_KERNEL);
834 if (!pdev->cmd_buffer_addr)
835 goto err_out_1;
836
837 /* number of ts msgs to ignore before taking one into account */
838 pdev->usb_if->cm_ignore_count = 5;
839
840 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
841 PCAN_USBPRO_INFO_FW,
842 &pdev->usb_if->fw_info,
843 sizeof(pdev->usb_if->fw_info));
844 if (err) {
845 dev_err(dev->netdev->dev.parent,
846 "unable to read %s firmware info (err %d)\n",
847 dev->adapter->name, err);
848 goto err_out_2;
849 }
850
851 /* explicit use of dev_xxx() instead of netdev_xxx() here:
852 * information displayed are related to the device itself, not
853 * to the canx (channel) device.
854 */
855 dev_info(dev->netdev->dev.parent,
856 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
857 dev->adapter->name, pdev->usb_if->fw_info.hw_version,
858 pdev->usb_if->fw_info.fw_version[0],
859 pdev->usb_if->fw_info.fw_version[1],
860 pdev->usb_if->fw_info.fw_version[2],
861 dev->adapter->ctrl_count);
862
863 /* the currently supported hw is non-ISO */
864 dev->can.ctrlmode = CAN_CTRLMODE_FD_NON_ISO;
865
866 /* tell the hardware the can driver is running */
867 err = pcan_usb_fd_drv_loaded(dev, 1);
868 if (err) {
869 dev_err(dev->netdev->dev.parent,
870 "unable to tell %s driver is loaded (err %d)\n",
871 dev->adapter->name, err);
872 goto err_out_2;
873 }
874 } else {
875 /* otherwise, simply copy previous sibling's values */
876 struct pcan_usb_fd_device *ppdev =
877 container_of(dev->prev_siblings,
878 struct pcan_usb_fd_device, dev);
879
880 pdev->usb_if = ppdev->usb_if;
881 pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
882 }
883
884 pdev->usb_if->dev[dev->ctrl_idx] = dev;
885 dev->device_number =
886 le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
887
888 /* set clock domain */
889 for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
890 if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
891 break;
892
893 if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
894 dev_warn(dev->netdev->dev.parent,
895 "incompatible clock frequencies\n");
896 err = -EINVAL;
897 goto err_out_2;
898 }
899
900 pcan_usb_fd_set_clock_domain(dev, i);
901
902 /* set LED in default state (end of init phase) */
903 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
904
905 return 0;
906
907err_out_2:
908 kfree(pdev->cmd_buffer_addr);
909err_out_1:
910 kfree(pdev->usb_if);
911err_out:
912 return err;
913}
914
915/* called when driver module is being unloaded */
916static void pcan_usb_fd_exit(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 /* when rmmod called before unplug and if down, should reset things
922 * before leaving
923 */
924 if (dev->can.state != CAN_STATE_STOPPED) {
925 /* set bus off on the corresponding channel */
926 pcan_usb_fd_set_bus(dev, 0);
927 }
928
929 /* switch off corresponding CAN LEDs */
930 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
931
932 /* if channel #0 (only) */
933 if (dev->ctrl_idx == 0) {
934 /* turn off calibration message if any device were opened */
935 if (pdev->usb_if->dev_opened_count > 0)
936 pcan_usb_fd_set_filter_ext(dev, 0,
937 PUCAN_FLTEXT_ERROR,
938 PCAN_UFD_FLTEXT_CALIBRATION);
939
940 /* tell USB adapter that the driver is being unloaded */
941 pcan_usb_fd_drv_loaded(dev, 0);
942 }
943}
944
945/* called when the USB adapter is unplugged */
946static void pcan_usb_fd_free(struct peak_usb_device *dev)
947{
948 /* last device: can free shared objects now */
949 if (!dev->prev_siblings && !dev->next_siblings) {
950 struct pcan_usb_fd_device *pdev =
951 container_of(dev, struct pcan_usb_fd_device, dev);
952
953 /* free commands buffer */
954 kfree(pdev->cmd_buffer_addr);
955
956 /* free usb interface object */
957 kfree(pdev->usb_if);
958 }
959}
960
961/* describes the PCAN-USB FD adapter */
962const struct peak_usb_adapter pcan_usb_fd = {
963 .name = "PCAN-USB FD",
964 .device_id = PCAN_USBFD_PRODUCT_ID,
965 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
966 .ctrlmode_supported = CAN_CTRLMODE_FD |
967 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
968 .clock = {
969 .freq = PCAN_UFD_CRYSTAL_HZ,
970 },
971 .bittiming_const = {
972 .name = "pcan_usb_fd",
973 .tseg1_min = 1,
974 .tseg1_max = 64,
975 .tseg2_min = 1,
976 .tseg2_max = 16,
977 .sjw_max = 16,
978 .brp_min = 1,
979 .brp_max = 1024,
980 .brp_inc = 1,
981 },
982 .data_bittiming_const = {
983 .name = "pcan_usb_fd",
984 .tseg1_min = 1,
985 .tseg1_max = 16,
986 .tseg2_min = 1,
987 .tseg2_max = 8,
988 .sjw_max = 4,
989 .brp_min = 1,
990 .brp_max = 1024,
991 .brp_inc = 1,
992 },
993
994 /* size of device private data */
995 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
996
997 /* timestamps usage */
998 .ts_used_bits = 32,
999 .ts_period = 1000000, /* calibration period in ts. */
1000 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1001 .us_per_ts_shift = 0,
1002
1003 /* give here messages in/out endpoints */
1004 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1005 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1006
1007 /* size of rx/tx usb buffers */
1008 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1009 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1010
1011 /* device callbacks */
1012 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1013 .dev_init = pcan_usb_fd_init,
1014
1015 .dev_exit = pcan_usb_fd_exit,
1016 .dev_free = pcan_usb_fd_free,
1017 .dev_set_bus = pcan_usb_fd_set_bus,
1018 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1019 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1020 .dev_decode_buf = pcan_usb_fd_decode_buf,
1021 .dev_start = pcan_usb_fd_start,
1022 .dev_stop = pcan_usb_fd_stop,
1023 .dev_restart_async = pcan_usb_fd_restart_async,
1024 .dev_encode_msg = pcan_usb_fd_encode_msg,
1025
1026 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1027};
1028
1029/* describes the PCAN-USB Pro FD adapter */
1030const struct peak_usb_adapter pcan_usb_pro_fd = {
1031 .name = "PCAN-USB Pro FD",
1032 .device_id = PCAN_USBPROFD_PRODUCT_ID,
1033 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1034 .ctrlmode_supported = CAN_CTRLMODE_FD |
1035 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1036 .clock = {
1037 .freq = PCAN_UFD_CRYSTAL_HZ,
1038 },
1039 .bittiming_const = {
1040 .name = "pcan_usb_pro_fd",
1041 .tseg1_min = 1,
1042 .tseg1_max = 64,
1043 .tseg2_min = 1,
1044 .tseg2_max = 16,
1045 .sjw_max = 16,
1046 .brp_min = 1,
1047 .brp_max = 1024,
1048 .brp_inc = 1,
1049 },
1050 .data_bittiming_const = {
1051 .name = "pcan_usb_pro_fd",
1052 .tseg1_min = 1,
1053 .tseg1_max = 16,
1054 .tseg2_min = 1,
1055 .tseg2_max = 8,
1056 .sjw_max = 4,
1057 .brp_min = 1,
1058 .brp_max = 1024,
1059 .brp_inc = 1,
1060 },
1061
1062 /* size of device private data */
1063 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1064
1065 /* timestamps usage */
1066 .ts_used_bits = 32,
1067 .ts_period = 1000000, /* calibration period in ts. */
1068 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1069 .us_per_ts_shift = 0,
1070
1071 /* give here messages in/out endpoints */
1072 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1073 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1074
1075 /* size of rx/tx usb buffers */
1076 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1077 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1078
1079 /* device callbacks */
1080 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */
1081 .dev_init = pcan_usb_fd_init,
1082
1083 .dev_exit = pcan_usb_fd_exit,
1084 .dev_free = pcan_usb_fd_free,
1085 .dev_set_bus = pcan_usb_fd_set_bus,
1086 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1087 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1088 .dev_decode_buf = pcan_usb_fd_decode_buf,
1089 .dev_start = pcan_usb_fd_start,
1090 .dev_stop = pcan_usb_fd_stop,
1091 .dev_restart_async = pcan_usb_fd_restart_async,
1092 .dev_encode_msg = pcan_usb_fd_encode_msg,
1093
1094 .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1095};