es2.c 37.2 KB
Newer Older
1 2 3
/*
 * Greybus "AP" USB driver for "ES2" controller chips
 *
4 5
 * Copyright 2014-2015 Google Inc.
 * Copyright 2014-2015 Linaro Ltd.
6 7 8
 *
 * Released under the GPLv2 only.
 */
9
#include <linux/kthread.h>
10 11
#include <linux/sizes.h>
#include <linux/usb.h>
12 13
#include <linux/kfifo.h>
#include <linux/debugfs.h>
14
#include <linux/list.h>
15
#include <asm/unaligned.h>
16

17
#include "arpc.h"
18
#include "greybus.h"
A
Alex Elder 已提交
19
#include "greybus_trace.h"
20
#include "connection.h"
21

22

23 24 25
/* Default timeout for USB vendor requests. */
#define ES2_USB_CTRL_TIMEOUT	500

26 27 28
/* Default timeout for ARPC CPort requests */
#define ES2_ARPC_CPORT_TIMEOUT	500

29 30 31 32
/* Fixed CPort numbers */
#define ES2_CPORT_CDSI0		16
#define ES2_CPORT_CDSI1		17

33 34
/* Memory sizes for the buffers sent to/from the ES2 controller */
#define ES2_GBUF_MSG_SIZE_MAX	2048
35

36
/* Memory sizes for the ARPC buffers */
37
#define ARPC_OUT_SIZE_MAX	U16_MAX
38 39
#define ARPC_IN_SIZE_MAX	128

40
static const struct usb_device_id id_table[] = {
41
	{ USB_DEVICE(0x18d1, 0x1eaf) },
42 43 44 45
	{ },
};
MODULE_DEVICE_TABLE(usb, id_table);

46 47
#define APB1_LOG_SIZE		SZ_16K

48 49 50 51 52 53 54 55 56 57
/*
 * Number of CPort IN urbs in flight at any point in time.
 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
 * flight.
 */
#define NUM_CPORT_IN_URB	4

/* Number of CPort OUT urbs in flight at any point in time.
 * Adjust if we get messages saying we are out of urbs in the system log.
 */
58
#define NUM_CPORT_OUT_URB	8
59

60 61 62 63 64
/*
 * Number of ARPC in urbs in flight at any point in time.
 */
#define NUM_ARPC_IN_URB		2

65 66 67 68 69
/*
 * @endpoint: bulk in endpoint for CPort data
 * @urb: array of urbs for the CPort in messages
 * @buffer: array of buffers for the @cport_in_urb urbs
 */
70
struct es2_cport_in {
71 72 73 74 75
	__u8 endpoint;
	struct urb *urb[NUM_CPORT_IN_URB];
	u8 *buffer[NUM_CPORT_IN_URB];
};

76
/**
77
 * es2_ap_dev - ES2 USB Bridge to AP structure
78 79
 * @usb_dev: pointer to the USB device we are.
 * @usb_intf: pointer to the USB interface we are bound to.
80
 * @hd: pointer to our gb_host_device structure
81 82

 * @cport_in: endpoint, urbs and buffer for cport in messages
83
 * @cport_out_endpoint: endpoint for for cport out messages
84 85 86
 * @cport_out_urb: array of urbs for the CPort out messages
 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
 *			not.
87 88
 * @cport_out_urb_cancelled: array of flags indicating whether the
 *			corresponding @cport_out_urb is being cancelled
89
 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
90
 *
91 92 93 94
 * @apb_log_task: task pointer for logging thread
 * @apb_log_dentry: file system entry for the log file interface
 * @apb_log_enable_dentry: file system entry for enabling logging
 * @apb_log_fifo: kernel FIFO to carry logged data
95 96 97
 * @arpc_urb: array of urbs for the ARPC in messages
 * @arpc_buffer: array of buffers for the @arpc_urb urbs
 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
98 99 100
 * @arpc_id_cycle: gives an unique id to ARPC
 * @arpc_lock: locks ARPC list
 * @arpcs: list of in progress ARPCs
101
 */
102
struct es2_ap_dev {
103 104
	struct usb_device *usb_dev;
	struct usb_interface *usb_intf;
105
	struct gb_host_device *hd;
106

107
	struct es2_cport_in cport_in;
108
	__u8 cport_out_endpoint;
109 110
	struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
	bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
111
	bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
112
	spinlock_t cport_out_urb_lock;
113

114 115
	bool cdsi1_in_use;

116 117 118 119
	struct task_struct *apb_log_task;
	struct dentry *apb_log_dentry;
	struct dentry *apb_log_enable_dentry;
	DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
120 121 122 123

	__u8 arpc_endpoint_in;
	struct urb *arpc_urb[NUM_ARPC_IN_URB];
	u8 *arpc_buffer[NUM_ARPC_IN_URB];
124 125 126 127

	int arpc_id_cycle;
	spinlock_t arpc_lock;
	struct list_head arpcs;
128 129
};

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/**
 * timesync_enable_request - Enable timesync in an APBridge
 * @count: number of TimeSync Pulses to expect
 * @frame_time: the initial FrameTime at the first TimeSync Pulse
 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
 * @refclk: The AP mandated reference clock to run FrameTime at
 */
struct timesync_enable_request {
	__u8	count;
	__le64	frame_time;
	__le32	strobe_delay;
	__le32	refclk;
} __packed;

/**
 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
 * @frame_time: An array of authoritative FrameTimes provided by the SVC
 *              and relayed to the APBridge by the AP
 */
struct timesync_authoritative_request {
	__le64	frame_time[GB_TIMESYNC_MAX_STROBES];
} __packed;

