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