]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/staging/greybus/operation.c
greybus: Random spell fixes
[karo-tx-linux.git] / drivers / staging / greybus / operation.c
index f6940a375364f97b62987380f449b351a0b297b6..442046562bfbaeb6cca4eead118dbdca59eb397e 100644 (file)
@@ -81,7 +81,7 @@ static void gb_pending_operation_insert(struct gb_operation *operation)
        spin_unlock_irq(&gb_operations_lock);
 
        /* Store the operation id in the request header */
-       header = operation->request.header;
+       header = operation->request->header;
        header->operation_id = cpu_to_le16(operation->id);
 }
 
@@ -146,7 +146,7 @@ static void gb_message_cancel(struct gb_message *message)
 }
 
 /*
- * An operations's response message has arrived.  If no callback was
+ * An operation's response message has arrived.  If no callback was
  * supplied it was submitted for asynchronous completion, so we notify
  * any waiters.  Otherwise we assume calling the completion is enough
  * and nobody else will be waiting.
@@ -167,9 +167,8 @@ int gb_operation_wait(struct gb_operation *operation)
        ret = wait_for_completion_interruptible(&operation->completion);
        /* If interrupted, cancel the in-flight buffer */
        if (ret < 0)
-               gb_message_cancel(&operation->request);
+               gb_message_cancel(operation->request);
        return ret;
-
 }
 
 static void gb_operation_request_handle(struct gb_operation *operation)
@@ -177,7 +176,7 @@ static void gb_operation_request_handle(struct gb_operation *operation)
        struct gb_protocol *protocol = operation->connection->protocol;
        struct gb_operation_msg_hdr *header;
 
-       header = operation->request.header;
+       header = operation->request->header;
 
        /*
         * If the protocol has no incoming request handler, report
@@ -205,7 +204,7 @@ static void gb_operation_recv_work(struct work_struct *recv_work)
        bool incoming_request;
 
        operation = container_of(recv_work, struct gb_operation, recv_work);
-       incoming_request = operation->response.header == NULL;
+       incoming_request = operation->response->header == NULL;
        if (incoming_request)
                gb_operation_request_handle(operation);
        gb_operation_complete(operation);
@@ -229,89 +228,78 @@ static void operation_timeout(struct work_struct *work)
        gb_operation_complete(operation);
 }
 
-static void *
-gb_buffer_alloc(struct greybus_host_device *hd, size_t size, gfp_t gfp_flags)
+/*
+ * Given a pointer to the header in a message sent on a given host
+ * device, return the associated message structure.  (This "header"
+ * is just the buffer pointer we supply to the host device for
+ * sending.)
+ */
+static struct gb_message *
+gb_hd_message_find(struct greybus_host_device *hd, void *header)
 {
-       u8 *buffer;
-
-       buffer = kzalloc(hd->buffer_headroom + size, gfp_flags);
-       if (buffer)
-               buffer += hd->buffer_headroom;
-
-       return buffer;
-}
+       struct gb_message *message;
+       u8 *result;
 
-static void
-gb_buffer_free(struct greybus_host_device *hd, void *buffer)
-{
-       u8 *allocated = buffer;
+       result = (u8 *)header - hd->buffer_headroom - sizeof(*message);
+       message = (struct gb_message *)result;
 
-       if (allocated)
-               kfree(allocated - hd->buffer_headroom);
+       return message;
 }
 
 /*
- * Allocate a buffer to be used for an operation request or response
- * message.  For outgoing messages, both types of message contain a
+ * Allocate a message to be used for an operation request or
+ * response.  For outgoing messages, both types of message contain a
  * common header, which is filled in here.  Incoming requests or
  * responses also contain the same header, but there's no need to
  * initialize it here (it'll be overwritten by the incoming
  * message).
+ *
+ * Our message structure consists of:
+ *     message structure
+ *     headroom
+ *     message header  \_ these combined are
+ *     message payload /  the message size
  */
