@@ -261,14 +261,21 @@ struct canfd_quirk {
261261 u8 quirk ;
262262} __packed ;
263263
264+ /* struct gs_host_frame::echo_id == GS_HOST_FRAME_ECHO_ID_RX indicates
265+ * a regular RX'ed CAN frame
266+ */
267+ #define GS_HOST_FRAME_ECHO_ID_RX 0xffffffff
268+
264269struct gs_host_frame {
265- u32 echo_id ;
266- __le32 can_id ;
270+ struct_group (header ,
271+ u32 echo_id ;
272+ __le32 can_id ;
267273
268- u8 can_dlc ;
269- u8 channel ;
270- u8 flags ;
271- u8 reserved ;
274+ u8 can_dlc ;
275+ u8 channel ;
276+ u8 flags ;
277+ u8 reserved ;
278+ );
272279
273280 union {
274281 DECLARE_FLEX_ARRAY (struct classic_can , classic_can );
@@ -568,6 +575,37 @@ gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
568575 return len ;
569576}
570577
578+ static unsigned int
579+ gs_usb_get_minimum_rx_length (const struct gs_can * dev , const struct gs_host_frame * hf ,
580+ unsigned int * data_length_p )
581+ {
582+ unsigned int minimum_length , data_length = 0 ;
583+
584+ if (hf -> flags & GS_CAN_FLAG_FD ) {
585+ if (hf -> echo_id == GS_HOST_FRAME_ECHO_ID_RX )
586+ data_length = can_fd_dlc2len (hf -> can_dlc );
587+
588+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
589+ /* timestamp follows data field of max size */
590+ minimum_length = struct_size (hf , canfd_ts , 1 );
591+ else
592+ minimum_length = sizeof (hf -> header ) + data_length ;
593+ } else {
594+ if (hf -> echo_id == GS_HOST_FRAME_ECHO_ID_RX &&
595+ !(hf -> can_id & cpu_to_le32 (CAN_RTR_FLAG )))
596+ data_length = can_cc_dlc2len (hf -> can_dlc );
597+
598+ if (dev -> feature & GS_CAN_FEATURE_HW_TIMESTAMP )
599+ /* timestamp follows data field of max size */
600+ minimum_length = struct_size (hf , classic_can_ts , 1 );
601+ else
602+ minimum_length = sizeof (hf -> header ) + data_length ;
603+ }
604+
605+ * data_length_p = data_length ;
606+ return minimum_length ;
607+ }
608+
571609static void gs_usb_receive_bulk_callback (struct urb * urb )
572610{
573611 struct gs_usb * parent = urb -> context ;
@@ -576,6 +614,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
576614 int rc ;
577615 struct net_device_stats * stats ;
578616 struct gs_host_frame * hf = urb -> transfer_buffer ;
617+ unsigned int minimum_length , data_length ;
579618 struct gs_tx_context * txc ;
580619 struct can_frame * cf ;
581620 struct canfd_frame * cfd ;
@@ -594,6 +633,15 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
594633 return ;
595634 }
596635
636+ minimum_length = sizeof (hf -> header );
637+ if (urb -> actual_length < minimum_length ) {
638+ dev_err_ratelimited (& parent -> udev -> dev ,
639+ "short read (actual_length=%u, minimum_length=%u)\n" ,
640+ urb -> actual_length , minimum_length );
641+
642+ goto resubmit_urb ;
643+ }
644+
597645 /* device reports out of range channel id */
598646 if (hf -> channel >= parent -> channel_cnt )
599647 goto device_detach ;
@@ -609,20 +657,33 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
609657 if (!netif_running (netdev ))
610658 goto resubmit_urb ;
611659
612- if (hf -> echo_id == -1 ) { /* normal rx */
660+ minimum_length = gs_usb_get_minimum_rx_length (dev , hf , & data_length );
661+ if (urb -> actual_length < minimum_length ) {
662+ stats -> rx_errors ++ ;
663+ stats -> rx_length_errors ++ ;
664+
665+ if (net_ratelimit ())
666+ netdev_err (netdev ,
667+ "short read (actual_length=%u, minimum_length=%u)\n" ,
668+ urb -> actual_length , minimum_length );
669+
670+ goto resubmit_urb ;
671+ }
672+
673+ if (hf -> echo_id == GS_HOST_FRAME_ECHO_ID_RX ) { /* normal rx */
613674 if (hf -> flags & GS_CAN_FLAG_FD ) {
614675 skb = alloc_canfd_skb (netdev , & cfd );
615676 if (!skb )
616677 return ;
617678
618679 cfd -> can_id = le32_to_cpu (hf -> can_id );
619- cfd -> len = can_fd_dlc2len ( hf -> can_dlc ) ;
680+ cfd -> len = data_length ;
620681 if (hf -> flags & GS_CAN_FLAG_BRS )
621682 cfd -> flags |= CANFD_BRS ;
622683 if (hf -> flags & GS_CAN_FLAG_ESI )
623684 cfd -> flags |= CANFD_ESI ;
624685
625- memcpy (cfd -> data , hf -> canfd -> data , cfd -> len );
686+ memcpy (cfd -> data , hf -> canfd -> data , data_length );
626687 } else {
627688 skb = alloc_can_skb (netdev , & cf );
628689 if (!skb )
@@ -631,7 +692,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
631692 cf -> can_id = le32_to_cpu (hf -> can_id );
632693 can_frame_set_cc_len (cf , hf -> can_dlc , dev -> can .ctrlmode );
633694
634- memcpy (cf -> data , hf -> classic_can -> data , 8 );
695+ memcpy (cf -> data , hf -> classic_can -> data , data_length );
635696
636697 /* ERROR frames tell us information about the controller */
637698 if (le32_to_cpu (hf -> can_id ) & CAN_ERR_FLAG )
@@ -687,7 +748,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
687748resubmit_urb :
688749 usb_fill_bulk_urb (urb , parent -> udev ,
689750 parent -> pipe_in ,
690- hf , dev -> parent -> hf_size_rx ,
751+ hf , parent -> hf_size_rx ,
691752 gs_usb_receive_bulk_callback , parent );
692753
693754 rc = usb_submit_urb (urb , GFP_ATOMIC );
@@ -750,8 +811,21 @@ static void gs_usb_xmit_callback(struct urb *urb)
750811 struct gs_can * dev = txc -> dev ;
751812 struct net_device * netdev = dev -> netdev ;
752813
753- if (urb -> status )
754- netdev_info (netdev , "usb xmit fail %u\n" , txc -> echo_id );
814+ if (!urb -> status )
815+ return ;
816+
817+ if (urb -> status != - ESHUTDOWN && net_ratelimit ())
818+ netdev_info (netdev , "failed to xmit URB %u: %pe\n" ,
819+ txc -> echo_id , ERR_PTR (urb -> status ));
820+
821+ netdev -> stats .tx_dropped ++ ;
822+ netdev -> stats .tx_errors ++ ;
823+
824+ can_free_echo_skb (netdev , txc -> echo_id , NULL );
825+ gs_free_tx_context (txc );
826+ atomic_dec (& dev -> active_tx_urbs );
827+
828+ netif_wake_queue (netdev );
755829}
756830
757831static netdev_tx_t gs_can_start_xmit (struct sk_buff * skb ,
0 commit comments