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>
25 #include <linux/bitops.h>
28 #include <asm/firmware.h>
29 #include <asm/lv1call.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: %016llxh\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)
166 result = lv1_get_virtual_uart_param(dev->port_number,
167 PARAM_TX_TRIGGER, &tx);
171 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
172 __func__, __LINE__, ps3_result(result));
176 result = lv1_get_virtual_uart_param(dev->port_number,
177 PARAM_RX_BUF_SIZE, &size);
180 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
181 __func__, __LINE__, ps3_result(result));
185 result = lv1_get_virtual_uart_param(dev->port_number,
186 PARAM_RX_TRIGGER, &val);
189 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
190 __func__, __LINE__, ps3_result(result));
194 trig->rx = size - val;
196 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
202 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
208 result = lv1_set_virtual_uart_param(dev->port_number,
209 PARAM_TX_TRIGGER, tx);
212 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
213 __func__, __LINE__, ps3_result(result));
217 result = lv1_get_virtual_uart_param(dev->port_number,
218 PARAM_RX_BUF_SIZE, &size);
221 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
222 __func__, __LINE__, ps3_result(result));
226 result = lv1_set_virtual_uart_param(dev->port_number,
227 PARAM_RX_TRIGGER, size - rx);
230 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
231 __func__, __LINE__, ps3_result(result));
235 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
241 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
246 result = lv1_get_virtual_uart_param(dev->port_number,
247 PARAM_RX_BYTES, bytes_waiting);
250 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
251 __func__, __LINE__, ps3_result(result));
253 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
259 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
260 * @dev: The struct ps3_system_bus_device instance.
261 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
264 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
268 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
270 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
272 priv->interrupt_mask = mask;
274 result = lv1_set_virtual_uart_param(dev->port_number,
275 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
278 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
279 __func__, __LINE__, ps3_result(result));
284 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
285 unsigned long *status)
288 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
291 result = lv1_get_virtual_uart_param(dev->port_number,
292 PARAM_INTERRUPT_STATUS, &tmp);
295 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
296 __func__, __LINE__, ps3_result(result));
298 *status = tmp & priv->interrupt_mask;
300 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
301 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
306 int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
308 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
310 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
311 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
312 | INTERRUPT_MASK_TX);
315 int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
317 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
319 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
320 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
321 | INTERRUPT_MASK_RX);
324 int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
326 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
328 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
329 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
330 | INTERRUPT_MASK_DISCONNECT);
333 int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
335 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
337 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
338 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
339 & ~INTERRUPT_MASK_TX) : 0;
342 int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
344 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
346 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
347 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
348 & ~INTERRUPT_MASK_RX) : 0;
351 int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
353 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
355 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
356 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
357 & ~INTERRUPT_MASK_DISCONNECT) : 0;
361 * ps3_vuart_raw_write - Low level write helper.
362 * @dev: The struct ps3_system_bus_device instance.
364 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
367 static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
368 const void *buf, unsigned int bytes, u64 *bytes_written)
371 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
373 result = lv1_write_virtual_uart(dev->port_number,
374 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
377 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
378 "%s\n", __func__, __LINE__, ps3_result(result));
382 priv->stats.bytes_written += *bytes_written;
384 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
385 *bytes_written, bytes, priv->stats.bytes_written);
391 * ps3_vuart_raw_read - Low level read helper.
392 * @dev: The struct ps3_system_bus_device instance.
394 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
397 static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
398 unsigned int bytes, u64 *bytes_read)
401 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
403 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
405 result = lv1_read_virtual_uart(dev->port_number,
406 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
409 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
410 __func__, __LINE__, ps3_result(result));
414 priv->stats.bytes_read += *bytes_read;
416 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
417 *bytes_read, bytes, priv->stats.bytes_read);
423 * ps3_vuart_clear_rx_bytes - Discard bytes received.
424 * @dev: The struct ps3_system_bus_device instance.
425 * @bytes: Max byte count to discard, zero = all pending.
427 * Used to clear pending rx interrupt source. Will not block.
430 void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
434 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
438 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
442 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
444 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
449 /* Add some extra space for recently arrived data. */
453 tmp = kmalloc(bytes, GFP_KERNEL);
458 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
462 /* Don't include these bytes in the stats. */
464 priv->stats.bytes_read -= bytes_waiting;
466 EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
469 * struct list_buffer - An element for a port device fifo buffer list.
473 struct list_head link;
474 const unsigned char *head;
475 const unsigned char *tail;
476 unsigned long dbg_number;
477 unsigned char data[];
481 * ps3_vuart_write - the entry point for writing data to a port
482 * @dev: The struct ps3_system_bus_device instance.
484 * If the port is idle on entry as much of the incoming data is written to
485 * the port as the port will accept. Otherwise a list buffer is created
486 * and any remaning incoming data is copied to that buffer. The buffer is
487 * then enqueued for transmision via the transmit interrupt.
490 int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
493 static unsigned long dbg_number;
495 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
497 struct list_buffer *lb;
499 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
502 spin_lock_irqsave(&priv->tx_list.lock, flags);
504 if (list_empty(&priv->tx_list.head)) {
507 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
509 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
513 "%s:%d: ps3_vuart_raw_write failed\n",
518 if (bytes_written == bytes) {
519 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
520 __func__, __LINE__, bytes);
524 bytes -= bytes_written;
525 buf += bytes_written;
527 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
529 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
534 memcpy(lb->data, buf, bytes);
536 lb->tail = lb->data + bytes;
537 lb->dbg_number = ++dbg_number;
539 spin_lock_irqsave(&priv->tx_list.lock, flags);
540 list_add_tail(&lb->link, &priv->tx_list.head);
541 ps3_vuart_enable_interrupt_tx(dev);
542 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
544 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
545 __func__, __LINE__, lb->dbg_number, bytes);
549 EXPORT_SYMBOL_GPL(ps3_vuart_write);
552 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
553 * @dev: The struct ps3_system_bus_device instance.
554 * @bytes_queued: Number of bytes queued to the buffer list.
556 * Must be called with priv->rx_list.lock held.
559 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
562 static unsigned long dbg_number;
564 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
565 struct list_buffer *lb;
570 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
579 /* Add some extra space for recently arrived data. */
583 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
588 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
591 lb->tail = lb->data + bytes;
592 lb->dbg_number = ++dbg_number;
594 list_add_tail(&lb->link, &priv->rx_list.head);
595 priv->rx_list.bytes_held += bytes;
597 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
598 __func__, __LINE__, lb->dbg_number, bytes);
600 *bytes_queued = bytes;
606 * ps3_vuart_read - The entry point for reading data from a port.
608 * Queue data waiting at the port, and if enough bytes to satisfy the request
609 * are held in the buffer list those bytes are dequeued and copied to the
610 * caller's buffer. Emptied list buffers are retiered. If the request cannot
611 * be statified by bytes held in the list buffers -EAGAIN is returned.
614 int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
618 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
620 struct list_buffer *lb, *n;
621 unsigned long bytes_read;
623 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
626 spin_lock_irqsave(&priv->rx_list.lock, flags);
628 /* Queue rx bytes here for polled reads. */
630 while (priv->rx_list.bytes_held < bytes) {
633 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
634 if (result || !tmp) {
635 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
637 bytes - priv->rx_list.bytes_held);
638 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
643 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
644 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
646 memcpy(buf, lb->head, bytes_read);
649 priv->rx_list.bytes_held -= bytes_read;
651 if (bytes_read < lb->tail - lb->head) {
652 lb->head += bytes_read;
653 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
654 "bytes\n", __func__, __LINE__, lb->dbg_number,
656 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
660 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
661 "bytes\n", __func__, __LINE__, lb->dbg_number,
668 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
671 EXPORT_SYMBOL_GPL(ps3_vuart_read);
674 * ps3_vuart_work - Asynchronous read handler.
677 static void ps3_vuart_work(struct work_struct *work)
679 struct ps3_system_bus_device *dev =
680 ps3_vuart_work_to_system_bus_dev(work);
681 struct ps3_vuart_port_driver *drv =
682 ps3_system_bus_dev_to_vuart_drv(dev);
688 int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
690 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
693 if (priv->rx_list.work.trigger) {
694 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
701 PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
703 spin_lock_irqsave(&priv->rx_list.lock, flags);
704 if (priv->rx_list.bytes_held >= bytes) {
705 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
706 __func__, __LINE__, bytes);
707 schedule_work(&priv->rx_list.work.work);
708 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
712 priv->rx_list.work.trigger = bytes;
713 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
715 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
716 __LINE__, bytes, bytes);
720 EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
722 void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
724 to_port_priv(dev)->rx_list.work.trigger = 0;
726 EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
729 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
731 * Services the transmit interrupt for the port. Writes as much data from the
732 * buffer list as the port will accept. Retires any emptied list buffers and
733 * adjusts the final list buffer state for a partial write.
736 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
739 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
741 struct list_buffer *lb, *n;
742 unsigned long bytes_total = 0;
744 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
746 spin_lock_irqsave(&priv->tx_list.lock, flags);
748 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
752 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
757 "%s:%d: ps3_vuart_raw_write failed\n",
762 bytes_total += bytes_written;
764 if (bytes_written < lb->tail - lb->head) {
765 lb->head += bytes_written;
767 "%s:%d cleared buf_%lu, %llxh bytes\n",
768 __func__, __LINE__, lb->dbg_number,
773 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
780 ps3_vuart_disable_interrupt_tx(dev);
782 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
783 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
784 __func__, __LINE__, bytes_total);
789 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
791 * Services the receive interrupt for the port. Creates a list buffer and
792 * copies all waiting port data to that buffer and enqueues the buffer in the
793 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
796 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
799 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
803 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
805 spin_lock_irqsave(&priv->rx_list.lock, flags);
806 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
809 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
813 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
814 >= priv->rx_list.work.trigger) {
815 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
816 __func__, __LINE__, priv->rx_list.work.trigger);
817 priv->rx_list.work.trigger = 0;
818 schedule_work(&priv->rx_list.work.work);
821 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
825 static int ps3_vuart_handle_interrupt_disconnect(
826 struct ps3_system_bus_device *dev)
828 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
829 BUG_ON("no support");
834 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
836 * Services any pending interrupt types for the port. Passes control to the
837 * third stage type specific interrupt handler. Returns control to the first
838 * stage handler after one iteration.
841 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
844 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
845 unsigned long status;
847 result = ps3_vuart_get_interrupt_status(dev, &status);
852 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
855 if (status & INTERRUPT_MASK_DISCONNECT) {
856 priv->stats.disconnect_interrupts++;
857 result = ps3_vuart_handle_interrupt_disconnect(dev);
859 ps3_vuart_disable_interrupt_disconnect(dev);
862 if (status & INTERRUPT_MASK_TX) {
863 priv->stats.tx_interrupts++;
864 result = ps3_vuart_handle_interrupt_tx(dev);
866 ps3_vuart_disable_interrupt_tx(dev);
869 if (status & INTERRUPT_MASK_RX) {
870 priv->stats.rx_interrupts++;
871 result = ps3_vuart_handle_interrupt_rx(dev);
873 ps3_vuart_disable_interrupt_rx(dev);
879 struct vuart_bus_priv {
880 struct ports_bmp *bmp;
882 struct mutex probe_mutex;
884 struct ps3_system_bus_device *devices[PORT_COUNT];
885 } static vuart_bus_priv;
888 * ps3_vuart_irq_handler - first stage interrupt handler
890 * Loops finding any interrupting port and its associated instance data.
891 * Passes control to the second stage port specific interrupt handler. Loops
892 * until all outstanding interrupts are serviced.
895 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
897 struct vuart_bus_priv *bus_priv = _private;
904 dump_ports_bmp(bus_priv->bmp);
906 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
908 if (port == BITS_PER_LONG)
911 BUG_ON(port >= PORT_COUNT);
912 BUG_ON(!bus_priv->devices[port]);
914 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
920 static int ps3_vuart_bus_interrupt_get(void)
924 pr_debug(" -> %s:%d\n", __func__, __LINE__);
926 vuart_bus_priv.use_count++;
928 BUG_ON(vuart_bus_priv.use_count > 2);
930 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 mutex_lock(&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__);
1080 mutex_unlock(&vuart_bus_priv.probe_mutex);
1085 ps3_vuart_set_interrupt_mask(dev, 0);
1086 kfree(dev->driver_priv);
1087 dev->driver_priv = NULL;
1089 vuart_bus_priv.devices[dev->port_number] = NULL;
1091 ps3_vuart_bus_interrupt_put();
1092 fail_setup_interrupt:
1093 mutex_unlock(&vuart_bus_priv.probe_mutex);
1094 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
1099 * ps3_vuart_cleanup - common cleanup helper.
1100 * @dev: The struct ps3_system_bus_device instance.
1102 * Cleans interrupts and HV resources. Must be called with
1103 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1104 * ps3_vuart_shutdown. After this call, polled reading will still work.
1107 static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
1109 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1111 ps3_vuart_cancel_async(dev);
1112 ps3_vuart_set_interrupt_mask(dev, 0);
1113 ps3_vuart_bus_interrupt_put();
1118 * ps3_vuart_remove - Completely clean the device instance.
1119 * @dev: The struct ps3_system_bus_device instance.
1121 * Cleans all memory, interrupts and HV resources. After this call the
1122 * device can no longer be used.
1125 static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
1127 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1128 struct ps3_vuart_port_driver *drv;
1132 mutex_lock(&vuart_bus_priv.probe_mutex);
1134 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1137 if (!dev->core.driver) {
1138 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1140 mutex_unlock(&vuart_bus_priv.probe_mutex);
1144 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1151 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1156 ps3_vuart_cleanup(dev);
1158 vuart_bus_priv.devices[dev->port_number] = NULL;
1162 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1163 mutex_unlock(&vuart_bus_priv.probe_mutex);
1168 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1169 * @dev: The struct ps3_system_bus_device instance.
1171 * Cleans interrupts and HV resources. After this call the
1172 * device can still be used in polling mode. This behavior required
1173 * by sys-manager to be able to complete the device power operation
1177 static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1179 struct ps3_vuart_port_driver *drv;
1183 mutex_lock(&vuart_bus_priv.probe_mutex);
1185 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1188 if (!dev->core.driver) {
1189 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1191 mutex_unlock(&vuart_bus_priv.probe_mutex);
1195 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1201 else if (drv->remove) {
1202 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1203 __func__, __LINE__);
1206 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1211 ps3_vuart_cleanup(dev);
1213 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1215 mutex_unlock(&vuart_bus_priv.probe_mutex);
1219 static int __init ps3_vuart_bus_init(void)
1221 pr_debug("%s:%d:\n", __func__, __LINE__);
1223 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1226 mutex_init(&vuart_bus_priv.probe_mutex);
1231 static void __exit ps3_vuart_bus_exit(void)
1233 pr_debug("%s:%d:\n", __func__, __LINE__);
1236 core_initcall(ps3_vuart_bus_init);
1237 module_exit(ps3_vuart_bus_exit);
1240 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1243 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1247 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1249 BUG_ON(!drv->core.match_id);
1250 BUG_ON(!drv->core.core.name);
1252 drv->core.probe = ps3_vuart_probe;
1253 drv->core.remove = ps3_vuart_remove;
1254 drv->core.shutdown = ps3_vuart_shutdown;
1256 result = ps3_system_bus_driver_register(&drv->core);
1259 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1262 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1265 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1267 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1268 ps3_system_bus_driver_unregister(&drv->core);
1270 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);