153 154 155 156 157 158 159 160
struct arpc {
	struct list_head list;
	struct arpc_request_message *req;
	struct arpc_response_message *resp;
	struct completion response_received;
	bool active;
};

161
static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
162
{
163
	return (struct es2_ap_dev *)&hd->hd_priv;
164 165 166
}

static void cport_out_callback(struct urb *urb);
167 168
static void usb_log_enable(struct es2_ap_dev *es2);
static void usb_log_disable(struct es2_ap_dev *es2);
169 170
static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
		     size_t size, int *result, unsigned int timeout);
171

172
static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
173 174
{
	struct usb_device *udev = es2->usb_dev;
175
	u8 *data;
176 177
	int retval;

178 179
	data = kmalloc(size, GFP_KERNEL);
	if (!data)
180
		return -ENOMEM;
181
	memcpy(data, req, size);
182 183

	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
184
				 cmd,
185
				 USB_DIR_OUT | USB_TYPE_VENDOR |
186
				 USB_RECIP_INTERFACE,
187
				 0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
188
	if (retval < 0)
189 190 191
		dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
	else
		retval = 0;
192

193
	kfree(data);
194
	return retval;
195
}
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

static void ap_urb_complete(struct urb *urb)
{
	struct usb_ctrlrequest *dr = urb->context;

	kfree(dr);
	usb_free_urb(urb);
}

static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
{
	struct usb_device *udev = es2->usb_dev;
	struct urb *urb;
	struct usb_ctrlrequest *dr;
	u8 *buf;
	int retval;

	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (!urb)
		return -ENOMEM;

	dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
	if (!dr) {
		usb_free_urb(urb);
		return -ENOMEM;
	}

	buf = (u8 *)dr + sizeof(*dr);
	memcpy(buf, req, size);

	dr->bRequest = cmd;
	dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
	dr->wValue = 0;
	dr->wIndex = 0;
	dr->wLength = cpu_to_le16(size);

	usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
			     (unsigned char *)dr, buf, size,
			     ap_urb_complete, dr);
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval) {
		usb_free_urb(urb);
		kfree(dr);
	}
	return retval;
}

static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
		     bool async)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);

	if (async)
		return output_async(es2, req, size, cmd);

	return output_sync(es2, req, size, cmd);
}
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static int es2_cport_in_enable(struct es2_ap_dev *es2,
				struct es2_cport_in *cport_in)
{
	struct urb *urb;
	int ret;
	int i;

	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
		urb = cport_in->urb[i];

		ret = usb_submit_urb(urb, GFP_KERNEL);
		if (ret) {
			dev_err(&es2->usb_dev->dev,
					"failed to submit in-urb: %d\n", ret);
			goto err_kill_urbs;
		}
	}

	return 0;

err_kill_urbs:
	for (--i; i >= 0; --i) {
		urb = cport_in->urb[i];
		usb_kill_urb(urb);
	}

	return ret;
}

283 284 285 286 287 288 289 290 291 292 293 294
static void es2_cport_in_disable(struct es2_ap_dev *es2,
				struct es2_cport_in *cport_in)
{
	struct urb *urb;
	int i;

	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
		urb = cport_in->urb[i];
		usb_kill_urb(urb);
	}
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
static int es2_arpc_in_enable(struct es2_ap_dev *es2)
{
	struct urb *urb;
	int ret;
	int i;

	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
		urb = es2->arpc_urb[i];

		ret = usb_submit_urb(urb, GFP_KERNEL);
		if (ret) {
			dev_err(&es2->usb_dev->dev,
				"failed to submit arpc in-urb: %d\n", ret);
			goto err_kill_urbs;
		}
	}

	return 0;

err_kill_urbs:
	for (--i; i >= 0; --i) {
		urb = es2->arpc_urb[i];
		usb_kill_urb(urb);
	}

	return ret;
}

static void es2_arpc_in_disable(struct es2_ap_dev *es2)
{
	struct urb *urb;
	int i;

	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
		urb = es2->arpc_urb[i];
		usb_kill_urb(urb);
	}
}

334
static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
335 336 337 338 339
{
	struct urb *urb = NULL;
	unsigned long flags;
	int i;

340
	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
341 342 343

	/* Look in our pool of allocated urbs first, as that's the "fastest" */
	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
344 345 346 347
		if (es2->cport_out_urb_busy[i] == false &&
				es2->cport_out_urb_cancelled[i] == false) {
			es2->cport_out_urb_busy[i] = true;
			urb = es2->cport_out_urb[i];
348 349 350
			break;
		}
	}
351
	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
352 353 354 355 356 357 358
	if (urb)
		return urb;

	/*
	 * Crap, pool is empty, complain to the syslog and go allocate one
	 * dynamically as we have to succeed.
	 */
359
	dev_dbg(&es2->usb_dev->dev,
360 361 362 363
		"No free CPort OUT urbs, having to dynamically allocate one!\n");
	return usb_alloc_urb(0, gfp_mask);
}

364
static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
365 366 367 368 369 370 371
{
	unsigned long flags;
	int i;
	/*
	 * See if this was an urb in our pool, if so mark it "free", otherwise
	 * we need to free it ourselves.
	 */
372
	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
373
	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
374 375
		if (urb == es2->cport_out_urb[i]) {
			es2->cport_out_urb_busy[i] = false;
376 377 378 379
			urb = NULL;
			break;
		}
	}
380
	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
381 382 383 384 385

	/* If urb is not NULL, then we need to free this urb */
	usb_free_urb(urb);
}

386 387 388 389 390 391 392
/*
 * We (ab)use the operation-message header pad bytes to transfer the
 * cport id in order to minimise overhead.
 */
