blob: 295cc6839551859838dd9b2b2e3778a2e8276d72 [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 */
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01009#include <linux/kthread.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080010#include <linux/sizes.h>
11#include <linux/usb.h>
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010012#include <linux/kfifo.h>
13#include <linux/debugfs.h>
Johan Hovold491e60d2015-04-07 11:27:20 +020014#include <asm/unaligned.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080015
16#include "greybus.h"
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080017#include "kernel_ver.h"
Bryan O'Donoghue3f2a8092015-08-11 13:50:52 +010018#include "connection.h"
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080019
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080020/* Memory sizes for the buffers sent to/from the ES1 controller */
Greg Kroah-Hartmancce31032015-06-09 13:47:48 -070021#define ES1_GBUF_MSG_SIZE_MAX 2048
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080022
23static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman2bf4c872015-03-02 08:52:07 -080024 /* Made up numbers for the SVC USB Bridge in ES2 */
25 { USB_DEVICE(0xffff, 0x0002) },
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080026 { },
27};
28MODULE_DEVICE_TABLE(usb, id_table);
29
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010030#define APB1_LOG_SIZE SZ_16K
31static struct dentry *apb1_log_dentry;
32static struct dentry *apb1_log_enable_dentry;
33static struct task_struct *apb1_log_task;
34static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
35
Alexandre Bailonfc1a5362015-06-15 18:08:14 +020036/* Number of cport present on USB bridge */
37#define CPORT_MAX 44
38
Alexandre Bailon606addd2015-06-15 18:08:13 +020039/* Number of bulk in and bulk out couple */
40#define NUM_BULKS 7
41
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080042/*
43 * Number of CPort IN urbs in flight at any point in time.
44 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
45 * flight.
46 */
47#define NUM_CPORT_IN_URB 4
48
49/* Number of CPort OUT urbs in flight at any point in time.
50 * Adjust if we get messages saying we are out of urbs in the system log.
51 */
Alexandre Bailon606addd2015-06-15 18:08:13 +020052#define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080053
Alexandre Bailon611c17392015-06-15 18:08:11 +020054/* vendor request AP message */
55#define REQUEST_SVC 0x01
56
57/* vendor request APB1 log */
58#define REQUEST_LOG 0x02
59
Alexandre Bailonfc1a5362015-06-15 18:08:14 +020060/* vendor request to map a cport to bulk in and bulk out endpoints */
61#define REQUEST_EP_MAPPING 0x03
62
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020063/*
64 * @endpoint: bulk in endpoint for CPort data
65 * @urb: array of urbs for the CPort in messages
66 * @buffer: array of buffers for the @cport_in_urb urbs
67 */
68struct es1_cport_in {
69 __u8 endpoint;
70 struct urb *urb[NUM_CPORT_IN_URB];
71 u8 *buffer[NUM_CPORT_IN_URB];
72};
73
74/*
75 * @endpoint: bulk out endpoint for CPort data
76 */
77struct es1_cport_out {
78 __u8 endpoint;
79};
80
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080081/**
82 * es1_ap_dev - ES1 USB Bridge to AP structure
83 * @usb_dev: pointer to the USB device we are.
84 * @usb_intf: pointer to the USB interface we are bound to.
85 * @hd: pointer to our greybus_host_device structure
86 * @control_endpoint: endpoint to send data to SVC
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020087
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020088 * @cport_in: endpoint, urbs and buffer for cport in messages
89 * @cport_out: endpoint for for cport out messages
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080090 * @cport_out_urb: array of urbs for the CPort out messages
91 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
92 * not.
Johan Hovold3e136cc2015-07-01 12:37:21 +020093 * @cport_out_urb_cancelled: array of flags indicating whether the
94 * corresponding @cport_out_urb is being cancelled
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080095 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
96 */
97struct es1_ap_dev {
98 struct usb_device *usb_dev;
99 struct usb_interface *usb_intf;
100 struct greybus_host_device *hd;
101
102 __u8 control_endpoint;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800103
Alexandre Bailon606addd2015-06-15 18:08:13 +0200104 struct es1_cport_in cport_in[NUM_BULKS];
105 struct es1_cport_out cport_out[NUM_BULKS];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800106 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
107 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
Johan Hovold3e136cc2015-07-01 12:37:21 +0200108 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800109 spinlock_t cport_out_urb_lock;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200110
111 int cport_to_ep[CPORT_MAX];
112};
113
114struct cport_to_ep {
115 __le16 cport_id;
116 __u8 endpoint_in;
117 __u8 endpoint_out;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800118};
119
120static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
121{
122 return (struct es1_ap_dev *)&hd->hd_priv;
123}
124
125static void cport_out_callback(struct urb *urb);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100126static void usb_log_enable(struct es1_ap_dev *es1);
127static void usb_log_disable(struct es1_ap_dev *es1);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800128
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200129static int cport_to_ep(struct es1_ap_dev *es1, u16 cport_id)
130{
131 if (cport_id >= CPORT_MAX)
132 return 0;
133 return es1->cport_to_ep[cport_id];
134}
135
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800136#define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800137
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200138static int ep_in_use(struct es1_ap_dev *es1, int bulk_ep_set)
139{
140 int i;
141
142 for (i = 0; i < CPORT_MAX; i++) {
143 if (es1->cport_to_ep[i] == bulk_ep_set)
144 return 1;
145 }
146 return 0;
147}
148
149int map_cport_to_ep(struct es1_ap_dev *es1,
150 u16 cport_id, int bulk_ep_set)
151{
152 int retval;
153 struct cport_to_ep *cport_to_ep;
154
155 if (bulk_ep_set == 0 || bulk_ep_set >= NUM_BULKS)
156 return -EINVAL;
157 if (cport_id >= CPORT_MAX)
158 return -EINVAL;
159 if (bulk_ep_set && ep_in_use(es1, bulk_ep_set))
160 return -EINVAL;
161
162 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
163 if (!cport_to_ep)
164 return -ENOMEM;
165
166 es1->cport_to_ep[cport_id] = bulk_ep_set;
167 cport_to_ep->cport_id = cpu_to_le16(cport_id);
168 cport_to_ep->endpoint_in = es1->cport_in[bulk_ep_set].endpoint;
169 cport_to_ep->endpoint_out = es1->cport_out[bulk_ep_set].endpoint;
170
171 retval = usb_control_msg(es1->usb_dev,
172 usb_sndctrlpipe(es1->usb_dev,
173 es1->control_endpoint),
174 REQUEST_EP_MAPPING,
175 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
176 0x00, 0x00,
177 (char *)cport_to_ep,
178 sizeof(*cport_to_ep),
179 ES1_TIMEOUT);
180 if (retval == sizeof(*cport_to_ep))
181 retval = 0;
182 kfree(cport_to_ep);
183
184 return retval;
185}
186
187int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
188{
189 return map_cport_to_ep(es1, cport_id, 0);
190}
191
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800192static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
193{
194 struct urb *urb = NULL;
195 unsigned long flags;
196 int i;
197
198 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
199
200 /* Look in our pool of allocated urbs first, as that's the "fastest" */
201 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Johan Hovold3e136cc2015-07-01 12:37:21 +0200202 if (es1->cport_out_urb_busy[i] == false &&
203 es1->cport_out_urb_cancelled[i] == false) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800204 es1->cport_out_urb_busy[i] = true;
205 urb = es1->cport_out_urb[i];
206 break;
207 }
208 }
209 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
210 if (urb)
211 return urb;
212
213 /*
214 * Crap, pool is empty, complain to the syslog and go allocate one
215 * dynamically as we have to succeed.
216 */
217 dev_err(&es1->usb_dev->dev,
218 "No free CPort OUT urbs, having to dynamically allocate one!\n");
219 return usb_alloc_urb(0, gfp_mask);
220}
221
222static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
223{
224 unsigned long flags;
225 int i;
226 /*
227 * See if this was an urb in our pool, if so mark it "free", otherwise
228 * we need to free it ourselves.
229 */
230 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
231 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
232 if (urb == es1->cport_out_urb[i]) {
233 es1->cport_out_urb_busy[i] = false;
234 urb = NULL;
235 break;
236 }
237 }
238 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
239
240 /* If urb is not NULL, then we need to free this urb */
241 usb_free_urb(urb);
242}
243
244/*
Alex Elderd29b3d62015-06-13 11:02:08 -0500245 * We (ab)use the operation-message header pad bytes to transfer the
246 * cport id in order to minimise overhead.
247 */
248static void
249gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
250{
Alex Elder4bc13892015-06-13 11:02:11 -0500251 header->pad[0] = cport_id;
Alex Elderd29b3d62015-06-13 11:02:08 -0500252}
253
254/* Clear the pad bytes used for the CPort id */
255static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
256{
Alex Elder4bc13892015-06-13 11:02:11 -0500257 header->pad[0] = 0;
Alex Elderd29b3d62015-06-13 11:02:08 -0500258}
259
260/* Extract the CPort id packed into the header, and clear it */
261static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
262{
Alex Elder4bc13892015-06-13 11:02:11 -0500263 u16 cport_id = header->pad[0];
Alex Elderd29b3d62015-06-13 11:02:08 -0500264
265 gb_message_cport_clear(header);
266
267 return cport_id;
268}
269
270/*
Johan Hovold3e136cc2015-07-01 12:37:21 +0200271 * Returns zero if the message was successfully queued, or a negative errno
272 * otherwise.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800273 */
Johan Hovold3e136cc2015-07-01 12:37:21 +0200274static int message_send(struct greybus_host_device *hd, u16 cport_id,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200275 struct gb_message *message, gfp_t gfp_mask)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800276{
277 struct es1_ap_dev *es1 = hd_to_es1(hd);
278 struct usb_device *udev = es1->usb_dev;
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200279 size_t buffer_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800280 int retval;
281 struct urb *urb;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200282 int bulk_ep_set;
Johan Hovold3e136cc2015-07-01 12:37:21 +0200283 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800284
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800285 /*
286 * The data actually transferred will include an indication
287 * of where the data should be sent. Do one last check of
288 * the target CPort id before filling it in.
289 */
Alex Elder821c6202015-06-13 11:02:07 -0500290 if (!cport_id_valid(cport_id)) {
291 pr_err("invalid destination cport 0x%02x\n", cport_id);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200292 return -EINVAL;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800293 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800294
295 /* Find a free urb */
296 urb = next_free_urb(es1, gfp_mask);
297 if (!urb)
Johan Hovold3e136cc2015-07-01 12:37:21 +0200298 return -ENOMEM;
299
300 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
301 message->hcpriv = urb;
302 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800303
Alex Elderd29b3d62015-06-13 11:02:08 -0500304 /* Pack the cport id into the message header */
305 gb_message_cport_pack(message->header, cport_id);
Johan Hovold491e60d2015-04-07 11:27:20 +0200306
Alex Elder821c6202015-06-13 11:02:07 -0500307 buffer_size = sizeof(*message->header) + message->payload_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800308
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200309 bulk_ep_set = cport_to_ep(es1, cport_id);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800310 usb_fill_bulk_urb(urb, udev,
Alexandre Bailon606addd2015-06-15 18:08:13 +0200311 usb_sndbulkpipe(udev,
312 es1->cport_out[bulk_ep_set].endpoint),
Alex Elder821c6202015-06-13 11:02:07 -0500313 message->buffer, buffer_size,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200314 cport_out_callback, message);
Alexandre Bailon977e2092015-08-31 09:00:16 +0200315 urb->transfer_flags |= URB_ZERO_PACKET;
Bryan O'Donoghue3f2a8092015-08-11 13:50:52 +0100316 gb_connection_push_timestamp(message->operation->connection);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800317 retval = usb_submit_urb(urb, gfp_mask);
318 if (retval) {
319 pr_err("error %d submitting URB\n", retval);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200320
321 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
322 message->hcpriv = NULL;
323 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
324
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800325 free_urb(es1, urb);
Alex Elderd29b3d62015-06-13 11:02:08 -0500326 gb_message_cport_clear(message->header);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200327
328 return retval;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800329 }
330
Johan Hovold3e136cc2015-07-01 12:37:21 +0200331 return 0;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800332}
333
334/*
Johan Hovold3e136cc2015-07-01 12:37:21 +0200335 * Can not be called in atomic context.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800336 */
Johan Hovold3e136cc2015-07-01 12:37:21 +0200337static void message_cancel(struct gb_message *message)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800338{
Johan Hovold3e136cc2015-07-01 12:37:21 +0200339 struct greybus_host_device *hd = message->operation->connection->hd;
340 struct es1_ap_dev *es1 = hd_to_es1(hd);
341 struct urb *urb;
342 int i;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800343
Johan Hovold3e136cc2015-07-01 12:37:21 +0200344 might_sleep();
345
346 spin_lock_irq(&es1->cport_out_urb_lock);
347 urb = message->hcpriv;
348
349 /* Prevent dynamically allocated urb from being deallocated. */
350 usb_get_urb(urb);
351
352 /* Prevent pre-allocated urb from being reused. */
353 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
354 if (urb == es1->cport_out_urb[i]) {
355 es1->cport_out_urb_cancelled[i] = true;
356 break;
357 }
358 }
359 spin_unlock_irq(&es1->cport_out_urb_lock);
360
361 usb_kill_urb(urb);
362
363 if (i < NUM_CPORT_OUT_URB) {
364 spin_lock_irq(&es1->cport_out_urb_lock);
365 es1->cport_out_urb_cancelled[i] = false;
366 spin_unlock_irq(&es1->cport_out_urb_lock);
367 }
368
369 usb_free_urb(urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800370}
371
372static struct greybus_host_driver es1_driver = {
373 .hd_priv_size = sizeof(struct es1_ap_dev),
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200374 .message_send = message_send,
375 .message_cancel = message_cancel,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800376};
377
378/* Common function to report consistent warnings based on URB status */
379static int check_urb_status(struct urb *urb)
380{
381 struct device *dev = &urb->dev->dev;
382 int status = urb->status;
383
384 switch (status) {
385 case 0:
386 return 0;
387
388 case -EOVERFLOW:
389 dev_err(dev, "%s: overflow actual length is %d\n",
390 __func__, urb->actual_length);
391 case -ECONNRESET:
392 case -ENOENT:
393 case -ESHUTDOWN:
394 case -EILSEQ:
395 case -EPROTO:
396 /* device is gone, stop sending */
397 return status;
398 }
399 dev_err(dev, "%s: unknown status %d\n", __func__, status);
400
401 return -EAGAIN;
402}
403
404static void ap_disconnect(struct usb_interface *interface)
405{
406 struct es1_ap_dev *es1;
407 struct usb_device *udev;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200408 int bulk_in;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800409 int i;
410
411 es1 = usb_get_intfdata(interface);
412 if (!es1)
413 return;
414
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100415 usb_log_disable(es1);
416
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800417 /* Tear down everything! */
418 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
419 struct urb *urb = es1->cport_out_urb[i];
420
421 if (!urb)
422 break;
423 usb_kill_urb(urb);
424 usb_free_urb(urb);
425 es1->cport_out_urb[i] = NULL;
426 es1->cport_out_urb_busy[i] = false; /* just to be anal */
427 }
428
Alexandre Bailon606addd2015-06-15 18:08:13 +0200429 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
430 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
431 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
432 struct urb *urb = cport_in->urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800433
Alexandre Bailon606addd2015-06-15 18:08:13 +0200434 if (!urb)
435 break;
436 usb_kill_urb(urb);
437 usb_free_urb(urb);
438 kfree(cport_in->buffer[i]);
439 cport_in->buffer[i] = NULL;
440 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800441 }
442
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800443 usb_set_intfdata(interface, NULL);
444 udev = es1->usb_dev;
445 greybus_remove_hd(es1->hd);
446
447 usb_put_dev(udev);
448}
449
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800450static void cport_in_callback(struct urb *urb)
451{
452 struct greybus_host_device *hd = urb->context;
453 struct device *dev = &urb->dev->dev;
Johan Hovold491e60d2015-04-07 11:27:20 +0200454 struct gb_operation_msg_hdr *header;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800455 int status = check_urb_status(urb);
456 int retval;
457 u16 cport_id;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800458
459 if (status) {
460 if ((status == -EAGAIN) || (status == -EPROTO))
461 goto exit;
462 dev_err(dev, "urb cport in error %d (dropped)\n", status);
463 return;
464 }
465
Johan Hovold491e60d2015-04-07 11:27:20 +0200466 if (urb->actual_length < sizeof(*header)) {
467 dev_err(dev, "%s: short message received\n", __func__);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800468 goto exit;
469 }
470
Alex Elderd29b3d62015-06-13 11:02:08 -0500471 /* Extract the CPort id, which is packed in the message header */
Johan Hovold491e60d2015-04-07 11:27:20 +0200472 header = urb->transfer_buffer;
Alex Elderd29b3d62015-06-13 11:02:08 -0500473 cport_id = gb_message_cport_unpack(header);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800474
Alex Elder821c6202015-06-13 11:02:07 -0500475 if (cport_id_valid(cport_id))
476 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
Johan Hovold491e60d2015-04-07 11:27:20 +0200477 urb->actual_length);
Alex Elder821c6202015-06-13 11:02:07 -0500478 else
479 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
480 __func__, cport_id);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800481exit:
482 /* put our urb back in the request pool */
483 retval = usb_submit_urb(urb, GFP_ATOMIC);
484 if (retval)
485 dev_err(dev, "%s: error %d in submitting urb.\n",
486 __func__, retval);
487}
488
489static void cport_out_callback(struct urb *urb)
490{
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200491 struct gb_message *message = urb->context;
492 struct greybus_host_device *hd = message->operation->connection->hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800493 struct es1_ap_dev *es1 = hd_to_es1(hd);
494 int status = check_urb_status(urb);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200495 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800496
Alex Elderd29b3d62015-06-13 11:02:08 -0500497 gb_message_cport_clear(message->header);
Johan Hovold491e60d2015-04-07 11:27:20 +0200498
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800499 /*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200500 * Tell the submitter that the message send (attempt) is
501 * complete, and report the status.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800502 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200503 greybus_message_sent(hd, message, status);
504
Johan Hovold3e136cc2015-07-01 12:37:21 +0200505 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
506 message->hcpriv = NULL;
507 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
508
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800509 free_urb(es1, urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800510}
511
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200512#define APB1_LOG_MSG_SIZE 64
513static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100514{
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100515 int retval;
516
517 /* SVC messages go down our control pipe */
518 do {
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100519 retval = usb_control_msg(es1->usb_dev,
520 usb_rcvctrlpipe(es1->usb_dev,
521 es1->control_endpoint),
Alexandre Bailon611c17392015-06-15 18:08:11 +0200522 REQUEST_LOG,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100523 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
524 0x00, 0x00,
525 buf,
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200526 APB1_LOG_MSG_SIZE,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100527 ES1_TIMEOUT);
528 if (retval > 0)
529 kfifo_in(&apb1_log_fifo, buf, retval);
530 } while (retval > 0);
531}
532
533static int apb1_log_poll(void *data)
534{
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200535 struct es1_ap_dev *es1 = data;
536 char *buf;
537
538 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
539 if (!buf)
540 return -ENOMEM;
541
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100542 while (!kthread_should_stop()) {
543 msleep(1000);
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200544 apb1_log_get(es1, buf);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100545 }
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200546
547 kfree(buf);
548
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100549 return 0;
550}
551
552static ssize_t apb1_log_read(struct file *f, char __user *buf,
553 size_t count, loff_t *ppos)
554{
555 ssize_t ret;
556 size_t copied;
557 char *tmp_buf;
558
559 if (count > APB1_LOG_SIZE)
560 count = APB1_LOG_SIZE;
561
562 tmp_buf = kmalloc(count, GFP_KERNEL);
563 if (!tmp_buf)
564 return -ENOMEM;
565
566 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
567 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
568
569 kfree(tmp_buf);
570
571 return ret;
572}
573
574static const struct file_operations apb1_log_fops = {
575 .read = apb1_log_read,
576};
577
578static void usb_log_enable(struct es1_ap_dev *es1)
579{
Alex Eldere0feaf12015-03-27 15:20:49 -0500580 if (!IS_ERR_OR_NULL(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100581 return;
582
583 /* get log from APB1 */
584 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
Alex Eldere0feaf12015-03-27 15:20:49 -0500585 if (IS_ERR(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100586 return;
587 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
588 gb_debugfs_get(), NULL,
589 &apb1_log_fops);
590}
591
592static void usb_log_disable(struct es1_ap_dev *es1)
593{
Alex Eldere0feaf12015-03-27 15:20:49 -0500594 if (IS_ERR_OR_NULL(apb1_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100595 return;
596
597 debugfs_remove(apb1_log_dentry);
598 apb1_log_dentry = NULL;
599
600 kthread_stop(apb1_log_task);
601 apb1_log_task = NULL;
602}
603
604static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
605 size_t count, loff_t *ppos)
606{
607 char tmp_buf[3];
Alex Eldere0feaf12015-03-27 15:20:49 -0500608 int enable = !IS_ERR_OR_NULL(apb1_log_task);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100609
610 sprintf(tmp_buf, "%d\n", enable);
611 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
612}
613
614static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
615 size_t count, loff_t *ppos)
616{
617 int enable;
618 ssize_t retval;
619 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
620
621 retval = kstrtoint_from_user(buf, count, 10, &enable);
622 if (retval)
623 return retval;
624
625 if (enable)
626 usb_log_enable(es1);
627 else
628 usb_log_disable(es1);
629
630 return count;
631}
632
633static const struct file_operations apb1_log_enable_fops = {
634 .read = apb1_log_enable_read,
635 .write = apb1_log_enable_write,
636};
637
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800638/*
639 * The ES1 USB Bridge device contains 4 endpoints
640 * 1 Control - usual USB stuff + AP -> SVC messages
641 * 1 Interrupt IN - SVC -> AP messages
642 * 1 Bulk IN - CPort data in
643 * 1 Bulk OUT - CPort data out
644 */
645static int ap_probe(struct usb_interface *interface,
646 const struct usb_device_id *id)
647{
648 struct es1_ap_dev *es1;
649 struct greybus_host_device *hd;
650 struct usb_device *udev;
651 struct usb_host_interface *iface_desc;
652 struct usb_endpoint_descriptor *endpoint;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200653 int bulk_in = 0;
654 int bulk_out = 0;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800655 int retval = -ENOMEM;
656 int i;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800657
Alex Elder4bc13892015-06-13 11:02:11 -0500658 /* We need to fit a CPort ID in one byte of a message header */
659 BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
660
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800661 udev = usb_get_dev(interface_to_usbdev(interface));
662
Johan Hovoldd9336672015-05-19 11:22:43 +0200663 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
Alex Elder8ea70fe2015-05-22 09:52:45 -0500664 if (IS_ERR(hd)) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800665 usb_put_dev(udev);
Alex Elder8ea70fe2015-05-22 09:52:45 -0500666 return PTR_ERR(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800667 }
668
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800669 es1 = hd_to_es1(hd);
670 es1->hd = hd;
671 es1->usb_intf = interface;
672 es1->usb_dev = udev;
673 spin_lock_init(&es1->cport_out_urb_lock);
674 usb_set_intfdata(interface, es1);
675
676 /* Control endpoint is the pipe to talk to this AP, so save it off */
677 endpoint = &udev->ep0.desc;
678 es1->control_endpoint = endpoint->bEndpointAddress;
679
680 /* find all 3 of our endpoints */
681 iface_desc = interface->cur_altsetting;
682 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
683 endpoint = &iface_desc->endpoint[i].desc;
684
Greg Kroah-Hartmanb767ee42015-07-24 17:09:48 -0700685 if (usb_endpoint_is_bulk_in(endpoint)) {
Alexandre Bailon606addd2015-06-15 18:08:13 +0200686 es1->cport_in[bulk_in++].endpoint =
687 endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800688 } else if (usb_endpoint_is_bulk_out(endpoint)) {
Alexandre Bailon606addd2015-06-15 18:08:13 +0200689 es1->cport_out[bulk_out++].endpoint =
690 endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800691 } else {
692 dev_err(&udev->dev,
693 "Unknown endpoint type found, address %x\n",
694 endpoint->bEndpointAddress);
695 }
696 }
Greg Kroah-Hartmanb767ee42015-07-24 17:09:48 -0700697 if ((bulk_in == 0) ||
Alexandre Bailon606addd2015-06-15 18:08:13 +0200698 (bulk_out == 0)) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800699 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
700 goto error;
701 }
702
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800703 /* Allocate buffers for our cport in messages and start them up */
Alexandre Bailon606addd2015-06-15 18:08:13 +0200704 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
705 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
706 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
707 struct urb *urb;
708 u8 *buffer;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800709
Alexandre Bailon606addd2015-06-15 18:08:13 +0200710 urb = usb_alloc_urb(0, GFP_KERNEL);
711 if (!urb)
712 goto error;
713 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
714 if (!buffer)
715 goto error;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800716
Alexandre Bailon606addd2015-06-15 18:08:13 +0200717 usb_fill_bulk_urb(urb, udev,
718 usb_rcvbulkpipe(udev,
719 cport_in->endpoint),
720 buffer, ES1_GBUF_MSG_SIZE_MAX,
721 cport_in_callback, hd);
722 cport_in->urb[i] = urb;
723 cport_in->buffer[i] = buffer;
724 retval = usb_submit_urb(urb, GFP_KERNEL);
725 if (retval)
726 goto error;
727 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800728 }
729
730 /* Allocate urbs for our CPort OUT messages */
731 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
732 struct urb *urb;
733
734 urb = usb_alloc_urb(0, GFP_KERNEL);
735 if (!urb)
736 goto error;
737
738 es1->cport_out_urb[i] = urb;
739 es1->cport_out_urb_busy[i] = false; /* just to be anal */
740 }
741
Johan Hovold86f918e2015-06-23 14:17:41 +0200742 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
743 (S_IWUSR | S_IRUGO),
744 gb_debugfs_get(), es1,
745 &apb1_log_enable_fops);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800746 return 0;
747error:
748 ap_disconnect(interface);
749
750 return retval;
751}
752
753static struct usb_driver es1_ap_driver = {
Rob Herringc13c8bf2015-05-05 11:04:22 -0500754 .name = "es2_ap_driver",
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800755 .probe = ap_probe,
756 .disconnect = ap_disconnect,
757 .id_table = id_table,
758};
759
760module_usb_driver(es1_ap_driver);
761
Greg Kroah-Hartman6cf42a42015-04-13 19:51:33 +0200762MODULE_LICENSE("GPL v2");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800763MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");