blob: 1df6ab5d339dcb026055af18184c49da1255733d [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08002/*
3 * Greybus "AP" USB driver for "ES2" controller chips
4 *
Alex Elder142f8dd2015-03-26 21:25:06 -05005 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08007 */
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01008#include <linux/kthread.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08009#include <linux/sizes.h>
10#include <linux/usb.h>
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010011#include <linux/kfifo.h>
12#include <linux/debugfs.h>
Alexandre Bailonc14118a2016-07-07 07:41:00 -050013#include <linux/list.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020014#include <linux/greybus.h>
Johan Hovold491e60d2015-04-07 11:27:20 +020015#include <asm/unaligned.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080016
Johan Hovold7d0e76d2016-08-10 12:58:41 +020017#include "arpc.h"
Greg Kroah-Hartmanb81beec2019-08-25 07:54:29 +020018#include "greybus_trace.h"
Johan Hovold05061502016-05-11 10:18:01 +020019
Johan Hovold54f34e12016-08-03 14:09:34 +020020
Johan Hovold60793c92016-08-03 14:09:35 +020021/* Default timeout for USB vendor requests. */
22#define ES2_USB_CTRL_TIMEOUT 500
23
Johan Hovold54f34e12016-08-03 14:09:34 +020024/* Default timeout for ARPC CPort requests */
25#define ES2_ARPC_CPORT_TIMEOUT 500
26
Johan Hovold05061502016-05-11 10:18:01 +020027/* Fixed CPort numbers */
28#define ES2_CPORT_CDSI0 16
29#define ES2_CPORT_CDSI1 17
30
Alex Elder4b1d8202015-10-27 22:18:37 -050031/* Memory sizes for the buffers sent to/from the ES2 controller */
32#define ES2_GBUF_MSG_SIZE_MAX 2048
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080033
Alexandre Bailon9d9d3772016-07-07 07:41:00 -050034/* Memory sizes for the ARPC buffers */
Alexandre Bailonc14118a2016-07-07 07:41:00 -050035#define ARPC_OUT_SIZE_MAX U16_MAX
Alexandre Bailon9d9d3772016-07-07 07:41:00 -050036#define ARPC_IN_SIZE_MAX 128
37
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080038static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman8f0a6542015-10-22 16:40:41 -070039 { USB_DEVICE(0x18d1, 0x1eaf) },
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080040 { },
41};
42MODULE_DEVICE_TABLE(usb, id_table);
43
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010044#define APB1_LOG_SIZE SZ_16K
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010045
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080046/*
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 */
Greg Kroah-Hartmanfc994f02016-08-17 13:21:51 +020056#define NUM_CPORT_OUT_URB 8
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080057
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020058/*
Alexandre Bailon9d9d3772016-07-07 07:41:00 -050059 * Number of ARPC in urbs in flight at any point in time.
60 */
61#define NUM_ARPC_IN_URB 2
62
63/*
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020064 * @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 */
Alex Elder4b1d8202015-10-27 22:18:37 -050068struct es2_cport_in {
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020069 __u8 endpoint;
70 struct urb *urb[NUM_CPORT_IN_URB];
71 u8 *buffer[NUM_CPORT_IN_URB];
72};
73
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080074/**
Alex Elder4b1d8202015-10-27 22:18:37 -050075 * es2_ap_dev - ES2 USB Bridge to AP structure
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080076 * @usb_dev: pointer to the USB device we are.
77 * @usb_intf: pointer to the USB interface we are bound to.
Johan Hovold25376362015-11-03 18:03:23 +010078 * @hd: pointer to our gb_host_device structure
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020079
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020080 * @cport_in: endpoint, urbs and buffer for cport in messages
Greg Kroah-Hartman403074b2016-08-17 16:27:49 +020081 * @cport_out_endpoint: endpoint for for cport out messages
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080082 * @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.
Johan Hovold3e136cc2015-07-01 12:37:21 +020085 * @cport_out_urb_cancelled: array of flags indicating whether the
86 * corresponding @cport_out_urb is being cancelled
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080087 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
Alex Elder1482b3e2015-10-27 22:18:38 -050088 *
Alex Elder3be0e172015-10-27 22:18:41 -050089 * @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
Alexandre Bailon9d9d3772016-07-07 07:41:00 -050093 * @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
Alexandre Bailonc14118a2016-07-07 07:41:00 -050096 * @arpc_id_cycle: gives an unique id to ARPC
97 * @arpc_lock: locks ARPC list
98 * @arpcs: list of in progress ARPCs
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080099 */
Alex Elder4b1d8202015-10-27 22:18:37 -0500100struct es2_ap_dev {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800101 struct usb_device *usb_dev;
102 struct usb_interface *usb_intf;
Johan Hovold25376362015-11-03 18:03:23 +0100103 struct gb_host_device *hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800104
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +0200105 struct es2_cport_in cport_in;
Greg Kroah-Hartman403074b2016-08-17 16:27:49 +0200106 __u8 cport_out_endpoint;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800107 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
108 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
Johan Hovold3e136cc2015-07-01 12:37:21 +0200109 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800110 spinlock_t cport_out_urb_lock;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200111
Johan Hovold045d3562016-05-11 10:18:04 +0200112 bool cdsi1_in_use;
113
Alex Elder3be0e172015-10-27 22:18:41 -0500114 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);
Alexandre Bailon9d9d3772016-07-07 07:41:00 -0500118
119 __u8 arpc_endpoint_in;
120 struct urb *arpc_urb[NUM_ARPC_IN_URB];
121 u8 *arpc_buffer[NUM_ARPC_IN_URB];
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500122
123 int arpc_id_cycle;
124 spinlock_t arpc_lock;
125 struct list_head arpcs;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200126};
127
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500128struct 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
Johan Hovold25376362015-11-03 18:03:23 +0100136static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800137{
Alex Elder4b1d8202015-10-27 22:18:37 -0500138 return (struct es2_ap_dev *)&hd->hd_priv;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800139}
140
141static void cport_out_callback(struct urb *urb);
Alex Elder4b1d8202015-10-27 22:18:37 -0500142static void usb_log_enable(struct es2_ap_dev *es2);
143static void usb_log_disable(struct es2_ap_dev *es2);
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500144static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
145 size_t size, int *result, unsigned int timeout);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800146
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800147static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200148{
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200149 struct usb_device *udev = es2->usb_dev;
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800150 u8 *data;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200151 int retval;
152
sayli karnik9edae492016-10-16 14:19:48 +0530153 data = kmemdup(req, size, GFP_KERNEL);
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800154 if (!data)
Laurent Pinchartccb58032015-12-22 03:00:37 +0200155 return -ENOMEM;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200156
157 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800158 cmd,
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200159 USB_DIR_OUT | USB_TYPE_VENDOR |
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800160 USB_RECIP_INTERFACE,
Johan Hovold60793c92016-08-03 14:09:35 +0200161 0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
Laurent Pinchartccb58032015-12-22 03:00:37 +0200162 if (retval < 0)
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800163 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
164 else
165 retval = 0;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200166
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800167 kfree(data);
Laurent Pinchartccb58032015-12-22 03:00:37 +0200168 return retval;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200169}
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800170
171static 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
179static 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
217static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100218 bool async)
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800219{
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}
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200227
Johan Hovold0ce68ce42015-11-04 18:55:14 +0100228static int es2_cport_in_enable(struct es2_ap_dev *es2,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100229 struct es2_cport_in *cport_in)
Johan Hovold0ce68ce42015-11-04 18:55:14 +0100230{
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,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100241 "failed to submit in-urb: %d\n", ret);
Johan Hovold0ce68ce42015-11-04 18:55:14 +0100242 goto err_kill_urbs;
243 }
244 }
245
246 return 0;
247
248err_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
Johan Hovoldf6624ca2015-11-04 18:55:16 +0100257static void es2_cport_in_disable(struct es2_ap_dev *es2,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100258 struct es2_cport_in *cport_in)
Johan Hovoldf6624ca2015-11-04 18:55:16 +0100259{
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
Alexandre Bailon9d9d3772016-07-07 07:41:00 -0500269static 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
288err_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
297static 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
Alex Elder4b1d8202015-10-27 22:18:37 -0500308static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800309{
310 struct urb *urb = NULL;
311 unsigned long flags;
312 int i;
313
Alex Elder4b1d8202015-10-27 22:18:37 -0500314 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800315
316 /* Look in our pool of allocated urbs first, as that's the "fastest" */
317 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Ioannis Valasakis2eece0a2018-11-09 13:54:57 +0000318 if (!es2->cport_out_urb_busy[i] &&
319 !es2->cport_out_urb_cancelled[i]) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500320 es2->cport_out_urb_busy[i] = true;
321 urb = es2->cport_out_urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800322 break;
323 }
324 }
Alex Elder4b1d8202015-10-27 22:18:37 -0500325 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800326 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 */
Johan Hovolded972e32015-12-15 15:51:48 +0100333 dev_dbg(&es2->usb_dev->dev,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800334 "No free CPort OUT urbs, having to dynamically allocate one!\n");
335 return usb_alloc_urb(0, gfp_mask);
336}
337
Alex Elder4b1d8202015-10-27 22:18:37 -0500338static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800339{
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 */
Alex Elder4b1d8202015-10-27 22:18:37 -0500346 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800347 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500348 if (urb == es2->cport_out_urb[i]) {
349 es2->cport_out_urb_busy[i] = false;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800350 urb = NULL;
351 break;
352 }
353 }
Alex Elder4b1d8202015-10-27 22:18:37 -0500354 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800355
356 /* If urb is not NULL, then we need to free this urb */
357 usb_free_urb(urb);
358}
359
360/*
Alex Elderd29b3d62015-06-13 11:02:08 -0500361 * We (ab)use the operation-message header pad bytes to transfer the
362 * cport id in order to minimise overhead.
363 */
364static void
365gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
366{
Alex Elder4bc13892015-06-13 11:02:11 -0500367 header->pad[0] = cport_id;
Alex Elderd29b3d62015-06-13 11:02:08 -0500368}
369
370/* Clear the pad bytes used for the CPort id */
371static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
372{
Alex Elder4bc13892015-06-13 11:02:11 -0500373 header->pad[0] = 0;
Alex Elderd29b3d62015-06-13 11:02:08 -0500374}
375
376/* Extract the CPort id packed into the header, and clear it */
377static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
378{
Alex Elder4bc13892015-06-13 11:02:11 -0500379 u16 cport_id = header->pad[0];
Alex Elderd29b3d62015-06-13 11:02:08 -0500380
381 gb_message_cport_clear(header);
382
383 return cport_id;
384}
385
386/*
Johan Hovold3e136cc2015-07-01 12:37:21 +0200387 * Returns zero if the message was successfully queued, or a negative errno
388 * otherwise.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800389 */
Johan Hovold25376362015-11-03 18:03:23 +0100390static int message_send(struct gb_host_device *hd, u16 cport_id,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200391 struct gb_message *message, gfp_t gfp_mask)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800392{
Alex Elder4b1d8202015-10-27 22:18:37 -0500393 struct es2_ap_dev *es2 = hd_to_es2(hd);
394 struct usb_device *udev = es2->usb_dev;
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200395 size_t buffer_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800396 int retval;
397 struct urb *urb;
Johan Hovold3e136cc2015-07-01 12:37:21 +0200398 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800399
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800400 /*
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 */
Fabien Parent144670c2015-09-02 15:50:35 +0200405 if (!cport_id_valid(hd, cport_id)) {
Johan Hovold100e9002015-12-07 15:05:38 +0100406 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200407 return -EINVAL;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800408 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800409
410 /* Find a free urb */
Alex Elder4b1d8202015-10-27 22:18:37 -0500411 urb = next_free_urb(es2, gfp_mask);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800412 if (!urb)
Johan Hovold3e136cc2015-07-01 12:37:21 +0200413 return -ENOMEM;
414
Alex Elder4b1d8202015-10-27 22:18:37 -0500415 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200416 message->hcpriv = urb;
Alex Elder4b1d8202015-10-27 22:18:37 -0500417 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800418
Alex Elderd29b3d62015-06-13 11:02:08 -0500419 /* Pack the cport id into the message header */
420 gb_message_cport_pack(message->header, cport_id);
Johan Hovold491e60d2015-04-07 11:27:20 +0200421
Alex Elder821c6202015-06-13 11:02:07 -0500422 buffer_size = sizeof(*message->header) + message->payload_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800423
424 usb_fill_bulk_urb(urb, udev,
Alexandre Bailon606addd2015-06-15 18:08:13 +0200425 usb_sndbulkpipe(udev,
Greg Kroah-Hartman403074b2016-08-17 16:27:49 +0200426 es2->cport_out_endpoint),
Alex Elder821c6202015-06-13 11:02:07 -0500427 message->buffer, buffer_size,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200428 cport_out_callback, message);
Alexandre Bailon977e2092015-08-31 09:00:16 +0200429 urb->transfer_flags |= URB_ZERO_PACKET;
Alex Elder495787a2016-06-03 15:55:38 -0500430
431 trace_gb_message_submit(message);
432
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800433 retval = usb_submit_urb(urb, gfp_mask);
434 if (retval) {
Viresh Kumar05e30952016-06-23 23:22:15 +0530435 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200436
Alex Elder4b1d8202015-10-27 22:18:37 -0500437 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200438 message->hcpriv = NULL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500439 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200440
Alex Elder4b1d8202015-10-27 22:18:37 -0500441 free_urb(es2, urb);
Alex Elderd29b3d62015-06-13 11:02:08 -0500442 gb_message_cport_clear(message->header);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200443
444 return retval;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800445 }
446
Johan Hovold3e136cc2015-07-01 12:37:21 +0200447 return 0;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800448}
449
450/*
Johan Hovold3e136cc2015-07-01 12:37:21 +0200451 * Can not be called in atomic context.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800452 */
Johan Hovold3e136cc2015-07-01 12:37:21 +0200453static void message_cancel(struct gb_message *message)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800454{
Johan Hovold25376362015-11-03 18:03:23 +0100455 struct gb_host_device *hd = message->operation->connection->hd;
Alex Elder4b1d8202015-10-27 22:18:37 -0500456 struct es2_ap_dev *es2 = hd_to_es2(hd);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200457 struct urb *urb;
458 int i;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800459
Johan Hovold3e136cc2015-07-01 12:37:21 +0200460 might_sleep();
461
Johan Hovold07af9342016-06-27 20:07:11 +0200462 spin_lock_irq(&es2->cport_out_urb_lock);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200463 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) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500470 if (urb == es2->cport_out_urb[i]) {
471 es2->cport_out_urb_cancelled[i] = true;
Johan Hovold3e136cc2015-07-01 12:37:21 +0200472 break;
473 }
474 }
Johan Hovold07af9342016-06-27 20:07:11 +0200475 spin_unlock_irq(&es2->cport_out_urb_lock);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200476
477 usb_kill_urb(urb);
478
479 if (i < NUM_CPORT_OUT_URB) {
Johan Hovold07af9342016-06-27 20:07:11 +0200480 spin_lock_irq(&es2->cport_out_urb_lock);
Alex Elder4b1d8202015-10-27 22:18:37 -0500481 es2->cport_out_urb_cancelled[i] = false;
Johan Hovold07af9342016-06-27 20:07:11 +0200482 spin_unlock_irq(&es2->cport_out_urb_lock);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200483 }
484
485 usb_free_urb(urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800486}
487
Johan Hovold045d3562016-05-11 10:18:04 +0200488static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100489 unsigned long flags)
Johan Hovold045d3562016-05-11 10:18:04 +0200490{
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 &&
Cristian Sicilia8478c352018-11-25 17:58:15 +0100503 flags & GB_CONNECTION_FLAG_CDSI1) {
Johan Hovold045d3562016-05-11 10:18:04 +0200504 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;
517 } else if (cport_id < hd->num_cports) {
518 ida_start = cport_id;
519 ida_end = cport_id + 1;
520 } else {
521 dev_err(&hd->dev, "cport %d not available\n", cport_id);
522 return -EINVAL;
523 }
524
525 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
526}
527
528static 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_simple_remove(&hd->cport_id_map, cport_id);
539}
540
Johan Hovold74ec7592016-06-22 11:42:05 +0200541static 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;
Bryan O'Donoghue6a1d2952016-07-18 14:59:36 +0100547 u32 connection_flags;
Johan Hovold74ec7592016-06-22 11:42:05 +0200548 int ret;
549
550 req = kzalloc(sizeof(*req), GFP_KERNEL);
551 if (!req)
552 return -ENOMEM;
553
Bryan O'Donoghue6a1d2952016-07-18 14:59:36 +0100554 connection_flags = 0;
Johan Hovold74ec7592016-06-22 11:42:05 +0200555 if (flags & GB_CONNECTION_FLAG_CONTROL)
Bryan O'Donoghue6a1d2952016-07-18 14:59:36 +0100556 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
Johan Hovold74ec7592016-06-22 11:42:05 +0200557 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
Bryan O'Donoghue6a1d2952016-07-18 14:59:36 +0100558 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
559
560 req->flags = cpu_to_le32(connection_flags);
Johan Hovold74ec7592016-06-22 11:42:05 +0200561
562 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100563 cport_id, connection_flags);
Johan Hovold74ec7592016-06-22 11:42:05 +0200564
565 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Cristian Sicilia8478c352018-11-25 17:58:15 +0100566 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);
Johan Hovold74ec7592016-06-22 11:42:05 +0200570 if (ret != sizeof(*req)) {
571 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100572 cport_id);
Johan Hovold74ec7592016-06-22 11:42:05 +0200573 if (ret >= 0)
574 ret = -EIO;
575
576 goto out;
577 }
578
579 ret = 0;
580out:
581 kfree(req);
582
583 return ret;
584}
585
Johan Hovolda9dc1cf2016-08-10 12:58:42 +0200586static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id)
587{
588 struct es2_ap_dev *es2 = hd_to_es2(hd);
589 struct device *dev = &es2->usb_dev->dev;
590 struct arpc_cport_connected_req req;
591 int ret;
592
593 req.cport_id = cpu_to_le16(cport_id);
594 ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req),
595 NULL, ES2_ARPC_CPORT_TIMEOUT);
596 if (ret) {
597 dev_err(dev, "failed to set connected state for cport %u: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100598 cport_id, ret);
Johan Hovolda9dc1cf2016-08-10 12:58:42 +0200599 return ret;
600 }
601
602 return 0;
603}
604
Johan Hovoldd2dee942016-08-26 12:55:47 +0200605static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id)
606{
607 struct es2_ap_dev *es2 = hd_to_es2(hd);
608 struct device *dev = &es2->usb_dev->dev;
609 struct arpc_cport_flush_req req;
610 int ret;
611
612 req.cport_id = cpu_to_le16(cport_id);
613 ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req),
614 NULL, ES2_ARPC_CPORT_TIMEOUT);
615 if (ret) {
616 dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret);
617 return ret;
618 }
619
620 return 0;
621}
622
Johan Hovold77e52b32016-08-26 12:55:48 +0200623static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100624 u8 phase, unsigned int timeout)
Johan Hovold77e52b32016-08-26 12:55:48 +0200625{
626 struct es2_ap_dev *es2 = hd_to_es2(hd);
627 struct device *dev = &es2->usb_dev->dev;
628 struct arpc_cport_shutdown_req req;
629 int result;
630 int ret;
631
632 if (timeout > U16_MAX)
633 return -EINVAL;
634
635 req.cport_id = cpu_to_le16(cport_id);
636 req.timeout = cpu_to_le16(timeout);
637 req.phase = phase;
638 ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req),
639 &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
640 if (ret) {
641 dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100642 cport_id, ret, result);
Johan Hovold77e52b32016-08-26 12:55:48 +0200643 return ret;
644 }
645
646 return 0;
647}
648
Johan Hovold0e7cd0d2016-08-10 12:58:43 +0200649static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100650 size_t peer_space, unsigned int timeout)
Johan Hovold0e7cd0d2016-08-10 12:58:43 +0200651{
652 struct es2_ap_dev *es2 = hd_to_es2(hd);
653 struct device *dev = &es2->usb_dev->dev;
654 struct arpc_cport_quiesce_req req;
655 int result;
656 int ret;
657
658 if (peer_space > U16_MAX)
659 return -EINVAL;
660
661 if (timeout > U16_MAX)
662 return -EINVAL;
663
664 req.cport_id = cpu_to_le16(cport_id);
665 req.peer_space = cpu_to_le16(peer_space);
666 req.timeout = cpu_to_le16(timeout);
667 ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req),
668 &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
669 if (ret) {
670 dev_err(dev, "failed to quiesce cport %u: %d (%d)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100671 cport_id, ret, result);
Johan Hovold0e7cd0d2016-08-10 12:58:43 +0200672 return ret;
673 }
674
675 return 0;
676}
677
Johan Hovold6471c002016-08-10 12:58:44 +0200678static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id)
679{
680 struct es2_ap_dev *es2 = hd_to_es2(hd);
681 struct device *dev = &es2->usb_dev->dev;
682 struct arpc_cport_clear_req req;
683 int ret;
684
685 req.cport_id = cpu_to_le16(cport_id);
686 ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req),
687 NULL, ES2_ARPC_CPORT_TIMEOUT);
688 if (ret) {
689 dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret);
690 return ret;
691 }
692
693 return 0;
694}
695
Johan Hovold25376362015-11-03 18:03:23 +0100696static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100697{
698 int retval;
Alex Elder4b1d8202015-10-27 22:18:37 -0500699 struct es2_ap_dev *es2 = hd_to_es2(hd);
700 struct usb_device *udev = es2->usb_dev;
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100701
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100702 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800703 GB_APB_REQUEST_LATENCY_TAG_EN,
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100704 USB_DIR_OUT | USB_TYPE_VENDOR |
705 USB_RECIP_INTERFACE, cport_id, 0, NULL,
Johan Hovold60793c92016-08-03 14:09:35 +0200706 0, ES2_USB_CTRL_TIMEOUT);
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100707
708 if (retval < 0)
709 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
710 cport_id);
711 return retval;
712}
713
Johan Hovold25376362015-11-03 18:03:23 +0100714static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100715{
716 int retval;
Alex Elder4b1d8202015-10-27 22:18:37 -0500717 struct es2_ap_dev *es2 = hd_to_es2(hd);
718 struct usb_device *udev = es2->usb_dev;
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100719
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100720 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800721 GB_APB_REQUEST_LATENCY_TAG_DIS,
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100722 USB_DIR_OUT | USB_TYPE_VENDOR |
723 USB_RECIP_INTERFACE, cport_id, 0, NULL,
Johan Hovold60793c92016-08-03 14:09:35 +0200724 0, ES2_USB_CTRL_TIMEOUT);
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100725
726 if (retval < 0)
727 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
728 cport_id);
729 return retval;
730}
731
Johan Hovolda8cc0202015-11-03 18:03:24 +0100732static struct gb_hd_driver es2_driver = {
Bryan O'Donoghuec8a657b2016-05-15 19:37:48 +0100733 .hd_priv_size = sizeof(struct es2_ap_dev),
734 .message_send = message_send,
735 .message_cancel = message_cancel,
736 .cport_allocate = es2_cport_allocate,
737 .cport_release = es2_cport_release,
Johan Hovold74ec7592016-06-22 11:42:05 +0200738 .cport_enable = cport_enable,
Johan Hovolda9dc1cf2016-08-10 12:58:42 +0200739 .cport_connected = es2_cport_connected,
Johan Hovoldd2dee942016-08-26 12:55:47 +0200740 .cport_flush = es2_cport_flush,
Johan Hovold77e52b32016-08-26 12:55:48 +0200741 .cport_shutdown = es2_cport_shutdown,
Johan Hovold0e7cd0d2016-08-10 12:58:43 +0200742 .cport_quiesce = es2_cport_quiesce,
Johan Hovold6471c002016-08-10 12:58:44 +0200743 .cport_clear = es2_cport_clear,
Bryan O'Donoghuec8a657b2016-05-15 19:37:48 +0100744 .latency_tag_enable = latency_tag_enable,
745 .latency_tag_disable = latency_tag_disable,
746 .output = output,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800747};
748
749/* Common function to report consistent warnings based on URB status */
750static int check_urb_status(struct urb *urb)
751{
752 struct device *dev = &urb->dev->dev;
753 int status = urb->status;
754
755 switch (status) {
756 case 0:
757 return 0;
758
759 case -EOVERFLOW:
760 dev_err(dev, "%s: overflow actual length is %d\n",
761 __func__, urb->actual_length);
Gustavo A. R. Silva37b8b732020-07-27 13:32:58 -0500762 fallthrough;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800763 case -ECONNRESET:
764 case -ENOENT:
765 case -ESHUTDOWN:
766 case -EILSEQ:
767 case -EPROTO:
768 /* device is gone, stop sending */
769 return status;
770 }
771 dev_err(dev, "%s: unknown status %d\n", __func__, status);
772
773 return -EAGAIN;
774}
775
Johan Hovold57bc17f2015-11-04 18:55:20 +0100776static void es2_destroy(struct es2_ap_dev *es2)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800777{
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800778 struct usb_device *udev;
Greg Kroah-Hartman1521eb62016-08-17 16:44:11 +0200779 struct urb *urb;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800780 int i;
781
Johan Hovold74cd6502015-11-04 18:55:18 +0100782 debugfs_remove(es2->apb_log_enable_dentry);
Alex Elder4b1d8202015-10-27 22:18:37 -0500783 usb_log_disable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100784
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800785 /* Tear down everything! */
786 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Greg Kroah-Hartman1521eb62016-08-17 16:44:11 +0200787 urb = es2->cport_out_urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800788 usb_kill_urb(urb);
789 usb_free_urb(urb);
Alex Elder4b1d8202015-10-27 22:18:37 -0500790 es2->cport_out_urb[i] = NULL;
791 es2->cport_out_urb_busy[i] = false; /* just to be anal */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800792 }
793
Alexandre Bailon9d9d3772016-07-07 07:41:00 -0500794 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
Greg Kroah-Hartman1521eb62016-08-17 16:44:11 +0200795 usb_free_urb(es2->arpc_urb[i]);
Alexandre Bailon9d9d3772016-07-07 07:41:00 -0500796 kfree(es2->arpc_buffer[i]);
797 es2->arpc_buffer[i] = NULL;
798 }
799
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +0200800 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
801 usb_free_urb(es2->cport_in.urb[i]);
802 kfree(es2->cport_in.buffer[i]);
803 es2->cport_in.buffer[i] = NULL;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800804 }
805
Vaibhav Agarwal52033fd2016-05-27 10:49:24 +0530806 /* release reserved CDSI0 and CDSI1 cports */
807 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
808 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
809
Alex Elder4b1d8202015-10-27 22:18:37 -0500810 udev = es2->usb_dev;
Johan Hovoldc1700472015-11-04 18:55:22 +0100811 gb_hd_put(es2->hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800812
813 usb_put_dev(udev);
814}
815
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800816static void cport_in_callback(struct urb *urb)
817{
Johan Hovold25376362015-11-03 18:03:23 +0100818 struct gb_host_device *hd = urb->context;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800819 struct device *dev = &urb->dev->dev;
Johan Hovold491e60d2015-04-07 11:27:20 +0200820 struct gb_operation_msg_hdr *header;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800821 int status = check_urb_status(urb);
822 int retval;
823 u16 cport_id;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800824
825 if (status) {
826 if ((status == -EAGAIN) || (status == -EPROTO))
827 goto exit;
Viresh Kumar62de6e02016-04-21 11:31:15 +0530828
829 /* The urb is being unlinked */
830 if (status == -ENOENT || status == -ESHUTDOWN)
831 return;
832
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800833 dev_err(dev, "urb cport in error %d (dropped)\n", status);
834 return;
835 }
836
Johan Hovold491e60d2015-04-07 11:27:20 +0200837 if (urb->actual_length < sizeof(*header)) {
Johan Hovold305a0312015-10-13 19:10:20 +0200838 dev_err(dev, "short message received\n");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800839 goto exit;
840 }
841
Alex Elderd29b3d62015-06-13 11:02:08 -0500842 /* Extract the CPort id, which is packed in the message header */
Johan Hovold491e60d2015-04-07 11:27:20 +0200843 header = urb->transfer_buffer;
Alex Elderd29b3d62015-06-13 11:02:08 -0500844 cport_id = gb_message_cport_unpack(header);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800845
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700846 if (cport_id_valid(hd, cport_id)) {
Alex Elder821c6202015-06-13 11:02:07 -0500847 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100848 urb->actual_length);
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700849 } else {
Johan Hovold100e9002015-12-07 15:05:38 +0100850 dev_err(dev, "invalid cport id %u received\n", cport_id);
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700851 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800852exit:
853 /* put our urb back in the request pool */
854 retval = usb_submit_urb(urb, GFP_ATOMIC);
855 if (retval)
Johan Hovold305a0312015-10-13 19:10:20 +0200856 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800857}
858
859static void cport_out_callback(struct urb *urb)
860{
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200861 struct gb_message *message = urb->context;
Johan Hovold25376362015-11-03 18:03:23 +0100862 struct gb_host_device *hd = message->operation->connection->hd;
Alex Elder4b1d8202015-10-27 22:18:37 -0500863 struct es2_ap_dev *es2 = hd_to_es2(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800864 int status = check_urb_status(urb);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200865 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800866
Alex Elderd29b3d62015-06-13 11:02:08 -0500867 gb_message_cport_clear(message->header);
Johan Hovold491e60d2015-04-07 11:27:20 +0200868
Alex Elder4b1d8202015-10-27 22:18:37 -0500869 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold58c85122015-09-26 14:37:59 -0700870 message->hcpriv = NULL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500871 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Johan Hovold58c85122015-09-26 14:37:59 -0700872
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800873 /*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200874 * Tell the submitter that the message send (attempt) is
875 * complete, and report the status.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800876 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200877 greybus_message_sent(hd, message, status);
878
Alex Elder4b1d8202015-10-27 22:18:37 -0500879 free_urb(es2, urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800880}
881
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500882static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
883{
884 struct arpc *rpc;
885
886 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
887 return NULL;
888
889 rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
890 if (!rpc)
891 return NULL;
892
893 INIT_LIST_HEAD(&rpc->list);
894 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
895 if (!rpc->req)
896 goto err_free_rpc;
897
898 rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
Johan Hovoldfcba5d02016-07-27 16:37:20 +0200899 if (!rpc->resp)
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500900 goto err_free_req;
901
902 rpc->req->type = type;
Rui Miguel Silva7fec2bc2016-10-19 13:44:25 +0100903 rpc->req->size = cpu_to_le16(sizeof(*rpc->req) + size);
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500904 memcpy(rpc->req->data, payload, size);
905
906 init_completion(&rpc->response_received);
907
908 return rpc;
909
910err_free_req:
911 kfree(rpc->req);
912err_free_rpc:
913 kfree(rpc);
914
915 return NULL;
916}
917
918static void arpc_free(struct arpc *rpc)
919{
920 kfree(rpc->req);
921 kfree(rpc->resp);
922 kfree(rpc);
923}
924
Johan Hovold165a74a2016-07-14 12:55:00 -0500925static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500926{
927 struct arpc *rpc;
928
929 list_for_each_entry(rpc, &es2->arpcs, list) {
Johan Hovold165a74a2016-07-14 12:55:00 -0500930 if (rpc->req->id == id)
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500931 return rpc;
932 }
933
934 return NULL;
935}
936
937static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
938{
939 rpc->active = true;
Viresh Kumare8180272016-07-14 12:18:00 -0500940 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
Johan Hovoldc0219cb2016-07-27 16:37:22 +0200941 list_add_tail(&rpc->list, &es2->arpcs);
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500942}
943
944static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
945{
946 if (rpc->active) {
947 rpc->active = false;
948 list_del(&rpc->list);
949 }
950}
951
952static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
953{
954 struct usb_device *udev = es2->usb_dev;
955 int retval;
956
957 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Johan Hovold83d474f2016-08-10 12:58:40 +0200958 GB_APB_REQUEST_ARPC_RUN,
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500959 USB_DIR_OUT | USB_TYPE_VENDOR |
960 USB_RECIP_INTERFACE,
961 0, 0,
Viresh Kumare8180272016-07-14 12:18:00 -0500962 rpc->req, le16_to_cpu(rpc->req->size),
Johan Hovold60793c92016-08-03 14:09:35 +0200963 ES2_USB_CTRL_TIMEOUT);
Viresh Kumare8180272016-07-14 12:18:00 -0500964 if (retval != le16_to_cpu(rpc->req->size)) {
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500965 dev_err(&udev->dev,
966 "failed to send ARPC request %d: %d\n",
967 rpc->req->type, retval);
968 if (retval > 0)
969 retval = -EIO;
970 return retval;
971 }
972
973 return 0;
974}
975
976static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
977 size_t size, int *result, unsigned int timeout)
978{
979 struct arpc *rpc;
980 unsigned long flags;
981 int retval;
982
Johan Hovold121bae22016-08-03 14:09:33 +0200983 if (result)
984 *result = 0;
Johan Hovold4ae2d962016-08-03 14:09:32 +0200985
Alexandre Bailonc14118a2016-07-07 07:41:00 -0500986 rpc = arpc_alloc(payload, size, type);
987 if (!rpc)
988 return -ENOMEM;
989
990 spin_lock_irqsave(&es2->arpc_lock, flags);
991 arpc_add(es2, rpc);
992 spin_unlock_irqrestore(&es2->arpc_lock, flags);
993
994 retval = arpc_send(es2, rpc, timeout);
995 if (retval)
996 goto out_arpc_del;
997
998 retval = wait_for_completion_interruptible_timeout(
999 &rpc->response_received,
1000 msecs_to_jiffies(timeout));
1001 if (retval <= 0) {
1002 if (!retval)
1003 retval = -ETIMEDOUT;
1004 goto out_arpc_del;
1005 }
1006
Johan Hovold4ae2d962016-08-03 14:09:32 +02001007 if (rpc->resp->result) {
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001008 retval = -EREMOTEIO;
Johan Hovold121bae22016-08-03 14:09:33 +02001009 if (result)
1010 *result = rpc->resp->result;
Johan Hovold4ae2d962016-08-03 14:09:32 +02001011 } else {
Johan Hovold34873942016-07-13 09:34:00 -05001012 retval = 0;
Johan Hovold4ae2d962016-08-03 14:09:32 +02001013 }
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001014
1015out_arpc_del:
1016 spin_lock_irqsave(&es2->arpc_lock, flags);
1017 arpc_del(es2, rpc);
1018 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1019 arpc_free(rpc);
1020
1021 if (retval < 0 && retval != -EREMOTEIO) {
1022 dev_err(&es2->usb_dev->dev,
1023 "failed to execute ARPC: %d\n", retval);
1024 }
1025
1026 return retval;
1027}
1028
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001029static void arpc_in_callback(struct urb *urb)
1030{
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001031 struct es2_ap_dev *es2 = urb->context;
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001032 struct device *dev = &urb->dev->dev;
1033 int status = check_urb_status(urb);
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001034 struct arpc *rpc;
1035 struct arpc_response_message *resp;
1036 unsigned long flags;
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001037 int retval;
1038
1039 if (status) {
1040 if ((status == -EAGAIN) || (status == -EPROTO))
1041 goto exit;
1042
1043 /* The urb is being unlinked */
1044 if (status == -ENOENT || status == -ESHUTDOWN)
1045 return;
1046
1047 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1048 return;
1049 }
1050
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001051 if (urb->actual_length < sizeof(*resp)) {
1052 dev_err(dev, "short aprc response received\n");
1053 goto exit;
1054 }
1055
1056 resp = urb->transfer_buffer;
1057 spin_lock_irqsave(&es2->arpc_lock, flags);
Johan Hovold165a74a2016-07-14 12:55:00 -05001058 rpc = arpc_find(es2, resp->id);
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001059 if (!rpc) {
Johan Hovold178457e2016-07-27 16:37:21 +02001060 dev_err(dev, "invalid arpc response id received: %u\n",
1061 le16_to_cpu(resp->id));
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001062 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1063 goto exit;
1064 }
1065
1066 arpc_del(es2, rpc);
1067 memcpy(rpc->resp, resp, sizeof(*resp));
1068 complete(&rpc->response_received);
1069 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1070
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001071exit:
1072 /* put our urb back in the request pool */
1073 retval = usb_submit_urb(urb, GFP_ATOMIC);
1074 if (retval)
1075 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1076}
1077
Johan Hovoldc15ccab2015-04-07 11:27:12 +02001078#define APB1_LOG_MSG_SIZE 64
Alex Elder3be0e172015-10-27 22:18:41 -05001079static void apb_log_get(struct es2_ap_dev *es2, char *buf)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001080{
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001081 int retval;
1082
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001083 do {
Alex Elder4b1d8202015-10-27 22:18:37 -05001084 retval = usb_control_msg(es2->usb_dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001085 usb_rcvctrlpipe(es2->usb_dev, 0),
1086 GB_APB_REQUEST_LOG,
1087 USB_DIR_IN | USB_TYPE_VENDOR |
1088 USB_RECIP_INTERFACE,
1089 0x00, 0x00,
1090 buf,
1091 APB1_LOG_MSG_SIZE,
1092 ES2_USB_CTRL_TIMEOUT);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001093 if (retval > 0)
Alex Elder3be0e172015-10-27 22:18:41 -05001094 kfifo_in(&es2->apb_log_fifo, buf, retval);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001095 } while (retval > 0);
1096}
1097
Alex Elder3be0e172015-10-27 22:18:41 -05001098static int apb_log_poll(void *data)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001099{
Alex Elder4b1d8202015-10-27 22:18:37 -05001100 struct es2_ap_dev *es2 = data;
Johan Hovoldc15ccab2015-04-07 11:27:12 +02001101 char *buf;
1102
1103 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1104 if (!buf)
1105 return -ENOMEM;
1106
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001107 while (!kthread_should_stop()) {
1108 msleep(1000);
Alex Elder3be0e172015-10-27 22:18:41 -05001109 apb_log_get(es2, buf);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001110 }
Johan Hovoldc15ccab2015-04-07 11:27:12 +02001111
1112 kfree(buf);
1113
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001114 return 0;
1115}
1116
Alex Elder3be0e172015-10-27 22:18:41 -05001117static ssize_t apb_log_read(struct file *f, char __user *buf,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001118 size_t count, loff_t *ppos)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001119{
Al Viro45063092016-12-04 18:24:56 -05001120 struct es2_ap_dev *es2 = file_inode(f)->i_private;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001121 ssize_t ret;
1122 size_t copied;
1123 char *tmp_buf;
1124
1125 if (count > APB1_LOG_SIZE)
1126 count = APB1_LOG_SIZE;
1127
1128 tmp_buf = kmalloc(count, GFP_KERNEL);
1129 if (!tmp_buf)
1130 return -ENOMEM;
1131
Alex Elder3be0e172015-10-27 22:18:41 -05001132 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001133 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1134
1135 kfree(tmp_buf);
1136
1137 return ret;
1138}
1139
Alex Elder3be0e172015-10-27 22:18:41 -05001140static const struct file_operations apb_log_fops = {
1141 .read = apb_log_read,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001142};
1143
Alex Elder4b1d8202015-10-27 22:18:37 -05001144static void usb_log_enable(struct es2_ap_dev *es2)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001145{
Alex Elder3be0e172015-10-27 22:18:41 -05001146 if (!IS_ERR_OR_NULL(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001147 return;
1148
1149 /* get log from APB1 */
Alex Elder3be0e172015-10-27 22:18:41 -05001150 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1151 if (IS_ERR(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001152 return;
Alex Elder3be0e172015-10-27 22:18:41 -05001153 /* XXX We will need to rename this per APB */
Gioh Kim563a8412017-02-09 17:30:12 +01001154 es2->apb_log_dentry = debugfs_create_file("apb_log", 0444,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001155 gb_debugfs_get(), es2,
1156 &apb_log_fops);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001157}
1158
Alex Elder4b1d8202015-10-27 22:18:37 -05001159static void usb_log_disable(struct es2_ap_dev *es2)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001160{
Alex Elder3be0e172015-10-27 22:18:41 -05001161 if (IS_ERR_OR_NULL(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001162 return;
1163
Alex Elder3be0e172015-10-27 22:18:41 -05001164 debugfs_remove(es2->apb_log_dentry);
1165 es2->apb_log_dentry = NULL;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001166
Alex Elder3be0e172015-10-27 22:18:41 -05001167 kthread_stop(es2->apb_log_task);
1168 es2->apb_log_task = NULL;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001169}
1170
Alex Elder3be0e172015-10-27 22:18:41 -05001171static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001172 size_t count, loff_t *ppos)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001173{
Al Viro45063092016-12-04 18:24:56 -05001174 struct es2_ap_dev *es2 = file_inode(f)->i_private;
Alex Elder3be0e172015-10-27 22:18:41 -05001175 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
Alex Elder4b1d8202015-10-27 22:18:37 -05001176 char tmp_buf[3];
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001177
1178 sprintf(tmp_buf, "%d\n", enable);
1179 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1180}
1181
Alex Elder3be0e172015-10-27 22:18:41 -05001182static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001183 size_t count, loff_t *ppos)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001184{
1185 int enable;
1186 ssize_t retval;
Al Viro45063092016-12-04 18:24:56 -05001187 struct es2_ap_dev *es2 = file_inode(f)->i_private;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001188
1189 retval = kstrtoint_from_user(buf, count, 10, &enable);
1190 if (retval)
1191 return retval;
1192
1193 if (enable)
Alex Elder4b1d8202015-10-27 22:18:37 -05001194 usb_log_enable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001195 else
Alex Elder4b1d8202015-10-27 22:18:37 -05001196 usb_log_disable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001197
1198 return count;
1199}
1200
Alex Elder3be0e172015-10-27 22:18:41 -05001201static const struct file_operations apb_log_enable_fops = {
1202 .read = apb_log_enable_read,
1203 .write = apb_log_enable_write,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01001204};
1205
Alex Elder3be0e172015-10-27 22:18:41 -05001206static int apb_get_cport_count(struct usb_device *udev)
Fabien Parent24a61122015-09-02 15:50:37 +02001207{
1208 int retval;
1209 __le16 *cport_count;
1210
Johan Hovold478ce722016-02-17 19:30:40 +01001211 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
Fabien Parent24a61122015-09-02 15:50:37 +02001212 if (!cport_count)
1213 return -ENOMEM;
1214
1215 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -08001216 GB_APB_REQUEST_CPORT_COUNT,
Fabien Parent24a61122015-09-02 15:50:37 +02001217 USB_DIR_IN | USB_TYPE_VENDOR |
1218 USB_RECIP_INTERFACE, 0, 0, cport_count,
Johan Hovold60793c92016-08-03 14:09:35 +02001219 sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
Johan Hovold478ce722016-02-17 19:30:40 +01001220 if (retval != sizeof(*cport_count)) {
Fabien Parent24a61122015-09-02 15:50:37 +02001221 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1222 retval);
Johan Hovold478ce722016-02-17 19:30:40 +01001223
1224 if (retval >= 0)
1225 retval = -EIO;
1226
Fabien Parent24a61122015-09-02 15:50:37 +02001227 goto out;
1228 }
1229
1230 retval = le16_to_cpu(*cport_count);
1231
1232 /* We need to fit a CPort ID in one byte of a message header */
1233 if (retval > U8_MAX) {
1234 retval = U8_MAX;
1235 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1236 }
1237
1238out:
1239 kfree(cport_count);
1240 return retval;
1241}
1242
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001243/*
Johan Hovold4d5c4462015-11-02 11:56:57 +01001244 * The ES2 USB Bridge device has 15 endpoints
1245 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1246 * 7 Bulk IN - CPort data in
1247 * 7 Bulk OUT - CPort data out
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001248 */
1249static int ap_probe(struct usb_interface *interface,
1250 const struct usb_device_id *id)
1251{
Alex Elder4b1d8202015-10-27 22:18:37 -05001252 struct es2_ap_dev *es2;
Johan Hovold25376362015-11-03 18:03:23 +01001253 struct gb_host_device *hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001254 struct usb_device *udev;
1255 struct usb_host_interface *iface_desc;
1256 struct usb_endpoint_descriptor *endpoint;
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001257 __u8 ep_addr;
Johan Hovoldbe5d01f2016-05-11 10:18:00 +02001258 int retval;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001259 int i;
Fabien Parent24a61122015-09-02 15:50:37 +02001260 int num_cports;
Greg Kroah-Hartman403074b2016-08-17 16:27:49 +02001261 bool bulk_out_found = false;
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001262 bool bulk_in_found = false;
1263 bool arpc_in_found = false;
Alex Elder4bc13892015-06-13 11:02:11 -05001264
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001265 udev = usb_get_dev(interface_to_usbdev(interface));
1266
Alex Elder3be0e172015-10-27 22:18:41 -05001267 num_cports = apb_get_cport_count(udev);
Fabien Parent24a61122015-09-02 15:50:37 +02001268 if (num_cports < 0) {
1269 usb_put_dev(udev);
1270 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1271 num_cports);
1272 return num_cports;
1273 }
1274
Johan Hovoldd6e139b2015-11-03 18:03:25 +01001275 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001276 num_cports);
Alex Elder8ea70fe2015-05-22 09:52:45 -05001277 if (IS_ERR(hd)) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001278 usb_put_dev(udev);
Alex Elder8ea70fe2015-05-22 09:52:45 -05001279 return PTR_ERR(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001280 }
1281
Alex Elder4b1d8202015-10-27 22:18:37 -05001282 es2 = hd_to_es2(hd);
1283 es2->hd = hd;
1284 es2->usb_intf = interface;
1285 es2->usb_dev = udev;
1286 spin_lock_init(&es2->cport_out_urb_lock);
Alex Elder3be0e172015-10-27 22:18:41 -05001287 INIT_KFIFO(es2->apb_log_fifo);
Alex Elder4b1d8202015-10-27 22:18:37 -05001288 usb_set_intfdata(interface, es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001289
Johan Hovold05061502016-05-11 10:18:01 +02001290 /*
1291 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1292 * dynamically.
1293 */
1294 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1295 if (retval)
1296 goto error;
1297 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1298 if (retval)
1299 goto error;
1300
Johan Hovold4d5c4462015-11-02 11:56:57 +01001301 /* find all bulk endpoints */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001302 iface_desc = interface->cur_altsetting;
1303 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1304 endpoint = &iface_desc->endpoint[i].desc;
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001305 ep_addr = endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001306
Greg Kroah-Hartmanb767ee42015-07-24 17:09:48 -07001307 if (usb_endpoint_is_bulk_in(endpoint)) {
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001308 if (!bulk_in_found) {
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001309 es2->cport_in.endpoint = ep_addr;
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001310 bulk_in_found = true;
1311 } else if (!arpc_in_found) {
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001312 es2->arpc_endpoint_in = ep_addr;
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001313 arpc_in_found = true;
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001314 } else {
1315 dev_warn(&udev->dev,
1316 "Unused bulk IN endpoint found: 0x%02x\n",
1317 ep_addr);
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001318 }
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001319 continue;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001320 }
Greg Kroah-Hartman27229102016-08-17 17:11:04 +02001321 if (usb_endpoint_is_bulk_out(endpoint)) {
1322 if (!bulk_out_found) {
1323 es2->cport_out_endpoint = ep_addr;
1324 bulk_out_found = true;
1325 } else {
1326 dev_warn(&udev->dev,
1327 "Unused bulk OUT endpoint found: 0x%02x\n",
1328 ep_addr);
1329 }
1330 continue;
1331 }
1332 dev_warn(&udev->dev,
1333 "Unknown endpoint type found, address 0x%02x\n",
1334 ep_addr);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001335 }
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001336 if (!bulk_in_found || !arpc_in_found || !bulk_out_found) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001337 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
Johan Hovoldbe5d01f2016-05-11 10:18:00 +02001338 retval = -ENODEV;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001339 goto error;
1340 }
1341
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001342 /* Allocate buffers for our cport in messages */
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001343 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1344 struct urb *urb;
1345 u8 *buffer;
Alex Elder4b1d8202015-10-27 22:18:37 -05001346
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001347 urb = usb_alloc_urb(0, GFP_KERNEL);
1348 if (!urb) {
1349 retval = -ENOMEM;
1350 goto error;
Alexandre Bailon606addd2015-06-15 18:08:13 +02001351 }
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001352 es2->cport_in.urb[i] = urb;
1353
1354 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1355 if (!buffer) {
1356 retval = -ENOMEM;
1357 goto error;
1358 }
1359
1360 usb_fill_bulk_urb(urb, udev,
1361 usb_rcvbulkpipe(udev, es2->cport_in.endpoint),
1362 buffer, ES2_GBUF_MSG_SIZE_MAX,
1363 cport_in_callback, hd);
1364
1365 es2->cport_in.buffer[i] = buffer;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001366 }
1367
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001368 /* Allocate buffers for ARPC in messages */
1369 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1370 struct urb *urb;
1371 u8 *buffer;
1372
1373 urb = usb_alloc_urb(0, GFP_KERNEL);
1374 if (!urb) {
1375 retval = -ENOMEM;
1376 goto error;
1377 }
Johan Hovold5f62eab2016-08-17 15:43:32 +02001378 es2->arpc_urb[i] = urb;
1379
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001380 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1381 if (!buffer) {
1382 retval = -ENOMEM;
1383 goto error;
1384 }
1385
1386 usb_fill_bulk_urb(urb, udev,
1387 usb_rcvbulkpipe(udev,
1388 es2->arpc_endpoint_in),
1389 buffer, ARPC_IN_SIZE_MAX,
1390 arpc_in_callback, es2);
1391
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001392 es2->arpc_buffer[i] = buffer;
1393 }
1394
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001395 /* Allocate urbs for our CPort OUT messages */
1396 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1397 struct urb *urb;
1398
1399 urb = usb_alloc_urb(0, GFP_KERNEL);
Johan Hovoldbe5d01f2016-05-11 10:18:00 +02001400 if (!urb) {
1401 retval = -ENOMEM;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001402 goto error;
Johan Hovoldbe5d01f2016-05-11 10:18:00 +02001403 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001404
Alex Elder4b1d8202015-10-27 22:18:37 -05001405 es2->cport_out_urb[i] = urb;
1406 es2->cport_out_urb_busy[i] = false; /* just to be anal */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001407 }
1408
Alex Elder3be0e172015-10-27 22:18:41 -05001409 /* XXX We will need to rename this per APB */
1410 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001411 0644,
1412 gb_debugfs_get(), es2,
1413 &apb_log_enable_fops);
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001414
Alexandre Bailonc14118a2016-07-07 07:41:00 -05001415 INIT_LIST_HEAD(&es2->arpcs);
1416 spin_lock_init(&es2->arpc_lock);
1417
Wei Yongjun1305f2b2016-10-19 13:17:53 +00001418 retval = es2_arpc_in_enable(es2);
1419 if (retval)
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001420 goto error;
1421
Johan Hovoldc1700472015-11-04 18:55:22 +01001422 retval = gb_hd_add(hd);
1423 if (retval)
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001424 goto err_disable_arpc_in;
Johan Hovoldc1700472015-11-04 18:55:22 +01001425
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001426 retval = es2_cport_in_enable(es2, &es2->cport_in);
1427 if (retval)
1428 goto err_hd_del;
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001429
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001430 return 0;
Johan Hovold57bc17f2015-11-04 18:55:20 +01001431
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001432err_hd_del:
Johan Hovoldc1700472015-11-04 18:55:22 +01001433 gb_hd_del(hd);
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001434err_disable_arpc_in:
1435 es2_arpc_in_disable(es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001436error:
Johan Hovold57bc17f2015-11-04 18:55:20 +01001437 es2_destroy(es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001438
1439 return retval;
1440}
1441
Viresh Kumar2437f1c2016-04-21 11:31:16 +05301442static void ap_disconnect(struct usb_interface *interface)
1443{
1444 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
Viresh Kumar2437f1c2016-04-21 11:31:16 +05301445
1446 gb_hd_del(es2->hd);
1447
Greg Kroah-Hartman7330c482016-08-17 16:37:39 +02001448 es2_cport_in_disable(es2, &es2->cport_in);
Alexandre Bailon9d9d3772016-07-07 07:41:00 -05001449 es2_arpc_in_disable(es2);
Viresh Kumar2437f1c2016-04-21 11:31:16 +05301450
1451 es2_destroy(es2);
1452}
1453
Alex Elder4b1d8202015-10-27 22:18:37 -05001454static struct usb_driver es2_ap_driver = {
Rob Herringc13c8bf2015-05-05 11:04:22 -05001455 .name = "es2_ap_driver",
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001456 .probe = ap_probe,
1457 .disconnect = ap_disconnect,
1458 .id_table = id_table,
Viresh Kumara0f997b2016-04-21 11:31:13 +05301459 .soft_unbind = 1,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001460};
1461
Alex Elder4b1d8202015-10-27 22:18:37 -05001462module_usb_driver(es2_ap_driver);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001463
Greg Kroah-Hartman6cf42a42015-04-13 19:51:33 +02001464MODULE_LICENSE("GPL v2");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001465MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");