GNU Linux-libre 4.14.254-gnu1
[releases.git] / drivers / staging / greybus / operation.h
1 /*
2  * Greybus operations
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #ifndef __OPERATION_H
11 #define __OPERATION_H
12
13 #include <linux/completion.h>
14
15 struct gb_operation;
16
17 /* The default amount of time a request is given to complete */
18 #define GB_OPERATION_TIMEOUT_DEFAULT    1000    /* milliseconds */
19
20 /*
21  * The top bit of the type in an operation message header indicates
22  * whether the message is a request (bit clear) or response (bit set)
23  */
24 #define GB_MESSAGE_TYPE_RESPONSE        ((u8)0x80)
25
26 enum gb_operation_result {
27         GB_OP_SUCCESS           = 0x00,
28         GB_OP_INTERRUPTED       = 0x01,
29         GB_OP_TIMEOUT           = 0x02,
30         GB_OP_NO_MEMORY         = 0x03,
31         GB_OP_PROTOCOL_BAD      = 0x04,
32         GB_OP_OVERFLOW          = 0x05,
33         GB_OP_INVALID           = 0x06,
34         GB_OP_RETRY             = 0x07,
35         GB_OP_NONEXISTENT       = 0x08,
36         GB_OP_UNKNOWN_ERROR     = 0xfe,
37         GB_OP_MALFUNCTION       = 0xff,
38 };
39
40 #define GB_OPERATION_MESSAGE_SIZE_MIN   sizeof(struct gb_operation_msg_hdr)
41 #define GB_OPERATION_MESSAGE_SIZE_MAX   U16_MAX
42
43 /*
44  * Protocol code should only examine the payload and payload_size fields, and
45  * host-controller drivers may use the hcpriv field. All other fields are
46  * intended to be private to the operations core code.
47  */
48 struct gb_message {
49         struct gb_operation             *operation;
50         struct gb_operation_msg_hdr     *header;
51
52         void                            *payload;
53         size_t                          payload_size;
54
55         void                            *buffer;
56
57         void                            *hcpriv;
58 };
59
60 #define GB_OPERATION_FLAG_INCOMING              BIT(0)
61 #define GB_OPERATION_FLAG_UNIDIRECTIONAL        BIT(1)
62 #define GB_OPERATION_FLAG_SHORT_RESPONSE        BIT(2)
63 #define GB_OPERATION_FLAG_CORE                  BIT(3)
64
65 #define GB_OPERATION_FLAG_USER_MASK     (GB_OPERATION_FLAG_SHORT_RESPONSE | \
66                                          GB_OPERATION_FLAG_UNIDIRECTIONAL)
67
68 /*
69  * A Greybus operation is a remote procedure call performed over a
70  * connection between two UniPro interfaces.
71  *
72  * Every operation consists of a request message sent to the other
73  * end of the connection coupled with a reply message returned to
74  * the sender.  Every operation has a type, whose interpretation is
75  * dependent on the protocol associated with the connection.
76  *
77  * Only four things in an operation structure are intended to be
78  * directly usable by protocol handlers:  the operation's connection
79  * pointer; the operation type; the request message payload (and
80  * size); and the response message payload (and size).  Note that a
81  * message with a 0-byte payload has a null message payload pointer.
82  *
83  * In addition, every operation has a result, which is an errno
84  * value.  Protocol handlers access the operation result using
85  * gb_operation_result().
86  */
87 typedef void (*gb_operation_callback)(struct gb_operation *);
88 struct gb_operation {
89         struct gb_connection    *connection;
90         struct gb_message       *request;
91         struct gb_message       *response;
92
93         unsigned long           flags;
94         u8                      type;
95         u16                     id;
96         int                     errno;          /* Operation result */
97
98         struct work_struct      work;
99         gb_operation_callback   callback;
100         struct completion       completion;
101         struct timer_list       timer;
102
103         struct kref             kref;
104         atomic_t                waiters;
105
106         int                     active;
107         struct list_head        links;          /* connection->operations */
108 };
109
110 static inline bool
111 gb_operation_is_incoming(struct gb_operation *operation)
112 {
113         return operation->flags & GB_OPERATION_FLAG_INCOMING;
114 }
115
116 static inline bool
117 gb_operation_is_unidirectional(struct gb_operation *operation)
118 {
119         return operation->flags & GB_OPERATION_FLAG_UNIDIRECTIONAL;
120 }
121
122 static inline bool
123 gb_operation_short_response_allowed(struct gb_operation *operation)
124 {
125         return operation->flags & GB_OPERATION_FLAG_SHORT_RESPONSE;
126 }
127
128 static inline bool gb_operation_is_core(struct gb_operation *operation)
129 {
130         return operation->flags & GB_OPERATION_FLAG_CORE;
131 }
132
133 void gb_connection_recv(struct gb_connection *connection,
134                                         void *data, size_t size);
135
136 int gb_operation_result(struct gb_operation *operation);
137
138 size_t gb_operation_get_payload_size_max(struct gb_connection *connection);
139 struct gb_operation *
140 gb_operation_create_flags(struct gb_connection *connection,
141                                 u8 type, size_t request_size,
142                                 size_t response_size, unsigned long flags,
143                                 gfp_t gfp);
144
145 static inline struct gb_operation *
146 gb_operation_create(struct gb_connection *connection,
147                                 u8 type, size_t request_size,
148                                 size_t response_size, gfp_t gfp)
149 {
150         return gb_operation_create_flags(connection, type, request_size,
151                                                 response_size, 0, gfp);
152 }
153
154 struct gb_operation *
155 gb_operation_create_core(struct gb_connection *connection,
156                                 u8 type, size_t request_size,
157                                 size_t response_size, unsigned long flags,
158                                 gfp_t gfp);
159
160 void gb_operation_get(struct gb_operation *operation);
161 void gb_operation_put(struct gb_operation *operation);
162
163 bool gb_operation_response_alloc(struct gb_operation *operation,
164                                         size_t response_size, gfp_t gfp);
165
166 int gb_operation_request_send(struct gb_operation *operation,
167                                 gb_operation_callback callback,
168                                 unsigned int timeout,
169                                 gfp_t gfp);
170 int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
171                                                 unsigned int timeout);
172 static inline int
173 gb_operation_request_send_sync(struct gb_operation *operation)
174 {
175         return gb_operation_request_send_sync_timeout(operation,
176                         GB_OPERATION_TIMEOUT_DEFAULT);
177 }
178
179 void gb_operation_cancel(struct gb_operation *operation, int errno);
180 void gb_operation_cancel_incoming(struct gb_operation *operation, int errno);
181
182 void greybus_message_sent(struct gb_host_device *hd,
183                                 struct gb_message *message, int status);
184
185 int gb_operation_sync_timeout(struct gb_connection *connection, int type,
186                                 void *request, int request_size,
187                                 void *response, int response_size,
188                                 unsigned int timeout);
189 int gb_operation_unidirectional_timeout(struct gb_connection *connection,
190                                 int type, void *request, int request_size,
191                                 unsigned int timeout);
192
193 static inline int gb_operation_sync(struct gb_connection *connection, int type,
194                       void *request, int request_size,
195                       void *response, int response_size)
196 {
197         return gb_operation_sync_timeout(connection, type,
198                         request, request_size, response, response_size,
199                         GB_OPERATION_TIMEOUT_DEFAULT);
200 }
201
202 static inline int gb_operation_unidirectional(struct gb_connection *connection,
203                                 int type, void *request, int request_size)
204 {
205         return gb_operation_unidirectional_timeout(connection, type,
206                         request, request_size, GB_OPERATION_TIMEOUT_DEFAULT);
207 }
208
209 int gb_operation_init(void);
210 void gb_operation_exit(void);
211
212 #endif /* !__OPERATION_H */