summaryrefslogtreecommitdiff
path: root/drivers/net/can/usb/ucan.c
blob: 81e942f713e644ee69917a6682e9ba3568bf417f (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
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
// SPDX-License-Identifier: GPL-2.0

/* Driver for Theobroma Systems UCAN devices, Protocol Version 3
 *
 * Copyright (C) 2018 Theobroma Systems Design und Consulting GmbH
 *
 *
 * General Description:
 *
 * The USB Device uses three Endpoints:
 *
 *   CONTROL Endpoint: Is used the setup the device (start, stop,
 *   info, configure).
 *
 *   IN Endpoint: 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>

#define UCAN_DRIVER_NAME "ucan"
#define UCAN_MAX_RX_URBS 8
/* the CAN controller needs a while to enable/disable the bus */
#define UCAN_USB_CTL_PIPE_TIMEOUT 1000
/* this driver currently supports protocol version 3 only */
#define UCAN_PROTOCOL_VERSION_MIN 3
#define UCAN_PROTOCOL_VERSION_MAX 3

/* 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 Endpoint: 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,
	/* recover from bus-off state */
	UCAN_COMMAND_RESTART = 8,
};

/* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
 * Undefined bits must be set to 0.
 */
enum {
	UCAN_MODE_LOOPBACK = BIT(0),
	UCAN_MODE_SILENT = BIT(1),
	UCAN_MODE_3_SAMPLES = BIT(2),
	UCAN_MODE_ONE_SHOT = BIT(3),
	UCAN_MODE_BERR_REPORT = BIT(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_TX_COMPLETE = 1,  /* CAN frame transmission completed */
	UCAN_IN_RX = 2,           /* CAN frame received */
};

struct ucan_ctl_cmd_start {
	__le16 mode;         /* OR-ing any of UCAN_MODE_* */
} __packed;

struct ucan_ctl_cmd_set_bittiming {
	__le32 tq;           /* Time quanta (TQ) in nanoseconds */
	__le16 brp;          /* TQ Prescaler */
	__le16 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;

struct ucan_ctl_cmd_device_info {
	__le32 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;
	__le16 brp_inc;
	__le32 brp_min;
	__le32 brp_max;      /* ...can_bittiming fields */
	__le16 ctrlmodes;    /* supported control modes */
	__le16 hwfilter;     /* Number of HW filter banks */
	__le16 rxmboxes;     /* Number of receive Mailboxes */
} __packed;

struct ucan_ctl_cmd_get_protocol_version {
	__le32 version;
} __packed;

union ucan_ctl_payload {
	/* Setup Bittiming
	 * bmRequest == UCAN_COMMAND_START
	 */
	struct ucan_ctl_cmd_start cmd_start;
	/* Setup Bittiming
	 * bmRequest == UCAN_COMMAND_SET_BITTIMING
	 */
	struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
	/* Get Device Information
	 * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
	 */
	struct ucan_ctl_cmd_device_info cmd_get_device_info;
	/* Get Protocol Version
	 * bmRequest == UCAN_COMMAND_GET;
	 * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION
	 */
	struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;

	u8 raw[128];
} __packed;

enum {
	UCAN_TX_COMPLETE_SUCCESS = BIT(0),
};

/* Transmission Complete within ucan_message_in */
struct ucan_tx_complete_entry_t {
	u8 echo_index;
	u8 flags;
} __packed __aligned(0x2);

/* CAN Data message format within ucan_message_in/out */
struct ucan_can_msg {
	/* note DLC is computed by
	 *    msg.len - sizeof (msg.len)
	 *            - sizeof (msg.type)
	 *            - sizeof (msg.can_msg.id)
	 */
	__le32 id;

	union {
		u8 data[CAN_MAX_DLEN];  /* Data of CAN frames */
		u8 dlc;                 /* RTR dlc */
	};
} __packed;

/* OUT Endpoint, outbound messages */
struct ucan_message_out {
	__le16 len; /* Length of the content include header */
	u8 type;    /* UCAN_OUT_TX and friends */
	u8 subtype; /* command sub type */

