summaryrefslogtreecommitdiff
path: root/drivers/net/can/usb/ucan.c
blob: f447d836179e1459cd48fe9efe733b001f400eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
// SPDX-License-Identifier: GPL-2.0

/* Driver for Theobroma Systems UCAN devices
 *
 * Copyright (C) 2015 Theobroma Systems Design und Consulting GmbH
 *
 * This driver is inspired by the 4.0.0 version of drivers/net/can/usb/ems_usb.c
 *
 *
 * General Description:
 *
 * The USB Device uses three Endpoints:
 *
 *   Interrupt Endpoint: Once the device is started the device sends
 *   its TX FIFO status (space left) on this endpoint. The driver uses
 *   this information for flow control.
 *
 *   IN Enpoint: The device sends CAN Frame Messages and Device
 *   Information using the IN endpoint.
 *
 *   OUT Endpoint: The driver sends configuration requests, and CAN
 *   Frames on the out endpoint.
 *
 *   Error Handling: If error reporting is turned on the device
 *   encodes error into CAN error frames (see uapi/linux/can/error.h)
 *   and sends it using the IN Endpoint. The driver updates statistics
 *   and forward it.
 */

#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/can/error.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/signal.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/usb.h>

#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/can/error.h>

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Martin Elshuber, Theobroma Systems Design und Consulting GmbH <martin.elshuber@theobroma-systems.com>");
MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");

#define MAX_TX_URBS 8
#define MAX_RX_URBS 8
#define TX_QUEUE_STOP_THRESHOLD 1

static struct usb_device_id ucan_table[] = {
	{USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)}, /* Mule (soldered onto compute modules) */
	{USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)}, /* Seal (standalone USB stick) */
	{} /* Terminating entry */
};

MODULE_DEVICE_TABLE(usb, ucan_table);

/* UCAN Message Definitions --------------------------------------------
 *
 *  ucan_message_out_t and ucan_message_in_t define the messages
 *  transmitted on the OUT and IN endpoint.
 *
 *  Multibyte fields are transmitted with little endianness
 *
 *  INTR Endpoint: a single uint32_t storing the current space in the fifo
 *
 *  OUT Enpoint: single message of type ucan_message_out_t is
 *    transmitted on the out endpoint
 *
 *  IN Endpoint: multiple messages ucan_message_in_t concateted in
 *    the following way:
 *
 *      m[n].len <=> the length if message n(including the header in bytes)
 *      m[n] is is aligned to a 4 byte boundary, hence
 *        offset(m[0])   := 0;
 *        offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3
 *
 *      this implies that
 *        offset(m[n]) % 4 <=> 0
 */

/* Device Global Commands */
enum {
	UCAN_DEVICE_GET_FW_STRING = 0,
};

/* UCAN Commands */
enum {
	/* start the can transceiver - val defines the operation mode */
	UCAN_COMMAND_START    = 0,
	/* cancel pending transmissions and stop the can transceiver */
	UCAN_COMMAND_STOP     = 1,
	/* send can transceiver into low-power sleep mode */
	UCAN_COMMAND_SLEEP    = 2,
	/* wake up can transceiver from low-power sleep mode */
	UCAN_COMMAND_WAKEUP   = 3,
	/* reset the can transceiver */
	UCAN_COMMAND_RESET    = 4,
	/* get piece of info from the can transceiver - subcmd defines what
	 * piece
	 */
	UCAN_COMMAND_GET      = 5,
	/* clear or disable hardware filter - subcmd defines which of the two */
	UCAN_COMMAND_FILTER   = 6,
	/* Setup bittiming */
	UCAN_COMMAND_SET_BITTIMING   = 7,
};

/* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
 * Undefined bits must be set to 0.
 */
enum {
	UCAN_MODE_LOOPBACK    = (1 << 0),
	UCAN_MODE_SILENT      = (1 << 1),
	UCAN_MODE_3_SAMPLES   = (1 << 2),
	UCAN_MODE_ONE_SHOT    = (1 << 3),
	UCAN_MODE_BERR_REPORT = (1 << 4),
};

/* UCAN_COMMAND_GET subcommands */
enum {
	UCAN_COMMAND_GET_INFO = 0,
	UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
};

/* UCAN_COMMAND_FILTER subcommands */
enum {
	UCAN_FILTER_CLEAR     = 0,
	UCAN_FILTER_DISABLE   = 1,
	UCAN_FILTER_ENABLE    = 2,
};

/* OUT endpoint message types */
enum {
	UCAN_OUT_TX            = 2, /* transmit a CAN frame */
};

/* IN endpoint message types */
enum {
	UCAN_IN_RX = 2,
};

