@@ -303,12 +303,6 @@ struct gs_can {
303303 struct can_bittiming_const bt_const , data_bt_const ;
304304 unsigned int channel ; /* channel number */
305305
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-
312306 u32 feature ;
313307 unsigned int hf_size_tx ;
314308
@@ -325,6 +319,13 @@ struct gs_usb {
325319 struct gs_can * canch [GS_MAX_INTF ];
326320 struct usb_anchor rx_submitted ;
327321 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+
328329 unsigned int hf_size_rx ;
329330 u8 active_channels ;
330331};
@@ -388,15 +389,15 @@ static int gs_cmd_reset(struct gs_can *dev)
388389 GFP_KERNEL );
389390}
390391
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 ,
392393 u32 * timestamp_p )
393394{
394395 __le32 timestamp ;
395396 int rc ;
396397
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 ,
398399 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE ,
399- dev -> channel , 0 ,
400+ 0 , 0 ,
400401 & timestamp , sizeof (timestamp ),
401402 USB_CTRL_GET_TIMEOUT ,
402403 GFP_KERNEL );
@@ -410,73 +411,74 @@ static inline int gs_usb_get_timestamp(const struct gs_can *dev,
410411
411412static u64 gs_usb_timestamp_read (const struct cyclecounter * cc ) __must_hold (& dev - > tc_lock )
412413{
413- struct gs_can * dev = container_of (cc , struct gs_can , cc );
414+ struct gs_usb * parent = container_of (cc , struct gs_usb , cc );
414415 u32 timestamp = 0 ;
415416 int err ;
416417
417- lockdep_assert_held (& dev -> tc_lock );
418+ lockdep_assert_held (& parent -> tc_lock );
418419
419420 /* 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 );
423424 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 );
427428
428429 return timestamp ;
429430}
430431
431432static void gs_usb_timestamp_work (struct work_struct * work )
432433{
433434 struct delayed_work * delayed_work = to_delayed_work (work );
434- struct gs_can * dev ;
435+ struct gs_usb * parent ;
435436
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 );
440441
441- schedule_delayed_work (& dev -> timestamp ,
442+ schedule_delayed_work (& parent -> timestamp ,
442443 GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ );
443444}
444445
445446static void gs_usb_skb_set_timestamp (struct gs_can * dev ,
446447 struct sk_buff * skb , u32 timestamp )
447448{
448449 struct skb_shared_hwtstamps * hwtstamps = skb_hwtstamps (skb );
450+ struct gs_usb * parent = dev -> parent ;
449451 u64 ns ;
450452
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 );
454456
455457 hwtstamps -> hwtstamp = ns_to_ktime (ns );
456458}
457459
458- static void gs_usb_timestamp_init (struct gs_can * dev )
460+ static void gs_usb_timestamp_init (struct gs_usb * parent )
459461{
460- struct cyclecounter * cc = & dev -> cc ;
462+ struct cyclecounter * cc = & parent -> cc ;
461463
462464 cc -> read = gs_usb_timestamp_read ;
463465 cc -> mask = CYCLECOUNTER_MASK (32 );
464466 cc -> shift = 32 - bits_per (NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ );
465467 cc -> mult = clocksource_hz2mult (GS_USB_TIMESTAMP_TIMER_HZ , cc -> shift );
466468
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 );
471473
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 ,
474476 GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ );
475477}
476478
477- static void gs_usb_timestamp_stop (struct gs_can * dev )
479+ static void gs_usb_timestamp_stop (struct gs_usb * parent )
478480{
479- cancel_delayed_work_sync (& dev -> timestamp );
481+ cancel_delayed_work_sync (& parent -> timestamp );
480482}
481483
482484static 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)
560562 if (!netif_device_present (netdev ))
561563 return ;
562564
565+ if (!netif_running (netdev ))
566+ goto resubmit_urb ;
567+
563568 if (hf -> echo_id == -1 ) { /* normal rx */
564569 if (hf -> flags & GS_CAN_FLAG_FD ) {
565570 skb = alloc_canfd_skb (dev -> netdev , & cfd );
@@ -833,6 +838,7 @@ static int gs_can_open(struct net_device *netdev)
833838 .mode = cpu_to_le32 (GS_CAN_MODE_START ),
834839 };
835840 struct gs_host_frame * hf ;
841+ struct urb * urb = NULL ;
836842 u32 ctrlmode ;
837843 u32 flags = 0 ;
838844 int rc , i ;
@@ -855,23 +861,27 @@ static int gs_can_open(struct net_device *netdev)
855861 }
856862
857863 if (!parent -> active_channels ) {
864+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
865+ gs_usb_timestamp_init (parent );
866+
858867 for (i = 0 ; i < GS_MAX_RX_URBS ; i ++ ) {
859- struct urb * urb ;
860868 u8 * buf ;
861869
862870 /* alloc rx urb */
863871 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+ }
866876
867877 /* alloc rx buffer */
868878 buf = kmalloc (dev -> parent -> hf_size_rx ,
869879 GFP_KERNEL );
870880 if (!buf ) {
871881 netdev_err (netdev ,
872882 "No memory left for USB buffer\n" );
873- usb_free_urb ( urb ) ;
874- return - ENOMEM ;
883+ rc = - ENOMEM ;
884+ goto out_usb_free_urb ;
875885 }
876886
877887 /* fill, anchor, and submit rx urb */
@@ -894,9 +904,7 @@ static int gs_can_open(struct net_device *netdev)
894904 netdev_err (netdev ,
895905 "usb_submit failed (err=%d)\n" , rc );
896906
897- usb_unanchor_urb (urb );
898- usb_free_urb (urb );
899- break ;
907+ goto out_usb_unanchor_urb ;
900908 }
901909
902910 /* Drop reference,
@@ -926,13 +934,9 @@ static int gs_can_open(struct net_device *netdev)
926934 flags |= GS_CAN_MODE_FD ;
927935
928936 /* if hardware supports timestamps, enable it */
929- if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP ) {
937+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
930938 flags |= GS_CAN_MODE_HW_TIMESTAMP ;
931939
932- /* start polling timestamp */
933- gs_usb_timestamp_init (dev );
934- }
935-
936940 /* finally start device */
937941 dev -> can .state = CAN_STATE_ERROR_ACTIVE ;
938942 dm .flags = cpu_to_le32 (flags );
@@ -942,17 +946,32 @@ static int gs_can_open(struct net_device *netdev)
942946 GFP_KERNEL );
943947 if (rc ) {
944948 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 );
947949 dev -> can .state = CAN_STATE_STOPPED ;
948- return rc ;
950+
951+ goto out_usb_kill_anchored_urbs ;
949952 }
950953
951954 parent -> active_channels ++ ;
952955 if (!(dev -> can .ctrlmode & CAN_CTRLMODE_LISTENONLY ))
953956 netif_start_queue (netdev );
954957
955958 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 ;
956975}
957976
958977static int gs_usb_get_state (const struct net_device * netdev ,
@@ -998,14 +1017,13 @@ static int gs_can_close(struct net_device *netdev)
9981017
9991018 netif_stop_queue (netdev );
10001019
1001- /* stop polling timestamp */
1002- if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
1003- gs_usb_timestamp_stop (dev );
1004-
10051020 /* Stop polling */
10061021 parent -> active_channels -- ;
10071022 if (!parent -> active_channels ) {
10081023 usb_kill_anchored_urbs (& parent -> rx_submitted );
1024+
1025+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
1026+ gs_usb_timestamp_stop (parent );
10091027 }
10101028
10111029 /* Stop sending URBs */
0 commit comments