blob: 23e27782fe9f740cdee7265aa6d5f0624afa440e [file] [log] [blame]
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001/*
2 * Greybus "AP" USB driver for "ES2" controller chips
3 *
Alex Elder142f8dd2015-03-26 21:25:06 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08006 *
7 * Released under the GPLv2 only.
8 */
9#include <linux/kernel.h>
10#include <linux/module.h>
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010011#include <linux/kthread.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080012#include <linux/slab.h>
13#include <linux/errno.h>
14#include <linux/sizes.h>
15#include <linux/usb.h>
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010016#include <linux/kfifo.h>
17#include <linux/debugfs.h>
18#include <linux/uaccess.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080019
20#include "greybus.h"
21#include "svc_msg.h"
22#include "kernel_ver.h"
23
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080024/* Memory sizes for the buffers sent to/from the ES1 controller */
25#define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
26#define ES1_GBUF_MSG_SIZE_MAX PAGE_SIZE
27
28static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman2bf4c872015-03-02 08:52:07 -080029 /* Made up numbers for the SVC USB Bridge in ES2 */
30 { USB_DEVICE(0xffff, 0x0002) },
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080031 { },
32};
33MODULE_DEVICE_TABLE(usb, id_table);
34
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010035#define APB1_LOG_SIZE SZ_16K
36static struct dentry *apb1_log_dentry;
37static struct dentry *apb1_log_enable_dentry;
38static struct task_struct *apb1_log_task;
39static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
40
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080041/*
42 * Number of CPort IN urbs in flight at any point in time.
43 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
44 * flight.
45 */
46#define NUM_CPORT_IN_URB 4
47
48/* Number of CPort OUT urbs in flight at any point in time.
49 * Adjust if we get messages saying we are out of urbs in the system log.
50 */
51#define NUM_CPORT_OUT_URB 8
52
53/**
54 * es1_ap_dev - ES1 USB Bridge to AP structure
55 * @usb_dev: pointer to the USB device we are.
56 * @usb_intf: pointer to the USB interface we are bound to.
57 * @hd: pointer to our greybus_host_device structure
58 * @control_endpoint: endpoint to send data to SVC
59 * @svc_endpoint: endpoint for SVC data in
60 * @cport_in_endpoint: bulk in endpoint for CPort data
61 * @cport-out_endpoint: bulk out endpoint for CPort data
62 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
63 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
64 * @cport_in_urb: array of urbs for the CPort in messages
65 * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
66 * @cport_out_urb: array of urbs for the CPort out messages
67 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
68 * not.
69 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
70 */
71struct es1_ap_dev {
72 struct usb_device *usb_dev;
73 struct usb_interface *usb_intf;
74 struct greybus_host_device *hd;
75
76 __u8 control_endpoint;
77 __u8 svc_endpoint;
78 __u8 cport_in_endpoint;
79 __u8 cport_out_endpoint;
80
81 u8 *svc_buffer;
82 struct urb *svc_urb;
83
84 struct urb *cport_in_urb[NUM_CPORT_IN_URB];
85 u8 *cport_in_buffer[NUM_CPORT_IN_URB];
86 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
87 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
88 spinlock_t cport_out_urb_lock;
89};
90
91static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
92{
93 return (struct es1_ap_dev *)&hd->hd_priv;
94}
95
96static void cport_out_callback(struct urb *urb);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010097static void usb_log_enable(struct es1_ap_dev *es1);
98static void usb_log_disable(struct es1_ap_dev *es1);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080099
100/*
101 * Buffer constraints for the host driver.
102 *
103 * A "buffer" is used to hold data to be transferred for Greybus by
104 * the host driver. A buffer is represented by a "buffer pointer",
105 * which defines a region of memory used by the host driver for
106 * transferring the data. When Greybus allocates a buffer, it must
107 * do so subject to the constraints associated with the host driver.
108 * These constraints are specified by two parameters: the
109 * headroom; and the maximum buffer size.
110 *
111 * +------------------+
112 * | Host driver | \
113 * | reserved area | }- headroom
114 * | . . . | /
115 * buffer pointer ---> +------------------+
116 * | Buffer space for | \
117 * | transferred data | }- buffer size
118 * | . . . | / (limited to size_max)
119 * +------------------+
120 *
121 * headroom: Every buffer must have at least this much space
122 * *before* the buffer pointer, reserved for use by the
123 * host driver. I.e., ((char *)buffer - headroom) must
124 * point to valid memory, usable only by the host driver.
125 * size_max: The maximum size of a buffer (not including the
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100126 * headroom) must not exceed this.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800127 */
128static void hd_buffer_constraints(struct greybus_host_device *hd)
129{
130 /*
131 * Only one byte is required, but this produces a result
132 * that's better aligned for the user.
133 */
134 hd->buffer_headroom = sizeof(u32); /* For cport id */
135 hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
136 BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
137}
138
139#define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
140static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
141{
142 struct es1_ap_dev *es1 = hd_to_es1(hd);
143 int retval;
144
145 /* SVC messages go down our control pipe */
146 retval = usb_control_msg(es1->usb_dev,
147 usb_sndctrlpipe(es1->usb_dev,
148 es1->control_endpoint),
149 0x01, /* vendor request AP message */
150 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
151 0x00, 0x00,
152 (char *)svc_msg,
153 sizeof(*svc_msg),
154 ES1_TIMEOUT);
155 if (retval != sizeof(*svc_msg))
156 return retval;
157
158 return 0;
159}
160
161static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
162{
163 struct urb *urb = NULL;
164 unsigned long flags;
165 int i;
166
167 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
168
169 /* Look in our pool of allocated urbs first, as that's the "fastest" */
170 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
171 if (es1->cport_out_urb_busy[i] == false) {
172 es1->cport_out_urb_busy[i] = true;
173 urb = es1->cport_out_urb[i];
174 break;
175 }
176 }
177 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
178 if (urb)
179 return urb;
180
181 /*
182 * Crap, pool is empty, complain to the syslog and go allocate one
183 * dynamically as we have to succeed.
184 */
185 dev_err(&es1->usb_dev->dev,
186 "No free CPort OUT urbs, having to dynamically allocate one!\n");
187 return usb_alloc_urb(0, gfp_mask);
188}
189
190static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
191{
192 unsigned long flags;
193 int i;
194 /*
195 * See if this was an urb in our pool, if so mark it "free", otherwise
196 * we need to free it ourselves.
197 */
198 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
199 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
200 if (urb == es1->cport_out_urb[i]) {
201 es1->cport_out_urb_busy[i] = false;
202 urb = NULL;
203 break;
204 }
205 }
206 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
207
208 /* If urb is not NULL, then we need to free this urb */
209 usb_free_urb(urb);
210}
211
212/*
213 * Returns an opaque cookie value if successful, or a pointer coded
214 * error otherwise. If the caller wishes to cancel the in-flight
215 * buffer, it must supply the returned cookie to the cancel routine.
216 */
217static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
218 void *buffer, size_t buffer_size, gfp_t gfp_mask)
219{
220 struct es1_ap_dev *es1 = hd_to_es1(hd);
221 struct usb_device *udev = es1->usb_dev;
222 u8 *transfer_buffer = buffer;
223 int transfer_buffer_size;
224 int retval;
225 struct urb *urb;
226
227 if (!buffer) {
228 pr_err("null buffer supplied to send\n");
229 return ERR_PTR(-EINVAL);
230 }
231 if (buffer_size > (size_t)INT_MAX) {
232 pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
233 return ERR_PTR(-EINVAL);
234 }
235 transfer_buffer--;
236 transfer_buffer_size = buffer_size + 1;
237
238 /*
239 * The data actually transferred will include an indication
240 * of where the data should be sent. Do one last check of
241 * the target CPort id before filling it in.
242 */
243 if (cport_id == CPORT_ID_BAD) {
244 pr_err("request to send inbound data buffer\n");
245 return ERR_PTR(-EINVAL);
246 }
247 if (cport_id > (u16)U8_MAX) {
248 pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
249 return ERR_PTR(-EINVAL);
250 }
251 /* OK, the destination is fine; record it in the transfer buffer */
252 *transfer_buffer = cport_id;
253
254 /* Find a free urb */
255 urb = next_free_urb(es1, gfp_mask);
256 if (!urb)
257 return ERR_PTR(-ENOMEM);
258
259 usb_fill_bulk_urb(urb, udev,
260 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
261 transfer_buffer, transfer_buffer_size,
262 cport_out_callback, hd);
263 retval = usb_submit_urb(urb, gfp_mask);
264 if (retval) {
265 pr_err("error %d submitting URB\n", retval);
266 free_urb(es1, urb);
267 return ERR_PTR(retval);
268 }
269
Alex Elder142f8dd2015-03-26 21:25:06 -0500270 return urb;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800271}
272
273/*
274 * The cookie value supplied is the value that buffer_send()
275 * returned to its caller. It identifies the buffer that should be
276 * canceled. This function must also handle (which is to say,
277 * ignore) a null cookie value.
278 */
279static void buffer_cancel(void *cookie)
280{
281
282 /*
283 * We really should be defensive and track all outstanding
284 * (sent) buffers rather than trusting the cookie provided
285 * is valid. For the time being, this will do.
286 */
287 if (cookie)
Alex Elder142f8dd2015-03-26 21:25:06 -0500288 usb_kill_urb(cookie);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800289}
290
291static struct greybus_host_driver es1_driver = {
292 .hd_priv_size = sizeof(struct es1_ap_dev),
293 .buffer_send = buffer_send,
294 .buffer_cancel = buffer_cancel,
295 .submit_svc = submit_svc,
296};
297
298/* Common function to report consistent warnings based on URB status */
299static int check_urb_status(struct urb *urb)
300{
301 struct device *dev = &urb->dev->dev;
302 int status = urb->status;
303
304 switch (status) {
305 case 0:
306 return 0;
307
308 case -EOVERFLOW:
309 dev_err(dev, "%s: overflow actual length is %d\n",
310 __func__, urb->actual_length);
311 case -ECONNRESET:
312 case -ENOENT:
313 case -ESHUTDOWN:
314 case -EILSEQ:
315 case -EPROTO:
316 /* device is gone, stop sending */
317 return status;
318 }
319 dev_err(dev, "%s: unknown status %d\n", __func__, status);
320
321 return -EAGAIN;
322}
323
324static void ap_disconnect(struct usb_interface *interface)
325{
326 struct es1_ap_dev *es1;
327 struct usb_device *udev;
328 int i;
329
330 es1 = usb_get_intfdata(interface);
331 if (!es1)
332 return;
333
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100334 usb_log_disable(es1);
335
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800336 /* Tear down everything! */
337 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
338 struct urb *urb = es1->cport_out_urb[i];
339
340 if (!urb)
341 break;
342 usb_kill_urb(urb);
343 usb_free_urb(urb);
344 es1->cport_out_urb[i] = NULL;
345 es1->cport_out_urb_busy[i] = false; /* just to be anal */
346 }
347
348 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
349 struct urb *urb = es1->cport_in_urb[i];
350
351 if (!urb)
352 break;
353 usb_kill_urb(urb);
354 usb_free_urb(urb);
355 kfree(es1->cport_in_buffer[i]);
356 es1->cport_in_buffer[i] = NULL;
357 }
358
359 usb_kill_urb(es1->svc_urb);
360 usb_free_urb(es1->svc_urb);
361 es1->svc_urb = NULL;
362 kfree(es1->svc_buffer);
363 es1->svc_buffer = NULL;
364
365 usb_set_intfdata(interface, NULL);
366 udev = es1->usb_dev;
367 greybus_remove_hd(es1->hd);
368
369 usb_put_dev(udev);
370}
371
372/* Callback for when we get a SVC message */
373static void svc_in_callback(struct urb *urb)
374{
375 struct greybus_host_device *hd = urb->context;
376 struct device *dev = &urb->dev->dev;
377 int status = check_urb_status(urb);
378 int retval;
379
380 if (status) {
381 if ((status == -EAGAIN) || (status == -EPROTO))
382 goto exit;
383 dev_err(dev, "urb svc in error %d (dropped)\n", status);
384 return;
385 }
386
387 /* We have a message, create a new message structure, add it to the
388 * list, and wake up our thread that will process the messages.
389 */
390 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
391
392exit:
393 /* resubmit the urb to get more messages */
394 retval = usb_submit_urb(urb, GFP_ATOMIC);
395 if (retval)
396 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
397}
398
399static void cport_in_callback(struct urb *urb)
400{
401 struct greybus_host_device *hd = urb->context;
402 struct device *dev = &urb->dev->dev;
403 int status = check_urb_status(urb);
404 int retval;
405 u16 cport_id;
406 u8 *data;
407
408 if (status) {
409 if ((status == -EAGAIN) || (status == -EPROTO))
410 goto exit;
411 dev_err(dev, "urb cport in error %d (dropped)\n", status);
412 return;
413 }
414
415 /* The size has to be at least one, for the cport id */
416 if (!urb->actual_length) {
417 dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
418 goto exit;
419 }
420
421 /*
422 * Our CPort number is the first byte of the data stream,
423 * the rest of the stream is "real" data
424 */
425 data = urb->transfer_buffer;
426 cport_id = (u16)data[0];
427 data = &data[1];
428
429 /* Pass this data to the greybus core */
430 greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
431
432exit:
433 /* put our urb back in the request pool */
434 retval = usb_submit_urb(urb, GFP_ATOMIC);
435 if (retval)
436 dev_err(dev, "%s: error %d in submitting urb.\n",
437 __func__, retval);
438}
439
440static void cport_out_callback(struct urb *urb)
441{
442 struct greybus_host_device *hd = urb->context;
443 struct es1_ap_dev *es1 = hd_to_es1(hd);
444 int status = check_urb_status(urb);
445 u8 *data = urb->transfer_buffer + 1;
446
447 /*
448 * Tell the submitter that the buffer send (attempt) is
449 * complete, and report the status. The submitter's buffer
450 * starts after the one-byte CPort id we inserted.
451 */
452 data = urb->transfer_buffer + 1;
453 greybus_data_sent(hd, data, status);
454
455 free_urb(es1, urb);
456 /*
457 * Rest assured Greg, this craziness is getting fixed.
458 *
459 * Yes, you are right, we aren't telling anyone that the urb finished.
460 * "That's crazy! How does this all even work?" you might be saying.
461 * The "magic" is the idea that greybus works on the "operation" level,
462 * not the "send a buffer" level. All operations are "round-trip" with
463 * a response from the device that the operation finished, or it will
464 * time out. Because of that, we don't care that this urb finished, or
465 * failed, or did anything else, as higher levels of the protocol stack
466 * will handle completions and timeouts and the rest.
467 *
468 * This protocol is "needed" due to some hardware restrictions on the
469 * current generation of Unipro controllers. Think about it for a
470 * minute, this is a USB driver, talking to a Unipro bridge, impedance
471 * mismatch is huge, yet the Unipro controller are even more
472 * underpowered than this little USB controller. We rely on the round
473 * trip to keep stalls in the Unipro controllers from happening so that
474 * we can keep data flowing properly, no matter how slow it might be.
475 *
476 * Once again, a wonderful bus protocol cut down in its prime by a naive
477 * controller chip. We dream of the day we have a "real" HCD for
478 * Unipro. Until then, we suck it up and make the hardware work, as
479 * that's the job of the firmware and kernel.
480 * </rant>
481 */
482}
483
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100484static void apb1_log_get(struct es1_ap_dev *es1)
485{
486 char buf[65];
487 int retval;
488
489 /* SVC messages go down our control pipe */
490 do {
491 memset(buf, 0, 65);
492 retval = usb_control_msg(es1->usb_dev,
493 usb_rcvctrlpipe(es1->usb_dev,
494 es1->control_endpoint),
495 0x02, /* vendor request APB1 log */
496 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
497 0x00, 0x00,
498 buf,
499 64,
500 ES1_TIMEOUT);
501 if (retval > 0)
502 kfifo_in(&apb1_log_fifo, buf, retval);
503 } while (retval > 0);
504}
505
506static int apb1_log_poll(void *data)
507{
508 while (!kthread_should_stop()) {
509 msleep(1000);
510 apb1_log_get((struct es1_ap_dev *)data);
511 }
512 return 0;
513}
514
515static ssize_t apb1_log_read(struct file *f, char __user *buf,
516 size_t count, loff_t *ppos)
517{
518 ssize_t ret;
519 size_t copied;
520 char *tmp_buf;
521
522 if (count > APB1_LOG_SIZE)
523 count = APB1_LOG_SIZE;
524
525 tmp_buf = kmalloc(count, GFP_KERNEL);
526 if (!tmp_buf)
527 return -ENOMEM;
528
529 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
530 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
531
532 kfree(tmp_buf);
533
534 return ret;
535}
536
537static const struct file_operations apb1_log_fops = {
538 .read = apb1_log_read,
539};
540
541static void usb_log_enable(struct es1_ap_dev *es1)
542{
Alex Eldere0feaf12015-03-27 15:20:49 -0500543 if (!IS_ERR_OR_NULL(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100544 return;
545
546 /* get log from APB1 */
547 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
Alex Eldere0feaf12015-03-27 15:20:49 -0500548 if (IS_ERR(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100549 return;
550 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
551 gb_debugfs_get(), NULL,
552 &apb1_log_fops);
553}
554
555static void usb_log_disable(struct es1_ap_dev *es1)
556{
Alex Eldere0feaf12015-03-27 15:20:49 -0500557 if (IS_ERR_OR_NULL(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100558 return;
559
560 debugfs_remove(apb1_log_dentry);
561 apb1_log_dentry = NULL;
562
563 kthread_stop(apb1_log_task);
564 apb1_log_task = NULL;
565}
566
567static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
568 size_t count, loff_t *ppos)
569{
570 char tmp_buf[3];
Alex Eldere0feaf12015-03-27 15:20:49 -0500571 int enable = !IS_ERR_OR_NULL(apb1_log_task);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100572
573 sprintf(tmp_buf, "%d\n", enable);
574 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
575}
576
577static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
578 size_t count, loff_t *ppos)
579{
580 int enable;
581 ssize_t retval;
582 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
583
584 retval = kstrtoint_from_user(buf, count, 10, &enable);
585 if (retval)
586 return retval;
587
588 if (enable)
589 usb_log_enable(es1);
590 else
591 usb_log_disable(es1);
592
593 return count;
594}
595
596static const struct file_operations apb1_log_enable_fops = {
597 .read = apb1_log_enable_read,
598 .write = apb1_log_enable_write,
599};
600
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800601/*
602 * The ES1 USB Bridge device contains 4 endpoints
603 * 1 Control - usual USB stuff + AP -> SVC messages
604 * 1 Interrupt IN - SVC -> AP messages
605 * 1 Bulk IN - CPort data in
606 * 1 Bulk OUT - CPort data out
607 */
608static int ap_probe(struct usb_interface *interface,
609 const struct usb_device_id *id)
610{
611 struct es1_ap_dev *es1;
612 struct greybus_host_device *hd;
613 struct usb_device *udev;
614 struct usb_host_interface *iface_desc;
615 struct usb_endpoint_descriptor *endpoint;
616 bool int_in_found = false;
617 bool bulk_in_found = false;
618 bool bulk_out_found = false;
619 int retval = -ENOMEM;
620 int i;
621 u8 svc_interval = 0;
622
623 udev = usb_get_dev(interface_to_usbdev(interface));
624
625 hd = greybus_create_hd(&es1_driver, &udev->dev);
626 if (!hd) {
627 usb_put_dev(udev);
628 return -ENOMEM;
629 }
630
631 /* Fill in the buffer allocation constraints */
632 hd_buffer_constraints(hd);
633
634 es1 = hd_to_es1(hd);
635 es1->hd = hd;
636 es1->usb_intf = interface;
637 es1->usb_dev = udev;
638 spin_lock_init(&es1->cport_out_urb_lock);
639 usb_set_intfdata(interface, es1);
640
641 /* Control endpoint is the pipe to talk to this AP, so save it off */
642 endpoint = &udev->ep0.desc;
643 es1->control_endpoint = endpoint->bEndpointAddress;
644
645 /* find all 3 of our endpoints */
646 iface_desc = interface->cur_altsetting;
647 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
648 endpoint = &iface_desc->endpoint[i].desc;
649
650 if (usb_endpoint_is_int_in(endpoint)) {
651 es1->svc_endpoint = endpoint->bEndpointAddress;
652 svc_interval = endpoint->bInterval;
653 int_in_found = true;
654 } else if (usb_endpoint_is_bulk_in(endpoint)) {
655 es1->cport_in_endpoint = endpoint->bEndpointAddress;
656 bulk_in_found = true;
657 } else if (usb_endpoint_is_bulk_out(endpoint)) {
658 es1->cport_out_endpoint = endpoint->bEndpointAddress;
659 bulk_out_found = true;
660 } else {
661 dev_err(&udev->dev,
662 "Unknown endpoint type found, address %x\n",
663 endpoint->bEndpointAddress);
664 }
665 }
666 if ((int_in_found == false) ||
667 (bulk_in_found == false) ||
668 (bulk_out_found == false)) {
669 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
670 goto error;
671 }
672
673 /* Create our buffer and URB to get SVC messages, and start it up */
674 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
675 if (!es1->svc_buffer)
676 goto error;
677
678 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
679 if (!es1->svc_urb)
680 goto error;
681
682 usb_fill_int_urb(es1->svc_urb, udev,
683 usb_rcvintpipe(udev, es1->svc_endpoint),
684 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
685 hd, svc_interval);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800686
687 /* Allocate buffers for our cport in messages and start them up */
688 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
689 struct urb *urb;
690 u8 *buffer;
691
692 urb = usb_alloc_urb(0, GFP_KERNEL);
693 if (!urb)
694 goto error;
695 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
696 if (!buffer)
697 goto error;
698
699 usb_fill_bulk_urb(urb, udev,
700 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
701 buffer, ES1_GBUF_MSG_SIZE_MAX,
702 cport_in_callback, hd);
703 es1->cport_in_urb[i] = urb;
704 es1->cport_in_buffer[i] = buffer;
705 retval = usb_submit_urb(urb, GFP_KERNEL);
706 if (retval)
707 goto error;
708 }
709
710 /* Allocate urbs for our CPort OUT messages */
711 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
712 struct urb *urb;
713
714 urb = usb_alloc_urb(0, GFP_KERNEL);
715 if (!urb)
716 goto error;
717
718 es1->cport_out_urb[i] = urb;
719 es1->cport_out_urb_busy[i] = false; /* just to be anal */
720 }
721
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100722 /* Start up our svc urb, which allows events to start flowing */
723 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
724 if (retval)
725 goto error;
726
727 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
728 (S_IWUSR | S_IRUGO),
729 gb_debugfs_get(), es1,
730 &apb1_log_enable_fops);
731
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800732 return 0;
733error:
734 ap_disconnect(interface);
735
736 return retval;
737}
738
739static struct usb_driver es1_ap_driver = {
740 .name = "es1_ap_driver",
741 .probe = ap_probe,
742 .disconnect = ap_disconnect,
743 .id_table = id_table,
744};
745
746module_usb_driver(es1_ap_driver);
747
748MODULE_LICENSE("GPL");
749MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");