typedef union {
	/***************************************************
	 * Setup Bittiming
	 * bmRequest == UCAN_COMMAND_START
	 ***************************************************/
	struct {
		u16 mode;
	} __packed cmd_start;
	/***************************************************
	 * Setup Bittiming
	 * bmRequest == UCAN_COMMAND_SET_BITTIMING
	 ***************************************************/
	struct {
		u32 tq;              /* Time quanta (TQ) in nanoseconds */
		u16 brp;             /* TQ Prescaler */
		u16 sample_point;    /* Samplepoint on tenth percent */
		u8 prop_seg;         /* Propagation segment in TQs */
		u8 phase_seg1;       /* Phase buffer segment 1 in TQs */
		u8 phase_seg2;       /* Phase buffer segment 2 in TQs */
		u8 sjw;              /* Synchronisation jump width in TQs */
	} __packed cmd_set_bittiming;
	/***************************************************
	 * Setup HW Filter Modes
	 * bmRequest == UCAN_ENA_FILTER
	 ***************************************************/
	struct {
		u32 id;
		u32 mask;
		u16 mbox;
	} __packed cmd_ena_filter;
	/***************************************************
	 * Get Device Information
	 * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
	 ***************************************************/
	struct {
		u32 freq;   /* Clock Frequency for tq generation */
		u8 tx_fifo; /* Size of the transmission fifo */
		u8 sjw_max;   /* can_bittiming fields... */
		u8 tseg1_min;
		u8 tseg1_max;
		u8 tseg2_min;
		u8 tseg2_max;
		u16 brp_inc;
		u32 brp_min;
		u32 brp_max; /* ...can_bittiming fields */
		u16 ctrlmodes; /* supported control modes */
		u16 hwfilter;  /* Number of HW filter banks */
		u16 rxmboxes;  /* Number of receive Mailboxes */
	} __packed device_info;
	struct {
		u32 version;
	} __packed protocol_version;
	struct {
		u8 d[128];
	} raw;
} __packed ucan_ctl_payload_t;


/* OUT Enpoint, outbound messages */
struct ucan_message_out {
	u16 len;  /* Length of the content include header */
	u16 type; /* UCAN_OUT_COMMAND and friends */
	union {
		/***************************************************
		 * Transmit CAN frame
		 * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
		 ***************************************************/
		struct {
			 /* note DLC is computed by
			  *    msg.len - sizeof (msg.len)
			  *            - sizeof (msg.type)
			  *            - sizeof (msg.can_msg.id)
			  */
			u32 id;
			u8 data[8];
			/* ensure data alignment to 4
			 * by moving dlc after data
			 */
		} __packed can_msg;

		/***************************************************
		 * Transmit RTR CAN frame
		 * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) != 0)
		 ***************************************************/
		struct {
			u32 id;
			u8 dlc;
		} __packed can_rtr_msg;
	} msg;
} __packed __aligned(0x4);

/* IN Enpoint, inbound messages */
struct ucan_message_in {
	u16 len;  /* Length of the content include header */
	u16 type; /* UCAN_IN_DEVICE_INFO and friends */

	union {
		/***************************************************
		 * CAN Frame received
		 * (type == UCAN_IN_RX)
		 * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
		 ***************************************************/
		struct {
			 /* note DLC is computed by
			  *    msg.len - sizeof (msg.len)
			  *            - sizeof (msg.type)
			  *            - sizeof (msg.can_msg.id)
			  */
			u32 id;
			u8 data[8];
			/* ensure data alignment to 4
			 * by moving dlc after data
			 */
		} __packed can_msg;

		/***************************************************
		 * CAN RTR Frame received
		 * (type == UCAN_IN_RX)
		 * && ((msg.can_msg.id & CAN_RTR_FLAG) != 0)
		 ***************************************************/
		struct {
			u32 id;
			u8 dlc;
		} __packed can_rtr_msg;

	} __aligned(0x4) msg;
} __packed;

/* Macros to calculate message lengths */
#define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
#define UCAN_OUT_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))

#define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
#define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))

struct ucan;

/* Context Information for transmission URBs */
struct ucan_urb_context {
	struct ucan *up;
	u32 echo_index;
	u8 dlc;
};

/* Information reported by the USB device */
struct ucan_device_info {
	struct can_bittiming_const bittiming_const;
	int tx_fifo;
};

/* Driver private data */
struct ucan {
	struct can_priv can; /* must be the first member */

	u8 intf_index;
	struct usb_device *udev;
	struct usb_interface *intf;
	struct net_device *netdev;

	struct usb_endpoint_descriptor *out_ep;
	struct usb_endpoint_descriptor *in_ep;
	struct usb_endpoint_descriptor *irq_ep;

	struct usb_anchor rx_urbs;
	struct usb_anchor tx_urbs;
	struct urb *irq_urb;
	u32 *irq_data;

	ucan_ctl_payload_t *ctl_msg_buffer;
	struct ucan_device_info device_info;

	atomic_t active_tx_urbs;
	atomic_t free_slots;
	struct ucan_urb_context tx_urb_contexts[MAX_TX_URBS];
};

static void ucan_tx_wake_queue_if_possible(struct ucan *up)
{
	if ( (netif_queue_stopped(up->netdev)) &&
	        (atomic_read(&up->active_tx_urbs) < MAX_TX_URBS) &&
		(atomic_read(&up->free_slots) >= TX_QUEUE_STOP_THRESHOLD) ) {
		netif_wake_queue(up->netdev);
	}
}

static int ucan_ctrl_command_out(struct ucan *up, u8 cmd, u16 subcmd, size_t datalen)
{
	if (datalen > sizeof(ucan_ctl_payload_t))
		return -ENOMEM;

	return usb_control_msg(up->udev,
			usb_sndctrlpipe(up->udev, 0),
			cmd,
			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
			cpu_to_le16(subcmd),
			up->intf_index,
			up->ctl_msg_buffer,
			datalen,
			500);
}

