vuart.c 27.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  PS3 virtual uart
 *
 *  Copyright (C) 2006 Sony Computer Entertainment Inc.
 *  Copyright 2006 Sony Corp.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
24
#include <linux/workqueue.h>
25 26
#include <asm/ps3.h>

Geoff Levand's avatar
Geoff Levand committed
27
#include <asm/firmware.h>
28 29 30 31 32 33 34
#include <asm/lv1call.h>
#include <asm/bitops.h>

#include "vuart.h"

MODULE_AUTHOR("Sony Corporation");
MODULE_LICENSE("GPL v2");
Geoff Levand's avatar
Geoff Levand committed
35
MODULE_DESCRIPTION("PS3 vuart");
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 153

/**
 * vuart - An inter-partition data link service.
 *  port 0: PS3 AV Settings.
 *  port 2: PS3 System Manager.
 *
 * The vuart provides a bi-directional byte stream data link between logical
 * partitions.  Its primary role is as a communications link between the guest
 * OS and the system policy module.  The current HV does not support any
 * connections other than those listed.
 */

enum {PORT_COUNT = 3,};

enum vuart_param {
	PARAM_TX_TRIGGER = 0,
	PARAM_RX_TRIGGER = 1,
	PARAM_INTERRUPT_MASK = 2,
	PARAM_RX_BUF_SIZE = 3, /* read only */
	PARAM_RX_BYTES = 4, /* read only */
	PARAM_TX_BUF_SIZE = 5, /* read only */
	PARAM_TX_BYTES = 6, /* read only */
	PARAM_INTERRUPT_STATUS = 7, /* read only */
};

enum vuart_interrupt_bit {
	INTERRUPT_BIT_TX = 0,
	INTERRUPT_BIT_RX = 1,
	INTERRUPT_BIT_DISCONNECT = 2,
};

enum vuart_interrupt_mask {
	INTERRUPT_MASK_TX = 1,
	INTERRUPT_MASK_RX = 2,
	INTERRUPT_MASK_DISCONNECT = 4,
};

/**
 * struct ports_bmp - bitmap indicating ports needing service.
 *
 * A 256 bit read only bitmap indicating ports needing service.  Do not write
 * to these bits.  Must not cross a page boundary.
 */

struct ports_bmp {
	u64 status;
	u64 unused[3];
} __attribute__ ((aligned (32)));

#define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
static void __attribute__ ((unused)) _dump_ports_bmp(
	const struct ports_bmp* bmp, const char* func, int line)
{
	pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
}

static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
	unsigned int *port_number)
{
	switch(match_id) {
	case PS3_MATCH_ID_AV_SETTINGS:
		*port_number = 0;
		return 0;
	case PS3_MATCH_ID_SYSTEM_MANAGER:
		*port_number = 2;
		return 0;
	default:
		WARN_ON(1);
		*port_number = UINT_MAX;
		return -EINVAL;
	};
}

#define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
	const char* func, int line)
{
#if defined(DEBUG)
	static const char *strings[] = {
		"tx_trigger      ",
		"rx_trigger      ",
		"interrupt_mask  ",
		"rx_buf_size     ",
		"rx_bytes        ",
		"tx_buf_size     ",
		"tx_bytes        ",
		"interrupt_status",
	};
	int result;
	unsigned int i;
	u64 value;

	for (i = 0; i < ARRAY_SIZE(strings); i++) {
		result = lv1_get_virtual_uart_param(port_number, i, &value);

		if (result) {
			pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
				port_number, strings[i], ps3_result(result));
			continue;
		}
		pr_debug("%s:%d: port_%u: %s = %lxh\n",
			func, line, port_number, strings[i], value);
	}
#endif
}

struct vuart_triggers {
	unsigned long rx;
	unsigned long tx;
};

