Fixed to stop services of functionfs when gadget is disabled
[platform/core/system/libdevice-node.git] / hw / usb_gadget.h
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 #ifndef __HW_USB_GADGET_H__
20 #define __HW_USB_GADGET_H__
21
22 #include <hw/common.h>
23
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 /**
30  * The id of this device
31  */
32 #define USB_GADGET_DEVICE_ID    "usb_gadget"
33
34 /**
35  * The version of this device
36  */
37 #define USB_GADGET_DEVICE_VERSION       MAKE_VERSION(0,1)
38
39 typedef enum {
40         USB_FUNCTION_IDX_MTP         = 0,
41         USB_FUNCTION_IDX_ACM         = 1,
42         USB_FUNCTION_IDX_SDB         = 2,
43         USB_FUNCTION_IDX_RNDIS       = 3,
44         USB_FUNCTION_IDX_DIAG        = 4,
45         USB_FUNCTION_IDX_CONN_GADGET = 5,
46         USB_FUNCTION_IDX_DM          = 6,
47         USB_FUNCTION_IDX_RMNET       = 7,
48         USB_FUNCTION_IDX_MAX         = USB_FUNCTION_IDX_RMNET + 1
49 } usb_function_idx_e;
50
51 typedef enum {
52         USB_FUNCTION_NONE        = 0,
53         USB_FUNCTION_MTP         = 1 << USB_FUNCTION_IDX_MTP,
54         USB_FUNCTION_ACM         = 1 << USB_FUNCTION_IDX_ACM,
55         USB_FUNCTION_SDB         = 1 << USB_FUNCTION_IDX_SDB,
56         USB_FUNCTION_RNDIS       = 1 << USB_FUNCTION_IDX_RNDIS,
57         USB_FUNCTION_DIAG        = 1 << USB_FUNCTION_IDX_DIAG,
58         USB_FUNCTION_CONN_GADGET = 1 << USB_FUNCTION_IDX_CONN_GADGET,
59         USB_FUNCTION_DM          = 1 << USB_FUNCTION_IDX_DM,
60         USB_FUNCTION_RMNET       = 1 << USB_FUNCTION_IDX_RMNET
61 } usb_function_e;
62
63
64 /*
65  * legacy enable(usb plug)    : enable gadget -> handler(1) -> ffs_service start -> service start
66  * legacy disable(usb unplug) : service stop -> ffs_service stop -> handler(0) -> disable gadget
67  *
68  * configfs init(booting)       : ffs_service.socket start
69  * configfs enable(usb plug)*   : enable gadget -> handler(1) -> service start
70  * configfs disable(usb unplug) : service stop -> handler(0) -> disable gadget -> ffs_service.service stop
71  * configfs deinit              : ffs_service.socket stop
72  *
73  * Since ffs_service works by socket activation, it will be started automatically when data is enqueued to the usb socket.
74  * So when enabling configfs gadget, it doesn't start ffs_service.
75  */
76 struct usb_function {
77         int id;
78         const char *name;
79         const char *instance;
80
81         const char *ffs_service; /* only used in configfs */
82         const char *service;
83
84         void (*handler)(int enable);
85
86         int (*clone)(struct usb_function *func, struct usb_function **_clone);
87         void (*free_func)(struct usb_function *func);
88 };
89
90 struct usb_configuration_attributes {
91         uint8_t bmAttributs;
92         int MaxPower;
93 };
94
95 struct usb_configuration_strings {
96         uint16_t lang_code;
97         char *config_str;
98 };
99
100 struct usb_configuration {
101         struct usb_configuration_attributes attrs;
102         struct usb_configuration_strings *strs;
103         struct usb_function **funcs;
104 };
105
106 struct usb_gadget_attrs {
107         uint8_t bDeviceClass;
108         uint8_t bDeviceSubClass;
109         uint8_t bDeviceProtocol;
110         uint16_t idVendor;
111         uint16_t idProduct;
112         uint16_t bcdDevice;
113 };
114
115 struct usb_gadget_strings {
116         uint16_t lang_code;
117         char *manufacturer;
118         char *product;
119         char *serial;
120 };
121
122 struct usb_gadget {
123         struct usb_gadget_attrs attrs;
124         struct usb_gadget_strings *strs;
125         struct usb_function **funcs;
126         struct usb_configuration **configs;
127 };
128
129 struct usb_gadget_id {
130         unsigned int function_mask;
131 };
132
133 struct usb_gadget_translator {
134         struct hw_common common;
135
136         int (*id_to_gadget)(struct usb_gadget_id *gadget_id,
137                          struct usb_gadget **gadget);
138
139         void (*cleanup_gadget)(struct usb_gadget *gadget);
140 };
141
142 int simple_translator_open(struct hw_info *info,
143                 const char *id, struct hw_common **common);
144 int simple_translator_close(struct hw_common *common);
145
146 extern struct usb_function *_available_funcs[];
147
148 #endif