static void
gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
{
393
	header->pad[0] = cport_id;
394 395 396 397 398
}

/* Clear the pad bytes used for the CPort id */
static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
{
399
	header->pad[0] = 0;
400 401 402 403 404
}

/* Extract the CPort id packed into the header, and clear it */
static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
{
405
	u16 cport_id = header->pad[0];
406 407 408 409 410 411

	gb_message_cport_clear(header);

	return cport_id;
}

412
/*
413 414
 * Returns zero if the message was successfully queued, or a negative errno
 * otherwise.
415
 */
416
static int message_send(struct gb_host_device *hd, u16 cport_id,
417
			struct gb_message *message, gfp_t gfp_mask)
418
{
419 420
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
421
	size_t buffer_size;
422 423
	int retval;
	struct urb *urb;
424
	unsigned long flags;
425 426 427 428 429 430

	/*
	 * The data actually transferred will include an indication
	 * of where the data should be sent.  Do one last check of
	 * the target CPort id before filling it in.
	 */
431
	if (!cport_id_valid(hd, cport_id)) {
432
		dev_err(&udev->dev, "invalid cport %u\n", cport_id);
433
		return -EINVAL;
434 435 436
	}

	/* Find a free urb */
437
	urb = next_free_urb(es2, gfp_mask);
438
	if (!urb)
439 440
		return -ENOMEM;

441
	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
442
	message->hcpriv = urb;
443
	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
444

445 446
	/* Pack the cport id into the message header */
	gb_message_cport_pack(message->header, cport_id);
447

A
Alex Elder 已提交
448
	buffer_size = sizeof(*message->header) + message->payload_size;
449

450
	usb_fill_bulk_urb(urb, udev,
451
			  usb_sndbulkpipe(udev,
452
					  es2->cport_out_endpoint),
A
Alex Elder 已提交
453
			  message->buffer, buffer_size,
454
			  cport_out_callback, message);
455
	urb->transfer_flags |= URB_ZERO_PACKET;
A
Alex Elder 已提交
456 457 458

	trace_gb_message_submit(message);

459 460
	retval = usb_submit_urb(urb, gfp_mask);
	if (retval) {
461
		dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
462

463
		spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
464
		message->hcpriv = NULL;
465
		spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
466

467
		free_urb(es2, urb);
468
		gb_message_cport_clear(message->header);
469 470

		return retval;
471 472
	}

473
	return 0;
474 475 476
}

/*
477
 * Can not be called in atomic context.
478
 */
479
static void message_cancel(struct gb_message *message)
480
{
481
	struct gb_host_device *hd = message->operation->connection->hd;
482
	struct es2_ap_dev *es2 = hd_to_es2(hd);
483 484
	struct urb *urb;
	int i;
485

486 487
	might_sleep();

488
	spin_lock_irq(&es2->cport_out_urb_lock);
489 490 491 492 493 494 495
	urb = message->hcpriv;

	/* Prevent dynamically allocated urb from being deallocated. */
	usb_get_urb(urb);

	/* Prevent pre-allocated urb from being reused. */
	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
496 497
		if (urb == es2->cport_out_urb[i]) {
			es2->cport_out_urb_cancelled[i] = true;
498 499 500
			break;
		}
	}
501
	spin_unlock_irq(&es2->cport_out_urb_lock);
502 503 504 505

	usb_kill_urb(urb);

	if (i < NUM_CPORT_OUT_URB) {
506
		spin_lock_irq(&es2->cport_out_urb_lock);
507
		es2->cport_out_urb_cancelled[i] = false;
508
		spin_unlock_irq(&es2->cport_out_urb_lock);
509 510 511
	}

	usb_free_urb(urb);
512 513
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
				unsigned long flags)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct ida *id_map = &hd->cport_id_map;
	int ida_start, ida_end;

	switch (cport_id) {
	case ES2_CPORT_CDSI0:
	case ES2_CPORT_CDSI1:
		dev_err(&hd->dev, "cport %d not available\n", cport_id);
		return -EBUSY;
	}

	if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
			flags & GB_CONNECTION_FLAG_CDSI1) {
		if (es2->cdsi1_in_use) {
			dev_err(&hd->dev, "CDSI1 already in use\n");
			return -EBUSY;
		}

		es2->cdsi1_in_use = true;

		return ES2_CPORT_CDSI1;
	}

	if (cport_id < 0) {
		ida_start = 0;
		ida_end = hd->num_cports;
	} else if (cport_id < hd->num_cports) {
		ida_start = cport_id;
		ida_end = cport_id + 1;
	} else {
		dev_err(&hd->dev, "cport %d not available\n", cport_id);
		return -EINVAL;
	}

	return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
}

static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);

	switch (cport_id) {
	case ES2_CPORT_CDSI1:
		es2->cdsi1_in_use = false;
		return;
	}

	ida_simple_remove(&hd->cport_id_map, cport_id);
}

567 568 569 570 571 572
static int cport_enable(struct gb_host_device *hd, u16 cport_id,
			unsigned long flags)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
	struct gb_apb_request_cport_flags *req;
573
	u32 connection_flags;
574 575 576 577 578 579
	int ret;

	req = kzalloc(sizeof(*req), GFP_KERNEL);
	if (!req)
		return -ENOMEM;

580
	connection_flags = 0;
581
	if (flags & GB_CONNECTION_FLAG_CONTROL)
582
		connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
583
	if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
584 585 586
		connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;

	req->flags = cpu_to_le32(connection_flags);
587 588

	dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
589
			cport_id, connection_flags);
