[PATCH] Revert "swsusp: disable nonboot CPUs before entering platform suspend"
[linux-2.6/kvm.git] / drivers / ps3 / vuart.c
blob6c12744eeb9d24a5c4249c24386d7f8f14d40b08
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 <asm/ps3.h>
27 #include <asm/firmware.h>
28 #include <asm/lv1call.h>
29 #include <asm/bitops.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 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.
80 struct ports_bmp {
81 u64 status;
82 u64 unused[3];
83 } __attribute__ ((aligned (32)));
85 /* redefine dev_dbg to do a syntax check */
87 #if !defined(DEBUG)
88 #undef dev_dbg
89 static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg(
90 const struct device *_dev, const char *fmt, ...) {return 0;}
91 #endif
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)
103 switch(match_id) {
104 case PS3_MATCH_ID_AV_SETTINGS:
105 *port_number = 0;
106 return 0;
107 case PS3_MATCH_ID_SYSTEM_MANAGER:
108 *port_number = 2;
109 return 0;
110 default:
111 WARN_ON(1);
112 *port_number = UINT_MAX;
113 return -EINVAL;
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)
121 #if defined(DEBUG)
122 static const char *strings[] = {
123 "tx_trigger ",
124 "rx_trigger ",
125 "interrupt_mask ",
126 "rx_buf_size ",
127 "rx_bytes ",
128 "tx_buf_size ",
129 "tx_bytes ",
130 "interrupt_status",
132 int result;
133 unsigned int i;
134 u64 value;
136 for (i = 0; i < ARRAY_SIZE(strings); i++) {
137 result = lv1_get_virtual_uart_param(port_number, i, &value);
139 if (result) {
140 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
141 port_number, strings[i], ps3_result(result));
142 continue;
144 pr_debug("%s:%d: port_%u: %s = %lxh\n",
145 func, line, port_number, strings[i], value);
147 #endif
150 struct vuart_triggers {
151 unsigned long rx;
152 unsigned long tx;
155 int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
156 struct vuart_triggers *trig)
158 int result;
159 unsigned long size;
160 unsigned long val;
162 result = lv1_get_virtual_uart_param(dev->priv->port_number,
163 PARAM_TX_TRIGGER, &trig->tx);
165 if (result) {
166 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
167 __func__, __LINE__, ps3_result(result));
168 return result;
171 result = lv1_get_virtual_uart_param(dev->priv->port_number,
172 PARAM_RX_BUF_SIZE, &size);
174 if (result) {
175 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
176 __func__, __LINE__, ps3_result(result));
177 return result;
180 result = lv1_get_virtual_uart_param(dev->priv->port_number,
181 PARAM_RX_TRIGGER, &val);
183 if (result) {
184 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
185 __func__, __LINE__, ps3_result(result));
186 return result;
189 trig->rx = size - val;
191 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
192 trig->tx, trig->rx);
194 return result;
197 int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
198 unsigned int rx)
200 int result;
201 unsigned long size;
203 result = lv1_set_virtual_uart_param(dev->priv->port_number,
204 PARAM_TX_TRIGGER, tx);
206 if (result) {
207 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
208 __func__, __LINE__, ps3_result(result));
209 return result;
212 result = lv1_get_virtual_uart_param(dev->priv->port_number,
213 PARAM_RX_BUF_SIZE, &size);
215 if (result) {
216 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
217 __func__, __LINE__, ps3_result(result));
218 return result;
221 result = lv1_set_virtual_uart_param(dev->priv->port_number,
222 PARAM_RX_TRIGGER, size - rx);
224 if (result) {
225 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
226 __func__, __LINE__, ps3_result(result));
227 return result;
230 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
231 tx, rx);
233 return result;
236 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
237 u64 *bytes_waiting)
239 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
240 PARAM_RX_BYTES, bytes_waiting);
242 if (result)
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__,
247 *bytes_waiting);
248 return result;
251 static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
252 unsigned long mask)
254 int result;
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);
263 if (result)
264 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
265 __func__, __LINE__, ps3_result(result));
267 return result;
270 static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
271 unsigned long *status)
273 u64 tmp;
274 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
275 PARAM_INTERRUPT_STATUS, &tmp);
277 if (result)
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);
286 return result;
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)
340 int result;
342 result = lv1_write_virtual_uart(dev->priv->port_number,
343 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
345 if (result) {
346 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
347 "%s\n", __func__, __LINE__, ps3_result(result));
348 return 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);
356 return result;
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)
368 int result;
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);
375 if (result) {
376 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
377 __func__, __LINE__, ps3_result(result));
378 return 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);
386 return result;
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,
397 unsigned int bytes)
399 int result;
400 u64 bytes_waiting;
401 void* tmp;
403 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
405 BUG_ON(result);
407 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
409 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
411 if (!bytes)
412 return;
414 /* Add some extra space for recently arrived data. */
416 bytes += 128;
418 tmp = kmalloc(bytes, GFP_KERNEL);
420 if (!tmp)
421 return;
423 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
425 kfree(tmp);
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.
436 struct list_buffer {
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,
454 unsigned int bytes)
456 static unsigned long dbg_number;
457 int result;
458 unsigned long flags;
459 struct list_buffer *lb;
461 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
462 bytes, bytes);
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);
473 if (result) {
474 dev_dbg(&dev->core,
475 "%s:%d: ps3_vuart_raw_write failed\n",
476 __func__, __LINE__);
477 return result;
480 if (bytes_written == bytes) {
481 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
482 __func__, __LINE__, bytes);
483 return 0;
486 bytes -= bytes_written;
487 buf += bytes_written;
488 } else
489 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
491 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
493 if (!lb) {
494 return -ENOMEM;
497 memcpy(lb->data, buf, bytes);
498 lb->head = lb->data;
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);
510 return 0;
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,
523 unsigned int bytes)
525 unsigned long flags;
526 struct list_buffer *lb, *n;
527 unsigned long bytes_read;
529 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
530 bytes, bytes);
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",
537 __func__, __LINE__,
538 bytes - dev->priv->rx_list.bytes_held);
539 return -EAGAIN;
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);
546 buf += bytes_read;
547 bytes -= 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,
554 bytes_read);
555 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
556 return 0;
559 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
560 "bytes\n", __func__, __LINE__, lb->dbg_number,
561 bytes_read);
563 list_del(&lb->link);
564 kfree(lb);
567 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
568 return 0;
571 int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
572 unsigned int bytes)
574 unsigned long flags;
576 if(dev->priv->work.trigger) {
577 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
578 __func__, __LINE__);
579 return -EAGAIN;
582 BUG_ON(!bytes);
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);
592 return 0;
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);
601 return 0;
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)
619 int result = 0;
620 unsigned long flags;
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,
633 &bytes_written);
635 if (result) {
636 dev_dbg(&dev->core,
637 "%s:%d: ps3_vuart_raw_write failed\n",
638 __func__, __LINE__);
639 break;
642 bytes_total += bytes_written;
644 if (bytes_written < lb->tail - lb->head) {
645 lb->head += bytes_written;
646 dev_dbg(&dev->core,
647 "%s:%d cleared buf_%lu, %lxh bytes\n",
648 __func__, __LINE__, lb->dbg_number,
649 bytes_written);
650 goto port_full;
653 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
654 lb->dbg_number);
656 list_del(&lb->link);
657 kfree(lb);
660 ps3_vuart_disable_interrupt_tx(dev);
661 port_full:
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);
665 return result;
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;
679 int result = 0;
680 unsigned long flags;
681 struct list_buffer *lb;
682 unsigned long bytes;
684 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
686 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
688 if (result)
689 return -EIO;
691 BUG_ON(!bytes);
693 /* Add some extra space for recently arrived data. */
695 bytes += 128;
697 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
699 if (!lb)
700 return -ENOMEM;
702 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
704 lb->head = lb->data;
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);
725 return 0;
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");
733 return -1;
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)
746 int result;
747 unsigned long status;
749 result = ps3_vuart_get_interrupt_status(dev, &status);
751 if (result)
752 return result;
754 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
755 status);
757 if (status & INTERRUPT_MASK_DISCONNECT) {
758 dev->priv->stats.disconnect_interrupts++;
759 result = ps3_vuart_handle_interrupt_disconnect(dev);
760 if (result)
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);
767 if (result)
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);
774 if (result)
775 ps3_vuart_disable_interrupt_rx(dev);
778 return 0;
781 struct vuart_bus_priv {
782 const struct ports_bmp bmp;
783 unsigned int virq;
784 struct semaphore probe_mutex;
785 int use_count;
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;
801 BUG_ON(!_private);
802 bus_priv = (struct vuart_bus_priv *)_private;
804 while (1) {
805 unsigned int port;
807 dump_ports_bmp(&bus_priv->bmp);
809 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
811 if (port == BITS_PER_LONG)
812 break;
814 BUG_ON(port >= PORT_COUNT);
815 BUG_ON(!bus_priv->devices[port]);
817 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
820 return IRQ_HANDLED;
823 static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
825 int result;
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"));
835 return result;
838 static int ps3_vuart_probe(struct device *_dev)
840 int result;
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__);
848 BUG_ON(!drv);
850 down(&vuart_bus_priv.probe_mutex);
852 /* Setup vuart_bus_priv.devices[]. */
854 result = ps3_vuart_match_id_to_port(dev->match_id,
855 &port_number);
857 if (result) {
858 dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
859 __func__, __LINE__, dev->match_id);
860 result = -EINVAL;
861 goto fail_match;
864 if (vuart_bus_priv.devices[port_number]) {
865 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
866 __LINE__, port_number);
867 result = -EBUSY;
868 goto fail_match;
871 vuart_bus_priv.devices[port_number] = dev;
873 /* Setup dev->priv. */
875 dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
877 if (!dev->priv) {
878 result = -ENOMEM;
879 goto fail_alloc;
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);
900 if (result) {
901 dev_dbg(&dev->core,
902 "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
903 __func__, __LINE__, result);
904 result = -EPERM;
905 goto fail_alloc_irq;
908 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
909 IRQF_DISABLED, "vuart", &vuart_bus_priv);
911 if (result) {
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);
926 if (drv->probe)
927 result = drv->probe(dev);
928 else {
929 result = 0;
930 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
931 __LINE__);
934 if (result) {
935 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
936 __func__, __LINE__);
937 down(&vuart_bus_priv.probe_mutex);
938 goto fail_probe;
941 up(&vuart_bus_priv.probe_mutex);
943 return result;
945 fail_probe:
946 ps3_vuart_set_interrupt_mask(dev, 0);
947 fail_request_irq:
948 ps3_free_vuart_irq(vuart_bus_priv.virq);
949 vuart_bus_priv.virq = NO_IRQ;
950 fail_alloc_irq:
951 --vuart_bus_priv.use_count;
952 kfree(dev->priv);
953 dev->priv = NULL;
954 fail_alloc:
955 vuart_bus_priv.devices[port_number] = NULL;
956 fail_match:
957 up(&vuart_bus_priv.probe_mutex);
958 dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
959 return result;
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__,
971 dev->core.bus_id);
973 BUG_ON(vuart_bus_priv.use_count < 1);
975 if (drv->remove)
976 drv->remove(dev);
977 else
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] = NULL;
983 if (--vuart_bus_priv.use_count == 0) {
984 BUG();
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;
990 kfree(dev->priv);
991 dev->priv = NULL;
993 up(&vuart_bus_priv.probe_mutex);
994 return 0;
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__,
1004 dev->core.bus_id);
1006 if (drv->shutdown)
1007 drv->shutdown(dev);
1008 else
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)
1029 int result;
1031 pr_debug("%s:%d:\n", __func__, __LINE__);
1033 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1034 return -ENODEV;
1036 init_MUTEX(&vuart_bus_priv.probe_mutex);
1037 result = bus_register(&ps3_vuart_bus);
1038 BUG_ON(result);
1040 return result;
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)
1058 #if defined(DEBUG)
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));
1065 #endif
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",
1083 dev_count++);
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)
1098 int result;
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);
1103 return result;
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);