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 cport_in_callback(struct urb *urb)
695 struct gb_host_device *hd = urb->context;
696 struct device *dev = &urb->dev->dev;
697 struct gb_operation_msg_hdr *header;
698 int status = check_urb_status(urb);
703 if ((status == -EAGAIN) || (status == -EPROTO))
706 /* The urb is being unlinked */
707 if (status == -ENOENT || status == -ESHUTDOWN)
710 dev_err(dev, "urb cport in error %d (dropped)\n", status);
714 if (urb->actual_length < sizeof(*header)) {
715 dev_err(dev, "short message received\n");
719 /* Extract the CPort id, which is packed in the message header */
720 header = urb->transfer_buffer;
721 cport_id = gb_message_cport_unpack(header);
723 if (cport_id_valid(hd, cport_id)) {
724 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
725 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
728 dev_err(dev, "invalid cport id %u received\n", cport_id);
731 /* put our urb back in the request pool */
732 retval = usb_submit_urb(urb, GFP_ATOMIC);
734 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
737 static void cport_out_callback(struct urb *urb)
739 struct gb_message *message = urb->context;
740 struct gb_host_device *hd = message->operation->connection->hd;
741 struct es2_ap_dev *es2 = hd_to_es2(hd);
742 int status = check_urb_status(urb);
745 gb_message_cport_clear(message->header);
747 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
748 message->hcpriv = NULL;
749 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
752 * Tell the submitter that the message send (attempt) is
753 * complete, and report the status.
755 greybus_message_sent(hd, message, status);
760 #define APB1_LOG_MSG_SIZE 64
761 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
765 /* SVC messages go down our control pipe */
767 retval = usb_control_msg(es2->usb_dev,
768 usb_rcvctrlpipe(es2->usb_dev, 0),
770 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
776 kfifo_in(&es2->apb_log_fifo, buf, retval);
777 } while (retval > 0);
780 static int apb_log_poll(void *data)
782 struct es2_ap_dev *es2 = data;
785 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
789 while (!kthread_should_stop()) {
791 apb_log_get(es2, buf);
799 static ssize_t apb_log_read(struct file *f, char __user *buf,
800 size_t count, loff_t *ppos)
802 struct es2_ap_dev *es2 = f->f_inode->i_private;
807 if (count > APB1_LOG_SIZE)
808 count = APB1_LOG_SIZE;
810 tmp_buf = kmalloc(count, GFP_KERNEL);
814 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
815 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
822 static const struct file_operations apb_log_fops = {
823 .read = apb_log_read,
826 static void usb_log_enable(struct es2_ap_dev *es2)
828 if (!IS_ERR_OR_NULL(es2->apb_log_task))
831 /* get log from APB1 */
832 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
833 if (IS_ERR(es2->apb_log_task))
835 /* XXX We will need to rename this per APB */
836 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
837 gb_debugfs_get(), es2,
841 static void usb_log_disable(struct es2_ap_dev *es2)
843 if (IS_ERR_OR_NULL(es2->apb_log_task))
846 debugfs_remove(es2->apb_log_dentry);
847 es2->apb_log_dentry = NULL;
849 kthread_stop(es2->apb_log_task);
850 es2->apb_log_task = NULL;
853 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
854 size_t count, loff_t *ppos)
856 struct es2_ap_dev *es2 = f->f_inode->i_private;
857 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
860 sprintf(tmp_buf, "%d\n", enable);
861 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
864 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
865 size_t count, loff_t *ppos)
869 struct es2_ap_dev *es2 = f->f_inode->i_private;
871 retval = kstrtoint_from_user(buf, count, 10, &enable);
878 usb_log_disable(es2);
883 static const struct file_operations apb_log_enable_fops = {
884 .read = apb_log_enable_read,
885 .write = apb_log_enable_write,
888 static int apb_get_cport_count(struct usb_device *udev)
893 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
897 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
898 GB_APB_REQUEST_CPORT_COUNT,
899 USB_DIR_IN | USB_TYPE_VENDOR |
900 USB_RECIP_INTERFACE, 0, 0, cport_count,
901 sizeof(*cport_count), ES2_TIMEOUT);
902 if (retval != sizeof(*cport_count)) {
903 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
912 retval = le16_to_cpu(*cport_count);
914 /* We need to fit a CPort ID in one byte of a message header */
915 if (retval > U8_MAX) {
917 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
926 * The ES2 USB Bridge device has 15 endpoints
927 * 1 Control - usual USB stuff + AP -> APBridgeA messages
928 * 7 Bulk IN - CPort data in
929 * 7 Bulk OUT - CPort data out
931 static int ap_probe(struct usb_interface *interface,
932 const struct usb_device_id *id)
934 struct es2_ap_dev *es2;
935 struct gb_host_device *hd;
936 struct usb_device *udev;
937 struct usb_host_interface *iface_desc;
938 struct usb_endpoint_descriptor *endpoint;
941 int retval = -ENOMEM;
946 udev = usb_get_dev(interface_to_usbdev(interface));
948 num_cports = apb_get_cport_count(udev);
949 if (num_cports < 0) {
951 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
956 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
964 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
965 * won't be allocated dynamically.
968 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
969 } while (cport_id > 0);
973 es2->usb_intf = interface;
975 spin_lock_init(&es2->cport_out_urb_lock);
976 INIT_KFIFO(es2->apb_log_fifo);
977 usb_set_intfdata(interface, es2);
979 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
981 if (!es2->cport_to_ep) {
986 /* find all bulk endpoints */
987 iface_desc = interface->cur_altsetting;
988 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
989 endpoint = &iface_desc->endpoint[i].desc;
991 if (usb_endpoint_is_bulk_in(endpoint)) {
992 es2->cport_in[bulk_in++].endpoint =
993 endpoint->bEndpointAddress;
994 } else if (usb_endpoint_is_bulk_out(endpoint)) {
995 es2->cport_out[bulk_out++].endpoint =
996 endpoint->bEndpointAddress;
999 "Unknown endpoint type found, address 0x%02x\n",
1000 endpoint->bEndpointAddress);
1003 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1004 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1008 /* Allocate buffers for our cport in messages */
1009 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1010 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1012 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1016 urb = usb_alloc_urb(0, GFP_KERNEL);
1019 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1023 usb_fill_bulk_urb(urb, udev,
1024 usb_rcvbulkpipe(udev,
1025 cport_in->endpoint),
1026 buffer, ES2_GBUF_MSG_SIZE_MAX,
1027 cport_in_callback, hd);
1028 cport_in->urb[i] = urb;
1029 cport_in->buffer[i] = buffer;
1033 /* Allocate urbs for our CPort OUT messages */
1034 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1037 urb = usb_alloc_urb(0, GFP_KERNEL);
1041 es2->cport_out_urb[i] = urb;
1042 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1045 /* XXX We will need to rename this per APB */
1046 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1047 (S_IWUSR | S_IRUGO),
1048 gb_debugfs_get(), es2,
1049 &apb_log_enable_fops);
1051 retval = gb_hd_add(hd);
1055 for (i = 0; i < NUM_BULKS; ++i) {
1056 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1058 goto err_disable_cport_in;
1063 err_disable_cport_in:
1064 for (--i; i >= 0; --i)
1065 es2_cport_in_disable(es2, &es2->cport_in[i]);
1073 static void ap_disconnect(struct usb_interface *interface)
1075 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1080 for (i = 0; i < NUM_BULKS; ++i)
1081 es2_cport_in_disable(es2, &es2->cport_in[i]);
1086 static struct usb_driver es2_ap_driver = {
1087 .name = "es2_ap_driver",
1089 .disconnect = ap_disconnect,
1090 .id_table = id_table,
1094 module_usb_driver(es2_ap_driver);
1096 MODULE_LICENSE("GPL v2");
1097 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");