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
;
106 struct list_head links
; /* connection->operations */
110 gb_operation_is_incoming(struct gb_operation
*operation
)
112 return operation
->flags
& GB_OPERATION_FLAG_INCOMING
;
116 gb_operation_is_unidirectional(struct gb_operation
*operation
)
118 return operation
->flags
& GB_OPERATION_FLAG_UNIDIRECTIONAL
;
122 gb_operation_short_response_allowed(struct gb_operation
*operation
)
124 return operation
->flags
& GB_OPERATION_FLAG_SHORT_RESPONSE
;
127 static inline bool gb_operation_is_core(struct gb_operation
*operation
)
129 return operation
->flags
& GB_OPERATION_FLAG_CORE
;
132 void gb_connection_recv(struct gb_connection
*connection
,
133 void *data
, size_t size
);
135 int gb_operation_result(struct gb_operation
*operation
);
137 size_t gb_operation_get_payload_size_max(struct gb_connection
*connection
);
138 struct gb_operation
*
139 gb_operation_create_flags(struct gb_connection
*connection
,
140 u8 type
, size_t request_size
,
141 size_t response_size
, unsigned long flags
,
144 static inline struct gb_operation
*
145 gb_operation_create(struct gb_connection
*connection
,
146 u8 type
, size_t request_size
,
147 size_t response_size
, gfp_t gfp
)
149 return gb_operation_create_flags(connection
, type
, request_size
,
150 response_size
, 0, gfp
);
153 struct gb_operation
*
154 gb_operation_create_core(struct gb_connection
*connection
,
155 u8 type
, size_t request_size
,
156 size_t response_size
, unsigned long flags
,
159 void gb_operation_get(struct gb_operation
*operation
);
160 void gb_operation_put(struct gb_operation
*operation
);
162 bool gb_operation_response_alloc(struct gb_operation
*operation
,
163 size_t response_size
, gfp_t gfp
);
165 int gb_operation_request_send(struct gb_operation
*operation
,
166 gb_operation_callback callback
,
168 int gb_operation_request_send_sync_timeout(struct gb_operation
*operation
,
169 unsigned int timeout
);
171 gb_operation_request_send_sync(struct gb_operation
*operation
)
173 return gb_operation_request_send_sync_timeout(operation
,
174 GB_OPERATION_TIMEOUT_DEFAULT
);
177 void gb_operation_cancel(struct gb_operation
*operation
, int errno
);
178 void gb_operation_cancel_incoming(struct gb_operation
*operation
, int errno
);
180 void greybus_message_sent(struct gb_host_device
*hd
,
181 struct gb_message
*message
, int status
);
183 int gb_operation_sync_timeout(struct gb_connection
*connection
, int type
,
184 void *request
, int request_size
,
185 void *response
, int response_size
,
186 unsigned int timeout
);
187 int gb_operation_unidirectional_timeout(struct gb_connection
*connection
,
188 int type
, void *request
, int request_size
,
189 unsigned int timeout
);
191 static inline int gb_operation_sync(struct gb_connection
*connection
, int type
,
192 void *request
, int request_size
,
193 void *response
, int response_size
)
195 return gb_operation_sync_timeout(connection
, type
,
196 request
, request_size
, response
, response_size
,
197 GB_OPERATION_TIMEOUT_DEFAULT
);
200 static inline int gb_operation_unidirectional(struct gb_connection
*connection
,
201 int type
, void *request
, int request_size
)
203 return gb_operation_unidirectional_timeout(connection
, type
,
204 request
, request_size
, GB_OPERATION_TIMEOUT_DEFAULT
);
207 int gb_operation_init(void);
208 void gb_operation_exit(void);
210 #endif /* !__OPERATION_H */