static int ucan_ctrl_command_in(struct ucan *up, u8 cmd, u16 subcmd, size_t datalen)
{
	if (datalen > sizeof(ucan_ctl_payload_t))
		return -ENOMEM;

	return usb_control_msg(up->udev,
			usb_rcvctrlpipe(up->udev, 0),
			cmd,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
			cpu_to_le16(subcmd),
			up->intf_index,
			up->ctl_msg_buffer,
			datalen,
			500);
}

static int ucan_device_request_in(struct ucan *up, u8 cmd, u16 subcmd, size_t datalen)
{
	if (datalen > sizeof(ucan_ctl_payload_t))
		return -ENOMEM;

	return usb_control_msg(up->udev,
			usb_rcvctrlpipe(up->udev, 0),
			cmd,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			cpu_to_le16(subcmd),
			0,
			up->ctl_msg_buffer,
			datalen,
			500);
}

/* Request the device Information */
static int ucan_get_device_info(struct ucan *up)
{
	int ret;
	struct can_bittiming_const *bittiming =
		&up->device_info.bittiming_const;

	ret = ucan_ctrl_command_in(up, UCAN_COMMAND_GET, UCAN_COMMAND_GET_INFO,
				sizeof(up->ctl_msg_buffer->device_info));
	if (ret < 0)
		return ret;
	if (ret < sizeof(up->ctl_msg_buffer->device_info))
		return -EINVAL;

	/* store the data */
	up->can.clock.freq = le32_to_cpu(up->ctl_msg_buffer->device_info.freq);
	up->device_info.tx_fifo = up->ctl_msg_buffer->device_info.tx_fifo;
	strcpy(bittiming->name, "ucan");
	bittiming->tseg1_min = up->ctl_msg_buffer->device_info.tseg1_min;
	bittiming->tseg1_max = up->ctl_msg_buffer->device_info.tseg1_max;
	bittiming->tseg2_min = up->ctl_msg_buffer->device_info.tseg2_min;
	bittiming->tseg2_max = up->ctl_msg_buffer->device_info.tseg2_max;
	bittiming->sjw_max   = up->ctl_msg_buffer->device_info.sjw_max;
	bittiming->brp_min   = le32_to_cpu(up->ctl_msg_buffer->device_info.brp_min);
	bittiming->brp_max   = le32_to_cpu(up->ctl_msg_buffer->device_info.brp_max);
	bittiming->brp_inc   = le16_to_cpu(up->ctl_msg_buffer->device_info.brp_inc);

	up->can.ctrlmode_supported = 0;

	if (le16_to_cpu(up->ctl_msg_buffer->device_info.ctrlmodes) & UCAN_MODE_LOOPBACK)
		up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
	if (le16_to_cpu(up->ctl_msg_buffer->device_info.ctrlmodes) & UCAN_MODE_SILENT)
		up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
	if (le16_to_cpu(up->ctl_msg_buffer->device_info.ctrlmodes) & UCAN_MODE_3_SAMPLES)
		up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
	if (le16_to_cpu(up->ctl_msg_buffer->device_info.ctrlmodes) & UCAN_MODE_ONE_SHOT)
		up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
	if (le16_to_cpu(up->ctl_msg_buffer->device_info.ctrlmodes) & UCAN_MODE_BERR_REPORT)
		up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;

	return 0;
}

/* Callback when the device sends the IRQ sate *
 *
 * This function simply stores the current TX fifo state for flow
 * control
 */
static void ucan_read_irq_callback(struct urb *urb)
{
	int ret;
	struct ucan *up = urb->context;
	struct net_device *netdev = up->netdev;

	switch (urb->status) {
	case 0:
		atomic_set(&up->free_slots, le32_to_cpu(*up->irq_data));
		ucan_tx_wake_queue_if_possible(up);
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
	case -ETIME:
	case -EPROTO:
		dev_dbg(&up->udev->dev, "%s ENOENT|ESHUTDOWN|ETIME\n",
			__func__);
		return;
	default:
		dev_warn(&up->udev->dev, "%s error (%d)\n", __func__,
			 urb->status);
		return;
	}

	usb_fill_int_urb(urb, up->udev,
			 usb_rcvintpipe(up->udev, up->irq_ep->bEndpointAddress &
						      USB_ENDPOINT_NUMBER_MASK),
			 urb->transfer_buffer, up->irq_ep->wMaxPacketSize,
			 ucan_read_irq_callback, up, up->irq_ep->bInterval);

	ret = usb_submit_urb(urb, GFP_KERNEL);

	if (ret == -ENODEV)
		netif_device_detach(netdev);
	else if (ret)
		dev_err(&up->udev->dev, "failed resubmitting urb: %d\n", ret);
}

