blob: e39bd58e3a5c246e1813a81bd38df468850d11b7 [file] [log] [blame]
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001/*
2 * Greybus "AP" USB driver for "ES2" controller chips
3 *
Alex Elder142f8dd2015-03-26 21:25:06 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08006 *
7 * Released under the GPLv2 only.
8 */
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +01009#include <linux/kthread.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080010#include <linux/sizes.h>
11#include <linux/usb.h>
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010012#include <linux/kfifo.h>
13#include <linux/debugfs.h>
Johan Hovold491e60d2015-04-07 11:27:20 +020014#include <asm/unaligned.h>
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080015
16#include "greybus.h"
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080017#include "kernel_ver.h"
Bryan O'Donoghue3f2a8092015-08-11 13:50:52 +010018#include "connection.h"
Bryan O'Donoghue6872c462015-09-22 18:06:39 -070019#include "greybus_trace.h"
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080020
Alex Elder4b1d8202015-10-27 22:18:37 -050021/* Memory sizes for the buffers sent to/from the ES2 controller */
22#define ES2_GBUF_MSG_SIZE_MAX 2048
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080023
24static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman8f0a6542015-10-22 16:40:41 -070025 { USB_DEVICE(0x18d1, 0x1eaf) },
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080026 { },
27};
28MODULE_DEVICE_TABLE(usb, id_table);
29
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010030#define APB1_LOG_SIZE SZ_16K
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +010031
Alexandre Bailon606addd2015-06-15 18:08:13 +020032/* Number of bulk in and bulk out couple */
33#define NUM_BULKS 7
34
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080035/*
36 * Number of CPort IN urbs in flight at any point in time.
37 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
38 * flight.
39 */
40#define NUM_CPORT_IN_URB 4
41
42/* Number of CPort OUT urbs in flight at any point in time.
43 * Adjust if we get messages saying we are out of urbs in the system log.
44 */
Alexandre Bailon606addd2015-06-15 18:08:13 +020045#define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080046
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020047/*
48 * @endpoint: bulk in endpoint for CPort data
49 * @urb: array of urbs for the CPort in messages
50 * @buffer: array of buffers for the @cport_in_urb urbs
51 */
Alex Elder4b1d8202015-10-27 22:18:37 -050052struct es2_cport_in {
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020053 __u8 endpoint;
54 struct urb *urb[NUM_CPORT_IN_URB];
55 u8 *buffer[NUM_CPORT_IN_URB];
56};
57
58/*
59 * @endpoint: bulk out endpoint for CPort data
60 */
Alex Elder4b1d8202015-10-27 22:18:37 -050061struct es2_cport_out {
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020062 __u8 endpoint;
63};
64
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080065/**
Alex Elder4b1d8202015-10-27 22:18:37 -050066 * es2_ap_dev - ES2 USB Bridge to AP structure
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080067 * @usb_dev: pointer to the USB device we are.
68 * @usb_intf: pointer to the USB interface we are bound to.
Johan Hovold25376362015-11-03 18:03:23 +010069 * @hd: pointer to our gb_host_device structure
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020070
Alexandre Bailonddc09ac2015-06-15 18:08:12 +020071 * @cport_in: endpoint, urbs and buffer for cport in messages
72 * @cport_out: endpoint for for cport out messages
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080073 * @cport_out_urb: array of urbs for the CPort out messages
74 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
75 * not.
Johan Hovold3e136cc2015-07-01 12:37:21 +020076 * @cport_out_urb_cancelled: array of flags indicating whether the
77 * corresponding @cport_out_urb is being cancelled
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080078 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
Alex Elder1482b3e2015-10-27 22:18:38 -050079 *
Alex Elder3be0e172015-10-27 22:18:41 -050080 * @apb_log_task: task pointer for logging thread
81 * @apb_log_dentry: file system entry for the log file interface
82 * @apb_log_enable_dentry: file system entry for enabling logging
83 * @apb_log_fifo: kernel FIFO to carry logged data
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080084 */
Alex Elder4b1d8202015-10-27 22:18:37 -050085struct es2_ap_dev {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080086 struct usb_device *usb_dev;
87 struct usb_interface *usb_intf;
Johan Hovold25376362015-11-03 18:03:23 +010088 struct gb_host_device *hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080089
Alex Elder4b1d8202015-10-27 22:18:37 -050090 struct es2_cport_in cport_in[NUM_BULKS];
91 struct es2_cport_out cport_out[NUM_BULKS];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080092 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
93 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
Johan Hovold3e136cc2015-07-01 12:37:21 +020094 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080095 spinlock_t cport_out_urb_lock;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +020096
Fabien Parentc011d552015-09-02 15:50:36 +020097 int *cport_to_ep;
Alex Elder1482b3e2015-10-27 22:18:38 -050098
Alex Elder3be0e172015-10-27 22:18:41 -050099 struct task_struct *apb_log_task;
100 struct dentry *apb_log_dentry;
101 struct dentry *apb_log_enable_dentry;
102 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200103};
104
Alexandre Bailone5acf732015-09-14 18:20:44 +0200105/**
106 * cport_to_ep - information about cport to endpoints mapping
107 * @cport_id: the id of cport to map to endpoints
108 * @endpoint_in: the endpoint number to use for in transfer
109 * @endpoint_out: he endpoint number to use for out transfer
110 */
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200111struct cport_to_ep {
112 __le16 cport_id;
113 __u8 endpoint_in;
114 __u8 endpoint_out;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800115};
116
Johan Hovold25376362015-11-03 18:03:23 +0100117static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800118{
Alex Elder4b1d8202015-10-27 22:18:37 -0500119 return (struct es2_ap_dev *)&hd->hd_priv;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800120}
121
122static void cport_out_callback(struct urb *urb);
Alex Elder4b1d8202015-10-27 22:18:37 -0500123static void usb_log_enable(struct es2_ap_dev *es2);
124static void usb_log_disable(struct es2_ap_dev *es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800125
Alexandre Bailone5acf732015-09-14 18:20:44 +0200126/* Get the endpoints pair mapped to the cport */
Alex Elder4b1d8202015-10-27 22:18:37 -0500127static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200128{
Alex Elder4b1d8202015-10-27 22:18:37 -0500129 if (cport_id >= es2->hd->num_cports)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200130 return 0;
Alex Elder4b1d8202015-10-27 22:18:37 -0500131 return es2->cport_to_ep[cport_id];
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200132}
133
Alex Elder4b1d8202015-10-27 22:18:37 -0500134#define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800135
Greg Kroah-Hartman608f6612015-09-22 09:42:52 -0700136/* Disable for now until we work all of this out to keep a warning-free build */
137#if 0
Alexandre Bailone5acf732015-09-14 18:20:44 +0200138/* Test if the endpoints pair is already mapped to a cport */
Alex Elder4b1d8202015-10-27 22:18:37 -0500139static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200140{
141 int i;
142
Alex Elder4b1d8202015-10-27 22:18:37 -0500143 for (i = 0; i < es2->hd->num_cports; i++) {
144 if (es2->cport_to_ep[i] == ep_pair)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200145 return 1;
146 }
147 return 0;
148}
149
Alexandre Bailone5acf732015-09-14 18:20:44 +0200150/* Configure the endpoint mapping and send the request to APBridge */
Alex Elder4b1d8202015-10-27 22:18:37 -0500151static int map_cport_to_ep(struct es2_ap_dev *es2,
Alexandre Bailon1ff3dc92015-09-14 18:20:42 +0200152 u16 cport_id, int ep_pair)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200153{
154 int retval;
155 struct cport_to_ep *cport_to_ep;
156
Alexandre Bailon1ff3dc92015-09-14 18:20:42 +0200157 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200158 return -EINVAL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500159 if (cport_id >= es2->hd->num_cports)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200160 return -EINVAL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500161 if (ep_pair && ep_pair_in_use(es2, ep_pair))
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200162 return -EINVAL;
163
164 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
165 if (!cport_to_ep)
166 return -ENOMEM;
167
Alex Elder4b1d8202015-10-27 22:18:37 -0500168 es2->cport_to_ep[cport_id] = ep_pair;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200169 cport_to_ep->cport_id = cpu_to_le16(cport_id);
Alex Elder4b1d8202015-10-27 22:18:37 -0500170 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
171 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200172
Alex Elder4b1d8202015-10-27 22:18:37 -0500173 retval = usb_control_msg(es2->usb_dev,
174 usb_sndctrlpipe(es2->usb_dev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800175 GB_APB_REQUEST_EP_MAPPING,
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200176 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
177 0x00, 0x00,
178 (char *)cport_to_ep,
179 sizeof(*cport_to_ep),
Alex Elder4b1d8202015-10-27 22:18:37 -0500180 ES2_TIMEOUT);
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200181 if (retval == sizeof(*cport_to_ep))
182 retval = 0;
183 kfree(cport_to_ep);
184
185 return retval;
186}
187
Alexandre Bailone5acf732015-09-14 18:20:44 +0200188/* Unmap a cport: use the muxed endpoints pair */
Alex Elder4b1d8202015-10-27 22:18:37 -0500189static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200190{
Alex Elder4b1d8202015-10-27 22:18:37 -0500191 return map_cport_to_ep(es2, cport_id, 0);
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200192}
Greg Kroah-Hartman608f6612015-09-22 09:42:52 -0700193#endif
Alexandre Bailonfc1a5362015-06-15 18:08:14 +0200194
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800195static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200196{
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200197 struct usb_device *udev = es2->usb_dev;
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800198 u8 *data;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200199 int retval;
200
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800201 data = kmalloc(size, GFP_KERNEL);
202 if (!data)
Laurent Pinchartccb58032015-12-22 03:00:37 +0200203 return -ENOMEM;
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800204 memcpy(data, req, size);
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200205
206 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800207 cmd,
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200208 USB_DIR_OUT | USB_TYPE_VENDOR |
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800209 USB_RECIP_INTERFACE,
210 0, 0, data, size, ES2_TIMEOUT);
Laurent Pinchartccb58032015-12-22 03:00:37 +0200211 if (retval < 0)
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800212 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
213 else
214 retval = 0;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200215
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800216 kfree(data);
Laurent Pinchartccb58032015-12-22 03:00:37 +0200217 return retval;
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200218}
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800219
220static void ap_urb_complete(struct urb *urb)
221{
222 struct usb_ctrlrequest *dr = urb->context;
223
224 kfree(dr);
225 usb_free_urb(urb);
226}
227
228static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
229{
230 struct usb_device *udev = es2->usb_dev;
231 struct urb *urb;
232 struct usb_ctrlrequest *dr;
233 u8 *buf;
234 int retval;
235
236 urb = usb_alloc_urb(0, GFP_ATOMIC);
237 if (!urb)
238 return -ENOMEM;
239
240 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
241 if (!dr) {
242 usb_free_urb(urb);
243 return -ENOMEM;
244 }
245
246 buf = (u8 *)dr + sizeof(*dr);
247 memcpy(buf, req, size);
248
249 dr->bRequest = cmd;
250 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
251 dr->wValue = 0;
252 dr->wIndex = 0;
253 dr->wLength = cpu_to_le16(size);
254
255 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
256 (unsigned char *)dr, buf, size,
257 ap_urb_complete, dr);
258 retval = usb_submit_urb(urb, GFP_ATOMIC);
259 if (retval) {
260 usb_free_urb(urb);
261 kfree(dr);
262 }
263 return retval;
264}
265
266static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
267 bool async)
268{
269 struct es2_ap_dev *es2 = hd_to_es2(hd);
270
271 if (async)
272 return output_async(es2, req, size, cmd);
273
274 return output_sync(es2, req, size, cmd);
275}
Laurent Pinchart8e2b7da2015-12-18 21:23:24 +0200276
Johan Hovold0ce68ce42015-11-04 18:55:14 +0100277static int es2_cport_in_enable(struct es2_ap_dev *es2,
278 struct es2_cport_in *cport_in)
279{
280 struct urb *urb;
281 int ret;
282 int i;
283
284 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
285 urb = cport_in->urb[i];
286
287 ret = usb_submit_urb(urb, GFP_KERNEL);
288 if (ret) {
289 dev_err(&es2->usb_dev->dev,
290 "failed to submit in-urb: %d\n", ret);
291 goto err_kill_urbs;
292 }
293 }
294
295 return 0;
296
297err_kill_urbs:
298 for (--i; i >= 0; --i) {
299 urb = cport_in->urb[i];
300 usb_kill_urb(urb);
301 }
302
303 return ret;
304}
305
Johan Hovoldf6624ca2015-11-04 18:55:16 +0100306static void es2_cport_in_disable(struct es2_ap_dev *es2,
307 struct es2_cport_in *cport_in)
308{
309 struct urb *urb;
310 int i;
311
312 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
313 urb = cport_in->urb[i];
314 usb_kill_urb(urb);
315 }
316}
317
Alex Elder4b1d8202015-10-27 22:18:37 -0500318static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800319{
320 struct urb *urb = NULL;
321 unsigned long flags;
322 int i;
323
Alex Elder4b1d8202015-10-27 22:18:37 -0500324 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800325
326 /* Look in our pool of allocated urbs first, as that's the "fastest" */
327 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500328 if (es2->cport_out_urb_busy[i] == false &&
329 es2->cport_out_urb_cancelled[i] == false) {
330 es2->cport_out_urb_busy[i] = true;
331 urb = es2->cport_out_urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800332 break;
333 }
334 }
Alex Elder4b1d8202015-10-27 22:18:37 -0500335 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800336 if (urb)
337 return urb;
338
339 /*
340 * Crap, pool is empty, complain to the syslog and go allocate one
341 * dynamically as we have to succeed.
342 */
Johan Hovolded972e32015-12-15 15:51:48 +0100343 dev_dbg(&es2->usb_dev->dev,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800344 "No free CPort OUT urbs, having to dynamically allocate one!\n");
345 return usb_alloc_urb(0, gfp_mask);
346}
347
Alex Elder4b1d8202015-10-27 22:18:37 -0500348static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800349{
350 unsigned long flags;
351 int i;
352 /*
353 * See if this was an urb in our pool, if so mark it "free", otherwise
354 * we need to free it ourselves.
355 */
Alex Elder4b1d8202015-10-27 22:18:37 -0500356 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800357 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500358 if (urb == es2->cport_out_urb[i]) {
359 es2->cport_out_urb_busy[i] = false;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800360 urb = NULL;
361 break;
362 }
363 }
Alex Elder4b1d8202015-10-27 22:18:37 -0500364 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800365
366 /* If urb is not NULL, then we need to free this urb */
367 usb_free_urb(urb);
368}
369
370/*
Alex Elderd29b3d62015-06-13 11:02:08 -0500371 * We (ab)use the operation-message header pad bytes to transfer the
372 * cport id in order to minimise overhead.
373 */
374static void
375gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
376{
Alex Elder4bc13892015-06-13 11:02:11 -0500377 header->pad[0] = cport_id;
Alex Elderd29b3d62015-06-13 11:02:08 -0500378}
379
380/* Clear the pad bytes used for the CPort id */
381static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
382{
Alex Elder4bc13892015-06-13 11:02:11 -0500383 header->pad[0] = 0;
Alex Elderd29b3d62015-06-13 11:02:08 -0500384}
385
386/* Extract the CPort id packed into the header, and clear it */
387static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
388{
Alex Elder4bc13892015-06-13 11:02:11 -0500389 u16 cport_id = header->pad[0];
Alex Elderd29b3d62015-06-13 11:02:08 -0500390
391 gb_message_cport_clear(header);
392
393 return cport_id;
394}
395
396/*
Johan Hovold3e136cc2015-07-01 12:37:21 +0200397 * Returns zero if the message was successfully queued, or a negative errno
398 * otherwise.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800399 */
Johan Hovold25376362015-11-03 18:03:23 +0100400static int message_send(struct gb_host_device *hd, u16 cport_id,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200401 struct gb_message *message, gfp_t gfp_mask)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800402{
Alex Elder4b1d8202015-10-27 22:18:37 -0500403 struct es2_ap_dev *es2 = hd_to_es2(hd);
404 struct usb_device *udev = es2->usb_dev;
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200405 size_t buffer_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800406 int retval;
407 struct urb *urb;
Alexandre Bailon1ff3dc92015-09-14 18:20:42 +0200408 int ep_pair;
Johan Hovold3e136cc2015-07-01 12:37:21 +0200409 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800410
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800411 /*
412 * The data actually transferred will include an indication
413 * of where the data should be sent. Do one last check of
414 * the target CPort id before filling it in.
415 */
Fabien Parent144670c2015-09-02 15:50:35 +0200416 if (!cport_id_valid(hd, cport_id)) {
Johan Hovold100e9002015-12-07 15:05:38 +0100417 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200418 return -EINVAL;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800419 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800420
421 /* Find a free urb */
Alex Elder4b1d8202015-10-27 22:18:37 -0500422 urb = next_free_urb(es2, gfp_mask);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800423 if (!urb)
Johan Hovold3e136cc2015-07-01 12:37:21 +0200424 return -ENOMEM;
425
Alex Elder4b1d8202015-10-27 22:18:37 -0500426 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200427 message->hcpriv = urb;
Alex Elder4b1d8202015-10-27 22:18:37 -0500428 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800429
Alex Elderd29b3d62015-06-13 11:02:08 -0500430 /* Pack the cport id into the message header */
431 gb_message_cport_pack(message->header, cport_id);
Johan Hovold491e60d2015-04-07 11:27:20 +0200432
Alex Elder821c6202015-06-13 11:02:07 -0500433 buffer_size = sizeof(*message->header) + message->payload_size;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800434
Alex Elder4b1d8202015-10-27 22:18:37 -0500435 ep_pair = cport_to_ep_pair(es2, cport_id);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800436 usb_fill_bulk_urb(urb, udev,
Alexandre Bailon606addd2015-06-15 18:08:13 +0200437 usb_sndbulkpipe(udev,
Alex Elder4b1d8202015-10-27 22:18:37 -0500438 es2->cport_out[ep_pair].endpoint),
Alex Elder821c6202015-06-13 11:02:07 -0500439 message->buffer, buffer_size,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200440 cport_out_callback, message);
Alexandre Bailon977e2092015-08-31 09:00:16 +0200441 urb->transfer_flags |= URB_ZERO_PACKET;
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700442 trace_gb_host_device_send(hd, cport_id, buffer_size);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800443 retval = usb_submit_urb(urb, gfp_mask);
444 if (retval) {
Johan Hovold305a0312015-10-13 19:10:20 +0200445 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200446
Alex Elder4b1d8202015-10-27 22:18:37 -0500447 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200448 message->hcpriv = NULL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500449 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200450
Alex Elder4b1d8202015-10-27 22:18:37 -0500451 free_urb(es2, urb);
Alex Elderd29b3d62015-06-13 11:02:08 -0500452 gb_message_cport_clear(message->header);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200453
454 return retval;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800455 }
456
Johan Hovold3e136cc2015-07-01 12:37:21 +0200457 return 0;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800458}
459
460/*
Johan Hovold3e136cc2015-07-01 12:37:21 +0200461 * Can not be called in atomic context.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800462 */
Johan Hovold3e136cc2015-07-01 12:37:21 +0200463static void message_cancel(struct gb_message *message)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800464{
Johan Hovold25376362015-11-03 18:03:23 +0100465 struct gb_host_device *hd = message->operation->connection->hd;
Alex Elder4b1d8202015-10-27 22:18:37 -0500466 struct es2_ap_dev *es2 = hd_to_es2(hd);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200467 struct urb *urb;
468 int i;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800469
Johan Hovold3e136cc2015-07-01 12:37:21 +0200470 might_sleep();
471
Alex Elder4b1d8202015-10-27 22:18:37 -0500472 spin_lock_irq(&es2->cport_out_urb_lock);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200473 urb = message->hcpriv;
474
475 /* Prevent dynamically allocated urb from being deallocated. */
476 usb_get_urb(urb);
477
478 /* Prevent pre-allocated urb from being reused. */
479 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500480 if (urb == es2->cport_out_urb[i]) {
481 es2->cport_out_urb_cancelled[i] = true;
Johan Hovold3e136cc2015-07-01 12:37:21 +0200482 break;
483 }
484 }
Alex Elder4b1d8202015-10-27 22:18:37 -0500485 spin_unlock_irq(&es2->cport_out_urb_lock);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200486
487 usb_kill_urb(urb);
488
489 if (i < NUM_CPORT_OUT_URB) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500490 spin_lock_irq(&es2->cport_out_urb_lock);
491 es2->cport_out_urb_cancelled[i] = false;
492 spin_unlock_irq(&es2->cport_out_urb_lock);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200493 }
494
495 usb_free_urb(urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800496}
497
Johan Hovold25376362015-11-03 18:03:23 +0100498static int cport_reset(struct gb_host_device *hd, u16 cport_id)
Fabien Parent82ee1e62015-10-13 17:34:50 +0200499{
Alex Elder4b1d8202015-10-27 22:18:37 -0500500 struct es2_ap_dev *es2 = hd_to_es2(hd);
501 struct usb_device *udev = es2->usb_dev;
Fabien Parent82ee1e62015-10-13 17:34:50 +0200502 int retval;
503
504 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800505 GB_APB_REQUEST_RESET_CPORT,
Fabien Parent82ee1e62015-10-13 17:34:50 +0200506 USB_DIR_OUT | USB_TYPE_VENDOR |
Viresh Kumar69166d22015-11-09 15:05:03 +0530507 USB_RECIP_INTERFACE, cport_id, 0,
Alex Elder4b1d8202015-10-27 22:18:37 -0500508 NULL, 0, ES2_TIMEOUT);
Fabien Parent82ee1e62015-10-13 17:34:50 +0200509 if (retval < 0) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530510 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
Fabien Parent82ee1e62015-10-13 17:34:50 +0200511 retval);
512 return retval;
513 }
514
515 return 0;
516}
517
Johan Hovold25376362015-11-03 18:03:23 +0100518static int cport_enable(struct gb_host_device *hd, u16 cport_id)
Fabien Parent82ee1e62015-10-13 17:34:50 +0200519{
520 int retval;
521
522 if (cport_id != GB_SVC_CPORT_ID) {
523 retval = cport_reset(hd, cport_id);
524 if (retval)
525 return retval;
526 }
527
528 return 0;
529}
530
Johan Hovold25376362015-11-03 18:03:23 +0100531static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100532{
533 int retval;
Alex Elder4b1d8202015-10-27 22:18:37 -0500534 struct es2_ap_dev *es2 = hd_to_es2(hd);
535 struct usb_device *udev = es2->usb_dev;
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100536
537 if (!cport_id_valid(hd, cport_id)) {
Johan Hovold100e9002015-12-07 15:05:38 +0100538 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100539 return -EINVAL;
540 }
541
542 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800543 GB_APB_REQUEST_LATENCY_TAG_EN,
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100544 USB_DIR_OUT | USB_TYPE_VENDOR |
545 USB_RECIP_INTERFACE, cport_id, 0, NULL,
Alex Elder4b1d8202015-10-27 22:18:37 -0500546 0, ES2_TIMEOUT);
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100547
548 if (retval < 0)
549 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
550 cport_id);
551 return retval;
552}
553
Johan Hovold25376362015-11-03 18:03:23 +0100554static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100555{
556 int retval;
Alex Elder4b1d8202015-10-27 22:18:37 -0500557 struct es2_ap_dev *es2 = hd_to_es2(hd);
558 struct usb_device *udev = es2->usb_dev;
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100559
560 if (!cport_id_valid(hd, cport_id)) {
Johan Hovold100e9002015-12-07 15:05:38 +0100561 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100562 return -EINVAL;
563 }
564
565 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800566 GB_APB_REQUEST_LATENCY_TAG_DIS,
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100567 USB_DIR_OUT | USB_TYPE_VENDOR |
568 USB_RECIP_INTERFACE, cport_id, 0, NULL,
Alex Elder4b1d8202015-10-27 22:18:37 -0500569 0, ES2_TIMEOUT);
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100570
571 if (retval < 0)
572 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
573 cport_id);
574 return retval;
575}
576
Johan Hovolda8cc0202015-11-03 18:03:24 +0100577static struct gb_hd_driver es2_driver = {
Alex Elder4b1d8202015-10-27 22:18:37 -0500578 .hd_priv_size = sizeof(struct es2_ap_dev),
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200579 .message_send = message_send,
580 .message_cancel = message_cancel,
Fabien Parent82ee1e62015-10-13 17:34:50 +0200581 .cport_enable = cport_enable,
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100582 .latency_tag_enable = latency_tag_enable,
583 .latency_tag_disable = latency_tag_disable,
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800584 .output = output,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800585};
586
587/* Common function to report consistent warnings based on URB status */
588static int check_urb_status(struct urb *urb)
589{
590 struct device *dev = &urb->dev->dev;
591 int status = urb->status;
592
593 switch (status) {
594 case 0:
595 return 0;
596
597 case -EOVERFLOW:
598 dev_err(dev, "%s: overflow actual length is %d\n",
599 __func__, urb->actual_length);
600 case -ECONNRESET:
601 case -ENOENT:
602 case -ESHUTDOWN:
603 case -EILSEQ:
604 case -EPROTO:
605 /* device is gone, stop sending */
606 return status;
607 }
608 dev_err(dev, "%s: unknown status %d\n", __func__, status);
609
610 return -EAGAIN;
611}
612
Johan Hovold57bc17f2015-11-04 18:55:20 +0100613static void es2_destroy(struct es2_ap_dev *es2)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800614{
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800615 struct usb_device *udev;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200616 int bulk_in;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800617 int i;
618
Johan Hovold74cd6502015-11-04 18:55:18 +0100619 debugfs_remove(es2->apb_log_enable_dentry);
Alex Elder4b1d8202015-10-27 22:18:37 -0500620 usb_log_disable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100621
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800622 /* Tear down everything! */
623 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500624 struct urb *urb = es2->cport_out_urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800625
626 if (!urb)
627 break;
628 usb_kill_urb(urb);
629 usb_free_urb(urb);
Alex Elder4b1d8202015-10-27 22:18:37 -0500630 es2->cport_out_urb[i] = NULL;
631 es2->cport_out_urb_busy[i] = false; /* just to be anal */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800632 }
633
Alexandre Bailon606addd2015-06-15 18:08:13 +0200634 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500635 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
636
Alexandre Bailon606addd2015-06-15 18:08:13 +0200637 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
638 struct urb *urb = cport_in->urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800639
Alexandre Bailon606addd2015-06-15 18:08:13 +0200640 if (!urb)
641 break;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200642 usb_free_urb(urb);
643 kfree(cport_in->buffer[i]);
644 cport_in->buffer[i] = NULL;
645 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800646 }
647
Johan Hovolda5202862015-11-04 18:55:23 +0100648 kfree(es2->cport_to_ep);
649
Alex Elder4b1d8202015-10-27 22:18:37 -0500650 udev = es2->usb_dev;
Johan Hovoldc1700472015-11-04 18:55:22 +0100651 gb_hd_put(es2->hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800652
653 usb_put_dev(udev);
654}
655
Johan Hovold57bc17f2015-11-04 18:55:20 +0100656static void ap_disconnect(struct usb_interface *interface)
657{
658 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
659 int i;
660
661 for (i = 0; i < NUM_BULKS; ++i)
662 es2_cport_in_disable(es2, &es2->cport_in[i]);
663
Johan Hovoldc1700472015-11-04 18:55:22 +0100664 gb_hd_del(es2->hd);
665
Johan Hovold57bc17f2015-11-04 18:55:20 +0100666 es2_destroy(es2);
667}
668
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800669static void cport_in_callback(struct urb *urb)
670{
Johan Hovold25376362015-11-03 18:03:23 +0100671 struct gb_host_device *hd = urb->context;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800672 struct device *dev = &urb->dev->dev;
Johan Hovold491e60d2015-04-07 11:27:20 +0200673 struct gb_operation_msg_hdr *header;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800674 int status = check_urb_status(urb);
675 int retval;
676 u16 cport_id;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800677
678 if (status) {
679 if ((status == -EAGAIN) || (status == -EPROTO))
680 goto exit;
681 dev_err(dev, "urb cport in error %d (dropped)\n", status);
682 return;
683 }
684
Johan Hovold491e60d2015-04-07 11:27:20 +0200685 if (urb->actual_length < sizeof(*header)) {
Johan Hovold305a0312015-10-13 19:10:20 +0200686 dev_err(dev, "short message received\n");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800687 goto exit;
688 }
689
Alex Elderd29b3d62015-06-13 11:02:08 -0500690 /* Extract the CPort id, which is packed in the message header */
Johan Hovold491e60d2015-04-07 11:27:20 +0200691 header = urb->transfer_buffer;
Alex Elderd29b3d62015-06-13 11:02:08 -0500692 cport_id = gb_message_cport_unpack(header);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800693
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700694 if (cport_id_valid(hd, cport_id)) {
695 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
Alex Elder821c6202015-06-13 11:02:07 -0500696 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
Johan Hovold491e60d2015-04-07 11:27:20 +0200697 urb->actual_length);
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700698 } else {
Johan Hovold100e9002015-12-07 15:05:38 +0100699 dev_err(dev, "invalid cport id %u received\n", cport_id);
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700700 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800701exit:
702 /* put our urb back in the request pool */
703 retval = usb_submit_urb(urb, GFP_ATOMIC);
704 if (retval)
Johan Hovold305a0312015-10-13 19:10:20 +0200705 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800706}
707
708static void cport_out_callback(struct urb *urb)
709{
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200710 struct gb_message *message = urb->context;
Johan Hovold25376362015-11-03 18:03:23 +0100711 struct gb_host_device *hd = message->operation->connection->hd;
Alex Elder4b1d8202015-10-27 22:18:37 -0500712 struct es2_ap_dev *es2 = hd_to_es2(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800713 int status = check_urb_status(urb);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200714 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800715
Alex Elderd29b3d62015-06-13 11:02:08 -0500716 gb_message_cport_clear(message->header);
Johan Hovold491e60d2015-04-07 11:27:20 +0200717
Alex Elder4b1d8202015-10-27 22:18:37 -0500718 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold58c85122015-09-26 14:37:59 -0700719 message->hcpriv = NULL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500720 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Johan Hovold58c85122015-09-26 14:37:59 -0700721
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800722 /*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200723 * Tell the submitter that the message send (attempt) is
724 * complete, and report the status.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800725 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200726 greybus_message_sent(hd, message, status);
727
Alex Elder4b1d8202015-10-27 22:18:37 -0500728 free_urb(es2, urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800729}
730
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200731#define APB1_LOG_MSG_SIZE 64
Alex Elder3be0e172015-10-27 22:18:41 -0500732static void apb_log_get(struct es2_ap_dev *es2, char *buf)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100733{
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100734 int retval;
735
736 /* SVC messages go down our control pipe */
737 do {
Alex Elder4b1d8202015-10-27 22:18:37 -0500738 retval = usb_control_msg(es2->usb_dev,
739 usb_rcvctrlpipe(es2->usb_dev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800740 GB_APB_REQUEST_LOG,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100741 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
742 0x00, 0x00,
743 buf,
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200744 APB1_LOG_MSG_SIZE,
Alex Elder4b1d8202015-10-27 22:18:37 -0500745 ES2_TIMEOUT);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100746 if (retval > 0)
Alex Elder3be0e172015-10-27 22:18:41 -0500747 kfifo_in(&es2->apb_log_fifo, buf, retval);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100748 } while (retval > 0);
749}
750
Alex Elder3be0e172015-10-27 22:18:41 -0500751static int apb_log_poll(void *data)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100752{
Alex Elder4b1d8202015-10-27 22:18:37 -0500753 struct es2_ap_dev *es2 = data;
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200754 char *buf;
755
756 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
757 if (!buf)
758 return -ENOMEM;
759
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100760 while (!kthread_should_stop()) {
761 msleep(1000);
Alex Elder3be0e172015-10-27 22:18:41 -0500762 apb_log_get(es2, buf);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100763 }
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200764
765 kfree(buf);
766
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100767 return 0;
768}
769
Alex Elder3be0e172015-10-27 22:18:41 -0500770static ssize_t apb_log_read(struct file *f, char __user *buf,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100771 size_t count, loff_t *ppos)
772{
Alex Elder8995a392015-10-27 22:18:39 -0500773 struct es2_ap_dev *es2 = f->f_inode->i_private;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100774 ssize_t ret;
775 size_t copied;
776 char *tmp_buf;
777
778 if (count > APB1_LOG_SIZE)
779 count = APB1_LOG_SIZE;
780
781 tmp_buf = kmalloc(count, GFP_KERNEL);
782 if (!tmp_buf)
783 return -ENOMEM;
784
Alex Elder3be0e172015-10-27 22:18:41 -0500785 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100786 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
787
788 kfree(tmp_buf);
789
790 return ret;
791}
792
Alex Elder3be0e172015-10-27 22:18:41 -0500793static const struct file_operations apb_log_fops = {
794 .read = apb_log_read,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100795};
796
Alex Elder4b1d8202015-10-27 22:18:37 -0500797static void usb_log_enable(struct es2_ap_dev *es2)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100798{
Alex Elder3be0e172015-10-27 22:18:41 -0500799 if (!IS_ERR_OR_NULL(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100800 return;
801
802 /* get log from APB1 */
Alex Elder3be0e172015-10-27 22:18:41 -0500803 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
804 if (IS_ERR(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100805 return;
Alex Elder3be0e172015-10-27 22:18:41 -0500806 /* XXX We will need to rename this per APB */
807 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100808 gb_debugfs_get(), NULL,
Alex Elder3be0e172015-10-27 22:18:41 -0500809 &apb_log_fops);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100810}
811
Alex Elder4b1d8202015-10-27 22:18:37 -0500812static void usb_log_disable(struct es2_ap_dev *es2)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100813{
Alex Elder3be0e172015-10-27 22:18:41 -0500814 if (IS_ERR_OR_NULL(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100815 return;
816
Alex Elder3be0e172015-10-27 22:18:41 -0500817 debugfs_remove(es2->apb_log_dentry);
818 es2->apb_log_dentry = NULL;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100819
Alex Elder3be0e172015-10-27 22:18:41 -0500820 kthread_stop(es2->apb_log_task);
821 es2->apb_log_task = NULL;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100822}
823
Alex Elder3be0e172015-10-27 22:18:41 -0500824static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100825 size_t count, loff_t *ppos)
826{
Alex Elder1482b3e2015-10-27 22:18:38 -0500827 struct es2_ap_dev *es2 = f->f_inode->i_private;
Alex Elder3be0e172015-10-27 22:18:41 -0500828 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
Alex Elder4b1d8202015-10-27 22:18:37 -0500829 char tmp_buf[3];
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100830
831 sprintf(tmp_buf, "%d\n", enable);
832 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
833}
834
Alex Elder3be0e172015-10-27 22:18:41 -0500835static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100836 size_t count, loff_t *ppos)
837{
838 int enable;
839 ssize_t retval;
Alex Elder1482b3e2015-10-27 22:18:38 -0500840 struct es2_ap_dev *es2 = f->f_inode->i_private;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100841
842 retval = kstrtoint_from_user(buf, count, 10, &enable);
843 if (retval)
844 return retval;
845
846 if (enable)
Alex Elder4b1d8202015-10-27 22:18:37 -0500847 usb_log_enable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100848 else
Alex Elder4b1d8202015-10-27 22:18:37 -0500849 usb_log_disable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100850
851 return count;
852}
853
Alex Elder3be0e172015-10-27 22:18:41 -0500854static const struct file_operations apb_log_enable_fops = {
855 .read = apb_log_enable_read,
856 .write = apb_log_enable_write,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100857};
858
Alex Elder3be0e172015-10-27 22:18:41 -0500859static int apb_get_cport_count(struct usb_device *udev)
Fabien Parent24a61122015-09-02 15:50:37 +0200860{
861 int retval;
862 __le16 *cport_count;
863
864 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
865 if (!cport_count)
866 return -ENOMEM;
867
868 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800869 GB_APB_REQUEST_CPORT_COUNT,
Fabien Parent24a61122015-09-02 15:50:37 +0200870 USB_DIR_IN | USB_TYPE_VENDOR |
871 USB_RECIP_INTERFACE, 0, 0, cport_count,
Alex Elder4b1d8202015-10-27 22:18:37 -0500872 sizeof(*cport_count), ES2_TIMEOUT);
Fabien Parent24a61122015-09-02 15:50:37 +0200873 if (retval < 0) {
874 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
875 retval);
876 goto out;
877 }
878
879 retval = le16_to_cpu(*cport_count);
880
881 /* We need to fit a CPort ID in one byte of a message header */
882 if (retval > U8_MAX) {
883 retval = U8_MAX;
884 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
885 }
886
887out:
888 kfree(cport_count);
889 return retval;
890}
891
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800892/*
Johan Hovold4d5c4462015-11-02 11:56:57 +0100893 * The ES2 USB Bridge device has 15 endpoints
894 * 1 Control - usual USB stuff + AP -> APBridgeA messages
895 * 7 Bulk IN - CPort data in
896 * 7 Bulk OUT - CPort data out
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800897 */
898static int ap_probe(struct usb_interface *interface,
899 const struct usb_device_id *id)
900{
Alex Elder4b1d8202015-10-27 22:18:37 -0500901 struct es2_ap_dev *es2;
Johan Hovold25376362015-11-03 18:03:23 +0100902 struct gb_host_device *hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800903 struct usb_device *udev;
904 struct usb_host_interface *iface_desc;
905 struct usb_endpoint_descriptor *endpoint;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200906 int bulk_in = 0;
907 int bulk_out = 0;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800908 int retval = -ENOMEM;
909 int i;
Fabien Parent24a61122015-09-02 15:50:37 +0200910 int num_cports;
Laurent Pincharta75fd8b2015-12-08 19:54:59 +0200911 int cport_id;
Alex Elder4bc13892015-06-13 11:02:11 -0500912
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800913 udev = usb_get_dev(interface_to_usbdev(interface));
914
Alex Elder3be0e172015-10-27 22:18:41 -0500915 num_cports = apb_get_cport_count(udev);
Fabien Parent24a61122015-09-02 15:50:37 +0200916 if (num_cports < 0) {
917 usb_put_dev(udev);
918 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
919 num_cports);
920 return num_cports;
921 }
922
Johan Hovoldd6e139b2015-11-03 18:03:25 +0100923 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
924 num_cports);
Alex Elder8ea70fe2015-05-22 09:52:45 -0500925 if (IS_ERR(hd)) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800926 usb_put_dev(udev);
Alex Elder8ea70fe2015-05-22 09:52:45 -0500927 return PTR_ERR(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800928 }
929
Laurent Pincharta75fd8b2015-12-08 19:54:59 +0200930 /*
931 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
932 * won't be allocated dynamically.
933 */
934 do {
935 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
936 } while (cport_id > 0);
937
Alex Elder4b1d8202015-10-27 22:18:37 -0500938 es2 = hd_to_es2(hd);
939 es2->hd = hd;
940 es2->usb_intf = interface;
941 es2->usb_dev = udev;
942 spin_lock_init(&es2->cport_out_urb_lock);
Alex Elder3be0e172015-10-27 22:18:41 -0500943 INIT_KFIFO(es2->apb_log_fifo);
Alex Elder4b1d8202015-10-27 22:18:37 -0500944 usb_set_intfdata(interface, es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800945
Alex Elder4b1d8202015-10-27 22:18:37 -0500946 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
Fabien Parentc011d552015-09-02 15:50:36 +0200947 GFP_KERNEL);
Alex Elder4b1d8202015-10-27 22:18:37 -0500948 if (!es2->cport_to_ep) {
Fabien Parentc011d552015-09-02 15:50:36 +0200949 retval = -ENOMEM;
950 goto error;
951 }
952
Johan Hovold4d5c4462015-11-02 11:56:57 +0100953 /* find all bulk endpoints */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800954 iface_desc = interface->cur_altsetting;
955 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
956 endpoint = &iface_desc->endpoint[i].desc;
957
Greg Kroah-Hartmanb767ee42015-07-24 17:09:48 -0700958 if (usb_endpoint_is_bulk_in(endpoint)) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500959 es2->cport_in[bulk_in++].endpoint =
Alexandre Bailon606addd2015-06-15 18:08:13 +0200960 endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800961 } else if (usb_endpoint_is_bulk_out(endpoint)) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500962 es2->cport_out[bulk_out++].endpoint =
Alexandre Bailon606addd2015-06-15 18:08:13 +0200963 endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800964 } else {
965 dev_err(&udev->dev,
Viresh Kumarb933fa42015-12-04 21:30:10 +0530966 "Unknown endpoint type found, address 0x%02x\n",
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800967 endpoint->bEndpointAddress);
968 }
969 }
Johan Hovoldb6d80852015-11-04 18:55:13 +0100970 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800971 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
972 goto error;
973 }
974
Johan Hovold0ce68ce42015-11-04 18:55:14 +0100975 /* Allocate buffers for our cport in messages */
Alexandre Bailon606addd2015-06-15 18:08:13 +0200976 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500977 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
978
Alexandre Bailon606addd2015-06-15 18:08:13 +0200979 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
980 struct urb *urb;
981 u8 *buffer;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800982
Alexandre Bailon606addd2015-06-15 18:08:13 +0200983 urb = usb_alloc_urb(0, GFP_KERNEL);
984 if (!urb)
985 goto error;
Alex Elder4b1d8202015-10-27 22:18:37 -0500986 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
Alexandre Bailon606addd2015-06-15 18:08:13 +0200987 if (!buffer)
988 goto error;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800989
Alexandre Bailon606addd2015-06-15 18:08:13 +0200990 usb_fill_bulk_urb(urb, udev,
991 usb_rcvbulkpipe(udev,
992 cport_in->endpoint),
Alex Elder4b1d8202015-10-27 22:18:37 -0500993 buffer, ES2_GBUF_MSG_SIZE_MAX,
Alexandre Bailon606addd2015-06-15 18:08:13 +0200994 cport_in_callback, hd);
995 cport_in->urb[i] = urb;
996 cport_in->buffer[i] = buffer;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200997 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800998 }
999
1000 /* Allocate urbs for our CPort OUT messages */
1001 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1002 struct urb *urb;
1003
1004 urb = usb_alloc_urb(0, GFP_KERNEL);
1005 if (!urb)
1006 goto error;
1007
Alex Elder4b1d8202015-10-27 22:18:37 -05001008 es2->cport_out_urb[i] = urb;
1009 es2->cport_out_urb_busy[i] = false; /* just to be anal */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001010 }
1011
Alex Elder3be0e172015-10-27 22:18:41 -05001012 /* XXX We will need to rename this per APB */
1013 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
Johan Hovold86f918e2015-06-23 14:17:41 +02001014 (S_IWUSR | S_IRUGO),
Alex Elder4b1d8202015-10-27 22:18:37 -05001015 gb_debugfs_get(), es2,
Alex Elder3be0e172015-10-27 22:18:41 -05001016 &apb_log_enable_fops);
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001017
Johan Hovoldc1700472015-11-04 18:55:22 +01001018 retval = gb_hd_add(hd);
1019 if (retval)
1020 goto error;
1021
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001022 for (i = 0; i < NUM_BULKS; ++i) {
1023 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1024 if (retval)
Johan Hovold57bc17f2015-11-04 18:55:20 +01001025 goto err_disable_cport_in;
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001026 }
1027
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001028 return 0;
Johan Hovold57bc17f2015-11-04 18:55:20 +01001029
1030err_disable_cport_in:
1031 for (--i; i >= 0; --i)
1032 es2_cport_in_disable(es2, &es2->cport_in[i]);
Johan Hovoldc1700472015-11-04 18:55:22 +01001033 gb_hd_del(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001034error:
Johan Hovold57bc17f2015-11-04 18:55:20 +01001035 es2_destroy(es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001036
1037 return retval;
1038}
1039
Alex Elder4b1d8202015-10-27 22:18:37 -05001040static struct usb_driver es2_ap_driver = {
Rob Herringc13c8bf2015-05-05 11:04:22 -05001041 .name = "es2_ap_driver",
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001042 .probe = ap_probe,
1043 .disconnect = ap_disconnect,
1044 .id_table = id_table,
1045};
1046
Alex Elder4b1d8202015-10-27 22:18:37 -05001047module_usb_driver(es2_ap_driver);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001048
Greg Kroah-Hartman6cf42a42015-04-13 19:51:33 +02001049MODULE_LICENSE("GPL v2");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001050MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");