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