int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
	struct vuart_triggers *trig)
{
	int result;
	unsigned long size;
	unsigned long val;

Geoff Levand's avatar
Geoff Levand committed
154
	result = lv1_get_virtual_uart_param(dev->priv->port_number,
155 156 157 158 159 160 161 162
		PARAM_TX_TRIGGER, &trig->tx);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

Geoff Levand's avatar
Geoff Levand committed
163
	result = lv1_get_virtual_uart_param(dev->priv->port_number,
164 165 166 167 168 169 170 171
		PARAM_RX_BUF_SIZE, &size);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

Geoff Levand's avatar
Geoff Levand committed
172
	result = lv1_get_virtual_uart_param(dev->priv->port_number,
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		PARAM_RX_TRIGGER, &val);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

	trig->rx = size - val;

	dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
		trig->tx, trig->rx);

	return result;
}

int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
	unsigned int rx)
{
	int result;
	unsigned long size;

Geoff Levand's avatar
Geoff Levand committed
195
	result = lv1_set_virtual_uart_param(dev->priv->port_number,
196 197 198 199 200 201 202 203
		PARAM_TX_TRIGGER, tx);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

Geoff Levand's avatar
Geoff Levand committed
204
	result = lv1_get_virtual_uart_param(dev->priv->port_number,
205 206 207 208 209 210 211 212
		PARAM_RX_BUF_SIZE, &size);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

Geoff Levand's avatar
Geoff Levand committed
213
	result = lv1_set_virtual_uart_param(dev->priv->port_number,
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		PARAM_RX_TRIGGER, size - rx);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

	dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
		tx, rx);

	return result;
}

static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
Geoff Levand's avatar
Geoff Levand committed
229
	u64 *bytes_waiting)
230
{
Geoff Levand's avatar
Geoff Levand committed
231
	int result = lv1_get_virtual_uart_param(dev->priv->port_number,
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		PARAM_RX_BYTES, bytes_waiting);

	if (result)
		dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
			__func__, __LINE__, ps3_result(result));

	dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
		*bytes_waiting);
	return result;
}

static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
	unsigned long mask)
{
	int result;

	dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);

Geoff Levand's avatar
Geoff Levand committed
250
	dev->priv->interrupt_mask = mask;
251

Geoff Levand's avatar
Geoff Levand committed
252 253
	result = lv1_set_virtual_uart_param(dev->priv->port_number,
		PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
254 255 256 257 258 259 260 261

	if (result)
		dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
			__func__, __LINE__, ps3_result(result));

	return result;
}

Geoff Levand's avatar
Geoff Levand committed
262
static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
263 264
	unsigned long *status)
{
Geoff Levand's avatar
Geoff Levand committed
265 266 267
	u64 tmp;
	int result = lv1_get_virtual_uart_param(dev->priv->port_number,
		PARAM_INTERRUPT_STATUS, &tmp);
268 269 270 271 272

	if (result)
		dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
			__func__, __LINE__, ps3_result(result));

Geoff Levand's avatar
Geoff Levand committed
273 274
	*status = tmp & dev->priv->interrupt_mask;

275
	dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
Geoff Levand's avatar
Geoff Levand committed
276
		__func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
277 278 279 280 281 282

	return result;
}

int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
{
Geoff Levand's avatar
Geoff Levand committed
283 284
	return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
		: ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
285 286 287 288 289
		| INTERRUPT_MASK_TX);
}

int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
{
Geoff Levand's avatar
Geoff Levand committed
290 291
	return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
		: ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
292 293 294 295 296
		| INTERRUPT_MASK_RX);
}

int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
{
Geoff Levand's avatar
Geoff Levand committed
297 298
	return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
		: ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
299 300 301 302 303
		| INTERRUPT_MASK_DISCONNECT);
}

int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
{
Geoff Levand's avatar
Geoff Levand committed
304 305
	return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX)
		? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
306 307 308 309 310
		& ~INTERRUPT_MASK_TX) : 0;
}

int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
{
Geoff Levand's avatar
Geoff Levand committed
311 312
	return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX)
		? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
313 314 315 316 317
		& ~INTERRUPT_MASK_RX) : 0;
}

