hidreq.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Lubos Slovak
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00036 #include <stdint.h>
00037 #include <errno.h>
00038 #include <str_error.h>
00039 
00040 #include <usb/hid/hid.h>
00041 #include <usb/debug.h>
00042 #include <usb/dev/request.h>
00043 #include <usb/dev/pipes.h>
00044 
00045 #include <usb/hid/request.h>
00046 
00047 /*----------------------------------------------------------------------------*/
00060 int usbhid_req_set_report(usb_pipe_t *ctrl_pipe, int iface_no,
00061     usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size)
00062 {
00063         if (ctrl_pipe == NULL) {
00064                 usb_log_warning("usbhid_req_set_report(): no pipe given.\n");
00065                 return EINVAL;
00066         }
00067         
00068         if (iface_no < 0) {
00069                 usb_log_warning("usbhid_req_set_report(): no interface given."
00070                     "\n");
00071                 return EINVAL;
00072         }
00073         
00074         /*
00075          * No need for checking other parameters, as they are checked in
00076          * the called function (usb_control_request_set()).
00077          */
00078         
00079         int rc;
00080         
00081         uint16_t value = 0;
00082         value |= (type << 8);
00083 
00084         usb_log_debug("Sending Set Report request to the device.\n");
00085         
00086         rc = usb_control_request_set(ctrl_pipe, 
00087             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, 
00088             USB_HIDREQ_SET_REPORT, value, iface_no, buffer, buf_size);
00089 
00090         if (rc != EOK) {
00091                 usb_log_warning("Error sending Set Report request to the "
00092                     "device: %s.\n", str_error(rc));
00093                 return rc;
00094         }
00095         
00096         return EOK;
00097 }
00098 
00099 /*----------------------------------------------------------------------------*/
00110 int usbhid_req_set_protocol(usb_pipe_t *ctrl_pipe, int iface_no,
00111     usb_hid_protocol_t protocol)
00112 {
00113         if (ctrl_pipe == NULL) {
00114                 usb_log_warning("usbhid_req_set_report(): no pipe given.\n");
00115                 return EINVAL;
00116         }
00117         
00118         if (iface_no < 0) {
00119                 usb_log_warning("usbhid_req_set_report(): no interface given."
00120                     "\n");
00121                 return EINVAL;
00122         }
00123         
00124         /*
00125          * No need for checking other parameters, as they are checked in
00126          * the called function (usb_control_request_set()).
00127          */
00128         
00129         int rc;
00130 
00131         usb_log_debug("Sending Set Protocol request to the device ("
00132             "protocol: %d, iface: %d).\n", protocol, iface_no);
00133         
00134         rc = usb_control_request_set(ctrl_pipe, 
00135             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, 
00136             USB_HIDREQ_SET_PROTOCOL, protocol, iface_no, NULL, 0);
00137 
00138         if (rc != EOK) {
00139                 usb_log_warning("Error sending Set Protocol request to the "
00140                     "device: %s.\n", str_error(rc));
00141                 return rc;
00142         }
00143         
00144         return EOK;
00145 }
00146 
00147 /*----------------------------------------------------------------------------*/
00159 int usbhid_req_set_idle(usb_pipe_t *ctrl_pipe, int iface_no, uint8_t duration)
00160 {
00161         if (ctrl_pipe == NULL) {
00162                 usb_log_warning("usbhid_req_set_report(): no pipe given.\n");
00163                 return EINVAL;
00164         }
00165         
00166         if (iface_no < 0) {
00167                 usb_log_warning("usbhid_req_set_report(): no interface given."
00168                     "\n");
00169                 return EINVAL;
00170         }
00171         
00172         /*
00173          * No need for checking other parameters, as they are checked in
00174          * the called function (usb_control_request_set()).
00175          */
00176         
00177         int rc;
00178 
00179         usb_log_debug("Sending Set Idle request to the device ("
00180             "duration: %u, iface: %d).\n", duration, iface_no);
00181         
00182         uint16_t value = duration << 8;
00183         
00184         rc = usb_control_request_set(ctrl_pipe, 
00185             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, 
00186             USB_HIDREQ_SET_IDLE, value, iface_no, NULL, 0);
00187 
00188         if (rc != EOK) {
00189                 usb_log_warning("Error sending Set Idle request to the device: "
00190                     "%s.\n", str_error(rc));
00191                 return rc;
00192         }
00193         
00194         return EOK;
00195 }
00196 
00197 /*----------------------------------------------------------------------------*/
00212 int usbhid_req_get_report(usb_pipe_t *ctrl_pipe, int iface_no, 
00213     usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size, 
00214     size_t *actual_size)
00215 {
00216         if (ctrl_pipe == NULL) {
00217                 usb_log_warning("usbhid_req_set_report(): no pipe given.\n");
00218                 return EINVAL;
00219         }
00220         
00221         if (iface_no < 0) {
00222                 usb_log_warning("usbhid_req_set_report(): no interface given."
00223                     "\n");
00224                 return EINVAL;
00225         }
00226         
00227         /*
00228          * No need for checking other parameters, as they are checked in
00229          * the called function (usb_control_request_set()).
00230          */
00231         
00232         int rc;
00233 
00234         uint16_t value = 0;
00235         value |= (type << 8);
00236         
00237         usb_log_debug("Sending Get Report request to the device.\n");
00238         
00239         rc = usb_control_request_get(ctrl_pipe, 
00240             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, 
00241             USB_HIDREQ_GET_REPORT, value, iface_no, buffer, buf_size,
00242             actual_size);
00243 
00244         if (rc != EOK) {
00245                 usb_log_warning("Error sending Get Report request to the device: "
00246                     "%s.\n", str_error(rc));
00247                 return rc;
00248         }
00249         
00250         return EOK;
00251 }
00252 
00253 /*----------------------------------------------------------------------------*/
00264 int usbhid_req_get_protocol(usb_pipe_t *ctrl_pipe, int iface_no, 
00265     usb_hid_protocol_t *protocol)
00266 {
00267         if (ctrl_pipe == NULL) {
00268                 usb_log_warning("usbhid_req_set_report(): no pipe given.\n");
00269                 return EINVAL;
00270         }
00271         
00272         if (iface_no < 0) {
00273                 usb_log_warning("usbhid_req_set_report(): no interface given."
00274                     "\n");
00275                 return EINVAL;
00276         }
00277         
00278         /*
00279          * No need for checking other parameters, as they are checked in
00280          * the called function (usb_control_request_set()).
00281          */
00282         
00283         int rc; 
00284 
00285         usb_log_debug("Sending Get Protocol request to the device ("
00286             "iface: %d).\n", iface_no);
00287         
00288         uint8_t buffer[1];
00289         size_t actual_size = 0;
00290         
00291         rc = usb_control_request_get(ctrl_pipe, 
00292             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, 
00293             USB_HIDREQ_GET_PROTOCOL, 0, iface_no, buffer, 1, &actual_size);
00294 
00295         if (rc != EOK) {
00296                 usb_log_warning("Error sending Get Protocol request to the "
00297                     "device: %s.\n", str_error(rc));
00298                 return rc;
00299         }
00300         
00301         if (actual_size != 1) {
00302                 usb_log_warning("Wrong data size: %zu, expected: 1.\n",
00303                         actual_size);
00304                 return ELIMIT;
00305         }
00306         
00307         *protocol = buffer[0];
00308         
00309         return EOK;
00310 }
00311 
00312 /*----------------------------------------------------------------------------*/
00326 int usbhid_req_get_idle(usb_pipe_t *ctrl_pipe, int iface_no, uint8_t *duration)
00327 {
00328         if (ctrl_pipe == NULL) {
00329                 usb_log_warning("usbhid_req_set_report(): no pipe given.\n");
00330                 return EINVAL;
00331         }
00332         
00333         if (iface_no < 0) {
00334                 usb_log_warning("usbhid_req_set_report(): no interface given."
00335                     "\n");
00336                 return EINVAL;
00337         }
00338         
00339         /*
00340          * No need for checking other parameters, as they are checked in
00341          * the called function (usb_control_request_set()).
00342          */
00343         
00344         int rc;
00345 
00346         usb_log_debug("Sending Get Idle request to the device ("
00347             "iface: %d).\n", iface_no);
00348         
00349         uint16_t value = 0;
00350         uint8_t buffer[1];
00351         size_t actual_size = 0;
00352         
00353         rc = usb_control_request_get(ctrl_pipe, 
00354             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, 
00355             USB_HIDREQ_GET_IDLE, value, iface_no, buffer, 1, 
00356             &actual_size);
00357 
00358         if (rc != EOK) {
00359                 usb_log_warning("Error sending Get Idle request to the device: "
00360                     "%s.\n", str_error(rc));
00361                 return rc;
00362         }
00363         
00364         if (actual_size != 1) {
00365                 usb_log_warning("Wrong data size: %zu, expected: 1.\n",
00366                         actual_size);
00367                 return ELIMIT;
00368         }
00369         
00370         *duration = buffer[0];
00371         
00372         return EOK;
00373 }
00374 
00375 /*----------------------------------------------------------------------------*/
00376 

Generated on Thu Jun 2 07:45:48 2011 for HelenOS/USB by  doxygen 1.4.7