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