tools/usb_gadget/linux_gadgetfs.py
def StartEndpoint(self, endpoint_desc):
    """Activate an endpoint.

    To enable a hardware endpoint the appropriate endpoint file must be opened
    and the endpoint descriptors written to it. Linux requires both full- and
    high-speed descriptors to be written for a high-speed device but since the
    endpoint is always reinitialized after disconnect only the high-speed
    endpoint will be valid in this case.

    Args:
      endpoint_desc: Endpoint descriptor.

    Raises:
      RuntimeError: If the hardware endpoint is in use or the configuration
          is not supported by the hardware.
    """
    endpoint_addr = endpoint_desc.bEndpointAddress
    name, hw_ep_type, hw_ep_size = self._hw_eps[endpoint_addr]

    if name in self._ep_fds:
      raise RuntimeError('Hardware endpoint {} already in use.'.format(name))

    ep_type = USB_TRANSFER_TYPE_TO_MASK[
        endpoint_desc.bmAttributes & usb_constants.TransferType.MASK]
    ep_size = endpoint_desc.wMaxPacketSize

    if not hw_ep_type & ep_type:
      raise RuntimeError('Hardware endpoint {} does not support this transfer '
                         'type.'.format(name))
    elif hw_ep_size < ep_size:
      raise RuntimeError('Hardware endpoint {} only supports a maximum packet '
                         'size of {}, {} requested.'
                         .format(name, hw_ep_size, ep_size))

    fd = os.open(os.path.join(self._ep_dir, name), os.O_RDWR)

    buf = struct.pack('=I', 1)
    if self._gadget.GetSpeed() == usb_constants.Speed.HIGH:
      # The full speed endpoint descriptor will not be used but Linux requires
      # one to be provided.
      full_speed_endpoint = usb_descriptors.EndpointDescriptor(
          bEndpointAddress=endpoint_desc.bEndpointAddress,
          bmAttributes=0,
          wMaxPacketSize=0,
          bInterval=0)
      buf = ''.join([buf, full_speed_endpoint.Encode(), endpoint_desc.Encode()])
    else:
      buf = ''.join([buf, endpoint_desc.Encode()])
    os.write(fd, buf)

    pipe_r, pipe_w = multiprocessing.Pipe(False)
    child = None

    # gadgetfs doesn't support polling on the endpoint file descriptors (why?)
    # so we have to start background threads for each.
    if endpoint_addr & usb_constants.Dir.IN:
      def WriterProcess():
        while True:
          data = pipe_r.recv()
          written = os.write(fd, data)
          print('IN bEndpointAddress=0x{:02X} length={}'
                .format(endpoint_addr, written))

      child = multiprocessing.Process(target=WriterProcess)
      self._ep_fds[endpoint_addr] = fd, child, pipe_w
    else:
      def ReceivePacket(unused_fd, unused_events):
        data = pipe_r.recv()
        print('OUT bEndpointAddress=0x{:02X} length={}'
              .format(endpoint_addr, len(data)))
        self._gadget.ReceivePacket(endpoint_addr, data)

      def ReaderProcess():
        while True:
          data = os.read(fd, ep_size)
          pipe_w.send(data)

      child = multiprocessing.Process(target=ReaderProcess)
      pipe_fd = pipe_r.fileno()
      self._io_loop.add_handler(pipe_fd, ReceivePacket, self._io_loop.READ)
      self._ep_fds[endpoint_addr] = fd, child, pipe_r

    child.start()
    print('Started endpoint 0x{:02X}.'.format(endpoint_addr))
Activate an endpoint.

    To enable a hardware endpoint the appropriate endpoint file must be opened
    and the endpoint descriptors written to it. Linux requires both full- and
    high-speed descriptors to be written for a high-speed device but since the
    endpoint is always reinitialized after disconnect only the high-speed
    endpoint will be valid in this case.

    Args:
      endpoint_desc: Endpoint descriptor.

    Raises:
      RuntimeError: If the hardware endpoint is in use or the configuration
          is not supported by the hardware.
    