/* Handle a CAN error frame that we have received from the device */
static void ucan_handle_error_frame(struct ucan *up, struct ucan_message_in *m, u32 canid) {
	enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
	struct net_device_stats *net_stats = &up->netdev->stats;
	struct can_device_stats *can_stats = &up->can.can_stats;

	if (canid & CAN_ERR_LOSTARB)
		can_stats->arbitration_lost++;

	if (canid & CAN_ERR_BUSERROR)
		can_stats->bus_error++;

	if (canid & CAN_ERR_ACK)
		net_stats->tx_errors++;

	if (canid & CAN_ERR_BUSOFF)
		new_state = CAN_STATE_BUS_OFF;

	/* controller problems, details in data[1] */
	if (canid & CAN_ERR_CRTL) {
		u8 d1 = m->msg.can_msg.data[1];

		if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
			new_state = max(new_state, (enum can_state)CAN_STATE_ERROR_PASSIVE);

		if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
			new_state = max(new_state, (enum can_state)CAN_STATE_ERROR_WARNING);

		if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
			net_stats->rx_over_errors++;
	}

	/* protocol error, details in data[2] */
	if (canid & CAN_ERR_PROT) {
		u8 d2 = m->msg.can_msg.data[2];

		if (d2 & CAN_ERR_PROT_TX)
			net_stats->tx_errors++;
		else
			net_stats->rx_errors++;
	}

	/* we switched into a better state */
	if (up->can.state >= new_state) {
		up->can.state = new_state;
		return;
	}

	/* we switched into a worse state */
	dev_dbg(&up->udev->dev, "%s: switching from state %d to %d\n", __func__, up->can.state, new_state);
	up->can.state = new_state;
	switch(new_state) {
		case CAN_STATE_BUS_OFF:
			can_stats->bus_off++;
			can_bus_off(up->netdev);
			netdev_info(up->netdev, "link has gone into BUS-OFF state\n");
			break;
		case CAN_STATE_ERROR_PASSIVE:
			can_stats->error_passive++;
			break;
		case CAN_STATE_ERROR_WARNING:
			can_stats->error_warning++;
			break;
		default:
			break;
	}
}

/* Callback on reception of a can frame via the IN endpoint
 *
 * This function allocates an skb and transferres it to the Linux
 * network stack
 */
static void ucan_rx_can_msg(struct ucan *up, struct ucan_message_in *m)
{
	int len;
	u32 canid;
	struct can_frame *cf;
	struct sk_buff *skb;
	struct net_device_stats *stats = &up->netdev->stats;

	/* get the contents of the length field */
	len = le16_to_cpu(m->len);

	/* check sanity */
	if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
		dev_warn(&up->udev->dev, "invalid input message len\n");
		return;
	}

	/* handle error frames */
	canid = le32_to_cpu(m->msg.can_msg.id);
	if (canid & CAN_ERR_FLAG) {
		ucan_handle_error_frame(up, m, canid);
		/* drop frame if berr-reporting is off */
		if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
			return;
	}

	/* allocate skb */
	skb = alloc_can_skb(up->netdev, &cf);
	if (!skb)
		return;

	/* fill the can frame */
	cf->can_id = le32_to_cpu(m->msg.can_msg.id);
	cf->can_dlc = len - (UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id));

	if (cf->can_dlc > sizeof(m->msg.can_msg.data))
		goto err_freeskb;

	if (cf->can_dlc < 0)
		goto err_freeskb;

	if (cf->can_id & CAN_EFF_FLAG)
		cf->can_id &=
		    (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG | CAN_ERR_FLAG);
	else
		cf->can_id &= (CAN_SFF_MASK | CAN_RTR_FLAG | CAN_ERR_FLAG);

	if (cf->can_id & CAN_RTR_FLAG) {
		cf->can_id |= CAN_RTR_FLAG;
		cf->can_dlc = m->msg.can_rtr_msg.dlc;
	} else {
		memcpy(cf->data, m->msg.can_msg.data, cf->can_dlc);
	}

	/* pass it to Linux */
	netif_receive_skb(skb);

	/* don't count error frames as real packets */
	if (!(canid & CAN_ERR_FLAG)) {
		stats->rx_packets++;
		stats->rx_bytes += cf->can_dlc;
	}

	return;
err_freeskb:
	kfree_skb(skb);
}

/* callback on reception of a USB message */
static void ucan_read_bulk_callback(struct urb *urb)
{
	int ret;
	int len;
	int pos;
	struct ucan *up = urb->context;
	struct net_device *netdev = up->netdev;
	struct ucan_message_in m;

	/* check URB status */
	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -EPIPE:
	case -EPROTO:
	case -ESHUTDOWN:
	case -ETIME:
		dev_dbg(&up->udev->dev, "%s ENOENT|ESHUTDOWN|ETIME\n",
			__func__);
		return;
	default:
		goto resubmit;
	}

	/* iterate over input */
	pos = 0;
	while (pos < urb->actual_length) {
		/* check sanity (length of header) */
		if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
			dev_warn(&up->udev->dev, "invalid input message %d; too short (no header)\n",
				 urb->actual_length);
			goto resubmit;
		}

		/* copy the message header */
		memcpy(&m, urb->transfer_buffer + pos, UCAN_IN_HDR_SIZE);
		len = le16_to_cpu(m.len);

		/* check sanity (length of content) */
		if (urb->actual_length - pos < len) {
			dev_warn(&up->udev->dev, "invalid input message al:%d pos:%d len:%d; too short (no data)\n",
				urb->actual_length, pos, len);
			print_hex_dump(KERN_WARNING, "raw data: ", DUMP_PREFIX_ADDRESS, 16, 1, urb->transfer_buffer, urb->actual_length, true);

			goto resubmit;
		}

		// copy remainder of packet
		memcpy(&m.msg, urb->transfer_buffer + UCAN_IN_HDR_SIZE + pos,
			len - UCAN_IN_HDR_SIZE);

		switch (__le16_to_cpu(m.type)) {
		case UCAN_IN_RX:
			ucan_rx_can_msg(up, &m);
			break;
		default:
			dev_warn(&up->udev->dev,
				 "invalid input message type\n");
			break;
		}

		/* proceed to next message */
		pos += len;
		/* align to 4 byte boundary */
		pos = round_up(pos, 4);
	}

