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