blob: e920563351ff4cf9c4d830cd2a1a065d741c8aa1 [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
Fabien Parente70055b2016-02-23 18:46:10 +0100577static int fct_flow_enable(struct gb_host_device *hd, u16 cport_id)
578{
579 int retval;
580 struct es2_ap_dev *es2 = hd_to_es2(hd);
581 struct usb_device *udev = es2->usb_dev;
582
583 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
584 GB_APB_REQUEST_FCT_FLOW_EN,
585 USB_DIR_OUT | USB_TYPE_VENDOR |
586 USB_RECIP_INTERFACE, cport_id, 0, NULL,
587 0, ES2_TIMEOUT);
588 if (retval < 0)
589 dev_err(&udev->dev, "Cannot enable FCT flow for cport %u: %d\n",
590 cport_id, retval);
591 return retval;
592}
593
594static int fct_flow_disable(struct gb_host_device *hd, u16 cport_id)
595{
596 int retval;
597 struct es2_ap_dev *es2 = hd_to_es2(hd);
598 struct usb_device *udev = es2->usb_dev;
599
600 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
601 GB_APB_REQUEST_FCT_FLOW_DIS,
602 USB_DIR_OUT | USB_TYPE_VENDOR |
603 USB_RECIP_INTERFACE, cport_id, 0, NULL,
604 0, ES2_TIMEOUT);
605 if (retval < 0)
606 dev_err(&udev->dev,
607 "Cannot disable FCT flow for cport %u: %d\n",
608 cport_id, retval);
609 return retval;
610}
611
Johan Hovolda8cc0202015-11-03 18:03:24 +0100612static struct gb_hd_driver es2_driver = {
Alex Elder4b1d8202015-10-27 22:18:37 -0500613 .hd_priv_size = sizeof(struct es2_ap_dev),
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200614 .message_send = message_send,
615 .message_cancel = message_cancel,
Fabien Parent82ee1e62015-10-13 17:34:50 +0200616 .cport_enable = cport_enable,
Bryan O'Donoghue608ab2f2015-10-15 16:10:41 +0100617 .latency_tag_enable = latency_tag_enable,
618 .latency_tag_disable = latency_tag_disable,
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -0800619 .output = output,
Fabien Parente70055b2016-02-23 18:46:10 +0100620 .fct_flow_enable = fct_flow_enable,
621 .fct_flow_disable = fct_flow_disable,
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800622};
623
624/* Common function to report consistent warnings based on URB status */
625static int check_urb_status(struct urb *urb)
626{
627 struct device *dev = &urb->dev->dev;
628 int status = urb->status;
629
630 switch (status) {
631 case 0:
632 return 0;
633
634 case -EOVERFLOW:
635 dev_err(dev, "%s: overflow actual length is %d\n",
636 __func__, urb->actual_length);
637 case -ECONNRESET:
638 case -ENOENT:
639 case -ESHUTDOWN:
640 case -EILSEQ:
641 case -EPROTO:
642 /* device is gone, stop sending */
643 return status;
644 }
645 dev_err(dev, "%s: unknown status %d\n", __func__, status);
646
647 return -EAGAIN;
648}
649
Johan Hovold57bc17f2015-11-04 18:55:20 +0100650static void es2_destroy(struct es2_ap_dev *es2)
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800651{
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800652 struct usb_device *udev;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200653 int bulk_in;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800654 int i;
655
Johan Hovold74cd6502015-11-04 18:55:18 +0100656 debugfs_remove(es2->apb_log_enable_dentry);
Alex Elder4b1d8202015-10-27 22:18:37 -0500657 usb_log_disable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100658
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800659 /* Tear down everything! */
660 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500661 struct urb *urb = es2->cport_out_urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800662
663 if (!urb)
664 break;
665 usb_kill_urb(urb);
666 usb_free_urb(urb);
Alex Elder4b1d8202015-10-27 22:18:37 -0500667 es2->cport_out_urb[i] = NULL;
668 es2->cport_out_urb_busy[i] = false; /* just to be anal */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800669 }
670
Alexandre Bailon606addd2015-06-15 18:08:13 +0200671 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
Alex Elder4b1d8202015-10-27 22:18:37 -0500672 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
673
Alexandre Bailon606addd2015-06-15 18:08:13 +0200674 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
675 struct urb *urb = cport_in->urb[i];
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800676
Alexandre Bailon606addd2015-06-15 18:08:13 +0200677 if (!urb)
678 break;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200679 usb_free_urb(urb);
680 kfree(cport_in->buffer[i]);
681 cport_in->buffer[i] = NULL;
682 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800683 }
684
Johan Hovolda5202862015-11-04 18:55:23 +0100685 kfree(es2->cport_to_ep);
686
Alex Elder4b1d8202015-10-27 22:18:37 -0500687 udev = es2->usb_dev;
Johan Hovoldc1700472015-11-04 18:55:22 +0100688 gb_hd_put(es2->hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800689
690 usb_put_dev(udev);
691}
692
Johan Hovold57bc17f2015-11-04 18:55:20 +0100693static void ap_disconnect(struct usb_interface *interface)
694{
695 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
696 int i;
697
698 for (i = 0; i < NUM_BULKS; ++i)
699 es2_cport_in_disable(es2, &es2->cport_in[i]);
700
Johan Hovoldc1700472015-11-04 18:55:22 +0100701 gb_hd_del(es2->hd);
702
Johan Hovold57bc17f2015-11-04 18:55:20 +0100703 es2_destroy(es2);
704}
705
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800706static void cport_in_callback(struct urb *urb)
707{
Johan Hovold25376362015-11-03 18:03:23 +0100708 struct gb_host_device *hd = urb->context;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800709 struct device *dev = &urb->dev->dev;
Johan Hovold491e60d2015-04-07 11:27:20 +0200710 struct gb_operation_msg_hdr *header;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800711 int status = check_urb_status(urb);
712 int retval;
713 u16 cport_id;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800714
715 if (status) {
716 if ((status == -EAGAIN) || (status == -EPROTO))
717 goto exit;
718 dev_err(dev, "urb cport in error %d (dropped)\n", status);
719 return;
720 }
721
Johan Hovold491e60d2015-04-07 11:27:20 +0200722 if (urb->actual_length < sizeof(*header)) {
Johan Hovold305a0312015-10-13 19:10:20 +0200723 dev_err(dev, "short message received\n");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800724 goto exit;
725 }
726
Alex Elderd29b3d62015-06-13 11:02:08 -0500727 /* Extract the CPort id, which is packed in the message header */
Johan Hovold491e60d2015-04-07 11:27:20 +0200728 header = urb->transfer_buffer;
Alex Elderd29b3d62015-06-13 11:02:08 -0500729 cport_id = gb_message_cport_unpack(header);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800730
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700731 if (cport_id_valid(hd, cport_id)) {
732 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
Alex Elder821c6202015-06-13 11:02:07 -0500733 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
Johan Hovold491e60d2015-04-07 11:27:20 +0200734 urb->actual_length);
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700735 } else {
Johan Hovold100e9002015-12-07 15:05:38 +0100736 dev_err(dev, "invalid cport id %u received\n", cport_id);
Bryan O'Donoghue6872c462015-09-22 18:06:39 -0700737 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800738exit:
739 /* put our urb back in the request pool */
740 retval = usb_submit_urb(urb, GFP_ATOMIC);
741 if (retval)
Johan Hovold305a0312015-10-13 19:10:20 +0200742 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800743}
744
745static void cport_out_callback(struct urb *urb)
746{
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200747 struct gb_message *message = urb->context;
Johan Hovold25376362015-11-03 18:03:23 +0100748 struct gb_host_device *hd = message->operation->connection->hd;
Alex Elder4b1d8202015-10-27 22:18:37 -0500749 struct es2_ap_dev *es2 = hd_to_es2(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800750 int status = check_urb_status(urb);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200751 unsigned long flags;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800752
Alex Elderd29b3d62015-06-13 11:02:08 -0500753 gb_message_cport_clear(message->header);
Johan Hovold491e60d2015-04-07 11:27:20 +0200754
Alex Elder4b1d8202015-10-27 22:18:37 -0500755 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
Johan Hovold58c85122015-09-26 14:37:59 -0700756 message->hcpriv = NULL;
Alex Elder4b1d8202015-10-27 22:18:37 -0500757 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
Johan Hovold58c85122015-09-26 14:37:59 -0700758
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800759 /*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200760 * Tell the submitter that the message send (attempt) is
761 * complete, and report the status.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800762 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200763 greybus_message_sent(hd, message, status);
764
Alex Elder4b1d8202015-10-27 22:18:37 -0500765 free_urb(es2, urb);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800766}
767
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200768#define APB1_LOG_MSG_SIZE 64
Alex Elder3be0e172015-10-27 22:18:41 -0500769static void apb_log_get(struct es2_ap_dev *es2, char *buf)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100770{
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100771 int retval;
772
773 /* SVC messages go down our control pipe */
774 do {
Alex Elder4b1d8202015-10-27 22:18:37 -0500775 retval = usb_control_msg(es2->usb_dev,
776 usb_rcvctrlpipe(es2->usb_dev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800777 GB_APB_REQUEST_LOG,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100778 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
779 0x00, 0x00,
780 buf,
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200781 APB1_LOG_MSG_SIZE,
Alex Elder4b1d8202015-10-27 22:18:37 -0500782 ES2_TIMEOUT);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100783 if (retval > 0)
Alex Elder3be0e172015-10-27 22:18:41 -0500784 kfifo_in(&es2->apb_log_fifo, buf, retval);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100785 } while (retval > 0);
786}
787
Alex Elder3be0e172015-10-27 22:18:41 -0500788static int apb_log_poll(void *data)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100789{
Alex Elder4b1d8202015-10-27 22:18:37 -0500790 struct es2_ap_dev *es2 = data;
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200791 char *buf;
792
793 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
794 if (!buf)
795 return -ENOMEM;
796
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100797 while (!kthread_should_stop()) {
798 msleep(1000);
Alex Elder3be0e172015-10-27 22:18:41 -0500799 apb_log_get(es2, buf);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100800 }
Johan Hovoldc15ccab2015-04-07 11:27:12 +0200801
802 kfree(buf);
803
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100804 return 0;
805}
806
Alex Elder3be0e172015-10-27 22:18:41 -0500807static ssize_t apb_log_read(struct file *f, char __user *buf,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100808 size_t count, loff_t *ppos)
809{
Alex Elder8995a392015-10-27 22:18:39 -0500810 struct es2_ap_dev *es2 = f->f_inode->i_private;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100811 ssize_t ret;
812 size_t copied;
813 char *tmp_buf;
814
815 if (count > APB1_LOG_SIZE)
816 count = APB1_LOG_SIZE;
817
818 tmp_buf = kmalloc(count, GFP_KERNEL);
819 if (!tmp_buf)
820 return -ENOMEM;
821
Alex Elder3be0e172015-10-27 22:18:41 -0500822 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100823 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
824
825 kfree(tmp_buf);
826
827 return ret;
828}
829
Alex Elder3be0e172015-10-27 22:18:41 -0500830static const struct file_operations apb_log_fops = {
831 .read = apb_log_read,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100832};
833
Alex Elder4b1d8202015-10-27 22:18:37 -0500834static void usb_log_enable(struct es2_ap_dev *es2)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100835{
Alex Elder3be0e172015-10-27 22:18:41 -0500836 if (!IS_ERR_OR_NULL(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100837 return;
838
839 /* get log from APB1 */
Alex Elder3be0e172015-10-27 22:18:41 -0500840 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
841 if (IS_ERR(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100842 return;
Alex Elder3be0e172015-10-27 22:18:41 -0500843 /* XXX We will need to rename this per APB */
844 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100845 gb_debugfs_get(), NULL,
Alex Elder3be0e172015-10-27 22:18:41 -0500846 &apb_log_fops);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100847}
848
Alex Elder4b1d8202015-10-27 22:18:37 -0500849static void usb_log_disable(struct es2_ap_dev *es2)
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100850{
Alex Elder3be0e172015-10-27 22:18:41 -0500851 if (IS_ERR_OR_NULL(es2->apb_log_task))
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100852 return;
853
Alex Elder3be0e172015-10-27 22:18:41 -0500854 debugfs_remove(es2->apb_log_dentry);
855 es2->apb_log_dentry = NULL;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100856
Alex Elder3be0e172015-10-27 22:18:41 -0500857 kthread_stop(es2->apb_log_task);
858 es2->apb_log_task = NULL;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100859}
860
Alex Elder3be0e172015-10-27 22:18:41 -0500861static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100862 size_t count, loff_t *ppos)
863{
Alex Elder1482b3e2015-10-27 22:18:38 -0500864 struct es2_ap_dev *es2 = f->f_inode->i_private;
Alex Elder3be0e172015-10-27 22:18:41 -0500865 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
Alex Elder4b1d8202015-10-27 22:18:37 -0500866 char tmp_buf[3];
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100867
868 sprintf(tmp_buf, "%d\n", enable);
869 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
870}
871
Alex Elder3be0e172015-10-27 22:18:41 -0500872static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100873 size_t count, loff_t *ppos)
874{
875 int enable;
876 ssize_t retval;
Alex Elder1482b3e2015-10-27 22:18:38 -0500877 struct es2_ap_dev *es2 = f->f_inode->i_private;
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100878
879 retval = kstrtoint_from_user(buf, count, 10, &enable);
880 if (retval)
881 return retval;
882
883 if (enable)
Alex Elder4b1d8202015-10-27 22:18:37 -0500884 usb_log_enable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100885 else
Alex Elder4b1d8202015-10-27 22:18:37 -0500886 usb_log_disable(es2);
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100887
888 return count;
889}
890
Alex Elder3be0e172015-10-27 22:18:41 -0500891static const struct file_operations apb_log_enable_fops = {
892 .read = apb_log_enable_read,
893 .write = apb_log_enable_write,
Greg Kroah-Hartmanca3ec292015-03-27 11:38:07 +0100894};
895
Alex Elder3be0e172015-10-27 22:18:41 -0500896static int apb_get_cport_count(struct usb_device *udev)
Fabien Parent24a61122015-09-02 15:50:37 +0200897{
898 int retval;
899 __le16 *cport_count;
900
Johan Hovold478ce722016-02-17 19:30:40 +0100901 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
Fabien Parent24a61122015-09-02 15:50:37 +0200902 if (!cport_count)
903 return -ENOMEM;
904
905 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Greg Kroah-Hartmane5273382015-12-31 11:14:33 -0800906 GB_APB_REQUEST_CPORT_COUNT,
Fabien Parent24a61122015-09-02 15:50:37 +0200907 USB_DIR_IN | USB_TYPE_VENDOR |
908 USB_RECIP_INTERFACE, 0, 0, cport_count,
Alex Elder4b1d8202015-10-27 22:18:37 -0500909 sizeof(*cport_count), ES2_TIMEOUT);
Johan Hovold478ce722016-02-17 19:30:40 +0100910 if (retval != sizeof(*cport_count)) {
Fabien Parent24a61122015-09-02 15:50:37 +0200911 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
912 retval);
Johan Hovold478ce722016-02-17 19:30:40 +0100913
914 if (retval >= 0)
915 retval = -EIO;
916
Fabien Parent24a61122015-09-02 15:50:37 +0200917 goto out;
918 }
919
920 retval = le16_to_cpu(*cport_count);
921
922 /* We need to fit a CPort ID in one byte of a message header */
923 if (retval > U8_MAX) {
924 retval = U8_MAX;
925 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
926 }
927
928out:
929 kfree(cport_count);
930 return retval;
931}
932
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800933/*
Johan Hovold4d5c4462015-11-02 11:56:57 +0100934 * The ES2 USB Bridge device has 15 endpoints
935 * 1 Control - usual USB stuff + AP -> APBridgeA messages
936 * 7 Bulk IN - CPort data in
937 * 7 Bulk OUT - CPort data out
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800938 */
939static int ap_probe(struct usb_interface *interface,
940 const struct usb_device_id *id)
941{
Alex Elder4b1d8202015-10-27 22:18:37 -0500942 struct es2_ap_dev *es2;
Johan Hovold25376362015-11-03 18:03:23 +0100943 struct gb_host_device *hd;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800944 struct usb_device *udev;
945 struct usb_host_interface *iface_desc;
946 struct usb_endpoint_descriptor *endpoint;
Alexandre Bailon606addd2015-06-15 18:08:13 +0200947 int bulk_in = 0;
948 int bulk_out = 0;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800949 int retval = -ENOMEM;
950 int i;
Fabien Parent24a61122015-09-02 15:50:37 +0200951 int num_cports;
Laurent Pincharta75fd8b2015-12-08 19:54:59 +0200952 int cport_id;
Alex Elder4bc13892015-06-13 11:02:11 -0500953
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800954 udev = usb_get_dev(interface_to_usbdev(interface));
955
Alex Elder3be0e172015-10-27 22:18:41 -0500956 num_cports = apb_get_cport_count(udev);
Fabien Parent24a61122015-09-02 15:50:37 +0200957 if (num_cports < 0) {
958 usb_put_dev(udev);
959 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
960 num_cports);
961 return num_cports;
962 }
963
Johan Hovoldd6e139b2015-11-03 18:03:25 +0100964 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
965 num_cports);
Alex Elder8ea70fe2015-05-22 09:52:45 -0500966 if (IS_ERR(hd)) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800967 usb_put_dev(udev);
Alex Elder8ea70fe2015-05-22 09:52:45 -0500968 return PTR_ERR(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800969 }
970
Laurent Pincharta75fd8b2015-12-08 19:54:59 +0200971 /*
972 * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
973 * won't be allocated dynamically.
974 */
975 do {
976 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
977 } while (cport_id > 0);
978
Alex Elder4b1d8202015-10-27 22:18:37 -0500979 es2 = hd_to_es2(hd);
980 es2->hd = hd;
981 es2->usb_intf = interface;
982 es2->usb_dev = udev;
983 spin_lock_init(&es2->cport_out_urb_lock);
Alex Elder3be0e172015-10-27 22:18:41 -0500984 INIT_KFIFO(es2->apb_log_fifo);
Alex Elder4b1d8202015-10-27 22:18:37 -0500985 usb_set_intfdata(interface, es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800986
Alex Elder4b1d8202015-10-27 22:18:37 -0500987 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
Fabien Parentc011d552015-09-02 15:50:36 +0200988 GFP_KERNEL);
Alex Elder4b1d8202015-10-27 22:18:37 -0500989 if (!es2->cport_to_ep) {
Fabien Parentc011d552015-09-02 15:50:36 +0200990 retval = -ENOMEM;
991 goto error;
992 }
993
Johan Hovold4d5c4462015-11-02 11:56:57 +0100994 /* find all bulk endpoints */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800995 iface_desc = interface->cur_altsetting;
996 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
997 endpoint = &iface_desc->endpoint[i].desc;
998
Greg Kroah-Hartmanb767ee42015-07-24 17:09:48 -0700999 if (usb_endpoint_is_bulk_in(endpoint)) {
Alex Elder4b1d8202015-10-27 22:18:37 -05001000 es2->cport_in[bulk_in++].endpoint =
Alexandre Bailon606addd2015-06-15 18:08:13 +02001001 endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001002 } else if (usb_endpoint_is_bulk_out(endpoint)) {
Alex Elder4b1d8202015-10-27 22:18:37 -05001003 es2->cport_out[bulk_out++].endpoint =
Alexandre Bailon606addd2015-06-15 18:08:13 +02001004 endpoint->bEndpointAddress;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001005 } else {
1006 dev_err(&udev->dev,
Viresh Kumarb933fa42015-12-04 21:30:10 +05301007 "Unknown endpoint type found, address 0x%02x\n",
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001008 endpoint->bEndpointAddress);
1009 }
1010 }
Johan Hovoldb6d80852015-11-04 18:55:13 +01001011 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001012 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1013 goto error;
1014 }
1015
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001016 /* Allocate buffers for our cport in messages */
Alexandre Bailon606addd2015-06-15 18:08:13 +02001017 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
Alex Elder4b1d8202015-10-27 22:18:37 -05001018 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1019
Alexandre Bailon606addd2015-06-15 18:08:13 +02001020 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1021 struct urb *urb;
1022 u8 *buffer;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001023
Alexandre Bailon606addd2015-06-15 18:08:13 +02001024 urb = usb_alloc_urb(0, GFP_KERNEL);
1025 if (!urb)
1026 goto error;
Alex Elder4b1d8202015-10-27 22:18:37 -05001027 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
Alexandre Bailon606addd2015-06-15 18:08:13 +02001028 if (!buffer)
1029 goto error;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001030
Alexandre Bailon606addd2015-06-15 18:08:13 +02001031 usb_fill_bulk_urb(urb, udev,
1032 usb_rcvbulkpipe(udev,
1033 cport_in->endpoint),
Alex Elder4b1d8202015-10-27 22:18:37 -05001034 buffer, ES2_GBUF_MSG_SIZE_MAX,
Alexandre Bailon606addd2015-06-15 18:08:13 +02001035 cport_in_callback, hd);
1036 cport_in->urb[i] = urb;
1037 cport_in->buffer[i] = buffer;
Alexandre Bailon606addd2015-06-15 18:08:13 +02001038 }
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001039 }
1040
1041 /* Allocate urbs for our CPort OUT messages */
1042 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1043 struct urb *urb;
1044
1045 urb = usb_alloc_urb(0, GFP_KERNEL);
1046 if (!urb)
1047 goto error;
1048
Alex Elder4b1d8202015-10-27 22:18:37 -05001049 es2->cport_out_urb[i] = urb;
1050 es2->cport_out_urb_busy[i] = false; /* just to be anal */
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001051 }
1052
Alex Elder3be0e172015-10-27 22:18:41 -05001053 /* XXX We will need to rename this per APB */
1054 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
Johan Hovold86f918e2015-06-23 14:17:41 +02001055 (S_IWUSR | S_IRUGO),
Alex Elder4b1d8202015-10-27 22:18:37 -05001056 gb_debugfs_get(), es2,
Alex Elder3be0e172015-10-27 22:18:41 -05001057 &apb_log_enable_fops);
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001058
Johan Hovoldc1700472015-11-04 18:55:22 +01001059 retval = gb_hd_add(hd);
1060 if (retval)
1061 goto error;
1062
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001063 for (i = 0; i < NUM_BULKS; ++i) {
1064 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1065 if (retval)
Johan Hovold57bc17f2015-11-04 18:55:20 +01001066 goto err_disable_cport_in;
Johan Hovold0ce68ce42015-11-04 18:55:14 +01001067 }
1068
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001069 return 0;
Johan Hovold57bc17f2015-11-04 18:55:20 +01001070
1071err_disable_cport_in:
1072 for (--i; i >= 0; --i)
1073 es2_cport_in_disable(es2, &es2->cport_in[i]);
Johan Hovoldc1700472015-11-04 18:55:22 +01001074 gb_hd_del(hd);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001075error:
Johan Hovold57bc17f2015-11-04 18:55:20 +01001076 es2_destroy(es2);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001077
1078 return retval;
1079}
1080
Alex Elder4b1d8202015-10-27 22:18:37 -05001081static struct usb_driver es2_ap_driver = {
Rob Herringc13c8bf2015-05-05 11:04:22 -05001082 .name = "es2_ap_driver",
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001083 .probe = ap_probe,
1084 .disconnect = ap_disconnect,
1085 .id_table = id_table,
1086};
1087
Alex Elder4b1d8202015-10-27 22:18:37 -05001088module_usb_driver(es2_ap_driver);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001089
Greg Kroah-Hartman6cf42a42015-04-13 19:51:33 +02001090MODULE_LICENSE("GPL v2");
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001091MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");