]> err.no Git - linux-2.6/blob - drivers/ps3/vuart.c
[POWERPC] PS3: Vuart cleanups
[linux-2.6] / drivers / ps3 / vuart.c
1 /*
2  *  PS3 virtual uart
3  *
4  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5  *  Copyright 2006 Sony Corp.
6  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <asm/ps3.h>
25
26 #include <asm/firmware.h>
27 #include <asm/lv1call.h>
28 #include <asm/bitops.h>
29
30 #include "vuart.h"
31
32 MODULE_AUTHOR("Sony Corporation");
33 MODULE_LICENSE("GPL v2");
34 MODULE_DESCRIPTION("PS3 vuart");
35
36 /**
37  * vuart - An inter-partition data link service.
38  *  port 0: PS3 AV Settings.
39  *  port 2: PS3 System Manager.
40  *
41  * The vuart provides a bi-directional byte stream data link between logical
42  * partitions.  Its primary role is as a communications link between the guest
43  * OS and the system policy module.  The current HV does not support any
44  * connections other than those listed.
45  */
46
47 enum {PORT_COUNT = 3,};
48
49 enum vuart_param {
50         PARAM_TX_TRIGGER = 0,
51         PARAM_RX_TRIGGER = 1,
52         PARAM_INTERRUPT_MASK = 2,
53         PARAM_RX_BUF_SIZE = 3, /* read only */
54         PARAM_RX_BYTES = 4, /* read only */
55         PARAM_TX_BUF_SIZE = 5, /* read only */
56         PARAM_TX_BYTES = 6, /* read only */
57         PARAM_INTERRUPT_STATUS = 7, /* read only */
58 };
59
60 enum vuart_interrupt_bit {
61         INTERRUPT_BIT_TX = 0,
62         INTERRUPT_BIT_RX = 1,
63         INTERRUPT_BIT_DISCONNECT = 2,
64 };
65
66 enum vuart_interrupt_mask {
67         INTERRUPT_MASK_TX = 1,
68         INTERRUPT_MASK_RX = 2,
69         INTERRUPT_MASK_DISCONNECT = 4,
70 };
71
72 /**
73  * struct ports_bmp - bitmap indicating ports needing service.
74  *
75  * A 256 bit read only bitmap indicating ports needing service.  Do not write
76  * to these bits.  Must not cross a page boundary.
77  */
78
79 struct ports_bmp {
80         u64 status;
81         u64 unused[3];
82 } __attribute__ ((aligned (32)));
83
84 /* redefine dev_dbg to do a syntax check */
85
86 #if !defined(DEBUG)
87 #undef dev_dbg
88 static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg(
89         const struct device *_dev, const char *fmt, ...) {return 0;}
90 #endif
91
92 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
93 static void __attribute__ ((unused)) _dump_ports_bmp(
94         const struct ports_bmp* bmp, const char* func, int line)
95 {
96         pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
97 }
98
99 static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
100         unsigned int *port_number)
101 {
102         switch(match_id) {
103         case PS3_MATCH_ID_AV_SETTINGS:
104                 *port_number = 0;
105                 return 0;
106         case PS3_MATCH_ID_SYSTEM_MANAGER:
107                 *port_number = 2;
108                 return 0;
109         default:
110                 WARN_ON(1);
111                 *port_number = UINT_MAX;
112                 return -EINVAL;
113         };
114 }
115
116 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
117 static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
118         const char* func, int line)
119 {
120 #if defined(DEBUG)
121         static const char *strings[] = {
122                 "tx_trigger      ",
123                 "rx_trigger      ",
124                 "interrupt_mask  ",
125                 "rx_buf_size     ",
126                 "rx_bytes        ",
127                 "tx_buf_size     ",
128                 "tx_bytes        ",
129                 "interrupt_status",
130         };
131         int result;
132         unsigned int i;
133         u64 value;
134
135         for (i = 0; i < ARRAY_SIZE(strings); i++) {
136                 result = lv1_get_virtual_uart_param(port_number, i, &value);
137
138                 if (result) {
139                         pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
140                                 port_number, strings[i], ps3_result(result));
141                         continue;
142                 }
143                 pr_debug("%s:%d: port_%u: %s = %lxh\n",
144                         func, line, port_number, strings[i], value);
145         }
146 #endif
147 }
148
149 struct vuart_triggers {
150         unsigned long rx;
151         unsigned long tx;
152 };
153
154 int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
155         struct vuart_triggers *trig)
156 {
157         int result;
158         unsigned long size;
159         unsigned long val;
160
161         result = lv1_get_virtual_uart_param(dev->priv->port_number,
162                 PARAM_TX_TRIGGER, &trig->tx);
163
164         if (result) {
165                 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
166                         __func__, __LINE__, ps3_result(result));
167                 return result;
168         }
169
170         result = lv1_get_virtual_uart_param(dev->priv->port_number,
171                 PARAM_RX_BUF_SIZE, &size);
172
173         if (result) {
174                 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
175                         __func__, __LINE__, ps3_result(result));
176                 return result;
177         }
178
179         result = lv1_get_virtual_uart_param(dev->priv->port_number,
180                 PARAM_RX_TRIGGER, &val);
181
182         if (result) {
183                 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
184                         __func__, __LINE__, ps3_result(result));
185                 return result;
186         }
187
188         trig->rx = size - val;
189
190         dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
191                 trig->tx, trig->rx);
192
193         return result;
194 }
195
196 int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
197         unsigned int rx)
198 {
199         int result;
200         unsigned long size;
201
202         result = lv1_set_virtual_uart_param(dev->priv->port_number,
203                 PARAM_TX_TRIGGER, tx);
204
205         if (result) {
206                 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
207                         __func__, __LINE__, ps3_result(result));
208                 return result;
209         }
210
211         result = lv1_get_virtual_uart_param(dev->priv->port_number,
212                 PARAM_RX_BUF_SIZE, &size);
213
214         if (result) {
215                 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
216                         __func__, __LINE__, ps3_result(result));
217                 return result;
218         }
219
220         result = lv1_set_virtual_uart_param(dev->priv->port_number,
221                 PARAM_RX_TRIGGER, size - rx);
222
223         if (result) {
224                 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
225                         __func__, __LINE__, ps3_result(result));
226                 return result;
227         }
228
229         dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
230                 tx, rx);
231
232         return result;
233 }
234
235 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
236         u64 *bytes_waiting)
237 {
238         int result = lv1_get_virtual_uart_param(dev->priv->port_number,
239                 PARAM_RX_BYTES, bytes_waiting);
240
241         if (result)
242                 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
243                         __func__, __LINE__, ps3_result(result));
244
245         dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
246                 *bytes_waiting);
247         return result;
248 }
249
250 static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
251         unsigned long mask)
252 {
253         int result;
254
255         dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
256
257         dev->priv->interrupt_mask = mask;
258
259         result = lv1_set_virtual_uart_param(dev->priv->port_number,
260                 PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
261
262         if (result)
263                 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
264                         __func__, __LINE__, ps3_result(result));
265
266         return result;
267 }
268
269 static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
270         unsigned long *status)
271 {
272         u64 tmp;
273         int result = lv1_get_virtual_uart_param(dev->priv->port_number,
274                 PARAM_INTERRUPT_STATUS, &tmp);
275
276         if (result)
277                 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
278                         __func__, __LINE__, ps3_result(result));
279
280         *status = tmp & dev->priv->interrupt_mask;
281
282         dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
283                 __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
284
285         return result;
286 }
287
288 int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
289 {
290         return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
291                 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
292                 | INTERRUPT_MASK_TX);
293 }
294
295 int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
296 {
297         return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
298                 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
299                 | INTERRUPT_MASK_RX);
300 }
301
302 int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
303 {
304         return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
305                 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
306                 | INTERRUPT_MASK_DISCONNECT);
307 }
308
309 int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
310 {
311         return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX)
312                 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
313                 & ~INTERRUPT_MASK_TX) : 0;
314 }
315
316 int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
317 {
318         return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX)
319                 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
320                 & ~INTERRUPT_MASK_RX) : 0;
321 }
322
323 int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
324 {
325         return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
326                 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
327                 & ~INTERRUPT_MASK_DISCONNECT) : 0;
328 }
329
330 /**
331  * ps3_vuart_raw_write - Low level write helper.
332  *
333  * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
334  */
335
336 static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
337         const void* buf, unsigned int bytes, unsigned long *bytes_written)
338 {
339         int result;
340
341         result = lv1_write_virtual_uart(dev->priv->port_number,
342                 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
343
344         if (result) {
345                 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
346                         "%s\n", __func__, __LINE__, ps3_result(result));
347                 return result;
348         }
349
350         dev->priv->stats.bytes_written += *bytes_written;
351
352         dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
353                 *bytes_written, bytes, dev->priv->stats.bytes_written);
354
355         return result;
356 }
357
358 /**
359  * ps3_vuart_raw_read - Low level read helper.
360  *
361  * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
362  */
363
364 static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
365         unsigned int bytes, unsigned long *bytes_read)
366 {
367         int result;
368
369         dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
370
371         result = lv1_read_virtual_uart(dev->priv->port_number,
372                 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
373
374         if (result) {
375                 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
376                         __func__, __LINE__, ps3_result(result));
377                 return result;
378         }
379
380         dev->priv->stats.bytes_read += *bytes_read;
381
382         dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
383                 *bytes_read, bytes, dev->priv->stats.bytes_read);
384
385         return result;
386 }
387
388 /**
389  * ps3_vuart_clear_rx_bytes - Discard bytes received.
390  * @bytes: Max byte count to discard, zero = all pending.
391  *
392  * Used to clear pending rx interrupt source.  Will not block.
393  */
394
395 void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
396         unsigned int bytes)
397 {
398         int result;
399         u64 bytes_waiting;
400         void* tmp;
401
402         result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
403
404         BUG_ON(result);
405
406         bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
407
408         dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
409
410         if (!bytes)
411                 return;
412
413         /* Add some extra space for recently arrived data. */
414
415         bytes += 128;
416
417         tmp = kmalloc(bytes, GFP_KERNEL);
418
419         if (!tmp)
420                 return;
421
422         ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
423
424         kfree(tmp);
425
426         /* Don't include these bytes in the stats. */
427
428         dev->priv->stats.bytes_read -= bytes_waiting;
429 }
430
431 /**
432  * struct list_buffer - An element for a port device fifo buffer list.
433  */
434
435 struct list_buffer {
436         struct list_head link;
437         const unsigned char *head;
438         const unsigned char *tail;
439         unsigned long dbg_number;
440         unsigned char data[];
441 };
442
443 /**
444  * ps3_vuart_write - the entry point for writing data to a port
445  *
446  * If the port is idle on entry as much of the incoming data is written to
447  * the port as the port will accept.  Otherwise a list buffer is created
448  * and any remaning incoming data is copied to that buffer.  The buffer is
449  * then enqueued for transmision via the transmit interrupt.
450  */
451
452 int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
453         unsigned int bytes)
454 {
455         static unsigned long dbg_number;
456         int result;
457         unsigned long flags;
458         struct list_buffer *lb;
459
460         dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
461                 bytes, bytes);
462
463         spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
464
465         if (list_empty(&dev->priv->tx_list.head)) {
466                 unsigned long bytes_written;
467
468                 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
469
470                 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
471
472                 if (result) {
473                         dev_dbg(&dev->core,
474                                 "%s:%d: ps3_vuart_raw_write failed\n",
475                                 __func__, __LINE__);
476                         return result;
477                 }
478
479                 if (bytes_written == bytes) {
480                         dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
481                                 __func__, __LINE__, bytes);
482                         return 0;
483                 }
484
485                 bytes -= bytes_written;
486                 buf += bytes_written;
487         } else
488                 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
489
490         lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
491
492         if (!lb) {
493                 return -ENOMEM;
494         }
495
496         memcpy(lb->data, buf, bytes);
497         lb->head = lb->data;
498         lb->tail = lb->data + bytes;
499         lb->dbg_number = ++dbg_number;
500
501         spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
502         list_add_tail(&lb->link, &dev->priv->tx_list.head);
503         ps3_vuart_enable_interrupt_tx(dev);
504         spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
505
506         dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
507                 __func__, __LINE__, lb->dbg_number, bytes);
508
509         return 0;
510 }
511
512 /**
513  * ps3_vuart_read - the entry point for reading data from a port
514  *
515  * If enough bytes to satisfy the request are held in the buffer list those
516  * bytes are dequeued and copied to the caller's buffer.  Emptied list buffers
517  * are retiered.  If the request cannot be statified by bytes held in the list
518  * buffers -EAGAIN is returned.
519  */
520
521 int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
522         unsigned int bytes)
523 {
524         unsigned long flags;
525         struct list_buffer *lb, *n;
526         unsigned long bytes_read;
527
528         dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
529                 bytes, bytes);
530
531         spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
532
533         if (dev->priv->rx_list.bytes_held < bytes) {
534                 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
535                 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
536                         __func__, __LINE__,
537                         bytes - dev->priv->rx_list.bytes_held);
538                 return -EAGAIN;
539         }
540
541         list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) {
542                 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
543
544                 memcpy(buf, lb->head, bytes_read);
545                 buf += bytes_read;
546                 bytes -= bytes_read;
547                 dev->priv->rx_list.bytes_held -= bytes_read;
548
549                 if (bytes_read < lb->tail - lb->head) {
550                         lb->head += bytes_read;
551                         dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
552                                 "bytes\n", __func__, __LINE__, lb->dbg_number,
553                                 bytes_read);
554                         spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
555                         return 0;
556                 }
557
558                 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
559                         "bytes\n", __func__, __LINE__, lb->dbg_number,
560                         bytes_read);
561
562                 list_del(&lb->link);
563                 kfree(lb);
564         }
565
566         spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
567         return 0;
568 }
569
570 /**
571  * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
572  *
573  * Services the transmit interrupt for the port.  Writes as much data from the
574  * buffer list as the port will accept.  Retires any emptied list buffers and
575  * adjusts the final list buffer state for a partial write.
576  */
577
578 static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
579 {
580         int result = 0;
581         unsigned long flags;
582         struct list_buffer *lb, *n;
583         unsigned long bytes_total = 0;
584
585         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
586
587         spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
588
589         list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
590
591                 unsigned long bytes_written;
592
593                 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
594                         &bytes_written);
595
596                 if (result) {
597                         dev_dbg(&dev->core,
598                                 "%s:%d: ps3_vuart_raw_write failed\n",
599                                 __func__, __LINE__);
600                         break;
601                 }
602
603                 bytes_total += bytes_written;
604
605                 if (bytes_written < lb->tail - lb->head) {
606                         lb->head += bytes_written;
607                         dev_dbg(&dev->core,
608                                 "%s:%d cleared buf_%lu, %lxh bytes\n",
609                                 __func__, __LINE__, lb->dbg_number,
610                                 bytes_written);
611                         goto port_full;
612                 }
613
614                 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
615                         lb->dbg_number);
616
617                 list_del(&lb->link);
618                 kfree(lb);
619         }
620
621         ps3_vuart_disable_interrupt_tx(dev);
622 port_full:
623         spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
624         dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
625                 __func__, __LINE__, bytes_total);
626         return result;
627 }
628
629 /**
630  * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
631  *
632  * Services the receive interrupt for the port.  Creates a list buffer and
633  * copies all waiting port data to that buffer and enqueues the buffer in the
634  * buffer list.  Buffer list data is dequeued via ps3_vuart_read.
635  */
636
637 static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
638 {
639         static unsigned long dbg_number;
640         int result = 0;
641         unsigned long flags;
642         struct list_buffer *lb;
643         unsigned long bytes;
644
645         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
646
647         result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
648
649         if (result)
650                 return -EIO;
651
652         BUG_ON(!bytes);
653
654         /* Add some extra space for recently arrived data. */
655
656         bytes += 128;
657
658         lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
659
660         if (!lb)
661                 return -ENOMEM;
662
663         ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
664
665         lb->head = lb->data;
666         lb->tail = lb->data + bytes;
667         lb->dbg_number = ++dbg_number;
668
669         spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
670         list_add_tail(&lb->link, &dev->priv->rx_list.head);
671         dev->priv->rx_list.bytes_held += bytes;
672         spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
673
674         dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
675                 __func__, __LINE__, lb->dbg_number, bytes);
676
677         return 0;
678 }
679
680 static int ps3_vuart_handle_interrupt_disconnect(
681         struct ps3_vuart_port_device *dev)
682 {
683         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
684         BUG_ON("no support");
685         return -1;
686 }
687
688 /**
689  * ps3_vuart_handle_port_interrupt - second stage interrupt handler
690  *
691  * Services any pending interrupt types for the port.  Passes control to the
692  * third stage type specific interrupt handler.  Returns control to the first
693  * stage handler after one iteration.
694  */
695
696 static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
697 {
698         int result;
699         unsigned long status;
700
701         result = ps3_vuart_get_interrupt_status(dev, &status);
702
703         if (result)
704                 return result;
705
706         dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
707                 status);
708
709         if (status & INTERRUPT_MASK_DISCONNECT) {
710                 dev->priv->stats.disconnect_interrupts++;
711                 result = ps3_vuart_handle_interrupt_disconnect(dev);
712                 if (result)
713                         ps3_vuart_disable_interrupt_disconnect(dev);
714         }
715
716         if (status & INTERRUPT_MASK_TX) {
717                 dev->priv->stats.tx_interrupts++;
718                 result = ps3_vuart_handle_interrupt_tx(dev);
719                 if (result)
720                         ps3_vuart_disable_interrupt_tx(dev);
721         }
722
723         if (status & INTERRUPT_MASK_RX) {
724                 dev->priv->stats.rx_interrupts++;
725                 result = ps3_vuart_handle_interrupt_rx(dev);
726                 if (result)
727                         ps3_vuart_disable_interrupt_rx(dev);
728         }
729
730         return 0;
731 }
732
733 struct vuart_bus_priv {
734         const struct ports_bmp bmp;
735         unsigned int virq;
736         struct semaphore probe_mutex;
737         int use_count;
738         struct ps3_vuart_port_device *devices[PORT_COUNT];
739 } static vuart_bus_priv;
740
741 /**
742  * ps3_vuart_irq_handler - first stage interrupt handler
743  *
744  * Loops finding any interrupting port and its associated instance data.
745  * Passes control to the second stage port specific interrupt handler.  Loops
746  * until all outstanding interrupts are serviced.
747  */
748
749 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
750 {
751         struct vuart_bus_priv *bus_priv;
752
753         BUG_ON(!_private);
754         bus_priv = (struct vuart_bus_priv *)_private;
755
756         while (1) {
757                 unsigned int port;
758
759                 dump_ports_bmp(&bus_priv->bmp);
760
761                 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
762
763                 if (port == BITS_PER_LONG)
764                         break;
765
766                 BUG_ON(port >= PORT_COUNT);
767                 BUG_ON(!bus_priv->devices[port]);
768
769                 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
770         }
771
772         return IRQ_HANDLED;
773 }
774
775 static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
776 {
777         int result;
778         struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
779         struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
780
781         result = dev->match_id == drv->match_id;
782
783         dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
784                 __LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
785                 drv->core.name, (result ? "match" : "miss"));
786
787         return result;
788 }
789
790 static int ps3_vuart_probe(struct device *_dev)
791 {
792         int result;
793         unsigned int port_number;
794         struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
795         struct ps3_vuart_port_driver *drv =
796                 to_ps3_vuart_port_driver(_dev->driver);
797
798         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
799
800         BUG_ON(!drv);
801
802         down(&vuart_bus_priv.probe_mutex);
803
804         /* Setup vuart_bus_priv.devices[]. */
805
806         result = ps3_vuart_match_id_to_port(dev->match_id,
807                 &port_number);
808
809         if (result) {
810                 dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
811                         __func__, __LINE__, dev->match_id);
812                 result = -EINVAL;
813                 goto fail_match;
814         }
815
816         if (vuart_bus_priv.devices[port_number]) {
817                 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
818                         __LINE__, port_number);
819                 result = -EBUSY;
820                 goto fail_match;
821         }
822
823         vuart_bus_priv.devices[port_number] = dev;
824
825         /* Setup dev->priv. */
826
827         dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
828
829         if (!dev->priv) {
830                 result = -ENOMEM;
831                 goto fail_alloc;
832         }
833
834         dev->priv->port_number = port_number;
835
836         INIT_LIST_HEAD(&dev->priv->tx_list.head);
837         spin_lock_init(&dev->priv->tx_list.lock);
838
839         INIT_LIST_HEAD(&dev->priv->rx_list.head);
840         spin_lock_init(&dev->priv->rx_list.lock);
841
842         if (++vuart_bus_priv.use_count == 1) {
843
844                 result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY,
845                         (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
846
847                 if (result) {
848                         dev_dbg(&dev->core,
849                                 "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
850                                 __func__, __LINE__, result);
851                         result = -EPERM;
852                         goto fail_alloc_irq;
853                 }
854
855                 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
856                         IRQF_DISABLED, "vuart", &vuart_bus_priv);
857
858                 if (result) {
859                         dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
860                                 __func__, __LINE__, result);
861                         goto fail_request_irq;
862                 }
863         }
864
865         /* clear stale pending interrupts */
866
867         ps3_vuart_clear_rx_bytes(dev, 0);
868
869         ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
870
871         ps3_vuart_set_triggers(dev, 1, 1);
872
873         if (drv->probe)
874                 result = drv->probe(dev);
875         else {
876                 result = 0;
877                 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
878                         __LINE__);
879         }
880
881         if (result) {
882                 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
883                         __func__, __LINE__);
884                 down(&vuart_bus_priv.probe_mutex);
885                 goto fail_probe;
886         }
887
888         up(&vuart_bus_priv.probe_mutex);
889
890         return result;
891
892 fail_probe:
893         ps3_vuart_set_interrupt_mask(dev, 0);
894 fail_request_irq:
895         ps3_free_vuart_irq(vuart_bus_priv.virq);
896         vuart_bus_priv.virq = NO_IRQ;
897 fail_alloc_irq:
898         --vuart_bus_priv.use_count;
899         kfree(dev->priv);
900         dev->priv = NULL;
901 fail_alloc:
902         vuart_bus_priv.devices[port_number] = 0;
903 fail_match:
904         up(&vuart_bus_priv.probe_mutex);
905         dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
906         return result;
907 }
908
909 static int ps3_vuart_remove(struct device *_dev)
910 {
911         struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
912         struct ps3_vuart_port_driver *drv =
913                 to_ps3_vuart_port_driver(_dev->driver);
914
915         down(&vuart_bus_priv.probe_mutex);
916
917         dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
918                 dev->core.bus_id);
919
920         BUG_ON(vuart_bus_priv.use_count < 1);
921
922         if (drv->remove)
923                 drv->remove(dev);
924         else
925                 dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
926                         __LINE__, dev->core.bus_id);
927
928         vuart_bus_priv.devices[dev->priv->port_number] = 0;
929
930         if (--vuart_bus_priv.use_count == 0) {
931                 BUG();
932                 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
933                 ps3_free_vuart_irq(vuart_bus_priv.virq);
934                 vuart_bus_priv.virq = NO_IRQ;
935         }
936
937         kfree(dev->priv);
938         dev->priv = NULL;
939
940         up(&vuart_bus_priv.probe_mutex);
941         return 0;
942 }
943
944 static void ps3_vuart_shutdown(struct device *_dev)
945 {
946         struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
947         struct ps3_vuart_port_driver *drv =
948                 to_ps3_vuart_port_driver(_dev->driver);
949
950         dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
951                 dev->core.bus_id);
952
953         if (drv->shutdown)
954                 drv->shutdown(dev);
955         else
956                 dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__,
957                         __LINE__, dev->core.bus_id);
958 }
959
960 /**
961  * ps3_vuart_bus - The vuart bus instance.
962  *
963  * The vuart is managed as a bus that port devices connect to.
964  */
965
966 struct bus_type ps3_vuart_bus = {
967         .name = "ps3_vuart",
968         .match = ps3_vuart_match,
969         .probe = ps3_vuart_probe,
970         .remove = ps3_vuart_remove,
971         .shutdown = ps3_vuart_shutdown,
972 };
973
974 int __init ps3_vuart_bus_init(void)
975 {
976         int result;
977
978         pr_debug("%s:%d:\n", __func__, __LINE__);
979
980         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
981                 return 0;
982
983         init_MUTEX(&vuart_bus_priv.probe_mutex);
984         result = bus_register(&ps3_vuart_bus);
985         BUG_ON(result);
986
987         return result;
988 }
989
990 void __exit ps3_vuart_bus_exit(void)
991 {
992         pr_debug("%s:%d:\n", __func__, __LINE__);
993         bus_unregister(&ps3_vuart_bus);
994 }
995
996 core_initcall(ps3_vuart_bus_init);
997 module_exit(ps3_vuart_bus_exit);
998
999 /**
1000  * ps3_vuart_port_release_device - Remove a vuart port device.
1001  */
1002
1003 static void ps3_vuart_port_release_device(struct device *_dev)
1004 {
1005 #if defined(DEBUG)
1006         struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
1007
1008         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1009
1010         BUG_ON(dev->priv && "forgot to free");
1011         memset(&dev->core, 0, sizeof(dev->core));
1012 #endif
1013 }
1014
1015 /**
1016  * ps3_vuart_port_device_register - Add a vuart port device.
1017  */
1018
1019 int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
1020 {
1021         static unsigned int dev_count = 1;
1022
1023         BUG_ON(dev->priv && "forgot to free");
1024
1025         dev->core.parent = NULL;
1026         dev->core.bus = &ps3_vuart_bus;
1027         dev->core.release = ps3_vuart_port_release_device;
1028
1029         snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
1030                 dev_count++);
1031
1032         dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
1033
1034         return device_register(&dev->core);
1035 }
1036
1037 EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
1038
1039 /**
1040  * ps3_vuart_port_driver_register - Add a vuart port device driver.
1041  */
1042
1043 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1044 {
1045         int result;
1046
1047         pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
1048         drv->core.bus = &ps3_vuart_bus;
1049         result = driver_register(&drv->core);
1050         return result;
1051 }
1052
1053 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1054
1055 /**
1056  * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1057  */
1058
1059 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1060 {
1061         pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
1062         driver_unregister(&drv->core);
1063 }
1064
1065 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);