greybus: es-drivers: add outbound timestamp to connection
[linux-2.6-microblaze.git] / drivers / staging / greybus / es2.c
1 /*
2  * Greybus "AP" USB driver for "ES2" controller chips
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
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>
15
16 #include "greybus.h"
17 #include "svc_msg.h"
18 #include "kernel_ver.h"
19 #include "connection.h"
20
21 /* Memory sizes for the buffers sent to/from the ES1 controller */
22 #define ES1_SVC_MSG_SIZE        (sizeof(struct svc_msg) + SZ_64K)
23 #define ES1_GBUF_MSG_SIZE_MAX   2048
24
25 static const struct usb_device_id id_table[] = {
26         /* Made up numbers for the SVC USB Bridge in ES2 */
27         { USB_DEVICE(0xffff, 0x0002) },
28         { },
29 };
30 MODULE_DEVICE_TABLE(usb, id_table);
31
32 #define APB1_LOG_SIZE           SZ_16K
33 static struct dentry *apb1_log_dentry;
34 static struct dentry *apb1_log_enable_dentry;
35 static struct task_struct *apb1_log_task;
36 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
37
38 /* Number of cport present on USB bridge */
39 #define CPORT_MAX               44
40
41 /* Number of bulk in and bulk out couple */
42 #define NUM_BULKS               7
43
44 /*
45  * Number of CPort IN urbs in flight at any point in time.
46  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
47  * flight.
48  */
49 #define NUM_CPORT_IN_URB        4
50
51 /* Number of CPort OUT urbs in flight at any point in time.
52  * Adjust if we get messages saying we are out of urbs in the system log.
53  */
54 #define NUM_CPORT_OUT_URB       (8 * NUM_BULKS)
55
56 /* vendor request AP message */
57 #define REQUEST_SVC             0x01
58
59 /* vendor request APB1 log */
60 #define REQUEST_LOG             0x02
61
62 /* vendor request to map a cport to bulk in and bulk out endpoints */
63 #define REQUEST_EP_MAPPING      0x03
64
65 /*
66  * @endpoint: bulk in endpoint for CPort data
67  * @urb: array of urbs for the CPort in messages
68  * @buffer: array of buffers for the @cport_in_urb urbs
69  */
70 struct es1_cport_in {
71         __u8 endpoint;
72         struct urb *urb[NUM_CPORT_IN_URB];
73         u8 *buffer[NUM_CPORT_IN_URB];
74 };
75
76 /*
77  * @endpoint: bulk out endpoint for CPort data
78  */
79 struct es1_cport_out {
80         __u8 endpoint;
81 };
82
83 /**
84  * es1_ap_dev - ES1 USB Bridge to AP structure
85  * @usb_dev: pointer to the USB device we are.
86  * @usb_intf: pointer to the USB interface we are bound to.
87  * @hd: pointer to our greybus_host_device structure
88  * @control_endpoint: endpoint to send data to SVC
89  * @svc_endpoint: endpoint for SVC data in
90
91  * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
92  * @svc_urb: urb for SVC messages coming in on @svc_endpoint
93  * @cport_in: endpoint, urbs and buffer for cport in messages
94  * @cport_out: endpoint for for cport out messages
95  * @cport_out_urb: array of urbs for the CPort out messages
96  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
97  *                      not.
98  * @cport_out_urb_cancelled: array of flags indicating whether the
99  *                      corresponding @cport_out_urb is being cancelled
100  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
101  */
102 struct es1_ap_dev {
103         struct usb_device *usb_dev;
104         struct usb_interface *usb_intf;
105         struct greybus_host_device *hd;
106
107         __u8 control_endpoint;
108         __u8 svc_endpoint;
109
110         u8 *svc_buffer;
111         struct urb *svc_urb;
112
113         struct es1_cport_in cport_in[NUM_BULKS];
114         struct es1_cport_out cport_out[NUM_BULKS];
115         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
116         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
117         bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
118         spinlock_t cport_out_urb_lock;
119
120         int cport_to_ep[CPORT_MAX];
121 };
122
123 struct cport_to_ep {
124         __le16 cport_id;
125         __u8 endpoint_in;
126         __u8 endpoint_out;
127 };
128
129 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
130 {
131         return (struct es1_ap_dev *)&hd->hd_priv;
132 }
133
134 static void cport_out_callback(struct urb *urb);
135 static void usb_log_enable(struct es1_ap_dev *es1);
136 static void usb_log_disable(struct es1_ap_dev *es1);
137
138 static int cport_to_ep(struct es1_ap_dev *es1, u16 cport_id)
139 {
140         if (cport_id >= CPORT_MAX)
141                 return 0;
142         return es1->cport_to_ep[cport_id];
143 }
144
145 #define ES1_TIMEOUT     500     /* 500 ms for the SVC to do something */
146 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
147 {
148         struct es1_ap_dev *es1 = hd_to_es1(hd);
149         int retval;
150
151         /* SVC messages go down our control pipe */
152         retval = usb_control_msg(es1->usb_dev,
153                                  usb_sndctrlpipe(es1->usb_dev,
154                                                  es1->control_endpoint),
155                                  REQUEST_SVC,
156                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
157                                  0x00, 0x00,
158                                  (char *)svc_msg,
159                                  sizeof(*svc_msg),
160                                  ES1_TIMEOUT);
161         if (retval != sizeof(*svc_msg))
162                 return retval;
163
164         return 0;
165 }
166
167 static int ep_in_use(struct es1_ap_dev *es1, int bulk_ep_set)
168 {
169         int i;
170
171         for (i = 0; i < CPORT_MAX; i++) {
172                 if (es1->cport_to_ep[i] == bulk_ep_set)
173                         return 1;
174         }
175         return 0;
176 }
177
178 int map_cport_to_ep(struct es1_ap_dev *es1,
179                                 u16 cport_id, int bulk_ep_set)
180 {
181         int retval;
182         struct cport_to_ep *cport_to_ep;
183
184         if (bulk_ep_set == 0 || bulk_ep_set >= NUM_BULKS)
185                 return -EINVAL;
186         if (cport_id >= CPORT_MAX)
187                 return -EINVAL;
188         if (bulk_ep_set && ep_in_use(es1, bulk_ep_set))
189                 return -EINVAL;
190
191         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
192         if (!cport_to_ep)
193                 return -ENOMEM;
194
195         es1->cport_to_ep[cport_id] = bulk_ep_set;
196         cport_to_ep->cport_id = cpu_to_le16(cport_id);
197         cport_to_ep->endpoint_in = es1->cport_in[bulk_ep_set].endpoint;
198         cport_to_ep->endpoint_out = es1->cport_out[bulk_ep_set].endpoint;
199
200         retval = usb_control_msg(es1->usb_dev,
201                                  usb_sndctrlpipe(es1->usb_dev,
202                                                  es1->control_endpoint),
203                                  REQUEST_EP_MAPPING,
204                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
205                                  0x00, 0x00,
206                                  (char *)cport_to_ep,
207                                  sizeof(*cport_to_ep),
208                                  ES1_TIMEOUT);
209         if (retval == sizeof(*cport_to_ep))
210                 retval = 0;
211         kfree(cport_to_ep);
212
213         return retval;
214 }
215
216 int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
217 {
218         return map_cport_to_ep(es1, cport_id, 0);
219 }
220
221 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
222 {
223         struct urb *urb = NULL;
224         unsigned long flags;
225         int i;
226
227         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
228
229         /* Look in our pool of allocated urbs first, as that's the "fastest" */
230         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
231                 if (es1->cport_out_urb_busy[i] == false &&
232                                 es1->cport_out_urb_cancelled[i] == false) {
233                         es1->cport_out_urb_busy[i] = true;
234                         urb = es1->cport_out_urb[i];
235                         break;
236                 }
237         }
238         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
239         if (urb)
240                 return urb;
241
242         /*
243          * Crap, pool is empty, complain to the syslog and go allocate one
244          * dynamically as we have to succeed.
245          */
246         dev_err(&es1->usb_dev->dev,
247                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
248         return usb_alloc_urb(0, gfp_mask);
249 }
250
251 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
252 {
253         unsigned long flags;
254         int i;
255         /*
256          * See if this was an urb in our pool, if so mark it "free", otherwise
257          * we need to free it ourselves.
258          */
259         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
260         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
261                 if (urb == es1->cport_out_urb[i]) {
262                         es1->cport_out_urb_busy[i] = false;
263                         urb = NULL;
264                         break;
265                 }
266         }
267         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
268
269         /* If urb is not NULL, then we need to free this urb */
270         usb_free_urb(urb);
271 }
272
273 /*
274  * We (ab)use the operation-message header pad bytes to transfer the
275  * cport id in order to minimise overhead.
276  */
277 static void
278 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
279 {
280         header->pad[0] = cport_id;
281 }
282
283 /* Clear the pad bytes used for the CPort id */
284 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
285 {
286         header->pad[0] = 0;
287 }
288
289 /* Extract the CPort id packed into the header, and clear it */
290 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
291 {
292         u16 cport_id = header->pad[0];
293
294         gb_message_cport_clear(header);
295
296         return cport_id;
297 }
298
299 /*
300  * Returns zero if the message was successfully queued, or a negative errno
301  * otherwise.
302  */
303 static int message_send(struct greybus_host_device *hd, u16 cport_id,
304                         struct gb_message *message, gfp_t gfp_mask)
305 {
306         struct es1_ap_dev *es1 = hd_to_es1(hd);
307         struct usb_device *udev = es1->usb_dev;
308         size_t buffer_size;
309         int retval;
310         struct urb *urb;
311         int bulk_ep_set;
312         unsigned long flags;
313
314         /*
315          * The data actually transferred will include an indication
316          * of where the data should be sent.  Do one last check of
317          * the target CPort id before filling it in.
318          */
319         if (!cport_id_valid(cport_id)) {
320                 pr_err("invalid destination cport 0x%02x\n", cport_id);
321                 return -EINVAL;
322         }
323
324         /* Find a free urb */
325         urb = next_free_urb(es1, gfp_mask);
326         if (!urb)
327                 return -ENOMEM;
328
329         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
330         message->hcpriv = urb;
331         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
332
333         /* Pack the cport id into the message header */
334         gb_message_cport_pack(message->header, cport_id);
335
336         buffer_size = sizeof(*message->header) + message->payload_size;
337
338         bulk_ep_set = cport_to_ep(es1, cport_id);
339         usb_fill_bulk_urb(urb, udev,
340                           usb_sndbulkpipe(udev,
341                                           es1->cport_out[bulk_ep_set].endpoint),
342                           message->buffer, buffer_size,
343                           cport_out_callback, message);
344         gb_connection_push_timestamp(message->operation->connection);
345         retval = usb_submit_urb(urb, gfp_mask);
346         if (retval) {
347                 pr_err("error %d submitting URB\n", retval);
348
349                 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
350                 message->hcpriv = NULL;
351                 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
352
353                 free_urb(es1, urb);
354                 gb_message_cport_clear(message->header);
355
356                 return retval;
357         }
358
359         return 0;
360 }
361
362 /*
363  * Can not be called in atomic context.
364  */
365 static void message_cancel(struct gb_message *message)
366 {
367         struct greybus_host_device *hd = message->operation->connection->hd;
368         struct es1_ap_dev *es1 = hd_to_es1(hd);
369         struct urb *urb;
370         int i;
371
372         might_sleep();
373
374         spin_lock_irq(&es1->cport_out_urb_lock);
375         urb = message->hcpriv;
376
377         /* Prevent dynamically allocated urb from being deallocated. */
378         usb_get_urb(urb);
379
380         /* Prevent pre-allocated urb from being reused. */
381         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
382                 if (urb == es1->cport_out_urb[i]) {
383                         es1->cport_out_urb_cancelled[i] = true;
384                         break;
385                 }
386         }
387         spin_unlock_irq(&es1->cport_out_urb_lock);
388
389         usb_kill_urb(urb);
390
391         if (i < NUM_CPORT_OUT_URB) {
392                 spin_lock_irq(&es1->cport_out_urb_lock);
393                 es1->cport_out_urb_cancelled[i] = false;
394                 spin_unlock_irq(&es1->cport_out_urb_lock);
395         }
396
397         usb_free_urb(urb);
398 }
399
400 static struct greybus_host_driver es1_driver = {
401         .hd_priv_size           = sizeof(struct es1_ap_dev),
402         .message_send           = message_send,
403         .message_cancel         = message_cancel,
404         .submit_svc             = submit_svc,
405 };
406
407 /* Common function to report consistent warnings based on URB status */
408 static int check_urb_status(struct urb *urb)
409 {
410         struct device *dev = &urb->dev->dev;
411         int status = urb->status;
412
413         switch (status) {
414         case 0:
415                 return 0;
416
417         case -EOVERFLOW:
418                 dev_err(dev, "%s: overflow actual length is %d\n",
419                         __func__, urb->actual_length);
420         case -ECONNRESET:
421         case -ENOENT:
422         case -ESHUTDOWN:
423         case -EILSEQ:
424         case -EPROTO:
425                 /* device is gone, stop sending */
426                 return status;
427         }
428         dev_err(dev, "%s: unknown status %d\n", __func__, status);
429
430         return -EAGAIN;
431 }
432
433 static void ap_disconnect(struct usb_interface *interface)
434 {
435         struct es1_ap_dev *es1;
436         struct usb_device *udev;
437         int bulk_in;
438         int i;
439
440         es1 = usb_get_intfdata(interface);
441         if (!es1)
442                 return;
443
444         usb_log_disable(es1);
445
446         /* Tear down everything! */
447         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
448                 struct urb *urb = es1->cport_out_urb[i];
449
450                 if (!urb)
451                         break;
452                 usb_kill_urb(urb);
453                 usb_free_urb(urb);
454                 es1->cport_out_urb[i] = NULL;
455                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
456         }
457
458         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
459                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
460                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
461                         struct urb *urb = cport_in->urb[i];
462
463                         if (!urb)
464                                 break;
465                         usb_kill_urb(urb);
466                         usb_free_urb(urb);
467                         kfree(cport_in->buffer[i]);
468                         cport_in->buffer[i] = NULL;
469                 }
470         }
471
472         usb_kill_urb(es1->svc_urb);
473         usb_free_urb(es1->svc_urb);
474         es1->svc_urb = NULL;
475         kfree(es1->svc_buffer);
476         es1->svc_buffer = NULL;
477
478         usb_set_intfdata(interface, NULL);
479         udev = es1->usb_dev;
480         greybus_remove_hd(es1->hd);
481
482         usb_put_dev(udev);
483 }
484
485 /* Callback for when we get a SVC message */
486 static void svc_in_callback(struct urb *urb)
487 {
488         struct greybus_host_device *hd = urb->context;
489         struct device *dev = &urb->dev->dev;
490         int status = check_urb_status(urb);
491         int retval;
492
493         if (status) {
494                 if ((status == -EAGAIN) || (status == -EPROTO))
495                         goto exit;
496                 dev_err(dev, "urb svc in error %d (dropped)\n", status);
497                 return;
498         }
499
500         /* We have a message, create a new message structure, add it to the
501          * list, and wake up our thread that will process the messages.
502          */
503         greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
504
505 exit:
506         /* resubmit the urb to get more messages */
507         retval = usb_submit_urb(urb, GFP_ATOMIC);
508         if (retval)
509                 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
510 }
511
512 static void cport_in_callback(struct urb *urb)
513 {
514         struct greybus_host_device *hd = urb->context;
515         struct device *dev = &urb->dev->dev;
516         struct gb_operation_msg_hdr *header;
517         int status = check_urb_status(urb);
518         int retval;
519         u16 cport_id;
520
521         if (status) {
522                 if ((status == -EAGAIN) || (status == -EPROTO))
523                         goto exit;
524                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
525                 return;
526         }
527
528         if (urb->actual_length < sizeof(*header)) {
529                 dev_err(dev, "%s: short message received\n", __func__);
530                 goto exit;
531         }
532
533         /* Extract the CPort id, which is packed in the message header */
534         header = urb->transfer_buffer;
535         cport_id = gb_message_cport_unpack(header);
536
537         if (cport_id_valid(cport_id))
538                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
539                                                         urb->actual_length);
540         else
541                 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
542                                 __func__, cport_id);
543 exit:
544         /* put our urb back in the request pool */
545         retval = usb_submit_urb(urb, GFP_ATOMIC);
546         if (retval)
547                 dev_err(dev, "%s: error %d in submitting urb.\n",
548                         __func__, retval);
549 }
550
551 static void cport_out_callback(struct urb *urb)
552 {
553         struct gb_message *message = urb->context;
554         struct greybus_host_device *hd = message->operation->connection->hd;
555         struct es1_ap_dev *es1 = hd_to_es1(hd);
556         int status = check_urb_status(urb);
557         unsigned long flags;
558
559         gb_message_cport_clear(message->header);
560
561         /*
562          * Tell the submitter that the message send (attempt) is
563          * complete, and report the status.
564          */
565         greybus_message_sent(hd, message, status);
566
567         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
568         message->hcpriv = NULL;
569         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
570
571         free_urb(es1, urb);
572 }
573
574 #define APB1_LOG_MSG_SIZE       64
575 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
576 {
577         int retval;
578
579         /* SVC messages go down our control pipe */
580         do {
581                 retval = usb_control_msg(es1->usb_dev,
582                                         usb_rcvctrlpipe(es1->usb_dev,
583                                                         es1->control_endpoint),
584                                         REQUEST_LOG,
585                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
586                                         0x00, 0x00,
587                                         buf,
588                                         APB1_LOG_MSG_SIZE,
589                                         ES1_TIMEOUT);
590                 if (retval > 0)
591                         kfifo_in(&apb1_log_fifo, buf, retval);
592         } while (retval > 0);
593 }
594
595 static int apb1_log_poll(void *data)
596 {
597         struct es1_ap_dev *es1 = data;
598         char *buf;
599
600         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
601         if (!buf)
602                 return -ENOMEM;
603
604         while (!kthread_should_stop()) {
605                 msleep(1000);
606                 apb1_log_get(es1, buf);
607         }
608
609         kfree(buf);
610
611         return 0;
612 }
613
614 static ssize_t apb1_log_read(struct file *f, char __user *buf,
615                                 size_t count, loff_t *ppos)
616 {
617         ssize_t ret;
618         size_t copied;
619         char *tmp_buf;
620
621         if (count > APB1_LOG_SIZE)
622                 count = APB1_LOG_SIZE;
623
624         tmp_buf = kmalloc(count, GFP_KERNEL);
625         if (!tmp_buf)
626                 return -ENOMEM;
627
628         copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
629         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
630
631         kfree(tmp_buf);
632
633         return ret;
634 }
635
636 static const struct file_operations apb1_log_fops = {
637         .read   = apb1_log_read,
638 };
639
640 static void usb_log_enable(struct es1_ap_dev *es1)
641 {
642         if (!IS_ERR_OR_NULL(apb1_log_task))
643                 return;
644
645         /* get log from APB1 */
646         apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
647         if (IS_ERR(apb1_log_task))
648                 return;
649         apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
650                                                 gb_debugfs_get(), NULL,
651                                                 &apb1_log_fops);
652 }
653
654 static void usb_log_disable(struct es1_ap_dev *es1)
655 {
656         if (IS_ERR_OR_NULL(apb1_log_task))
657                 return;
658
659         debugfs_remove(apb1_log_dentry);
660         apb1_log_dentry = NULL;
661
662         kthread_stop(apb1_log_task);
663         apb1_log_task = NULL;
664 }
665
666 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
667                                 size_t count, loff_t *ppos)
668 {
669         char tmp_buf[3];
670         int enable = !IS_ERR_OR_NULL(apb1_log_task);
671
672         sprintf(tmp_buf, "%d\n", enable);
673         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
674 }
675
676 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
677                                 size_t count, loff_t *ppos)
678 {
679         int enable;
680         ssize_t retval;
681         struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
682
683         retval = kstrtoint_from_user(buf, count, 10, &enable);
684         if (retval)
685                 return retval;
686
687         if (enable)
688                 usb_log_enable(es1);
689         else
690                 usb_log_disable(es1);
691
692         return count;
693 }
694
695 static const struct file_operations apb1_log_enable_fops = {
696         .read   = apb1_log_enable_read,
697         .write  = apb1_log_enable_write,
698 };
699
700 /*
701  * The ES1 USB Bridge device contains 4 endpoints
702  * 1 Control - usual USB stuff + AP -> SVC messages
703  * 1 Interrupt IN - SVC -> AP messages
704  * 1 Bulk IN - CPort data in
705  * 1 Bulk OUT - CPort data out
706  */
707 static int ap_probe(struct usb_interface *interface,
708                     const struct usb_device_id *id)
709 {
710         struct es1_ap_dev *es1;
711         struct greybus_host_device *hd;
712         struct usb_device *udev;
713         struct usb_host_interface *iface_desc;
714         struct usb_endpoint_descriptor *endpoint;
715         bool int_in_found = false;
716         int bulk_in = 0;
717         int bulk_out = 0;
718         int retval = -ENOMEM;
719         int i;
720         u16 endo_id = 0x4755;   // FIXME - get endo "ID" from the SVC
721         u8 ap_intf_id = 0x01;   // FIXME - get endo "ID" from the SVC
722         u8 svc_interval = 0;
723
724         /* We need to fit a CPort ID in one byte of a message header */
725         BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
726
727         udev = usb_get_dev(interface_to_usbdev(interface));
728
729         hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
730         if (IS_ERR(hd)) {
731                 usb_put_dev(udev);
732                 return PTR_ERR(hd);
733         }
734
735         es1 = hd_to_es1(hd);
736         es1->hd = hd;
737         es1->usb_intf = interface;
738         es1->usb_dev = udev;
739         spin_lock_init(&es1->cport_out_urb_lock);
740         usb_set_intfdata(interface, es1);
741
742         /* Control endpoint is the pipe to talk to this AP, so save it off */
743         endpoint = &udev->ep0.desc;
744         es1->control_endpoint = endpoint->bEndpointAddress;
745
746         /* find all 3 of our endpoints */
747         iface_desc = interface->cur_altsetting;
748         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
749                 endpoint = &iface_desc->endpoint[i].desc;
750
751                 if (usb_endpoint_is_int_in(endpoint)) {
752                         es1->svc_endpoint = endpoint->bEndpointAddress;
753                         svc_interval = endpoint->bInterval;
754                         int_in_found = true;
755                 } else if (usb_endpoint_is_bulk_in(endpoint)) {
756                         es1->cport_in[bulk_in++].endpoint =
757                                 endpoint->bEndpointAddress;
758                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
759                         es1->cport_out[bulk_out++].endpoint =
760                                 endpoint->bEndpointAddress;
761                 } else {
762                         dev_err(&udev->dev,
763                                 "Unknown endpoint type found, address %x\n",
764                                 endpoint->bEndpointAddress);
765                 }
766         }
767         if ((int_in_found == false) ||
768             (bulk_in == 0) ||
769             (bulk_out == 0)) {
770                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
771                 goto error;
772         }
773
774         /* Create our buffer and URB to get SVC messages, and start it up */
775         es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
776         if (!es1->svc_buffer)
777                 goto error;
778
779         es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
780         if (!es1->svc_urb)
781                 goto error;
782
783         usb_fill_int_urb(es1->svc_urb, udev,
784                          usb_rcvintpipe(udev, es1->svc_endpoint),
785                          es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
786                          hd, svc_interval);
787
788         /* Allocate buffers for our cport in messages and start them up */
789         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
790                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
791                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
792                         struct urb *urb;
793                         u8 *buffer;
794
795                         urb = usb_alloc_urb(0, GFP_KERNEL);
796                         if (!urb)
797                                 goto error;
798                         buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
799                         if (!buffer)
800                                 goto error;
801
802                         usb_fill_bulk_urb(urb, udev,
803                                           usb_rcvbulkpipe(udev,
804                                                           cport_in->endpoint),
805                                           buffer, ES1_GBUF_MSG_SIZE_MAX,
806                                           cport_in_callback, hd);
807                         cport_in->urb[i] = urb;
808                         cport_in->buffer[i] = buffer;
809                         retval = usb_submit_urb(urb, GFP_KERNEL);
810                         if (retval)
811                                 goto error;
812                 }
813         }
814
815         /* Allocate urbs for our CPort OUT messages */
816         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
817                 struct urb *urb;
818
819                 urb = usb_alloc_urb(0, GFP_KERNEL);
820                 if (!urb)
821                         goto error;
822
823                 es1->cport_out_urb[i] = urb;
824                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
825         }
826
827         /*
828          * XXX Soon this will be initiated later, with a combination
829          * XXX of a Control protocol probe operation and a
830          * XXX subsequent Control protocol connected operation for
831          * XXX the SVC connection.  At that point we know we're
832          * XXX properly connected to an Endo.
833          */
834         retval = greybus_endo_setup(hd, endo_id, ap_intf_id);
835         if (retval)
836                 goto error;
837
838         /* Start up our svc urb, which allows events to start flowing */
839         retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
840         if (retval)
841                 goto error;
842
843         apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
844                                                         (S_IWUSR | S_IRUGO),
845                                                         gb_debugfs_get(), es1,
846                                                         &apb1_log_enable_fops);
847         return 0;
848 error:
849         ap_disconnect(interface);
850
851         return retval;
852 }
853
854 static struct usb_driver es1_ap_driver = {
855         .name =         "es2_ap_driver",
856         .probe =        ap_probe,
857         .disconnect =   ap_disconnect,
858         .id_table =     id_table,
859 };
860
861 module_usb_driver(es1_ap_driver);
862
863 MODULE_LICENSE("GPL v2");
864 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");