	union {
		/* Transmit CAN frame
		 * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
		 * subtype stores the echo id
		 */
		struct ucan_can_msg can_msg;
	} msg;
} __packed __aligned(0x4);

/* IN Endpoint, inbound messages */
struct ucan_message_in {
	__le16 len; /* Length of the content include header */
	u8 type;    /* UCAN_IN_RX and friends */
	u8 subtype; /* command sub type */

	union {
		/* CAN Frame received
		 * (type == UCAN_IN_RX)
		 * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
		 */
		struct ucan_can_msg can_msg;

		/* CAN transmission complete
		 * (type == UCAN_IN_TX_COMPLETE)
		 */
		struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
	} __aligned(0x4) msg;
} __packed;

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

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

struct ucan_priv;

/* Context Information for transmission URBs */
struct ucan_urb_context {
	struct ucan_priv *up;
	u8 dlc;
	bool allocated;
};

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

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

	/* linux USB device structures */
	struct usb_device *udev;
	struct usb_interface *intf;
	struct net_device *netdev;

	/* lock for can->echo_skb (used around
	 * can_put/get/free_echo_skb
	 */
	spinlock_t echo_skb_lock;

	/* usb device information information */
	u8 intf_index;
	u8 in_ep_addr;
	u8 out_ep_addr;
	u16 in_ep_size;

	/* transmission and reception buffers */
	struct usb_anchor rx_urbs;
	struct usb_anchor tx_urbs;

	union ucan_ctl_payload *ctl_msg_buffer;
	struct ucan_device_info device_info;

	/* transmission control information and locks */
	spinlock_t context_lock;
	unsigned int available_tx_urbs;
	struct ucan_urb_context *context_array;
};

static u8 ucan_get_can_dlc(struct ucan_can_msg *msg, u16 len)
{
	if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
		return get_can_dlc(msg->dlc);
	else
		return get_can_dlc(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id)));
}

static void ucan_release_context_array(struct ucan_priv *up)
{
	if (!up->context_array)
		return;

	/* lock is not needed because, driver is currently opening or closing */
	up->available_tx_urbs = 0;

	kfree(up->context_array);
	up->context_array = NULL;
}

static int ucan_alloc_context_array(struct ucan_priv *up)
{
	int i;

	/* release contexts if any */
	ucan_release_context_array(up);

	up->context_array = kcalloc(up->device_info.tx_fifo,
				    sizeof(*up->context_array),
				    GFP_KERNEL);
	if (!up->context_array) {
		netdev_err(up->netdev,
			   "Not enough memory to allocate tx contexts\n");
		return -ENOMEM;
	}

	for (i = 0; i < up->device_info.tx_fifo; i++) {
		up->context_array[i].allocated = false;
		up->context_array[i].up = up;
	}

	/* lock is not needed because, driver is currently opening */
	up->available_tx_urbs = up->device_info.tx_fifo;

	return 0;
}

static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up)
{
	int i;
	unsigned long flags;
	struct ucan_urb_context *ret = NULL;

	if (WARN_ON_ONCE(!up->context_array))
		return NULL;

	/* execute context operation atomically */
	spin_lock_irqsave(&up->context_lock, flags);

	for (i = 0; i < up->device_info.tx_fifo; i++) {
		if (!up->context_array[i].allocated) {
			/* update context */
			ret = &up->context_array[i];
			up->context_array[i].allocated = true;

			/* stop queue if necessary */
			up->available_tx_urbs--;
			if (!up->available_tx_urbs)
				netif_stop_queue(up->netdev);

			break;
		}
	}

	spin_unlock_irqrestore(&up->context_lock, flags);
	return ret;
}

static bool ucan_release_context(struct ucan_priv *up,
				 struct ucan_urb_context *ctx)
{
	unsigned long flags;
	bool ret = false;

	if (WARN_ON_ONCE(!up->context_array))
		return false;

	/* execute context operation atomically */
	spin_lock_irqsave(&up->context_lock, flags);

	/* context was not allocated, maybe the device sent garbage */
	if (ctx->allocated) {
		ctx->allocated = false;

		/* check if the queue needs to be woken */
		if (!up->available_tx_urbs)
			netif_wake_queue(up->netdev);
		up->available_tx_urbs++;

		ret = true;
	}

	spin_unlock_irqrestore(&up->context_lock, flags);
	return ret;
}

