/
usb_moded.c
2007 lines (1685 loc) · 55.7 KB
/
usb_moded.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.c
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
* Copyright (C) 2012-2018 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Philippe De Swert <phdeswer@lumi.maa>
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Jonni Rainisto <jonni.rainisto@jollamobile.com>
* @author: Pekka Lundstrom <pekka.lundstrom@jollamobile.com>
* @author: Vesa Halttunen <vesa.halttunen@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
* @author: Thomas Perl <thomas.perl@jolla.com>
* @author: Matti Lehtimaki <matti.lehtimaki@gmail.com>
* @author: Thomas Perl <m@thp.io>
* @author: Martin Jones <martin.jones@jollamobile.com>
* @author: Andrew den Exter <andrew.den.exter@jolla.com>
* @author: Andrew den Exter <andrew.den.exter@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 <getopt.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <libkmod.h>
#ifdef SYSTEMD
# include <systemd/sd-daemon.h>
#endif
#include "usb_moded.h"
#include "usb_moded-modes.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-udev.h"
#include "usb_moded-modules.h"
#include "usb_moded-log.h"
#include "usb_moded-devicelock.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
#include "usb_moded-appsync.h"
#include "usb_moded-trigger.h"
#include "usb_moded-config.h"
#include "usb_moded-config-private.h"
#include "usb_moded-network.h"
#include "usb_moded-mac.h"
#include "usb_moded-android.h"
#include "usb_moded-configfs.h"
#include "usb_moded-systemd.h"
#ifdef MEEGOLOCK
# include "usb_moded-dsme.h"
#endif
/* ========================================================================= *
* Constants
* ========================================================================= */
/* Wakelogging is noisy, do not log it by default */
#ifndef VERBOSE_WAKELOCKING
# define VERBOSE_WAKELOCKING 0
#endif
/** Default allowed cable detection delay
*
* To comply with USB standards, the delay should be
* less than 2 seconds to ensure timely enumeration.
*
* Any value <= zero means no delay.
*/
#define CABLE_CONNECTION_DELAY_DEFAULT 0
/** Maximum allowed cable detection delay
*
* Must be shorter than initial probing delay expected by
* dsme (currently 5 seconds) to avoid reboot loops in
* act dead mode.
*
* And shorter than USB_MODED_SUSPEND_DELAY_DEFAULT_MS to
* allow the timer to trigger also in display off scenarios.
*/
#define CABLE_CONNECTION_DELAY_MAXIMUM 4000
/* ========================================================================= *
* Types
* ========================================================================= */
/** A struct containing all the usb_moded info needed
*/
typedef struct usb_mode
{
/** Connection status
*
* Access only via:
* - usbmoded_get_connection_state()
* - usbmoded_set_connection_state()
*/
bool connected;
/** Mount status, true for mounted -UNUSED atm- */
bool mounted;
/** Used to keep an active gadget for broken Android kernels */
bool android_usb_broken;
/** The logical mode name
*
* Full set of valid modes can occur here
*/
char *internal_mode;
/* The hardware mode name
*
* How the usb hardware has been configured.
*
* For example internal_mode=MODE_ASK gets
* mapped to hardware_mode=MODE_CHARGING */
char *hardware_mode;
/* The external mode;
*
* What was the last mode signaled over D-Bus.
*/
char *external_mode;
/**< The module name for the specific mode */
char *module;
/**< Contains the mode data */
struct mode_list_elem *data;
} usb_mode;
/** Mapping usb mode from internal to hardware/broadcast use */
typedef struct modemapping_t
{
/** Any valid usb mode */
const char *internal_mode;
/** Mode to use for usb configuration, or NULL = internal */
const char *hardware_mode;
/** Mode to use for D-Bus broadcast, or NULL = internal */
const char *external_mode;
} modemapping_t;
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- usbmoded -- */
static const char *usbmoded_map_mode_to_hardware(const char *internal_mode);
static const char *usbmoded_map_mode_to_external(const char *internal_mode);
// ----------------------------------------------------------------
void usbmoded_rethink_usb_charging_fallback(void);
static bool usbmoded_switch_to_charging (void);
static void usbmoded_switch_to_mode (const char *mode);
const char *usbmoded_get_hardware_mode (void);
static void usbmoded_update_hardware_mode (void);
const char *usbmoded_get_external_mode (void);
static void usbmoded_update_external_mode (void);
// from here and there
const char *usbmoded_get_usb_mode (void);
void usbmoded_set_usb_mode (const char *internal_mode);
// from usbmoded_set_usb_connected()
// usbmoded_set_charger_connected()
bool usbmoded_get_connection_state (void);
static bool usbmoded_set_connection_state (bool state);
// from usbmoded_set_usb_connected()
static void usbmoded_set_usb_disconnected_state_silent(void);
static void usbmoded_set_usb_disconnected_state (void);
void usbmoded_set_usb_connected_state (void);
// from udev cable_state_changed()
void usbmoded_set_usb_connected (bool connected);
void usbmoded_set_charger_connected (bool state);
// ----------------------------------------------------------------
// internal movements
// ...
const char *usbmoded_get_android_bootup_function (void);
void usbmoded_set_android_bootup_function (const char *function);
static void usbmoded_set_cable_connection_delay (int delay_ms);
static bool usbmoded_mode_in_list (const char *mode, char *const *modes);
int usbmoded_valid_mode (const char *mode);
gchar *usbmoded_get_mode_list (mode_list_type_t type);
const char *usbmoded_get_usb_module (void);
void usbmoded_set_usb_module (const char *module);
struct mode_list_elem *usbmoded_get_usb_mode_data (void);
void usbmoded_set_usb_mode_data (struct mode_list_elem *data);
void usbmoded_send_supported_modes_signal (void);
void usbmoded_send_available_modes_signal (void);
void usbmoded_send_hidden_modes_signal (void);
void usbmoded_send_whitelisted_modes_signal(void);
static void usbmoded_write_to_sysfs_file (const char *path, const char *text);
void usbmoded_acquire_wakelock (const char *wakelock_name);
void usbmoded_release_wakelock (const char *wakelock_name);
static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
void usbmoded_exit_mainloop (int exitcode);
static void usbmoded_handle_signal (int signum);
static void usbmoded_init (void);
static void usbmoded_cleanup (void);
int usbmoded_system_ (const char *file, int line, const char *func, const char *command);
FILE *usbmoded_popen_ (const char *file, int line, const char *func, const char *command, const char *type);
void usbmoded_usleep_ (const char *file, int line, const char *func, useconds_t usec);
static void usbmoded_usage (void);
/* -- sigpipe -- */
static gboolean sigpipe_read_signal_cb (GIOChannel *channel, GIOCondition condition, gpointer data);
static void sigpipe_trap_signal_cb (int sig);
static bool sigpipe_crate_pipe (void);
static void sigpipe_trap_signals (void);
static bool sigpipe_init (void);
/* ========================================================================= *
* Data
* ========================================================================= */
static int usb_moded_exitcode = EXIT_FAILURE;
static GMainLoop *usb_moded_mainloop = NULL;
bool usbmoded_rescue_mode = false;
static bool diag_mode = false;
static bool hw_fallback = false;
static bool android_broken_usb = false;
static bool android_ignore_udev_events = false;
static bool android_ignore_next_udev_disconnect_event = false;
#ifdef SYSTEMD
static bool systemd_notify = false;
#endif
/** Optional android usb function to setup during bootup */
static gchar *android_bootup_function = 0;
/** Currently allowed cable detection delay
*/
int usbmoded_cable_connection_delay = CABLE_CONNECTION_DELAY_DEFAULT;
static struct usb_mode current_mode = {
.connected = false,
.mounted = false,
.android_usb_broken = false,
.internal_mode = NULL,
.hardware_mode = NULL,
.external_mode = NULL,
.module = NULL,
.data = NULL,
};
static GList *modelist = 0;
/** Path to init-done flag file */
static const char init_done_flagfile[] = "/run/systemd/boot-status/init-done";
/** cached init-done-reached state */
static bool init_done_reached = false;
/** Flag for: USB_MODED_WAKELOCK_STATE_CHANGE has been acquired */
static bool blocking_suspend = false;
/** Timer for releasing USB_MODED_WAKELOCK_STATE_CHANGE */
static guint allow_suspend_timer_id = 0;
/** Pipe fd for transferring signals to mainloop context */
static int sigpipe_fd = -1;
static const modemapping_t modemapping[] =
{
{
.internal_mode = MODE_UNDEFINED,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
{
.internal_mode = MODE_ASK,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
{
.internal_mode = MODE_MASS_STORAGE,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_DEVELOPER,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_MTP,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_HOST,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_CONNECTION_SHARING,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_DIAG,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_ADB,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_PC_SUITE,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_CHARGING,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
{
.internal_mode = MODE_CHARGING_FALLBACK,
.hardware_mode = MODE_CHARGING,
.external_mode = MODE_ASK,
},
{
.internal_mode = MODE_CHARGER,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
// sentinel
{
.internal_mode = 0,
.hardware_mode = 0,
.external_mode = 0,
}
};
/* ========================================================================= *
* Functions
* ========================================================================= */
static const char *
usbmoded_map_mode_to_hardware(const char *internal_mode)
{
const char *hardware_mode = 0;
for( size_t i = 0; modemapping[i].internal_mode; ++i ) {
if( strcmp(modemapping[i].internal_mode, internal_mode) )
continue;
hardware_mode = modemapping[i].hardware_mode;
break;
}
return hardware_mode ?: internal_mode;
}
static const char *
usbmoded_map_mode_to_external(const char *internal_mode)
{
const char *external_mode = 0;
for( size_t i = 0; modemapping[i].internal_mode; ++i ) {
if( strcmp(modemapping[i].internal_mode, internal_mode) )
continue;
external_mode = modemapping[i].external_mode;
break;
}
return external_mode ?: internal_mode;
}
/** Check if we can/should leave charging fallback mode
*
* Called when device lock status, or device status (dsme)
* changes.
*/
void
usbmoded_rethink_usb_charging_fallback(void)
{
/* Cable must be connected */
if( !usbmoded_get_connection_state() )
goto EXIT;
/* Suitable usb-mode mode selected */
const char *usb_mode = usbmoded_get_usb_mode();
if( strcmp(usb_mode, MODE_UNDEFINED) &&
strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
goto EXIT;
#ifdef MEEGOLOCK
/* If device locking is supported, the device must be in
* unlocked state (or rescue mode must be active).
*/
if( !devicelock_have_export_permission() && !usbmoded_rescue_mode ) {
log_notice("device is locked; stay in %s", usb_mode);
goto EXIT;
}
/* Device must be in USER state or in rescue mode
*/
if( !dsme_in_user_state() && !usbmoded_rescue_mode ) {
log_notice("device is not in USER mode; stay in %s", usb_mode);
goto EXIT;
}
#endif
log_debug("attempt to leave %s", usb_mode);
usbmoded_set_usb_connected_state();
EXIT:
return;
}
static bool usbmoded_switch_to_charging(void)
{
bool ack = true;
modules_check_module_state(MODULE_MASS_STORAGE);
/* for charging we use a fake file_storage
* (blame USB certification for this insanity */
usbmoded_set_usb_module(MODULE_MASS_STORAGE);
/* MODULE_CHARGING has all the parameters defined,
* so it will not match the g_file_storage rule in
* modules_load_module */
if( modules_load_module(MODULE_CHARGING) == 0 )
goto SUCCESS;
/* if charging mode setting did not succeed we
* might be dealing with android */
if (android_ignore_udev_events)
android_ignore_next_udev_disconnect_event = true;
usbmoded_set_usb_module(MODULE_NONE);
if( android_set_charging_mode() )
goto SUCCESS;
if( configfs_set_charging_mode() )
goto SUCCESS;
log_err("switch to charging mode failed");
ack = false;
SUCCESS:
return ack;
}
static void usbmoded_switch_to_mode(const char *mode)
{
/* set return to 1 to be sure to error out if no matching mode is found either */
int ret=1;
log_debug("Setting %s\n", mode);
/* Mode mapping should mean we only see MODE_CHARGING here, but just
* in case redirect fixed charging related things to charging ... */
if( !strcmp(mode, MODE_CHARGING) ||
!strcmp(mode, MODE_CHARGING_FALLBACK) ||
!strcmp(mode, MODE_CHARGER) ||
!strcmp(mode, MODE_UNDEFINED) ||
!strcmp(mode, MODE_ASK)) {
goto CHARGE;
}
#ifdef MEEGOLOCK
/* Potentially data exposing modes are allowed only when
* device has been booted to user mode and unlocked.
*
* Except if rescue mode is still active.
*/
bool can_export = (dsme_in_user_state() &&
devicelock_have_export_permission());
if( !can_export && !usbmoded_rescue_mode ) {
log_warning("Policy does not allow mode: %s", mode);
goto CHARGE;
}
#endif
/* go through all the dynamic modes if the modelist exists*/
for( GList *iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if( strcmp(mode, data->mode_name) )
continue;
log_debug("Matching mode %s found.\n", mode);
modules_check_module_state(data->mode_module);
usbmoded_set_usb_module(data->mode_module);
ret = modules_load_module(data->mode_module);
/* set data before calling any of the dynamic mode functions
* as they will use the usbmoded_get_usb_mode_data function */
usbmoded_set_usb_mode_data(data);
/* check if modules are ok before continuing */
if( ret == 0 ) {
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = true;
}
ret = modesetting_set_dynamic_mode();
if( ret == 0 )
goto SUCCESS;
}
}
log_warning("mode setting failed, fall back to charging");
CHARGE:
if( usbmoded_switch_to_charging() )
goto SUCCESS;
log_crit("failed to activate charging, all bets are off");
/* FIXME: double check this error path */
/* If we get here then usb_module loading failed,
* no mode matched, and charging setup failed too.
*/
usbmoded_set_usb_module(MODULE_NONE);
mode = MODE_UNDEFINED;
usbmoded_set_usb_mode_data(NULL);
log_debug("mode setting failed or device disconnected, mode to set was = %s\n", mode);
SUCCESS:
return;
}
const char *usbmoded_get_hardware_mode(void)
{
return current_mode.hardware_mode ?: MODE_UNDEFINED;
}
static void usbmoded_update_hardware_mode(void)
{
const char *internal_mode = usbmoded_get_usb_mode();
const char *hardware_mode = usbmoded_map_mode_to_hardware(internal_mode);
gchar *previous = current_mode.hardware_mode;
if( !g_strcmp0(previous, hardware_mode) )
goto EXIT;
log_debug("hardware_mode: %s -> %s",
previous, hardware_mode);
current_mode.hardware_mode = g_strdup(hardware_mode);
g_free(previous);
// DO THE MODESWITCH
usbmoded_switch_to_mode(current_mode.hardware_mode);
EXIT:
return;
}
const char *usbmoded_get_external_mode(void)
{
return current_mode.external_mode ?: MODE_UNDEFINED;
}
static void usbmoded_update_external_mode(void)
{
const char *internal_mode = usbmoded_get_usb_mode();
const char *external_mode = usbmoded_map_mode_to_external(internal_mode);
gchar *previous = current_mode.external_mode;
if( !g_strcmp0(previous, external_mode) )
goto EXIT;
log_debug("external_mode: %s -> %s",
previous, external_mode);
current_mode.external_mode = g_strdup(external_mode);
g_free(previous);
// DO THE DBUS BROADCAST
umdbus_send_state_signal(current_mode.external_mode);
EXIT:
return;
}
/** get the usb mode
*
* @return the currently set mode
*
*/
const char * usbmoded_get_usb_mode(void)
{
return current_mode.internal_mode;
}
/** set the usb mode
*
* @param mode The requested USB mode
*
*/
void usbmoded_set_usb_mode(const char *internal_mode)
{
gchar *previous = current_mode.internal_mode;
if( !g_strcmp0(previous, internal_mode) )
goto EXIT;
log_debug("internal_mode: %s -> %s",
previous, internal_mode);
current_mode.internal_mode = g_strdup(internal_mode);
g_free(previous);
// PROPAGATE DOWN TO USB
usbmoded_update_hardware_mode();
// PROPAGATE UP DBUS
usbmoded_update_external_mode();
EXIT:
return;
}
/** Get if the cable (pc or charger) is connected or not
*
* @ return true if connected, false if disconnected
*/
bool usbmoded_get_connection_state(void)
{
return current_mode.connected;
}
/** Set if the cable (pc or charger) is connected or not
*
* @param state true for connected, false for disconnected
*
* @return true if value changed, false otherwise
*/
static bool usbmoded_set_connection_state(bool state)
{
bool changed = false;
if( current_mode.connected != state ) {
log_debug("current_mode.connected: %d -> %d",
current_mode.connected, state);
current_mode.connected = state;
changed = true;
}
return changed;
}
/* set disconnected without sending signals. */
static void usbmoded_set_usb_disconnected_state_silent(void)
{
if(!usbmoded_get_connection_state())
{
log_debug("Resetting connection data after HUP\n");
/* unload modules and general cleanup if not charging */
if(strcmp(usbmoded_get_usb_mode(), MODE_CHARGING) ||
strcmp(usbmoded_get_usb_mode(), MODE_CHARGING_FALLBACK))
modesetting_cleanup(usbmoded_get_usb_module());
/* Nothing else as we do not need to do anything for cleaning up charging mode */
modules_cleanup_module(usbmoded_get_usb_module());
usbmoded_set_usb_mode(MODE_UNDEFINED);
}
}
static void usbmoded_set_usb_disconnected_state(void)
{
/* signal usb disconnected */
umdbus_send_state_signal(USB_DISCONNECTED);
/* unload modules and general cleanup if not charging */
if( strcmp(usbmoded_get_usb_mode(), MODE_CHARGING) &&
strcmp(usbmoded_get_usb_mode(), MODE_CHARGING_FALLBACK))
modesetting_cleanup(usbmoded_get_usb_module());
/* Nothing else as we do not need to do anything for cleaning up charging mode */
modules_cleanup_module(usbmoded_get_usb_module());
usbmoded_set_usb_mode(MODE_UNDEFINED);
}
/** set the chosen usb state
*
*/
void usbmoded_set_usb_connected_state(void)
{
char *mode_to_set = 0;
bool can_export = true;
if(usbmoded_rescue_mode)
{
log_debug("Entering rescue mode!\n");
usbmoded_set_usb_mode(MODE_DEVELOPER);
goto EXIT;
}
if(diag_mode)
{
log_debug("Entering diagnostic mode!\n");
if(modelist)
{
GList *iter = modelist;
struct mode_list_elem *data = iter->data;
usbmoded_set_usb_mode(data->mode_name);
}
goto EXIT;
}
mode_to_set = config_get_mode_setting();
#ifdef MEEGOLOCK
/* Check if we are allowed to export system contents 0 is unlocked.
* We check also if the device is in user state or not.
* If not we do not export anything. We presume ACT_DEAD charging
*/
can_export = (devicelock_have_export_permission()
&& dsme_in_user_state());
#endif
if( mode_to_set && can_export )
{
/* This is safe to do here as the starting condition is
* MODE_UNDEFINED, and having a devicelock being activated when
* a mode is set will not interrupt it */
if(!strcmp(mode_to_set, usbmoded_get_usb_mode()))
goto EXIT;
if (!strcmp(MODE_ASK, mode_to_set))
{
/* If charging mode is the only available selection, don't ask
* just select it */
gchar *available_modes = usbmoded_get_mode_list(AVAILABLE_MODES_LIST);
if (!strcmp(MODE_CHARGING, available_modes)) {
gchar *temp = mode_to_set;
mode_to_set = available_modes;
available_modes = temp;
}
g_free(available_modes);
}
if(!strcmp(MODE_ASK, mode_to_set))
{
/* send signal, mode will be set when the dialog service calls
* the set_mode method call.
*/
umdbus_send_state_signal(USB_CONNECTED_DIALOG_SHOW);
/* in case there was nobody listening for the UI, they will know
* that the UI is needed by requesting the current mode */
usbmoded_set_usb_mode(MODE_ASK);
}
else
usbmoded_set_usb_mode(mode_to_set);
}
else
{
/* config is corrupted or we do not have a mode configured, fallback to charging
* We also fall back here in case the device is locked and we do not
* export the system contents. Or if we are in acting dead mode.
*/
usbmoded_set_usb_mode(MODE_CHARGING_FALLBACK);
}
EXIT:
free(mode_to_set);
}
/** set the usb connection status
*
* @param connected The connection status, true for connected
*
*/
void usbmoded_set_usb_connected(bool connected)
{
if( !connected && android_ignore_next_udev_disconnect_event ) {
/* FIXME: udev event processing is changed so that
* disconnect notifications are not repeated
* so whatever this is supposed to do - it is
* broken.
*/
android_ignore_next_udev_disconnect_event = false;
goto EXIT;
}
/* Do not go through the routine if already connected to avoid
* spurious load/unloads due to faulty signalling
* NOKIA: careful with devicelock
*/
if( !usbmoded_set_connection_state(connected) )
goto EXIT;
if( usbmoded_get_connection_state() ) {
log_debug("usb connected\n");
/* signal usb connected */
umdbus_send_state_signal(USB_CONNECTED);
usbmoded_set_usb_connected_state();
}
else {
log_debug("usb disconnected\n");
usbmoded_set_usb_disconnected_state();
/* Some android kernels check for an active gadget to enable charging and
* cable detection, meaning USB is completely dead unless we keep the gadget
* active
*/
if(current_mode.android_usb_broken) {
android_set_charging_mode();
configfs_set_charging_mode();
}
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = true;
}
}
EXIT:
return;
}
/** set and track charger state
*
*/
void usbmoded_set_charger_connected(bool state)
{
/* check if charger is already connected
* to avoid spamming dbus */
if( !usbmoded_set_connection_state(state) )
goto EXIT;
if( usbmoded_get_connection_state() ) {
umdbus_send_state_signal(CHARGER_CONNECTED);
usbmoded_set_usb_mode(MODE_CHARGER);
}
else {
umdbus_send_state_signal(CHARGER_DISCONNECTED);
usbmoded_set_usb_mode(MODE_UNDEFINED);
}
EXIT:
return;
}
/** Get android usb function to setup during bootup
*
* @returns function name, or NULL if no function was requested
*/
const char *usbmoded_get_android_bootup_function(void)
{
return android_bootup_function;
}
/** Set android usb function to setup during bootup
*
* @param function usb function name, or NULL
*/
void usbmoded_set_android_bootup_function(const char *function)
{
char *value = function ? g_strdup(function) : 0;
g_free(android_bootup_function);
android_bootup_function = value;
}
/** Helper for setting allowed cable detection delay
*
* Used for implementing --max-cable-delay=<ms> option.
*/
static void usbmoded_set_cable_connection_delay(int delay_ms)
{
if( delay_ms < CABLE_CONNECTION_DELAY_MAXIMUM )
usbmoded_cable_connection_delay = delay_ms;
else {
usbmoded_cable_connection_delay = CABLE_CONNECTION_DELAY_MAXIMUM;
log_warning("using maximum connection delay: %d ms",
usbmoded_cable_connection_delay);
}
}
/* check if a mode is in a list */
static bool usbmoded_mode_in_list(const char *mode, char * const *modes)
{
int i;
if (!modes)
return false;
for(i = 0; modes[i] != NULL; i++)
{
if(!strcmp(modes[i], mode))
return true;
}
return false;
}
/** check if a given usb_mode exists
*
* @param mode The mode to look for
* @return 0 if mode exists, 1 if it does not exist
*
*/
int usbmoded_valid_mode(const char *mode)
{
int valid = 1;
/* MODE_ASK, MODE_CHARGER and MODE_CHARGING_FALLBACK are not modes that are settable seen their special 'internal' status
* so we only check the modes that are announed outside. Only exception is the built in MODE_CHARGING */
if(!strcmp(MODE_CHARGING, mode))
valid = 0;
else
{
char *whitelist;
gchar **whitelist_split = NULL;
whitelist = config_get_mode_whitelist();
if (whitelist)
{
whitelist_split = g_strsplit(whitelist, ",", 0);
g_free(whitelist);
}
/* check dynamic modes */
if(modelist)
{
GList *iter;
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if(!strcmp(mode, data->mode_name))
{
if (!whitelist_split || usbmoded_mode_in_list(data->mode_name, whitelist_split))
valid = 0;
break;
}
}
g_strfreev(whitelist_split);
}
}
return valid;
}
/** make a list of all available usb modes
*
* @param type The type of list to return. Supported or available.
* @return a comma-separated list of modes (MODE_ASK not included as it is not a real mode)
*
*/
gchar *usbmoded_get_mode_list(mode_list_type_t type)
{
GString *modelist_str;
modelist_str = g_string_new(NULL);