1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * uvc_configfs.c 4 * 5 * Configfs support for the uvc function. 6 * 7 * Copyright (c) 2014 Samsung Electronics Co., Ltd. 8 * http://www.samsung.com 9 * 10 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com> 11 */ 12 #include "u_uvc.h" 13 #include "uvc_configfs.h" 14 15 /* ----------------------------------------------------------------------------- 16 * Global Utility Structures and Macros 17 */ 18 19 #define UVCG_STREAMING_CONTROL_SIZE 1 20 21 #define UVC_ATTR(prefix, cname, aname) \ 22 static struct configfs_attribute prefix##attr_##cname = { \ 23 .ca_name = __stringify(aname), \ 24 .ca_mode = S_IRUGO | S_IWUGO, \ 25 .ca_owner = THIS_MODULE, \ 26 .show = prefix##cname##_show, \ 27 .store = prefix##cname##_store, \ 28 } 29 30 #define UVC_ATTR_RO(prefix, cname, aname) \ 31 static struct configfs_attribute prefix##attr_##cname = { \ 32 .ca_name = __stringify(aname), \ 33 .ca_mode = S_IRUGO, \ 34 .ca_owner = THIS_MODULE, \ 35 .show = prefix##cname##_show, \ 36 } 37 38 static inline struct f_uvc_opts *to_f_uvc_opts(struct config_item *item) 39 { 40 return container_of(to_config_group(item), struct f_uvc_opts, 41 func_inst.group); 42 } 43 44 struct uvcg_config_group_type { 45 struct config_item_type type; 46 const char *name; 47 const struct uvcg_config_group_type **children; 48 int (*create_children)(struct config_group *group); 49 }; 50 51 static void uvcg_config_item_release(struct config_item *item) 52 { 53 struct config_group *group = to_config_group(item); 54 55 kfree(group); 56 } 57 58 static struct configfs_item_operations uvcg_config_item_ops = { 59 .release = uvcg_config_item_release, 60 }; 61 62 static int uvcg_config_create_group(struct config_group *parent, 63 const struct uvcg_config_group_type *type); 64 65 static int uvcg_config_create_children(struct config_group *group, 66 const struct uvcg_config_group_type *type) 67 { 68 const struct uvcg_config_group_type **child; 69 int ret; 70 71 if (type->create_children) 72 return type->create_children(group); 73 74 for (child = type->children; child && *child; ++child) { 75 ret = uvcg_config_create_group(group, *child); 76 if (ret < 0) 77 return ret; 78 } 79 80 return 0; 81 } 82 83 static int uvcg_config_create_group(struct config_group *parent, 84 const struct uvcg_config_group_type *type) 85 { 86 struct config_group *group; 87 88 group = kzalloc(sizeof(*group), GFP_KERNEL); 89 if (!group) 90 return -ENOMEM; 91 92 config_group_init_type_name(group, type->name, &type->type); 93 configfs_add_default_group(group, parent); 94 95 return uvcg_config_create_children(group, type); 96 } 97 98 static void uvcg_config_remove_children(struct config_group *group) 99 { 100 struct config_group *child, *n; 101 102 list_for_each_entry_safe(child, n, &group->default_groups, group_entry) { 103 list_del(&child->group_entry); 104 uvcg_config_remove_children(child); 105 config_item_put(&child->cg_item); 106 } 107 } 108 109 /* ----------------------------------------------------------------------------- 110 * control/header/<NAME> 111 * control/header 112 */ 113 114 DECLARE_UVC_HEADER_DESCRIPTOR(1); 115 116 struct uvcg_control_header { 117 struct config_item item; 118 struct UVC_HEADER_DESCRIPTOR(1) desc; 119 unsigned linked; 120 }; 121 122 static struct uvcg_control_header *to_uvcg_control_header(struct config_item *item) 123 { 124 return container_of(item, struct uvcg_control_header, item); 125 } 126 127 #define UVCG_CTRL_HDR_ATTR(cname, aname, conv, str2u, uxx, vnoc, limit) \ 128 static ssize_t uvcg_control_header_##cname##_show( \ 129 struct config_item *item, char *page) \ 130 { \ 131 struct uvcg_control_header *ch = to_uvcg_control_header(item); \ 132 struct f_uvc_opts *opts; \ 133 struct config_item *opts_item; \ 134 struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex;\ 135 int result; \ 136 \ 137 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 138 \ 139 opts_item = ch->item.ci_parent->ci_parent->ci_parent; \ 140 opts = to_f_uvc_opts(opts_item); \ 141 \ 142 mutex_lock(&opts->lock); \ 143 result = sprintf(page, "%d\n", conv(ch->desc.aname)); \ 144 mutex_unlock(&opts->lock); \ 145 \ 146 mutex_unlock(su_mutex); \ 147 return result; \ 148 } \ 149 \ 150 static ssize_t \ 151 uvcg_control_header_##cname##_store(struct config_item *item, \ 152 const char *page, size_t len) \ 153 { \ 154 struct uvcg_control_header *ch = to_uvcg_control_header(item); \ 155 struct f_uvc_opts *opts; \ 156 struct config_item *opts_item; \ 157 struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex;\ 158 int ret; \ 159 uxx num; \ 160 \ 161 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 162 \ 163 opts_item = ch->item.ci_parent->ci_parent->ci_parent; \ 164 opts = to_f_uvc_opts(opts_item); \ 165 \ 166 mutex_lock(&opts->lock); \ 167 if (ch->linked || opts->refcnt) { \ 168 ret = -EBUSY; \ 169 goto end; \ 170 } \ 171 \ 172 ret = str2u(page, 0, &num); \ 173 if (ret) \ 174 goto end; \ 175 \ 176 if (num > limit) { \ 177 ret = -EINVAL; \ 178 goto end; \ 179 } \ 180 ch->desc.aname = vnoc(num); \ 181 ret = len; \ 182 end: \ 183 mutex_unlock(&opts->lock); \ 184 mutex_unlock(su_mutex); \ 185 return ret; \ 186 } \ 187 \ 188 UVC_ATTR(uvcg_control_header_, cname, aname) 189 190 UVCG_CTRL_HDR_ATTR(bcd_uvc, bcdUVC, le16_to_cpu, kstrtou16, u16, cpu_to_le16, 191 0xffff); 192 193 UVCG_CTRL_HDR_ATTR(dw_clock_frequency, dwClockFrequency, le32_to_cpu, kstrtou32, 194 u32, cpu_to_le32, 0x7fffffff); 195 196 #undef UVCG_CTRL_HDR_ATTR 197 198 static struct configfs_attribute *uvcg_control_header_attrs[] = { 199 &uvcg_control_header_attr_bcd_uvc, 200 &uvcg_control_header_attr_dw_clock_frequency, 201 NULL, 202 }; 203 204 static const struct config_item_type uvcg_control_header_type = { 205 .ct_item_ops = &uvcg_config_item_ops, 206 .ct_attrs = uvcg_control_header_attrs, 207 .ct_owner = THIS_MODULE, 208 }; 209 210 static struct config_item *uvcg_control_header_make(struct config_group *group, 211 const char *name) 212 { 213 struct uvcg_control_header *h; 214 215 h = kzalloc(sizeof(*h), GFP_KERNEL); 216 if (!h) 217 return ERR_PTR(-ENOMEM); 218 219 h->desc.bLength = UVC_DT_HEADER_SIZE(1); 220 h->desc.bDescriptorType = USB_DT_CS_INTERFACE; 221 h->desc.bDescriptorSubType = UVC_VC_HEADER; 222 h->desc.bcdUVC = cpu_to_le16(0x0100); 223 h->desc.dwClockFrequency = cpu_to_le32(48000000); 224 225 config_item_init_type_name(&h->item, name, &uvcg_control_header_type); 226 227 return &h->item; 228 } 229 230 static struct configfs_group_operations uvcg_control_header_grp_ops = { 231 .make_item = uvcg_control_header_make, 232 }; 233 234 static const struct uvcg_config_group_type uvcg_control_header_grp_type = { 235 .type = { 236 .ct_item_ops = &uvcg_config_item_ops, 237 .ct_group_ops = &uvcg_control_header_grp_ops, 238 .ct_owner = THIS_MODULE, 239 }, 240 .name = "header", 241 }; 242 243 /* ----------------------------------------------------------------------------- 244 * control/processing/default 245 */ 246 247 #define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, conv) \ 248 static ssize_t uvcg_default_processing_##cname##_show( \ 249 struct config_item *item, char *page) \ 250 { \ 251 struct config_group *group = to_config_group(item); \ 252 struct f_uvc_opts *opts; \ 253 struct config_item *opts_item; \ 254 struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ 255 struct uvc_processing_unit_descriptor *pd; \ 256 int result; \ 257 \ 258 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 259 \ 260 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; \ 261 opts = to_f_uvc_opts(opts_item); \ 262 pd = &opts->uvc_processing; \ 263 \ 264 mutex_lock(&opts->lock); \ 265 result = sprintf(page, "%d\n", conv(pd->aname)); \ 266 mutex_unlock(&opts->lock); \ 267 \ 268 mutex_unlock(su_mutex); \ 269 return result; \ 270 } \ 271 \ 272 UVC_ATTR_RO(uvcg_default_processing_, cname, aname) 273 274 #define identity_conv(x) (x) 275 276 UVCG_DEFAULT_PROCESSING_ATTR(b_unit_id, bUnitID, identity_conv); 277 UVCG_DEFAULT_PROCESSING_ATTR(b_source_id, bSourceID, identity_conv); 278 UVCG_DEFAULT_PROCESSING_ATTR(w_max_multiplier, wMaxMultiplier, le16_to_cpu); 279 UVCG_DEFAULT_PROCESSING_ATTR(i_processing, iProcessing, identity_conv); 280 281 #undef identity_conv 282 283 #undef UVCG_DEFAULT_PROCESSING_ATTR 284 285 static ssize_t uvcg_default_processing_bm_controls_show( 286 struct config_item *item, char *page) 287 { 288 struct config_group *group = to_config_group(item); 289 struct f_uvc_opts *opts; 290 struct config_item *opts_item; 291 struct mutex *su_mutex = &group->cg_subsys->su_mutex; 292 struct uvc_processing_unit_descriptor *pd; 293 int result, i; 294 char *pg = page; 295 296 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 297 298 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; 299 opts = to_f_uvc_opts(opts_item); 300 pd = &opts->uvc_processing; 301 302 mutex_lock(&opts->lock); 303 for (result = 0, i = 0; i < pd->bControlSize; ++i) { 304 result += sprintf(pg, "%d\n", pd->bmControls[i]); 305 pg = page + result; 306 } 307 mutex_unlock(&opts->lock); 308 309 mutex_unlock(su_mutex); 310 311 return result; 312 } 313 314 UVC_ATTR_RO(uvcg_default_processing_, bm_controls, bmControls); 315 316 static struct configfs_attribute *uvcg_default_processing_attrs[] = { 317 &uvcg_default_processing_attr_b_unit_id, 318 &uvcg_default_processing_attr_b_source_id, 319 &uvcg_default_processing_attr_w_max_multiplier, 320 &uvcg_default_processing_attr_bm_controls, 321 &uvcg_default_processing_attr_i_processing, 322 NULL, 323 }; 324 325 static const struct uvcg_config_group_type uvcg_default_processing_type = { 326 .type = { 327 .ct_item_ops = &uvcg_config_item_ops, 328 .ct_attrs = uvcg_default_processing_attrs, 329 .ct_owner = THIS_MODULE, 330 }, 331 .name = "default", 332 }; 333 334 /* ----------------------------------------------------------------------------- 335 * control/processing 336 */ 337 338 static const struct uvcg_config_group_type uvcg_processing_grp_type = { 339 .type = { 340 .ct_item_ops = &uvcg_config_item_ops, 341 .ct_owner = THIS_MODULE, 342 }, 343 .name = "processing", 344 .children = (const struct uvcg_config_group_type*[]) { 345 &uvcg_default_processing_type, 346 NULL, 347 }, 348 }; 349 350 /* ----------------------------------------------------------------------------- 351 * control/terminal/camera/default 352 */ 353 354 #define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, conv) \ 355 static ssize_t uvcg_default_camera_##cname##_show( \ 356 struct config_item *item, char *page) \ 357 { \ 358 struct config_group *group = to_config_group(item); \ 359 struct f_uvc_opts *opts; \ 360 struct config_item *opts_item; \ 361 struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ 362 struct uvc_camera_terminal_descriptor *cd; \ 363 int result; \ 364 \ 365 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 366 \ 367 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent-> \ 368 ci_parent; \ 369 opts = to_f_uvc_opts(opts_item); \ 370 cd = &opts->uvc_camera_terminal; \ 371 \ 372 mutex_lock(&opts->lock); \ 373 result = sprintf(page, "%d\n", conv(cd->aname)); \ 374 mutex_unlock(&opts->lock); \ 375 \ 376 mutex_unlock(su_mutex); \ 377 \ 378 return result; \ 379 } \ 380 \ 381 UVC_ATTR_RO(uvcg_default_camera_, cname, aname) 382 383 #define identity_conv(x) (x) 384 385 UVCG_DEFAULT_CAMERA_ATTR(b_terminal_id, bTerminalID, identity_conv); 386 UVCG_DEFAULT_CAMERA_ATTR(w_terminal_type, wTerminalType, le16_to_cpu); 387 UVCG_DEFAULT_CAMERA_ATTR(b_assoc_terminal, bAssocTerminal, identity_conv); 388 UVCG_DEFAULT_CAMERA_ATTR(i_terminal, iTerminal, identity_conv); 389 UVCG_DEFAULT_CAMERA_ATTR(w_objective_focal_length_min, wObjectiveFocalLengthMin, 390 le16_to_cpu); 391 UVCG_DEFAULT_CAMERA_ATTR(w_objective_focal_length_max, wObjectiveFocalLengthMax, 392 le16_to_cpu); 393 UVCG_DEFAULT_CAMERA_ATTR(w_ocular_focal_length, wOcularFocalLength, 394 le16_to_cpu); 395 396 #undef identity_conv 397 398 #undef UVCG_DEFAULT_CAMERA_ATTR 399 400 static ssize_t uvcg_default_camera_bm_controls_show( 401 struct config_item *item, char *page) 402 { 403 struct config_group *group = to_config_group(item); 404 struct f_uvc_opts *opts; 405 struct config_item *opts_item; 406 struct mutex *su_mutex = &group->cg_subsys->su_mutex; 407 struct uvc_camera_terminal_descriptor *cd; 408 int result, i; 409 char *pg = page; 410 411 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 412 413 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent-> 414 ci_parent; 415 opts = to_f_uvc_opts(opts_item); 416 cd = &opts->uvc_camera_terminal; 417 418 mutex_lock(&opts->lock); 419 for (result = 0, i = 0; i < cd->bControlSize; ++i) { 420 result += sprintf(pg, "%d\n", cd->bmControls[i]); 421 pg = page + result; 422 } 423 mutex_unlock(&opts->lock); 424 425 mutex_unlock(su_mutex); 426 return result; 427 } 428 429 UVC_ATTR_RO(uvcg_default_camera_, bm_controls, bmControls); 430 431 static struct configfs_attribute *uvcg_default_camera_attrs[] = { 432 &uvcg_default_camera_attr_b_terminal_id, 433 &uvcg_default_camera_attr_w_terminal_type, 434 &uvcg_default_camera_attr_b_assoc_terminal, 435 &uvcg_default_camera_attr_i_terminal, 436 &uvcg_default_camera_attr_w_objective_focal_length_min, 437 &uvcg_default_camera_attr_w_objective_focal_length_max, 438 &uvcg_default_camera_attr_w_ocular_focal_length, 439 &uvcg_default_camera_attr_bm_controls, 440 NULL, 441 }; 442 443 static const struct uvcg_config_group_type uvcg_default_camera_type = { 444 .type = { 445 .ct_item_ops = &uvcg_config_item_ops, 446 .ct_attrs = uvcg_default_camera_attrs, 447 .ct_owner = THIS_MODULE, 448 }, 449 .name = "default", 450 }; 451 452 /* ----------------------------------------------------------------------------- 453 * control/terminal/camera 454 */ 455 456 static const struct uvcg_config_group_type uvcg_camera_grp_type = { 457 .type = { 458 .ct_item_ops = &uvcg_config_item_ops, 459 .ct_owner = THIS_MODULE, 460 }, 461 .name = "camera", 462 .children = (const struct uvcg_config_group_type*[]) { 463 &uvcg_default_camera_type, 464 NULL, 465 }, 466 }; 467 468 /* ----------------------------------------------------------------------------- 469 * control/terminal/output/default 470 */ 471 472 #define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, conv) \ 473 static ssize_t uvcg_default_output_##cname##_show( \ 474 struct config_item *item, char *page) \ 475 { \ 476 struct config_group *group = to_config_group(item); \ 477 struct f_uvc_opts *opts; \ 478 struct config_item *opts_item; \ 479 struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ 480 struct uvc_output_terminal_descriptor *cd; \ 481 int result; \ 482 \ 483 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 484 \ 485 opts_item = group->cg_item.ci_parent->ci_parent-> \ 486 ci_parent->ci_parent; \ 487 opts = to_f_uvc_opts(opts_item); \ 488 cd = &opts->uvc_output_terminal; \ 489 \ 490 mutex_lock(&opts->lock); \ 491 result = sprintf(page, "%d\n", conv(cd->aname)); \ 492 mutex_unlock(&opts->lock); \ 493 \ 494 mutex_unlock(su_mutex); \ 495 \ 496 return result; \ 497 } \ 498 \ 499 UVC_ATTR_RO(uvcg_default_output_, cname, aname) 500 501 #define identity_conv(x) (x) 502 503 UVCG_DEFAULT_OUTPUT_ATTR(b_terminal_id, bTerminalID, identity_conv); 504 UVCG_DEFAULT_OUTPUT_ATTR(w_terminal_type, wTerminalType, le16_to_cpu); 505 UVCG_DEFAULT_OUTPUT_ATTR(b_assoc_terminal, bAssocTerminal, identity_conv); 506 UVCG_DEFAULT_OUTPUT_ATTR(b_source_id, bSourceID, identity_conv); 507 UVCG_DEFAULT_OUTPUT_ATTR(i_terminal, iTerminal, identity_conv); 508 509 #undef identity_conv 510 511 #undef UVCG_DEFAULT_OUTPUT_ATTR 512 513 static struct configfs_attribute *uvcg_default_output_attrs[] = { 514 &uvcg_default_output_attr_b_terminal_id, 515 &uvcg_default_output_attr_w_terminal_type, 516 &uvcg_default_output_attr_b_assoc_terminal, 517 &uvcg_default_output_attr_b_source_id, 518 &uvcg_default_output_attr_i_terminal, 519 NULL, 520 }; 521 522 static const struct uvcg_config_group_type uvcg_default_output_type = { 523 .type = { 524 .ct_item_ops = &uvcg_config_item_ops, 525 .ct_attrs = uvcg_default_output_attrs, 526 .ct_owner = THIS_MODULE, 527 }, 528 .name = "default", 529 }; 530 531 /* ----------------------------------------------------------------------------- 532 * control/terminal/output 533 */ 534 535 static const struct uvcg_config_group_type uvcg_output_grp_type = { 536 .type = { 537 .ct_item_ops = &uvcg_config_item_ops, 538 .ct_owner = THIS_MODULE, 539 }, 540 .name = "output", 541 .children = (const struct uvcg_config_group_type*[]) { 542 &uvcg_default_output_type, 543 NULL, 544 }, 545 }; 546 547 /* ----------------------------------------------------------------------------- 548 * control/terminal 549 */ 550 551 static const struct uvcg_config_group_type uvcg_terminal_grp_type = { 552 .type = { 553 .ct_item_ops = &uvcg_config_item_ops, 554 .ct_owner = THIS_MODULE, 555 }, 556 .name = "terminal", 557 .children = (const struct uvcg_config_group_type*[]) { 558 &uvcg_camera_grp_type, 559 &uvcg_output_grp_type, 560 NULL, 561 }, 562 }; 563 564 /* ----------------------------------------------------------------------------- 565 * control/class/{fs|ss} 566 */ 567 568 struct uvcg_control_class_group { 569 struct config_group group; 570 const char *name; 571 }; 572 573 static inline struct uvc_descriptor_header 574 **uvcg_get_ctl_class_arr(struct config_item *i, struct f_uvc_opts *o) 575 { 576 struct uvcg_control_class_group *group = 577 container_of(i, struct uvcg_control_class_group, 578 group.cg_item); 579 580 if (!strcmp(group->name, "fs")) 581 return o->uvc_fs_control_cls; 582 583 if (!strcmp(group->name, "ss")) 584 return o->uvc_ss_control_cls; 585 586 return NULL; 587 } 588 589 static int uvcg_control_class_allow_link(struct config_item *src, 590 struct config_item *target) 591 { 592 struct config_item *control, *header; 593 struct f_uvc_opts *opts; 594 struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex; 595 struct uvc_descriptor_header **class_array; 596 struct uvcg_control_header *target_hdr; 597 int ret = -EINVAL; 598 599 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 600 601 control = src->ci_parent->ci_parent; 602 header = config_group_find_item(to_config_group(control), "header"); 603 if (!header || target->ci_parent != header) 604 goto out; 605 606 opts = to_f_uvc_opts(control->ci_parent); 607 608 mutex_lock(&opts->lock); 609 610 class_array = uvcg_get_ctl_class_arr(src, opts); 611 if (!class_array) 612 goto unlock; 613 if (opts->refcnt || class_array[0]) { 614 ret = -EBUSY; 615 goto unlock; 616 } 617 618 target_hdr = to_uvcg_control_header(target); 619 ++target_hdr->linked; 620 class_array[0] = (struct uvc_descriptor_header *)&target_hdr->desc; 621 ret = 0; 622 623 unlock: 624 mutex_unlock(&opts->lock); 625 out: 626 config_item_put(header); 627 mutex_unlock(su_mutex); 628 return ret; 629 } 630 631 static void uvcg_control_class_drop_link(struct config_item *src, 632 struct config_item *target) 633 { 634 struct config_item *control, *header; 635 struct f_uvc_opts *opts; 636 struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex; 637 struct uvc_descriptor_header **class_array; 638 struct uvcg_control_header *target_hdr; 639 640 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 641 642 control = src->ci_parent->ci_parent; 643 header = config_group_find_item(to_config_group(control), "header"); 644 if (!header || target->ci_parent != header) 645 goto out; 646 647 opts = to_f_uvc_opts(control->ci_parent); 648 649 mutex_lock(&opts->lock); 650 651 class_array = uvcg_get_ctl_class_arr(src, opts); 652 if (!class_array || opts->refcnt) 653 goto unlock; 654 655 target_hdr = to_uvcg_control_header(target); 656 --target_hdr->linked; 657 class_array[0] = NULL; 658 659 unlock: 660 mutex_unlock(&opts->lock); 661 out: 662 config_item_put(header); 663 mutex_unlock(su_mutex); 664 } 665 666 static struct configfs_item_operations uvcg_control_class_item_ops = { 667 .release = uvcg_config_item_release, 668 .allow_link = uvcg_control_class_allow_link, 669 .drop_link = uvcg_control_class_drop_link, 670 }; 671 672 static const struct config_item_type uvcg_control_class_type = { 673 .ct_item_ops = &uvcg_control_class_item_ops, 674 .ct_owner = THIS_MODULE, 675 }; 676 677 /* ----------------------------------------------------------------------------- 678 * control/class 679 */ 680 681 static int uvcg_control_class_create_children(struct config_group *parent) 682 { 683 static const char * const names[] = { "fs", "ss" }; 684 unsigned int i; 685 686 for (i = 0; i < ARRAY_SIZE(names); ++i) { 687 struct uvcg_control_class_group *group; 688 689 group = kzalloc(sizeof(*group), GFP_KERNEL); 690 if (!group) 691 return -ENOMEM; 692 693 group->name = names[i]; 694 695 config_group_init_type_name(&group->group, group->name, 696 &uvcg_control_class_type); 697 configfs_add_default_group(&group->group, parent); 698 } 699 700 return 0; 701 } 702 703 static const struct uvcg_config_group_type uvcg_control_class_grp_type = { 704 .type = { 705 .ct_item_ops = &uvcg_config_item_ops, 706 .ct_owner = THIS_MODULE, 707 }, 708 .name = "class", 709 .create_children = uvcg_control_class_create_children, 710 }; 711 712 /* ----------------------------------------------------------------------------- 713 * control 714 */ 715 716 static ssize_t uvcg_default_control_b_interface_number_show( 717 struct config_item *item, char *page) 718 { 719 struct config_group *group = to_config_group(item); 720 struct mutex *su_mutex = &group->cg_subsys->su_mutex; 721 struct config_item *opts_item; 722 struct f_uvc_opts *opts; 723 int result = 0; 724 725 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 726 727 opts_item = item->ci_parent; 728 opts = to_f_uvc_opts(opts_item); 729 730 mutex_lock(&opts->lock); 731 result += sprintf(page, "%u\n", opts->control_interface); 732 mutex_unlock(&opts->lock); 733 734 mutex_unlock(su_mutex); 735 736 return result; 737 } 738 739 UVC_ATTR_RO(uvcg_default_control_, b_interface_number, bInterfaceNumber); 740 741 static struct configfs_attribute *uvcg_default_control_attrs[] = { 742 &uvcg_default_control_attr_b_interface_number, 743 NULL, 744 }; 745 746 static const struct uvcg_config_group_type uvcg_control_grp_type = { 747 .type = { 748 .ct_item_ops = &uvcg_config_item_ops, 749 .ct_attrs = uvcg_default_control_attrs, 750 .ct_owner = THIS_MODULE, 751 }, 752 .name = "control", 753 .children = (const struct uvcg_config_group_type*[]) { 754 &uvcg_control_header_grp_type, 755 &uvcg_processing_grp_type, 756 &uvcg_terminal_grp_type, 757 &uvcg_control_class_grp_type, 758 NULL, 759 }, 760 }; 761 762 /* ----------------------------------------------------------------------------- 763 * streaming/uncompressed 764 * streaming/mjpeg 765 */ 766 767 static const char * const uvcg_format_names[] = { 768 "uncompressed", 769 "mjpeg", 770 }; 771 772 enum uvcg_format_type { 773 UVCG_UNCOMPRESSED = 0, 774 UVCG_MJPEG, 775 }; 776 777 struct uvcg_format { 778 struct config_group group; 779 enum uvcg_format_type type; 780 unsigned linked; 781 unsigned num_frames; 782 __u8 bmaControls[UVCG_STREAMING_CONTROL_SIZE]; 783 }; 784 785 static struct uvcg_format *to_uvcg_format(struct config_item *item) 786 { 787 return container_of(to_config_group(item), struct uvcg_format, group); 788 } 789 790 static ssize_t uvcg_format_bma_controls_show(struct uvcg_format *f, char *page) 791 { 792 struct f_uvc_opts *opts; 793 struct config_item *opts_item; 794 struct mutex *su_mutex = &f->group.cg_subsys->su_mutex; 795 int result, i; 796 char *pg = page; 797 798 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 799 800 opts_item = f->group.cg_item.ci_parent->ci_parent->ci_parent; 801 opts = to_f_uvc_opts(opts_item); 802 803 mutex_lock(&opts->lock); 804 result = sprintf(pg, "0x"); 805 pg += result; 806 for (i = 0; i < UVCG_STREAMING_CONTROL_SIZE; ++i) { 807 result += sprintf(pg, "%x\n", f->bmaControls[i]); 808 pg = page + result; 809 } 810 mutex_unlock(&opts->lock); 811 812 mutex_unlock(su_mutex); 813 return result; 814 } 815 816 static ssize_t uvcg_format_bma_controls_store(struct uvcg_format *ch, 817 const char *page, size_t len) 818 { 819 struct f_uvc_opts *opts; 820 struct config_item *opts_item; 821 struct mutex *su_mutex = &ch->group.cg_subsys->su_mutex; 822 int ret = -EINVAL; 823 824 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 825 826 opts_item = ch->group.cg_item.ci_parent->ci_parent->ci_parent; 827 opts = to_f_uvc_opts(opts_item); 828 829 mutex_lock(&opts->lock); 830 if (ch->linked || opts->refcnt) { 831 ret = -EBUSY; 832 goto end; 833 } 834 835 if (len < 4 || *page != '0' || 836 (*(page + 1) != 'x' && *(page + 1) != 'X')) 837 goto end; 838 ret = hex2bin(ch->bmaControls, page + 2, 1); 839 if (ret < 0) 840 goto end; 841 ret = len; 842 end: 843 mutex_unlock(&opts->lock); 844 mutex_unlock(su_mutex); 845 return ret; 846 } 847 848 struct uvcg_format_ptr { 849 struct uvcg_format *fmt; 850 struct list_head entry; 851 }; 852 853 /* ----------------------------------------------------------------------------- 854 * streaming/header/<NAME> 855 * streaming/header 856 */ 857 858 struct uvcg_streaming_header { 859 struct config_item item; 860 struct uvc_input_header_descriptor desc; 861 unsigned linked; 862 struct list_head formats; 863 unsigned num_fmt; 864 }; 865 866 static struct uvcg_streaming_header *to_uvcg_streaming_header(struct config_item *item) 867 { 868 return container_of(item, struct uvcg_streaming_header, item); 869 } 870 871 static int uvcg_streaming_header_allow_link(struct config_item *src, 872 struct config_item *target) 873 { 874 struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex; 875 struct config_item *opts_item; 876 struct f_uvc_opts *opts; 877 struct uvcg_streaming_header *src_hdr; 878 struct uvcg_format *target_fmt = NULL; 879 struct uvcg_format_ptr *format_ptr; 880 int i, ret = -EINVAL; 881 882 src_hdr = to_uvcg_streaming_header(src); 883 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 884 885 opts_item = src->ci_parent->ci_parent->ci_parent; 886 opts = to_f_uvc_opts(opts_item); 887 888 mutex_lock(&opts->lock); 889 890 if (src_hdr->linked) { 891 ret = -EBUSY; 892 goto out; 893 } 894 895 /* 896 * Linking is only allowed to direct children of the format nodes 897 * (streaming/uncompressed or streaming/mjpeg nodes). First check that 898 * the grand-parent of the target matches the grand-parent of the source 899 * (the streaming node), and then verify that the target parent is a 900 * format node. 901 */ 902 if (src->ci_parent->ci_parent != target->ci_parent->ci_parent) 903 goto out; 904 905 for (i = 0; i < ARRAY_SIZE(uvcg_format_names); ++i) { 906 if (!strcmp(target->ci_parent->ci_name, uvcg_format_names[i])) 907 break; 908 } 909 910 if (i == ARRAY_SIZE(uvcg_format_names)) 911 goto out; 912 913 target_fmt = container_of(to_config_group(target), struct uvcg_format, 914 group); 915 if (!target_fmt) 916 goto out; 917 918 format_ptr = kzalloc(sizeof(*format_ptr), GFP_KERNEL); 919 if (!format_ptr) { 920 ret = -ENOMEM; 921 goto out; 922 } 923 ret = 0; 924 format_ptr->fmt = target_fmt; 925 list_add_tail(&format_ptr->entry, &src_hdr->formats); 926 ++src_hdr->num_fmt; 927 928 out: 929 mutex_unlock(&opts->lock); 930 mutex_unlock(su_mutex); 931 return ret; 932 } 933 934 static void uvcg_streaming_header_drop_link(struct config_item *src, 935 struct config_item *target) 936 { 937 struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex; 938 struct config_item *opts_item; 939 struct f_uvc_opts *opts; 940 struct uvcg_streaming_header *src_hdr; 941 struct uvcg_format *target_fmt = NULL; 942 struct uvcg_format_ptr *format_ptr, *tmp; 943 944 src_hdr = to_uvcg_streaming_header(src); 945 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 946 947 opts_item = src->ci_parent->ci_parent->ci_parent; 948 opts = to_f_uvc_opts(opts_item); 949 950 mutex_lock(&opts->lock); 951 target_fmt = container_of(to_config_group(target), struct uvcg_format, 952 group); 953 if (!target_fmt) 954 goto out; 955 956 list_for_each_entry_safe(format_ptr, tmp, &src_hdr->formats, entry) 957 if (format_ptr->fmt == target_fmt) { 958 list_del(&format_ptr->entry); 959 kfree(format_ptr); 960 --src_hdr->num_fmt; 961 break; 962 } 963 964 out: 965 mutex_unlock(&opts->lock); 966 mutex_unlock(su_mutex); 967 } 968 969 static struct configfs_item_operations uvcg_streaming_header_item_ops = { 970 .release = uvcg_config_item_release, 971 .allow_link = uvcg_streaming_header_allow_link, 972 .drop_link = uvcg_streaming_header_drop_link, 973 }; 974 975 #define UVCG_STREAMING_HEADER_ATTR(cname, aname, conv) \ 976 static ssize_t uvcg_streaming_header_##cname##_show( \ 977 struct config_item *item, char *page) \ 978 { \ 979 struct uvcg_streaming_header *sh = to_uvcg_streaming_header(item); \ 980 struct f_uvc_opts *opts; \ 981 struct config_item *opts_item; \ 982 struct mutex *su_mutex = &sh->item.ci_group->cg_subsys->su_mutex;\ 983 int result; \ 984 \ 985 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 986 \ 987 opts_item = sh->item.ci_parent->ci_parent->ci_parent; \ 988 opts = to_f_uvc_opts(opts_item); \ 989 \ 990 mutex_lock(&opts->lock); \ 991 result = sprintf(page, "%d\n", conv(sh->desc.aname)); \ 992 mutex_unlock(&opts->lock); \ 993 \ 994 mutex_unlock(su_mutex); \ 995 return result; \ 996 } \ 997 \ 998 UVC_ATTR_RO(uvcg_streaming_header_, cname, aname) 999 1000 #define identity_conv(x) (x) 1001 1002 UVCG_STREAMING_HEADER_ATTR(bm_info, bmInfo, identity_conv); 1003 UVCG_STREAMING_HEADER_ATTR(b_terminal_link, bTerminalLink, identity_conv); 1004 UVCG_STREAMING_HEADER_ATTR(b_still_capture_method, bStillCaptureMethod, 1005 identity_conv); 1006 UVCG_STREAMING_HEADER_ATTR(b_trigger_support, bTriggerSupport, identity_conv); 1007 UVCG_STREAMING_HEADER_ATTR(b_trigger_usage, bTriggerUsage, identity_conv); 1008 1009 #undef identity_conv 1010 1011 #undef UVCG_STREAMING_HEADER_ATTR 1012 1013 static struct configfs_attribute *uvcg_streaming_header_attrs[] = { 1014 &uvcg_streaming_header_attr_bm_info, 1015 &uvcg_streaming_header_attr_b_terminal_link, 1016 &uvcg_streaming_header_attr_b_still_capture_method, 1017 &uvcg_streaming_header_attr_b_trigger_support, 1018 &uvcg_streaming_header_attr_b_trigger_usage, 1019 NULL, 1020 }; 1021 1022 static const struct config_item_type uvcg_streaming_header_type = { 1023 .ct_item_ops = &uvcg_streaming_header_item_ops, 1024 .ct_attrs = uvcg_streaming_header_attrs, 1025 .ct_owner = THIS_MODULE, 1026 }; 1027 1028 static struct config_item 1029 *uvcg_streaming_header_make(struct config_group *group, const char *name) 1030 { 1031 struct uvcg_streaming_header *h; 1032 1033 h = kzalloc(sizeof(*h), GFP_KERNEL); 1034 if (!h) 1035 return ERR_PTR(-ENOMEM); 1036 1037 INIT_LIST_HEAD(&h->formats); 1038 h->desc.bDescriptorType = USB_DT_CS_INTERFACE; 1039 h->desc.bDescriptorSubType = UVC_VS_INPUT_HEADER; 1040 h->desc.bTerminalLink = 3; 1041 h->desc.bControlSize = UVCG_STREAMING_CONTROL_SIZE; 1042 1043 config_item_init_type_name(&h->item, name, &uvcg_streaming_header_type); 1044 1045 return &h->item; 1046 } 1047 1048 static struct configfs_group_operations uvcg_streaming_header_grp_ops = { 1049 .make_item = uvcg_streaming_header_make, 1050 }; 1051 1052 static const struct uvcg_config_group_type uvcg_streaming_header_grp_type = { 1053 .type = { 1054 .ct_item_ops = &uvcg_config_item_ops, 1055 .ct_group_ops = &uvcg_streaming_header_grp_ops, 1056 .ct_owner = THIS_MODULE, 1057 }, 1058 .name = "header", 1059 }; 1060 1061 /* ----------------------------------------------------------------------------- 1062 * streaming/<mode>/<format>/<NAME> 1063 */ 1064 1065 struct uvcg_frame { 1066 struct config_item item; 1067 enum uvcg_format_type fmt_type; 1068 struct { 1069 u8 b_length; 1070 u8 b_descriptor_type; 1071 u8 b_descriptor_subtype; 1072 u8 b_frame_index; 1073 u8 bm_capabilities; 1074 u16 w_width; 1075 u16 w_height; 1076 u32 dw_min_bit_rate; 1077 u32 dw_max_bit_rate; 1078 u32 dw_max_video_frame_buffer_size; 1079 u32 dw_default_frame_interval; 1080 u8 b_frame_interval_type; 1081 } __attribute__((packed)) frame; 1082 u32 *dw_frame_interval; 1083 }; 1084 1085 static struct uvcg_frame *to_uvcg_frame(struct config_item *item) 1086 { 1087 return container_of(item, struct uvcg_frame, item); 1088 } 1089 1090 #define UVCG_FRAME_ATTR(cname, aname, to_cpu_endian, to_little_endian, bits) \ 1091 static ssize_t uvcg_frame_##cname##_show(struct config_item *item, char *page)\ 1092 { \ 1093 struct uvcg_frame *f = to_uvcg_frame(item); \ 1094 struct f_uvc_opts *opts; \ 1095 struct config_item *opts_item; \ 1096 struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex;\ 1097 int result; \ 1098 \ 1099 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1100 \ 1101 opts_item = f->item.ci_parent->ci_parent->ci_parent->ci_parent; \ 1102 opts = to_f_uvc_opts(opts_item); \ 1103 \ 1104 mutex_lock(&opts->lock); \ 1105 result = sprintf(page, "%d\n", to_cpu_endian(f->frame.cname)); \ 1106 mutex_unlock(&opts->lock); \ 1107 \ 1108 mutex_unlock(su_mutex); \ 1109 return result; \ 1110 } \ 1111 \ 1112 static ssize_t uvcg_frame_##cname##_store(struct config_item *item, \ 1113 const char *page, size_t len)\ 1114 { \ 1115 struct uvcg_frame *f = to_uvcg_frame(item); \ 1116 struct f_uvc_opts *opts; \ 1117 struct config_item *opts_item; \ 1118 struct uvcg_format *fmt; \ 1119 struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex;\ 1120 int ret; \ 1121 u##bits num; \ 1122 \ 1123 ret = kstrtou##bits(page, 0, &num); \ 1124 if (ret) \ 1125 return ret; \ 1126 \ 1127 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1128 \ 1129 opts_item = f->item.ci_parent->ci_parent->ci_parent->ci_parent; \ 1130 opts = to_f_uvc_opts(opts_item); \ 1131 fmt = to_uvcg_format(f->item.ci_parent); \ 1132 \ 1133 mutex_lock(&opts->lock); \ 1134 if (fmt->linked || opts->refcnt) { \ 1135 ret = -EBUSY; \ 1136 goto end; \ 1137 } \ 1138 \ 1139 f->frame.cname = to_little_endian(num); \ 1140 ret = len; \ 1141 end: \ 1142 mutex_unlock(&opts->lock); \ 1143 mutex_unlock(su_mutex); \ 1144 return ret; \ 1145 } \ 1146 \ 1147 UVC_ATTR(uvcg_frame_, cname, aname); 1148 1149 #define noop_conversion(x) (x) 1150 1151 UVCG_FRAME_ATTR(bm_capabilities, bmCapabilities, noop_conversion, 1152 noop_conversion, 8); 1153 UVCG_FRAME_ATTR(w_width, wWidth, le16_to_cpu, cpu_to_le16, 16); 1154 UVCG_FRAME_ATTR(w_height, wHeight, le16_to_cpu, cpu_to_le16, 16); 1155 UVCG_FRAME_ATTR(dw_min_bit_rate, dwMinBitRate, le32_to_cpu, cpu_to_le32, 32); 1156 UVCG_FRAME_ATTR(dw_max_bit_rate, dwMaxBitRate, le32_to_cpu, cpu_to_le32, 32); 1157 UVCG_FRAME_ATTR(dw_max_video_frame_buffer_size, dwMaxVideoFrameBufferSize, 1158 le32_to_cpu, cpu_to_le32, 32); 1159 UVCG_FRAME_ATTR(dw_default_frame_interval, dwDefaultFrameInterval, 1160 le32_to_cpu, cpu_to_le32, 32); 1161 1162 #undef noop_conversion 1163 1164 #undef UVCG_FRAME_ATTR 1165 1166 static ssize_t uvcg_frame_dw_frame_interval_show(struct config_item *item, 1167 char *page) 1168 { 1169 struct uvcg_frame *frm = to_uvcg_frame(item); 1170 struct f_uvc_opts *opts; 1171 struct config_item *opts_item; 1172 struct mutex *su_mutex = &frm->item.ci_group->cg_subsys->su_mutex; 1173 int result, i; 1174 char *pg = page; 1175 1176 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 1177 1178 opts_item = frm->item.ci_parent->ci_parent->ci_parent->ci_parent; 1179 opts = to_f_uvc_opts(opts_item); 1180 1181 mutex_lock(&opts->lock); 1182 for (result = 0, i = 0; i < frm->frame.b_frame_interval_type; ++i) { 1183 result += sprintf(pg, "%d\n", 1184 le32_to_cpu(frm->dw_frame_interval[i])); 1185 pg = page + result; 1186 } 1187 mutex_unlock(&opts->lock); 1188 1189 mutex_unlock(su_mutex); 1190 return result; 1191 } 1192 1193 static inline int __uvcg_count_frm_intrv(char *buf, void *priv) 1194 { 1195 ++*((int *)priv); 1196 return 0; 1197 } 1198 1199 static inline int __uvcg_fill_frm_intrv(char *buf, void *priv) 1200 { 1201 u32 num, **interv; 1202 int ret; 1203 1204 ret = kstrtou32(buf, 0, &num); 1205 if (ret) 1206 return ret; 1207 1208 interv = priv; 1209 **interv = cpu_to_le32(num); 1210 ++*interv; 1211 1212 return 0; 1213 } 1214 1215 static int __uvcg_iter_frm_intrv(const char *page, size_t len, 1216 int (*fun)(char *, void *), void *priv) 1217 { 1218 /* sign, base 2 representation, newline, terminator */ 1219 char buf[1 + sizeof(u32) * 8 + 1 + 1]; 1220 const char *pg = page; 1221 int i, ret; 1222 1223 if (!fun) 1224 return -EINVAL; 1225 1226 while (pg - page < len) { 1227 i = 0; 1228 while (i < sizeof(buf) && (pg - page < len) && 1229 *pg != '\0' && *pg != '\n') 1230 buf[i++] = *pg++; 1231 if (i == sizeof(buf)) 1232 return -EINVAL; 1233 while ((pg - page < len) && (*pg == '\0' || *pg == '\n')) 1234 ++pg; 1235 buf[i] = '\0'; 1236 ret = fun(buf, priv); 1237 if (ret) 1238 return ret; 1239 } 1240 1241 return 0; 1242 } 1243 1244 static ssize_t uvcg_frame_dw_frame_interval_store(struct config_item *item, 1245 const char *page, size_t len) 1246 { 1247 struct uvcg_frame *ch = to_uvcg_frame(item); 1248 struct f_uvc_opts *opts; 1249 struct config_item *opts_item; 1250 struct uvcg_format *fmt; 1251 struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex; 1252 int ret = 0, n = 0; 1253 u32 *frm_intrv, *tmp; 1254 1255 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 1256 1257 opts_item = ch->item.ci_parent->ci_parent->ci_parent->ci_parent; 1258 opts = to_f_uvc_opts(opts_item); 1259 fmt = to_uvcg_format(ch->item.ci_parent); 1260 1261 mutex_lock(&opts->lock); 1262 if (fmt->linked || opts->refcnt) { 1263 ret = -EBUSY; 1264 goto end; 1265 } 1266 1267 ret = __uvcg_iter_frm_intrv(page, len, __uvcg_count_frm_intrv, &n); 1268 if (ret) 1269 goto end; 1270 1271 tmp = frm_intrv = kcalloc(n, sizeof(u32), GFP_KERNEL); 1272 if (!frm_intrv) { 1273 ret = -ENOMEM; 1274 goto end; 1275 } 1276 1277 ret = __uvcg_iter_frm_intrv(page, len, __uvcg_fill_frm_intrv, &tmp); 1278 if (ret) { 1279 kfree(frm_intrv); 1280 goto end; 1281 } 1282 1283 kfree(ch->dw_frame_interval); 1284 ch->dw_frame_interval = frm_intrv; 1285 ch->frame.b_frame_interval_type = n; 1286 ret = len; 1287 1288 end: 1289 mutex_unlock(&opts->lock); 1290 mutex_unlock(su_mutex); 1291 return ret; 1292 } 1293 1294 UVC_ATTR(uvcg_frame_, dw_frame_interval, dwFrameInterval); 1295 1296 static struct configfs_attribute *uvcg_frame_attrs[] = { 1297 &uvcg_frame_attr_bm_capabilities, 1298 &uvcg_frame_attr_w_width, 1299 &uvcg_frame_attr_w_height, 1300 &uvcg_frame_attr_dw_min_bit_rate, 1301 &uvcg_frame_attr_dw_max_bit_rate, 1302 &uvcg_frame_attr_dw_max_video_frame_buffer_size, 1303 &uvcg_frame_attr_dw_default_frame_interval, 1304 &uvcg_frame_attr_dw_frame_interval, 1305 NULL, 1306 }; 1307 1308 static const struct config_item_type uvcg_frame_type = { 1309 .ct_item_ops = &uvcg_config_item_ops, 1310 .ct_attrs = uvcg_frame_attrs, 1311 .ct_owner = THIS_MODULE, 1312 }; 1313 1314 static struct config_item *uvcg_frame_make(struct config_group *group, 1315 const char *name) 1316 { 1317 struct uvcg_frame *h; 1318 struct uvcg_format *fmt; 1319 struct f_uvc_opts *opts; 1320 struct config_item *opts_item; 1321 1322 h = kzalloc(sizeof(*h), GFP_KERNEL); 1323 if (!h) 1324 return ERR_PTR(-ENOMEM); 1325 1326 h->frame.b_descriptor_type = USB_DT_CS_INTERFACE; 1327 h->frame.b_frame_index = 1; 1328 h->frame.w_width = cpu_to_le16(640); 1329 h->frame.w_height = cpu_to_le16(360); 1330 h->frame.dw_min_bit_rate = cpu_to_le32(18432000); 1331 h->frame.dw_max_bit_rate = cpu_to_le32(55296000); 1332 h->frame.dw_max_video_frame_buffer_size = cpu_to_le32(460800); 1333 h->frame.dw_default_frame_interval = cpu_to_le32(666666); 1334 1335 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; 1336 opts = to_f_uvc_opts(opts_item); 1337 1338 mutex_lock(&opts->lock); 1339 fmt = to_uvcg_format(&group->cg_item); 1340 if (fmt->type == UVCG_UNCOMPRESSED) { 1341 h->frame.b_descriptor_subtype = UVC_VS_FRAME_UNCOMPRESSED; 1342 h->fmt_type = UVCG_UNCOMPRESSED; 1343 } else if (fmt->type == UVCG_MJPEG) { 1344 h->frame.b_descriptor_subtype = UVC_VS_FRAME_MJPEG; 1345 h->fmt_type = UVCG_MJPEG; 1346 } else { 1347 mutex_unlock(&opts->lock); 1348 kfree(h); 1349 return ERR_PTR(-EINVAL); 1350 } 1351 ++fmt->num_frames; 1352 mutex_unlock(&opts->lock); 1353 1354 config_item_init_type_name(&h->item, name, &uvcg_frame_type); 1355 1356 return &h->item; 1357 } 1358 1359 static void uvcg_frame_drop(struct config_group *group, struct config_item *item) 1360 { 1361 struct uvcg_format *fmt; 1362 struct f_uvc_opts *opts; 1363 struct config_item *opts_item; 1364 1365 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; 1366 opts = to_f_uvc_opts(opts_item); 1367 1368 mutex_lock(&opts->lock); 1369 fmt = to_uvcg_format(&group->cg_item); 1370 --fmt->num_frames; 1371 mutex_unlock(&opts->lock); 1372 1373 config_item_put(item); 1374 } 1375 1376 /* ----------------------------------------------------------------------------- 1377 * streaming/uncompressed/<NAME> 1378 */ 1379 1380 struct uvcg_uncompressed { 1381 struct uvcg_format fmt; 1382 struct uvc_format_uncompressed desc; 1383 }; 1384 1385 static struct uvcg_uncompressed *to_uvcg_uncompressed(struct config_item *item) 1386 { 1387 return container_of( 1388 container_of(to_config_group(item), struct uvcg_format, group), 1389 struct uvcg_uncompressed, fmt); 1390 } 1391 1392 static struct configfs_group_operations uvcg_uncompressed_group_ops = { 1393 .make_item = uvcg_frame_make, 1394 .drop_item = uvcg_frame_drop, 1395 }; 1396 1397 static ssize_t uvcg_uncompressed_guid_format_show(struct config_item *item, 1398 char *page) 1399 { 1400 struct uvcg_uncompressed *ch = to_uvcg_uncompressed(item); 1401 struct f_uvc_opts *opts; 1402 struct config_item *opts_item; 1403 struct mutex *su_mutex = &ch->fmt.group.cg_subsys->su_mutex; 1404 1405 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 1406 1407 opts_item = ch->fmt.group.cg_item.ci_parent->ci_parent->ci_parent; 1408 opts = to_f_uvc_opts(opts_item); 1409 1410 mutex_lock(&opts->lock); 1411 memcpy(page, ch->desc.guidFormat, sizeof(ch->desc.guidFormat)); 1412 mutex_unlock(&opts->lock); 1413 1414 mutex_unlock(su_mutex); 1415 1416 return sizeof(ch->desc.guidFormat); 1417 } 1418 1419 static ssize_t uvcg_uncompressed_guid_format_store(struct config_item *item, 1420 const char *page, size_t len) 1421 { 1422 struct uvcg_uncompressed *ch = to_uvcg_uncompressed(item); 1423 struct f_uvc_opts *opts; 1424 struct config_item *opts_item; 1425 struct mutex *su_mutex = &ch->fmt.group.cg_subsys->su_mutex; 1426 int ret; 1427 1428 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 1429 1430 opts_item = ch->fmt.group.cg_item.ci_parent->ci_parent->ci_parent; 1431 opts = to_f_uvc_opts(opts_item); 1432 1433 mutex_lock(&opts->lock); 1434 if (ch->fmt.linked || opts->refcnt) { 1435 ret = -EBUSY; 1436 goto end; 1437 } 1438 1439 memcpy(ch->desc.guidFormat, page, 1440 min(sizeof(ch->desc.guidFormat), len)); 1441 ret = sizeof(ch->desc.guidFormat); 1442 1443 end: 1444 mutex_unlock(&opts->lock); 1445 mutex_unlock(su_mutex); 1446 return ret; 1447 } 1448 1449 UVC_ATTR(uvcg_uncompressed_, guid_format, guidFormat); 1450 1451 #define UVCG_UNCOMPRESSED_ATTR_RO(cname, aname, conv) \ 1452 static ssize_t uvcg_uncompressed_##cname##_show( \ 1453 struct config_item *item, char *page) \ 1454 { \ 1455 struct uvcg_uncompressed *u = to_uvcg_uncompressed(item); \ 1456 struct f_uvc_opts *opts; \ 1457 struct config_item *opts_item; \ 1458 struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \ 1459 int result; \ 1460 \ 1461 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1462 \ 1463 opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\ 1464 opts = to_f_uvc_opts(opts_item); \ 1465 \ 1466 mutex_lock(&opts->lock); \ 1467 result = sprintf(page, "%d\n", conv(u->desc.aname)); \ 1468 mutex_unlock(&opts->lock); \ 1469 \ 1470 mutex_unlock(su_mutex); \ 1471 return result; \ 1472 } \ 1473 \ 1474 UVC_ATTR_RO(uvcg_uncompressed_, cname, aname); 1475 1476 #define UVCG_UNCOMPRESSED_ATTR(cname, aname, conv) \ 1477 static ssize_t uvcg_uncompressed_##cname##_show( \ 1478 struct config_item *item, char *page) \ 1479 { \ 1480 struct uvcg_uncompressed *u = to_uvcg_uncompressed(item); \ 1481 struct f_uvc_opts *opts; \ 1482 struct config_item *opts_item; \ 1483 struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \ 1484 int result; \ 1485 \ 1486 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1487 \ 1488 opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\ 1489 opts = to_f_uvc_opts(opts_item); \ 1490 \ 1491 mutex_lock(&opts->lock); \ 1492 result = sprintf(page, "%d\n", conv(u->desc.aname)); \ 1493 mutex_unlock(&opts->lock); \ 1494 \ 1495 mutex_unlock(su_mutex); \ 1496 return result; \ 1497 } \ 1498 \ 1499 static ssize_t \ 1500 uvcg_uncompressed_##cname##_store(struct config_item *item, \ 1501 const char *page, size_t len) \ 1502 { \ 1503 struct uvcg_uncompressed *u = to_uvcg_uncompressed(item); \ 1504 struct f_uvc_opts *opts; \ 1505 struct config_item *opts_item; \ 1506 struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \ 1507 int ret; \ 1508 u8 num; \ 1509 \ 1510 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1511 \ 1512 opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\ 1513 opts = to_f_uvc_opts(opts_item); \ 1514 \ 1515 mutex_lock(&opts->lock); \ 1516 if (u->fmt.linked || opts->refcnt) { \ 1517 ret = -EBUSY; \ 1518 goto end; \ 1519 } \ 1520 \ 1521 ret = kstrtou8(page, 0, &num); \ 1522 if (ret) \ 1523 goto end; \ 1524 \ 1525 if (num > 255) { \ 1526 ret = -EINVAL; \ 1527 goto end; \ 1528 } \ 1529 u->desc.aname = num; \ 1530 ret = len; \ 1531 end: \ 1532 mutex_unlock(&opts->lock); \ 1533 mutex_unlock(su_mutex); \ 1534 return ret; \ 1535 } \ 1536 \ 1537 UVC_ATTR(uvcg_uncompressed_, cname, aname); 1538 1539 #define identity_conv(x) (x) 1540 1541 UVCG_UNCOMPRESSED_ATTR_RO(b_format_index, bFormatIndex, identity_conv); 1542 UVCG_UNCOMPRESSED_ATTR(b_bits_per_pixel, bBitsPerPixel, identity_conv); 1543 UVCG_UNCOMPRESSED_ATTR(b_default_frame_index, bDefaultFrameIndex, 1544 identity_conv); 1545 UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, identity_conv); 1546 UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, identity_conv); 1547 UVCG_UNCOMPRESSED_ATTR_RO(bm_interface_flags, bmInterfaceFlags, identity_conv); 1548 1549 #undef identity_conv 1550 1551 #undef UVCG_UNCOMPRESSED_ATTR 1552 #undef UVCG_UNCOMPRESSED_ATTR_RO 1553 1554 static inline ssize_t 1555 uvcg_uncompressed_bma_controls_show(struct config_item *item, char *page) 1556 { 1557 struct uvcg_uncompressed *unc = to_uvcg_uncompressed(item); 1558 return uvcg_format_bma_controls_show(&unc->fmt, page); 1559 } 1560 1561 static inline ssize_t 1562 uvcg_uncompressed_bma_controls_store(struct config_item *item, 1563 const char *page, size_t len) 1564 { 1565 struct uvcg_uncompressed *unc = to_uvcg_uncompressed(item); 1566 return uvcg_format_bma_controls_store(&unc->fmt, page, len); 1567 } 1568 1569 UVC_ATTR(uvcg_uncompressed_, bma_controls, bmaControls); 1570 1571 static struct configfs_attribute *uvcg_uncompressed_attrs[] = { 1572 &uvcg_uncompressed_attr_b_format_index, 1573 &uvcg_uncompressed_attr_guid_format, 1574 &uvcg_uncompressed_attr_b_bits_per_pixel, 1575 &uvcg_uncompressed_attr_b_default_frame_index, 1576 &uvcg_uncompressed_attr_b_aspect_ratio_x, 1577 &uvcg_uncompressed_attr_b_aspect_ratio_y, 1578 &uvcg_uncompressed_attr_bm_interface_flags, 1579 &uvcg_uncompressed_attr_bma_controls, 1580 NULL, 1581 }; 1582 1583 static const struct config_item_type uvcg_uncompressed_type = { 1584 .ct_item_ops = &uvcg_config_item_ops, 1585 .ct_group_ops = &uvcg_uncompressed_group_ops, 1586 .ct_attrs = uvcg_uncompressed_attrs, 1587 .ct_owner = THIS_MODULE, 1588 }; 1589 1590 static struct config_group *uvcg_uncompressed_make(struct config_group *group, 1591 const char *name) 1592 { 1593 static char guid[] = { 1594 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, 1595 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 1596 }; 1597 struct uvcg_uncompressed *h; 1598 1599 h = kzalloc(sizeof(*h), GFP_KERNEL); 1600 if (!h) 1601 return ERR_PTR(-ENOMEM); 1602 1603 h->desc.bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE; 1604 h->desc.bDescriptorType = USB_DT_CS_INTERFACE; 1605 h->desc.bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED; 1606 memcpy(h->desc.guidFormat, guid, sizeof(guid)); 1607 h->desc.bBitsPerPixel = 16; 1608 h->desc.bDefaultFrameIndex = 1; 1609 h->desc.bAspectRatioX = 0; 1610 h->desc.bAspectRatioY = 0; 1611 h->desc.bmInterfaceFlags = 0; 1612 h->desc.bCopyProtect = 0; 1613 1614 h->fmt.type = UVCG_UNCOMPRESSED; 1615 config_group_init_type_name(&h->fmt.group, name, 1616 &uvcg_uncompressed_type); 1617 1618 return &h->fmt.group; 1619 } 1620 1621 static struct configfs_group_operations uvcg_uncompressed_grp_ops = { 1622 .make_group = uvcg_uncompressed_make, 1623 }; 1624 1625 static const struct uvcg_config_group_type uvcg_uncompressed_grp_type = { 1626 .type = { 1627 .ct_item_ops = &uvcg_config_item_ops, 1628 .ct_group_ops = &uvcg_uncompressed_grp_ops, 1629 .ct_owner = THIS_MODULE, 1630 }, 1631 .name = "uncompressed", 1632 }; 1633 1634 /* ----------------------------------------------------------------------------- 1635 * streaming/mjpeg/<NAME> 1636 */ 1637 1638 struct uvcg_mjpeg { 1639 struct uvcg_format fmt; 1640 struct uvc_format_mjpeg desc; 1641 }; 1642 1643 static struct uvcg_mjpeg *to_uvcg_mjpeg(struct config_item *item) 1644 { 1645 return container_of( 1646 container_of(to_config_group(item), struct uvcg_format, group), 1647 struct uvcg_mjpeg, fmt); 1648 } 1649 1650 static struct configfs_group_operations uvcg_mjpeg_group_ops = { 1651 .make_item = uvcg_frame_make, 1652 .drop_item = uvcg_frame_drop, 1653 }; 1654 1655 #define UVCG_MJPEG_ATTR_RO(cname, aname, conv) \ 1656 static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ 1657 { \ 1658 struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \ 1659 struct f_uvc_opts *opts; \ 1660 struct config_item *opts_item; \ 1661 struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \ 1662 int result; \ 1663 \ 1664 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1665 \ 1666 opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\ 1667 opts = to_f_uvc_opts(opts_item); \ 1668 \ 1669 mutex_lock(&opts->lock); \ 1670 result = sprintf(page, "%d\n", conv(u->desc.aname)); \ 1671 mutex_unlock(&opts->lock); \ 1672 \ 1673 mutex_unlock(su_mutex); \ 1674 return result; \ 1675 } \ 1676 \ 1677 UVC_ATTR_RO(uvcg_mjpeg_, cname, aname) 1678 1679 #define UVCG_MJPEG_ATTR(cname, aname, conv) \ 1680 static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ 1681 { \ 1682 struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \ 1683 struct f_uvc_opts *opts; \ 1684 struct config_item *opts_item; \ 1685 struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \ 1686 int result; \ 1687 \ 1688 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1689 \ 1690 opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\ 1691 opts = to_f_uvc_opts(opts_item); \ 1692 \ 1693 mutex_lock(&opts->lock); \ 1694 result = sprintf(page, "%d\n", conv(u->desc.aname)); \ 1695 mutex_unlock(&opts->lock); \ 1696 \ 1697 mutex_unlock(su_mutex); \ 1698 return result; \ 1699 } \ 1700 \ 1701 static ssize_t \ 1702 uvcg_mjpeg_##cname##_store(struct config_item *item, \ 1703 const char *page, size_t len) \ 1704 { \ 1705 struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \ 1706 struct f_uvc_opts *opts; \ 1707 struct config_item *opts_item; \ 1708 struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \ 1709 int ret; \ 1710 u8 num; \ 1711 \ 1712 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1713 \ 1714 opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\ 1715 opts = to_f_uvc_opts(opts_item); \ 1716 \ 1717 mutex_lock(&opts->lock); \ 1718 if (u->fmt.linked || opts->refcnt) { \ 1719 ret = -EBUSY; \ 1720 goto end; \ 1721 } \ 1722 \ 1723 ret = kstrtou8(page, 0, &num); \ 1724 if (ret) \ 1725 goto end; \ 1726 \ 1727 if (num > 255) { \ 1728 ret = -EINVAL; \ 1729 goto end; \ 1730 } \ 1731 u->desc.aname = num; \ 1732 ret = len; \ 1733 end: \ 1734 mutex_unlock(&opts->lock); \ 1735 mutex_unlock(su_mutex); \ 1736 return ret; \ 1737 } \ 1738 \ 1739 UVC_ATTR(uvcg_mjpeg_, cname, aname) 1740 1741 #define identity_conv(x) (x) 1742 1743 UVCG_MJPEG_ATTR_RO(b_format_index, bFormatIndex, identity_conv); 1744 UVCG_MJPEG_ATTR(b_default_frame_index, bDefaultFrameIndex, 1745 identity_conv); 1746 UVCG_MJPEG_ATTR_RO(bm_flags, bmFlags, identity_conv); 1747 UVCG_MJPEG_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, identity_conv); 1748 UVCG_MJPEG_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, identity_conv); 1749 UVCG_MJPEG_ATTR_RO(bm_interface_flags, bmInterfaceFlags, identity_conv); 1750 1751 #undef identity_conv 1752 1753 #undef UVCG_MJPEG_ATTR 1754 #undef UVCG_MJPEG_ATTR_RO 1755 1756 static inline ssize_t 1757 uvcg_mjpeg_bma_controls_show(struct config_item *item, char *page) 1758 { 1759 struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); 1760 return uvcg_format_bma_controls_show(&u->fmt, page); 1761 } 1762 1763 static inline ssize_t 1764 uvcg_mjpeg_bma_controls_store(struct config_item *item, 1765 const char *page, size_t len) 1766 { 1767 struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); 1768 return uvcg_format_bma_controls_store(&u->fmt, page, len); 1769 } 1770 1771 UVC_ATTR(uvcg_mjpeg_, bma_controls, bmaControls); 1772 1773 static struct configfs_attribute *uvcg_mjpeg_attrs[] = { 1774 &uvcg_mjpeg_attr_b_format_index, 1775 &uvcg_mjpeg_attr_b_default_frame_index, 1776 &uvcg_mjpeg_attr_bm_flags, 1777 &uvcg_mjpeg_attr_b_aspect_ratio_x, 1778 &uvcg_mjpeg_attr_b_aspect_ratio_y, 1779 &uvcg_mjpeg_attr_bm_interface_flags, 1780 &uvcg_mjpeg_attr_bma_controls, 1781 NULL, 1782 }; 1783 1784 static const struct config_item_type uvcg_mjpeg_type = { 1785 .ct_item_ops = &uvcg_config_item_ops, 1786 .ct_group_ops = &uvcg_mjpeg_group_ops, 1787 .ct_attrs = uvcg_mjpeg_attrs, 1788 .ct_owner = THIS_MODULE, 1789 }; 1790 1791 static struct config_group *uvcg_mjpeg_make(struct config_group *group, 1792 const char *name) 1793 { 1794 struct uvcg_mjpeg *h; 1795 1796 h = kzalloc(sizeof(*h), GFP_KERNEL); 1797 if (!h) 1798 return ERR_PTR(-ENOMEM); 1799 1800 h->desc.bLength = UVC_DT_FORMAT_MJPEG_SIZE; 1801 h->desc.bDescriptorType = USB_DT_CS_INTERFACE; 1802 h->desc.bDescriptorSubType = UVC_VS_FORMAT_MJPEG; 1803 h->desc.bDefaultFrameIndex = 1; 1804 h->desc.bAspectRatioX = 0; 1805 h->desc.bAspectRatioY = 0; 1806 h->desc.bmInterfaceFlags = 0; 1807 h->desc.bCopyProtect = 0; 1808 1809 h->fmt.type = UVCG_MJPEG; 1810 config_group_init_type_name(&h->fmt.group, name, 1811 &uvcg_mjpeg_type); 1812 1813 return &h->fmt.group; 1814 } 1815 1816 static struct configfs_group_operations uvcg_mjpeg_grp_ops = { 1817 .make_group = uvcg_mjpeg_make, 1818 }; 1819 1820 static const struct uvcg_config_group_type uvcg_mjpeg_grp_type = { 1821 .type = { 1822 .ct_item_ops = &uvcg_config_item_ops, 1823 .ct_group_ops = &uvcg_mjpeg_grp_ops, 1824 .ct_owner = THIS_MODULE, 1825 }, 1826 .name = "mjpeg", 1827 }; 1828 1829 /* ----------------------------------------------------------------------------- 1830 * streaming/color_matching/default 1831 */ 1832 1833 #define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, conv) \ 1834 static ssize_t uvcg_default_color_matching_##cname##_show( \ 1835 struct config_item *item, char *page) \ 1836 { \ 1837 struct config_group *group = to_config_group(item); \ 1838 struct f_uvc_opts *opts; \ 1839 struct config_item *opts_item; \ 1840 struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ 1841 struct uvc_color_matching_descriptor *cd; \ 1842 int result; \ 1843 \ 1844 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ 1845 \ 1846 opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; \ 1847 opts = to_f_uvc_opts(opts_item); \ 1848 cd = &opts->uvc_color_matching; \ 1849 \ 1850 mutex_lock(&opts->lock); \ 1851 result = sprintf(page, "%d\n", conv(cd->aname)); \ 1852 mutex_unlock(&opts->lock); \ 1853 \ 1854 mutex_unlock(su_mutex); \ 1855 return result; \ 1856 } \ 1857 \ 1858 UVC_ATTR_RO(uvcg_default_color_matching_, cname, aname) 1859 1860 #define identity_conv(x) (x) 1861 1862 UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_color_primaries, bColorPrimaries, 1863 identity_conv); 1864 UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_transfer_characteristics, 1865 bTransferCharacteristics, identity_conv); 1866 UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_matrix_coefficients, bMatrixCoefficients, 1867 identity_conv); 1868 1869 #undef identity_conv 1870 1871 #undef UVCG_DEFAULT_COLOR_MATCHING_ATTR 1872 1873 static struct configfs_attribute *uvcg_default_color_matching_attrs[] = { 1874 &uvcg_default_color_matching_attr_b_color_primaries, 1875 &uvcg_default_color_matching_attr_b_transfer_characteristics, 1876 &uvcg_default_color_matching_attr_b_matrix_coefficients, 1877 NULL, 1878 }; 1879 1880 static const struct uvcg_config_group_type uvcg_default_color_matching_type = { 1881 .type = { 1882 .ct_item_ops = &uvcg_config_item_ops, 1883 .ct_attrs = uvcg_default_color_matching_attrs, 1884 .ct_owner = THIS_MODULE, 1885 }, 1886 .name = "default", 1887 }; 1888 1889 /* ----------------------------------------------------------------------------- 1890 * streaming/color_matching 1891 */ 1892 1893 static const struct uvcg_config_group_type uvcg_color_matching_grp_type = { 1894 .type = { 1895 .ct_item_ops = &uvcg_config_item_ops, 1896 .ct_owner = THIS_MODULE, 1897 }, 1898 .name = "color_matching", 1899 .children = (const struct uvcg_config_group_type*[]) { 1900 &uvcg_default_color_matching_type, 1901 NULL, 1902 }, 1903 }; 1904 1905 /* ----------------------------------------------------------------------------- 1906 * streaming/class/{fs|hs|ss} 1907 */ 1908 1909 struct uvcg_streaming_class_group { 1910 struct config_group group; 1911 const char *name; 1912 }; 1913 1914 static inline struct uvc_descriptor_header 1915 ***__uvcg_get_stream_class_arr(struct config_item *i, struct f_uvc_opts *o) 1916 { 1917 struct uvcg_streaming_class_group *group = 1918 container_of(i, struct uvcg_streaming_class_group, 1919 group.cg_item); 1920 1921 if (!strcmp(group->name, "fs")) 1922 return &o->uvc_fs_streaming_cls; 1923 1924 if (!strcmp(group->name, "hs")) 1925 return &o->uvc_hs_streaming_cls; 1926 1927 if (!strcmp(group->name, "ss")) 1928 return &o->uvc_ss_streaming_cls; 1929 1930 return NULL; 1931 } 1932 1933 enum uvcg_strm_type { 1934 UVCG_HEADER = 0, 1935 UVCG_FORMAT, 1936 UVCG_FRAME 1937 }; 1938 1939 /* 1940 * Iterate over a hierarchy of streaming descriptors' config items. 1941 * The items are created by the user with configfs. 1942 * 1943 * It "processes" the header pointed to by @priv1, then for each format 1944 * that follows the header "processes" the format itself and then for 1945 * each frame inside a format "processes" the frame. 1946 * 1947 * As a "processing" function the @fun is used. 1948 * 1949 * __uvcg_iter_strm_cls() is used in two context: first, to calculate 1950 * the amount of memory needed for an array of streaming descriptors 1951 * and second, to actually fill the array. 1952 * 1953 * @h: streaming header pointer 1954 * @priv2: an "inout" parameter (the caller might want to see the changes to it) 1955 * @priv3: an "inout" parameter (the caller might want to see the changes to it) 1956 * @fun: callback function for processing each level of the hierarchy 1957 */ 1958 static int __uvcg_iter_strm_cls(struct uvcg_streaming_header *h, 1959 void *priv2, void *priv3, 1960 int (*fun)(void *, void *, void *, int, enum uvcg_strm_type type)) 1961 { 1962 struct uvcg_format_ptr *f; 1963 struct config_group *grp; 1964 struct config_item *item; 1965 struct uvcg_frame *frm; 1966 int ret, i, j; 1967 1968 if (!fun) 1969 return -EINVAL; 1970 1971 i = j = 0; 1972 ret = fun(h, priv2, priv3, 0, UVCG_HEADER); 1973 if (ret) 1974 return ret; 1975 list_for_each_entry(f, &h->formats, entry) { 1976 ret = fun(f->fmt, priv2, priv3, i++, UVCG_FORMAT); 1977 if (ret) 1978 return ret; 1979 grp = &f->fmt->group; 1980 list_for_each_entry(item, &grp->cg_children, ci_entry) { 1981 frm = to_uvcg_frame(item); 1982 ret = fun(frm, priv2, priv3, j++, UVCG_FRAME); 1983 if (ret) 1984 return ret; 1985 } 1986 } 1987 1988 return ret; 1989 } 1990 1991 /* 1992 * Count how many bytes are needed for an array of streaming descriptors. 1993 * 1994 * @priv1: pointer to a header, format or frame 1995 * @priv2: inout parameter, accumulated size of the array 1996 * @priv3: inout parameter, accumulated number of the array elements 1997 * @n: unused, this function's prototype must match @fun in __uvcg_iter_strm_cls 1998 */ 1999 static int __uvcg_cnt_strm(void *priv1, void *priv2, void *priv3, int n, 2000 enum uvcg_strm_type type) 2001 { 2002 size_t *size = priv2; 2003 size_t *count = priv3; 2004 2005 switch (type) { 2006 case UVCG_HEADER: { 2007 struct uvcg_streaming_header *h = priv1; 2008 2009 *size += sizeof(h->desc); 2010 /* bmaControls */ 2011 *size += h->num_fmt * UVCG_STREAMING_CONTROL_SIZE; 2012 } 2013 break; 2014 case UVCG_FORMAT: { 2015 struct uvcg_format *fmt = priv1; 2016 2017 if (fmt->type == UVCG_UNCOMPRESSED) { 2018 struct uvcg_uncompressed *u = 2019 container_of(fmt, struct uvcg_uncompressed, 2020 fmt); 2021 2022 *size += sizeof(u->desc); 2023 } else if (fmt->type == UVCG_MJPEG) { 2024 struct uvcg_mjpeg *m = 2025 container_of(fmt, struct uvcg_mjpeg, fmt); 2026 2027 *size += sizeof(m->desc); 2028 } else { 2029 return -EINVAL; 2030 } 2031 } 2032 break; 2033 case UVCG_FRAME: { 2034 struct uvcg_frame *frm = priv1; 2035 int sz = sizeof(frm->dw_frame_interval); 2036 2037 *size += sizeof(frm->frame); 2038 *size += frm->frame.b_frame_interval_type * sz; 2039 } 2040 break; 2041 } 2042 2043 ++*count; 2044 2045 return 0; 2046 } 2047 2048 /* 2049 * Fill an array of streaming descriptors. 2050 * 2051 * @priv1: pointer to a header, format or frame 2052 * @priv2: inout parameter, pointer into a block of memory 2053 * @priv3: inout parameter, pointer to a 2-dimensional array 2054 */ 2055 static int __uvcg_fill_strm(void *priv1, void *priv2, void *priv3, int n, 2056 enum uvcg_strm_type type) 2057 { 2058 void **dest = priv2; 2059 struct uvc_descriptor_header ***array = priv3; 2060 size_t sz; 2061 2062 **array = *dest; 2063 ++*array; 2064 2065 switch (type) { 2066 case UVCG_HEADER: { 2067 struct uvc_input_header_descriptor *ihdr = *dest; 2068 struct uvcg_streaming_header *h = priv1; 2069 struct uvcg_format_ptr *f; 2070 2071 memcpy(*dest, &h->desc, sizeof(h->desc)); 2072 *dest += sizeof(h->desc); 2073 sz = UVCG_STREAMING_CONTROL_SIZE; 2074 list_for_each_entry(f, &h->formats, entry) { 2075 memcpy(*dest, f->fmt->bmaControls, sz); 2076 *dest += sz; 2077 } 2078 ihdr->bLength = sizeof(h->desc) + h->num_fmt * sz; 2079 ihdr->bNumFormats = h->num_fmt; 2080 } 2081 break; 2082 case UVCG_FORMAT: { 2083 struct uvcg_format *fmt = priv1; 2084 2085 if (fmt->type == UVCG_UNCOMPRESSED) { 2086 struct uvcg_uncompressed *u = 2087 container_of(fmt, struct uvcg_uncompressed, 2088 fmt); 2089 2090 u->desc.bFormatIndex = n + 1; 2091 u->desc.bNumFrameDescriptors = fmt->num_frames; 2092 memcpy(*dest, &u->desc, sizeof(u->desc)); 2093 *dest += sizeof(u->desc); 2094 } else if (fmt->type == UVCG_MJPEG) { 2095 struct uvcg_mjpeg *m = 2096 container_of(fmt, struct uvcg_mjpeg, fmt); 2097 2098 m->desc.bFormatIndex = n + 1; 2099 m->desc.bNumFrameDescriptors = fmt->num_frames; 2100 memcpy(*dest, &m->desc, sizeof(m->desc)); 2101 *dest += sizeof(m->desc); 2102 } else { 2103 return -EINVAL; 2104 } 2105 } 2106 break; 2107 case UVCG_FRAME: { 2108 struct uvcg_frame *frm = priv1; 2109 struct uvc_descriptor_header *h = *dest; 2110 2111 sz = sizeof(frm->frame); 2112 memcpy(*dest, &frm->frame, sz); 2113 *dest += sz; 2114 sz = frm->frame.b_frame_interval_type * 2115 sizeof(*frm->dw_frame_interval); 2116 memcpy(*dest, frm->dw_frame_interval, sz); 2117 *dest += sz; 2118 if (frm->fmt_type == UVCG_UNCOMPRESSED) 2119 h->bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE( 2120 frm->frame.b_frame_interval_type); 2121 else if (frm->fmt_type == UVCG_MJPEG) 2122 h->bLength = UVC_DT_FRAME_MJPEG_SIZE( 2123 frm->frame.b_frame_interval_type); 2124 } 2125 break; 2126 } 2127 2128 return 0; 2129 } 2130 2131 static int uvcg_streaming_class_allow_link(struct config_item *src, 2132 struct config_item *target) 2133 { 2134 struct config_item *streaming, *header; 2135 struct f_uvc_opts *opts; 2136 struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex; 2137 struct uvc_descriptor_header ***class_array, **cl_arr; 2138 struct uvcg_streaming_header *target_hdr; 2139 void *data, *data_save; 2140 size_t size = 0, count = 0; 2141 int ret = -EINVAL; 2142 2143 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 2144 2145 streaming = src->ci_parent->ci_parent; 2146 header = config_group_find_item(to_config_group(streaming), "header"); 2147 if (!header || target->ci_parent != header) 2148 goto out; 2149 2150 opts = to_f_uvc_opts(streaming->ci_parent); 2151 2152 mutex_lock(&opts->lock); 2153 2154 class_array = __uvcg_get_stream_class_arr(src, opts); 2155 if (!class_array || *class_array || opts->refcnt) { 2156 ret = -EBUSY; 2157 goto unlock; 2158 } 2159 2160 target_hdr = to_uvcg_streaming_header(target); 2161 ret = __uvcg_iter_strm_cls(target_hdr, &size, &count, __uvcg_cnt_strm); 2162 if (ret) 2163 goto unlock; 2164 2165 count += 2; /* color_matching, NULL */ 2166 *class_array = kcalloc(count, sizeof(void *), GFP_KERNEL); 2167 if (!*class_array) { 2168 ret = -ENOMEM; 2169 goto unlock; 2170 } 2171 2172 data = data_save = kzalloc(size, GFP_KERNEL); 2173 if (!data) { 2174 kfree(*class_array); 2175 *class_array = NULL; 2176 ret = -ENOMEM; 2177 goto unlock; 2178 } 2179 cl_arr = *class_array; 2180 ret = __uvcg_iter_strm_cls(target_hdr, &data, &cl_arr, 2181 __uvcg_fill_strm); 2182 if (ret) { 2183 kfree(*class_array); 2184 *class_array = NULL; 2185 /* 2186 * __uvcg_fill_strm() called from __uvcg_iter_stream_cls() 2187 * might have advanced the "data", so use a backup copy 2188 */ 2189 kfree(data_save); 2190 goto unlock; 2191 } 2192 *cl_arr = (struct uvc_descriptor_header *)&opts->uvc_color_matching; 2193 2194 ++target_hdr->linked; 2195 ret = 0; 2196 2197 unlock: 2198 mutex_unlock(&opts->lock); 2199 out: 2200 config_item_put(header); 2201 mutex_unlock(su_mutex); 2202 return ret; 2203 } 2204 2205 static void uvcg_streaming_class_drop_link(struct config_item *src, 2206 struct config_item *target) 2207 { 2208 struct config_item *streaming, *header; 2209 struct f_uvc_opts *opts; 2210 struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex; 2211 struct uvc_descriptor_header ***class_array; 2212 struct uvcg_streaming_header *target_hdr; 2213 2214 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 2215 2216 streaming = src->ci_parent->ci_parent; 2217 header = config_group_find_item(to_config_group(streaming), "header"); 2218 if (!header || target->ci_parent != header) 2219 goto out; 2220 2221 opts = to_f_uvc_opts(streaming->ci_parent); 2222 2223 mutex_lock(&opts->lock); 2224 2225 class_array = __uvcg_get_stream_class_arr(src, opts); 2226 if (!class_array || !*class_array) 2227 goto unlock; 2228 2229 if (opts->refcnt) 2230 goto unlock; 2231 2232 target_hdr = to_uvcg_streaming_header(target); 2233 --target_hdr->linked; 2234 kfree(**class_array); 2235 kfree(*class_array); 2236 *class_array = NULL; 2237 2238 unlock: 2239 mutex_unlock(&opts->lock); 2240 out: 2241 config_item_put(header); 2242 mutex_unlock(su_mutex); 2243 } 2244 2245 static struct configfs_item_operations uvcg_streaming_class_item_ops = { 2246 .release = uvcg_config_item_release, 2247 .allow_link = uvcg_streaming_class_allow_link, 2248 .drop_link = uvcg_streaming_class_drop_link, 2249 }; 2250 2251 static const struct config_item_type uvcg_streaming_class_type = { 2252 .ct_item_ops = &uvcg_streaming_class_item_ops, 2253 .ct_owner = THIS_MODULE, 2254 }; 2255 2256 /* ----------------------------------------------------------------------------- 2257 * streaming/class 2258 */ 2259 2260 static int uvcg_streaming_class_create_children(struct config_group *parent) 2261 { 2262 static const char * const names[] = { "fs", "hs", "ss" }; 2263 unsigned int i; 2264 2265 for (i = 0; i < ARRAY_SIZE(names); ++i) { 2266 struct uvcg_streaming_class_group *group; 2267 2268 group = kzalloc(sizeof(*group), GFP_KERNEL); 2269 if (!group) 2270 return -ENOMEM; 2271 2272 group->name = names[i]; 2273 2274 config_group_init_type_name(&group->group, group->name, 2275 &uvcg_streaming_class_type); 2276 configfs_add_default_group(&group->group, parent); 2277 } 2278 2279 return 0; 2280 } 2281 2282 static const struct uvcg_config_group_type uvcg_streaming_class_grp_type = { 2283 .type = { 2284 .ct_item_ops = &uvcg_config_item_ops, 2285 .ct_owner = THIS_MODULE, 2286 }, 2287 .name = "class", 2288 .create_children = uvcg_streaming_class_create_children, 2289 }; 2290 2291 /* ----------------------------------------------------------------------------- 2292 * streaming 2293 */ 2294 2295 static ssize_t uvcg_default_streaming_b_interface_number_show( 2296 struct config_item *item, char *page) 2297 { 2298 struct config_group *group = to_config_group(item); 2299 struct mutex *su_mutex = &group->cg_subsys->su_mutex; 2300 struct config_item *opts_item; 2301 struct f_uvc_opts *opts; 2302 int result = 0; 2303 2304 mutex_lock(su_mutex); /* for navigating configfs hierarchy */ 2305 2306 opts_item = item->ci_parent; 2307 opts = to_f_uvc_opts(opts_item); 2308 2309 mutex_lock(&opts->lock); 2310 result += sprintf(page, "%u\n", opts->streaming_interface); 2311 mutex_unlock(&opts->lock); 2312 2313 mutex_unlock(su_mutex); 2314 2315 return result; 2316 } 2317 2318 UVC_ATTR_RO(uvcg_default_streaming_, b_interface_number, bInterfaceNumber); 2319 2320 static struct configfs_attribute *uvcg_default_streaming_attrs[] = { 2321 &uvcg_default_streaming_attr_b_interface_number, 2322 NULL, 2323 }; 2324 2325 static const struct uvcg_config_group_type uvcg_streaming_grp_type = { 2326 .type = { 2327 .ct_item_ops = &uvcg_config_item_ops, 2328 .ct_attrs = uvcg_default_streaming_attrs, 2329 .ct_owner = THIS_MODULE, 2330 }, 2331 .name = "streaming", 2332 .children = (const struct uvcg_config_group_type*[]) { 2333 &uvcg_streaming_header_grp_type, 2334 &uvcg_uncompressed_grp_type, 2335 &uvcg_mjpeg_grp_type, 2336 &uvcg_color_matching_grp_type, 2337 &uvcg_streaming_class_grp_type, 2338 NULL, 2339 }, 2340 }; 2341 2342 /* ----------------------------------------------------------------------------- 2343 * UVC function 2344 */ 2345 2346 static void uvc_func_item_release(struct config_item *item) 2347 { 2348 struct f_uvc_opts *opts = to_f_uvc_opts(item); 2349 2350 uvcg_config_remove_children(to_config_group(item)); 2351 usb_put_function_instance(&opts->func_inst); 2352 } 2353 2354 static struct configfs_item_operations uvc_func_item_ops = { 2355 .release = uvc_func_item_release, 2356 }; 2357 2358 #define UVCG_OPTS_ATTR(cname, aname, conv, str2u, uxx, vnoc, limit) \ 2359 static ssize_t f_uvc_opts_##cname##_show( \ 2360 struct config_item *item, char *page) \ 2361 { \ 2362 struct f_uvc_opts *opts = to_f_uvc_opts(item); \ 2363 int result; \ 2364 \ 2365 mutex_lock(&opts->lock); \ 2366 result = sprintf(page, "%d\n", conv(opts->cname)); \ 2367 mutex_unlock(&opts->lock); \ 2368 \ 2369 return result; \ 2370 } \ 2371 \ 2372 static ssize_t \ 2373 f_uvc_opts_##cname##_store(struct config_item *item, \ 2374 const char *page, size_t len) \ 2375 { \ 2376 struct f_uvc_opts *opts = to_f_uvc_opts(item); \ 2377 int ret; \ 2378 uxx num; \ 2379 \ 2380 mutex_lock(&opts->lock); \ 2381 if (opts->refcnt) { \ 2382 ret = -EBUSY; \ 2383 goto end; \ 2384 } \ 2385 \ 2386 ret = str2u(page, 0, &num); \ 2387 if (ret) \ 2388 goto end; \ 2389 \ 2390 if (num > limit) { \ 2391 ret = -EINVAL; \ 2392 goto end; \ 2393 } \ 2394 opts->cname = vnoc(num); \ 2395 ret = len; \ 2396 end: \ 2397 mutex_unlock(&opts->lock); \ 2398 return ret; \ 2399 } \ 2400 \ 2401 UVC_ATTR(f_uvc_opts_, cname, cname) 2402 2403 #define identity_conv(x) (x) 2404 2405 UVCG_OPTS_ATTR(streaming_interval, streaming_interval, identity_conv, 2406 kstrtou8, u8, identity_conv, 16); 2407 UVCG_OPTS_ATTR(streaming_maxpacket, streaming_maxpacket, le16_to_cpu, 2408 kstrtou16, u16, le16_to_cpu, 3072); 2409 UVCG_OPTS_ATTR(streaming_maxburst, streaming_maxburst, identity_conv, 2410 kstrtou8, u8, identity_conv, 15); 2411 2412 #undef identity_conv 2413 2414 #undef UVCG_OPTS_ATTR 2415 2416 static struct configfs_attribute *uvc_attrs[] = { 2417 &f_uvc_opts_attr_streaming_interval, 2418 &f_uvc_opts_attr_streaming_maxpacket, 2419 &f_uvc_opts_attr_streaming_maxburst, 2420 NULL, 2421 }; 2422 2423 static const struct uvcg_config_group_type uvc_func_type = { 2424 .type = { 2425 .ct_item_ops = &uvc_func_item_ops, 2426 .ct_attrs = uvc_attrs, 2427 .ct_owner = THIS_MODULE, 2428 }, 2429 .name = "", 2430 .children = (const struct uvcg_config_group_type*[]) { 2431 &uvcg_control_grp_type, 2432 &uvcg_streaming_grp_type, 2433 NULL, 2434 }, 2435 }; 2436 2437 int uvcg_attach_configfs(struct f_uvc_opts *opts) 2438 { 2439 int ret; 2440 2441 config_group_init_type_name(&opts->func_inst.group, uvc_func_type.name, 2442 &uvc_func_type.type); 2443 2444 ret = uvcg_config_create_children(&opts->func_inst.group, 2445 &uvc_func_type); 2446 if (ret < 0) 2447 config_group_put(&opts->func_inst.group); 2448 2449 return ret; 2450 } 2451