-static int gb_operation_message_init(struct gb_operation *operation,
-                                       u8 type, size_t size,
-                                       bool request, gfp_t gfp_flags)
+static struct gb_message *
+gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
+                               size_t payload_size, gfp_t gfp_flags)
 {
-       struct gb_connection *connection = operation->connection;
-       struct greybus_host_device *hd = connection->hd;
        struct gb_message *message;
        struct gb_operation_msg_hdr *header;
+       size_t message_size = payload_size + sizeof(*header);
+       size_t size;
+       u8 *buffer;
 
-       size += sizeof(*header);
-       if (size > hd->buffer_size_max)
-               return -E2BIG;
-
-       if (request) {
-               message = &operation->request;
-       } else {
-               message = &operation->response;
-               type |= GB_OPERATION_TYPE_RESPONSE;
-       }
+       if (message_size > hd->buffer_size_max)
+               return NULL;
 
-       message->header = gb_buffer_alloc(hd, size, gfp_flags);
-       if (!message->header)
-               return -ENOMEM;
-       message->size = size;
+       size = sizeof(*message) + hd->buffer_headroom + message_size;
+       message = kzalloc(size, gfp_flags);
+       if (!message)
+               return NULL;
+       buffer = &message->buffer[0];
+       header = (struct gb_operation_msg_hdr *)(buffer + hd->buffer_headroom);
 
        /* Fill in the header structure */
-       header = message->header;
-       header->size = cpu_to_le16(size);
+       header->size = cpu_to_le16(message_size);
        header->operation_id = 0;       /* Filled in when submitted */
        header->type = type;
 
+       message->header = header;
        message->payload = header + 1;
-       message->operation = operation;
+       message->size = message_size;
 
-       return 0;
+       return message;
 }
 
-static void gb_operation_message_exit(struct gb_message *message)
+static void gb_operation_message_free(struct gb_message *message)
 {
-       struct greybus_host_device *hd;
-
-       hd = message->operation->connection->hd;
-       gb_buffer_free(hd, message->header);
-
-       message->operation = NULL;
-       message->payload = NULL;
-       message->header = NULL;
-       message->size = 0;
+       kfree(message);
 }
 
 /*
- * Map an enum gb_operation_status value (which is represted in a
- * message as a single back a single byte) to an appropriate Linux
- * negative errno.
+ * Map an enum gb_operation_status value (which is represented in a
+ * message as a single byte) to an appropriate Linux negative errno.
  */
 int gb_operation_status_map(u8 status)
 {
@@ -339,7 +327,7 @@ int gb_operation_status_map(u8 status)
 
 /*
  * Create a Greybus operation to be sent over the given connection.
- * The request buffer will big enough for a payload of the given
+ * The request buffer will be big enough for a payload of the given
  * size.  Outgoing requests must specify the size of the response
  * buffer size, which must be sufficient to hold all expected
  * response data.
@@ -358,25 +346,28 @@ gb_operation_create_common(struct gb_connection *connection, bool outgoing,
                                u8 type, size_t request_size,
                                size_t response_size)
 {
+       struct greybus_host_device *hd = connection->hd;
        struct gb_operation *operation;
        gfp_t gfp_flags = response_size ? GFP_KERNEL : GFP_ATOMIC;
-       int ret;
 
        operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
        if (!operation)
                return NULL;
        operation->connection = connection;
 
-       ret = gb_operation_message_init(operation, type, request_size,
-                                               true, gfp_flags);
-       if (ret)
+       operation->request = gb_operation_message_alloc(hd, type, request_size,
+                                                       gfp_flags);
+       if (!operation->request)
                goto err_cache;
+       operation->request->operation = operation;
 
        if (outgoing) {
-               ret = gb_operation_message_init(operation, type, response_size,
-                                               false, GFP_KERNEL);
-               if (ret)
+               type |= GB_OPERATION_TYPE_RESPONSE;
+               operation->response = gb_operation_message_alloc(hd, type,
+                                               response_size, GFP_KERNEL);
+               if (!operation->response)
                        goto err_request;
+               operation->response->operation = operation;
        }
 
        INIT_WORK(&operation->recv_work, gb_operation_recv_work);
@@ -392,7 +383,7 @@ gb_operation_create_common(struct gb_connection *connection, bool outgoing,
        return operation;
 
 err_request:
-       gb_operation_message_exit(&operation->request);
+       gb_operation_message_free(operation->request);
 err_cache:
        kmem_cache_free(gb_operation_cache, operation);
 
@@ -430,8 +421,8 @@ static void _gb_operation_destroy(struct kref *kref)
        list_del(&operation->links);
        spin_unlock_irq(&gb_operations_lock);
 
-       gb_operation_message_exit(&operation->response);
-       gb_operation_message_exit(&operation->request);
+       gb_operation_message_free(operation->response);
+       gb_operation_message_free(operation->request);
 
        kmem_cache_free(gb_operation_cache, operation);
 }
@@ -478,7 +469,7 @@ int gb_operation_request_send(struct gb_operation *operation,
        schedule_delayed_work(&operation->timeout_work, timeout);
 
        /* All set, send the request */
-       ret = gb_message_send(&operation->request, GFP_KERNEL);
+       ret = gb_message_send(operation->request, GFP_KERNEL);
        if (ret)
                return ret;
 
@@ -498,6 +489,32 @@ int gb_operation_response_send(struct gb_operation *operation)
        return 0;
 }
 
