usb_gadget: apply next HAL architecture (hal api + backend)
[platform/adaptation/tw3/device-manager-plugin-tw3.git] / hw / usb_gadget / usb_gadget.c
1 /*
2  * libdevice-node
3  *
4  * Copyright (c) 2016 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 <stdlib.h>
21
22 #include <hal/hal-common-interface.h>
23 #include <hal/device/hal-common-interface.h>
24 #include <hal/device/hal-usb_gadget-interface.h>
25
26 #include "common.h"
27
28 static int usb_gadget_init(void **data)
29 {
30         hal_backend_usb_gadget_funcs *usb_gadget_funcs;
31
32         if (!data)
33                 return -EINVAL;
34
35         usb_gadget_funcs = calloc(1, sizeof(hal_backend_usb_gadget_funcs));
36         if (!usb_gadget_funcs)
37                 return -ENOMEM;
38
39         // usb_gadget_translator
40         if (simple_translator_open(usb_gadget_funcs)) {
41                 _E("No USB gadget translator");
42                 goto error_translator_open;
43         }
44
45         // usb_client
46         if (hw_cfs_gadget_open(usb_gadget_funcs)) {
47                 _I("No USB client");
48                 goto error_gadget_open;
49         }
50
51         *data = (void *)usb_gadget_funcs;
52
53         return 0;
54
55 error_gadget_open:
56         simple_translator_close(usb_gadget_funcs);
57
58 error_translator_open:
59         free(usb_gadget_funcs);
60
61         return -ENODEV;
62 }
63
64 static int usb_gadget_exit(void *data)
65 {
66         hal_backend_usb_gadget_funcs *usb_gadget_funcs = (hal_backend_usb_gadget_funcs *)data;
67
68         if (usb_gadget_funcs) {
69                 hw_cfs_gadget_close(usb_gadget_funcs);
70                 simple_translator_close(usb_gadget_funcs);
71                 free(usb_gadget_funcs);
72         }
73
74         return 0;
75 }
76
77 hal_backend EXPORT hal_backend_device_usb_gadget_data = {
78         .name = "usb_gadget",
79         .vendor = "R800",
80         .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
81         .init = usb_gadget_init,
82         .exit = usb_gadget_exit,
83 };