/
usb_moded-configfs.c
1054 lines (824 loc) · 25.3 KB
/
usb_moded-configfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file usb_moded-configfs.c
*
* Copyright (C) 2018-2019 Jolla. All rights reserved.
*
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "usb_moded-configfs.h"
#include "usb_moded.h"
#include "usb_moded-android.h"
#include "usb_moded-common.h"
#include "usb_moded-config-private.h"
#include "usb_moded-log.h"
#include "usb_moded-mac.h"
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
/* ========================================================================= *
* Constants
* ========================================================================= */
/* Due to legacy these defaults must match what is required by Sony XA2 port */
#define DEFAULT_GADGET_BASE_DIRECTORY "/config/usb_gadget/g1"
#define DEFAULT_GADGET_FUNC_DIRECTORY "functions"
#define DEFAULT_GADGET_CONF_DIRECTORY "configs/b.1"
#define DEFAULT_GADGET_CTRL_UDC "UDC"
#define DEFAULT_GADGET_CTRL_ID_VENDOR "idVendor"
#define DEFAULT_GADGET_CTRL_ID_PRODUCT "idProduct"
#define DEFAULT_GADGET_CTRL_MANUFACTURER "strings/0x409/manufacturer"
#define DEFAULT_GADGET_CTRL_PRODUCT "strings/0x409/product"
#define DEFAULT_GADGET_CTRL_SERIAL "strings/0x409/serialnumber"
#define DEFAULT_FUNCTION_MASS_STORAGE "mass_storage.usb0"
#define DEFAULT_FUNCTION_RNDIS "rndis_bam.rndis"
#define DEFAULT_FUNCTION_MTP "ffs.mtp"
#define DEFAULT_RNDIS_CTRL_WCEIS "wceis"
#define DEFAULT_RNDIS_CTRL_ETHADDR "ethaddr"
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* CONFIGFS
* ------------------------------------------------------------------------- */
static gchar *configfs_get_conf (const char *key, const char *def);
static void configfs_read_configuration (void);
static int configfs_file_type (const char *path);
static const char *configfs_function_path (char *buff, size_t size, const char *func, ...);
static const char *configfs_unit_path (char *buff, size_t size, const char *func, const char *unit);
static const char *configfs_config_path (char *buff, size_t size, const char *func);
static bool configfs_mkdir (const char *path);
static bool configfs_rmdir (const char *path);
static const char *configfs_register_function (const char *function);
#ifdef DEAD_CODE
static bool configfs_unregister_function (const char *function);
#endif //DEAD_CODE
static const char *configfs_add_unit (const char *function, const char *unit);
static bool configfs_remove_unit (const char *function, const char *unit);
static bool configfs_enable_function (const char *function);
static bool configfs_disable_function (const char *function);
static bool configfs_disable_all_functions (void);
static char *configfs_strip (char *str);
bool configfs_in_use (void);
static bool configfs_probe (void);
static const char *configfs_udc_enable_value (void);
static bool configfs_write_file (const char *path, const char *text);
static bool configfs_read_file (const char *path, char *buff, size_t size);
#ifdef DEAD_CODE
static bool configfs_read_udc (char *buff, size_t size);
#endif // DEAD_CODE
static bool configfs_write_udc (const char *text);
bool configfs_set_udc (bool enable);
bool configfs_init (void);
void configfs_quit (void);
bool configfs_set_charging_mode (void);
bool configfs_set_productid (const char *id);
bool configfs_set_vendorid (const char *id);
static const char *configfs_map_function (const char *func);
bool configfs_set_function (const char *func);
bool configfs_add_mass_storage_lun (int lun);
bool configfs_remove_mass_storage_lun(int lun);
bool configfs_set_mass_storage_attr (int lun, const char *attr, const char *value);
/* ========================================================================= *
* Data
* ========================================================================= */
static int configfs_probed = -1;
static gchar *GADGET_BASE_DIRECTORY = 0;
static gchar *GADGET_FUNC_DIRECTORY = 0;
static gchar *GADGET_CONF_DIRECTORY = 0;
static gchar *GADGET_CTRL_UDC = 0;
static gchar *GADGET_CTRL_ID_VENDOR = 0;
static gchar *GADGET_CTRL_ID_PRODUCT = 0;
static gchar *GADGET_CTRL_MANUFACTURER = 0;
static gchar *GADGET_CTRL_PRODUCT = 0;
static gchar *GADGET_CTRL_SERIAL = 0;
static gchar *FUNCTION_MASS_STORAGE = 0;
static gchar *FUNCTION_RNDIS = 0;
static gchar *FUNCTION_MTP = 0;
static gchar *RNDIS_CTRL_WCEIS = 0;
static gchar *RNDIS_CTRL_ETHADDR = 0;
/* ========================================================================= *
* Settings
* ========================================================================= */
static gchar *configfs_get_conf(const char *key, const char *def)
{
LOG_REGISTER_CONTEXT;
return config_get_conf_string("configfs", key) ?: g_strdup(def);
}
/** Parse configfs configuration entries
*
* The defaults correspond with ini-file like (h3113 values):
*
* [configfs]
* gadget_base_directory = /config/usb_gadget/g1
* gadget_func_directory = functions
* gadget_conf_directory = configs/b.1
* function_mass_storage = mass_storage.usb0
* function_rndis = rndis_bam.rndis
* function_mtp = ffs.mtp
*/
static void configfs_read_configuration(void)
{
LOG_REGISTER_CONTEXT;
/* This must be done only once
*/
static bool done = false;
if( done )
goto EXIT;
done = true;
gchar *temp_setting;
/* Gadget directories
*/
GADGET_BASE_DIRECTORY =
configfs_get_conf("gadget_base_directory",
DEFAULT_GADGET_BASE_DIRECTORY);
temp_setting = configfs_get_conf("gadget_func_directory",
DEFAULT_GADGET_FUNC_DIRECTORY);
GADGET_FUNC_DIRECTORY = g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
temp_setting);
g_free(temp_setting);
temp_setting = configfs_get_conf("gadget_conf_directory",
DEFAULT_GADGET_CONF_DIRECTORY);
GADGET_CONF_DIRECTORY =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
temp_setting);
g_free(temp_setting);
/* Gadget control files
*/
GADGET_CTRL_UDC =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
DEFAULT_GADGET_CTRL_UDC);
GADGET_CTRL_ID_VENDOR =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
DEFAULT_GADGET_CTRL_ID_VENDOR);
GADGET_CTRL_ID_PRODUCT =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
DEFAULT_GADGET_CTRL_ID_PRODUCT);
GADGET_CTRL_MANUFACTURER =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
DEFAULT_GADGET_CTRL_MANUFACTURER);
GADGET_CTRL_PRODUCT =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
DEFAULT_GADGET_CTRL_PRODUCT);
GADGET_CTRL_SERIAL =
g_strdup_printf("%s/%s",
GADGET_BASE_DIRECTORY,
DEFAULT_GADGET_CTRL_SERIAL);
/* Functions
*/
FUNCTION_MASS_STORAGE =
configfs_get_conf("function_mass_storage",
DEFAULT_FUNCTION_MASS_STORAGE);
FUNCTION_RNDIS =
configfs_get_conf("function_rndis",
DEFAULT_FUNCTION_RNDIS);
FUNCTION_MTP =
configfs_get_conf("function_mtp",
DEFAULT_FUNCTION_MTP);
/* Function control files */
RNDIS_CTRL_WCEIS =
g_strdup_printf("%s/%s/%s",
GADGET_FUNC_DIRECTORY,
FUNCTION_RNDIS,
DEFAULT_RNDIS_CTRL_WCEIS);
RNDIS_CTRL_ETHADDR =
g_strdup_printf("%s/%s/%s",
GADGET_FUNC_DIRECTORY,
FUNCTION_RNDIS,
DEFAULT_RNDIS_CTRL_ETHADDR);
EXIT:
return;
}
/* ========================================================================= *
* Functions
* ========================================================================= */
static int configfs_file_type(const char *path)
{
LOG_REGISTER_CONTEXT;
int type = -1;
if( !path )
goto EXIT;
struct stat st;
if( lstat(path, &st) == -1 )
goto EXIT;
type = st.st_mode & S_IFMT;
EXIT:
return type;
}
static const char *
configfs_function_path(char *buff, size_t size, const char *func, ...)
{
LOG_REGISTER_CONTEXT;
char *pos = buff;
char *end = buff + size;
snprintf(pos, end-pos, "%s", GADGET_FUNC_DIRECTORY);
va_list va;
va_start(va, func);
while( func ) {
pos = strchr(pos, 0);
snprintf(pos, end-pos, "/%s", func);
func = va_arg(va, char *);
}
va_end(va);
return buff;
}
static const char *
configfs_unit_path(char *buff, size_t size, const char *func, const char *unit)
{
LOG_REGISTER_CONTEXT;
return configfs_function_path(buff, size, func, unit, NULL);
}
static const char *
configfs_config_path(char *buff, size_t size, const char *func)
{
LOG_REGISTER_CONTEXT;
snprintf(buff, size, "%s/%s", GADGET_CONF_DIRECTORY, func);
return buff;
}
static bool
configfs_mkdir(const char *path)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( mkdir(path, 0775) == -1 && errno != EEXIST ) {
log_err("%s: mkdir failed: %m", path);
goto EXIT;
}
if( configfs_file_type(path) != S_IFDIR ) {
log_err("%s: is not a directory", path);
goto EXIT;
}
ack = true;
EXIT:
return ack;
}
static bool
configfs_rmdir(const char *path)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( rmdir(path) == -1 && errno != ENOENT ) {
log_err("%s: rmdir failed: %m", path);
goto EXIT;
}
ack = true;
EXIT:
return ack;
}
static const char *
configfs_register_function(const char *function)
{
LOG_REGISTER_CONTEXT;
const char *res = 0;
static char fpath[256];
configfs_function_path(fpath, sizeof fpath, function, NULL);
if( !configfs_mkdir(fpath) )
goto EXIT;
log_debug("function %s is registered", function);
res = fpath;
EXIT:
return res;
}
#ifdef DEAD_CODE
static bool
configfs_unregister_function(const char *function)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
char fpath[256];
configfs_function_path(fpath, sizeof fpath, function, NULL);
if( !configfs_rmdir(fpath) )
goto EXIT;
log_debug("function %s is unregistered", function);
ack = true;
EXIT:
return ack;
}
#endif
static const char *
configfs_add_unit(const char *function, const char *unit)
{
LOG_REGISTER_CONTEXT;
const char *res = 0;
static char upath[256];
configfs_unit_path(upath, sizeof upath, function, unit);
if( !configfs_mkdir(upath) )
goto EXIT;
log_debug("function %s unit %s added", function, unit);
res = upath;
EXIT:
return res;
}
static bool
configfs_remove_unit(const char *function, const char *unit)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
static char upath[256];
configfs_unit_path(upath, sizeof upath, function, unit);
if( !configfs_rmdir(upath) )
goto EXIT;
log_debug("function %s unit %s removed", function, unit);
ack = true;
EXIT:
return ack;
}
static bool
configfs_enable_function(const char *function)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
const char *fpath = configfs_register_function(function);
if( !fpath ) {
log_err("function %s is not registered", function);
goto EXIT;
}
char cpath[256];
configfs_config_path(cpath, sizeof cpath, function);
switch( configfs_file_type(cpath) ) {
case S_IFLNK:
if( unlink(cpath) == -1 ) {
log_err("%s: unlink failed: %m", cpath);
goto EXIT;
}
/* fall through */
case -1:
if( symlink(fpath, cpath) == -1 ) {
log_err("%s: failed to symlink to %s: %m", cpath, fpath);
goto EXIT;
}
break;
default:
log_err("%s: is not a symlink", cpath);
goto EXIT;
}
log_debug("function %s is enabled", function);
ack = true;
EXIT:
return ack;
}
static bool
configfs_disable_function(const char *function)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
char cpath[256];
configfs_config_path(cpath, sizeof cpath, function);
if( configfs_file_type(cpath) != S_IFLNK ) {
log_err("%s: is not a symlink", cpath);
goto EXIT;
}
if( unlink(cpath) == -1 ) {
log_err("%s: unlink failed: %m", cpath);
goto EXIT;
}
log_debug("function %s is disabled", function);
ack = true;
EXIT:
return ack;
}
static bool
configfs_disable_all_functions(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
DIR *dir = 0;
if( !(dir = opendir(GADGET_CONF_DIRECTORY)) ) {
log_err("%s: opendir failed: %m", GADGET_CONF_DIRECTORY);
goto EXIT;
}
ack = true;
struct dirent *de;
while( (de = readdir(dir)) ) {
if( de->d_type != DT_LNK )
continue;
if( !configfs_disable_function(de->d_name) )
ack = false;
}
if( ack )
log_debug("all functions are disabled");
EXIT:
if( dir )
closedir(dir);
return ack;
}
static char *configfs_strip(char *str)
{
LOG_REGISTER_CONTEXT;
unsigned char *src = (unsigned char *)str;
unsigned char *dst = (unsigned char *)str;
while( *src > 0 && *src <= 32 ) ++src;
for( ;; )
{
while( *src > 32 ) *dst++ = *src++;
while( *src > 0 && *src <= 32 ) ++src;
if( *src == 0 ) break;
*dst++ = ' ';
}
*dst = 0;
return str;
}
bool
configfs_in_use(void)
{
LOG_REGISTER_CONTEXT;
if( configfs_probed < 0 )
log_debug("configfs_in_use() called before configfs_probe()");
return configfs_probed > 0;
}
static bool
configfs_probe(void)
{
LOG_REGISTER_CONTEXT;
configfs_read_configuration();
if( configfs_probed <= 0 ) {
configfs_probed = (access(GADGET_BASE_DIRECTORY, F_OK) == 0 &&
access(GADGET_CTRL_UDC, F_OK) == 0);
log_warning("CONFIGFS %sdetected", configfs_probed ? "" : "not ");
}
return configfs_in_use();
}
static const char *
configfs_udc_enable_value(void)
{
LOG_REGISTER_CONTEXT;
static bool probed = false;
static char *value = 0;
if( !probed ) {
probed = true;
/* Find first symlink in /sys/class/udc directory */
struct dirent *de;
DIR *dir = opendir("/sys/class/udc");
if( dir ) {
while( (de = readdir(dir)) ) {
if( de->d_type != DT_LNK )
continue;
if( de->d_name[0] == '.' )
continue;
value = strdup(de->d_name);
break;
}
closedir(dir);
}
}
return value ?: "";
}
static bool
configfs_write_file(const char *path, const char *text)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
int fd = -1;
if( !path || !text )
goto EXIT;
log_debug("WRITE %s '%s'", path, text);
char buff[64];
snprintf(buff, sizeof buff, "%s\n", text);
size_t size = strlen(buff);
if( (fd = open(path, O_WRONLY)) == -1 ) {
log_err("%s: can't open for writing: %m", path);
goto EXIT;
}
int rc = write(fd, text, size);
if( rc == -1 ) {
log_err("%s: write failure: %m", path);
goto EXIT;
}
if( (size_t)rc != size ) {
log_err("%s: write failure: partial success", path);
goto EXIT;
}
ack = true;
EXIT:
if( fd != -1 )
close(fd);
return ack;
}
static bool
configfs_read_file(const char *path, char *buff, size_t size)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
int fd = -1;
if( !path || !buff )
goto EXIT;
if( size < 2 )
goto EXIT;
if( (fd = open(path, O_RDONLY)) == -1 ) {
log_err("%s: can't open for reading: %m", path);
goto EXIT;
}
int rc = read(fd, buff, size - 1);
if( rc == -1 ) {
log_err("%s: read failure: %m", path);
goto EXIT;
}
buff[rc] = 0;
configfs_strip(buff);
ack = true;
log_debug("READ %s '%s'", path, buff);
EXIT:
if( fd != -1 )
close(fd);
return ack;
}
#ifdef DEAD_CODE
static bool
configfs_read_udc(char *buff, size_t size)
{
LOG_REGISTER_CONTEXT;
return configfs_read_file(GADGET_CTRL_UDC, buff, size);
}
#endif
static bool
configfs_write_udc(const char *text)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
char prev[64];
if( !configfs_read_file(GADGET_CTRL_UDC, prev, sizeof prev) )
goto EXIT;
if( strcmp(prev, text) ) {
if( !configfs_write_file(GADGET_CTRL_UDC, text) )
goto EXIT;
}
ack = true;
EXIT:
return ack;
}
bool
configfs_set_udc(bool enable)
{
LOG_REGISTER_CONTEXT;
log_debug("UDC - %s", enable ? "ENABLE" : "DISABLE");
const char *value = "";
if( enable )
value = configfs_udc_enable_value();
return configfs_write_udc(value);
}
/** initialize the basic configfs values
*
* @return true if configfs backend is ready for use, false otherwise
*/
bool
configfs_init(void)
{
LOG_REGISTER_CONTEXT;
if( !configfs_probe() )
goto EXIT;
/* Disable */
configfs_set_udc(false);
/* Configure */
gchar *text;
if( (text = config_get_android_vendor_id()) ) {
configfs_write_file(GADGET_CTRL_ID_VENDOR, text);
g_free(text);
}
if( (text = config_get_android_product_id()) ) {
configfs_write_file(GADGET_CTRL_ID_PRODUCT, text);
g_free(text);
}
if( (text = config_get_android_manufacturer()) ) {
configfs_write_file(GADGET_CTRL_MANUFACTURER, text);
g_free(text);
}
if( (text = config_get_android_product()) ) {
configfs_write_file(GADGET_CTRL_PRODUCT, text);
g_free(text);
}
if( (text = android_get_serial()) ) {
configfs_write_file(GADGET_CTRL_SERIAL, text);
g_free(text);
}
/* Prep: charging_only */
configfs_register_function(FUNCTION_MASS_STORAGE);
/* Prep: mtp_mode */
configfs_register_function(FUNCTION_MTP);
if( access("/dev/mtp/ep0", F_OK) == -1 ) {
common_system("/bin/mount -o uid=100000,gid=100000 -t functionfs mtp /dev/mtp");
}
/* Prep: developer_mode */
configfs_register_function(FUNCTION_RNDIS);
if( (text = mac_read_mac()) ) {
configfs_write_file(RNDIS_CTRL_ETHADDR, text);
g_free(text);
}
/* For rndis to be discovered correctly in M$ Windows (vista and later) */
configfs_write_file(RNDIS_CTRL_WCEIS, "1");
/* Leave disabled, will enable on cable connect detected */
EXIT:
return configfs_in_use();
}
/** Cleanup resources allocated by configfs backend
*/
void
configfs_quit(void)
{
g_free(GADGET_BASE_DIRECTORY),
GADGET_BASE_DIRECTORY = 0;
g_free(GADGET_FUNC_DIRECTORY),
GADGET_FUNC_DIRECTORY = 0;
g_free(GADGET_CONF_DIRECTORY),
GADGET_CONF_DIRECTORY = 0;
g_free(GADGET_CTRL_UDC),
GADGET_CTRL_UDC = 0;
g_free(GADGET_CTRL_ID_VENDOR),
GADGET_CTRL_ID_VENDOR= 0;
g_free(GADGET_CTRL_ID_PRODUCT),
GADGET_CTRL_ID_PRODUCT= 0;
g_free(GADGET_CTRL_MANUFACTURER),
GADGET_CTRL_MANUFACTURER= 0;
g_free(GADGET_CTRL_PRODUCT),
GADGET_CTRL_PRODUCT = 0;
g_free(GADGET_CTRL_SERIAL),
GADGET_CTRL_SERIAL = 0;
g_free(FUNCTION_MASS_STORAGE),
FUNCTION_MASS_STORAGE = 0;
g_free(FUNCTION_RNDIS),
FUNCTION_RNDIS = 0;
g_free(FUNCTION_MTP),
FUNCTION_MTP = 0;
g_free(RNDIS_CTRL_WCEIS),
RNDIS_CTRL_WCEIS = 0;
g_free(RNDIS_CTRL_ETHADDR),
RNDIS_CTRL_ETHADDR= 0;
}
/* Set a charging mode for the configfs gadget
*
* @return true if successful, false on failure
*/
bool
configfs_set_charging_mode(void)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( !configfs_set_function("mass_storage") )
goto EXIT;
/* TODO: make this configurable */
configfs_set_productid("0AFE");
if( !configfs_set_udc(true) )
goto EXIT;
ack = true;
EXIT:
log_debug("CONFIGFS %s() -> %d", __func__, ack);
return ack;
}
/* Set a product id for the configfs gadget
*
* @return true if successful, false on failure
*/
bool
configfs_set_productid(const char *id)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( id && configfs_in_use() ) {
/* Config files have things like "0A02".
* Kernel wants to see "0x0a02" ... */
char *end = 0;
unsigned num = strtol(id, &end, 16);
char str[16];
if( end > id && *end == 0 ) {
snprintf(str, sizeof str, "0x%04x", num);
id = str;
}
ack = configfs_write_file(GADGET_CTRL_ID_PRODUCT, id);
}
log_debug("CONFIGFS %s(%s) -> %d", __func__, id, ack);
return ack;
}
/* Set a vendor id for the configfs gadget
*
* @return true if successful, false on failure
*/
bool
configfs_set_vendorid(const char *id)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( id && configfs_in_use() ) {
log_debug("%s(%s) was called", __func__, id);
/* Config files have things like "0A02".
* Kernel wants to see "0x0a02" ... */
char *end = 0;
unsigned num = strtol(id, &end, 16);
char str[16];
if( end > id && *end == 0 ) {
snprintf(str, sizeof str, "0x%04x", num);
id = str;
}
ack = configfs_write_file(GADGET_CTRL_ID_VENDOR, id);
}
log_debug("CONFIGFS %s(%s) -> %d", __func__, id, ack);
return ack;
}
static const char *
configfs_map_function(const char *func)
{
LOG_REGISTER_CONTEXT;
if( func == 0 )
;
else if( !strcmp(func, "mass_storage") )
func = FUNCTION_MASS_STORAGE;
else if( !strcmp(func, "rndis") )
func = FUNCTION_RNDIS;
else if( !strcmp(func, "mtp") )
func = FUNCTION_MTP;
else if( !strcmp(func, "ffs") ) // existing config files ...
func = FUNCTION_MTP;
return func;
}
/* Set a function
*
* @param func Mame of function to enable, or NULL to disable all
*
* @return true if successful, false on failure
*/
bool
configfs_set_function(const char *func)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if( !configfs_in_use() )
goto EXIT;
/* Normalize names used by usb-moded itself and already
* existing configuration files etc.
*/
func = configfs_map_function(func);
if( !configfs_set_udc(false) )
goto EXIT;
if( !configfs_disable_all_functions() )
goto EXIT;
if( func && !configfs_enable_function(func) )
goto EXIT;
/* Leave disabled, so that caller can adjust attributes
* etc before enabling */
ack = true;
EXIT:
log_debug("CONFIGFS %s(%s) -> %d", __func__, func, ack);
return ack;
}
bool
configfs_add_mass_storage_lun(int lun)