Newer
Older
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
index 519b012..4eaa563 100644
--- a/src/function/ms.c
+++ b/src/function/ms.c
@@ -64,6 +64,7 @@ static struct {
[USBG_F_MS_LUN_NOFUA] = MS_LUN_BOOL_ATTR(nofua),
[USBG_F_MS_LUN_REMOVABLE] = MS_LUN_BOOL_ATTR(removable),
[USBG_F_MS_LUN_FILE] = MS_LUN_STRING_ATTR(file),
+ [USBG_F_MS_LUN_INQUIRY_STRING] = MS_LUN_STRING_ATTR(inquiry_string),
};
static inline int lun_select(const struct dirent *dent)
diff --git a/src/function/printer.c b/src/function/printer.c
new file mode 100644
index 0000000..7270dd6
--- /dev/null
+++ b/src/function/printer.c
@@ -0,0 +1,34 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+
+#include "usbg/usbg.h"
+#include "usbg/usbg_internal.h"
+
+#ifdef HAS_GADGET_SCHEMES
+#include <libconfig.h>
+#endif
+
+struct usbg_f_printer {
+ struct usbg_function func;
+};
+
+GENERIC_ALLOC_INST(printer, struct usbg_f_printer, func);
+
+GENERIC_FREE_INST(printer, struct usbg_f_printer, func);
+
+#define PRINTER_FUNCTION_OPTS \
+ .alloc_inst = printer_alloc_inst, \
+ .free_inst = printer_free_inst
+
+struct usbg_function_type usbg_f_type_printer = {
+ .name = "printer",
+ PRINTER_FUNCTION_OPTS};
diff --git a/src/function/uac2.c b/src/function/uac2.c
index f2c1a49..9ca8b66 100644
--- a/src/function/uac2.c
+++ b/src/function/uac2.c
@@ -23,6 +23,26 @@ struct usbg_f_uac2 {
struct usbg_function func;
};
+#define UAC2_BOOL_ATTR(_name) \
+ { \
+ .name = #_name, \
+ .offset = offsetof(struct usbg_f_uac2_attrs, _name), \
+ .get = usbg_get_bool, \
+ .set = usbg_set_bool, \
+ .import = usbg_get_config_node_bool, \
+ .export = usbg_set_config_node_bool, \
+ }
+
+#define UAC2_STRING_ATTR(_name) \
+ { \
+ .name = #_name, \
+ .offset = offsetof(struct usbg_f_uac2_attrs, _name), \
+ .get = usbg_get_string, \
+ .set = usbg_set_string, \
+ .export = usbg_set_config_node_string, \
+ .import = usbg_get_config_node_string, \
+ }
+
#define UAC2_DEC_ATTR(_name) \
{ \
.name = #_name, \
@@ -47,6 +67,22 @@ static struct {
[USBG_F_UAC2_P_CHMASK] = UAC2_DEC_ATTR(p_chmask),
[USBG_F_UAC2_P_SRATE] = UAC2_DEC_ATTR(p_srate),
[USBG_F_UAC2_P_SSIZE] = UAC2_DEC_ATTR(p_ssize),
+ [USBG_F_UAC2_P_HS_BINT] = UAC2_DEC_ATTR(p_hs_bint),
+ [USBG_F_UAC2_C_HS_BINT] = UAC2_DEC_ATTR(c_hs_bint),
+ [USBG_F_UAC2_C_SYNC] = UAC2_STRING_ATTR(c_sync),
+ [USBG_F_UAC2_REQ_NUMBER] = UAC2_DEC_ATTR(req_number),
+ [USBG_F_UAC2_FB_MAX] = UAC2_DEC_ATTR(fb_max),
+ [USBG_F_UAC2_P_MUTE_PRESENT] = UAC2_BOOL_ATTR(p_mute_present),
+ [USBG_F_UAC2_P_VOLUME_PRESENT] = UAC2_BOOL_ATTR(p_volume_present),
+ [USBG_F_UAC2_P_VOLUME_MIN] = UAC2_DEC_ATTR(p_volume_min),
+ [USBG_F_UAC2_P_VOLUME_MAX] = UAC2_DEC_ATTR(p_volume_max),
+ [USBG_F_UAC2_P_VOLUME_RES] = UAC2_DEC_ATTR(p_volume_res),
+ [USBG_F_UAC2_C_MUTE_PRESENT] = UAC2_BOOL_ATTR(c_mute_present),
+ [USBG_F_UAC2_C_VOLUME_PRESENT] = UAC2_BOOL_ATTR(c_volume_present),
+ [USBG_F_UAC2_C_VOLUME_MIN] = UAC2_DEC_ATTR(c_volume_min),
+ [USBG_F_UAC2_C_VOLUME_MAX] = UAC2_DEC_ATTR(c_volume_max),
+ [USBG_F_UAC2_C_VOLUME_RES] = UAC2_DEC_ATTR(c_volume_res),
+ [USBG_F_UAC2_FUNCTION_NAME] = UAC2_STRING_ATTR(function_name),
};
#undef UAC2_DEC_ATTR
diff --git a/src/function/uvc.c b/src/function/uvc.c
new file mode 100644
index 0000000..f39594b
--- /dev/null
+++ b/src/function/uvc.c
@@ -0,0 +1,1219 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+
+#include "usbg/usbg.h"
+#include "usbg/usbg_internal.h"
+#include "usbg/function/uvc.h"
+
+#include <errno.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <malloc.h>
+#include <ftw.h>
+#ifdef HAS_GADGET_SCHEMES
+#include <libconfig.h>
+#endif
+
+#define UVC_PATH_CONTROL "control"
+#define UVC_PATH_HEADER "header/h"
+#define UVC_PATH_CLASS_FS "class/fs/h"
+#define UVC_PATH_CLASS_HS "class/hs/h"
+#define UVC_PATH_CLASS_SS "class/ss/h"
+#define UVC_PATH_STREAMING "streaming"
+#define UVC_PATH_STREAMING_UNCOMPRESSED "uncompressed/u"
+#define UVC_PATH_STREAMING_MJPEG "mjpeg/m"
+
+#define MAX_FRAMES 16
+#define MAX_FORMATS 2
+
+const char * format_names[MAX_FORMATS] = {
+ UVC_PATH_STREAMING_MJPEG,
+ UVC_PATH_STREAMING_UNCOMPRESSED,
+};
+
+struct formats {
+ bool frames[MAX_FRAMES];
+ bool frames_initiated;
+};
+
+struct usbg_f_uvc
+{
+ struct usbg_function func;
+ struct formats formats[MAX_FORMATS];
+ bool formats_initiated;
+};
+
+#define UVC_DEC_ATTR(_name) \
+ { \
+ .name = #_name, \
+ .offset = offsetof(struct usbg_f_uvc_config_attrs, _name), \
+ .get = usbg_get_dec, \
+ .set = usbg_set_dec, \
+ .import = usbg_get_config_node_int, \
+ .export = usbg_set_config_node_int, \
+ }
+
+#define UVC_STRING_ATTR(_name) \
+ { \
+ .name = #_name, \
+ .offset = offsetof(struct usbg_f_uvc_config_attrs, _name), \
+ .get = usbg_get_string, \
+ .set = usbg_set_string, \
+ .export = usbg_set_config_node_string, \
+ .import = usbg_get_config_node_string, \
+ }
+
+struct {
+ const char *name;
+ size_t offset;
+ usbg_attr_get_func get;
+ usbg_attr_set_func set;
+ usbg_import_node_func import;
+ usbg_export_node_func export;
+} uvc_config_attr[USBG_F_UVC_CONFIG_ATTR_MAX] = {
+ [USBG_F_UVC_CONFIG_MAXBURST] = UVC_DEC_ATTR(streaming_maxburst),
+ [USBG_F_UVC_CONFIG_MAXPACKET] = UVC_DEC_ATTR(streaming_maxpacket),
+ [USBG_F_UVC_CONFIG_INTERVAL] = UVC_DEC_ATTR(streaming_interval),
+ [USBG_F_UVC_CONFIG_FUNCTION_NAME] = UVC_STRING_ATTR(function_name),
+};
+
+#undef UVC_DEC_ATTR
+
+#define UVC_DEC_ATTR(_name) \
+ { \
+ .name = #_name, \
+ .offset = offsetof(struct usbg_f_uvc_frame_attrs, _name), \
+ .get = usbg_get_dec, \
+ .set = usbg_set_dec, \
+ .import = usbg_get_config_node_int, \
+ .export = usbg_set_config_node_int, \
+ }
+
+struct {
+ const char *name;
+ size_t offset;
+ usbg_attr_get_func get;
+ usbg_attr_set_func set;
+ usbg_import_node_func import;
+ usbg_export_node_func export;
+} uvc_frame_attr[USBG_F_UVC_FRAME_ATTR_MAX] = {
+ [USBG_F_UVC_FRAME_INDEX] = UVC_DEC_ATTR(bFrameIndex),
+ [USBG_F_UVC_FRAME_CAPABILITIES] = UVC_DEC_ATTR(bmCapabilities),
+ [USBG_F_UVC_FRAME_MIN_BITRATE] = UVC_DEC_ATTR(dwMinBitRate),
+ [USBG_F_UVC_FRAME_MAX_BITRATE] = UVC_DEC_ATTR(dwMaxBitRate),
+ [USBG_F_UVC_FRAME_MAX_VIDEO_BUFFERSIZE] = UVC_DEC_ATTR(dwMaxVideoFrameBufferSize),
+ [USBG_F_UVC_FRAME_DEFAULT_INTERVAL] = UVC_DEC_ATTR(dwDefaultFrameInterval),
+ [USBG_F_UVC_FRAME_INTERVAL] = UVC_DEC_ATTR(dwFrameInterval),
+ [USBG_F_UVC_FRAME_HEIGHT] = UVC_DEC_ATTR(wHeight),
+ [USBG_F_UVC_FRAME_WIDTH] = UVC_DEC_ATTR(wWidth),
+};
+
+#undef UVC_DEC_ATTR
+
+#define UVC_DEC_ATTR(_name) \
+ { \
+ .name = #_name, \
+ .offset = offsetof(struct usbg_f_uvc_format_attrs, _name), \
+ .get = usbg_get_dec, \
+ .set = usbg_set_dec, \
+ .import = usbg_get_config_node_int, \
+ .export = usbg_set_config_node_int, \
+ }
+
+#define UVC_DEC_ATTR_RO(_name) \
+ { \
+ .name = #_name, \
+ .ro = true, \
+ .offset = offsetof(struct usbg_f_uvc_format_attrs, _name), \
+ .get = usbg_get_dec, \
+ .export = usbg_set_config_node_int, \
+ }
+
+struct {
+ const char *name;
+ size_t offset;
+ bool ro;
+ usbg_attr_get_func get;
+ usbg_attr_set_func set;
+ usbg_import_node_func import;
+ usbg_export_node_func export;
+} uvc_format_attr[USBG_F_UVC_FORMAT_ATTR_MAX] = {
+ [USBG_F_UVC_FORMAT_CONTROLS] = UVC_DEC_ATTR_RO(bmaControls),
+ [USBG_F_UVC_FORMAT_INTERFACE_FLAGS] = UVC_DEC_ATTR_RO(bmInterfaceFlags),
+ [USBG_F_UVC_FORMAT_ASPECTRATIO_X] = UVC_DEC_ATTR_RO(bAspectRatioX),
+ [USBG_F_UVC_FORMAT_ASPECTRATIO_Y] = UVC_DEC_ATTR_RO(bAspectRatioY),
+ [USBG_F_UVC_FORMAT_DEFAULT_FRAME_INDEX] = UVC_DEC_ATTR(bDefaultFrameIndex),
+ [USBG_F_UVC_FORMAT_FORMAT_INDEX] = UVC_DEC_ATTR_RO(bFormatIndex),
+};
+
+
+#undef UVC_DEC_ATTR
+#undef UVC_DEC_ATTR_RO
+
+static inline int frame_select(const struct dirent *dent)
+{
+ int ret;
+ int id;
+
+ ret = file_select(dent);
+ if (!ret)
+ goto out;
+
+ ret = sscanf(dent->d_name, "frame.%d", &id);
+out:
+ return ret;
+}
+
+static inline int frame_sort(const struct dirent **d1, const struct dirent **d2)
+{
+ int ret;
+ int id1, id2;
+
+ ret = sscanf((*d1)->d_name, "frame.%d", &id1);
+ if (ret != 1)
+ goto err;
+
+ ret = sscanf((*d2)->d_name, "frame.%d", &id2);
+ if (ret != 1)
+ goto err;
+
+ return id1 < id2 ? -1 : id1 > id2;
+err:
+ /*
+ * This should not happened because dentries has been
+ * already checked by frame_select function. This
+ * error procedure is just in case.
+ */
+ return -1;
+}
+
+int init_frames(struct usbg_f_uvc *uvc, int j)
+{
+ struct dirent **dent;
+ char fpath[USBG_MAX_PATH_LENGTH];
+ int nmb, i, id, ret = 0;
+
+ nmb = snprintf(fpath, sizeof(fpath), "%s/%s/" UVC_PATH_STREAMING "/%s/",
+ uvc->func.path, uvc->func.name, format_names[j]);
+ if (nmb >= sizeof(fpath)) {
+ ret = USBG_ERROR_PATH_TOO_LONG;
+ return ret;
+ }
+
+ nmb = scandir(fpath, &dent, frame_select, frame_sort);
+ if (nmb < 0) {
+ ret = usbg_translate_error(errno);
+ return ret;
+ }
+
+ for (i = 0; i < nmb; ++i) {
+ /* don't check the error as we know that name is valid */
+ sscanf(dent[i]->d_name, "frame.%d", &id);
+ uvc->formats[j].frames[id] = true;
+ free(dent[i]);
+ }
+ free(dent);
+
+ uvc->formats[j].frames_initiated = true;
+
+ return 0;
+}
+
+static inline struct formats *get_formats_mask(struct usbg_f_uvc *uvc)
+{
+ int i, ret;
+
+ if (!uvc->formats_initiated) {
+ uvc->formats_initiated = true;
+ for (i = 0; i < MAX_FORMATS; ++i) {
+ ret = init_frames(uvc, i);
+ if (ret)
+ uvc->formats_initiated = false;
+ }
+ }
+
+ return uvc->formats;
+}
+
+GENERIC_ALLOC_INST(uvc_internal, struct usbg_f_uvc, func);
+static int uvc_alloc_inst(struct usbg_function_type *type,
+ usbg_function_type type_code,
+ const char *instance, const char *path,
+ struct usbg_gadget *parent,
+ struct usbg_function **f)
+{
+ struct usbg_f_uvc *uvcf;
+ int ret;
+
+ ret = uvc_internal_alloc_inst(type, type_code, instance,
+ path, parent, f);
+ if (ret)
+ goto out;
+
+ uvcf = usbg_to_uvc_function(*f);
+ if (!uvcf)
+ return -ENOMEM;
+
+ memset(uvcf->formats, 0, sizeof(uvcf->formats));
+ uvcf->formats_initiated = false;
+out:
+ return ret;
+}
+
+GENERIC_FREE_INST(uvc, struct usbg_f_uvc, func);
+
+static int uvc_set_attrs(struct usbg_function *f, void *f_attrs)
+{
+ return usbg_f_uvc_set_attrs(usbg_to_uvc_function(f), f_attrs);
+}
+
+static int uvc_get_attrs(struct usbg_function *f, void *f_attrs)
+{
+ return usbg_f_uvc_get_attrs(usbg_to_uvc_function(f), f_attrs);
+}
+
+static void uvc_cleanup_attrs(struct usbg_function *f, void *f_attrs)
+{
+ return usbg_f_uvc_cleanup_attrs(f_attrs);
+}
+
+int usbg_f_uvc_get_config_attr_val(usbg_f_uvc *uvcf, enum usbg_f_uvc_config_attr iattr,
+ union usbg_f_uvc_config_attr_val *val)
+{
+ char ipath[USBG_MAX_PATH_LENGTH];
+ int nmb;
+
+ nmb = snprintf(ipath, sizeof(ipath), "%s/%s/",
+ uvcf->func.path, uvcf->func.name);
+ if (nmb >= sizeof(ipath))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+
+ return uvc_config_attr[iattr].get(ipath, "",
+ uvc_config_attr[iattr].name, val);
+}
+
+int usbg_f_uvc_set_config_attr_val(usbg_f_uvc *uvcf, enum usbg_f_uvc_config_attr iattr,
+ union usbg_f_uvc_config_attr_val val)
+{
+ char ipath[USBG_MAX_PATH_LENGTH];
+ int nmb;
+
+ nmb = snprintf(ipath, sizeof(ipath), "%s/%s/",
+ uvcf->func.path, uvcf->func.name);
+ if (nmb >= sizeof(ipath))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ return uvc_config_attr[iattr].set(ipath, "",
+ uvc_config_attr[iattr].name, &val);
+}
+
+int usbg_f_uvc_get_config_attrs(usbg_f_uvc *uvcf, struct usbg_f_uvc_config_attrs *iattrs)
+{
+ int i;
+ int ret = 0;
+
+ for (i = USBG_F_UVC_CONFIG_ATTR_MIN; i < USBG_F_UVC_CONFIG_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_get_config_attr_val(uvcf, i,
+ (union usbg_f_uvc_config_attr_val *)
+ ((char *)iattrs
+ + uvc_config_attr[i].offset));
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+int usbg_f_uvc_set_config_attrs(usbg_f_uvc *uvcf, const struct usbg_f_uvc_config_attrs *iattrs)
+{
+ int i;
+ int ret = 0;
+
+ for (i = USBG_F_UVC_FRAME_ATTR_MIN; i < USBG_F_UVC_FRAME_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_set_config_attr_val(uvcf, i,
+ *(union usbg_f_uvc_config_attr_val *)
+ ((char *)iattrs
+ + uvc_config_attr[i].offset));
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+int usbg_f_uvc_get_frame_attr_val(usbg_f_uvc *uvcf, const char* format, int frame_id,
+ enum usbg_f_uvc_frame_attr fattr,
+ union usbg_f_uvc_frame_attr_val *val)
+{
+ char fpath[USBG_MAX_PATH_LENGTH];
+ int nmb;
+
+ nmb = snprintf(fpath, sizeof(fpath), "%s/%s/" UVC_PATH_STREAMING "/%s/frame.%d/",
+ uvcf->func.path, uvcf->func.name, format, frame_id);
+ if (nmb >= sizeof(fpath))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ return uvc_frame_attr[fattr].get(fpath, "",
+ uvc_frame_attr[fattr].name, val);
+}
+
+int usbg_f_uvc_set_frame_attr_val(usbg_f_uvc *uvcf, const char* format, int frame_id,
+ enum usbg_f_uvc_frame_attr fattr,
+ union usbg_f_uvc_frame_attr_val val)
+{
+ char fpath[USBG_MAX_PATH_LENGTH];
+ int nmb;
+
+ nmb = snprintf(fpath, sizeof(fpath), "%s/%s/" UVC_PATH_STREAMING "/%s/frame.%d/",
+ uvcf->func.path, uvcf->func.name, format, frame_id);
+ if (nmb >= sizeof(fpath))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ return uvc_frame_attr[fattr].set(fpath, "",
+ uvc_frame_attr[fattr].name, &val);
+}
+
+int usbg_f_uvc_get_frame_attrs(usbg_f_uvc *uvcf, const char* format, int frame_id,
+ struct usbg_f_uvc_frame_attrs *fattrs)
+{
+ int i;
+ int ret = 0;
+
+ for (i = USBG_F_UVC_FRAME_ATTR_MIN; i < USBG_F_UVC_FRAME_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_get_frame_attr_val(uvcf, format, frame_id, i,
+ (union usbg_f_uvc_frame_attr_val *)
+ ((char *)fattrs
+ + uvc_frame_attr[i].offset));
+ if (ret)
+ break;
+ }
+
+ fattrs->bFrameIndex = frame_id;
+
+ return ret;
+}
+
+int usbg_f_uvc_set_frame_attrs(usbg_f_uvc *uvcf, const char* format, int frame_id,
+ const struct usbg_f_uvc_frame_attrs *fattrs)
+{
+ int i;
+ int ret = 0;
+
+ for (i = USBG_F_UVC_FRAME_ATTR_MIN; i < USBG_F_UVC_FRAME_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_set_frame_attr_val(uvcf, format, frame_id, i,
+ *(union usbg_f_uvc_frame_attr_val *)
+ ((char *)fattrs
+ + uvc_frame_attr[i].offset));
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+int usbg_f_uvc_get_format_attr_val(usbg_f_uvc *uvcf, const char* format,
+ enum usbg_f_uvc_format_attr fattr,
+ union usbg_f_uvc_format_attr_val *val)
+{
+ char fpath[USBG_MAX_PATH_LENGTH];
+ int nmb;
+
+ nmb = snprintf(fpath, sizeof(fpath), "%s/%s/" UVC_PATH_STREAMING "/%s/",
+ uvcf->func.path, uvcf->func.name, format);
+ if (nmb >= sizeof(fpath))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ return uvc_format_attr[fattr].get(fpath, "",
+ uvc_format_attr[fattr].name, val);
+}
+
+int usbg_f_uvc_set_format_attr_val(usbg_f_uvc *uvcf, const char* format,
+ enum usbg_f_uvc_format_attr fattr,
+ union usbg_f_uvc_format_attr_val val)
+{
+ char fpath[USBG_MAX_PATH_LENGTH];
+ int nmb;
+
+ nmb = snprintf(fpath, sizeof(fpath), "%s/%s/" UVC_PATH_STREAMING "/%s/",
+ uvcf->func.path, uvcf->func.name, format);
+ if (nmb >= sizeof(fpath))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ return uvc_format_attr[fattr].set(fpath, "",
+ uvc_format_attr[fattr].name, &val);
+}
+
+int usbg_f_uvc_get_format_attrs(usbg_f_uvc *uvcf, const char* format,
+ struct usbg_f_uvc_format_attrs *fattrs)
+{
+ int i;
+ int ret = 0;
+
+ for (i = USBG_F_UVC_FORMAT_ATTR_MIN; i < USBG_F_UVC_FORMAT_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_get_format_attr_val(uvcf, format, i,
+ (union usbg_f_uvc_format_attr_val *)
+ ((char *)fattrs
+ + uvc_format_attr[i].offset));
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+int usbg_f_uvc_set_format_attrs(usbg_f_uvc *uvcf, const char* format,
+ const struct usbg_f_uvc_format_attrs *fattrs)
+{
+ int i;
+ int ret = 0;
+
+ for (i = USBG_F_UVC_FORMAT_ATTR_MIN; i < USBG_F_UVC_FORMAT_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_set_format_attr_val(uvcf, format, i,
+ *(union usbg_f_uvc_format_attr_val *)
+ ((char *)fattrs
+ + uvc_format_attr[i].offset));
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+static int uvc_create_dir(const char *path)
+{
+ char tmp[USBG_MAX_PATH_LENGTH];
+ int nmb, ret = USBG_SUCCESS;
+ char *p = NULL;
+ size_t len;
+
+ nmb = snprintf(tmp, sizeof(tmp), "%s", path);
+ if(nmb >= sizeof(tmp))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ len = strlen(tmp);
+ if(tmp[len - 1] == '/')
+ tmp[len - 1] = 0;
+
+ for (p = tmp + 1; *p; p++) {
+ if(*p == '/') {
+ *p = 0;
+ if((mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) != 0) && errno != EEXIST) {
+ ret = usbg_translate_error(errno);
+ break;
+ }
+ *p = '/';
+ }
+ }
+ if(ret != USBG_SUCCESS)
+ return ret;
+
+ if((mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) != 0) && errno != EEXIST)
+ return usbg_translate_error(errno);
+
+ return ret;
+}
+
+static int uvc_link(char *path, char *to, char *from)
+{
+ char oldname[USBG_MAX_PATH_LENGTH];
+ char newname[USBG_MAX_PATH_LENGTH];
+ int ret = USBG_SUCCESS;
+ int nmb;
+
+ nmb = snprintf(oldname, sizeof(oldname), "%s/%s", path, to);
+ if (nmb >= sizeof(oldname))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ nmb = snprintf(newname, sizeof(newname), "%s/%s", path, from);
+ if (nmb >= sizeof(newname))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ if(symlink(oldname, newname))
+ return usbg_translate_error(errno);
+
+ return ret;
+}
+
+static int uvc_set_class(usbg_f_uvc *uvcf, char *cs)
+{
+ char path[USBG_MAX_PATH_LENGTH];
+ char header_path[USBG_MAX_PATH_LENGTH];
+ int nmb;
+ int ret;
+
+ nmb = snprintf(path, sizeof(path), "%s/%s/%s", uvcf->func.path, uvcf->func.name, cs);
+ if (nmb >= sizeof(path))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ nmb = snprintf(header_path, sizeof(header_path), "%s/" UVC_PATH_HEADER, path);
+ if (nmb >= sizeof(header_path))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ ret = uvc_create_dir(header_path);
+ if (ret != USBG_SUCCESS)
+ return ret;
+
+ if (!strncmp(cs, UVC_PATH_STREAMING, strlen(UVC_PATH_STREAMING))) {
+ char check_path[USBG_MAX_PATH_LENGTH];
+ struct stat buffer;
+
+ nmb = snprintf(check_path, sizeof(check_path), "%s/" UVC_PATH_STREAMING_UNCOMPRESSED, path);
+ if (nmb >= sizeof(check_path))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ ret = stat(check_path, &buffer);
+ if (!ret) {
+ ret = uvc_link(path, UVC_PATH_STREAMING_UNCOMPRESSED, "header/h/u");
+ if (ret != USBG_SUCCESS)
+ return ret;
+ }
+
+ nmb = snprintf(check_path, sizeof(check_path), "%s/" UVC_PATH_STREAMING_MJPEG, path);
+ if (nmb >= sizeof(check_path))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ ret = stat(check_path, &buffer);
+ if (!ret) {
+ ret = uvc_link(path, UVC_PATH_STREAMING_MJPEG, "header/h/m");
+ if (ret != USBG_SUCCESS)
+ return ret;
+ }
+
+ ret = uvc_link(path, UVC_PATH_HEADER, UVC_PATH_CLASS_HS);
+ if (ret)
+ return ret;
+ }
+
+ ret = uvc_link(path, UVC_PATH_HEADER, UVC_PATH_CLASS_FS);
+ if (ret)
+ return ret;
+
+ return uvc_link(path, UVC_PATH_HEADER, UVC_PATH_CLASS_SS);
+}
+
+#ifdef HAS_GADGET_SCHEMES
+
+int usbg_f_uvc_get_nframes(const bool *frames)
+{
+ int nframes = 0;
+ int i;
+
+ if (!frames)
+ return USBG_ERROR_INVALID_PARAM;
+
+ for (i = 0; i < MAX_FRAMES; ++i)
+ nframes += frames[i] ? 1 : 0;
+
+ return nframes;
+}
+
+int usbg_f_uvc_create_frame(usbg_f_uvc *uvcf, const char* format, bool *frames,
+ int frame_id, struct usbg_f_uvc_frame_attrs *fattrs)
+{
+ char frame_path[USBG_MAX_PATH_LENGTH];
+ int nmb;
+ int ret;
+
+ if (frame_id >= MAX_FRAMES)
+ return USBG_ERROR_INVALID_PARAM;
+
+ if (frames[frame_id])
+ return USBG_ERROR_EXIST;
+
+ nmb = snprintf(frame_path, sizeof(frame_path), "%s/%s/" UVC_PATH_STREAMING "/%s/frame.%d/",
+ uvcf->func.path, uvcf->func.name, format, frame_id);
+ if (nmb >= sizeof(frame_path))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ ret = uvc_create_dir(frame_path);
+ if (ret)
+ return usbg_translate_error(errno);
+
+ if (fattrs) {
+ ret = usbg_f_uvc_set_frame_attrs(uvcf, format, frame_id, fattrs);
+ if (ret)
+ goto remove_frame;
+ }
+
+ frames[frame_id] = true;
+ return 0;
+
+remove_frame:
+ rmdir(frame_path);
+ return ret;
+}
+
+static int uvc_import_frame_attrs(struct usbg_f_uvc *uvcf, const char* format,
+ int frame_id, config_setting_t *root)
+{
+ union usbg_f_uvc_frame_attr_val val;
+ int ret = 0;
+ int i;
+
+ for (i = USBG_F_UVC_FRAME_ATTR_MIN; i < USBG_F_UVC_FRAME_ATTR_MAX; ++i) {
+ ret = uvc_frame_attr[i].import(root, uvc_frame_attr[i].name, &val);
+ /* node not found */
+ if (ret == 0)
+ continue;
+ /* error */
+ if (ret < 0)
+ break;
+
+ ret = usbg_f_uvc_set_frame_attr_val(uvcf, format, frame_id, i, val);
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+static int uvc_import_format_attrs(struct usbg_f_uvc *uvcf, const char* format,
+ config_setting_t *root)
+{
+ union usbg_f_uvc_format_attr_val val;
+ int ret = 0;
+ int i;
+
+ for (i = USBG_F_UVC_FORMAT_ATTR_MIN; i < USBG_F_UVC_FORMAT_ATTR_MAX; ++i) {
+ if (uvc_format_attr[i].ro)
+ continue;
+
+ ret = uvc_format_attr[i].import(root, uvc_format_attr[i].name, &val);
+ /* node not found */
+ if (ret == 0)
+ continue;
+ /* error */
+ if (ret < 0)
+ break;
+
+ ret = usbg_f_uvc_set_format_attr_val(uvcf, format, i, val);
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+static int uvc_import_format(struct usbg_f_uvc *uvcf, const char *format, bool *frames, config_setting_t *root)
+{
+ config_setting_t *frames_node, *node;
+ int i, nframes;
+ int ret = 0;
+
+ frames_node = config_setting_get_member(root, "frames");
+ if (!frames_node) {
+ ret = USBG_ERROR_INVALID_PARAM;
+ goto out;
+ }
+
+ if (!config_setting_is_list(frames_node)) {
+ ret = USBG_ERROR_INVALID_TYPE;
+ goto out;
+ }
+
+ nframes = config_setting_length(frames_node);
+ for (i = 0; i < nframes; ++i) {
+ node = config_setting_get_elem(frames_node, i);
+ if (!node) {
+ ret = USBG_ERROR_INVALID_FORMAT;
+ goto out;
+ }
+
+ if (!config_setting_is_group(node)) {
+ ret = USBG_ERROR_INVALID_TYPE;
+ goto out;
+ }
+ if (!frames[i]) {
+ ret = usbg_f_uvc_create_frame(uvcf, format, frames, i, NULL);
+ if (ret)
+ goto out;
+ }
+ ret = uvc_import_frame_attrs(uvcf, format, i, node);
+ if (ret)
+ goto out;
+ }
+
+out:
+ return ret;
+};
+
+static int uvc_import_config(struct usbg_f_uvc *uvcf, config_setting_t *root)
+{
+ union usbg_f_uvc_config_attr_val val;
+ config_setting_t *config_node;
+ int ret = 0;
+ int i;
+
+ config_node = config_setting_get_member(root, "config");
+ if (!config_node) {
+ ret = USBG_ERROR_INVALID_PARAM;
+ return ret;
+ }
+
+ if (!config_setting_is_group(config_node)) {
+ ret = USBG_ERROR_INVALID_TYPE;
+ return ret;
+ }
+
+ for (i = USBG_F_UVC_CONFIG_ATTR_MIN; i < USBG_F_UVC_CONFIG_ATTR_MAX; ++i) {
+ ret = uvc_config_attr[i].import(config_node, uvc_config_attr[i].name, &val);
+ /* node not found */
+ if (ret == 0)
+ continue;
+ /* error */
+ if (ret < 0)
+ break;
+
+ ret = usbg_f_uvc_set_config_attr_val(uvcf, i, val);
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+static int uvc_libconfig_import(struct usbg_function *f, config_setting_t *root)
+{
+ struct usbg_f_uvc *uvcf = usbg_to_uvc_function(f);
+ config_setting_t *formats_node, *node;
+ int nmb, ret, i, nformats;
+ char fp[USBG_MAX_PATH_LENGTH];
+ const char *format;
+ struct formats *formats;
+
+ formats_node = config_setting_get_member(root, "formats");
+ if (!formats_node) {
+ ret = USBG_ERROR_INVALID_PARAM;
+ goto out;
+ }
+
+ if (!config_setting_is_group(formats_node)) {
+ ret = USBG_ERROR_INVALID_TYPE;
+ goto out;
+ }
+
+ nformats = config_setting_length(formats_node);
+ formats = get_formats_mask(uvcf);
+ for (i = 0; i < nformats; ++i) {
+ node = config_setting_get_elem(formats_node, i);
+ if (!node) {
+ ret = USBG_ERROR_INVALID_FORMAT;
+ goto out;
+ }
+
+ if (!config_setting_is_group(node)) {
+ ret = USBG_ERROR_INVALID_TYPE;
+ goto out;
+ }
+
+ format = config_setting_name(node);
+
+ nmb = snprintf(fp, sizeof(fp), "%s/%c", format, format[0]);
+ if (nmb >= sizeof(fp))
+ return USBG_ERROR_PATH_TOO_LONG;
+
+ ret = uvc_import_format(uvcf, fp, formats[i].frames, node);
+ if (ret)
+ goto out;
+
+ ret = uvc_import_format_attrs(uvcf, fp, node);
+ if (ret)
+ goto out;
+ }
+
+ ret = uvc_import_config(uvcf, root);
+ if (ret != USBG_SUCCESS)
+ return ret;
+
+ ret = uvc_set_class(uvcf, UVC_PATH_CONTROL);
+ if (ret != USBG_SUCCESS)
+ return ret;
+
+ ret = uvc_set_class(uvcf, UVC_PATH_STREAMING);
+ if (ret != USBG_SUCCESS)
+ return ret;
+
+out:
+ return ret;
+
+}
+
+static int uvc_export_config(struct usbg_f_uvc *uvcf, config_setting_t *root)
+{
+ config_setting_t *config_node;
+ union usbg_f_uvc_config_attr_val val;
+ int i;
+ int ret = 0;
+
+ config_node = config_setting_add(root, "config", CONFIG_TYPE_GROUP);
+ if (!config_node)
+ goto out;
+
+ for (i = USBG_F_UVC_CONFIG_ATTR_MIN; i < USBG_F_UVC_CONFIG_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_get_config_attr_val(uvcf, i, &val);
+ if (ret)
+ break;
+
+ ret = uvc_config_attr[i].export(config_node, uvc_config_attr[i].name, &val);
+ if (ret)
+ break;
+ }
+
+out:
+ return ret;
+};
+
+static int uvc_export_format_attrs(struct usbg_f_uvc *uvcf, const char *format,
+ config_setting_t *root)
+{
+ union usbg_f_uvc_format_attr_val val;
+ int ret = 0;
+ int i;
+
+ for (i = USBG_F_UVC_FORMAT_ATTR_MIN; i < USBG_F_UVC_FORMAT_ATTR_MAX; ++i) {
+ ret = usbg_f_uvc_get_format_attr_val(uvcf, format, i, &val);
+ if (ret)
+ break;
+
+ ret = uvc_format_attr[i].export(root, uvc_format_attr[i].name, &val);
+ if (ret)