OSDN Git Service

greybus: introduce gb_operation_message_init()
authorAlex Elder <elder@linaro.org>
Tue, 2 Dec 2014 14:30:33 +0000 (08:30 -0600)
committerGreg Kroah-Hartman <greg@kroah.com>
Tue, 2 Dec 2014 22:39:42 +0000 (14:39 -0800)
Separate the allocation of a message structure from its basic
initialization.  This will allow very common fixed-size operation
response buffers to be allocated from a slab cache.

Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
drivers/staging/greybus/operation.c

index f474e8f..d335bd2 100644 (file)
@@ -301,6 +301,43 @@ gb_hd_message_find(struct greybus_host_device *hd, void *header)
        return message;
 }
 
+static void gb_operation_message_init(struct greybus_host_device *hd,
+                               struct gb_message *message, u16 operation_id,
+                               size_t message_size, u8 type)
+{
+       struct gb_operation_msg_hdr *header;
+       u8 *buffer;
+
+       BUG_ON(message_size < sizeof(*header));
+       buffer = &message->buffer[0];
+       header = (struct gb_operation_msg_hdr *)(buffer + hd->buffer_headroom);
+
+       message->header = header;
+       message->payload = header + 1;
+       message->size = message_size;
+
+       /*
+        * The type supplied for incoming message buffers will be
+        * 0x00.  Such buffers will be overwritten by arriving data
+        * so there's no need to initialize the message header.
+        */
+       if (type) {
+               /*
+                * For a request, the operation id gets filled in
+                * when the message is sent.  For a response, it
+                * will be copied from the request by the caller.
+                *
+                * The result field in a request message must be
+                * zero.  It will be set just prior to sending for
+                * a response.
+                */
+               header->size = cpu_to_le16(message_size);
+               header->operation_id = 0;
+               header->type = type;
+               header->result = 0;
+       }
+}
+
 /*
  * Allocate a message to be used for an operation request or response.
  * Both types of message contain a common header.  The request message
@@ -325,7 +362,6 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
        struct gb_operation_msg_hdr *header;
        size_t message_size = payload_size + sizeof(*header);
        size_t size;
-       u8 *buffer;
 
        if (hd->buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
                pr_warn("limiting buffer size to %u\n",
@@ -340,33 +376,9 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
        message = kzalloc(size, gfp_flags);
        if (!message)
                return NULL;
-       buffer = &message->buffer[0];
-       header = (struct gb_operation_msg_hdr *)(buffer + hd->buffer_headroom);
 
-       message->header = header;
-       message->payload = header + 1;
-       message->size = message_size;
-
-       /*
-        * The type supplied for incoming message buffers will be
-        * 0x00.  Such buffers will be overwritten by arriving data
-        * so there's no need to initialize the message header.
-        */
-       if (type) {
-               /*
-                * For a request, the operation id gets filled in
-                * when the message is sent.  For a response, it
-                * will be copied from the request by the caller.
-                *
-                * The result field in a request message must be
-                * zero.  It will be set just prior to sending for
-                * a response.
-                */
-               header->size = cpu_to_le16(message_size);
-               header->operation_id = 0;
-               header->type = type;
-               header->result = 0;
-       }
+       /* Initialize the message.  Operation id is filled in later. */
+       gb_operation_message_init(hd, message, 0, message_size, type);
 
        return message;
 }