static int ucan_ctrl_command_out(struct ucan_priv *up,
				 u8 cmd, u16 subcmd, u16 datalen)
{
	return usb_control_msg(up->udev,
			       usb_sndctrlpipe(up->udev, 0),
			       cmd,
			       USB_DIR_OUT | USB_TYPE_VENDOR |
						USB_RECIP_INTERFACE,
			       subcmd,
			       up->intf_index,
			       up->ctl_msg_buffer,
			       datalen,
			       UCAN_USB_CTL_PIPE_TIMEOUT);
}

static int ucan_device_request_in(struct ucan_priv *up,
				  u8 cmd, u16 subcmd, u16 datalen)
{
	return usb_control_msg(up->udev,
			       usb_rcvctrlpipe(up->udev, 0),
			       cmd,
			       USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			       subcmd,
			       0,
			       up->ctl_msg_buffer,
			       datalen,
			       UCAN_USB_CTL_PIPE_TIMEOUT);
}

/* Parse the device information structure reported by the device and
 * setup private variables accordingly
 */
static void ucan_parse_device_info(struct ucan_priv *up,
				   struct ucan_ctl_cmd_device_info *device_info)
{
	struct can_bittiming_const *bittiming =
		&up->device_info.bittiming_const;
	u16 ctrlmodes;

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

	ctrlmodes = le16_to_cpu(device_info->ctrlmodes);

	up->can.ctrlmode_supported = 0;

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

/* Handle a CAN error frame that we have received from the device.
 * Returns true if the can state has changed.
 */
static bool ucan_handle_error_frame(struct ucan_priv *up,
				    struct ucan_message_in *m,
				    canid_t canid)
{
	enum can_state new_state = up->can.state;
	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_OVERFLOW)
			net_stats->rx_over_errors++;

		/* controller state bits: if multiple are set the worst wins */
		if (d1 & CAN_ERR_CRTL_ACTIVE)
			new_state = CAN_STATE_ERROR_ACTIVE;

		if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
			new_state = CAN_STATE_ERROR_WARNING;

		if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
			new_state = CAN_STATE_ERROR_PASSIVE;
	}

	/* 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++;
	}

	/* no state change - we are done */
	if (up->can.state == new_state)
		return false;

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

	/* we switched into a worse state */
	up->can.state = new_state;
	switch (new_state) {
	case CAN_STATE_BUS_OFF:
		can_stats->bus_off++;
		can_bus_off(up->netdev);
		break;
	case CAN_STATE_ERROR_PASSIVE:
		can_stats->error_passive++;
		break;
	case CAN_STATE_ERROR_WARNING:
		can_stats->error_warning++;
		break;
	default:
		break;
	}
	return true;
}

/* 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_priv *up, struct ucan_message_in *m)
{
	int len;
	canid_t 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)) {
		netdev_warn(up->netdev, "invalid input message len: %d\n", len);
		return;
	}

	/* handle error frames */
	canid = le32_to_cpu(m->msg.can_msg.id);
	if (canid & CAN_ERR_FLAG) {
		bool busstate_changed = ucan_handle_error_frame(up, m, canid);

		/* if berr-reporting is off only state changes get through */
		if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
		    !busstate_changed)
			return;
	} else {
		canid_t canid_mask;
		/* compute the mask for canid */
		canid_mask = CAN_RTR_FLAG;
		if (canid & CAN_EFF_FLAG)
			canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
		else
			canid_mask |= CAN_SFF_MASK;

		if (canid & ~canid_mask)
			netdev_warn(up->netdev,
				    "unexpected bits set (canid %x, mask %x)",
				    canid, canid_mask);

		canid &= canid_mask;
	}

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

	/* fill the can frame */
	cf->can_id = canid;

	/* compute DLC taking RTR_FLAG into account */
	cf->can_dlc = ucan_get_can_dlc(&m->msg.can_msg, len);

	/* copy the payload of non RTR frames */
	if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
		memcpy(cf->data, m->msg.can_msg.data, cf->can_dlc);

	/* don't count error frames as real packets */
	stats->rx_packets++;
	stats->rx_bytes += cf->can_dlc;

	/* pass it to Linux */
	netif_rx(skb);
}

/* callback indicating completed transmission */
static void ucan_tx_complete_msg(struct ucan_priv *up,
				 struct ucan_message_in *m)
{
	unsigned long flags;
	u16 count, i;
	u8 echo_index, dlc;
	u16 len = le16_to_cpu(m->len);

