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