int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
{
Geoff Levand's avatar
Geoff Levand committed
318 319
	return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
		? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
320 321 322 323 324 325 326 327 328 329 330 331 332 333
		& ~INTERRUPT_MASK_DISCONNECT) : 0;
}

/**
 * ps3_vuart_raw_write - Low level write helper.
 *
 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
 */

static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
	const void* buf, unsigned int bytes, unsigned long *bytes_written)
{
	int result;

Geoff Levand's avatar
Geoff Levand committed
334
	result = lv1_write_virtual_uart(dev->priv->port_number,
335 336 337 338 339 340 341 342
		ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
			"%s\n", __func__, __LINE__, ps3_result(result));
		return result;
	}

Geoff Levand's avatar
Geoff Levand committed
343
	dev->priv->stats.bytes_written += *bytes_written;
344

Geoff Levand's avatar
Geoff Levand committed
345 346
	dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
		*bytes_written, bytes, dev->priv->stats.bytes_written);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

	return result;
}

/**
 * ps3_vuart_raw_read - Low level read helper.
 *
 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
 */

static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
	unsigned int bytes, unsigned long *bytes_read)
{
	int result;

	dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);

Geoff Levand's avatar
Geoff Levand committed
364
	result = lv1_read_virtual_uart(dev->priv->port_number,
365 366 367 368 369 370 371 372
		ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
			__func__, __LINE__, ps3_result(result));
		return result;
	}

Geoff Levand's avatar
Geoff Levand committed
373
	dev->priv->stats.bytes_read += *bytes_read;
374 375

	dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand's avatar
Geoff Levand committed
376
		*bytes_read, bytes, dev->priv->stats.bytes_read);
377 378 379 380

	return result;
}

Geoff Levand's avatar
Geoff Levand committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
/**
 * ps3_vuart_clear_rx_bytes - Discard bytes received.
 * @bytes: Max byte count to discard, zero = all pending.
 *
 * Used to clear pending rx interrupt source.  Will not block.
 */

void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
	unsigned int bytes)
{
	int result;
	u64 bytes_waiting;
	void* tmp;

	result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);

	BUG_ON(result);

	bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;

	dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);

	if (!bytes)
		return;

	/* Add some extra space for recently arrived data. */

	bytes += 128;

	tmp = kmalloc(bytes, GFP_KERNEL);

	if (!tmp)
		return;

	ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);

	kfree(tmp);

	/* Don't include these bytes in the stats. */

	dev->priv->stats.bytes_read -= bytes_waiting;
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
/**
 * struct list_buffer - An element for a port device fifo buffer list.
 */

struct list_buffer {
	struct list_head link;
	const unsigned char *head;
	const unsigned char *tail;
	unsigned long dbg_number;
	unsigned char data[];
};

/**
 * ps3_vuart_write - the entry point for writing data to a port
 *
 * If the port is idle on entry as much of the incoming data is written to
 * the port as the port will accept.  Otherwise a list buffer is created
 * and any remaning incoming data is copied to that buffer.  The buffer is
 * then enqueued for transmision via the transmit interrupt.
 */

int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
	unsigned int bytes)
{
	static unsigned long dbg_number;
	int result;
	unsigned long flags;
	struct list_buffer *lb;

	dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
		bytes, bytes);

Geoff Levand's avatar
Geoff Levand committed
456
	spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
457

Geoff Levand's avatar
Geoff Levand committed
458
	if (list_empty(&dev->priv->tx_list.head)) {
459 460 461 462
		unsigned long bytes_written;

		result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);

Geoff Levand's avatar
Geoff Levand committed
463
		spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

		if (result) {
			dev_dbg(&dev->core,
				"%s:%d: ps3_vuart_raw_write failed\n",
				__func__, __LINE__);
			return result;
		}

		if (bytes_written == bytes) {
			dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
				__func__, __LINE__, bytes);
			return 0;
		}

		bytes -= bytes_written;
		buf += bytes_written;
	} else
Geoff Levand's avatar
Geoff Levand committed
481
		spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