590 591 592 593 594

	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				GB_APB_REQUEST_CPORT_FLAGS,
				USB_DIR_OUT | USB_TYPE_VENDOR |
				USB_RECIP_INTERFACE, cport_id, 0,
595
				req, sizeof(*req), ES2_USB_CTRL_TIMEOUT);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	if (ret != sizeof(*req)) {
		dev_err(&udev->dev, "failed to set cport flags for port %d\n",
				cport_id);
		if (ret >= 0)
			ret = -EIO;

		goto out;
	}

	ret = 0;
out:
	kfree(req);

	return ret;
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct device *dev = &es2->usb_dev->dev;
	struct arpc_cport_connected_req req;
	int ret;

	req.cport_id = cpu_to_le16(cport_id);
	ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req),
			NULL, ES2_ARPC_CPORT_TIMEOUT);
	if (ret) {
		dev_err(dev, "failed to set connected state for cport %u: %d\n",
				cport_id, ret);
		return ret;
	}

	return 0;
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct device *dev = &es2->usb_dev->dev;
	struct arpc_cport_flush_req req;
	int ret;

	req.cport_id = cpu_to_le16(cport_id);
	ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req),
			NULL, ES2_ARPC_CPORT_TIMEOUT);
	if (ret) {
		dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret);
		return ret;
	}

	return 0;
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id,
				u8 phase, unsigned int timeout)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct device *dev = &es2->usb_dev->dev;
	struct arpc_cport_shutdown_req req;
	int result;
	int ret;

	if (timeout > U16_MAX)
		return -EINVAL;

	req.cport_id = cpu_to_le16(cport_id);
	req.timeout = cpu_to_le16(timeout);
	req.phase = phase;
	ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req),
			&result, ES2_ARPC_CPORT_TIMEOUT + timeout);
	if (ret) {
		dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n",
				cport_id, ret, result);
		return ret;
	}

	return 0;
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id,
				size_t peer_space, unsigned int timeout)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct device *dev = &es2->usb_dev->dev;
	struct arpc_cport_quiesce_req req;
	int result;
	int ret;

	if (peer_space > U16_MAX)
		return -EINVAL;

	if (timeout > U16_MAX)
		return -EINVAL;

	req.cport_id = cpu_to_le16(cport_id);
	req.peer_space = cpu_to_le16(peer_space);
	req.timeout = cpu_to_le16(timeout);
	ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req),
			&result, ES2_ARPC_CPORT_TIMEOUT + timeout);
	if (ret) {
		dev_err(dev, "failed to quiesce cport %u: %d (%d)\n",
				cport_id, ret, result);
		return ret;
	}

	return 0;
}

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id)
{
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct device *dev = &es2->usb_dev->dev;
	struct arpc_cport_clear_req req;
	int ret;

	req.cport_id = cpu_to_le16(cport_id);
	ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req),
			NULL, ES2_ARPC_CPORT_TIMEOUT);
	if (ret) {
		dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret);
		return ret;
	}

	return 0;
}

722
static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
723 724
{
	int retval;
725 726
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
727 728

	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
729
				 GB_APB_REQUEST_LATENCY_TAG_EN,
730 731
				 USB_DIR_OUT | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, cport_id, 0, NULL,
732
				 0, ES2_USB_CTRL_TIMEOUT);
733 734 735 736 737 738 739

	if (retval < 0)
		dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
			cport_id);
	return retval;
}

740
static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
741 742
{
	int retval;
743 744
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
745 746

	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
747
				 GB_APB_REQUEST_LATENCY_TAG_DIS,
748 749
				 USB_DIR_OUT | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, cport_id, 0, NULL,
750
				 0, ES2_USB_CTRL_TIMEOUT);
751 752 753 754 755 756 757

	if (retval < 0)
		dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
			cport_id);
	return retval;
}

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
static int timesync_enable(struct gb_host_device *hd, u8 count,
			   u64 frame_time, u32 strobe_delay, u32 refclk)
{
	int retval;
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
	struct gb_control_timesync_enable_request *request;

	request = kzalloc(sizeof(*request), GFP_KERNEL);
	if (!request)
		return -ENOMEM;

	request->count = count;
	request->frame_time = cpu_to_le64(frame_time);
	request->strobe_delay = cpu_to_le32(strobe_delay);
	request->refclk = cpu_to_le32(refclk);
	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
775
				 GB_APB_REQUEST_TIMESYNC_ENABLE,
776 777
				 USB_DIR_OUT | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, 0, 0, request,
778
				 sizeof(*request), ES2_USB_CTRL_TIMEOUT);
779 780 781 782 783 784 785 786 787 788 789 790 791 792
	if (retval < 0)
		dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);

	kfree(request);
	return retval;
}

static int timesync_disable(struct gb_host_device *hd)
{
	int retval;
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;

	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
793
				 GB_APB_REQUEST_TIMESYNC_DISABLE,
794 795
				 USB_DIR_OUT | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, 0, 0, NULL,
796
				 0, ES2_USB_CTRL_TIMEOUT);
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	if (retval < 0)
		dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);

	return retval;
}

static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
{
	int retval, i;
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
	struct timesync_authoritative_request *request;

	request = kzalloc(sizeof(*request), GFP_KERNEL);
	if (!request)
		return -ENOMEM;

	for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
		request->frame_time[i] = cpu_to_le64(frame_time[i]);

	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
818
				 GB_APB_REQUEST_TIMESYNC_AUTHORITATIVE,
819 820
				 USB_DIR_OUT | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, 0, 0, request,
821
				 sizeof(*request), ES2_USB_CTRL_TIMEOUT);
822 823 824 825 826 827 828 829 830 831 832 833
	if (retval < 0)
		dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);

	kfree(request);
	return retval;
}