resubmit:
	/* resubmit urb when done */
	usb_fill_bulk_urb(urb, up->udev,
	    usb_rcvbulkpipe(up->udev, up->in_ep->bEndpointAddress &
					  USB_ENDPOINT_NUMBER_MASK),
	    urb->transfer_buffer, up->in_ep->wMaxPacketSize,
	    ucan_read_bulk_callback, up);

	ret = usb_submit_urb(urb, GFP_KERNEL);

	if (ret == -ENODEV)
		netif_device_detach(netdev);
	else if (ret)
		dev_err(&up->udev->dev,
			"failed resubmitting read bulk urb: %d\n", ret);
}

/* callback after transmission of a USB message */
static void ucan_write_bulk_callback(struct urb *urb)
{
	struct ucan_urb_context *context = urb->context;
	struct ucan *up;

	/* get the urb context */
	if (WARN_ON(!context))
		return;

	up = context->up;

	/* free up our allocated buffer */
	usb_free_coherent(urb->dev, sizeof(struct ucan_message_out), urb->transfer_buffer,
			  urb->transfer_dma);

	atomic_dec(&up->active_tx_urbs);

	/* sanity check */
	if (!netif_device_present(up->netdev))
		return;

	/* urb state check */
	if (urb->status)
		netdev_info(up->netdev, "Tx URB aborted (%d)\n", urb->status);

	up->netdev->trans_start = jiffies;

	/* update statistics */
	up->netdev->stats.tx_packets++;
	up->netdev->stats.tx_bytes += context->dlc;

	/* echo can frame */
	can_get_echo_skb(up->netdev, context->echo_index);

	/* Release context */
	context->echo_index = -1;

	/* restart the queue if necessary */
	ucan_tx_wake_queue_if_possible(up);
}

/* Open the network device */
static int ucan_open(struct net_device *netdev)
{
	int i;
	int ret;
	void *buf;
	u16 ctrlmode;
	struct urb *urb;
	struct ucan *up = netdev_priv(netdev);

	/* call CAN layer open */
	ret = open_candev(netdev);
	if (ret)
		goto err;

	ret = -ENOMEM;

	/* set the queue state as empty */
	atomic_set(&up->free_slots, up->device_info.tx_fifo);

	/* initialize IRQ endpoint */
	up->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!up->irq_urb)
		goto err;

	up->irq_data = kzalloc(up->irq_ep->wMaxPacketSize, GFP_KERNEL);
	if (!up->irq_data) {
		usb_free_urb(up->irq_urb);
		goto err;
	}

	usb_fill_int_urb(up->irq_urb, up->udev,
			 usb_rcvintpipe(up->udev, up->irq_ep->bEndpointAddress &
						      USB_ENDPOINT_NUMBER_MASK),
			 up->irq_data, up->irq_ep->wMaxPacketSize,
			 ucan_read_irq_callback, up, up->irq_ep->bInterval);

	ret = usb_submit_urb(up->irq_urb, GFP_KERNEL);
	if (ret) {
		kfree(up->irq_data);
		usb_free_urb(up->irq_urb);
		goto err;
	}

	/* initialize IN enpoint */
	for (i = 0; i < MAX_RX_URBS; i++) {
		ret = -ENOMEM;

		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!urb)
			goto err;

		buf = usb_alloc_coherent(up->udev, up->in_ep->wMaxPacketSize,
					 GFP_KERNEL, &urb->transfer_dma);
		if (!buf) {
			usb_free_urb(urb);
			goto err;
		}

		usb_fill_bulk_urb(urb, up->udev,
		    usb_rcvbulkpipe(up->udev, up->in_ep->bEndpointAddress &
						  USB_ENDPOINT_NUMBER_MASK),
		    buf, up->in_ep->wMaxPacketSize, ucan_read_bulk_callback,
		    up);
		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
		usb_anchor_urb(urb, &up->rx_urbs);

		ret = usb_submit_urb(urb, GFP_KERNEL);

		if (ret) {
			usb_unanchor_urb(urb);
			usb_free_coherent(up->udev, up->in_ep->wMaxPacketSize,
					  buf, urb->transfer_dma);
			usb_free_urb(urb);
			goto err;
		}

		/* Drop reference, USB core will take care of freeing it */
		usb_free_urb(urb);
	}

	/* check the control mode */
	ctrlmode = 0;
	if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
		ctrlmode |= UCAN_MODE_LOOPBACK;
	if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
		ctrlmode |= UCAN_MODE_SILENT;
	if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
		ctrlmode |= UCAN_MODE_3_SAMPLES;
	if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
		ctrlmode |= UCAN_MODE_ONE_SHOT;

	// Enable this in any case - filtering is down within the receive path
	ctrlmode |= UCAN_MODE_BERR_REPORT;
	up->ctl_msg_buffer->cmd_start.mode =  cpu_to_le16(ctrlmode);

	/* start the USB device */
	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
	if (ret < 0)
		goto err;

	up->can.state = CAN_STATE_ERROR_ACTIVE;

	/* start the network queue */
	netif_start_queue(netdev);

	return 0;

err:
	usb_kill_anchored_urbs(&up->rx_urbs);
	return ret;
}

