1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
8 * Released under the GPLv2 only.
14 #include <linux/completion.h>
18 /* The default amount of time a request is given to complete */
19 #define GB_OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */
22 * The top bit of the type in an operation message header indicates
23 * whether the message is a request (bit clear) or response (bit set)
25 #define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80)
27 enum gb_operation_result
{
29 GB_OP_INTERRUPTED
= 0x01,
31 GB_OP_NO_MEMORY
= 0x03,
32 GB_OP_PROTOCOL_BAD
= 0x04,
33 GB_OP_OVERFLOW
= 0x05,
36 GB_OP_NONEXISTENT
= 0x08,
37 GB_OP_UNKNOWN_ERROR
= 0xfe,
38 GB_OP_MALFUNCTION
= 0xff,
41 #define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr)
42 #define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX
45 * Protocol code should only examine the payload and payload_size fields, and
46 * host-controller drivers may use the hcpriv field. All other fields are
47 * intended to be private to the operations core code.
50 struct gb_operation
*operation
;
51 struct gb_operation_msg_hdr
*header
;
61 #define GB_OPERATION_FLAG_INCOMING BIT(0)
62 #define GB_OPERATION_FLAG_UNIDIRECTIONAL BIT(1)
63 #define GB_OPERATION_FLAG_SHORT_RESPONSE BIT(2)
64 #define GB_OPERATION_FLAG_CORE BIT(3)
66 #define GB_OPERATION_FLAG_USER_MASK (GB_OPERATION_FLAG_SHORT_RESPONSE | \
67 GB_OPERATION_FLAG_UNIDIRECTIONAL)
70 * A Greybus operation is a remote procedure call performed over a
71 * connection between two UniPro interfaces.
73 * Every operation consists of a request message sent to the other
74 * end of the connection coupled with a reply message returned to
75 * the sender. Every operation has a type, whose interpretation is
76 * dependent on the protocol associated with the connection.
78 * Only four things in an operation structure are intended to be
79 * directly usable by protocol handlers: the operation's connection
80 * pointer; the operation type; the request message payload (and
81 * size); and the response message payload (and size). Note that a
82 * message with a 0-byte payload has a null message payload pointer.
84 * In addition, every operation has a result, which is an errno
85 * value. Protocol handlers access the operation result using
86 * gb_operation_result().
88 typedef void (*gb_operation_callback
)(struct gb_operation
*);
90 struct gb_connection
*connection
;
91 struct gb_message
*request
;
92 struct gb_message
*response
;
97 int errno
; /* Operation result */
99 struct work_struct work
;
100 gb_operation_callback callback
;
101 struct completion completion
;
102 struct timer_list timer
;
108 struct list_head links
; /* connection->operations */
114 gb_operation_is_incoming(struct gb_operation
*operation
)
116 return operation
->flags
& GB_OPERATION_FLAG_INCOMING
;
120 gb_operation_is_unidirectional(struct gb_operation
*operation
)
122 return operation
->flags
& GB_OPERATION_FLAG_UNIDIRECTIONAL
;
126 gb_operation_short_response_allowed(struct gb_operation
*operation
)
128 return operation
->flags
& GB_OPERATION_FLAG_SHORT_RESPONSE
;
131 static inline bool gb_operation_is_core(struct gb_operation
*operation
)
133 return operation
->flags
& GB_OPERATION_FLAG_CORE
;
136 void gb_connection_recv(struct gb_connection
*connection
,
137 void *data
, size_t size
);
139 int gb_operation_result(struct gb_operation
*operation
);
141 size_t gb_operation_get_payload_size_max(struct gb_connection
*connection
);
142 struct gb_operation
*
143 gb_operation_create_flags(struct gb_connection
*connection
,
144 u8 type
, size_t request_size
,
145 size_t response_size
, unsigned long flags
,
148 static inline struct gb_operation
*
149 gb_operation_create(struct gb_connection
*connection
,
150 u8 type
, size_t request_size
,
151 size_t response_size
, gfp_t gfp
)
153 return gb_operation_create_flags(connection
, type
, request_size
,
154 response_size
, 0, gfp
);
157 struct gb_operation
*
158 gb_operation_create_core(struct gb_connection
*connection
,
159 u8 type
, size_t request_size
,
160 size_t response_size
, unsigned long flags
,
163 void gb_operation_get(struct gb_operation
*operation
);
164 void gb_operation_put(struct gb_operation
*operation
);
166 bool gb_operation_response_alloc(struct gb_operation
*operation
,
167 size_t response_size
, gfp_t gfp
);
169 int gb_operation_request_send(struct gb_operation
*operation
,
170 gb_operation_callback callback
,
171 unsigned int timeout
,
173 int gb_operation_request_send_sync_timeout(struct gb_operation
*operation
,
174 unsigned int timeout
);
176 gb_operation_request_send_sync(struct gb_operation
*operation
)
178 return gb_operation_request_send_sync_timeout(operation
,
179 GB_OPERATION_TIMEOUT_DEFAULT
);
182 void gb_operation_cancel(struct gb_operation
*operation
, int errno
);
183 void gb_operation_cancel_incoming(struct gb_operation
*operation
, int errno
);
185 void greybus_message_sent(struct gb_host_device
*hd
,
186 struct gb_message
*message
, int status
);
188 int gb_operation_sync_timeout(struct gb_connection
*connection
, int type
,
189 void *request
, int request_size
,
190 void *response
, int response_size
,
191 unsigned int timeout
);
192 int gb_operation_unidirectional_timeout(struct gb_connection
*connection
,
193 int type
, void *request
, int request_size
,
194 unsigned int timeout
);
196 static inline int gb_operation_sync(struct gb_connection
*connection
, int type
,
197 void *request
, int request_size
,
198 void *response
, int response_size
)
200 return gb_operation_sync_timeout(connection
, type
,
201 request
, request_size
, response
, response_size
,
202 GB_OPERATION_TIMEOUT_DEFAULT
);
205 static inline int gb_operation_unidirectional(struct gb_connection
*connection
,
206 int type
, void *request
, int request_size
)
208 return gb_operation_unidirectional_timeout(connection
, type
,
209 request
, request_size
, GB_OPERATION_TIMEOUT_DEFAULT
);
212 static inline void *gb_operation_get_data(struct gb_operation
*operation
)
214 return operation
->private;
217 static inline void gb_operation_set_data(struct gb_operation
*operation
,
220 operation
->private = data
;
223 int gb_operation_init(void);
224 void gb_operation_exit(void);
226 #endif /* !__OPERATION_H */