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