Subversion Repositories Kolibri OS

Rev

Rev 3555 | Rev 3908 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. When the kernel detects a connected USB device, it configures the device in
  2. terms of USB protocol - SET_ADDRESS + SET_CONFIGURATION, the first
  3. configuration is always selected. The kernel also reads device descriptor to
  4. print some information, reads and parses configuration descriptor. For every
  5. interface the kernel looks for class code of this interface and loads the
  6. corresponding COFF driver. Currently the correspondence is hardcoded into
  7. the kernel code and looks as follows: 3 = usbhid.obj, 7 = usbprint.obj,
  8. 8 = usbstor.obj, 9 is handled by the kernel itself, other = usbother.obj.
  9.  
  10. The driver must be standard driver in COFF format, exporting procedure
  11. named "START" and a variable named "version". Loader calls "START" procedure
  12. as stdcall with one parameter DRV_ENTRY = 1; if initialization is successful,
  13. the "START" procedure is also called by shutdown code with one parameter
  14. DRV_EXIT = -1.
  15.  
  16. The driver must register itself as a USB driver in "START" procedure.
  17. This is done by call to exported function RegUSBDriver and passing the returned
  18. value as result of "START" procedure.
  19.  
  20. void* __stdcall RegUSBDriver(
  21.     const char* name,
  22.     void* handler,
  23.     const USBFUNC* usbfunc
  24. );
  25.  
  26. The parameter 'name' should match the name of driver, "usbhid" for usbhid.obj.
  27. The parameter 'handler' is optional; if it is non-NULL, it should point to
  28. the standard handler for IOCTL interface as in non-USB drivers.
  29. The parameter 'usbfunc' is a pointer to the following structure:
  30.  
  31. struc USBFUNC
  32. {
  33.   .strucsize         dd ? ; size of the structure, including this field
  34.   .add_device        dd ? ; pointer to AddDevice function in the driver
  35.                           ; required
  36.   .device_disconnect dd ? ; pointer to DeviceDisconnected function in the driver
  37.                           ; optional, may be NULL
  38. ; other functions may be added in the future
  39. }
  40.  
  41. The driver should implement the function
  42.  
  43. void* __stdcall AddDevice(
  44.     void* pipe0,
  45.     void* configdescr,
  46.     void* interfacedescr
  47. );
  48.  
  49. The parameter 'pipe0' is a handle of the control pipe for endpoint zero
  50. of the device. It can be used as the argument of USBControlTransferAsync.
  51. The parameter 'configdescr' points to USB configuration descriptor
  52. and all associated data, as returned by GET_DESCRIPTOR request.
  53. The total length of all associated data is contained in the configuration
  54. descriptor.
  55. The parameter 'interfacedescr' points to USB interface descriptor corresponding
  56. to the interface which is initializing. This is a pointer inside data
  57. associated with the configuration descriptor.
  58. Note that one device can implement many interfaces, so AddDevice may be
  59. called several times with the same 'configdescr' and different 'interfacedescr'.
  60. The returned value NULL means that the initialization has failed.
  61. Any other value means that configuration was successful; the kernel does not
  62. try to interpret the value. It can be, for example, pointer to the internal
  63. data allocated with Kmalloc, or index in some internal table. Remember that
  64. Kmalloc() is NOT stdcall, it destroys ebx.
  65.  
  66. The driver can implement the function
  67.  
  68. void __stdcall DeviceDisconnected(
  69.     void* devicedata
  70. );
  71.  
  72. If this function is implemented, the kernel calls it when the device is
  73. disconnected, passing the returned value of AddDevice as 'devicedata'.
  74.  
  75. The driver can use the following functions exported by the kernel.
  76.  
  77. void* __stdcall USBOpenPipe(
  78.     void* pipe0,
  79.     int endpoint,
  80.     int maxpacketsize,
  81.     int type,
  82.     int interval
  83. );
  84.  
  85. The parameter 'pipe0' is a handle of the pipe for endpoint zero for
  86. the device, as passed to AddDevice. It is used to identify the device.
  87. The parameter 'endpoint' is endpoint number as defined by USB. Lower
  88. 4 bits form the number itself, bit 7 - highest bit of low byte -
  89. is 0/1 for OUT/IN endpoints, other bits should be zero.
  90. The parameter 'maxpacketsize' sets the maximum packet size for this pipe.
  91. The parameter 'type' selects the type of the endpoint as defined by USB:
  92. 0 = control, 1 = isochronous (not supported yet), 2 = bulk, 3 = interrupt.
  93. The parameter 'interval' is ignored for control and bulk endpoints.
  94. For interrupt endpoints, it sets the polling interval in milliseconds.
  95. The function returns a handle to the pipe or NULL on failure.
  96. The output handle becomes invalid when a) it is explicitly closed with
  97. the following function or b) the function DeviceDisconnected provided
  98. by the driver returns.
  99.  
  100. void __stdcall USBClosePipe(
  101.     void* pipe
  102. );
  103.  
  104. Releases all resources associated with the given pipe. The only parameter
  105. must be a handle returned by USBOpenPipe.
  106. When a device is disconnected, all associated pipes are closed by the kernel;
  107. there is no need to ever call this function if all pipes are used continuously
  108. while a device is connected.
  109.  
  110. void* __stdcall USBNormalTransferAsync(
  111.     void* pipe,
  112.     void* buffer,
  113.     int size,
  114.     void* callback,
  115.     void* calldata,
  116.     int flags
  117. );
  118. void* __stdcall USBControlTransferAsync(
  119.     void* pipe,
  120.     void* setup,
  121.     void* buffer,
  122.     int size,
  123.     void* callback,
  124.     void* calldata,
  125.     int flags
  126. );
  127.  
  128. The first function inserts a bulk or interrupt transfer to the transfer queue
  129. for given pipe. Type and direction of transfer are fixed for bulk and interrupt
  130. endpoints and are set in USBOpenPipe. The second function inserts a control
  131. transfer to the transfer queue for given pipe. Direction of a control transfer
  132. is concluded from 'setup' packet, bit 7 of byte 0 is set for IN transfers
  133. and cleared for OUT transfers. These function return immediately; when data
  134. are transferred, the callback function will be called.
  135.  
  136. The parameter 'pipe' is a handle returned by USBOpenPipe.
  137. The parameter 'setup' of USBControlTransferAsync points to 8-byte
  138. configuration packet as defined by USB.
  139. The parameter 'buffer' is a pointer to buffer. For IN transfers, it will be
  140. filled with the data. For OUT transfers, it should contain data to be
  141. transferred. It can be NULL for an empty transfer or if no additional data are
  142. required for a control transfer.
  143. The parameter 'size' is size of data to transfer. It can be 0 for an empty
  144. transfer or if no additional data are required for a control transfer.
  145. The parameter 'callback' is a pointer to a function which will be called
  146. when the transfer will be done.
  147. The parameter 'calldata' will be passed as is to the callback function.
  148. For example, it can be NULL, it can be a pointer to device data or it can be
  149. a pointer to data used to pass additional parameters between caller and
  150. callback. The transfer-specific data can also be associated with 'buffer',
  151. preceding (negative offsets from 'buffer') or following (offsets more than
  152. or equal to 'size') the buffer itself.
  153. The parameter 'flags' is the bitmask.
  154. The bit 0 is ignored for OUT transfers, for IN transfers it controls whether
  155. the device can transfer less data than 'size' bytes. If the bit is 0, a small
  156. transfer is an error; if the bit is 1, a small transfer is OK.
  157. All other bits are reserved and should be zero.
  158. The returned value is NULL if an error occured and non-NULL if the transfer
  159. was successfully queued. If an error will occur later, the callback function
  160. will be notified.
  161.  
  162. void __stdcall CallbackFunction(
  163.     void* pipe,
  164.     int status,
  165.     void* buffer,
  166.     int length,
  167.     void* calldata
  168. );
  169.  
  170. The parameters 'pipe', 'buffer', 'calldata' are the same as for the
  171. corresponding USB*TransferAsync.
  172. The parameter 'length' is the number of bytes transferred. For
  173. control transfers, this includes 8 bytes from SETUP stage, so
  174. 0 means that SETUP stage failed and 'size'+8 means full transfer.
  175. The parameter 'status' is nonzero if an error occured.
  176. USB_STATUS_OK           = 0     ; no error
  177. USB_STATUS_CRC          = 1     ; CRC error
  178. USB_STATUS_BITSTUFF     = 2     ; bit stuffing violation
  179. USB_STATUS_TOGGLE       = 3     ; data toggle mismatch
  180. USB_STATUS_STALL        = 4     ; device returned STALL
  181. USB_STATUS_NORESPONSE   = 5     ; device not responding
  182. USB_STATUS_PIDCHECK     = 6     ; invalid PID check bits
  183. USB_STATUS_WRONGPID     = 7     ; unexpected PID value
  184. USB_STATUS_OVERRUN      = 8     ; too many data from endpoint
  185. USB_STATUS_UNDERRUN     = 9     ; too few data from endpoint
  186. USB_STATUS_BUFOVERRUN   = 12    ; overflow of internal controller buffer
  187.                                 ; possible only for isochronous transfers
  188. USB_STATUS_BUFUNDERRUN  = 13    ; underflow of internal controller buffer
  189.                                 ; possible only for isochronous transfers
  190. USB_STATUS_CLOSED       = 16    ; pipe closed, either explicitly with USBClosePipe
  191.                                 ; or due to device disconnect
  192.  
  193. If several transfers are queued for the same pipe, their callback functions
  194. are called in the same order as they were queued.
  195. When a pipe is closed, either explicitly with USBClosePipe, or
  196. implicitly due to device disconnect, all callback functions are called
  197. with USB_STATUS_CLOSED. The call to DeviceDisconnected() occurs after
  198. all callbacks.
  199.