Activate an endpoint.
To enable a hardware endpoint the appropriate endpoint file must be opened
and the endpoint descriptors written to it. Linux requires both full- and
high-speed descriptors to be written for a high-speed device but since the
endpoint is always reinitialized after disconnect only the high-speed
endpoint will be valid in this case.
Activate an endpoint . To enable a hardware endpoint the appropriate endpoint file must be opened and the endpoint descriptors written to it . Linux requires both full - and high - speed descriptors to be written for a high - speed device but since the endpoint is always reinitialized after disconnect only the high - speed endpoint will be valid in this case .
def StartEndpoint(self, endpoint_desc):
    endpoint_addr = endpoint_desc.bEndpointAddress
    name, hw_ep_type, hw_ep_size = self._hw_eps[endpoint_addr]
    if name in self._ep_fds:
      raise RuntimeError('Hardware endpoint {} already in use.'.format(name))
    ep_type = USB_TRANSFER_TYPE_TO_MASK[
        endpoint_desc.bmAttributes & usb_constants.TransferType.MASK]
    ep_size = endpoint_desc.wMaxPacketSize
    if not hw_ep_type & ep_type:
      raise RuntimeError('Hardware endpoint {} does not support this transfer '
                         'type.'.format(name))
    elif hw_ep_size < ep_size:
      raise RuntimeError('Hardware endpoint {} only supports a maximum packet '
                         'size of {}, {} requested.'
                         .format(name, hw_ep_size, ep_size))
    fd = os.open(os.path.join(self._ep_dir, name), os.O_RDWR)
    buf = struct.pack('=I', 1)
    if self._gadget.GetSpeed() == usb_constants.Speed.HIGH:
      full_speed_endpoint = usb_descriptors.EndpointDescriptor(
          bEndpointAddress=endpoint_desc.bEndpointAddress,
          bmAttributes=0,
          wMaxPacketSize=0,
          bInterval=0)
      buf = ''.join([buf, full_speed_endpoint.Encode(), endpoint_desc.Encode()])
    else:
      buf = ''.join([buf, endpoint_desc.Encode()])
    os.write(fd, buf)
    pipe_r, pipe_w = multiprocessing.Pipe(False)
    child = None
    if endpoint_addr & usb_constants.Dir.IN:
      def WriterProcess():
        while True:
          data = pipe_r.recv()
          written = os.write(fd, data)
          print('IN bEndpointAddress=0x{:02X} length={}'
                .format(endpoint_addr, written))
      child = multiprocessing.Process(target=WriterProcess)
      self._ep_fds[endpoint_addr] = fd, child, pipe_w
    else:
      def ReceivePacket(unused_fd, unused_events):
        data = pipe_r.recv()
        print('OUT bEndpointAddress=0x{:02X} length={}'
              .format(endpoint_addr, len(data)))
        self._gadget.ReceivePacket(endpoint_addr, data)
      def ReaderProcess():
        while True:
          data = os.read(fd, ep_size)
          pipe_w.send(data)
      child = multiprocessing.Process(target=ReaderProcess)
      pipe_fd = pipe_r.fileno()
      self._io_loop.add_handler(pipe_fd, ReceivePacket, self._io_loop.READ)
      self._ep_fds[endpoint_addr] = fd, child, pipe_r
    child.start()
    print('Started endpoint 0x{:02X}.'.format(endpoint_addr))
def StartEndpoint ( self , endpoint_desc ) : endpoint_addr = endpoint_desc . bEndpointAddress name , hw_ep_type , hw_ep_size = self . _hw_eps [ endpoint_addr ] if name in self . _ep_fds : raise RuntimeError ( 'Hardware endpoint {} already in use.' . format ( name ) ) ep_type = USB_TRANSFER_TYPE_TO_MASK [ endpoint_desc . bmAttributes & usb_constants . TransferType . MASK ] ep_size = endpoint_desc . wMaxPacketSize if not hw_ep_type & ep_type : raise RuntimeError ( 'Hardware endpoint {} does not support this transfer ' 'type.' . format ( name ) ) elif hw_ep_size < ep_size : raise RuntimeError ( 'Hardware endpoint {} only supports a maximum packet ' 'size of {}, {} requested.' . format ( name , hw_ep_size , ep_size ) ) fd = os . open ( os . path . join ( self . _ep_dir , name ) , os . O_RDWR ) buf = struct . pack ( '=I' , 1 ) if self . _gadget . GetSpeed ( ) == usb_constants . Speed . HIGH : full_speed_endpoint = usb_descriptors . EndpointDescriptor ( bEndpointAddress = endpoint_desc . bEndpointAddress , bmAttributes = 0 , wMaxPacketSize = 0 , bInterval = 0 ) buf = '' . join ( [ buf , full_speed_endpoint . Encode ( ) , endpoint_desc . Encode ( ) ] ) else : buf = '' . join ( [ buf , endpoint_desc . Encode ( ) ] ) os . write ( fd , buf ) pipe_r , pipe_w = multiprocessing . Pipe ( False ) child = None if endpoint_addr & usb_constants . Dir . IN : def WriterProcess ( ) : while True : data = pipe_r . recv ( ) written = os . write ( fd , data ) print ( 'IN bEndpointAddress=0x{:02X} length={}' . format ( endpoint_addr , written ) ) child = multiprocessing . Process ( target = WriterProcess ) self . _ep_fds [ endpoint_addr ] = fd , child , pipe_w else : def ReceivePacket ( unused_fd , unused_events ) : data = pipe_r . recv ( ) print ( 'OUT bEndpointAddress=0x{:02X} length={}' . format ( endpoint_addr , len ( data ) ) ) self . _gadget . ReceivePacket ( endpoint_addr , data ) def ReaderProcess ( ) : while True : data = os . read ( fd , ep_size ) pipe_w . send ( data ) child = multiprocessing . Process ( target = ReaderProcess ) pipe_fd = pipe_r . fileno ( ) self . _io_loop . add_handler ( pipe_fd , ReceivePacket , self . _io_loop . READ ) self . _ep_fds [ endpoint_addr ] = fd , child , pipe_r child . start ( ) print ( 'Started endpoint 0x{:02X}.' . format ( endpoint_addr ) )
Activate an endpoint.
Activate an endpoint .
"""Activate an endpoint.

    To enable a hardware endpoint the appropriate endpoint file must be opened
    and the endpoint descriptors written to it. Linux requires both full- and
    high-speed descriptors to be written for a high-speed device but since the
    endpoint is always reinitialized after disconnect only the high-speed
    endpoint will be valid in this case.

    Args:
      endpoint_desc: Endpoint descriptor.

    Raises:
      RuntimeError: If the hardware endpoint is in use or the configuration
          is not supported by the hardware.
    """ # The full speed endpoint descriptor will not be used but Linux requires # one to be provided. # gadgetfs doesn't support polling on the endpoint file descriptors (why?) # so we have to start background threads for each.
If the hardware endpoint is in use or the configuration
is not supported by the hardware.
If the hardware endpoint is in use or the configuration is not supported by the hardware .
RuntimeError
self
endpoint_desc
None