482 483 484 485 486 487 488 489 490 491 492 493

	lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);

	if (!lb) {
		return -ENOMEM;
	}

	memcpy(lb->data, buf, bytes);
	lb->head = lb->data;
	lb->tail = lb->data + bytes;
	lb->dbg_number = ++dbg_number;

Geoff Levand's avatar
Geoff Levand committed
494 495
	spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
	list_add_tail(&lb->link, &dev->priv->tx_list.head);
496
	ps3_vuart_enable_interrupt_tx(dev);
Geoff Levand's avatar
Geoff Levand committed
497
	spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

	dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
		__func__, __LINE__, lb->dbg_number, bytes);

	return 0;
}

/**
 * ps3_vuart_read - the entry point for reading data from a port
 *
 * If enough bytes to satisfy the request are held in the buffer list those
 * bytes are dequeued and copied to the caller's buffer.  Emptied list buffers
 * are retiered.  If the request cannot be statified by bytes held in the list
 * buffers -EAGAIN is returned.
 */

int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
	unsigned int bytes)
{
	unsigned long flags;
	struct list_buffer *lb, *n;
	unsigned long bytes_read;

	dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
		bytes, bytes);

Geoff Levand's avatar
Geoff Levand committed
524
	spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
525

Geoff Levand's avatar
Geoff Levand committed
526 527
	if (dev->priv->rx_list.bytes_held < bytes) {
		spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
528
		dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
Geoff Levand's avatar
Geoff Levand committed
529 530
			__func__, __LINE__,
			bytes - dev->priv->rx_list.bytes_held);
531 532 533
		return -EAGAIN;
	}

Geoff Levand's avatar
Geoff Levand committed
534
	list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) {
535 536 537 538 539
		bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);

		memcpy(buf, lb->head, bytes_read);
		buf += bytes_read;
		bytes -= bytes_read;
Geoff Levand's avatar
Geoff Levand committed
540
		dev->priv->rx_list.bytes_held -= bytes_read;
541 542 543

		if (bytes_read < lb->tail - lb->head) {
			lb->head += bytes_read;
Geoff Levand's avatar
Geoff Levand committed
544 545 546 547
			dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
				"bytes\n", __func__, __LINE__, lb->dbg_number,
				bytes_read);
			spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
548 549 550
			return 0;
		}

Geoff Levand's avatar
Geoff Levand committed
551 552 553
		dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
			"bytes\n", __func__, __LINE__, lb->dbg_number,
			bytes_read);
554 555 556 557 558

		list_del(&lb->link);
		kfree(lb);
	}

Geoff Levand's avatar
Geoff Levand committed
559
	spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
560 561 562
	return 0;
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
	unsigned int bytes)
{
	unsigned long flags;

	if(dev->priv->work.trigger) {
		dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
			__func__, __LINE__);
		return -EAGAIN;
	}

	BUG_ON(!bytes);

	PREPARE_WORK(&dev->priv->work.work, func);

	spin_lock_irqsave(&dev->priv->work.lock, flags);
	if(dev->priv->rx_list.bytes_held >= bytes) {
		dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
			__func__, __LINE__, bytes);
		schedule_work(&dev->priv->work.work);
		spin_unlock_irqrestore(&dev->priv->work.lock, flags);
		return 0;
	}

	dev->priv->work.trigger = bytes;
	spin_unlock_irqrestore(&dev->priv->work.lock, flags);

	dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
		__LINE__, bytes, bytes);

	return 0;
}

void ps3_vuart_cancel_async(struct ps3_vuart_port_device *dev)
{
	dev->priv->work.trigger = 0;
}

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
/**
 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
 *
 * Services the transmit interrupt for the port.  Writes as much data from the
 * buffer list as the port will accept.  Retires any emptied list buffers and
 * adjusts the final list buffer state for a partial write.
 */

static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
{
	int result = 0;
	unsigned long flags;
	struct list_buffer *lb, *n;
	unsigned long bytes_total = 0;

	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);

Geoff Levand's avatar
Geoff Levand committed
618
	spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