/* callback when Linux needs to send a can frame */
static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
				   struct net_device *netdev)
{
	int i, ret, mlen;
	struct urb *urb;
	struct ucan_urb_context *context;
	struct ucan *up = netdev_priv(netdev);
	struct can_frame *cf = (struct can_frame *)skb->data;
	struct ucan_message_out *m;

	/* check skb */
	if (can_dropped_invalid_skb(netdev, skb))
		return NETDEV_TX_OK;

	/* create a URB, and a buffer for it, and copy the data to the URB */
	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (!urb) {
		netdev_err(netdev, "No memory left for URBs\n");
		goto drop;
	}

	m = usb_alloc_coherent(up->udev, sizeof(struct ucan_message_out), GFP_ATOMIC, &urb->transfer_dma);
	if (!m) {
		netdev_err(netdev, "No memory left for USB buffer\n");
		usb_free_urb(urb);
		goto drop;
	}

	/* build the USB message */
	if (cf->can_dlc > sizeof(m->msg.can_msg.data))
		cf->can_dlc = sizeof(m->msg.can_msg.data);

	m->type = cpu_to_le16(UCAN_OUT_TX);
	m->msg.can_msg.id = cpu_to_le32(cf->can_id);

	if (cf->can_id & CAN_RTR_FLAG) {
		mlen = UCAN_OUT_LEN(m->msg.can_rtr_msg);
		m->msg.can_rtr_msg.dlc = cf->can_dlc;
	} else {
		mlen = UCAN_OUT_HDR_SIZE +
			sizeof(m->msg.can_msg.id) + cf->can_dlc;
		memcpy(m->msg.can_msg.data, cf->data, cf->can_dlc);
	}
	m->len = cpu_to_le16(mlen);

	/* allocate a context */
	context = NULL;
	for (i = 0; i < MAX_TX_URBS; i++) {
		if (up->tx_urb_contexts[i].echo_index == -1) {
			context = &up->tx_urb_contexts[i];
			context->up = up;
			context->echo_index = i;
			context->dlc = cf->can_dlc;
			atomic_inc(&up->active_tx_urbs);
			break;
		}
	}

	WARN_ON_ONCE(!context);
	if (!context) {
		usb_free_coherent(up->udev, sizeof(struct ucan_message_out), m, urb->transfer_dma);
		usb_free_urb(urb);
		goto drop;
	}

	/* build the urb */
	usb_fill_bulk_urb(urb, up->udev,
			usb_sndbulkpipe(up->udev, up->out_ep->bEndpointAddress &
					USB_ENDPOINT_NUMBER_MASK),
			m, mlen, ucan_write_bulk_callback, context);
	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
	usb_anchor_urb(urb, &up->tx_urbs);
	can_put_echo_skb(skb, up->netdev, context->echo_index);
	/* transmit it */
	ret = usb_submit_urb(urb, GFP_ATOMIC);

	// cleanup urb
	if (ret) {
		usb_unanchor_urb(urb);
		usb_free_coherent(up->udev, sizeof(struct ucan_message_out), m, urb->transfer_dma);

		/* on error, clean up */
		can_free_echo_skb(up->netdev, context->echo_index);
		dev_kfree_skb(skb);

		context->echo_index = -1;
		atomic_dec(&up->active_tx_urbs);

		if (ret == -ENODEV) {
			netif_device_detach(up->netdev);
		} else {
			netdev_warn(up->netdev, "failed tx_urb %d\n", ret);
			up->netdev->stats.tx_dropped++;
		}
	}
	else {
		atomic_dec(&up->free_slots);

		up->netdev->trans_start = jiffies;

		/* Slow down tx path, if fifo state is low */
		if ((atomic_read(&up->active_tx_urbs) >= MAX_TX_URBS) ||
			(atomic_read(&up->free_slots) < TX_QUEUE_STOP_THRESHOLD)) {
			netif_stop_queue(netdev);
		}
	}

	/* release ref, as we do not need the urb anymore */
	usb_free_urb(urb);

	return NETDEV_TX_OK;
drop:
	dev_kfree_skb(skb);
	up->netdev->stats.tx_dropped++;

	return NETDEV_TX_OK;
}

/* Device goes down
 *
 * Clean up used resources
 */
static int ucan_close(struct net_device *netdev)
{
	int ret;
	struct ucan *up = netdev_priv(netdev);

	up->can.state = CAN_STATE_STOPPED;

	netif_stop_queue(netdev);

	ret = ucan_ctrl_command_out( up, UCAN_COMMAND_STOP, 0, 0);
	if (ret < 0)
		dev_err(&up->udev->dev,
			 "Could not stop UCAN device, code: %d\n", ret);

	ret = ucan_ctrl_command_out( up, UCAN_COMMAND_RESET, 0, 0);
	if (ret < 0)
		dev_err(&up->udev->dev,
			 "Could not reset UCAN device, code: %d\n", ret);

	usb_kill_urb(up->irq_urb);
	usb_kill_anchored_urbs(&up->rx_urbs);
	usb_kill_anchored_urbs(&up->tx_urbs);
	atomic_set(&up->active_tx_urbs, 0);
	atomic_set(&up->free_slots, 0);

	kfree(up->irq_data);

	close_candev(up->netdev);
	return 0;
}

