greybus: remove redundant latency-tag sanity checks
[cascardo/linux.git] / drivers / staging / greybus / es2.c
index 75052af..5bd348f 100644 (file)
 #include "connection.h"
 #include "greybus_trace.h"
 
+
+/* Fixed CPort numbers */
+#define ES2_CPORT_CDSI0                16
+#define ES2_CPORT_CDSI1                17
+
 /* Memory sizes for the buffers sent to/from the ES2 controller */
 #define ES2_GBUF_MSG_SIZE_MAX  2048
 
 static const struct usb_device_id id_table[] = {
-       { USB_DEVICE(0xffff, 0x0002) }, /* Made up number, delete once firmware is fixed to use real number */
        { USB_DEVICE(0x18d1, 0x1eaf) },
        { },
 };
@@ -45,22 +49,6 @@ MODULE_DEVICE_TABLE(usb, id_table);
  */
 #define NUM_CPORT_OUT_URB      (8 * NUM_BULKS)
 
-/* vendor request APB1 log */
-#define REQUEST_LOG            0x02
-
-/* vendor request to map a cport to bulk in and bulk out endpoints */
-#define REQUEST_EP_MAPPING     0x03
-
-/* vendor request to get the number of cports available */
-#define REQUEST_CPORT_COUNT    0x04
-
-/* vendor request to reset a cport state */
-#define REQUEST_RESET_CPORT    0x05
-
-/* vendor request to time the latency of messages on a given cport */
-#define REQUEST_LATENCY_TAG_EN 0x06
-#define REQUEST_LATENCY_TAG_DIS        0x07
-
 /*
  * @endpoint: bulk in endpoint for CPort data
  * @urb: array of urbs for the CPort in messages
@@ -111,6 +99,8 @@ struct es2_ap_dev {
        bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
        spinlock_t cport_out_urb_lock;
 
+       bool cdsi1_in_use;
+
        int *cport_to_ep;
 
        struct task_struct *apb_log_task;
@@ -131,6 +121,29 @@ struct cport_to_ep {
        __u8 endpoint_out;
 };
 
+/**
+ * timesync_enable_request - Enable timesync in an APBridge
+ * @count: number of TimeSync Pulses to expect
+ * @frame_time: the initial FrameTime at the first TimeSync Pulse
+ * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
+ * @refclk: The AP mandated reference clock to run FrameTime at
+ */
+struct timesync_enable_request {
+       __u8    count;
+       __le64  frame_time;
+       __le32  strobe_delay;
+       __le32  refclk;
+} __packed;
+
+/**
+ * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
+ * @frame_time: An array of authoritative FrameTimes provided by the SVC
+ *              and relayed to the APBridge by the AP
+ */
+struct timesync_authoritative_request {
+       __le64  frame_time[GB_TIMESYNC_MAX_STROBES];
+} __packed;
+
 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
 {
        return (struct es2_ap_dev *)&hd->hd_priv;
@@ -189,7 +202,7 @@ static int map_cport_to_ep(struct es2_ap_dev *es2,
 
        retval = usb_control_msg(es2->usb_dev,
                                 usb_sndctrlpipe(es2->usb_dev, 0),
-                                REQUEST_EP_MAPPING,
+                                GB_APB_REQUEST_EP_MAPPING,
                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
                                 0x00, 0x00,
                                 (char *)cport_to_ep,
@@ -209,6 +222,88 @@ static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
 }
 #endif
 
+static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
+{
+       struct usb_device *udev = es2->usb_dev;
+       u8 *data;
+       int retval;
+
+       data = kmalloc(size, GFP_KERNEL);
+       if (!data)
+               return -ENOMEM;
+       memcpy(data, req, size);
+
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                cmd,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE,
+                                0, 0, data, size, ES2_TIMEOUT);
+       if (retval < 0)
+               dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
+       else
+               retval = 0;
+
+       kfree(data);
+       return retval;
+}
+
+static void ap_urb_complete(struct urb *urb)
+{
+       struct usb_ctrlrequest *dr = urb->context;
+
+       kfree(dr);
+       usb_free_urb(urb);
+}
+
+static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
+{
+       struct usb_device *udev = es2->usb_dev;
+       struct urb *urb;
+       struct usb_ctrlrequest *dr;
+       u8 *buf;
+       int retval;
+
+       urb = usb_alloc_urb(0, GFP_ATOMIC);
+       if (!urb)
+               return -ENOMEM;
+
+       dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
+       if (!dr) {
+               usb_free_urb(urb);
+               return -ENOMEM;
+       }
+
+       buf = (u8 *)dr + sizeof(*dr);
+       memcpy(buf, req, size);
+
+       dr->bRequest = cmd;
+       dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
+       dr->wValue = 0;
+       dr->wIndex = 0;
+       dr->wLength = cpu_to_le16(size);
+
+       usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
+                            (unsigned char *)dr, buf, size,
+                            ap_urb_complete, dr);
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval) {
+               usb_free_urb(urb);
+               kfree(dr);
+       }
+       return retval;
+}
+
+static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
+                    bool async)
+{
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+
+       if (async)
+               return output_async(es2, req, size, cmd);
+
+       return output_sync(es2, req, size, cmd);
+}
+
 static int es2_cport_in_enable(struct es2_ap_dev *es2,
                                struct es2_cport_in *cport_in)
 {
@@ -275,7 +370,7 @@ static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
         * Crap, pool is empty, complain to the syslog and go allocate one
         * dynamically as we have to succeed.
         */
-       dev_err(&es2->usb_dev->dev,
+       dev_dbg(&es2->usb_dev->dev,
                "No free CPort OUT urbs, having to dynamically allocate one!\n");
        return usb_alloc_urb(0, gfp_mask);
 }
@@ -349,8 +444,7 @@ static int message_send(struct gb_host_device *hd, u16 cport_id,
         * the target CPort id before filling it in.
         */
        if (!cport_id_valid(hd, cport_id)) {
-               dev_err(&udev->dev, "invalid destination cport 0x%02x\n",
-                               cport_id);
+               dev_err(&udev->dev, "invalid cport %u\n", cport_id);
                return -EINVAL;
        }
 
@@ -437,13 +531,20 @@ static int cport_reset(struct gb_host_device *hd, u16 cport_id)
        struct usb_device *udev = es2->usb_dev;
        int retval;
 
+       switch (cport_id) {
+       case GB_SVC_CPORT_ID:
+       case ES2_CPORT_CDSI0:
+       case ES2_CPORT_CDSI1:
+               return 0;
+       }
+
        retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
-                                REQUEST_RESET_CPORT,
+                                GB_APB_REQUEST_RESET_CPORT,
                                 USB_DIR_OUT | USB_TYPE_VENDOR |
-                                USB_RECIP_INTERFACE, 0, cport_id,
+                                USB_RECIP_INTERFACE, cport_id, 0,
                                 NULL, 0, ES2_TIMEOUT);
        if (retval < 0) {
-               dev_err(&udev->dev, "failed to reset cport %hu: %d\n", cport_id,
+               dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
                        retval);
                return retval;
        }
@@ -451,15 +552,66 @@ static int cport_reset(struct gb_host_device *hd, u16 cport_id)
        return 0;
 }
 
