1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus "AP" USB driver for "ES2" controller chips
4 *
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
7 */
8 #include <linux/kthread.h>
9 #include <linux/sizes.h>
10 #include <linux/usb.h>
11 #include <linux/kfifo.h>
12 #include <linux/debugfs.h>
13 #include <linux/list.h>
14 #include <linux/greybus.h>
15 #include <linux/unaligned.h>
16
17 #include "arpc.h"
18 #include "greybus_trace.h"
19
20
21 /* Default timeout for USB vendor requests. */
22 #define ES2_USB_CTRL_TIMEOUT 500
23
24 /* Default timeout for ARPC CPort requests */
25 #define ES2_ARPC_CPORT_TIMEOUT 500
26
27 /* Fixed CPort numbers */
28 #define ES2_CPORT_CDSI0 16
29 #define ES2_CPORT_CDSI1 17
30
31 /* Memory sizes for the buffers sent to/from the ES2 controller */
32 #define ES2_GBUF_MSG_SIZE_MAX 2048
33
34 /* Memory sizes for the ARPC buffers */
35 #define ARPC_OUT_SIZE_MAX U16_MAX
36 #define ARPC_IN_SIZE_MAX 128
37
38 static const struct usb_device_id id_table[] = {
39 { USB_DEVICE(0x18d1, 0x1eaf) },
40 { },
41 };
42 MODULE_DEVICE_TABLE(usb, id_table);
43
44 #define APB1_LOG_SIZE SZ_16K
45
46 /*
47 * Number of CPort IN urbs in flight at any point in time.
48 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
49 * flight.
50 */
51 #define NUM_CPORT_IN_URB 4
52
53 /* Number of CPort OUT urbs in flight at any point in time.
54 * Adjust if we get messages saying we are out of urbs in the system log.
55 */
56 #define NUM_CPORT_OUT_URB 8
57
58 /*
59 * Number of ARPC in urbs in flight at any point in time.
60 */
61 #define NUM_ARPC_IN_URB 2
62
63 /*
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 */
68 struct es2_cport_in {
69 __u8 endpoint;
70 struct urb *urb[NUM_CPORT_IN_URB];
71 u8 *buffer[NUM_CPORT_IN_URB];
72 };
73
74 /**
75 * struct es2_ap_dev - ES2 USB Bridge to AP structure
76 * @usb_dev: pointer to the USB device we are.
77 * @usb_intf: pointer to the USB interface we are bound to.
78 * @hd: pointer to our gb_host_device structure
79 *
80 * @cport_in: endpoint, urbs and buffer for cport in messages
81 * @cport_out_endpoint: endpoint for cport out messages
82 * @cport_out_urb: array of urbs for the CPort out messages
83 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
84 * not.
85 * @cport_out_urb_cancelled: array of flags indicating whether the
86 * corresponding @cport_out_urb is being cancelled
87 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
88 * @cdsi1_in_use: true if cport CDSI1 is in use
89 * @apb_log_task: task pointer for logging thread
90 * @apb_log_dentry: file system entry for the log file interface
91 * @apb_log_enable_dentry: file system entry for enabling logging
92 * @apb_log_fifo: kernel FIFO to carry logged data
93 * @arpc_urb: array of urbs for the ARPC in messages
94 * @arpc_buffer: array of buffers for the @arpc_urb urbs
95 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
96 * @arpc_id_cycle: gives an unique id to ARPC
97 * @arpc_lock: locks ARPC list
98 * @arpcs: list of in progress ARPCs
99 */
100 struct es2_ap_dev {
101 struct usb_device *usb_dev;
102 struct usb_interface *usb_intf;
103 struct gb_host_device *hd;
104
105 struct es2_cport_in cport_in;
106 __u8 cport_out_endpoint;
107 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
108 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
109 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
110 spinlock_t cport_out_urb_lock;
111
112 bool cdsi1_in_use;
113
114 struct task_struct *apb_log_task;
115 struct dentry *apb_log_dentry;
116 struct dentry *apb_log_enable_dentry;
117 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
118
119 __u8 arpc_endpoint_in;
120 struct urb *arpc_urb[NUM_ARPC_IN_URB];
121 u8 *arpc_buffer[NUM_ARPC_IN_URB];
122
123 int arpc_id_cycle;
124 spinlock_t arpc_lock;
125 struct list_head arpcs;
126 };
127
128 struct arpc {
129 struct list_head list;
130 struct arpc_request_message *req;
131 struct arpc_response_message *resp;
132 struct completion response_received;
133 bool active;
134 };
135
hd_to_es2(struct gb_host_device * hd)136 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
137 {
138 return (struct es2_ap_dev *)&hd->hd_priv;
139 }
140
141 static void cport_out_callback(struct urb *urb);
142 static void usb_log_enable(struct es2_ap_dev *es2);
143 static void usb_log_disable(struct es2_ap_dev *es2);
144 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
145 size_t size, int *result, unsigned int timeout);
146
output_sync(struct es2_ap_dev * es2,void * req,u16 size,u8 cmd)147 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
148 {
149 struct usb_device *udev = es2->usb_dev;
150 u8 *data;
151 int retval;
152
153 data = kmemdup(req, size, GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
156
157 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
158 cmd,
159 USB_DIR_OUT | USB_TYPE_VENDOR |
160 USB_RECIP_INTERFACE,
161 0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
162 if (retval < 0)
163 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
164 else
165 retval = 0;
166
167 kfree(data);
168 return retval;
169 }
170
ap_urb_complete(struct urb * urb)171 static void ap_urb_complete(struct urb *urb)
172 {
173 struct usb_ctrlrequest *dr = urb->context;
174
175 kfree(dr);
176 usb_free_urb(urb);
177 }
178
output_async(struct es2_ap_dev * es2,void * req,u16 size,u8 cmd)179 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
180 {
181 struct usb_device *udev = es2->usb_dev;
182 struct urb *urb;
183 struct usb_ctrlrequest *dr;
184 u8 *buf;
185 int retval;
186
187 urb = usb_alloc_urb(0, GFP_ATOMIC);
188 if (!urb)
189 return -ENOMEM;
190
191 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
192 if (!dr) {
193 usb_free_urb(urb);
194 return -ENOMEM;
195 }
196
197 buf = (u8 *)dr + sizeof(*dr);
198 memcpy(buf, req, size);
199
200 dr->bRequest = cmd;
201 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
202 dr->wValue = 0;
203 dr->wIndex = 0;
204 dr->wLength = cpu_to_le16(size);
205
206 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
207 (unsigned char *)dr, buf, size,
208 ap_urb_complete, dr);
209 retval = usb_submit_urb(urb, GFP_ATOMIC);
210 if (retval) {
211 usb_free_urb(urb);
212 kfree(dr);
213 }
214 return retval;
215 }
216
output(struct gb_host_device * hd,void * req,u16 size,u8 cmd,bool async)217 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
218 bool async)
219 {
220 struct es2_ap_dev *es2 = hd_to_es2(hd);
221
222 if (async)
223 return output_async(es2, req, size, cmd);
224
225 return output_sync(es2, req, size, cmd);
226 }
227
es2_cport_in_enable(struct es2_ap_dev * es2,struct es2_cport_in * cport_in)228 static int es2_cport_in_enable(struct es2_ap_dev *es2,
229 struct es2_cport_in *cport_in)
230 {
231 struct urb *urb;
232 int ret;
233 int i;
234
235 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
236 urb = cport_in->urb[i];
237
238 ret = usb_submit_urb(urb, GFP_KERNEL);
239 if (ret) {
240 dev_err(&es2->usb_dev->dev,
241 "failed to submit in-urb: %d\n", ret);
242 goto err_kill_urbs;
243 }
244 }
245
246 return 0;
247
248 err_kill_urbs:
249 for (--i; i >= 0; --i) {
250 urb = cport_in->urb[i];
251 usb_kill_urb(urb);
252 }
253
254 return ret;
255 }
256
es2_cport_in_disable(struct es2_ap_dev * es2,struct es2_cport_in * cport_in)257 static void es2_cport_in_disable(struct es2_ap_dev *es2,
258 struct es2_cport_in *cport_in)
259 {
260 struct urb *urb;
261 int i;
262
263 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
264 urb = cport_in->urb[i];
265 usb_kill_urb(urb);
266 }
267 }
268
es2_arpc_in_enable(struct es2_ap_dev * es2)269 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
270 {
271 struct urb *urb;
272 int ret;
273 int i;
274
275 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
276 urb = es2->arpc_urb[i];
277
278 ret = usb_submit_urb(urb, GFP_KERNEL);
279 if (ret) {
280 dev_err(&es2->usb_dev->dev,
281 "failed to submit arpc in-urb: %d\n", ret);
282 goto err_kill_urbs;
283 }
284 }
285
286 return 0;
287
288 err_kill_urbs:
289 for (--i; i >= 0; --i) {
290 urb = es2->arpc_urb[i];
291 usb_kill_urb(urb);
292 }
293
294 return ret;
295 }
296
es2_arpc_in_disable(struct es2_ap_dev * es2)297 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
298 {
299 struct urb *urb;
300 int i;
301
302 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
303 urb = es2->arpc_urb[i];
304 usb_kill_urb(urb);
305 }
306 }
307
next_free_urb(struct es2_ap_dev * es2,gfp_t gfp_mask)308 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
309 {
310 struct urb *urb = NULL;
311 unsigned long flags;
312 int i;
313
314 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
315
316 /* Look in our pool of allocated urbs first, as that's the "fastest" */
317 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
318 if (!es2->cport_out_urb_busy[i] &&
319 !es2->cport_out_urb_cancelled[i]) {
320 es2->cport_out_urb_busy[i] = true;
321 urb = es2->cport_out_urb[i];
322 break;
323 }
324 }
325 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
326 if (urb)
327 return urb;
328
329 /*
330 * Crap, pool is empty, complain to the syslog and go allocate one
331 * dynamically as we have to succeed.
332 */
333 dev_dbg(&es2->usb_dev->dev,
334 "No free CPort OUT urbs, having to dynamically allocate one!\n");
335 return usb_alloc_urb(0, gfp_mask);
336 }
337
free_urb(struct es2_ap_dev * es2,struct urb * urb)338 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
339 {
340 unsigned long flags;
341 int i;
342 /*
343 * See if this was an urb in our pool, if so mark it "free", otherwise
344 * we need to free it ourselves.
345 */
346 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
347 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
348 if (urb == es2->cport_out_urb[i]) {
349 es2->cport_out_urb_busy[i] = false;
350 urb = NULL;
351 break;
352 }
353 }
354 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
355
356 /* If urb is not NULL, then we need to free this urb */
357 usb_free_urb(urb);
358 }
359
360 /*
361 * We (ab)use the operation-message header pad bytes to transfer the
362 * cport id in order to minimise overhead.
363 */
364 static void
gb_message_cport_pack(struct gb_operation_msg_hdr * header,u16 cport_id)365 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
366 {
367 header->pad[0] = cport_id;
368 }
369
370 /* Clear the pad bytes used for the CPort id */
gb_message_cport_clear(struct gb_operation_msg_hdr * header)371 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
372 {
373 header->pad[0] = 0;
374 }
375
376 /* Extract the CPort id packed into the header, and clear it */
gb_message_cport_unpack(struct gb_operation_msg_hdr * header)377 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
378 {
379 u16 cport_id = header->pad[0];
380
381 gb_message_cport_clear(header);
382
383 return cport_id;
384 }
385
386 /*
387 * Returns zero if the message was successfully queued, or a negative errno
388 * otherwise.
389 */
message_send(struct gb_host_device * hd,u16 cport_id,struct gb_message * message,gfp_t gfp_mask)390 static int message_send(struct gb_host_device *hd, u16 cport_id,
391 struct gb_message *message, gfp_t gfp_mask)
392 {
393 struct es2_ap_dev *es2 = hd_to_es2(hd);
394 struct usb_device *udev = es2->usb_dev;
395 size_t buffer_size;
396 int retval;
397 struct urb *urb;
398 unsigned long flags;
399
400 /*
401 * The data actually transferred will include an indication
402 * of where the data should be sent. Do one last check of
403 * the target CPort id before filling it in.
404 */
405 if (!cport_id_valid(hd, cport_id)) {
406 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
407 return -EINVAL;
408 }
409
410 /* Find a free urb */
411 urb = next_free_urb(es2, gfp_mask);
412 if (!urb)
413 return -ENOMEM;
414
415 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
416 message->hcpriv = urb;
417 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
418
419 /* Pack the cport id into the message header */
420 gb_message_cport_pack(message->header, cport_id);
421
422 buffer_size = sizeof(*message->header) + message->payload_size;
423
424 usb_fill_bulk_urb(urb, udev,
425 usb_sndbulkpipe(udev,
426 es2->cport_out_endpoint),
427 message->buffer, buffer_size,
428 cport_out_callback, message);
429 urb->transfer_flags |= URB_ZERO_PACKET;
430
431 trace_gb_message_submit(message);
432
433 retval = usb_submit_urb(urb, gfp_mask);
434 if (retval) {
435 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
436
437 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
438 message->hcpriv = NULL;
439 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
440
441 free_urb(es2, urb);
442 gb_message_cport_clear(message->header);
443
444 return retval;
445 }
446
447 return 0;
448 }
449
450 /*
451 * Can not be called in atomic context.
452 */
message_cancel(struct gb_message * message)453 static void message_cancel(struct gb_message *message)
454 {
455 struct gb_host_device *hd = message->operation->connection->hd;
456 struct es2_ap_dev *es2 = hd_to_es2(hd);
457 struct urb *urb;
458 int i;
459
460 might_sleep();
461
462 spin_lock_irq(&es2->cport_out_urb_lock);
463 urb = message->hcpriv;
464
465 /* Prevent dynamically allocated urb from being deallocated. */
466 usb_get_urb(urb);
467
468 /* Prevent pre-allocated urb from being reused. */
469 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
470 if (urb == es2->cport_out_urb[i]) {
471 es2->cport_out_urb_cancelled[i] = true;
472 break;
473 }
474 }
475 spin_unlock_irq(&es2->cport_out_urb_lock);
476
477 usb_kill_urb(urb);
478
479 if (i < NUM_CPORT_OUT_URB) {
480 spin_lock_irq(&es2->cport_out_urb_lock);
481 es2->cport_out_urb_cancelled[i] = false;
482 spin_unlock_irq(&es2->cport_out_urb_lock);
483 }
484
485 usb_free_urb(urb);
486 }
487
es2_cport_allocate(struct gb_host_device * hd,int cport_id,unsigned long flags)488 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
489 unsigned long flags)
490 {
491 struct es2_ap_dev *es2 = hd_to_es2(hd);
492 struct ida *id_map = &hd->cport_id_map;
493 int ida_start, ida_end;
494
495 switch (cport_id) {
496 case ES2_CPORT_CDSI0:
497 case ES2_CPORT_CDSI1:
498 dev_err(&hd->dev, "cport %d not available\n", cport_id);
499 return -EBUSY;
500 }
501
502 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
503 flags & GB_CONNECTION_FLAG_CDSI1) {
504 if (es2->cdsi1_in_use) {
505 dev_err(&hd->dev, "CDSI1 already in use\n");
506 return -EBUSY;
507 }
508
509 es2->cdsi1_in_use = true;
510
511 return ES2_CPORT_CDSI1;
512 }
513
514 if (cport_id < 0) {
515 ida_start = 0;
516 ida_end = hd->num_cports - 1;
517 } else if (cport_id < hd->num_cports) {
518 ida_start = cport_id;
519 ida_end = cport_id;
520 } else {
521 dev_err(&hd->dev, "cport %d not available\n", cport_id);
522 return -EINVAL;
523 }
524
525 return ida_alloc_range(id_map, ida_start, ida_end, GFP_KERNEL);
526 }
527
es2_cport_release(struct gb_host_device * hd,u16 cport_id)528 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
529 {
530 struct es2_ap_dev *es2 = hd_to_es2(hd);
531
532 switch (cport_id) {
533 case ES2_CPORT_CDSI1:
534 es2->cdsi1_in_use = false;
535 return;
536 }
537
538 ida_free(&hd->cport_id_map, cport_id);
539 }
540
cport_enable(struct gb_host_device * hd,u16 cport_id,unsigned long flags)541 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
542 unsigned long flags)
543 {
544 struct es2_ap_dev *es2 = hd_to_es2(hd);
545 struct usb_device *udev = es2->usb_dev;
546 struct gb_apb_request_cport_flags *req;
547 u32 connection_flags;
548 int ret;
549
550 req = kzalloc_obj(*req);
551 if (!req)
552 return -ENOMEM;
553
554 connection_flags = 0;
555 if (flags & GB_CONNECTION_FLAG_CONTROL)
556 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
557 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
558 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
559
560 req->flags = cpu_to_le32(connection_flags);
561
562 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
563 cport_id, connection_flags);
564
565 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
566 GB_APB_REQUEST_CPORT_FLAGS,
567 USB_DIR_OUT | USB_TYPE_VENDOR |
568 USB_RECIP_INTERFACE, cport_id, 0,
569 req, sizeof(*req), ES2_USB_CTRL_TIMEOUT);
570 if (ret < 0) {
571 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
572 cport_id);
573 goto out;
574 }
575
576 ret = 0;
577 out:
578 kfree(req);
579
580 return ret;
581 }
582
es2_cport_connected(struct gb_host_device * hd,u16 cport_id)583 static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id)
584 {
585 struct es2_ap_dev *es2 = hd_to_es2(hd);
586 struct device *dev = &es2->usb_dev->dev;
587 struct arpc_cport_connected_req req;
588 int ret;
589
590 req.cport_id = cpu_to_le16(cport_id);
591 ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req),
592 NULL, ES2_ARPC_CPORT_TIMEOUT);
593 if (ret) {
594 dev_err(dev, "failed to set connected state for cport %u: %d\n",
595 cport_id, ret);
596 return ret;
597 }
598
599 return 0;
600 }
601
es2_cport_flush(struct gb_host_device * hd,u16 cport_id)602 static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id)
603 {
604 struct es2_ap_dev *es2 = hd_to_es2(hd);
605 struct device *dev = &es2->usb_dev->dev;
606 struct arpc_cport_flush_req req;
607 int ret;
608
609 req.cport_id = cpu_to_le16(cport_id);
610 ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req),
611 NULL, ES2_ARPC_CPORT_TIMEOUT);
612 if (ret) {
613 dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret);
614 return ret;
615 }
616
617 return 0;
618 }
619
es2_cport_shutdown(struct gb_host_device * hd,u16 cport_id,u8 phase,unsigned int timeout)620 static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id,
621 u8 phase, unsigned int timeout)
622 {
623 struct es2_ap_dev *es2 = hd_to_es2(hd);
624 struct device *dev = &es2->usb_dev->dev;
625 struct arpc_cport_shutdown_req req;
626 int result;
627 int ret;
628
629 if (timeout > U16_MAX)
630 return -EINVAL;
631
632 req.cport_id = cpu_to_le16(cport_id);
633 req.timeout = cpu_to_le16(timeout);
634 req.phase = phase;
635 ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req),
636 &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
637 if (ret) {
638 dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n",
639 cport_id, ret, result);
640 return ret;
641 }
642
643 return 0;
644 }
645
es2_cport_quiesce(struct gb_host_device * hd,u16 cport_id,size_t peer_space,unsigned int timeout)646 static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id,
647 size_t peer_space, unsigned int timeout)
648 {
649 struct es2_ap_dev *es2 = hd_to_es2(hd);
650 struct device *dev = &es2->usb_dev->dev;
651 struct arpc_cport_quiesce_req req;
652 int result;
653 int ret;
654
655 if (peer_space > U16_MAX)
656 return -EINVAL;
657
658 if (timeout > U16_MAX)
659 return -EINVAL;
660
661 req.cport_id = cpu_to_le16(cport_id);
662 req.peer_space = cpu_to_le16(peer_space);
663 req.timeout = cpu_to_le16(timeout);
664 ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req),
665 &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
666 if (ret) {
667 dev_err(dev, "failed to quiesce cport %u: %d (%d)\n",
668 cport_id, ret, result);
669 return ret;
670 }
671
672 return 0;
673 }
674
es2_cport_clear(struct gb_host_device * hd,u16 cport_id)675 static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id)
676 {
677 struct es2_ap_dev *es2 = hd_to_es2(hd);
678 struct device *dev = &es2->usb_dev->dev;
679 struct arpc_cport_clear_req req;
680 int ret;
681
682 req.cport_id = cpu_to_le16(cport_id);
683 ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req),
684 NULL, ES2_ARPC_CPORT_TIMEOUT);
685 if (ret) {
686 dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret);
687 return ret;
688 }
689
690 return 0;
691 }
692
latency_tag_enable(struct gb_host_device * hd,u16 cport_id)693 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
694 {
695 int retval;
696 struct es2_ap_dev *es2 = hd_to_es2(hd);
697 struct usb_device *udev = es2->usb_dev;
698
699 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
700 GB_APB_REQUEST_LATENCY_TAG_EN,
701 USB_DIR_OUT | USB_TYPE_VENDOR |
702 USB_RECIP_INTERFACE, cport_id, 0, NULL,
703 0, ES2_USB_CTRL_TIMEOUT);
704
705 if (retval < 0)
706 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
707 cport_id);
708 return retval;
709 }
710
latency_tag_disable(struct gb_host_device * hd,u16 cport_id)711 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
712 {
713 int retval;
714 struct es2_ap_dev *es2 = hd_to_es2(hd);
715 struct usb_device *udev = es2->usb_dev;
716
717 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
718 GB_APB_REQUEST_LATENCY_TAG_DIS,
719 USB_DIR_OUT | USB_TYPE_VENDOR |
720 USB_RECIP_INTERFACE, cport_id, 0, NULL,
721 0, ES2_USB_CTRL_TIMEOUT);
722
723 if (retval < 0)
724 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
725 cport_id);
726 return retval;
727 }
728
729 static struct gb_hd_driver es2_driver = {
730 .hd_priv_size = sizeof(struct es2_ap_dev),
731 .message_send = message_send,
732 .message_cancel = message_cancel,
733 .cport_allocate = es2_cport_allocate,
734 .cport_release = es2_cport_release,
735 .cport_enable = cport_enable,
736 .cport_connected = es2_cport_connected,
737 .cport_flush = es2_cport_flush,
738 .cport_shutdown = es2_cport_shutdown,
739 .cport_quiesce = es2_cport_quiesce,
740 .cport_clear = es2_cport_clear,
741 .latency_tag_enable = latency_tag_enable,
742 .latency_tag_disable = latency_tag_disable,
743 .output = output,
744 };
745
746 /* Common function to report consistent warnings based on URB status */
check_urb_status(struct urb * urb)747 static int check_urb_status(struct urb *urb)
748 {
749 struct device *dev = &urb->dev->dev;
750 int status = urb->status;
751
752 switch (status) {
753 case 0:
754 return 0;
755
756 case -EOVERFLOW:
757 dev_err(dev, "%s: overflow actual length is %d\n",
758 __func__, urb->actual_length);
759 fallthrough;
760 case -ECONNRESET:
761 case -ENOENT:
762 case -ESHUTDOWN:
763 case -EILSEQ:
764 case -EPROTO:
765 /* device is gone, stop sending */
766 return status;
767 }
768 dev_err(dev, "%s: unknown status %d\n", __func__, status);
769
770 return -EAGAIN;
771 }
772
es2_destroy(struct es2_ap_dev * es2)773 static void es2_destroy(struct es2_ap_dev *es2)
774 {
775 struct urb *urb;
776 int i;
777
778 debugfs_remove(es2->apb_log_enable_dentry);
779 usb_log_disable(es2);
780
781 /* Tear down everything! */
782 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
783 urb = es2->cport_out_urb[i];
784 usb_kill_urb(urb);
785 usb_free_urb(urb);
786 es2->cport_out_urb[i] = NULL;
787 es2->cport_out_urb_busy[i] = false; /* just to be anal */
788 }
789
790 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
791 usb_free_urb(es2->arpc_urb[i]);
792 kfree(es2->arpc_buffer[i]);
793 es2->arpc_buffer[i] = NULL;
794 }
795
796 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
797 usb_free_urb(es2->cport_in.urb[i]);
798 kfree(es2->cport_in.buffer[i]);
799 es2->cport_in.buffer[i] = NULL;
800 }
801
802 /* release reserved CDSI0 and CDSI1 cports */
803 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
804 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
805
806 gb_hd_put(es2->hd);
807 }
808
cport_in_callback(struct urb * urb)809 static void cport_in_callback(struct urb *urb)
810 {
811 struct gb_host_device *hd = urb->context;
812 struct device *dev = &urb->dev->dev;
813 struct gb_operation_msg_hdr *header;
814 int status = check_urb_status(urb);
815 int retval;
816 u16 cport_id;
817
818 if (status) {
819 if ((status == -EAGAIN) || (status == -EPROTO))
820 goto exit;
821
822 /* The urb is being unlinked */
823 if (status == -ENOENT || status == -ESHUTDOWN)
824 return;
825
826 dev_err(dev, "urb cport in error %d (dropped)\n", status);
827 return;
828 }
829
830 if (urb->actual_length < sizeof(*header)) {
831 dev_err(dev, "short message received\n");
832 goto exit;
833 }
834
835 /* Extract the CPort id, which is packed in the message header */
836 header = urb->transfer_buffer;
837 cport_id = gb_message_cport_unpack(header);
838
839 if (cport_id_valid(hd, cport_id)) {
840 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
841 urb->actual_length);
842 } else {
843 dev_err(dev, "invalid cport id %u received\n", cport_id);
844 }
845 exit:
846 /* put our urb back in the request pool */
847 retval = usb_submit_urb(urb, GFP_ATOMIC);
848 if (retval)
849 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
850 }
851
cport_out_callback(struct urb * urb)852 static void cport_out_callback(struct urb *urb)
853 {
854 struct gb_message *message = urb->context;
855 struct gb_host_device *hd = message->operation->connection->hd;
856 struct es2_ap_dev *es2 = hd_to_es2(hd);
857 int status = check_urb_status(urb);
858 unsigned long flags;
859
860 gb_message_cport_clear(message->header);
861
862 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
863 message->hcpriv = NULL;
864 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
865
866 /*
867 * Tell the submitter that the message send (attempt) is
868 * complete, and report the status.
869 */
870 greybus_message_sent(hd, message, status);
871
872 free_urb(es2, urb);
873 }
874
arpc_alloc(void * payload,u16 size,u8 type)875 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
876 {
877 struct arpc *rpc;
878
879 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
880 return NULL;
881
882 rpc = kzalloc_obj(*rpc);
883 if (!rpc)
884 return NULL;
885
886 INIT_LIST_HEAD(&rpc->list);
887 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
888 if (!rpc->req)
889 goto err_free_rpc;
890
891 rpc->resp = kzalloc_obj(*rpc->resp);
892 if (!rpc->resp)
893 goto err_free_req;
894
895 rpc->req->type = type;
896 rpc->req->size = cpu_to_le16(sizeof(*rpc->req) + size);
897 memcpy(rpc->req->data, payload, size);
898
899 init_completion(&rpc->response_received);
900
901 return rpc;
902
903 err_free_req:
904 kfree(rpc->req);
905 err_free_rpc:
906 kfree(rpc);
907
908 return NULL;
909 }
910
arpc_free(struct arpc * rpc)911 static void arpc_free(struct arpc *rpc)
912 {
913 kfree(rpc->req);
914 kfree(rpc->resp);
915 kfree(rpc);
916 }
917
arpc_find(struct es2_ap_dev * es2,__le16 id)918 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
919 {
920 struct arpc *rpc;
921
922 list_for_each_entry(rpc, &es2->arpcs, list) {
923 if (rpc->req->id == id)
924 return rpc;
925 }
926
927 return NULL;
928 }
929
arpc_add(struct es2_ap_dev * es2,struct arpc * rpc)930 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
931 {
932 rpc->active = true;
933 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
934 list_add_tail(&rpc->list, &es2->arpcs);
935 }
936
arpc_del(struct es2_ap_dev * es2,struct arpc * rpc)937 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
938 {
939 if (rpc->active) {
940 rpc->active = false;
941 list_del(&rpc->list);
942 }
943 }
944
arpc_send(struct es2_ap_dev * es2,struct arpc * rpc,int timeout)945 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
946 {
947 struct usb_device *udev = es2->usb_dev;
948 int retval;
949
950 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
951 GB_APB_REQUEST_ARPC_RUN,
952 USB_DIR_OUT | USB_TYPE_VENDOR |
953 USB_RECIP_INTERFACE,
954 0, 0,
955 rpc->req, le16_to_cpu(rpc->req->size),
956 ES2_USB_CTRL_TIMEOUT);
957 if (retval < 0) {
958 dev_err(&udev->dev,
959 "failed to send ARPC request %d: %d\n",
960 rpc->req->type, retval);
961 return retval;
962 }
963
964 return 0;
965 }
966
arpc_sync(struct es2_ap_dev * es2,u8 type,void * payload,size_t size,int * result,unsigned int timeout)967 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
968 size_t size, int *result, unsigned int timeout)
969 {
970 struct arpc *rpc;
971 unsigned long flags;
972 int retval;
973
974 if (result)
975 *result = 0;
976
977 rpc = arpc_alloc(payload, size, type);
978 if (!rpc)
979 return -ENOMEM;
980
981 spin_lock_irqsave(&es2->arpc_lock, flags);
982 arpc_add(es2, rpc);
983 spin_unlock_irqrestore(&es2->arpc_lock, flags);
984
985 retval = arpc_send(es2, rpc, timeout);
986 if (retval)
987 goto out_arpc_del;
988
989 retval = wait_for_completion_interruptible_timeout(
990 &rpc->response_received,
991 msecs_to_jiffies(timeout));
992 if (retval <= 0) {
993 if (!retval)
994 retval = -ETIMEDOUT;
995 goto out_arpc_del;
996 }
997
998 if (rpc->resp->result) {
999 retval = -EREMOTEIO;
1000 if (result)
1001 *result = rpc->resp->result;
1002 } else {
1003 retval = 0;
1004 }
1005
1006 out_arpc_del:
1007 spin_lock_irqsave(&es2->arpc_lock, flags);
1008 arpc_del(es2, rpc);
1009 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1010 arpc_free(rpc);
1011
1012 if (retval < 0 && retval != -EREMOTEIO) {
1013 dev_err(&es2->usb_dev->dev,
1014 "failed to execute ARPC: %d\n", retval);
1015 }
1016
1017 return retval;
1018 }
1019
arpc_in_callback(struct urb * urb)1020 static void arpc_in_callback(struct urb *urb)
1021 {
1022 struct es2_ap_dev *es2 = urb->context;
1023 struct device *dev = &urb->dev->dev;
1024 int status = check_urb_status(urb);
1025 struct arpc *rpc;
1026 struct arpc_response_message *resp;
1027 unsigned long flags;
1028 int retval;
1029
1030 if (status) {
1031 if ((status == -EAGAIN) || (status == -EPROTO))
1032 goto exit;
1033
1034 /* The urb is being unlinked */
1035 if (status == -ENOENT || status == -ESHUTDOWN)
1036 return;
1037
1038 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1039 return;
1040 }
1041
1042 if (urb->actual_length < sizeof(*resp)) {
1043 dev_err(dev, "short aprc response received\n");
1044 goto exit;
1045 }
1046
1047 resp = urb->transfer_buffer;
1048 spin_lock_irqsave(&es2->arpc_lock, flags);
1049 rpc = arpc_find(es2, resp->id);
1050 if (!rpc) {
1051 dev_err(dev, "invalid arpc response id received: %u\n",
1052 le16_to_cpu(resp->id));
1053 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1054 goto exit;
1055 }
1056
1057 arpc_del(es2, rpc);
1058 memcpy(rpc->resp, resp, sizeof(*resp));
1059 complete(&rpc->response_received);
1060 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1061
1062 exit:
1063 /* put our urb back in the request pool */
1064 retval = usb_submit_urb(urb, GFP_ATOMIC);
1065 if (retval)
1066 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1067 }
1068
1069 #define APB1_LOG_MSG_SIZE 64
apb_log_get(struct es2_ap_dev * es2,char * buf)1070 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1071 {
1072 int retval;
1073
1074 do {
1075 retval = usb_control_msg(es2->usb_dev,
1076 usb_rcvctrlpipe(es2->usb_dev, 0),
1077 GB_APB_REQUEST_LOG,
1078 USB_DIR_IN | USB_TYPE_VENDOR |
1079 USB_RECIP_INTERFACE,
1080 0x00, 0x00,
1081 buf,
1082 APB1_LOG_MSG_SIZE,
1083 ES2_USB_CTRL_TIMEOUT);
1084 if (retval > 0)
1085 kfifo_in(&es2->apb_log_fifo, buf, retval);
1086 } while (retval > 0);
1087 }
1088
apb_log_poll(void * data)1089 static int apb_log_poll(void *data)
1090 {
1091 struct es2_ap_dev *es2 = data;
1092 char *buf;
1093
1094 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1095 if (!buf)
1096 return -ENOMEM;
1097
1098 while (!kthread_should_stop()) {
1099 msleep(1000);
1100 apb_log_get(es2, buf);
1101 }
1102
1103 kfree(buf);
1104
1105 return 0;
1106 }
1107
apb_log_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1108 static ssize_t apb_log_read(struct file *f, char __user *buf,
1109 size_t count, loff_t *ppos)
1110 {
1111 struct es2_ap_dev *es2 = file_inode(f)->i_private;
1112 ssize_t ret;
1113 size_t copied;
1114 char *tmp_buf;
1115
1116 if (count > APB1_LOG_SIZE)
1117 count = APB1_LOG_SIZE;
1118
1119 tmp_buf = kmalloc(count, GFP_KERNEL);
1120 if (!tmp_buf)
1121 return -ENOMEM;
1122
1123 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1124 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1125
1126 kfree(tmp_buf);
1127
1128 return ret;
1129 }
1130
1131 static const struct file_operations apb_log_fops = {
1132 .read = apb_log_read,
1133 };
1134
usb_log_enable(struct es2_ap_dev * es2)1135 static void usb_log_enable(struct es2_ap_dev *es2)
1136 {
1137 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1138 return;
1139
1140 /* get log from APB1 */
1141 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1142 if (IS_ERR(es2->apb_log_task))
1143 return;
1144 /* XXX We will need to rename this per APB */
1145 es2->apb_log_dentry = debugfs_create_file("apb_log", 0444,
1146 gb_debugfs_get(), es2,
1147 &apb_log_fops);
1148 }
1149
usb_log_disable(struct es2_ap_dev * es2)1150 static void usb_log_disable(struct es2_ap_dev *es2)
1151 {
1152 if (IS_ERR_OR_NULL(es2->apb_log_task))
1153 return;
1154
1155 debugfs_remove(es2->apb_log_dentry);
1156 es2->apb_log_dentry = NULL;
1157
1158 kthread_stop(es2->apb_log_task);
1159 es2->apb_log_task = NULL;
1160 }
1161
apb_log_enable_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1162 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1163 size_t count, loff_t *ppos)
1164 {
1165 struct es2_ap_dev *es2 = file_inode(f)->i_private;
1166 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1167 char tmp_buf[3];
1168
1169 sprintf(tmp_buf, "%d\n", enable);
1170 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 2);
1171 }
1172
apb_log_enable_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)1173 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1174 size_t count, loff_t *ppos)
1175 {
1176 int enable;
1177 ssize_t retval;
1178 struct es2_ap_dev *es2 = file_inode(f)->i_private;
1179
1180 retval = kstrtoint_from_user(buf, count, 10, &enable);
1181 if (retval)
1182 return retval;
1183
1184 if (enable)
1185 usb_log_enable(es2);
1186 else
1187 usb_log_disable(es2);
1188
1189 return count;
1190 }
1191
1192 static const struct file_operations apb_log_enable_fops = {
1193 .read = apb_log_enable_read,
1194 .write = apb_log_enable_write,
1195 };
1196
apb_get_cport_count(struct usb_device * udev)1197 static int apb_get_cport_count(struct usb_device *udev)
1198 {
1199 int retval;
1200 __le16 *cport_count;
1201
1202 cport_count = kzalloc_obj(*cport_count);
1203 if (!cport_count)
1204 return -ENOMEM;
1205
1206 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1207 GB_APB_REQUEST_CPORT_COUNT,
1208 USB_DIR_IN | USB_TYPE_VENDOR |
1209 USB_RECIP_INTERFACE, 0, 0, cport_count,
1210 sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
1211 if (retval != sizeof(*cport_count)) {
1212 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1213 retval);
1214
1215 if (retval >= 0)
1216 retval = -EIO;
1217
1218 goto out;
1219 }
1220
1221 retval = le16_to_cpu(*cport_count);
1222
1223 /* We need to fit a CPort ID in one byte of a message header */
1224 if (retval > U8_MAX) {
1225 retval = U8_MAX;
1226 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1227 }
1228
1229 out:
1230 kfree(cport_count);
1231 return retval;
1232 }
1233
1234 /*
1235 * The ES2 USB Bridge device has 15 endpoints
1236 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1237 * 7 Bulk IN - CPort data in
1238 * 7 Bulk OUT - CPort data out
1239 */
ap_probe(struct usb_interface * interface,const struct usb_device_id * id)1240 static int ap_probe(struct usb_interface *interface,
1241 const struct usb_device_id *id)
1242 {
1243 struct es2_ap_dev *es2;
1244 struct gb_host_device *hd;
1245 struct usb_device *udev;
1246 struct usb_host_interface *iface_desc;
1247 struct usb_endpoint_descriptor *endpoint;
1248 __u8 ep_addr;
1249 int retval;
1250 int i;
1251 int num_cports;
1252 bool bulk_out_found = false;
1253 bool bulk_in_found = false;
1254 bool arpc_in_found = false;
1255
1256 udev = interface_to_usbdev(interface);
1257
1258 num_cports = apb_get_cport_count(udev);
1259 if (num_cports < 0) {
1260 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1261 num_cports);
1262 return num_cports;
1263 }
1264
1265 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1266 num_cports);
1267 if (IS_ERR(hd))
1268 return PTR_ERR(hd);
1269
1270 es2 = hd_to_es2(hd);
1271 es2->hd = hd;
1272 es2->usb_intf = interface;
1273 es2->usb_dev = udev;
1274 spin_lock_init(&es2->cport_out_urb_lock);
1275 INIT_KFIFO(es2->apb_log_fifo);
1276 usb_set_intfdata(interface, es2);
1277
1278 /*
1279 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1280 * dynamically.
1281 */
1282 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1283 if (retval)
1284 goto error;
1285 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1286 if (retval)
1287 goto error;
1288
1289 /* find all bulk endpoints */
1290 iface_desc = interface->cur_altsetting;
1291 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1292 endpoint = &iface_desc->endpoint[i].desc;
1293 ep_addr = endpoint->bEndpointAddress;
1294
1295 if (usb_endpoint_is_bulk_in(endpoint)) {
1296 if (!bulk_in_found) {
1297 es2->cport_in.endpoint = ep_addr;
1298 bulk_in_found = true;
1299 } else if (!arpc_in_found) {
1300 es2->arpc_endpoint_in = ep_addr;
1301 arpc_in_found = true;
1302 } else {
1303 dev_warn(&udev->dev,
1304 "Unused bulk IN endpoint found: 0x%02x\n",
1305 ep_addr);
1306 }
1307 continue;
1308 }
1309 if (usb_endpoint_is_bulk_out(endpoint)) {
1310 if (!bulk_out_found) {
1311 es2->cport_out_endpoint = ep_addr;
1312 bulk_out_found = true;
1313 } else {
1314 dev_warn(&udev->dev,
1315 "Unused bulk OUT endpoint found: 0x%02x\n",
1316 ep_addr);
1317 }
1318 continue;
1319 }
1320 dev_warn(&udev->dev,
1321 "Unknown endpoint type found, address 0x%02x\n",
1322 ep_addr);
1323 }
1324 if (!bulk_in_found || !arpc_in_found || !bulk_out_found) {
1325 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1326 retval = -ENODEV;
1327 goto error;
1328 }
1329
1330 /* Allocate buffers for our cport in messages */
1331 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1332 struct urb *urb;
1333 u8 *buffer;
1334
1335 urb = usb_alloc_urb(0, GFP_KERNEL);
1336 if (!urb) {
1337 retval = -ENOMEM;
1338 goto error;
1339 }
1340 es2->cport_in.urb[i] = urb;
1341
1342 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1343 if (!buffer) {
1344 retval = -ENOMEM;
1345 goto error;
1346 }
1347
1348 usb_fill_bulk_urb(urb, udev,
1349 usb_rcvbulkpipe(udev, es2->cport_in.endpoint),
1350 buffer, ES2_GBUF_MSG_SIZE_MAX,
1351 cport_in_callback, hd);
1352
1353 es2->cport_in.buffer[i] = buffer;
1354 }
1355
1356 /* Allocate buffers for ARPC in messages */
1357 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1358 struct urb *urb;
1359 u8 *buffer;
1360
1361 urb = usb_alloc_urb(0, GFP_KERNEL);
1362 if (!urb) {
1363 retval = -ENOMEM;
1364 goto error;
1365 }
1366 es2->arpc_urb[i] = urb;
1367
1368 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1369 if (!buffer) {
1370 retval = -ENOMEM;
1371 goto error;
1372 }
1373
1374 usb_fill_bulk_urb(urb, udev,
1375 usb_rcvbulkpipe(udev,
1376 es2->arpc_endpoint_in),
1377 buffer, ARPC_IN_SIZE_MAX,
1378 arpc_in_callback, es2);
1379
1380 es2->arpc_buffer[i] = buffer;
1381 }
1382
1383 /* Allocate urbs for our CPort OUT messages */
1384 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1385 struct urb *urb;
1386
1387 urb = usb_alloc_urb(0, GFP_KERNEL);
1388 if (!urb) {
1389 retval = -ENOMEM;
1390 goto error;
1391 }
1392
1393 es2->cport_out_urb[i] = urb;
1394 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1395 }
1396
1397 /* XXX We will need to rename this per APB */
1398 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1399 0644,
1400 gb_debugfs_get(), es2,
1401 &apb_log_enable_fops);
1402
1403 INIT_LIST_HEAD(&es2->arpcs);
1404 spin_lock_init(&es2->arpc_lock);
1405
1406 retval = es2_arpc_in_enable(es2);
1407 if (retval)
1408 goto error;
1409
1410 retval = gb_hd_add(hd);
1411 if (retval)
1412 goto err_disable_arpc_in;
1413
1414 retval = es2_cport_in_enable(es2, &es2->cport_in);
1415 if (retval)
1416 goto err_hd_del;
1417
1418 return 0;
1419
1420 err_hd_del:
1421 gb_hd_del(hd);
1422 err_disable_arpc_in:
1423 es2_arpc_in_disable(es2);
1424 error:
1425 es2_destroy(es2);
1426
1427 return retval;
1428 }
1429
ap_disconnect(struct usb_interface * interface)1430 static void ap_disconnect(struct usb_interface *interface)
1431 {
1432 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1433
1434 gb_hd_del(es2->hd);
1435
1436 es2_cport_in_disable(es2, &es2->cport_in);
1437 es2_arpc_in_disable(es2);
1438
1439 es2_destroy(es2);
1440 }
1441
1442 static struct usb_driver es2_ap_driver = {
1443 .name = "es2_ap_driver",
1444 .probe = ap_probe,
1445 .disconnect = ap_disconnect,
1446 .id_table = id_table,
1447 .soft_unbind = 1,
1448 };
1449
1450 module_usb_driver(es2_ap_driver);
1451
1452 MODULE_DESCRIPTION("Greybus AP USB driver for ES2 controller chips");
1453 MODULE_LICENSE("GPL v2");
1454 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
1455