Merge remote-tracking branches 'asoc/fix/adau1701' and 'asoc/fix/tlv320aic32x4' into...
[linux-2.6-block.git] / drivers / net / can / usb / peak_usb / pcan_usb_pro.c
CommitLineData
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
28MODULE_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 */
61struct 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 */
69struct 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 */
76struct 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) */
88static 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 */
110static 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
123static 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 */
138static 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 */
218static 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 */
241static 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
325static 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
363static 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
373static 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
388static 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
406static 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
416static 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
426static 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
437static 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
463static 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
479static 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 */
491static 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
f14e2243 507static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
d8a19935 508{
f14e2243
MKB
509 u8 *buffer;
510 int err;
511
512 buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
513 if (!buffer)
514 return -ENOMEM;
d8a19935
SG
515
516 buffer[0] = 0;
517 buffer[1] = !!loaded;
518
f14e2243
MKB
519 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
520 PCAN_USBPRO_FCT_DRVLD, buffer,
521 PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
522 kfree(buffer);
523
524 return err;
d8a19935
SG
525}
526
527static inline
528struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
529{
530 struct pcan_usb_pro_device *pdev =
531 container_of(dev, struct pcan_usb_pro_device, dev);
532 return pdev->usb_if;
533}
534
535static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
536 struct pcan_usb_pro_rxmsg *rx)
537{
538 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
539 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
540 struct net_device *netdev = dev->netdev;
541 struct can_frame *can_frame;
542 struct sk_buff *skb;
543 struct timeval tv;
c9faaa09 544 struct skb_shared_hwtstamps *hwts;
d8a19935
SG
545
546 skb = alloc_can_skb(netdev, &can_frame);
547 if (!skb)
548 return -ENOMEM;
549
550 can_frame->can_id = le32_to_cpu(rx->id);
551 can_frame->can_dlc = rx->len & 0x0f;
552
553 if (rx->flags & PCAN_USBPRO_EXT)
554 can_frame->can_id |= CAN_EFF_FLAG;
555
556 if (rx->flags & PCAN_USBPRO_RTR)
557 can_frame->can_id |= CAN_RTR_FLAG;
558 else
559 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
560
561 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
c9faaa09
OH
562 hwts = skb_hwtstamps(skb);
563 hwts->hwtstamp = timeval_to_ktime(tv);
d8a19935
SG
564
565 netif_rx(skb);
566 netdev->stats.rx_packets++;
567 netdev->stats.rx_bytes += can_frame->can_dlc;
568
569 return 0;
570}
571
572static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
573 struct pcan_usb_pro_rxstatus *er)
574{
575 const u32 raw_status = le32_to_cpu(er->status);
576 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
577 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
578 struct net_device *netdev = dev->netdev;
579 struct can_frame *can_frame;
580 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
581 u8 err_mask = 0;
582 struct sk_buff *skb;
583 struct timeval tv;
c9faaa09 584 struct skb_shared_hwtstamps *hwts;
d8a19935
SG
585
586 /* nothing should be sent while in BUS_OFF state */
587 if (dev->can.state == CAN_STATE_BUS_OFF)
588 return 0;
589
590 if (!raw_status) {
591 /* no error bit (back to active state) */
592 dev->can.state = CAN_STATE_ERROR_ACTIVE;
593 return 0;
594 }
595
596 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
597 PCAN_USBPRO_STATUS_QOVERRUN)) {
598 /* trick to bypass next comparison and process other errors */
599 new_state = CAN_STATE_MAX;
600 }
601
602 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
603 new_state = CAN_STATE_BUS_OFF;
604 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
605 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
606 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
607
608 if (rx_err_cnt > 127)
609 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
610 else if (rx_err_cnt > 96)
611 err_mask |= CAN_ERR_CRTL_RX_WARNING;
612
613 if (tx_err_cnt > 127)
614 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
615 else if (tx_err_cnt > 96)
616 err_mask |= CAN_ERR_CRTL_TX_WARNING;
617
618 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
619 CAN_ERR_CRTL_TX_WARNING))
620 new_state = CAN_STATE_ERROR_WARNING;
621 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
622 CAN_ERR_CRTL_TX_PASSIVE))
623 new_state = CAN_STATE_ERROR_PASSIVE;
624 }
625
626 /* donot post any error if current state didn't change */
627 if (dev->can.state == new_state)
628 return 0;
629
630 /* allocate an skb to store the error frame */
631 skb = alloc_can_err_skb(netdev, &can_frame);
632 if (!skb)
633 return -ENOMEM;
634
635 switch (new_state) {
636 case CAN_STATE_BUS_OFF:
637 can_frame->can_id |= CAN_ERR_BUSOFF;
638 can_bus_off(netdev);
639 break;
640
641 case CAN_STATE_ERROR_PASSIVE:
642 can_frame->can_id |= CAN_ERR_CRTL;
643 can_frame->data[1] |= err_mask;
644 dev->can.can_stats.error_passive++;
645 break;
646
647 case CAN_STATE_ERROR_WARNING:
648 can_frame->can_id |= CAN_ERR_CRTL;
649 can_frame->data[1] |= err_mask;
650 dev->can.can_stats.error_warning++;
651 break;
652
653 case CAN_STATE_ERROR_ACTIVE:
654 break;
655
656 default:
657 /* CAN_STATE_MAX (trick to handle other errors) */
658 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
659 can_frame->can_id |= CAN_ERR_PROT;
660 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
661 netdev->stats.rx_over_errors++;
662 netdev->stats.rx_errors++;
663 }
664
665 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
666 can_frame->can_id |= CAN_ERR_CRTL;
667 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
668 netdev->stats.rx_over_errors++;
669 netdev->stats.rx_errors++;
670 }
671
672 new_state = CAN_STATE_ERROR_ACTIVE;
673 break;
674 }
675
676 dev->can.state = new_state;
677
678 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
c9faaa09
OH
679 hwts = skb_hwtstamps(skb);
680 hwts->hwtstamp = timeval_to_ktime(tv);
d8a19935
SG
681 netif_rx(skb);
682 netdev->stats.rx_packets++;
683 netdev->stats.rx_bytes += can_frame->can_dlc;
684
685 return 0;
686}
687
688static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
689 struct pcan_usb_pro_rxts *ts)
690{
691 /* should wait until clock is stabilized */
692 if (usb_if->cm_ignore_count > 0)
693 usb_if->cm_ignore_count--;
694 else
695 peak_usb_set_ts_now(&usb_if->time_ref,
696 le32_to_cpu(ts->ts64[1]));
697}
698
699/*
700 * callback for bulk IN urb
701 */
702static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
703{
704 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
705 struct net_device *netdev = dev->netdev;
706 struct pcan_usb_pro_msg usb_msg;
707 u8 *rec_ptr, *msg_end;
708 u16 rec_cnt;
709 int err = 0;
710
711 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
712 urb->actual_length);
713 if (!rec_ptr) {
714 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
715 return -EINVAL;
716 }
717
718 /* loop reading all the records from the incoming message */
719 msg_end = urb->transfer_buffer + urb->actual_length;
720 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
721 for (; rec_cnt > 0; rec_cnt--) {
722 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
723 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
724
725 if (!sizeof_rec) {
726 netdev_err(netdev,
727 "got unsupported rec in usb msg:\n");
728 err = -ENOTSUPP;
729 break;
730 }
731
732 /* check if the record goes out of current packet */
733 if (rec_ptr + sizeof_rec > msg_end) {
734 netdev_err(netdev,
735 "got frag rec: should inc usb rx buf size\n");
736 err = -EBADMSG;
737 break;
738 }
739
740 switch (pr->data_type) {
741 case PCAN_USBPRO_RXMSG8:
742 case PCAN_USBPRO_RXMSG4:
743 case PCAN_USBPRO_RXMSG0:
744 case PCAN_USBPRO_RXRTR:
745 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
746 if (err < 0)
747 goto fail;
748 break;
749
750 case PCAN_USBPRO_RXSTATUS:
751 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
752 if (err < 0)
753 goto fail;
754 break;
755
756 case PCAN_USBPRO_RXTS:
757 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
758 break;
759
760 default:
761 netdev_err(netdev,
762 "unhandled rec type 0x%02x (%d): ignored\n",
763 pr->data_type, pr->data_type);
764 break;
765 }
766
767 rec_ptr += sizeof_rec;
768 }
769
770fail:
771 if (err)
56b9f301
RD
772 pcan_dump_mem("received msg",
773 urb->transfer_buffer, urb->actual_length);
d8a19935
SG
774
775 return err;
776}
777
778static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
779 struct sk_buff *skb, u8 *obuf, size_t *size)
780{
781 struct can_frame *cf = (struct can_frame *)skb->data;
782 u8 data_type, len, flags;
783 struct pcan_usb_pro_msg usb_msg;
784
785 pcan_msg_init_empty(&usb_msg, obuf, *size);
786
787 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
788 data_type = PCAN_USBPRO_TXMSG0;
789 else if (cf->can_dlc <= 4)
790 data_type = PCAN_USBPRO_TXMSG4;
791 else
792 data_type = PCAN_USBPRO_TXMSG8;
793
794 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
795
796 flags = 0;
797 if (cf->can_id & CAN_EFF_FLAG)
798 flags |= 0x02;
799 if (cf->can_id & CAN_RTR_FLAG)
800 flags |= 0x01;
801
802 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
803 cf->data);
804
805 *size = usb_msg.rec_buffer_len;
806
807 return 0;
808}
809
810static int pcan_usb_pro_start(struct peak_usb_device *dev)
811{
812 struct pcan_usb_pro_device *pdev =
813 container_of(dev, struct pcan_usb_pro_device, dev);
814 int err;
815
816 err = pcan_usb_pro_set_silent(dev,
817 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
818 if (err)
819 return err;
820
821 /* filter mode: 0-> All OFF; 1->bypass */
822 err = pcan_usb_pro_set_filter(dev, 1);
823 if (err)
824 return err;
825
826 /* opening first device: */
827 if (pdev->usb_if->dev_opened_count == 0) {
828 /* reset time_ref */
829 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
830
831 /* ask device to send ts messages */
832 err = pcan_usb_pro_set_ts(dev, 1);
833 }
834
835 pdev->usb_if->dev_opened_count++;
836
837 return err;
838}
839
840/*
841 * stop interface
842 * (last chance before set bus off)
843 */
844static int pcan_usb_pro_stop(struct peak_usb_device *dev)
845{
846 struct pcan_usb_pro_device *pdev =
847 container_of(dev, struct pcan_usb_pro_device, dev);
848
849 /* turn off ts msgs for that interface if no other dev opened */
850 if (pdev->usb_if->dev_opened_count == 1)
851 pcan_usb_pro_set_ts(dev, 0);
852
853 pdev->usb_if->dev_opened_count--;
854
855 return 0;
856}
857
858/*
859 * called when probing to initialize a device object.
860 */
861static int pcan_usb_pro_init(struct peak_usb_device *dev)
862{
d8a19935
SG
863 struct pcan_usb_pro_device *pdev =
864 container_of(dev, struct pcan_usb_pro_device, dev);
f14e2243
MKB
865 struct pcan_usb_pro_interface *usb_if = NULL;
866 struct pcan_usb_pro_fwinfo *fi = NULL;
867 struct pcan_usb_pro_blinfo *bi = NULL;
868 int err;
d8a19935
SG
869
870 /* do this for 1st channel only */
871 if (!dev->prev_siblings) {
d8a19935
SG
872 /* allocate netdevices common structure attached to first one */
873 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
874 GFP_KERNEL);
f14e2243
MKB
875 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
876 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
877 if (!usb_if || !fi || !bi) {
878 err = -ENOMEM;
879 goto err_out;
880 }
d8a19935
SG
881
882 /* number of ts msgs to ignore before taking one into account */
883 usb_if->cm_ignore_count = 5;
884
885 /*
886 * explicit use of dev_xxx() instead of netdev_xxx() here:
887 * information displayed are related to the device itself, not
888 * to the canx netdevices.
889 */
890 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
891 PCAN_USBPRO_INFO_FW,
f14e2243 892 fi, sizeof(*fi));
d8a19935
SG
893 if (err) {
894 dev_err(dev->netdev->dev.parent,
895 "unable to read %s firmware info (err %d)\n",
896 pcan_usb_pro.name, err);
f14e2243 897 goto err_out;
d8a19935
SG
898 }
899
900 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
901 PCAN_USBPRO_INFO_BL,
f14e2243 902 bi, sizeof(*bi));
d8a19935
SG
903 if (err) {
904 dev_err(dev->netdev->dev.parent,
905 "unable to read %s bootloader info (err %d)\n",
906 pcan_usb_pro.name, err);
f14e2243 907 goto err_out;
d8a19935
SG
908 }
909
f14e2243
MKB
910 /* tell the device the can driver is running */
911 err = pcan_usb_pro_drv_loaded(dev, 1);
912 if (err)
913 goto err_out;
914
d8a19935
SG
915 dev_info(dev->netdev->dev.parent,
916 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
917 pcan_usb_pro.name,
f14e2243 918 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
d8a19935 919 pcan_usb_pro.ctrl_count);
d8a19935
SG
920 } else {
921 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
922 }
923
924 pdev->usb_if = usb_if;
925 usb_if->dev[dev->ctrl_idx] = dev;
926
927 /* set LED in default state (end of init phase) */
928 pcan_usb_pro_set_led(dev, 0, 1);
929
20fb4eb9
MKB
930 kfree(bi);
931 kfree(fi);
932
d8a19935 933 return 0;
f14e2243
MKB
934
935 err_out:
936 kfree(bi);
937 kfree(fi);
938 kfree(usb_if);
939
940 return err;
d8a19935
SG
941}
942
943static void pcan_usb_pro_exit(struct peak_usb_device *dev)
944{
945 struct pcan_usb_pro_device *pdev =
946 container_of(dev, struct pcan_usb_pro_device, dev);
947
948 /*
949 * when rmmod called before unplug and if down, should reset things
950 * before leaving
951 */
952 if (dev->can.state != CAN_STATE_STOPPED) {
953 /* set bus off on the corresponding channel */
954 pcan_usb_pro_set_bus(dev, 0);
955 }
956
957 /* if channel #0 (only) */
958 if (dev->ctrl_idx == 0) {
959 /* turn off calibration message if any device were opened */
960 if (pdev->usb_if->dev_opened_count > 0)
961 pcan_usb_pro_set_ts(dev, 0);
962
963 /* tell the PCAN-USB Pro device the driver is being unloaded */
964 pcan_usb_pro_drv_loaded(dev, 0);
965 }
966}
967
968/*
969 * called when PCAN-USB Pro adapter is unplugged
970 */
971static void pcan_usb_pro_free(struct peak_usb_device *dev)
972{
973 /* last device: can free pcan_usb_pro_interface object now */
974 if (!dev->prev_siblings && !dev->next_siblings)
975 kfree(pcan_usb_pro_dev_if(dev));
976}
977
978/*
979 * probe function for new PCAN-USB Pro usb interface
980 */
981static int pcan_usb_pro_probe(struct usb_interface *intf)
982{
983 struct usb_host_interface *if_desc;
984 int i;
985
986 if_desc = intf->altsetting;
987
988 /* check interface endpoint addresses */
989 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
990 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
991
992 /*
993 * below is the list of valid ep addreses. Any other ep address
994 * is considered as not-CAN interface address => no dev created
995 */
996 switch (ep->bEndpointAddress) {
997 case PCAN_USBPRO_EP_CMDOUT:
998 case PCAN_USBPRO_EP_CMDIN:
999 case PCAN_USBPRO_EP_MSGOUT_0:
1000 case PCAN_USBPRO_EP_MSGOUT_1:
1001 case PCAN_USBPRO_EP_MSGIN:
1002 case PCAN_USBPRO_EP_UNUSED:
1003 break;
1004 default:
1005 return -ENODEV;
1006 }
1007 }
1008
1009 return 0;
1010}
1011
1012/*
1013 * describe the PCAN-USB Pro adapter
1014 */
1015struct peak_usb_adapter pcan_usb_pro = {
1016 .name = "PCAN-USB Pro",
1017 .device_id = PCAN_USBPRO_PRODUCT_ID,
1018 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1019 .clock = {
1020 .freq = PCAN_USBPRO_CRYSTAL_HZ,
1021 },
1022 .bittiming_const = {
1023 .name = "pcan_usb_pro",
1024 .tseg1_min = 1,
1025 .tseg1_max = 16,
1026 .tseg2_min = 1,
1027 .tseg2_max = 8,
1028 .sjw_max = 4,
1029 .brp_min = 1,
1030 .brp_max = 1024,
1031 .brp_inc = 1,
1032 },
1033
1034 /* size of device private data */
1035 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1036
1037 /* timestamps usage */
1038 .ts_used_bits = 32,
1039 .ts_period = 1000000, /* calibration period in ts. */
1040 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1041 .us_per_ts_shift = 0,
1042
1043 /* give here messages in/out endpoints */
1044 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1045 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1046
1047 /* size of rx/tx usb buffers */
1048 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1049 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1050
1051 /* device callbacks */
1052 .intf_probe = pcan_usb_pro_probe,
1053 .dev_init = pcan_usb_pro_init,
1054 .dev_exit = pcan_usb_pro_exit,
1055 .dev_free = pcan_usb_pro_free,
1056 .dev_set_bus = pcan_usb_pro_set_bus,
1057 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1058 .dev_get_device_id = pcan_usb_pro_get_device_id,
1059 .dev_decode_buf = pcan_usb_pro_decode_buf,
1060 .dev_encode_msg = pcan_usb_pro_encode_msg,
1061 .dev_start = pcan_usb_pro_start,
1062 .dev_stop = pcan_usb_pro_stop,
1063 .dev_restart_async = pcan_usb_pro_restart_async,
1064};