usbdhidmouse.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //*****************************************************************************
  2. //
  3. // usbdhidmouse.c - USB HID Mouse device class driver.
  4. //
  5. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of AM1808 StarterWare USB Library and reused from revision 6288
  22. // of the Stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_types.h"
  26. #include "debug.h"
  27. #include "usb.h"
  28. #include "usblib.h"
  29. #include "usbdevice.h"
  30. #include "usbhid.h"
  31. #include "usbdhid.h"
  32. #include "usbdhidmouse.h"
  33. //*****************************************************************************
  34. //
  35. //! \addtogroup hid_mouse_device_class_api
  36. //! @{
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. // The report descriptor for the mouse class device.
  42. //
  43. //*****************************************************************************
  44. static const unsigned char g_pucMouseReportDescriptor[]=
  45. {
  46. UsagePage(USB_HID_GENERIC_DESKTOP),
  47. Usage(USB_HID_MOUSE),
  48. Collection(USB_HID_APPLICATION),
  49. Usage(USB_HID_POINTER),
  50. Collection(USB_HID_PHYSICAL),
  51. //
  52. // The buttons.
  53. //
  54. UsagePage(USB_HID_BUTTONS),
  55. UsageMinimum(1),
  56. UsageMaximum(3),
  57. LogicalMinimum(0),
  58. LogicalMaximum(1),
  59. //
  60. // 3 - 1 bit values for the buttons.
  61. //
  62. ReportSize(1),
  63. ReportCount(3),
  64. Input(USB_HID_INPUT_DATA | USB_HID_INPUT_VARIABLE |
  65. USB_HID_INPUT_ABS),
  66. //
  67. // 1 - 5 bit unused constant value to fill the 8 bits.
  68. //
  69. ReportSize(5),
  70. ReportCount(1),
  71. Input(USB_HID_INPUT_CONSTANT | USB_HID_INPUT_ARRAY |
  72. USB_HID_INPUT_ABS),
  73. //
  74. // The X and Y axis.
  75. //
  76. UsagePage(USB_HID_GENERIC_DESKTOP),
  77. Usage(USB_HID_X),
  78. Usage(USB_HID_Y),
  79. LogicalMinimum(-127),
  80. LogicalMaximum(127),
  81. //
  82. // 2 - 8 bit Values for x and y.
  83. //
  84. ReportSize(8),
  85. ReportCount(2),
  86. Input(USB_HID_INPUT_DATA | USB_HID_INPUT_VARIABLE |
  87. USB_HID_INPUT_RELATIVE),
  88. //
  89. // 2 - 8 bit Values for x and y. // Padding
  90. //
  91. ReportSize(8),
  92. ReportCount(MOUSE_REPORT_SIZE - 3),
  93. Input(USB_HID_INPUT_CONSTANT | USB_HID_INPUT_ARRAY |
  94. USB_HID_INPUT_ABS),
  95. EndCollection,
  96. EndCollection,
  97. };
  98. //*****************************************************************************
  99. //
  100. // The HID class descriptor table. For the mouse class, we have only a single
  101. // report descriptor.
  102. //
  103. //*****************************************************************************
  104. static const unsigned char * const g_pMouseClassDescriptors[] =
  105. {
  106. g_pucMouseReportDescriptor
  107. };
  108. //*****************************************************************************
  109. //
  110. // The HID descriptor for the mouse device.
  111. //
  112. //*****************************************************************************
  113. static const tHIDDescriptor g_sMouseHIDDescriptor =
  114. {
  115. 9, // bLength
  116. USB_HID_DTYPE_HID, // bDescriptorType
  117. 0x111, // bcdHID (version 1.11 compliant)
  118. 0, // bCountryCode (not localized)
  119. 1, // bNumDescriptors
  120. {
  121. {
  122. USB_HID_DTYPE_REPORT, // Report descriptor
  123. sizeof(g_pucMouseReportDescriptor) // Size of report descriptor
  124. }
  125. }
  126. };
  127. //*****************************************************************************
  128. //
  129. // Forward references for mouse device callback functions.
  130. //
  131. //*****************************************************************************
  132. static unsigned int HIDMouseRxHandler(void *pvCBData,
  133. unsigned int ulEvent,
  134. unsigned int ulMsgData,
  135. void *pvMsgData);
  136. static unsigned int HIDMouseTxHandler(void *pvCBData,
  137. unsigned int ulEvent,
  138. unsigned int ulMsgData,
  139. void *pvMsgData);
  140. //*****************************************************************************
  141. //
  142. // The HID mouse report offsets for this mouse application.
  143. //
  144. //*****************************************************************************
  145. #define HID_REPORT_BUTTONS 0
  146. #define HID_REPORT_X 1
  147. #define HID_REPORT_Y 2
  148. //*****************************************************************************
  149. //
  150. // Main HID device class event handler function.
  151. //
  152. // \param pvCBData is the event callback pointer provided during USBDHIDInit().
  153. // This is a pointer to our HID device structure (&g_sHIDMouseDevice).
  154. // \param ulEvent identifies the event we are being called back for.
  155. // \param ulMsgData is an event-specific value.
  156. // \param pvMsgData is an event-specific pointer.
  157. //
  158. // This function is called by the HID device class driver to inform the
  159. // application of particular asynchronous events related to operation of the
  160. // mouse HID device.
  161. //
  162. // \return Returns a value which is event-specific.
  163. //
  164. //*****************************************************************************
  165. static unsigned int
  166. HIDMouseRxHandler(void *pvCBData, unsigned int ulEvent,
  167. unsigned int ulMsgData, void *pvMsgData)
  168. {
  169. tHIDMouseInstance *psInst;
  170. tUSBDHIDMouseDevice *psDevice;
  171. //
  172. // Make sure we didn't get a NULL pointer.
  173. //
  174. ASSERT(pvCBData);
  175. //
  176. // Get a pointer to our instance data
  177. //
  178. psDevice = (tUSBDHIDMouseDevice *)pvCBData;
  179. psInst = psDevice->psPrivateHIDMouseData;
  180. //
  181. // Which event were we sent?
  182. //
  183. switch (ulEvent)
  184. {
  185. //
  186. // The host has connected to us and configured the device.
  187. //
  188. case USB_EVENT_CONNECTED:
  189. {
  190. psInst->ucUSBConfigured = true;
  191. //
  192. // Pass the information on to the client.
  193. //
  194. psDevice->pfnCallback(psDevice->pvCBData, USB_EVENT_CONNECTED,
  195. 0, (void *)0);
  196. break;
  197. }
  198. //
  199. // The host has disconnected from us.
  200. //
  201. case USB_EVENT_DISCONNECTED:
  202. {
  203. psInst->ucUSBConfigured = false;
  204. //
  205. // Pass the information on to the client.
  206. //
  207. psDevice->pfnCallback(psDevice->pvCBData, USB_EVENT_DISCONNECTED,
  208. 0, (void *)0);
  209. break;
  210. }
  211. //
  212. // The host is polling us for a particular report and the HID driver
  213. // is asking for the latest version to transmit.
  214. //
  215. case USBD_HID_EVENT_IDLE_TIMEOUT:
  216. case USBD_HID_EVENT_GET_REPORT:
  217. {
  218. //
  219. // We only support a single input report so we don't need to check
  220. // the ulMsgValue parameter in this case. Set the report pointer
  221. // in *pvMsgData and return the length of the report in bytes.
  222. //
  223. *(unsigned char **)pvMsgData = psInst->pucReport;
  224. return (8);
  225. }
  226. //
  227. // The device class driver has completed sending a report to the
  228. // host in response to a Get_Report request.
  229. //
  230. case USBD_HID_EVENT_REPORT_SENT:
  231. {
  232. //
  233. // We have nothing to do here.
  234. //
  235. break;
  236. }
  237. //
  238. // This event is sent in response to a host Set_Report request. The
  239. // mouse device has no output reports so we return a NULL pointer and
  240. // zero length to cause this request to be stalled.
  241. //
  242. case USBD_HID_EVENT_GET_REPORT_BUFFER:
  243. {
  244. //
  245. // We are being asked for a report that does not exist for
  246. // this device.
  247. //
  248. *(unsigned char **)pvMsgData = (void *)0;
  249. return (0);
  250. }
  251. //
  252. // The host is asking us to set either boot or report protocol (not
  253. // that it makes any difference to this particular mouse).
  254. //
  255. case USBD_HID_EVENT_SET_PROTOCOL:
  256. {
  257. psInst->ucProtocol = ulMsgData;
  258. break;
  259. }
  260. //
  261. // The host is asking us to tell it which protocol we are currently
  262. // using, boot or request.
  263. //
  264. case USBD_HID_EVENT_GET_PROTOCOL:
  265. {
  266. return (psInst->ucProtocol);
  267. }
  268. //
  269. // Pass ERROR, SUSPEND and RESUME to the client unchanged.
  270. //
  271. case USB_EVENT_ERROR:
  272. case USB_EVENT_SUSPEND:
  273. case USB_EVENT_RESUME:
  274. {
  275. return(psDevice->pfnCallback(psDevice->pvCBData, ulEvent,
  276. ulMsgData, pvMsgData));
  277. }
  278. //
  279. // We ignore all other events.
  280. //
  281. default:
  282. {
  283. break;
  284. }
  285. }
  286. return (0);
  287. }
  288. //*****************************************************************************
  289. //
  290. // HID device class transmit channel event handler function.
  291. //
  292. // \param pvCBData is the event callback pointer provided during USBDHIDInit().
  293. // This is a pointer to our HID device structure (&g_sHIDMouseDevice).
  294. // \param ulEvent identifies the event we are being called back for.
  295. // \param ulMsgData is an event-specific value.
  296. // \param pvMsgData is an event-specific pointer.
  297. //
  298. // This function is called by the HID device class driver to inform the
  299. // application of particular asynchronous events related to report
  300. // transmissions made using the interrupt IN endpoint.
  301. //
  302. // \return Returns a value which is event-specific.
  303. //
  304. //*****************************************************************************
  305. static unsigned int
  306. HIDMouseTxHandler(void *pvCBData, unsigned int ulEvent,
  307. unsigned int ulMsgData, void *pvMsgData)
  308. {
  309. tHIDMouseInstance *psInst;
  310. tUSBDHIDMouseDevice *psDevice;
  311. //
  312. // Make sure we didn't get a NULL pointer.
  313. //
  314. ASSERT(pvCBData);
  315. //
  316. // Get a pointer to our instance data
  317. //
  318. psDevice = (tUSBDHIDMouseDevice *)pvCBData;
  319. psInst = psDevice->psPrivateHIDMouseData;
  320. //
  321. // Which event were we sent?
  322. //
  323. switch (ulEvent)
  324. {
  325. //
  326. // A report transmitted via the interrupt IN endpoint was acknowledged
  327. // by the host.
  328. //
  329. case USB_EVENT_TX_COMPLETE:
  330. {
  331. //
  332. // Our last transmission is complete.
  333. //
  334. psInst->eMouseState = HID_MOUSE_STATE_IDLE;
  335. //
  336. // Pass the event on to the client.
  337. //
  338. psDevice->pfnCallback(psDevice->pvCBData, USB_EVENT_TX_COMPLETE,
  339. ulMsgData, (void *)0);
  340. break;
  341. }
  342. //
  343. // We ignore all other events related to transmission of reports via
  344. // the interrupt IN endpoint.
  345. //
  346. default:
  347. {
  348. break;
  349. }
  350. }
  351. return (0);
  352. }
  353. //*****************************************************************************
  354. //
  355. //! Initializes HID mouse device operation for a given USB controller.
  356. //!
  357. //! \param ulIndex is the index of the USB controller which is to be
  358. //! initialized for HID mouse device operation.
  359. //! \param psDevice points to a structure containing parameters customizing
  360. //! the operation of the HID mouse device.
  361. //!
  362. //! An application wishing to offer a USB HID mouse interface to a USB host
  363. //! must call this function to initialize the USB controller and attach the
  364. //! mouse device to the USB bus. This function performs all required USB
  365. //! initialization.
  366. //!
  367. //! On successful completion, this function will return the \e psDevice pointer
  368. //! passed to it. This must be passed on all future calls to the HID mouse
  369. //! device driver.
  370. //!
  371. //! When a host connects and configures the device, the application callback
  372. //! will receive \b USB_EVENT_CONNECTED after which calls can be made to
  373. //! USBDHIDMouseStateChange() to report pointer movement and button presses
  374. //! to the host.
  375. //!
  376. //! \note The application must not make any calls to the lower level USB device
  377. //! interfaces if interacting with USB via the USB HID mouse device API.
  378. //! Doing so will cause unpredictable (though almost certainly unpleasant)
  379. //! behavior.
  380. //!
  381. //! \return Returns NULL on failure or the psDevice pointer on success.
  382. //
  383. //*****************************************************************************
  384. void *
  385. USBDHIDMouseInit(unsigned int ulIndex, const tUSBDHIDMouseDevice *psDevice)
  386. {
  387. void *pvRetcode;
  388. tUSBDHIDDevice *psHIDDevice;
  389. //
  390. // Check parameter validity.
  391. //
  392. ASSERT(psDevice);
  393. ASSERT(psDevice->ppStringDescriptors);
  394. ASSERT(psDevice->psPrivateHIDMouseData);
  395. ASSERT(psDevice->pfnCallback);
  396. //
  397. // Get a pointer to the HID device data.
  398. //
  399. psHIDDevice = &psDevice->psPrivateHIDMouseData->sHIDDevice;
  400. //
  401. // Call the common initialization routine.
  402. //
  403. pvRetcode = USBDHIDMouseCompositeInit(ulIndex, psDevice);
  404. //
  405. // If we initialized the HID layer successfully, pass our device pointer
  406. // back as the return code, otherwise return NULL to indicate an error.
  407. //
  408. if(pvRetcode)
  409. {
  410. //
  411. // Initialize the lower layer HID driver and pass it the various
  412. // structures and descriptors necessary to declare that we are a
  413. // keyboard.
  414. //
  415. pvRetcode = USBDHIDInit(ulIndex, psHIDDevice);
  416. return((void *)psDevice);
  417. }
  418. else
  419. {
  420. return((void *)0);
  421. }
  422. }
  423. //*****************************************************************************
  424. //
  425. //! Initializes HID mouse device operation for a given USB controller.
  426. //!
  427. //! \param ulIndex is the index of the USB controller which is to be
  428. //! initialized for HID mouse device operation.
  429. //! \param psDevice points to a structure containing parameters customizing
  430. //! the operation of the HID mouse device.
  431. //!
  432. //! An application wishing to make use of a composite
  433. //! USB bulk communication channel needs to call this function.
  434. //! This function is used for initializing an instance related information of the
  435. //! HID mouse device.
  436. //!
  437. //! \return Returns zero on failure or a non-zero instance value that should be
  438. //! used with the remaining USB HID Mouse APIs.
  439. //
  440. //*****************************************************************************
  441. void *
  442. USBDHIDMouseCompositeInit(unsigned int ulIndex,
  443. const tUSBDHIDMouseDevice *psDevice)
  444. {
  445. tHIDMouseInstance *psInst;
  446. tUSBDHIDDevice *psHIDDevice;
  447. //
  448. // Check parameter validity.
  449. //
  450. ASSERT(psDevice);
  451. ASSERT(psDevice->ppStringDescriptors);
  452. ASSERT(psDevice->psPrivateHIDMouseData);
  453. ASSERT(psDevice->pfnCallback);
  454. //
  455. // Get a pointer to our instance data
  456. //
  457. psInst = psDevice->psPrivateHIDMouseData;
  458. //
  459. // Get a pointer to the HID device data.
  460. //
  461. psHIDDevice = &psDevice->psPrivateHIDMouseData->sHIDDevice;
  462. //
  463. // Initialize the various fields in our instance structure.
  464. //
  465. psInst->ucUSBConfigured = 0;
  466. psInst->ucProtocol = USB_HID_PROTOCOL_REPORT;
  467. psInst->sReportIdle.ucDuration4mS = 0;
  468. psInst->sReportIdle.ucReportID = 0;
  469. psInst->sReportIdle.ulTimeSinceReportmS = 0;
  470. psInst->sReportIdle.usTimeTillNextmS = 0;
  471. psInst->eMouseState = HID_MOUSE_STATE_UNCONFIGURED;
  472. //
  473. // Initialize the HID device class instance structure based on input from
  474. // the caller.
  475. //
  476. psHIDDevice->usPID = psDevice->usPID;
  477. psHIDDevice->usVID = psDevice->usVID;
  478. psHIDDevice->usMaxPowermA = psDevice->usMaxPowermA;
  479. psHIDDevice->ucPwrAttributes = psDevice->ucPwrAttributes;
  480. psHIDDevice->ucSubclass = USB_HID_SCLASS_BOOT;
  481. psHIDDevice->ucProtocol = USB_HID_PROTOCOL_MOUSE;
  482. psHIDDevice->ucNumInputReports = 1;
  483. psHIDDevice->psReportIdle = &psInst->sReportIdle;
  484. psHIDDevice->pfnRxCallback = HIDMouseRxHandler;
  485. psHIDDevice->pvRxCBData = (void *)psDevice;
  486. psHIDDevice->pfnTxCallback = HIDMouseTxHandler;
  487. psHIDDevice->pvTxCBData = (void *)psDevice;
  488. psHIDDevice->bUseOutEndpoint = false;
  489. psHIDDevice->psHIDDescriptor = &g_sMouseHIDDescriptor;
  490. psHIDDevice->ppClassDescriptors= g_pMouseClassDescriptors;
  491. psHIDDevice->ppStringDescriptors = psDevice->ppStringDescriptors;
  492. psHIDDevice->ulNumStringDescriptors = psDevice->ulNumStringDescriptors;
  493. psHIDDevice->psPrivateHIDData = &psInst->sHIDInstance;
  494. //
  495. // Initialize the lower layer HID driver and pass it the various structures
  496. // and descriptors necessary to declare that we are a keyboard.
  497. //
  498. return(USBDHIDCompositeInit(ulIndex, psHIDDevice));
  499. }
  500. //*****************************************************************************
  501. //
  502. //! Shuts down the HID mouse device.
  503. //!
  504. //! \param pvInstance is the pointer to the device instance structure.
  505. //!
  506. //! This function terminates HID mouse operation for the instance supplied
  507. //! and removes the device from the USB bus. Following this call, the \e
  508. //! pvInstance instance may not me used in any other call to the HID mouse
  509. //! device other than USBDHIDMouseInit().
  510. //!
  511. //! \return None.
  512. //
  513. //*****************************************************************************
  514. void
  515. USBDHIDMouseTerm(void *pvInstance)
  516. {
  517. tUSBDHIDMouseDevice *psDevice;
  518. tUSBDHIDDevice *psHIDDevice;
  519. ASSERT(pvInstance);
  520. //
  521. // Get a pointer to the device.
  522. //
  523. psDevice = (tUSBDHIDMouseDevice *)pvInstance;
  524. //
  525. // Get a pointer to the HID device data.
  526. //
  527. psHIDDevice = &psDevice->psPrivateHIDMouseData->sHIDDevice;
  528. //
  529. // Mark our device as no longer configured.
  530. //
  531. psDevice->psPrivateHIDMouseData->ucUSBConfigured = 0;
  532. //
  533. // Terminate the low level HID driver.
  534. //
  535. USBDHIDTerm(psHIDDevice);
  536. }
  537. //*****************************************************************************
  538. //
  539. //! Sets the client-specific pointer parameter for the mouse callback.
  540. //!
  541. //! \param pvInstance is the pointer to the mouse device instance structure.
  542. //! \param pvCBData is the pointer that client wishes to be provided on each
  543. //! event sent to the mouse callback function.
  544. //!
  545. //! The client uses this function to change the callback pointer passed in
  546. //! the first parameter on all callbacks to the \e pfnCallback function
  547. //! passed on USBDHIDMouseInit().
  548. //!
  549. //! If a client wants to make runtime changes in the callback pointer, it must
  550. //! ensure that the pvInstance structure passed to USBDHIDMouseInit() resides
  551. //! in RAM. If this structure is in flash, callback data changes will not be
  552. //! possible.
  553. //!
  554. //! \return Returns the previous callback pointer that was set for this
  555. //! instance.
  556. //
  557. //*****************************************************************************
  558. void *
  559. USBDHIDMouseSetCBData(void *pvInstance, void *pvCBData)
  560. {
  561. void *pvOldCBData;
  562. tUSBDHIDMouseDevice *psMouse;
  563. //
  564. // Check for a NULL pointer in the device parameter.
  565. //
  566. ASSERT(pvInstance);
  567. //
  568. // Get a pointer to our mouse device.
  569. //
  570. psMouse = (tUSBDHIDMouseDevice *)pvInstance;
  571. //
  572. // Save the old callback pointer and replace it with the new value.
  573. //
  574. pvOldCBData = psMouse->pvCBData;
  575. psMouse->pvCBData = pvCBData;
  576. //
  577. // Pass the old callback pointer back to the caller.
  578. //
  579. return(pvOldCBData);
  580. }
  581. //*****************************************************************************
  582. //
  583. //! Reports a mouse state change, pointer movement or button press, to the USB
  584. //! host.
  585. //!
  586. //! \param pvInstance is the pointer to the mouse device instance structure.
  587. //! \param cDeltaX is the relative horizontal pointer movement that the
  588. //! application wishes to report. Valid values are in the range [-127, 127]
  589. //! with positive values indicating movement to the right.
  590. //! \param cDeltaY is the relative vertical pointer movement that the
  591. //! application wishes to report. Valid values are in the range [-127, 127]
  592. //! with positive values indicating downward movement.
  593. //! \param ucButtons is a bit mask indicating which (if any) of the three
  594. //! mouse buttons is pressed. Valid values are logical OR combinations of
  595. //! \e MOUSE_REPORT_BUTTON_1, \e MOUSE_REPORT_BUTTON_2 and \e
  596. //! MOUSE_REPORT_BUTTON_3.
  597. //!
  598. //! This function is called to report changes in the mouse state to the USB
  599. //! host. These changes can be movement of the pointer, reported relative to
  600. //! its previous position, or changes in the states of up to 3 buttons that
  601. //! the mouse may support. The return code indicates whether or not the
  602. //! mouse report could be sent to the host. In cases where a previous
  603. //! report is still being transmitted, \b MOUSE_ERR_TX_ERROR will be returned
  604. //! and the state change will be ignored.
  605. //!
  606. //! \return Returns \b MOUSE_SUCCESS on success, \b MOUSE_ERR_TX_ERROR if an
  607. //! error occurred while attempting to schedule transmission of the mouse
  608. //! report to the host (typically due to a previous report which has not yet
  609. //! completed transmission or due to disconnection of the host) or \b
  610. //! MOUSE_ERR_NOT_CONFIGURED if called before a host has connected to and
  611. //! configured the device.
  612. //
  613. //*****************************************************************************
  614. unsigned int
  615. USBDHIDMouseStateChange(void *pvInstance, char cDeltaX, char cDeltaY,
  616. unsigned char ucButtons)
  617. {
  618. unsigned int ulRetcode;
  619. unsigned int ulCount;
  620. tHIDMouseInstance *psInst;
  621. tUSBDHIDMouseDevice *psDevice;
  622. tUSBDHIDDevice *psHIDDevice;
  623. //
  624. // Get a pointer to the device.
  625. //
  626. psDevice = (tUSBDHIDMouseDevice *)pvInstance;
  627. //
  628. // Get a pointer to the HID device data.
  629. //
  630. psHIDDevice = &psDevice->psPrivateHIDMouseData->sHIDDevice;
  631. //
  632. // Get a pointer to our instance data
  633. //
  634. psInst = psDevice->psPrivateHIDMouseData;
  635. //
  636. // Update the global mouse report with the information passed.
  637. //
  638. psInst->pucReport[HID_REPORT_BUTTONS] = ucButtons;
  639. psInst->pucReport[HID_REPORT_X] = (unsigned char)cDeltaX;
  640. psInst->pucReport[HID_REPORT_Y] = (unsigned char)cDeltaY;
  641. //
  642. // If we are not configured, return an error here before trying to send
  643. // anything.
  644. //
  645. if(!psInst->ucUSBConfigured)
  646. {
  647. return(MOUSE_ERR_NOT_CONFIGURED);
  648. }
  649. //
  650. // Only send a report if the transmitter is currently free.
  651. //
  652. if(USBDHIDTxPacketAvailable((void *)psHIDDevice))
  653. {
  654. //
  655. // Send the report to the host.
  656. //
  657. psInst->eMouseState = HID_MOUSE_STATE_SEND;
  658. ulCount = USBDHIDReportWrite((void *)psHIDDevice,
  659. psInst->pucReport, MOUSE_REPORT_SIZE,
  660. true);
  661. //
  662. // Did we schedule a packet for transmission correctly?
  663. //
  664. if(!ulCount)
  665. {
  666. //
  667. // No - report the error to the caller.
  668. //
  669. ulRetcode = MOUSE_ERR_TX_ERROR;
  670. }
  671. else
  672. {
  673. ulRetcode = MOUSE_SUCCESS;
  674. }
  675. }
  676. else
  677. {
  678. ulRetcode = MOUSE_ERR_TX_ERROR;
  679. }
  680. //
  681. // Return the relevant error code to the caller.
  682. //
  683. return(ulRetcode);
  684. }
  685. //*****************************************************************************
  686. //
  687. //! Reports the device power status (bus- or self-powered) to the USB library.
  688. //!
  689. //! \param pvInstance is the pointer to the mouse device instance structure.
  690. //! \param ucPower indicates the current power status, either \b
  691. //! USB_STATUS_SELF_PWR or \b USB_STATUS_BUS_PWR.
  692. //!
  693. //! Applications which support switching between bus- or self-powered
  694. //! operation should call this function whenever the power source changes
  695. //! to indicate the current power status to the USB library. This information
  696. //! is required by the USB library to allow correct responses to be provided
  697. //! when the host requests status from the device.
  698. //!
  699. //! \return None.
  700. //
  701. //*****************************************************************************
  702. void
  703. USBDHIDMousePowerStatusSet(void *pvInstance, unsigned char ucPower)
  704. {
  705. tUSBDHIDMouseDevice *psDevice;
  706. tUSBDHIDDevice *psHIDDevice;
  707. ASSERT(pvInstance);
  708. //
  709. // Get the keyboard device pointer.
  710. //
  711. psDevice = (tUSBDHIDMouseDevice *)pvInstance;
  712. //
  713. // Get a pointer to the HID device data.
  714. //
  715. psHIDDevice = &psDevice->psPrivateHIDMouseData->sHIDDevice;
  716. //
  717. // Pass the request through to the lower layer.
  718. //
  719. USBDHIDPowerStatusSet((void *)psHIDDevice, ucPower);
  720. }
  721. //*****************************************************************************
  722. //
  723. //! Requests a remote wake up to resume communication when in suspended state.
  724. //!
  725. //! \param pvInstance is the pointer to the mouse device instance structure.
  726. //!
  727. //! When the bus is suspended, an application which supports remote wake up
  728. //! (advertised to the host via the configuration descriptor) may call this
  729. //! function to initiate remote wake up signaling to the host. If the remote
  730. //! wake up feature has not been disabled by the host, this will cause the bus
  731. //! to resume operation within 20mS. If the host has disabled remote wake up,
  732. //! \b false will be returned to indicate that the wake up request was not
  733. //! successful.
  734. //!
  735. //! \return Returns \b true if the remote wake up is not disabled and the
  736. //! signaling was started or \b false if remote wake up is disabled or if
  737. //! signaling is currently ongoing following a previous call to this function.
  738. //
  739. //*****************************************************************************
  740. tBoolean
  741. USBDHIDMouseRemoteWakeupRequest(void *pvInstance)
  742. {
  743. tUSBDHIDMouseDevice *psDevice;
  744. tUSBDHIDDevice *psHIDDevice;
  745. ASSERT(pvInstance);
  746. //
  747. // Get the keyboard device pointer.
  748. //
  749. psDevice = (tUSBDHIDMouseDevice *)pvInstance;
  750. //
  751. // Get a pointer to the HID device data.
  752. //
  753. psHIDDevice = &psDevice->psPrivateHIDMouseData->sHIDDevice;
  754. //
  755. // Pass the request through to the lower layer.
  756. //
  757. return(USBDHIDRemoteWakeupRequest((void *)&psHIDDevice));
  758. }
  759. //*****************************************************************************
  760. //
  761. // Close the Doxygen group.
  762. //! @}
  763. //
  764. //*****************************************************************************