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"
22 /* Fixed CPort numbers */
23 #define ES2_CPORT_CDSI0 16
24 #define ES2_CPORT_CDSI1 17
26 /* Memory sizes for the buffers sent to/from the ES2 controller */
27 #define ES2_GBUF_MSG_SIZE_MAX 2048
29 static const struct usb_device_id id_table[] = {
30 { USB_DEVICE(0x18d1, 0x1eaf) },
33 MODULE_DEVICE_TABLE(usb, id_table);
35 #define APB1_LOG_SIZE SZ_16K
37 /* Number of bulk in and bulk out couple */
41 * Number of CPort IN urbs in flight at any point in time.
42 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
45 #define NUM_CPORT_IN_URB 4
47 /* Number of CPort OUT urbs in flight at any point in time.
48 * Adjust if we get messages saying we are out of urbs in the system log.
50 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
53 * @endpoint: bulk in endpoint for CPort data
54 * @urb: array of urbs for the CPort in messages
55 * @buffer: array of buffers for the @cport_in_urb urbs
59 struct urb *urb[NUM_CPORT_IN_URB];
60 u8 *buffer[NUM_CPORT_IN_URB];
64 * @endpoint: bulk out endpoint for CPort data
66 struct es2_cport_out {
71 * es2_ap_dev - ES2 USB Bridge to AP structure
72 * @usb_dev: pointer to the USB device we are.
73 * @usb_intf: pointer to the USB interface we are bound to.
74 * @hd: pointer to our gb_host_device structure
76 * @cport_in: endpoint, urbs and buffer for cport in messages
77 * @cport_out: endpoint for for cport out messages
78 * @cport_out_urb: array of urbs for the CPort out messages
79 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
81 * @cport_out_urb_cancelled: array of flags indicating whether the
82 * corresponding @cport_out_urb is being cancelled
83 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
85 * @apb_log_task: task pointer for logging thread
86 * @apb_log_dentry: file system entry for the log file interface
87 * @apb_log_enable_dentry: file system entry for enabling logging
88 * @apb_log_fifo: kernel FIFO to carry logged data
91 struct usb_device *usb_dev;
92 struct usb_interface *usb_intf;
93 struct gb_host_device *hd;
95 struct es2_cport_in cport_in[NUM_BULKS];
96 struct es2_cport_out cport_out[NUM_BULKS];
97 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
98 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
99 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
100 spinlock_t cport_out_urb_lock;
104 struct task_struct *apb_log_task;
105 struct dentry *apb_log_dentry;
106 struct dentry *apb_log_enable_dentry;
107 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
111 * cport_to_ep - information about cport to endpoints mapping
112 * @cport_id: the id of cport to map to endpoints
113 * @endpoint_in: the endpoint number to use for in transfer
114 * @endpoint_out: he endpoint number to use for out transfer
122 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
124 return (struct es2_ap_dev *)&hd->hd_priv;
127 static void cport_out_callback(struct urb *urb);
128 static void usb_log_enable(struct es2_ap_dev *es2);
129 static void usb_log_disable(struct es2_ap_dev *es2);
131 /* Get the endpoints pair mapped to the cport */
132 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
134 if (cport_id >= es2->hd->num_cports)
136 return es2->cport_to_ep[cport_id];
139 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
141 /* Disable for now until we work all of this out to keep a warning-free build */
143 /* Test if the endpoints pair is already mapped to a cport */
144 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
148 for (i = 0; i < es2->hd->num_cports; i++) {
149 if (es2->cport_to_ep[i] == ep_pair)
155 /* Configure the endpoint mapping and send the request to APBridge */
156 static int map_cport_to_ep(struct es2_ap_dev *es2,
157 u16 cport_id, int ep_pair)
160 struct cport_to_ep *cport_to_ep;
162 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
164 if (cport_id >= es2->hd->num_cports)
166 if (ep_pair && ep_pair_in_use(es2, ep_pair))
169 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
173 es2->cport_to_ep[cport_id] = ep_pair;
174 cport_to_ep->cport_id = cpu_to_le16(cport_id);
175 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
176 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
178 retval = usb_control_msg(es2->usb_dev,
179 usb_sndctrlpipe(es2->usb_dev, 0),
180 GB_APB_REQUEST_EP_MAPPING,
181 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
184 sizeof(*cport_to_ep),
186 if (retval == sizeof(*cport_to_ep))
193 /* Unmap a cport: use the muxed endpoints pair */
194 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
196 return map_cport_to_ep(es2, cport_id, 0);
200 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
202 struct usb_device *udev = es2->usb_dev;
206 data = kmalloc(size, GFP_KERNEL);
209 memcpy(data, req, size);
211 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
213 USB_DIR_OUT | USB_TYPE_VENDOR |
215 0, 0, data, size, ES2_TIMEOUT);
217 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
225 static void ap_urb_complete(struct urb *urb)
227 struct usb_ctrlrequest *dr = urb->context;
233 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
235 struct usb_device *udev = es2->usb_dev;
237 struct usb_ctrlrequest *dr;
241 urb = usb_alloc_urb(0, GFP_ATOMIC);
245 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
251 buf = (u8 *)dr + sizeof(*dr);
252 memcpy(buf, req, size);
255 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
258 dr->wLength = cpu_to_le16(size);
260 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
261 (unsigned char *)dr, buf, size,
262 ap_urb_complete, dr);
263 retval = usb_submit_urb(urb, GFP_ATOMIC);
271 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
274 struct es2_ap_dev *es2 = hd_to_es2(hd);
277 return output_async(es2, req, size, cmd);
279 return output_sync(es2, req, size, cmd);
282 static int es2_cport_in_enable(struct es2_ap_dev *es2,
283 struct es2_cport_in *cport_in)
289 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
290 urb = cport_in->urb[i];
292 ret = usb_submit_urb(urb, GFP_KERNEL);
294 dev_err(&es2->usb_dev->dev,
295 "failed to submit in-urb: %d\n", ret);
303 for (--i; i >= 0; --i) {
304 urb = cport_in->urb[i];
311 static void es2_cport_in_disable(struct es2_ap_dev *es2,
312 struct es2_cport_in *cport_in)
317 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
318 urb = cport_in->urb[i];
323 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
325 struct urb *urb = NULL;
329 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
331 /* Look in our pool of allocated urbs first, as that's the "fastest" */
332 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
333 if (es2->cport_out_urb_busy[i] == false &&
334 es2->cport_out_urb_cancelled[i] == false) {
335 es2->cport_out_urb_busy[i] = true;
336 urb = es2->cport_out_urb[i];
340 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
345 * Crap, pool is empty, complain to the syslog and go allocate one
346 * dynamically as we have to succeed.
348 dev_dbg(&es2->usb_dev->dev,
349 "No free CPort OUT urbs, having to dynamically allocate one!\n");
350 return usb_alloc_urb(0, gfp_mask);
353 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
358 * See if this was an urb in our pool, if so mark it "free", otherwise
359 * we need to free it ourselves.
361 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
362 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
363 if (urb == es2->cport_out_urb[i]) {
364 es2->cport_out_urb_busy[i] = false;
369 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
371 /* If urb is not NULL, then we need to free this urb */
376 * We (ab)use the operation-message header pad bytes to transfer the
377 * cport id in order to minimise overhead.
380 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
382 header->pad[0] = cport_id;
385 /* Clear the pad bytes used for the CPort id */
386 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
391 /* Extract the CPort id packed into the header, and clear it */
392 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
394 u16 cport_id = header->pad[0];
396 gb_message_cport_clear(header);
402 * Returns zero if the message was successfully queued, or a negative errno
405 static int message_send(struct gb_host_device *hd, u16 cport_id,
406 struct gb_message *message, gfp_t gfp_mask)
408 struct es2_ap_dev *es2 = hd_to_es2(hd);
409 struct usb_device *udev = es2->usb_dev;
417 * The data actually transferred will include an indication
418 * of where the data should be sent. Do one last check of
419 * the target CPort id before filling it in.
421 if (!cport_id_valid(hd, cport_id)) {
422 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
426 /* Find a free urb */
427 urb = next_free_urb(es2, gfp_mask);
431 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
432 message->hcpriv = urb;
433 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
435 /* Pack the cport id into the message header */
436 gb_message_cport_pack(message->header, cport_id);
438 buffer_size = sizeof(*message->header) + message->payload_size;
440 ep_pair = cport_to_ep_pair(es2, cport_id);
441 usb_fill_bulk_urb(urb, udev,
442 usb_sndbulkpipe(udev,
443 es2->cport_out[ep_pair].endpoint),
444 message->buffer, buffer_size,
445 cport_out_callback, message);
446 urb->transfer_flags |= URB_ZERO_PACKET;
447 trace_gb_host_device_send(hd, cport_id, buffer_size);
448 retval = usb_submit_urb(urb, gfp_mask);
450 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
452 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
453 message->hcpriv = NULL;
454 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
457 gb_message_cport_clear(message->header);
466 * Can not be called in atomic context.
468 static void message_cancel(struct gb_message *message)
470 struct gb_host_device *hd = message->operation->connection->hd;
471 struct es2_ap_dev *es2 = hd_to_es2(hd);
477 spin_lock_irq(&es2->cport_out_urb_lock);
478 urb = message->hcpriv;
480 /* Prevent dynamically allocated urb from being deallocated. */
483 /* Prevent pre-allocated urb from being reused. */
484 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
485 if (urb == es2->cport_out_urb[i]) {
486 es2->cport_out_urb_cancelled[i] = true;
490 spin_unlock_irq(&es2->cport_out_urb_lock);
494 if (i < NUM_CPORT_OUT_URB) {
495 spin_lock_irq(&es2->cport_out_urb_lock);
496 es2->cport_out_urb_cancelled[i] = false;
497 spin_unlock_irq(&es2->cport_out_urb_lock);
503 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
505 struct es2_ap_dev *es2 = hd_to_es2(hd);
506 struct usb_device *udev = es2->usb_dev;
510 case GB_SVC_CPORT_ID:
514 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
515 GB_APB_REQUEST_RESET_CPORT,
516 USB_DIR_OUT | USB_TYPE_VENDOR |
517 USB_RECIP_INTERFACE, cport_id, 0,
518 NULL, 0, ES2_TIMEOUT);
520 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
528 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
532 retval = cport_reset(hd, cport_id);
539 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
542 struct es2_ap_dev *es2 = hd_to_es2(hd);
543 struct usb_device *udev = es2->usb_dev;
545 if (!cport_id_valid(hd, cport_id)) {
546 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
550 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
551 GB_APB_REQUEST_LATENCY_TAG_EN,
552 USB_DIR_OUT | USB_TYPE_VENDOR |
553 USB_RECIP_INTERFACE, cport_id, 0, NULL,
557 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
562 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
565 struct es2_ap_dev *es2 = hd_to_es2(hd);
566 struct usb_device *udev = es2->usb_dev;
568 if (!cport_id_valid(hd, cport_id)) {
569 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
573 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
574 GB_APB_REQUEST_LATENCY_TAG_DIS,
575 USB_DIR_OUT | USB_TYPE_VENDOR |
576 USB_RECIP_INTERFACE, cport_id, 0, NULL,
580 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
585 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
588 struct es2_ap_dev *es2 = hd_to_es2(hd);
589 struct usb_device *udev = es2->usb_dev;
591 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
592 GB_APB_REQUEST_CPORT_FEAT_EN,
593 USB_DIR_OUT | USB_TYPE_VENDOR |
594 USB_RECIP_INTERFACE, cport_id, 0, NULL,
597 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
602 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
605 struct es2_ap_dev *es2 = hd_to_es2(hd);
606 struct usb_device *udev = es2->usb_dev;
608 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
609 GB_APB_REQUEST_CPORT_FEAT_DIS,
610 USB_DIR_OUT | USB_TYPE_VENDOR |
611 USB_RECIP_INTERFACE, cport_id, 0, NULL,
615 "Cannot disable CPort features for cport %u: %d\n",
620 static struct gb_hd_driver es2_driver = {
621 .hd_priv_size = sizeof(struct es2_ap_dev),
622 .message_send = message_send,
623 .message_cancel = message_cancel,
624 .cport_enable = cport_enable,
625 .latency_tag_enable = latency_tag_enable,
626 .latency_tag_disable = latency_tag_disable,
628 .cport_features_enable = cport_features_enable,
629 .cport_features_disable = cport_features_disable,
632 /* Common function to report consistent warnings based on URB status */
633 static int check_urb_status(struct urb *urb)
635 struct device *dev = &urb->dev->dev;
636 int status = urb->status;
643 dev_err(dev, "%s: overflow actual length is %d\n",
644 __func__, urb->actual_length);
650 /* device is gone, stop sending */
653 dev_err(dev, "%s: unknown status %d\n", __func__, status);
658 static void es2_destroy(struct es2_ap_dev *es2)
660 struct usb_device *udev;
664 debugfs_remove(es2->apb_log_enable_dentry);
665 usb_log_disable(es2);
667 /* Tear down everything! */
668 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
669 struct urb *urb = es2->cport_out_urb[i];
675 es2->cport_out_urb[i] = NULL;
676 es2->cport_out_urb_busy[i] = false; /* just to be anal */
679 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
680 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
682 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
683 struct urb *urb = cport_in->urb[i];
688 kfree(cport_in->buffer[i]);
689 cport_in->buffer[i] = NULL;
693 kfree(es2->cport_to_ep);
701 static void cport_in_callback(struct urb *urb)
703 struct gb_host_device *hd = urb->context;
704 struct device *dev = &urb->dev->dev;
705 struct gb_operation_msg_hdr *header;
706 int status = check_urb_status(urb);
711 if ((status == -EAGAIN) || (status == -EPROTO))
714 /* The urb is being unlinked */
715 if (status == -ENOENT || status == -ESHUTDOWN)
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;
953 udev = usb_get_dev(interface_to_usbdev(interface));
955 num_cports = apb_get_cport_count(udev);
956 if (num_cports < 0) {
958 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
963 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
972 es2->usb_intf = interface;
974 spin_lock_init(&es2->cport_out_urb_lock);
975 INIT_KFIFO(es2->apb_log_fifo);
976 usb_set_intfdata(interface, es2);
979 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
982 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
985 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
989 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
991 if (!es2->cport_to_ep) {
996 /* find all bulk endpoints */
997 iface_desc = interface->cur_altsetting;
998 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
999 endpoint = &iface_desc->endpoint[i].desc;
1001 if (usb_endpoint_is_bulk_in(endpoint)) {
1002 es2->cport_in[bulk_in++].endpoint =
1003 endpoint->bEndpointAddress;
1004 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1005 es2->cport_out[bulk_out++].endpoint =
1006 endpoint->bEndpointAddress;
1009 "Unknown endpoint type found, address 0x%02x\n",
1010 endpoint->bEndpointAddress);
1013 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1014 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1019 /* Allocate buffers for our cport in messages */
1020 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1021 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1023 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1027 urb = usb_alloc_urb(0, GFP_KERNEL);
1032 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1038 usb_fill_bulk_urb(urb, udev,
1039 usb_rcvbulkpipe(udev,
1040 cport_in->endpoint),
1041 buffer, ES2_GBUF_MSG_SIZE_MAX,
1042 cport_in_callback, hd);
1043 cport_in->urb[i] = urb;
1044 cport_in->buffer[i] = buffer;
1048 /* Allocate urbs for our CPort OUT messages */
1049 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1052 urb = usb_alloc_urb(0, GFP_KERNEL);
1058 es2->cport_out_urb[i] = urb;
1059 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1062 /* XXX We will need to rename this per APB */
1063 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1064 (S_IWUSR | S_IRUGO),
1065 gb_debugfs_get(), es2,
1066 &apb_log_enable_fops);
1068 retval = gb_hd_add(hd);
1072 for (i = 0; i < NUM_BULKS; ++i) {
1073 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1075 goto err_disable_cport_in;
1080 err_disable_cport_in:
1081 for (--i; i >= 0; --i)
1082 es2_cport_in_disable(es2, &es2->cport_in[i]);
1090 static void ap_disconnect(struct usb_interface *interface)
1092 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1097 for (i = 0; i < NUM_BULKS; ++i)
1098 es2_cport_in_disable(es2, &es2->cport_in[i]);
1103 static struct usb_driver es2_ap_driver = {
1104 .name = "es2_ap_driver",
1106 .disconnect = ap_disconnect,
1107 .id_table = id_table,
1111 module_usb_driver(es2_ap_driver);
1113 MODULE_LICENSE("GPL v2");
1114 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");