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;
509 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
510 GB_APB_REQUEST_RESET_CPORT,
511 USB_DIR_OUT | USB_TYPE_VENDOR |
512 USB_RECIP_INTERFACE, cport_id, 0,
513 NULL, 0, ES2_TIMEOUT);
515 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
523 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
527 if (cport_id != GB_SVC_CPORT_ID) {
528 retval = cport_reset(hd, cport_id);
536 static int latency_tag_enable(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 GB_APB_REQUEST_LATENCY_TAG_EN,
549 USB_DIR_OUT | USB_TYPE_VENDOR |
550 USB_RECIP_INTERFACE, cport_id, 0, NULL,
554 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
559 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
562 struct es2_ap_dev *es2 = hd_to_es2(hd);
563 struct usb_device *udev = es2->usb_dev;
565 if (!cport_id_valid(hd, cport_id)) {
566 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
570 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
571 GB_APB_REQUEST_LATENCY_TAG_DIS,
572 USB_DIR_OUT | USB_TYPE_VENDOR |
573 USB_RECIP_INTERFACE, cport_id, 0, NULL,
577 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
582 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
585 struct es2_ap_dev *es2 = hd_to_es2(hd);
586 struct usb_device *udev = es2->usb_dev;
588 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
589 GB_APB_REQUEST_CPORT_FEAT_EN,
590 USB_DIR_OUT | USB_TYPE_VENDOR |
591 USB_RECIP_INTERFACE, cport_id, 0, NULL,
594 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
599 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
602 struct es2_ap_dev *es2 = hd_to_es2(hd);
603 struct usb_device *udev = es2->usb_dev;
605 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
606 GB_APB_REQUEST_CPORT_FEAT_DIS,
607 USB_DIR_OUT | USB_TYPE_VENDOR |
608 USB_RECIP_INTERFACE, cport_id, 0, NULL,
612 "Cannot disable CPort features for cport %u: %d\n",
617 static struct gb_hd_driver es2_driver = {
618 .hd_priv_size = sizeof(struct es2_ap_dev),
619 .message_send = message_send,
620 .message_cancel = message_cancel,
621 .cport_enable = cport_enable,
622 .latency_tag_enable = latency_tag_enable,
623 .latency_tag_disable = latency_tag_disable,
625 .cport_features_enable = cport_features_enable,
626 .cport_features_disable = cport_features_disable,
629 /* Common function to report consistent warnings based on URB status */
630 static int check_urb_status(struct urb *urb)
632 struct device *dev = &urb->dev->dev;
633 int status = urb->status;
640 dev_err(dev, "%s: overflow actual length is %d\n",
641 __func__, urb->actual_length);
647 /* device is gone, stop sending */
650 dev_err(dev, "%s: unknown status %d\n", __func__, status);
655 static void es2_destroy(struct es2_ap_dev *es2)
657 struct usb_device *udev;
661 debugfs_remove(es2->apb_log_enable_dentry);
662 usb_log_disable(es2);
664 /* Tear down everything! */
665 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
666 struct urb *urb = es2->cport_out_urb[i];
672 es2->cport_out_urb[i] = NULL;
673 es2->cport_out_urb_busy[i] = false; /* just to be anal */
676 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
677 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
679 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
680 struct urb *urb = cport_in->urb[i];
685 kfree(cport_in->buffer[i]);
686 cport_in->buffer[i] = NULL;
690 kfree(es2->cport_to_ep);
698 static void cport_in_callback(struct urb *urb)
700 struct gb_host_device *hd = urb->context;
701 struct device *dev = &urb->dev->dev;
702 struct gb_operation_msg_hdr *header;
703 int status = check_urb_status(urb);
708 if ((status == -EAGAIN) || (status == -EPROTO))
711 /* The urb is being unlinked */
712 if (status == -ENOENT || status == -ESHUTDOWN)
715 dev_err(dev, "urb cport in error %d (dropped)\n", status);
719 if (urb->actual_length < sizeof(*header)) {
720 dev_err(dev, "short message received\n");
724 /* Extract the CPort id, which is packed in the message header */
725 header = urb->transfer_buffer;
726 cport_id = gb_message_cport_unpack(header);
728 if (cport_id_valid(hd, cport_id)) {
729 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
730 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
733 dev_err(dev, "invalid cport id %u received\n", cport_id);
736 /* put our urb back in the request pool */
737 retval = usb_submit_urb(urb, GFP_ATOMIC);
739 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
742 static void cport_out_callback(struct urb *urb)
744 struct gb_message *message = urb->context;
745 struct gb_host_device *hd = message->operation->connection->hd;
746 struct es2_ap_dev *es2 = hd_to_es2(hd);
747 int status = check_urb_status(urb);
750 gb_message_cport_clear(message->header);
752 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
753 message->hcpriv = NULL;
754 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
757 * Tell the submitter that the message send (attempt) is
758 * complete, and report the status.
760 greybus_message_sent(hd, message, status);
765 #define APB1_LOG_MSG_SIZE 64
766 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
770 /* SVC messages go down our control pipe */
772 retval = usb_control_msg(es2->usb_dev,
773 usb_rcvctrlpipe(es2->usb_dev, 0),
775 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
781 kfifo_in(&es2->apb_log_fifo, buf, retval);
782 } while (retval > 0);
785 static int apb_log_poll(void *data)
787 struct es2_ap_dev *es2 = data;
790 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
794 while (!kthread_should_stop()) {
796 apb_log_get(es2, buf);
804 static ssize_t apb_log_read(struct file *f, char __user *buf,
805 size_t count, loff_t *ppos)
807 struct es2_ap_dev *es2 = f->f_inode->i_private;
812 if (count > APB1_LOG_SIZE)
813 count = APB1_LOG_SIZE;
815 tmp_buf = kmalloc(count, GFP_KERNEL);
819 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
820 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
827 static const struct file_operations apb_log_fops = {
828 .read = apb_log_read,
831 static void usb_log_enable(struct es2_ap_dev *es2)
833 if (!IS_ERR_OR_NULL(es2->apb_log_task))
836 /* get log from APB1 */
837 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
838 if (IS_ERR(es2->apb_log_task))
840 /* XXX We will need to rename this per APB */
841 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
842 gb_debugfs_get(), es2,
846 static void usb_log_disable(struct es2_ap_dev *es2)
848 if (IS_ERR_OR_NULL(es2->apb_log_task))
851 debugfs_remove(es2->apb_log_dentry);
852 es2->apb_log_dentry = NULL;
854 kthread_stop(es2->apb_log_task);
855 es2->apb_log_task = NULL;
858 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
859 size_t count, loff_t *ppos)
861 struct es2_ap_dev *es2 = f->f_inode->i_private;
862 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
865 sprintf(tmp_buf, "%d\n", enable);
866 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
869 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
870 size_t count, loff_t *ppos)
874 struct es2_ap_dev *es2 = f->f_inode->i_private;
876 retval = kstrtoint_from_user(buf, count, 10, &enable);
883 usb_log_disable(es2);
888 static const struct file_operations apb_log_enable_fops = {
889 .read = apb_log_enable_read,
890 .write = apb_log_enable_write,
893 static int apb_get_cport_count(struct usb_device *udev)
898 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
902 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
903 GB_APB_REQUEST_CPORT_COUNT,
904 USB_DIR_IN | USB_TYPE_VENDOR |
905 USB_RECIP_INTERFACE, 0, 0, cport_count,
906 sizeof(*cport_count), ES2_TIMEOUT);
907 if (retval != sizeof(*cport_count)) {
908 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
917 retval = le16_to_cpu(*cport_count);
919 /* We need to fit a CPort ID in one byte of a message header */
920 if (retval > U8_MAX) {
922 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
931 * The ES2 USB Bridge device has 15 endpoints
932 * 1 Control - usual USB stuff + AP -> APBridgeA messages
933 * 7 Bulk IN - CPort data in
934 * 7 Bulk OUT - CPort data out
936 static int ap_probe(struct usb_interface *interface,
937 const struct usb_device_id *id)
939 struct es2_ap_dev *es2;
940 struct gb_host_device *hd;
941 struct usb_device *udev;
942 struct usb_host_interface *iface_desc;
943 struct usb_endpoint_descriptor *endpoint;
950 udev = usb_get_dev(interface_to_usbdev(interface));
952 num_cports = apb_get_cport_count(udev);
953 if (num_cports < 0) {
955 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
960 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
969 es2->usb_intf = interface;
971 spin_lock_init(&es2->cport_out_urb_lock);
972 INIT_KFIFO(es2->apb_log_fifo);
973 usb_set_intfdata(interface, es2);
976 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
979 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
982 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
986 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
988 if (!es2->cport_to_ep) {
993 /* find all bulk endpoints */
994 iface_desc = interface->cur_altsetting;
995 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
996 endpoint = &iface_desc->endpoint[i].desc;
998 if (usb_endpoint_is_bulk_in(endpoint)) {
999 es2->cport_in[bulk_in++].endpoint =
1000 endpoint->bEndpointAddress;
1001 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1002 es2->cport_out[bulk_out++].endpoint =
1003 endpoint->bEndpointAddress;
1006 "Unknown endpoint type found, address 0x%02x\n",
1007 endpoint->bEndpointAddress);
1010 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1011 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);
1029 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1035 usb_fill_bulk_urb(urb, udev,
1036 usb_rcvbulkpipe(udev,
1037 cport_in->endpoint),
1038 buffer, ES2_GBUF_MSG_SIZE_MAX,
1039 cport_in_callback, hd);
1040 cport_in->urb[i] = urb;
1041 cport_in->buffer[i] = buffer;
1045 /* Allocate urbs for our CPort OUT messages */
1046 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1049 urb = usb_alloc_urb(0, GFP_KERNEL);
1055 es2->cport_out_urb[i] = urb;
1056 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1059 /* XXX We will need to rename this per APB */
1060 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1061 (S_IWUSR | S_IRUGO),
1062 gb_debugfs_get(), es2,
1063 &apb_log_enable_fops);
1065 retval = gb_hd_add(hd);
1069 for (i = 0; i < NUM_BULKS; ++i) {
1070 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1072 goto err_disable_cport_in;
1077 err_disable_cport_in:
1078 for (--i; i >= 0; --i)
1079 es2_cport_in_disable(es2, &es2->cport_in[i]);
1087 static void ap_disconnect(struct usb_interface *interface)
1089 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1094 for (i = 0; i < NUM_BULKS; ++i)
1095 es2_cport_in_disable(es2, &es2->cport_in[i]);
1100 static struct usb_driver es2_ap_driver = {
1101 .name = "es2_ap_driver",
1103 .disconnect = ap_disconnect,
1104 .id_table = id_table,
1108 module_usb_driver(es2_ap_driver);
1110 MODULE_LICENSE("GPL v2");
1111 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");