619

Geoff Levand's avatar
Geoff Levand committed
620
	list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

		unsigned long bytes_written;

		result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
			&bytes_written);

		if (result) {
			dev_dbg(&dev->core,
				"%s:%d: ps3_vuart_raw_write failed\n",
				__func__, __LINE__);
			break;
		}

		bytes_total += bytes_written;

		if (bytes_written < lb->tail - lb->head) {
			lb->head += bytes_written;
			dev_dbg(&dev->core,
				"%s:%d cleared buf_%lu, %lxh bytes\n",
				__func__, __LINE__, lb->dbg_number,
				bytes_written);
			goto port_full;
		}

		dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
			lb->dbg_number);

		list_del(&lb->link);
		kfree(lb);
	}

	ps3_vuart_disable_interrupt_tx(dev);
port_full:
Geoff Levand's avatar
Geoff Levand committed
654
	spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
		__func__, __LINE__, bytes_total);
	return result;
}

/**
 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
 *
 * Services the receive interrupt for the port.  Creates a list buffer and
 * copies all waiting port data to that buffer and enqueues the buffer in the
 * buffer list.  Buffer list data is dequeued via ps3_vuart_read.
 */

static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
{
	static unsigned long dbg_number;
	int result = 0;
	unsigned long flags;
	struct list_buffer *lb;
	unsigned long bytes;

	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);

	result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);

	if (result)
		return -EIO;

	BUG_ON(!bytes);

Geoff Levand's avatar
Geoff Levand committed
685
	/* Add some extra space for recently arrived data. */
686 687 688 689 690 691 692 693 694 695 696 697 698 699

	bytes += 128;

	lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);

	if (!lb)
		return -ENOMEM;

	ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);

	lb->head = lb->data;
	lb->tail = lb->data + bytes;
	lb->dbg_number = ++dbg_number;

Geoff Levand's avatar
Geoff Levand committed
700 701 702 703
	spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
	list_add_tail(&lb->link, &dev->priv->rx_list.head);
	dev->priv->rx_list.bytes_held += bytes;
	spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
704

Geoff Levand's avatar
Geoff Levand committed
705
	dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
706 707
		__func__, __LINE__, lb->dbg_number, bytes);

708 709 710 711 712 713 714 715 716
	spin_lock_irqsave(&dev->priv->work.lock, flags);
	if(dev->priv->work.trigger
		&& dev->priv->rx_list.bytes_held >= dev->priv->work.trigger) {
		dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
			__func__, __LINE__, dev->priv->work.trigger);
		dev->priv->work.trigger = 0;
		schedule_work(&dev->priv->work.work);
	}
	spin_unlock_irqrestore(&dev->priv->work.lock, flags);
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	return 0;
}

static int ps3_vuart_handle_interrupt_disconnect(
	struct ps3_vuart_port_device *dev)
{
	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
	BUG_ON("no support");
	return -1;
}

/**
 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
 *
 * Services any pending interrupt types for the port.  Passes control to the
 * third stage type specific interrupt handler.  Returns control to the first
 * stage handler after one iteration.
 */

static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
{
	int result;
	unsigned long status;

Geoff Levand's avatar
Geoff Levand committed
741
	result = ps3_vuart_get_interrupt_status(dev, &status);
742 743 744 745 746 747 748 749

	if (result)
		return result;

	dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
		status);

	if (status & INTERRUPT_MASK_DISCONNECT) {
Geoff Levand's avatar
Geoff Levand committed
750
		dev->priv->stats.disconnect_interrupts++;
751 752 753 754 755 756
		result = ps3_vuart_handle_interrupt_disconnect(dev);
		if (result)
			ps3_vuart_disable_interrupt_disconnect(dev);
	}

	if (status & INTERRUPT_MASK_TX) {
Geoff Levand's avatar
Geoff Levand committed
757
		dev->priv->stats.tx_interrupts++;
758 759 760 761 762 763
		result = ps3_vuart_handle_interrupt_tx(dev);
		if (result)
			ps3_vuart_disable_interrupt_tx(dev);
	}

	if (status & INTERRUPT_MASK_RX) {
Geoff Levand's avatar
Geoff Levand committed
764
		dev->priv->stats.rx_interrupts++;
765 766 767 768 769 770 771 772
		result = ps3_vuart_handle_interrupt_rx(dev);
		if (result)
			ps3_vuart_disable_interrupt_rx(dev);
	}

	return 0;
}

