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