mmc/omap: make mmci-omap using platform_driver_probe
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ps3 / ps3-vuart.c
blobe4ad5ba5d0a3da7b4014c003f7582d9146323a44
1 /*
2 * PS3 virtual uart
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>
26 #include <asm/ps3.h>
28 #include <asm/firmware.h>
29 #include <asm/lv1call.h>
31 #include "vuart.h"
33 MODULE_AUTHOR("Sony Corporation");
34 MODULE_LICENSE("GPL v2");
35 MODULE_DESCRIPTION("PS3 vuart");
37 /**
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,};
50 enum vuart_param {
51 PARAM_TX_TRIGGER = 0,
52 PARAM_RX_TRIGGER = 1,
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 {
62 INTERRUPT_BIT_TX = 0,
63 INTERRUPT_BIT_RX = 1,
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,
73 /**
74 * struct ps3_vuart_port_priv - private vuart device data.
77 struct ps3_vuart_port_priv {
78 u64 interrupt_mask;
80 struct {
81 spinlock_t lock;
82 struct list_head head;
83 } tx_list;
84 struct {
85 struct ps3_vuart_work work;
86 unsigned long bytes_held;
87 spinlock_t lock;
88 struct list_head head;
89 } rx_list;
90 struct ps3_vuart_stats stats;
93 static struct ps3_vuart_port_priv *to_port_priv(
94 struct ps3_system_bus_device *dev)
96 BUG_ON(!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.
108 struct ports_bmp {
109 u64 status;
110 u64 unused[3];
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)
124 #if defined(DEBUG)
125 static const char *strings[] = {
126 "tx_trigger ",
127 "rx_trigger ",
128 "interrupt_mask ",
129 "rx_buf_size ",
130 "rx_bytes ",
131 "tx_buf_size ",
132 "tx_bytes ",
133 "interrupt_status",
135 int result;
136 unsigned int i;
137 u64 value;
139 for (i = 0; i < ARRAY_SIZE(strings); i++) {
140 result = lv1_get_virtual_uart_param(port_number, i, &value);
142 if (result) {
143 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
144 port_number, strings[i], ps3_result(result));
145 continue;
147 pr_debug("%s:%d: port_%u: %s = %lxh\n",
148 func, line, port_number, strings[i], value);
150 #endif
153 struct vuart_triggers {
154 unsigned long rx;
155 unsigned long tx;
158 int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
159 struct vuart_triggers *trig)
161 int result;
162 u64 size;
163 u64 val;
164 u64 tx;
166 result = lv1_get_virtual_uart_param(dev->port_number,
167 PARAM_TX_TRIGGER, &tx);
168 trig->tx = tx;
170 if (result) {
171 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
172 __func__, __LINE__, ps3_result(result));
173 return result;
176 result = lv1_get_virtual_uart_param(dev->port_number,
177 PARAM_RX_BUF_SIZE, &size);
179 if (result) {
180 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
181 __func__, __LINE__, ps3_result(result));
182 return result;
185 result = lv1_get_virtual_uart_param(dev->port_number,
186 PARAM_RX_TRIGGER, &val);
188 if (result) {
189 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
190 __func__, __LINE__, ps3_result(result));
191 return result;
194 trig->rx = size - val;
196 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
197 trig->tx, trig->rx);
199 return result;
202 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
203 unsigned int rx)
205 int result;
206 u64 size;
208 result = lv1_set_virtual_uart_param(dev->port_number,
209 PARAM_TX_TRIGGER, tx);
211 if (result) {
212 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
213 __func__, __LINE__, ps3_result(result));
214 return result;
217 result = lv1_get_virtual_uart_param(dev->port_number,
218 PARAM_RX_BUF_SIZE, &size);
220 if (result) {
221 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
222 __func__, __LINE__, ps3_result(result));
223 return result;
226 result = lv1_set_virtual_uart_param(dev->port_number,
227 PARAM_RX_TRIGGER, size - rx);
229 if (result) {
230 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
231 __func__, __LINE__, ps3_result(result));
232 return result;
235 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
236 tx, rx);
238 return result;
241 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
242 u64 *bytes_waiting)
244 int result;
246 result = lv1_get_virtual_uart_param(dev->port_number,
247 PARAM_RX_BYTES, bytes_waiting);
249 if (result)
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__,
254 *bytes_waiting);
255 return result;
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,
265 unsigned long mask)
267 int result;
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);
277 if (result)
278 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
279 __func__, __LINE__, ps3_result(result));
281 return result;
284 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
285 unsigned long *status)
287 int result;
288 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
289 u64 tmp;
291 result = lv1_get_virtual_uart_param(dev->port_number,
292 PARAM_INTERRUPT_STATUS, &tmp);
294 if (result)
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);
303 return result;
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)
370 int result;
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);
376 if (result) {
377 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
378 "%s\n", __func__, __LINE__, ps3_result(result));
379 return 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);
387 return result;
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)
400 int result;
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);
408 if (result) {
409 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
410 __func__, __LINE__, ps3_result(result));
411 return 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);
419 return result;
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,
431 unsigned int bytes)
433 int result;
434 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
435 u64 bytes_waiting;
436 void *tmp;
438 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
440 BUG_ON(result);
442 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
444 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
446 if (!bytes)
447 return;
449 /* Add some extra space for recently arrived data. */
451 bytes += 128;
453 tmp = kmalloc(bytes, GFP_KERNEL);
455 if (!tmp)
456 return;
458 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
460 kfree(tmp);
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.
472 struct list_buffer {
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,
491 unsigned int bytes)
493 static unsigned long dbg_number;
494 int result;
495 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
496 unsigned long flags;
497 struct list_buffer *lb;
499 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
500 bytes, bytes);
502 spin_lock_irqsave(&priv->tx_list.lock, flags);
504 if (list_empty(&priv->tx_list.head)) {
505 u64 bytes_written;
507 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
509 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
511 if (result) {
512 dev_dbg(&dev->core,
513 "%s:%d: ps3_vuart_raw_write failed\n",
514 __func__, __LINE__);
515 return result;
518 if (bytes_written == bytes) {
519 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
520 __func__, __LINE__, bytes);
521 return 0;
524 bytes -= bytes_written;
525 buf += bytes_written;
526 } else
527 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
529 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
531 if (!lb)
532 return -ENOMEM;
534 memcpy(lb->data, buf, bytes);
535 lb->head = lb->data;
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);
547 return 0;
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,
560 u64 *bytes_queued)
562 static unsigned long dbg_number;
563 int result;
564 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
565 struct list_buffer *lb;
566 u64 bytes;
568 *bytes_queued = 0;
570 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
571 BUG_ON(result);
573 if (result)
574 return -EIO;
576 if (!bytes)
577 return 0;
579 /* Add some extra space for recently arrived data. */
581 bytes += 128;
583 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
585 if (!lb)
586 return -ENOMEM;
588 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
590 lb->head = lb->data;
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;
602 return 0;
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,
615 unsigned int bytes)
617 int result;
618 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
619 unsigned long flags;
620 struct list_buffer *lb, *n;
621 unsigned long bytes_read;
623 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
624 bytes, bytes);
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) {
631 u64 tmp;
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",
636 __func__, __LINE__,
637 bytes - priv->rx_list.bytes_held);
638 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
639 return -EAGAIN;
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);
647 buf += bytes_read;
648 bytes -= 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,
655 bytes_read);
656 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
657 return 0;
660 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
661 "bytes\n", __func__, __LINE__, lb->dbg_number,
662 bytes_read);
664 list_del(&lb->link);
665 kfree(lb);
668 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
669 return 0;
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);
684 BUG_ON(!drv);
685 drv->work(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);
691 unsigned long flags;
693 if (priv->rx_list.work.trigger) {
694 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
695 __func__, __LINE__);
696 return -EAGAIN;
699 BUG_ON(!bytes);
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);
709 return 0;
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);
718 return 0;
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)
738 int result = 0;
739 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
740 unsigned long flags;
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) {
750 u64 bytes_written;
752 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
753 &bytes_written);
755 if (result) {
756 dev_dbg(&dev->core,
757 "%s:%d: ps3_vuart_raw_write failed\n",
758 __func__, __LINE__);
759 break;
762 bytes_total += bytes_written;
764 if (bytes_written < lb->tail - lb->head) {
765 lb->head += bytes_written;
766 dev_dbg(&dev->core,
767 "%s:%d cleared buf_%lu, %llxh bytes\n",
768 __func__, __LINE__, lb->dbg_number,
769 bytes_written);
770 goto port_full;
773 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
774 lb->dbg_number);
776 list_del(&lb->link);
777 kfree(lb);
780 ps3_vuart_disable_interrupt_tx(dev);
781 port_full:
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);
785 return result;
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)
798 int result;
799 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
800 unsigned long flags;
801 u64 bytes;
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);
808 if (result) {
809 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
810 return result;
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);
822 return result;
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");
830 return -1;
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)
843 int result;
844 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
845 unsigned long status;
847 result = ps3_vuart_get_interrupt_status(dev, &status);
849 if (result)
850 return result;
852 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
853 status);
855 if (status & INTERRUPT_MASK_DISCONNECT) {
856 priv->stats.disconnect_interrupts++;
857 result = ps3_vuart_handle_interrupt_disconnect(dev);
858 if (result)
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);
865 if (result)
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);
872 if (result)
873 ps3_vuart_disable_interrupt_rx(dev);
876 return 0;
879 struct vuart_bus_priv {
880 struct ports_bmp *bmp;
881 unsigned int virq;
882 struct mutex probe_mutex;
883 int use_count;
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;
899 BUG_ON(!bus_priv);
901 while (1) {
902 unsigned int port;
904 dump_ports_bmp(bus_priv->bmp);
906 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
908 if (port == BITS_PER_LONG)
909 break;
911 BUG_ON(port >= PORT_COUNT);
912 BUG_ON(!bus_priv->devices[port]);
914 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
917 return IRQ_HANDLED;
920 static int ps3_vuart_bus_interrupt_get(void)
922 int result;
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)
931 return 0;
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__);
939 result = -ENOMEM;
940 goto fail_bmp_malloc;
943 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
944 &vuart_bus_priv.virq);
946 if (result) {
947 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
948 __func__, __LINE__, result);
949 result = -EPERM;
950 goto fail_alloc_irq;
953 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
954 IRQF_DISABLED, "vuart", &vuart_bus_priv);
956 if (result) {
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__);
963 return result;
965 fail_request_irq:
966 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
967 vuart_bus_priv.virq = NO_IRQ;
968 fail_alloc_irq:
969 kfree(vuart_bus_priv.bmp);
970 vuart_bus_priv.bmp = NULL;
971 fail_bmp_malloc:
972 vuart_bus_priv.use_count--;
973 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
974 return result;
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)
986 return 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__);
997 return 0;
1000 static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
1002 int result;
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);
1013 BUG_ON(!drv);
1015 if (dev->port_number >= PORT_COUNT) {
1016 BUG();
1017 return -EINVAL;
1020 mutex_lock(&vuart_bus_priv.probe_mutex);
1022 result = ps3_vuart_bus_interrupt_get();
1024 if (result)
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);
1030 result = -EBUSY;
1031 goto fail_busy;
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),
1039 GFP_KERNEL);
1041 if (!dev->driver_priv) {
1042 result = -ENOMEM;
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);
1066 if (drv->probe)
1067 result = drv->probe(dev);
1068 else {
1069 result = 0;
1070 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1071 __LINE__);
1074 if (result) {
1075 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1076 __func__, __LINE__);
1077 goto fail_probe;
1080 mutex_unlock(&vuart_bus_priv.probe_mutex);
1082 return result;
1084 fail_probe:
1085 ps3_vuart_set_interrupt_mask(dev, 0);
1086 kfree(dev->driver_priv);
1087 dev->driver_priv = NULL;
1088 fail_dev_malloc:
1089 vuart_bus_priv.devices[dev->port_number] = NULL;
1090 fail_busy:
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__);
1095 return result;
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();
1114 return 0;
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;
1130 BUG_ON(!dev);
1132 mutex_lock(&vuart_bus_priv.probe_mutex);
1134 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1135 dev->match_id);
1137 if (!dev->core.driver) {
1138 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1139 __LINE__);
1140 mutex_unlock(&vuart_bus_priv.probe_mutex);
1141 return 0;
1144 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1146 BUG_ON(!drv);
1148 if (drv->remove) {
1149 drv->remove(dev);
1150 } else {
1151 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1152 __LINE__);
1153 BUG();
1156 ps3_vuart_cleanup(dev);
1158 vuart_bus_priv.devices[dev->port_number] = NULL;
1159 kfree(priv);
1160 priv = NULL;
1162 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1163 mutex_unlock(&vuart_bus_priv.probe_mutex);
1164 return 0;
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
1174 * sequence.
1177 static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1179 struct ps3_vuart_port_driver *drv;
1181 BUG_ON(!dev);
1183 mutex_lock(&vuart_bus_priv.probe_mutex);
1185 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1186 dev->match_id);
1188 if (!dev->core.driver) {
1189 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1190 __LINE__);
1191 mutex_unlock(&vuart_bus_priv.probe_mutex);
1192 return 0;
1195 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1197 BUG_ON(!drv);
1199 if (drv->shutdown)
1200 drv->shutdown(dev);
1201 else if (drv->remove) {
1202 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1203 __func__, __LINE__);
1204 drv->remove(dev);
1205 } else {
1206 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1207 __LINE__);
1208 BUG();
1211 ps3_vuart_cleanup(dev);
1213 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1215 mutex_unlock(&vuart_bus_priv.probe_mutex);
1216 return 0;
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))
1224 return -ENODEV;
1226 mutex_init(&vuart_bus_priv.probe_mutex);
1228 return 0;
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)
1245 int result;
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);
1257 return result;
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);