4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
27 #include <asm/firmware.h>
28 #include <asm/lv1call.h>
29 #include <asm/bitops.h>
33 MODULE_AUTHOR("Sony Corporation");
34 MODULE_LICENSE("GPL v2");
35 MODULE_DESCRIPTION("PS3 vuart");
38 * vuart - An inter-partition data link service.
39 * port 0: PS3 AV Settings.
40 * port 2: PS3 System Manager.
42 * The vuart provides a bi-directional byte stream data link between logical
43 * partitions. Its primary role is as a communications link between the guest
44 * OS and the system policy module. The current HV does not support any
45 * connections other than those listed.
48 enum {PORT_COUNT = 3,};
53 PARAM_INTERRUPT_MASK = 2,
54 PARAM_RX_BUF_SIZE = 3, /* read only */
55 PARAM_RX_BYTES = 4, /* read only */
56 PARAM_TX_BUF_SIZE = 5, /* read only */
57 PARAM_TX_BYTES = 6, /* read only */
58 PARAM_INTERRUPT_STATUS = 7, /* read only */
61 enum vuart_interrupt_bit {
64 INTERRUPT_BIT_DISCONNECT = 2,
67 enum vuart_interrupt_mask {
68 INTERRUPT_MASK_TX = 1,
69 INTERRUPT_MASK_RX = 2,
70 INTERRUPT_MASK_DISCONNECT = 4,
74 * struct 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.
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)
96 case PS3_MATCH_ID_AV_SETTINGS:
99 case PS3_MATCH_ID_SYSTEM_MANAGER:
104 *port_number = UINT_MAX;
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)
114 static const char *strings[] = {
128 for (i = 0; i < ARRAY_SIZE(strings); i++) {
129 result = lv1_get_virtual_uart_param(port_number, i, &value);
132 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
133 port_number, strings[i], ps3_result(result));
136 pr_debug("%s:%d: port_%u: %s = %lxh\n",
137 func, line, port_number, strings[i], value);
142 struct vuart_triggers {
147 int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
148 struct vuart_triggers *trig)
154 result = lv1_get_virtual_uart_param(dev->priv->port_number,
155 PARAM_TX_TRIGGER, &trig->tx);
158 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
159 __func__, __LINE__, ps3_result(result));
163 result = lv1_get_virtual_uart_param(dev->priv->port_number,
164 PARAM_RX_BUF_SIZE, &size);
167 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
168 __func__, __LINE__, ps3_result(result));
172 result = lv1_get_virtual_uart_param(dev->priv->port_number,
173 PARAM_RX_TRIGGER, &val);
176 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
177 __func__, __LINE__, ps3_result(result));
181 trig->rx = size - val;
183 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
189 int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
195 result = lv1_set_virtual_uart_param(dev->priv->port_number,
196 PARAM_TX_TRIGGER, tx);
199 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
200 __func__, __LINE__, ps3_result(result));
204 result = lv1_get_virtual_uart_param(dev->priv->port_number,
205 PARAM_RX_BUF_SIZE, &size);
208 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
209 __func__, __LINE__, ps3_result(result));
213 result = lv1_set_virtual_uart_param(dev->priv->port_number,
214 PARAM_RX_TRIGGER, size - rx);
217 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
218 __func__, __LINE__, ps3_result(result));
222 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
228 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
231 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
232 PARAM_RX_BYTES, bytes_waiting);
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__,
243 static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
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);
256 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
257 __func__, __LINE__, ps3_result(result));
262 static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
263 unsigned long *status)
266 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
267 PARAM_INTERRUPT_STATUS, &tmp);
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);
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)
334 result = lv1_write_virtual_uart(dev->priv->port_number,
335 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
338 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
339 "%s\n", __func__, __LINE__, ps3_result(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);
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)
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);
368 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
369 __func__, __LINE__, ps3_result(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);
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,
395 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
399 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
401 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
406 /* Add some extra space for recently arrived data. */
410 tmp = kmalloc(bytes, GFP_KERNEL);
415 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
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.
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,
448 static unsigned long dbg_number;
451 struct list_buffer *lb;
453 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
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);
467 "%s:%d: ps3_vuart_raw_write failed\n",
472 if (bytes_written == bytes) {
473 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
474 __func__, __LINE__, bytes);
478 bytes -= bytes_written;
479 buf += bytes_written;
481 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
483 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
489 memcpy(lb->data, buf, bytes);
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);
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,
518 struct list_buffer *lb, *n;
519 unsigned long bytes_read;
521 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
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",
530 bytes - dev->priv->rx_list.bytes_held);
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);
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,
547 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
551 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
552 "bytes\n", __func__, __LINE__, lb->dbg_number,
559 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
563 int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
568 if(dev->priv->work.trigger) {
569 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
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);
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);
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)
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,
629 "%s:%d: ps3_vuart_raw_write failed\n",
634 bytes_total += bytes_written;
636 if (bytes_written < lb->tail - lb->head) {
637 lb->head += bytes_written;
639 "%s:%d cleared buf_%lu, %lxh bytes\n",
640 __func__, __LINE__, lb->dbg_number,
645 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
652 ps3_vuart_disable_interrupt_tx(dev);
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);
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;
673 struct list_buffer *lb;
676 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
678 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
685 /* Add some extra space for recently arrived data. */
689 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
694 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
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);
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");
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)
739 unsigned long status;
741 result = ps3_vuart_get_interrupt_status(dev, &status);
746 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
749 if (status & INTERRUPT_MASK_DISCONNECT) {
750 dev->priv->stats.disconnect_interrupts++;
751 result = ps3_vuart_handle_interrupt_disconnect(dev);
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);
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);
767 ps3_vuart_disable_interrupt_rx(dev);
773 struct vuart_bus_priv {
774 const struct ports_bmp bmp;
776 struct semaphore probe_mutex;
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;
794 bus_priv = (struct vuart_bus_priv *)_private;
799 dump_ports_bmp(&bus_priv->bmp);
801 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
803 if (port == BITS_PER_LONG)
806 BUG_ON(port >= PORT_COUNT);
807 BUG_ON(!bus_priv->devices[port]);
809 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
815 static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
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"));
830 static int ps3_vuart_probe(struct device *_dev)
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__);
842 down(&vuart_bus_priv.probe_mutex);
844 /* Setup vuart_bus_priv.devices[]. */
846 result = ps3_vuart_match_id_to_port(dev->match_id,
850 dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
851 __func__, __LINE__, dev->match_id);
856 if (vuart_bus_priv.devices[port_number]) {
857 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
858 __LINE__, port_number);
863 vuart_bus_priv.devices[port_number] = dev;
865 /* Setup dev->priv. */
867 dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
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_vuart_irq_setup(PS3_BINDING_CPU_ANY,
890 (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
894 "%s:%d: ps3_vuart_irq_setup failed (%d)\n",
895 __func__, __LINE__, result);
900 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
901 IRQF_DISABLED, "vuart", &vuart_bus_priv);
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);
919 result = drv->probe(dev);
922 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
927 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
929 down(&vuart_bus_priv.probe_mutex);
933 up(&vuart_bus_priv.probe_mutex);
938 ps3_vuart_set_interrupt_mask(dev, 0);
940 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
941 vuart_bus_priv.virq = NO_IRQ;
943 --vuart_bus_priv.use_count;
947 vuart_bus_priv.devices[port_number] = NULL;
949 up(&vuart_bus_priv.probe_mutex);
950 dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
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__,
965 BUG_ON(vuart_bus_priv.use_count < 1);
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) {
977 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
978 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
979 vuart_bus_priv.virq = NO_IRQ;
985 up(&vuart_bus_priv.probe_mutex);
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__,
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)
1023 pr_debug("%s:%d:\n", __func__, __LINE__);
1025 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1028 init_MUTEX(&vuart_bus_priv.probe_mutex);
1029 result = bus_register(&ps3_vuart_bus);
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)
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));
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",
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)
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);
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);