@@ -550,46 +550,56 @@ cfg_if! {
550550 u8 :: from_str_radix( & read_file_to_trimmed_string( dir, file) ?, 16 ) . ok( )
551551 }
552552
553- fn read_usb_port_info ( device_path : & Path ) -> Option <SerialPortType > {
554- let device_path = device_path
553+ fn read_port_type ( path : & Path ) -> Option <SerialPortType > {
554+ let path = path
555555 . canonicalize( )
556556 . ok( ) ?;
557- let subsystem = device_path . join( "subsystem" ) . canonicalize( ) . ok( ) ?;
557+ let subsystem = path . join( "subsystem" ) . canonicalize( ) . ok( ) ?;
558558 let subsystem = subsystem. file_name( ) ?. to_string_lossy( ) ;
559559
560- let usb_interface_path = if subsystem == "usb-serial" {
561- device_path. parent( ) ?
562- } else if subsystem == "usb" {
563- & device_path
564- } else {
565- return None ;
566- } ;
567- let usb_device_path = usb_interface_path. parent( ) ?;
560+ match subsystem. as_ref( ) {
561+ // Broadcom SoC UARTs (of Raspberry Pi devices).
562+ "amba" => Some ( SerialPortType :: Unknown ) ,
563+ "pci" => Some ( SerialPortType :: PciPort ) ,
564+ "pnp" => Some ( SerialPortType :: Unknown ) ,
565+ "usb" => usb_port_type( & path) ,
566+ "usb-serial" => usb_port_type( path. parent( ) ?) ,
567+ _ => None ,
568+ }
569+ }
570+
571+ fn usb_port_type( interface_path: & Path ) -> Option <SerialPortType > {
572+ let info = read_usb_port_info( interface_path) ?;
573+ Some ( SerialPortType :: UsbPort ( info) )
574+ }
575+
576+ fn read_usb_port_info( interface_path: & Path ) -> Option <UsbPortInfo > {
577+ let device_path = interface_path. parent( ) ?;
568578
569- let vid = read_file_to_u16( & usb_device_path , & "idVendor" ) ?;
570- let pid = read_file_to_u16( & usb_device_path , & "idProduct" ) ?;
579+ let vid = read_file_to_u16( & device_path , "idVendor" ) ?;
580+ let pid = read_file_to_u16( & device_path , "idProduct" ) ?;
571581 #[ cfg( feature = "usbportinfo-interface" ) ]
572- let interface = read_file_to_u8( & usb_interface_path, & "bInterfaceNumber" ) ;
573- let serial_number = read_file_to_trimmed_string( & usb_device_path, & "serial" ) ;
574- let product = read_file_to_trimmed_string( & usb_device_path, & "product" ) ;
575- let manufacturer = read_file_to_trimmed_string( & usb_device_path, & "manufacturer" ) ;
576- Some ( SerialPortType :: UsbPort ( UsbPortInfo {
582+ let interface = read_file_to_u8( & interface_path, & "bInterfaceNumber" ) ;
583+ let serial_number = read_file_to_trimmed_string( & device_path, & "serial" ) ;
584+ let product = read_file_to_trimmed_string( & device_path, & "product" ) ;
585+ let manufacturer = read_file_to_trimmed_string( & device_path, & "manufacturer" ) ;
586+
587+ Some ( UsbPortInfo {
577588 vid,
578589 pid,
579590 serial_number,
580591 manufacturer,
581592 product,
582593 #[ cfg( feature = "usbportinfo-interface" ) ]
583594 interface,
584- } ) )
595+ } )
585596 }
586597
587598 /// Scans `/sys/class/tty` for serial devices (on Linux systems without libudev).
588599 pub fn available_ports( ) -> Result <Vec <SerialPortInfo >> {
589600 let mut vec = Vec :: new( ) ;
590601 let sys_path = Path :: new( "/sys/class/tty/" ) ;
591602 let dev_path = Path :: new( "/dev" ) ;
592- let mut s;
593603 for path in sys_path. read_dir( ) . expect( "/sys/class/tty/ doesn't exist on this system" ) {
594604 let raw_path = path?. path( ) . clone( ) ;
595605 let mut path = raw_path. clone( ) ;
@@ -599,16 +609,16 @@ cfg_if! {
599609 continue ;
600610 }
601611
602- let port_type = read_usb_port_info ( & path ) . unwrap_or ( SerialPortType :: Unknown ) ;
603-
604- path . push ( "driver_override" ) ;
605- if path . is_file ( ) {
606- s = String :: new ( ) ;
607- File :: open ( path ) ? . read_to_string ( & mut s ) ? ;
608- if & s == "(null) \n " {
609- continue ;
610- }
611- }
612+ // Determine port type and proceed, if it's a known.
613+ //
614+ // TODO: Switch to a likely more readable let-else statement when our MSRV supports
615+ // it.
616+ let port_type = read_port_type ( & path ) ;
617+ let port_type = if let Some ( port_type ) = port_type {
618+ port_type
619+ } else {
620+ continue ;
621+ } ;
612622
613623 // Generate the device file path `/dev/DEVICE` from the TTY class path
614624 // `/sys/class/tty/DEVICE` and emit a serial device if this path exists. There are
0 commit comments