	struct ucan_urb_context *context;

	if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
		netdev_err(up->netdev, "invalid tx complete length\n");
		return;
	}

	count = (len - UCAN_IN_HDR_SIZE) / 2;
	for (i = 0; i < count; i++) {
		/* we did not submit such echo ids */
		echo_index = m->msg.can_tx_complete_msg[i].echo_index;
		if (echo_index >= up->device_info.tx_fifo) {
			up->netdev->stats.tx_errors++;
			netdev_err(up->netdev,
				   "invalid echo_index %d received\n",
				   echo_index);
			continue;
		}

		/* gather information from the context */
		context = &up->context_array[echo_index];
		dlc = READ_ONCE(context->dlc);

		/* Release context and restart queue if necessary.
		 * Also check if the context was allocated
		 */
		if (!ucan_release_context(up, context))
			continue;

		spin_lock_irqsave(&up->echo_skb_lock, flags);
		if (m->msg.can_tx_complete_msg[i].flags &
		    UCAN_TX_COMPLETE_SUCCESS) {
			/* update statistics */
			up->netdev->stats.tx_packets++;
			up->netdev->stats.tx_bytes += dlc;
			can_get_echo_skb(up->netdev, echo_index);
		} else {
			up->netdev->stats.tx_dropped++;
			can_free_echo_skb(up->netdev, echo_index);
		}
		spin_unlock_irqrestore(&up->echo_skb_lock, flags);
	}
}

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

	/* the device is not up and the driver should not receive any
	 * data on the bulk in pipe
	 */
	if (WARN_ON(!up->context_array)) {
		usb_free_coherent(up->udev,
				  up->in_ep_size,
				  urb->transfer_buffer,
				  urb->transfer_dma);
		return;
	}

	/* check URB status */
	switch (urb->status) {
	case 0:
		break;
	case -ENOENT:
	case -EPIPE:
	case -EPROTO:
	case -ESHUTDOWN:
	case -ETIME:
		/* urb is not resubmitted -> free dma data */
		usb_free_coherent(up->udev,
				  up->in_ep_size,
				  urb->transfer_buffer,
				  urb->transfer_dma);
		netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n",
			   urb->status);
		return;
	default:
		goto resubmit;
	}

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

	/* iterate over input */
	pos = 0;
	while (pos < urb->actual_length) {
		int len;

		/* check sanity (length of header) */
		if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
			netdev_warn(up->netdev,
				    "invalid message (short; no hdr; l:%d)\n",
				    urb->actual_length);
			goto resubmit;
		}

		/* setup the message address */
		m = (struct ucan_message_in *)
			((u8 *)urb->transfer_buffer + pos);
		len = le16_to_cpu(m->len);

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

			goto resubmit;
		}

		switch (m->type) {
		case UCAN_IN_RX:
			ucan_rx_can_msg(up, m);
			break;
		case UCAN_IN_TX_COMPLETE:
			ucan_tx_complete_msg(up, m);
			break;
		default:
			netdev_warn(up->netdev,
				    "invalid message (type; t:%d)\n",
				    m->type);
			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_addr),
			  urb->transfer_buffer,
			  up->in_ep_size,
			  ucan_read_bulk_callback,
			  up);

	usb_anchor_urb(urb, &up->rx_urbs);
	ret = usb_submit_urb(urb, GFP_ATOMIC);

	if (ret < 0) {
		netdev_err(up->netdev,
			   "failed resubmitting read bulk urb: %d\n",
			   ret);

		usb_unanchor_urb(urb);
		usb_free_coherent(up->udev,
				  up->in_ep_size,
				  urb->transfer_buffer,
				  urb->transfer_dma);

		if (ret == -ENODEV)
			netif_device_detach(netdev);
	}
}

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

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

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

	up = context->up;
	if (WARN_ON_ONCE(!up))
		return;

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

	/* transmission failed (USB - the device will not send a TX complete) */
	if (urb->status) {
		netdev_warn(up->netdev,
			    "failed to transmit USB message to device: %d\n",
			     urb->status);

		/* update counters an cleanup */
		spin_lock_irqsave(&up->echo_skb_lock, flags);
		can_free_echo_skb(up->netdev, context - up->context_array);
		spin_unlock_irqrestore(&up->echo_skb_lock, flags);

		up->netdev->stats.tx_dropped++;

		/* release context and restart the queue if necessary */
		if (!ucan_release_context(up, context))
			netdev_err(up->netdev,
				   "urb failed, failed to release context\n");
	}
}

