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>
17 #include "kernel_ver.h"
18 #include "connection.h"
19 #include "greybus_trace.h"
21 /* Memory sizes for the buffers sent to/from the ES2 controller */
22 #define ES2_GBUF_MSG_SIZE_MAX 2048
24 static const struct usb_device_id id_table[] = {
25 { USB_DEVICE(0x18d1, 0x1eaf) },
28 MODULE_DEVICE_TABLE(usb, id_table);
30 #define APB1_LOG_SIZE SZ_16K
32 /* Number of bulk in and bulk out couple */
36 * Number of CPort IN urbs in flight at any point in time.
37 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
40 #define NUM_CPORT_IN_URB 4
42 /* Number of CPort OUT urbs in flight at any point in time.
43 * Adjust if we get messages saying we are out of urbs in the system log.
45 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
48 * @endpoint: bulk in endpoint for CPort data
49 * @urb: array of urbs for the CPort in messages
50 * @buffer: array of buffers for the @cport_in_urb urbs
54 struct urb *urb[NUM_CPORT_IN_URB];
55 u8 *buffer[NUM_CPORT_IN_URB];
59 * @endpoint: bulk out endpoint for CPort data
61 struct es2_cport_out {
66 * es2_ap_dev - ES2 USB Bridge to AP structure
67 * @usb_dev: pointer to the USB device we are.
68 * @usb_intf: pointer to the USB interface we are bound to.
69 * @hd: pointer to our gb_host_device structure
71 * @cport_in: endpoint, urbs and buffer for cport in messages
72 * @cport_out: endpoint for for cport out messages
73 * @cport_out_urb: array of urbs for the CPort out messages
74 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
76 * @cport_out_urb_cancelled: array of flags indicating whether the
77 * corresponding @cport_out_urb is being cancelled
78 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
80 * @apb_log_task: task pointer for logging thread
81 * @apb_log_dentry: file system entry for the log file interface
82 * @apb_log_enable_dentry: file system entry for enabling logging
83 * @apb_log_fifo: kernel FIFO to carry logged data
86 struct usb_device *usb_dev;
87 struct usb_interface *usb_intf;
88 struct gb_host_device *hd;
90 struct es2_cport_in cport_in[NUM_BULKS];
91 struct es2_cport_out cport_out[NUM_BULKS];
92 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
93 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
94 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
95 spinlock_t cport_out_urb_lock;
99 struct task_struct *apb_log_task;
100 struct dentry *apb_log_dentry;
101 struct dentry *apb_log_enable_dentry;
102 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
106 * cport_to_ep - information about cport to endpoints mapping
107 * @cport_id: the id of cport to map to endpoints
108 * @endpoint_in: the endpoint number to use for in transfer
109 * @endpoint_out: he endpoint number to use for out transfer
117 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
119 return (struct es2_ap_dev *)&hd->hd_priv;
122 static void cport_out_callback(struct urb *urb);
123 static void usb_log_enable(struct es2_ap_dev *es2);
124 static void usb_log_disable(struct es2_ap_dev *es2);
126 /* Get the endpoints pair mapped to the cport */
127 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
129 if (cport_id >= es2->hd->num_cports)
131 return es2->cport_to_ep[cport_id];
134 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
136 /* Disable for now until we work all of this out to keep a warning-free build */
138 /* Test if the endpoints pair is already mapped to a cport */
139 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
143 for (i = 0; i < es2->hd->num_cports; i++) {
144 if (es2->cport_to_ep[i] == ep_pair)
150 /* Configure the endpoint mapping and send the request to APBridge */
151 static int map_cport_to_ep(struct es2_ap_dev *es2,
152 u16 cport_id, int ep_pair)
155 struct cport_to_ep *cport_to_ep;
157 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
159 if (cport_id >= es2->hd->num_cports)
161 if (ep_pair && ep_pair_in_use(es2, ep_pair))
164 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
168 es2->cport_to_ep[cport_id] = ep_pair;
169 cport_to_ep->cport_id = cpu_to_le16(cport_id);
170 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
171 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
173 retval = usb_control_msg(es2->usb_dev,
174 usb_sndctrlpipe(es2->usb_dev, 0),
175 GB_APB_REQUEST_EP_MAPPING,
176 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
179 sizeof(*cport_to_ep),
181 if (retval == sizeof(*cport_to_ep))
188 /* Unmap a cport: use the muxed endpoints pair */
189 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
191 return map_cport_to_ep(es2, cport_id, 0);
195 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
197 struct usb_device *udev = es2->usb_dev;
201 data = kmalloc(size, GFP_KERNEL);
204 memcpy(data, req, size);
206 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
208 USB_DIR_OUT | USB_TYPE_VENDOR |
210 0, 0, data, size, ES2_TIMEOUT);
212 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
220 static void ap_urb_complete(struct urb *urb)
222 struct usb_ctrlrequest *dr = urb->context;
228 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
230 struct usb_device *udev = es2->usb_dev;
232 struct usb_ctrlrequest *dr;
236 urb = usb_alloc_urb(0, GFP_ATOMIC);
240 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
246 buf = (u8 *)dr + sizeof(*dr);
247 memcpy(buf, req, size);
250 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
253 dr->wLength = cpu_to_le16(size);
255 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
256 (unsigned char *)dr, buf, size,
257 ap_urb_complete, dr);
258 retval = usb_submit_urb(urb, GFP_ATOMIC);
266 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
269 struct es2_ap_dev *es2 = hd_to_es2(hd);
272 return output_async(es2, req, size, cmd);
274 return output_sync(es2, req, size, cmd);
277 static int es2_cport_in_enable(struct es2_ap_dev *es2,
278 struct es2_cport_in *cport_in)
284 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
285 urb = cport_in->urb[i];
287 ret = usb_submit_urb(urb, GFP_KERNEL);
289 dev_err(&es2->usb_dev->dev,
290 "failed to submit in-urb: %d\n", ret);
298 for (--i; i >= 0; --i) {
299 urb = cport_in->urb[i];
306 static void es2_cport_in_disable(struct es2_ap_dev *es2,
307 struct es2_cport_in *cport_in)
312 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
313 urb = cport_in->urb[i];
318 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
320 struct urb *urb = NULL;
324 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
326 /* Look in our pool of allocated urbs first, as that's the "fastest" */
327 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
328 if (es2->cport_out_urb_busy[i] == false &&
329 es2->cport_out_urb_cancelled[i] == false) {
330 es2->cport_out_urb_busy[i] = true;
331 urb = es2->cport_out_urb[i];
335 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
340 * Crap, pool is empty, complain to the syslog and go allocate one
341 * dynamically as we have to succeed.
343 dev_dbg(&es2->usb_dev->dev,
344 "No free CPort OUT urbs, having to dynamically allocate one!\n");
345 return usb_alloc_urb(0, gfp_mask);
348 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
353 * See if this was an urb in our pool, if so mark it "free", otherwise
354 * we need to free it ourselves.
356 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
357 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
358 if (urb == es2->cport_out_urb[i]) {
359 es2->cport_out_urb_busy[i] = false;
364 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
366 /* If urb is not NULL, then we need to free this urb */
371 * We (ab)use the operation-message header pad bytes to transfer the
372 * cport id in order to minimise overhead.
375 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
377 header->pad[0] = cport_id;
380 /* Clear the pad bytes used for the CPort id */
381 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
386 /* Extract the CPort id packed into the header, and clear it */
387 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
389 u16 cport_id = header->pad[0];
391 gb_message_cport_clear(header);
397 * Returns zero if the message was successfully queued, or a negative errno
400 static int message_send(struct gb_host_device *hd, u16 cport_id,
401 struct gb_message *message, gfp_t gfp_mask)
403 struct es2_ap_dev *es2 = hd_to_es2(hd);
404 struct usb_device *udev = es2->usb_dev;
412 * The data actually transferred will include an indication
413 * of where the data should be sent. Do one last check of
414 * the target CPort id before filling it in.
416 if (!cport_id_valid(hd, cport_id)) {
417 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
421 /* Find a free urb */
422 urb = next_free_urb(es2, gfp_mask);
426 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
427 message->hcpriv = urb;
428 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
430 /* Pack the cport id into the message header */
431 gb_message_cport_pack(message->header, cport_id);
433 buffer_size = sizeof(*message->header) + message->payload_size;
435 ep_pair = cport_to_ep_pair(es2, cport_id);
436 usb_fill_bulk_urb(urb, udev,
437 usb_sndbulkpipe(udev,
438 es2->cport_out[ep_pair].endpoint),
439 message->buffer, buffer_size,
440 cport_out_callback, message);
441 urb->transfer_flags |= URB_ZERO_PACKET;
442 trace_gb_host_device_send(hd, cport_id, buffer_size);
443 retval = usb_submit_urb(urb, gfp_mask);
445 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
447 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
448 message->hcpriv = NULL;
449 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
452 gb_message_cport_clear(message->header);
461 * Can not be called in atomic context.
463 static void message_cancel(struct gb_message *message)
465 struct gb_host_device *hd = message->operation->connection->hd;
466 struct es2_ap_dev *es2 = hd_to_es2(hd);
472 spin_lock_irq(&es2->cport_out_urb_lock);
473 urb = message->hcpriv;
475 /* Prevent dynamically allocated urb from being deallocated. */
478 /* Prevent pre-allocated urb from being reused. */
479 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
480 if (urb == es2->cport_out_urb[i]) {
481 es2->cport_out_urb_cancelled[i] = true;
485 spin_unlock_irq(&es2->cport_out_urb_lock);
489 if (i < NUM_CPORT_OUT_URB) {
490 spin_lock_irq(&es2->cport_out_urb_lock);
491 es2->cport_out_urb_cancelled[i] = false;
492 spin_unlock_irq(&es2->cport_out_urb_lock);
498 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
500 struct es2_ap_dev *es2 = hd_to_es2(hd);
501 struct usb_device *udev = es2->usb_dev;
504 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
505 GB_APB_REQUEST_RESET_CPORT,
506 USB_DIR_OUT | USB_TYPE_VENDOR |
507 USB_RECIP_INTERFACE, cport_id, 0,
508 NULL, 0, ES2_TIMEOUT);
510 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
518 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
522 if (cport_id != GB_SVC_CPORT_ID) {
523 retval = cport_reset(hd, cport_id);
531 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
534 struct es2_ap_dev *es2 = hd_to_es2(hd);
535 struct usb_device *udev = es2->usb_dev;
537 if (!cport_id_valid(hd, cport_id)) {
538 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
542 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
543 GB_APB_REQUEST_LATENCY_TAG_EN,
544 USB_DIR_OUT | USB_TYPE_VENDOR |
545 USB_RECIP_INTERFACE, cport_id, 0, NULL,
549 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
554 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
557 struct es2_ap_dev *es2 = hd_to_es2(hd);
558 struct usb_device *udev = es2->usb_dev;
560 if (!cport_id_valid(hd, cport_id)) {
561 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
565 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
566 GB_APB_REQUEST_LATENCY_TAG_DIS,
567 USB_DIR_OUT | USB_TYPE_VENDOR |
568 USB_RECIP_INTERFACE, cport_id, 0, NULL,
572 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
577 static struct gb_hd_driver es2_driver = {
578 .hd_priv_size = sizeof(struct es2_ap_dev),
579 .message_send = message_send,
580 .message_cancel = message_cancel,
581 .cport_enable = cport_enable,
582 .latency_tag_enable = latency_tag_enable,
583 .latency_tag_disable = latency_tag_disable,
587 /* Common function to report consistent warnings based on URB status */
588 static int check_urb_status(struct urb *urb)
590 struct device *dev = &urb->dev->dev;
591 int status = urb->status;
598 dev_err(dev, "%s: overflow actual length is %d\n",
599 __func__, urb->actual_length);
605 /* device is gone, stop sending */
608 dev_err(dev, "%s: unknown status %d\n", __func__, status);
613 static void es2_destroy(struct es2_ap_dev *es2)
615 struct usb_device *udev;
619 debugfs_remove(es2->apb_log_enable_dentry);
620 usb_log_disable(es2);
622 /* Tear down everything! */
623 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
624 struct urb *urb = es2->cport_out_urb[i];
630 es2->cport_out_urb[i] = NULL;
631 es2->cport_out_urb_busy[i] = false; /* just to be anal */
634 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
635 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
637 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
638 struct urb *urb = cport_in->urb[i];
643 kfree(cport_in->buffer[i]);
644 cport_in->buffer[i] = NULL;
648 kfree(es2->cport_to_ep);
656 static void ap_disconnect(struct usb_interface *interface)
658 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
661 for (i = 0; i < NUM_BULKS; ++i)
662 es2_cport_in_disable(es2, &es2->cport_in[i]);
669 static void cport_in_callback(struct urb *urb)
671 struct gb_host_device *hd = urb->context;
672 struct device *dev = &urb->dev->dev;
673 struct gb_operation_msg_hdr *header;
674 int status = check_urb_status(urb);
679 if ((status == -EAGAIN) || (status == -EPROTO))
681 dev_err(dev, "urb cport in error %d (dropped)\n", status);
685 if (urb->actual_length < sizeof(*header)) {
686 dev_err(dev, "short message received\n");
690 /* Extract the CPort id, which is packed in the message header */
691 header = urb->transfer_buffer;
692 cport_id = gb_message_cport_unpack(header);
694 if (cport_id_valid(hd, cport_id)) {
695 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
696 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
699 dev_err(dev, "invalid cport id %u received\n", cport_id);
702 /* put our urb back in the request pool */
703 retval = usb_submit_urb(urb, GFP_ATOMIC);
705 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
708 static void cport_out_callback(struct urb *urb)
710 struct gb_message *message = urb->context;
711 struct gb_host_device *hd = message->operation->connection->hd;
712 struct es2_ap_dev *es2 = hd_to_es2(hd);
713 int status = check_urb_status(urb);
716 gb_message_cport_clear(message->header);
718 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
719 message->hcpriv = NULL;
720 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
723 * Tell the submitter that the message send (attempt) is
724 * complete, and report the status.
726 greybus_message_sent(hd, message, status);
731 #define APB1_LOG_MSG_SIZE 64
732 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
736 /* SVC messages go down our control pipe */
738 retval = usb_control_msg(es2->usb_dev,
739 usb_rcvctrlpipe(es2->usb_dev, 0),
741 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
747 kfifo_in(&es2->apb_log_fifo, buf, retval);
748 } while (retval > 0);
751 static int apb_log_poll(void *data)
753 struct es2_ap_dev *es2 = data;
756 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
760 while (!kthread_should_stop()) {
762 apb_log_get(es2, buf);
770 static ssize_t apb_log_read(struct file *f, char __user *buf,
771 size_t count, loff_t *ppos)
773 struct es2_ap_dev *es2 = f->f_inode->i_private;
778 if (count > APB1_LOG_SIZE)
779 count = APB1_LOG_SIZE;
781 tmp_buf = kmalloc(count, GFP_KERNEL);
785 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
786 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
793 static const struct file_operations apb_log_fops = {
794 .read = apb_log_read,
797 static void usb_log_enable(struct es2_ap_dev *es2)
799 if (!IS_ERR_OR_NULL(es2->apb_log_task))
802 /* get log from APB1 */
803 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
804 if (IS_ERR(es2->apb_log_task))
806 /* XXX We will need to rename this per APB */
807 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
808 gb_debugfs_get(), NULL,
812 static void usb_log_disable(struct es2_ap_dev *es2)
814 if (IS_ERR_OR_NULL(es2->apb_log_task))
817 debugfs_remove(es2->apb_log_dentry);
818 es2->apb_log_dentry = NULL;
820 kthread_stop(es2->apb_log_task);
821 es2->apb_log_task = NULL;
824 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
825 size_t count, loff_t *ppos)
827 struct es2_ap_dev *es2 = f->f_inode->i_private;
828 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
831 sprintf(tmp_buf, "%d\n", enable);
832 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
835 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
836 size_t count, loff_t *ppos)
840 struct es2_ap_dev *es2 = f->f_inode->i_private;
842 retval = kstrtoint_from_user(buf, count, 10, &enable);
849 usb_log_disable(es2);
854 static const struct file_operations apb_log_enable_fops = {
855 .read = apb_log_enable_read,
856 .write = apb_log_enable_write,
859 static int apb_get_cport_count(struct usb_device *udev)
864 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
868 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
869 GB_APB_REQUEST_CPORT_COUNT,
870 USB_DIR_IN | USB_TYPE_VENDOR |
871 USB_RECIP_INTERFACE, 0, 0, cport_count,
872 sizeof(*cport_count), ES2_TIMEOUT);
874 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
879 retval = le16_to_cpu(*cport_count);
881 /* We need to fit a CPort ID in one byte of a message header */
882 if (retval > U8_MAX) {
884 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
893 * The ES2 USB Bridge device has 15 endpoints
894 * 1 Control - usual USB stuff + AP -> APBridgeA messages
895 * 7 Bulk IN - CPort data in
896 * 7 Bulk OUT - CPort data out
898 static int ap_probe(struct usb_interface *interface,
899 const struct usb_device_id *id)
901 struct es2_ap_dev *es2;
902 struct gb_host_device *hd;
903 struct usb_device *udev;
904 struct usb_host_interface *iface_desc;
905 struct usb_endpoint_descriptor *endpoint;
908 int retval = -ENOMEM;
913 udev = usb_get_dev(interface_to_usbdev(interface));
915 num_cports = apb_get_cport_count(udev);
916 if (num_cports < 0) {
918 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
923 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
931 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
932 * won't be allocated dynamically.
935 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
936 } while (cport_id > 0);
940 es2->usb_intf = interface;
942 spin_lock_init(&es2->cport_out_urb_lock);
943 INIT_KFIFO(es2->apb_log_fifo);
944 usb_set_intfdata(interface, es2);
946 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
948 if (!es2->cport_to_ep) {
953 /* find all bulk endpoints */
954 iface_desc = interface->cur_altsetting;
955 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
956 endpoint = &iface_desc->endpoint[i].desc;
958 if (usb_endpoint_is_bulk_in(endpoint)) {
959 es2->cport_in[bulk_in++].endpoint =
960 endpoint->bEndpointAddress;
961 } else if (usb_endpoint_is_bulk_out(endpoint)) {
962 es2->cport_out[bulk_out++].endpoint =
963 endpoint->bEndpointAddress;
966 "Unknown endpoint type found, address 0x%02x\n",
967 endpoint->bEndpointAddress);
970 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
971 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
975 /* Allocate buffers for our cport in messages */
976 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
977 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
979 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
983 urb = usb_alloc_urb(0, GFP_KERNEL);
986 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
990 usb_fill_bulk_urb(urb, udev,
991 usb_rcvbulkpipe(udev,
993 buffer, ES2_GBUF_MSG_SIZE_MAX,
994 cport_in_callback, hd);
995 cport_in->urb[i] = urb;
996 cport_in->buffer[i] = buffer;
1000 /* Allocate urbs for our CPort OUT messages */
1001 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1004 urb = usb_alloc_urb(0, GFP_KERNEL);
1008 es2->cport_out_urb[i] = urb;
1009 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1012 /* XXX We will need to rename this per APB */
1013 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1014 (S_IWUSR | S_IRUGO),
1015 gb_debugfs_get(), es2,
1016 &apb_log_enable_fops);
1018 retval = gb_hd_add(hd);
1022 for (i = 0; i < NUM_BULKS; ++i) {
1023 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1025 goto err_disable_cport_in;
1030 err_disable_cport_in:
1031 for (--i; i >= 0; --i)
1032 es2_cport_in_disable(es2, &es2->cport_in[i]);
1040 static struct usb_driver es2_ap_driver = {
1041 .name = "es2_ap_driver",
1043 .disconnect = ap_disconnect,
1044 .id_table = id_table,
1047 module_usb_driver(es2_ap_driver);
1049 MODULE_LICENSE("GPL v2");
1050 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");