/* CAN driver callbacks */
static const struct net_device_ops ucan_netdev_ops = {
	.ndo_open = ucan_open,
	.ndo_stop = ucan_close,
	.ndo_start_xmit = ucan_start_xmit,
	.ndo_change_mtu = can_change_mtu,
};

/* Request to set bittiming
 *
 * This function generates an USB set bittiming message and transmits
 * it to the device
 */
static int ucan_set_bittiming(struct net_device *netdev)
{
	int ret;
	struct ucan *up = netdev_priv(netdev);

	up->ctl_msg_buffer->cmd_set_bittiming.tq = cpu_to_le32(up->can.bittiming.tq);
	up->ctl_msg_buffer->cmd_set_bittiming.brp = cpu_to_le16(up->can.bittiming.brp);
	up->ctl_msg_buffer->cmd_set_bittiming.sample_point =
	    cpu_to_le32(up->can.bittiming.sample_point);
	up->ctl_msg_buffer->cmd_set_bittiming.prop_seg = up->can.bittiming.prop_seg;
	up->ctl_msg_buffer->cmd_set_bittiming.phase_seg1 = up->can.bittiming.phase_seg1;
	up->ctl_msg_buffer->cmd_set_bittiming.phase_seg2 = up->can.bittiming.phase_seg2;
	up->ctl_msg_buffer->cmd_set_bittiming.sjw = up->can.bittiming.sjw;

	dev_dbg(&up->udev->dev,
		"Setup bittiming\n"
		"  bitrate: %d\n"
		"  sample-point: %d\n"
		"  tq: %d\n"
		"  prop_seg: %d\n"
		"  phase_seg1 %d\n"
		"  phase_seg2 %d\n"
		"  sjw %d\n"
		"  brp %d\n",
		up->can.bittiming.bitrate, up->can.bittiming.sample_point,
		up->can.bittiming.tq, up->can.bittiming.prop_seg,
		up->can.bittiming.phase_seg1, up->can.bittiming.phase_seg2,
		up->can.bittiming.sjw, up->can.bittiming.brp);

	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING,
				0, sizeof(up->ctl_msg_buffer->cmd_set_bittiming));
	return (ret < 0) ? ret : 0;
}

/* Restart the device to get it out of BUS-OFF state.
 * Called when the user runs "ip link set can1 type can restart".
 */
static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
{
	// XXX TODO
	return -EOPNOTSUPP;
}