static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
{
	int i;

	for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
		if (urbs[i]) {
			usb_unanchor_urb(urbs[i]);
			usb_free_coherent(up->udev,
					  up->in_ep_size,
					  urbs[i]->transfer_buffer,
					  urbs[i]->transfer_dma);
			usb_free_urb(urbs[i]);
		}
	}

	memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
}

static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
					   struct urb **urbs)
{
	int i;

	memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);

	for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
		void *buf;

		urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
		if (!urbs[i])
			goto err;

		buf = usb_alloc_coherent(up->udev,
					 up->in_ep_size,
					 GFP_KERNEL, &urbs[i]->transfer_dma);
		if (!buf) {
			/* cleanup this urb */
			usb_free_urb(urbs[i]);
			urbs[i] = NULL;
			goto err;
		}

		usb_fill_bulk_urb(urbs[i], up->udev,
				  usb_rcvbulkpipe(up->udev,
						  up->in_ep_addr),
				  buf,
				  up->in_ep_size,
				  ucan_read_bulk_callback,
				  up);

		urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

		usb_anchor_urb(urbs[i], &up->rx_urbs);
	}
	return 0;

err:
	/* cleanup other unsubmitted urbs */
	ucan_cleanup_rx_urbs(up, urbs);
	return -ENOMEM;
}

/* Submits rx urbs with the semantic: Either submit all, or cleanup
 * everything. I case of errors submitted urbs are killed and all urbs in
 * the array are freed. I case of no errors every entry in the urb
 * array is set to NULL.
 */
static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
{
	int i, ret;

	/* Iterate over all urbs to submit. On success remove the urb
	 * from the list.
	 */
	for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
		ret = usb_submit_urb(urbs[i], GFP_KERNEL);
		if (ret) {
			netdev_err(up->netdev,
				   "could not submit urb; code: %d\n",
				   ret);
			goto err;
		}

		/* Anchor URB and drop reference, USB core will take
		 * care of freeing it
		 */
		usb_free_urb(urbs[i]);
		urbs[i] = NULL;
	}
	return 0;

err:
	/* Cleanup unsubmitted urbs */
	ucan_cleanup_rx_urbs(up, urbs);

	/* Kill urbs that are already submitted */
	usb_kill_anchored_urbs(&up->rx_urbs);

	return ret;
}

/* Open the network device */
static int ucan_open(struct net_device *netdev)
{
	int ret, ret_cleanup;
	u16 ctrlmode;
	struct urb *urbs[UCAN_MAX_RX_URBS];
	struct ucan_priv *up = netdev_priv(netdev);

	ret = ucan_alloc_context_array(up);
	if (ret)
		return ret;

	/* Allocate and prepare IN URBS - allocated and anchored
	 * urbs are stored in urbs[] for clean
	 */
	ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
	if (ret)
		goto err_contexts;

	/* 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);

	/* Driver is ready to receive data - start the USB device */
	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
	if (ret < 0) {
		netdev_err(up->netdev,
			   "could not start device, code: %d\n",
			   ret);
		goto err_reset;
	}

	/* Call CAN layer open */
	ret = open_candev(netdev);
	if (ret)
		goto err_stop;

	/* Driver is ready to receive data. Submit RX URBS */
	ret = ucan_submit_rx_urbs(up, urbs);
	if (ret)
		goto err_stop;

	up->can.state = CAN_STATE_ERROR_ACTIVE;

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

	return 0;

err_stop:
	/* The device have started already stop it */
	ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
	if (ret_cleanup < 0)
		netdev_err(up->netdev,
			   "could not stop device, code: %d\n",
			   ret_cleanup);

err_reset:
	/* The device might have received data, reset it for
	 * consistent state
	 */
	ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
	if (ret_cleanup < 0)
		netdev_err(up->netdev,
			   "could not reset device, code: %d\n",
			   ret_cleanup);

	/* clean up unsubmitted urbs */
	ucan_cleanup_rx_urbs(up, urbs);

err_contexts:
	ucan_release_context_array(up);
	return ret;
}

