@@ -303,12 +303,6 @@ struct gs_can {
303
303
struct can_bittiming_const bt_const , data_bt_const ;
304
304
unsigned int channel ; /* channel number */
305
305
306
- /* time counter for hardware timestamps */
307
- struct cyclecounter cc ;
308
- struct timecounter tc ;
309
- spinlock_t tc_lock ; /* spinlock to guard access tc->cycle_last */
310
- struct delayed_work timestamp ;
311
-
312
306
u32 feature ;
313
307
unsigned int hf_size_tx ;
314
308
@@ -325,6 +319,13 @@ struct gs_usb {
325
319
struct gs_can * canch [GS_MAX_INTF ];
326
320
struct usb_anchor rx_submitted ;
327
321
struct usb_device * udev ;
322
+
323
+ /* time counter for hardware timestamps */
324
+ struct cyclecounter cc ;
325
+ struct timecounter tc ;
326
+ spinlock_t tc_lock ; /* spinlock to guard access tc->cycle_last */
327
+ struct delayed_work timestamp ;
328
+
328
329
unsigned int hf_size_rx ;
329
330
u8 active_channels ;
330
331
};
@@ -388,15 +389,15 @@ static int gs_cmd_reset(struct gs_can *dev)
388
389
GFP_KERNEL );
389
390
}
390
391
391
- static inline int gs_usb_get_timestamp (const struct gs_can * dev ,
392
+ static inline int gs_usb_get_timestamp (const struct gs_usb * parent ,
392
393
u32 * timestamp_p )
393
394
{
394
395
__le32 timestamp ;
395
396
int rc ;
396
397
397
- rc = usb_control_msg_recv (dev -> udev , 0 , GS_USB_BREQ_TIMESTAMP ,
398
+ rc = usb_control_msg_recv (parent -> udev , 0 , GS_USB_BREQ_TIMESTAMP ,
398
399
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE ,
399
- dev -> channel , 0 ,
400
+ 0 , 0 ,
400
401
& timestamp , sizeof (timestamp ),
401
402
USB_CTRL_GET_TIMEOUT ,
402
403
GFP_KERNEL );
@@ -410,73 +411,74 @@ static inline int gs_usb_get_timestamp(const struct gs_can *dev,
410
411
411
412
static u64 gs_usb_timestamp_read (const struct cyclecounter * cc ) __must_hold (& dev - > tc_lock )
412
413
{
413
- struct gs_can * dev = container_of (cc , struct gs_can , cc );
414
+ struct gs_usb * parent = container_of (cc , struct gs_usb , cc );
414
415
u32 timestamp = 0 ;
415
416
int err ;
416
417
417
- lockdep_assert_held (& dev -> tc_lock );
418
+ lockdep_assert_held (& parent -> tc_lock );
418
419
419
420
/* drop lock for synchronous USB transfer */
420
- spin_unlock_bh (& dev -> tc_lock );
421
- err = gs_usb_get_timestamp (dev , & timestamp );
422
- spin_lock_bh (& dev -> tc_lock );
421
+ spin_unlock_bh (& parent -> tc_lock );
422
+ err = gs_usb_get_timestamp (parent , & timestamp );
423
+ spin_lock_bh (& parent -> tc_lock );
423
424
if (err )
424
- netdev_err ( dev -> netdev ,
425
- "Error %d while reading timestamp. HW timestamps may be inaccurate." ,
426
- err );
425
+ dev_err ( & parent -> udev -> dev ,
426
+ "Error %d while reading timestamp. HW timestamps may be inaccurate." ,
427
+ err );
427
428
428
429
return timestamp ;
429
430
}
430
431
431
432
static void gs_usb_timestamp_work (struct work_struct * work )
432
433
{
433
434
struct delayed_work * delayed_work = to_delayed_work (work );
434
- struct gs_can * dev ;
435
+ struct gs_usb * parent ;
435
436
436
- dev = container_of (delayed_work , struct gs_can , timestamp );
437
- spin_lock_bh (& dev -> tc_lock );
438
- timecounter_read (& dev -> tc );
439
- spin_unlock_bh (& dev -> tc_lock );
437
+ parent = container_of (delayed_work , struct gs_usb , timestamp );
438
+ spin_lock_bh (& parent -> tc_lock );
439
+ timecounter_read (& parent -> tc );
440
+ spin_unlock_bh (& parent -> tc_lock );
440
441
441
- schedule_delayed_work (& dev -> timestamp ,
442
+ schedule_delayed_work (& parent -> timestamp ,
442
443
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ );
443
444
}
444
445
445
446
static void gs_usb_skb_set_timestamp (struct gs_can * dev ,
446
447
struct sk_buff * skb , u32 timestamp )
447
448
{
448
449
struct skb_shared_hwtstamps * hwtstamps = skb_hwtstamps (skb );
450
+ struct gs_usb * parent = dev -> parent ;
449
451
u64 ns ;
450
452
451
- spin_lock_bh (& dev -> tc_lock );
452
- ns = timecounter_cyc2time (& dev -> tc , timestamp );
453
- spin_unlock_bh (& dev -> tc_lock );
453
+ spin_lock_bh (& parent -> tc_lock );
454
+ ns = timecounter_cyc2time (& parent -> tc , timestamp );
455
+ spin_unlock_bh (& parent -> tc_lock );
454
456
455
457
hwtstamps -> hwtstamp = ns_to_ktime (ns );
456
458
}
457
459
458
- static void gs_usb_timestamp_init (struct gs_can * dev )
460
+ static void gs_usb_timestamp_init (struct gs_usb * parent )
459
461
{
460
- struct cyclecounter * cc = & dev -> cc ;
462
+ struct cyclecounter * cc = & parent -> cc ;
461
463
462
464
cc -> read = gs_usb_timestamp_read ;
463
465
cc -> mask = CYCLECOUNTER_MASK (32 );
464
466
cc -> shift = 32 - bits_per (NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ );
465
467
cc -> mult = clocksource_hz2mult (GS_USB_TIMESTAMP_TIMER_HZ , cc -> shift );
466
468
467
- spin_lock_init (& dev -> tc_lock );
468
- spin_lock_bh (& dev -> tc_lock );
469
- timecounter_init (& dev -> tc , & dev -> cc , ktime_get_real_ns ());
470
- spin_unlock_bh (& dev -> tc_lock );
469
+ spin_lock_init (& parent -> tc_lock );
470
+ spin_lock_bh (& parent -> tc_lock );
471
+ timecounter_init (& parent -> tc , & parent -> cc , ktime_get_real_ns ());
472
+ spin_unlock_bh (& parent -> tc_lock );
471
473
472
- INIT_DELAYED_WORK (& dev -> timestamp , gs_usb_timestamp_work );
473
- schedule_delayed_work (& dev -> timestamp ,
474
+ INIT_DELAYED_WORK (& parent -> timestamp , gs_usb_timestamp_work );
475
+ schedule_delayed_work (& parent -> timestamp ,
474
476
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ );
475
477
}
476
478
477
- static void gs_usb_timestamp_stop (struct gs_can * dev )
479
+ static void gs_usb_timestamp_stop (struct gs_usb * parent )
478
480
{
479
- cancel_delayed_work_sync (& dev -> timestamp );
481
+ cancel_delayed_work_sync (& parent -> timestamp );
480
482
}
481
483
482
484
static void gs_update_state (struct gs_can * dev , struct can_frame * cf )
@@ -560,6 +562,9 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
560
562
if (!netif_device_present (netdev ))
561
563
return ;
562
564
565
+ if (!netif_running (netdev ))
566
+ goto resubmit_urb ;
567
+
563
568
if (hf -> echo_id == -1 ) { /* normal rx */
564
569
if (hf -> flags & GS_CAN_FLAG_FD ) {
565
570
skb = alloc_canfd_skb (dev -> netdev , & cfd );
@@ -833,6 +838,7 @@ static int gs_can_open(struct net_device *netdev)
833
838
.mode = cpu_to_le32 (GS_CAN_MODE_START ),
834
839
};
835
840
struct gs_host_frame * hf ;
841
+ struct urb * urb = NULL ;
836
842
u32 ctrlmode ;
837
843
u32 flags = 0 ;
838
844
int rc , i ;
@@ -855,23 +861,27 @@ static int gs_can_open(struct net_device *netdev)
855
861
}
856
862
857
863
if (!parent -> active_channels ) {
864
+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
865
+ gs_usb_timestamp_init (parent );
866
+
858
867
for (i = 0 ; i < GS_MAX_RX_URBS ; i ++ ) {
859
- struct urb * urb ;
860
868
u8 * buf ;
861
869
862
870
/* alloc rx urb */
863
871
urb = usb_alloc_urb (0 , GFP_KERNEL );
864
- if (!urb )
865
- return - ENOMEM ;
872
+ if (!urb ) {
873
+ rc = - ENOMEM ;
874
+ goto out_usb_kill_anchored_urbs ;
875
+ }
866
876
867
877
/* alloc rx buffer */
868
878
buf = kmalloc (dev -> parent -> hf_size_rx ,
869
879
GFP_KERNEL );
870
880
if (!buf ) {
871
881
netdev_err (netdev ,
872
882
"No memory left for USB buffer\n" );
873
- usb_free_urb ( urb ) ;
874
- return - ENOMEM ;
883
+ rc = - ENOMEM ;
884
+ goto out_usb_free_urb ;
875
885
}
876
886
877
887
/* fill, anchor, and submit rx urb */
@@ -894,9 +904,7 @@ static int gs_can_open(struct net_device *netdev)
894
904
netdev_err (netdev ,
895
905
"usb_submit failed (err=%d)\n" , rc );
896
906
897
- usb_unanchor_urb (urb );
898
- usb_free_urb (urb );
899
- break ;
907
+ goto out_usb_unanchor_urb ;
900
908
}
901
909
902
910
/* Drop reference,
@@ -926,13 +934,9 @@ static int gs_can_open(struct net_device *netdev)
926
934
flags |= GS_CAN_MODE_FD ;
927
935
928
936
/* if hardware supports timestamps, enable it */
929
- if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP ) {
937
+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
930
938
flags |= GS_CAN_MODE_HW_TIMESTAMP ;
931
939
932
- /* start polling timestamp */
933
- gs_usb_timestamp_init (dev );
934
- }
935
-
936
940
/* finally start device */
937
941
dev -> can .state = CAN_STATE_ERROR_ACTIVE ;
938
942
dm .flags = cpu_to_le32 (flags );
@@ -942,17 +946,32 @@ static int gs_can_open(struct net_device *netdev)
942
946
GFP_KERNEL );
943
947
if (rc ) {
944
948
netdev_err (netdev , "Couldn't start device (err=%d)\n" , rc );
945
- if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
946
- gs_usb_timestamp_stop (dev );
947
949
dev -> can .state = CAN_STATE_STOPPED ;
948
- return rc ;
950
+
951
+ goto out_usb_kill_anchored_urbs ;
949
952
}
950
953
951
954
parent -> active_channels ++ ;
952
955
if (!(dev -> can .ctrlmode & CAN_CTRLMODE_LISTENONLY ))
953
956
netif_start_queue (netdev );
954
957
955
958
return 0 ;
959
+
960
+ out_usb_unanchor_urb :
961
+ usb_unanchor_urb (urb );
962
+ out_usb_free_urb :
963
+ usb_free_urb (urb );
964
+ out_usb_kill_anchored_urbs :
965
+ if (!parent -> active_channels ) {
966
+ usb_kill_anchored_urbs (& dev -> tx_submitted );
967
+
968
+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
969
+ gs_usb_timestamp_stop (parent );
970
+ }
971
+
972
+ close_candev (netdev );
973
+
974
+ return rc ;
956
975
}
957
976
958
977
static int gs_usb_get_state (const struct net_device * netdev ,
@@ -998,14 +1017,13 @@ static int gs_can_close(struct net_device *netdev)
998
1017
999
1018
netif_stop_queue (netdev );
1000
1019
1001
- /* stop polling timestamp */
1002
- if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
1003
- gs_usb_timestamp_stop (dev );
1004
-
1005
1020
/* Stop polling */
1006
1021
parent -> active_channels -- ;
1007
1022
if (!parent -> active_channels ) {
1008
1023
usb_kill_anchored_urbs (& parent -> rx_submitted );
1024
+
1025
+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
1026
+ gs_usb_timestamp_stop (parent );
1009
1027
}
1010
1028
1011
1029
/* Stop sending URBs */
0 commit comments