Geoff Levand's avatar
Geoff Levand committed
773 774
struct vuart_bus_priv {
	const struct ports_bmp bmp;
775
	unsigned int virq;
Geoff Levand's avatar
Geoff Levand committed
776 777
	struct semaphore probe_mutex;
	int use_count;
778
	struct ps3_vuart_port_device *devices[PORT_COUNT];
Geoff Levand's avatar
Geoff Levand committed
779
} static vuart_bus_priv;
780 781 782 783 784 785 786 787 788 789 790

/**
 * ps3_vuart_irq_handler - first stage interrupt handler
 *
 * Loops finding any interrupting port and its associated instance data.
 * Passes control to the second stage port specific interrupt handler.  Loops
 * until all outstanding interrupts are serviced.
 */

static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
{
Geoff Levand's avatar
Geoff Levand committed
791
	struct vuart_bus_priv *bus_priv;
792 793

	BUG_ON(!_private);
Geoff Levand's avatar
Geoff Levand committed
794
	bus_priv = (struct vuart_bus_priv *)_private;
795 796 797 798

	while (1) {
		unsigned int port;

Geoff Levand's avatar
Geoff Levand committed
799
		dump_ports_bmp(&bus_priv->bmp);
800

Geoff Levand's avatar
Geoff Levand committed
801
		port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
802 803 804 805 806

		if (port == BITS_PER_LONG)
			break;

		BUG_ON(port >= PORT_COUNT);
Geoff Levand's avatar
Geoff Levand committed
807
		BUG_ON(!bus_priv->devices[port]);
808

Geoff Levand's avatar
Geoff Levand committed
809
		ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	}

	return IRQ_HANDLED;
}

static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
{
	int result;
	struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
	struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);

	result = dev->match_id == drv->match_id;

	dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
		__LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
		drv->core.name, (result ? "match" : "miss"));

	return result;
}

static int ps3_vuart_probe(struct device *_dev)
{
	int result;
Geoff Levand's avatar
Geoff Levand committed
833
	unsigned int port_number;
834 835 836 837 838 839 840 841
	struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
	struct ps3_vuart_port_driver *drv =
		to_ps3_vuart_port_driver(_dev->driver);

	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);

	BUG_ON(!drv);

Geoff Levand's avatar
Geoff Levand committed
842 843 844 845 846 847
	down(&vuart_bus_priv.probe_mutex);

	/* Setup vuart_bus_priv.devices[]. */

	result = ps3_vuart_match_id_to_port(dev->match_id,
		&port_number);
848 849 850 851 852 853 854 855

	if (result) {
		dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
			__func__, __LINE__, dev->match_id);
		result = -EINVAL;
		goto fail_match;
	}

Geoff Levand's avatar
Geoff Levand committed
856
	if (vuart_bus_priv.devices[port_number]) {
857
		dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
Geoff Levand's avatar
Geoff Levand committed
858
			__LINE__, port_number);
859 860 861 862
		result = -EBUSY;
		goto fail_match;
	}

Geoff Levand's avatar
Geoff Levand committed
863 864 865 866 867 868 869 870 871 872 873 874
	vuart_bus_priv.devices[port_number] = dev;

	/* Setup dev->priv. */

	dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);

	if (!dev->priv) {
		result = -ENOMEM;
		goto fail_alloc;
	}

	dev->priv->port_number = port_number;
875

Geoff Levand's avatar
Geoff Levand committed
876 877 878 879 880 881
	INIT_LIST_HEAD(&dev->priv->tx_list.head);
	spin_lock_init(&dev->priv->tx_list.lock);

	INIT_LIST_HEAD(&dev->priv->rx_list.head);
	spin_lock_init(&dev->priv->rx_list.lock);

