usbhhidmouse.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //*****************************************************************************
  2. //
  3. // usbhhidmouse.c - This file holds the application interfaces for USB
  4. // mouse devices.
  5. //
  6. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  7. // Software License Agreement
  8. //
  9. // Texas Instruments (TI) is supplying this software for use solely and
  10. // exclusively on TI's microcontroller products. The software is owned by
  11. // TI and/or its suppliers, and is protected under applicable copyright
  12. // laws. You may not combine this software with "viral" open-source
  13. // software in order to form a larger program.
  14. //
  15. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  16. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  17. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  19. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  20. // DAMAGES, FOR ANY REASON WHATSOEVER.
  21. //
  22. // This is part of AM1808 StarterWare USB Library, modified resused from revision 6288 of the
  23. // stellaris USB Library
  24. //
  25. //*****************************************************************************
  26. #include "hw_types.h"
  27. #include "usblib.h"
  28. #include "usbhost.h"
  29. #include "usbhid.h"
  30. #include "usbhhid.h"
  31. #include "usbhhidmouse.h"
  32. //*****************************************************************************
  33. //
  34. //! \addtogroup usblib_host_device
  35. //! @{
  36. //
  37. //*****************************************************************************
  38. //*****************************************************************************
  39. //
  40. // Prototypes for local functions.
  41. //
  42. //*****************************************************************************
  43. static unsigned int USBHMouseCallback(void *pvCBData,
  44. unsigned int ulEvent,
  45. unsigned int ulMsgParam,
  46. void *pvMsgData);
  47. //*****************************************************************************
  48. //
  49. // The size of a USB mouse report.
  50. //
  51. //*****************************************************************************
  52. #define USBHMS_REPORT_SIZE 4
  53. //*****************************************************************************
  54. //
  55. // These are the flags for the tUSBHMouse.ulHIDFlags member variable.
  56. //
  57. //*****************************************************************************
  58. #define USBHMS_DEVICE_PRESENT 0x00000001
  59. //*****************************************************************************
  60. //
  61. // This is the structure definition for a mouse device instance.
  62. //
  63. //*****************************************************************************
  64. typedef struct
  65. {
  66. //
  67. // Global flags for an instance of a mouse.
  68. //
  69. unsigned int ulHIDFlags;
  70. //
  71. // The applications registered callback.
  72. //
  73. tUSBCallback pfnCallback;
  74. //
  75. // The current state of the buttons.
  76. //
  77. unsigned char ucButtons;
  78. //
  79. // This is a local buffer to hold the current HID report that comes up
  80. // from the HID driver layer.
  81. //
  82. unsigned char pucBuffer[USBHMS_REPORT_SIZE];
  83. //
  84. // Heap data for the mouse currently used to read the HID Report
  85. // Descriptor.
  86. //
  87. unsigned char *pucHeap;
  88. //
  89. // Size of the heap in bytes.
  90. //
  91. unsigned int ulHeapSize;
  92. //
  93. // This is the instance value for the HID device that will be used for the
  94. // mouse.
  95. //
  96. unsigned int ulMouseInstance;
  97. }
  98. tUSBHMouse;
  99. //*****************************************************************************
  100. //
  101. // This is the per instance information for a mouse device.
  102. //
  103. //*****************************************************************************
  104. static tUSBHMouse g_sUSBHMouse =
  105. {
  106. 0
  107. };
  108. //*****************************************************************************
  109. //
  110. //! This function is used open an instance of a mouse.
  111. //!
  112. //! \param pfnCallback is the callback function to call when new events occur
  113. //! with the mouse returned.
  114. //! \param pucBuffer is the memory used by the driver to interact with the
  115. //! USB mouse.
  116. //! \param ulSize is the size of the buffer provided by \e pucBuffer.
  117. //!
  118. //! This function is used to open an instance of the mouse. The value
  119. //! returned from this function should be used as the instance identifier for
  120. //! all other USBHMouse calls. The \e pucBuffer memory buffer is used to
  121. //! access the mouse. The buffer size required is at least enough to hold
  122. //! a normal report descriptor for the device.
  123. //!
  124. //! \return Returns the instance identifier for the mouse that is attached.
  125. //! If there is no mouse present this will return 0.
  126. //
  127. //*****************************************************************************
  128. unsigned int
  129. USBHMouseOpen(unsigned int ulIndex, tUSBCallback pfnCallback,
  130. unsigned char *pucBuffer, unsigned int ulSize)
  131. {
  132. //
  133. // Save the callback and data pointers.
  134. //
  135. g_sUSBHMouse.pfnCallback = pfnCallback;
  136. //
  137. // Save the instance pointer for the HID device that was opened.
  138. //
  139. g_sUSBHMouse.ulMouseInstance = USBHHIDOpen(ulIndex, USBH_HID_DEV_MOUSE,
  140. USBHMouseCallback,
  141. (unsigned int)&g_sUSBHMouse);
  142. //
  143. // Save the heap buffer and size.
  144. //
  145. g_sUSBHMouse.pucHeap = pucBuffer;
  146. g_sUSBHMouse.ulHeapSize = ulSize;
  147. return(g_sUSBHMouse.ulMouseInstance);
  148. }
  149. //*****************************************************************************
  150. //
  151. //! This function is used close an instance of a mouse.
  152. //!
  153. //! \param ulInstance is the instance value for this mouse.
  154. //!
  155. //! This function is used to close an instance of the mouse that was opened
  156. //! with a call to USBHMouseOpen(). The \e ulInstance value is the value
  157. //! that was returned when the application called USBHMouseOpen().
  158. //!
  159. //! \return Returns 0.
  160. //
  161. //*****************************************************************************
  162. unsigned int
  163. USBHMouseClose(unsigned int ulInstance)
  164. {
  165. tUSBHMouse *pUSBHMouse;
  166. //
  167. // Recover the pointer to the instance data.
  168. //
  169. pUSBHMouse = (tUSBHMouse *)ulInstance;
  170. //
  171. // Reset the callback to null.
  172. //
  173. pUSBHMouse->pfnCallback = 0;
  174. //
  175. // Call the HID driver layer to close out this instance.
  176. //
  177. USBHHIDClose(pUSBHMouse->ulMouseInstance);
  178. return(0);
  179. }
  180. //*****************************************************************************
  181. //
  182. //! This function is used to initialize a mouse interface after a mouse has
  183. //! been detected.
  184. //!
  185. //! \param ulInstance is the instance value for this mouse.
  186. //!
  187. //! This function should be called after receiving a \b USB_EVENT_CONNECTED
  188. //! event in the callback function provided by USBHMouseOpen(), however it
  189. //! should only be called outside of the callback function. This will
  190. //! initialize the mouse interface and determine how it reports events to the
  191. //! USB host controller. The \e ulInstance value is the value that was
  192. //! returned when the application called USBHMouseOpen(). This function only
  193. //! needs to be called once per connection event but it should be called every
  194. //! time a \b USB_EVENT_CONNECTED event occurs.
  195. //!
  196. //! \return Non-zero values should be assumed to indicate an error condition.
  197. //
  198. //*****************************************************************************
  199. unsigned int
  200. USBHMouseInit(unsigned int ulInstance)
  201. {
  202. tUSBHMouse *pUSBHMouse;
  203. tHIDInstance *pHIDIntance;
  204. pHIDIntance = (tHIDInstance *)ulInstance;
  205. //
  206. // Recover the pointer to the instance data.
  207. //
  208. pUSBHMouse = (tUSBHMouse *)pHIDIntance->ulCBData;
  209. //
  210. // Set the initial rate to only update on mouse state changes.
  211. //
  212. USBHHIDSetIdle(pUSBHMouse->ulMouseInstance, 0, 0);
  213. //
  214. // Read out the Report Descriptor from the mouse and parse it for
  215. // the format of the reports coming back from the mouse.
  216. //
  217. USBHHIDGetReportDescriptor(pUSBHMouse->ulMouseInstance,
  218. pUSBHMouse->pucHeap,
  219. pUSBHMouse->ulHeapSize);
  220. //
  221. // Set the mouse to boot protocol.
  222. //
  223. USBHHIDSetProtocol(pUSBHMouse->ulMouseInstance, 1);
  224. return(0);
  225. }
  226. //*****************************************************************************
  227. //
  228. // This function handles updating the state of the mouse buttons and axis.
  229. //
  230. // \param pUSBHMouse is the pointer to an instance of the mouse data.
  231. //
  232. // This function will check for updates to buttons or X/Y movements and send
  233. // callbacks to the mouse callback function.
  234. //
  235. // \return None.
  236. //
  237. //*****************************************************************************
  238. static void
  239. UpdateMouseState(tUSBHMouse *pUSBHMouse)
  240. {
  241. unsigned int ulButton;
  242. if(pUSBHMouse->pucBuffer[0] != pUSBHMouse->ucButtons)
  243. {
  244. for(ulButton = 1; ulButton <= 0x4; ulButton <<= 1)
  245. {
  246. if(((pUSBHMouse->pucBuffer[0] & ulButton) != 0) &&
  247. ((pUSBHMouse->ucButtons & ulButton) == 0))
  248. {
  249. //
  250. // Send the mouse button press notification to the application.
  251. //
  252. pUSBHMouse->pfnCallback(0,
  253. USBH_EVENT_HID_MS_PRESS,
  254. ulButton,
  255. 0);
  256. }
  257. if(((pUSBHMouse->pucBuffer[0] & ulButton) == 0) &&
  258. ((pUSBHMouse->ucButtons & ulButton) != 0))
  259. {
  260. //
  261. // Send the mouse button release notification to the
  262. // application.
  263. //
  264. pUSBHMouse->pfnCallback(0,
  265. USBH_EVENT_HID_MS_REL,
  266. ulButton,
  267. 0);
  268. }
  269. }
  270. //
  271. // Save the new state.
  272. //
  273. pUSBHMouse->ucButtons = pUSBHMouse->pucBuffer[0];
  274. }
  275. if(pUSBHMouse->pucBuffer[1] != 0)
  276. {
  277. //
  278. // Send the mouse button release notification to the
  279. // application.
  280. //
  281. pUSBHMouse->pfnCallback(0,
  282. USBH_EVENT_HID_MS_X,
  283. (unsigned int)pUSBHMouse->pucBuffer[1],
  284. 0);
  285. }
  286. if(pUSBHMouse->pucBuffer[2] != 0)
  287. {
  288. //
  289. // Send the mouse button release notification to the
  290. // application.
  291. //
  292. pUSBHMouse->pfnCallback(0,
  293. USBH_EVENT_HID_MS_Y,
  294. (unsigned int)pUSBHMouse->pucBuffer[2],
  295. 0);
  296. }
  297. }
  298. //*****************************************************************************
  299. //
  300. //! This function handles event callbacks from the USB HID driver layer.
  301. //!
  302. //! \param pvCBData is the pointer that was passed in to the USBHHIDOpen()
  303. //! call.
  304. //! \param ulEvent is the event that has been passed up from the HID driver.
  305. //! \param ulMsgParam has meaning related to the \e ulEvent that occurred.
  306. //! \param pvMsgData has meaning related to the \e ulEvent that occurred.
  307. //!
  308. //! This function will receive all event updates from the HID driver layer.
  309. //! The mouse driver itself will mostly be concerned with report callbacks
  310. //! from the HID driver layer and parsing them into keystrokes for the
  311. //! application that has registered for callbacks with the USBHMouseOpen()
  312. //! call.
  313. //!
  314. //! \return Non-zero values should be assumed to indicate an error condition.
  315. //
  316. //*****************************************************************************
  317. unsigned int
  318. USBHMouseCallback(void *pvCBData, unsigned int ulEvent,
  319. unsigned int ulMsgParam, void *pvMsgData)
  320. {
  321. tUSBHMouse *pUSBHMouse;
  322. //
  323. // Recover the pointer to the instance data.
  324. //
  325. pUSBHMouse = (tUSBHMouse *)pvCBData;
  326. switch(ulEvent)
  327. {
  328. //
  329. // New mouse has been connected so notify the application.
  330. //
  331. case USB_EVENT_CONNECTED:
  332. {
  333. //
  334. // Remember that a mouse is present.
  335. //
  336. pUSBHMouse->ulHIDFlags |= USBHMS_DEVICE_PRESENT;
  337. //
  338. // Notify the application that a new mouse was connected.
  339. //
  340. pUSBHMouse->pfnCallback(0, ulEvent, ulMsgParam, pvMsgData);
  341. break;
  342. }
  343. case USB_EVENT_DISCONNECTED:
  344. {
  345. //
  346. // No mouse is present.
  347. //
  348. pUSBHMouse->ulHIDFlags &= ~USBHMS_DEVICE_PRESENT;
  349. //
  350. // Notify the application that the mouse was disconnected.
  351. //
  352. pUSBHMouse->pfnCallback(0, ulEvent, ulMsgParam, pvMsgData);
  353. break;
  354. }
  355. case USB_EVENT_RX_AVAILABLE:
  356. {
  357. //
  358. // New mouse report structure was received.
  359. //
  360. USBHHIDGetReport(pUSBHMouse->ulMouseInstance, 0,
  361. pUSBHMouse->pucBuffer,
  362. USBHMS_REPORT_SIZE);
  363. //
  364. // Update the current state of the mouse and notify the application
  365. // of any changes.
  366. //
  367. UpdateMouseState(pUSBHMouse);
  368. break;
  369. }
  370. }
  371. return(0);
  372. }
  373. //*****************************************************************************
  374. //
  375. //! @}
  376. //
  377. //*****************************************************************************