greybus: es2: Fix endian conversion issues
[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 "es2.h"
17 #include "greybus.h"
18 #include "kernel_ver.h"
19 #include "connection.h"
20 #include "greybus_trace.h"
21
22 /* Memory sizes for the buffers sent to/from the ES2 controller */
23 #define ES2_GBUF_MSG_SIZE_MAX   2048
24
25 static const struct usb_device_id id_table[] = {
26         { USB_DEVICE(0x18d1, 0x1eaf) },
27         { },
28 };
29 MODULE_DEVICE_TABLE(usb, id_table);
30
31 #define APB1_LOG_SIZE           SZ_16K
32
33 /* Number of bulk in and bulk out couple */
34 #define NUM_BULKS               7
35
36 /*
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
39  * flight.
40  */
41 #define NUM_CPORT_IN_URB        4
42
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.
45  */
46 #define NUM_CPORT_OUT_URB       (8 * NUM_BULKS)
47
48 /* vendor request APB1 log */
49 #define REQUEST_LOG             0x02
50
51 /* vendor request to map a cport to bulk in and bulk out endpoints */
52 #define REQUEST_EP_MAPPING      0x03
53
54 /* vendor request to get the number of cports available */
55 #define REQUEST_CPORT_COUNT     0x04
56
57 /* vendor request to reset a cport state */
58 #define REQUEST_RESET_CPORT     0x05
59
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
63
64 /* vendor request to control the CSI transmitter */
65 #define REQUEST_CSI_TX_CONTROL  0x08
66
67 /*
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
71  */
72 struct es2_cport_in {
73         __u8 endpoint;
74         struct urb *urb[NUM_CPORT_IN_URB];
75         u8 *buffer[NUM_CPORT_IN_URB];
76 };
77
78 /*
79  * @endpoint: bulk out endpoint for CPort data
80  */
81 struct es2_cport_out {
82         __u8 endpoint;
83 };
84
85 /**
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
90
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
95  *                      not.
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"
99  *
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
104  */
105 struct es2_ap_dev {
106         struct usb_device *usb_dev;
107         struct usb_interface *usb_intf;
108         struct gb_host_device *hd;
109
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;
116
117         int *cport_to_ep;
118
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);
123 };
124
125 /**
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
130  */
131 struct cport_to_ep {
132         __le16 cport_id;
133         __u8 endpoint_in;
134         __u8 endpoint_out;
135 };
136
137 struct es2_ap_csi_config_request {
138         u8 csi_id;
139         u8 clock_mode;
140         u8 num_lanes;
141         u8 padding;
142         __le32 bus_freq;
143 } __attribute__((__packed__));
144
145 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
146 {
147         return (struct es2_ap_dev *)&hd->hd_priv;
148 }
149
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);
153
154 /* Get the endpoints pair mapped to the cport */
155 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
156 {
157         if (cport_id >= es2->hd->num_cports)
158                 return 0;
159         return es2->cport_to_ep[cport_id];
160 }
161
162 #define ES2_TIMEOUT     500     /* 500 ms for the SVC to do something */
163
164 /* Disable for now until we work all of this out to keep a warning-free build */
165 #if 0
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)
168 {
169         int i;
170
171         for (i = 0; i < es2->hd->num_cports; i++) {
172                 if (es2->cport_to_ep[i] == ep_pair)
173                         return 1;
174         }
175         return 0;
176 }
177
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)
181 {
182         int retval;
183         struct cport_to_ep *cport_to_ep;
184
185         if (ep_pair < 0 || ep_pair >= NUM_BULKS)
186                 return -EINVAL;
187         if (cport_id >= es2->hd->num_cports)
188                 return -EINVAL;
189         if (ep_pair && ep_pair_in_use(es2, ep_pair))
190                 return -EINVAL;
191
192         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
193         if (!cport_to_ep)
194                 return -ENOMEM;
195
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;
200
201         retval = usb_control_msg(es2->usb_dev,
202                                  usb_sndctrlpipe(es2->usb_dev, 0),
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                                  ES2_TIMEOUT);
209         if (retval == sizeof(*cport_to_ep))
210                 retval = 0;
211         kfree(cport_to_ep);
212
213         return retval;
214 }
215
216 /* Unmap a cport: use the muxed endpoints pair */
217 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
218 {
219         return map_cport_to_ep(es2, cport_id, 0);
220 }
221 #endif
222
223 int es2_ap_csi_setup(struct gb_host_device *hd, bool start,
224                      struct es2_ap_csi_config *cfg)
225 {
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;
229         int retval;
230
231         cfg_req.csi_id = cfg->csi_id;
232
233         if (start) {
234                 cfg_req.clock_mode = cfg->clock_mode;
235                 cfg_req.num_lanes = cfg->num_lanes;
236                 cfg_req.padding = 0;
237                 cfg_req.bus_freq = cpu_to_le32(cfg->bus_freq);
238         } else {
239                 cfg_req.clock_mode = 0;
240                 cfg_req.num_lanes = 0;
241                 cfg_req.padding = 0;
242                 cfg_req.bus_freq = 0;
243         }
244
245         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
246                                  REQUEST_CSI_TX_CONTROL,
247                                  USB_DIR_OUT | USB_TYPE_VENDOR |
248                                  USB_RECIP_INTERFACE, 0, 0, &cfg_req,
249                                  sizeof(cfg_req), ES2_TIMEOUT);
250         if (retval < 0) {
251                 dev_err(&udev->dev, "failed to setup csi: %d\n", retval);
252                 return retval;
253         }
254
255         return 0;
256 }
257 EXPORT_SYMBOL_GPL(es2_ap_csi_setup);
258
259 static int es2_cport_in_enable(struct es2_ap_dev *es2,
260                                 struct es2_cport_in *cport_in)
261 {
262         struct urb *urb;
263         int ret;
264         int i;
265
266         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
267                 urb = cport_in->urb[i];
268
269                 ret = usb_submit_urb(urb, GFP_KERNEL);
270                 if (ret) {
271                         dev_err(&es2->usb_dev->dev,
272                                         "failed to submit in-urb: %d\n", ret);
273                         goto err_kill_urbs;
274                 }
275         }
276
277         return 0;
278
279 err_kill_urbs:
280         for (--i; i >= 0; --i) {
281                 urb = cport_in->urb[i];
282                 usb_kill_urb(urb);
283         }
284
285         return ret;
286 }
287
288 static void es2_cport_in_disable(struct es2_ap_dev *es2,
289                                 struct es2_cport_in *cport_in)
290 {
291         struct urb *urb;
292         int i;
293
294         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
295                 urb = cport_in->urb[i];
296                 usb_kill_urb(urb);
297         }
298 }
299
300 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
301 {
302         struct urb *urb = NULL;
303         unsigned long flags;
304         int i;
305
306         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
307
308         /* Look in our pool of allocated urbs first, as that's the "fastest" */
309         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
310                 if (es2->cport_out_urb_busy[i] == false &&
311                                 es2->cport_out_urb_cancelled[i] == false) {
312                         es2->cport_out_urb_busy[i] = true;
313                         urb = es2->cport_out_urb[i];
314                         break;
315                 }
316         }
317         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
318         if (urb)
319                 return urb;
320
321         /*
322          * Crap, pool is empty, complain to the syslog and go allocate one
323          * dynamically as we have to succeed.
324          */
325         dev_dbg(&es2->usb_dev->dev,
326                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
327         return usb_alloc_urb(0, gfp_mask);
328 }
329
330 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
331 {
332         unsigned long flags;
333         int i;
334         /*
335          * See if this was an urb in our pool, if so mark it "free", otherwise
336          * we need to free it ourselves.
337          */
338         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
339         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
340                 if (urb == es2->cport_out_urb[i]) {
341                         es2->cport_out_urb_busy[i] = false;
342                         urb = NULL;
343                         break;
344                 }
345         }
346         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
347
348         /* If urb is not NULL, then we need to free this urb */
349         usb_free_urb(urb);
350 }
351
352 /*
353  * We (ab)use the operation-message header pad bytes to transfer the
354  * cport id in order to minimise overhead.
355  */
356 static void
357 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
358 {
359         header->pad[0] = cport_id;
360 }
361
362 /* Clear the pad bytes used for the CPort id */
363 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
364 {
365         header->pad[0] = 0;
366 }
367
368 /* Extract the CPort id packed into the header, and clear it */
369 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
370 {
371         u16 cport_id = header->pad[0];
372
373         gb_message_cport_clear(header);
374
375         return cport_id;
376 }
377
378 /*
379  * Returns zero if the message was successfully queued, or a negative errno
380  * otherwise.
381  */
382 static int message_send(struct gb_host_device *hd, u16 cport_id,
383                         struct gb_message *message, gfp_t gfp_mask)
384 {
385         struct es2_ap_dev *es2 = hd_to_es2(hd);
386         struct usb_device *udev = es2->usb_dev;
387         size_t buffer_size;
388         int retval;
389         struct urb *urb;
390         int ep_pair;
391         unsigned long flags;
392
393         /*
394          * The data actually transferred will include an indication
395          * of where the data should be sent.  Do one last check of
396          * the target CPort id before filling it in.
397          */
398         if (!cport_id_valid(hd, cport_id)) {
399                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
400                 return -EINVAL;
401         }
402
403         /* Find a free urb */
404         urb = next_free_urb(es2, gfp_mask);
405         if (!urb)
406                 return -ENOMEM;
407
408         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
409         message->hcpriv = urb;
410         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
411
412         /* Pack the cport id into the message header */
413         gb_message_cport_pack(message->header, cport_id);
414
415         buffer_size = sizeof(*message->header) + message->payload_size;
416
417         ep_pair = cport_to_ep_pair(es2, cport_id);
418         usb_fill_bulk_urb(urb, udev,
419                           usb_sndbulkpipe(udev,
420                                           es2->cport_out[ep_pair].endpoint),
421                           message->buffer, buffer_size,
422                           cport_out_callback, message);
423         urb->transfer_flags |= URB_ZERO_PACKET;
424         trace_gb_host_device_send(hd, cport_id, buffer_size);
425         retval = usb_submit_urb(urb, gfp_mask);
426         if (retval) {
427                 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
428
429                 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
430                 message->hcpriv = NULL;
431                 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
432
433                 free_urb(es2, urb);
434                 gb_message_cport_clear(message->header);
435
436                 return retval;
437         }
438
439         return 0;
440 }
441
442 /*
443  * Can not be called in atomic context.
444  */
445 static void message_cancel(struct gb_message *message)
446 {
447         struct gb_host_device *hd = message->operation->connection->hd;
448         struct es2_ap_dev *es2 = hd_to_es2(hd);
449         struct urb *urb;
450         int i;
451
452         might_sleep();
453
454         spin_lock_irq(&es2->cport_out_urb_lock);
455         urb = message->hcpriv;
456
457         /* Prevent dynamically allocated urb from being deallocated. */
458         usb_get_urb(urb);
459
460         /* Prevent pre-allocated urb from being reused. */
461         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
462                 if (urb == es2->cport_out_urb[i]) {
463                         es2->cport_out_urb_cancelled[i] = true;
464                         break;
465                 }
466         }
467         spin_unlock_irq(&es2->cport_out_urb_lock);
468
469         usb_kill_urb(urb);
470
471         if (i < NUM_CPORT_OUT_URB) {
472                 spin_lock_irq(&es2->cport_out_urb_lock);
473                 es2->cport_out_urb_cancelled[i] = false;
474                 spin_unlock_irq(&es2->cport_out_urb_lock);
475         }
476
477         usb_free_urb(urb);
478 }
479
480 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
481 {
482         struct es2_ap_dev *es2 = hd_to_es2(hd);
483         struct usb_device *udev = es2->usb_dev;
484         int retval;
485
486         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
487                                  REQUEST_RESET_CPORT,
488                                  USB_DIR_OUT | USB_TYPE_VENDOR |
489                                  USB_RECIP_INTERFACE, cport_id, 0,
490                                  NULL, 0, ES2_TIMEOUT);
491         if (retval < 0) {
492                 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
493                         retval);
494                 return retval;
495         }
496
497         return 0;
498 }
499
500 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
501 {
502         int retval;
503
504         if (cport_id != GB_SVC_CPORT_ID) {
505                 retval = cport_reset(hd, cport_id);
506                 if (retval)
507                         return retval;
508         }
509
510         return 0;
511 }
512
513 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
514 {
515         int retval;
516         struct es2_ap_dev *es2 = hd_to_es2(hd);
517         struct usb_device *udev = es2->usb_dev;
518
519         if (!cport_id_valid(hd, cport_id)) {
520                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
521                 return -EINVAL;
522         }
523
524         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
525                                  REQUEST_LATENCY_TAG_EN,
526                                  USB_DIR_OUT | USB_TYPE_VENDOR |
527                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
528                                  0, ES2_TIMEOUT);
529
530         if (retval < 0)
531                 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
532                         cport_id);
533         return retval;
534 }
535
536 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
537 {
538         int retval;
539         struct es2_ap_dev *es2 = hd_to_es2(hd);
540         struct usb_device *udev = es2->usb_dev;
541
542         if (!cport_id_valid(hd, cport_id)) {
543                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
544                 return -EINVAL;
545         }
546
547         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
548                                  REQUEST_LATENCY_TAG_DIS,
549                                  USB_DIR_OUT | USB_TYPE_VENDOR |
550                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
551                                  0, ES2_TIMEOUT);
552
553         if (retval < 0)
554                 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
555                         cport_id);
556         return retval;
557 }
558
559 static struct gb_hd_driver es2_driver = {
560         .hd_priv_size           = sizeof(struct es2_ap_dev),
561         .message_send           = message_send,
562         .message_cancel         = message_cancel,
563         .cport_enable           = cport_enable,
564         .latency_tag_enable     = latency_tag_enable,
565         .latency_tag_disable    = latency_tag_disable,
566 };
567
568 /* Common function to report consistent warnings based on URB status */
569 static int check_urb_status(struct urb *urb)
570 {
571         struct device *dev = &urb->dev->dev;
572         int status = urb->status;
573
574         switch (status) {
575         case 0:
576                 return 0;
577
578         case -EOVERFLOW:
579                 dev_err(dev, "%s: overflow actual length is %d\n",
580                         __func__, urb->actual_length);
581         case -ECONNRESET:
582         case -ENOENT:
583         case -ESHUTDOWN:
584         case -EILSEQ:
585         case -EPROTO:
586                 /* device is gone, stop sending */
587                 return status;
588         }
589         dev_err(dev, "%s: unknown status %d\n", __func__, status);
590
591         return -EAGAIN;
592 }
593
594 static void es2_destroy(struct es2_ap_dev *es2)
595 {
596         struct usb_device *udev;
597         int bulk_in;
598         int i;
599
600         debugfs_remove(es2->apb_log_enable_dentry);
601         usb_log_disable(es2);
602
603         /* Tear down everything! */
604         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
605                 struct urb *urb = es2->cport_out_urb[i];
606
607                 if (!urb)
608                         break;
609                 usb_kill_urb(urb);
610                 usb_free_urb(urb);
611                 es2->cport_out_urb[i] = NULL;
612                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
613         }
614
615         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
616                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
617
618                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
619                         struct urb *urb = cport_in->urb[i];
620
621                         if (!urb)
622                                 break;
623                         usb_free_urb(urb);
624                         kfree(cport_in->buffer[i]);
625                         cport_in->buffer[i] = NULL;
626                 }
627         }
628
629         kfree(es2->cport_to_ep);
630
631         udev = es2->usb_dev;
632         gb_hd_put(es2->hd);
633
634         usb_put_dev(udev);
635 }
636
637 static void ap_disconnect(struct usb_interface *interface)
638 {
639         struct es2_ap_dev *es2 = usb_get_intfdata(interface);
640         int i;
641
642         for (i = 0; i < NUM_BULKS; ++i)
643                 es2_cport_in_disable(es2, &es2->cport_in[i]);
644
645         gb_hd_del(es2->hd);
646
647         es2_destroy(es2);
648 }
649
650 static void cport_in_callback(struct urb *urb)
651 {
652         struct gb_host_device *hd = urb->context;
653         struct device *dev = &urb->dev->dev;
654         struct gb_operation_msg_hdr *header;
655         int status = check_urb_status(urb);
656         int retval;
657         u16 cport_id;
658
659         if (status) {
660                 if ((status == -EAGAIN) || (status == -EPROTO))
661                         goto exit;
662                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
663                 return;
664         }
665
666         if (urb->actual_length < sizeof(*header)) {
667                 dev_err(dev, "short message received\n");
668                 goto exit;
669         }
670
671         /* Extract the CPort id, which is packed in the message header */
672         header = urb->transfer_buffer;
673         cport_id = gb_message_cport_unpack(header);
674
675         if (cport_id_valid(hd, cport_id)) {
676                 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
677                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
678                                                         urb->actual_length);
679         } else {
680                 dev_err(dev, "invalid cport id %u received\n", cport_id);
681         }
682 exit:
683         /* put our urb back in the request pool */
684         retval = usb_submit_urb(urb, GFP_ATOMIC);
685         if (retval)
686                 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
687 }
688
689 static void cport_out_callback(struct urb *urb)
690 {
691         struct gb_message *message = urb->context;
692         struct gb_host_device *hd = message->operation->connection->hd;
693         struct es2_ap_dev *es2 = hd_to_es2(hd);
694         int status = check_urb_status(urb);
695         unsigned long flags;
696
697         gb_message_cport_clear(message->header);
698
699         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
700         message->hcpriv = NULL;
701         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
702
703         /*
704          * Tell the submitter that the message send (attempt) is
705          * complete, and report the status.
706          */
707         greybus_message_sent(hd, message, status);
708
709         free_urb(es2, urb);
710 }
711
712 #define APB1_LOG_MSG_SIZE       64
713 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
714 {
715         int retval;
716
717         /* SVC messages go down our control pipe */
718         do {
719                 retval = usb_control_msg(es2->usb_dev,
720                                         usb_rcvctrlpipe(es2->usb_dev, 0),
721                                         REQUEST_LOG,
722                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
723                                         0x00, 0x00,
724                                         buf,
725                                         APB1_LOG_MSG_SIZE,
726                                         ES2_TIMEOUT);
727                 if (retval > 0)
728                         kfifo_in(&es2->apb_log_fifo, buf, retval);
729         } while (retval > 0);
730 }
731
732 static int apb_log_poll(void *data)
733 {
734         struct es2_ap_dev *es2 = data;
735         char *buf;
736
737         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
738         if (!buf)
739                 return -ENOMEM;
740
741         while (!kthread_should_stop()) {
742                 msleep(1000);
743                 apb_log_get(es2, buf);
744         }
745
746         kfree(buf);
747
748         return 0;
749 }
750
751 static ssize_t apb_log_read(struct file *f, char __user *buf,
752                                 size_t count, loff_t *ppos)
753 {
754         struct es2_ap_dev *es2 = f->f_inode->i_private;
755         ssize_t ret;
756         size_t copied;
757         char *tmp_buf;
758
759         if (count > APB1_LOG_SIZE)
760                 count = APB1_LOG_SIZE;
761
762         tmp_buf = kmalloc(count, GFP_KERNEL);
763         if (!tmp_buf)
764                 return -ENOMEM;
765
766         copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
767         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
768
769         kfree(tmp_buf);
770
771         return ret;
772 }
773
774 static const struct file_operations apb_log_fops = {
775         .read   = apb_log_read,
776 };
777
778 static void usb_log_enable(struct es2_ap_dev *es2)
779 {
780         if (!IS_ERR_OR_NULL(es2->apb_log_task))
781                 return;
782
783         /* get log from APB1 */
784         es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
785         if (IS_ERR(es2->apb_log_task))
786                 return;
787         /* XXX We will need to rename this per APB */
788         es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
789                                                 gb_debugfs_get(), NULL,
790                                                 &apb_log_fops);
791 }
792
793 static void usb_log_disable(struct es2_ap_dev *es2)
794 {
795         if (IS_ERR_OR_NULL(es2->apb_log_task))
796                 return;
797
798         debugfs_remove(es2->apb_log_dentry);
799         es2->apb_log_dentry = NULL;
800
801         kthread_stop(es2->apb_log_task);
802         es2->apb_log_task = NULL;
803 }
804
805 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
806                                 size_t count, loff_t *ppos)
807 {
808         struct es2_ap_dev *es2 = f->f_inode->i_private;
809         int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
810         char tmp_buf[3];
811
812         sprintf(tmp_buf, "%d\n", enable);
813         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
814 }
815
816 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
817                                 size_t count, loff_t *ppos)
818 {
819         int enable;
820         ssize_t retval;
821         struct es2_ap_dev *es2 = f->f_inode->i_private;
822
823         retval = kstrtoint_from_user(buf, count, 10, &enable);
824         if (retval)
825                 return retval;
826
827         if (enable)
828                 usb_log_enable(es2);
829         else
830                 usb_log_disable(es2);
831
832         return count;
833 }
834
835 static const struct file_operations apb_log_enable_fops = {
836         .read   = apb_log_enable_read,
837         .write  = apb_log_enable_write,
838 };
839
840 static int apb_get_cport_count(struct usb_device *udev)
841 {
842         int retval;
843         __le16 *cport_count;
844
845         cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
846         if (!cport_count)
847                 return -ENOMEM;
848
849         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
850                                  REQUEST_CPORT_COUNT,
851                                  USB_DIR_IN | USB_TYPE_VENDOR |
852                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
853                                  sizeof(*cport_count), ES2_TIMEOUT);
854         if (retval < 0) {
855                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
856                         retval);
857                 goto out;
858         }
859
860         retval = le16_to_cpu(*cport_count);
861
862         /* We need to fit a CPort ID in one byte of a message header */
863         if (retval > U8_MAX) {
864                 retval = U8_MAX;
865                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
866         }
867
868 out:
869         kfree(cport_count);
870         return retval;
871 }
872
873 /*
874  * The ES2 USB Bridge device has 15 endpoints
875  * 1 Control - usual USB stuff + AP -> APBridgeA messages
876  * 7 Bulk IN - CPort data in
877  * 7 Bulk OUT - CPort data out
878  */
879 static int ap_probe(struct usb_interface *interface,
880                     const struct usb_device_id *id)
881 {
882         struct es2_ap_dev *es2;
883         struct gb_host_device *hd;
884         struct usb_device *udev;
885         struct usb_host_interface *iface_desc;
886         struct usb_endpoint_descriptor *endpoint;
887         int bulk_in = 0;
888         int bulk_out = 0;
889         int retval = -ENOMEM;
890         int i;
891         int num_cports;
892         int cport_id;
893
894         udev = usb_get_dev(interface_to_usbdev(interface));
895
896         num_cports = apb_get_cport_count(udev);
897         if (num_cports < 0) {
898                 usb_put_dev(udev);
899                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
900                         num_cports);
901                 return num_cports;
902         }
903
904         hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
905                                 num_cports);
906         if (IS_ERR(hd)) {
907                 usb_put_dev(udev);
908                 return PTR_ERR(hd);
909         }
910
911         /*
912          * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
913          * won't be allocated dynamically.
914          */
915         do {
916                 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
917         } while (cport_id > 0);
918
919         es2 = hd_to_es2(hd);
920         es2->hd = hd;
921         es2->usb_intf = interface;
922         es2->usb_dev = udev;
923         spin_lock_init(&es2->cport_out_urb_lock);
924         INIT_KFIFO(es2->apb_log_fifo);
925         usb_set_intfdata(interface, es2);
926
927         es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
928                                    GFP_KERNEL);
929         if (!es2->cport_to_ep) {
930                 retval = -ENOMEM;
931                 goto error;
932         }
933
934         /* find all bulk endpoints */
935         iface_desc = interface->cur_altsetting;
936         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
937                 endpoint = &iface_desc->endpoint[i].desc;
938
939                 if (usb_endpoint_is_bulk_in(endpoint)) {
940                         es2->cport_in[bulk_in++].endpoint =
941                                 endpoint->bEndpointAddress;
942                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
943                         es2->cport_out[bulk_out++].endpoint =
944                                 endpoint->bEndpointAddress;
945                 } else {
946                         dev_err(&udev->dev,
947                                 "Unknown endpoint type found, address 0x%02x\n",
948                                 endpoint->bEndpointAddress);
949                 }
950         }
951         if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
952                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
953                 goto error;
954         }
955
956         /* Allocate buffers for our cport in messages */
957         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
958                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
959
960                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
961                         struct urb *urb;
962                         u8 *buffer;
963
964                         urb = usb_alloc_urb(0, GFP_KERNEL);
965                         if (!urb)
966                                 goto error;
967                         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
968                         if (!buffer)
969                                 goto error;
970
971                         usb_fill_bulk_urb(urb, udev,
972                                           usb_rcvbulkpipe(udev,
973                                                           cport_in->endpoint),
974                                           buffer, ES2_GBUF_MSG_SIZE_MAX,
975                                           cport_in_callback, hd);
976                         cport_in->urb[i] = urb;
977                         cport_in->buffer[i] = buffer;
978                 }
979         }
980
981         /* Allocate urbs for our CPort OUT messages */
982         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
983                 struct urb *urb;
984
985                 urb = usb_alloc_urb(0, GFP_KERNEL);
986                 if (!urb)
987                         goto error;
988
989                 es2->cport_out_urb[i] = urb;
990                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
991         }
992
993         /* XXX We will need to rename this per APB */
994         es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
995                                                         (S_IWUSR | S_IRUGO),
996                                                         gb_debugfs_get(), es2,
997                                                         &apb_log_enable_fops);
998
999         retval = gb_hd_add(hd);
1000         if (retval)
1001                 goto error;
1002
1003         for (i = 0; i < NUM_BULKS; ++i) {
1004                 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1005                 if (retval)
1006                         goto err_disable_cport_in;
1007         }
1008
1009         return 0;
1010
1011 err_disable_cport_in:
1012         for (--i; i >= 0; --i)
1013                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1014         gb_hd_del(hd);
1015 error:
1016         es2_destroy(es2);
1017
1018         return retval;
1019 }
1020
1021 static struct usb_driver es2_ap_driver = {
1022         .name =         "es2_ap_driver",
1023         .probe =        ap_probe,
1024         .disconnect =   ap_disconnect,
1025         .id_table =     id_table,
1026 };
1027
1028 module_usb_driver(es2_ap_driver);
1029
1030 MODULE_LICENSE("GPL v2");
1031 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");