2015-05-22 18:02:08 +00:00
|
|
|
/*
|
|
|
|
* SVC Greybus driver.
|
|
|
|
*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
* Copyright 2015 Linaro Ltd.
|
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
2015-08-06 07:14:55 +00:00
|
|
|
#include <linux/workqueue.h>
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-09-02 15:57:13 +00:00
|
|
|
#include "greybus.h"
|
|
|
|
|
2015-11-11 09:07:08 +00:00
|
|
|
#define CPORT_FLAGS_E2EFC BIT(0)
|
|
|
|
#define CPORT_FLAGS_CSD_N BIT(1)
|
|
|
|
#define CPORT_FLAGS_CSV_N BIT(2)
|
2015-07-24 23:02:34 +00:00
|
|
|
|
2015-07-24 10:02:21 +00:00
|
|
|
|
2015-12-02 17:23:29 +00:00
|
|
|
struct gb_svc_deferred_request {
|
2015-08-06 07:14:55 +00:00
|
|
|
struct work_struct work;
|
2015-12-02 17:23:29 +00:00
|
|
|
struct gb_operation *operation;
|
2015-08-06 07:14:55 +00:00
|
|
|
};
|
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
|
2015-11-25 14:59:09 +00:00
|
|
|
static ssize_t endo_id_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gb_svc *svc = to_gb_svc(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "0x%04x\n", svc->endo_id);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(endo_id);
|
|
|
|
|
|
|
|
static ssize_t ap_intf_id_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gb_svc *svc = to_gb_svc(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%u\n", svc->ap_intf_id);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(ap_intf_id);
|
|
|
|
|
|
|
|
static struct attribute *svc_attrs[] = {
|
|
|
|
&dev_attr_endo_id.attr,
|
|
|
|
&dev_attr_ap_intf_id.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(svc);
|
|
|
|
|
2015-08-31 11:51:07 +00:00
|
|
|
static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
|
2015-05-22 18:02:08 +00:00
|
|
|
{
|
|
|
|
struct gb_svc_intf_device_id_request request;
|
|
|
|
|
|
|
|
request.intf_id = intf_id;
|
|
|
|
request.device_id = device_id;
|
|
|
|
|
|
|
|
return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:51:06 +00:00
|
|
|
int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
|
2015-05-22 18:02:08 +00:00
|
|
|
{
|
|
|
|
struct gb_svc_intf_reset_request request;
|
|
|
|
|
|
|
|
request.intf_id = intf_id;
|
|
|
|
|
|
|
|
return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
}
|
2015-08-31 11:51:06 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-09-09 15:38:29 +00:00
|
|
|
int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
|
|
|
|
u32 *value)
|
|
|
|
{
|
|
|
|
struct gb_svc_dme_peer_get_request request;
|
|
|
|
struct gb_svc_dme_peer_get_response response;
|
|
|
|
u16 result;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
request.intf_id = intf_id;
|
|
|
|
request.attr = cpu_to_le16(attr);
|
|
|
|
request.selector = cpu_to_le16(selector);
|
|
|
|
|
|
|
|
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
|
|
|
|
&request, sizeof(request),
|
|
|
|
&response, sizeof(response));
|
|
|
|
if (ret) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to get DME attribute (%hhu %hx %hu): %d\n",
|
|
|
|
intf_id, attr, selector, ret);
|
2015-09-09 15:38:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = le16_to_cpu(response.result_code);
|
|
|
|
if (result) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "UniPro error while getting DME attribute (%hhu %hx %hu): %hu\n",
|
|
|
|
intf_id, attr, selector, result);
|
2015-09-09 15:38:29 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
*value = le32_to_cpu(response.attr_value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
|
|
|
|
|
|
|
|
int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
|
|
|
|
u32 value)
|
|
|
|
{
|
|
|
|
struct gb_svc_dme_peer_set_request request;
|
|
|
|
struct gb_svc_dme_peer_set_response response;
|
|
|
|
u16 result;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
request.intf_id = intf_id;
|
|
|
|
request.attr = cpu_to_le16(attr);
|
|
|
|
request.selector = cpu_to_le16(selector);
|
|
|
|
request.value = cpu_to_le32(value);
|
|
|
|
|
|
|
|
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
|
|
|
|
&request, sizeof(request),
|
|
|
|
&response, sizeof(response));
|
|
|
|
if (ret) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to set DME attribute (%hhu %hx %hu %u): %d\n",
|
|
|
|
intf_id, attr, selector, value, ret);
|
2015-09-09 15:38:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = le16_to_cpu(response.result_code);
|
|
|
|
if (result) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "UniPro error while setting DME attribute (%hhu %hx %hu %u): %hu\n",
|
|
|
|
intf_id, attr, selector, value, result);
|
2015-09-09 15:38:29 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
|
|
|
|
|
2015-09-24 21:40:29 +00:00
|
|
|
/*
|
|
|
|
* T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
|
|
|
|
* status attribute. AP needs to read and clear it, after reading a non-zero
|
|
|
|
* value from it.
|
|
|
|
*
|
|
|
|
* FIXME: This is module-hardware dependent and needs to be extended for every
|
|
|
|
* type of module we want to support.
|
|
|
|
*/
|
|
|
|
static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
|
|
|
|
{
|
2015-11-03 17:03:23 +00:00
|
|
|
struct gb_host_device *hd = intf->hd;
|
2015-09-24 21:40:29 +00:00
|
|
|
int ret;
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
/* Read and clear boot status in T_TstSrcIncrement */
|
|
|
|
ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
|
|
|
|
DME_ATTR_T_TST_SRC_INCREMENT,
|
|
|
|
DME_ATTR_SELECTOR_INDEX, &value);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A nonzero boot status indicates the module has finished
|
|
|
|
* booting. Clear it.
|
|
|
|
*/
|
|
|
|
if (!value) {
|
|
|
|
dev_err(&intf->dev, "Module not ready yet\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2015-10-07 19:40:24 +00:00
|
|
|
/*
|
|
|
|
* Check if the module needs to boot from unipro.
|
|
|
|
* For ES2: We need to check lowest 8 bits of 'value'.
|
|
|
|
* For ES3: We need to check highest 8 bits out of 32 of 'value'.
|
|
|
|
*
|
|
|
|
* FIXME: Add code to find if we are on ES2 or ES3 to have separate
|
|
|
|
* checks.
|
|
|
|
*/
|
|
|
|
if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
|
|
|
|
value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
|
|
|
|
intf->boot_over_unipro = true;
|
|
|
|
|
2015-09-24 21:40:29 +00:00
|
|
|
return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
|
|
|
|
DME_ATTR_T_TST_SRC_INCREMENT,
|
|
|
|
DME_ATTR_SELECTOR_INDEX, 0);
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:51:06 +00:00
|
|
|
int gb_svc_connection_create(struct gb_svc *svc,
|
2015-05-22 18:02:08 +00:00
|
|
|
u8 intf1_id, u16 cport1_id,
|
2015-10-07 19:40:24 +00:00
|
|
|
u8 intf2_id, u16 cport2_id,
|
|
|
|
bool boot_over_unipro)
|
2015-05-22 18:02:08 +00:00
|
|
|
{
|
|
|
|
struct gb_svc_conn_create_request request;
|
|
|
|
|
|
|
|
request.intf1_id = intf1_id;
|
2015-09-15 14:33:51 +00:00
|
|
|
request.cport1_id = cpu_to_le16(cport1_id);
|
2015-05-22 18:02:08 +00:00
|
|
|
request.intf2_id = intf2_id;
|
2015-09-15 14:33:51 +00:00
|
|
|
request.cport2_id = cpu_to_le16(cport2_id);
|
2015-07-24 23:02:34 +00:00
|
|
|
/*
|
|
|
|
* XXX: fix connections paramaters to TC0 and all CPort flags
|
|
|
|
* for now.
|
|
|
|
*/
|
|
|
|
request.tc = 0;
|
2015-10-07 19:40:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to skip setting E2EFC and other flags to the connection
|
|
|
|
* create request, for all cports, on an interface that need to boot
|
|
|
|
* over unipro, i.e. interfaces required to download firmware.
|
|
|
|
*/
|
|
|
|
if (boot_over_unipro)
|
|
|
|
request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
|
|
|
|
else
|
|
|
|
request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
|
2015-05-22 18:02:08 +00:00
|
|
|
|
|
|
|
return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
}
|
2015-08-31 11:51:06 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gb_svc_connection_create);
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-08-31 11:51:06 +00:00
|
|
|
void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
|
|
|
|
u8 intf2_id, u16 cport2_id)
|
2015-05-22 18:02:08 +00:00
|
|
|
{
|
|
|
|
struct gb_svc_conn_destroy_request request;
|
2015-08-31 11:51:05 +00:00
|
|
|
struct gb_connection *connection = svc->connection;
|
|
|
|
int ret;
|
2015-05-22 18:02:08 +00:00
|
|
|
|
|
|
|
request.intf1_id = intf1_id;
|
2015-09-15 14:33:51 +00:00
|
|
|
request.cport1_id = cpu_to_le16(cport1_id);
|
2015-05-22 18:02:08 +00:00
|
|
|
request.intf2_id = intf2_id;
|
2015-09-15 14:33:51 +00:00
|
|
|
request.cport2_id = cpu_to_le16(cport2_id);
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-08-31 11:51:05 +00:00
|
|
|
ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
2015-11-25 14:59:19 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&svc->dev, "failed to destroy connection (%hhu:%hu %hhu:%hu): %d\n",
|
|
|
|
intf1_id, cport1_id, intf2_id, cport2_id, ret);
|
|
|
|
}
|
2015-05-22 18:02:08 +00:00
|
|
|
}
|
2015-08-31 11:51:06 +00:00
|
|
|
EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-09-07 10:31:25 +00:00
|
|
|
/* Creates bi-directional routes between the devices */
|
2015-08-31 11:51:07 +00:00
|
|
|
static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
|
|
|
|
u8 intf2_id, u8 dev2_id)
|
2015-07-24 23:02:31 +00:00
|
|
|
{
|
|
|
|
struct gb_svc_route_create_request request;
|
|
|
|
|
|
|
|
request.intf1_id = intf1_id;
|
|
|
|
request.dev1_id = dev1_id;
|
|
|
|
request.intf2_id = intf2_id;
|
|
|
|
request.dev2_id = dev2_id;
|
|
|
|
|
|
|
|
return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
}
|
|
|
|
|
2015-09-07 12:35:26 +00:00
|
|
|
/* Destroys bi-directional routes between the devices */
|
|
|
|
static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
|
|
|
|
{
|
|
|
|
struct gb_svc_route_destroy_request request;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
request.intf1_id = intf1_id;
|
|
|
|
request.intf2_id = intf2_id;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
2015-11-25 14:59:19 +00:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&svc->dev, "failed to destroy route (%hhu %hhu): %d\n",
|
|
|
|
intf1_id, intf2_id, ret);
|
|
|
|
}
|
2015-09-07 12:35:26 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
static int gb_svc_version_request(struct gb_operation *op)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = op->connection;
|
2015-11-25 14:59:19 +00:00
|
|
|
struct gb_svc *svc = connection->private;
|
2015-09-15 08:48:01 +00:00
|
|
|
struct gb_protocol_version_request *request;
|
|
|
|
struct gb_protocol_version_response *response;
|
2015-07-21 12:14:19 +00:00
|
|
|
|
2015-11-19 17:28:01 +00:00
|
|
|
if (op->request->payload_size < sizeof(*request)) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "short version request (%zu < %zu)\n",
|
2015-11-19 17:28:01 +00:00
|
|
|
op->request->payload_size,
|
|
|
|
sizeof(*request));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:48:01 +00:00
|
|
|
request = op->request->payload;
|
2015-07-21 12:14:19 +00:00
|
|
|
|
2015-09-15 08:48:01 +00:00
|
|
|
if (request->major > GB_SVC_VERSION_MAJOR) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "unsupported major version (%hhu > %hhu)\n",
|
|
|
|
request->major, GB_SVC_VERSION_MAJOR);
|
2015-07-21 12:14:19 +00:00
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:48:01 +00:00
|
|
|
connection->module_major = request->major;
|
|
|
|
connection->module_minor = request->minor;
|
2015-08-11 02:06:14 +00:00
|
|
|
|
2015-11-25 14:59:19 +00:00
|
|
|
if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
|
2015-07-21 12:14:19 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-09-15 08:48:01 +00:00
|
|
|
response = op->response->payload;
|
|
|
|
response->major = connection->module_major;
|
|
|
|
response->minor = connection->module_minor;
|
2015-09-15 08:48:00 +00:00
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_svc_hello(struct gb_operation *op)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = op->connection;
|
2015-11-25 14:59:08 +00:00
|
|
|
struct gb_svc *svc = connection->private;
|
2015-07-21 12:14:19 +00:00
|
|
|
struct gb_svc_hello_request *hello_request;
|
|
|
|
int ret;
|
|
|
|
|
2015-08-11 01:59:19 +00:00
|
|
|
if (op->request->payload_size < sizeof(*hello_request)) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
|
|
|
|
op->request->payload_size,
|
|
|
|
sizeof(*hello_request));
|
2015-07-21 12:14:19 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hello_request = op->request->payload;
|
2015-11-25 14:59:09 +00:00
|
|
|
svc->endo_id = le16_to_cpu(hello_request->endo_id);
|
|
|
|
svc->ap_intf_id = hello_request->interface_id;
|
2015-07-21 12:14:19 +00:00
|
|
|
|
2015-11-25 14:59:08 +00:00
|
|
|
ret = device_add(&svc->dev);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:23:28 +00:00
|
|
|
static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
|
2015-09-23 23:48:08 +00:00
|
|
|
{
|
|
|
|
u8 intf_id = intf->interface_id;
|
|
|
|
u8 device_id;
|
|
|
|
|
|
|
|
device_id = intf->device_id;
|
2015-09-23 23:48:10 +00:00
|
|
|
gb_interface_remove(intf);
|
2015-09-23 23:48:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the two-way route between the AP and the interface.
|
|
|
|
*/
|
2015-11-25 14:59:09 +00:00
|
|
|
gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
|
2015-09-23 23:48:08 +00:00
|
|
|
|
|
|
|
ida_simple_remove(&svc->device_id_map, device_id);
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:23:29 +00:00
|
|
|
static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
|
2015-05-22 18:02:08 +00:00
|
|
|
{
|
2015-12-02 17:23:27 +00:00
|
|
|
struct gb_svc_intf_hotplug_request *request;
|
2015-12-02 17:23:29 +00:00
|
|
|
struct gb_connection *connection = operation->connection;
|
2015-08-06 07:14:55 +00:00
|
|
|
struct gb_svc *svc = connection->private;
|
2015-11-03 17:03:23 +00:00
|
|
|
struct gb_host_device *hd = connection->hd;
|
2015-07-21 12:14:19 +00:00
|
|
|
struct gb_interface *intf;
|
|
|
|
u8 intf_id, device_id;
|
|
|
|
int ret;
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-12-02 17:23:27 +00:00
|
|
|
/* The request message size has already been verified. */
|
2015-12-02 17:23:29 +00:00
|
|
|
request = operation->request->payload;
|
2015-12-02 17:23:27 +00:00
|
|
|
intf_id = request->intf_id;
|
|
|
|
|
|
|
|
dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-09-23 23:48:08 +00:00
|
|
|
intf = gb_interface_find(hd, intf_id);
|
|
|
|
if (intf) {
|
|
|
|
/*
|
|
|
|
* We have received a hotplug request for an interface that
|
|
|
|
* already exists.
|
|
|
|
*
|
|
|
|
* This can happen in cases like:
|
|
|
|
* - bootrom loading the firmware image and booting into that,
|
|
|
|
* which only generates a hotplug event. i.e. no hot-unplug
|
|
|
|
* event.
|
|
|
|
* - Or the firmware on the module crashed and sent hotplug
|
|
|
|
* request again to the SVC, which got propagated to AP.
|
|
|
|
*
|
|
|
|
* Remove the interface and add it again, and let user know
|
|
|
|
* about this with a print message.
|
|
|
|
*/
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_info(&svc->dev, "removing interface %hhu to add it again\n",
|
|
|
|
intf_id);
|
2015-12-02 17:23:28 +00:00
|
|
|
gb_svc_intf_remove(svc, intf);
|
2015-09-23 23:48:08 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
intf = gb_interface_create(hd, intf_id);
|
|
|
|
if (!intf) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to create interface %hhu\n",
|
|
|
|
intf_id);
|
2015-12-02 17:23:29 +00:00
|
|
|
return;
|
2015-07-21 12:14:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 21:40:29 +00:00
|
|
|
ret = gb_svc_read_and_clear_module_boot_status(intf);
|
|
|
|
if (ret)
|
|
|
|
goto destroy_interface;
|
|
|
|
|
2015-12-02 17:23:27 +00:00
|
|
|
intf->unipro_mfg_id = le32_to_cpu(request->data.unipro_mfg_id);
|
|
|
|
intf->unipro_prod_id = le32_to_cpu(request->data.unipro_prod_id);
|
|
|
|
intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
|
|
|
|
intf->product_id = le32_to_cpu(request->data.ara_prod_id);
|
2015-08-12 03:49:31 +00:00
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
/*
|
|
|
|
* Create a device id for the interface:
|
|
|
|
* - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
|
|
|
|
* - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
|
|
|
|
*
|
|
|
|
* XXX Do we need to allocate device ID for SVC or the AP here? And what
|
|
|
|
* XXX about an AP with multiple interface blocks?
|
|
|
|
*/
|
2015-09-15 07:18:08 +00:00
|
|
|
device_id = ida_simple_get(&svc->device_id_map,
|
2015-09-01 10:25:25 +00:00
|
|
|
GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
|
2015-07-21 12:14:19 +00:00
|
|
|
if (device_id < 0) {
|
|
|
|
ret = device_id;
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to allocate device id for interface %hhu: %d\n",
|
|
|
|
intf_id, ret);
|
2015-07-21 12:14:19 +00:00
|
|
|
goto destroy_interface;
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:51:06 +00:00
|
|
|
ret = gb_svc_intf_device_id(svc, intf_id, device_id);
|
2015-07-21 12:14:19 +00:00
|
|
|
if (ret) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to set device id %hhu for interface %hhu: %d\n",
|
|
|
|
device_id, intf_id, ret);
|
2015-07-21 12:14:19 +00:00
|
|
|
goto ida_put;
|
|
|
|
}
|
|
|
|
|
2015-07-24 23:02:32 +00:00
|
|
|
/*
|
|
|
|
* Create a two-way route between the AP and the new interface
|
|
|
|
*/
|
2015-11-25 14:59:09 +00:00
|
|
|
ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
|
2015-08-31 11:51:06 +00:00
|
|
|
intf_id, device_id);
|
2015-07-24 23:02:32 +00:00
|
|
|
if (ret) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to create route to interface %hhu (device id %hhu): %d\n",
|
|
|
|
intf_id, device_id, ret);
|
2015-09-07 12:35:26 +00:00
|
|
|
goto svc_id_free;
|
2015-07-24 23:02:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 12:14:19 +00:00
|
|
|
ret = gb_interface_init(intf, device_id);
|
|
|
|
if (ret) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_err(&svc->dev, "failed to initialize interface %hhu (device id %hhu): %d\n",
|
|
|
|
intf_id, device_id, ret);
|
2015-09-07 12:35:26 +00:00
|
|
|
goto destroy_route;
|
2015-07-21 12:14:19 +00:00
|
|
|
}
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-12-02 17:23:29 +00:00
|
|
|
return;
|
2015-07-21 12:14:19 +00:00
|
|
|
|
2015-09-07 12:35:26 +00:00
|
|
|
destroy_route:
|
2015-11-25 14:59:09 +00:00
|
|
|
gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
|
2015-07-21 12:14:19 +00:00
|
|
|
svc_id_free:
|
|
|
|
/*
|
|
|
|
* XXX Should we tell SVC that this id doesn't belong to interface
|
|
|
|
* XXX anymore.
|
|
|
|
*/
|
|
|
|
ida_put:
|
2015-09-15 07:18:08 +00:00
|
|
|
ida_simple_remove(&svc->device_id_map, device_id);
|
2015-07-21 12:14:19 +00:00
|
|
|
destroy_interface:
|
2015-09-23 23:48:10 +00:00
|
|
|
gb_interface_remove(intf);
|
2015-12-02 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 17:23:30 +00:00
|
|
|
static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
struct gb_svc *svc = operation->connection->private;
|
|
|
|
struct gb_svc_intf_hot_unplug_request *request;
|
|
|
|
struct gb_host_device *hd = operation->connection->hd;
|
|
|
|
struct gb_interface *intf;
|
|
|
|
u8 intf_id;
|
|
|
|
|
|
|
|
/* The request message size has already been verified. */
|
|
|
|
request = operation->request->payload;
|
|
|
|
intf_id = request->intf_id;
|
|
|
|
|
|
|
|
dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
|
|
|
|
|
|
|
|
intf = gb_interface_find(hd, intf_id);
|
|
|
|
if (!intf) {
|
|
|
|
dev_warn(&svc->dev, "could not find hot-unplug interface %hhu\n",
|
|
|
|
intf_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gb_svc_intf_remove(svc, intf);
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:23:29 +00:00
|
|
|
static void gb_svc_process_deferred_request(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct gb_svc_deferred_request *dr;
|
|
|
|
struct gb_operation *operation;
|
|
|
|
struct gb_svc *svc;
|
|
|
|
u8 type;
|
|
|
|
|
|
|
|
dr = container_of(work, struct gb_svc_deferred_request, work);
|
|
|
|
operation = dr->operation;
|
|
|
|
svc = operation->connection->private;
|
|
|
|
type = operation->request->header->type;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GB_SVC_TYPE_INTF_HOTPLUG:
|
|
|
|
gb_svc_process_intf_hotplug(operation);
|
|
|
|
break;
|
2015-12-02 17:23:30 +00:00
|
|
|
case GB_SVC_TYPE_INTF_HOT_UNPLUG:
|
|
|
|
gb_svc_process_intf_hot_unplug(operation);
|
|
|
|
break;
|
2015-12-02 17:23:29 +00:00
|
|
|
default:
|
|
|
|
dev_err(&svc->dev, "bad deferred request type: %02x\n", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
gb_operation_put(operation);
|
|
|
|
kfree(dr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_svc_queue_deferred_request(struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
struct gb_svc_deferred_request *dr;
|
|
|
|
|
|
|
|
dr = kmalloc(sizeof(*dr), GFP_KERNEL);
|
|
|
|
if (!dr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
gb_operation_get(operation);
|
|
|
|
|
|
|
|
dr->operation = operation;
|
|
|
|
INIT_WORK(&dr->work, gb_svc_process_deferred_request);
|
|
|
|
|
|
|
|
queue_work(system_unbound_wq, &dr->work);
|
|
|
|
|
|
|
|
return 0;
|
2015-08-06 07:14:55 +00:00
|
|
|
}
|
2015-07-21 12:14:19 +00:00
|
|
|
|
2015-08-06 07:14:55 +00:00
|
|
|
/*
|
|
|
|
* Bringing up a module can be time consuming, as that may require lots of
|
|
|
|
* initialization on the module side. Over that, we may also need to download
|
|
|
|
* the firmware first and flash that on the module.
|
|
|
|
*
|
|
|
|
* In order to make other hotplug events to not wait for all this to finish,
|
|
|
|
* handle most of module hotplug stuff outside of the hotplug callback, with
|
|
|
|
* help of a workqueue.
|
|
|
|
*/
|
|
|
|
static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
|
|
|
|
{
|
2015-11-25 14:59:19 +00:00
|
|
|
struct gb_svc *svc = op->connection->private;
|
2015-12-02 17:23:26 +00:00
|
|
|
struct gb_svc_intf_hotplug_request *request;
|
2015-08-06 07:14:55 +00:00
|
|
|
|
2015-12-02 17:23:26 +00:00
|
|
|
if (op->request->payload_size < sizeof(*request)) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
|
2015-12-02 17:23:26 +00:00
|
|
|
op->request->payload_size, sizeof(*request));
|
2015-08-06 07:14:55 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:23:26 +00:00
|
|
|
request = op->request->payload;
|
|
|
|
|
|
|
|
dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
|
|
|
|
|
2015-12-02 17:23:29 +00:00
|
|
|
return gb_svc_queue_deferred_request(op);
|
2015-05-22 18:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
|
|
|
|
{
|
2015-11-25 14:59:19 +00:00
|
|
|
struct gb_svc *svc = op->connection->private;
|
2015-12-02 17:23:26 +00:00
|
|
|
struct gb_svc_intf_hot_unplug_request *request;
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-12-02 17:23:26 +00:00
|
|
|
if (op->request->payload_size < sizeof(*request)) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
|
2015-12-02 17:23:26 +00:00
|
|
|
op->request->payload_size, sizeof(*request));
|
2015-05-22 18:02:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:23:26 +00:00
|
|
|
request = op->request->payload;
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-12-02 17:23:30 +00:00
|
|
|
dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
|
2015-05-22 18:02:08 +00:00
|
|
|
|
2015-12-02 17:23:30 +00:00
|
|
|
return gb_svc_queue_deferred_request(op);
|
2015-05-22 18:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_svc_intf_reset_recv(struct gb_operation *op)
|
|
|
|
{
|
2015-11-25 14:59:19 +00:00
|
|
|
struct gb_svc *svc = op->connection->private;
|
2015-05-22 18:02:08 +00:00
|
|
|
struct gb_message *request = op->request;
|
|
|
|
struct gb_svc_intf_reset_request *reset;
|
|
|
|
u8 intf_id;
|
|
|
|
|
|
|
|
if (request->payload_size < sizeof(*reset)) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
|
|
|
|
request->payload_size, sizeof(*reset));
|
2015-05-22 18:02:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
reset = request->payload;
|
|
|
|
|
|
|
|
intf_id = reset->intf_id;
|
|
|
|
|
|
|
|
/* FIXME Reset the interface here */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_svc_request_recv(u8 type, struct gb_operation *op)
|
|
|
|
{
|
2015-09-03 10:12:22 +00:00
|
|
|
struct gb_connection *connection = op->connection;
|
|
|
|
struct gb_svc *svc = connection->private;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SVC requests need to follow a specific order (at least initially) and
|
|
|
|
* below code takes care of enforcing that. The expected order is:
|
|
|
|
* - PROTOCOL_VERSION
|
|
|
|
* - SVC_HELLO
|
|
|
|
* - Any other request, but the earlier two.
|
|
|
|
*
|
|
|
|
* Incoming requests are guaranteed to be serialized and so we don't
|
|
|
|
* need to protect 'state' for any races.
|
|
|
|
*/
|
2015-05-22 18:02:08 +00:00
|
|
|
switch (type) {
|
2015-08-14 02:27:38 +00:00
|
|
|
case GB_REQUEST_TYPE_PROTOCOL_VERSION:
|
2015-09-03 10:12:22 +00:00
|
|
|
if (svc->state != GB_SVC_STATE_RESET)
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
2015-07-21 12:14:19 +00:00
|
|
|
case GB_SVC_TYPE_SVC_HELLO:
|
2015-09-03 10:12:22 +00:00
|
|
|
if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (svc->state != GB_SVC_STATE_SVC_HELLO)
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
|
|
|
|
type, svc->state);
|
2015-09-03 10:12:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GB_REQUEST_TYPE_PROTOCOL_VERSION:
|
|
|
|
ret = gb_svc_version_request(op);
|
|
|
|
if (!ret)
|
|
|
|
svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
|
|
|
|
return ret;
|
|
|
|
case GB_SVC_TYPE_SVC_HELLO:
|
|
|
|
ret = gb_svc_hello(op);
|
|
|
|
if (!ret)
|
|
|
|
svc->state = GB_SVC_STATE_SVC_HELLO;
|
|
|
|
return ret;
|
2015-05-22 18:02:08 +00:00
|
|
|
case GB_SVC_TYPE_INTF_HOTPLUG:
|
|
|
|
return gb_svc_intf_hotplug_recv(op);
|
|
|
|
case GB_SVC_TYPE_INTF_HOT_UNPLUG:
|
|
|
|
return gb_svc_intf_hot_unplug_recv(op);
|
|
|
|
case GB_SVC_TYPE_INTF_RESET:
|
|
|
|
return gb_svc_intf_reset_recv(op);
|
|
|
|
default:
|
2015-11-25 14:59:19 +00:00
|
|
|
dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
|
2015-05-22 18:02:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 14:59:06 +00:00
|
|
|
static void gb_svc_release(struct device *dev)
|
|
|
|
{
|
2015-11-25 14:59:08 +00:00
|
|
|
struct gb_svc *svc = to_gb_svc(dev);
|
2015-11-25 14:59:06 +00:00
|
|
|
|
|
|
|
ida_destroy(&svc->device_id_map);
|
|
|
|
kfree(svc);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct device_type greybus_svc_type = {
|
|
|
|
.name = "greybus_svc",
|
|
|
|
.release = gb_svc_release,
|
|
|
|
};
|
|
|
|
|
2015-05-22 18:02:08 +00:00
|
|
|
static int gb_svc_connection_init(struct gb_connection *connection)
|
|
|
|
{
|
2015-11-25 14:59:06 +00:00
|
|
|
struct gb_host_device *hd = connection->hd;
|
2015-05-22 18:02:08 +00:00
|
|
|
struct gb_svc *svc;
|
|
|
|
|
|
|
|
svc = kzalloc(sizeof(*svc), GFP_KERNEL);
|
|
|
|
if (!svc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-11-25 14:59:06 +00:00
|
|
|
svc->dev.parent = &hd->dev;
|
|
|
|
svc->dev.bus = &greybus_bus_type;
|
|
|
|
svc->dev.type = &greybus_svc_type;
|
2015-11-25 14:59:09 +00:00
|
|
|
svc->dev.groups = svc_groups;
|
2015-11-25 14:59:06 +00:00
|
|
|
svc->dev.dma_mask = svc->dev.parent->dma_mask;
|
|
|
|
device_initialize(&svc->dev);
|
|
|
|
|
|
|
|
dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
|
|
|
|
|
2015-11-25 14:59:07 +00:00
|
|
|
ida_init(&svc->device_id_map);
|
2015-09-03 10:12:22 +00:00
|
|
|
svc->state = GB_SVC_STATE_RESET;
|
2015-05-22 18:02:08 +00:00
|
|
|
svc->connection = connection;
|
|
|
|
connection->private = svc;
|
2015-07-21 12:14:18 +00:00
|
|
|
|
2015-11-25 14:59:06 +00:00
|
|
|
hd->svc = svc;
|
|
|
|
|
2015-07-21 12:14:20 +00:00
|
|
|
return 0;
|
2015-05-22 18:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_svc_connection_exit(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_svc *svc = connection->private;
|
|
|
|
|
2015-11-25 14:59:08 +00:00
|
|
|
if (device_is_registered(&svc->dev))
|
|
|
|
device_del(&svc->dev);
|
|
|
|
|
2015-07-24 23:02:33 +00:00
|
|
|
connection->hd->svc = NULL;
|
2015-07-21 12:14:18 +00:00
|
|
|
connection->private = NULL;
|
2015-11-25 14:59:06 +00:00
|
|
|
|
|
|
|
put_device(&svc->dev);
|
2015-05-22 18:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct gb_protocol svc_protocol = {
|
|
|
|
.name = "svc",
|
|
|
|
.id = GREYBUS_PROTOCOL_SVC,
|
2015-07-01 06:43:51 +00:00
|
|
|
.major = GB_SVC_VERSION_MAJOR,
|
|
|
|
.minor = GB_SVC_VERSION_MINOR,
|
2015-05-22 18:02:08 +00:00
|
|
|
.connection_init = gb_svc_connection_init,
|
|
|
|
.connection_exit = gb_svc_connection_exit,
|
|
|
|
.request_recv = gb_svc_request_recv,
|
2015-09-07 10:31:24 +00:00
|
|
|
.flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
|
|
|
|
GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
|
2015-11-25 14:59:13 +00:00
|
|
|
GB_PROTOCOL_SKIP_VERSION,
|
2015-05-22 18:02:08 +00:00
|
|
|
};
|
2015-07-03 11:30:29 +00:00
|
|
|
gb_builtin_protocol_driver(svc_protocol);
|