4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
13 #include <linux/completion.h>
17 /* The default amount of time a request is given to complete */
18 #define GB_OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */
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)
24 #define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80)
26 enum gb_operation_result
{
28 GB_OP_INTERRUPTED
= 0x01,
30 GB_OP_NO_MEMORY
= 0x03,
31 GB_OP_PROTOCOL_BAD
= 0x04,
32 GB_OP_OVERFLOW
= 0x05,
35 GB_OP_NONEXISTENT
= 0x08,
36 GB_OP_UNKNOWN_ERROR
= 0xfe,
37 GB_OP_MALFUNCTION
= 0xff,
40 #define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr)
41 #define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX
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.
49 struct gb_operation
*operation
;
50 struct gb_operation_msg_hdr
*header
;
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)
65 #define GB_OPERATION_FLAG_USER_MASK (GB_OPERATION_FLAG_SHORT_RESPONSE | \
66 GB_OPERATION_FLAG_UNIDIRECTIONAL)
69 * A Greybus operation is a remote procedure call performed over a
70 * connection between two UniPro interfaces.
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.
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.
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().
87 typedef void (*gb_operation_callback
)(struct gb_operation
*);
89 struct gb_connection
*connection
;
90 struct gb_message
*request
;
91 struct gb_message
*response
;
96 int errno
; /* Operation result */
98 struct work_struct work
;
99 gb_operation_callback callback
;
100 struct completion completion
;
101 struct timer_list timer
;
107 struct list_head links
; /* connection->operations */
111 gb_operation_is_incoming(struct gb_operation
*operation
)
113 return operation
->flags
& GB_OPERATION_FLAG_INCOMING
;
117 gb_operation_is_unidirectional(struct gb_operation
*operation
)
119 return operation
->flags
& GB_OPERATION_FLAG_UNIDIRECTIONAL
;
123 gb_operation_short_response_allowed(struct gb_operation
*operation
)
125 return operation
->flags
& GB_OPERATION_FLAG_SHORT_RESPONSE
;
128 static inline bool gb_operation_is_core(struct gb_operation
*operation
)
130 return operation
->flags
& GB_OPERATION_FLAG_CORE
;
133 void gb_connection_recv(struct gb_connection
*connection
,
134 void *data
, size_t size
);
136 int gb_operation_result(struct gb_operation
*operation
);
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
,
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
)
150 return gb_operation_create_flags(connection
, type
, request_size
,
151 response_size
, 0, gfp
);
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
,
160 void gb_operation_get(struct gb_operation
*operation
);
161 void gb_operation_put(struct gb_operation
*operation
);
163 bool gb_operation_response_alloc(struct gb_operation
*operation
,
164 size_t response_size
, gfp_t gfp
);
166 int gb_operation_request_send(struct gb_operation
*operation
,
167 gb_operation_callback callback
,
168 unsigned int timeout
,
170 int gb_operation_request_send_sync_timeout(struct gb_operation
*operation
,
171 unsigned int timeout
);
173 gb_operation_request_send_sync(struct gb_operation
*operation
)
175 return gb_operation_request_send_sync_timeout(operation
,
176 GB_OPERATION_TIMEOUT_DEFAULT
);
179 void gb_operation_cancel(struct gb_operation
*operation
, int errno
);
180 void gb_operation_cancel_incoming(struct gb_operation
*operation
, int errno
);
182 void greybus_message_sent(struct gb_host_device
*hd
,
183 struct gb_message
*message
, int status
);
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
);
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
)
197 return gb_operation_sync_timeout(connection
, type
,
198 request
, request_size
, response
, response_size
,
199 GB_OPERATION_TIMEOUT_DEFAULT
);
202 static inline int gb_operation_unidirectional(struct gb_connection
*connection
,
203 int type
, void *request
, int request_size
)
205 return gb_operation_unidirectional_timeout(connection
, type
,
206 request
, request_size
, GB_OPERATION_TIMEOUT_DEFAULT
);
209 int gb_operation_init(void);
210 void gb_operation_exit(void);
212 #endif /* !__OPERATION_H */