úÎ, &FC      !"#$%&'()*+,-./0123456789:;<=>?@AB#Bas van Dijk <v.dijk.bas@gmail.com>X0Control transfers can have three request types: Standard, Class and Vendor. We disallow Standard, requests however because with them you can 9destroy the safety guarantees that this module provides. DHandy type synonym that names the parameters of a control transfer. C2Class of transfer types that support enumerating. An enumerator for an  endpoint with either a   or    transfer type. +Chunk size. A good value for this would be  the D . E. -Timeout (in milliseconds) that this function / should wait for each chunk before giving up . due to no response being received. For no  timeout, use value 0. F-Class of transfer types that support writing Write bytes to an  endpoint with either a   or    transfer type.  Exceptions:  G if the endpoint halted.  H& if the device has been disconnected.  Another I. (Handy type synonym for write transfers. A  WriteAction9 is a function which takes the bytestring to write and a Qtimeout. The function returns an action which, when exectued, returns the number Nof bytes that were actually written paired with an indication if the transfer  timed out. J.Class of transfer types that support reading. Read bytes from an  endpoint with either a   or    transfer type.  Exceptions:  G if the endpoint halted.  K" if the device offered more data,  see Packets and overflows in the libusb documentation:   9http://libusb.sourceforge.net/api-1.0/packetoverflow.html.  H& if the device has been disconnected.  Another I. 'Handy type synonym for read transfers. A  ReadActionB is a function which takes a size which defines how many bytes to Iread and a timeout. The function returns an action which, when executed, Qperforms the actual read and returns the bytestring that was read paired with an &indication if the transfer timed out. 9In transfer direction (device -> host) used for reading. :Out transfer direction (host -> device) used for writing. I/CO operations on endpoints are type-safe. You can only read from an endpoint with an : transfer direction and you can only write to an endpoint with an  transfer direction. JReading and writing also have different implementations for the different endpoint transfer types like:   and  . I/O with endpoints of other transfer types like   and   is not possible. LThis type lifts the transfer direction and transfer type information to the type-level so that I/O operations like  and  can &specify which endpoints they support. 5You can retrieve the endpoints of an alternate using 6. L(A handle to a setted alternate setting. 'You get a handle to an alternate using 3, 4 or 5. The type variable sAlt is used to ensure that you can't )return this handle from these functions. M A supported 0 alternate setting which you can retrieve using  2. N*A regional handle to a claimed interface. DA regional handle to a claimed interface can be created by applying / or 0% to the interface you wish to claim. OFA supported interface of a configuration which you can retrieve using  .. To retrieve the P! descriptors of an interface use #. Q This exception can be thrown in , to indicate that the $device is currently not configured. !This exception can be thrown in:  )  +, , or -  3, 4 or 5 Cto indicate that the device was already configured with a setting.  A handle to an active ! which you can get using: +, , or -. The type variable sCfg is used to ensure that you can't return this handle from these functions. R!FA supported configuration of a USB device parameterized by the region r in which it was created. QNote that, just like a regional device handle, a configuration can be duplicated to a parent region using S. KAlso note that you can get the descriptor of the configuration by applying # to it. T"#*Get the descriptor of a given USB entity. $+A regional handle to an opened USB device. EA regional handle to an opened USB device can be created by applying % or &% to the USB device you wish to open. Note that you can also  duplicate& a regional device handle by applying S to it. U%AOpen a device and obtain a regional device handle. The device is 1automatically closed when the region terminates. DThis is a non-blocking function; no requests are sent over the bus.  Exceptions:  V* if there is a memory allocation failure.  W+ if the user has insufficient permissions.  H& if the device has been disconnected.  Another I. &LConvenience function which opens the device, applies the given continuation Pfunction to the resulting regional device handle and runs the resulting region. 'IConvenience function which finds the first device attached to the system Nwhich satisfies the given predicate on its descriptor, then opens that device Land applies the given continuation function to the resulting device handle.  Exceptions:  X< if no device is found which satisfies the given predicate.  V* if there is a memory allocation failure.  W+ if the user has insufficient permissions.  H& if the device has been disconnected.  Another I. $Predicate on the device descriptor. Continuation function YFInternally used function which searches through the given list of USB J entities (like Devices, Configs, Interfaces or Alternates) for the first M entity which satisfies the given predicate on its descriptor. Then opens or L sets that entity by applying the given open or set function to the entity. With Predicate on descriptor Continuation function ZIInternally used function for getting the actual USB device handle from a  regional device handle. (GConvenience function for retrieving the device from the given regional  handle. )KPerform a USB port reset to reinitialize a device. The system will attempt Qto restore the previous configuration and alternate settings after the reset has  completed. Note the constraint: pr `ParentOf` cr". This allows this function to be executed in any child region cr of the parent region pr in which the given regional handle was created. <You can only reset a device when all computations passed to +, , and -# have been terminated. If you call  resetDevice+ and such a computation is still running a  exception is thrown. LIf the reset fails, the descriptors change, or the previous state cannot be Prestored, the device will appear to be disconnected and reconnected. This means Othat the device handle is no longer valid (you should close it) and rediscover the device. A X. is raised to indicate that this is the case. OTODO: Think about how to handle the implications of the the previous paragraph! EThis is a blocking function which usually incurs a noticeable delay.  Exceptions:  ' if a configuration has been set using +,  , and -.  X* if re-enumeration is required, or if the ! device has been disconnected.  Another I. *FRetrieve the supported configurations from the given regional handle. @Note that the configuration is parameterized by the same region r in which the Lregional handle was created. This ensures you can never use a configuration outside that region. [JInternally used function for getting all the configuration descriptors of  the given device. +CSet the active configuration for a device and then apply the given =continuation function to the resulting configuration handle. OUSB devices support multiple configurations of which only one can be active at 2any given time. When a configuration is set using +, , or -> no threads can set a new configuration until the computation Apassed to these functions terminates. If you do try to set one a  exception will be thrown. PThe operating system may or may not have already set an active configuration on Pthe device. It is up to your application to ensure the correct configuration is Qselected before you attempt to claim interfaces and perform other operations. If 5you want to use the current active configuration use ,. KIf you call this function on a device already configured with the selected Mconfiguration, then this function will act as a lightweight device reset: it Pwill issue a SET_CONFIGURATION request using the current configuration, causing Nmost USB-related device state to be reset (altsetting reset to zero, endpoint halts cleared, toggles reset). You cannot change/:reset configuration if other applications or drivers have claimed interfaces. This is a blocking function.  Exceptions:  / if a configuration has already been set using  +, , or -.  \& if interfaces are currently claimed.  H% if the device has been disconnected  Another I. #The configuration you wish to set. Continuation function. ](Internally used function which throws a  exception if  the given MVar was set. If the given MVar wasn't set it will be set M before the given computation is performed. When the computation terminates, 1 wheter normally or by raising an exception, the MVar will be unset again. ,IApply the given continuation function to the configuration handle of the 9current active configuration of the given device handle. HThis function needs to determine the current active configuration. This <information may be cached by the operating system. If it isn't cached this Jfunction will block while a control transfer is submitted to retrieve the  information.  Exceptions:  / if a configuration has already been set using  +, , or -.  " if the device is not configured.  H& if the device has been disconnected.  Aanother I. Regional handle to the device " from which you want to use the  active configuration. Continuation function -MConvenience function which finds the first configuration of the given device Mhandle which satisfies the given predicate on its descriptor, then sets that Lconfiguration and applies the given function to the resulting configuration handle. This function calls + so do see its documentation.  Exceptions:  / if a configuration has already been set using  +, , or -.  X7 if no configuration is found that satisfies the given  predicate.  \& if interfaces are currently claimed.  H% if the device has been disconnected  Another I. "Regional handle to the device for  which you want to set a  configuration. Predicate on the configuration  descriptor. Continuation function. .ARetrieve the supported interfaces from the configuration handle. 0Note that the interface is parameterized by the sCfg of the configuration Ohandle it is derived from. This ensures that it can never be returned from the 2functions that created this configuration handle: +, , and -. NThe latter is useful because outside those functions the active configuration Emay change. If at that moment you still have an interface of the old -configuration claiming it would be an error. /HClaim the given interface in the region. When the region terminates the %interface is released automatically. ?Note that it is allowed to claim an already-claimed interface. LClaiming of interfaces is a purely logical operation; it does not cause any Mrequests to be sent over the bus. Interface claiming is used to instruct the Nunderlying operating system that your application wishes to take ownership of the interface. !This is a non-blocking function.  Exceptions:  \9 if another program or driver has claimed the interface.  H& if the device has been disconnected.  Another I. Interface you wish to claim 0!The interface you wish to claim. Continuation function. 1BConvenience function which finds the first interface of the given Mconfiguration handle which satisfies the given predicate on its descriptors, Othen claims that interfaces and applies the given continuation function to the resulting regional handle.  Exceptions:  X? if no interface was found that satisfies the fiven predicate.  \9 if another program or driver has claimed the interface.  H& if the device has been disconnected.  Another I. #Handle to a configuration of which # you want to claim an interface. (Predicate on the interface descriptors. Continuation function. 2KRetrieve the supported alternate settings from the given interface handle. ONote that the alternate setting is parameterized by the same type variables as Jthe interface handle. This ensures you can never use an alternate setting >outside the region in which the interface handle was created. 3HActivate an alternate setting for an interface and then apply the given 9continuation function to the resulting alternate handle. OSimillary to configurations, interfaces support multiple alternate settings of Owhich only one can be active at any given time. When an alternate is set using 3, 4 or 5 no threads can set a Qnew alternate until the computation passed to these functions terminates. If you do try to set one a  exception will be thrown. QThe operating system may already have set an alternate for the interface. If you .want to use this current active alternate use 4. This is a blocking function.  Exceptions:  H& if the device has been disconnected.  , if an alternate has already been set using  3, 4 or 5.  Another I. The alternate you wish to set. Continuation function. 4GApply the given function to the alternate handle of the current active (alternate of the give interface handle. KTo determine the current active alternate this function will block while a ;control transfer is submitted to retrieve the information.  Exceptions:  H& if the device has been disconnected.  , if an alternate has already been set using  3, 4 or 5.  Another I. Regional handle to the ! interface from which you want  to use the active alternate. Continuation function. 5LConvenience function which finds the first alternate of the given interface Mhandle which satisfies the given predicate on its descriptor, then sets that Lalternate and applies the given function to the resulting alternate handle. This function calls 3 so do see its documentation.  Exceptions:  X3 if no alternate is found that satisfies the given  predicate.  H& if the device has been disconnected.  , if an alternate has already been set using  3, 4 or 5.  Another I. Regional handle to the  interface for which you want  to set an alternate. Predicate on the interface  descriptor. Continuation function ^_6LRetrieve all the endpoints from the given alternate handle which are of the - given transfer direction and transfer type. Handle to the alternate from " which you want to retrieve its  endpoints.  Filter all endpoints which have  this transfer direction.  Filter all endpoints which have  this transfer type. 7Clear the halt/!stall condition for an endpoint. QEndpoints with halt status are unable to receive or transmit data until the halt condition is stalled. LYou should cancel all pending transfers before attempting to clear the halt  condition. This is a blocking function.  Exceptions:  H& if the device has been disconnected.  Another I. `ab8Perform a USB control& request that does not transfer data.  Exceptions:  c if the transfer timed out.  G8 if the control request was not supported by the device  H& if the device has been disconnected.  Another I. 9Perform a USB control read.  Exceptions:  G8 if the control request was not supported by the device  H& if the device has been disconnected.  Another I. :"A convenience function similar to 9 which checks if the A specified number of bytes to read were actually read. Throws an d  if this is not the case. ;Perform a USB control write.  Exceptions:  G8 if the control request was not supported by the device  H& if the device has been disconnected.  Another I. <(Retrieve a list of supported languages. This function may throw Is. =,Retrieve a string descriptor from a device. PThis is a convenience function which formulates the appropriate control message Oto retrieve the descriptor. The string returned is Unicode, as detailed in the USB specifications. This function may throw Is. /TODO: The following can be made more type-safe!  When I call =4 I would like the type system to guarantee that the given StrIx and LangId actually belong to the given Handle . In other Nwords I would like to get a type error when they are some arbitrary number or come from another device. 9Maximum number of characters in the requested string. An  d# will be thrown when the requested & string is larger than this number. >ERetrieve a string descriptor from a device using the first supported  language. PThis is a convenience function which formulates the appropriate control message Oto retrieve the descriptor. The string returned is Unicode, as detailed in the USB specifications. This function may throw Is. .Maximum number of characters in the requested  string. An d will be thrown when 4 the requested string is larger than this number. ?8Determine if a kernel driver is active on an interface. QIf a kernel driver is active, you cannot claim the interface, and libusb will be unable to perform I/O.  Exceptions:  H& if the device has been disconnected.  Another I. @*Detach a kernel driver from an interface. IIf successful, you will then be able to claim the interface and perform I/O.  Exceptions:  X! if no kernel driver was active.  e" if the interface does not exist.  H& if the device has been disconnected.  Another I. ARe-attach an interface'&s kernel driver, which was previously detached using @.  Exceptions:  X! if no kernel driver was active.  e" if the interface does not exist.  H& if the device has been disconnected.  \8 if the driver cannot be attached because the interface & is claimed by a program or driver.  Another I. BFIf a kernel driver is active on the specified interface the driver is Pdetached and the given action is executed. If the action terminates, whether by Mnormal termination or by raising an exception, the kernel driver is attached Qagain. If a kernel driver is not active on the specified interface the action is just executed.  Exceptions:  H& if the device has been disconnected.  Another I. fMonadic if ... then ... else ... NghijklmnoSp  !"#$%&'()*+,-./0123456789:;<=>?@ABC$%&'("#)!* +,-./01234567  89:;<=>?@ABC  !"##$%&'()*+,-./0123456789:;<=>?@ABq         !"#$%&'()*+,-./0123456789:;<=>?@>?AB>?C>?D>?EF>?G>?HIJ>?K>?L>?MNOP>?QRSTUVW>?X>?Y>?Z[H\]HI^HI_HI`HIaHIbHIcHIdHIeHIfg usb-safe-0.8System.USB.Safe RequestTypeVendorClass ControlActionenumReadEndpoint writeEndpoint WriteAction readEndpoint ReadAction InterruptBulk IsochronousControl TransferTypeInOutTransferDirectionEndpointAlternateHandle AlternateRegionalInterfaceHandle InterfaceNoActiveConfigSettingAlreadySet ConfigHandleConfig GetDescriptorgetDescRegionalDeviceHandle openDevice withDevicewithDeviceWhich getDevice resetDevice getConfigs setConfiguseActiveConfigsetConfigWhich getInterfacesclaim withInterfacewithInterfaceWhich getAlternates setAlternateuseActiveAlternatesetAlternateWhich getEndpoints clearHaltcontrol readControlreadControlExact writeControl getLanguages getStrDescgetStrDescFirstLangkernelDriverActivedetachKernelDriverattachKernelDriverwithDetachedKernelDriverEnumReadEndpointusb-0.5System.USB.Internal maxPacketSizeendpointMaxPacketSize WriteEndpoint PipeExceptionNoDeviceException USBException ReadEndpointOverflowExceptionregions-0.6.0.1#Control.Monad.Trans.Region.InternaldupNoMemExceptionAccessExceptionNotFoundExceptionuseWhichgetInternalDevHndlgetConfigDescs BusyExceptionwithUnsettedMVareqDireqType transferWithwrapreqTypeToInternalTimeoutException IOExceptionInvalidParamExceptionifM#Control.Monad.Trans.Region.ParentOfParentOf liftCatch mapRegionT liftCallCC forkTopRegion runTopRegion runRegionTRegionT TopRegionDup