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 int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
580 struct es2_ap_dev *es2 = hd_to_es2(hd);
581 struct usb_device *udev = es2->usb_dev;
583 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
584 GB_APB_REQUEST_CPORT_FEAT_EN,
585 USB_DIR_OUT | USB_TYPE_VENDOR |
586 USB_RECIP_INTERFACE, cport_id, 0, NULL,
589 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
594 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
597 struct es2_ap_dev *es2 = hd_to_es2(hd);
598 struct usb_device *udev = es2->usb_dev;
600 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
601 GB_APB_REQUEST_CPORT_FEAT_DIS,
602 USB_DIR_OUT | USB_TYPE_VENDOR |
603 USB_RECIP_INTERFACE, cport_id, 0, NULL,
607 "Cannot disable CPort features for cport %u: %d\n",
612 static struct gb_hd_driver es2_driver = {
613 .hd_priv_size = sizeof(struct es2_ap_dev),
614 .message_send = message_send,
615 .message_cancel = message_cancel,
616 .cport_enable = cport_enable,
617 .latency_tag_enable = latency_tag_enable,
618 .latency_tag_disable = latency_tag_disable,
620 .cport_features_enable = cport_features_enable,
621 .cport_features_disable = cport_features_disable,
624 /* Common function to report consistent warnings based on URB status */
625 static int check_urb_status(struct urb *urb)
627 struct device *dev = &urb->dev->dev;
628 int status = urb->status;
635 dev_err(dev, "%s: overflow actual length is %d\n",
636 __func__, urb->actual_length);
642 /* device is gone, stop sending */
645 dev_err(dev, "%s: unknown status %d\n", __func__, status);
650 static void es2_destroy(struct es2_ap_dev *es2)
652 struct usb_device *udev;
656 debugfs_remove(es2->apb_log_enable_dentry);
657 usb_log_disable(es2);
659 /* Tear down everything! */
660 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
661 struct urb *urb = es2->cport_out_urb[i];
667 es2->cport_out_urb[i] = NULL;
668 es2->cport_out_urb_busy[i] = false; /* just to be anal */
671 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
672 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
674 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
675 struct urb *urb = cport_in->urb[i];
680 kfree(cport_in->buffer[i]);
681 cport_in->buffer[i] = NULL;
685 kfree(es2->cport_to_ep);
693 static void ap_disconnect(struct usb_interface *interface)
695 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
700 for (i = 0; i < NUM_BULKS; ++i)
701 es2_cport_in_disable(es2, &es2->cport_in[i]);
706 static void cport_in_callback(struct urb *urb)
708 struct gb_host_device *hd = urb->context;
709 struct device *dev = &urb->dev->dev;
710 struct gb_operation_msg_hdr *header;
711 int status = check_urb_status(urb);
716 if ((status == -EAGAIN) || (status == -EPROTO))
718 dev_err(dev, "urb cport in error %d (dropped)\n", status);
722 if (urb->actual_length < sizeof(*header)) {
723 dev_err(dev, "short message received\n");
727 /* Extract the CPort id, which is packed in the message header */
728 header = urb->transfer_buffer;
729 cport_id = gb_message_cport_unpack(header);
731 if (cport_id_valid(hd, cport_id)) {
732 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
733 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
736 dev_err(dev, "invalid cport id %u received\n", cport_id);
739 /* put our urb back in the request pool */
740 retval = usb_submit_urb(urb, GFP_ATOMIC);
742 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
745 static void cport_out_callback(struct urb *urb)
747 struct gb_message *message = urb->context;
748 struct gb_host_device *hd = message->operation->connection->hd;
749 struct es2_ap_dev *es2 = hd_to_es2(hd);
750 int status = check_urb_status(urb);
753 gb_message_cport_clear(message->header);
755 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
756 message->hcpriv = NULL;
757 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
760 * Tell the submitter that the message send (attempt) is
761 * complete, and report the status.
763 greybus_message_sent(hd, message, status);
768 #define APB1_LOG_MSG_SIZE 64
769 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
773 /* SVC messages go down our control pipe */
775 retval = usb_control_msg(es2->usb_dev,
776 usb_rcvctrlpipe(es2->usb_dev, 0),
778 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
784 kfifo_in(&es2->apb_log_fifo, buf, retval);
785 } while (retval > 0);
788 static int apb_log_poll(void *data)
790 struct es2_ap_dev *es2 = data;
793 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
797 while (!kthread_should_stop()) {
799 apb_log_get(es2, buf);
807 static ssize_t apb_log_read(struct file *f, char __user *buf,
808 size_t count, loff_t *ppos)
810 struct es2_ap_dev *es2 = f->f_inode->i_private;
815 if (count > APB1_LOG_SIZE)
816 count = APB1_LOG_SIZE;
818 tmp_buf = kmalloc(count, GFP_KERNEL);
822 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
823 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
830 static const struct file_operations apb_log_fops = {
831 .read = apb_log_read,
834 static void usb_log_enable(struct es2_ap_dev *es2)
836 if (!IS_ERR_OR_NULL(es2->apb_log_task))
839 /* get log from APB1 */
840 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
841 if (IS_ERR(es2->apb_log_task))
843 /* XXX We will need to rename this per APB */
844 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
845 gb_debugfs_get(), es2,
849 static void usb_log_disable(struct es2_ap_dev *es2)
851 if (IS_ERR_OR_NULL(es2->apb_log_task))
854 debugfs_remove(es2->apb_log_dentry);
855 es2->apb_log_dentry = NULL;
857 kthread_stop(es2->apb_log_task);
858 es2->apb_log_task = NULL;
861 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
862 size_t count, loff_t *ppos)
864 struct es2_ap_dev *es2 = f->f_inode->i_private;
865 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
868 sprintf(tmp_buf, "%d\n", enable);
869 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
872 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
873 size_t count, loff_t *ppos)
877 struct es2_ap_dev *es2 = f->f_inode->i_private;
879 retval = kstrtoint_from_user(buf, count, 10, &enable);
886 usb_log_disable(es2);
891 static const struct file_operations apb_log_enable_fops = {
892 .read = apb_log_enable_read,
893 .write = apb_log_enable_write,
896 static int apb_get_cport_count(struct usb_device *udev)
901 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
905 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
906 GB_APB_REQUEST_CPORT_COUNT,
907 USB_DIR_IN | USB_TYPE_VENDOR |
908 USB_RECIP_INTERFACE, 0, 0, cport_count,
909 sizeof(*cport_count), ES2_TIMEOUT);
910 if (retval != sizeof(*cport_count)) {
911 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
920 retval = le16_to_cpu(*cport_count);
922 /* We need to fit a CPort ID in one byte of a message header */
923 if (retval > U8_MAX) {
925 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
934 * The ES2 USB Bridge device has 15 endpoints
935 * 1 Control - usual USB stuff + AP -> APBridgeA messages
936 * 7 Bulk IN - CPort data in
937 * 7 Bulk OUT - CPort data out
939 static int ap_probe(struct usb_interface *interface,
940 const struct usb_device_id *id)
942 struct es2_ap_dev *es2;
943 struct gb_host_device *hd;
944 struct usb_device *udev;
945 struct usb_host_interface *iface_desc;
946 struct usb_endpoint_descriptor *endpoint;
949 int retval = -ENOMEM;
954 udev = usb_get_dev(interface_to_usbdev(interface));
956 num_cports = apb_get_cport_count(udev);
957 if (num_cports < 0) {
959 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
964 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
972 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
973 * won't be allocated dynamically.
976 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
977 } while (cport_id > 0);
981 es2->usb_intf = interface;
983 spin_lock_init(&es2->cport_out_urb_lock);
984 INIT_KFIFO(es2->apb_log_fifo);
985 usb_set_intfdata(interface, es2);
987 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
989 if (!es2->cport_to_ep) {
994 /* find all bulk endpoints */
995 iface_desc = interface->cur_altsetting;
996 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
997 endpoint = &iface_desc->endpoint[i].desc;
999 if (usb_endpoint_is_bulk_in(endpoint)) {
1000 es2->cport_in[bulk_in++].endpoint =
1001 endpoint->bEndpointAddress;
1002 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1003 es2->cport_out[bulk_out++].endpoint =
1004 endpoint->bEndpointAddress;
1007 "Unknown endpoint type found, address 0x%02x\n",
1008 endpoint->bEndpointAddress);
1011 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1012 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1016 /* Allocate buffers for our cport in messages */
1017 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1018 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1020 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1024 urb = usb_alloc_urb(0, GFP_KERNEL);
1027 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1031 usb_fill_bulk_urb(urb, udev,
1032 usb_rcvbulkpipe(udev,
1033 cport_in->endpoint),
1034 buffer, ES2_GBUF_MSG_SIZE_MAX,
1035 cport_in_callback, hd);
1036 cport_in->urb[i] = urb;
1037 cport_in->buffer[i] = buffer;
1041 /* Allocate urbs for our CPort OUT messages */
1042 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1045 urb = usb_alloc_urb(0, GFP_KERNEL);
1049 es2->cport_out_urb[i] = urb;
1050 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1053 /* XXX We will need to rename this per APB */
1054 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1055 (S_IWUSR | S_IRUGO),
1056 gb_debugfs_get(), es2,
1057 &apb_log_enable_fops);
1059 retval = gb_hd_add(hd);
1063 for (i = 0; i < NUM_BULKS; ++i) {
1064 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1066 goto err_disable_cport_in;
1071 err_disable_cport_in:
1072 for (--i; i >= 0; --i)
1073 es2_cport_in_disable(es2, &es2->cport_in[i]);
1081 static struct usb_driver es2_ap_driver = {
1082 .name = "es2_ap_driver",
1084 .disconnect = ap_disconnect,
1085 .id_table = id_table,
1089 module_usb_driver(es2_ap_driver);
1091 MODULE_LICENSE("GPL v2");
1092 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");