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.

379 lines
15 KiB

  1. /*++
  2. Copyright (C) 1999- Microsoft Corporation
  3. Module Name:
  4. minidrv.h
  5. Abstract:
  6. This module declares CWiaMiniDriver class
  7. Author:
  8. William Hsieh (williamh) created
  9. Revision History:
  10. --*/
  11. #ifndef MINIDRV__H_
  12. #define MINIDRV__H_
  13. DECLARE_INTERFACE(INonDelegatingUnknown)
  14. {
  15. STDMETHOD(NonDelegatingQueryInterface) (THIS_ REFIID riid, LPVOID FAR* ppv) PURE;
  16. STDMETHOD_(ULONG,NonDelegatingAddRef) (THIS) PURE;
  17. STDMETHOD_(ULONG,NonDelegatingRelease) (THIS) PURE;
  18. };
  19. //
  20. // General purpose GUIDs
  21. //
  22. DEFINE_GUID(GUID_NULL, 0,0,0,0,0,0,0,0,0,0,0);
  23. DEFINE_GUID(FMT_NOTHING,
  24. 0x81a566e7,0x8620,0x4fba,0xbc,0x8e,0xb2,0x7c,0x17,0xad,0x9e,0xfd);
  25. //
  26. // This is the HRESULT code we used to report that a device error has occurred
  27. //
  28. const HRESULT HRESULT_DEVICE_ERROR = HRESULT_FROM_WIN32(ERROR_GEN_FAILURE);
  29. const HRESULT HRESULT_DEVICE_NOT_RESPONDING = HRESULT_FROM_WIN32(ERROR_TIMEOUT);
  30. //
  31. // Device error codes
  32. //
  33. enum
  34. {
  35. DEVERR_OK = 0,
  36. DEVERR_UNKNOWN
  37. };
  38. #define DEVERR_MIN DEVERR_OK
  39. #define DEVERR_MAX DEVERR_UNKNOWN
  40. //
  41. // Session ID to use
  42. //
  43. const ULONG WIA_SESSION_ID = 1;
  44. //
  45. // Handy constants for common item types
  46. //
  47. const ULONG ITEMTYPE_FILE = WiaItemTypeFile;
  48. const ULONG ITEMTYPE_IMAGE = WiaItemTypeFile | WiaItemTypeImage;
  49. const ULONG ITEMTYPE_AUDIO = WiaItemTypeFile | WiaItemTypeAudio;
  50. const ULONG ITEMTYPE_VIDEO = WiaItemTypeFile | WiaItemTypeVideo;
  51. const ULONG ITEMTYPE_FOLDER = WiaItemTypeFolder;
  52. const ULONG ITEMTYPE_BURST = WiaItemTypeFolder | WiaItemTypeBurst;
  53. const ULONG ITEMTYPE_HPAN = WiaItemTypeFolder | WiaItemTypeHPanorama;
  54. const ULONG ITEMTYPE_VPAN = WiaItemTypeFolder | WiaItemTypeVPanorama;
  55. //
  56. // Maximum number of vendor-defined events supported
  57. //
  58. const ULONG MAX_VENDOR_EVENTS = 128;
  59. //
  60. // Structure which holds everything needed for each format type.
  61. //
  62. typedef struct _FORMAT_INFO
  63. {
  64. LPGUID FormatGuid; // WIA format GUID
  65. PWSTR FormatString; // item name in a printf-style format string
  66. LONG ItemType; // WIA item type
  67. PWSTR ExtString; // file name extension
  68. } FORMAT_INFO, *PFORMAT_INFO;
  69. //
  70. // Structure for holding information about each property.
  71. //
  72. typedef struct _PROP_INFO
  73. {
  74. PROPID PropId; // WIA property id
  75. LPOLESTR PropName; // WIA property name
  76. } PROP_INFO, *PPROP_INFO;
  77. //
  78. // structure for holding information about vendor events
  79. class CVendorEventInfo
  80. {
  81. public:
  82. GUID *pGuid; // WIA GUID for event
  83. BSTR EventName; // may be NULL if vendor did not provide event name in INF file
  84. CVendorEventInfo() : pGuid(NULL), EventName(NULL) {};
  85. ~CVendorEventInfo()
  86. {
  87. if (pGuid) delete pGuid;
  88. SysFreeString(EventName);
  89. }
  90. };
  91. //
  92. // Driver item context
  93. //
  94. typedef struct tagDrvItemContext
  95. {
  96. CPtpObjectInfo *pObjectInfo; // pointer to the PTP ObjectInfo structure
  97. ULONG NumFormatInfos; // number of format infos stored
  98. WIA_FORMAT_INFO *pFormatInfos; // supported formats array
  99. ULONG ThumbSize; // thumnail image size in bytes
  100. BYTE *pThumb; // thumnail bits
  101. }DRVITEM_CONTEXT, *PDRVITEM_CONTEXT;
  102. #ifdef DEADCODE
  103. //
  104. // Tree node. These are used to temporarily hold the items between reading
  105. // all of the PTP objects and creating the PTP item tree.
  106. //
  107. typedef struct _OBJECT_TREE_NODE
  108. {
  109. CPtpObjectInfo *pObjectInfo;
  110. struct _OBJECT_TREE_NODE *pNext;
  111. struct _OBJECT_TREE_NODE *pFirstChild;
  112. } OBJECT_TREE_NODE, *POBJECT_TREE_NODE;
  113. #endif // DEADCODE
  114. //
  115. // Our minidriver clsid.
  116. //
  117. #if defined( _WIN32 ) && !defined( _NO_COM)
  118. // b5ee90b0-d5c5-11d2-82d5-00c04f8ec183
  119. DEFINE_GUID(CLSID_PTPWiaMiniDriver,
  120. 0xb5ee90b0,0xd5c5,0x11d2,0x82,0xd5,0x00,0xc0,0x4f,0x8e,0xc1,0x83);
  121. #endif
  122. class CWiaMiniDriver :
  123. public INonDelegatingUnknown,
  124. public IStiUSD,
  125. public IWiaMiniDrv
  126. {
  127. public:
  128. CWiaMiniDriver(LPUNKNOWN punkOuter);
  129. ~CWiaMiniDriver();
  130. //
  131. // INonDelegatingUnknown interface
  132. //
  133. STDMETHODIMP_(ULONG) NonDelegatingAddRef();
  134. STDMETHODIMP_(ULONG) NonDelegatingRelease();
  135. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, LPVOID * ppvObj);
  136. //
  137. // IUnknown interface
  138. //
  139. STDMETHODIMP_(ULONG) AddRef( void);
  140. STDMETHODIMP_(ULONG) Release( void);
  141. STDMETHODIMP QueryInterface( REFIID riid, LPVOID * ppvObj);
  142. //
  143. // IStiUSD interface
  144. //
  145. STDMETHODIMP Initialize(PSTIDEVICECONTROL pHelDcb, DWORD dwStiVersion, HKEY hParametersKey);
  146. STDMETHODIMP GetCapabilities (PSTI_USD_CAPS pDevCaps);
  147. STDMETHODIMP GetStatus (PSTI_DEVICE_STATUS pDevStatus);
  148. STDMETHODIMP DeviceReset();
  149. STDMETHODIMP Diagnostic(LPDIAG pBuffer);
  150. STDMETHODIMP SetNotificationHandle(HANDLE hEvent);
  151. STDMETHODIMP GetNotificationData(LPSTINOTIFY lpNotify);
  152. STDMETHODIMP Escape(STI_RAW_CONTROL_CODE EscapeFunction, LPVOID lpInData, DWORD cbInDataSize,
  153. LPVOID pOutData, DWORD dwOutDataSize, LPDWORD pdwActualDataSize);
  154. STDMETHODIMP GetLastError (LPDWORD pdwLastDeviceError);
  155. STDMETHODIMP GetLastErrorInfo (STI_ERROR_INFO *pLastErrorInfo);
  156. STDMETHODIMP LockDevice();
  157. STDMETHODIMP UnLockDevice();
  158. STDMETHODIMP RawReadData(LPVOID lpBuffer, LPDWORD lpdwNumberOfBytes, LPOVERLAPPED lpOverlapped);
  159. STDMETHODIMP RawWriteData(LPVOID lpBuffer, DWORD nNumberOfBytes, LPOVERLAPPED lpOverlapped);
  160. STDMETHODIMP RawReadCommand(LPVOID lpBuffer, LPDWORD lpdwNumberOfBytes, LPOVERLAPPED lpOverlapped);
  161. STDMETHODIMP RawWriteCommand(LPVOID lpBuffer, DWORD nNumberOfBytes, LPOVERLAPPED lpOverlapped);
  162. //
  163. // IWiaMiniDrv interface
  164. //
  165. STDMETHODIMP drvInitializeWia(BYTE *pWiasContext, LONG lFlags, BSTR bstrDeviceID, BSTR bstrRootFullItemName,
  166. IUnknown *pStiDevice, IUnknown *pIUnknownOuter, IWiaDrvItem **ppIDrvItemRoot,
  167. IUnknown **ppIUnknownInner, LONG *plDevErrVal);
  168. STDMETHODIMP drvUnInitializeWia(BYTE* pWiasContext);
  169. STDMETHODIMP drvDeviceCommand(BYTE *pWiasContext, LONG lFlags, const GUID *pGUIDCommand,
  170. IWiaDrvItem **ppMiniDrvItem, LONG *plDevErrVal);
  171. STDMETHODIMP drvDeleteItem(BYTE *pWiasContext, LONG lFlags, LONG *plDevErrVal);
  172. STDMETHODIMP drvGetCapabilities(BYTE *pWiasContext, LONG lFlags, LONG *pCelt,
  173. WIA_DEV_CAP_DRV **ppCapabilities, LONG *plDevErrVal);
  174. STDMETHODIMP drvInitItemProperties(BYTE *pWiasContext, LONG lFlags, LONG *plDevErrVal);
  175. STDMETHODIMP drvLockWiaDevice(BYTE *pWiasContext, LONG lFlags, LONG *plDevErrVal);
  176. STDMETHODIMP drvUnLockWiaDevice(BYTE *pWiasContext, LONG lFlags, LONG *plDevErrVal);
  177. STDMETHODIMP drvAnalyzeItem(BYTE *pWiasContext, LONG lFlags, LONG *plDevErrVal);
  178. STDMETHODIMP drvGetWiaFormatInfo(BYTE *pWiasContext, LONG lFlags, LONG *pCelt,
  179. WIA_FORMAT_INFO **ppwfi, LONG *plDevErrVal);
  180. STDMETHODIMP drvNotifyPnpEvent(const GUID *pEventGUID, BSTR bstrDeviceID, ULONG ulReserved);
  181. STDMETHODIMP drvReadItemProperties(BYTE *pWiaItem, LONG lFlags, ULONG nPropSpec,
  182. const PROPSPEC *pPropSpec, LONG *plDevErrVal);
  183. STDMETHODIMP drvWriteItemProperties(BYTE *pWiasContext, LONG lFLags,
  184. PMINIDRV_TRANSFER_CONTEXT pmdtc, LONG *plDevErrVal);
  185. STDMETHODIMP drvValidateItemProperties(BYTE *pWiasContext, LONG lFlags, ULONG nPropSpec,
  186. const PROPSPEC *pPropSpec, LONG *plDevErrVal);
  187. STDMETHODIMP drvAcquireItemData(BYTE *pWiasContext, LONG lFlags,
  188. PMINIDRV_TRANSFER_CONTEXT pDataContext, LONG *plDevErrVal);
  189. STDMETHODIMP drvGetDeviceErrorStr(LONG lFlags, LONG lDevErrVal, LPOLESTR *ppszDevErrStr, LONG *plDevErrVal);
  190. STDMETHODIMP drvFreeDrvItemContext(LONG lFlags, BYTE *pDevContext, LONG *plDevErrVal);
  191. //
  192. // Public helper functions (in eventcb.cpp)
  193. //
  194. HRESULT EventCallbackDispatch(PPTP_EVENT pEvent);
  195. private:
  196. //
  197. // Private helper functions (in minidriver.cpp)
  198. //
  199. HRESULT Shutdown();
  200. HRESULT TakePicture(BYTE *pWiasContext, IWiaDrvItem **ppNewItem);
  201. LONG GetTotalFreeImageSpace();
  202. HRESULT WiasContextToItemContext(BYTE *pWiasContext, DRVITEM_CONTEXT **ppItemContext,
  203. IWiaDrvItem **ppDrvItem = NULL);
  204. HRESULT LoadStrings();
  205. HRESULT GetResourceString(LONG lResourceID, WCHAR *pString, int length);
  206. HRESULT InitVendorExtentions(HKEY hkDevParams);
  207. HRESULT UpdateStorageInfo(ULONG StorageId);
  208. HRESULT NotifyWiaOnStateChanges();
  209. //
  210. // Private helper functions (in devitem.cpp)
  211. //
  212. HRESULT CreateDrvItemTree(IWiaDrvItem **ppRoot);
  213. HRESULT AddObject(DWORD ObjectHandle, BOOL bQueueEvent = FALSE, CPtpObjectInfo *pProvidedObjectInfo = NULL);
  214. HRESULT InitDeviceProperties(BYTE *pWiasContext);
  215. HRESULT ReadDeviceProperties(BYTE *pWiasContext, LONG NumPropSpecs, const PROPSPEC *pPropSpecs);
  216. HRESULT WriteDeviceProperties(BYTE *pWiasContext);
  217. HRESULT ValidateDeviceProperties(BYTE *pWiasContext, LONG NumPropSpecs, const PROPSPEC *pPropSpecs);
  218. HRESULT FindCorrDimension(BYTE *pWiasContext, LONG *pWidth, LONG *pHeight);
  219. int FindProportionalValue(int valueX, int minX, int maxX, int minY, int maxY, int stepY);
  220. PPROP_INFO PropCodeToPropInfo(WORD PropCode);
  221. int NumLogicalStorages();
  222. //
  223. // Private helper functions (in imgitem.cpp)
  224. //
  225. HRESULT InitItemProperties(BYTE *pWiasContext);
  226. HRESULT IsObjectProtected(CPtpObjectInfo *pObjectInfo, LONG &lProtection);
  227. HRESULT GetObjectTime(CPtpObjectInfo *pObjectInfo, SYSTEMTIME *pSystemTime);
  228. HRESULT ReadItemProperties(BYTE *pWiasContext, LONG NumPropSpecs, const PROPSPEC *pPropSpecs);
  229. HRESULT CacheThumbnail(PDRVITEM_CONTEXT pDrvItemCtx);
  230. HRESULT ValidateItemProperties(BYTE *pWiasContext, LONG NumPropSpecs, const PROPSPEC *pPropSpecs, LONG ItemType);
  231. HRESULT AcquireDataAndTranslate(BYTE *pWiasContext, DRVITEM_CONTEXT *pItemCtx, PMINIDRV_TRANSFER_CONTEXT pmdtc);
  232. HRESULT AcquireAndTranslateJpegWithoutGeometry(BYTE *pWiasContext, DRVITEM_CONTEXT *pItemCtx, PMINIDRV_TRANSFER_CONTEXT pmdtc);
  233. HRESULT AcquireAndTranslateAnyImage(BYTE *pWiasContext, DRVITEM_CONTEXT *pItemCtx, PMINIDRV_TRANSFER_CONTEXT pmdtc);
  234. HRESULT AcquireData(DRVITEM_CONTEXT *pItemCtx, PMINIDRV_TRANSFER_CONTEXT pmdtc);
  235. //
  236. // Event handling functions (in eventcb.cpp)
  237. //
  238. HRESULT AddNewObject(DWORD ObjectHandle);
  239. HRESULT RemoveObject(DWORD ObjectHandle);
  240. HRESULT AddNewStorage(DWORD StorageId);
  241. HRESULT RemoveStorage(DWORD StorageId);
  242. HRESULT DevicePropChanged(WORD PropCode);
  243. HRESULT ObjectInfoChanged(DWORD ObjectHandle);
  244. HRESULT StorageFull(DWORD StorageId);
  245. HRESULT StorageInfoChanged(DWORD StorageId);
  246. HRESULT CaptureComplete();
  247. HRESULT PostVendorEvent(WORD EventCode);
  248. //
  249. // Inline utilities
  250. //
  251. BOOL IsItemTypeFolder(LONG ItemType)
  252. {
  253. return ((WiaItemTypeFolder & ItemType) &&
  254. !(ItemType & (WiaItemTypeStorage | WiaItemTypeRoot)));
  255. }
  256. //
  257. // Member variables
  258. //
  259. WIA_DEV_CAP_DRV *m_Capabilities; // List of device capabilities. Build once and use every time we are asked
  260. UINT m_nEventCaps; // Number of events supported
  261. UINT m_nCmdCaps; // Number of commands supported
  262. BOOL m_fInitCaptureChecked; // Indicates if we have already queried camera for InitiateCapture command support
  263. int m_OpenApps; // Number of apps that are communicating with this driver
  264. IWiaDrvItem *m_pDrvItemRoot; // Pointer to the root of the driver item tree
  265. CPTPCamera *m_pPTPCamera; // Pointer to camera object--actually holds CUsbCamera object
  266. CPtpDeviceInfo m_DeviceInfo; // DeviceInfo structure for the camera
  267. CArray32 m_StorageIds; // Holds the current storage ids
  268. CWiaArray<CPtpStorageInfo>
  269. m_StorageInfos; // Holds the StorageInfo structures
  270. CWiaArray<CPtpPropDesc>
  271. m_PropDescs; // Property description structures
  272. CWiaMap<ULONG, IWiaDrvItem *>
  273. m_HandleItem; // Used to map PTP object handles to WIA driver items
  274. LONG m_NumImages; // The number of images currently on the device
  275. IStiDevice *m_pStiDevice; // Pointer to the driver's STI interface
  276. BSTR m_bstrDeviceId; // STI device ID
  277. BSTR m_bstrRootItemFullName; // Full name of root item
  278. PSTIDEVICECONTROL m_pDcb; // Pointer to the IStiDeviceControl interface
  279. HANDLE m_TakePictureDoneEvent; // Event handle to indicate when TakePicture command is done
  280. DWORD m_VendorExtId; // Vendor extension id (from registry)
  281. CWiaMap<WORD, PROP_INFO *>
  282. m_VendorPropMap; // Maps PropCodes to PROP_INFO structures
  283. CWiaMap<WORD, CVendorEventInfo*>
  284. m_VendorEventMap; // Maps EventCodes to event info
  285. HANDLE m_hPtpMutex; // Mutex used for exclusive access to device
  286. CArray32 m_DcimHandle; // ObjectHandle of the DCIM folder for each storage, if it exists
  287. CWiaMap<ULONG, IWiaDrvItem *>
  288. m_AncAssocParent; // Maps ancillary association handles to their parents
  289. DWORD m_dwObjectBeingSent; // Temporary location for object handle between SendObjectInfo and SendObject
  290. CWiaArray<DWORD> m_CapturedObjects; // List of new objects reported during capture operation, but not yet processed
  291. BOOL m_bTwoDigitsMillisecondsOutput; // In XP, PTP driver was sending DATETIME string to camera with two digits
  292. // for milliseconds (bug 699699). Some cameras may expect this format.
  293. ULONG m_Refs; // Reference count on the object
  294. IUnknown *m_punkOuter; // Pointer to outer IUnknown
  295. };
  296. //
  297. // Functions for handling PTP callbacks
  298. //
  299. HRESULT EventCallback(LPVOID pCallbackParam, PPTP_EVENT pEvent);
  300. HRESULT DataCallback(LPVOID pCallbackParam, LONG lPercentComplete,
  301. LONG lOffset, LONG lLength, BYTE **ppBuffer, LONG *plBufferSize);
  302. //
  303. // Helper functions
  304. //
  305. PFORMAT_INFO FormatCodeToFormatInfo(WORD FormatCode, WORD AssocType = 0);
  306. WORD FormatGuidToFormatCode(GUID *pFormatGuid);
  307. WORD PropIdToPropCode(PROPID PropId);
  308. VOID SplitImageSize(CBstr cbstr, LONG *pWidth, LONG *pHeight);
  309. //
  310. // Simple object for handling mutexes. It will make sure that the mutex is released
  311. // no matter how the function is exited.
  312. //
  313. class CPtpMutex
  314. {
  315. public:
  316. CPtpMutex(HANDLE hMutex);
  317. ~CPtpMutex();
  318. private:
  319. HANDLE m_hMutex;
  320. };
  321. #endif // #ifndef MINIDRV__H_