2 * Greybus "AP" USB driver for "ES2" controller chips
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
9 #include <linux/kthread.h>
10 #include <linux/sizes.h>
11 #include <linux/usb.h>
12 #include <linux/kfifo.h>
13 #include <linux/debugfs.h>
14 #include <asm/unaligned.h>
18 #include "kernel_ver.h"
19 #include "connection.h"
20 #include "greybus_trace.h"
22 /* Memory sizes for the buffers sent to/from the ES2 controller */
23 #define ES2_GBUF_MSG_SIZE_MAX 2048
25 static const struct usb_device_id id_table[] = {
26 { USB_DEVICE(0x18d1, 0x1eaf) },
29 MODULE_DEVICE_TABLE(usb, id_table);
31 #define APB1_LOG_SIZE SZ_16K
33 /* Number of bulk in and bulk out couple */
37 * Number of CPort IN urbs in flight at any point in time.
38 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
41 #define NUM_CPORT_IN_URB 4
43 /* Number of CPort OUT urbs in flight at any point in time.
44 * Adjust if we get messages saying we are out of urbs in the system log.
46 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
48 /* vendor request APB1 log */
49 #define REQUEST_LOG 0x02
51 /* vendor request to map a cport to bulk in and bulk out endpoints */
52 #define REQUEST_EP_MAPPING 0x03
54 /* vendor request to get the number of cports available */
55 #define REQUEST_CPORT_COUNT 0x04
57 /* vendor request to reset a cport state */
58 #define REQUEST_RESET_CPORT 0x05
60 /* vendor request to time the latency of messages on a given cport */
61 #define REQUEST_LATENCY_TAG_EN 0x06
62 #define REQUEST_LATENCY_TAG_DIS 0x07
64 /* vendor request to control the CSI transmitter */
65 #define REQUEST_CSI_TX_CONTROL 0x08
68 * @endpoint: bulk in endpoint for CPort data
69 * @urb: array of urbs for the CPort in messages
70 * @buffer: array of buffers for the @cport_in_urb urbs
74 struct urb *urb[NUM_CPORT_IN_URB];
75 u8 *buffer[NUM_CPORT_IN_URB];
79 * @endpoint: bulk out endpoint for CPort data
81 struct es2_cport_out {
86 * es2_ap_dev - ES2 USB Bridge to AP structure
87 * @usb_dev: pointer to the USB device we are.
88 * @usb_intf: pointer to the USB interface we are bound to.
89 * @hd: pointer to our gb_host_device structure
91 * @cport_in: endpoint, urbs and buffer for cport in messages
92 * @cport_out: endpoint for for cport out messages
93 * @cport_out_urb: array of urbs for the CPort out messages
94 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
96 * @cport_out_urb_cancelled: array of flags indicating whether the
97 * corresponding @cport_out_urb is being cancelled
98 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
100 * @apb_log_task: task pointer for logging thread
101 * @apb_log_dentry: file system entry for the log file interface
102 * @apb_log_enable_dentry: file system entry for enabling logging
103 * @apb_log_fifo: kernel FIFO to carry logged data
106 struct usb_device *usb_dev;
107 struct usb_interface *usb_intf;
108 struct gb_host_device *hd;
110 struct es2_cport_in cport_in[NUM_BULKS];
111 struct es2_cport_out cport_out[NUM_BULKS];
112 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
113 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
114 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
115 spinlock_t cport_out_urb_lock;
119 struct task_struct *apb_log_task;
120 struct dentry *apb_log_dentry;
121 struct dentry *apb_log_enable_dentry;
122 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
126 * cport_to_ep - information about cport to endpoints mapping
127 * @cport_id: the id of cport to map to endpoints
128 * @endpoint_in: the endpoint number to use for in transfer
129 * @endpoint_out: he endpoint number to use for out transfer
137 struct es2_ap_csi_config_request {
143 } __attribute__((__packed__));
145 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
147 return (struct es2_ap_dev *)&hd->hd_priv;
150 static void cport_out_callback(struct urb *urb);
151 static void usb_log_enable(struct es2_ap_dev *es2);
152 static void usb_log_disable(struct es2_ap_dev *es2);
154 /* Get the endpoints pair mapped to the cport */
155 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
157 if (cport_id >= es2->hd->num_cports)
159 return es2->cport_to_ep[cport_id];
162 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
164 /* Disable for now until we work all of this out to keep a warning-free build */
166 /* Test if the endpoints pair is already mapped to a cport */
167 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
171 for (i = 0; i < es2->hd->num_cports; i++) {
172 if (es2->cport_to_ep[i] == ep_pair)
178 /* Configure the endpoint mapping and send the request to APBridge */
179 static int map_cport_to_ep(struct es2_ap_dev *es2,
180 u16 cport_id, int ep_pair)
183 struct cport_to_ep *cport_to_ep;
185 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
187 if (cport_id >= es2->hd->num_cports)
189 if (ep_pair && ep_pair_in_use(es2, ep_pair))
192 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
196 es2->cport_to_ep[cport_id] = ep_pair;
197 cport_to_ep->cport_id = cpu_to_le16(cport_id);
198 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
199 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
201 retval = usb_control_msg(es2->usb_dev,
202 usb_sndctrlpipe(es2->usb_dev, 0),
204 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
207 sizeof(*cport_to_ep),
209 if (retval == sizeof(*cport_to_ep))
216 /* Unmap a cport: use the muxed endpoints pair */
217 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
219 return map_cport_to_ep(es2, cport_id, 0);
223 int es2_ap_csi_setup(struct gb_host_device *hd, bool start,
224 struct es2_ap_csi_config *cfg)
226 struct es2_ap_csi_config_request cfg_req;
227 struct es2_ap_dev *es2 = hd_to_es2(hd);
228 struct usb_device *udev = es2->usb_dev;
231 cfg_req.csi_id = cfg->csi_id;
234 cfg_req.clock_mode = cfg->clock_mode;
235 cfg_req.num_lanes = cfg->num_lanes;
237 cfg_req.bus_freq = cpu_to_le32(cfg->bus_freq);
239 cfg_req.clock_mode = 0;
240 cfg_req.num_lanes = 0;
242 cfg_req.bus_freq = 0;
245 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
246 REQUEST_CSI_TX_CONTROL,
247 USB_DIR_OUT | USB_TYPE_VENDOR |
248 USB_RECIP_INTERFACE, 0, 0, &cfg_req,
249 sizeof(cfg_req), ES2_TIMEOUT);
251 dev_err(&udev->dev, "failed to setup csi: %d\n", retval);
257 EXPORT_SYMBOL_GPL(es2_ap_csi_setup);
259 static int es2_cport_in_enable(struct es2_ap_dev *es2,
260 struct es2_cport_in *cport_in)
266 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
267 urb = cport_in->urb[i];
269 ret = usb_submit_urb(urb, GFP_KERNEL);
271 dev_err(&es2->usb_dev->dev,
272 "failed to submit in-urb: %d\n", ret);
280 for (--i; i >= 0; --i) {
281 urb = cport_in->urb[i];
288 static void es2_cport_in_disable(struct es2_ap_dev *es2,
289 struct es2_cport_in *cport_in)
294 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
295 urb = cport_in->urb[i];
300 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
302 struct urb *urb = NULL;
306 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
308 /* Look in our pool of allocated urbs first, as that's the "fastest" */
309 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
310 if (es2->cport_out_urb_busy[i] == false &&
311 es2->cport_out_urb_cancelled[i] == false) {
312 es2->cport_out_urb_busy[i] = true;
313 urb = es2->cport_out_urb[i];
317 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
322 * Crap, pool is empty, complain to the syslog and go allocate one
323 * dynamically as we have to succeed.
325 dev_dbg(&es2->usb_dev->dev,
326 "No free CPort OUT urbs, having to dynamically allocate one!\n");
327 return usb_alloc_urb(0, gfp_mask);
330 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
335 * See if this was an urb in our pool, if so mark it "free", otherwise
336 * we need to free it ourselves.
338 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
339 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
340 if (urb == es2->cport_out_urb[i]) {
341 es2->cport_out_urb_busy[i] = false;
346 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
348 /* If urb is not NULL, then we need to free this urb */
353 * We (ab)use the operation-message header pad bytes to transfer the
354 * cport id in order to minimise overhead.
357 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
359 header->pad[0] = cport_id;
362 /* Clear the pad bytes used for the CPort id */
363 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
368 /* Extract the CPort id packed into the header, and clear it */
369 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
371 u16 cport_id = header->pad[0];
373 gb_message_cport_clear(header);
379 * Returns zero if the message was successfully queued, or a negative errno
382 static int message_send(struct gb_host_device *hd, u16 cport_id,
383 struct gb_message *message, gfp_t gfp_mask)
385 struct es2_ap_dev *es2 = hd_to_es2(hd);
386 struct usb_device *udev = es2->usb_dev;
394 * The data actually transferred will include an indication
395 * of where the data should be sent. Do one last check of
396 * the target CPort id before filling it in.
398 if (!cport_id_valid(hd, cport_id)) {
399 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
403 /* Find a free urb */
404 urb = next_free_urb(es2, gfp_mask);
408 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
409 message->hcpriv = urb;
410 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
412 /* Pack the cport id into the message header */
413 gb_message_cport_pack(message->header, cport_id);
415 buffer_size = sizeof(*message->header) + message->payload_size;
417 ep_pair = cport_to_ep_pair(es2, cport_id);
418 usb_fill_bulk_urb(urb, udev,
419 usb_sndbulkpipe(udev,
420 es2->cport_out[ep_pair].endpoint),
421 message->buffer, buffer_size,
422 cport_out_callback, message);
423 urb->transfer_flags |= URB_ZERO_PACKET;
424 trace_gb_host_device_send(hd, cport_id, buffer_size);
425 retval = usb_submit_urb(urb, gfp_mask);
427 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
429 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
430 message->hcpriv = NULL;
431 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
434 gb_message_cport_clear(message->header);
443 * Can not be called in atomic context.
445 static void message_cancel(struct gb_message *message)
447 struct gb_host_device *hd = message->operation->connection->hd;
448 struct es2_ap_dev *es2 = hd_to_es2(hd);
454 spin_lock_irq(&es2->cport_out_urb_lock);
455 urb = message->hcpriv;
457 /* Prevent dynamically allocated urb from being deallocated. */
460 /* Prevent pre-allocated urb from being reused. */
461 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
462 if (urb == es2->cport_out_urb[i]) {
463 es2->cport_out_urb_cancelled[i] = true;
467 spin_unlock_irq(&es2->cport_out_urb_lock);
471 if (i < NUM_CPORT_OUT_URB) {
472 spin_lock_irq(&es2->cport_out_urb_lock);
473 es2->cport_out_urb_cancelled[i] = false;
474 spin_unlock_irq(&es2->cport_out_urb_lock);
480 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
482 struct es2_ap_dev *es2 = hd_to_es2(hd);
483 struct usb_device *udev = es2->usb_dev;
486 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
488 USB_DIR_OUT | USB_TYPE_VENDOR |
489 USB_RECIP_INTERFACE, cport_id, 0,
490 NULL, 0, ES2_TIMEOUT);
492 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
500 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
504 if (cport_id != GB_SVC_CPORT_ID) {
505 retval = cport_reset(hd, cport_id);
513 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
516 struct es2_ap_dev *es2 = hd_to_es2(hd);
517 struct usb_device *udev = es2->usb_dev;
519 if (!cport_id_valid(hd, cport_id)) {
520 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
524 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
525 REQUEST_LATENCY_TAG_EN,
526 USB_DIR_OUT | USB_TYPE_VENDOR |
527 USB_RECIP_INTERFACE, cport_id, 0, NULL,
531 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
536 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
539 struct es2_ap_dev *es2 = hd_to_es2(hd);
540 struct usb_device *udev = es2->usb_dev;
542 if (!cport_id_valid(hd, cport_id)) {
543 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
547 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
548 REQUEST_LATENCY_TAG_DIS,
549 USB_DIR_OUT | USB_TYPE_VENDOR |
550 USB_RECIP_INTERFACE, cport_id, 0, NULL,
554 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
559 static struct gb_hd_driver es2_driver = {
560 .hd_priv_size = sizeof(struct es2_ap_dev),
561 .message_send = message_send,
562 .message_cancel = message_cancel,
563 .cport_enable = cport_enable,
564 .latency_tag_enable = latency_tag_enable,
565 .latency_tag_disable = latency_tag_disable,
568 /* Common function to report consistent warnings based on URB status */
569 static int check_urb_status(struct urb *urb)
571 struct device *dev = &urb->dev->dev;
572 int status = urb->status;
579 dev_err(dev, "%s: overflow actual length is %d\n",
580 __func__, urb->actual_length);
586 /* device is gone, stop sending */
589 dev_err(dev, "%s: unknown status %d\n", __func__, status);
594 static void es2_destroy(struct es2_ap_dev *es2)
596 struct usb_device *udev;
600 debugfs_remove(es2->apb_log_enable_dentry);
601 usb_log_disable(es2);
603 /* Tear down everything! */
604 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
605 struct urb *urb = es2->cport_out_urb[i];
611 es2->cport_out_urb[i] = NULL;
612 es2->cport_out_urb_busy[i] = false; /* just to be anal */
615 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
616 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
618 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
619 struct urb *urb = cport_in->urb[i];
624 kfree(cport_in->buffer[i]);
625 cport_in->buffer[i] = NULL;
629 kfree(es2->cport_to_ep);
637 static void ap_disconnect(struct usb_interface *interface)
639 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
642 for (i = 0; i < NUM_BULKS; ++i)
643 es2_cport_in_disable(es2, &es2->cport_in[i]);
650 static void cport_in_callback(struct urb *urb)
652 struct gb_host_device *hd = urb->context;
653 struct device *dev = &urb->dev->dev;
654 struct gb_operation_msg_hdr *header;
655 int status = check_urb_status(urb);
660 if ((status == -EAGAIN) || (status == -EPROTO))
662 dev_err(dev, "urb cport in error %d (dropped)\n", status);
666 if (urb->actual_length < sizeof(*header)) {
667 dev_err(dev, "short message received\n");
671 /* Extract the CPort id, which is packed in the message header */
672 header = urb->transfer_buffer;
673 cport_id = gb_message_cport_unpack(header);
675 if (cport_id_valid(hd, cport_id)) {
676 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
677 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
680 dev_err(dev, "invalid cport id %u received\n", cport_id);
683 /* put our urb back in the request pool */
684 retval = usb_submit_urb(urb, GFP_ATOMIC);
686 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
689 static void cport_out_callback(struct urb *urb)
691 struct gb_message *message = urb->context;
692 struct gb_host_device *hd = message->operation->connection->hd;
693 struct es2_ap_dev *es2 = hd_to_es2(hd);
694 int status = check_urb_status(urb);
697 gb_message_cport_clear(message->header);
699 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
700 message->hcpriv = NULL;
701 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
704 * Tell the submitter that the message send (attempt) is
705 * complete, and report the status.
707 greybus_message_sent(hd, message, status);
712 #define APB1_LOG_MSG_SIZE 64
713 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
717 /* SVC messages go down our control pipe */
719 retval = usb_control_msg(es2->usb_dev,
720 usb_rcvctrlpipe(es2->usb_dev, 0),
722 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
728 kfifo_in(&es2->apb_log_fifo, buf, retval);
729 } while (retval > 0);
732 static int apb_log_poll(void *data)
734 struct es2_ap_dev *es2 = data;
737 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
741 while (!kthread_should_stop()) {
743 apb_log_get(es2, buf);
751 static ssize_t apb_log_read(struct file *f, char __user *buf,
752 size_t count, loff_t *ppos)
754 struct es2_ap_dev *es2 = f->f_inode->i_private;
759 if (count > APB1_LOG_SIZE)
760 count = APB1_LOG_SIZE;
762 tmp_buf = kmalloc(count, GFP_KERNEL);
766 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
767 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
774 static const struct file_operations apb_log_fops = {
775 .read = apb_log_read,
778 static void usb_log_enable(struct es2_ap_dev *es2)
780 if (!IS_ERR_OR_NULL(es2->apb_log_task))
783 /* get log from APB1 */
784 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
785 if (IS_ERR(es2->apb_log_task))
787 /* XXX We will need to rename this per APB */
788 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
789 gb_debugfs_get(), NULL,
793 static void usb_log_disable(struct es2_ap_dev *es2)
795 if (IS_ERR_OR_NULL(es2->apb_log_task))
798 debugfs_remove(es2->apb_log_dentry);
799 es2->apb_log_dentry = NULL;
801 kthread_stop(es2->apb_log_task);
802 es2->apb_log_task = NULL;
805 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
806 size_t count, loff_t *ppos)
808 struct es2_ap_dev *es2 = f->f_inode->i_private;
809 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
812 sprintf(tmp_buf, "%d\n", enable);
813 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
816 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
817 size_t count, loff_t *ppos)
821 struct es2_ap_dev *es2 = f->f_inode->i_private;
823 retval = kstrtoint_from_user(buf, count, 10, &enable);
830 usb_log_disable(es2);
835 static const struct file_operations apb_log_enable_fops = {
836 .read = apb_log_enable_read,
837 .write = apb_log_enable_write,
840 static int apb_get_cport_count(struct usb_device *udev)
845 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
849 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
851 USB_DIR_IN | USB_TYPE_VENDOR |
852 USB_RECIP_INTERFACE, 0, 0, cport_count,
853 sizeof(*cport_count), ES2_TIMEOUT);
855 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
860 retval = le16_to_cpu(*cport_count);
862 /* We need to fit a CPort ID in one byte of a message header */
863 if (retval > U8_MAX) {
865 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
874 * The ES2 USB Bridge device has 15 endpoints
875 * 1 Control - usual USB stuff + AP -> APBridgeA messages
876 * 7 Bulk IN - CPort data in
877 * 7 Bulk OUT - CPort data out
879 static int ap_probe(struct usb_interface *interface,
880 const struct usb_device_id *id)
882 struct es2_ap_dev *es2;
883 struct gb_host_device *hd;
884 struct usb_device *udev;
885 struct usb_host_interface *iface_desc;
886 struct usb_endpoint_descriptor *endpoint;
889 int retval = -ENOMEM;
894 udev = usb_get_dev(interface_to_usbdev(interface));
896 num_cports = apb_get_cport_count(udev);
897 if (num_cports < 0) {
899 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
904 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
912 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
913 * won't be allocated dynamically.
916 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
917 } while (cport_id > 0);
921 es2->usb_intf = interface;
923 spin_lock_init(&es2->cport_out_urb_lock);
924 INIT_KFIFO(es2->apb_log_fifo);
925 usb_set_intfdata(interface, es2);
927 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
929 if (!es2->cport_to_ep) {
934 /* find all bulk endpoints */
935 iface_desc = interface->cur_altsetting;
936 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
937 endpoint = &iface_desc->endpoint[i].desc;
939 if (usb_endpoint_is_bulk_in(endpoint)) {
940 es2->cport_in[bulk_in++].endpoint =
941 endpoint->bEndpointAddress;
942 } else if (usb_endpoint_is_bulk_out(endpoint)) {
943 es2->cport_out[bulk_out++].endpoint =
944 endpoint->bEndpointAddress;
947 "Unknown endpoint type found, address 0x%02x\n",
948 endpoint->bEndpointAddress);
951 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
952 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
956 /* Allocate buffers for our cport in messages */
957 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
958 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
960 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
964 urb = usb_alloc_urb(0, GFP_KERNEL);
967 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
971 usb_fill_bulk_urb(urb, udev,
972 usb_rcvbulkpipe(udev,
974 buffer, ES2_GBUF_MSG_SIZE_MAX,
975 cport_in_callback, hd);
976 cport_in->urb[i] = urb;
977 cport_in->buffer[i] = buffer;
981 /* Allocate urbs for our CPort OUT messages */
982 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
985 urb = usb_alloc_urb(0, GFP_KERNEL);
989 es2->cport_out_urb[i] = urb;
990 es2->cport_out_urb_busy[i] = false; /* just to be anal */
993 /* XXX We will need to rename this per APB */
994 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
996 gb_debugfs_get(), es2,
997 &apb_log_enable_fops);
999 retval = gb_hd_add(hd);
1003 for (i = 0; i < NUM_BULKS; ++i) {
1004 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1006 goto err_disable_cport_in;
1011 err_disable_cport_in:
1012 for (--i; i >= 0; --i)
1013 es2_cport_in_disable(es2, &es2->cport_in[i]);
1021 static struct usb_driver es2_ap_driver = {
1022 .name = "es2_ap_driver",
1024 .disconnect = ap_disconnect,
1025 .id_table = id_table,
1028 module_usb_driver(es2_ap_driver);
1030 MODULE_LICENSE("GPL v2");
1031 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");