hid_preparation.c

Go to the documentation of this file.
00001 #define HID_INTERNAL
00002 
00003 #include <hid.h>
00004 #include <hid_helpers.h>
00005 #include <constants.h>
00006 
00007 #include <debug.h>
00008 #include <assert.h>
00009 
00015 static hid_return hid_prepare_hid_descriptor(HIDInterface* const hidif)
00016 {
00017   ASSERT(hid_is_opened(hidif));
00018   ASSERT(hidif->hid_parser);
00019 
00020   TRACE("initialising the HID descriptor for USB device %s...", hidif->id);
00021 
00022   /* TODO: BUFLEN seems to depend on the device, so we need to do something
00023    * about the following.
00024    */
00025   byte const BUFLEN = 9;
00026   byte buffer[BUFLEN];
00027   
00028   TRACE("retrieving HID descriptor for USB device %s...", hidif->id);
00029   int len = usb_control_msg(hidif->dev_handle,
00030       USB_ENDPOINT_IN+1,
00031       USB_REQ_GET_DESCRIPTOR,
00032       (USB_DT_HID << 8) + 0, hidif->interface,
00033       (char*)buffer, BUFLEN,
00034       USB_TIMEOUT);
00035 
00036   if (len < 0) {
00037     WARNING("failed to get HID descriptor for USB device %s:%s", hidif->id, usb_strerror());
00038     return HID_RET_NOT_HID_DEVICE;
00039   }
00040 
00041   if (len < BUFLEN) {
00042     WARNING("HID descriptor for USB device %s is too short; "
00043         "expected: %d bytes; got: %d bytes.\n", hidif->id, BUFLEN, len);
00044     return HID_RET_HID_DESC_SHORT;
00045   }
00046 
00047   /* TODO:
00048    * the constants 7 and 8 should be exported.
00049    */
00050   hidif->hid_parser->ReportDescSize = buffer[7] | (buffer[8] << 8);
00051 
00052   NOTICE("successfully initialised HID descriptor for USB device %s (%d bytes).",
00053       hidif->id, hidif->hid_parser->ReportDescSize);
00054 
00055   return HID_RET_SUCCESS;
00056 }
00057 
00058 static hid_return hid_prepare_report_descriptor(HIDInterface* const hidif)
00059 {
00060   ASSERT(hid_is_opened(hidif));
00061   ASSERT(hidif->hid_parser);
00062 
00063   TRACE("initialising the report descriptor for USB device %s...", hidif->id);
00064 
00065   if (hidif->hid_parser->ReportDescSize > REPORT_DSC_SIZE) {
00066     ERROR("report descriptor size for USB device %s exceeds maximum size: "
00067         "%d > %d.\n", hidif->id, hidif->hid_parser->ReportDescSize,
00068         REPORT_DSC_SIZE);
00069 
00070     return HID_RET_REPORT_DESC_LONG;
00071   }
00072 
00073   TRACE("retrieving report descriptor for USB device %s...", hidif->id);
00074   int len = usb_control_msg(hidif->dev_handle,
00075       USB_ENDPOINT_IN+1,
00076       USB_REQ_GET_DESCRIPTOR,
00077       (USB_DT_REPORT << 8) + 0, hidif->interface,
00078       (char*)hidif->hid_parser->ReportDesc, hidif->hid_parser->ReportDescSize,
00079       USB_TIMEOUT);
00080 
00081   if (len < 0) {
00082     WARNING("failed to get report descriptor for USB device %s...", hidif->id);
00083     NOTICE("Error from libusb: %s", usb_strerror());
00084     return HID_RET_FAIL_GET_REPORT;
00085   }
00086 
00087   if (len < hidif->hid_parser->ReportDescSize) {
00088     WARNING("HID report descriptor for USB device %s is too short; "
00089         "expected: %d bytes; got: %d bytes.\n", hidif->id,
00090         hidif->hid_parser->ReportDescSize, len);
00091     return HID_RET_REPORT_DESC_SHORT;
00092   }
00093 
00094   NOTICE("successfully initialised report descriptor for USB device %s.",
00095       hidif->id);
00096 
00097   return HID_RET_SUCCESS;
00098 }
00099 
00100 hid_return hid_prepare_interface(HIDInterface* const hidif)
00101 {
00102   if (!hid_is_opened(hidif)) {
00103     ERROR("cannot prepare unopened HIDinterface.");
00104     return HID_RET_DEVICE_NOT_OPENED;
00105   }
00106   
00107   hid_return ret = hid_init_parser(hidif);
00108   if (ret != HID_RET_SUCCESS) {
00109     hid_close(hidif);
00110     return ret;
00111   }
00112 
00113   ret = hid_prepare_hid_descriptor(hidif);
00114   if (ret != HID_RET_SUCCESS) {
00115     hid_close(hidif);
00116     return ret;
00117   }
00118 
00119   ret = hid_prepare_report_descriptor(hidif);
00120   if (ret != HID_RET_SUCCESS) {
00121     hid_close(hidif);
00122     return ret;
00123   }
00124 
00125   ret = hid_prepare_parser(hidif);
00126   if (ret != HID_RET_SUCCESS) {
00127     hid_close(hidif);
00128     return ret;
00129   }
00130 
00131   return HID_RET_SUCCESS;
00132 }
00133 
00134 /* COPYRIGHT --
00135  *
00136  * This file is part of libhid, a user-space HID access library.
00137  * libhid is (c) 2003-2005
00138  *   Martin F. Krafft <libhid@pobox.madduck.net>
00139  *   Charles Lepple <clepple@ghz.cc>
00140  *   Arnaud Quette <arnaud.quette@free.fr> && <arnaud.quette@mgeups.com>
00141  * and distributed under the terms of the GNU General Public License.
00142  * See the file ./COPYING in the source distribution for more information.
00143  *
00144  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
00145  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
00146  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00147  */

Generated on Sun Mar 30 15:28:24 2008 for libhid by  doxygen 1.5.1