usb-gadget: access external symbol using dlsym(), not extern keyword
[platform/hal/backend/device-common.git] / src / usb_gadget / usb_cfs_client_common.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <errno.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/mount.h>
23 #include <linux/limits.h>
24
25 #include <usbg/usbg.h>
26 #include <usbg/function/net.h>
27
28 #include <libsyscommon/libsystemd.h>
29 #include <hal/device/hal-usb_gadget-interface.h>
30
31 #include "usb_gadget.h"
32 #include "hal-backend-common.h"
33
34 #define CONFIGFS_PATH "/sys/kernel/config"
35
36 #define CONFIGFS_GADGET_NAME "hal-gadget"
37 #define CONFIGFS_CONFIG_LABEL "hal-config"
38
39 #define NAME_INSTANCE_SEP '.'
40 #define MAX_INSTANCE_LEN 512
41
42 #define USB_FUNCS_PATH "/dev/usb-funcs"
43
44 enum cfs_function_service_operation {
45         CFS_FUNCTION_SERVICE_START,
46         CFS_FUNCTION_SERVICE_STOP,
47         CFS_FUNCTION_SERVICE_POST_STOP,
48 };
49
50 struct cfs_client {
51         usbg_state *ctx;
52         usbg_gadget *gadget;
53         usbg_udc *udc;
54 };
55
56 /* Based on values in slp-gadget kernel module */
57 struct usbg_gadget_attrs default_g_attrs = {
58         .idVendor = DEFAULT_VID,
59         .idProduct = DEFAULT_PID,
60         .bcdDevice = DEFAULT_BCD_DEVICE,
61 };
62
63 struct usbg_gadget_strs default_g_strs = {
64         .manufacturer = DEFAULT_MANUFACTURER,
65         .product = DEFAULT_PRODUCT,
66         .serial = DEFAULT_SERIAL,
67 };
68
69 struct cfs_client *g_cfs_client;
70
71 static struct usb_function *cfs_find_usb_function(usbg_function *function)
72 {
73         char *sep;
74         char buf[MAX_INSTANCE_LEN];
75         const char *instance = usbg_get_function_instance(function);
76         const char *name = usbg_get_function_type_str(usbg_get_function_type(function));
77
78         /* Ex. name:"ffs",  instance: "sdb.default" */
79         if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) {
80                 strncpy(buf, instance, sizeof(buf) - 1);
81                 buf[sizeof(buf) - 1] = '\0';
82
83                 /* Ex. "sdb.default" ==> "sdb" + "default" */
84                 sep = strchr(buf, NAME_INSTANCE_SEP);
85                 if (!sep || !sep[1])
86                         return NULL;
87                 *sep = '\0';
88
89                 name = buf;
90                 instance = sep + 1;
91         }
92
93         return find_usb_function_by_name_instance(name, instance);
94 }
95
96 static bool cfs_is_function_supported(struct usb_function *func)
97 {
98         char buf[PATH_MAX];
99
100         if (func->is_functionfs) {
101                 /* functionfs must have a service */
102                 if (!func->service)
103                         return false;
104
105                 snprintf(buf, sizeof(buf), "/usr/lib/systemd/system/%s.socket", func->service);
106                 if (access(buf, F_OK))
107                         return false;
108         } else {
109                 if (usbg_lookup_function_type(func->name) < 0)
110                         return false;
111         }
112
113         return true;
114 }
115
116 static bool cfs_is_gadget_supported(struct usb_gadget *gadget)
117 {
118         int i, j;
119         struct usb_configuration *config;
120
121         if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
122                 return false;
123
124         if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
125                 return false;
126
127         /* only strings in US_en are allowed */
128         if (gadget->strs.lang_code != DEFAULT_LANG)
129                 return false;
130
131         if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
132                 return false;
133
134         for (j = 0; gadget->configs[j]; ++j) {
135                 config = gadget->configs[j];
136
137                 if (!config->funcs)
138                         return false;
139
140                 for (i = 0; config->funcs[i]; ++i)
141                         if (!cfs_is_function_supported(config->funcs[i]))
142                                 return false;
143         }
144
145         return true;
146 }
147
148 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
149                                 struct usb_gadget_attrs *attrs)
150 {
151         int ret;
152         struct usbg_gadget_attrs gadget_attrs;
153
154         ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
155         if (ret)
156                 return ret;
157
158         gadget_attrs.bDeviceClass = attrs->bDeviceClass;
159         gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
160         gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
161         gadget_attrs.idVendor = attrs->idVendor;
162         gadget_attrs.idProduct = attrs->idProduct;
163         gadget_attrs.bcdDevice = attrs->bcdDevice;
164
165         ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
166
167         return ret;
168 }
169
170 static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
171 {
172         int ret;
173
174         if (!strs->manufacturer || !strs->product || !strs->serial)
175                 return -EINVAL;
176
177         ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
178         if (ret)
179                 return ret;
180
181         ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
182         if (ret)
183                 return ret;
184
185         ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_SERIAL_NUMBER, strs->lang_code, strs->serial);
186         if (ret)
187                 return ret;
188
189         return 0;
190 }
191
192 static int cfs_ensure_dir(char *path)
193 {
194         if (mkdir(path, 0770) < 0)
195                 return (errno == EEXIST) ? 0 : -errno;
196
197         return 0;
198 }
199
200
201 static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
202 {
203         int ret;
204         const char *name;
205         const char *service;
206         const char *instance;
207         const char *dev_name;
208         char buf[MAX_INSTANCE_LEN];
209
210         if (!usb_func || !function)
211                 return -EINVAL;
212
213         if (usbg_get_function_type(function) != USBG_F_FFS)
214                 return -EINVAL;
215
216         name = usb_func->name;
217         service = usb_func->service;
218         instance = usb_func->instance;
219         dev_name = usbg_get_function_instance(function);
220
221         /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default"  + '0' */
222         if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3  > sizeof(buf))
223                 return -ENAMETOOLONG;
224
225         /* mkdir /dev/usb-funcs */
226         ret = cfs_ensure_dir(USB_FUNCS_PATH);
227         if (ret < 0)
228                 return ret;
229
230         /* mkdir /dev/usb-funcs/sdb */
231         snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
232         ret = cfs_ensure_dir(buf);
233         if (ret < 0)
234                 goto out_rmdir;
235
236         /* mkdir /dev/usb-funcs/sdb/default */
237         snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
238         ret = cfs_ensure_dir(buf);
239         if (ret < 0)
240                 goto out_rmdir;
241
242         /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
243         ret = mount(dev_name, buf, "functionfs", 0, NULL);
244         if (ret < 0)
245                 goto out_rmdir;
246
247         /* start sdbd.socket */
248         ret = systemd_start_unit_wait_started(service, ".socket", -1);
249         if (ret < 0)
250                 goto out_unmount;
251
252         return 0;
253
254 out_unmount:
255         umount(buf);
256
257 out_rmdir:
258         snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
259         rmdir(buf);
260
261         snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
262         rmdir(buf);
263
264         rmdir(USB_FUNCS_PATH);
265
266         return ret;
267 }
268
269 static int cfs_cleanup_ffs_service(usbg_function *function)
270 {
271         int ret;
272         char buf[MAX_INSTANCE_LEN];
273         struct usb_function *usb_function;
274
275         if (!function)
276                 return -EINVAL;
277
278         usb_function = cfs_find_usb_function(function);
279         if (!usb_function)
280                 return -ENOENT;
281
282         /* stop .socket first and stop .service later becuase of socket activation */
283         if (usb_function->service) {
284                 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
285                 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
286         }
287
288         /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
289         ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
290         if (ret < 0)
291                 return ret;
292
293         ret = umount(buf);
294         if (ret < 0)
295                 return ret;
296
297         ret = rmdir(buf);
298         if (ret < 0)
299                 return ret;
300
301         /* remove /dev/usb-funcs/[sdb|mtp] directory */
302         ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
303         if (ret < 0)
304                 return ret;
305
306         ret = rmdir(buf);
307         if (ret < 0 && errno != ENOTEMPTY)
308                 return ret;
309
310         /* remove /dev/usb-funcs/ directory */
311         ret = rmdir(USB_FUNCS_PATH);
312         if (ret < 0 && errno != ENOTEMPTY)
313                 return ret;
314
315         return 0;
316 }
317
318
319 static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
320 {
321         int i, ret;
322         struct ether_addr ethaddr;
323         struct usbg_gadget_strs strs;
324         struct usbg_f_net *nf = usbg_to_net_function(func);
325
326         if (!nf)
327                 return -EINVAL;
328
329         ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
330         if (ret != USBG_SUCCESS)
331                 return ret;
332
333         for (i = 0; i < ETHER_ADDR_LEN; i++)
334                 ethaddr.ether_addr_octet[i] = 0;
335
336         for (i = 0; (i < 256) && strs.serial[i]; i++) {
337                 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
338         }
339         ethaddr.ether_addr_octet[0] &= 0xfe;     /* clear multicast bit */
340         ethaddr.ether_addr_octet[0] |= 0x02;     /* set local assignment bit (IEEE802) */
341
342         usbg_free_gadget_strs(&strs);
343
344         /* host_addr changes mac address */
345         ret = usbg_f_net_set_host_addr(nf, &ethaddr);
346
347         return ret;
348 }
349
350 static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
351 {
352         int ret;
353         usbg_config *config;
354         usbg_function *function;
355
356         /* delete all configs */
357 restart_rm_config:
358         usbg_for_each_config(config, cfs_client->gadget) {
359                 ret = usbg_rm_config(config, USBG_RM_RECURSE);
360                 if (ret)
361                         return ret;
362
363                 goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
364         }
365
366         /* delete all functions */
367 restart_rm_function:
368         usbg_for_each_function(function, cfs_client->gadget) {
369                 if (usbg_get_function_type(function) == USBG_F_FFS) {
370                         ret = cfs_cleanup_ffs_service(function);
371                         if (ret)
372                                 return ret;
373                 }
374
375                 ret = usbg_rm_function(function, USBG_RM_RECURSE);
376                 if (ret)
377                         return ret;
378
379                 goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
380         }
381
382         return 0;
383 }
384
385 static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
386 {
387         int i;
388         int ret;
389         int function_type;
390         usbg_config *config;
391         usbg_function *function;
392         struct usb_function *usb_func;
393         char instance[MAX_INSTANCE_LEN];
394         struct usbg_config_attrs cattrs = {
395                 .bmAttributes = usb_config->attrs.bmAttributs,
396                 .bMaxPower = usb_config->attrs.MaxPower/2,
397         };
398
399         if (!usb_config->funcs || !usb_config->funcs[0])
400                 return -EINVAL;
401
402         ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
403         if (ret)
404                 return ret;
405
406         if (usb_config->strs.config_str) {
407                 ret = usbg_set_config_string(config, usb_config->strs.lang_code, usb_config->strs.config_str);
408                 if (ret)
409                         return ret;
410         }
411
412         for (i = 0; usb_config->funcs[i]; ++i) {
413                 usb_func = usb_config->funcs[i];
414
415                 /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
416                 if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
417                         return -ENAMETOOLONG;
418
419                 /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
420                 if (usb_func->is_functionfs) {
421                         function_type = USBG_F_FFS;
422                         snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
423                 } else {
424                         function_type = usbg_lookup_function_type(usb_func->name);
425                         strncpy(instance, usb_func->instance, sizeof(instance) - 1);
426                         instance[sizeof(instance) - 1] = '\0';
427                 }
428
429                 function = usbg_get_function(cfs_client->gadget, function_type, instance);
430                 if (!function) {
431                         ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
432                         if (ret)
433                                 return ret;
434
435                         /* Setting rndis mac address. This should be done at this point,
436                          * since the node host_addr changes to read only after the function
437                          * is added to config. */
438                         if (usbg_get_function_type(function) == USBG_F_RNDIS)
439                                 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
440
441                         if (usbg_get_function_type(function) == USBG_F_FFS) {
442                                 ret = cfs_prep_ffs_service(usb_func, function);
443                                 if (ret)
444                                         return ret;
445                         }
446                 }
447
448                 ret = usbg_add_config_function(config, NULL, function);
449                 if (ret)
450                         return ret;
451         }
452
453         return 0;
454 }
455
456 static int cfs_reconfigure_gadget(struct usb_gadget *gadget)
457 {
458         int i;
459         int ret;
460
461         if (!g_cfs_client || !gadget)
462                 return -EINVAL;
463
464         /* Verify the gadget and check if function is supported */
465         if (!cfs_is_gadget_supported(gadget))
466                 return -ENOTSUP;
467
468         ret = cfs_set_gadget_attrs(g_cfs_client, &gadget->attrs);
469         if (ret)
470                 return ret;
471
472         ret = cfs_set_gadget_strs(g_cfs_client, &gadget->strs);
473         if (ret)
474                 return ret;
475
476         ret = cfs_cleanup_all_config_and_function(g_cfs_client);
477         if (ret)
478                 return ret;
479
480         for (i = 0; gadget->configs[i]; ++i) {
481                 ret = cfs_set_gadget_config(g_cfs_client, i + 1, gadget->configs[i]);
482                 if (ret)
483                         return ret;
484         }
485
486         return 0;
487 }
488
489 static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
490 {
491         usbg_function *function;
492         struct usb_function *usb_function;
493
494         usbg_for_each_function(function, gadget) {
495                 usb_function = cfs_find_usb_function(function);
496                 if (!usb_function)
497                         continue;
498
499                 switch(operation) {
500                 case CFS_FUNCTION_SERVICE_START:
501                         if (usb_function->handler)
502                                 usb_function->handler(1);
503
504                         /* functionfs service is automatically started by socket activation */
505                         if (!usb_function->is_functionfs && usb_function->service)
506                                 (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
507                         break;
508
509                 case CFS_FUNCTION_SERVICE_STOP:
510                         if (!usb_function->is_functionfs && usb_function->service && !usb_function->remain_after_disable)
511                                 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
512
513                         if (usb_function->handler)
514                                 usb_function->handler(0);
515                         break;
516
517                 case CFS_FUNCTION_SERVICE_POST_STOP:
518                         if (usb_function->is_functionfs && usb_function->service && !usb_function->remain_after_disable)
519                                 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
520                         break;
521
522                 default:
523                         break;
524                 }
525         }
526 }
527
528 static int cfs_enable(void)
529 {
530         int ret;
531
532         if (!g_cfs_client)
533                 return -EINVAL;
534
535         ret = usbg_enable_gadget(g_cfs_client->gadget, g_cfs_client->udc);
536         if (ret)
537                 return ret;
538
539         cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
540
541         return 0;
542 }
543
544 static int cfs_disable(void)
545 {
546         int ret;
547
548         if (!g_cfs_client)
549                 return -EINVAL;
550
551         cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
552
553         ret = usbg_disable_gadget(g_cfs_client->gadget); /* ignore error checking */
554
555         /*
556          * Since functionfs service works with socket activation, you must stop it after disabling gadget.
557          * If usb data may come in after stopping functionfs service and before disabling gadget,
558          * functionfs service wakes up again by socket activation.
559          */
560         cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
561
562         return ret;
563 }
564
565 EXPORT
566 int hw_cfs_gadget_open(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
567 {
568         int ret;
569
570         if (!usb_gadget_funcs)
571                 return -EINVAL;
572
573         g_cfs_client = calloc(1, sizeof(*g_cfs_client));
574         if (!g_cfs_client)
575                 return -ENOMEM;
576
577         ret = usbg_init(CONFIGFS_PATH, &g_cfs_client->ctx);
578         if (ret)
579                 goto err_usbg_init;
580
581         g_cfs_client->udc = usbg_get_first_udc(g_cfs_client->ctx);
582         if (!g_cfs_client->udc) {
583                 ret = -ENODEV;
584                 goto err_no_udc;
585         }
586
587         ret = usbg_create_gadget(g_cfs_client->ctx, CONFIGFS_GADGET_NAME,
588                                  &default_g_attrs, &default_g_strs, &g_cfs_client->gadget);
589         if (ret)
590                 goto err_create_gadget;
591
592         usb_gadget_funcs->enable = cfs_enable;
593         usb_gadget_funcs->disable = cfs_disable;
594         usb_gadget_funcs->reconfigure_gadget = cfs_reconfigure_gadget;
595
596         return 0;
597
598 err_create_gadget:
599 err_no_udc:
600         usbg_cleanup(g_cfs_client->ctx);
601 err_usbg_init:
602         free(g_cfs_client);
603
604         return ret;
605 }
606
607 EXPORT
608 int hw_cfs_gadget_close(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
609 {
610         usbg_function *function;
611         struct usb_function *usb_func;
612
613         if (!usb_gadget_funcs || !g_cfs_client)
614                 return -EINVAL;
615
616         usbg_for_each_function(function, g_cfs_client->gadget) {
617                 usb_func = cfs_find_usb_function(function);
618                 if (!usb_func)
619                         continue;
620
621                 if (usb_func->is_functionfs && usb_func->service) {
622                         (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
623                         (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
624                 }
625         }
626
627         /*
628          * For now we don't check for errors
629          * but we should somehow handle them
630          */
631         usbg_rm_gadget(g_cfs_client->gadget, USBG_RM_RECURSE);
632         usbg_cleanup(g_cfs_client->ctx);
633         free(g_cfs_client);
634
635         return 0;
636 }
637