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/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
27 #include <asm/firmware.h>
28 #include <asm/lv1call.h>
29 #include <asm/bitops.h>
33 MODULE_AUTHOR("Sony Corporation");
34 MODULE_LICENSE("GPL v2");
35 MODULE_DESCRIPTION("PS3 vuart");
38 * vuart - An inter-partition data link service.
39 * port 0: PS3 AV Settings.
40 * port 2: PS3 System Manager.
42 * The vuart provides a bi-directional byte stream data link between logical
43 * partitions. Its primary role is as a communications link between the guest
44 * OS and the system policy module. The current HV does not support any
45 * connections other than those listed.
48 enum {PORT_COUNT
= 3,};
53 PARAM_INTERRUPT_MASK
= 2,
54 PARAM_RX_BUF_SIZE
= 3, /* read only */
55 PARAM_RX_BYTES
= 4, /* read only */
56 PARAM_TX_BUF_SIZE
= 5, /* read only */
57 PARAM_TX_BYTES
= 6, /* read only */
58 PARAM_INTERRUPT_STATUS
= 7, /* read only */
61 enum vuart_interrupt_bit
{
64 INTERRUPT_BIT_DISCONNECT
= 2,
67 enum vuart_interrupt_mask
{
68 INTERRUPT_MASK_TX
= 1,
69 INTERRUPT_MASK_RX
= 2,
70 INTERRUPT_MASK_DISCONNECT
= 4,
74 * struct ports_bmp - bitmap indicating ports needing service.
76 * A 256 bit read only bitmap indicating ports needing service. Do not write
77 * to these bits. Must not cross a page boundary.
83 } __attribute__ ((aligned (32)));
85 /* redefine dev_dbg to do a syntax check */
89 static inline int __attribute__ ((format (printf
, 2, 3))) dev_dbg(
90 const struct device
*_dev
, const char *fmt
, ...) {return 0;}
93 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
94 static void __attribute__ ((unused
)) _dump_ports_bmp(
95 const struct ports_bmp
* bmp
, const char* func
, int line
)
97 pr_debug("%s:%d: ports_bmp: %016lxh\n", func
, line
, bmp
->status
);
100 static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id
,
101 unsigned int *port_number
)
104 case PS3_MATCH_ID_AV_SETTINGS
:
107 case PS3_MATCH_ID_SYSTEM_MANAGER
:
112 *port_number
= UINT_MAX
;
117 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
118 static void __attribute__ ((unused
)) _dump_port_params(unsigned int port_number
,
119 const char* func
, int line
)
122 static const char *strings
[] = {
136 for (i
= 0; i
< ARRAY_SIZE(strings
); i
++) {
137 result
= lv1_get_virtual_uart_param(port_number
, i
, &value
);
140 pr_debug("%s:%d: port_%u: %s failed: %s\n", func
, line
,
141 port_number
, strings
[i
], ps3_result(result
));
144 pr_debug("%s:%d: port_%u: %s = %lxh\n",
145 func
, line
, port_number
, strings
[i
], value
);
150 struct vuart_triggers
{
155 int ps3_vuart_get_triggers(struct ps3_vuart_port_device
*dev
,
156 struct vuart_triggers
*trig
)
162 result
= lv1_get_virtual_uart_param(dev
->priv
->port_number
,
163 PARAM_TX_TRIGGER
, &trig
->tx
);
166 dev_dbg(&dev
->core
, "%s:%d: tx_trigger failed: %s\n",
167 __func__
, __LINE__
, ps3_result(result
));
171 result
= lv1_get_virtual_uart_param(dev
->priv
->port_number
,
172 PARAM_RX_BUF_SIZE
, &size
);
175 dev_dbg(&dev
->core
, "%s:%d: tx_buf_size failed: %s\n",
176 __func__
, __LINE__
, ps3_result(result
));
180 result
= lv1_get_virtual_uart_param(dev
->priv
->port_number
,
181 PARAM_RX_TRIGGER
, &val
);
184 dev_dbg(&dev
->core
, "%s:%d: rx_trigger failed: %s\n",
185 __func__
, __LINE__
, ps3_result(result
));
189 trig
->rx
= size
- val
;
191 dev_dbg(&dev
->core
, "%s:%d: tx %lxh, rx %lxh\n", __func__
, __LINE__
,
197 int ps3_vuart_set_triggers(struct ps3_vuart_port_device
*dev
, unsigned int tx
,
203 result
= lv1_set_virtual_uart_param(dev
->priv
->port_number
,
204 PARAM_TX_TRIGGER
, tx
);
207 dev_dbg(&dev
->core
, "%s:%d: tx_trigger failed: %s\n",
208 __func__
, __LINE__
, ps3_result(result
));
212 result
= lv1_get_virtual_uart_param(dev
->priv
->port_number
,
213 PARAM_RX_BUF_SIZE
, &size
);
216 dev_dbg(&dev
->core
, "%s:%d: tx_buf_size failed: %s\n",
217 __func__
, __LINE__
, ps3_result(result
));
221 result
= lv1_set_virtual_uart_param(dev
->priv
->port_number
,
222 PARAM_RX_TRIGGER
, size
- rx
);
225 dev_dbg(&dev
->core
, "%s:%d: rx_trigger failed: %s\n",
226 __func__
, __LINE__
, ps3_result(result
));
230 dev_dbg(&dev
->core
, "%s:%d: tx %xh, rx %xh\n", __func__
, __LINE__
,
236 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device
*dev
,
239 int result
= lv1_get_virtual_uart_param(dev
->priv
->port_number
,
240 PARAM_RX_BYTES
, bytes_waiting
);
243 dev_dbg(&dev
->core
, "%s:%d: rx_bytes failed: %s\n",
244 __func__
, __LINE__
, ps3_result(result
));
246 dev_dbg(&dev
->core
, "%s:%d: %lxh\n", __func__
, __LINE__
,
251 static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device
*dev
,
256 dev_dbg(&dev
->core
, "%s:%d: %lxh\n", __func__
, __LINE__
, mask
);
258 dev
->priv
->interrupt_mask
= mask
;
260 result
= lv1_set_virtual_uart_param(dev
->priv
->port_number
,
261 PARAM_INTERRUPT_MASK
, dev
->priv
->interrupt_mask
);
264 dev_dbg(&dev
->core
, "%s:%d: interrupt_mask failed: %s\n",
265 __func__
, __LINE__
, ps3_result(result
));
270 static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device
*dev
,
271 unsigned long *status
)
274 int result
= lv1_get_virtual_uart_param(dev
->priv
->port_number
,
275 PARAM_INTERRUPT_STATUS
, &tmp
);
278 dev_dbg(&dev
->core
, "%s:%d: interrupt_status failed: %s\n",
279 __func__
, __LINE__
, ps3_result(result
));
281 *status
= tmp
& dev
->priv
->interrupt_mask
;
283 dev_dbg(&dev
->core
, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
284 __func__
, __LINE__
, dev
->priv
->interrupt_mask
, tmp
, *status
);
289 int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device
*dev
)
291 return (dev
->priv
->interrupt_mask
& INTERRUPT_MASK_TX
) ? 0
292 : ps3_vuart_set_interrupt_mask(dev
, dev
->priv
->interrupt_mask
293 | INTERRUPT_MASK_TX
);
296 int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device
*dev
)
298 return (dev
->priv
->interrupt_mask
& INTERRUPT_MASK_RX
) ? 0
299 : ps3_vuart_set_interrupt_mask(dev
, dev
->priv
->interrupt_mask
300 | INTERRUPT_MASK_RX
);
303 int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device
*dev
)
305 return (dev
->priv
->interrupt_mask
& INTERRUPT_MASK_DISCONNECT
) ? 0
306 : ps3_vuart_set_interrupt_mask(dev
, dev
->priv
->interrupt_mask
307 | INTERRUPT_MASK_DISCONNECT
);
310 int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device
*dev
)
312 return (dev
->priv
->interrupt_mask
& INTERRUPT_MASK_TX
)
313 ? ps3_vuart_set_interrupt_mask(dev
, dev
->priv
->interrupt_mask
314 & ~INTERRUPT_MASK_TX
) : 0;
317 int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device
*dev
)
319 return (dev
->priv
->interrupt_mask
& INTERRUPT_MASK_RX
)
320 ? ps3_vuart_set_interrupt_mask(dev
, dev
->priv
->interrupt_mask
321 & ~INTERRUPT_MASK_RX
) : 0;
324 int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device
*dev
)
326 return (dev
->priv
->interrupt_mask
& INTERRUPT_MASK_DISCONNECT
)
327 ? ps3_vuart_set_interrupt_mask(dev
, dev
->priv
->interrupt_mask
328 & ~INTERRUPT_MASK_DISCONNECT
) : 0;
332 * ps3_vuart_raw_write - Low level write helper.
334 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
337 static int ps3_vuart_raw_write(struct ps3_vuart_port_device
*dev
,
338 const void* buf
, unsigned int bytes
, unsigned long *bytes_written
)
342 result
= lv1_write_virtual_uart(dev
->priv
->port_number
,
343 ps3_mm_phys_to_lpar(__pa(buf
)), bytes
, bytes_written
);
346 dev_dbg(&dev
->core
, "%s:%d: lv1_write_virtual_uart failed: "
347 "%s\n", __func__
, __LINE__
, ps3_result(result
));
351 dev
->priv
->stats
.bytes_written
+= *bytes_written
;
353 dev_dbg(&dev
->core
, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__
, __LINE__
,
354 *bytes_written
, bytes
, dev
->priv
->stats
.bytes_written
);
360 * ps3_vuart_raw_read - Low level read helper.
362 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
365 static int ps3_vuart_raw_read(struct ps3_vuart_port_device
*dev
, void* buf
,
366 unsigned int bytes
, unsigned long *bytes_read
)
370 dev_dbg(&dev
->core
, "%s:%d: %xh\n", __func__
, __LINE__
, bytes
);
372 result
= lv1_read_virtual_uart(dev
->priv
->port_number
,
373 ps3_mm_phys_to_lpar(__pa(buf
)), bytes
, bytes_read
);
376 dev_dbg(&dev
->core
, "%s:%d: lv1_read_virtual_uart failed: %s\n",
377 __func__
, __LINE__
, ps3_result(result
));
381 dev
->priv
->stats
.bytes_read
+= *bytes_read
;
383 dev_dbg(&dev
->core
, "%s:%d: read %lxh/%xh=>%lxh\n", __func__
, __LINE__
,
384 *bytes_read
, bytes
, dev
->priv
->stats
.bytes_read
);
390 * ps3_vuart_clear_rx_bytes - Discard bytes received.
391 * @bytes: Max byte count to discard, zero = all pending.
393 * Used to clear pending rx interrupt source. Will not block.
396 void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device
*dev
,
403 result
= ps3_vuart_get_rx_bytes_waiting(dev
, &bytes_waiting
);
407 bytes
= bytes
? min(bytes
, (unsigned int)bytes_waiting
) : bytes_waiting
;
409 dev_dbg(&dev
->core
, "%s:%d: %u\n", __func__
, __LINE__
, bytes
);
414 /* Add some extra space for recently arrived data. */
418 tmp
= kmalloc(bytes
, GFP_KERNEL
);
423 ps3_vuart_raw_read(dev
, tmp
, bytes
, &bytes_waiting
);
427 /* Don't include these bytes in the stats. */
429 dev
->priv
->stats
.bytes_read
-= bytes_waiting
;
433 * struct list_buffer - An element for a port device fifo buffer list.
437 struct list_head link
;
438 const unsigned char *head
;
439 const unsigned char *tail
;
440 unsigned long dbg_number
;
441 unsigned char data
[];
445 * ps3_vuart_write - the entry point for writing data to a port
447 * If the port is idle on entry as much of the incoming data is written to
448 * the port as the port will accept. Otherwise a list buffer is created
449 * and any remaning incoming data is copied to that buffer. The buffer is
450 * then enqueued for transmision via the transmit interrupt.
453 int ps3_vuart_write(struct ps3_vuart_port_device
*dev
, const void* buf
,
456 static unsigned long dbg_number
;
459 struct list_buffer
*lb
;
461 dev_dbg(&dev
->core
, "%s:%d: %u(%xh) bytes\n", __func__
, __LINE__
,
464 spin_lock_irqsave(&dev
->priv
->tx_list
.lock
, flags
);
466 if (list_empty(&dev
->priv
->tx_list
.head
)) {
467 unsigned long bytes_written
;
469 result
= ps3_vuart_raw_write(dev
, buf
, bytes
, &bytes_written
);
471 spin_unlock_irqrestore(&dev
->priv
->tx_list
.lock
, flags
);
475 "%s:%d: ps3_vuart_raw_write failed\n",
480 if (bytes_written
== bytes
) {
481 dev_dbg(&dev
->core
, "%s:%d: wrote %xh bytes\n",
482 __func__
, __LINE__
, bytes
);
486 bytes
-= bytes_written
;
487 buf
+= bytes_written
;
489 spin_unlock_irqrestore(&dev
->priv
->tx_list
.lock
, flags
);
491 lb
= kmalloc(sizeof(struct list_buffer
) + bytes
, GFP_KERNEL
);
497 memcpy(lb
->data
, buf
, bytes
);
499 lb
->tail
= lb
->data
+ bytes
;
500 lb
->dbg_number
= ++dbg_number
;
502 spin_lock_irqsave(&dev
->priv
->tx_list
.lock
, flags
);
503 list_add_tail(&lb
->link
, &dev
->priv
->tx_list
.head
);
504 ps3_vuart_enable_interrupt_tx(dev
);
505 spin_unlock_irqrestore(&dev
->priv
->tx_list
.lock
, flags
);
507 dev_dbg(&dev
->core
, "%s:%d: queued buf_%lu, %xh bytes\n",
508 __func__
, __LINE__
, lb
->dbg_number
, bytes
);
514 * ps3_vuart_read - the entry point for reading data from a port
516 * If enough bytes to satisfy the request are held in the buffer list those
517 * bytes are dequeued and copied to the caller's buffer. Emptied list buffers
518 * are retiered. If the request cannot be statified by bytes held in the list
519 * buffers -EAGAIN is returned.
522 int ps3_vuart_read(struct ps3_vuart_port_device
*dev
, void* buf
,
526 struct list_buffer
*lb
, *n
;
527 unsigned long bytes_read
;
529 dev_dbg(&dev
->core
, "%s:%d: %u(%xh) bytes\n", __func__
, __LINE__
,
532 spin_lock_irqsave(&dev
->priv
->rx_list
.lock
, flags
);
534 if (dev
->priv
->rx_list
.bytes_held
< bytes
) {
535 spin_unlock_irqrestore(&dev
->priv
->rx_list
.lock
, flags
);
536 dev_dbg(&dev
->core
, "%s:%d: starved for %lxh bytes\n",
538 bytes
- dev
->priv
->rx_list
.bytes_held
);
542 list_for_each_entry_safe(lb
, n
, &dev
->priv
->rx_list
.head
, link
) {
543 bytes_read
= min((unsigned int)(lb
->tail
- lb
->head
), bytes
);
545 memcpy(buf
, lb
->head
, bytes_read
);
548 dev
->priv
->rx_list
.bytes_held
-= bytes_read
;
550 if (bytes_read
< lb
->tail
- lb
->head
) {
551 lb
->head
+= bytes_read
;
552 dev_dbg(&dev
->core
, "%s:%d: buf_%lu: dequeued %lxh "
553 "bytes\n", __func__
, __LINE__
, lb
->dbg_number
,
555 spin_unlock_irqrestore(&dev
->priv
->rx_list
.lock
, flags
);
559 dev_dbg(&dev
->core
, "%s:%d: buf_%lu: free, dequeued %lxh "
560 "bytes\n", __func__
, __LINE__
, lb
->dbg_number
,
567 spin_unlock_irqrestore(&dev
->priv
->rx_list
.lock
, flags
);
571 int ps3_vuart_read_async(struct ps3_vuart_port_device
*dev
, work_func_t func
,
576 if(dev
->priv
->work
.trigger
) {
577 dev_dbg(&dev
->core
, "%s:%d: warning, multiple calls\n",
584 PREPARE_WORK(&dev
->priv
->work
.work
, func
);
586 spin_lock_irqsave(&dev
->priv
->work
.lock
, flags
);
587 if(dev
->priv
->rx_list
.bytes_held
>= bytes
) {
588 dev_dbg(&dev
->core
, "%s:%d: schedule_work %xh bytes\n",
589 __func__
, __LINE__
, bytes
);
590 schedule_work(&dev
->priv
->work
.work
);
591 spin_unlock_irqrestore(&dev
->priv
->work
.lock
, flags
);
595 dev
->priv
->work
.trigger
= bytes
;
596 spin_unlock_irqrestore(&dev
->priv
->work
.lock
, flags
);
598 dev_dbg(&dev
->core
, "%s:%d: waiting for %u(%xh) bytes\n", __func__
,
599 __LINE__
, bytes
, bytes
);
604 void ps3_vuart_cancel_async(struct ps3_vuart_port_device
*dev
)
606 dev
->priv
->work
.trigger
= 0;
610 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
612 * Services the transmit interrupt for the port. Writes as much data from the
613 * buffer list as the port will accept. Retires any emptied list buffers and
614 * adjusts the final list buffer state for a partial write.
617 static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device
*dev
)
621 struct list_buffer
*lb
, *n
;
622 unsigned long bytes_total
= 0;
624 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
626 spin_lock_irqsave(&dev
->priv
->tx_list
.lock
, flags
);
628 list_for_each_entry_safe(lb
, n
, &dev
->priv
->tx_list
.head
, link
) {
630 unsigned long bytes_written
;
632 result
= ps3_vuart_raw_write(dev
, lb
->head
, lb
->tail
- lb
->head
,
637 "%s:%d: ps3_vuart_raw_write failed\n",
642 bytes_total
+= bytes_written
;
644 if (bytes_written
< lb
->tail
- lb
->head
) {
645 lb
->head
+= bytes_written
;
647 "%s:%d cleared buf_%lu, %lxh bytes\n",
648 __func__
, __LINE__
, lb
->dbg_number
,
653 dev_dbg(&dev
->core
, "%s:%d free buf_%lu\n", __func__
, __LINE__
,
660 ps3_vuart_disable_interrupt_tx(dev
);
662 spin_unlock_irqrestore(&dev
->priv
->tx_list
.lock
, flags
);
663 dev_dbg(&dev
->core
, "%s:%d wrote %lxh bytes total\n",
664 __func__
, __LINE__
, bytes_total
);
669 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
671 * Services the receive interrupt for the port. Creates a list buffer and
672 * copies all waiting port data to that buffer and enqueues the buffer in the
673 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
676 static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device
*dev
)
678 static unsigned long dbg_number
;
681 struct list_buffer
*lb
;
684 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
686 result
= ps3_vuart_get_rx_bytes_waiting(dev
, &bytes
);
693 /* Add some extra space for recently arrived data. */
697 lb
= kmalloc(sizeof(struct list_buffer
) + bytes
, GFP_ATOMIC
);
702 ps3_vuart_raw_read(dev
, lb
->data
, bytes
, &bytes
);
705 lb
->tail
= lb
->data
+ bytes
;
706 lb
->dbg_number
= ++dbg_number
;
708 spin_lock_irqsave(&dev
->priv
->rx_list
.lock
, flags
);
709 list_add_tail(&lb
->link
, &dev
->priv
->rx_list
.head
);
710 dev
->priv
->rx_list
.bytes_held
+= bytes
;
711 spin_unlock_irqrestore(&dev
->priv
->rx_list
.lock
, flags
);
713 dev_dbg(&dev
->core
, "%s:%d: buf_%lu: queued %lxh bytes\n",
714 __func__
, __LINE__
, lb
->dbg_number
, bytes
);
716 spin_lock_irqsave(&dev
->priv
->work
.lock
, flags
);
717 if(dev
->priv
->work
.trigger
718 && dev
->priv
->rx_list
.bytes_held
>= dev
->priv
->work
.trigger
) {
719 dev_dbg(&dev
->core
, "%s:%d: schedule_work %lxh bytes\n",
720 __func__
, __LINE__
, dev
->priv
->work
.trigger
);
721 dev
->priv
->work
.trigger
= 0;
722 schedule_work(&dev
->priv
->work
.work
);
724 spin_unlock_irqrestore(&dev
->priv
->work
.lock
, flags
);
728 static int ps3_vuart_handle_interrupt_disconnect(
729 struct ps3_vuart_port_device
*dev
)
731 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
732 BUG_ON("no support");
737 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
739 * Services any pending interrupt types for the port. Passes control to the
740 * third stage type specific interrupt handler. Returns control to the first
741 * stage handler after one iteration.
744 static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device
*dev
)
747 unsigned long status
;
749 result
= ps3_vuart_get_interrupt_status(dev
, &status
);
754 dev_dbg(&dev
->core
, "%s:%d: status: %lxh\n", __func__
, __LINE__
,
757 if (status
& INTERRUPT_MASK_DISCONNECT
) {
758 dev
->priv
->stats
.disconnect_interrupts
++;
759 result
= ps3_vuart_handle_interrupt_disconnect(dev
);
761 ps3_vuart_disable_interrupt_disconnect(dev
);
764 if (status
& INTERRUPT_MASK_TX
) {
765 dev
->priv
->stats
.tx_interrupts
++;
766 result
= ps3_vuart_handle_interrupt_tx(dev
);
768 ps3_vuart_disable_interrupt_tx(dev
);
771 if (status
& INTERRUPT_MASK_RX
) {
772 dev
->priv
->stats
.rx_interrupts
++;
773 result
= ps3_vuart_handle_interrupt_rx(dev
);
775 ps3_vuart_disable_interrupt_rx(dev
);
781 struct vuart_bus_priv
{
782 const struct ports_bmp bmp
;
784 struct semaphore probe_mutex
;
786 struct ps3_vuart_port_device
*devices
[PORT_COUNT
];
787 } static vuart_bus_priv
;
790 * ps3_vuart_irq_handler - first stage interrupt handler
792 * Loops finding any interrupting port and its associated instance data.
793 * Passes control to the second stage port specific interrupt handler. Loops
794 * until all outstanding interrupts are serviced.
797 static irqreturn_t
ps3_vuart_irq_handler(int irq
, void *_private
)
799 struct vuart_bus_priv
*bus_priv
;
802 bus_priv
= (struct vuart_bus_priv
*)_private
;
807 dump_ports_bmp(&bus_priv
->bmp
);
809 port
= (BITS_PER_LONG
- 1) - __ilog2(bus_priv
->bmp
.status
);
811 if (port
== BITS_PER_LONG
)
814 BUG_ON(port
>= PORT_COUNT
);
815 BUG_ON(!bus_priv
->devices
[port
]);
817 ps3_vuart_handle_port_interrupt(bus_priv
->devices
[port
]);
823 static int ps3_vuart_match(struct device
*_dev
, struct device_driver
*_drv
)
826 struct ps3_vuart_port_driver
*drv
= to_ps3_vuart_port_driver(_drv
);
827 struct ps3_vuart_port_device
*dev
= to_ps3_vuart_port_device(_dev
);
829 result
= dev
->match_id
== drv
->match_id
;
831 dev_info(&dev
->core
, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__
,
832 __LINE__
, dev
->match_id
, dev
->core
.bus_id
, drv
->match_id
,
833 drv
->core
.name
, (result
? "match" : "miss"));
838 static int ps3_vuart_probe(struct device
*_dev
)
841 unsigned int port_number
;
842 struct ps3_vuart_port_device
*dev
= to_ps3_vuart_port_device(_dev
);
843 struct ps3_vuart_port_driver
*drv
=
844 to_ps3_vuart_port_driver(_dev
->driver
);
846 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
850 down(&vuart_bus_priv
.probe_mutex
);
852 /* Setup vuart_bus_priv.devices[]. */
854 result
= ps3_vuart_match_id_to_port(dev
->match_id
,
858 dev_dbg(&dev
->core
, "%s:%d: unknown match_id (%d)\n",
859 __func__
, __LINE__
, dev
->match_id
);
864 if (vuart_bus_priv
.devices
[port_number
]) {
865 dev_dbg(&dev
->core
, "%s:%d: port busy (%d)\n", __func__
,
866 __LINE__
, port_number
);
871 vuart_bus_priv
.devices
[port_number
] = dev
;
873 /* Setup dev->priv. */
875 dev
->priv
= kzalloc(sizeof(struct ps3_vuart_port_priv
), GFP_KERNEL
);
882 dev
->priv
->port_number
= port_number
;
884 INIT_LIST_HEAD(&dev
->priv
->tx_list
.head
);
885 spin_lock_init(&dev
->priv
->tx_list
.lock
);
887 INIT_LIST_HEAD(&dev
->priv
->rx_list
.head
);
888 spin_lock_init(&dev
->priv
->rx_list
.lock
);
890 INIT_WORK(&dev
->priv
->work
.work
, NULL
);
891 spin_lock_init(&dev
->priv
->work
.lock
);
892 dev
->priv
->work
.trigger
= 0;
893 dev
->priv
->work
.dev
= dev
;
895 if (++vuart_bus_priv
.use_count
== 1) {
897 result
= ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY
,
898 (void*)&vuart_bus_priv
.bmp
.status
, &vuart_bus_priv
.virq
);
902 "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
903 __func__
, __LINE__
, result
);
908 result
= request_irq(vuart_bus_priv
.virq
, ps3_vuart_irq_handler
,
909 IRQF_DISABLED
, "vuart", &vuart_bus_priv
);
912 dev_info(&dev
->core
, "%s:%d: request_irq failed (%d)\n",
913 __func__
, __LINE__
, result
);
914 goto fail_request_irq
;
918 /* clear stale pending interrupts */
920 ps3_vuart_clear_rx_bytes(dev
, 0);
922 ps3_vuart_set_interrupt_mask(dev
, INTERRUPT_MASK_RX
);
924 ps3_vuart_set_triggers(dev
, 1, 1);
927 result
= drv
->probe(dev
);
930 dev_info(&dev
->core
, "%s:%d: no probe method\n", __func__
,
935 dev_dbg(&dev
->core
, "%s:%d: drv->probe failed\n",
937 down(&vuart_bus_priv
.probe_mutex
);
941 up(&vuart_bus_priv
.probe_mutex
);
946 ps3_vuart_set_interrupt_mask(dev
, 0);
948 ps3_free_vuart_irq(vuart_bus_priv
.virq
);
949 vuart_bus_priv
.virq
= NO_IRQ
;
951 --vuart_bus_priv
.use_count
;
955 vuart_bus_priv
.devices
[port_number
] = 0;
957 up(&vuart_bus_priv
.probe_mutex
);
958 dev_dbg(&dev
->core
, "%s:%d failed\n", __func__
, __LINE__
);
962 static int ps3_vuart_remove(struct device
*_dev
)
964 struct ps3_vuart_port_device
*dev
= to_ps3_vuart_port_device(_dev
);
965 struct ps3_vuart_port_driver
*drv
=
966 to_ps3_vuart_port_driver(_dev
->driver
);
968 down(&vuart_bus_priv
.probe_mutex
);
970 dev_dbg(&dev
->core
, "%s:%d: %s\n", __func__
, __LINE__
,
973 BUG_ON(vuart_bus_priv
.use_count
< 1);
978 dev_dbg(&dev
->core
, "%s:%d: %s no remove method\n", __func__
,
979 __LINE__
, dev
->core
.bus_id
);
981 vuart_bus_priv
.devices
[dev
->priv
->port_number
] = 0;
983 if (--vuart_bus_priv
.use_count
== 0) {
985 free_irq(vuart_bus_priv
.virq
, &vuart_bus_priv
);
986 ps3_free_vuart_irq(vuart_bus_priv
.virq
);
987 vuart_bus_priv
.virq
= NO_IRQ
;
993 up(&vuart_bus_priv
.probe_mutex
);
997 static void ps3_vuart_shutdown(struct device
*_dev
)
999 struct ps3_vuart_port_device
*dev
= to_ps3_vuart_port_device(_dev
);
1000 struct ps3_vuart_port_driver
*drv
=
1001 to_ps3_vuart_port_driver(_dev
->driver
);
1003 dev_dbg(&dev
->core
, "%s:%d: %s\n", __func__
, __LINE__
,
1009 dev_dbg(&dev
->core
, "%s:%d: %s no shutdown method\n", __func__
,
1010 __LINE__
, dev
->core
.bus_id
);
1014 * ps3_vuart_bus - The vuart bus instance.
1016 * The vuart is managed as a bus that port devices connect to.
1019 struct bus_type ps3_vuart_bus
= {
1020 .name
= "ps3_vuart",
1021 .match
= ps3_vuart_match
,
1022 .probe
= ps3_vuart_probe
,
1023 .remove
= ps3_vuart_remove
,
1024 .shutdown
= ps3_vuart_shutdown
,
1027 int __init
ps3_vuart_bus_init(void)
1031 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1033 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
1036 init_MUTEX(&vuart_bus_priv
.probe_mutex
);
1037 result
= bus_register(&ps3_vuart_bus
);
1043 void __exit
ps3_vuart_bus_exit(void)
1045 pr_debug("%s:%d:\n", __func__
, __LINE__
);
1046 bus_unregister(&ps3_vuart_bus
);
1049 core_initcall(ps3_vuart_bus_init
);
1050 module_exit(ps3_vuart_bus_exit
);
1053 * ps3_vuart_port_release_device - Remove a vuart port device.
1056 static void ps3_vuart_port_release_device(struct device
*_dev
)
1059 struct ps3_vuart_port_device
*dev
= to_ps3_vuart_port_device(_dev
);
1061 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
1063 BUG_ON(dev
->priv
&& "forgot to free");
1064 memset(&dev
->core
, 0, sizeof(dev
->core
));
1069 * ps3_vuart_port_device_register - Add a vuart port device.
1072 int ps3_vuart_port_device_register(struct ps3_vuart_port_device
*dev
)
1074 static unsigned int dev_count
= 1;
1076 BUG_ON(dev
->priv
&& "forgot to free");
1078 dev
->core
.parent
= NULL
;
1079 dev
->core
.bus
= &ps3_vuart_bus
;
1080 dev
->core
.release
= ps3_vuart_port_release_device
;
1082 snprintf(dev
->core
.bus_id
, sizeof(dev
->core
.bus_id
), "vuart_%02x",
1085 dev_dbg(&dev
->core
, "%s:%d register\n", __func__
, __LINE__
);
1087 return device_register(&dev
->core
);
1090 EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register
);
1093 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1096 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver
*drv
)
1100 pr_debug("%s:%d: (%s)\n", __func__
, __LINE__
, drv
->core
.name
);
1101 drv
->core
.bus
= &ps3_vuart_bus
;
1102 result
= driver_register(&drv
->core
);
1106 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register
);
1109 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1112 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver
*drv
)
1114 pr_debug("%s:%d: (%s)\n", __func__
, __LINE__
, drv
->core
.name
);
1115 driver_unregister(&drv
->core
);
1118 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister
);