2015-03-31 07:51:59 +00:00
|
|
|
/*
|
|
|
|
* Loopback bridge driver for the Greybus loopback module.
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
* Copyright 2014 Linaro Ltd.
|
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2015-07-28 17:34:36 +00:00
|
|
|
#include <linux/mutex.h>
|
2015-03-31 07:51:59 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/random.h>
|
2015-03-31 21:01:45 +00:00
|
|
|
#include <linux/sizes.h>
|
2015-04-16 07:53:59 +00:00
|
|
|
#include <asm/div64.h>
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
#include "greybus.h"
|
|
|
|
|
|
|
|
struct gb_loopback_stats {
|
2015-07-21 22:50:08 +00:00
|
|
|
u64 min;
|
|
|
|
u64 max;
|
|
|
|
u64 avg;
|
|
|
|
u64 sum;
|
|
|
|
u64 count;
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gb_loopback {
|
|
|
|
struct gb_connection *connection;
|
|
|
|
u8 version_major;
|
|
|
|
u8 version_minor;
|
|
|
|
|
2015-07-28 17:34:36 +00:00
|
|
|
struct mutex mutex;
|
2015-03-31 07:51:59 +00:00
|
|
|
struct task_struct *task;
|
2015-07-24 09:02:56 +00:00
|
|
|
wait_queue_head_t wq;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
int type;
|
|
|
|
u32 size;
|
2015-07-21 22:50:08 +00:00
|
|
|
u32 iteration_max;
|
|
|
|
u32 iteration_count;
|
2015-07-13 23:53:13 +00:00
|
|
|
size_t size_max;
|
2015-03-31 07:51:59 +00:00
|
|
|
int ms_wait;
|
|
|
|
|
|
|
|
struct gb_loopback_stats latency;
|
|
|
|
struct gb_loopback_stats throughput;
|
2015-07-21 22:50:10 +00:00
|
|
|
struct gb_loopback_stats requests_per_second;
|
2015-03-31 07:51:59 +00:00
|
|
|
u64 elapsed_nsecs;
|
|
|
|
u32 error;
|
|
|
|
};
|
|
|
|
|
2015-05-12 02:16:37 +00:00
|
|
|
#define GB_LOOPBACK_MS_WAIT_MAX 1000
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
/* Define get_version() routine */
|
|
|
|
define_get_version(gb_loopback, LOOPBACK);
|
|
|
|
|
|
|
|
/* interface sysfs attributes */
|
|
|
|
#define gb_loopback_ro_attr(field, type) \
|
|
|
|
static ssize_t field##_show(struct device *dev, \
|
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev); \
|
|
|
|
struct gb_loopback *gb = \
|
|
|
|
(struct gb_loopback *)connection->private; \
|
|
|
|
return sprintf(buf, "%"#type"\n", gb->field); \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RO(field)
|
|
|
|
|
|
|
|
#define gb_loopback_ro_stats_attr(name, field, type) \
|
|
|
|
static ssize_t name##_##field##_show(struct device *dev, \
|
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev); \
|
|
|
|
struct gb_loopback *gb = \
|
|
|
|
(struct gb_loopback *)connection->private; \
|
|
|
|
return sprintf(buf, "%"#type"\n", gb->name.field); \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RO(name##_##field)
|
|
|
|
|
|
|
|
#define gb_loopback_stats_attrs(field) \
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_ro_stats_attr(field, min, llu); \
|
|
|
|
gb_loopback_ro_stats_attr(field, max, llu); \
|
|
|
|
gb_loopback_ro_stats_attr(field, avg, llu);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
#define gb_loopback_attr(field, type) \
|
|
|
|
static ssize_t field##_show(struct device *dev, \
|
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev); \
|
|
|
|
struct gb_loopback *gb = \
|
|
|
|
(struct gb_loopback *)connection->private; \
|
|
|
|
return sprintf(buf, "%"#type"\n", gb->field); \
|
|
|
|
} \
|
|
|
|
static ssize_t field##_store(struct device *dev, \
|
|
|
|
struct device_attribute *attr, \
|
|
|
|
const char *buf, \
|
|
|
|
size_t len) \
|
|
|
|
{ \
|
|
|
|
int ret; \
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev); \
|
|
|
|
struct gb_loopback *gb = \
|
|
|
|
(struct gb_loopback *)connection->private; \
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_lock(&gb->mutex); \
|
2015-03-31 07:51:59 +00:00
|
|
|
ret = sscanf(buf, "%"#type, &gb->field); \
|
|
|
|
if (ret != 1) \
|
2015-07-28 17:34:36 +00:00
|
|
|
len = -EINVAL; \
|
|
|
|
else \
|
|
|
|
gb_loopback_check_attr(gb); \
|
|
|
|
mutex_unlock(&gb->mutex); \
|
2015-03-31 07:51:59 +00:00
|
|
|
return len; \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RW(field)
|
|
|
|
|
|
|
|
static void gb_loopback_reset_stats(struct gb_loopback *gb);
|
|
|
|
static void gb_loopback_check_attr(struct gb_loopback *gb)
|
|
|
|
{
|
2015-07-24 09:02:56 +00:00
|
|
|
if (gb->ms_wait > GB_LOOPBACK_MS_WAIT_MAX)
|
|
|
|
gb->ms_wait = GB_LOOPBACK_MS_WAIT_MAX;
|
|
|
|
if (gb->size > gb->size_max)
|
|
|
|
gb->size = gb->size_max;
|
|
|
|
gb->error = 0;
|
|
|
|
gb->iteration_count = 0;
|
|
|
|
gb_loopback_reset_stats(gb);
|
|
|
|
|
2015-07-13 19:20:48 +00:00
|
|
|
switch (gb->type) {
|
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
2015-07-13 19:20:49 +00:00
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
2015-07-24 09:02:56 +00:00
|
|
|
wake_up(&gb->wq);
|
2015-07-13 19:20:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gb->type = 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Time to send and receive one message */
|
|
|
|
gb_loopback_stats_attrs(latency);
|
2015-07-21 22:50:10 +00:00
|
|
|
/* Number of requests sent per second on this cport */
|
|
|
|
gb_loopback_stats_attrs(requests_per_second);
|
2015-03-31 07:51:59 +00:00
|
|
|
/* Quantity of data sent and received on this cport */
|
|
|
|
gb_loopback_stats_attrs(throughput);
|
2015-07-21 22:50:09 +00:00
|
|
|
/* Number of errors encountered during loop */
|
2015-03-31 07:51:59 +00:00
|
|
|
gb_loopback_ro_attr(error, d);
|
2015-07-21 22:50:09 +00:00
|
|
|
/* The current index of the for (i = 0; i < iteration_max; i++) loop */
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_ro_attr(iteration_count, u);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
/*
|
2015-07-21 22:50:07 +00:00
|
|
|
* Type of loopback message to send based on protocol type definitions
|
2015-03-31 07:51:59 +00:00
|
|
|
* 0 => Don't send message
|
2015-07-21 22:50:07 +00:00
|
|
|
* 2 => Send ping message continuously (message without payload)
|
|
|
|
* 3 => Send transer message continuously (message with payload,
|
|
|
|
* payload returned in response)
|
|
|
|
* 4 => Send a sink message (message with payload, no payload in response)
|
2015-03-31 07:51:59 +00:00
|
|
|
*/
|
|
|
|
gb_loopback_attr(type, d);
|
|
|
|
/* Size of transfer message payload: 0-4096 bytes */
|
|
|
|
gb_loopback_attr(size, u);
|
2015-05-12 02:16:36 +00:00
|
|
|
/* Time to wait between two messages: 0-1000 ms */
|
2015-03-31 07:51:59 +00:00
|
|
|
gb_loopback_attr(ms_wait, d);
|
2015-07-21 22:50:08 +00:00
|
|
|
/* Maximum iterations for a given operation: 1-(2^32-1), 0 implies infinite */
|
|
|
|
gb_loopback_attr(iteration_max, u);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
#define dev_stats_attrs(name) \
|
|
|
|
&dev_attr_##name##_min.attr, \
|
|
|
|
&dev_attr_##name##_max.attr, \
|
|
|
|
&dev_attr_##name##_avg.attr
|
|
|
|
|
|
|
|
static struct attribute *loopback_attrs[] = {
|
|
|
|
dev_stats_attrs(latency),
|
2015-07-21 22:50:10 +00:00
|
|
|
dev_stats_attrs(requests_per_second),
|
2015-03-31 07:51:59 +00:00
|
|
|
dev_stats_attrs(throughput),
|
|
|
|
&dev_attr_type.attr,
|
|
|
|
&dev_attr_size.attr,
|
|
|
|
&dev_attr_ms_wait.attr,
|
2015-07-21 22:50:08 +00:00
|
|
|
&dev_attr_iteration_count.attr,
|
|
|
|
&dev_attr_iteration_max.attr,
|
2015-03-31 07:51:59 +00:00
|
|
|
&dev_attr_error.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(loopback);
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static int gb_loopback_sink(struct gb_loopback *gb, u32 len)
|
2015-07-13 19:20:49 +00:00
|
|
|
{
|
|
|
|
struct timeval ts, te;
|
|
|
|
struct gb_loopback_transfer_request *request;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
request = kmalloc(len + sizeof(*request), GFP_KERNEL);
|
|
|
|
if (!request)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
request->len = cpu_to_le32(len);
|
|
|
|
|
|
|
|
do_gettimeofday(&ts);
|
|
|
|
retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_SINK,
|
|
|
|
request, len + sizeof(*request), NULL, 0);
|
2015-07-13 19:20:50 +00:00
|
|
|
|
2015-07-13 19:20:49 +00:00
|
|
|
do_gettimeofday(&te);
|
2015-07-28 17:34:35 +00:00
|
|
|
gb->elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
|
2015-07-13 19:20:49 +00:00
|
|
|
|
|
|
|
kfree(request);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static int gb_loopback_transfer(struct gb_loopback *gb, u32 len)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
struct timeval ts, te;
|
|
|
|
struct gb_loopback_transfer_request *request;
|
|
|
|
struct gb_loopback_transfer_response *response;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
request = kmalloc(len + sizeof(*request), GFP_KERNEL);
|
|
|
|
if (!request)
|
|
|
|
return -ENOMEM;
|
|
|
|
response = kmalloc(len + sizeof(*response), GFP_KERNEL);
|
|
|
|
if (!response) {
|
|
|
|
kfree(request);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->len = cpu_to_le32(len);
|
|
|
|
|
|
|
|
do_gettimeofday(&ts);
|
|
|
|
retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_TRANSFER,
|
|
|
|
request, len + sizeof(*request),
|
|
|
|
response, len + sizeof(*response));
|
|
|
|
do_gettimeofday(&te);
|
2015-07-28 17:34:35 +00:00
|
|
|
gb->elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
if (retval)
|
|
|
|
goto gb_error;
|
|
|
|
|
|
|
|
if (memcmp(request->data, response->data, len))
|
|
|
|
retval = -EREMOTEIO;
|
|
|
|
|
|
|
|
gb_error:
|
|
|
|
kfree(request);
|
|
|
|
kfree(response);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static int gb_loopback_ping(struct gb_loopback *gb)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
struct timeval ts, te;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
do_gettimeofday(&ts);
|
|
|
|
retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_PING,
|
|
|
|
NULL, 0, NULL, 0);
|
|
|
|
do_gettimeofday(&te);
|
2015-07-28 17:34:35 +00:00
|
|
|
gb->elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:16:39 +00:00
|
|
|
static int gb_loopback_request_recv(u8 type, struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = operation->connection;
|
2015-07-13 19:20:50 +00:00
|
|
|
struct gb_loopback *gb = connection->private;
|
2015-05-12 02:16:39 +00:00
|
|
|
struct gb_loopback_transfer_request *request;
|
|
|
|
struct gb_loopback_transfer_response *response;
|
2015-07-13 23:53:13 +00:00
|
|
|
size_t len;
|
2015-05-12 02:16:39 +00:00
|
|
|
|
|
|
|
/* By convention, the AP initiates the version operation */
|
|
|
|
switch (type) {
|
|
|
|
case GB_LOOPBACK_TYPE_PROTOCOL_VERSION:
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"module-initiated version operation\n");
|
|
|
|
return -EINVAL;
|
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
2015-07-13 19:20:49 +00:00
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
2015-05-12 02:16:39 +00:00
|
|
|
return 0;
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
|
|
|
if (operation->request->payload_size < sizeof(*request)) {
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"transfer request too small (%zu < %zu)\n",
|
|
|
|
operation->request->payload_size,
|
|
|
|
sizeof(*request));
|
|
|
|
return -EINVAL; /* -EMSGSIZE */
|
|
|
|
}
|
|
|
|
request = operation->request->payload;
|
|
|
|
len = le32_to_cpu(request->len);
|
2015-07-13 19:20:50 +00:00
|
|
|
if (len > gb->size_max) {
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"transfer request too large (%zu > %zu)\n",
|
|
|
|
len, gb->size_max);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:16:39 +00:00
|
|
|
if (len) {
|
2015-07-17 16:50:25 +00:00
|
|
|
if (!gb_operation_response_alloc(operation, len,
|
|
|
|
GFP_KERNEL)) {
|
2015-05-12 02:16:39 +00:00
|
|
|
dev_err(&connection->dev,
|
|
|
|
"error allocating response\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
response = operation->response->payload;
|
|
|
|
memcpy(response->data, request->data, len);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"unsupported request: %hhu\n", type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
static void gb_loopback_reset_stats(struct gb_loopback *gb)
|
|
|
|
{
|
|
|
|
struct gb_loopback_stats reset = {
|
|
|
|
.min = 0xffffffff,
|
|
|
|
};
|
|
|
|
memcpy(&gb->latency, &reset, sizeof(struct gb_loopback_stats));
|
|
|
|
memcpy(&gb->throughput, &reset, sizeof(struct gb_loopback_stats));
|
2015-07-21 22:50:10 +00:00
|
|
|
memcpy(&gb->requests_per_second, &reset,
|
|
|
|
sizeof(struct gb_loopback_stats));
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:50:08 +00:00
|
|
|
static void gb_loopback_update_stats(struct gb_loopback_stats *stats, u64 val)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-21 22:50:08 +00:00
|
|
|
if (stats->min > val)
|
|
|
|
stats->min = val;
|
|
|
|
if (stats->max < val)
|
|
|
|
stats->max = val;
|
|
|
|
stats->sum += val;
|
|
|
|
stats->count++;
|
|
|
|
stats->avg = stats->sum;
|
|
|
|
do_div(stats->avg, stats->count);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:50:10 +00:00
|
|
|
static void gb_loopback_requests_update(struct gb_loopback *gb, u32 latency)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-21 22:50:10 +00:00
|
|
|
u32 req = USEC_PER_SEC;
|
2015-07-21 22:50:08 +00:00
|
|
|
|
2015-07-21 22:50:10 +00:00
|
|
|
do_div(req, latency);
|
|
|
|
gb_loopback_update_stats(&gb->requests_per_second, req);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:50:08 +00:00
|
|
|
static void gb_loopback_throughput_update(struct gb_loopback *gb, u32 latency)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-21 22:50:08 +00:00
|
|
|
u32 throughput;
|
2015-07-13 19:20:51 +00:00
|
|
|
u32 aggregate_size = sizeof(struct gb_operation_msg_hdr) * 2;
|
|
|
|
|
|
|
|
switch (gb->type) {
|
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
|
|
|
break;
|
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
|
|
|
aggregate_size += sizeof(struct gb_loopback_transfer_request) +
|
|
|
|
gb->size;
|
|
|
|
break;
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
|
|
|
aggregate_size += sizeof(struct gb_loopback_transfer_request) +
|
|
|
|
sizeof(struct gb_loopback_transfer_response) +
|
|
|
|
gb->size * 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2015-07-21 22:50:08 +00:00
|
|
|
|
|
|
|
/* Calculate bytes per second */
|
|
|
|
throughput = USEC_PER_SEC;
|
|
|
|
do_div(throughput, latency);
|
|
|
|
throughput *= aggregate_size;
|
|
|
|
gb_loopback_update_stats(&gb->throughput, throughput);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static void gb_loopback_calculate_stats(struct gb_loopback *gb)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
u32 lat;
|
2015-04-16 07:53:59 +00:00
|
|
|
u64 tmp;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-21 22:50:08 +00:00
|
|
|
/* Express latency in terms of microseconds */
|
|
|
|
tmp = gb->elapsed_nsecs;
|
|
|
|
do_div(tmp, NSEC_PER_USEC);
|
2015-04-16 07:53:59 +00:00
|
|
|
lat = tmp;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-21 22:50:08 +00:00
|
|
|
/* Log latency stastic */
|
|
|
|
gb_loopback_update_stats(&gb->latency, lat);
|
|
|
|
|
2015-07-21 22:50:10 +00:00
|
|
|
/* Log throughput and requests using latency as benchmark */
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_throughput_update(gb, lat);
|
2015-07-21 22:50:10 +00:00
|
|
|
gb_loopback_requests_update(gb, lat);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_loopback_fn(void *data)
|
|
|
|
{
|
|
|
|
int error = 0;
|
2015-07-28 17:34:36 +00:00
|
|
|
int ms_wait;
|
2015-03-31 07:51:59 +00:00
|
|
|
struct gb_loopback *gb = (struct gb_loopback *)data;
|
|
|
|
|
2015-07-24 09:02:56 +00:00
|
|
|
while (1) {
|
|
|
|
if (!gb->type)
|
|
|
|
wait_event_interruptible(gb->wq, gb->type ||
|
|
|
|
kthread_should_stop());
|
|
|
|
if (kthread_should_stop())
|
|
|
|
break;
|
2015-07-28 17:34:36 +00:00
|
|
|
|
|
|
|
mutex_lock(&gb->mutex);
|
2015-07-21 22:50:08 +00:00
|
|
|
if (gb->iteration_max) {
|
|
|
|
if (gb->iteration_count < gb->iteration_max) {
|
|
|
|
gb->iteration_count++;
|
2015-07-22 18:09:23 +00:00
|
|
|
sysfs_notify(&gb->connection->dev.kobj, NULL,
|
|
|
|
"iteration_count");
|
2015-07-21 22:50:08 +00:00
|
|
|
} else {
|
|
|
|
gb->type = 0;
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_unlock(&gb->mutex);
|
2015-07-21 22:50:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 19:20:48 +00:00
|
|
|
if (gb->type == GB_LOOPBACK_TYPE_PING)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_ping(gb);
|
2015-07-13 19:20:48 +00:00
|
|
|
else if (gb->type == GB_LOOPBACK_TYPE_TRANSFER)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_transfer(gb, gb->size);
|
2015-07-13 19:20:49 +00:00
|
|
|
else if (gb->type == GB_LOOPBACK_TYPE_SINK)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_sink(gb, gb->size);
|
2015-03-31 07:51:59 +00:00
|
|
|
if (error)
|
|
|
|
gb->error++;
|
2015-07-28 17:34:35 +00:00
|
|
|
gb_loopback_calculate_stats(gb);
|
2015-07-28 17:34:36 +00:00
|
|
|
ms_wait = gb->ms_wait;
|
|
|
|
mutex_unlock(&gb->mutex);
|
|
|
|
if (ms_wait)
|
|
|
|
msleep(ms_wait);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_loopback_connection_init(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
gb = kzalloc(sizeof(*gb), GFP_KERNEL);
|
|
|
|
if (!gb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
gb->connection = connection;
|
|
|
|
connection->private = gb;
|
2015-03-31 21:02:34 +00:00
|
|
|
retval = sysfs_create_groups(&connection->dev.kobj, loopback_groups);
|
2015-03-31 07:51:59 +00:00
|
|
|
if (retval)
|
2015-05-14 16:03:04 +00:00
|
|
|
goto out_free;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
/* Check the version */
|
|
|
|
retval = get_version(gb);
|
|
|
|
if (retval)
|
2015-05-14 16:03:04 +00:00
|
|
|
goto out_get_ver;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-13 19:20:50 +00:00
|
|
|
/* Calculate maximum payload */
|
|
|
|
gb->size_max = gb_operation_get_payload_size_max(connection);
|
|
|
|
if (gb->size_max <= sizeof(struct gb_loopback_transfer_request)) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto out_get_ver;
|
|
|
|
}
|
|
|
|
gb->size_max -= sizeof(struct gb_loopback_transfer_request);
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
gb_loopback_reset_stats(gb);
|
2015-07-24 09:02:56 +00:00
|
|
|
init_waitqueue_head(&gb->wq);
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_init(&gb->mutex);
|
2015-03-31 07:51:59 +00:00
|
|
|
gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback");
|
|
|
|
if (IS_ERR(gb->task)) {
|
2015-05-12 02:16:35 +00:00
|
|
|
retval = PTR_ERR(gb->task);
|
2015-05-14 16:03:04 +00:00
|
|
|
goto out_get_ver;
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2015-05-14 16:03:04 +00:00
|
|
|
out_get_ver:
|
|
|
|
sysfs_remove_groups(&connection->dev.kobj, loopback_groups);
|
|
|
|
out_free:
|
2015-03-31 07:51:59 +00:00
|
|
|
kfree(gb);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_loopback_connection_exit(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb = connection->private;
|
|
|
|
|
|
|
|
if (!IS_ERR_OR_NULL(gb->task))
|
|
|
|
kthread_stop(gb->task);
|
2015-03-31 21:02:34 +00:00
|
|
|
sysfs_remove_groups(&connection->dev.kobj, loopback_groups);
|
2015-03-31 07:51:59 +00:00
|
|
|
kfree(gb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct gb_protocol loopback_protocol = {
|
|
|
|
.name = "loopback",
|
|
|
|
.id = GREYBUS_PROTOCOL_LOOPBACK,
|
|
|
|
.major = GB_LOOPBACK_VERSION_MAJOR,
|
|
|
|
.minor = GB_LOOPBACK_VERSION_MINOR,
|
|
|
|
.connection_init = gb_loopback_connection_init,
|
|
|
|
.connection_exit = gb_loopback_connection_exit,
|
2015-05-12 02:16:39 +00:00
|
|
|
.request_recv = gb_loopback_request_recv,
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
gb_protocol_driver(&loopback_protocol);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|