static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
{
	int retval;
	struct es2_ap_dev *es2 = hd_to_es2(hd);
	struct usb_device *udev = es2->usb_dev;
834
	__le64 *response_frame_time;
835 836 837 838 839 840

	response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
	if (!response_frame_time)
		return -ENOMEM;

	retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
841
				 GB_APB_REQUEST_TIMESYNC_GET_LAST_EVENT,
842 843
				 USB_DIR_IN | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
844 845
				 sizeof(*response_frame_time),
				 ES2_USB_CTRL_TIMEOUT);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

	if (retval != sizeof(*response_frame_time)) {
		dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
			retval);

		if (retval >= 0)
			retval = -EIO;

		goto out;
	}
	*frame_time = le64_to_cpu(*response_frame_time);
	retval = 0;
out:
	kfree(response_frame_time);
	return retval;
}

863
static struct gb_hd_driver es2_driver = {
864 865 866 867 868
	.hd_priv_size			= sizeof(struct es2_ap_dev),
	.message_send			= message_send,
	.message_cancel			= message_cancel,
	.cport_allocate			= es2_cport_allocate,
	.cport_release			= es2_cport_release,
869
	.cport_enable			= cport_enable,
870
	.cport_connected		= es2_cport_connected,
871
	.cport_flush			= es2_cport_flush,
872
	.cport_shutdown			= es2_cport_shutdown,
873
	.cport_quiesce			= es2_cport_quiesce,
874
	.cport_clear			= es2_cport_clear,
875 876 877 878 879 880 881
	.latency_tag_enable		= latency_tag_enable,
	.latency_tag_disable		= latency_tag_disable,
	.output				= output,
	.timesync_enable		= timesync_enable,
	.timesync_disable		= timesync_disable,
	.timesync_authoritative		= timesync_authoritative,
	.timesync_get_last_event	= timesync_get_last_event,
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
};

/* Common function to report consistent warnings based on URB status */
static int check_urb_status(struct urb *urb)
{
	struct device *dev = &urb->dev->dev;
	int status = urb->status;

	switch (status) {
	case 0:
		return 0;

	case -EOVERFLOW:
		dev_err(dev, "%s: overflow actual length is %d\n",
			__func__, urb->actual_length);
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
	case -EILSEQ:
	case -EPROTO:
		/* device is gone, stop sending */
		return status;
	}
	dev_err(dev, "%s: unknown status %d\n", __func__, status);

	return -EAGAIN;
}

910
static void es2_destroy(struct es2_ap_dev *es2)
911 912
{
	struct usb_device *udev;
913
	struct urb *urb;
914 915
	int i;

916
	debugfs_remove(es2->apb_log_enable_dentry);
917
	usb_log_disable(es2);
918

919 920
	/* Tear down everything! */
	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
921
		urb = es2->cport_out_urb[i];
922 923
		usb_kill_urb(urb);
		usb_free_urb(urb);
924 925
		es2->cport_out_urb[i] = NULL;
		es2->cport_out_urb_busy[i] = false;	/* just to be anal */
926 927
	}

928
	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
929
		usb_free_urb(es2->arpc_urb[i]);
930 931 932 933
		kfree(es2->arpc_buffer[i]);
		es2->arpc_buffer[i] = NULL;
	}

934 935 936 937
	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
		usb_free_urb(es2->cport_in.urb[i]);
		kfree(es2->cport_in.buffer[i]);
		es2->cport_in.buffer[i] = NULL;
938 939
	}

940 941 942 943
	/* release reserved CDSI0 and CDSI1 cports */
	gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
	gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);

944
	udev = es2->usb_dev;
945
	gb_hd_put(es2->hd);
946 947 948 949 950 951

	usb_put_dev(udev);
}

static void cport_in_callback(struct urb *urb)
{
952
	struct gb_host_device *hd = urb->context;
953
	struct device *dev = &urb->dev->dev;
954
	struct gb_operation_msg_hdr *header;
955 956 957 958 959 960 961
	int status = check_urb_status(urb);
	int retval;
	u16 cport_id;

	if (status) {
		if ((status == -EAGAIN) || (status == -EPROTO))
			goto exit;
962 963 964 965 966

		/* The urb is being unlinked */
		if (status == -ENOENT || status == -ESHUTDOWN)
			return;

967 968 969 970
		dev_err(dev, "urb cport in error %d (dropped)\n", status);
		return;
	}

971
	if (urb->actual_length < sizeof(*header)) {
972
		dev_err(dev, "short message received\n");
973 974 975
		goto exit;
	}

976
	/* Extract the CPort id, which is packed in the message header */
977
	header = urb->transfer_buffer;
978
	cport_id = gb_message_cport_unpack(header);
979

980
	if (cport_id_valid(hd, cport_id)) {
A
Alex Elder 已提交
981
		greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
982
							urb->actual_length);
983
	} else {
984
		dev_err(dev, "invalid cport id %u received\n", cport_id);
985
	}
986 987 988 989
exit:
	/* put our urb back in the request pool */
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
990
		dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
991 992 993 994
}

