Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[sfrench/cifs-2.6.git] / 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 <linux/workqueue.h>
25 #include <asm/ps3.h>
26
27 #include <asm/firmware.h>
28 #include <asm/lv1call.h>
29 #include <asm/bitops.h>
30
31 #include "vuart.h"
32
33 MODULE_AUTHOR("Sony Corporation");
34 MODULE_LICENSE("GPL v2");
35 MODULE_DESCRIPTION("PS3 vuart");
36
37 /**
38  * vuart - An inter-partition data link service.
39  *  port 0: PS3 AV Settings.
40  *  port 2: PS3 System Manager.
41  *
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.
46  */
47
48 enum {PORT_COUNT = 3,};
49
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 */
59 };
60
61 enum vuart_interrupt_bit {
62         INTERRUPT_BIT_TX = 0,
63         INTERRUPT_BIT_RX = 1,
64         INTERRUPT_BIT_DISCONNECT = 2,
65 };
66
67 enum vuart_interrupt_mask {
68         INTERRUPT_MASK_TX = 1,
69         INTERRUPT_MASK_RX = 2,
70         INTERRUPT_MASK_DISCONNECT = 4,
71 };
72
73 /**
74  * struct ports_bmp - bitmap indicating ports needing service.
75  *
76  * A 256 bit read only bitmap indicating ports needing service.  Do not write
77  * to these bits.  Must not cross a page boundary.
78  */
79
80 struct ports_bmp {
81         u64 status;
82         u64 unused[3];
83 } __attribute__ ((aligned (32)));
84
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)
88 {
89         pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
90 }
91
92 static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
93         unsigned int *port_number)
94 {
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;
106         };
107 }
108
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)
112 {
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",
123         };
124         int result;
125         unsigned int i;
126         u64 value;
127
128         for (i = 0; i < ARRAY_SIZE(strings); i++) {
129                 result = lv1_get_virtual_uart_param(port_number, i, &value);
130
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;
135                 }
136                 pr_debug("%s:%d: port_%u: %s = %lxh\n",
137                         func, line, port_number, strings[i], value);
138         }
139 #endif
140 }
141
142 struct vuart_triggers {
143         unsigned long rx;
144         unsigned long tx;
145 };
146
147 int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
148         struct vuart_triggers *trig)
149 {
150         int result;
151         unsigned long size;
152         unsigned long val;
153
154         result = lv1_get_virtual_uart_param(dev->priv->port_number,
155                 PARAM_TX_TRIGGER, &trig->tx);
156
157         if (result) {
158                 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
159                         __func__, __LINE__, ps3_result(result));
160                 return result;
161         }
162
163         result = lv1_get_virtual_uart_param(dev->priv->port_number,
164                 PARAM_RX_BUF_SIZE, &size);
165
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;
170         }
171
172         result = lv1_get_virtual_uart_param(dev->priv->port_number,
173                 PARAM_RX_TRIGGER, &val);
174
175         if (result) {
176                 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
177                         __func__, __LINE__, ps3_result(result));
178                 return result;
179         }
180
181         trig->rx = size - val;
182
183         dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
184                 trig->tx, trig->rx);
185
186         return result;
187 }
188
189 int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
190         unsigned int rx)
191 {
192         int result;
193         unsigned long size;
194
195         result = lv1_set_virtual_uart_param(dev->priv->port_number,
196                 PARAM_TX_TRIGGER, tx);
197
198         if (result) {
199                 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
200                         __func__, __LINE__, ps3_result(result));
201                 return result;
202         }
203
204         result = lv1_get_virtual_uart_param(dev->priv->port_number,
205                 PARAM_RX_BUF_SIZE, &size);
206
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;
211         }
212
213         result = lv1_set_virtual_uart_param(dev->priv->port_number,
214                 PARAM_RX_TRIGGER, size - rx);
215
216         if (result) {
217                 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
218                         __func__, __LINE__, ps3_result(result));
219                 return result;
220         }
221
222         dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
223                 tx, rx);
224
225         return result;
226 }
227
228 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
229         u64 *bytes_waiting)
230 {
231         int result = lv1_get_virtual_uart_param(dev->priv->port_number,
232                 PARAM_RX_BYTES, bytes_waiting);
233
234         if (result)
235                 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
236                         __func__, __LINE__, ps3_result(result));
237
238         dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
239                 *bytes_waiting);
240         return result;
241 }
242
243 static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
244         unsigned long mask)
245 {
246         int result;
247
248         dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
249
250         dev->priv->interrupt_mask = mask;
251
252         result = lv1_set_virtual_uart_param(dev->priv->port_number,
253                 PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
254
255         if (result)
256                 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
257                         __func__, __LINE__, ps3_result(result));
258
259         return result;
260 }
261
262 static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
263         unsigned long *status)
264 {
265         u64 tmp;
266         int result = lv1_get_virtual_uart_param(dev->priv->port_number,
267                 PARAM_INTERRUPT_STATUS, &tmp);
268
269         if (result)
270                 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
271                         __func__, __LINE__, ps3_result(result));
272
273         *status = tmp & dev->priv->interrupt_mask;
274
275         dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
276                 __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
277
278         return result;
279 }
280
281 int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
282 {
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);
286 }
287
288 int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
289 {
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);
293 }
294
295 int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
296 {
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);
300 }
301
302 int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
303 {
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;
307 }
308
309 int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
310 {
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;
314 }
315
316 int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
317 {
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;
321 }
322
323 /**
324  * ps3_vuart_raw_write - Low level write helper.
325  *
326  * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
327  */
328
329 static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
330         const void* buf, unsigned int bytes, unsigned long *bytes_written)
331 {
332         int result;
333
334         result = lv1_write_virtual_uart(dev->priv->port_number,
335                 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
336
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;
341         }
342
343         dev->priv->stats.bytes_written += *bytes_written;
344
345         dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
346                 *bytes_written, bytes, dev->priv->stats.bytes_written);
347
348         return result;
349 }
350
351 /**
352  * ps3_vuart_raw_read - Low level read helper.
353  *
354  * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
355  */
356
357 static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
358         unsigned int bytes, unsigned long *bytes_read)
359 {
360         int result;
361
362         dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
363
364         result = lv1_read_virtual_uart(dev->priv->port_number,
365                 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
366
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;
371         }
372
373         dev->priv->stats.bytes_read += *bytes_read;
374
375         dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
376                 *bytes_read, bytes, dev->priv->stats.bytes_read);
377
378         return result;
379 }
380
381 /**
382  * ps3_vuart_clear_rx_bytes - Discard bytes received.
383  * @bytes: Max byte count to discard, zero = all pending.
384  *
385  * Used to clear pending rx interrupt source.  Will not block.
386  */
387
388 void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
389         unsigned int bytes)
390 {
391         int result;
392         u64 bytes_waiting;
393         void* tmp;
394
395         result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
396
397         BUG_ON(result);
398
399         bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
400
401         dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
402
403         if (!bytes)
404                 return;
405
406         /* Add some extra space for recently arrived data. */
407
408         bytes += 128;
409
410         tmp = kmalloc(bytes, GFP_KERNEL);
411
412         if (!tmp)
413                 return;
414
415         ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
416
417         kfree(tmp);
418
419         /* Don't include these bytes in the stats. */
420
421         dev->priv->stats.bytes_read -= bytes_waiting;
422 }
423
424 /**
425  * struct list_buffer - An element for a port device fifo buffer list.
426  */
427
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[];
434 };
435
436 /**
437  * ps3_vuart_write - the entry point for writing data to a port
438  *
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.
443  */
444
445 int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
446         unsigned int bytes)
447 {
448         static unsigned long dbg_number;
449         int result;
450         unsigned long flags;
451         struct list_buffer *lb;
452
453         dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
454                 bytes, bytes);
455
456         spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
457
458         if (list_empty(&dev->priv->tx_list.head)) {
459                 unsigned long bytes_written;
460
461                 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
462
463                 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
464
465                 if (result) {
466                         dev_dbg(&dev->core,
467                                 "%s:%d: ps3_vuart_raw_write failed\n",
468                                 __func__, __LINE__);
469                         return result;
470                 }
471
472                 if (bytes_written == bytes) {
473                         dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
474                                 __func__, __LINE__, bytes);
475                         return 0;
476                 }
477
478                 bytes -= bytes_written;
479                 buf += bytes_written;
480         } else
481                 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
482
483         lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
484
485         if (!lb) {
486                 return -ENOMEM;
487         }
488
489         memcpy(lb->data, buf, bytes);
490         lb->head = lb->data;
491         lb->tail = lb->data + bytes;
492         lb->dbg_number = ++dbg_number;
493
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);
498
499         dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
500                 __func__, __LINE__, lb->dbg_number, bytes);
501
502         return 0;
503 }
504
505 /**
506  * ps3_vuart_read - the entry point for reading data from a port
507  *
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.
512  */
513
514 int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
515         unsigned int bytes)
516 {
517         unsigned long flags;
518         struct list_buffer *lb, *n;
519         unsigned long bytes_read;
520
521         dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
522                 bytes, bytes);
523
524         spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
525
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;
532         }
533
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);
536
537                 memcpy(buf, lb->head, bytes_read);
538                 buf += bytes_read;
539                 bytes -= bytes_read;
540                 dev->priv->rx_list.bytes_held -= bytes_read;
541
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;
549                 }
550
551                 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
552                         "bytes\n", __func__, __LINE__, lb->dbg_number,
553                         bytes_read);
554
555                 list_del(&lb->link);
556                 kfree(lb);
557         }
558
559         spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
560         return 0;
561 }
562
563 int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
564         unsigned int bytes)
565 {
566         unsigned long flags;
567
568         if(dev->priv->work.trigger) {
569                 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
570                         __func__, __LINE__);
571                 return -EAGAIN;
572         }
573
574         BUG_ON(!bytes);
575
576         PREPARE_WORK(&dev->priv->work.work, func);
577
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;
585         }
586
587         dev->priv->work.trigger = bytes;
588         spin_unlock_irqrestore(&dev->priv->work.lock, flags);
589
590         dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
591                 __LINE__, bytes, bytes);
592
593         return 0;
594 }
595
596 void ps3_vuart_cancel_async(struct ps3_vuart_port_device *dev)
597 {
598         dev->priv->work.trigger = 0;
599 }
600
601 /**
602  * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
603  *
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.
607  */
608
609 static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
610 {
611         int result = 0;
612         unsigned long flags;
613         struct list_buffer *lb, *n;
614         unsigned long bytes_total = 0;
615
616         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
617
618         spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
619
620         list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
621
622                 unsigned long bytes_written;
623
624                 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
625                         &bytes_written);
626
627                 if (result) {
628                         dev_dbg(&dev->core,
629                                 "%s:%d: ps3_vuart_raw_write failed\n",
630                                 __func__, __LINE__);
631                         break;
632                 }
633
634                 bytes_total += bytes_written;
635
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;
643                 }
644
645                 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
646                         lb->dbg_number);
647
648                 list_del(&lb->link);
649                 kfree(lb);
650         }
651
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;
658 }
659
660 /**
661  * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
662  *
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.
666  */
667
668 static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
669 {
670         static unsigned long dbg_number;
671         int result = 0;
672         unsigned long flags;
673         struct list_buffer *lb;
674         unsigned long bytes;
675
676         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
677
678         result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
679
680         if (result)
681                 return -EIO;
682
683         BUG_ON(!bytes);
684
685         /* Add some extra space for recently arrived data. */
686
687         bytes += 128;
688
689         lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
690
691         if (!lb)
692                 return -ENOMEM;
693
694         ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
695
696         lb->head = lb->data;
697         lb->tail = lb->data + bytes;
698         lb->dbg_number = ++dbg_number;
699
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);
704
705         dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
706                 __func__, __LINE__, lb->dbg_number, bytes);
707
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);
715         }
716         spin_unlock_irqrestore(&dev->priv->work.lock, flags);
717         return 0;
718 }
719
720 static int ps3_vuart_handle_interrupt_disconnect(
721         struct ps3_vuart_port_device *dev)
722 {
723         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
724         BUG_ON("no support");
725         return -1;
726 }
727
728 /**
729  * ps3_vuart_handle_port_interrupt - second stage interrupt handler
730  *
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.
734  */
735
736 static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
737 {
738         int result;
739         unsigned long status;
740
741         result = ps3_vuart_get_interrupt_status(dev, &status);
742
743         if (result)
744                 return result;
745
746         dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
747                 status);
748
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);
754         }
755
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);
761         }
762
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);
768         }
769
770         return 0;
771 }
772
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;
780
781 /**
782  * ps3_vuart_irq_handler - first stage interrupt handler
783  *
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.
787  */
788
789 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
790 {
791         struct vuart_bus_priv *bus_priv;
792
793         BUG_ON(!_private);
794         bus_priv = (struct vuart_bus_priv *)_private;
795
796         while (1) {
797                 unsigned int port;
798
799                 dump_ports_bmp(&bus_priv->bmp);
800
801                 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
802
803                 if (port == BITS_PER_LONG)
804                         break;
805
806                 BUG_ON(port >= PORT_COUNT);
807                 BUG_ON(!bus_priv->devices[port]);
808
809                 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
810         }
811
812         return IRQ_HANDLED;
813 }
814
815 static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
816 {
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);
820
821         result = dev->match_id == drv->match_id;
822
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"));
826
827         return result;
828 }
829
830 static int ps3_vuart_probe(struct device *_dev)
831 {
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);
837
838         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
839
840         BUG_ON(!drv);
841
842         down(&vuart_bus_priv.probe_mutex);
843
844         /* Setup vuart_bus_priv.devices[]. */
845
846         result = ps3_vuart_match_id_to_port(dev->match_id,
847                 &port_number);
848
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;
854         }
855
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;
861         }
862
863         vuart_bus_priv.devices[port_number] = dev;
864
865         /* Setup dev->priv. */
866
867         dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
868
869         if (!dev->priv) {
870                 result = -ENOMEM;
871                 goto fail_alloc;
872         }
873
874         dev->priv->port_number = port_number;
875
876         INIT_LIST_HEAD(&dev->priv->tx_list.head);
877         spin_lock_init(&dev->priv->tx_list.lock);
878
879         INIT_LIST_HEAD(&dev->priv->rx_list.head);
880         spin_lock_init(&dev->priv->rx_list.lock);
881
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;
886
887         if (++vuart_bus_priv.use_count == 1) {
888
889                 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY,
890                         (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
891
892                 if (result) {
893                         dev_dbg(&dev->core,
894                                 "%s:%d: ps3_vuart_irq_setup failed (%d)\n",
895                                 __func__, __LINE__, result);
896                         result = -EPERM;
897                         goto fail_alloc_irq;
898                 }
899
900                 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
901                         IRQF_DISABLED, "vuart", &vuart_bus_priv);
902
903                 if (result) {
904                         dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
905                                 __func__, __LINE__, result);
906                         goto fail_request_irq;
907                 }
908         }
909
910         /* clear stale pending interrupts */
911
912         ps3_vuart_clear_rx_bytes(dev, 0);
913
914         ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
915
916         ps3_vuart_set_triggers(dev, 1, 1);
917
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__);
924         }
925
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;
931         }
932
933         up(&vuart_bus_priv.probe_mutex);
934
935         return result;
936
937 fail_probe:
938         ps3_vuart_set_interrupt_mask(dev, 0);
939 fail_request_irq:
940         ps3_vuart_irq_destroy(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;
952 }
953
954 static int ps3_vuart_remove(struct device *_dev)
955 {
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);
959
960         down(&vuart_bus_priv.probe_mutex);
961
962         dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
963                 dev->core.bus_id);
964
965         BUG_ON(vuart_bus_priv.use_count < 1);
966
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);
972
973         vuart_bus_priv.devices[dev->priv->port_number] = NULL;
974
975         if (--vuart_bus_priv.use_count == 0) {
976                 BUG();
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;
980         }
981
982         kfree(dev->priv);
983         dev->priv = NULL;
984
985         up(&vuart_bus_priv.probe_mutex);
986         return 0;
987 }
988
989 static void ps3_vuart_shutdown(struct device *_dev)
990 {
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);
994
995         dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
996                 dev->core.bus_id);
997
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);
1003 }
1004
1005 /**
1006  * ps3_vuart_bus - The vuart bus instance.
1007  *
1008  * The vuart is managed as a bus that port devices connect to.
1009  */
1010
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,
1017 };
1018
1019 int __init ps3_vuart_bus_init(void)
1020 {
1021         int result;
1022
1023         pr_debug("%s:%d:\n", __func__, __LINE__);
1024
1025         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1026                 return -ENODEV;
1027
1028         init_MUTEX(&vuart_bus_priv.probe_mutex);
1029         result = bus_register(&ps3_vuart_bus);
1030         BUG_ON(result);
1031
1032         return result;
1033 }
1034
1035 void __exit ps3_vuart_bus_exit(void)
1036 {
1037         pr_debug("%s:%d:\n", __func__, __LINE__);
1038         bus_unregister(&ps3_vuart_bus);
1039 }
1040
1041 core_initcall(ps3_vuart_bus_init);
1042 module_exit(ps3_vuart_bus_exit);
1043
1044 /**
1045  * ps3_vuart_port_release_device - Remove a vuart port device.
1046  */
1047
1048 static void ps3_vuart_port_release_device(struct device *_dev)
1049 {
1050 #if defined(DEBUG)
1051         struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
1052
1053         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1054
1055         BUG_ON(dev->priv && "forgot to free");
1056         memset(&dev->core, 0, sizeof(dev->core));
1057 #endif
1058 }
1059
1060 /**
1061  * ps3_vuart_port_device_register - Add a vuart port device.
1062  */
1063
1064 int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
1065 {
1066         static unsigned int dev_count = 1;
1067
1068         BUG_ON(dev->priv && "forgot to free");
1069
1070         dev->core.parent = NULL;
1071         dev->core.bus = &ps3_vuart_bus;
1072         dev->core.release = ps3_vuart_port_release_device;
1073
1074         snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
1075                 dev_count++);
1076
1077         dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
1078
1079         return device_register(&dev->core);
1080 }
1081
1082 EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
1083
1084 /**
1085  * ps3_vuart_port_driver_register - Add a vuart port device driver.
1086  */
1087
1088 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1089 {
1090         int result;
1091
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;
1096 }
1097
1098 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1099
1100 /**
1101  * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1102  */
1103
1104 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1105 {
1106         pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
1107         driver_unregister(&drv->core);
1108 }
1109
1110 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);