14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
20 #include <linux/videodev2.h>
22 #include <linux/wait.h>
28 #define UVC_CTRL_DATA_CURRENT 0
29 #define UVC_CTRL_DATA_BACKUP 1
30 #define UVC_CTRL_DATA_MIN 2
31 #define UVC_CTRL_DATA_MAX 3
32 #define UVC_CTRL_DATA_RES 4
33 #define UVC_CTRL_DATA_DEF 5
34 #define UVC_CTRL_DATA_LAST 6
356 static struct uvc_menu_info power_line_frequency_controls[] = {
364 { 1,
"Manual Mode" },
365 { 4,
"Shutter Priority Mode" },
366 { 8,
"Aperture Priority Mode" },
376 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
390 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
391 data[2] =
min((
int)
abs(value), 0xff);
397 .name =
"Brightness",
429 .name =
"Saturation",
459 .name =
"Backlight Compensation",
479 .name =
"Power Line Frequency",
486 .menu_info = power_line_frequency_controls,
487 .menu_count =
ARRAY_SIZE(power_line_frequency_controls),
502 .name =
"Exposure, Auto",
509 .menu_info = exposure_auto_controls,
510 .menu_count =
ARRAY_SIZE(exposure_auto_controls),
515 .name =
"Exposure, Auto Priority",
525 .name =
"Exposure (Absolute)",
537 .name =
"White Balance Temperature, Auto",
548 .name =
"White Balance Temperature",
560 .name =
"White Balance Component, Auto",
572 .name =
"White Balance Blue Component",
584 .name =
"White Balance Red Component",
596 .name =
"Focus (absolute)",
608 .name =
"Focus, Auto",
619 .name =
"Iris, Absolute",
629 .name =
"Iris, Relative",
639 .name =
"Zoom, Absolute",
649 .name =
"Zoom, Continuous",
656 .get = uvc_ctrl_get_zoom,
657 .set = uvc_ctrl_set_zoom,
661 .name =
"Pan (Absolute)",
671 .name =
"Tilt (Absolute)",
700 static inline int uvc_test_bit(
const __u8 *data,
int bit)
702 return (data[bit >> 3] >> (bit & 7)) & 1;
705 static inline void uvc_clear_bit(
__u8 *data,
int bit)
707 data[bit >> 3] &= ~(1 << (bit & 7));
727 for (; bits > 0; data++) {
729 value |= offset > 0 ? (byte >>
offset) : (byte << (-offset));
730 bits -= 8 - (offset > 0 ? offset : 0);
732 mask = (1 <<
bits) - 1;
737 value |= -(value & (1 << (mapping->
size - 1)));
748 int bits = mapping->
size;
749 int offset = mapping->
offset;
763 for (; bits > 0; data++) {
765 *data = (*data & ~mask) | ((value << offset) &
mask);
766 value >>= offset ? offset : 8;
778 static const __u8 uvc_media_transport_input_guid[16] =
786 return memcmp(uvc_camera_guid, guid, 16) == 0;
789 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
792 return memcmp(uvc_processing_guid, guid, 16) == 0;
807 static void __uvc_find_control(
struct uvc_entity *entity,
__u32 v4l2_id,
824 if ((map->
id == v4l2_id) && !next) {
830 if ((*mapping ==
NULL || (*mapping)->id > map->
id) &&
831 (map->
id > v4l2_id) && next) {
853 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
858 if (ctrl ==
NULL && !next)
872 chain->
dev->intfnum, ctrl->
info.selector,
881 chain->
dev->intfnum, ctrl->
info.selector,
889 chain->
dev->intfnum, ctrl->
info.selector,
897 chain->
dev->intfnum, ctrl->
info.selector,
910 "UVC non compliance - GET_RES failed on "
911 "an XU control. Enabling workaround.\n");
934 chain->
dev->intfnum, ctrl->
info.selector,
948 for (i = 0; i < mapping->
menu_count; ++
i, ++menu) {
949 if (menu->
value == *value) {
969 memset(v4l2_ctrl, 0,
sizeof *v4l2_ctrl);
970 v4l2_ctrl->
id = mapping->
id;
973 v4l2_ctrl->
flags = 0;
982 &master_map, &master_ctrl, 0);
985 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
994 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1008 v4l2_ctrl->
step = 1;
1011 for (i = 0; i < mapping->
menu_count; ++
i, ++menu) {
1023 v4l2_ctrl->
step = 1;
1029 v4l2_ctrl->
step = 0;
1062 ctrl = uvc_find_control(chain, v4l2_ctrl->
id, &mapping);
1068 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1090 u32 id = query_menu->
id;
1093 memset(query_menu, 0,
sizeof(*query_menu));
1094 query_menu->
id =
id;
1101 ctrl = uvc_find_control(chain, query_menu->
id, &mapping);
1119 ret = uvc_ctrl_populate_cache(chain, ctrl);
1126 if (!(bitmap & menu_info->
value)) {
1151 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1155 ev->
id = v4l2_ctrl.
id;
1157 ev->
u.
ctrl.changes = changes;
1173 if (list_empty(&mapping->
ev_subs))
1176 uvc_ctrl_fill_event(handle->
chain, &ev, ctrl, mapping, value, changes);
1179 if (sev->
fh && (sev->
fh != &handle->
vfh ||
1186 static void uvc_ctrl_send_slave_event(
struct uvc_fh *handle,
1200 for (i = 0; i < xctrls_count; i++) {
1201 if (xctrls[i].
id == slave_id)
1205 __uvc_find_control(master->
entity, slave_id, &mapping, &ctrl, 0);
1209 if (__uvc_ctrl_get(handle->
chain, ctrl, mapping, &val) == 0)
1212 uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
1215 static void uvc_ctrl_send_events(
struct uvc_fh *handle,
1224 for (i = 0; i < xctrls_count; ++
i) {
1225 ctrl = uvc_find_control(handle->
chain, xctrls[i].
id, &mapping);
1230 uvc_ctrl_send_slave_event(handle, ctrl,
1232 xctrls, xctrls_count);
1240 for (j = 0; j < xctrls_count; j++) {
1241 if (xctrls[j].
id == mapping->
master_id) {
1248 uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
1264 ctrl = uvc_find_control(handle->
chain, sev->
id, &mapping);
1276 if (__uvc_ctrl_get(handle->
chain, ctrl, mapping, &val) == 0)
1279 uvc_ctrl_fill_event(handle->
chain, &ev, ctrl, mapping, val,
1302 .add = uvc_ctrl_add_event,
1303 .del = uvc_ctrl_del_event,
1373 if (rollback || ret < 0)
1389 unsigned int xctrls_count)
1397 ret = uvc_ctrl_commit_entity(chain->
dev, entity, rollback);
1403 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1415 ctrl = uvc_find_control(chain, xctrl->
id, &mapping);
1419 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->
value);
1433 ctrl = uvc_find_control(chain, xctrl->
id, &mapping);
1441 ret = uvc_ctrl_populate_cache(chain, ctrl);
1455 xctrl->
value = min + (xctrl->
value - min + step/2) / step * step;
1457 value = xctrl->
value;
1462 value = xctrl->
value;
1476 ret = uvc_ctrl_populate_cache(chain, ctrl);
1483 if (!(step & value))
1490 value = xctrl->
value;
1501 0, ctrl->
info.size);
1505 ctrl->
info.selector,
1522 mapping->
set(mapping, value,
1534 static void uvc_ctrl_fixup_xu_info(
struct uvc_device *dev,
1537 struct uvc_ctrl_fixup {
1544 static const struct uvc_ctrl_fixup fixups[] = {
1545 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1549 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1553 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1565 if (fixups[i].entity == ctrl->
entity->id &&
1566 fixups[i].selector == info->
selector) {
1567 info->
flags = fixups[
i].flags;
1576 static int uvc_ctrl_fill_xu_info(
struct uvc_device *dev,
1596 "GET_LEN failed on control %pUl/%u (%d).\n",
1608 "GET_INFO failed on control %pUl/%u (%d).\n",
1622 uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1625 "flags { get %u set %u auto %u }.\n",
1628 (info->
flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1639 static int uvc_ctrl_init_xu_ctrl(
struct uvc_device *dev,
1648 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1652 ret = uvc_ctrl_add_info(dev, ctrl, &info);
1655 "%pUl/%u on device %s entity %u\n", info.
entity,
1666 unsigned int i, found = 0;
1675 entity->
id == xqry->
unit)
1679 if (entity->
id != xqry->
unit) {
1703 ret = uvc_ctrl_init_xu_ctrl(chain->
dev, ctrl);
1711 size = ctrl->
info.size;
1713 switch (xqry->
query) {
1743 if (size != xqry->
size) {
1748 if (reqflags && !(ctrl->
info.flags & reqflags)) {
1810 ctrl->
info.entity, ctrl->
info.index,
1811 ctrl->
info.selector);
1815 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1836 INIT_LIST_HEAD(&ctrl->
info.mappings);
1849 "entity %u\n", ctrl->
info.entity, ctrl->
info.selector,
1861 static int __uvc_ctrl_add_mapping(
struct uvc_device *dev,
1875 INIT_LIST_HEAD(&map->
ev_subs);
1885 map->
get = uvc_get_le_value;
1887 map->
set = uvc_set_le_value;
1891 "Adding mapping '%s' to control %pUl/%u.\n",
1909 "id 0x%08x is invalid.\n", mapping->
name,
1919 !uvc_entity_match_guid(entity, mapping->
entity))
1940 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
1947 if (mapping->
id == map->
id) {
1949 "control id 0x%08x already exists.\n",
1950 mapping->
name, mapping->
id);
1960 "mappings count (%u) exceeded.\n", mapping->
name,
1966 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1980 static void uvc_ctrl_prune_entity(
struct uvc_device *dev,
1983 struct uvc_ctrl_blacklist {
1988 static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1989 { { USB_DEVICE(0x13d3, 0x509b) }, 9 },
1990 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 },
1991 { { USB_DEVICE(0x5986, 0x0241) }, 2 },
1993 static const struct uvc_ctrl_blacklist camera_blacklist[] = {
1994 { { USB_DEVICE(0x06f8, 0x3005) }, 9 },
1997 const struct uvc_ctrl_blacklist *blacklist;
2005 blacklist = processing_blacklist;
2012 blacklist = camera_blacklist;
2014 controls = entity->
camera.bmControls;
2015 size = entity->
camera.bControlSize;
2022 for (i = 0; i <
count; ++
i) {
2026 if (blacklist[i].
index >= 8 * size ||
2027 !uvc_test_bit(controls, blacklist[i].
index))
2031 "removing it.\n", entity->
id, blacklist[i].index);
2033 uvc_clear_bit(controls, blacklist[i].index);
2057 for (; info < iend; ++
info) {
2058 if (uvc_entity_match_guid(ctrl->
entity, info->
entity) &&
2060 uvc_ctrl_add_info(dev, ctrl, info);
2068 for (; mapping < mend; ++
mapping) {
2069 if (uvc_entity_match_guid(ctrl->
entity, mapping->
entity) &&
2071 __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2090 bmControls = entity->
extension.bmControls;
2091 bControlSize = entity->
extension.bControlSize;
2094 bControlSize = entity->
processing.bControlSize;
2096 bmControls = entity->
camera.bmControls;
2097 bControlSize = entity->
camera.bControlSize;
2101 uvc_ctrl_prune_entity(dev, entity);
2104 ncontrols =
memweight(bmControls, bControlSize);
2108 entity->
controls = kcalloc(ncontrols,
sizeof(*ctrl),
2116 for (i = 0; i < bControlSize * 8; ++
i) {
2117 if (uvc_test_bit(bmControls, i) == 0)
2123 uvc_ctrl_init_ctrl(dev, ctrl);
2134 static void uvc_ctrl_cleanup_mappings(
struct uvc_device *dev,
2159 uvc_ctrl_cleanup_mappings(dev, ctrl);