+static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
+                               unsigned long flags)
+{
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct ida *id_map = &hd->cport_id_map;
+       int ida_start, ida_end;
+
+       switch (cport_id) {
+       case ES2_CPORT_CDSI0:
+       case ES2_CPORT_CDSI1:
+               dev_err(&hd->dev, "cport %d not available\n", cport_id);
+               return -EBUSY;
+       }
+
+       if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
+                       flags & GB_CONNECTION_FLAG_CDSI1) {
+               if (es2->cdsi1_in_use) {
+                       dev_err(&hd->dev, "CDSI1 already in use\n");
+                       return -EBUSY;
+               }
+
+               es2->cdsi1_in_use = true;
+
+               return ES2_CPORT_CDSI1;
+       }
+
+       if (cport_id < 0) {
+               ida_start = 0;
+               ida_end = hd->num_cports;
+       } else if (cport_id < hd->num_cports) {
+               ida_start = cport_id;
+               ida_end = cport_id + 1;
+       } else {
+               dev_err(&hd->dev, "cport %d not available\n", cport_id);
+               return -EINVAL;
+       }
+
+       return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
+}
+
+static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
+{
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+
+       switch (cport_id) {
+       case ES2_CPORT_CDSI1:
+               es2->cdsi1_in_use = false;
+               return;
+       }
+
+       ida_simple_remove(&hd->cport_id_map, cport_id);
+}
+
 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
 {
        int retval;
 
-       if (cport_id != GB_SVC_CPORT_ID) {
-               retval = cport_reset(hd, cport_id);
-               if (retval)
-                       return retval;
-       }
+       retval = cport_reset(hd, cport_id);
+       if (retval)
+               return retval;
 
        return 0;
 }