/* Probe the device, reset it and gather general device information */
static int ucan_probe(struct usb_interface *intf,
		      const struct usb_device_id *id)
{
	int ret;
	int i;
	u32 protocol_version;
	struct usb_device *udev;
	struct net_device *netdev;
	struct usb_host_interface *iface_desc;
	struct ucan *up;
	struct usb_endpoint_descriptor *ep;

	udev = interface_to_usbdev(intf);

	/* check if the interface is sane */
	ret = -ENODEV;
	iface_desc = intf->cur_altsetting;
	if (!iface_desc)
		goto err;

	/* Infvalid interface Settings */
	if (iface_desc->desc.bNumEndpoints != 3)
		goto err;

	dev_info(&udev->dev, "Found UCAN device on interface #%d\n",
		 iface_desc->desc.iInterface);

	/* allocate driver resources */
	ret = -ENOMEM;
	netdev = alloc_candev(sizeof(struct ucan), MAX_TX_URBS);
	if (!netdev)
		goto err;

	up = netdev_priv(netdev);

	/* get interface descriptors */
	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
		ep = &iface_desc->endpoint[i].desc;

		if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
		    ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
		     USB_ENDPOINT_XFER_BULK)) {
			/* In Endpoint */
			up->in_ep = ep;
		} else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
			    0) &&
			   ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
			    USB_ENDPOINT_XFER_BULK)) {
			/* Out Endpoint */
			up->out_ep = ep;
		} else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) !=
			    0) &&
			   ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
			    USB_ENDPOINT_XFER_INT)) {
			/* Out Endpoint */
			up->irq_ep = ep;
		}
	}

	/* check if all interfaces are sane */
	if (!up->in_ep || !up->out_ep || !up->irq_ep) {
		dev_err(&udev->dev, "invalid endpoint configuration\n");
		goto err_free_candev;
	}
	if (up->in_ep->wMaxPacketSize < sizeof(struct ucan_message_in)) {
		dev_err(&udev->dev, "invalid in_ep MaxPacketSize\n");
		goto err_free_candev;
	}
	if (up->out_ep->wMaxPacketSize < sizeof(struct ucan_message_out)) {
		dev_err(&udev->dev, "invalid out_ep MaxPacketSize\n");
		goto err_free_candev;
	}
	if (up->irq_ep->wMaxPacketSize < sizeof(u32)) {
		dev_err(&udev->dev, "invalid irq_ep MaxPacketSize\n");
		goto err_free_candev;
	}

	dev_dbg(&udev->dev,
		 "using EP %02x for input with max packet size 0x%x\n",
		 up->in_ep->bEndpointAddress, up->in_ep->wMaxPacketSize);
	dev_dbg(&udev->dev,
		 "using EP %02x for output with max packet size 0x%x\n",
		 up->out_ep->bEndpointAddress, up->out_ep->wMaxPacketSize);
	dev_dbg(&udev->dev,
		 "using EP %02x for irq input with max packet size 0x%x\n",
		 up->irq_ep->bEndpointAddress, up->irq_ep->wMaxPacketSize);

	/* initialze data */
	up->udev = udev;
	up->intf = intf;
	up->netdev = netdev;
	up->intf_index = iface_desc->desc.bInterfaceNumber;

	up->can.state = CAN_STATE_STOPPED;
	up->can.bittiming_const = &up->device_info.bittiming_const;
	up->can.do_set_bittiming = ucan_set_bittiming;
	up->can.do_set_mode = &ucan_set_mode;
	netdev->netdev_ops = &ucan_netdev_ops;

	usb_set_intfdata(intf, up);
	SET_NETDEV_DEV(netdev, &intf->dev);

	/* allocate memory for command messages */
	up->ctl_msg_buffer =
		devm_kzalloc(&udev->dev, sizeof(ucan_ctl_payload_t), GFP_KERNEL);
	if (!up->ctl_msg_buffer)
		goto err_free_candev;

	ret = ucan_ctrl_command_in(up, UCAN_COMMAND_GET, UCAN_COMMAND_GET_PROTOCOL_VERSION, sizeof(ucan_ctl_payload_t));

	/* older firmware version do not support this command - those are not supported by this driver */
	if (ret != 4) {
		dev_err(&udev->dev,
			"Could not read protocol version, ret=%d. The firmware on this device is too old, please update!\n",
			ret);
		if (ret >= 0)
			ret = -EINVAL;
		goto err_free_candev;
	}

	/* this driver currently supports protocol version 2 only */
	protocol_version = le32_to_cpu(up->ctl_msg_buffer->protocol_version.version);
	if (protocol_version != 2) {
		dev_err(&udev->dev, "Device protocol version %d is not supported", protocol_version);
		ret = -EINVAL;
		goto err_free_candev;
	}
	dev_info(&udev->dev, "Device protocol version %d ok", protocol_version);

	/* just print some device information - if available */
	ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0, sizeof(ucan_ctl_payload_t));
	if (ret <= 0) {
		dev_info(&udev->dev, "Device did not report firmware string\n");
	} else {
		/* ensure zero terminiation */
		char str[sizeof(ucan_ctl_payload_t)+1];
		strncpy(str, up->ctl_msg_buffer->raw.d, sizeof(ucan_ctl_payload_t));
		dev_info(&udev->dev, "Device firmware string: %s\n", str);
	}

	/* device is compatible, reset it */
	ret = ucan_ctrl_command_out( up, UCAN_COMMAND_RESET, 0, 0);
	if (ret < 0)
		goto err_free_candev;

	/* gather device information */
	ret = ucan_get_device_info(up);
	if (ret)
		goto err_free_candev;

	dev_info(&up->udev->dev,
	    "Device Reports:\n"
	    "  Frequency [Hz]           : %d\n"
	    "  TX Fifo [length]         : %d\n"
	    "  Time Segment 1 [min,max] : %d - %d\n"
	    "  Time Segment 2 [min,max] : %d - %d\n"
	    "  SWJ [max]                : %d\n"
	    "  Prescale [min-max,step]  : %d - %d, %d\n"
	    "  Supported modes          :%s%s%s%s%s [%x]\n",
	    up->can.clock.freq, up->device_info.tx_fifo,
	    up->can.bittiming_const->tseg1_min,
	    up->can.bittiming_const->tseg1_max,
	    up->can.bittiming_const->tseg2_min,
	    up->can.bittiming_const->tseg2_max,
	    up->can.bittiming_const->sjw_max, up->can.bittiming_const->brp_min,
	    up->can.bittiming_const->brp_max, up->can.bittiming_const->brp_inc,
	    (up->can.ctrlmode_supported & CAN_CTRLMODE_LOOPBACK) ? " Loopback"
								 : "",
	    (up->can.ctrlmode_supported & CAN_CTRLMODE_LISTENONLY) ? " Silent"
								   : "",
	    (up->can.ctrlmode_supported & CAN_CTRLMODE_3_SAMPLES)
		? " 3-Sampling"
		: "",
	    (up->can.ctrlmode_supported & CAN_CTRLMODE_ONE_SHOT) ? " OneShot"
								 : "",
	    (up->can.ctrlmode_supported & CAN_CTRLMODE_BERR_REPORTING)
		? " BusErrReport"
		: "",
	    up->can.ctrlmode_supported);

	atomic_set(&up->active_tx_urbs, 0);
	for (i = 0; i < MAX_TX_URBS; i++)
		up->tx_urb_contexts[i].echo_index = -1;

	init_usb_anchor(&up->rx_urbs);
	init_usb_anchor(&up->tx_urbs);

	up->can.state = CAN_STATE_STOPPED;

	/* register the device */
	ret = register_candev(netdev);
	if (ret)
		goto err_free_candev;

	/* success */
	return 0;

err_free_candev:
	free_candev(netdev);
err:
	return ret;
}

/* disconnect the device */
static void ucan_disconnect(struct usb_interface *intf)
{
	struct usb_device *udev;
	struct ucan *up = usb_get_intfdata(intf);

	udev = interface_to_usbdev(intf);

	usb_set_intfdata(intf, NULL);

	if (up) {
		unregister_netdev(up->netdev);
		free_candev(up->netdev);
	}
}

/* driver callbacks */
static struct usb_driver ucan_driver = {
	.name = "ucan",
	.probe = ucan_probe,
	.disconnect = ucan_disconnect,
	.id_table = ucan_table,
};

module_usb_driver(ucan_driver);