]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
greybus: Remove "gb-" prefix from .c files
authorViresh Kumar <viresh.kumar@linaro.org>
Wed, 21 Jan 2015 10:40:41 +0000 (16:10 +0530)
committerGreg Kroah-Hartman <greg@kroah.com>
Thu, 22 Jan 2015 03:27:20 +0000 (11:27 +0800)
Some files are still prefixed with "gb-" with the reasoning that the modules
would be named so, i.e.  gb-*.ko. But this can be done by playing a bit in
Makefile instead and keep uniform naming of .c files.

Lets try it.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
drivers/staging/greybus/Makefile
drivers/staging/greybus/battery.c [new file with mode: 0644]
drivers/staging/greybus/es1.c [new file with mode: 0644]
drivers/staging/greybus/es2.c [new file with mode: 0644]
drivers/staging/greybus/gb-battery.c [deleted file]
drivers/staging/greybus/gb-es1.c [deleted file]
drivers/staging/greybus/gb-es2.c [deleted file]
drivers/staging/greybus/gb-vibrator.c [deleted file]
drivers/staging/greybus/vibrator.c [new file with mode: 0644]

index 55b4a37c58fbb5b5924e29ebfa7ea75001adeb39..79af8128aac90a75886179d931e4a2ab8cd0c493 100644 (file)
@@ -17,6 +17,12 @@ gb-phy-y :=  gpb.o           \
                i2c.o   \
                usb.o
 
+# Prefix all modules with gb-
+gb-vibrator-y := vibrator.o
+gb-battery-y := battery.o
+gb-es1-y := es1.o
+gb-es2-y := es2.o
+
 obj-m += greybus.o
 obj-m += gb-phy.o
 obj-m += gb-vibrator.o