@@ -470,14 +622,8 @@ static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
        struct es2_ap_dev *es2 = hd_to_es2(hd);
        struct usb_device *udev = es2->usb_dev;
 
-       if (!cport_id_valid(hd, cport_id)) {
-               dev_err(&udev->dev, "invalid destination cport 0x%02x\n",
-                       cport_id);
-               return -EINVAL;
-       }
-
        retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
-                                REQUEST_LATENCY_TAG_EN,
+                                GB_APB_REQUEST_LATENCY_TAG_EN,
                                 USB_DIR_OUT | USB_TYPE_VENDOR |
                                 USB_RECIP_INTERFACE, cport_id, 0, NULL,
                                 0, ES2_TIMEOUT);
@@ -494,14 +640,8 @@ static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
        struct es2_ap_dev *es2 = hd_to_es2(hd);
        struct usb_device *udev = es2->usb_dev;
 
-       if (!cport_id_valid(hd, cport_id)) {
-               dev_err(&udev->dev, "invalid destination cport 0x%02x\n",
-                       cport_id);
-               return -EINVAL;
-       }
-
        retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
-                                REQUEST_LATENCY_TAG_DIS,
+                                GB_APB_REQUEST_LATENCY_TAG_DIS,
                                 USB_DIR_OUT | USB_TYPE_VENDOR |
                                 USB_RECIP_INTERFACE, cport_id, 0, NULL,
                                 0, ES2_TIMEOUT);
@@ -512,13 +652,161 @@ static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
        return retval;
 }
 
+static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
+{
+       int retval;
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                GB_APB_REQUEST_CPORT_FEAT_EN,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE, cport_id, 0, NULL,
+                                0, ES2_TIMEOUT);
+       if (retval < 0)
+               dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
+                       cport_id, retval);
+       return retval;
+}
+
+static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
+{
+       int retval;
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                GB_APB_REQUEST_CPORT_FEAT_DIS,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE, cport_id, 0, NULL,
+                                0, ES2_TIMEOUT);
+       if (retval < 0)
+               dev_err(&udev->dev,
+                       "Cannot disable CPort features for cport %u: %d\n",
+                       cport_id, retval);
+       return retval;
+}
+
+static int timesync_enable(struct gb_host_device *hd, u8 count,
+                          u64 frame_time, u32 strobe_delay, u32 refclk)
+{
+       int retval;
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+       struct gb_control_timesync_enable_request *request;
+
+       request = kzalloc(sizeof(*request), GFP_KERNEL);
+       if (!request)
+               return -ENOMEM;
+
+       request->count = count;
+       request->frame_time = cpu_to_le64(frame_time);
+       request->strobe_delay = cpu_to_le32(strobe_delay);
+       request->refclk = cpu_to_le32(refclk);
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                REQUEST_TIMESYNC_ENABLE,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE, 0, 0, request,
+                                sizeof(*request), ES2_TIMEOUT);
+       if (retval < 0)
+               dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
+
+       kfree(request);
+       return retval;
+}
+
+static int timesync_disable(struct gb_host_device *hd)
+{
+       int retval;
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                REQUEST_TIMESYNC_DISABLE,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE, 0, 0, NULL,
+                                0, ES2_TIMEOUT);
+       if (retval < 0)
+               dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
+
+       return retval;
+}
+
+static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
+{
+       int retval, i;
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+       struct timesync_authoritative_request *request;
+
+       request = kzalloc(sizeof(*request), GFP_KERNEL);
+       if (!request)
+               return -ENOMEM;
+
+       for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
+               request->frame_time[i] = cpu_to_le64(frame_time[i]);
+
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                REQUEST_TIMESYNC_AUTHORITATIVE,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE, 0, 0, request,
+                                sizeof(*request), ES2_TIMEOUT);
+       if (retval < 0)
+               dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
+
+       kfree(request);
+       return retval;
+}
+
+static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
+{
+       int retval;
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+       __le64 *response_frame_time;
+
+       response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
+       if (!response_frame_time)
+               return -ENOMEM;
+
+       retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
+                                REQUEST_TIMESYNC_GET_LAST_EVENT,
+                                USB_DIR_IN | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE, 0, 0, response_frame_time,
+                                sizeof(*response_frame_time), ES2_TIMEOUT);
+
+       if (retval != sizeof(*response_frame_time)) {
+               dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
+                       retval);
+
+               if (retval >= 0)
+                       retval = -EIO;
+
+               goto out;
+       }
+       *frame_time = le64_to_cpu(*response_frame_time);
+       retval = 0;
+out:
+       kfree(response_frame_time);
+       return retval;
+}
+
 static struct gb_hd_driver es2_driver = {
-       .hd_priv_size           = sizeof(struct es2_ap_dev),
-       .message_send           = message_send,
-       .message_cancel         = message_cancel,
-       .cport_enable           = cport_enable,
-       .latency_tag_enable     = latency_tag_enable,
-       .latency_tag_disable    = latency_tag_disable,
+       .hd_priv_size                   = sizeof(struct es2_ap_dev),
+       .message_send                   = message_send,
+       .message_cancel                 = message_cancel,
+       .cport_allocate                 = es2_cport_allocate,
+       .cport_release                  = es2_cport_release,
+       .cport_enable                   = cport_enable,
+       .latency_tag_enable             = latency_tag_enable,
+       .latency_tag_disable            = latency_tag_disable,
+       .output                         = output,
+       .cport_features_enable          = cport_features_enable,
+       .cport_features_disable         = cport_features_disable,
+       .timesync_enable                = timesync_enable,
+       .timesync_disable               = timesync_disable,
+       .timesync_authoritative         = timesync_authoritative,
+       .timesync_get_last_event        = timesync_get_last_event,
 };
 
 /* Common function to report consistent warnings based on URB status */
