@@ -496,11 +496,14 @@ static err_t eth_netif_device_init(struct netif *netif)
496496 device = (rt_device_t ) ethif ;
497497 if (rt_device_init (device ) != RT_EOK )
498498 {
499- return ERR_IF ;
499+ rt_kprintf ( "eth device initialization failed!\n" ) ;
500500 }
501- if ( rt_device_open ( device , RT_DEVICE_FLAG_RDWR ) != RT_EOK )
501+ else
502502 {
503- return ERR_IF ;
503+ if (rt_device_open (device , RT_DEVICE_FLAG_RDWR ) != RT_EOK )
504+ {
505+ rt_kprintf ("eth device open failed!\n" );
506+ }
504507 }
505508
506509 /* copy device flags to netif flags */
@@ -684,11 +687,14 @@ static err_t af_unix_eth_netif_device_init(struct netif *netif)
684687 device = (rt_device_t ) ethif ;
685688 if (rt_device_init (device ) != RT_EOK )
686689 {
687- return ERR_IF ;
690+ rt_kprintf ( "eth device initialization failed!\n" ) ;
688691 }
689- if ( rt_device_open ( device , RT_DEVICE_FLAG_RDWR ) != RT_EOK )
692+ else
690693 {
691- return ERR_IF ;
694+ if (rt_device_open (device , RT_DEVICE_FLAG_RDWR ) != RT_EOK )
695+ {
696+ rt_kprintf ("eth device open failed!\n" );
697+ }
692698 }
693699
694700 /* copy device flags to netif flags */
@@ -850,23 +856,64 @@ rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
850856
851857 RT_ASSERT (dev != RT_NULL );
852858
859+ struct eth_device * enetif ;
860+ enetif = (struct eth_device * )dev -> netif -> state ;
861+ rt_device_t device = (rt_device_t )(& (enetif -> parent ));
862+
863+
853864 level = rt_spin_lock_irqsave (& (dev -> spinlock ));
854865 dev -> link_changed = 0x01 ;
855866 if (up == RT_TRUE )
867+ {
868+ /* @note Check whether the eth device was successfully initialized, otherwise re-initialize */
869+ if (!(device -> flag & RT_DEVICE_FLAG_ACTIVATED ))
870+ {
871+ if (rt_device_init (device ) != RT_EOK )
872+ {
873+ rt_kprintf ("eth device initialization failed!\n" );
874+ goto err ;
875+ }
876+ if (rt_device_open (device , RT_DEVICE_FLAG_RDWR ) != RT_EOK )
877+ {
878+ rt_kprintf ("eth device open failed!\n" );
879+ goto err ;
880+ }
881+ }
856882 dev -> link_status = 0x01 ;
883+ }
857884 else
858885 dev -> link_status = 0x00 ;
859886 rt_spin_unlock_irqrestore (& (dev -> spinlock ), level );
860887
861888 /* post message to ethernet thread */
862889 return rt_mb_send (& eth_rx_thread_mb , (rt_ubase_t )dev );
890+
891+ err :
892+ rt_spin_unlock_irqrestore (& (dev -> spinlock ), level );
893+ return - RT_ERROR ;
863894}
864895#else
865896/* NOTE: please not use it in interrupt when no RxThread exist */
866897rt_err_t eth_device_linkchange (struct eth_device * dev , rt_bool_t up )
867898{
868899 if (up == RT_TRUE )
900+ {
901+ /* @note Check whether the eth device was successfully initialized, otherwise re-initialize */
902+ if (!(device -> flag & RT_DEVICE_FLAG_ACTIVATED ))
903+ {
904+ if (rt_device_init (device ) != RT_EOK )
905+ {
906+ rt_kprintf ("eth device initialization failed!\n" );
907+ return - RT_ERROR ;
908+ }
909+ if (rt_device_open (device , RT_DEVICE_FLAG_RDWR ) != RT_EOK )
910+ {
911+ rt_kprintf ("eth device open failed!\n" );
912+ return - RT_ERROR ;
913+ }
914+ }
869915 netifapi_netif_set_link_up (dev -> netif );
916+ }
870917 else
871918 netifapi_netif_set_link_down (dev -> netif );
872919
0 commit comments