Add libusb and libuvc
[rtmpclient.git] / app / src / main / jni / libuvc-0.0.6 / src / ctrl.c
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 *  Copyright (C) 2010-2012 Ken Tossell
5 *  All rights reserved.
6 *
7 *  Redistribution and use in source and binary forms, with or without
8 *  modification, are permitted provided that the following conditions
9 *  are met:
10 *
11 *   * Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17 *   * Neither the name of the author nor other contributors may be
18 *     used to endorse or promote products derived from this software
19 *     without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 *  POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 /**
35  * @defgroup ctrl Video capture and processing controls
36  * @brief Functions for manipulating device settings and stream parameters
37  *
38  * The `uvc_get_*` and `uvc_set_*` functions are used to read and write the settings associated
39  * with the device's input, processing and output units.
40  */
41
42 #include "libuvc/libuvc.h"
43 #include "libuvc/libuvc_internal.h"
44
45 static const int REQ_TYPE_SET = 0x21;
46 static const int REQ_TYPE_GET = 0xa1;
47
48 /***** GENERIC CONTROLS *****/
49 /**
50  * @brief Get the length of a control on a terminal or unit.
51  * 
52  * @param devh UVC device handle
53  * @param unit Unit or Terminal ID; obtain this from the uvc_extension_unit_t describing the extension unit
54  * @param ctrl Vendor-specific control number to query
55  * @return On success, the length of the control as reported by the device. Otherwise,
56  *   a uvc_error_t error describing the error encountered.
57  * @ingroup ctrl
58  */
59 int uvc_get_ctrl_len(uvc_device_handle_t *devh, uint8_t unit, uint8_t ctrl) {
60   unsigned char buf[2];
61
62   int ret = libusb_control_transfer(
63     devh->usb_devh,
64     REQ_TYPE_GET, UVC_GET_LEN,
65     ctrl << 8,
66     unit << 8 | devh->info->ctrl_if.bInterfaceNumber,           // XXX saki
67     buf,
68     2,
69     0 /* timeout */);
70
71   if (ret < 0)
72     return ret;
73   else
74     return (unsigned short)SW_TO_SHORT(buf);
75 }
76
77 /**
78  * @brief Perform a GET_* request from an extension unit.
79  * 
80  * @param devh UVC device handle
81  * @param unit Unit ID; obtain this from the uvc_extension_unit_t describing the extension unit
82  * @param ctrl Control number to query
83  * @param data Data buffer to be filled by the device
84  * @param len Size of data buffer
85  * @param req_code GET_* request to execute
86  * @return On success, the number of bytes actually transferred. Otherwise,
87  *   a uvc_error_t error describing the error encountered.
88  * @ingroup ctrl
89  */
90 int uvc_get_ctrl(uvc_device_handle_t *devh, uint8_t unit, uint8_t ctrl, void *data, int len, enum uvc_req_code req_code) {
91   return libusb_control_transfer(
92     devh->usb_devh,
93     REQ_TYPE_GET, req_code,
94     ctrl << 8,
95     unit << 8 | devh->info->ctrl_if.bInterfaceNumber,           // XXX saki
96     data,
97     len,
98     0 /* timeout */);
99 }
100
101 /**
102  * @brief Perform a SET_CUR request to a terminal or unit.
103  * 
104  * @param devh UVC device handle
105  * @param unit Unit or Terminal ID
106  * @param ctrl Control number to set
107  * @param data Data buffer to be sent to the device
108  * @param len Size of data buffer
109  * @return On success, the number of bytes actually transferred. Otherwise,
110  *   a uvc_error_t error describing the error encountered.
111  * @ingroup ctrl
112  */
113 int uvc_set_ctrl(uvc_device_handle_t *devh, uint8_t unit, uint8_t ctrl, void *data, int len) {
114   return libusb_control_transfer(
115     devh->usb_devh,
116     REQ_TYPE_SET, UVC_SET_CUR,
117     ctrl << 8,
118     unit << 8 | devh->info->ctrl_if.bInterfaceNumber,           // XXX saki
119     data,
120     len,
121     0 /* timeout */);
122 }
123
124 /***** INTERFACE CONTROLS *****/
125 uvc_error_t uvc_get_power_mode(uvc_device_handle_t *devh, enum uvc_device_power_mode *mode, enum uvc_req_code req_code) {
126   uint8_t mode_char;
127   uvc_error_t ret;
128
129   ret = libusb_control_transfer(
130     devh->usb_devh,
131     REQ_TYPE_GET, req_code,
132     UVC_VC_VIDEO_POWER_MODE_CONTROL << 8,
133     devh->info->ctrl_if.bInterfaceNumber,       // XXX saki
134     &mode_char,
135     sizeof(mode_char),
136     0);
137
138   if (ret == 1) {
139     *mode = mode_char;
140     return UVC_SUCCESS;
141   } else {
142     return ret;
143   }
144 }
145
146 uvc_error_t uvc_set_power_mode(uvc_device_handle_t *devh, enum uvc_device_power_mode mode) {
147   uint8_t mode_char = mode;
148   uvc_error_t ret;
149
150   ret = libusb_control_transfer(
151     devh->usb_devh,
152     REQ_TYPE_SET, UVC_SET_CUR,
153     UVC_VC_VIDEO_POWER_MODE_CONTROL << 8,
154     devh->info->ctrl_if.bInterfaceNumber,       // XXX saki
155     &mode_char,
156     sizeof(mode_char),
157     0);
158
159   if (ret == 1)
160     return UVC_SUCCESS;
161   else
162     return ret;
163 }
164
165 /** @todo Request Error Code Control (UVC 1.5, 4.2.1.2) */