@@ -547,21 +835,13 @@ static int check_urb_status(struct urb *urb)
        return -EAGAIN;
 }
 
-static void ap_disconnect(struct usb_interface *interface)
+static void es2_destroy(struct es2_ap_dev *es2)
 {
-       struct es2_ap_dev *es2;
        struct usb_device *udev;
-       int *cport_to_ep;
        int bulk_in;
        int i;
 
-       es2 = usb_get_intfdata(interface);
-       if (!es2)
-               return;
-
-       for (i = 0; i < NUM_BULKS; ++i)
-               es2_cport_in_disable(es2, &es2->cport_in[i]);
-
+       debugfs_remove(es2->apb_log_enable_dentry);
        usb_log_disable(es2);
 
        /* Tear down everything! */
@@ -590,12 +870,11 @@ static void ap_disconnect(struct usb_interface *interface)
                }
        }
 
-       usb_set_intfdata(interface, NULL);
+       kfree(es2->cport_to_ep);
+
        udev = es2->usb_dev;
-       cport_to_ep = es2->cport_to_ep;
-       gb_hd_remove(es2->hd);
+       gb_hd_put(es2->hd);
 
-       kfree(cport_to_ep);
        usb_put_dev(udev);
 }
 
@@ -611,6 +890,11 @@ static void cport_in_callback(struct urb *urb)
        if (status) {
                if ((status == -EAGAIN) || (status == -EPROTO))
                        goto exit;
+
+               /* The urb is being unlinked */
+               if (status == -ENOENT || status == -ESHUTDOWN)
+                       return;
+
                dev_err(dev, "urb cport in error %d (dropped)\n", status);
                return;
        }
@@ -629,7 +913,7 @@ static void cport_in_callback(struct urb *urb)
                greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
                                                        urb->actual_length);
        } else {
-               dev_err(dev, "invalid cport id 0x%02x received\n", cport_id);
+               dev_err(dev, "invalid cport id %u received\n", cport_id);
        }
 exit:
        /* put our urb back in the request pool */
@@ -670,7 +954,7 @@ static void apb_log_get(struct es2_ap_dev *es2, char *buf)
        do {
                retval = usb_control_msg(es2->usb_dev,
                                        usb_rcvctrlpipe(es2->usb_dev, 0),
-                                       REQUEST_LOG,
+                                       GB_APB_REQUEST_LOG,
                                        USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
                                        0x00, 0x00,
                                        buf,
@@ -738,7 +1022,7 @@ static void usb_log_enable(struct es2_ap_dev *es2)
                return;
        /* XXX We will need to rename this per APB */
        es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
-                                               gb_debugfs_get(), NULL,
+                                               gb_debugfs_get(), es2,
                                                &apb_log_fops);
 }
 
