4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
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.
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.
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
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/workqueue.h>
26 #include <linux/bitops.h>
29 #include <asm/firmware.h>
30 #include <asm/lv1call.h>
34 MODULE_AUTHOR("Sony Corporation");
35 MODULE_LICENSE("GPL v2");
36 MODULE_DESCRIPTION("PS3 vuart");
39 * vuart - An inter-partition data link service.
40 * port 0: PS3 AV Settings.
41 * port 2: PS3 System Manager.
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.
49 enum {PORT_COUNT
= 3,};
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 */
62 enum vuart_interrupt_bit
{
65 INTERRUPT_BIT_DISCONNECT
= 2,
68 enum vuart_interrupt_mask
{
69 INTERRUPT_MASK_TX
= 1,
70 INTERRUPT_MASK_RX
= 2,
71 INTERRUPT_MASK_DISCONNECT
= 4,
75 * struct ps3_vuart_port_priv - private vuart device data.
78 struct ps3_vuart_port_priv
{
83 struct list_head head
;
86 struct ps3_vuart_work work
;
87 unsigned long bytes_held
;
89 struct list_head head
;
91 struct ps3_vuart_stats stats
;
94 static struct ps3_vuart_port_priv
*to_port_priv(
95 struct ps3_system_bus_device
*dev
)
98 BUG_ON(!dev
->driver_priv
);
99 return (struct ps3_vuart_port_priv
*)dev
->driver_priv
;
103 * struct ports_bmp - bitmap indicating ports needing service.
105 * A 256 bit read only bitmap indicating ports needing service. Do not write
106 * to these bits. Must not cross a page boundary.
112 } __attribute__((aligned(32)));
114 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
115 static void __maybe_unused
_dump_ports_bmp(
116 const struct ports_bmp
*bmp
, const char *func
, int line
)
118 pr_debug("%s:%d: ports_bmp: %016llxh\n", func
, line
, bmp
->status
);
121 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
122 static void __maybe_unused
_dump_port_params(unsigned int port_number
,
123 const char *func
, int line
)
126 static const char *strings
[] = {
140 for (i
= 0; i
< ARRAY_SIZE(strings
); i
++) {
141 result
= lv1_get_virtual_uart_param(port_number
, i
, &value
);
144 pr_debug("%s:%d: port_%u: %s failed: %s\n", func
, line
,
145 port_number
, strings
[i
], ps3_result(result
));
148 pr_debug("%s:%d: port_%u: %s = %lxh\n",
149 func
, line
, port_number
, strings
[i
], value
);
154 struct vuart_triggers
{
159 int ps3_vuart_get_triggers(struct ps3_system_bus_device
*dev
,
160 struct vuart_triggers
*trig
)
167 result
= lv1_get_virtual_uart_param(dev
->port_number
,
168 PARAM_TX_TRIGGER
, &tx
);
172 dev_dbg(&dev
->core
, "%s:%d: tx_trigger failed: %s\n",
173 __func__
, __LINE__
, ps3_result(result
));
177 result
= lv1_get_virtual_uart_param(dev
->port_number
,
178 PARAM_RX_BUF_SIZE
, &size
);
181 dev_dbg(&dev
->core
, "%s:%d: tx_buf_size failed: %s\n",
182 __func__
, __LINE__
, ps3_result(result
));
186 result
= lv1_get_virtual_uart_param(dev
->port_number
,
187 PARAM_RX_TRIGGER
, &val
);
190 dev_dbg(&dev
->core
, "%s:%d: rx_trigger failed: %s\n",
191 __func__
, __LINE__
, ps3_result(result
));
195 trig
->rx
= size
- val
;
197 dev_dbg(&dev
->core
, "%s:%d: tx %lxh, rx %lxh\n", __func__
, __LINE__
,
203 int ps3_vuart_set_triggers(struct ps3_system_bus_device
*dev
, unsigned int tx
,
209 result
= lv1_set_virtual_uart_param(dev
->port_number
,
210 PARAM_TX_TRIGGER
, tx
);
213 dev_dbg(&dev
->core
, "%s:%d: tx_trigger failed: %s\n",
214 __func__
, __LINE__
, ps3_result(result
));
218 result
= lv1_get_virtual_uart_param(dev
->port_number
,
219 PARAM_RX_BUF_SIZE
, &size
);
222 dev_dbg(&dev
->core
, "%s:%d: tx_buf_size failed: %s\n",
223 __func__
, __LINE__
, ps3_result(result
));
227 result
= lv1_set_virtual_uart_param(dev
->port_number
,
228 PARAM_RX_TRIGGER
, size
- rx
);
231 dev_dbg(&dev
->core
, "%s:%d: rx_trigger failed: %s\n",
232 __func__
, __LINE__
, ps3_result(result
));
236 dev_dbg(&dev
->core
, "%s:%d: tx %xh, rx %xh\n", __func__
, __LINE__
,
242 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device
*dev
,
247 result
= lv1_get_virtual_uart_param(dev
->port_number
,
248 PARAM_RX_BYTES
, bytes_waiting
);
251 dev_dbg(&dev
->core
, "%s:%d: rx_bytes failed: %s\n",
252 __func__
, __LINE__
, ps3_result(result
));
254 dev_dbg(&dev
->core
, "%s:%d: %llxh\n", __func__
, __LINE__
,
260 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
261 * @dev: The struct ps3_system_bus_device instance.
262 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
265 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device
*dev
,
269 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
271 dev_dbg(&dev
->core
, "%s:%d: %lxh\n", __func__
, __LINE__
, mask
);
273 priv
->interrupt_mask
= mask
;
275 result
= lv1_set_virtual_uart_param(dev
->port_number
,
276 PARAM_INTERRUPT_MASK
, priv
->interrupt_mask
);
279 dev_dbg(&dev
->core
, "%s:%d: interrupt_mask failed: %s\n",
280 __func__
, __LINE__
, ps3_result(result
));
285 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device
*dev
,
286 unsigned long *status
)
289 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
292 result
= lv1_get_virtual_uart_param(dev
->port_number
,
293 PARAM_INTERRUPT_STATUS
, &tmp
);
296 dev_dbg(&dev
->core
, "%s:%d: interrupt_status failed: %s\n",
297 __func__
, __LINE__
, ps3_result(result
));
299 *status
= tmp
& priv
->interrupt_mask
;
301 dev_dbg(&dev
->core
, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
302 __func__
, __LINE__
, priv
->interrupt_mask
, tmp
, *status
);
307 int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device
*dev
)
309 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
311 return (priv
->interrupt_mask
& INTERRUPT_MASK_TX
) ? 0
312 : ps3_vuart_set_interrupt_mask(dev
, priv
->interrupt_mask
313 | INTERRUPT_MASK_TX
);
316 int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device
*dev
)
318 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
320 return (priv
->interrupt_mask
& INTERRUPT_MASK_RX
) ? 0
321 : ps3_vuart_set_interrupt_mask(dev
, priv
->interrupt_mask
322 | INTERRUPT_MASK_RX
);
325 int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device
*dev
)
327 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
329 return (priv
->interrupt_mask
& INTERRUPT_MASK_DISCONNECT
) ? 0
330 : ps3_vuart_set_interrupt_mask(dev
, priv
->interrupt_mask
331 | INTERRUPT_MASK_DISCONNECT
);
334 int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device
*dev
)
336 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
338 return (priv
->interrupt_mask
& INTERRUPT_MASK_TX
)
339 ? ps3_vuart_set_interrupt_mask(dev
, priv
->interrupt_mask
340 & ~INTERRUPT_MASK_TX
) : 0;
343 int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device
*dev
)
345 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
347 return (priv
->interrupt_mask
& INTERRUPT_MASK_RX
)
348 ? ps3_vuart_set_interrupt_mask(dev
, priv
->interrupt_mask
349 & ~INTERRUPT_MASK_RX
) : 0;
352 int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device
*dev
)
354 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
356 return (priv
->interrupt_mask
& INTERRUPT_MASK_DISCONNECT
)
357 ? ps3_vuart_set_interrupt_mask(dev
, priv
->interrupt_mask
358 & ~INTERRUPT_MASK_DISCONNECT
) : 0;
362 * ps3_vuart_raw_write - Low level write helper.
363 * @dev: The struct ps3_system_bus_device instance.
365 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
368 static int ps3_vuart_raw_write(struct ps3_system_bus_device
*dev
,
369 const void *buf
, unsigned int bytes
, u64
*bytes_written
)
372 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
374 result
= lv1_write_virtual_uart(dev
->port_number
,
375 ps3_mm_phys_to_lpar(__pa(buf
)), bytes
, bytes_written
);
378 dev_dbg(&dev
->core
, "%s:%d: lv1_write_virtual_uart failed: "
379 "%s\n", __func__
, __LINE__
, ps3_result(result
));
383 priv
->stats
.bytes_written
+= *bytes_written
;
385 dev_dbg(&dev
->core
, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__
, __LINE__
,
386 *bytes_written
, bytes
, priv
->stats
.bytes_written
);
392 * ps3_vuart_raw_read - Low level read helper.
393 * @dev: The struct ps3_system_bus_device instance.
395 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
398 static int ps3_vuart_raw_read(struct ps3_system_bus_device
*dev
, void *buf
,
399 unsigned int bytes
, u64
*bytes_read
)
402 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
404 dev_dbg(&dev
->core
, "%s:%d: %xh\n", __func__
, __LINE__
, bytes
);
406 result
= lv1_read_virtual_uart(dev
->port_number
,
407 ps3_mm_phys_to_lpar(__pa(buf
)), bytes
, bytes_read
);
410 dev_dbg(&dev
->core
, "%s:%d: lv1_read_virtual_uart failed: %s\n",
411 __func__
, __LINE__
, ps3_result(result
));
415 priv
->stats
.bytes_read
+= *bytes_read
;
417 dev_dbg(&dev
->core
, "%s:%d: read %llxh/%xh=>%lxh\n", __func__
, __LINE__
,
418 *bytes_read
, bytes
, priv
->stats
.bytes_read
);
424 * ps3_vuart_clear_rx_bytes - Discard bytes received.
425 * @dev: The struct ps3_system_bus_device instance.
426 * @bytes: Max byte count to discard, zero = all pending.
428 * Used to clear pending rx interrupt source. Will not block.
431 void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device
*dev
,
435 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
439 result
= ps3_vuart_get_rx_bytes_waiting(dev
, &bytes_waiting
);
443 bytes
= bytes
? min(bytes
, (unsigned int)bytes_waiting
) : bytes_waiting
;
445 dev_dbg(&dev
->core
, "%s:%d: %u\n", __func__
, __LINE__
, bytes
);
450 /* Add some extra space for recently arrived data. */
454 tmp
= kmalloc(bytes
, GFP_KERNEL
);
459 ps3_vuart_raw_read(dev
, tmp
, bytes
, &bytes_waiting
);
463 /* Don't include these bytes in the stats. */
465 priv
->stats
.bytes_read
-= bytes_waiting
;
467 EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes
);
470 * struct list_buffer - An element for a port device fifo buffer list.
474 struct list_head link
;
475 const unsigned char *head
;
476 const unsigned char *tail
;
477 unsigned long dbg_number
;
478 unsigned char data
[];
482 * ps3_vuart_write - the entry point for writing data to a port
483 * @dev: The struct ps3_system_bus_device instance.
485 * If the port is idle on entry as much of the incoming data is written to
486 * the port as the port will accept. Otherwise a list buffer is created
487 * and any remaning incoming data is copied to that buffer. The buffer is
488 * then enqueued for transmision via the transmit interrupt.
491 int ps3_vuart_write(struct ps3_system_bus_device
*dev
, const void *buf
,
494 static unsigned long dbg_number
;
496 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
498 struct list_buffer
*lb
;
500 dev_dbg(&dev
->core
, "%s:%d: %u(%xh) bytes\n", __func__
, __LINE__
,
503 spin_lock_irqsave(&priv
->tx_list
.lock
, flags
);
505 if (list_empty(&priv
->tx_list
.head
)) {
508 result
= ps3_vuart_raw_write(dev
, buf
, bytes
, &bytes_written
);
510 spin_unlock_irqrestore(&priv
->tx_list
.lock
, flags
);
514 "%s:%d: ps3_vuart_raw_write failed\n",
519 if (bytes_written
== bytes
) {
520 dev_dbg(&dev
->core
, "%s:%d: wrote %xh bytes\n",
521 __func__
, __LINE__
, bytes
);
525 bytes
-= bytes_written
;
526 buf
+= bytes_written
;
528 spin_unlock_irqrestore(&priv
->tx_list
.lock
, flags
);
530 lb
= kmalloc(sizeof(struct list_buffer
) + bytes
, GFP_KERNEL
);
535 memcpy(lb
->data
, buf
, bytes
);
537 lb
->tail
= lb
->data
+ bytes
;
538 lb
->dbg_number
= ++dbg_number
;
540 spin_lock_irqsave(&priv
->tx_list
.lock
, flags
);
541 list_add_tail(&lb
->link
, &priv
->tx_list
.head
);
542 ps3_vuart_enable_interrupt_tx(dev
);
543 spin_unlock_irqrestore(&priv
->tx_list
.lock
, flags
);
545 dev_dbg(&dev
->core
, "%s:%d: queued buf_%lu, %xh bytes\n",
546 __func__
, __LINE__
, lb
->dbg_number
, bytes
);
550 EXPORT_SYMBOL_GPL(ps3_vuart_write
);
553 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
554 * @dev: The struct ps3_system_bus_device instance.
555 * @bytes_queued: Number of bytes queued to the buffer list.
557 * Must be called with priv->rx_list.lock held.
560 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device
*dev
,
563 static unsigned long dbg_number
;
565 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
566 struct list_buffer
*lb
;
571 result
= ps3_vuart_get_rx_bytes_waiting(dev
, &bytes
);
580 /* Add some extra space for recently arrived data. */
584 lb
= kmalloc(sizeof(struct list_buffer
) + bytes
, GFP_ATOMIC
);
589 ps3_vuart_raw_read(dev
, lb
->data
, bytes
, &bytes
);
592 lb
->tail
= lb
->data
+ bytes
;
593 lb
->dbg_number
= ++dbg_number
;
595 list_add_tail(&lb
->link
, &priv
->rx_list
.head
);
596 priv
->rx_list
.bytes_held
+= bytes
;
598 dev_dbg(&dev
->core
, "%s:%d: buf_%lu: queued %llxh bytes\n",
599 __func__
, __LINE__
, lb
->dbg_number
, bytes
);
601 *bytes_queued
= bytes
;
607 * ps3_vuart_read - The entry point for reading data from a port.
609 * Queue data waiting at the port, and if enough bytes to satisfy the request
610 * are held in the buffer list those bytes are dequeued and copied to the
611 * caller's buffer. Emptied list buffers are retiered. If the request cannot
612 * be statified by bytes held in the list buffers -EAGAIN is returned.
615 int ps3_vuart_read(struct ps3_system_bus_device
*dev
, void *buf
,
619 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
621 struct list_buffer
*lb
, *n
;
622 unsigned long bytes_read
;
624 dev_dbg(&dev
->core
, "%s:%d: %u(%xh) bytes\n", __func__
, __LINE__
,
627 spin_lock_irqsave(&priv
->rx_list
.lock
, flags
);
629 /* Queue rx bytes here for polled reads. */
631 while (priv
->rx_list
.bytes_held
< bytes
) {
634 result
= ps3_vuart_queue_rx_bytes(dev
, &tmp
);
635 if (result
|| !tmp
) {
636 dev_dbg(&dev
->core
, "%s:%d: starved for %lxh bytes\n",
638 bytes
- priv
->rx_list
.bytes_held
);
639 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
644 list_for_each_entry_safe(lb
, n
, &priv
->rx_list
.head
, link
) {
645 bytes_read
= min((unsigned int)(lb
->tail
- lb
->head
), bytes
);
647 memcpy(buf
, lb
->head
, bytes_read
);
650 priv
->rx_list
.bytes_held
-= bytes_read
;
652 if (bytes_read
< lb
->tail
- lb
->head
) {
653 lb
->head
+= bytes_read
;
654 dev_dbg(&dev
->core
, "%s:%d: buf_%lu: dequeued %lxh "
655 "bytes\n", __func__
, __LINE__
, lb
->dbg_number
,
657 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
661 dev_dbg(&dev
->core
, "%s:%d: buf_%lu: free, dequeued %lxh "
662 "bytes\n", __func__
, __LINE__
, lb
->dbg_number
,
669 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
672 EXPORT_SYMBOL_GPL(ps3_vuart_read
);
675 * ps3_vuart_work - Asynchronous read handler.
678 static void ps3_vuart_work(struct work_struct
*work
)
680 struct ps3_system_bus_device
*dev
=
681 ps3_vuart_work_to_system_bus_dev(work
);
682 struct ps3_vuart_port_driver
*drv
=
683 ps3_system_bus_dev_to_vuart_drv(dev
);
689 int ps3_vuart_read_async(struct ps3_system_bus_device
*dev
, unsigned int bytes
)
691 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
694 if (priv
->rx_list
.work
.trigger
) {
695 dev_dbg(&dev
->core
, "%s:%d: warning, multiple calls\n",
702 PREPARE_WORK(&priv
->rx_list
.work
.work
, ps3_vuart_work
);
704 spin_lock_irqsave(&priv
->rx_list
.lock
, flags
);
705 if (priv
->rx_list
.bytes_held
>= bytes
) {
706 dev_dbg(&dev
->core
, "%s:%d: schedule_work %xh bytes\n",
707 __func__
, __LINE__
, bytes
);
708 schedule_work(&priv
->rx_list
.work
.work
);
709 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
713 priv
->rx_list
.work
.trigger
= bytes
;
714 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
716 dev_dbg(&dev
->core
, "%s:%d: waiting for %u(%xh) bytes\n", __func__
,
717 __LINE__
, bytes
, bytes
);
721 EXPORT_SYMBOL_GPL(ps3_vuart_read_async
);
723 void ps3_vuart_cancel_async(struct ps3_system_bus_device
*dev
)
725 to_port_priv(dev
)->rx_list
.work
.trigger
= 0;
727 EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async
);
730 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
732 * Services the transmit interrupt for the port. Writes as much data from the
733 * buffer list as the port will accept. Retires any emptied list buffers and
734 * adjusts the final list buffer state for a partial write.
737 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device
*dev
)
740 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
742 struct list_buffer
*lb
, *n
;
743 unsigned long bytes_total
= 0;
745 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
747 spin_lock_irqsave(&priv
->tx_list
.lock
, flags
);
749 list_for_each_entry_safe(lb
, n
, &priv
->tx_list
.head
, link
) {
753 result
= ps3_vuart_raw_write(dev
, lb
->head
, lb
->tail
- lb
->head
,
758 "%s:%d: ps3_vuart_raw_write failed\n",
763 bytes_total
+= bytes_written
;
765 if (bytes_written
< lb
->tail
- lb
->head
) {
766 lb
->head
+= bytes_written
;
768 "%s:%d cleared buf_%lu, %llxh bytes\n",
769 __func__
, __LINE__
, lb
->dbg_number
,
774 dev_dbg(&dev
->core
, "%s:%d free buf_%lu\n", __func__
, __LINE__
,
781 ps3_vuart_disable_interrupt_tx(dev
);
783 spin_unlock_irqrestore(&priv
->tx_list
.lock
, flags
);
784 dev_dbg(&dev
->core
, "%s:%d wrote %lxh bytes total\n",
785 __func__
, __LINE__
, bytes_total
);
790 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
792 * Services the receive interrupt for the port. Creates a list buffer and
793 * copies all waiting port data to that buffer and enqueues the buffer in the
794 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
797 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device
*dev
)
800 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
804 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
806 spin_lock_irqsave(&priv
->rx_list
.lock
, flags
);
807 result
= ps3_vuart_queue_rx_bytes(dev
, &bytes
);
810 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
814 if (priv
->rx_list
.work
.trigger
&& priv
->rx_list
.bytes_held
815 >= priv
->rx_list
.work
.trigger
) {
816 dev_dbg(&dev
->core
, "%s:%d: schedule_work %lxh bytes\n",
817 __func__
, __LINE__
, priv
->rx_list
.work
.trigger
);
818 priv
->rx_list
.work
.trigger
= 0;
819 schedule_work(&priv
->rx_list
.work
.work
);
822 spin_unlock_irqrestore(&priv
->rx_list
.lock
, flags
);
826 static int ps3_vuart_handle_interrupt_disconnect(
827 struct ps3_system_bus_device
*dev
)
829 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
830 BUG_ON("no support");
835 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
837 * Services any pending interrupt types for the port. Passes control to the
838 * third stage type specific interrupt handler. Returns control to the first
839 * stage handler after one iteration.
842 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device
*dev
)
845 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
846 unsigned long status
;
848 result
= ps3_vuart_get_interrupt_status(dev
, &status
);
853 dev_dbg(&dev
->core
, "%s:%d: status: %lxh\n", __func__
, __LINE__
,
856 if (status
& INTERRUPT_MASK_DISCONNECT
) {
857 priv
->stats
.disconnect_interrupts
++;
858 result
= ps3_vuart_handle_interrupt_disconnect(dev
);
860 ps3_vuart_disable_interrupt_disconnect(dev
);
863 if (status
& INTERRUPT_MASK_TX
) {
864 priv
->stats
.tx_interrupts
++;
865 result
= ps3_vuart_handle_interrupt_tx(dev
);
867 ps3_vuart_disable_interrupt_tx(dev
);
870 if (status
& INTERRUPT_MASK_RX
) {
871 priv
->stats
.rx_interrupts
++;
872 result
= ps3_vuart_handle_interrupt_rx(dev
);
874 ps3_vuart_disable_interrupt_rx(dev
);
880 struct vuart_bus_priv
{
881 struct ports_bmp
*bmp
;
883 struct mutex probe_mutex
;
885 struct ps3_system_bus_device
*devices
[PORT_COUNT
];
886 } static vuart_bus_priv
;
889 * ps3_vuart_irq_handler - first stage interrupt handler
891 * Loops finding any interrupting port and its associated instance data.
892 * Passes control to the second stage port specific interrupt handler. Loops
893 * until all outstanding interrupts are serviced.
896 static irqreturn_t
ps3_vuart_irq_handler(int irq
, void *_private
)
898 struct vuart_bus_priv
*bus_priv
= _private
;
905 dump_ports_bmp(bus_priv
->bmp
);
907 port
= (BITS_PER_LONG
- 1) - __ilog2(bus_priv
->bmp
->status
);
909 if (port
== BITS_PER_LONG
)
912 BUG_ON(port
>= PORT_COUNT
);
913 BUG_ON(!bus_priv
->devices
[port
]);
915 ps3_vuart_handle_port_interrupt(bus_priv
->devices
[port
]);
921 static int ps3_vuart_bus_interrupt_get(void)
925 pr_debug(" -> %s:%d\n", __func__
, __LINE__
);
927 vuart_bus_priv
.use_count
++;
929 BUG_ON(vuart_bus_priv
.use_count
> 2);
931 if (vuart_bus_priv
.use_count
!= 1)
934 BUG_ON(vuart_bus_priv
.bmp
);
936 vuart_bus_priv
.bmp
= kzalloc(sizeof(struct ports_bmp
), GFP_KERNEL
);
938 if (!vuart_bus_priv
.bmp
) {
939 pr_debug("%s:%d: kzalloc failed.\n", __func__
, __LINE__
);
941 goto fail_bmp_malloc
;
944 result
= ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY
, vuart_bus_priv
.bmp
,
945 &vuart_bus_priv
.virq
);
948 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
949 __func__
, __LINE__
, result
);
954 result
= request_irq(vuart_bus_priv
.virq
, ps3_vuart_irq_handler
,
955 IRQF_DISABLED
, "vuart", &vuart_bus_priv
);
958 pr_debug("%s:%d: request_irq failed (%d)\n",
959 __func__
, __LINE__
, result
);
960 goto fail_request_irq
;
963 pr_debug(" <- %s:%d: ok\n", __func__
, __LINE__
);
967 ps3_vuart_irq_destroy(vuart_bus_priv
.virq
);
968 vuart_bus_priv
.virq
= NO_IRQ
;
970 kfree(vuart_bus_priv
.bmp
);
971 vuart_bus_priv
.bmp
= NULL
;
973 vuart_bus_priv
.use_count
--;
974 pr_debug(" <- %s:%d: failed\n", __func__
, __LINE__
);
978 static int ps3_vuart_bus_interrupt_put(void)
980 pr_debug(" -> %s:%d\n", __func__
, __LINE__
);
982 vuart_bus_priv
.use_count
--;
984 BUG_ON(vuart_bus_priv
.use_count
< 0);
986 if (vuart_bus_priv
.use_count
!= 0)
989 free_irq(vuart_bus_priv
.virq
, &vuart_bus_priv
);
991 ps3_vuart_irq_destroy(vuart_bus_priv
.virq
);
992 vuart_bus_priv
.virq
= NO_IRQ
;
994 kfree(vuart_bus_priv
.bmp
);
995 vuart_bus_priv
.bmp
= NULL
;
997 pr_debug(" <- %s:%d\n", __func__
, __LINE__
);
1001 static int ps3_vuart_probe(struct ps3_system_bus_device
*dev
)
1004 struct ps3_vuart_port_driver
*drv
;
1005 struct ps3_vuart_port_priv
*priv
= NULL
;
1007 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
1009 drv
= ps3_system_bus_dev_to_vuart_drv(dev
);
1011 dev_dbg(&dev
->core
, "%s:%d: (%s)\n", __func__
, __LINE__
,
1012 drv
->core
.core
.name
);
1016 if (dev
->port_number
>= PORT_COUNT
) {
1021 mutex_lock(&vuart_bus_priv
.probe_mutex
);
1023 result
= ps3_vuart_bus_interrupt_get();
1026 goto fail_setup_interrupt
;
1028 if (vuart_bus_priv
.devices
[dev
->port_number
]) {
1029 dev_dbg(&dev
->core
, "%s:%d: port busy (%d)\n", __func__
,
1030 __LINE__
, dev
->port_number
);
1035 vuart_bus_priv
.devices
[dev
->port_number
] = dev
;
1037 /* Setup dev->driver_priv. */
1039 dev
->driver_priv
= kzalloc(sizeof(struct ps3_vuart_port_priv
),
1042 if (!dev
->driver_priv
) {
1044 goto fail_dev_malloc
;
1047 priv
= to_port_priv(dev
);
1049 INIT_LIST_HEAD(&priv
->tx_list
.head
);
1050 spin_lock_init(&priv
->tx_list
.lock
);
1052 INIT_LIST_HEAD(&priv
->rx_list
.head
);
1053 spin_lock_init(&priv
->rx_list
.lock
);
1055 INIT_WORK(&priv
->rx_list
.work
.work
, NULL
);
1056 priv
->rx_list
.work
.trigger
= 0;
1057 priv
->rx_list
.work
.dev
= dev
;
1059 /* clear stale pending interrupts */
1061 ps3_vuart_clear_rx_bytes(dev
, 0);
1063 ps3_vuart_set_interrupt_mask(dev
, INTERRUPT_MASK_RX
);
1065 ps3_vuart_set_triggers(dev
, 1, 1);
1068 result
= drv
->probe(dev
);
1071 dev_info(&dev
->core
, "%s:%d: no probe method\n", __func__
,
1076 dev_dbg(&dev
->core
, "%s:%d: drv->probe failed\n",
1077 __func__
, __LINE__
);
1081 mutex_unlock(&vuart_bus_priv
.probe_mutex
);
1086 ps3_vuart_set_interrupt_mask(dev
, 0);
1087 kfree(dev
->driver_priv
);
1088 dev
->driver_priv
= NULL
;
1090 vuart_bus_priv
.devices
[dev
->port_number
] = NULL
;
1092 ps3_vuart_bus_interrupt_put();
1093 fail_setup_interrupt
:
1094 mutex_unlock(&vuart_bus_priv
.probe_mutex
);
1095 dev_dbg(&dev
->core
, "%s:%d: failed\n", __func__
, __LINE__
);
1100 * ps3_vuart_cleanup - common cleanup helper.
1101 * @dev: The struct ps3_system_bus_device instance.
1103 * Cleans interrupts and HV resources. Must be called with
1104 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1105 * ps3_vuart_shutdown. After this call, polled reading will still work.
1108 static int ps3_vuart_cleanup(struct ps3_system_bus_device
*dev
)
1110 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
1112 ps3_vuart_cancel_async(dev
);
1113 ps3_vuart_set_interrupt_mask(dev
, 0);
1114 ps3_vuart_bus_interrupt_put();
1119 * ps3_vuart_remove - Completely clean the device instance.
1120 * @dev: The struct ps3_system_bus_device instance.
1122 * Cleans all memory, interrupts and HV resources. After this call the
1123 * device can no longer be used.
1126 static int ps3_vuart_remove(struct ps3_system_bus_device
*dev
)
1128 struct ps3_vuart_port_priv
*priv
= to_port_priv(dev
);
1129 struct ps3_vuart_port_driver
*drv
;
1133 mutex_lock(&vuart_bus_priv
.probe_mutex
);
1135 dev_dbg(&dev
->core
, " -> %s:%d: match_id %d\n", __func__
, __LINE__
,
1138 if (!dev
->core
.driver
) {
1139 dev_dbg(&dev
->core
, "%s:%d: no driver bound\n", __func__
,
1141 mutex_unlock(&vuart_bus_priv
.probe_mutex
);
1145 drv
= ps3_system_bus_dev_to_vuart_drv(dev
);
1152 dev_dbg(&dev
->core
, "%s:%d: no remove method\n", __func__
,
1157 ps3_vuart_cleanup(dev
);
1159 vuart_bus_priv
.devices
[dev
->port_number
] = NULL
;
1163 dev_dbg(&dev
->core
, " <- %s:%d\n", __func__
, __LINE__
);
1164 mutex_unlock(&vuart_bus_priv
.probe_mutex
);
1169 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1170 * @dev: The struct ps3_system_bus_device instance.
1172 * Cleans interrupts and HV resources. After this call the
1173 * device can still be used in polling mode. This behavior required
1174 * by sys-manager to be able to complete the device power operation
1178 static int ps3_vuart_shutdown(struct ps3_system_bus_device
*dev
)
1180 struct ps3_vuart_port_driver
*drv
;
1184 mutex_lock(&vuart_bus_priv
.probe_mutex
);
1186 dev_dbg(&dev
->core
, " -> %s:%d: match_id %d\n", __func__
, __LINE__
,
1189 if (!dev
->core
.driver
) {
1190 dev_dbg(&dev
->core
, "%s:%d: no driver bound\n", __func__
,
1192 mutex_unlock(&vuart_bus_priv
.probe_mutex
);
1196 drv
= ps3_system_bus_dev_to_vuart_drv(dev
);
1202 else if (drv
->remove
) {
1203 dev_dbg(&dev
->core
, "%s:%d: no shutdown, calling remove\n",
1204 __func__
, __LINE__
);
1207 dev_dbg(&dev
->core
, "%s:%d: no shutdown method\n", __func__
,
1212 ps3_vuart_cleanup(dev
);
1214 dev_dbg(&dev
->core
, " <- %s:%d\n", __func__
, __LINE__
);
1216 mutex_unlock(&vuart_bus_priv
.probe_mutex
);
1220 static int __init
ps3_vuart_bus_init(void)
1222 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1224 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
1227 mutex_init(&vuart_bus_priv
.probe_mutex
);
1232 static void __exit
ps3_vuart_bus_exit(void)
1234 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1237 core_initcall(ps3_vuart_bus_init
);
1238 module_exit(ps3_vuart_bus_exit
);
1241 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1244 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver
*drv
)
1248 pr_debug("%s:%d: (%s)\n", __func__
, __LINE__
, drv
->core
.core
.name
);
1250 BUG_ON(!drv
->core
.match_id
);
1251 BUG_ON(!drv
->core
.core
.name
);
1253 drv
->core
.probe
= ps3_vuart_probe
;
1254 drv
->core
.remove
= ps3_vuart_remove
;
1255 drv
->core
.shutdown
= ps3_vuart_shutdown
;
1257 result
= ps3_system_bus_driver_register(&drv
->core
);
1260 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register
);
1263 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1266 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver
*drv
)
1268 pr_debug("%s:%d: (%s)\n", __func__
, __LINE__
, drv
->core
.core
.name
);
1269 ps3_system_bus_driver_unregister(&drv
->core
);
1271 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister
);