+/*
+ * This function is called when a buffer send request has completed.
+ * The "header" is the message header--the beginning of what we
+ * asked to have sent.
+ *
+ * XXX Mismatch between errno here and operation result code
+ */
+void
+greybus_data_sent(struct greybus_host_device *hd, void *header, int status)
+{
+       struct gb_message *message;
+       struct gb_operation *operation;
+
+       /* If there's no error, there's really nothing to do */
+       if (!status)
+               return; /* Mark it complete? */
+
+       /* XXX Right now we assume we're an outgoing request */
+       message = gb_hd_message_find(hd, header);
+       operation = message->operation;
+       gb_connection_err(operation->connection, "send error %d\n", status);
+       operation->result = status;     /* XXX */
+       gb_operation_complete(operation);
+}
+EXPORT_SYMBOL_GPL(greybus_data_sent);
+
 /*
  * We've received data on a connection, and it doesn't look like a
  * response, so we assume it's a request.
@@ -516,7 +533,7 @@ void gb_connection_recv_request(struct gb_connection *connection,
                return;         /* XXX Respond with pre-allocated ENOMEM */
        }
        operation->id = operation_id;
-       memcpy(operation->request.header, data, size);
+       memcpy(operation->request->header, data, size);
 
        /* The rest will be handled in work queue context */
        queue_work(gb_operation_recv_workqueue, &operation->recv_work);
@@ -525,7 +542,7 @@ void gb_connection_recv_request(struct gb_connection *connection,
 /*
  * We've received data that appears to be an operation response
  * message.  Look up the operation, and record that we've received
- * its repsonse.
+ * its response.
  *
  * This is called in interrupt context, so just copy the incoming
  * data into the response buffer and handle the rest via workqueue.
@@ -546,7 +563,7 @@ static void gb_connection_recv_response(struct gb_connection *connection,
        cancel_delayed_work(&operation->timeout_work);
        gb_pending_operation_remove(operation);
 
-       message = &operation->response;
+       message = operation->response;
        if (size <= message->size) {
                /* Transfer the operation result from the response header */
                header = message->header;
@@ -609,9 +626,9 @@ void gb_connection_recv(struct gb_connection *connection,
 void gb_operation_cancel(struct gb_operation *operation)
 {
        operation->canceled = true;
-       gb_message_cancel(&operation->request);
-       if (operation->response.header)
-               gb_message_cancel(&operation->response);
+       gb_message_cancel(operation->request);
+       if (operation->response->header)
+               gb_message_cancel(operation->response);
 }
 
 int gb_operation_init(void)