@@ -794,18 +1078,22 @@ static int apb_get_cport_count(struct usb_device *udev)
        int retval;
        __le16 *cport_count;
 
-       cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
+       cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
        if (!cport_count)
                return -ENOMEM;
 
        retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
-                                REQUEST_CPORT_COUNT,
+                                GB_APB_REQUEST_CPORT_COUNT,
                                 USB_DIR_IN | USB_TYPE_VENDOR |
                                 USB_RECIP_INTERFACE, 0, 0, cport_count,
                                 sizeof(*cport_count), ES2_TIMEOUT);
-       if (retval < 0) {
+       if (retval != sizeof(*cport_count)) {
                dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
                        retval);
+
+               if (retval >= 0)
+                       retval = -EIO;
+
                goto out;
        }
 
@@ -838,7 +1126,7 @@ static int ap_probe(struct usb_interface *interface,
        struct usb_endpoint_descriptor *endpoint;
        int bulk_in = 0;
        int bulk_out = 0;
-       int retval = -ENOMEM;
+       int retval;
        int i;
        int num_cports;
 
@@ -867,6 +1155,17 @@ static int ap_probe(struct usb_interface *interface,
        INIT_KFIFO(es2->apb_log_fifo);
        usb_set_intfdata(interface, es2);
 
+       /*
+        * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
+        * dynamically.
+        */
+       retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
+       if (retval)
+               goto error;
+       retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
+       if (retval)
+               goto error;
+
        es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
                                   GFP_KERNEL);
        if (!es2->cport_to_ep) {
@@ -887,12 +1186,13 @@ static int ap_probe(struct usb_interface *interface,
                                endpoint->bEndpointAddress;
                } else {
                        dev_err(&udev->dev,
-                               "Unknown endpoint type found, address %x\n",
+                               "Unknown endpoint type found, address 0x%02x\n",
                                endpoint->bEndpointAddress);
                }
        }
        if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
                dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
+               retval = -ENODEV;
                goto error;
        }
 
@@ -905,11 +1205,15 @@ static int ap_probe(struct usb_interface *interface,
                        u8 *buffer;
 
                        urb = usb_alloc_urb(0, GFP_KERNEL);
-                       if (!urb)
+                       if (!urb) {
+                               retval = -ENOMEM;
                                goto error;
+                       }
                        buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
-                       if (!buffer)
+                       if (!buffer) {
+                               retval = -ENOMEM;
                                goto error;
+                       }
 
                        usb_fill_bulk_urb(urb, udev,
                                          usb_rcvbulkpipe(udev,
@@ -926,8 +1230,10 @@ static int ap_probe(struct usb_interface *interface,
                struct urb *urb;
 
                urb = usb_alloc_urb(0, GFP_KERNEL);
-               if (!urb)
+               if (!urb) {
+                       retval = -ENOMEM;
                        goto error;
+               }
 
                es2->cport_out_urb[i] = urb;
                es2->cport_out_urb_busy[i] = false;     /* just to be anal */
@@ -939,24 +1245,47 @@ static int ap_probe(struct usb_interface *interface,
                                                        gb_debugfs_get(), es2,
                                                        &apb_log_enable_fops);
 
+       retval = gb_hd_add(hd);
+       if (retval)
+               goto error;
+
        for (i = 0; i < NUM_BULKS; ++i) {
                retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
                if (retval)
-                       goto error;
+                       goto err_disable_cport_in;
        }
 
        return 0;
+
+err_disable_cport_in:
+       for (--i; i >= 0; --i)
+               es2_cport_in_disable(es2, &es2->cport_in[i]);
+       gb_hd_del(hd);
 error:
-       ap_disconnect(interface);
+       es2_destroy(es2);
 
        return retval;
 }
 
+static void ap_disconnect(struct usb_interface *interface)
+{
+       struct es2_ap_dev *es2 = usb_get_intfdata(interface);
+       int i;
+
+       gb_hd_del(es2->hd);
+
+       for (i = 0; i < NUM_BULKS; ++i)
+               es2_cport_in_disable(es2, &es2->cport_in[i]);
+
+       es2_destroy(es2);
+}
+
 static struct usb_driver es2_ap_driver = {
        .name =         "es2_ap_driver",
        .probe =        ap_probe,
        .disconnect =   ap_disconnect,
        .id_table =     id_table,
+       .soft_unbind =  1,
 };
 
 module_usb_driver(es2_ap_driver);