static void cport_out_callback(struct urb *urb)
{
995
	struct gb_message *message = urb->context;
996
	struct gb_host_device *hd = message->operation->connection->hd;
997
	struct es2_ap_dev *es2 = hd_to_es2(hd);
998
	int status = check_urb_status(urb);
999
	unsigned long flags;
1000

1001
	gb_message_cport_clear(message->header);
1002

1003
	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1004
	message->hcpriv = NULL;
1005
	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1006

1007
	/*
1008 1009
	 * Tell the submitter that the message send (attempt) is
	 * complete, and report the status.
1010
	 */
1011 1012
	greybus_message_sent(hd, message, status);

1013
	free_urb(es2, urb);
1014 1015
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
{
	struct arpc *rpc;

	if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
		return NULL;

	rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
	if (!rpc)
		return NULL;

	INIT_LIST_HEAD(&rpc->list);
	rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
	if (!rpc->req)
		goto err_free_rpc;

	rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1033
	if (!rpc->resp)
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
		goto err_free_req;

	rpc->req->type = type;
	rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
	memcpy(rpc->req->data, payload, size);

	init_completion(&rpc->response_received);

	return rpc;

err_free_req:
	kfree(rpc->req);
err_free_rpc:
	kfree(rpc);

	return NULL;
}

static void arpc_free(struct arpc *rpc)
{
	kfree(rpc->req);
	kfree(rpc->resp);
	kfree(rpc);
}

1059
static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1060 1061 1062 1063
{
	struct arpc *rpc;

	list_for_each_entry(rpc, &es2->arpcs, list) {
1064
		if (rpc->req->id == id)
1065 1066 1067 1068 1069 1070 1071 1072 1073
			return rpc;
	}

	return NULL;
}

static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
{
	rpc->active = true;
1074
	rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1075
	list_add_tail(&rpc->list, &es2->arpcs);
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
}

static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
{
	if (rpc->active) {
		rpc->active = false;
		list_del(&rpc->list);
	}
}

static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
{
	struct usb_device *udev = es2->usb_dev;
	int retval;

	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1092
				 GB_APB_REQUEST_ARPC_RUN,
1093 1094 1095
				 USB_DIR_OUT | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE,
				 0, 0,
1096
				 rpc->req, le16_to_cpu(rpc->req->size),
1097
				 ES2_USB_CTRL_TIMEOUT);
1098
	if (retval != le16_to_cpu(rpc->req->size)) {
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
		dev_err(&udev->dev,
			"failed to send ARPC request %d: %d\n",
			rpc->req->type, retval);
		if (retval > 0)
			retval = -EIO;
		return retval;
	}

	return 0;
}

static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
		     size_t size, int *result, unsigned int timeout)
{
	struct arpc *rpc;
	unsigned long flags;
	int retval;

1117 1118
	if (result)
		*result = 0;
1119

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	rpc = arpc_alloc(payload, size, type);
	if (!rpc)
		return -ENOMEM;

	spin_lock_irqsave(&es2->arpc_lock, flags);
	arpc_add(es2, rpc);
	spin_unlock_irqrestore(&es2->arpc_lock, flags);

	retval = arpc_send(es2, rpc, timeout);
	if (retval)
		goto out_arpc_del;

	retval = wait_for_completion_interruptible_timeout(
						&rpc->response_received,
						msecs_to_jiffies(timeout));
	if (retval <= 0) {
		if (!retval)
			retval = -ETIMEDOUT;
		goto out_arpc_del;
	}

1141
	if (rpc->resp->result) {
1142
		retval = -EREMOTEIO;
1143 1144
		if (result)
			*result = rpc->resp->result;
1145
	} else {
1146
		retval = 0;
1147
	}
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

out_arpc_del:
	spin_lock_irqsave(&es2->arpc_lock, flags);
	arpc_del(es2, rpc);
	spin_unlock_irqrestore(&es2->arpc_lock, flags);
	arpc_free(rpc);

	if (retval < 0 && retval != -EREMOTEIO) {
		dev_err(&es2->usb_dev->dev,
			"failed to execute ARPC: %d\n", retval);
	}

	return retval;
}

1163 1164
static void arpc_in_callback(struct urb *urb)
{
1165
	struct es2_ap_dev *es2 = urb->context;
1166 1167
	struct device *dev = &urb->dev->dev;
	int status = check_urb_status(urb);
1168 1169 1170
	struct arpc *rpc;
	struct arpc_response_message *resp;
	unsigned long flags;
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	int retval;

	if (status) {
		if ((status == -EAGAIN) || (status == -EPROTO))
			goto exit;

		/* The urb is being unlinked */
		if (status == -ENOENT || status == -ESHUTDOWN)
			return;

		dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
		return;
	}

1185 1186 1187 1188 1189 1190 1191
	if (urb->actual_length < sizeof(*resp)) {
		dev_err(dev, "short aprc response received\n");
		goto exit;
	}

	resp = urb->transfer_buffer;
	spin_lock_irqsave(&es2->arpc_lock, flags);
1192
	rpc = arpc_find(es2, resp->id);
1193
	if (!rpc) {
1194 1195
		dev_err(dev, "invalid arpc response id received: %u\n",
			le16_to_cpu(resp->id));
1196 1197 1198 1199 1200 1201 1202 1203 1204
		spin_unlock_irqrestore(&es2->arpc_lock, flags);
		goto exit;
	}

	arpc_del(es2, rpc);
	memcpy(rpc->resp, resp, sizeof(*resp));
	complete(&rpc->response_received);
	spin_unlock_irqrestore(&es2->arpc_lock, flags);

1205 1206 1207 1208 1209 1210 1211
exit:
	/* put our urb back in the request pool */
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
		dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
}

1212
#define APB1_LOG_MSG_SIZE	64
1213
static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1214 1215 1216 1217
{
	int retval;

	do {
1218 1219
		retval = usb_control_msg(es2->usb_dev,
					usb_rcvctrlpipe(es2->usb_dev, 0),
1220
					GB_APB_REQUEST_LOG,
1221 1222 1223
					USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
					0x00, 0x00,
					buf,
1224
					APB1_LOG_MSG_SIZE,
1225
					ES2_USB_CTRL_TIMEOUT);
1226
		if (retval > 0)
1227
			kfifo_in(&es2->apb_log_fifo, buf, retval);
1228 1229 1230
	} while (retval > 0);
}

