Skip to content

Latest commit

 

History

History
434 lines (363 loc) · 11.7 KB

usb_moded-modules.c

File metadata and controls

434 lines (363 loc) · 11.7 KB
 
Mar 22, 2011
Mar 22, 2011
1
/**
Aug 24, 2018
Aug 24, 2018
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
* @file usb_moded-modules.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: Thomas Perl <m@thp.io>
* @author: Slava Monich <slava.monich@jolla.com>
* @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
*/
Mar 22, 2011
Mar 22, 2011
29
30
31
32
33
34
35
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <glib.h>
Feb 1, 2013
Feb 1, 2013
36
#include <glib/gprintf.h>
Mar 22, 2011
Mar 22, 2011
37
Nov 13, 2012
Nov 13, 2012
38
39
#include <libkmod.h>
Mar 22, 2011
Mar 22, 2011
40
41
42
43
44
#include "usb_moded.h"
#include "usb_moded-modules.h"
#include "usb_moded-log.h"
#include "usb_moded-config.h"
#include "usb_moded-dbus-private.h"
Jun 28, 2011
Jun 28, 2011
45
#include "usb_moded-modesetting.h"
Jul 15, 2011
Jul 15, 2011
46
#include "usb_moded-modes.h"
Mar 22, 2011
Mar 22, 2011
47
Aug 24, 2018
Aug 24, 2018
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- modules -- */
bool modules_init (void);
bool modules_in_use (void);
void modules_quit (void);
int modules_load_module (const char *module);
int modules_unload_module (const char *module);
static int modules_module_is_loaded (const char *module);
const char *modules_get_loaded_module (void);
int modules_cleanup_module (const char *module);
static int modules_prepare_for_module_switch(int force);
void modules_check_module_state (const char *module_name);
/* ========================================================================= *
* Data
* ========================================================================= */
/* kmod context - initialized at start in usbmoded_init by ctx_init()
* and cleaned up by ctx_cleanup() functions */
Nov 7, 2016
Nov 7, 2016
71
static struct kmod_ctx *ctx = 0;
Nov 13, 2012
Nov 13, 2012
72
Aug 24, 2018
Aug 24, 2018
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
/* ========================================================================= *
* Functions
* ========================================================================= */
static bool modules_have_module(const char *module)
{
// TODO: not fully untested due to lack of suitable hw
bool ack = false;
struct kmod_list *list = 0;
if( kmod_module_new_from_lookup(ctx, module, &list) < 0 )
goto EXIT;
if( list == 0 )
goto EXIT;
ack = true;
EXIT:
if( list )
kmod_module_unref_list(list);
log_debug("module %s does%s exist", module, ack ? "" : "not ");
return ack;
}
static int modules_probed = -1;
bool modules_in_use(void)
{
if( modules_probed < 0 )
log_debug("modules_in_use() called before modules_probe()");
return modules_probed > 0;
}
static bool modules_probe(void)
{
static const char * const lut[] = {
MODULE_MASS_STORAGE,
MODULE_FILE_STORAGE,
MODULE_DEVELOPER,
MODULE_MTP,
0
};
if( modules_probed == -1 ) {
modules_probed = false;
/* Check if we have at least one of the kernel modules we
* expect to use for something.
*/
for( size_t i = 0; lut[i] ; ++i ) {
if( modules_have_module(lut[i]) ) {
modules_probed = true;
break;
}
}
log_warning("MODULES %sdetected", modules_probed ? "" : "not ");
}
return modules_in_use();
}
Aug 5, 2014
Aug 5, 2014
138
/* kmod module init */
Aug 24, 2018
Aug 24, 2018
139
bool modules_init(void)
Aug 5, 2014
Aug 5, 2014
140
{
Aug 24, 2018
Aug 24, 2018
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
bool ack = false;
if( !ctx ) {
if( !(ctx = kmod_new(NULL, NULL)) )
goto EXIT;
}
if( kmod_load_resources(ctx) < 0 )
goto EXIT;
if( !modules_probe() )
goto EXIT;
ack = true;
EXIT:
return ack;
Aug 5, 2014
Aug 5, 2014
157
158
159
}
/* kmod module cleanup */
Aug 24, 2018
Aug 24, 2018
160
void modules_quit(void)
Aug 5, 2014
Aug 5, 2014
161
{
Nov 7, 2016
Nov 7, 2016
162
if( ctx )
Aug 24, 2018
Aug 24, 2018
163
kmod_unref(ctx), ctx = 0;
Aug 5, 2014
Aug 5, 2014
164
165
}
Aug 24, 2018
Aug 24, 2018
166
/** load module
Mar 22, 2011
Mar 22, 2011
167
168
169
170
171
*
* @param module Name of the module to load
* @return 0 on success, non-zero on failure
*
*/
Aug 24, 2018
Aug 24, 2018
172
int modules_load_module(const char *module)
Mar 22, 2011
Mar 22, 2011
173
{
Aug 24, 2018
Aug 24, 2018
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
int ret = 0;
const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
struct kmod_module *mod;
char *charging_args = NULL;
char *load = NULL;
if(!strcmp(module, MODULE_NONE))
return 0;
if( !modules_in_use() ) {
log_warning("load module %s - without module support", module);
return -1;
}
/* copy module to load as it might be modified if we're trying charging mode */
load = strdup(module);
if(!strcmp(module, MODULE_CHARGING) || !strcmp(module, MODULE_CHARGE_FALLBACK))
{
/* split the string in module name and argument, they are the same for MODULE_CHARGE_FALLBACK
so no need to handle them separately */
gchar **strings;
/* since the mass_storage module is the newer one and we check against it to avoid
loading failures we use it here, as we fall back to g_file_storage if g_mass_storage
fails to load */
strings = g_strsplit(MODULE_CHARGE_FALLBACK, " ", 2);
//log_debug("module args = %s, module = %s\n", strings[1], strings[0]);
charging_args = strdup(strings[1]);
/* load was already assigned. Free it to re-assign */
free(load);
load = strdup(strings[0]);
g_strfreev(strings);
}
ret = kmod_module_new_from_name(ctx, load, &mod);
/* since kmod_module_new_from_name does not check if the module
exists we test it's path in case we deal with the mass-storage one */
if(!strcmp(module, MODULE_MASS_STORAGE) &&
(kmod_module_get_path(mod) == NULL))
{
log_debug("Fallback on older g_file_storage\n");
ret = kmod_module_new_from_name(ctx, MODULE_FILE_STORAGE, &mod);
}
if(!charging_args)
ret = kmod_module_probe_insert_module(mod, probe_flags, NULL, NULL, NULL, NULL);
else
{
ret = kmod_module_probe_insert_module(mod, probe_flags, charging_args, NULL, NULL, NULL);
free(charging_args);
}
kmod_module_unref(mod);
free(load);
if( ret == 0)
log_info("Module %s loaded successfully\n", module);
else
log_info("Module %s failed to load\n", module);
return(ret);
Mar 22, 2011
Mar 22, 2011
234
235
236
}
/** unload module
Aug 24, 2018
Aug 24, 2018
237
*
Mar 22, 2011
Mar 22, 2011
238
239
240
241
* @param module Name of the module to unload
* @return 0 on success, non-zero on failure
*
*/
Aug 24, 2018
Aug 24, 2018
242
int modules_unload_module(const char *module)
Mar 22, 2011
Mar 22, 2011
243
{
Aug 24, 2018
Aug 24, 2018
244
int ret = 0;
Mar 22, 2011
Mar 22, 2011
245
Aug 24, 2018
Aug 24, 2018
246
struct kmod_module *mod;
Nov 13, 2012
Nov 13, 2012
247
Aug 24, 2018
Aug 24, 2018
248
249
if(!strcmp(module, MODULE_NONE))
return 0;
May 27, 2013
May 27, 2013
250
Aug 24, 2018
Aug 24, 2018
251
252
253
254
if( !modules_in_use() ) {
log_warning("unload module %s - without module support", module);
return -1;
}
Nov 13, 2012
Nov 13, 2012
255
Aug 24, 2018
Aug 24, 2018
256
257
258
259
260
kmod_module_new_from_name(ctx, module, &mod);
ret = kmod_module_remove_module(mod, KMOD_REMOVE_NOWAIT);
kmod_module_unref(mod);
return(ret);
Mar 22, 2011
Mar 22, 2011
261
262
}
Nov 13, 2012
Nov 13, 2012
263
264
265
266
/** Check which state a module is in
*
* @return 1 if loaded, 0 when not loaded
*/
Aug 24, 2018
Aug 24, 2018
267
static int modules_module_is_loaded(const char *module)
Nov 13, 2012
Nov 13, 2012
268
{
Aug 24, 2018
Aug 24, 2018
269
270
271
272
273
274
275
276
277
278
int ret = 0;
struct kmod_module *mod;
kmod_module_new_from_name(ctx, module, &mod);
ret = kmod_module_get_initstate(mod);
kmod_module_unref(mod);
if( ret == KMOD_MODULE_LIVE)
return 1;
return 0;
Nov 13, 2012
Nov 13, 2012
279
280
}
Aug 24, 2018
Aug 24, 2018
281
/** find which module is loaded
Mar 22, 2011
Mar 22, 2011
282
283
284
*
* @return The name of the loaded module, or NULL if no modules are loaded.
*/
Aug 24, 2018
Aug 24, 2018
285
const char * modules_get_loaded_module(void)
Mar 22, 2011
Mar 22, 2011
286
{
Aug 24, 2018
Aug 24, 2018
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
if( !modules_in_use() ) {
log_warning("get loaded module - without module support");
return 0;
}
if(modules_module_is_loaded("g_ether"))
return(MODULE_DEVELOPER);
if(modules_module_is_loaded("g_ncm"))
return("g_ncm");
if(modules_module_is_loaded("g_ffs"))
return(MODULE_MTP);
if(modules_module_is_loaded("g_mass_storage"))
return(MODULE_MASS_STORAGE);
if(modules_module_is_loaded("g_file_storage"))
return(MODULE_FILE_STORAGE);
if(modules_module_is_loaded(usbmoded_get_usb_module()))
return(usbmoded_get_usb_module());
/* no module loaded */
return(0);
Mar 22, 2011
Mar 22, 2011
312
313
314
315
316
317
318
319
}
/** clean up for modules when usb gets disconnected
*
* @param module The name of the module to unload
* @return 0 on success, non-zero on failure
*
*/
Aug 24, 2018
Aug 24, 2018
320
int modules_cleanup_module(const char *module)
Mar 22, 2011
Mar 22, 2011
321
{
Aug 24, 2018
Aug 24, 2018
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
int retry = 0, failure;
if(!strcmp(module, MODULE_NONE))
goto END;
if( !modules_in_use() ) {
log_warning("cleanup module %s - without module support", module);
goto END;
}
/* wait a bit for all components listening on dbus to clean up their act
usbmoded_sleep(2); */
/* check if things were not reconnected in that timespan
if(usbmoded_get_connection_state())
return(0);
*/
failure = modules_unload_module(module);
/* if we have MODULE_MASS_STORAGE it might be MODULE_FILE_STORAGE might
be loaded. So check and unload that one if unloading fails first time */
if(failure && !strcmp(MODULE_MASS_STORAGE, module))
failure = modules_unload_module(MODULE_FILE_STORAGE);
/* if it still failed it might be the mode has not been cleaned-up correctly,
so we clean up the mode to be sure */
if(failure)
{
modesetting_cleanup(modules_get_loaded_module());
}
while(failure)
{
/* module did not get unloaded. We will wait a bit and try again */
usbmoded_sleep(1);
/* send the REALLY disconnect message */
umdbus_send_state_signal(USB_REALLY_DISCONNECT);
failure = modules_unload_module(module);
log_debug("unloading failure = %d\n", failure);
if(!failure)
break;
if(!modules_get_loaded_module())
goto END;
retry++;
if(retry == 2)
break;
}
if(!failure)
log_info("Module %s unloaded successfully\n", module);
else
{
log_err("Module %s did not unload! Failing and going to undefined.\n", module);
return(1);
}
Mar 22, 2011
Mar 22, 2011
376
END:
Aug 24, 2018
Aug 24, 2018
377
return(0);
Mar 22, 2011
Mar 22, 2011
378
379
380
381
382
383
384
385
386
}
/** try to unload modules to support switching
*
*
* @param force force unloading with a nasty clean-up on TRUE, or just try unloading when FALSE
* @return 0 on success, 1 on failure, 2 if hard clean-up failed
*/
Aug 24, 2018
Aug 24, 2018
387
static int modules_prepare_for_module_switch (int force)
Mar 22, 2011
Mar 22, 2011
388
{
Aug 24, 2018
Aug 24, 2018
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
if( !modules_in_use() ) {
log_warning("prep for module switch force=%d - without module support", force);
return 0;
}
const char *unload;
int ret = 1;
unload = modules_get_loaded_module();
if(unload)
{
if(force)
ret = modules_cleanup_module(unload);
else
ret = modules_unload_module(unload);
}
if(ret && force)
return(2);
return ret;
Mar 22, 2011
Mar 22, 2011
408
409
}
Aug 24, 2018
Aug 24, 2018
410
/** check for loaded modules and clean-up if they are not for the chosen mode
Mar 22, 2011
Mar 22, 2011
411
412
413
414
*
* @param module_name module name to check for
*
*/
Aug 24, 2018
Aug 24, 2018
415
void modules_check_module_state(const char *module_name)
Mar 22, 2011
Mar 22, 2011
416
{
Aug 24, 2018
Aug 24, 2018
417
418
419
420
421
422
if( !modules_in_use() ) {
log_warning("check module %s state - without module support", module_name);
return;
}
const char *module;
Mar 22, 2011
Mar 22, 2011
423
Aug 24, 2018
Aug 24, 2018
424
425
426
module = modules_get_loaded_module();
if(module != NULL)
{
Mar 22, 2011
Mar 22, 2011
427
/* do nothing if the right module is already loaded */
Aug 24, 2018
Aug 24, 2018
428
if(strcmp(module, module_name) != 0)
Mar 22, 2011
Mar 22, 2011
429
{
Aug 24, 2018
Aug 24, 2018
430
431
log_debug("%s not loaded, cleaning up\n", module_name);
modules_prepare_for_module_switch(TRUE);
Mar 22, 2011
Mar 22, 2011
432
}
Aug 24, 2018
Aug 24, 2018
433
}
Mar 22, 2011
Mar 22, 2011
434
}