static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
				       struct ucan_urb_context *context,
				       struct can_frame *cf,
				       u8 echo_index)
{
	int mlen;
	struct urb *urb;
	struct ucan_message_out *m;

	/* 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(up->netdev, "no memory left for URBs\n");
		return NULL;
	}

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

	/* build the USB message */
	m->type = UCAN_OUT_TX;
	m->msg.can_msg.id = cpu_to_le32(cf->can_id);

	if (cf->can_id & CAN_RTR_FLAG) {
		mlen = UCAN_OUT_HDR_SIZE +
			offsetof(struct ucan_can_msg, dlc) +
			sizeof(m->msg.can_msg.dlc);
		m->msg.can_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);

	context->dlc = cf->can_dlc;

	m->subtype = echo_index;

	/* build the urb */
	usb_fill_bulk_urb(urb, up->udev,
			  usb_sndbulkpipe(up->udev,
					  up->out_ep_addr),
			  m, mlen, ucan_write_bulk_callback, context);
	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

	return urb;
}

static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
{
	usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
			  urb->transfer_buffer, urb->transfer_dma);
	usb_free_urb(urb);
}

/* callback when Linux needs to send a can frame */
static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
				   struct net_device *netdev)
{
	unsigned long flags;
	int ret;
	u8 echo_index;
	struct urb *urb;
	struct ucan_urb_context *context;
	struct ucan_priv *up = netdev_priv(netdev);
	struct can_frame *cf = (struct can_frame *)skb->data;

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

	/* allocate a context and slow down tx path, if fifo state is low */
	context = ucan_alloc_context(up);
	echo_index = context - up->context_array;

	if (WARN_ON_ONCE(!context))
		return NETDEV_TX_BUSY;

	/* prepare urb for transmission */
	urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
	if (!urb)
		goto drop;

	/* put the skb on can loopback stack */
	spin_lock_irqsave(&up->echo_skb_lock, flags);
	can_put_echo_skb(skb, up->netdev, echo_index);
	spin_unlock_irqrestore(&up->echo_skb_lock, flags);

	/* transmit it */
	usb_anchor_urb(urb, &up->tx_urbs);
	ret = usb_submit_urb(urb, GFP_ATOMIC);

	/* cleanup urb */
	if (ret) {
		/* on error, clean up */
		usb_unanchor_urb(urb);
		ucan_clean_up_tx_urb(up, urb);
		if (!ucan_release_context(up, context))
			netdev_err(up->netdev,
				   "xmit err: failed to release context\n");

		/* remove the skb from the echo stack - this also
		 * frees the skb
		 */
		spin_lock_irqsave(&up->echo_skb_lock, flags);
		can_free_echo_skb(up->netdev, echo_index);
		spin_unlock_irqrestore(&up->echo_skb_lock, flags);

		if (ret == -ENODEV) {
			netif_device_detach(up->netdev);
		} else {
			netdev_warn(up->netdev,
				    "xmit err: failed to submit urb %d\n",
				    ret);
			up->netdev->stats.tx_dropped++;
		}
		return NETDEV_TX_OK;
	}

	netif_trans_update(netdev);

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

	return NETDEV_TX_OK;

drop:
	if (!ucan_release_context(up, context))
		netdev_err(up->netdev,
			   "xmit drop: failed to release context\n");
	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_priv *up = netdev_priv(netdev);

	up->can.state = CAN_STATE_STOPPED;

	/* stop sending data */
	usb_kill_anchored_urbs(&up->tx_urbs);

	/* stop receiving data */
	usb_kill_anchored_urbs(&up->rx_urbs);

	/* stop and reset can device */
	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
	if (ret < 0)
		netdev_err(up->netdev,
			   "could not stop device, code: %d\n",
			   ret);

	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
	if (ret < 0)
		netdev_err(up->netdev,
			   "could not reset device, code: %d\n",
			   ret);

	netif_stop_queue(netdev);

