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