882 883 884 885 886
	INIT_WORK(&dev->priv->work.work, NULL);
	spin_lock_init(&dev->priv->work.lock);
	dev->priv->work.trigger = 0;
	dev->priv->work.dev = dev;

Geoff Levand's avatar
Geoff Levand committed
887
	if (++vuart_bus_priv.use_count == 1) {
888

889
		result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY,
Geoff Levand's avatar
Geoff Levand committed
890
			(void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
891 892 893

		if (result) {
			dev_dbg(&dev->core,
894
				"%s:%d: ps3_vuart_irq_setup failed (%d)\n",
895 896 897 898 899
				__func__, __LINE__, result);
			result = -EPERM;
			goto fail_alloc_irq;
		}

Geoff Levand's avatar
Geoff Levand committed
900 901
		result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
			IRQF_DISABLED, "vuart", &vuart_bus_priv);
902 903 904 905 906 907 908 909 910

		if (result) {
			dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
				__func__, __LINE__, result);
			goto fail_request_irq;
		}
	}

	/* clear stale pending interrupts */
Geoff Levand's avatar
Geoff Levand committed
911 912 913 914

	ps3_vuart_clear_rx_bytes(dev, 0);

	ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
915 916 917 918 919 920 921 922 923 924 925 926 927 928

	ps3_vuart_set_triggers(dev, 1, 1);

	if (drv->probe)
		result = drv->probe(dev);
	else {
		result = 0;
		dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
			__LINE__);
	}

	if (result) {
		dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
			__func__, __LINE__);
Geoff Levand's avatar
Geoff Levand committed
929
		down(&vuart_bus_priv.probe_mutex);
930 931 932
		goto fail_probe;
	}

Geoff Levand's avatar
Geoff Levand committed
933 934
	up(&vuart_bus_priv.probe_mutex);

935 936 937
	return result;

fail_probe:
Geoff Levand's avatar
Geoff Levand committed
938
	ps3_vuart_set_interrupt_mask(dev, 0);
939
fail_request_irq:
940
	ps3_vuart_irq_destroy(vuart_bus_priv.virq);
Geoff Levand's avatar
Geoff Levand committed
941
	vuart_bus_priv.virq = NO_IRQ;
942
fail_alloc_irq:
Geoff Levand's avatar
Geoff Levand committed
943 944 945 946
	--vuart_bus_priv.use_count;
	kfree(dev->priv);
	dev->priv = NULL;
fail_alloc:
Al Viro's avatar
Al Viro committed
947
	vuart_bus_priv.devices[port_number] = NULL;
948
fail_match:
Geoff Levand's avatar
Geoff Levand committed
949
	up(&vuart_bus_priv.probe_mutex);
950 951 952 953 954 955 956 957 958 959
	dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
	return result;
}

static int ps3_vuart_remove(struct device *_dev)
{
	struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
	struct ps3_vuart_port_driver *drv =
		to_ps3_vuart_port_driver(_dev->driver);

Geoff Levand's avatar
Geoff Levand committed
960 961
	down(&vuart_bus_priv.probe_mutex);

962 963 964
	dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
		dev->core.bus_id);

Geoff Levand's avatar
Geoff Levand committed
965
	BUG_ON(vuart_bus_priv.use_count < 1);
966 967 968 969 970 971 972

	if (drv->remove)
		drv->remove(dev);
	else
		dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
			__LINE__, dev->core.bus_id);

Al Viro's avatar
Al Viro committed
973
	vuart_bus_priv.devices[dev->priv->port_number] = NULL;
974

Geoff Levand's avatar
Geoff Levand committed
975 976 977
	if (--vuart_bus_priv.use_count == 0) {
		BUG();
		free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
978
		ps3_vuart_irq_destroy(vuart_bus_priv.virq);
Geoff Levand's avatar
Geoff Levand committed
979
		vuart_bus_priv.virq = NO_IRQ;
980
	}
