[POWERPC] Use menuconfig objects II - Macintosh
[wrt350n-kernel.git] / drivers / ps3 / vuart.c
blob7d7cab1d91b460a7311db72c8869e77d8eaf9de1
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 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
86 static void __attribute__ ((unused)) _dump_ports_bmp(
87 const struct ports_bmp* bmp, const char* func, int line)
89 pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
92 static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
93 unsigned int *port_number)
95 switch(match_id) {
96 case PS3_MATCH_ID_AV_SETTINGS:
97 *port_number = 0;
98 return 0;
99 case PS3_MATCH_ID_SYSTEM_MANAGER:
100 *port_number = 2;
101 return 0;
102 default:
103 WARN_ON(1);
104 *port_number = UINT_MAX;
105 return -EINVAL;
109 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
110 static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
111 const char* func, int line)
113 #if defined(DEBUG)
114 static const char *strings[] = {
115 "tx_trigger ",
116 "rx_trigger ",
117 "interrupt_mask ",
118 "rx_buf_size ",
119 "rx_bytes ",
120 "tx_buf_size ",
121 "tx_bytes ",
122 "interrupt_status",
124 int result;
125 unsigned int i;
126 u64 value;
128 for (i = 0; i < ARRAY_SIZE(strings); i++) {
129 result = lv1_get_virtual_uart_param(port_number, i, &value);
131 if (result) {
132 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
133 port_number, strings[i], ps3_result(result));
134 continue;
136 pr_debug("%s:%d: port_%u: %s = %lxh\n",
137 func, line, port_number, strings[i], value);
139 #endif
142 struct vuart_triggers {
143 unsigned long rx;
144 unsigned long tx;
147 int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
148 struct vuart_triggers *trig)
150 int result;
151 unsigned long size;
152 unsigned long val;
154 result = lv1_get_virtual_uart_param(dev->priv->port_number,
155 PARAM_TX_TRIGGER, &trig->tx);
157 if (result) {
158 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
159 __func__, __LINE__, ps3_result(result));
160 return result;
163 result = lv1_get_virtual_uart_param(dev->priv->port_number,
164 PARAM_RX_BUF_SIZE, &size);
166 if (result) {
167 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
168 __func__, __LINE__, ps3_result(result));
169 return result;
172 result = lv1_get_virtual_uart_param(dev->priv->port_number,
173 PARAM_RX_TRIGGER, &val);
175 if (result) {
176 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
177 __func__, __LINE__, ps3_result(result));
178 return result;
181 trig->rx = size - val;
183 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
184 trig->tx, trig->rx);
186 return result;
189 int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
190 unsigned int rx)
192 int result;
193 unsigned long size;
195 result = lv1_set_virtual_uart_param(dev->priv->port_number,
196 PARAM_TX_TRIGGER, tx);
198 if (result) {
199 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
200 __func__, __LINE__, ps3_result(result));
201 return result;
204 result = lv1_get_virtual_uart_param(dev->priv->port_number,
205 PARAM_RX_BUF_SIZE, &size);
207 if (result) {
208 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
209 __func__, __LINE__, ps3_result(result));
210 return result;
213 result = lv1_set_virtual_uart_param(dev->priv->port_number,
214 PARAM_RX_TRIGGER, size - rx);
216 if (result) {
217 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
218 __func__, __LINE__, ps3_result(result));
219 return result;
222 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
223 tx, rx);
225 return result;
228 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
229 u64 *bytes_waiting)
231 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
232 PARAM_RX_BYTES, bytes_waiting);
234 if (result)
235 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
236 __func__, __LINE__, ps3_result(result));
238 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
239 *bytes_waiting);
240 return result;
243 static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
244 unsigned long mask)
246 int result;
248 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
250 dev->priv->interrupt_mask = mask;
252 result = lv1_set_virtual_uart_param(dev->priv->port_number,
253 PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
255 if (result)
256 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
257 __func__, __LINE__, ps3_result(result));
259 return result;
262 static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
263 unsigned long *status)
265 u64 tmp;
266 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
267 PARAM_INTERRUPT_STATUS, &tmp);
269 if (result)
270 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
271 __func__, __LINE__, ps3_result(result));
273 *status = tmp & dev->priv->interrupt_mask;
275 dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
276 __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
278 return result;
281 int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
283 return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
284 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
285 | INTERRUPT_MASK_TX);
288 int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
290 return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
291 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
292 | INTERRUPT_MASK_RX);
295 int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
297 return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
298 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
299 | INTERRUPT_MASK_DISCONNECT);
302 int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
304 return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX)
305 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
306 & ~INTERRUPT_MASK_TX) : 0;
309 int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
311 return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX)
312 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
313 & ~INTERRUPT_MASK_RX) : 0;
316 int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
318 return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
319 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
320 & ~INTERRUPT_MASK_DISCONNECT) : 0;
324 * ps3_vuart_raw_write - Low level write helper.
326 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
329 static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
330 const void* buf, unsigned int bytes, unsigned long *bytes_written)
332 int result;
334 result = lv1_write_virtual_uart(dev->priv->port_number,
335 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
337 if (result) {
338 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
339 "%s\n", __func__, __LINE__, ps3_result(result));
340 return result;
343 dev->priv->stats.bytes_written += *bytes_written;
345 dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
346 *bytes_written, bytes, dev->priv->stats.bytes_written);
348 return result;
352 * ps3_vuart_raw_read - Low level read helper.
354 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
357 static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
358 unsigned int bytes, unsigned long *bytes_read)
360 int result;
362 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
364 result = lv1_read_virtual_uart(dev->priv->port_number,
365 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
367 if (result) {
368 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
369 __func__, __LINE__, ps3_result(result));
370 return result;
373 dev->priv->stats.bytes_read += *bytes_read;
375 dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
376 *bytes_read, bytes, dev->priv->stats.bytes_read);
378 return result;
382 * ps3_vuart_clear_rx_bytes - Discard bytes received.
383 * @bytes: Max byte count to discard, zero = all pending.
385 * Used to clear pending rx interrupt source. Will not block.
388 void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
389 unsigned int bytes)
391 int result;
392 u64 bytes_waiting;
393 void* tmp;
395 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
397 BUG_ON(result);
399 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
401 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
403 if (!bytes)
404 return;
406 /* Add some extra space for recently arrived data. */
408 bytes += 128;
410 tmp = kmalloc(bytes, GFP_KERNEL);
412 if (!tmp)
413 return;
415 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
417 kfree(tmp);
419 /* Don't include these bytes in the stats. */
421 dev->priv->stats.bytes_read -= bytes_waiting;
425 * struct list_buffer - An element for a port device fifo buffer list.
428 struct list_buffer {
429 struct list_head link;
430 const unsigned char *head;
431 const unsigned char *tail;
432 unsigned long dbg_number;
433 unsigned char data[];
437 * ps3_vuart_write - the entry point for writing data to a port
439 * If the port is idle on entry as much of the incoming data is written to
440 * the port as the port will accept. Otherwise a list buffer is created
441 * and any remaning incoming data is copied to that buffer. The buffer is
442 * then enqueued for transmision via the transmit interrupt.
445 int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
446 unsigned int bytes)
448 static unsigned long dbg_number;
449 int result;
450 unsigned long flags;
451 struct list_buffer *lb;
453 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
454 bytes, bytes);
456 spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
458 if (list_empty(&dev->priv->tx_list.head)) {
459 unsigned long bytes_written;
461 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
463 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
465 if (result) {
466 dev_dbg(&dev->core,
467 "%s:%d: ps3_vuart_raw_write failed\n",
468 __func__, __LINE__);
469 return result;
472 if (bytes_written == bytes) {
473 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
474 __func__, __LINE__, bytes);
475 return 0;
478 bytes -= bytes_written;
479 buf += bytes_written;
480 } else
481 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
483 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
485 if (!lb) {
486 return -ENOMEM;
489 memcpy(lb->data, buf, bytes);
490 lb->head = lb->data;
491 lb->tail = lb->data + bytes;
492 lb->dbg_number = ++dbg_number;
494 spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
495 list_add_tail(&lb->link, &dev->priv->tx_list.head);
496 ps3_vuart_enable_interrupt_tx(dev);
497 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
499 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
500 __func__, __LINE__, lb->dbg_number, bytes);
502 return 0;
506 * ps3_vuart_read - the entry point for reading data from a port
508 * If enough bytes to satisfy the request are held in the buffer list those
509 * bytes are dequeued and copied to the caller's buffer. Emptied list buffers
510 * are retiered. If the request cannot be statified by bytes held in the list
511 * buffers -EAGAIN is returned.
514 int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
515 unsigned int bytes)
517 unsigned long flags;
518 struct list_buffer *lb, *n;
519 unsigned long bytes_read;
521 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
522 bytes, bytes);
524 spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
526 if (dev->priv->rx_list.bytes_held < bytes) {
527 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
528 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
529 __func__, __LINE__,
530 bytes - dev->priv->rx_list.bytes_held);
531 return -EAGAIN;
534 list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) {
535 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
537 memcpy(buf, lb->head, bytes_read);
538 buf += bytes_read;
539 bytes -= bytes_read;
540 dev->priv->rx_list.bytes_held -= bytes_read;
542 if (bytes_read < lb->tail - lb->head) {
543 lb->head += bytes_read;
544 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
545 "bytes\n", __func__, __LINE__, lb->dbg_number,
546 bytes_read);
547 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
548 return 0;
551 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
552 "bytes\n", __func__, __LINE__, lb->dbg_number,
553 bytes_read);
555 list_del(&lb->link);
556 kfree(lb);
559 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
560 return 0;
563 int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
564 unsigned int bytes)
566 unsigned long flags;
568 if(dev->priv->work.trigger) {
569 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
570 __func__, __LINE__);
571 return -EAGAIN;
574 BUG_ON(!bytes);
576 PREPARE_WORK(&dev->priv->work.work, func);
578 spin_lock_irqsave(&dev->priv->work.lock, flags);
579 if(dev->priv->rx_list.bytes_held >= bytes) {
580 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
581 __func__, __LINE__, bytes);
582 schedule_work(&dev->priv->work.work);
583 spin_unlock_irqrestore(&dev->priv->work.lock, flags);
584 return 0;
587 dev->priv->work.trigger = bytes;
588 spin_unlock_irqrestore(&dev->priv->work.lock, flags);
590 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
591 __LINE__, bytes, bytes);
593 return 0;
596 void ps3_vuart_cancel_async(struct ps3_vuart_port_device *dev)
598 dev->priv->work.trigger = 0;
602 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
604 * Services the transmit interrupt for the port. Writes as much data from the
605 * buffer list as the port will accept. Retires any emptied list buffers and
606 * adjusts the final list buffer state for a partial write.
609 static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
611 int result = 0;
612 unsigned long flags;
613 struct list_buffer *lb, *n;
614 unsigned long bytes_total = 0;
616 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
618 spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
620 list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
622 unsigned long bytes_written;
624 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
625 &bytes_written);
627 if (result) {
628 dev_dbg(&dev->core,
629 "%s:%d: ps3_vuart_raw_write failed\n",
630 __func__, __LINE__);
631 break;
634 bytes_total += bytes_written;
636 if (bytes_written < lb->tail - lb->head) {
637 lb->head += bytes_written;
638 dev_dbg(&dev->core,
639 "%s:%d cleared buf_%lu, %lxh bytes\n",
640 __func__, __LINE__, lb->dbg_number,
641 bytes_written);
642 goto port_full;
645 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
646 lb->dbg_number);
648 list_del(&lb->link);
649 kfree(lb);
652 ps3_vuart_disable_interrupt_tx(dev);
653 port_full:
654 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
655 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
656 __func__, __LINE__, bytes_total);
657 return result;
661 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
663 * Services the receive interrupt for the port. Creates a list buffer and
664 * copies all waiting port data to that buffer and enqueues the buffer in the
665 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
668 static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
670 static unsigned long dbg_number;
671 int result = 0;
672 unsigned long flags;
673 struct list_buffer *lb;
674 unsigned long bytes;
676 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
678 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
680 if (result)
681 return -EIO;
683 BUG_ON(!bytes);
685 /* Add some extra space for recently arrived data. */
687 bytes += 128;
689 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
691 if (!lb)
692 return -ENOMEM;
694 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
696 lb->head = lb->data;
697 lb->tail = lb->data + bytes;
698 lb->dbg_number = ++dbg_number;
700 spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
701 list_add_tail(&lb->link, &dev->priv->rx_list.head);
702 dev->priv->rx_list.bytes_held += bytes;
703 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
705 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
706 __func__, __LINE__, lb->dbg_number, bytes);
708 spin_lock_irqsave(&dev->priv->work.lock, flags);
709 if(dev->priv->work.trigger
710 && dev->priv->rx_list.bytes_held >= dev->priv->work.trigger) {
711 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
712 __func__, __LINE__, dev->priv->work.trigger);
713 dev->priv->work.trigger = 0;
714 schedule_work(&dev->priv->work.work);
716 spin_unlock_irqrestore(&dev->priv->work.lock, flags);
717 return 0;
720 static int ps3_vuart_handle_interrupt_disconnect(
721 struct ps3_vuart_port_device *dev)
723 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
724 BUG_ON("no support");
725 return -1;
729 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
731 * Services any pending interrupt types for the port. Passes control to the
732 * third stage type specific interrupt handler. Returns control to the first
733 * stage handler after one iteration.
736 static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
738 int result;
739 unsigned long status;
741 result = ps3_vuart_get_interrupt_status(dev, &status);
743 if (result)
744 return result;
746 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
747 status);
749 if (status & INTERRUPT_MASK_DISCONNECT) {
750 dev->priv->stats.disconnect_interrupts++;
751 result = ps3_vuart_handle_interrupt_disconnect(dev);
752 if (result)
753 ps3_vuart_disable_interrupt_disconnect(dev);
756 if (status & INTERRUPT_MASK_TX) {
757 dev->priv->stats.tx_interrupts++;
758 result = ps3_vuart_handle_interrupt_tx(dev);
759 if (result)
760 ps3_vuart_disable_interrupt_tx(dev);
763 if (status & INTERRUPT_MASK_RX) {
764 dev->priv->stats.rx_interrupts++;
765 result = ps3_vuart_handle_interrupt_rx(dev);
766 if (result)
767 ps3_vuart_disable_interrupt_rx(dev);
770 return 0;
773 struct vuart_bus_priv {
774 const struct ports_bmp bmp;
775 unsigned int virq;
776 struct semaphore probe_mutex;
777 int use_count;
778 struct ps3_vuart_port_device *devices[PORT_COUNT];
779 } static vuart_bus_priv;
782 * ps3_vuart_irq_handler - first stage interrupt handler
784 * Loops finding any interrupting port and its associated instance data.
785 * Passes control to the second stage port specific interrupt handler. Loops
786 * until all outstanding interrupts are serviced.
789 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
791 struct vuart_bus_priv *bus_priv;
793 BUG_ON(!_private);
794 bus_priv = (struct vuart_bus_priv *)_private;
796 while (1) {
797 unsigned int port;
799 dump_ports_bmp(&bus_priv->bmp);
801 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
803 if (port == BITS_PER_LONG)
804 break;
806 BUG_ON(port >= PORT_COUNT);
807 BUG_ON(!bus_priv->devices[port]);
809 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
812 return IRQ_HANDLED;
815 static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
817 int result;
818 struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
819 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
821 result = dev->match_id == drv->match_id;
823 dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
824 __LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
825 drv->core.name, (result ? "match" : "miss"));
827 return result;
830 static int ps3_vuart_probe(struct device *_dev)
832 int result;
833 unsigned int port_number;
834 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
835 struct ps3_vuart_port_driver *drv =
836 to_ps3_vuart_port_driver(_dev->driver);
838 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
840 BUG_ON(!drv);
842 down(&vuart_bus_priv.probe_mutex);
844 /* Setup vuart_bus_priv.devices[]. */
846 result = ps3_vuart_match_id_to_port(dev->match_id,
847 &port_number);
849 if (result) {
850 dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
851 __func__, __LINE__, dev->match_id);
852 result = -EINVAL;
853 goto fail_match;
856 if (vuart_bus_priv.devices[port_number]) {
857 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
858 __LINE__, port_number);
859 result = -EBUSY;
860 goto fail_match;
863 vuart_bus_priv.devices[port_number] = dev;
865 /* Setup dev->priv. */
867 dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
869 if (!dev->priv) {
870 result = -ENOMEM;
871 goto fail_alloc;
874 dev->priv->port_number = port_number;
876 INIT_LIST_HEAD(&dev->priv->tx_list.head);
877 spin_lock_init(&dev->priv->tx_list.lock);
879 INIT_LIST_HEAD(&dev->priv->rx_list.head);
880 spin_lock_init(&dev->priv->rx_list.lock);
882 INIT_WORK(&dev->priv->work.work, NULL);
883 spin_lock_init(&dev->priv->work.lock);
884 dev->priv->work.trigger = 0;
885 dev->priv->work.dev = dev;
887 if (++vuart_bus_priv.use_count == 1) {
889 result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY,
890 (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
892 if (result) {
893 dev_dbg(&dev->core,
894 "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
895 __func__, __LINE__, result);
896 result = -EPERM;
897 goto fail_alloc_irq;
900 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
901 IRQF_DISABLED, "vuart", &vuart_bus_priv);
903 if (result) {
904 dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
905 __func__, __LINE__, result);
906 goto fail_request_irq;
910 /* clear stale pending interrupts */
912 ps3_vuart_clear_rx_bytes(dev, 0);
914 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
916 ps3_vuart_set_triggers(dev, 1, 1);
918 if (drv->probe)
919 result = drv->probe(dev);
920 else {
921 result = 0;
922 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
923 __LINE__);
926 if (result) {
927 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
928 __func__, __LINE__);
929 down(&vuart_bus_priv.probe_mutex);
930 goto fail_probe;
933 up(&vuart_bus_priv.probe_mutex);
935 return result;
937 fail_probe:
938 ps3_vuart_set_interrupt_mask(dev, 0);
939 fail_request_irq:
940 ps3_free_vuart_irq(vuart_bus_priv.virq);
941 vuart_bus_priv.virq = NO_IRQ;
942 fail_alloc_irq:
943 --vuart_bus_priv.use_count;
944 kfree(dev->priv);
945 dev->priv = NULL;
946 fail_alloc:
947 vuart_bus_priv.devices[port_number] = NULL;
948 fail_match:
949 up(&vuart_bus_priv.probe_mutex);
950 dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
951 return result;
954 static int ps3_vuart_remove(struct device *_dev)
956 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
957 struct ps3_vuart_port_driver *drv =
958 to_ps3_vuart_port_driver(_dev->driver);
960 down(&vuart_bus_priv.probe_mutex);
962 dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
963 dev->core.bus_id);
965 BUG_ON(vuart_bus_priv.use_count < 1);
967 if (drv->remove)
968 drv->remove(dev);
969 else
970 dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
971 __LINE__, dev->core.bus_id);
973 vuart_bus_priv.devices[dev->priv->port_number] = NULL;
975 if (--vuart_bus_priv.use_count == 0) {
976 BUG();
977 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
978 ps3_free_vuart_irq(vuart_bus_priv.virq);
979 vuart_bus_priv.virq = NO_IRQ;
982 kfree(dev->priv);
983 dev->priv = NULL;
985 up(&vuart_bus_priv.probe_mutex);
986 return 0;
989 static void ps3_vuart_shutdown(struct device *_dev)
991 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
992 struct ps3_vuart_port_driver *drv =
993 to_ps3_vuart_port_driver(_dev->driver);
995 dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
996 dev->core.bus_id);
998 if (drv->shutdown)
999 drv->shutdown(dev);
1000 else
1001 dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__,
1002 __LINE__, dev->core.bus_id);
1006 * ps3_vuart_bus - The vuart bus instance.
1008 * The vuart is managed as a bus that port devices connect to.
1011 struct bus_type ps3_vuart_bus = {
1012 .name = "ps3_vuart",
1013 .match = ps3_vuart_match,
1014 .probe = ps3_vuart_probe,
1015 .remove = ps3_vuart_remove,
1016 .shutdown = ps3_vuart_shutdown,
1019 int __init ps3_vuart_bus_init(void)
1021 int result;
1023 pr_debug("%s:%d:\n", __func__, __LINE__);
1025 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1026 return -ENODEV;
1028 init_MUTEX(&vuart_bus_priv.probe_mutex);
1029 result = bus_register(&ps3_vuart_bus);
1030 BUG_ON(result);
1032 return result;
1035 void __exit ps3_vuart_bus_exit(void)
1037 pr_debug("%s:%d:\n", __func__, __LINE__);
1038 bus_unregister(&ps3_vuart_bus);
1041 core_initcall(ps3_vuart_bus_init);
1042 module_exit(ps3_vuart_bus_exit);
1045 * ps3_vuart_port_release_device - Remove a vuart port device.
1048 static void ps3_vuart_port_release_device(struct device *_dev)
1050 #if defined(DEBUG)
1051 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
1053 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1055 BUG_ON(dev->priv && "forgot to free");
1056 memset(&dev->core, 0, sizeof(dev->core));
1057 #endif
1061 * ps3_vuart_port_device_register - Add a vuart port device.
1064 int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
1066 static unsigned int dev_count = 1;
1068 BUG_ON(dev->priv && "forgot to free");
1070 dev->core.parent = NULL;
1071 dev->core.bus = &ps3_vuart_bus;
1072 dev->core.release = ps3_vuart_port_release_device;
1074 snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
1075 dev_count++);
1077 dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
1079 return device_register(&dev->core);
1082 EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
1085 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1088 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1090 int result;
1092 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
1093 drv->core.bus = &ps3_vuart_bus;
1094 result = driver_register(&drv->core);
1095 return result;
1098 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1101 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1104 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1106 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
1107 driver_unregister(&drv->core);
1110 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);