Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Greybus "AP" USB driver for "ES2" controller chips
0004  *
0005  * Copyright 2014-2015 Google Inc.
0006  * Copyright 2014-2015 Linaro Ltd.
0007  */
0008 #include <linux/kthread.h>
0009 #include <linux/sizes.h>
0010 #include <linux/usb.h>
0011 #include <linux/kfifo.h>
0012 #include <linux/debugfs.h>
0013 #include <linux/list.h>
0014 #include <linux/greybus.h>
0015 #include <asm/unaligned.h>
0016 
0017 #include "arpc.h"
0018 #include "greybus_trace.h"
0019 
0020 
0021 /* Default timeout for USB vendor requests. */
0022 #define ES2_USB_CTRL_TIMEOUT    500
0023 
0024 /* Default timeout for ARPC CPort requests */
0025 #define ES2_ARPC_CPORT_TIMEOUT  500
0026 
0027 /* Fixed CPort numbers */
0028 #define ES2_CPORT_CDSI0     16
0029 #define ES2_CPORT_CDSI1     17
0030 
0031 /* Memory sizes for the buffers sent to/from the ES2 controller */
0032 #define ES2_GBUF_MSG_SIZE_MAX   2048
0033 
0034 /* Memory sizes for the ARPC buffers */
0035 #define ARPC_OUT_SIZE_MAX   U16_MAX
0036 #define ARPC_IN_SIZE_MAX    128
0037 
0038 static const struct usb_device_id id_table[] = {
0039     { USB_DEVICE(0x18d1, 0x1eaf) },
0040     { },
0041 };
0042 MODULE_DEVICE_TABLE(usb, id_table);
0043 
0044 #define APB1_LOG_SIZE       SZ_16K
0045 
0046 /*
0047  * Number of CPort IN urbs in flight at any point in time.
0048  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
0049  * flight.
0050  */
0051 #define NUM_CPORT_IN_URB    4
0052 
0053 /* Number of CPort OUT urbs in flight at any point in time.
0054  * Adjust if we get messages saying we are out of urbs in the system log.
0055  */
0056 #define NUM_CPORT_OUT_URB   8
0057 
0058 /*
0059  * Number of ARPC in urbs in flight at any point in time.
0060  */
0061 #define NUM_ARPC_IN_URB     2
0062 
0063 /*
0064  * @endpoint: bulk in endpoint for CPort data
0065  * @urb: array of urbs for the CPort in messages
0066  * @buffer: array of buffers for the @cport_in_urb urbs
0067  */
0068 struct es2_cport_in {
0069     __u8 endpoint;
0070     struct urb *urb[NUM_CPORT_IN_URB];
0071     u8 *buffer[NUM_CPORT_IN_URB];
0072 };
0073 
0074 /**
0075  * struct es2_ap_dev - ES2 USB Bridge to AP structure
0076  * @usb_dev: pointer to the USB device we are.
0077  * @usb_intf: pointer to the USB interface we are bound to.
0078  * @hd: pointer to our gb_host_device structure
0079  *
0080  * @cport_in: endpoint, urbs and buffer for cport in messages
0081  * @cport_out_endpoint: endpoint for cport out messages
0082  * @cport_out_urb: array of urbs for the CPort out messages
0083  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
0084  *          not.
0085  * @cport_out_urb_cancelled: array of flags indicating whether the
0086  *          corresponding @cport_out_urb is being cancelled
0087  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
0088  * @cdsi1_in_use: true if cport CDSI1 is in use
0089  * @apb_log_task: task pointer for logging thread
0090  * @apb_log_dentry: file system entry for the log file interface
0091  * @apb_log_enable_dentry: file system entry for enabling logging
0092  * @apb_log_fifo: kernel FIFO to carry logged data
0093  * @arpc_urb: array of urbs for the ARPC in messages
0094  * @arpc_buffer: array of buffers for the @arpc_urb urbs
0095  * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
0096  * @arpc_id_cycle: gives an unique id to ARPC
0097  * @arpc_lock: locks ARPC list
0098  * @arpcs: list of in progress ARPCs
0099  */
0100 struct es2_ap_dev {
0101     struct usb_device *usb_dev;
0102     struct usb_interface *usb_intf;
0103     struct gb_host_device *hd;
0104 
0105     struct es2_cport_in cport_in;
0106     __u8 cport_out_endpoint;
0107     struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
0108     bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
0109     bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
0110     spinlock_t cport_out_urb_lock;
0111 
0112     bool cdsi1_in_use;
0113 
0114     struct task_struct *apb_log_task;
0115     struct dentry *apb_log_dentry;
0116     struct dentry *apb_log_enable_dentry;
0117     DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
0118 
0119     __u8 arpc_endpoint_in;
0120     struct urb *arpc_urb[NUM_ARPC_IN_URB];
0121     u8 *arpc_buffer[NUM_ARPC_IN_URB];
0122 
0123     int arpc_id_cycle;
0124     spinlock_t arpc_lock;
0125     struct list_head arpcs;
0126 };
0127 
0128 struct arpc {
0129     struct list_head list;
0130     struct arpc_request_message *req;
0131     struct arpc_response_message *resp;
0132     struct completion response_received;
0133     bool active;
0134 };
0135 
0136 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
0137 {
0138     return (struct es2_ap_dev *)&hd->hd_priv;
0139 }
0140 
0141 static void cport_out_callback(struct urb *urb);
0142 static void usb_log_enable(struct es2_ap_dev *es2);
0143 static void usb_log_disable(struct es2_ap_dev *es2);
0144 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
0145              size_t size, int *result, unsigned int timeout);
0146 
0147 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
0148 {
0149     struct usb_device *udev = es2->usb_dev;
0150     u8 *data;
0151     int retval;
0152 
0153     data = kmemdup(req, size, GFP_KERNEL);
0154     if (!data)
0155         return -ENOMEM;
0156 
0157     retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0158                  cmd,
0159                  USB_DIR_OUT | USB_TYPE_VENDOR |
0160                  USB_RECIP_INTERFACE,
0161                  0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
0162     if (retval < 0)
0163         dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
0164     else
0165         retval = 0;
0166 
0167     kfree(data);
0168     return retval;
0169 }
0170 
0171 static void ap_urb_complete(struct urb *urb)
0172 {
0173     struct usb_ctrlrequest *dr = urb->context;
0174 
0175     kfree(dr);
0176     usb_free_urb(urb);
0177 }
0178 
0179 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
0180 {
0181     struct usb_device *udev = es2->usb_dev;
0182     struct urb *urb;
0183     struct usb_ctrlrequest *dr;
0184     u8 *buf;
0185     int retval;
0186 
0187     urb = usb_alloc_urb(0, GFP_ATOMIC);
0188     if (!urb)
0189         return -ENOMEM;
0190 
0191     dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
0192     if (!dr) {
0193         usb_free_urb(urb);
0194         return -ENOMEM;
0195     }
0196 
0197     buf = (u8 *)dr + sizeof(*dr);
0198     memcpy(buf, req, size);
0199 
0200     dr->bRequest = cmd;
0201     dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
0202     dr->wValue = 0;
0203     dr->wIndex = 0;
0204     dr->wLength = cpu_to_le16(size);
0205 
0206     usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
0207                  (unsigned char *)dr, buf, size,
0208                  ap_urb_complete, dr);
0209     retval = usb_submit_urb(urb, GFP_ATOMIC);
0210     if (retval) {
0211         usb_free_urb(urb);
0212         kfree(dr);
0213     }
0214     return retval;
0215 }
0216 
0217 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
0218           bool async)
0219 {
0220     struct es2_ap_dev *es2 = hd_to_es2(hd);
0221 
0222     if (async)
0223         return output_async(es2, req, size, cmd);
0224 
0225     return output_sync(es2, req, size, cmd);
0226 }
0227 
0228 static int es2_cport_in_enable(struct es2_ap_dev *es2,
0229                    struct es2_cport_in *cport_in)
0230 {
0231     struct urb *urb;
0232     int ret;
0233     int i;
0234 
0235     for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
0236         urb = cport_in->urb[i];
0237 
0238         ret = usb_submit_urb(urb, GFP_KERNEL);
0239         if (ret) {
0240             dev_err(&es2->usb_dev->dev,
0241                 "failed to submit in-urb: %d\n", ret);
0242             goto err_kill_urbs;
0243         }
0244     }
0245 
0246     return 0;
0247 
0248 err_kill_urbs:
0249     for (--i; i >= 0; --i) {
0250         urb = cport_in->urb[i];
0251         usb_kill_urb(urb);
0252     }
0253 
0254     return ret;
0255 }
0256 
0257 static void es2_cport_in_disable(struct es2_ap_dev *es2,
0258                  struct es2_cport_in *cport_in)
0259 {
0260     struct urb *urb;
0261     int i;
0262 
0263     for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
0264         urb = cport_in->urb[i];
0265         usb_kill_urb(urb);
0266     }
0267 }
0268 
0269 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
0270 {
0271     struct urb *urb;
0272     int ret;
0273     int i;
0274 
0275     for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
0276         urb = es2->arpc_urb[i];
0277 
0278         ret = usb_submit_urb(urb, GFP_KERNEL);
0279         if (ret) {
0280             dev_err(&es2->usb_dev->dev,
0281                 "failed to submit arpc in-urb: %d\n", ret);
0282             goto err_kill_urbs;
0283         }
0284     }
0285 
0286     return 0;
0287 
0288 err_kill_urbs:
0289     for (--i; i >= 0; --i) {
0290         urb = es2->arpc_urb[i];
0291         usb_kill_urb(urb);
0292     }
0293 
0294     return ret;
0295 }
0296 
0297 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
0298 {
0299     struct urb *urb;
0300     int i;
0301 
0302     for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
0303         urb = es2->arpc_urb[i];
0304         usb_kill_urb(urb);
0305     }
0306 }
0307 
0308 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
0309 {
0310     struct urb *urb = NULL;
0311     unsigned long flags;
0312     int i;
0313 
0314     spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
0315 
0316     /* Look in our pool of allocated urbs first, as that's the "fastest" */
0317     for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
0318         if (!es2->cport_out_urb_busy[i] &&
0319             !es2->cport_out_urb_cancelled[i]) {
0320             es2->cport_out_urb_busy[i] = true;
0321             urb = es2->cport_out_urb[i];
0322             break;
0323         }
0324     }
0325     spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
0326     if (urb)
0327         return urb;
0328 
0329     /*
0330      * Crap, pool is empty, complain to the syslog and go allocate one
0331      * dynamically as we have to succeed.
0332      */
0333     dev_dbg(&es2->usb_dev->dev,
0334         "No free CPort OUT urbs, having to dynamically allocate one!\n");
0335     return usb_alloc_urb(0, gfp_mask);
0336 }
0337 
0338 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
0339 {
0340     unsigned long flags;
0341     int i;
0342     /*
0343      * See if this was an urb in our pool, if so mark it "free", otherwise
0344      * we need to free it ourselves.
0345      */
0346     spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
0347     for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
0348         if (urb == es2->cport_out_urb[i]) {
0349             es2->cport_out_urb_busy[i] = false;
0350             urb = NULL;
0351             break;
0352         }
0353     }
0354     spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
0355 
0356     /* If urb is not NULL, then we need to free this urb */
0357     usb_free_urb(urb);
0358 }
0359 
0360 /*
0361  * We (ab)use the operation-message header pad bytes to transfer the
0362  * cport id in order to minimise overhead.
0363  */
0364 static void
0365 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
0366 {
0367     header->pad[0] = cport_id;
0368 }
0369 
0370 /* Clear the pad bytes used for the CPort id */
0371 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
0372 {
0373     header->pad[0] = 0;
0374 }
0375 
0376 /* Extract the CPort id packed into the header, and clear it */
0377 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
0378 {
0379     u16 cport_id = header->pad[0];
0380 
0381     gb_message_cport_clear(header);
0382 
0383     return cport_id;
0384 }
0385 
0386 /*
0387  * Returns zero if the message was successfully queued, or a negative errno
0388  * otherwise.
0389  */
0390 static int message_send(struct gb_host_device *hd, u16 cport_id,
0391             struct gb_message *message, gfp_t gfp_mask)
0392 {
0393     struct es2_ap_dev *es2 = hd_to_es2(hd);
0394     struct usb_device *udev = es2->usb_dev;
0395     size_t buffer_size;
0396     int retval;
0397     struct urb *urb;
0398     unsigned long flags;
0399 
0400     /*
0401      * The data actually transferred will include an indication
0402      * of where the data should be sent.  Do one last check of
0403      * the target CPort id before filling it in.
0404      */
0405     if (!cport_id_valid(hd, cport_id)) {
0406         dev_err(&udev->dev, "invalid cport %u\n", cport_id);
0407         return -EINVAL;
0408     }
0409 
0410     /* Find a free urb */
0411     urb = next_free_urb(es2, gfp_mask);
0412     if (!urb)
0413         return -ENOMEM;
0414 
0415     spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
0416     message->hcpriv = urb;
0417     spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
0418 
0419     /* Pack the cport id into the message header */
0420     gb_message_cport_pack(message->header, cport_id);
0421 
0422     buffer_size = sizeof(*message->header) + message->payload_size;
0423 
0424     usb_fill_bulk_urb(urb, udev,
0425               usb_sndbulkpipe(udev,
0426                       es2->cport_out_endpoint),
0427               message->buffer, buffer_size,
0428               cport_out_callback, message);
0429     urb->transfer_flags |= URB_ZERO_PACKET;
0430 
0431     trace_gb_message_submit(message);
0432 
0433     retval = usb_submit_urb(urb, gfp_mask);
0434     if (retval) {
0435         dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
0436 
0437         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
0438         message->hcpriv = NULL;
0439         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
0440 
0441         free_urb(es2, urb);
0442         gb_message_cport_clear(message->header);
0443 
0444         return retval;
0445     }
0446 
0447     return 0;
0448 }
0449 
0450 /*
0451  * Can not be called in atomic context.
0452  */
0453 static void message_cancel(struct gb_message *message)
0454 {
0455     struct gb_host_device *hd = message->operation->connection->hd;
0456     struct es2_ap_dev *es2 = hd_to_es2(hd);
0457     struct urb *urb;
0458     int i;
0459 
0460     might_sleep();
0461 
0462     spin_lock_irq(&es2->cport_out_urb_lock);
0463     urb = message->hcpriv;
0464 
0465     /* Prevent dynamically allocated urb from being deallocated. */
0466     usb_get_urb(urb);
0467 
0468     /* Prevent pre-allocated urb from being reused. */
0469     for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
0470         if (urb == es2->cport_out_urb[i]) {
0471             es2->cport_out_urb_cancelled[i] = true;
0472             break;
0473         }
0474     }
0475     spin_unlock_irq(&es2->cport_out_urb_lock);
0476 
0477     usb_kill_urb(urb);
0478 
0479     if (i < NUM_CPORT_OUT_URB) {
0480         spin_lock_irq(&es2->cport_out_urb_lock);
0481         es2->cport_out_urb_cancelled[i] = false;
0482         spin_unlock_irq(&es2->cport_out_urb_lock);
0483     }
0484 
0485     usb_free_urb(urb);
0486 }
0487 
0488 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
0489                   unsigned long flags)
0490 {
0491     struct es2_ap_dev *es2 = hd_to_es2(hd);
0492     struct ida *id_map = &hd->cport_id_map;
0493     int ida_start, ida_end;
0494 
0495     switch (cport_id) {
0496     case ES2_CPORT_CDSI0:
0497     case ES2_CPORT_CDSI1:
0498         dev_err(&hd->dev, "cport %d not available\n", cport_id);
0499         return -EBUSY;
0500     }
0501 
0502     if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
0503         flags & GB_CONNECTION_FLAG_CDSI1) {
0504         if (es2->cdsi1_in_use) {
0505             dev_err(&hd->dev, "CDSI1 already in use\n");
0506             return -EBUSY;
0507         }
0508 
0509         es2->cdsi1_in_use = true;
0510 
0511         return ES2_CPORT_CDSI1;
0512     }
0513 
0514     if (cport_id < 0) {
0515         ida_start = 0;
0516         ida_end = hd->num_cports;
0517     } else if (cport_id < hd->num_cports) {
0518         ida_start = cport_id;
0519         ida_end = cport_id + 1;
0520     } else {
0521         dev_err(&hd->dev, "cport %d not available\n", cport_id);
0522         return -EINVAL;
0523     }
0524 
0525     return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
0526 }
0527 
0528 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
0529 {
0530     struct es2_ap_dev *es2 = hd_to_es2(hd);
0531 
0532     switch (cport_id) {
0533     case ES2_CPORT_CDSI1:
0534         es2->cdsi1_in_use = false;
0535         return;
0536     }
0537 
0538     ida_simple_remove(&hd->cport_id_map, cport_id);
0539 }
0540 
0541 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
0542             unsigned long flags)
0543 {
0544     struct es2_ap_dev *es2 = hd_to_es2(hd);
0545     struct usb_device *udev = es2->usb_dev;
0546     struct gb_apb_request_cport_flags *req;
0547     u32 connection_flags;
0548     int ret;
0549 
0550     req = kzalloc(sizeof(*req), GFP_KERNEL);
0551     if (!req)
0552         return -ENOMEM;
0553 
0554     connection_flags = 0;
0555     if (flags & GB_CONNECTION_FLAG_CONTROL)
0556         connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
0557     if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
0558         connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
0559 
0560     req->flags = cpu_to_le32(connection_flags);
0561 
0562     dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
0563         cport_id, connection_flags);
0564 
0565     ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0566                   GB_APB_REQUEST_CPORT_FLAGS,
0567                   USB_DIR_OUT | USB_TYPE_VENDOR |
0568                   USB_RECIP_INTERFACE, cport_id, 0,
0569                   req, sizeof(*req), ES2_USB_CTRL_TIMEOUT);
0570     if (ret < 0) {
0571         dev_err(&udev->dev, "failed to set cport flags for port %d\n",
0572             cport_id);
0573         goto out;
0574     }
0575 
0576     ret = 0;
0577 out:
0578     kfree(req);
0579 
0580     return ret;
0581 }
0582 
0583 static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id)
0584 {
0585     struct es2_ap_dev *es2 = hd_to_es2(hd);
0586     struct device *dev = &es2->usb_dev->dev;
0587     struct arpc_cport_connected_req req;
0588     int ret;
0589 
0590     req.cport_id = cpu_to_le16(cport_id);
0591     ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req),
0592             NULL, ES2_ARPC_CPORT_TIMEOUT);
0593     if (ret) {
0594         dev_err(dev, "failed to set connected state for cport %u: %d\n",
0595             cport_id, ret);
0596         return ret;
0597     }
0598 
0599     return 0;
0600 }
0601 
0602 static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id)
0603 {
0604     struct es2_ap_dev *es2 = hd_to_es2(hd);
0605     struct device *dev = &es2->usb_dev->dev;
0606     struct arpc_cport_flush_req req;
0607     int ret;
0608 
0609     req.cport_id = cpu_to_le16(cport_id);
0610     ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req),
0611             NULL, ES2_ARPC_CPORT_TIMEOUT);
0612     if (ret) {
0613         dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret);
0614         return ret;
0615     }
0616 
0617     return 0;
0618 }
0619 
0620 static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id,
0621                   u8 phase, unsigned int timeout)
0622 {
0623     struct es2_ap_dev *es2 = hd_to_es2(hd);
0624     struct device *dev = &es2->usb_dev->dev;
0625     struct arpc_cport_shutdown_req req;
0626     int result;
0627     int ret;
0628 
0629     if (timeout > U16_MAX)
0630         return -EINVAL;
0631 
0632     req.cport_id = cpu_to_le16(cport_id);
0633     req.timeout = cpu_to_le16(timeout);
0634     req.phase = phase;
0635     ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req),
0636             &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
0637     if (ret) {
0638         dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n",
0639             cport_id, ret, result);
0640         return ret;
0641     }
0642 
0643     return 0;
0644 }
0645 
0646 static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id,
0647                  size_t peer_space, unsigned int timeout)
0648 {
0649     struct es2_ap_dev *es2 = hd_to_es2(hd);
0650     struct device *dev = &es2->usb_dev->dev;
0651     struct arpc_cport_quiesce_req req;
0652     int result;
0653     int ret;
0654 
0655     if (peer_space > U16_MAX)
0656         return -EINVAL;
0657 
0658     if (timeout > U16_MAX)
0659         return -EINVAL;
0660 
0661     req.cport_id = cpu_to_le16(cport_id);
0662     req.peer_space = cpu_to_le16(peer_space);
0663     req.timeout = cpu_to_le16(timeout);
0664     ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req),
0665             &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
0666     if (ret) {
0667         dev_err(dev, "failed to quiesce cport %u: %d (%d)\n",
0668             cport_id, ret, result);
0669         return ret;
0670     }
0671 
0672     return 0;
0673 }
0674 
0675 static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id)
0676 {
0677     struct es2_ap_dev *es2 = hd_to_es2(hd);
0678     struct device *dev = &es2->usb_dev->dev;
0679     struct arpc_cport_clear_req req;
0680     int ret;
0681 
0682     req.cport_id = cpu_to_le16(cport_id);
0683     ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req),
0684             NULL, ES2_ARPC_CPORT_TIMEOUT);
0685     if (ret) {
0686         dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret);
0687         return ret;
0688     }
0689 
0690     return 0;
0691 }
0692 
0693 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
0694 {
0695     int retval;
0696     struct es2_ap_dev *es2 = hd_to_es2(hd);
0697     struct usb_device *udev = es2->usb_dev;
0698 
0699     retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0700                  GB_APB_REQUEST_LATENCY_TAG_EN,
0701                  USB_DIR_OUT | USB_TYPE_VENDOR |
0702                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
0703                  0, ES2_USB_CTRL_TIMEOUT);
0704 
0705     if (retval < 0)
0706         dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
0707             cport_id);
0708     return retval;
0709 }
0710 
0711 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
0712 {
0713     int retval;
0714     struct es2_ap_dev *es2 = hd_to_es2(hd);
0715     struct usb_device *udev = es2->usb_dev;
0716 
0717     retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0718                  GB_APB_REQUEST_LATENCY_TAG_DIS,
0719                  USB_DIR_OUT | USB_TYPE_VENDOR |
0720                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
0721                  0, ES2_USB_CTRL_TIMEOUT);
0722 
0723     if (retval < 0)
0724         dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
0725             cport_id);
0726     return retval;
0727 }
0728 
0729 static struct gb_hd_driver es2_driver = {
0730     .hd_priv_size           = sizeof(struct es2_ap_dev),
0731     .message_send           = message_send,
0732     .message_cancel         = message_cancel,
0733     .cport_allocate         = es2_cport_allocate,
0734     .cport_release          = es2_cport_release,
0735     .cport_enable           = cport_enable,
0736     .cport_connected        = es2_cport_connected,
0737     .cport_flush            = es2_cport_flush,
0738     .cport_shutdown         = es2_cport_shutdown,
0739     .cport_quiesce          = es2_cport_quiesce,
0740     .cport_clear            = es2_cport_clear,
0741     .latency_tag_enable     = latency_tag_enable,
0742     .latency_tag_disable        = latency_tag_disable,
0743     .output             = output,
0744 };
0745 
0746 /* Common function to report consistent warnings based on URB status */
0747 static int check_urb_status(struct urb *urb)
0748 {
0749     struct device *dev = &urb->dev->dev;
0750     int status = urb->status;
0751 
0752     switch (status) {
0753     case 0:
0754         return 0;
0755 
0756     case -EOVERFLOW:
0757         dev_err(dev, "%s: overflow actual length is %d\n",
0758             __func__, urb->actual_length);
0759         fallthrough;
0760     case -ECONNRESET:
0761     case -ENOENT:
0762     case -ESHUTDOWN:
0763     case -EILSEQ:
0764     case -EPROTO:
0765         /* device is gone, stop sending */
0766         return status;
0767     }
0768     dev_err(dev, "%s: unknown status %d\n", __func__, status);
0769 
0770     return -EAGAIN;
0771 }
0772 
0773 static void es2_destroy(struct es2_ap_dev *es2)
0774 {
0775     struct usb_device *udev;
0776     struct urb *urb;
0777     int i;
0778 
0779     debugfs_remove(es2->apb_log_enable_dentry);
0780     usb_log_disable(es2);
0781 
0782     /* Tear down everything! */
0783     for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
0784         urb = es2->cport_out_urb[i];
0785         usb_kill_urb(urb);
0786         usb_free_urb(urb);
0787         es2->cport_out_urb[i] = NULL;
0788         es2->cport_out_urb_busy[i] = false; /* just to be anal */
0789     }
0790 
0791     for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
0792         usb_free_urb(es2->arpc_urb[i]);
0793         kfree(es2->arpc_buffer[i]);
0794         es2->arpc_buffer[i] = NULL;
0795     }
0796 
0797     for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
0798         usb_free_urb(es2->cport_in.urb[i]);
0799         kfree(es2->cport_in.buffer[i]);
0800         es2->cport_in.buffer[i] = NULL;
0801     }
0802 
0803     /* release reserved CDSI0 and CDSI1 cports */
0804     gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
0805     gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
0806 
0807     udev = es2->usb_dev;
0808     gb_hd_put(es2->hd);
0809 
0810     usb_put_dev(udev);
0811 }
0812 
0813 static void cport_in_callback(struct urb *urb)
0814 {
0815     struct gb_host_device *hd = urb->context;
0816     struct device *dev = &urb->dev->dev;
0817     struct gb_operation_msg_hdr *header;
0818     int status = check_urb_status(urb);
0819     int retval;
0820     u16 cport_id;
0821 
0822     if (status) {
0823         if ((status == -EAGAIN) || (status == -EPROTO))
0824             goto exit;
0825 
0826         /* The urb is being unlinked */
0827         if (status == -ENOENT || status == -ESHUTDOWN)
0828             return;
0829 
0830         dev_err(dev, "urb cport in error %d (dropped)\n", status);
0831         return;
0832     }
0833 
0834     if (urb->actual_length < sizeof(*header)) {
0835         dev_err(dev, "short message received\n");
0836         goto exit;
0837     }
0838 
0839     /* Extract the CPort id, which is packed in the message header */
0840     header = urb->transfer_buffer;
0841     cport_id = gb_message_cport_unpack(header);
0842 
0843     if (cport_id_valid(hd, cport_id)) {
0844         greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
0845                   urb->actual_length);
0846     } else {
0847         dev_err(dev, "invalid cport id %u received\n", cport_id);
0848     }
0849 exit:
0850     /* put our urb back in the request pool */
0851     retval = usb_submit_urb(urb, GFP_ATOMIC);
0852     if (retval)
0853         dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
0854 }
0855 
0856 static void cport_out_callback(struct urb *urb)
0857 {
0858     struct gb_message *message = urb->context;
0859     struct gb_host_device *hd = message->operation->connection->hd;
0860     struct es2_ap_dev *es2 = hd_to_es2(hd);
0861     int status = check_urb_status(urb);
0862     unsigned long flags;
0863 
0864     gb_message_cport_clear(message->header);
0865 
0866     spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
0867     message->hcpriv = NULL;
0868     spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
0869 
0870     /*
0871      * Tell the submitter that the message send (attempt) is
0872      * complete, and report the status.
0873      */
0874     greybus_message_sent(hd, message, status);
0875 
0876     free_urb(es2, urb);
0877 }
0878 
0879 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
0880 {
0881     struct arpc *rpc;
0882 
0883     if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
0884         return NULL;
0885 
0886     rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
0887     if (!rpc)
0888         return NULL;
0889 
0890     INIT_LIST_HEAD(&rpc->list);
0891     rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
0892     if (!rpc->req)
0893         goto err_free_rpc;
0894 
0895     rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
0896     if (!rpc->resp)
0897         goto err_free_req;
0898 
0899     rpc->req->type = type;
0900     rpc->req->size = cpu_to_le16(sizeof(*rpc->req) + size);
0901     memcpy(rpc->req->data, payload, size);
0902 
0903     init_completion(&rpc->response_received);
0904 
0905     return rpc;
0906 
0907 err_free_req:
0908     kfree(rpc->req);
0909 err_free_rpc:
0910     kfree(rpc);
0911 
0912     return NULL;
0913 }
0914 
0915 static void arpc_free(struct arpc *rpc)
0916 {
0917     kfree(rpc->req);
0918     kfree(rpc->resp);
0919     kfree(rpc);
0920 }
0921 
0922 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
0923 {
0924     struct arpc *rpc;
0925 
0926     list_for_each_entry(rpc, &es2->arpcs, list) {
0927         if (rpc->req->id == id)
0928             return rpc;
0929     }
0930 
0931     return NULL;
0932 }
0933 
0934 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
0935 {
0936     rpc->active = true;
0937     rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
0938     list_add_tail(&rpc->list, &es2->arpcs);
0939 }
0940 
0941 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
0942 {
0943     if (rpc->active) {
0944         rpc->active = false;
0945         list_del(&rpc->list);
0946     }
0947 }
0948 
0949 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
0950 {
0951     struct usb_device *udev = es2->usb_dev;
0952     int retval;
0953 
0954     retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0955                  GB_APB_REQUEST_ARPC_RUN,
0956                  USB_DIR_OUT | USB_TYPE_VENDOR |
0957                  USB_RECIP_INTERFACE,
0958                  0, 0,
0959                  rpc->req, le16_to_cpu(rpc->req->size),
0960                  ES2_USB_CTRL_TIMEOUT);
0961     if (retval < 0) {
0962         dev_err(&udev->dev,
0963             "failed to send ARPC request %d: %d\n",
0964             rpc->req->type, retval);
0965         return retval;
0966     }
0967 
0968     return 0;
0969 }
0970 
0971 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
0972              size_t size, int *result, unsigned int timeout)
0973 {
0974     struct arpc *rpc;
0975     unsigned long flags;
0976     int retval;
0977 
0978     if (result)
0979         *result = 0;
0980 
0981     rpc = arpc_alloc(payload, size, type);
0982     if (!rpc)
0983         return -ENOMEM;
0984 
0985     spin_lock_irqsave(&es2->arpc_lock, flags);
0986     arpc_add(es2, rpc);
0987     spin_unlock_irqrestore(&es2->arpc_lock, flags);
0988 
0989     retval = arpc_send(es2, rpc, timeout);
0990     if (retval)
0991         goto out_arpc_del;
0992 
0993     retval = wait_for_completion_interruptible_timeout(
0994                         &rpc->response_received,
0995                         msecs_to_jiffies(timeout));
0996     if (retval <= 0) {
0997         if (!retval)
0998             retval = -ETIMEDOUT;
0999         goto out_arpc_del;
1000     }
1001 
1002     if (rpc->resp->result) {
1003         retval = -EREMOTEIO;
1004         if (result)
1005             *result = rpc->resp->result;
1006     } else {
1007         retval = 0;
1008     }
1009 
1010 out_arpc_del:
1011     spin_lock_irqsave(&es2->arpc_lock, flags);
1012     arpc_del(es2, rpc);
1013     spin_unlock_irqrestore(&es2->arpc_lock, flags);
1014     arpc_free(rpc);
1015 
1016     if (retval < 0 && retval != -EREMOTEIO) {
1017         dev_err(&es2->usb_dev->dev,
1018             "failed to execute ARPC: %d\n", retval);
1019     }
1020 
1021     return retval;
1022 }
1023 
1024 static void arpc_in_callback(struct urb *urb)
1025 {
1026     struct es2_ap_dev *es2 = urb->context;
1027     struct device *dev = &urb->dev->dev;
1028     int status = check_urb_status(urb);
1029     struct arpc *rpc;
1030     struct arpc_response_message *resp;
1031     unsigned long flags;
1032     int retval;
1033 
1034     if (status) {
1035         if ((status == -EAGAIN) || (status == -EPROTO))
1036             goto exit;
1037 
1038         /* The urb is being unlinked */
1039         if (status == -ENOENT || status == -ESHUTDOWN)
1040             return;
1041 
1042         dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1043         return;
1044     }
1045 
1046     if (urb->actual_length < sizeof(*resp)) {
1047         dev_err(dev, "short aprc response received\n");
1048         goto exit;
1049     }
1050 
1051     resp = urb->transfer_buffer;
1052     spin_lock_irqsave(&es2->arpc_lock, flags);
1053     rpc = arpc_find(es2, resp->id);
1054     if (!rpc) {
1055         dev_err(dev, "invalid arpc response id received: %u\n",
1056             le16_to_cpu(resp->id));
1057         spin_unlock_irqrestore(&es2->arpc_lock, flags);
1058         goto exit;
1059     }
1060 
1061     arpc_del(es2, rpc);
1062     memcpy(rpc->resp, resp, sizeof(*resp));
1063     complete(&rpc->response_received);
1064     spin_unlock_irqrestore(&es2->arpc_lock, flags);
1065 
1066 exit:
1067     /* put our urb back in the request pool */
1068     retval = usb_submit_urb(urb, GFP_ATOMIC);
1069     if (retval)
1070         dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1071 }
1072 
1073 #define APB1_LOG_MSG_SIZE   64
1074 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1075 {
1076     int retval;
1077 
1078     do {
1079         retval = usb_control_msg(es2->usb_dev,
1080                      usb_rcvctrlpipe(es2->usb_dev, 0),
1081                      GB_APB_REQUEST_LOG,
1082                      USB_DIR_IN | USB_TYPE_VENDOR |
1083                      USB_RECIP_INTERFACE,
1084                      0x00, 0x00,
1085                      buf,
1086                      APB1_LOG_MSG_SIZE,
1087                      ES2_USB_CTRL_TIMEOUT);
1088         if (retval > 0)
1089             kfifo_in(&es2->apb_log_fifo, buf, retval);
1090     } while (retval > 0);
1091 }
1092 
1093 static int apb_log_poll(void *data)
1094 {
1095     struct es2_ap_dev *es2 = data;
1096     char *buf;
1097 
1098     buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1099     if (!buf)
1100         return -ENOMEM;
1101 
1102     while (!kthread_should_stop()) {
1103         msleep(1000);
1104         apb_log_get(es2, buf);
1105     }
1106 
1107     kfree(buf);
1108 
1109     return 0;
1110 }
1111 
1112 static ssize_t apb_log_read(struct file *f, char __user *buf,
1113                 size_t count, loff_t *ppos)
1114 {
1115     struct es2_ap_dev *es2 = file_inode(f)->i_private;
1116     ssize_t ret;
1117     size_t copied;
1118     char *tmp_buf;
1119 
1120     if (count > APB1_LOG_SIZE)
1121         count = APB1_LOG_SIZE;
1122 
1123     tmp_buf = kmalloc(count, GFP_KERNEL);
1124     if (!tmp_buf)
1125         return -ENOMEM;
1126 
1127     copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1128     ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1129 
1130     kfree(tmp_buf);
1131 
1132     return ret;
1133 }
1134 
1135 static const struct file_operations apb_log_fops = {
1136     .read   = apb_log_read,
1137 };
1138 
1139 static void usb_log_enable(struct es2_ap_dev *es2)
1140 {
1141     if (!IS_ERR_OR_NULL(es2->apb_log_task))
1142         return;
1143 
1144     /* get log from APB1 */
1145     es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1146     if (IS_ERR(es2->apb_log_task))
1147         return;
1148     /* XXX We will need to rename this per APB */
1149     es2->apb_log_dentry = debugfs_create_file("apb_log", 0444,
1150                           gb_debugfs_get(), es2,
1151                           &apb_log_fops);
1152 }
1153 
1154 static void usb_log_disable(struct es2_ap_dev *es2)
1155 {
1156     if (IS_ERR_OR_NULL(es2->apb_log_task))
1157         return;
1158 
1159     debugfs_remove(es2->apb_log_dentry);
1160     es2->apb_log_dentry = NULL;
1161 
1162     kthread_stop(es2->apb_log_task);
1163     es2->apb_log_task = NULL;
1164 }
1165 
1166 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1167                    size_t count, loff_t *ppos)
1168 {
1169     struct es2_ap_dev *es2 = file_inode(f)->i_private;
1170     int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1171     char tmp_buf[3];
1172 
1173     sprintf(tmp_buf, "%d\n", enable);
1174     return simple_read_from_buffer(buf, count, ppos, tmp_buf, 2);
1175 }
1176 
1177 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1178                     size_t count, loff_t *ppos)
1179 {
1180     int enable;
1181     ssize_t retval;
1182     struct es2_ap_dev *es2 = file_inode(f)->i_private;
1183 
1184     retval = kstrtoint_from_user(buf, count, 10, &enable);
1185     if (retval)
1186         return retval;
1187 
1188     if (enable)
1189         usb_log_enable(es2);
1190     else
1191         usb_log_disable(es2);
1192 
1193     return count;
1194 }
1195 
1196 static const struct file_operations apb_log_enable_fops = {
1197     .read   = apb_log_enable_read,
1198     .write  = apb_log_enable_write,
1199 };
1200 
1201 static int apb_get_cport_count(struct usb_device *udev)
1202 {
1203     int retval;
1204     __le16 *cport_count;
1205 
1206     cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1207     if (!cport_count)
1208         return -ENOMEM;
1209 
1210     retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1211                  GB_APB_REQUEST_CPORT_COUNT,
1212                  USB_DIR_IN | USB_TYPE_VENDOR |
1213                  USB_RECIP_INTERFACE, 0, 0, cport_count,
1214                  sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
1215     if (retval != sizeof(*cport_count)) {
1216         dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1217             retval);
1218 
1219         if (retval >= 0)
1220             retval = -EIO;
1221 
1222         goto out;
1223     }
1224 
1225     retval = le16_to_cpu(*cport_count);
1226 
1227     /* We need to fit a CPort ID in one byte of a message header */
1228     if (retval > U8_MAX) {
1229         retval = U8_MAX;
1230         dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1231     }
1232 
1233 out:
1234     kfree(cport_count);
1235     return retval;
1236 }
1237 
1238 /*
1239  * The ES2 USB Bridge device has 15 endpoints
1240  * 1 Control - usual USB stuff + AP -> APBridgeA messages
1241  * 7 Bulk IN - CPort data in
1242  * 7 Bulk OUT - CPort data out
1243  */
1244 static int ap_probe(struct usb_interface *interface,
1245             const struct usb_device_id *id)
1246 {
1247     struct es2_ap_dev *es2;
1248     struct gb_host_device *hd;
1249     struct usb_device *udev;
1250     struct usb_host_interface *iface_desc;
1251     struct usb_endpoint_descriptor *endpoint;
1252     __u8 ep_addr;
1253     int retval;
1254     int i;
1255     int num_cports;
1256     bool bulk_out_found = false;
1257     bool bulk_in_found = false;
1258     bool arpc_in_found = false;
1259 
1260     udev = usb_get_dev(interface_to_usbdev(interface));
1261 
1262     num_cports = apb_get_cport_count(udev);
1263     if (num_cports < 0) {
1264         usb_put_dev(udev);
1265         dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1266             num_cports);
1267         return num_cports;
1268     }
1269 
1270     hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1271               num_cports);
1272     if (IS_ERR(hd)) {
1273         usb_put_dev(udev);
1274         return PTR_ERR(hd);
1275     }
1276 
1277     es2 = hd_to_es2(hd);
1278     es2->hd = hd;
1279     es2->usb_intf = interface;
1280     es2->usb_dev = udev;
1281     spin_lock_init(&es2->cport_out_urb_lock);
1282     INIT_KFIFO(es2->apb_log_fifo);
1283     usb_set_intfdata(interface, es2);
1284 
1285     /*
1286      * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1287      * dynamically.
1288      */
1289     retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1290     if (retval)
1291         goto error;
1292     retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1293     if (retval)
1294         goto error;
1295 
1296     /* find all bulk endpoints */
1297     iface_desc = interface->cur_altsetting;
1298     for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1299         endpoint = &iface_desc->endpoint[i].desc;
1300         ep_addr = endpoint->bEndpointAddress;
1301 
1302         if (usb_endpoint_is_bulk_in(endpoint)) {
1303             if (!bulk_in_found) {
1304                 es2->cport_in.endpoint = ep_addr;
1305                 bulk_in_found = true;
1306             } else if (!arpc_in_found) {
1307                 es2->arpc_endpoint_in = ep_addr;
1308                 arpc_in_found = true;
1309             } else {
1310                 dev_warn(&udev->dev,
1311                      "Unused bulk IN endpoint found: 0x%02x\n",
1312                      ep_addr);
1313             }
1314             continue;
1315         }
1316         if (usb_endpoint_is_bulk_out(endpoint)) {
1317             if (!bulk_out_found) {
1318                 es2->cport_out_endpoint = ep_addr;
1319                 bulk_out_found = true;
1320             } else {
1321                 dev_warn(&udev->dev,
1322                      "Unused bulk OUT endpoint found: 0x%02x\n",
1323                      ep_addr);
1324             }
1325             continue;
1326         }
1327         dev_warn(&udev->dev,
1328              "Unknown endpoint type found, address 0x%02x\n",
1329              ep_addr);
1330     }
1331     if (!bulk_in_found || !arpc_in_found || !bulk_out_found) {
1332         dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1333         retval = -ENODEV;
1334         goto error;
1335     }
1336 
1337     /* Allocate buffers for our cport in messages */
1338     for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1339         struct urb *urb;
1340         u8 *buffer;
1341 
1342         urb = usb_alloc_urb(0, GFP_KERNEL);
1343         if (!urb) {
1344             retval = -ENOMEM;
1345             goto error;
1346         }
1347         es2->cport_in.urb[i] = urb;
1348 
1349         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1350         if (!buffer) {
1351             retval = -ENOMEM;
1352             goto error;
1353         }
1354 
1355         usb_fill_bulk_urb(urb, udev,
1356                   usb_rcvbulkpipe(udev, es2->cport_in.endpoint),
1357                   buffer, ES2_GBUF_MSG_SIZE_MAX,
1358                   cport_in_callback, hd);
1359 
1360         es2->cport_in.buffer[i] = buffer;
1361     }
1362 
1363     /* Allocate buffers for ARPC in messages */
1364     for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1365         struct urb *urb;
1366         u8 *buffer;
1367 
1368         urb = usb_alloc_urb(0, GFP_KERNEL);
1369         if (!urb) {
1370             retval = -ENOMEM;
1371             goto error;
1372         }
1373         es2->arpc_urb[i] = urb;
1374 
1375         buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1376         if (!buffer) {
1377             retval = -ENOMEM;
1378             goto error;
1379         }
1380 
1381         usb_fill_bulk_urb(urb, udev,
1382                   usb_rcvbulkpipe(udev,
1383                           es2->arpc_endpoint_in),
1384                   buffer, ARPC_IN_SIZE_MAX,
1385                   arpc_in_callback, es2);
1386 
1387         es2->arpc_buffer[i] = buffer;
1388     }
1389 
1390     /* Allocate urbs for our CPort OUT messages */
1391     for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1392         struct urb *urb;
1393 
1394         urb = usb_alloc_urb(0, GFP_KERNEL);
1395         if (!urb) {
1396             retval = -ENOMEM;
1397             goto error;
1398         }
1399 
1400         es2->cport_out_urb[i] = urb;
1401         es2->cport_out_urb_busy[i] = false; /* just to be anal */
1402     }
1403 
1404     /* XXX We will need to rename this per APB */
1405     es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1406                              0644,
1407                              gb_debugfs_get(), es2,
1408                              &apb_log_enable_fops);
1409 
1410     INIT_LIST_HEAD(&es2->arpcs);
1411     spin_lock_init(&es2->arpc_lock);
1412 
1413     retval = es2_arpc_in_enable(es2);
1414     if (retval)
1415         goto error;
1416 
1417     retval = gb_hd_add(hd);
1418     if (retval)
1419         goto err_disable_arpc_in;
1420 
1421     retval = es2_cport_in_enable(es2, &es2->cport_in);
1422     if (retval)
1423         goto err_hd_del;
1424 
1425     return 0;
1426 
1427 err_hd_del:
1428     gb_hd_del(hd);
1429 err_disable_arpc_in:
1430     es2_arpc_in_disable(es2);
1431 error:
1432     es2_destroy(es2);
1433 
1434     return retval;
1435 }
1436 
1437 static void ap_disconnect(struct usb_interface *interface)
1438 {
1439     struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1440 
1441     gb_hd_del(es2->hd);
1442 
1443     es2_cport_in_disable(es2, &es2->cport_in);
1444     es2_arpc_in_disable(es2);
1445 
1446     es2_destroy(es2);
1447 }
1448 
1449 static struct usb_driver es2_ap_driver = {
1450     .name =     "es2_ap_driver",
1451     .probe =    ap_probe,
1452     .disconnect =   ap_disconnect,
1453     .id_table = id_table,
1454     .soft_unbind =  1,
1455 };
1456 
1457 module_usb_driver(es2_ap_driver);
1458 
1459 MODULE_LICENSE("GPL v2");
1460 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");