diff --git a/drivers/staging/greybus/battery.c b/drivers/staging/greybus/battery.c
new file mode 100644 (file)
index 0000000..2130d73
--- /dev/null
@@ -0,0 +1,373 @@
+/*
+ * Battery driver for a Greybus module.
+ *
+ * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
+ *
+ * Released under the GPLv2 only.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/power_supply.h>
+#include "greybus.h"
+
+struct gb_battery {
+       struct power_supply bat;
+       // FIXME
+       // we will want to keep the battery stats in here as we will be getting
+       // updates from the SVC "on the fly" so we don't have to always go ask
+       // the battery for some information.  Hopefully...
+       struct gb_connection *connection;
+       u8 version_major;
+       u8 version_minor;
+
+};
+#define to_gb_battery(x) container_of(x, struct gb_battery, bat)
+
+/* Version of the Greybus battery protocol we support */
+#define        GB_BATTERY_VERSION_MAJOR                0x00
+#define        GB_BATTERY_VERSION_MINOR                0x01
+
+/* Greybus battery request types */
+#define        GB_BATTERY_TYPE_INVALID                 0x00
+#define        GB_BATTERY_TYPE_PROTOCOL_VERSION        0x01
+#define        GB_BATTERY_TYPE_TECHNOLOGY              0x02
+#define        GB_BATTERY_TYPE_STATUS                  0x03
+#define        GB_BATTERY_TYPE_MAX_VOLTAGE             0x04
+#define        GB_BATTERY_TYPE_PERCENT_CAPACITY        0x05
+#define        GB_BATTERY_TYPE_TEMPERATURE             0x06
+#define        GB_BATTERY_TYPE_VOLTAGE                 0x07
+#define        GB_BATTERY_TYPE_CURRENT                 0x08
+#define GB_BATTERY_TYPE_CAPACITY               0x09    // TODO - POWER_SUPPLY_PROP_CURRENT_MAX
+#define GB_BATTERY_TYPE_SHUTDOWN_TEMP          0x0a    // TODO - POWER_SUPPLY_PROP_TEMP_ALERT_MAX
+
+struct gb_battery_proto_version_response {
+       __u8    major;
+       __u8    minor;
+};
+
+/* Should match up with battery types in linux/power_supply.h */
+#define GB_BATTERY_TECH_UNKNOWN                        0x0000
+#define GB_BATTERY_TECH_NiMH                   0x0001
+#define GB_BATTERY_TECH_LION                   0x0002
+#define GB_BATTERY_TECH_LIPO                   0x0003
+#define GB_BATTERY_TECH_LiFe                   0x0004
+#define GB_BATTERY_TECH_NiCd                   0x0005
+#define GB_BATTERY_TECH_LiMn                   0x0006
+
+struct gb_battery_technology_response {
+       __le32  technology;
+};
+
+/* Should match up with battery status in linux/power_supply.h */
+#define GB_BATTERY_STATUS_UNKNOWN              0x0000
+#define GB_BATTERY_STATUS_CHARGING             0x0001
+#define GB_BATTERY_STATUS_DISCHARGING          0x0002
+#define GB_BATTERY_STATUS_NOT_CHARGING         0x0003
+#define GB_BATTERY_STATUS_FULL                 0x0004
+
+struct gb_battery_status_response {
+       __le16  battery_status;
+};
+
+struct gb_battery_max_voltage_response {
+       __le32  max_voltage;
+};
+
+struct gb_battery_capacity_response {
+       __le32  capacity;
+};
+
+struct gb_battery_temperature_response {
+       __le32  temperature;
+};
+
+struct gb_battery_voltage_response {
+       __le32  voltage;
+};
+
+/*
+ * This request only uses the connection field, and if successful,
+ * fills in the major and minor protocol version of the target.
+ */
+static int get_version(struct gb_battery *gb)
+{
+       struct gb_battery_proto_version_response version_response;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection,
+                                  GB_BATTERY_TYPE_PROTOCOL_VERSION,
+                                  NULL, 0,
+                                  &version_response, sizeof(version_response));
+       if (retval)
+               return retval;
+
+       if (version_response.major > GB_BATTERY_VERSION_MAJOR) {
+               pr_err("unsupported major version (%hhu > %hhu)\n",
+                       version_response.major, GB_BATTERY_VERSION_MAJOR);
+               return -ENOTSUPP;
+       }
+
+       gb->version_major = version_response.major;
+       gb->version_minor = version_response.minor;
+       return 0;
+}
+
+static int get_tech(struct gb_battery *gb)
+{
+       struct gb_battery_technology_response tech_response;
+       u32 technology;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_TECHNOLOGY,
+                                  NULL, 0,
+                                  &tech_response, sizeof(tech_response));
+       if (retval)
+               return retval;
+
+       /*
+        * Map greybus values to power_supply values.  Hopefully these are
+        * "identical" which should allow gcc to optimize the code away to
+        * nothing.
+        */
+       technology = le32_to_cpu(tech_response.technology);
+       switch (technology) {
+       case GB_BATTERY_TECH_NiMH:
+               technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
+               break;
+       case GB_BATTERY_TECH_LION:
+               technology = POWER_SUPPLY_TECHNOLOGY_LION;
+               break;
+       case GB_BATTERY_TECH_LIPO:
+               technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
+               break;
+       case GB_BATTERY_TECH_LiFe:
+               technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
+               break;
+       case GB_BATTERY_TECH_NiCd:
+               technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
+               break;
+       case GB_BATTERY_TECH_LiMn:
+               technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
+               break;
+       case GB_BATTERY_TECH_UNKNOWN:
+       default:
+               technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
+               break;
+       }
+       return technology;
+}
+
+static int get_status(struct gb_battery *gb)
+{
+       struct gb_battery_status_response status_response;
+       u16 battery_status;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_STATUS,
+                                  NULL, 0,
+                                  &status_response, sizeof(status_response));
+       if (retval)
+               return retval;
+
+       /*
+        * Map greybus values to power_supply values.  Hopefully these are
+        * "identical" which should allow gcc to optimize the code away to
+        * nothing.
+        */
+       battery_status = le16_to_cpu(status_response.battery_status);
+       switch (battery_status) {
+       case GB_BATTERY_STATUS_CHARGING:
+               battery_status = POWER_SUPPLY_STATUS_CHARGING;
+               break;
+       case GB_BATTERY_STATUS_DISCHARGING:
+               battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
+               break;
+       case GB_BATTERY_STATUS_NOT_CHARGING:
+               battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
+               break;
+       case GB_BATTERY_STATUS_FULL:
+               battery_status = POWER_SUPPLY_STATUS_FULL;
+               break;
+       case GB_BATTERY_STATUS_UNKNOWN:
+       default:
+               battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
+               break;
+       }
+       return battery_status;
+}
+
+static int get_max_voltage(struct gb_battery *gb)
+{
+       struct gb_battery_max_voltage_response volt_response;
+       u32 max_voltage;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_MAX_VOLTAGE,
+                                  NULL, 0,
+                                  &volt_response, sizeof(volt_response));
+       if (retval)
+               return retval;
+
+       max_voltage = le32_to_cpu(volt_response.max_voltage);
+       return max_voltage;
+}
+
+static int get_percent_capacity(struct gb_battery *gb)
+{
+       struct gb_battery_capacity_response capacity_response;
+       u32 capacity;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection,
+                                  GB_BATTERY_TYPE_PERCENT_CAPACITY,
+                                  NULL, 0, &capacity_response,
+                                  sizeof(capacity_response));
+       if (retval)
+               return retval;
+
+       capacity = le32_to_cpu(capacity_response.capacity);
+       return capacity;
+}
+
+static int get_temp(struct gb_battery *gb)
+{
+       struct gb_battery_temperature_response temp_response;
+       u32 temperature;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_TEMPERATURE,
+                                  NULL, 0,
+                                  &temp_response, sizeof(temp_response));
+       if (retval)
+               return retval;
+
+       temperature = le32_to_cpu(temp_response.temperature);
+       return temperature;
+}
+
+static int get_voltage(struct gb_battery *gb)
+{
+       struct gb_battery_voltage_response voltage_response;
+       u32 voltage;
+       int retval;
+
+       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_VOLTAGE,
+                                  NULL, 0,
+                                  &voltage_response, sizeof(voltage_response));
+       if (retval)
+               return retval;
+
+       voltage = le32_to_cpu(voltage_response.voltage);
+       return voltage;
+}
+
+static int get_property(struct power_supply *b,
+                       enum power_supply_property psp,
+                       union power_supply_propval *val)
+{
+       struct gb_battery *gb = to_gb_battery(b);
+
+       switch (psp) {
+       case POWER_SUPPLY_PROP_TECHNOLOGY:
+               val->intval = get_tech(gb);
+               break;
+
+       case POWER_SUPPLY_PROP_STATUS:
+               val->intval = get_status(gb);
+               break;
+
+       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+               val->intval = get_max_voltage(gb);
+               break;
+
+       case POWER_SUPPLY_PROP_CAPACITY:
+               val->intval = get_percent_capacity(gb);
+               break;
+
+       case POWER_SUPPLY_PROP_TEMP:
+               val->intval = get_temp(gb);
+               break;
+
+       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+               val->intval = get_voltage(gb);
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+// FIXME - verify this list, odds are some can be removed and others added.
+static enum power_supply_property battery_props[] = {
+       POWER_SUPPLY_PROP_TECHNOLOGY,
+       POWER_SUPPLY_PROP_STATUS,
+       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+       POWER_SUPPLY_PROP_CAPACITY,
+       POWER_SUPPLY_PROP_TEMP,
+       POWER_SUPPLY_PROP_VOLTAGE_NOW,
+};
+
+static int gb_battery_connection_init(struct gb_connection *connection)
+{
+       struct gb_battery *gb;
+       struct power_supply *b;
+       int retval;
+
+       gb = kzalloc(sizeof(*gb), GFP_KERNEL);
+       if (!gb)
+               return -ENOMEM;
+
+       gb->connection = connection;
+       connection->private = gb;
+
+       /* Check the version */
+       retval = get_version(gb);
+       if (retval) {
+               kfree(gb);
+               return retval;
+       }
+
+       b = &gb->bat;
+       // FIXME - get a better (i.e. unique) name
+       // FIXME - anything else needs to be set?
+       b->name                 = "gb_battery";
+       b->type                 = POWER_SUPPLY_TYPE_BATTERY,
+       b->properties           = battery_props,
+       b->num_properties       = ARRAY_SIZE(battery_props),
+       b->get_property         = get_property,
+
+       retval = power_supply_register(&connection->bundle->intf->dev, b);
+       if (retval) {
+               kfree(gb);
+               return retval;
+       }
+
+       return 0;
+}
+
+static void gb_battery_connection_exit(struct gb_connection *connection)
+{
+       struct gb_battery *gb = connection->private;
+
+       power_supply_unregister(&gb->bat);
+       kfree(gb);
+}
+
+static struct gb_protocol battery_protocol = {
+       .name                   = "battery",
+       .id                     = GREYBUS_PROTOCOL_BATTERY,
+       .major                  = 0,
+       .minor                  = 1,
+       .connection_init        = gb_battery_connection_init,
+       .connection_exit        = gb_battery_connection_exit,
+       .request_recv           = NULL, /* no incoming requests */
+};
+
+gb_protocol_driver(&battery_protocol);
+
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/staging/greybus/es1.c b/drivers/staging/greybus/es1.c
new file mode 100644 (file)
index 0000000..2ec5d7b
--- /dev/null
@@ -0,0 +1,618 @@
+/*
+ * Greybus "AP" USB driver
+ *
+ * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
+ *
+ * Released under the GPLv2 only.
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/sizes.h>
+#include <linux/usb.h>
+
+#include "greybus.h"
+#include "svc_msg.h"
+#include "kernel_ver.h"
+
+/*
+ * Macros for making pointers explicitly opaque, such that the result
+ * isn't valid but also can't be mistaken for an ERR_PTR() value.
+ */
+#define conceal_urb(urb)       ((void *)((uintptr_t)(urb) ^ 0xbad))
+#define reveal_urb(cookie)     ((void *)((uintptr_t)(cookie) ^ 0xbad))
+
+/* Memory sizes for the buffers sent to/from the ES1 controller */
+#define ES1_SVC_MSG_SIZE       (sizeof(struct svc_msg) + SZ_64K)
+#define ES1_GBUF_MSG_SIZE_MAX  PAGE_SIZE
+
+static const struct usb_device_id id_table[] = {
+       /* Made up numbers for the SVC USB Bridge in ES1 */
+       { USB_DEVICE(0xffff, 0x0001) },
+       { },
+};
+MODULE_DEVICE_TABLE(usb, id_table);
+
+/*
+ * Number of CPort IN urbs in flight at any point in time.
+ * Adjust if we are having stalls in the USB buffer due to not enough urbs in
+ * flight.
+ */
+#define NUM_CPORT_IN_URB       4
+
+/* Number of CPort OUT urbs in flight at any point in time.
+ * Adjust if we get messages saying we are out of urbs in the system log.
+ */
+#define NUM_CPORT_OUT_URB      8
+
+/**
+ * es1_ap_dev - ES1 USB Bridge to AP structure
+ * @usb_dev: pointer to the USB device we are.
+ * @usb_intf: pointer to the USB interface we are bound to.
+ * @hd: pointer to our greybus_host_device structure
+ * @control_endpoint: endpoint to send data to SVC
+ * @svc_endpoint: endpoint for SVC data in
+ * @cport_in_endpoint: bulk in endpoint for CPort data
+ * @cport-out_endpoint: bulk out endpoint for CPort data
+ * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
+ * @svc_urb: urb for SVC messages coming in on @svc_endpoint
+ * @cport_in_urb: array of urbs for the CPort in messages
+ * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
+ * @cport_out_urb: array of urbs for the CPort out messages
+ * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
+ *                     not.
+ * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
+ */
+struct es1_ap_dev {
+       struct usb_device *usb_dev;
+       struct usb_interface *usb_intf;
+       struct greybus_host_device *hd;
+
+       __u8 control_endpoint;
+       __u8 svc_endpoint;
+       __u8 cport_in_endpoint;
+       __u8 cport_out_endpoint;
+
+       u8 *svc_buffer;
+       struct urb *svc_urb;
+
+       struct urb *cport_in_urb[NUM_CPORT_IN_URB];
+       u8 *cport_in_buffer[NUM_CPORT_IN_URB];
+       struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
+       bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
+       spinlock_t cport_out_urb_lock;
+};
+
+static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
+{
+       return (struct es1_ap_dev *)&hd->hd_priv;
+}
+
+static void cport_out_callback(struct urb *urb);
+
+/*
+ * Buffer constraints for the host driver.
+ *
+ * A "buffer" is used to hold data to be transferred for Greybus by
+ * the host driver.  A buffer is represented by a "buffer pointer",
+ * which defines a region of memory used by the host driver for
+ * transferring the data.  When Greybus allocates a buffer, it must
+ * do so subject to the constraints associated with the host driver.
+ * These constraints are specified by two parameters: the
+ * headroom; and the maximum buffer size.
+ *
+ *                     +------------------+
+ *                     |    Host driver   | \
+ *                     |   reserved area  |  }- headroom
+ *                     |      . . .       | /
+ *  buffer pointer ---> +------------------+
+ *                     | Buffer space for | \
+ *                     | transferred data |  }- buffer size
+ *                     |      . . .       | /   (limited to size_max)
+ *                     +------------------+
+ *
+ *  headroom:  Every buffer must have at least this much space
+ *             *before* the buffer pointer, reserved for use by the
+ *             host driver.  I.e., ((char *)buffer - headroom) must
+ *             point to valid memory, usable only by the host driver.
+ *  size_max:  The maximum size of a buffer (not including the
+ *             headroom) must not exceed this.
+ */
+static void hd_buffer_constraints(struct greybus_host_device *hd)
+{
+       /*
+        * Only one byte is required, but this produces a result
+        * that's better aligned for the user.
+        */
+       hd->buffer_headroom = sizeof(u32);      /* For cport id */
+       hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
+       BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
+}
+
+#define ES1_TIMEOUT    500     /* 500 ms for the SVC to do something */
+static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
+{
+       struct es1_ap_dev *es1 = hd_to_es1(hd);
+       int retval;
+
+       /* SVC messages go down our control pipe */
+       retval = usb_control_msg(es1->usb_dev,
+                                usb_sndctrlpipe(es1->usb_dev,
+                                                es1->control_endpoint),
+                                0x01,  /* vendor request AP message */
+                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
+                                0x00, 0x00,
+                                (char *)svc_msg,
+                                sizeof(*svc_msg),
+                                ES1_TIMEOUT);
+       if (retval != sizeof(*svc_msg))
+               return retval;
+
+       return 0;
+}
+
+static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
+{
+       struct urb *urb = NULL;
+       unsigned long flags;
+       int i;
+
+       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
+
+       /* Look in our pool of allocated urbs first, as that's the "fastest" */
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               if (es1->cport_out_urb_busy[i] == false) {
+                       es1->cport_out_urb_busy[i] = true;
+                       urb = es1->cport_out_urb[i];
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
+       if (urb)
+               return urb;
+
+       /*
+        * Crap, pool is empty, complain to the syslog and go allocate one
+        * dynamically as we have to succeed.
+        */
+       dev_err(&es1->usb_dev->dev,
+               "No free CPort OUT urbs, having to dynamically allocate one!\n");
+       return usb_alloc_urb(0, gfp_mask);
+}
+
+static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
+{
+       unsigned long flags;
+       int i;
+       /*
+        * See if this was an urb in our pool, if so mark it "free", otherwise
+        * we need to free it ourselves.
+        */
+       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               if (urb == es1->cport_out_urb[i]) {
+                       es1->cport_out_urb_busy[i] = false;
+                       urb = NULL;
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
+
+       /* If urb is not NULL, then we need to free this urb */
+       usb_free_urb(urb);
+}
+
+/*
+ * Returns an opaque cookie value if successful, or a pointer coded
+ * error otherwise.  If the caller wishes to cancel the in-flight
+ * buffer, it must supply the returned cookie to the cancel routine.
+ */
+static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
+                       void *buffer, size_t buffer_size, gfp_t gfp_mask)
+{
+       struct es1_ap_dev *es1 = hd_to_es1(hd);
+       struct usb_device *udev = es1->usb_dev;
+       u8 *transfer_buffer = buffer;
+       int transfer_buffer_size;
+       int retval;
+       struct urb *urb;
+
+       if (!buffer) {
+               pr_err("null buffer supplied to send\n");
+               return ERR_PTR(-EINVAL);
+       }
+       if (buffer_size > (size_t)INT_MAX) {
+               pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
+               return ERR_PTR(-EINVAL);
+       }
+       transfer_buffer--;
+       transfer_buffer_size = buffer_size + 1;
+
+       /*
+        * The data actually transferred will include an indication
+        * of where the data should be sent.  Do one last check of
+        * the target CPort id before filling it in.
+        */
+       if (cport_id == CPORT_ID_BAD) {
+               pr_err("request to send inbound data buffer\n");
+               return ERR_PTR(-EINVAL);
+       }
+       if (cport_id > (u16)U8_MAX) {
+               pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
+               return ERR_PTR(-EINVAL);
+       }
+       /* OK, the destination is fine; record it in the transfer buffer */
+       *transfer_buffer = cport_id;
+
+       /* Find a free urb */
+       urb = next_free_urb(es1, gfp_mask);
+       if (!urb)
+               return ERR_PTR(-ENOMEM);
+
+       usb_fill_bulk_urb(urb, udev,
+                         usb_sndbulkpipe(udev, es1->cport_out_endpoint),
+                         transfer_buffer, transfer_buffer_size,
+                         cport_out_callback, hd);
+       retval = usb_submit_urb(urb, gfp_mask);
+       if (retval) {
+               pr_err("error %d submitting URB\n", retval);
+               free_urb(es1, urb);
+               return ERR_PTR(retval);
+       }
+
+       return conceal_urb(urb);
+}
+
+/*
+ * The cookie value supplied is the value that buffer_send()
+ * returned to its caller.  It identifies the buffer that should be
+ * canceled.  This function must also handle (which is to say,
+ * ignore) a null cookie value.
+ */
+static void buffer_cancel(void *cookie)
+{
+
+       /*
+        * We really should be defensive and track all outstanding
+        * (sent) buffers rather than trusting the cookie provided
+        * is valid.  For the time being, this will do.
+        */
+       if (cookie)
+               usb_kill_urb(reveal_urb(cookie));
+}
+
+static struct greybus_host_driver es1_driver = {
+       .hd_priv_size           = sizeof(struct es1_ap_dev),
+       .buffer_send            = buffer_send,
+       .buffer_cancel          = buffer_cancel,
+       .submit_svc             = submit_svc,
+};
+
+/* Common function to report consistent warnings based on URB status */
+static int check_urb_status(struct urb *urb)
+{
+       struct device *dev = &urb->dev->dev;
+       int status = urb->status;
+
+       switch (status) {
+       case 0:
+               return 0;
+
+       case -EOVERFLOW:
+               dev_err(dev, "%s: overflow actual length is %d\n",
+                       __func__, urb->actual_length);
+       case -ECONNRESET:
+       case -ENOENT:
+       case -ESHUTDOWN:
+       case -EILSEQ:
+       case -EPROTO:
+               /* device is gone, stop sending */
+               return status;
+       }
+       dev_err(dev, "%s: unknown status %d\n", __func__, status);
+
+       return -EAGAIN;
+}
+
+static void ap_disconnect(struct usb_interface *interface)
+{
+       struct es1_ap_dev *es1;
+       struct usb_device *udev;
+       int i;
+
+       es1 = usb_get_intfdata(interface);
+       if (!es1)
+               return;
+
+       /* Tear down everything! */
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               struct urb *urb = es1->cport_out_urb[i];
+
+               if (!urb)
+                       break;
+               usb_kill_urb(urb);
+               usb_free_urb(urb);
+               es1->cport_out_urb[i] = NULL;
+               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
+       }
+
+       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
+               struct urb *urb = es1->cport_in_urb[i];
+
+               if (!urb)
+                       break;
+               usb_kill_urb(urb);
+               usb_free_urb(urb);
+               kfree(es1->cport_in_buffer[i]);
+               es1->cport_in_buffer[i] = NULL;
+       }
+
+       usb_kill_urb(es1->svc_urb);
+       usb_free_urb(es1->svc_urb);
+       es1->svc_urb = NULL;
+       kfree(es1->svc_buffer);
+       es1->svc_buffer = NULL;
+
+       usb_set_intfdata(interface, NULL);
+       udev = es1->usb_dev;
+       greybus_remove_hd(es1->hd);
+
+       usb_put_dev(udev);
+}
+
+/* Callback for when we get a SVC message */
+static void svc_in_callback(struct urb *urb)
+{
+       struct greybus_host_device *hd = urb->context;
+       struct device *dev = &urb->dev->dev;
+       int status = check_urb_status(urb);
+       int retval;
+
+       if (status) {
+               if ((status == -EAGAIN) || (status == -EPROTO))
+                       goto exit;
+               dev_err(dev, "urb svc in error %d (dropped)\n", status);
+               return;
+       }
+
+       /* We have a message, create a new message structure, add it to the
+        * list, and wake up our thread that will process the messages.
+        */
+       greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
+
+exit:
+       /* resubmit the urb to get more messages */
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval)
+               dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
+}
+
+static void cport_in_callback(struct urb *urb)
+{
+       struct greybus_host_device *hd = urb->context;
+       struct device *dev = &urb->dev->dev;
+       int status = check_urb_status(urb);
+       int retval;
+       u16 cport_id;
+       u8 *data;
+
+       if (status) {
+               if ((status == -EAGAIN) || (status == -EPROTO))
+                       goto exit;
+               dev_err(dev, "urb cport in error %d (dropped)\n", status);
+               return;
+       }
+
+       /* The size has to be at least one, for the cport id */
+       if (!urb->actual_length) {
+               dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
+               goto exit;
+       }
+
+       /*
+        * Our CPort number is the first byte of the data stream,
+        * the rest of the stream is "real" data
+        */
+       data = urb->transfer_buffer;
+       cport_id = (u16)data[0];
+       data = &data[1];
+
+       /* Pass this data to the greybus core */
+       greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
+
+exit:
+       /* put our urb back in the request pool */
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval)
+               dev_err(dev, "%s: error %d in submitting urb.\n",
+                       __func__, retval);
+}
+
+static void cport_out_callback(struct urb *urb)
+{
+       struct greybus_host_device *hd = urb->context;
+       struct es1_ap_dev *es1 = hd_to_es1(hd);
+       int status = check_urb_status(urb);
+       u8 *data = urb->transfer_buffer + 1;
+
+       /*
+        * Tell the submitter that the buffer send (attempt) is
+        * complete, and report the status.  The submitter's buffer
+        * starts after the one-byte CPort id we inserted.
+        */
+       data = urb->transfer_buffer + 1;
+       greybus_data_sent(hd, data, status);
+
+       free_urb(es1, urb);
+       /*
+        * Rest assured Greg, this craziness is getting fixed.
+        *
+        * Yes, you are right, we aren't telling anyone that the urb finished.
+        * "That's crazy!  How does this all even work?" you might be saying.
+        * The "magic" is the idea that greybus works on the "operation" level,
+        * not the "send a buffer" level.  All operations are "round-trip" with
+        * a response from the device that the operation finished, or it will
+        * time out.  Because of that, we don't care that this urb finished, or
+        * failed, or did anything else, as higher levels of the protocol stack
+        * will handle completions and timeouts and the rest.
+        *
+        * This protocol is "needed" due to some hardware restrictions on the
+        * current generation of Unipro controllers.  Think about it for a
+        * minute, this is a USB driver, talking to a Unipro bridge, impedance
+        * mismatch is huge, yet the Unipro controller are even more
+        * underpowered than this little USB controller.  We rely on the round
+        * trip to keep stalls in the Unipro controllers from happening so that
+        * we can keep data flowing properly, no matter how slow it might be.
+        *
+        * Once again, a wonderful bus protocol cut down in its prime by a naive
+        * controller chip.  We dream of the day we have a "real" HCD for
+        * Unipro.  Until then, we suck it up and make the hardware work, as
+        * that's the job of the firmware and kernel.
+        * </rant>
+        */
+}
+
+/*
+ * The ES1 USB Bridge device contains 4 endpoints
+ * 1 Control - usual USB stuff + AP -> SVC messages
+ * 1 Interrupt IN - SVC -> AP messages
+ * 1 Bulk IN - CPort data in
+ * 1 Bulk OUT - CPort data out
+ */
+static int ap_probe(struct usb_interface *interface,
+                   const struct usb_device_id *id)
+{
+       struct es1_ap_dev *es1;
+       struct greybus_host_device *hd;
+       struct usb_device *udev;
+       struct usb_host_interface *iface_desc;
+       struct usb_endpoint_descriptor *endpoint;
+       bool int_in_found = false;
+       bool bulk_in_found = false;
+       bool bulk_out_found = false;
+       int retval = -ENOMEM;
+       int i;
+       u8 svc_interval = 0;
+
+       udev = usb_get_dev(interface_to_usbdev(interface));
+
+       hd = greybus_create_hd(&es1_driver, &udev->dev);
+       if (!hd) {
+               usb_put_dev(udev);
+               return -ENOMEM;
+       }
+
+       /* Fill in the buffer allocation constraints */
+       hd_buffer_constraints(hd);
+
+       es1 = hd_to_es1(hd);
+       es1->hd = hd;
+       es1->usb_intf = interface;
+       es1->usb_dev = udev;
+       spin_lock_init(&es1->cport_out_urb_lock);
+       usb_set_intfdata(interface, es1);
+
+       /* Control endpoint is the pipe to talk to this AP, so save it off */
+       endpoint = &udev->ep0.desc;
+       es1->control_endpoint = endpoint->bEndpointAddress;
+
+       /* find all 3 of our endpoints */
+       iface_desc = interface->cur_altsetting;
+       for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
+               endpoint = &iface_desc->endpoint[i].desc;
+
+               if (usb_endpoint_is_int_in(endpoint)) {
+                       es1->svc_endpoint = endpoint->bEndpointAddress;
+                       svc_interval = endpoint->bInterval;
+                       int_in_found = true;
+               } else if (usb_endpoint_is_bulk_in(endpoint)) {
+                       es1->cport_in_endpoint = endpoint->bEndpointAddress;
+                       bulk_in_found = true;
+               } else if (usb_endpoint_is_bulk_out(endpoint)) {
+                       es1->cport_out_endpoint = endpoint->bEndpointAddress;
+                       bulk_out_found = true;
+               } else {
+                       dev_err(&udev->dev,
+                               "Unknown endpoint type found, address %x\n",
+                               endpoint->bEndpointAddress);
+               }
+       }
+       if ((int_in_found == false) ||
+           (bulk_in_found == false) ||
+           (bulk_out_found == false)) {
+               dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
+               goto error;
+       }
+
+       /* Create our buffer and URB to get SVC messages, and start it up */
+       es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
+       if (!es1->svc_buffer)
+               goto error;
+
+       es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
+       if (!es1->svc_urb)
+               goto error;
+
+       usb_fill_int_urb(es1->svc_urb, udev,
+                        usb_rcvintpipe(udev, es1->svc_endpoint),
+                        es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
+                        hd, svc_interval);
+       retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
+       if (retval)
+               goto error;
+
+       /* Allocate buffers for our cport in messages and start them up */
+       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
+               struct urb *urb;
+               u8 *buffer;
+
+               urb = usb_alloc_urb(0, GFP_KERNEL);
+               if (!urb)
+                       goto error;
+               buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
+               if (!buffer)
+                       goto error;
+
+               usb_fill_bulk_urb(urb, udev,
+                                 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
+                                 buffer, ES1_GBUF_MSG_SIZE_MAX,
+                                 cport_in_callback, hd);
+               es1->cport_in_urb[i] = urb;
+               es1->cport_in_buffer[i] = buffer;
+               retval = usb_submit_urb(urb, GFP_KERNEL);
+               if (retval)
+                       goto error;
+       }
+
+       /* Allocate urbs for our CPort OUT messages */
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               struct urb *urb;
+
+               urb = usb_alloc_urb(0, GFP_KERNEL);
+               if (!urb)
+                       goto error;
+
+               es1->cport_out_urb[i] = urb;
+               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
+       }
+
+       return 0;
+error:
+       ap_disconnect(interface);
+
+       return retval;
+}
+
+static struct usb_driver es1_ap_driver = {
+       .name =         "es1_ap_driver",
+       .probe =        ap_probe,
+       .disconnect =   ap_disconnect,
+       .id_table =     id_table,
+};
+
+module_usb_driver(es1_ap_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
diff --git a/drivers/staging/greybus/es2.c b/drivers/staging/greybus/es2.c
new file mode 100644 (file)
index 0000000..4154cce
--- /dev/null
@@ -0,0 +1,618 @@
+/*
+ * Greybus "AP" USB driver for "ES2" controller chips
+ *
+ * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
+ *
+ * Released under the GPLv2 only.
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/sizes.h>
+#include <linux/usb.h>
+
+#include "greybus.h"
+#include "svc_msg.h"
+#include "kernel_ver.h"
+
+/*
+ * Macros for making pointers explicitly opaque, such that the result
+ * isn't valid but also can't be mistaken for an ERR_PTR() value.
+ */
+#define conceal_urb(urb)       ((void *)((uintptr_t)(urb) ^ 0xbad))
+#define reveal_urb(cookie)     ((void *)((uintptr_t)(cookie) ^ 0xbad))
+
+/* Memory sizes for the buffers sent to/from the ES1 controller */
+#define ES1_SVC_MSG_SIZE       (sizeof(struct svc_msg) + SZ_64K)
+#define ES1_GBUF_MSG_SIZE_MAX  PAGE_SIZE
+
+static const struct usb_device_id id_table[] = {
+       /* Made up numbers for the SVC USB Bridge in ES1 */
+       { USB_DEVICE(0xffff, 0x0001) },
+       { },
+};
+MODULE_DEVICE_TABLE(usb, id_table);
+
+/*
+ * Number of CPort IN urbs in flight at any point in time.
+ * Adjust if we are having stalls in the USB buffer due to not enough urbs in
+ * flight.
+ */
+#define NUM_CPORT_IN_URB       4
+
+/* Number of CPort OUT urbs in flight at any point in time.
+ * Adjust if we get messages saying we are out of urbs in the system log.
+ */
+#define NUM_CPORT_OUT_URB      8
+
+/**
+ * es1_ap_dev - ES1 USB Bridge to AP structure
+ * @usb_dev: pointer to the USB device we are.
+ * @usb_intf: pointer to the USB interface we are bound to.
+ * @hd: pointer to our greybus_host_device structure
+ * @control_endpoint: endpoint to send data to SVC
+ * @svc_endpoint: endpoint for SVC data in
+ * @cport_in_endpoint: bulk in endpoint for CPort data
+ * @cport-out_endpoint: bulk out endpoint for CPort data
+ * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
+ * @svc_urb: urb for SVC messages coming in on @svc_endpoint
+ * @cport_in_urb: array of urbs for the CPort in messages
+ * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
+ * @cport_out_urb: array of urbs for the CPort out messages
+ * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
+ *                     not.
+ * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
+ */
+struct es1_ap_dev {
+       struct usb_device *usb_dev;
+       struct usb_interface *usb_intf;
+       struct greybus_host_device *hd;
+
+       __u8 control_endpoint;
+       __u8 svc_endpoint;
+       __u8 cport_in_endpoint;
+       __u8 cport_out_endpoint;
+
+       u8 *svc_buffer;
+       struct urb *svc_urb;
+
+       struct urb *cport_in_urb[NUM_CPORT_IN_URB];
+       u8 *cport_in_buffer[NUM_CPORT_IN_URB];
+       struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
+       bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
+       spinlock_t cport_out_urb_lock;
+};
+
+static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
+{
+       return (struct es1_ap_dev *)&hd->hd_priv;
+}
+
+static void cport_out_callback(struct urb *urb);
+
+/*
+ * Buffer constraints for the host driver.
+ *
+ * A "buffer" is used to hold data to be transferred for Greybus by
+ * the host driver.  A buffer is represented by a "buffer pointer",
+ * which defines a region of memory used by the host driver for
+ * transferring the data.  When Greybus allocates a buffer, it must
+ * do so subject to the constraints associated with the host driver.
+ * These constraints are specified by two parameters: the
+ * headroom; and the maximum buffer size.
+ *
+ *                     +------------------+
+ *                     |    Host driver   | \
+ *                     |   reserved area  |  }- headroom
+ *                     |      . . .       | /
+ *  buffer pointer ---> +------------------+
+ *                     | Buffer space for | \
+ *                     | transferred data |  }- buffer size
+ *                     |      . . .       | /   (limited to size_max)
+ *                     +------------------+
+ *
+ *  headroom:  Every buffer must have at least this much space
+ *             *before* the buffer pointer, reserved for use by the
+ *             host driver.  I.e., ((char *)buffer - headroom) must
+ *             point to valid memory, usable only by the host driver.
+ *  size_max:  The maximum size of a buffer (not including the
+ *             headroom) must not exceed this.
+ */
+static void hd_buffer_constraints(struct greybus_host_device *hd)
+{
+       /*
+        * Only one byte is required, but this produces a result
+        * that's better aligned for the user.
+        */
+       hd->buffer_headroom = sizeof(u32);      /* For cport id */
+       hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
+       BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
+}
+
+#define ES1_TIMEOUT    500     /* 500 ms for the SVC to do something */
+static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
+{
+       struct es1_ap_dev *es1 = hd_to_es1(hd);
+       int retval;
+
+       /* SVC messages go down our control pipe */
+       retval = usb_control_msg(es1->usb_dev,
+                                usb_sndctrlpipe(es1->usb_dev,
+                                                es1->control_endpoint),
+                                0x01,  /* vendor request AP message */
+                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
+                                0x00, 0x00,
+                                (char *)svc_msg,
+                                sizeof(*svc_msg),
+                                ES1_TIMEOUT);
+       if (retval != sizeof(*svc_msg))
+               return retval;
+
+       return 0;
+}
+
+static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
+{
+       struct urb *urb = NULL;
+       unsigned long flags;
+       int i;
+
+       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
+
+       /* Look in our pool of allocated urbs first, as that's the "fastest" */
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               if (es1->cport_out_urb_busy[i] == false) {
+                       es1->cport_out_urb_busy[i] = true;
+                       urb = es1->cport_out_urb[i];
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
+       if (urb)
+               return urb;
+
+       /*
+        * Crap, pool is empty, complain to the syslog and go allocate one
+        * dynamically as we have to succeed.
+        */
+       dev_err(&es1->usb_dev->dev,
+               "No free CPort OUT urbs, having to dynamically allocate one!\n");
+       return usb_alloc_urb(0, gfp_mask);
+}
+
+static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
+{
+       unsigned long flags;
+       int i;
+       /*
+        * See if this was an urb in our pool, if so mark it "free", otherwise
+        * we need to free it ourselves.
+        */
+       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               if (urb == es1->cport_out_urb[i]) {
+                       es1->cport_out_urb_busy[i] = false;
+                       urb = NULL;
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
+
+       /* If urb is not NULL, then we need to free this urb */
+       usb_free_urb(urb);
+}
+
+/*
+ * Returns an opaque cookie value if successful, or a pointer coded
+ * error otherwise.  If the caller wishes to cancel the in-flight
+ * buffer, it must supply the returned cookie to the cancel routine.
+ */
+static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
+                       void *buffer, size_t buffer_size, gfp_t gfp_mask)
+{
+       struct es1_ap_dev *es1 = hd_to_es1(hd);
+       struct usb_device *udev = es1->usb_dev;
+       u8 *transfer_buffer = buffer;
+       int transfer_buffer_size;
+       int retval;
+       struct urb *urb;
+
+       if (!buffer) {
+               pr_err("null buffer supplied to send\n");
+               return ERR_PTR(-EINVAL);
+       }
+       if (buffer_size > (size_t)INT_MAX) {
+               pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
+               return ERR_PTR(-EINVAL);
+       }
+       transfer_buffer--;
+       transfer_buffer_size = buffer_size + 1;
+
+       /*
+        * The data actually transferred will include an indication
+        * of where the data should be sent.  Do one last check of
+        * the target CPort id before filling it in.
+        */
+       if (cport_id == CPORT_ID_BAD) {
+               pr_err("request to send inbound data buffer\n");
+               return ERR_PTR(-EINVAL);
+       }
+       if (cport_id > (u16)U8_MAX) {
+               pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
+               return ERR_PTR(-EINVAL);
+       }
+       /* OK, the destination is fine; record it in the transfer buffer */
+       *transfer_buffer = cport_id;
+
+       /* Find a free urb */
+       urb = next_free_urb(es1, gfp_mask);
+       if (!urb)
+               return ERR_PTR(-ENOMEM);
+
+       usb_fill_bulk_urb(urb, udev,
+                         usb_sndbulkpipe(udev, es1->cport_out_endpoint),
+                         transfer_buffer, transfer_buffer_size,
+                         cport_out_callback, hd);
+       retval = usb_submit_urb(urb, gfp_mask);
+       if (retval) {
+               pr_err("error %d submitting URB\n", retval);
+               free_urb(es1, urb);
+               return ERR_PTR(retval);
+       }
+
+       return conceal_urb(urb);
+}
+
+/*
+ * The cookie value supplied is the value that buffer_send()
+ * returned to its caller.  It identifies the buffer that should be
+ * canceled.  This function must also handle (which is to say,
+ * ignore) a null cookie value.
+ */
+static void buffer_cancel(void *cookie)
+{
+
+       /*
+        * We really should be defensive and track all outstanding
+        * (sent) buffers rather than trusting the cookie provided
+        * is valid.  For the time being, this will do.
+        */
+       if (cookie)
+               usb_kill_urb(reveal_urb(cookie));
+}
+
+static struct greybus_host_driver es1_driver = {
+       .hd_priv_size           = sizeof(struct es1_ap_dev),
+       .buffer_send            = buffer_send,
+       .buffer_cancel          = buffer_cancel,
+       .submit_svc             = submit_svc,
+};
+
+/* Common function to report consistent warnings based on URB status */
+static int check_urb_status(struct urb *urb)
+{
+       struct device *dev = &urb->dev->dev;
+       int status = urb->status;
+
+       switch (status) {
+       case 0:
+               return 0;
+
+       case -EOVERFLOW:
+               dev_err(dev, "%s: overflow actual length is %d\n",
+                       __func__, urb->actual_length);
+       case -ECONNRESET:
+       case -ENOENT:
+       case -ESHUTDOWN:
+       case -EILSEQ:
+       case -EPROTO:
+               /* device is gone, stop sending */
+               return status;
+       }
+       dev_err(dev, "%s: unknown status %d\n", __func__, status);
+
+       return -EAGAIN;
+}
+
+static void ap_disconnect(struct usb_interface *interface)
+{
+       struct es1_ap_dev *es1;
+       struct usb_device *udev;
+       int i;
+
+       es1 = usb_get_intfdata(interface);
+       if (!es1)
+               return;
+
+       /* Tear down everything! */
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               struct urb *urb = es1->cport_out_urb[i];
+
+               if (!urb)
+                       break;
+               usb_kill_urb(urb);
+               usb_free_urb(urb);
+               es1->cport_out_urb[i] = NULL;
+               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
+       }
+
+       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
+               struct urb *urb = es1->cport_in_urb[i];
+
+               if (!urb)
+                       break;
+               usb_kill_urb(urb);
+               usb_free_urb(urb);
+               kfree(es1->cport_in_buffer[i]);
+               es1->cport_in_buffer[i] = NULL;
+       }
+
+       usb_kill_urb(es1->svc_urb);
+       usb_free_urb(es1->svc_urb);
+       es1->svc_urb = NULL;
+       kfree(es1->svc_buffer);
+       es1->svc_buffer = NULL;
+
+       usb_set_intfdata(interface, NULL);
+       udev = es1->usb_dev;
+       greybus_remove_hd(es1->hd);
+
+       usb_put_dev(udev);
+}
+
+/* Callback for when we get a SVC message */
+static void svc_in_callback(struct urb *urb)
+{
+       struct greybus_host_device *hd = urb->context;
+       struct device *dev = &urb->dev->dev;
+       int status = check_urb_status(urb);
+       int retval;
+
+       if (status) {
+               if ((status == -EAGAIN) || (status == -EPROTO))
+                       goto exit;
+               dev_err(dev, "urb svc in error %d (dropped)\n", status);
+               return;
+       }
+
+       /* We have a message, create a new message structure, add it to the
+        * list, and wake up our thread that will process the messages.
+        */
+       greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
+
+exit:
+       /* resubmit the urb to get more messages */
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval)
+               dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
+}
+
+static void cport_in_callback(struct urb *urb)
+{
+       struct greybus_host_device *hd = urb->context;
+       struct device *dev = &urb->dev->dev;
+       int status = check_urb_status(urb);
+       int retval;
+       u16 cport_id;
+       u8 *data;
+
+       if (status) {
+               if ((status == -EAGAIN) || (status == -EPROTO))
+                       goto exit;
+               dev_err(dev, "urb cport in error %d (dropped)\n", status);
+               return;
+       }
+
+       /* The size has to be at least one, for the cport id */
+       if (!urb->actual_length) {
+               dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
+               goto exit;
+       }
+
+       /*
+        * Our CPort number is the first byte of the data stream,
+        * the rest of the stream is "real" data
+        */
+       data = urb->transfer_buffer;
+       cport_id = (u16)data[0];
+       data = &data[1];
+
+       /* Pass this data to the greybus core */
+       greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
+
+exit:
+       /* put our urb back in the request pool */
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval)
+               dev_err(dev, "%s: error %d in submitting urb.\n",
+                       __func__, retval);
+}
+
+static void cport_out_callback(struct urb *urb)
+{
+       struct greybus_host_device *hd = urb->context;
+       struct es1_ap_dev *es1 = hd_to_es1(hd);
+       int status = check_urb_status(urb);
+       u8 *data = urb->transfer_buffer + 1;
+
+       /*
+        * Tell the submitter that the buffer send (attempt) is
+        * complete, and report the status.  The submitter's buffer
+        * starts after the one-byte CPort id we inserted.
+        */
+       data = urb->transfer_buffer + 1;
+       greybus_data_sent(hd, data, status);
+
+       free_urb(es1, urb);
+       /*
+        * Rest assured Greg, this craziness is getting fixed.
+        *
+        * Yes, you are right, we aren't telling anyone that the urb finished.
+        * "That's crazy!  How does this all even work?" you might be saying.
+        * The "magic" is the idea that greybus works on the "operation" level,
+        * not the "send a buffer" level.  All operations are "round-trip" with
+        * a response from the device that the operation finished, or it will
+        * time out.  Because of that, we don't care that this urb finished, or
+        * failed, or did anything else, as higher levels of the protocol stack
+        * will handle completions and timeouts and the rest.
+        *
+        * This protocol is "needed" due to some hardware restrictions on the
+        * current generation of Unipro controllers.  Think about it for a
+        * minute, this is a USB driver, talking to a Unipro bridge, impedance
+        * mismatch is huge, yet the Unipro controller are even more
+        * underpowered than this little USB controller.  We rely on the round
+        * trip to keep stalls in the Unipro controllers from happening so that
+        * we can keep data flowing properly, no matter how slow it might be.
+        *
+        * Once again, a wonderful bus protocol cut down in its prime by a naive
+        * controller chip.  We dream of the day we have a "real" HCD for
+        * Unipro.  Until then, we suck it up and make the hardware work, as
+        * that's the job of the firmware and kernel.
+        * </rant>
+        */
+}
+
+/*
+ * The ES1 USB Bridge device contains 4 endpoints
+ * 1 Control - usual USB stuff + AP -> SVC messages
+ * 1 Interrupt IN - SVC -> AP messages
+ * 1 Bulk IN - CPort data in
+ * 1 Bulk OUT - CPort data out
+ */
+static int ap_probe(struct usb_interface *interface,
+                   const struct usb_device_id *id)
+{
+       struct es1_ap_dev *es1;
+       struct greybus_host_device *hd;
+       struct usb_device *udev;
+       struct usb_host_interface *iface_desc;
+       struct usb_endpoint_descriptor *endpoint;
+       bool int_in_found = false;
+       bool bulk_in_found = false;
+       bool bulk_out_found = false;
+       int retval = -ENOMEM;
+       int i;
+       u8 svc_interval = 0;
+
+       udev = usb_get_dev(interface_to_usbdev(interface));
+
+       hd = greybus_create_hd(&es1_driver, &udev->dev);
+       if (!hd) {
+               usb_put_dev(udev);
+               return -ENOMEM;
+       }
+
+       /* Fill in the buffer allocation constraints */
+       hd_buffer_constraints(hd);
+
+       es1 = hd_to_es1(hd);
+       es1->hd = hd;
+       es1->usb_intf = interface;
+       es1->usb_dev = udev;
+       spin_lock_init(&es1->cport_out_urb_lock);
+       usb_set_intfdata(interface, es1);
+
+       /* Control endpoint is the pipe to talk to this AP, so save it off */
+       endpoint = &udev->ep0.desc;
+       es1->control_endpoint = endpoint->bEndpointAddress;
+
+       /* find all 3 of our endpoints */
+       iface_desc = interface->cur_altsetting;
+       for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
+               endpoint = &iface_desc->endpoint[i].desc;
+
+               if (usb_endpoint_is_int_in(endpoint)) {
+                       es1->svc_endpoint = endpoint->bEndpointAddress;
+                       svc_interval = endpoint->bInterval;
+                       int_in_found = true;
+               } else if (usb_endpoint_is_bulk_in(endpoint)) {
+                       es1->cport_in_endpoint = endpoint->bEndpointAddress;
+                       bulk_in_found = true;
+               } else if (usb_endpoint_is_bulk_out(endpoint)) {
+                       es1->cport_out_endpoint = endpoint->bEndpointAddress;
+                       bulk_out_found = true;
+               } else {
+                       dev_err(&udev->dev,
+                               "Unknown endpoint type found, address %x\n",
+                               endpoint->bEndpointAddress);
+               }
+       }
+       if ((int_in_found == false) ||
+           (bulk_in_found == false) ||
+           (bulk_out_found == false)) {
+               dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
+               goto error;
+       }
+
+       /* Create our buffer and URB to get SVC messages, and start it up */
+       es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
+       if (!es1->svc_buffer)
+               goto error;
+
+       es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
+       if (!es1->svc_urb)
+               goto error;
+
+       usb_fill_int_urb(es1->svc_urb, udev,
+                        usb_rcvintpipe(udev, es1->svc_endpoint),
+                        es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
+                        hd, svc_interval);
+       retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
+       if (retval)
+               goto error;
+
+       /* Allocate buffers for our cport in messages and start them up */
+       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
+               struct urb *urb;
+               u8 *buffer;
+
+               urb = usb_alloc_urb(0, GFP_KERNEL);
+               if (!urb)
+                       goto error;
+               buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
+               if (!buffer)
+                       goto error;
+
+               usb_fill_bulk_urb(urb, udev,
+                                 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
+                                 buffer, ES1_GBUF_MSG_SIZE_MAX,
+                                 cport_in_callback, hd);
+               es1->cport_in_urb[i] = urb;
+               es1->cport_in_buffer[i] = buffer;
+               retval = usb_submit_urb(urb, GFP_KERNEL);
+               if (retval)
+                       goto error;
+       }
+
+       /* Allocate urbs for our CPort OUT messages */
+       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
+               struct urb *urb;
+
+               urb = usb_alloc_urb(0, GFP_KERNEL);
+               if (!urb)
+                       goto error;
+
+               es1->cport_out_urb[i] = urb;
+               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
+       }
+
+       return 0;
+error:
+       ap_disconnect(interface);
+
+       return retval;
+}
+
+static struct usb_driver es1_ap_driver = {
+       .name =         "es1_ap_driver",
+       .probe =        ap_probe,
+       .disconnect =   ap_disconnect,
+       .id_table =     id_table,
+};
+
+module_usb_driver(es1_ap_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
diff --git a/drivers/staging/greybus/gb-battery.c b/drivers/staging/greybus/gb-battery.c
deleted file mode 100644 (file)
index 2130d73..0000000
+++ /dev/null
@@ -1,373 +0,0 @@
-/*
- * Battery driver for a Greybus module.
- *
- * Copyright 2014 Google Inc.
- * Copyright 2014 Linaro Ltd.
- *
- * Released under the GPLv2 only.
- */
-
-#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/slab.h>
-#include <linux/power_supply.h>
-#include "greybus.h"
-
-struct gb_battery {
-       struct power_supply bat;
-       // FIXME
-       // we will want to keep the battery stats in here as we will be getting
-       // updates from the SVC "on the fly" so we don't have to always go ask
-       // the battery for some information.  Hopefully...
-       struct gb_connection *connection;
-       u8 version_major;
-       u8 version_minor;
-
-};
-#define to_gb_battery(x) container_of(x, struct gb_battery, bat)
-
-/* Version of the Greybus battery protocol we support */
-#define        GB_BATTERY_VERSION_MAJOR                0x00
-#define        GB_BATTERY_VERSION_MINOR                0x01
-
-/* Greybus battery request types */
-#define        GB_BATTERY_TYPE_INVALID                 0x00
-#define        GB_BATTERY_TYPE_PROTOCOL_VERSION        0x01
-#define        GB_BATTERY_TYPE_TECHNOLOGY              0x02
-#define        GB_BATTERY_TYPE_STATUS                  0x03
-#define        GB_BATTERY_TYPE_MAX_VOLTAGE             0x04
-#define        GB_BATTERY_TYPE_PERCENT_CAPACITY        0x05
-#define        GB_BATTERY_TYPE_TEMPERATURE             0x06
-#define        GB_BATTERY_TYPE_VOLTAGE                 0x07
-#define        GB_BATTERY_TYPE_CURRENT                 0x08
-#define GB_BATTERY_TYPE_CAPACITY               0x09    // TODO - POWER_SUPPLY_PROP_CURRENT_MAX
-#define GB_BATTERY_TYPE_SHUTDOWN_TEMP          0x0a    // TODO - POWER_SUPPLY_PROP_TEMP_ALERT_MAX
-
-struct gb_battery_proto_version_response {
-       __u8    major;
-       __u8    minor;
-};
-
-/* Should match up with battery types in linux/power_supply.h */
-#define GB_BATTERY_TECH_UNKNOWN                        0x0000
-#define GB_BATTERY_TECH_NiMH                   0x0001
-#define GB_BATTERY_TECH_LION                   0x0002
-#define GB_BATTERY_TECH_LIPO                   0x0003
-#define GB_BATTERY_TECH_LiFe                   0x0004
-#define GB_BATTERY_TECH_NiCd                   0x0005
-#define GB_BATTERY_TECH_LiMn                   0x0006
-
-struct gb_battery_technology_response {
-       __le32  technology;
-};
-
-/* Should match up with battery status in linux/power_supply.h */
-#define GB_BATTERY_STATUS_UNKNOWN              0x0000
-#define GB_BATTERY_STATUS_CHARGING             0x0001
-#define GB_BATTERY_STATUS_DISCHARGING          0x0002
-#define GB_BATTERY_STATUS_NOT_CHARGING         0x0003
-#define GB_BATTERY_STATUS_FULL                 0x0004
-
-struct gb_battery_status_response {
-       __le16  battery_status;
-};
-
-struct gb_battery_max_voltage_response {
-       __le32  max_voltage;
-};
-
-struct gb_battery_capacity_response {
-       __le32  capacity;
-};
-
-struct gb_battery_temperature_response {
-       __le32  temperature;
-};
-
-struct gb_battery_voltage_response {
-       __le32  voltage;
-};
-
-/*
- * This request only uses the connection field, and if successful,
- * fills in the major and minor protocol version of the target.
- */
-static int get_version(struct gb_battery *gb)
-{
-       struct gb_battery_proto_version_response version_response;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection,
-                                  GB_BATTERY_TYPE_PROTOCOL_VERSION,
-                                  NULL, 0,
-                                  &version_response, sizeof(version_response));
-       if (retval)
-               return retval;
-
-       if (version_response.major > GB_BATTERY_VERSION_MAJOR) {
-               pr_err("unsupported major version (%hhu > %hhu)\n",
-                       version_response.major, GB_BATTERY_VERSION_MAJOR);
-               return -ENOTSUPP;
-       }
-
-       gb->version_major = version_response.major;
-       gb->version_minor = version_response.minor;
-       return 0;
-}
-
-static int get_tech(struct gb_battery *gb)
-{
-       struct gb_battery_technology_response tech_response;
-       u32 technology;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_TECHNOLOGY,
-                                  NULL, 0,
-                                  &tech_response, sizeof(tech_response));
-       if (retval)
-               return retval;
-
-       /*
-        * Map greybus values to power_supply values.  Hopefully these are
-        * "identical" which should allow gcc to optimize the code away to
-        * nothing.
-        */
-       technology = le32_to_cpu(tech_response.technology);
-       switch (technology) {
-       case GB_BATTERY_TECH_NiMH:
-               technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
-               break;
-       case GB_BATTERY_TECH_LION:
-               technology = POWER_SUPPLY_TECHNOLOGY_LION;
-               break;
-       case GB_BATTERY_TECH_LIPO:
-               technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
-               break;
-       case GB_BATTERY_TECH_LiFe:
-               technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
-               break;
-       case GB_BATTERY_TECH_NiCd:
-               technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
-               break;
-       case GB_BATTERY_TECH_LiMn:
-               technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
-               break;
-       case GB_BATTERY_TECH_UNKNOWN:
-       default:
-               technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
-               break;
-       }
-       return technology;
-}
-
-static int get_status(struct gb_battery *gb)
-{
-       struct gb_battery_status_response status_response;
-       u16 battery_status;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_STATUS,
-                                  NULL, 0,
-                                  &status_response, sizeof(status_response));
-       if (retval)
-               return retval;
-
-       /*
-        * Map greybus values to power_supply values.  Hopefully these are
-        * "identical" which should allow gcc to optimize the code away to
-        * nothing.
-        */
-       battery_status = le16_to_cpu(status_response.battery_status);
-       switch (battery_status) {
-       case GB_BATTERY_STATUS_CHARGING:
-               battery_status = POWER_SUPPLY_STATUS_CHARGING;
-               break;
-       case GB_BATTERY_STATUS_DISCHARGING:
-               battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
-               break;
-       case GB_BATTERY_STATUS_NOT_CHARGING:
-               battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
-               break;
-       case GB_BATTERY_STATUS_FULL:
-               battery_status = POWER_SUPPLY_STATUS_FULL;
-               break;
-       case GB_BATTERY_STATUS_UNKNOWN:
-       default:
-               battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
-               break;
-       }
-       return battery_status;
-}
-
-static int get_max_voltage(struct gb_battery *gb)
-{
-       struct gb_battery_max_voltage_response volt_response;
-       u32 max_voltage;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_MAX_VOLTAGE,
-                                  NULL, 0,
-                                  &volt_response, sizeof(volt_response));
-       if (retval)
-               return retval;
-
-       max_voltage = le32_to_cpu(volt_response.max_voltage);
-       return max_voltage;
-}
-
-static int get_percent_capacity(struct gb_battery *gb)
-{
-       struct gb_battery_capacity_response capacity_response;
-       u32 capacity;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection,
-                                  GB_BATTERY_TYPE_PERCENT_CAPACITY,
-                                  NULL, 0, &capacity_response,
-                                  sizeof(capacity_response));
-       if (retval)
-               return retval;
-
-       capacity = le32_to_cpu(capacity_response.capacity);
-       return capacity;
-}
-
-static int get_temp(struct gb_battery *gb)
-{
-       struct gb_battery_temperature_response temp_response;
-       u32 temperature;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_TEMPERATURE,
-                                  NULL, 0,
-                                  &temp_response, sizeof(temp_response));
-       if (retval)
-               return retval;
-
-       temperature = le32_to_cpu(temp_response.temperature);
-       return temperature;
-}
-
-static int get_voltage(struct gb_battery *gb)
-{
-       struct gb_battery_voltage_response voltage_response;
-       u32 voltage;
-       int retval;
-
-       retval = gb_operation_sync(gb->connection, GB_BATTERY_TYPE_VOLTAGE,
-                                  NULL, 0,
-                                  &voltage_response, sizeof(voltage_response));
-       if (retval)
-               return retval;
-
-       voltage = le32_to_cpu(voltage_response.voltage);
-       return voltage;
-}
-
-static int get_property(struct power_supply *b,
-                       enum power_supply_property psp,
-                       union power_supply_propval *val)
-{
-       struct gb_battery *gb = to_gb_battery(b);
-
-       switch (psp) {
-       case POWER_SUPPLY_PROP_TECHNOLOGY:
-               val->intval = get_tech(gb);
-               break;
-
-       case POWER_SUPPLY_PROP_STATUS:
-               val->intval = get_status(gb);
-               break;
-
-       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
-               val->intval = get_max_voltage(gb);
-               break;
-
-       case POWER_SUPPLY_PROP_CAPACITY:
-               val->intval = get_percent_capacity(gb);
-               break;
-
-       case POWER_SUPPLY_PROP_TEMP:
-               val->intval = get_temp(gb);
-               break;
-
-       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
-               val->intval = get_voltage(gb);
-               break;
-
-       default:
-               return -EINVAL;
-       }
-
-       return 0;
-}
-
-// FIXME - verify this list, odds are some can be removed and others added.
-static enum power_supply_property battery_props[] = {
-       POWER_SUPPLY_PROP_TECHNOLOGY,
-       POWER_SUPPLY_PROP_STATUS,
-       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
-       POWER_SUPPLY_PROP_CAPACITY,
-       POWER_SUPPLY_PROP_TEMP,
-       POWER_SUPPLY_PROP_VOLTAGE_NOW,
-};
-
-static int gb_battery_connection_init(struct gb_connection *connection)
-{
-       struct gb_battery *gb;
-       struct power_supply *b;
-       int retval;
-
-       gb = kzalloc(sizeof(*gb), GFP_KERNEL);
-       if (!gb)
-               return -ENOMEM;
-
-       gb->connection = connection;
-       connection->private = gb;
-
-       /* Check the version */
-       retval = get_version(gb);
-       if (retval) {
-               kfree(gb);
-               return retval;
-       }
-
-       b = &gb->bat;
-       // FIXME - get a better (i.e. unique) name
-       // FIXME - anything else needs to be set?
-       b->name                 = "gb_battery";
-       b->type                 = POWER_SUPPLY_TYPE_BATTERY,
-       b->properties           = battery_props,
-       b->num_properties       = ARRAY_SIZE(battery_props),
-       b->get_property         = get_property,
-
-       retval = power_supply_register(&connection->bundle->intf->dev, b);
-       if (retval) {
-               kfree(gb);
-               return retval;
-       }
-
-       return 0;
-}
-
-static void gb_battery_connection_exit(struct gb_connection *connection)
-{
-       struct gb_battery *gb = connection->private;
-
-       power_supply_unregister(&gb->bat);
-       kfree(gb);
-}
-
-static struct gb_protocol battery_protocol = {
-       .name                   = "battery",
-       .id                     = GREYBUS_PROTOCOL_BATTERY,
-       .major                  = 0,
-       .minor                  = 1,
-       .connection_init        = gb_battery_connection_init,
-       .connection_exit        = gb_battery_connection_exit,
-       .request_recv           = NULL, /* no incoming requests */
-};
-
-gb_protocol_driver(&battery_protocol);
-
-MODULE_LICENSE("GPL v2");
diff --git a/drivers/staging/greybus/gb-es1.c b/drivers/staging/greybus/gb-es1.c
deleted file mode 100644 (file)
index 2ec5d7b..0000000
+++ /dev/null
@@ -1,618 +0,0 @@
-/*
- * Greybus "AP" USB driver
- *
- * Copyright 2014 Google Inc.
- * Copyright 2014 Linaro Ltd.
- *
- * Released under the GPLv2 only.
- */
-#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/slab.h>
-#include <linux/errno.h>
-#include <linux/sizes.h>
-#include <linux/usb.h>
-
-#include "greybus.h"
-#include "svc_msg.h"
-#include "kernel_ver.h"
-
-/*
- * Macros for making pointers explicitly opaque, such that the result
- * isn't valid but also can't be mistaken for an ERR_PTR() value.
- */
-#define conceal_urb(urb)       ((void *)((uintptr_t)(urb) ^ 0xbad))
-#define reveal_urb(cookie)     ((void *)((uintptr_t)(cookie) ^ 0xbad))
-
-/* Memory sizes for the buffers sent to/from the ES1 controller */
-#define ES1_SVC_MSG_SIZE       (sizeof(struct svc_msg) + SZ_64K)
-#define ES1_GBUF_MSG_SIZE_MAX  PAGE_SIZE
-
-static const struct usb_device_id id_table[] = {
-       /* Made up numbers for the SVC USB Bridge in ES1 */
-       { USB_DEVICE(0xffff, 0x0001) },
-       { },
-};
-MODULE_DEVICE_TABLE(usb, id_table);
-
-/*
- * Number of CPort IN urbs in flight at any point in time.
- * Adjust if we are having stalls in the USB buffer due to not enough urbs in
- * flight.
- */
-#define NUM_CPORT_IN_URB       4
-
-/* Number of CPort OUT urbs in flight at any point in time.
- * Adjust if we get messages saying we are out of urbs in the system log.
- */
-#define NUM_CPORT_OUT_URB      8
-
-/**
- * es1_ap_dev - ES1 USB Bridge to AP structure
- * @usb_dev: pointer to the USB device we are.
- * @usb_intf: pointer to the USB interface we are bound to.
- * @hd: pointer to our greybus_host_device structure
- * @control_endpoint: endpoint to send data to SVC
- * @svc_endpoint: endpoint for SVC data in
- * @cport_in_endpoint: bulk in endpoint for CPort data
- * @cport-out_endpoint: bulk out endpoint for CPort data
- * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
- * @svc_urb: urb for SVC messages coming in on @svc_endpoint
- * @cport_in_urb: array of urbs for the CPort in messages
- * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
- * @cport_out_urb: array of urbs for the CPort out messages
- * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
- *                     not.
- * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
- */
-struct es1_ap_dev {
-       struct usb_device *usb_dev;
-       struct usb_interface *usb_intf;
-       struct greybus_host_device *hd;
-
-       __u8 control_endpoint;
-       __u8 svc_endpoint;
-       __u8 cport_in_endpoint;
-       __u8 cport_out_endpoint;
-
-       u8 *svc_buffer;
-       struct urb *svc_urb;
-
-       struct urb *cport_in_urb[NUM_CPORT_IN_URB];
-       u8 *cport_in_buffer[NUM_CPORT_IN_URB];
-       struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
-       bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
-       spinlock_t cport_out_urb_lock;
-};
-
-static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
-{
-       return (struct es1_ap_dev *)&hd->hd_priv;
-}
-
-static void cport_out_callback(struct urb *urb);
-
-/*
- * Buffer constraints for the host driver.
- *
- * A "buffer" is used to hold data to be transferred for Greybus by
- * the host driver.  A buffer is represented by a "buffer pointer",
- * which defines a region of memory used by the host driver for
- * transferring the data.  When Greybus allocates a buffer, it must
- * do so subject to the constraints associated with the host driver.
- * These constraints are specified by two parameters: the
- * headroom; and the maximum buffer size.
- *
- *                     +------------------+
- *                     |    Host driver   | \
- *                     |   reserved area  |  }- headroom
- *                     |      . . .       | /
- *  buffer pointer ---> +------------------+
- *                     | Buffer space for | \
- *                     | transferred data |  }- buffer size
- *                     |      . . .       | /   (limited to size_max)
- *                     +------------------+
- *
- *  headroom:  Every buffer must have at least this much space
- *             *before* the buffer pointer, reserved for use by the
- *             host driver.  I.e., ((char *)buffer - headroom) must
- *             point to valid memory, usable only by the host driver.
- *  size_max:  The maximum size of a buffer (not including the
- *             headroom) must not exceed this.
- */
-static void hd_buffer_constraints(struct greybus_host_device *hd)
-{
-       /*
-        * Only one byte is required, but this produces a result
-        * that's better aligned for the user.
-        */
-       hd->buffer_headroom = sizeof(u32);      /* For cport id */
-       hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
-       BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
-}
-
-#define ES1_TIMEOUT    500     /* 500 ms for the SVC to do something */
-static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
-{
-       struct es1_ap_dev *es1 = hd_to_es1(hd);
-       int retval;
-
-       /* SVC messages go down our control pipe */
-       retval = usb_control_msg(es1->usb_dev,
-                                usb_sndctrlpipe(es1->usb_dev,
-                                                es1->control_endpoint),
-                                0x01,  /* vendor request AP message */
-                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
-                                0x00, 0x00,
-                                (char *)svc_msg,
-                                sizeof(*svc_msg),
-                                ES1_TIMEOUT);
-       if (retval != sizeof(*svc_msg))
-               return retval;
-
-       return 0;
-}
-
-static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
-{
-       struct urb *urb = NULL;
-       unsigned long flags;
-       int i;
-
-       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
-
-       /* Look in our pool of allocated urbs first, as that's the "fastest" */
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               if (es1->cport_out_urb_busy[i] == false) {
-                       es1->cport_out_urb_busy[i] = true;
-                       urb = es1->cport_out_urb[i];
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
-       if (urb)
-               return urb;
-
-       /*
-        * Crap, pool is empty, complain to the syslog and go allocate one
-        * dynamically as we have to succeed.
-        */
-       dev_err(&es1->usb_dev->dev,
-               "No free CPort OUT urbs, having to dynamically allocate one!\n");
-       return usb_alloc_urb(0, gfp_mask);
-}
-
-static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
-{
-       unsigned long flags;
-       int i;
-       /*
-        * See if this was an urb in our pool, if so mark it "free", otherwise
-        * we need to free it ourselves.
-        */
-       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               if (urb == es1->cport_out_urb[i]) {
-                       es1->cport_out_urb_busy[i] = false;
-                       urb = NULL;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
-
-       /* If urb is not NULL, then we need to free this urb */
-       usb_free_urb(urb);
-}
-
-/*
- * Returns an opaque cookie value if successful, or a pointer coded
- * error otherwise.  If the caller wishes to cancel the in-flight
- * buffer, it must supply the returned cookie to the cancel routine.
- */
-static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
-                       void *buffer, size_t buffer_size, gfp_t gfp_mask)
-{
-       struct es1_ap_dev *es1 = hd_to_es1(hd);
-       struct usb_device *udev = es1->usb_dev;
-       u8 *transfer_buffer = buffer;
-       int transfer_buffer_size;
-       int retval;
-       struct urb *urb;
-
-       if (!buffer) {
-               pr_err("null buffer supplied to send\n");
-               return ERR_PTR(-EINVAL);
-       }
-       if (buffer_size > (size_t)INT_MAX) {
-               pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
-               return ERR_PTR(-EINVAL);
-       }
-       transfer_buffer--;
-       transfer_buffer_size = buffer_size + 1;
-
-       /*
-        * The data actually transferred will include an indication
-        * of where the data should be sent.  Do one last check of
-        * the target CPort id before filling it in.
-        */
-       if (cport_id == CPORT_ID_BAD) {
-               pr_err("request to send inbound data buffer\n");
-               return ERR_PTR(-EINVAL);
-       }
-       if (cport_id > (u16)U8_MAX) {
-               pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
-               return ERR_PTR(-EINVAL);
-       }
-       /* OK, the destination is fine; record it in the transfer buffer */
-       *transfer_buffer = cport_id;
-
-       /* Find a free urb */
-       urb = next_free_urb(es1, gfp_mask);
-       if (!urb)
-               return ERR_PTR(-ENOMEM);
-
-       usb_fill_bulk_urb(urb, udev,
-                         usb_sndbulkpipe(udev, es1->cport_out_endpoint),
-                         transfer_buffer, transfer_buffer_size,
-                         cport_out_callback, hd);
-       retval = usb_submit_urb(urb, gfp_mask);
-       if (retval) {
-               pr_err("error %d submitting URB\n", retval);
-               free_urb(es1, urb);
-               return ERR_PTR(retval);
-       }
-
-       return conceal_urb(urb);
-}
-
-/*
- * The cookie value supplied is the value that buffer_send()
- * returned to its caller.  It identifies the buffer that should be
- * canceled.  This function must also handle (which is to say,
- * ignore) a null cookie value.
- */
-static void buffer_cancel(void *cookie)
-{
-
-       /*
-        * We really should be defensive and track all outstanding
-        * (sent) buffers rather than trusting the cookie provided
-        * is valid.  For the time being, this will do.
-        */
-       if (cookie)
-               usb_kill_urb(reveal_urb(cookie));
-}
-
-static struct greybus_host_driver es1_driver = {
-       .hd_priv_size           = sizeof(struct es1_ap_dev),
-       .buffer_send            = buffer_send,
-       .buffer_cancel          = buffer_cancel,
-       .submit_svc             = submit_svc,
-};
-
-/* Common function to report consistent warnings based on URB status */
-static int check_urb_status(struct urb *urb)
-{
-       struct device *dev = &urb->dev->dev;
-       int status = urb->status;
-
-       switch (status) {
-       case 0:
-               return 0;
-
-       case -EOVERFLOW:
-               dev_err(dev, "%s: overflow actual length is %d\n",
-                       __func__, urb->actual_length);
-       case -ECONNRESET:
-       case -ENOENT:
-       case -ESHUTDOWN:
-       case -EILSEQ:
-       case -EPROTO:
-               /* device is gone, stop sending */
-               return status;
-       }
-       dev_err(dev, "%s: unknown status %d\n", __func__, status);
-
-       return -EAGAIN;
-}
-
-static void ap_disconnect(struct usb_interface *interface)
-{
-       struct es1_ap_dev *es1;
-       struct usb_device *udev;
-       int i;
-
-       es1 = usb_get_intfdata(interface);
-       if (!es1)
-               return;
-
-       /* Tear down everything! */
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               struct urb *urb = es1->cport_out_urb[i];
-
-               if (!urb)
-                       break;
-               usb_kill_urb(urb);
-               usb_free_urb(urb);
-               es1->cport_out_urb[i] = NULL;
-               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
-       }
-
-       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
-               struct urb *urb = es1->cport_in_urb[i];
-
-               if (!urb)
-                       break;
-               usb_kill_urb(urb);
-               usb_free_urb(urb);
-               kfree(es1->cport_in_buffer[i]);
-               es1->cport_in_buffer[i] = NULL;
-       }
-
-       usb_kill_urb(es1->svc_urb);
-       usb_free_urb(es1->svc_urb);
-       es1->svc_urb = NULL;
-       kfree(es1->svc_buffer);
-       es1->svc_buffer = NULL;
-
-       usb_set_intfdata(interface, NULL);
-       udev = es1->usb_dev;
-       greybus_remove_hd(es1->hd);
-
-       usb_put_dev(udev);
-}
-
-/* Callback for when we get a SVC message */
-static void svc_in_callback(struct urb *urb)
-{
-       struct greybus_host_device *hd = urb->context;
-       struct device *dev = &urb->dev->dev;
-       int status = check_urb_status(urb);
-       int retval;
-
-       if (status) {
-               if ((status == -EAGAIN) || (status == -EPROTO))
-                       goto exit;
-               dev_err(dev, "urb svc in error %d (dropped)\n", status);
-               return;
-       }
-
-       /* We have a message, create a new message structure, add it to the
-        * list, and wake up our thread that will process the messages.
-        */
-       greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
-
-exit:
-       /* resubmit the urb to get more messages */
-       retval = usb_submit_urb(urb, GFP_ATOMIC);
-       if (retval)
-               dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
-}
-
-static void cport_in_callback(struct urb *urb)
-{
-       struct greybus_host_device *hd = urb->context;
-       struct device *dev = &urb->dev->dev;
-       int status = check_urb_status(urb);
-       int retval;
-       u16 cport_id;
-       u8 *data;
-
-       if (status) {
-               if ((status == -EAGAIN) || (status == -EPROTO))
-                       goto exit;
-               dev_err(dev, "urb cport in error %d (dropped)\n", status);
-               return;
-       }
-
-       /* The size has to be at least one, for the cport id */
-       if (!urb->actual_length) {
-               dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
-               goto exit;
-       }
-
-       /*
-        * Our CPort number is the first byte of the data stream,
-        * the rest of the stream is "real" data
-        */
-       data = urb->transfer_buffer;
-       cport_id = (u16)data[0];
-       data = &data[1];
-
-       /* Pass this data to the greybus core */
-       greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
-
-exit:
-       /* put our urb back in the request pool */
-       retval = usb_submit_urb(urb, GFP_ATOMIC);
-       if (retval)
-               dev_err(dev, "%s: error %d in submitting urb.\n",
-                       __func__, retval);
-}
-
-static void cport_out_callback(struct urb *urb)
-{
-       struct greybus_host_device *hd = urb->context;
-       struct es1_ap_dev *es1 = hd_to_es1(hd);
-       int status = check_urb_status(urb);
-       u8 *data = urb->transfer_buffer + 1;
-
-       /*
-        * Tell the submitter that the buffer send (attempt) is
-        * complete, and report the status.  The submitter's buffer
-        * starts after the one-byte CPort id we inserted.
-        */
-       data = urb->transfer_buffer + 1;
-       greybus_data_sent(hd, data, status);
-
-       free_urb(es1, urb);
-       /*
-        * Rest assured Greg, this craziness is getting fixed.
-        *
-        * Yes, you are right, we aren't telling anyone that the urb finished.
-        * "That's crazy!  How does this all even work?" you might be saying.
-        * The "magic" is the idea that greybus works on the "operation" level,
-        * not the "send a buffer" level.  All operations are "round-trip" with
-        * a response from the device that the operation finished, or it will
-        * time out.  Because of that, we don't care that this urb finished, or
-        * failed, or did anything else, as higher levels of the protocol stack
-        * will handle completions and timeouts and the rest.
-        *
-        * This protocol is "needed" due to some hardware restrictions on the
-        * current generation of Unipro controllers.  Think about it for a
-        * minute, this is a USB driver, talking to a Unipro bridge, impedance
-        * mismatch is huge, yet the Unipro controller are even more
-        * underpowered than this little USB controller.  We rely on the round
-        * trip to keep stalls in the Unipro controllers from happening so that
-        * we can keep data flowing properly, no matter how slow it might be.
-        *
-        * Once again, a wonderful bus protocol cut down in its prime by a naive
-        * controller chip.  We dream of the day we have a "real" HCD for
-        * Unipro.  Until then, we suck it up and make the hardware work, as
-        * that's the job of the firmware and kernel.
-        * </rant>
-        */
-}
-
-/*
- * The ES1 USB Bridge device contains 4 endpoints
- * 1 Control - usual USB stuff + AP -> SVC messages
- * 1 Interrupt IN - SVC -> AP messages
- * 1 Bulk IN - CPort data in
- * 1 Bulk OUT - CPort data out
- */
-static int ap_probe(struct usb_interface *interface,
-                   const struct usb_device_id *id)
-{
-       struct es1_ap_dev *es1;
-       struct greybus_host_device *hd;
-       struct usb_device *udev;
-       struct usb_host_interface *iface_desc;
-       struct usb_endpoint_descriptor *endpoint;
-       bool int_in_found = false;
-       bool bulk_in_found = false;
-       bool bulk_out_found = false;
-       int retval = -ENOMEM;
-       int i;
-       u8 svc_interval = 0;
-
-       udev = usb_get_dev(interface_to_usbdev(interface));
-
-       hd = greybus_create_hd(&es1_driver, &udev->dev);
-       if (!hd) {
-               usb_put_dev(udev);
-               return -ENOMEM;
-       }
-
-       /* Fill in the buffer allocation constraints */
-       hd_buffer_constraints(hd);
-
-       es1 = hd_to_es1(hd);
-       es1->hd = hd;
-       es1->usb_intf = interface;
-       es1->usb_dev = udev;
-       spin_lock_init(&es1->cport_out_urb_lock);
-       usb_set_intfdata(interface, es1);
-
-       /* Control endpoint is the pipe to talk to this AP, so save it off */
-       endpoint = &udev->ep0.desc;
-       es1->control_endpoint = endpoint->bEndpointAddress;
-
-       /* find all 3 of our endpoints */
-       iface_desc = interface->cur_altsetting;
-       for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
-               endpoint = &iface_desc->endpoint[i].desc;
-
-               if (usb_endpoint_is_int_in(endpoint)) {
-                       es1->svc_endpoint = endpoint->bEndpointAddress;
-                       svc_interval = endpoint->bInterval;
-                       int_in_found = true;
-               } else if (usb_endpoint_is_bulk_in(endpoint)) {
-                       es1->cport_in_endpoint = endpoint->bEndpointAddress;
-                       bulk_in_found = true;
-               } else if (usb_endpoint_is_bulk_out(endpoint)) {
-                       es1->cport_out_endpoint = endpoint->bEndpointAddress;
-                       bulk_out_found = true;
-               } else {
-                       dev_err(&udev->dev,
-                               "Unknown endpoint type found, address %x\n",
-                               endpoint->bEndpointAddress);
-               }
-       }
-       if ((int_in_found == false) ||
-           (bulk_in_found == false) ||
-           (bulk_out_found == false)) {
-               dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
-               goto error;
-       }
-
-       /* Create our buffer and URB to get SVC messages, and start it up */
-       es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
-       if (!es1->svc_buffer)
-               goto error;
-
-       es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
-       if (!es1->svc_urb)
-               goto error;
-
-       usb_fill_int_urb(es1->svc_urb, udev,
-                        usb_rcvintpipe(udev, es1->svc_endpoint),
-                        es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
-                        hd, svc_interval);
-       retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
-       if (retval)
-               goto error;
-
-       /* Allocate buffers for our cport in messages and start them up */
-       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
-               struct urb *urb;
-               u8 *buffer;
-
-               urb = usb_alloc_urb(0, GFP_KERNEL);
-               if (!urb)
-                       goto error;
-               buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
-               if (!buffer)
-                       goto error;
-
-               usb_fill_bulk_urb(urb, udev,
-                                 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
-                                 buffer, ES1_GBUF_MSG_SIZE_MAX,
-                                 cport_in_callback, hd);
-               es1->cport_in_urb[i] = urb;
-               es1->cport_in_buffer[i] = buffer;
-               retval = usb_submit_urb(urb, GFP_KERNEL);
-               if (retval)
-                       goto error;
-       }
-
-       /* Allocate urbs for our CPort OUT messages */
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               struct urb *urb;
-
-               urb = usb_alloc_urb(0, GFP_KERNEL);
-               if (!urb)
-                       goto error;
-
-               es1->cport_out_urb[i] = urb;
-               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
-       }
-
-       return 0;
-error:
-       ap_disconnect(interface);
-
-       return retval;
-}
-
-static struct usb_driver es1_ap_driver = {
-       .name =         "es1_ap_driver",
-       .probe =        ap_probe,
-       .disconnect =   ap_disconnect,
-       .id_table =     id_table,
-};
-
-module_usb_driver(es1_ap_driver);
-
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
diff --git a/drivers/staging/greybus/gb-es2.c b/drivers/staging/greybus/gb-es2.c
deleted file mode 100644 (file)
index 4154cce..0000000
+++ /dev/null
@@ -1,618 +0,0 @@
-/*
- * Greybus "AP" USB driver for "ES2" controller chips
- *
- * Copyright 2014 Google Inc.
- * Copyright 2014 Linaro Ltd.
- *
- * Released under the GPLv2 only.
- */
-#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/slab.h>
-#include <linux/errno.h>
-#include <linux/sizes.h>
-#include <linux/usb.h>
-
-#include "greybus.h"
-#include "svc_msg.h"
-#include "kernel_ver.h"
-
-/*
- * Macros for making pointers explicitly opaque, such that the result
- * isn't valid but also can't be mistaken for an ERR_PTR() value.
- */
-#define conceal_urb(urb)       ((void *)((uintptr_t)(urb) ^ 0xbad))
-#define reveal_urb(cookie)     ((void *)((uintptr_t)(cookie) ^ 0xbad))
-
-/* Memory sizes for the buffers sent to/from the ES1 controller */
-#define ES1_SVC_MSG_SIZE       (sizeof(struct svc_msg) + SZ_64K)
-#define ES1_GBUF_MSG_SIZE_MAX  PAGE_SIZE
-
-static const struct usb_device_id id_table[] = {
-       /* Made up numbers for the SVC USB Bridge in ES1 */
-       { USB_DEVICE(0xffff, 0x0001) },
-       { },
-};
-MODULE_DEVICE_TABLE(usb, id_table);
-
-/*
- * Number of CPort IN urbs in flight at any point in time.
- * Adjust if we are having stalls in the USB buffer due to not enough urbs in
- * flight.
- */
-#define NUM_CPORT_IN_URB       4
-
-/* Number of CPort OUT urbs in flight at any point in time.
- * Adjust if we get messages saying we are out of urbs in the system log.
- */
-#define NUM_CPORT_OUT_URB      8
-
-/**
- * es1_ap_dev - ES1 USB Bridge to AP structure
- * @usb_dev: pointer to the USB device we are.
- * @usb_intf: pointer to the USB interface we are bound to.
- * @hd: pointer to our greybus_host_device structure
- * @control_endpoint: endpoint to send data to SVC
- * @svc_endpoint: endpoint for SVC data in
- * @cport_in_endpoint: bulk in endpoint for CPort data
- * @cport-out_endpoint: bulk out endpoint for CPort data
- * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
- * @svc_urb: urb for SVC messages coming in on @svc_endpoint
- * @cport_in_urb: array of urbs for the CPort in messages
- * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
- * @cport_out_urb: array of urbs for the CPort out messages
- * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
- *                     not.
- * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
- */
-struct es1_ap_dev {
-       struct usb_device *usb_dev;
-       struct usb_interface *usb_intf;
-       struct greybus_host_device *hd;
-
-       __u8 control_endpoint;
-       __u8 svc_endpoint;
-       __u8 cport_in_endpoint;
-       __u8 cport_out_endpoint;
-
-       u8 *svc_buffer;
-       struct urb *svc_urb;
-
-       struct urb *cport_in_urb[NUM_CPORT_IN_URB];
-       u8 *cport_in_buffer[NUM_CPORT_IN_URB];
-       struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
-       bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
-       spinlock_t cport_out_urb_lock;
-};
-
-static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
-{
-       return (struct es1_ap_dev *)&hd->hd_priv;
-}
-
-static void cport_out_callback(struct urb *urb);
-
-/*
- * Buffer constraints for the host driver.
- *
- * A "buffer" is used to hold data to be transferred for Greybus by
- * the host driver.  A buffer is represented by a "buffer pointer",
- * which defines a region of memory used by the host driver for
- * transferring the data.  When Greybus allocates a buffer, it must
- * do so subject to the constraints associated with the host driver.
- * These constraints are specified by two parameters: the
- * headroom; and the maximum buffer size.
- *
- *                     +------------------+
- *                     |    Host driver   | \
- *                     |   reserved area  |  }- headroom
- *                     |      . . .       | /
- *  buffer pointer ---> +------------------+
- *                     | Buffer space for | \
- *                     | transferred data |  }- buffer size
- *                     |      . . .       | /   (limited to size_max)
- *                     +------------------+
- *
- *  headroom:  Every buffer must have at least this much space
- *             *before* the buffer pointer, reserved for use by the
- *             host driver.  I.e., ((char *)buffer - headroom) must
- *             point to valid memory, usable only by the host driver.
- *  size_max:  The maximum size of a buffer (not including the
- *             headroom) must not exceed this.
- */
-static void hd_buffer_constraints(struct greybus_host_device *hd)
-{
-       /*
-        * Only one byte is required, but this produces a result
-        * that's better aligned for the user.
-        */
-       hd->buffer_headroom = sizeof(u32);      /* For cport id */
-       hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
-       BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
-}
-
-#define ES1_TIMEOUT    500     /* 500 ms for the SVC to do something */
-static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
-{
-       struct es1_ap_dev *es1 = hd_to_es1(hd);
-       int retval;
-
-       /* SVC messages go down our control pipe */
-       retval = usb_control_msg(es1->usb_dev,
-                                usb_sndctrlpipe(es1->usb_dev,
-                                                es1->control_endpoint),
-                                0x01,  /* vendor request AP message */
-                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
-                                0x00, 0x00,
-                                (char *)svc_msg,
-                                sizeof(*svc_msg),
-                                ES1_TIMEOUT);
-       if (retval != sizeof(*svc_msg))
-               return retval;
-
-       return 0;
-}
-
-static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
-{
-       struct urb *urb = NULL;
-       unsigned long flags;
-       int i;
-
-       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
-
-       /* Look in our pool of allocated urbs first, as that's the "fastest" */
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               if (es1->cport_out_urb_busy[i] == false) {
-                       es1->cport_out_urb_busy[i] = true;
-                       urb = es1->cport_out_urb[i];
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
-       if (urb)
-               return urb;
-
-       /*
-        * Crap, pool is empty, complain to the syslog and go allocate one
-        * dynamically as we have to succeed.
-        */
-       dev_err(&es1->usb_dev->dev,
-               "No free CPort OUT urbs, having to dynamically allocate one!\n");
-       return usb_alloc_urb(0, gfp_mask);
-}
-
-static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
-{
-       unsigned long flags;
-       int i;
-       /*
-        * See if this was an urb in our pool, if so mark it "free", otherwise
-        * we need to free it ourselves.
-        */
-       spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               if (urb == es1->cport_out_urb[i]) {
-                       es1->cport_out_urb_busy[i] = false;
-                       urb = NULL;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
-
-       /* If urb is not NULL, then we need to free this urb */
-       usb_free_urb(urb);
-}
-
-/*
- * Returns an opaque cookie value if successful, or a pointer coded
- * error otherwise.  If the caller wishes to cancel the in-flight
- * buffer, it must supply the returned cookie to the cancel routine.
- */
-static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
-                       void *buffer, size_t buffer_size, gfp_t gfp_mask)
-{
-       struct es1_ap_dev *es1 = hd_to_es1(hd);
-       struct usb_device *udev = es1->usb_dev;
-       u8 *transfer_buffer = buffer;
-       int transfer_buffer_size;
-       int retval;
-       struct urb *urb;
-
-       if (!buffer) {
-               pr_err("null buffer supplied to send\n");
-               return ERR_PTR(-EINVAL);
-       }
-       if (buffer_size > (size_t)INT_MAX) {
-               pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
-               return ERR_PTR(-EINVAL);
-       }
-       transfer_buffer--;
-       transfer_buffer_size = buffer_size + 1;
-
-       /*
-        * The data actually transferred will include an indication
-        * of where the data should be sent.  Do one last check of
-        * the target CPort id before filling it in.
-        */
-       if (cport_id == CPORT_ID_BAD) {
-               pr_err("request to send inbound data buffer\n");
-               return ERR_PTR(-EINVAL);
-       }
-       if (cport_id > (u16)U8_MAX) {
-               pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
-               return ERR_PTR(-EINVAL);
-       }
-       /* OK, the destination is fine; record it in the transfer buffer */
-       *transfer_buffer = cport_id;
-
-       /* Find a free urb */
-       urb = next_free_urb(es1, gfp_mask);
-       if (!urb)
-               return ERR_PTR(-ENOMEM);
-
-       usb_fill_bulk_urb(urb, udev,
-                         usb_sndbulkpipe(udev, es1->cport_out_endpoint),
-                         transfer_buffer, transfer_buffer_size,
-                         cport_out_callback, hd);
-       retval = usb_submit_urb(urb, gfp_mask);
-       if (retval) {
-               pr_err("error %d submitting URB\n", retval);
-               free_urb(es1, urb);
-               return ERR_PTR(retval);
-       }
-
-       return conceal_urb(urb);
-}
-
-/*
- * The cookie value supplied is the value that buffer_send()
- * returned to its caller.  It identifies the buffer that should be
- * canceled.  This function must also handle (which is to say,
- * ignore) a null cookie value.
- */
-static void buffer_cancel(void *cookie)
-{
-
-       /*
-        * We really should be defensive and track all outstanding
-        * (sent) buffers rather than trusting the cookie provided
-        * is valid.  For the time being, this will do.
-        */
-       if (cookie)
-               usb_kill_urb(reveal_urb(cookie));
-}
-
-static struct greybus_host_driver es1_driver = {
-       .hd_priv_size           = sizeof(struct es1_ap_dev),
-       .buffer_send            = buffer_send,
-       .buffer_cancel          = buffer_cancel,
-       .submit_svc             = submit_svc,
-};
-
-/* Common function to report consistent warnings based on URB status */
-static int check_urb_status(struct urb *urb)
-{
-       struct device *dev = &urb->dev->dev;
-       int status = urb->status;
-
-       switch (status) {
-       case 0:
-               return 0;
-
-       case -EOVERFLOW:
-               dev_err(dev, "%s: overflow actual length is %d\n",
-                       __func__, urb->actual_length);
-       case -ECONNRESET:
-       case -ENOENT:
-       case -ESHUTDOWN:
-       case -EILSEQ:
-       case -EPROTO:
-               /* device is gone, stop sending */
-               return status;
-       }
-       dev_err(dev, "%s: unknown status %d\n", __func__, status);
-
-       return -EAGAIN;
-}
-
-static void ap_disconnect(struct usb_interface *interface)
-{
-       struct es1_ap_dev *es1;
-       struct usb_device *udev;
-       int i;
-
-       es1 = usb_get_intfdata(interface);
-       if (!es1)
-               return;
-
-       /* Tear down everything! */
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               struct urb *urb = es1->cport_out_urb[i];
-
-               if (!urb)
-                       break;
-               usb_kill_urb(urb);
-               usb_free_urb(urb);
-               es1->cport_out_urb[i] = NULL;
-               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
-       }
-
-       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
-               struct urb *urb = es1->cport_in_urb[i];
-
-               if (!urb)
-                       break;
-               usb_kill_urb(urb);
-               usb_free_urb(urb);
-               kfree(es1->cport_in_buffer[i]);
-               es1->cport_in_buffer[i] = NULL;
-       }
-
-       usb_kill_urb(es1->svc_urb);
-       usb_free_urb(es1->svc_urb);
-       es1->svc_urb = NULL;
-       kfree(es1->svc_buffer);
-       es1->svc_buffer = NULL;
-
-       usb_set_intfdata(interface, NULL);
-       udev = es1->usb_dev;
-       greybus_remove_hd(es1->hd);
-
-       usb_put_dev(udev);
-}
-
-/* Callback for when we get a SVC message */
-static void svc_in_callback(struct urb *urb)
-{
-       struct greybus_host_device *hd = urb->context;
-       struct device *dev = &urb->dev->dev;
-       int status = check_urb_status(urb);
-       int retval;
-
-       if (status) {
-               if ((status == -EAGAIN) || (status == -EPROTO))
-                       goto exit;
-               dev_err(dev, "urb svc in error %d (dropped)\n", status);
-               return;
-       }
-
-       /* We have a message, create a new message structure, add it to the
-        * list, and wake up our thread that will process the messages.
-        */
-       greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
-
-exit:
-       /* resubmit the urb to get more messages */
-       retval = usb_submit_urb(urb, GFP_ATOMIC);
-       if (retval)
-               dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
-}
-
-static void cport_in_callback(struct urb *urb)
-{
-       struct greybus_host_device *hd = urb->context;
-       struct device *dev = &urb->dev->dev;
-       int status = check_urb_status(urb);
-       int retval;
-       u16 cport_id;
-       u8 *data;
-
-       if (status) {
-               if ((status == -EAGAIN) || (status == -EPROTO))
-                       goto exit;
-               dev_err(dev, "urb cport in error %d (dropped)\n", status);
-               return;
-       }
-
-       /* The size has to be at least one, for the cport id */
-       if (!urb->actual_length) {
-               dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
-               goto exit;
-       }
-
-       /*
-        * Our CPort number is the first byte of the data stream,
-        * the rest of the stream is "real" data
-        */
-       data = urb->transfer_buffer;
-       cport_id = (u16)data[0];
-       data = &data[1];
-
-       /* Pass this data to the greybus core */
-       greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
-
-exit:
-       /* put our urb back in the request pool */
-       retval = usb_submit_urb(urb, GFP_ATOMIC);
-       if (retval)
-               dev_err(dev, "%s: error %d in submitting urb.\n",
-                       __func__, retval);
-}
-
-static void cport_out_callback(struct urb *urb)
-{
-       struct greybus_host_device *hd = urb->context;
-       struct es1_ap_dev *es1 = hd_to_es1(hd);
-       int status = check_urb_status(urb);
-       u8 *data = urb->transfer_buffer + 1;
-
-       /*
-        * Tell the submitter that the buffer send (attempt) is
-        * complete, and report the status.  The submitter's buffer
-        * starts after the one-byte CPort id we inserted.
-        */
-       data = urb->transfer_buffer + 1;
-       greybus_data_sent(hd, data, status);
-
-       free_urb(es1, urb);
-       /*
-        * Rest assured Greg, this craziness is getting fixed.
-        *
-        * Yes, you are right, we aren't telling anyone that the urb finished.
-        * "That's crazy!  How does this all even work?" you might be saying.
-        * The "magic" is the idea that greybus works on the "operation" level,
-        * not the "send a buffer" level.  All operations are "round-trip" with
-        * a response from the device that the operation finished, or it will
-        * time out.  Because of that, we don't care that this urb finished, or
-        * failed, or did anything else, as higher levels of the protocol stack
-        * will handle completions and timeouts and the rest.
-        *
-        * This protocol is "needed" due to some hardware restrictions on the
-        * current generation of Unipro controllers.  Think about it for a
-        * minute, this is a USB driver, talking to a Unipro bridge, impedance
-        * mismatch is huge, yet the Unipro controller are even more
-        * underpowered than this little USB controller.  We rely on the round
-        * trip to keep stalls in the Unipro controllers from happening so that
-        * we can keep data flowing properly, no matter how slow it might be.
-        *
-        * Once again, a wonderful bus protocol cut down in its prime by a naive
-        * controller chip.  We dream of the day we have a "real" HCD for
-        * Unipro.  Until then, we suck it up and make the hardware work, as
-        * that's the job of the firmware and kernel.
-        * </rant>
-        */
-}
-
-/*
- * The ES1 USB Bridge device contains 4 endpoints
- * 1 Control - usual USB stuff + AP -> SVC messages
- * 1 Interrupt IN - SVC -> AP messages
- * 1 Bulk IN - CPort data in
- * 1 Bulk OUT - CPort data out
- */
-static int ap_probe(struct usb_interface *interface,
-                   const struct usb_device_id *id)
-{
-       struct es1_ap_dev *es1;
-       struct greybus_host_device *hd;
-       struct usb_device *udev;
-       struct usb_host_interface *iface_desc;
-       struct usb_endpoint_descriptor *endpoint;
-       bool int_in_found = false;
-       bool bulk_in_found = false;
-       bool bulk_out_found = false;
-       int retval = -ENOMEM;
-       int i;
-       u8 svc_interval = 0;
-
-       udev = usb_get_dev(interface_to_usbdev(interface));
-
-       hd = greybus_create_hd(&es1_driver, &udev->dev);
-       if (!hd) {
-               usb_put_dev(udev);
-               return -ENOMEM;
-       }
-
-       /* Fill in the buffer allocation constraints */
-       hd_buffer_constraints(hd);
-
-       es1 = hd_to_es1(hd);
-       es1->hd = hd;
-       es1->usb_intf = interface;
-       es1->usb_dev = udev;
-       spin_lock_init(&es1->cport_out_urb_lock);
-       usb_set_intfdata(interface, es1);
-
-       /* Control endpoint is the pipe to talk to this AP, so save it off */
-       endpoint = &udev->ep0.desc;
-       es1->control_endpoint = endpoint->bEndpointAddress;
-
-       /* find all 3 of our endpoints */
-       iface_desc = interface->cur_altsetting;
-       for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
-               endpoint = &iface_desc->endpoint[i].desc;
-
-               if (usb_endpoint_is_int_in(endpoint)) {
-                       es1->svc_endpoint = endpoint->bEndpointAddress;
-                       svc_interval = endpoint->bInterval;
-                       int_in_found = true;
-               } else if (usb_endpoint_is_bulk_in(endpoint)) {
-                       es1->cport_in_endpoint = endpoint->bEndpointAddress;
-                       bulk_in_found = true;
-               } else if (usb_endpoint_is_bulk_out(endpoint)) {
-                       es1->cport_out_endpoint = endpoint->bEndpointAddress;
-                       bulk_out_found = true;
-               } else {
-                       dev_err(&udev->dev,
-                               "Unknown endpoint type found, address %x\n",
-                               endpoint->bEndpointAddress);
-               }
-       }
-       if ((int_in_found == false) ||
-           (bulk_in_found == false) ||
-           (bulk_out_found == false)) {
-               dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
-               goto error;
-       }
-
-       /* Create our buffer and URB to get SVC messages, and start it up */
-       es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
-       if (!es1->svc_buffer)
-               goto error;
-
-       es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
-       if (!es1->svc_urb)
-               goto error;
-
-       usb_fill_int_urb(es1->svc_urb, udev,
-                        usb_rcvintpipe(udev, es1->svc_endpoint),
-                        es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
-                        hd, svc_interval);
-       retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
-       if (retval)
-               goto error;
-
-       /* Allocate buffers for our cport in messages and start them up */
-       for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
-               struct urb *urb;
-               u8 *buffer;
-
-               urb = usb_alloc_urb(0, GFP_KERNEL);
-               if (!urb)
-                       goto error;
-               buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
-               if (!buffer)
-                       goto error;
-
-               usb_fill_bulk_urb(urb, udev,
-                                 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
-                                 buffer, ES1_GBUF_MSG_SIZE_MAX,
-                                 cport_in_callback, hd);
-               es1->cport_in_urb[i] = urb;
-               es1->cport_in_buffer[i] = buffer;
-               retval = usb_submit_urb(urb, GFP_KERNEL);
-               if (retval)
-                       goto error;
-       }
-
-       /* Allocate urbs for our CPort OUT messages */
-       for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
-               struct urb *urb;
-
-               urb = usb_alloc_urb(0, GFP_KERNEL);
-               if (!urb)
-                       goto error;
-
-               es1->cport_out_urb[i] = urb;
-               es1->cport_out_urb_busy[i] = false;     /* just to be anal */
-       }
-
-       return 0;
-error:
-       ap_disconnect(interface);
-
-       return retval;
-}
-
-static struct usb_driver es1_ap_driver = {
-       .name =         "es1_ap_driver",
-       .probe =        ap_probe,
-       .disconnect =   ap_disconnect,
-       .id_table =     id_table,
-};
-
-module_usb_driver(es1_ap_driver);
-
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
diff --git a/drivers/staging/greybus/gb-vibrator.c b/drivers/staging/greybus/gb-vibrator.c
deleted file mode 100644 (file)
index b5332df..0000000
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Greybus Vibrator protocol driver.
- *
- * Copyright 2014 Google Inc.
- * Copyright 2014 Linaro Ltd.
- *
- * Released under the GPLv2 only.
- */
-
-#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/slab.h>
-#include <linux/device.h>
-#include <linux/kdev_t.h>
-#include <linux/idr.h>
-#include "greybus.h"
-
-struct gb_vibrator_device {
-       struct gb_connection    *connection;
-       struct device           *dev;
-       int                     minor;          /* vibrator minor number */
-       u8                      version_major;
-       u8                      version_minor;
-};
-
-/* Version of the Greybus vibrator protocol we support */
-#define        GB_VIBRATOR_VERSION_MAJOR               0x00
-#define        GB_VIBRATOR_VERSION_MINOR               0x01
-
-/* Greybus Vibrator request types */
-#define        GB_VIBRATOR_TYPE_INVALID                0x00
-#define        GB_VIBRATOR_TYPE_PROTOCOL_VERSION       0x01
-#define        GB_VIBRATOR_TYPE_ON                     0x02
-#define        GB_VIBRATOR_TYPE_OFF                    0x03
-#define        GB_VIBRATOR_TYPE_RESPONSE               0x80    /* OR'd with rest */
-
-struct gb_vibrator_proto_version_response {
-       __u8    major;
-       __u8    minor;
-};
-
-struct gb_vibrator_on_request {
-       __le16  timeout_ms;
-};
-
-/*
- * This request only uses the connection field, and if successful,
- * fills in the major and minor protocol version of the target.
- */
-static int get_version(struct gb_vibrator_device *vib)
-{
-       struct gb_connection *connection = vib->connection;
-       struct gb_vibrator_proto_version_response version_response;
-       int retval;
-
-       retval = gb_operation_sync(connection,
-                                  GB_VIBRATOR_TYPE_PROTOCOL_VERSION,
-                                  NULL, 0,
-                                  &version_response, sizeof(version_response));
-       if (retval)
-               return retval;
-
-       if (version_response.major > GB_VIBRATOR_VERSION_MAJOR) {
-               dev_err(&connection->dev,
-                       "unsupported major version (%hhu > %hhu)\n",
-                       version_response.major, GB_VIBRATOR_VERSION_MAJOR);
-               return -ENOTSUPP;
-       }
-
-       vib->version_major = version_response.major;
-       vib->version_minor = version_response.minor;
-       return 0;
-}
-
-static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms)
-{
-       struct gb_vibrator_on_request request;
-
-       request.timeout_ms = cpu_to_le16(timeout_ms);
-       return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON,
-                                &request, sizeof(request), NULL, 0);
-}
-
-static int turn_off(struct gb_vibrator_device *vib)
-{
-       return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF,
-                                NULL, 0, NULL, 0);
-}
-
-static ssize_t timeout_store(struct device *dev, struct device_attribute *attr,
-                            const char *buf, size_t count)
-{
-       struct gb_vibrator_device *vib = dev_get_drvdata(dev);
-       unsigned long val;
-       int retval;
-
-       retval = kstrtoul(buf, 10, &val);
-       if (retval < 0) {
-               dev_err(dev, "could not parse timeout value %d\n", retval);
-               return retval;
-       }
-
-       if (val)
-               retval = turn_on(vib, (u16)val);
-       else
-               retval = turn_off(vib);
-       if (retval)
-               return retval;
-
-       return count;
-}
-static DEVICE_ATTR_WO(timeout);
-
-static struct attribute *vibrator_attrs[] = {
-       &dev_attr_timeout.attr,
-       NULL,
-};
-ATTRIBUTE_GROUPS(vibrator);
-
-static struct class vibrator_class = {
-       .name           = "vibrator",
-       .owner          = THIS_MODULE,
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
-       .dev_groups     = vibrator_groups,
-#endif
-};
-
-static DEFINE_IDR(minors);
-
-static int gb_vibrator_connection_init(struct gb_connection *connection)
-{
-       struct gb_vibrator_device *vib;
-       struct device *dev;
-       int retval;
-
-       vib = kzalloc(sizeof(*vib), GFP_KERNEL);
-       if (!vib)
-               return -ENOMEM;
-
-       vib->connection = connection;
-       connection->private = vib;
-
-       retval = get_version(vib);
-       if (retval)
-               goto error;
-
-       /*
-        * For now we create a device in sysfs for the vibrator, but odds are
-        * there is a "real" device somewhere in the kernel for this, but I
-        * can't find it at the moment...
-        */
-       vib->minor = idr_alloc(&minors, vib, 0, 0, GFP_KERNEL);
-       if (vib->minor < 0) {
-               retval = vib->minor;
-               goto error;
-       }
-       dev = device_create(&vibrator_class, &connection->dev, MKDEV(0, 0), vib,
-                           "vibrator%d", vib->minor);
-       if (IS_ERR(dev)) {
-               retval = -EINVAL;
-               goto error;
-       }
-       vib->dev = dev;
-
-#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
-       /*
-        * Newer kernels handle this in a race-free manner, by the dev_groups
-        * field in the struct class up above.  But for older kernels, we need
-        * to "open code this :(
-        */
-       retval = sysfs_create_group(&dev->kobj, vibrator_groups[0]);
-       if (retval) {
-               device_unregister(dev);
-               goto error;
-       }
-#endif
-
-       return 0;
-
-error:
-       kfree(vib);
-       return retval;
-}
-
-static void gb_vibrator_connection_exit(struct gb_connection *connection)
-{
-       struct gb_vibrator_device *vib = connection->private;
-
-#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
-       sysfs_remove_group(&vib->dev->kobj, vibrator_groups[0]);
-#endif
-       idr_remove(&minors, vib->minor);
-       device_unregister(vib->dev);
-       kfree(vib);
-}
-
-static struct gb_protocol vibrator_protocol = {
-       .name                   = "vibrator",
-       .id                     = GREYBUS_PROTOCOL_VIBRATOR,
-       .major                  = 0,
-       .minor                  = 1,
-       .connection_init        = gb_vibrator_connection_init,
-       .connection_exit        = gb_vibrator_connection_exit,
-       .request_recv           = NULL, /* no incoming requests */
-};
-
-static __init int protocol_init(void)
-{
-       int retval;
-
-       retval = class_register(&vibrator_class);
-       if (retval)
-               return retval;
-
-       return gb_protocol_register(&vibrator_protocol);
-}
-
-static __exit void protocol_exit(void)
-{
-       gb_protocol_deregister(&vibrator_protocol);
-       class_unregister(&vibrator_class);
-}
-
-module_init(protocol_init);
-module_exit(protocol_exit);
-
-MODULE_LICENSE("GPL v2");
diff --git a/drivers/staging/greybus/vibrator.c b/drivers/staging/greybus/vibrator.c
new file mode 100644 (file)
index 0000000..b5332df
--- /dev/null
@@ -0,0 +1,227 @@
+/*
+ * Greybus Vibrator protocol driver.
+ *
+ * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
+ *
+ * Released under the GPLv2 only.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/device.h>
+#include <linux/kdev_t.h>
+#include <linux/idr.h>
+#include "greybus.h"
+
+struct gb_vibrator_device {
+       struct gb_connection    *connection;
+       struct device           *dev;
+       int                     minor;          /* vibrator minor number */
+       u8                      version_major;
+       u8                      version_minor;
+};
+
+/* Version of the Greybus vibrator protocol we support */
+#define        GB_VIBRATOR_VERSION_MAJOR               0x00
+#define        GB_VIBRATOR_VERSION_MINOR               0x01
+
+/* Greybus Vibrator request types */
+#define        GB_VIBRATOR_TYPE_INVALID                0x00
+#define        GB_VIBRATOR_TYPE_PROTOCOL_VERSION       0x01
+#define        GB_VIBRATOR_TYPE_ON                     0x02
+#define        GB_VIBRATOR_TYPE_OFF                    0x03
+#define        GB_VIBRATOR_TYPE_RESPONSE               0x80    /* OR'd with rest */
+
+struct gb_vibrator_proto_version_response {
+       __u8    major;
+       __u8    minor;
+};
+
+struct gb_vibrator_on_request {
+       __le16  timeout_ms;
+};
+
+/*
+ * This request only uses the connection field, and if successful,
+ * fills in the major and minor protocol version of the target.
+ */
+static int get_version(struct gb_vibrator_device *vib)
+{
+       struct gb_connection *connection = vib->connection;
+       struct gb_vibrator_proto_version_response version_response;
+       int retval;
+
+       retval = gb_operation_sync(connection,
+                                  GB_VIBRATOR_TYPE_PROTOCOL_VERSION,
+                                  NULL, 0,
+                                  &version_response, sizeof(version_response));
+       if (retval)
+               return retval;
+
+       if (version_response.major > GB_VIBRATOR_VERSION_MAJOR) {
+               dev_err(&connection->dev,
+                       "unsupported major version (%hhu > %hhu)\n",
+                       version_response.major, GB_VIBRATOR_VERSION_MAJOR);
+               return -ENOTSUPP;
+       }
+
+       vib->version_major = version_response.major;
+       vib->version_minor = version_response.minor;
+       return 0;
+}
+
+static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms)
+{
+       struct gb_vibrator_on_request request;
+
+       request.timeout_ms = cpu_to_le16(timeout_ms);
+       return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON,
+                                &request, sizeof(request), NULL, 0);
+}
+
+static int turn_off(struct gb_vibrator_device *vib)
+{
+       return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF,
+                                NULL, 0, NULL, 0);
+}
+
+static ssize_t timeout_store(struct device *dev, struct device_attribute *attr,
+                            const char *buf, size_t count)
+{
+       struct gb_vibrator_device *vib = dev_get_drvdata(dev);
+       unsigned long val;
+       int retval;
+
+       retval = kstrtoul(buf, 10, &val);
+       if (retval < 0) {
+               dev_err(dev, "could not parse timeout value %d\n", retval);
+               return retval;
+       }
+
+       if (val)
+               retval = turn_on(vib, (u16)val);
+       else
+               retval = turn_off(vib);
+       if (retval)
+               return retval;
+
+       return count;
+}
+static DEVICE_ATTR_WO(timeout);
+
+static struct attribute *vibrator_attrs[] = {
+       &dev_attr_timeout.attr,
+       NULL,
+};
+ATTRIBUTE_GROUPS(vibrator);
+
+static struct class vibrator_class = {
+       .name           = "vibrator",
+       .owner          = THIS_MODULE,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
+       .dev_groups     = vibrator_groups,
+#endif
+};
+
+static DEFINE_IDR(minors);
+
+static int gb_vibrator_connection_init(struct gb_connection *connection)
+{
+       struct gb_vibrator_device *vib;
+       struct device *dev;
+       int retval;
+
+       vib = kzalloc(sizeof(*vib), GFP_KERNEL);
+       if (!vib)
+               return -ENOMEM;
+
+       vib->connection = connection;
+       connection->private = vib;
+
+       retval = get_version(vib);
+       if (retval)
+               goto error;
+
+       /*
+        * For now we create a device in sysfs for the vibrator, but odds are
+        * there is a "real" device somewhere in the kernel for this, but I
+        * can't find it at the moment...
+        */
+       vib->minor = idr_alloc(&minors, vib, 0, 0, GFP_KERNEL);
+       if (vib->minor < 0) {
+               retval = vib->minor;
+               goto error;
+       }
+       dev = device_create(&vibrator_class, &connection->dev, MKDEV(0, 0), vib,
+                           "vibrator%d", vib->minor);
+       if (IS_ERR(dev)) {
+               retval = -EINVAL;
+               goto error;
+       }
+       vib->dev = dev;
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
+       /*
+        * Newer kernels handle this in a race-free manner, by the dev_groups
+        * field in the struct class up above.  But for older kernels, we need
+        * to "open code this :(
+        */
+       retval = sysfs_create_group(&dev->kobj, vibrator_groups[0]);
+       if (retval) {
+               device_unregister(dev);
+               goto error;
+       }
+#endif
+
+       return 0;
+
+error:
+       kfree(vib);
+       return retval;
+}
+
+static void gb_vibrator_connection_exit(struct gb_connection *connection)
+{
+       struct gb_vibrator_device *vib = connection->private;
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
+       sysfs_remove_group(&vib->dev->kobj, vibrator_groups[0]);
+#endif
+       idr_remove(&minors, vib->minor);
+       device_unregister(vib->dev);
+       kfree(vib);
+}
+
+static struct gb_protocol vibrator_protocol = {
+       .name                   = "vibrator",
+       .id                     = GREYBUS_PROTOCOL_VIBRATOR,
+       .major                  = 0,
+       .minor                  = 1,
+       .connection_init        = gb_vibrator_connection_init,
+       .connection_exit        = gb_vibrator_connection_exit,
+       .request_recv           = NULL, /* no incoming requests */
+};
+
+static __init int protocol_init(void)
+{
+       int retval;
+
+       retval = class_register(&vibrator_class);
+       if (retval)
+               return retval;
+
+       return gb_protocol_register(&vibrator_protocol);
+}
+
+static __exit void protocol_exit(void)
+{
+       gb_protocol_deregister(&vibrator_protocol);
+       class_unregister(&vibrator_class);
+}
+
+module_init(protocol_init);
+module_exit(protocol_exit);
+
+MODULE_LICENSE("GPL v2");