Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.7 KiB

  1. // Gemplus (C) 1999
  2. //
  3. // Version 1.0
  4. // Author: Sergey Ivanov
  5. // Date of creation - 04.03.1999
  6. // Change log:
  7. //
  8. #ifndef USB_DEV
  9. #define USB_DEV
  10. #include "wdmdev.h"
  11. #include "debug.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #pragma warning (disable:4200)
  16. #include <usbdi.h>
  17. #include <usbdlib.h>
  18. #pragma warning (default:4200)
  19. #ifdef __cplusplus
  20. }
  21. #endif
  22. // Type of request driver can construct
  23. #define COMMAND_REQUEST 1
  24. #define RESPONSE_REQUEST 2
  25. #define INTERRUPT_REQUEST 3
  26. // Default buffers' sizes (4k)
  27. // This values will be used as a requests to the bus driver.
  28. // It looks like bus driver will not accept values greater then these.
  29. // It will complain with "invalid parameter" status.
  30. // ??? Is this limitation of bus driver or our driver design?
  31. #define DEFAULT_COMMAND_BUFFER_SIZE 0x100
  32. #define DEFAULT_RESPONSE_BUFFER_SIZE 0x100
  33. #define DEFAULT_INTERRUPT_BUFFER_SIZE 0x100
  34. // If we set Xfer size greater then 256, bus driver crashes with GPF
  35. // The problem still is under investigation...
  36. #define GUR_MAX_TRANSFER_SIZE 256
  37. #pragma LOCKEDCODE
  38. // Power request callback
  39. NTSTATUS onPowerRequestCompletion(IN PDEVICE_OBJECT DeviceObject,IN UCHAR MinorFunction,
  40. IN POWER_STATE PowerState,IN PVOID Context,IN PIO_STATUS_BLOCK IoStatus);
  41. NTSTATUS onPowerIrpComplete(IN PDEVICE_OBJECT NullDeviceObject,IN PIRP Irp,IN PVOID Context);
  42. #pragma PAGEDCODE
  43. class CWDMDevice;
  44. class CUSBDevice : public CWDMDevice
  45. {
  46. public:
  47. NTSTATUS m_Status;
  48. SAFE_DESTRUCTORS();
  49. private:
  50. ULONG Idle_conservation;
  51. ULONG Idle_performance;
  52. // Max xfer size with our device
  53. ULONG m_MaximumTransferSize;
  54. // USB device endpoints
  55. USBD_PIPE_HANDLE m_ControlPipe;
  56. USBD_PIPE_HANDLE m_InterruptPipe;
  57. USBD_PIPE_HANDLE m_ResponsePipe;
  58. USBD_PIPE_HANDLE m_CommandPipe;
  59. // ptr to the USB device descriptor
  60. // for this device
  61. PUSB_DEVICE_DESCRIPTOR m_DeviceDescriptor;
  62. // USB configuration handle and ptr for the configuration the
  63. // device is currently in
  64. USBD_CONFIGURATION_HANDLE m_ConfigurationHandle;
  65. PUSB_CONFIGURATION_DESCRIPTOR m_Configuration;
  66. // we support one interface
  67. // this is a copy of the info structure
  68. // returned from select_configuration or
  69. // select_interface
  70. PUSBD_INTERFACE_INFORMATION m_Interface;
  71. //Bus drivers set the appropriate values in this structure in response
  72. //to an IRP_MN_QUERY_CAPABILITIES IRP. Function and filter drivers might
  73. //alter the capabilities set by the bus driver.
  74. DEVICE_CAPABILITIES Capabilities;
  75. // used to save the currently-being-handled system-requested power irp request
  76. //PIRP PowerIrp;
  77. // Xfer buffers will be dynamically allocated at device start
  78. ULONG ResponseBufferLength;
  79. PVOID m_ResponseBuffer;// Bulk IN pipe
  80. LONG Response_ErrorNum;
  81. ULONG CommandBufferLength;
  82. PVOID m_CommandBuffer;// Bulk OUT pipe
  83. LONG Command_ErrorNum;
  84. ULONG InterruptBufferLength;
  85. PVOID m_InterruptBuffer;// Interrupt IN pipe
  86. LONG Interrupt_ErrorNum;
  87. public:
  88. CUSBDevice();
  89. ~CUSBDevice();
  90. virtual VOID dispose()
  91. {
  92. removeRef();
  93. if(!getRefCount()) self_delete();
  94. else
  95. {
  96. TRACE("FAILED TO DISPOSE OBJECT! refcount %x\n",getRefCount());
  97. }
  98. };
  99. protected:
  100. virtual NTSTATUS PnPHandler(LONG HandlerID,IN PIRP Irp);
  101. virtual NTSTATUS PnP_HandleRemoveDevice(IN PIRP Irp);
  102. virtual NTSTATUS PnP_HandleStartDevice(IN PIRP Irp);
  103. virtual NTSTATUS PnP_HandleStopDevice(IN PIRP Irp);
  104. virtual NTSTATUS PnP_StartDevice();
  105. virtual VOID PnP_StopDevice();
  106. virtual NTSTATUS PnP_HandleQueryRemove(IN PIRP Irp);
  107. virtual NTSTATUS PnP_HandleCancelRemove(IN PIRP Irp);
  108. virtual NTSTATUS PnP_HandleQueryStop(IN PIRP Irp);
  109. virtual NTSTATUS PnP_HandleCancelStop(IN PIRP Irp);
  110. virtual NTSTATUS PnP_HandleQueryRelations(IN PIRP Irp);
  111. virtual NTSTATUS PnP_HandleQueryInterface(IN PIRP Irp);
  112. virtual NTSTATUS PnP_HandleQueryCapabilities(IN PIRP Irp);
  113. virtual NTSTATUS PnP_HandleQueryResources(IN PIRP Irp);
  114. virtual NTSTATUS PnP_HandleQueryResRequirements(IN PIRP Irp);
  115. virtual NTSTATUS PnP_HandleQueryText(IN PIRP Irp);
  116. virtual NTSTATUS PnP_HandleFilterResRequirements(IN PIRP Irp);
  117. virtual NTSTATUS PnP_HandleReadConfig(IN PIRP Irp);
  118. virtual NTSTATUS PnP_HandleWriteConfig(IN PIRP Irp);
  119. virtual NTSTATUS PnP_HandleEject(IN PIRP Irp);
  120. virtual NTSTATUS PnP_HandleSetLock(IN PIRP Irp);
  121. virtual NTSTATUS PnP_HandleQueryID(IN PIRP Irp);
  122. virtual NTSTATUS PnP_HandleQueryPnPState(IN PIRP Irp);
  123. virtual NTSTATUS PnP_HandleQueryBusInfo(IN PIRP Irp);
  124. virtual NTSTATUS PnP_HandleUsageNotification(IN PIRP Irp);
  125. virtual NTSTATUS PnP_HandleSurprizeRemoval(IN PIRP Irp);
  126. private:
  127. // USB device support functions
  128. PURB buildBusTransferRequest(CIoPacket* Irp,UCHAR Command);
  129. VOID finishBusTransferRequest(CIoPacket* Irp,UCHAR Command);
  130. NTSTATUS QueryBusCapabilities(PDEVICE_CAPABILITIES Capabilities);
  131. PUSB_DEVICE_DESCRIPTOR getDeviceDescriptor();
  132. PUSB_CONFIGURATION_DESCRIPTOR getConfigurationDescriptor();
  133. PUSBD_INTERFACE_INFORMATION activateInterface(PUSB_CONFIGURATION_DESCRIPTOR Configuration);
  134. NTSTATUS disactivateInterface();
  135. //
  136. NTSTATUS resetDevice();
  137. NTSTATUS resetPipe(IN USBD_PIPE_HANDLE Pipe);
  138. NTSTATUS resetAllPipes();
  139. NTSTATUS abortPipes();
  140. // Low level communication functions...
  141. virtual NTSTATUS sendRequestToDevice(CIoPacket* Irp,PIO_COMPLETION_ROUTINE Routine);
  142. virtual NTSTATUS sendRequestToDeviceAndWait(CIoPacket* Irp);
  143. // Handle requests for specific pipes..
  144. virtual NTSTATUS readSynchronously(CIoPacket* Irp,IN USBD_PIPE_HANDLE Pipe);
  145. virtual NTSTATUS writeSynchronously(CIoPacket* Irp,IN USBD_PIPE_HANDLE Pipe);
  146. // Support for the reader interface
  147. virtual NTSTATUS send(CIoPacket* Irp);
  148. virtual NTSTATUS sendAndWait(CIoPacket* Irp);
  149. // virtual NTSTATUS writeAndWait(PUCHAR pRequest,ULONG RequestLength,PUCHAR pReply,ULONG* pReplyLength);
  150. // virtual NTSTATUS readAndWait(PUCHAR pRequest,ULONG RequestLength,PUCHAR pReply,ULONG* pReplyLength);
  151. public:
  152. virtual NTSTATUS writeAndWait(PUCHAR pRequest,ULONG RequestLength,PUCHAR pReply,ULONG* pReplyLength);
  153. virtual NTSTATUS readAndWait(PUCHAR pRequest,ULONG RequestLength,PUCHAR pReply,ULONG* pReplyLength);
  154. virtual NTSTATUS pnpRequest(IN PIRP Irp);
  155. #define _POWER_
  156. #ifdef _POWER_
  157. // POWER MANAGEMENT FUNCTIONS
  158. virtual NTSTATUS powerRequest(IN PIRP Irp);
  159. virtual VOID activatePowerHandler(LONG HandlerID);
  160. virtual VOID disActivatePowerHandler(LONG HandlerID);
  161. virtual NTSTATUS callPowerHandler(LONG HandlerID,IN PIRP Irp);
  162. virtual BOOLEAN setDevicePowerState(IN DEVICE_POWER_STATE DeviceState);
  163. virtual VOID onSystemPowerDown();
  164. virtual VOID onSystemPowerUp();
  165. // Handlers
  166. virtual NTSTATUS power_HandleSetPower(IN PIRP Irp);
  167. virtual NTSTATUS power_HandleWaitWake(IN PIRP Irp);
  168. virtual NTSTATUS power_HandleSequencePower(IN PIRP Irp);
  169. virtual NTSTATUS power_HandleQueryPower(IN PIRP Irp);
  170. // callback
  171. #endif
  172. // USB device specific implementations of system callbacks
  173. // They ovewrite base class defaults.
  174. virtual NTSTATUS open(PIRP Irp)
  175. {
  176. TRACE("***** USB OPEN DEVICE *****\n");
  177. if (!NT_SUCCESS(acquireRemoveLock()))
  178. return completeDeviceRequest(Irp, STATUS_DELETE_PENDING, 0);
  179. releaseRemoveLock();
  180. return completeDeviceRequest(Irp, STATUS_SUCCESS, 0);
  181. };//Create
  182. virtual NTSTATUS close(PIRP Irp)
  183. {
  184. TRACE("***** USB CLOSE DEVICE *****\n");
  185. return completeDeviceRequest(Irp, STATUS_SUCCESS, 0);
  186. };
  187. virtual NTSTATUS deviceControl(IN PIRP Irp);
  188. virtual NTSTATUS read(IN PIRP Irp);
  189. virtual NTSTATUS write(IN PIRP Irp);
  190. virtual NTSTATUS createDeviceObjectByName(PDEVICE_OBJECT* ppFdo);
  191. };
  192. #endif // If defined