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