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 = kzalloc(sizeof(*cfg_req), GFP_KERNEL);
235 cfg_req->csi_id = cfg->csi_id;
238 cfg_req->clock_mode = cfg->clock_mode;
239 cfg_req->num_lanes = cfg->num_lanes;
240 cfg_req->bus_freq = cpu_to_le32(cfg->bus_freq);
243 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
244 REQUEST_CSI_TX_CONTROL,
245 USB_DIR_OUT | USB_TYPE_VENDOR |
246 USB_RECIP_INTERFACE, 0, 0, cfg_req,
247 sizeof(*cfg_req), ES2_TIMEOUT);
249 dev_err(&udev->dev, "failed to setup csi: %d\n", retval);
254 EXPORT_SYMBOL_GPL(es2_ap_csi_setup);
256 static int es2_cport_in_enable(struct es2_ap_dev *es2,
257 struct es2_cport_in *cport_in)
263 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
264 urb = cport_in->urb[i];
266 ret = usb_submit_urb(urb, GFP_KERNEL);
268 dev_err(&es2->usb_dev->dev,
269 "failed to submit in-urb: %d\n", ret);
277 for (--i; i >= 0; --i) {
278 urb = cport_in->urb[i];
285 static void es2_cport_in_disable(struct es2_ap_dev *es2,
286 struct es2_cport_in *cport_in)
291 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
292 urb = cport_in->urb[i];
297 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
299 struct urb *urb = NULL;
303 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
305 /* Look in our pool of allocated urbs first, as that's the "fastest" */
306 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
307 if (es2->cport_out_urb_busy[i] == false &&
308 es2->cport_out_urb_cancelled[i] == false) {
309 es2->cport_out_urb_busy[i] = true;
310 urb = es2->cport_out_urb[i];
314 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
319 * Crap, pool is empty, complain to the syslog and go allocate one
320 * dynamically as we have to succeed.
322 dev_dbg(&es2->usb_dev->dev,
323 "No free CPort OUT urbs, having to dynamically allocate one!\n");
324 return usb_alloc_urb(0, gfp_mask);
327 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
332 * See if this was an urb in our pool, if so mark it "free", otherwise
333 * we need to free it ourselves.
335 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
336 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
337 if (urb == es2->cport_out_urb[i]) {
338 es2->cport_out_urb_busy[i] = false;
343 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
345 /* If urb is not NULL, then we need to free this urb */
350 * We (ab)use the operation-message header pad bytes to transfer the
351 * cport id in order to minimise overhead.
354 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
356 header->pad[0] = cport_id;
359 /* Clear the pad bytes used for the CPort id */
360 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
365 /* Extract the CPort id packed into the header, and clear it */
366 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
368 u16 cport_id = header->pad[0];
370 gb_message_cport_clear(header);
376 * Returns zero if the message was successfully queued, or a negative errno
379 static int message_send(struct gb_host_device *hd, u16 cport_id,
380 struct gb_message *message, gfp_t gfp_mask)
382 struct es2_ap_dev *es2 = hd_to_es2(hd);
383 struct usb_device *udev = es2->usb_dev;
391 * The data actually transferred will include an indication
392 * of where the data should be sent. Do one last check of
393 * the target CPort id before filling it in.
395 if (!cport_id_valid(hd, cport_id)) {
396 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
400 /* Find a free urb */
401 urb = next_free_urb(es2, gfp_mask);
405 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
406 message->hcpriv = urb;
407 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
409 /* Pack the cport id into the message header */
410 gb_message_cport_pack(message->header, cport_id);
412 buffer_size = sizeof(*message->header) + message->payload_size;
414 ep_pair = cport_to_ep_pair(es2, cport_id);
415 usb_fill_bulk_urb(urb, udev,
416 usb_sndbulkpipe(udev,
417 es2->cport_out[ep_pair].endpoint),
418 message->buffer, buffer_size,
419 cport_out_callback, message);
420 urb->transfer_flags |= URB_ZERO_PACKET;
421 trace_gb_host_device_send(hd, cport_id, buffer_size);
422 retval = usb_submit_urb(urb, gfp_mask);
424 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
426 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
427 message->hcpriv = NULL;
428 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
431 gb_message_cport_clear(message->header);
440 * Can not be called in atomic context.
442 static void message_cancel(struct gb_message *message)
444 struct gb_host_device *hd = message->operation->connection->hd;
445 struct es2_ap_dev *es2 = hd_to_es2(hd);
451 spin_lock_irq(&es2->cport_out_urb_lock);
452 urb = message->hcpriv;
454 /* Prevent dynamically allocated urb from being deallocated. */
457 /* Prevent pre-allocated urb from being reused. */
458 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
459 if (urb == es2->cport_out_urb[i]) {
460 es2->cport_out_urb_cancelled[i] = true;
464 spin_unlock_irq(&es2->cport_out_urb_lock);
468 if (i < NUM_CPORT_OUT_URB) {
469 spin_lock_irq(&es2->cport_out_urb_lock);
470 es2->cport_out_urb_cancelled[i] = false;
471 spin_unlock_irq(&es2->cport_out_urb_lock);
477 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
479 struct es2_ap_dev *es2 = hd_to_es2(hd);
480 struct usb_device *udev = es2->usb_dev;
483 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
485 USB_DIR_OUT | USB_TYPE_VENDOR |
486 USB_RECIP_INTERFACE, cport_id, 0,
487 NULL, 0, ES2_TIMEOUT);
489 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
497 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
501 if (cport_id != GB_SVC_CPORT_ID) {
502 retval = cport_reset(hd, cport_id);
510 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
513 struct es2_ap_dev *es2 = hd_to_es2(hd);
514 struct usb_device *udev = es2->usb_dev;
516 if (!cport_id_valid(hd, cport_id)) {
517 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
521 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
522 REQUEST_LATENCY_TAG_EN,
523 USB_DIR_OUT | USB_TYPE_VENDOR |
524 USB_RECIP_INTERFACE, cport_id, 0, NULL,
528 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
533 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
536 struct es2_ap_dev *es2 = hd_to_es2(hd);
537 struct usb_device *udev = es2->usb_dev;
539 if (!cport_id_valid(hd, cport_id)) {
540 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
544 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
545 REQUEST_LATENCY_TAG_DIS,
546 USB_DIR_OUT | USB_TYPE_VENDOR |
547 USB_RECIP_INTERFACE, cport_id, 0, NULL,
551 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
556 static struct gb_hd_driver es2_driver = {
557 .hd_priv_size = sizeof(struct es2_ap_dev),
558 .message_send = message_send,
559 .message_cancel = message_cancel,
560 .cport_enable = cport_enable,
561 .latency_tag_enable = latency_tag_enable,
562 .latency_tag_disable = latency_tag_disable,
565 /* Common function to report consistent warnings based on URB status */
566 static int check_urb_status(struct urb *urb)
568 struct device *dev = &urb->dev->dev;
569 int status = urb->status;
576 dev_err(dev, "%s: overflow actual length is %d\n",
577 __func__, urb->actual_length);
583 /* device is gone, stop sending */
586 dev_err(dev, "%s: unknown status %d\n", __func__, status);
591 static void es2_destroy(struct es2_ap_dev *es2)
593 struct usb_device *udev;
597 debugfs_remove(es2->apb_log_enable_dentry);
598 usb_log_disable(es2);
600 /* Tear down everything! */
601 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
602 struct urb *urb = es2->cport_out_urb[i];
608 es2->cport_out_urb[i] = NULL;
609 es2->cport_out_urb_busy[i] = false; /* just to be anal */
612 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
613 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
615 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
616 struct urb *urb = cport_in->urb[i];
621 kfree(cport_in->buffer[i]);
622 cport_in->buffer[i] = NULL;
626 kfree(es2->cport_to_ep);
634 static void ap_disconnect(struct usb_interface *interface)
636 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
639 for (i = 0; i < NUM_BULKS; ++i)
640 es2_cport_in_disable(es2, &es2->cport_in[i]);
647 static void cport_in_callback(struct urb *urb)
649 struct gb_host_device *hd = urb->context;
650 struct device *dev = &urb->dev->dev;
651 struct gb_operation_msg_hdr *header;
652 int status = check_urb_status(urb);
657 if ((status == -EAGAIN) || (status == -EPROTO))
659 dev_err(dev, "urb cport in error %d (dropped)\n", status);
663 if (urb->actual_length < sizeof(*header)) {
664 dev_err(dev, "short message received\n");
668 /* Extract the CPort id, which is packed in the message header */
669 header = urb->transfer_buffer;
670 cport_id = gb_message_cport_unpack(header);
672 if (cport_id_valid(hd, cport_id)) {
673 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
674 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
677 dev_err(dev, "invalid cport id %u received\n", cport_id);
680 /* put our urb back in the request pool */
681 retval = usb_submit_urb(urb, GFP_ATOMIC);
683 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
686 static void cport_out_callback(struct urb *urb)
688 struct gb_message *message = urb->context;
689 struct gb_host_device *hd = message->operation->connection->hd;
690 struct es2_ap_dev *es2 = hd_to_es2(hd);
691 int status = check_urb_status(urb);
694 gb_message_cport_clear(message->header);
696 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
697 message->hcpriv = NULL;
698 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
701 * Tell the submitter that the message send (attempt) is
702 * complete, and report the status.
704 greybus_message_sent(hd, message, status);
709 #define APB1_LOG_MSG_SIZE 64
710 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
714 /* SVC messages go down our control pipe */
716 retval = usb_control_msg(es2->usb_dev,
717 usb_rcvctrlpipe(es2->usb_dev, 0),
719 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
725 kfifo_in(&es2->apb_log_fifo, buf, retval);
726 } while (retval > 0);
729 static int apb_log_poll(void *data)
731 struct es2_ap_dev *es2 = data;
734 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
738 while (!kthread_should_stop()) {
740 apb_log_get(es2, buf);
748 static ssize_t apb_log_read(struct file *f, char __user *buf,
749 size_t count, loff_t *ppos)
751 struct es2_ap_dev *es2 = f->f_inode->i_private;
756 if (count > APB1_LOG_SIZE)
757 count = APB1_LOG_SIZE;
759 tmp_buf = kmalloc(count, GFP_KERNEL);
763 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
764 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
771 static const struct file_operations apb_log_fops = {
772 .read = apb_log_read,
775 static void usb_log_enable(struct es2_ap_dev *es2)
777 if (!IS_ERR_OR_NULL(es2->apb_log_task))
780 /* get log from APB1 */
781 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
782 if (IS_ERR(es2->apb_log_task))
784 /* XXX We will need to rename this per APB */
785 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
786 gb_debugfs_get(), NULL,
790 static void usb_log_disable(struct es2_ap_dev *es2)
792 if (IS_ERR_OR_NULL(es2->apb_log_task))
795 debugfs_remove(es2->apb_log_dentry);
796 es2->apb_log_dentry = NULL;
798 kthread_stop(es2->apb_log_task);
799 es2->apb_log_task = NULL;
802 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
803 size_t count, loff_t *ppos)
805 struct es2_ap_dev *es2 = f->f_inode->i_private;
806 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
809 sprintf(tmp_buf, "%d\n", enable);
810 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
813 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
814 size_t count, loff_t *ppos)
818 struct es2_ap_dev *es2 = f->f_inode->i_private;
820 retval = kstrtoint_from_user(buf, count, 10, &enable);
827 usb_log_disable(es2);
832 static const struct file_operations apb_log_enable_fops = {
833 .read = apb_log_enable_read,
834 .write = apb_log_enable_write,
837 static int apb_get_cport_count(struct usb_device *udev)
842 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
846 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
848 USB_DIR_IN | USB_TYPE_VENDOR |
849 USB_RECIP_INTERFACE, 0, 0, cport_count,
850 sizeof(*cport_count), ES2_TIMEOUT);
852 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
857 retval = le16_to_cpu(*cport_count);
859 /* We need to fit a CPort ID in one byte of a message header */
860 if (retval > U8_MAX) {
862 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
871 * The ES2 USB Bridge device has 15 endpoints
872 * 1 Control - usual USB stuff + AP -> APBridgeA messages
873 * 7 Bulk IN - CPort data in
874 * 7 Bulk OUT - CPort data out
876 static int ap_probe(struct usb_interface *interface,
877 const struct usb_device_id *id)
879 struct es2_ap_dev *es2;
880 struct gb_host_device *hd;
881 struct usb_device *udev;
882 struct usb_host_interface *iface_desc;
883 struct usb_endpoint_descriptor *endpoint;
886 int retval = -ENOMEM;
891 udev = usb_get_dev(interface_to_usbdev(interface));
893 num_cports = apb_get_cport_count(udev);
894 if (num_cports < 0) {
896 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
901 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
909 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
910 * won't be allocated dynamically.
913 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
914 } while (cport_id > 0);
918 es2->usb_intf = interface;
920 spin_lock_init(&es2->cport_out_urb_lock);
921 INIT_KFIFO(es2->apb_log_fifo);
922 usb_set_intfdata(interface, es2);
924 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
926 if (!es2->cport_to_ep) {
931 /* find all bulk endpoints */
932 iface_desc = interface->cur_altsetting;
933 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
934 endpoint = &iface_desc->endpoint[i].desc;
936 if (usb_endpoint_is_bulk_in(endpoint)) {
937 es2->cport_in[bulk_in++].endpoint =
938 endpoint->bEndpointAddress;
939 } else if (usb_endpoint_is_bulk_out(endpoint)) {
940 es2->cport_out[bulk_out++].endpoint =
941 endpoint->bEndpointAddress;
944 "Unknown endpoint type found, address 0x%02x\n",
945 endpoint->bEndpointAddress);
948 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
949 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
953 /* Allocate buffers for our cport in messages */
954 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
955 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
957 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
961 urb = usb_alloc_urb(0, GFP_KERNEL);
964 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
968 usb_fill_bulk_urb(urb, udev,
969 usb_rcvbulkpipe(udev,
971 buffer, ES2_GBUF_MSG_SIZE_MAX,
972 cport_in_callback, hd);
973 cport_in->urb[i] = urb;
974 cport_in->buffer[i] = buffer;
978 /* Allocate urbs for our CPort OUT messages */
979 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
982 urb = usb_alloc_urb(0, GFP_KERNEL);
986 es2->cport_out_urb[i] = urb;
987 es2->cport_out_urb_busy[i] = false; /* just to be anal */
990 /* XXX We will need to rename this per APB */
991 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
993 gb_debugfs_get(), es2,
994 &apb_log_enable_fops);
996 retval = gb_hd_add(hd);
1000 for (i = 0; i < NUM_BULKS; ++i) {
1001 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1003 goto err_disable_cport_in;
1008 err_disable_cport_in:
1009 for (--i; i >= 0; --i)
1010 es2_cport_in_disable(es2, &es2->cport_in[i]);
1018 static struct usb_driver es2_ap_driver = {
1019 .name = "es2_ap_driver",
1021 .disconnect = ap_disconnect,
1022 .id_table = id_table,
1025 module_usb_driver(es2_ap_driver);
1027 MODULE_LICENSE("GPL v2");
1028 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");