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 ps3_vuart_port_priv - private vuart device data.
77 struct ps3_vuart_port_priv {
82 struct list_head head;
85 struct ps3_vuart_work work;
86 unsigned long bytes_held;
88 struct list_head head;
90 struct ps3_vuart_stats stats;
93 static struct ps3_vuart_port_priv *to_port_priv(
94 struct ps3_system_bus_device *dev)
97 BUG_ON(!dev->driver_priv);
98 return (struct ps3_vuart_port_priv *)dev->driver_priv;
102 * struct ports_bmp - bitmap indicating ports needing service.
104 * A 256 bit read only bitmap indicating ports needing service. Do not write
105 * to these bits. Must not cross a page boundary.
111 } __attribute__ ((aligned (32)));
113 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
114 static void __maybe_unused _dump_ports_bmp(
115 const struct ports_bmp* bmp, const char* func, int line)
117 pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
120 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
121 static void __maybe_unused _dump_port_params(unsigned int port_number,
122 const char* func, int line)
125 static const char *strings[] = {
139 for (i = 0; i < ARRAY_SIZE(strings); i++) {
140 result = lv1_get_virtual_uart_param(port_number, i, &value);
143 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
144 port_number, strings[i], ps3_result(result));
147 pr_debug("%s:%d: port_%u: %s = %lxh\n",
148 func, line, port_number, strings[i], value);
153 struct vuart_triggers {
158 int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
159 struct vuart_triggers *trig)
165 result = lv1_get_virtual_uart_param(dev->port_number,
166 PARAM_TX_TRIGGER, &trig->tx);
169 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
170 __func__, __LINE__, ps3_result(result));
174 result = lv1_get_virtual_uart_param(dev->port_number,
175 PARAM_RX_BUF_SIZE, &size);
178 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
179 __func__, __LINE__, ps3_result(result));
183 result = lv1_get_virtual_uart_param(dev->port_number,
184 PARAM_RX_TRIGGER, &val);
187 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
188 __func__, __LINE__, ps3_result(result));
192 trig->rx = size - val;
194 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
200 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
206 result = lv1_set_virtual_uart_param(dev->port_number,
207 PARAM_TX_TRIGGER, tx);
210 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
211 __func__, __LINE__, ps3_result(result));
215 result = lv1_get_virtual_uart_param(dev->port_number,
216 PARAM_RX_BUF_SIZE, &size);
219 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
220 __func__, __LINE__, ps3_result(result));
224 result = lv1_set_virtual_uart_param(dev->port_number,
225 PARAM_RX_TRIGGER, size - rx);
228 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
229 __func__, __LINE__, ps3_result(result));
233 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
239 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
244 result = lv1_get_virtual_uart_param(dev->port_number,
245 PARAM_RX_BYTES, bytes_waiting);
248 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
249 __func__, __LINE__, ps3_result(result));
251 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
257 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
258 * @dev: The struct ps3_system_bus_device instance.
259 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
262 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
266 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
268 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
270 priv->interrupt_mask = mask;
272 result = lv1_set_virtual_uart_param(dev->port_number,
273 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
276 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
277 __func__, __LINE__, ps3_result(result));
282 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
283 unsigned long *status)
286 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
289 result = lv1_get_virtual_uart_param(dev->port_number,
290 PARAM_INTERRUPT_STATUS, &tmp);
293 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
294 __func__, __LINE__, ps3_result(result));
296 *status = tmp & priv->interrupt_mask;
298 dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
299 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
304 int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
306 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
308 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
309 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
310 | INTERRUPT_MASK_TX);
313 int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
315 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
317 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
318 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
319 | INTERRUPT_MASK_RX);
322 int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
324 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
326 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
327 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
328 | INTERRUPT_MASK_DISCONNECT);
331 int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
333 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
335 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
336 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
337 & ~INTERRUPT_MASK_TX) : 0;
340 int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
342 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
344 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
345 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
346 & ~INTERRUPT_MASK_RX) : 0;
349 int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
351 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
353 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
354 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
355 & ~INTERRUPT_MASK_DISCONNECT) : 0;
359 * ps3_vuart_raw_write - Low level write helper.
360 * @dev: The struct ps3_system_bus_device instance.
362 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
365 static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
366 const void* buf, unsigned int bytes, unsigned long *bytes_written)
369 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
371 result = lv1_write_virtual_uart(dev->port_number,
372 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
375 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
376 "%s\n", __func__, __LINE__, ps3_result(result));
380 priv->stats.bytes_written += *bytes_written;
382 dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
383 *bytes_written, bytes, priv->stats.bytes_written);
389 * ps3_vuart_raw_read - Low level read helper.
390 * @dev: The struct ps3_system_bus_device instance.
392 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
395 static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
396 unsigned int bytes, unsigned long *bytes_read)
399 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
401 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
403 result = lv1_read_virtual_uart(dev->port_number,
404 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
407 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
408 __func__, __LINE__, ps3_result(result));
412 priv->stats.bytes_read += *bytes_read;
414 dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
415 *bytes_read, bytes, priv->stats.bytes_read);
421 * ps3_vuart_clear_rx_bytes - Discard bytes received.
422 * @dev: The struct ps3_system_bus_device instance.
423 * @bytes: Max byte count to discard, zero = all pending.
425 * Used to clear pending rx interrupt source. Will not block.
428 void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
432 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
436 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
440 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
442 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
447 /* Add some extra space for recently arrived data. */
451 tmp = kmalloc(bytes, GFP_KERNEL);
456 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
460 /* Don't include these bytes in the stats. */
462 priv->stats.bytes_read -= bytes_waiting;
464 EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
467 * struct list_buffer - An element for a port device fifo buffer list.
471 struct list_head link;
472 const unsigned char *head;
473 const unsigned char *tail;
474 unsigned long dbg_number;
475 unsigned char data[];
479 * ps3_vuart_write - the entry point for writing data to a port
480 * @dev: The struct ps3_system_bus_device instance.
482 * If the port is idle on entry as much of the incoming data is written to
483 * the port as the port will accept. Otherwise a list buffer is created
484 * and any remaning incoming data is copied to that buffer. The buffer is
485 * then enqueued for transmision via the transmit interrupt.
488 int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
491 static unsigned long dbg_number;
493 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
495 struct list_buffer *lb;
497 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
500 spin_lock_irqsave(&priv->tx_list.lock, flags);
502 if (list_empty(&priv->tx_list.head)) {
503 unsigned long bytes_written;
505 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
507 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
511 "%s:%d: ps3_vuart_raw_write failed\n",
516 if (bytes_written == bytes) {
517 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
518 __func__, __LINE__, bytes);
522 bytes -= bytes_written;
523 buf += bytes_written;
525 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
527 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
533 memcpy(lb->data, buf, bytes);
535 lb->tail = lb->data + bytes;
536 lb->dbg_number = ++dbg_number;
538 spin_lock_irqsave(&priv->tx_list.lock, flags);
539 list_add_tail(&lb->link, &priv->tx_list.head);
540 ps3_vuart_enable_interrupt_tx(dev);
541 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
543 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
544 __func__, __LINE__, lb->dbg_number, bytes);
548 EXPORT_SYMBOL_GPL(ps3_vuart_write);
551 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
552 * @dev: The struct ps3_system_bus_device instance.
553 * @bytes_queued: Number of bytes queued to the buffer list.
555 * Must be called with priv->rx_list.lock held.
558 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
561 static unsigned long dbg_number;
563 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
564 struct list_buffer *lb;
569 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
578 /* Add some extra space for recently arrived data. */
582 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
587 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
590 lb->tail = lb->data + bytes;
591 lb->dbg_number = ++dbg_number;
593 list_add_tail(&lb->link, &priv->rx_list.head);
594 priv->rx_list.bytes_held += bytes;
596 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
597 __func__, __LINE__, lb->dbg_number, bytes);
599 *bytes_queued = bytes;
605 * ps3_vuart_read - The entry point for reading data from a port.
607 * Queue data waiting at the port, and if enough bytes to satisfy the request
608 * are held in the buffer list those bytes are dequeued and copied to the
609 * caller's buffer. Emptied list buffers are retiered. If the request cannot
610 * be statified by bytes held in the list buffers -EAGAIN is returned.
613 int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
617 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
619 struct list_buffer *lb, *n;
620 unsigned long bytes_read;
622 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
625 spin_lock_irqsave(&priv->rx_list.lock, flags);
627 /* Queue rx bytes here for polled reads. */
629 while (priv->rx_list.bytes_held < bytes) {
632 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
633 if (result || !tmp) {
634 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
636 bytes - priv->rx_list.bytes_held);
637 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
642 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
643 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
645 memcpy(buf, lb->head, bytes_read);
648 priv->rx_list.bytes_held -= bytes_read;
650 if (bytes_read < lb->tail - lb->head) {
651 lb->head += bytes_read;
652 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
653 "bytes\n", __func__, __LINE__, lb->dbg_number,
655 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
659 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
660 "bytes\n", __func__, __LINE__, lb->dbg_number,
667 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
670 EXPORT_SYMBOL_GPL(ps3_vuart_read);
673 * ps3_vuart_work - Asynchronous read handler.
676 static void ps3_vuart_work(struct work_struct *work)
678 struct ps3_system_bus_device *dev =
679 ps3_vuart_work_to_system_bus_dev(work);
680 struct ps3_vuart_port_driver *drv =
681 ps3_system_bus_dev_to_vuart_drv(dev);
687 int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
689 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
692 if (priv->rx_list.work.trigger) {
693 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
700 PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
702 spin_lock_irqsave(&priv->rx_list.lock, flags);
703 if (priv->rx_list.bytes_held >= bytes) {
704 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
705 __func__, __LINE__, bytes);
706 schedule_work(&priv->rx_list.work.work);
707 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
711 priv->rx_list.work.trigger = bytes;
712 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
714 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
715 __LINE__, bytes, bytes);
719 EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
721 void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
723 to_port_priv(dev)->rx_list.work.trigger = 0;
725 EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
728 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
730 * Services the transmit interrupt for the port. Writes as much data from the
731 * buffer list as the port will accept. Retires any emptied list buffers and
732 * adjusts the final list buffer state for a partial write.
735 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
738 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
740 struct list_buffer *lb, *n;
741 unsigned long bytes_total = 0;
743 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
745 spin_lock_irqsave(&priv->tx_list.lock, flags);
747 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
749 unsigned long bytes_written;
751 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
756 "%s:%d: ps3_vuart_raw_write failed\n",
761 bytes_total += bytes_written;
763 if (bytes_written < lb->tail - lb->head) {
764 lb->head += bytes_written;
766 "%s:%d cleared buf_%lu, %lxh bytes\n",
767 __func__, __LINE__, lb->dbg_number,
772 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
779 ps3_vuart_disable_interrupt_tx(dev);
781 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
782 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
783 __func__, __LINE__, bytes_total);
788 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
790 * Services the receive interrupt for the port. Creates a list buffer and
791 * copies all waiting port data to that buffer and enqueues the buffer in the
792 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
795 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
798 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
802 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
804 spin_lock_irqsave(&priv->rx_list.lock, flags);
805 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
808 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
812 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
813 >= priv->rx_list.work.trigger) {
814 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
815 __func__, __LINE__, priv->rx_list.work.trigger);
816 priv->rx_list.work.trigger = 0;
817 schedule_work(&priv->rx_list.work.work);
820 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
824 static int ps3_vuart_handle_interrupt_disconnect(
825 struct ps3_system_bus_device *dev)
827 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
828 BUG_ON("no support");
833 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
835 * Services any pending interrupt types for the port. Passes control to the
836 * third stage type specific interrupt handler. Returns control to the first
837 * stage handler after one iteration.
840 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
843 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
844 unsigned long status;
846 result = ps3_vuart_get_interrupt_status(dev, &status);
851 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
854 if (status & INTERRUPT_MASK_DISCONNECT) {
855 priv->stats.disconnect_interrupts++;
856 result = ps3_vuart_handle_interrupt_disconnect(dev);
858 ps3_vuart_disable_interrupt_disconnect(dev);
861 if (status & INTERRUPT_MASK_TX) {
862 priv->stats.tx_interrupts++;
863 result = ps3_vuart_handle_interrupt_tx(dev);
865 ps3_vuart_disable_interrupt_tx(dev);
868 if (status & INTERRUPT_MASK_RX) {
869 priv->stats.rx_interrupts++;
870 result = ps3_vuart_handle_interrupt_rx(dev);
872 ps3_vuart_disable_interrupt_rx(dev);
878 struct vuart_bus_priv {
879 struct ports_bmp *bmp;
881 struct semaphore probe_mutex;
883 struct ps3_system_bus_device *devices[PORT_COUNT];
884 } static vuart_bus_priv;
887 * ps3_vuart_irq_handler - first stage interrupt handler
889 * Loops finding any interrupting port and its associated instance data.
890 * Passes control to the second stage port specific interrupt handler. Loops
891 * until all outstanding interrupts are serviced.
894 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
896 struct vuart_bus_priv *bus_priv = _private;
903 dump_ports_bmp(bus_priv->bmp);
905 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
907 if (port == BITS_PER_LONG)
910 BUG_ON(port >= PORT_COUNT);
911 BUG_ON(!bus_priv->devices[port]);
913 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
919 static int ps3_vuart_bus_interrupt_get(void)
923 pr_debug(" -> %s:%d\n", __func__, __LINE__);
925 vuart_bus_priv.use_count++;
927 BUG_ON(vuart_bus_priv.use_count > 2);
929 if (vuart_bus_priv.use_count != 1) {
933 BUG_ON(vuart_bus_priv.bmp);
935 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
937 if (!vuart_bus_priv.bmp) {
938 pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
940 goto fail_bmp_malloc;
943 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
944 &vuart_bus_priv.virq);
947 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
948 __func__, __LINE__, result);
953 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
954 IRQF_DISABLED, "vuart", &vuart_bus_priv);
957 pr_debug("%s:%d: request_irq failed (%d)\n",
958 __func__, __LINE__, result);
959 goto fail_request_irq;
962 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
966 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
967 vuart_bus_priv.virq = NO_IRQ;
969 kfree(vuart_bus_priv.bmp);
970 vuart_bus_priv.bmp = NULL;
972 vuart_bus_priv.use_count--;
973 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
977 static int ps3_vuart_bus_interrupt_put(void)
979 pr_debug(" -> %s:%d\n", __func__, __LINE__);
981 vuart_bus_priv.use_count--;
983 BUG_ON(vuart_bus_priv.use_count < 0);
985 if (vuart_bus_priv.use_count != 0)
988 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
990 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
991 vuart_bus_priv.virq = NO_IRQ;
993 kfree(vuart_bus_priv.bmp);
994 vuart_bus_priv.bmp = NULL;
996 pr_debug(" <- %s:%d\n", __func__, __LINE__);
1000 static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
1003 struct ps3_vuart_port_driver *drv;
1004 struct ps3_vuart_port_priv *priv = NULL;
1006 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1008 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1010 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
1011 drv->core.core.name);
1015 if (dev->port_number >= PORT_COUNT) {
1020 down(&vuart_bus_priv.probe_mutex);
1022 result = ps3_vuart_bus_interrupt_get();
1025 goto fail_setup_interrupt;
1027 if (vuart_bus_priv.devices[dev->port_number]) {
1028 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
1029 __LINE__, dev->port_number);
1034 vuart_bus_priv.devices[dev->port_number] = dev;
1036 /* Setup dev->driver_priv. */
1038 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1041 if (!dev->driver_priv) {
1043 goto fail_dev_malloc;
1046 priv = to_port_priv(dev);
1048 INIT_LIST_HEAD(&priv->tx_list.head);
1049 spin_lock_init(&priv->tx_list.lock);
1051 INIT_LIST_HEAD(&priv->rx_list.head);
1052 spin_lock_init(&priv->rx_list.lock);
1054 INIT_WORK(&priv->rx_list.work.work, NULL);
1055 priv->rx_list.work.trigger = 0;
1056 priv->rx_list.work.dev = dev;
1058 /* clear stale pending interrupts */
1060 ps3_vuart_clear_rx_bytes(dev, 0);
1062 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
1064 ps3_vuart_set_triggers(dev, 1, 1);
1067 result = drv->probe(dev);
1070 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1075 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1076 __func__, __LINE__);
1077 down(&vuart_bus_priv.probe_mutex);
1081 up(&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 up(&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 down(&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 up(&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 up(&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 down(&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 up(&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 up(&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 init_MUTEX(&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);