blob: 10b21df253a08b5c3deec134f3d483ab66f078f0 [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 */
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200135 hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX - hd->buffer_headroom;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800136 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 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200217static void *message_send(struct greybus_host_device *hd, u16 cport_id,
218 struct gb_message *message, gfp_t gfp_mask)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800219{
220 struct es1_ap_dev *es1 = hd_to_es1(hd);
221 struct usb_device *udev = es1->usb_dev;
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200222 u8 *transfer_buffer;
223 size_t buffer_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800224 int transfer_buffer_size;
225 int retval;
226 struct urb *urb;
227
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200228 buffer_size = hd->buffer_headroom + sizeof(*message->header) +
229 message->payload_size;
230 transfer_buffer = message->buffer + hd->buffer_headroom - 1;
231 transfer_buffer_size = buffer_size - (hd->buffer_headroom - 1);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800232
233 /*
234 * The data actually transferred will include an indication
235 * of where the data should be sent. Do one last check of
236 * the target CPort id before filling it in.
237 */
238 if (cport_id == CPORT_ID_BAD) {
239 pr_err("request to send inbound data buffer\n");
240 return ERR_PTR(-EINVAL);
241 }
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200242 if (cport_id > U8_MAX) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800243 pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
244 return ERR_PTR(-EINVAL);
245 }
246 /* OK, the destination is fine; record it in the transfer buffer */
247 *transfer_buffer = cport_id;
248
249 /* Find a free urb */
250 urb = next_free_urb(es1, gfp_mask);
251 if (!urb)
252 return ERR_PTR(-ENOMEM);
253
254 usb_fill_bulk_urb(urb, udev,
255 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
256 transfer_buffer, transfer_buffer_size,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200257 cport_out_callback, message);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800258 retval = usb_submit_urb(urb, gfp_mask);
259 if (retval) {
260 pr_err("error %d submitting URB\n", retval);
261 free_urb(es1, urb);
262 return ERR_PTR(retval);
263 }
264
Alex Elder142f8dd2015-03-26 21:25:06 -0500265 return urb;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800266}
267
268/*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200269 * The cookie value supplied is the value that message_send()
270 * returned to its caller. It identifies the message that should be
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800271 * canceled. This function must also handle (which is to say,
272 * ignore) a null cookie value.
273 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200274static void message_cancel(void *cookie)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800275{
276
277 /*
278 * We really should be defensive and track all outstanding
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200279 * (sent) messages rather than trusting the cookie provided
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800280 * is valid. For the time being, this will do.
281 */
282 if (cookie)
Alex Elder142f8dd2015-03-26 21:25:06 -0500283 usb_kill_urb(cookie);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800284}
285
286static struct greybus_host_driver es1_driver = {
287 .hd_priv_size = sizeof(struct es1_ap_dev),
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200288 .message_send = message_send,
289 .message_cancel = message_cancel,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800290 .submit_svc = submit_svc,
291};
292
293/* Common function to report consistent warnings based on URB status */
294static int check_urb_status(struct urb *urb)
295{
296 struct device *dev = &urb->dev->dev;
297 int status = urb->status;
298
299 switch (status) {
300 case 0:
301 return 0;
302
303 case -EOVERFLOW:
304 dev_err(dev, "%s: overflow actual length is %d\n",
305 __func__, urb->actual_length);
306 case -ECONNRESET:
307 case -ENOENT:
308 case -ESHUTDOWN:
309 case -EILSEQ:
310 case -EPROTO:
311 /* device is gone, stop sending */
312 return status;
313 }
314 dev_err(dev, "%s: unknown status %d\n", __func__, status);
315
316 return -EAGAIN;
317}
318
319static void ap_disconnect(struct usb_interface *interface)
320{
321 struct es1_ap_dev *es1;
322 struct usb_device *udev;
323 int i;
324
325 es1 = usb_get_intfdata(interface);
326 if (!es1)
327 return;
328
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100329 usb_log_disable(es1);
330
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800331 /* Tear down everything! */
332 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
333 struct urb *urb = es1->cport_out_urb[i];
334
335 if (!urb)
336 break;
337 usb_kill_urb(urb);
338 usb_free_urb(urb);
339 es1->cport_out_urb[i] = NULL;
340 es1->cport_out_urb_busy[i] = false; /* just to be anal */
341 }
342
343 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
344 struct urb *urb = es1->cport_in_urb[i];
345
346 if (!urb)
347 break;
348 usb_kill_urb(urb);
349 usb_free_urb(urb);
350 kfree(es1->cport_in_buffer[i]);
351 es1->cport_in_buffer[i] = NULL;
352 }
353
354 usb_kill_urb(es1->svc_urb);
355 usb_free_urb(es1->svc_urb);
356 es1->svc_urb = NULL;
357 kfree(es1->svc_buffer);
358 es1->svc_buffer = NULL;
359
360 usb_set_intfdata(interface, NULL);
361 udev = es1->usb_dev;
362 greybus_remove_hd(es1->hd);
363
364 usb_put_dev(udev);
365}
366
367/* Callback for when we get a SVC message */
368static void svc_in_callback(struct urb *urb)
369{
370 struct greybus_host_device *hd = urb->context;
371 struct device *dev = &urb->dev->dev;
372 int status = check_urb_status(urb);
373 int retval;
374
375 if (status) {
376 if ((status == -EAGAIN) || (status == -EPROTO))
377 goto exit;
378 dev_err(dev, "urb svc in error %d (dropped)\n", status);
379 return;
380 }
381
382 /* We have a message, create a new message structure, add it to the
383 * list, and wake up our thread that will process the messages.
384 */
385 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
386
387exit:
388 /* resubmit the urb to get more messages */
389 retval = usb_submit_urb(urb, GFP_ATOMIC);
390 if (retval)
391 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
392}
393
394static void cport_in_callback(struct urb *urb)
395{
396 struct greybus_host_device *hd = urb->context;
397 struct device *dev = &urb->dev->dev;
398 int status = check_urb_status(urb);
399 int retval;
400 u16 cport_id;
401 u8 *data;
402
403 if (status) {
404 if ((status == -EAGAIN) || (status == -EPROTO))
405 goto exit;
406 dev_err(dev, "urb cport in error %d (dropped)\n", status);
407 return;
408 }
409
410 /* The size has to be at least one, for the cport id */
411 if (!urb->actual_length) {
412 dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
413 goto exit;
414 }
415
416 /*
417 * Our CPort number is the first byte of the data stream,
418 * the rest of the stream is "real" data
419 */
420 data = urb->transfer_buffer;
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200421 cport_id = data[0];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800422 data = &data[1];
423
424 /* Pass this data to the greybus core */
425 greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
426
427exit:
428 /* put our urb back in the request pool */
429 retval = usb_submit_urb(urb, GFP_ATOMIC);
430 if (retval)
431 dev_err(dev, "%s: error %d in submitting urb.\n",
432 __func__, retval);
433}
434
435static void cport_out_callback(struct urb *urb)
436{
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200437 struct gb_message *message = urb->context;
438 struct greybus_host_device *hd = message->operation->connection->hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800439 struct es1_ap_dev *es1 = hd_to_es1(hd);
440 int status = check_urb_status(urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800441
442 /*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200443 * Tell the submitter that the message send (attempt) is
444 * complete, and report the status.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800445 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200446 greybus_message_sent(hd, message, status);
447
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800448 free_urb(es1, urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800449}
450
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200451#define APB1_LOG_MSG_SIZE 64
452static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100453{
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100454 int retval;
455
456 /* SVC messages go down our control pipe */
457 do {
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100458 retval = usb_control_msg(es1->usb_dev,
459 usb_rcvctrlpipe(es1->usb_dev,
460 es1->control_endpoint),
461 0x02, /* vendor request APB1 log */
462 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
463 0x00, 0x00,
464 buf,
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200465 APB1_LOG_MSG_SIZE,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100466 ES1_TIMEOUT);
467 if (retval > 0)
468 kfifo_in(&apb1_log_fifo, buf, retval);
469 } while (retval > 0);
470}
471
472static int apb1_log_poll(void *data)
473{
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200474 struct es1_ap_dev *es1 = data;
475 char *buf;
476
477 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
478 if (!buf)
479 return -ENOMEM;
480
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100481 while (!kthread_should_stop()) {
482 msleep(1000);
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200483 apb1_log_get(es1, buf);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100484 }
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200485
486 kfree(buf);
487
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100488 return 0;
489}
490
491static ssize_t apb1_log_read(struct file *f, char __user *buf,
492 size_t count, loff_t *ppos)
493{
494 ssize_t ret;
495 size_t copied;
496 char *tmp_buf;
497
498 if (count > APB1_LOG_SIZE)
499 count = APB1_LOG_SIZE;
500
501 tmp_buf = kmalloc(count, GFP_KERNEL);
502 if (!tmp_buf)
503 return -ENOMEM;
504
505 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
506 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
507
508 kfree(tmp_buf);
509
510 return ret;
511}
512
513static const struct file_operations apb1_log_fops = {
514 .read = apb1_log_read,
515};
516
517static void usb_log_enable(struct es1_ap_dev *es1)
518{
Alex Eldere0feaf12015-03-27 15:20:49 -0500519 if (!IS_ERR_OR_NULL(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100520 return;
521
522 /* get log from APB1 */
523 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
Alex Eldere0feaf12015-03-27 15:20:49 -0500524 if (IS_ERR(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100525 return;
526 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
527 gb_debugfs_get(), NULL,
528 &apb1_log_fops);
529}
530
531static void usb_log_disable(struct es1_ap_dev *es1)
532{
Alex Eldere0feaf12015-03-27 15:20:49 -0500533 if (IS_ERR_OR_NULL(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100534 return;
535
536 debugfs_remove(apb1_log_dentry);
537 apb1_log_dentry = NULL;
538
539 kthread_stop(apb1_log_task);
540 apb1_log_task = NULL;
541}
542
543static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
544 size_t count, loff_t *ppos)
545{
546 char tmp_buf[3];
Alex Eldere0feaf12015-03-27 15:20:49 -0500547 int enable = !IS_ERR_OR_NULL(apb1_log_task);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100548
549 sprintf(tmp_buf, "%d\n", enable);
550 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
551}
552
553static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
554 size_t count, loff_t *ppos)
555{
556 int enable;
557 ssize_t retval;
558 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
559
560 retval = kstrtoint_from_user(buf, count, 10, &enable);
561 if (retval)
562 return retval;
563
564 if (enable)
565 usb_log_enable(es1);
566 else
567 usb_log_disable(es1);
568
569 return count;
570}
571
572static const struct file_operations apb1_log_enable_fops = {
573 .read = apb1_log_enable_read,
574 .write = apb1_log_enable_write,
575};
576
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800577/*
578 * The ES1 USB Bridge device contains 4 endpoints
579 * 1 Control - usual USB stuff + AP -> SVC messages
580 * 1 Interrupt IN - SVC -> AP messages
581 * 1 Bulk IN - CPort data in
582 * 1 Bulk OUT - CPort data out
583 */
584static int ap_probe(struct usb_interface *interface,
585 const struct usb_device_id *id)
586{
587 struct es1_ap_dev *es1;
588 struct greybus_host_device *hd;
589 struct usb_device *udev;
590 struct usb_host_interface *iface_desc;
591 struct usb_endpoint_descriptor *endpoint;
592 bool int_in_found = false;
593 bool bulk_in_found = false;
594 bool bulk_out_found = false;
595 int retval = -ENOMEM;
596 int i;
597 u8 svc_interval = 0;
598
599 udev = usb_get_dev(interface_to_usbdev(interface));
600
601 hd = greybus_create_hd(&es1_driver, &udev->dev);
602 if (!hd) {
603 usb_put_dev(udev);
604 return -ENOMEM;
605 }
606
607 /* Fill in the buffer allocation constraints */
608 hd_buffer_constraints(hd);
609
610 es1 = hd_to_es1(hd);
611 es1->hd = hd;
612 es1->usb_intf = interface;
613 es1->usb_dev = udev;
614 spin_lock_init(&es1->cport_out_urb_lock);
615 usb_set_intfdata(interface, es1);
616
617 /* Control endpoint is the pipe to talk to this AP, so save it off */
618 endpoint = &udev->ep0.desc;
619 es1->control_endpoint = endpoint->bEndpointAddress;
620
621 /* find all 3 of our endpoints */
622 iface_desc = interface->cur_altsetting;
623 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
624 endpoint = &iface_desc->endpoint[i].desc;
625
626 if (usb_endpoint_is_int_in(endpoint)) {
627 es1->svc_endpoint = endpoint->bEndpointAddress;
628 svc_interval = endpoint->bInterval;
629 int_in_found = true;
630 } else if (usb_endpoint_is_bulk_in(endpoint)) {
631 es1->cport_in_endpoint = endpoint->bEndpointAddress;
632 bulk_in_found = true;
633 } else if (usb_endpoint_is_bulk_out(endpoint)) {
634 es1->cport_out_endpoint = endpoint->bEndpointAddress;
635 bulk_out_found = true;
636 } else {
637 dev_err(&udev->dev,
638 "Unknown endpoint type found, address %x\n",
639 endpoint->bEndpointAddress);
640 }
641 }
642 if ((int_in_found == false) ||
643 (bulk_in_found == false) ||
644 (bulk_out_found == false)) {
645 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
646 goto error;
647 }
648
649 /* Create our buffer and URB to get SVC messages, and start it up */
650 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
651 if (!es1->svc_buffer)
652 goto error;
653
654 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
655 if (!es1->svc_urb)
656 goto error;
657
658 usb_fill_int_urb(es1->svc_urb, udev,
659 usb_rcvintpipe(udev, es1->svc_endpoint),
660 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
661 hd, svc_interval);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800662
663 /* Allocate buffers for our cport in messages and start them up */
664 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
665 struct urb *urb;
666 u8 *buffer;
667
668 urb = usb_alloc_urb(0, GFP_KERNEL);
669 if (!urb)
670 goto error;
671 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
672 if (!buffer)
673 goto error;
674
675 usb_fill_bulk_urb(urb, udev,
676 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
677 buffer, ES1_GBUF_MSG_SIZE_MAX,
678 cport_in_callback, hd);
679 es1->cport_in_urb[i] = urb;
680 es1->cport_in_buffer[i] = buffer;
681 retval = usb_submit_urb(urb, GFP_KERNEL);
682 if (retval)
683 goto error;
684 }
685
686 /* Allocate urbs for our CPort OUT messages */
687 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
688 struct urb *urb;
689
690 urb = usb_alloc_urb(0, GFP_KERNEL);
691 if (!urb)
692 goto error;
693
694 es1->cport_out_urb[i] = urb;
695 es1->cport_out_urb_busy[i] = false; /* just to be anal */
696 }
697
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100698 /* Start up our svc urb, which allows events to start flowing */
699 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
700 if (retval)
701 goto error;
702
703 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
704 (S_IWUSR | S_IRUGO),
705 gb_debugfs_get(), es1,
706 &apb1_log_enable_fops);
707
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800708 return 0;
709error:
710 ap_disconnect(interface);
711
712 return retval;
713}
714
715static struct usb_driver es1_ap_driver = {
716 .name = "es1_ap_driver",
717 .probe = ap_probe,
718 .disconnect = ap_disconnect,
719 .id_table = id_table,
720};
721
722module_usb_driver(es1_ap_driver);
723
724MODULE_LICENSE("GPL");
725MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");