	ucan_release_context_array(up);

	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_priv *up = netdev_priv(netdev);
	struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;

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

	ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
				    sizeof(*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)
{
	int ret;
	unsigned long flags;
	struct ucan_priv *up = netdev_priv(netdev);

	switch (mode) {
	case CAN_MODE_START:
		netdev_dbg(up->netdev, "restarting device\n");

		ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
		up->can.state = CAN_STATE_ERROR_ACTIVE;

		/* check if queue can be restarted,
		 * up->available_tx_urbs must be protected by the
		 * lock
		 */
		spin_lock_irqsave(&up->context_lock, flags);

		if (up->available_tx_urbs > 0)
			netif_wake_queue(up->netdev);

		spin_unlock_irqrestore(&up->context_lock, flags);

		return ret;
	default:
		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_priv *up;
	struct usb_endpoint_descriptor *ep;
	u16 in_ep_size;
	u16 out_ep_size;
	u8 in_ep_addr;
	u8 out_ep_addr;
	union ucan_ctl_payload *ctl_msg_buffer;
	char firmware_str[sizeof(union ucan_ctl_payload) + 1];

	udev = interface_to_usbdev(intf);

	/* Stage 1 - Interface Parsing
	 * ---------------------------
	 *
	 * Identifie the device USB interface descriptor and its
	 * endpoints. Probing is aborted on errors.
	 */

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

	dev_info(&udev->dev,
		 "%s: probing device on interface #%d\n",
		 UCAN_DRIVER_NAME,
		 iface_desc->desc.bInterfaceNumber);

	/* interface sanity check */
	if (iface_desc->desc.bNumEndpoints != 2) {
		dev_err(&udev->dev,
			"%s: invalid EP count (%d)",
			UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
		goto err_firmware_needs_update;
	}

	/* check interface endpoints */
	in_ep_addr = 0;
	out_ep_addr = 0;
	in_ep_size = 0;
	out_ep_size = 0;
	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 */
			in_ep_addr = ep->bEndpointAddress;
			in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
			in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
		} else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
			    0) &&
			   ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
			    USB_ENDPOINT_XFER_BULK)) {
			/* Out Endpoint */
			out_ep_addr = ep->bEndpointAddress;
			out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
			out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
		}
	}

	/* check if interface is sane */
	if (!in_ep_addr || !out_ep_addr) {
		dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
			UCAN_DRIVER_NAME);
		goto err_firmware_needs_update;
	}
	if (in_ep_size < sizeof(struct ucan_message_in)) {
		dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
			UCAN_DRIVER_NAME);
		goto err_firmware_needs_update;
	}
	if (out_ep_size < sizeof(struct ucan_message_out)) {
		dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
			UCAN_DRIVER_NAME);
		goto err_firmware_needs_update;
	}

	/* Stage 2 - Device Identification
	 * -------------------------------
	 *
	 * The device interface seems to be a ucan device. Do further
	 * compatibility checks. On error probing is aborted, on
	 * success this stage leaves the ctl_msg_buffer with the
	 * reported contents of a GET_INFO command (supported
	 * bittimings, tx_fifo depth). This information is used in
	 * Stage 3 for the final driver initialisation.
	 */

	/* Prepare Memory for control transferes */
	ctl_msg_buffer = devm_kzalloc(&udev->dev,
				      sizeof(union ucan_ctl_payload),
				      GFP_KERNEL);
	if (!ctl_msg_buffer) {
		dev_err(&udev->dev,
			"%s: failed to allocate control pipe memory\n",
			UCAN_DRIVER_NAME);
		return -ENOMEM;
	}

	/* get protocol version
	 *
	 * note: ucan_ctrl_command_* wrappers cannot be used yet
	 * because `up` is initialised in Stage 3
	 */
	ret = usb_control_msg(udev,
			      usb_rcvctrlpipe(udev, 0),
			      UCAN_COMMAND_GET,
			      USB_DIR_IN | USB_TYPE_VENDOR |
					USB_RECIP_INTERFACE,
			      UCAN_COMMAND_GET_PROTOCOL_VERSION,
			      iface_desc->desc.bInterfaceNumber,
			      ctl_msg_buffer,
			      sizeof(union ucan_ctl_payload),
			      UCAN_USB_CTL_PIPE_TIMEOUT);

	/* older firmware version do not support this command - those
	 * are not supported by this drive
	 */
	if (ret != 4) {
		dev_err(&udev->dev,
			"%s: could not read protocol version, ret=%d\n",
			UCAN_DRIVER_NAME, ret);
		if (ret >= 0)
			ret = -EINVAL;
		goto err_firmware_needs_update;
	}

	/* this driver currently supports protocol version 3 only */
	protocol_version =
		le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
	if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
	    protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
		dev_err(&udev->dev,
			"%s: device protocol version %d is not supported\n",
			UCAN_DRIVER_NAME, protocol_version);
		goto err_firmware_needs_update;
	}

	/* request the device information and store it in ctl_msg_buffer
	 *
	 * note: ucan_ctrl_command_* wrappers connot be used yet
	 * because `up` is initialised in Stage 3
	 */
	ret = usb_control_msg(udev,
			      usb_rcvctrlpipe(udev, 0),
			      UCAN_COMMAND_GET,
			      USB_DIR_IN | USB_TYPE_VENDOR |
					USB_RECIP_INTERFACE,
			      UCAN_COMMAND_GET_INFO,
			      iface_desc->desc.bInterfaceNumber,
			      ctl_msg_buffer,
			      sizeof(ctl_msg_buffer->cmd_get_device_info),
			      UCAN_USB_CTL_PIPE_TIMEOUT);

	if (ret < 0) {
		dev_err(&udev->dev, "%s: failed to retrieve device info\n",
			UCAN_DRIVER_NAME);
		goto err_firmware_needs_update;
	}
	if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
		dev_err(&udev->dev, "%s: device reported invalid device info\n",
			UCAN_DRIVER_NAME);
		goto err_firmware_needs_update;
	}
	if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
		dev_err(&udev->dev,
			"%s: device reported invalid tx-fifo size\n",
			UCAN_DRIVER_NAME);
		goto err_firmware_needs_update;
	}

	/* Stage 3 - Driver Initialisation
	 * -------------------------------
	 *
	 * Register device to Linux, prepare private structures and
	 * reset the device.
	 */

	/* allocate driver resources */
	netdev = alloc_candev(sizeof(struct ucan_priv),
			      ctl_msg_buffer->cmd_get_device_info.tx_fifo);
	if (!netdev) {
		dev_err(&udev->dev,
			"%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
		return -ENOMEM;
	}

	up = netdev_priv(netdev);

	/* initialze data */
	up->udev = udev;
	up->intf = intf;
	up->netdev = netdev;
	up->intf_index = iface_desc->desc.bInterfaceNumber;
	up->in_ep_addr = in_ep_addr;
	up->out_ep_addr = out_ep_addr;
	up->in_ep_size = in_ep_size;
	up->ctl_msg_buffer = ctl_msg_buffer;
	up->context_array = NULL;
	up->available_tx_urbs = 0;

	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;
	spin_lock_init(&up->context_lock);
	spin_lock_init(&up->echo_skb_lock);
	netdev->netdev_ops = &ucan_netdev_ops;

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

	/* parse device information
	 * the data retrieved in Stage 2 is still available in
	 * up->ctl_msg_buffer
	 */
	ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);

	/* just print some device information - if available */
	ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
				     sizeof(union ucan_ctl_payload));
	if (ret > 0) {
		/* copy string while ensuring zero terminiation */
		strncpy(firmware_str, up->ctl_msg_buffer->raw,
			sizeof(union ucan_ctl_payload));
		firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
	} else {
		strcpy(firmware_str, "unknown");
	}

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

	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;

	/* initialisation complete, log device info */
	netdev_info(up->netdev, "registered device\n");
	netdev_info(up->netdev, "firmware string: %s\n", firmware_str);

	/* success */
	return 0;

err_free_candev:
	free_candev(netdev);
	return ret;

err_firmware_needs_update:
	dev_err(&udev->dev,
		"%s: probe failed; try to update the device firmware\n",
		UCAN_DRIVER_NAME);
	return -ENODEV;
}

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

	usb_set_intfdata(intf, NULL);

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

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

MODULE_DEVICE_TABLE(usb, ucan_table);
/* driver callbacks */
static struct usb_driver ucan_driver = {
	.name = UCAN_DRIVER_NAME,
	.probe = ucan_probe,
	.disconnect = ucan_disconnect,
	.id_table = ucan_table,
};

module_usb_driver(ucan_driver);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");