Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-block.git] / drivers / ps3 / ps3-vuart.c
CommitLineData
74e95d5d
GL
1/*
2 * PS3 virtual uart
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
5a0e3ad6 22#include <linux/slab.h>
74e95d5d
GL
23#include <linux/module.h>
24#include <linux/interrupt.h>
ea1547d3 25#include <linux/workqueue.h>
1977f032 26#include <linux/bitops.h>
74e95d5d
GL
27#include <asm/ps3.h>
28
75c86e74 29#include <asm/firmware.h>
74e95d5d 30#include <asm/lv1call.h>
74e95d5d
GL
31
32#include "vuart.h"
33
34MODULE_AUTHOR("Sony Corporation");
35MODULE_LICENSE("GPL v2");
75c86e74 36MODULE_DESCRIPTION("PS3 vuart");
74e95d5d
GL
37
38/**
39 * vuart - An inter-partition data link service.
40 * port 0: PS3 AV Settings.
41 * port 2: PS3 System Manager.
42 *
43 * The vuart provides a bi-directional byte stream data link between logical
44 * partitions. Its primary role is as a communications link between the guest
45 * OS and the system policy module. The current HV does not support any
46 * connections other than those listed.
47 */
48
49enum {PORT_COUNT = 3,};
50
51enum vuart_param {
52 PARAM_TX_TRIGGER = 0,
53 PARAM_RX_TRIGGER = 1,
54 PARAM_INTERRUPT_MASK = 2,
55 PARAM_RX_BUF_SIZE = 3, /* read only */
56 PARAM_RX_BYTES = 4, /* read only */
57 PARAM_TX_BUF_SIZE = 5, /* read only */
58 PARAM_TX_BYTES = 6, /* read only */
59 PARAM_INTERRUPT_STATUS = 7, /* read only */
60};
61
62enum vuart_interrupt_bit {
63 INTERRUPT_BIT_TX = 0,
64 INTERRUPT_BIT_RX = 1,
65 INTERRUPT_BIT_DISCONNECT = 2,
66};
67
68enum vuart_interrupt_mask {
69 INTERRUPT_MASK_TX = 1,
70 INTERRUPT_MASK_RX = 2,
71 INTERRUPT_MASK_DISCONNECT = 4,
72};
73
7626e78d
GL
74/**
75 * struct ps3_vuart_port_priv - private vuart device data.
76 */
77
78struct ps3_vuart_port_priv {
79 u64 interrupt_mask;
80
81 struct {
82 spinlock_t lock;
83 struct list_head head;
84 } tx_list;
85 struct {
86 struct ps3_vuart_work work;
87 unsigned long bytes_held;
88 spinlock_t lock;
89 struct list_head head;
90 } rx_list;
91 struct ps3_vuart_stats stats;
92};
93
94static struct ps3_vuart_port_priv *to_port_priv(
95 struct ps3_system_bus_device *dev)
96{
97 BUG_ON(!dev);
98 BUG_ON(!dev->driver_priv);
99 return (struct ps3_vuart_port_priv *)dev->driver_priv;
100}
101
74e95d5d
GL
102/**
103 * struct ports_bmp - bitmap indicating ports needing service.
104 *
105 * A 256 bit read only bitmap indicating ports needing service. Do not write
106 * to these bits. Must not cross a page boundary.
107 */
108
109struct ports_bmp {
110 u64 status;
111 u64 unused[3];
d0e5c218 112} __attribute__((aligned(32)));
74e95d5d 113
74e95d5d 114#define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
848cfdc5 115static void __maybe_unused _dump_ports_bmp(
d0e5c218 116 const struct ports_bmp *bmp, const char *func, int line)
74e95d5d 117{
a9dad6e5 118 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
74e95d5d
GL
119}
120
74e95d5d 121#define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
848cfdc5 122static void __maybe_unused _dump_port_params(unsigned int port_number,
d0e5c218 123 const char *func, int line)
74e95d5d
GL
124{
125#if defined(DEBUG)
126 static const char *strings[] = {
127 "tx_trigger ",
128 "rx_trigger ",
129 "interrupt_mask ",
130 "rx_buf_size ",
131 "rx_bytes ",
132 "tx_buf_size ",
133 "tx_bytes ",
134 "interrupt_status",
135 };
136 int result;
137 unsigned int i;
138 u64 value;
139
140 for (i = 0; i < ARRAY_SIZE(strings); i++) {
141 result = lv1_get_virtual_uart_param(port_number, i, &value);
142
143 if (result) {
144 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
145 port_number, strings[i], ps3_result(result));
146 continue;
147 }
148 pr_debug("%s:%d: port_%u: %s = %lxh\n",
149 func, line, port_number, strings[i], value);
150 }
151#endif
152}
153
7626e78d 154int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
74e95d5d
GL
155 struct vuart_triggers *trig)
156{
157 int result;
b17b3df1
SR
158 u64 size;
159 u64 val;
160 u64 tx;
74e95d5d 161
7626e78d 162 result = lv1_get_virtual_uart_param(dev->port_number,
b17b3df1
SR
163 PARAM_TX_TRIGGER, &tx);
164 trig->tx = tx;
74e95d5d
GL
165
166 if (result) {
167 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
168 __func__, __LINE__, ps3_result(result));
169 return result;
170 }
171
7626e78d 172 result = lv1_get_virtual_uart_param(dev->port_number,
74e95d5d
GL
173 PARAM_RX_BUF_SIZE, &size);
174
175 if (result) {
176 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
177 __func__, __LINE__, ps3_result(result));
178 return result;
179 }
180
7626e78d 181 result = lv1_get_virtual_uart_param(dev->port_number,
74e95d5d
GL
182 PARAM_RX_TRIGGER, &val);
183
184 if (result) {
185 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
186 __func__, __LINE__, ps3_result(result));
187 return result;
188 }
189
190 trig->rx = size - val;
191
192 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
193 trig->tx, trig->rx);
194
195 return result;
196}
197
7626e78d 198int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
74e95d5d
GL
199 unsigned int rx)
200{
201 int result;
b17b3df1 202 u64 size;
74e95d5d 203
7626e78d 204 result = lv1_set_virtual_uart_param(dev->port_number,
74e95d5d
GL
205 PARAM_TX_TRIGGER, tx);
206
207 if (result) {
208 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
209 __func__, __LINE__, ps3_result(result));
210 return result;
211 }
212
7626e78d 213 result = lv1_get_virtual_uart_param(dev->port_number,
74e95d5d
GL
214 PARAM_RX_BUF_SIZE, &size);
215
216 if (result) {
217 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
218 __func__, __LINE__, ps3_result(result));
219 return result;
220 }
221
7626e78d 222 result = lv1_set_virtual_uart_param(dev->port_number,
74e95d5d
GL
223 PARAM_RX_TRIGGER, size - rx);
224
225 if (result) {
226 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
227 __func__, __LINE__, ps3_result(result));
228 return result;
229 }
230
231 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
232 tx, rx);
233
234 return result;
235}
236
7626e78d 237static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
75c86e74 238 u64 *bytes_waiting)
74e95d5d 239{
7626e78d
GL
240 int result;
241
242 result = lv1_get_virtual_uart_param(dev->port_number,
74e95d5d
GL
243 PARAM_RX_BYTES, bytes_waiting);
244
245 if (result)
246 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
247 __func__, __LINE__, ps3_result(result));
248
a9dad6e5 249 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
74e95d5d
GL
250 *bytes_waiting);
251 return result;
252}
253
7626e78d
GL
254/**
255 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
256 * @dev: The struct ps3_system_bus_device instance.
257 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
258 */
259
260static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
74e95d5d
GL
261 unsigned long mask)
262{
263 int result;
7626e78d 264 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d
GL
265
266 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
267
7626e78d 268 priv->interrupt_mask = mask;
74e95d5d 269
7626e78d
GL
270 result = lv1_set_virtual_uart_param(dev->port_number,
271 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
74e95d5d
GL
272
273 if (result)
274 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
275 __func__, __LINE__, ps3_result(result));
276
277 return result;
278}
279
7626e78d 280static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
74e95d5d
GL
281 unsigned long *status)
282{
7626e78d
GL
283 int result;
284 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
75c86e74 285 u64 tmp;
7626e78d
GL
286
287 result = lv1_get_virtual_uart_param(dev->port_number,
75c86e74 288 PARAM_INTERRUPT_STATUS, &tmp);
74e95d5d
GL
289
290 if (result)
291 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
292 __func__, __LINE__, ps3_result(result));
293
7626e78d 294 *status = tmp & priv->interrupt_mask;
75c86e74 295
a9dad6e5 296 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
7626e78d 297 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
74e95d5d
GL
298
299 return result;
300}
301
7626e78d 302int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
74e95d5d 303{
7626e78d
GL
304 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
305
306 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
307 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
74e95d5d
GL
308 | INTERRUPT_MASK_TX);
309}
310
7626e78d 311int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
74e95d5d 312{
7626e78d
GL
313 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
314
315 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
316 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
74e95d5d
GL
317 | INTERRUPT_MASK_RX);
318}
319
7626e78d 320int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
74e95d5d 321{
7626e78d
GL
322 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
323
324 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
325 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
74e95d5d
GL
326 | INTERRUPT_MASK_DISCONNECT);
327}
328
7626e78d 329int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
74e95d5d 330{
7626e78d
GL
331 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
332
333 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
334 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
74e95d5d
GL
335 & ~INTERRUPT_MASK_TX) : 0;
336}
337
7626e78d 338int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
74e95d5d 339{
7626e78d
GL
340 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
341
342 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
343 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
74e95d5d
GL
344 & ~INTERRUPT_MASK_RX) : 0;
345}
346
7626e78d 347int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
74e95d5d 348{
7626e78d
GL
349 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
350
351 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
352 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
74e95d5d
GL
353 & ~INTERRUPT_MASK_DISCONNECT) : 0;
354}
355
356/**
357 * ps3_vuart_raw_write - Low level write helper.
7626e78d 358 * @dev: The struct ps3_system_bus_device instance.
74e95d5d
GL
359 *
360 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
361 */
362
7626e78d 363static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
b17b3df1 364 const void *buf, unsigned int bytes, u64 *bytes_written)
74e95d5d
GL
365{
366 int result;
7626e78d 367 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d 368
7626e78d 369 result = lv1_write_virtual_uart(dev->port_number,
74e95d5d
GL
370 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
371
372 if (result) {
373 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
374 "%s\n", __func__, __LINE__, ps3_result(result));
375 return result;
376 }
377
7626e78d 378 priv->stats.bytes_written += *bytes_written;
74e95d5d 379
b17b3df1 380 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
7626e78d 381 *bytes_written, bytes, priv->stats.bytes_written);
74e95d5d
GL
382
383 return result;
384}
385
386/**
387 * ps3_vuart_raw_read - Low level read helper.
7626e78d 388 * @dev: The struct ps3_system_bus_device instance.
74e95d5d
GL
389 *
390 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
391 */
392
7626e78d 393static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
b17b3df1 394 unsigned int bytes, u64 *bytes_read)
74e95d5d
GL
395{
396 int result;
7626e78d 397 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d
GL
398
399 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
400
7626e78d 401 result = lv1_read_virtual_uart(dev->port_number,
74e95d5d
GL
402 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
403
404 if (result) {
405 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
406 __func__, __LINE__, ps3_result(result));
407 return result;
408 }
409
7626e78d 410 priv->stats.bytes_read += *bytes_read;
74e95d5d 411
b17b3df1 412 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
7626e78d 413 *bytes_read, bytes, priv->stats.bytes_read);
74e95d5d
GL
414
415 return result;
416}
417
75c86e74
GL
418/**
419 * ps3_vuart_clear_rx_bytes - Discard bytes received.
7626e78d 420 * @dev: The struct ps3_system_bus_device instance.
75c86e74
GL
421 * @bytes: Max byte count to discard, zero = all pending.
422 *
423 * Used to clear pending rx interrupt source. Will not block.
424 */
425
7626e78d 426void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
75c86e74
GL
427 unsigned int bytes)
428{
429 int result;
7626e78d 430 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
75c86e74 431 u64 bytes_waiting;
d0e5c218 432 void *tmp;
75c86e74
GL
433
434 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
435
436 BUG_ON(result);
437
438 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
439
440 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
441
442 if (!bytes)
443 return;
444
445 /* Add some extra space for recently arrived data. */
446
447 bytes += 128;
448
449 tmp = kmalloc(bytes, GFP_KERNEL);
450
451 if (!tmp)
452 return;
453
454 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
455
456 kfree(tmp);
457
458 /* Don't include these bytes in the stats. */
459
7626e78d 460 priv->stats.bytes_read -= bytes_waiting;
75c86e74 461}
7626e78d 462EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
75c86e74 463
74e95d5d
GL
464/**
465 * struct list_buffer - An element for a port device fifo buffer list.
466 */
467
468struct list_buffer {
469 struct list_head link;
470 const unsigned char *head;
471 const unsigned char *tail;
472 unsigned long dbg_number;
473 unsigned char data[];
474};
475
476/**
477 * ps3_vuart_write - the entry point for writing data to a port
7626e78d 478 * @dev: The struct ps3_system_bus_device instance.
74e95d5d
GL
479 *
480 * If the port is idle on entry as much of the incoming data is written to
481 * the port as the port will accept. Otherwise a list buffer is created
482 * and any remaning incoming data is copied to that buffer. The buffer is
483 * then enqueued for transmision via the transmit interrupt.
484 */
485
7626e78d 486int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
74e95d5d
GL
487 unsigned int bytes)
488{
489 static unsigned long dbg_number;
490 int result;
7626e78d 491 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d
GL
492 unsigned long flags;
493 struct list_buffer *lb;
494
495 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
496 bytes, bytes);
497
7626e78d 498 spin_lock_irqsave(&priv->tx_list.lock, flags);
74e95d5d 499
7626e78d 500 if (list_empty(&priv->tx_list.head)) {
b17b3df1 501 u64 bytes_written;
74e95d5d
GL
502
503 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
504
7626e78d 505 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
74e95d5d
GL
506
507 if (result) {
508 dev_dbg(&dev->core,
509 "%s:%d: ps3_vuart_raw_write failed\n",
510 __func__, __LINE__);
511 return result;
512 }
513
514 if (bytes_written == bytes) {
515 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
516 __func__, __LINE__, bytes);
517 return 0;
518 }
519
520 bytes -= bytes_written;
521 buf += bytes_written;
522 } else
7626e78d 523 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
74e95d5d
GL
524
525 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
526
d0e5c218 527 if (!lb)
74e95d5d 528 return -ENOMEM;
74e95d5d
GL
529
530 memcpy(lb->data, buf, bytes);
531 lb->head = lb->data;
532 lb->tail = lb->data + bytes;
533 lb->dbg_number = ++dbg_number;
534
7626e78d
GL
535 spin_lock_irqsave(&priv->tx_list.lock, flags);
536 list_add_tail(&lb->link, &priv->tx_list.head);
74e95d5d 537 ps3_vuart_enable_interrupt_tx(dev);
7626e78d 538 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
74e95d5d
GL
539
540 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
541 __func__, __LINE__, lb->dbg_number, bytes);
542
543 return 0;
544}
7626e78d
GL
545EXPORT_SYMBOL_GPL(ps3_vuart_write);
546
547/**
548 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
549 * @dev: The struct ps3_system_bus_device instance.
550 * @bytes_queued: Number of bytes queued to the buffer list.
551 *
552 * Must be called with priv->rx_list.lock held.
553 */
554
555static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
556 u64 *bytes_queued)
557{
558 static unsigned long dbg_number;
559 int result;
560 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
561 struct list_buffer *lb;
562 u64 bytes;
563
564 *bytes_queued = 0;
565
566 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
567 BUG_ON(result);
568
569 if (result)
570 return -EIO;
571
572 if (!bytes)
573 return 0;
574
575 /* Add some extra space for recently arrived data. */
576
577 bytes += 128;
578
579 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
580
581 if (!lb)
582 return -ENOMEM;
583
584 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
585
586 lb->head = lb->data;
587 lb->tail = lb->data + bytes;
588 lb->dbg_number = ++dbg_number;
589
590 list_add_tail(&lb->link, &priv->rx_list.head);
591 priv->rx_list.bytes_held += bytes;
592
a9dad6e5 593 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
7626e78d
GL
594 __func__, __LINE__, lb->dbg_number, bytes);
595
596 *bytes_queued = bytes;
597
598 return 0;
599}
74e95d5d
GL
600
601/**
7626e78d 602 * ps3_vuart_read - The entry point for reading data from a port.
74e95d5d 603 *
7626e78d
GL
604 * Queue data waiting at the port, and if enough bytes to satisfy the request
605 * are held in the buffer list those bytes are dequeued and copied to the
606 * caller's buffer. Emptied list buffers are retiered. If the request cannot
607 * be statified by bytes held in the list buffers -EAGAIN is returned.
74e95d5d
GL
608 */
609
7626e78d 610int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
74e95d5d
GL
611 unsigned int bytes)
612{
7626e78d
GL
613 int result;
614 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d
GL
615 unsigned long flags;
616 struct list_buffer *lb, *n;
617 unsigned long bytes_read;
618
619 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
620 bytes, bytes);
621
7626e78d 622 spin_lock_irqsave(&priv->rx_list.lock, flags);
74e95d5d 623
7626e78d
GL
624 /* Queue rx bytes here for polled reads. */
625
626 while (priv->rx_list.bytes_held < bytes) {
627 u64 tmp;
628
629 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
630 if (result || !tmp) {
631 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
632 __func__, __LINE__,
633 bytes - priv->rx_list.bytes_held);
634 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
635 return -EAGAIN;
636 }
74e95d5d
GL
637 }
638
7626e78d 639 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
74e95d5d
GL
640 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
641
642 memcpy(buf, lb->head, bytes_read);
643 buf += bytes_read;
644 bytes -= bytes_read;
7626e78d 645 priv->rx_list.bytes_held -= bytes_read;
74e95d5d
GL
646
647 if (bytes_read < lb->tail - lb->head) {
648 lb->head += bytes_read;
75c86e74
GL
649 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
650 "bytes\n", __func__, __LINE__, lb->dbg_number,
651 bytes_read);
7626e78d 652 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
74e95d5d
GL
653 return 0;
654 }
655
75c86e74
GL
656 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
657 "bytes\n", __func__, __LINE__, lb->dbg_number,
658 bytes_read);
74e95d5d
GL
659
660 list_del(&lb->link);
661 kfree(lb);
662 }
74e95d5d 663
7626e78d 664 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
74e95d5d
GL
665 return 0;
666}
7626e78d 667EXPORT_SYMBOL_GPL(ps3_vuart_read);
74e95d5d 668
7626e78d
GL
669/**
670 * ps3_vuart_work - Asynchronous read handler.
671 */
672
673static void ps3_vuart_work(struct work_struct *work)
674{
675 struct ps3_system_bus_device *dev =
676 ps3_vuart_work_to_system_bus_dev(work);
677 struct ps3_vuart_port_driver *drv =
678 ps3_system_bus_dev_to_vuart_drv(dev);
679
680 BUG_ON(!drv);
681 drv->work(dev);
682}
683
684int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
ea1547d3 685{
7626e78d 686 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
ea1547d3
GL
687 unsigned long flags;
688
7626e78d 689 if (priv->rx_list.work.trigger) {
ea1547d3
GL
690 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
691 __func__, __LINE__);
692 return -EAGAIN;
693 }
694
695 BUG_ON(!bytes);
696
7626e78d
GL
697 spin_lock_irqsave(&priv->rx_list.lock, flags);
698 if (priv->rx_list.bytes_held >= bytes) {
ea1547d3
GL
699 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
700 __func__, __LINE__, bytes);
7626e78d
GL
701 schedule_work(&priv->rx_list.work.work);
702 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
ea1547d3
GL
703 return 0;
704 }
705
7626e78d
GL
706 priv->rx_list.work.trigger = bytes;
707 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
ea1547d3
GL
708
709 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
710 __LINE__, bytes, bytes);
711
712 return 0;
713}
7626e78d 714EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
ea1547d3 715
7626e78d 716void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
ea1547d3 717{
7626e78d 718 to_port_priv(dev)->rx_list.work.trigger = 0;
ea1547d3 719}
7626e78d 720EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
ea1547d3 721
74e95d5d
GL
722/**
723 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
724 *
725 * Services the transmit interrupt for the port. Writes as much data from the
726 * buffer list as the port will accept. Retires any emptied list buffers and
727 * adjusts the final list buffer state for a partial write.
728 */
729
7626e78d 730static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
74e95d5d
GL
731{
732 int result = 0;
7626e78d 733 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d
GL
734 unsigned long flags;
735 struct list_buffer *lb, *n;
736 unsigned long bytes_total = 0;
737
738 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
739
7626e78d 740 spin_lock_irqsave(&priv->tx_list.lock, flags);
74e95d5d 741
7626e78d 742 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
74e95d5d 743
b17b3df1 744 u64 bytes_written;
74e95d5d
GL
745
746 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
747 &bytes_written);
748
749 if (result) {
750 dev_dbg(&dev->core,
751 "%s:%d: ps3_vuart_raw_write failed\n",
752 __func__, __LINE__);
753 break;
754 }
755
756 bytes_total += bytes_written;
757
758 if (bytes_written < lb->tail - lb->head) {
759 lb->head += bytes_written;
760 dev_dbg(&dev->core,
b17b3df1 761 "%s:%d cleared buf_%lu, %llxh bytes\n",
74e95d5d
GL
762 __func__, __LINE__, lb->dbg_number,
763 bytes_written);
764 goto port_full;
765 }
766
767 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
768 lb->dbg_number);
769
770 list_del(&lb->link);
771 kfree(lb);
772 }
773
774 ps3_vuart_disable_interrupt_tx(dev);
775port_full:
7626e78d 776 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
74e95d5d
GL
777 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
778 __func__, __LINE__, bytes_total);
779 return result;
780}
781
782/**
783 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
784 *
785 * Services the receive interrupt for the port. Creates a list buffer and
786 * copies all waiting port data to that buffer and enqueues the buffer in the
787 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
788 */
789
7626e78d 790static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
74e95d5d 791{
7626e78d
GL
792 int result;
793 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d 794 unsigned long flags;
7626e78d 795 u64 bytes;
74e95d5d
GL
796
797 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
798
7626e78d
GL
799 spin_lock_irqsave(&priv->rx_list.lock, flags);
800 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
74e95d5d 801
7626e78d
GL
802 if (result) {
803 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
804 return result;
805 }
74e95d5d 806
7626e78d
GL
807 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
808 >= priv->rx_list.work.trigger) {
ea1547d3 809 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
7626e78d
GL
810 __func__, __LINE__, priv->rx_list.work.trigger);
811 priv->rx_list.work.trigger = 0;
812 schedule_work(&priv->rx_list.work.work);
ea1547d3 813 }
7626e78d
GL
814
815 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
816 return result;
74e95d5d
GL
817}
818
819static int ps3_vuart_handle_interrupt_disconnect(
7626e78d 820 struct ps3_system_bus_device *dev)
74e95d5d
GL
821{
822 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
823 BUG_ON("no support");
824 return -1;
825}
826
827/**
828 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
829 *
830 * Services any pending interrupt types for the port. Passes control to the
831 * third stage type specific interrupt handler. Returns control to the first
832 * stage handler after one iteration.
833 */
834
7626e78d 835static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
74e95d5d
GL
836{
837 int result;
7626e78d 838 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
74e95d5d
GL
839 unsigned long status;
840
75c86e74 841 result = ps3_vuart_get_interrupt_status(dev, &status);
74e95d5d
GL
842
843 if (result)
844 return result;
845
846 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
847 status);
848
849 if (status & INTERRUPT_MASK_DISCONNECT) {
7626e78d 850 priv->stats.disconnect_interrupts++;
74e95d5d
GL
851 result = ps3_vuart_handle_interrupt_disconnect(dev);
852 if (result)
853 ps3_vuart_disable_interrupt_disconnect(dev);
854 }
855
856 if (status & INTERRUPT_MASK_TX) {
7626e78d 857 priv->stats.tx_interrupts++;
74e95d5d
GL
858 result = ps3_vuart_handle_interrupt_tx(dev);
859 if (result)
860 ps3_vuart_disable_interrupt_tx(dev);
861 }
862
863 if (status & INTERRUPT_MASK_RX) {
7626e78d 864 priv->stats.rx_interrupts++;
74e95d5d
GL
865 result = ps3_vuart_handle_interrupt_rx(dev);
866 if (result)
867 ps3_vuart_disable_interrupt_rx(dev);
868 }
869
870 return 0;
871}
872
75c86e74 873struct vuart_bus_priv {
7626e78d 874 struct ports_bmp *bmp;
74e95d5d 875 unsigned int virq;
c4e6752d 876 struct mutex probe_mutex;
75c86e74 877 int use_count;
7626e78d 878 struct ps3_system_bus_device *devices[PORT_COUNT];
75c86e74 879} static vuart_bus_priv;
74e95d5d
GL
880
881/**
882 * ps3_vuart_irq_handler - first stage interrupt handler
883 *
884 * Loops finding any interrupting port and its associated instance data.
885 * Passes control to the second stage port specific interrupt handler. Loops
886 * until all outstanding interrupts are serviced.
887 */
888
889static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
890{
7626e78d 891 struct vuart_bus_priv *bus_priv = _private;
74e95d5d 892
7626e78d 893 BUG_ON(!bus_priv);
74e95d5d
GL
894
895 while (1) {
896 unsigned int port;
897
7626e78d 898 dump_ports_bmp(bus_priv->bmp);
74e95d5d 899
7626e78d 900 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
74e95d5d
GL
901
902 if (port == BITS_PER_LONG)
903 break;
904
905 BUG_ON(port >= PORT_COUNT);
75c86e74 906 BUG_ON(!bus_priv->devices[port]);
74e95d5d 907
75c86e74 908 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
74e95d5d
GL
909 }
910
911 return IRQ_HANDLED;
912}
913
7626e78d 914static int ps3_vuart_bus_interrupt_get(void)
74e95d5d
GL
915{
916 int result;
74e95d5d 917
7626e78d
GL
918 pr_debug(" -> %s:%d\n", __func__, __LINE__);
919
920 vuart_bus_priv.use_count++;
921
922 BUG_ON(vuart_bus_priv.use_count > 2);
923
d0e5c218 924 if (vuart_bus_priv.use_count != 1)
7626e78d 925 return 0;
7626e78d
GL
926
927 BUG_ON(vuart_bus_priv.bmp);
928
929 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
930
931 if (!vuart_bus_priv.bmp) {
932 pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
933 result = -ENOMEM;
934 goto fail_bmp_malloc;
935 }
936
937 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
938 &vuart_bus_priv.virq);
939
940 if (result) {
941 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
942 __func__, __LINE__, result);
943 result = -EPERM;
944 goto fail_alloc_irq;
945 }
946
947 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
6ea741a1 948 0, "vuart", &vuart_bus_priv);
74e95d5d 949
7626e78d
GL
950 if (result) {
951 pr_debug("%s:%d: request_irq failed (%d)\n",
952 __func__, __LINE__, result);
953 goto fail_request_irq;
954 }
74e95d5d 955
7626e78d 956 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
74e95d5d 957 return result;
7626e78d
GL
958
959fail_request_irq:
960 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
961 vuart_bus_priv.virq = NO_IRQ;
962fail_alloc_irq:
963 kfree(vuart_bus_priv.bmp);
964 vuart_bus_priv.bmp = NULL;
965fail_bmp_malloc:
966 vuart_bus_priv.use_count--;
967 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
968 return result;
969}
970
971static int ps3_vuart_bus_interrupt_put(void)
972{
973 pr_debug(" -> %s:%d\n", __func__, __LINE__);
974
975 vuart_bus_priv.use_count--;
976
977 BUG_ON(vuart_bus_priv.use_count < 0);
978
979 if (vuart_bus_priv.use_count != 0)
980 return 0;
981
982 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
983
984 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
985 vuart_bus_priv.virq = NO_IRQ;
986
987 kfree(vuart_bus_priv.bmp);
988 vuart_bus_priv.bmp = NULL;
989
990 pr_debug(" <- %s:%d\n", __func__, __LINE__);
991 return 0;
74e95d5d
GL
992}
993
7626e78d 994static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
74e95d5d
GL
995{
996 int result;
7626e78d
GL
997 struct ps3_vuart_port_driver *drv;
998 struct ps3_vuart_port_priv *priv = NULL;
74e95d5d
GL
999
1000 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1001
7626e78d
GL
1002 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1003
1004 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
1005 drv->core.core.name);
1006
74e95d5d
GL
1007 BUG_ON(!drv);
1008
7626e78d
GL
1009 if (dev->port_number >= PORT_COUNT) {
1010 BUG();
1011 return -EINVAL;
1012 }
75c86e74 1013
c4e6752d 1014 mutex_lock(&vuart_bus_priv.probe_mutex);
75c86e74 1015
7626e78d 1016 result = ps3_vuart_bus_interrupt_get();
74e95d5d 1017
7626e78d
GL
1018 if (result)
1019 goto fail_setup_interrupt;
74e95d5d 1020
7626e78d 1021 if (vuart_bus_priv.devices[dev->port_number]) {
74e95d5d 1022 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
7626e78d 1023 __LINE__, dev->port_number);
74e95d5d 1024 result = -EBUSY;
7626e78d 1025 goto fail_busy;
74e95d5d
GL
1026 }
1027
7626e78d 1028 vuart_bus_priv.devices[dev->port_number] = dev;
75c86e74 1029
7626e78d 1030 /* Setup dev->driver_priv. */
75c86e74 1031
7626e78d
GL
1032 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1033 GFP_KERNEL);
75c86e74 1034
7626e78d 1035 if (!dev->driver_priv) {
75c86e74 1036 result = -ENOMEM;
7626e78d 1037 goto fail_dev_malloc;
75c86e74
GL
1038 }
1039
7626e78d 1040 priv = to_port_priv(dev);
75c86e74 1041
7626e78d
GL
1042 INIT_LIST_HEAD(&priv->tx_list.head);
1043 spin_lock_init(&priv->tx_list.lock);
75c86e74 1044
7626e78d
GL
1045 INIT_LIST_HEAD(&priv->rx_list.head);
1046 spin_lock_init(&priv->rx_list.lock);
ea1547d3 1047
4a8bb7f5 1048 INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work);
7626e78d
GL
1049 priv->rx_list.work.trigger = 0;
1050 priv->rx_list.work.dev = dev;
74e95d5d 1051
74e95d5d 1052 /* clear stale pending interrupts */
75c86e74
GL
1053
1054 ps3_vuart_clear_rx_bytes(dev, 0);
1055
1056 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
74e95d5d
GL
1057
1058 ps3_vuart_set_triggers(dev, 1, 1);
1059
1060 if (drv->probe)
1061 result = drv->probe(dev);
1062 else {
1063 result = 0;
1064 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1065 __LINE__);
1066 }
1067
1068 if (result) {
1069 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1070 __func__, __LINE__);
1071 goto fail_probe;
1072 }
1073
c4e6752d 1074 mutex_unlock(&vuart_bus_priv.probe_mutex);
75c86e74 1075
74e95d5d
GL
1076 return result;
1077
1078fail_probe:
75c86e74 1079 ps3_vuart_set_interrupt_mask(dev, 0);
7626e78d
GL
1080 kfree(dev->driver_priv);
1081 dev->driver_priv = NULL;
1082fail_dev_malloc:
1083 vuart_bus_priv.devices[dev->port_number] = NULL;
1084fail_busy:
1085 ps3_vuart_bus_interrupt_put();
1086fail_setup_interrupt:
c4e6752d 1087 mutex_unlock(&vuart_bus_priv.probe_mutex);
7626e78d 1088 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
74e95d5d
GL
1089 return result;
1090}
1091
7626e78d
GL
1092/**
1093 * ps3_vuart_cleanup - common cleanup helper.
1094 * @dev: The struct ps3_system_bus_device instance.
1095 *
1096 * Cleans interrupts and HV resources. Must be called with
1097 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1098 * ps3_vuart_shutdown. After this call, polled reading will still work.
1099 */
1100
1101static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
74e95d5d 1102{
7626e78d
GL
1103 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1104
1105 ps3_vuart_cancel_async(dev);
1106 ps3_vuart_set_interrupt_mask(dev, 0);
1107 ps3_vuart_bus_interrupt_put();
1108 return 0;
1109}
1110
1111/**
1112 * ps3_vuart_remove - Completely clean the device instance.
1113 * @dev: The struct ps3_system_bus_device instance.
1114 *
1115 * Cleans all memory, interrupts and HV resources. After this call the
1116 * device can no longer be used.
1117 */
1118
1119static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
1120{
1121 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1122 struct ps3_vuart_port_driver *drv;
1123
1124 BUG_ON(!dev);
74e95d5d 1125
c4e6752d 1126 mutex_lock(&vuart_bus_priv.probe_mutex);
75c86e74 1127
7626e78d
GL
1128 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1129 dev->match_id);
74e95d5d 1130
7626e78d
GL
1131 if (!dev->core.driver) {
1132 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1133 __LINE__);
c4e6752d 1134 mutex_unlock(&vuart_bus_priv.probe_mutex);
7626e78d
GL
1135 return 0;
1136 }
74e95d5d 1137
7626e78d 1138 drv = ps3_system_bus_dev_to_vuart_drv(dev);
74e95d5d 1139
7626e78d 1140 BUG_ON(!drv);
74e95d5d 1141
7626e78d
GL
1142 if (drv->remove) {
1143 drv->remove(dev);
1144 } else {
1145 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1146 __LINE__);
75c86e74 1147 BUG();
74e95d5d 1148 }
75c86e74 1149
7626e78d
GL
1150 ps3_vuart_cleanup(dev);
1151
1152 vuart_bus_priv.devices[dev->port_number] = NULL;
1153 kfree(priv);
1154 priv = NULL;
75c86e74 1155
7626e78d 1156 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
c4e6752d 1157 mutex_unlock(&vuart_bus_priv.probe_mutex);
74e95d5d
GL
1158 return 0;
1159}
1160
1161/**
7626e78d
GL
1162 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1163 * @dev: The struct ps3_system_bus_device instance.
74e95d5d 1164 *
7626e78d
GL
1165 * Cleans interrupts and HV resources. After this call the
1166 * device can still be used in polling mode. This behavior required
1167 * by sys-manager to be able to complete the device power operation
1168 * sequence.
74e95d5d
GL
1169 */
1170
7626e78d 1171static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
74e95d5d 1172{
7626e78d 1173 struct ps3_vuart_port_driver *drv;
74e95d5d 1174
7626e78d 1175 BUG_ON(!dev);
75c86e74 1176
c4e6752d 1177 mutex_lock(&vuart_bus_priv.probe_mutex);
75c86e74 1178
7626e78d
GL
1179 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1180 dev->match_id);
75c86e74 1181
7626e78d
GL
1182 if (!dev->core.driver) {
1183 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1184 __LINE__);
c4e6752d 1185 mutex_unlock(&vuart_bus_priv.probe_mutex);
7626e78d
GL
1186 return 0;
1187 }
74e95d5d 1188
7626e78d 1189 drv = ps3_system_bus_dev_to_vuart_drv(dev);
74e95d5d 1190
7626e78d 1191 BUG_ON(!drv);
74e95d5d 1192
7626e78d
GL
1193 if (drv->shutdown)
1194 drv->shutdown(dev);
1195 else if (drv->remove) {
1196 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1197 __func__, __LINE__);
1198 drv->remove(dev);
1199 } else {
1200 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1201 __LINE__);
1202 BUG();
1203 }
74e95d5d 1204
7626e78d 1205 ps3_vuart_cleanup(dev);
75c86e74 1206
7626e78d 1207 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
75c86e74 1208
c4e6752d 1209 mutex_unlock(&vuart_bus_priv.probe_mutex);
7626e78d 1210 return 0;
74e95d5d
GL
1211}
1212
7626e78d 1213static int __init ps3_vuart_bus_init(void)
74e95d5d 1214{
7626e78d 1215 pr_debug("%s:%d:\n", __func__, __LINE__);
75c86e74 1216
7626e78d
GL
1217 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1218 return -ENODEV;
74e95d5d 1219
c4e6752d 1220 mutex_init(&vuart_bus_priv.probe_mutex);
74e95d5d 1221
7626e78d
GL
1222 return 0;
1223}
74e95d5d 1224
7626e78d
GL
1225static void __exit ps3_vuart_bus_exit(void)
1226{
1227 pr_debug("%s:%d:\n", __func__, __LINE__);
74e95d5d
GL
1228}
1229
7626e78d
GL
1230core_initcall(ps3_vuart_bus_init);
1231module_exit(ps3_vuart_bus_exit);
74e95d5d
GL
1232
1233/**
1234 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1235 */
1236
1237int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1238{
1239 int result;
1240
7626e78d
GL
1241 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1242
1243 BUG_ON(!drv->core.match_id);
1244 BUG_ON(!drv->core.core.name);
1245
1246 drv->core.probe = ps3_vuart_probe;
1247 drv->core.remove = ps3_vuart_remove;
1248 drv->core.shutdown = ps3_vuart_shutdown;
1249
1250 result = ps3_system_bus_driver_register(&drv->core);
74e95d5d
GL
1251 return result;
1252}
74e95d5d
GL
1253EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1254
1255/**
1256 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1257 */
1258
1259void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1260{
7626e78d
GL
1261 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1262 ps3_system_bus_driver_unregister(&drv->core);
74e95d5d 1263}
74e95d5d 1264EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);