Geoff Levand's avatar
Geoff Levand committed
981 982 983 984 985

	kfree(dev->priv);
	dev->priv = NULL;

	up(&vuart_bus_priv.probe_mutex);
986 987 988
	return 0;
}

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
static void ps3_vuart_shutdown(struct device *_dev)
{
	struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
	struct ps3_vuart_port_driver *drv =
		to_ps3_vuart_port_driver(_dev->driver);

	dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
		dev->core.bus_id);

	if (drv->shutdown)
		drv->shutdown(dev);
	else
		dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__,
			__LINE__, dev->core.bus_id);
}

1005
/**
Geoff Levand's avatar
Geoff Levand committed
1006
 * ps3_vuart_bus - The vuart bus instance.
1007 1008 1009 1010
 *
 * The vuart is managed as a bus that port devices connect to.
 */

Geoff Levand's avatar
Geoff Levand committed
1011
struct bus_type ps3_vuart_bus = {
1012 1013 1014 1015
        .name = "ps3_vuart",
	.match = ps3_vuart_match,
	.probe = ps3_vuart_probe,
	.remove = ps3_vuart_remove,
1016
	.shutdown = ps3_vuart_shutdown,
1017 1018
};

Geoff Levand's avatar
Geoff Levand committed
1019
int __init ps3_vuart_bus_init(void)
1020 1021 1022 1023
{
	int result;

	pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand's avatar
Geoff Levand committed
1024 1025

	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1026
		return -ENODEV;
Geoff Levand's avatar
Geoff Levand committed
1027 1028 1029

	init_MUTEX(&vuart_bus_priv.probe_mutex);
	result = bus_register(&ps3_vuart_bus);
1030
	BUG_ON(result);
Geoff Levand's avatar
Geoff Levand committed
1031

1032 1033 1034
	return result;
}

Geoff Levand's avatar
Geoff Levand committed
1035
void __exit ps3_vuart_bus_exit(void)
1036 1037
{
	pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand's avatar
Geoff Levand committed
1038
	bus_unregister(&ps3_vuart_bus);
1039 1040
}

Geoff Levand's avatar
Geoff Levand committed
1041 1042
core_initcall(ps3_vuart_bus_init);
module_exit(ps3_vuart_bus_exit);
1043 1044 1045 1046 1047 1048 1049 1050

/**
 * ps3_vuart_port_release_device - Remove a vuart port device.
 */

static void ps3_vuart_port_release_device(struct device *_dev)
{
#if defined(DEBUG)
Geoff Levand's avatar
Geoff Levand committed
1051 1052 1053 1054 1055 1056
	struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);

	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);

	BUG_ON(dev->priv && "forgot to free");
	memset(&dev->core, 0, sizeof(dev->core));
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
#endif
}

/**
 * ps3_vuart_port_device_register - Add a vuart port device.
 */

int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
{
	static unsigned int dev_count = 1;

Geoff Levand's avatar
Geoff Levand committed
1068 1069
	BUG_ON(dev->priv && "forgot to free");

1070
	dev->core.parent = NULL;
Geoff Levand's avatar
Geoff Levand committed
1071
	dev->core.bus = &ps3_vuart_bus;
1072 1073 1074 1075 1076 1077 1078
	dev->core.release = ps3_vuart_port_release_device;

	snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
		dev_count++);

	dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);

Geoff Levand's avatar
Geoff Levand committed
1079
	return device_register(&dev->core);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
}

EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);

/**
 * ps3_vuart_port_driver_register - Add a vuart port device driver.
 */

int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
{
	int result;

	pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
Geoff Levand's avatar
Geoff Levand committed
1093
	drv->core.bus = &ps3_vuart_bus;
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	result = driver_register(&drv->core);
	return result;
}

EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);

/**
 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
 */

void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
{
Geoff Levand's avatar
Geoff Levand committed
1106
	pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
1107 1108 1109 1110
	driver_unregister(&drv->core);
}

EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);