1231
static int apb_log_poll(void *data)
1232
{
1233
	struct es2_ap_dev *es2 = data;
1234 1235 1236 1237 1238 1239
	char *buf;

	buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

1240 1241
	while (!kthread_should_stop()) {
		msleep(1000);
1242
		apb_log_get(es2, buf);
1243
	}
1244 1245 1246

	kfree(buf);

1247 1248 1249
	return 0;
}

1250
static ssize_t apb_log_read(struct file *f, char __user *buf,
1251 1252
				size_t count, loff_t *ppos)
{
1253
	struct es2_ap_dev *es2 = f->f_inode->i_private;
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	ssize_t ret;
	size_t copied;
	char *tmp_buf;

	if (count > APB1_LOG_SIZE)
		count = APB1_LOG_SIZE;

	tmp_buf = kmalloc(count, GFP_KERNEL);
	if (!tmp_buf)
		return -ENOMEM;

1265
	copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1266 1267 1268 1269 1270 1271 1272
	ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);

	kfree(tmp_buf);

	return ret;
}

1273 1274
static const struct file_operations apb_log_fops = {
	.read	= apb_log_read,
1275 1276
};

1277
static void usb_log_enable(struct es2_ap_dev *es2)
1278
{
1279
	if (!IS_ERR_OR_NULL(es2->apb_log_task))
1280 1281 1282
		return;

	/* get log from APB1 */
1283 1284
	es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
	if (IS_ERR(es2->apb_log_task))
1285
		return;
1286 1287
	/* XXX We will need to rename this per APB */
	es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1288
						gb_debugfs_get(), es2,
1289
						&apb_log_fops);
1290 1291
}

1292
static void usb_log_disable(struct es2_ap_dev *es2)
1293
{
1294
	if (IS_ERR_OR_NULL(es2->apb_log_task))
1295 1296
		return;

1297 1298
	debugfs_remove(es2->apb_log_dentry);
	es2->apb_log_dentry = NULL;
1299

1300 1301
	kthread_stop(es2->apb_log_task);
	es2->apb_log_task = NULL;
1302 1303
}

1304
static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1305 1306
				size_t count, loff_t *ppos)
{
1307
	struct es2_ap_dev *es2 = f->f_inode->i_private;
1308
	int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1309
	char tmp_buf[3];
1310 1311 1312 1313 1314

	sprintf(tmp_buf, "%d\n", enable);
	return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
}

1315
static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1316 1317 1318 1319
				size_t count, loff_t *ppos)
{
	int enable;
	ssize_t retval;
1320
	struct es2_ap_dev *es2 = f->f_inode->i_private;
1321 1322 1323 1324 1325 1326

	retval = kstrtoint_from_user(buf, count, 10, &enable);
	if (retval)
		return retval;

	if (enable)
1327
		usb_log_enable(es2);
1328
	else
1329
		usb_log_disable(es2);
1330 1331 1332 1333

	return count;
}

1334 1335 1336
static const struct file_operations apb_log_enable_fops = {
	.read	= apb_log_enable_read,
	.write	= apb_log_enable_write,
1337 1338
};

1339
static int apb_get_cport_count(struct usb_device *udev)
1340 1341 1342 1343
{
	int retval;
	__le16 *cport_count;

1344
	cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1345 1346 1347 1348
	if (!cport_count)
		return -ENOMEM;

	retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1349
				 GB_APB_REQUEST_CPORT_COUNT,
1350 1351
				 USB_DIR_IN | USB_TYPE_VENDOR |
				 USB_RECIP_INTERFACE, 0, 0, cport_count,
1352
				 sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
1353
	if (retval != sizeof(*cport_count)) {
1354 1355
		dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
			retval);
1356 1357 1358 1359

		if (retval >= 0)
			retval = -EIO;

1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
		goto out;
	}

	retval = le16_to_cpu(*cport_count);

	/* We need to fit a CPort ID in one byte of a message header */
	if (retval > U8_MAX) {
		retval = U8_MAX;
		dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
	}

out:
	kfree(cport_count);
	return retval;
}

1376
/*
1377 1378 1379 1380
 * The ES2 USB Bridge device has 15 endpoints
 * 1 Control - usual USB stuff + AP -> APBridgeA messages
 * 7 Bulk IN - CPort data in
 * 7 Bulk OUT - CPort data out
1381 1382 1383 1384
 */
static int ap_probe(struct usb_interface *interface,
		    const struct usb_device_id *id)
{
1385
	struct es2_ap_dev *es2;
1386
	struct gb_host_device *hd;
1387 1388 1389
	struct usb_device *udev;
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint;
1390
	__u8 ep_addr;
1391
	int retval;
1392
	int i;
1393
	int num_cports;
1394
	bool bulk_out_found = false;
1395 1396
	bool bulk_in_found = false;
	bool arpc_in_found = false;
1397

1398 1399
	udev = usb_get_dev(interface_to_usbdev(interface));

1400
	num_cports = apb_get_cport_count(udev);
1401 1402 1403 1404 1405 1406 1407
	if (num_cports < 0) {
		usb_put_dev(udev);
		dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
			num_cports);
		return num_cports;
	}

1408 1409
	hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
				num_cports);
1410
	if (IS_ERR(hd)) {
1411
		usb_put_dev(udev);
1412
		return PTR_ERR(hd);
1413 1414
	}

1415 1416 1417 1418 1419
	es2 = hd_to_es2(hd);
	es2->hd = hd;
	es2->usb_intf = interface;
	es2->usb_dev = udev;
	spin_lock_init(&es2->cport_out_urb_lock);
1420
	INIT_KFIFO(es2->apb_log_fifo);
1421
	usb_set_intfdata(interface, es2);
1422

1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	/*
	 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
	 * dynamically.
	 */
	retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
	if (retval)
		goto error;
	retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
	if (retval)
		goto error;

1434
	/* find all bulk endpoints */
1435 1436 1437
	iface_desc = interface->cur_altsetting;
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;
1438
		ep_addr = endpoint->bEndpointAddress;
1439

1440
		if (usb_endpoint_is_bulk_in(endpoint)) {
1441
			if (!bulk_in_found) {
1442
				es2->cport_in.endpoint = ep_addr;
1443 1444
				bulk_in_found = true;
			} else if (!arpc_in_found) {
1445
				es2->arpc_endpoint_in = ep_addr;
1446
				arpc_in_found = true;
1447 1448 1449 1450
			} else {
				dev_warn(&udev->dev,
					 "Unused bulk IN endpoint found: 0x%02x\n",
					 ep_addr);
1451
			}
1452
			continue;
1453
		}
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
		if (usb_endpoint_is_bulk_out(endpoint)) {
			if (!bulk_out_found) {
				es2->cport_out_endpoint = ep_addr;
				bulk_out_found = true;
			} else {
				dev_warn(&udev->dev,
					 "Unused bulk OUT endpoint found: 0x%02x\n",
					 ep_addr);
			}
			continue;
		}
		dev_warn(&udev->dev,
			 "Unknown endpoint type found, address 0x%02x\n",
			 ep_addr);
1468
	}
1469
	if (!bulk_in_found || !arpc_in_found || !bulk_out_found) {
1470
		dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1471
		retval = -ENODEV;
1472 1473 1474
		goto error;
	}

1475
	/* Allocate buffers for our cport in messages */
1476 1477 1478
	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
		struct urb *urb;
		u8 *buffer;
1479

1480 1481 1482 1483 1484 1485
		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!urb) {
			retval = -ENOMEM;
			goto error;
		}
		es2->cport_in.urb[i] = urb;
1486

1487 1488 1489 1490 1491
		buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
		if (!buffer) {
			retval = -ENOMEM;
			goto error;
		}
1492

1493 1494 1495 1496
		usb_fill_bulk_urb(urb, udev,
				  usb_rcvbulkpipe(udev, es2->cport_in.endpoint),
				  buffer, ES2_GBUF_MSG_SIZE_MAX,
				  cport_in_callback, hd);
1497

1498
		es2->cport_in.buffer[i] = buffer;
1499 1500
	}

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	/* Allocate buffers for ARPC in messages */
	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
		struct urb *urb;
		u8 *buffer;

		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!urb) {
			retval = -ENOMEM;
			goto error;
		}
1511 1512
		es2->arpc_urb[i] = urb;

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
		buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
		if (!buffer) {
			retval = -ENOMEM;
			goto error;
		}

		usb_fill_bulk_urb(urb, udev,
				  usb_rcvbulkpipe(udev,
						  es2->arpc_endpoint_in),
				  buffer, ARPC_IN_SIZE_MAX,
				  arpc_in_callback, es2);

		es2->arpc_buffer[i] = buffer;
	}

1528 1529 1530 1531 1532
	/* Allocate urbs for our CPort OUT messages */
	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
		struct urb *urb;

		urb = usb_alloc_urb(0, GFP_KERNEL);
1533 1534
		if (!urb) {
			retval = -ENOMEM;
1535
			goto error;
1536
		}
1537

1538 1539
		es2->cport_out_urb[i] = urb;
		es2->cport_out_urb_busy[i] = false;	/* just to be anal */
1540 1541
	}

1542 1543
	/* XXX We will need to rename this per APB */
	es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1544
							(S_IWUSR | S_IRUGO),
1545
							gb_debugfs_get(), es2,
1546
							&apb_log_enable_fops);
1547

1548 1549 1550
	INIT_LIST_HEAD(&es2->arpcs);
	spin_lock_init(&es2->arpc_lock);

1551 1552
	retval = es2_arpc_in_enable(es2);
	if (retval)
1553 1554
		goto error;

1555 1556
	retval = gb_hd_add(hd);
	if (retval)
1557
		goto err_disable_arpc_in;
1558

1559 1560 1561
	retval = es2_cport_in_enable(es2, &es2->cport_in);
	if (retval)
		goto err_hd_del;
1562

1563
	return 0;
1564

1565
err_hd_del:
1566
	gb_hd_del(hd);
1567 1568
err_disable_arpc_in:
	es2_arpc_in_disable(es2);
1569
error:
1570
	es2_destroy(es2);
1571 1572 1573 1574

	return retval;
}

1575 1576 1577 1578 1579 1580
static void ap_disconnect(struct usb_interface *interface)
{
	struct es2_ap_dev *es2 = usb_get_intfdata(interface);

	gb_hd_del(es2->hd);

1581
	es2_cport_in_disable(es2, &es2->cport_in);
1582
	es2_arpc_in_disable(es2);
1583 1584 1585 1586

	es2_destroy(es2);
}

1587
static struct usb_driver es2_ap_driver = {
1588
	.name =		"es2_ap_driver",
1589 1590 1591
	.probe =	ap_probe,
	.disconnect =	ap_disconnect,
	.id_table =	id_table,
1592
	.soft_unbind =	1,
1593 1594
};

1595
module_usb_driver(es2_ap_driver);
1596

1597
MODULE_LICENSE("GPL v2");
1598
MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");