0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/bitops.h>
0015 #include <asm/ps3.h>
0016
0017 #include <asm/firmware.h>
0018 #include <asm/lv1call.h>
0019
0020 #include "vuart.h"
0021
0022 MODULE_AUTHOR("Sony Corporation");
0023 MODULE_LICENSE("GPL v2");
0024 MODULE_DESCRIPTION("PS3 vuart");
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 enum {PORT_COUNT = 3,};
0038
0039 enum vuart_param {
0040 PARAM_TX_TRIGGER = 0,
0041 PARAM_RX_TRIGGER = 1,
0042 PARAM_INTERRUPT_MASK = 2,
0043 PARAM_RX_BUF_SIZE = 3,
0044 PARAM_RX_BYTES = 4,
0045 PARAM_TX_BUF_SIZE = 5,
0046 PARAM_TX_BYTES = 6,
0047 PARAM_INTERRUPT_STATUS = 7,
0048 };
0049
0050 enum vuart_interrupt_bit {
0051 INTERRUPT_BIT_TX = 0,
0052 INTERRUPT_BIT_RX = 1,
0053 INTERRUPT_BIT_DISCONNECT = 2,
0054 };
0055
0056 enum vuart_interrupt_mask {
0057 INTERRUPT_MASK_TX = 1,
0058 INTERRUPT_MASK_RX = 2,
0059 INTERRUPT_MASK_DISCONNECT = 4,
0060 };
0061
0062
0063
0064
0065
0066 struct ps3_vuart_port_priv {
0067 u64 interrupt_mask;
0068
0069 struct {
0070 spinlock_t lock;
0071 struct list_head head;
0072 } tx_list;
0073 struct {
0074 struct ps3_vuart_work work;
0075 unsigned long bytes_held;
0076 spinlock_t lock;
0077 struct list_head head;
0078 } rx_list;
0079 struct ps3_vuart_stats stats;
0080 };
0081
0082 static struct ps3_vuart_port_priv *to_port_priv(
0083 struct ps3_system_bus_device *dev)
0084 {
0085 BUG_ON(!dev);
0086 BUG_ON(!dev->driver_priv);
0087 return (struct ps3_vuart_port_priv *)dev->driver_priv;
0088 }
0089
0090
0091
0092
0093
0094
0095
0096
0097 struct ports_bmp {
0098 u64 status;
0099 u64 unused[3];
0100 } __attribute__((aligned(32)));
0101
0102 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
0103 static void __maybe_unused _dump_ports_bmp(
0104 const struct ports_bmp *bmp, const char *func, int line)
0105 {
0106 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
0107 }
0108
0109 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
0110 static void __maybe_unused _dump_port_params(unsigned int port_number,
0111 const char *func, int line)
0112 {
0113 #if defined(DEBUG)
0114 static const char *strings[] = {
0115 "tx_trigger ",
0116 "rx_trigger ",
0117 "interrupt_mask ",
0118 "rx_buf_size ",
0119 "rx_bytes ",
0120 "tx_buf_size ",
0121 "tx_bytes ",
0122 "interrupt_status",
0123 };
0124 int result;
0125 unsigned int i;
0126 u64 value;
0127
0128 for (i = 0; i < ARRAY_SIZE(strings); i++) {
0129 result = lv1_get_virtual_uart_param(port_number, i, &value);
0130
0131 if (result) {
0132 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
0133 port_number, strings[i], ps3_result(result));
0134 continue;
0135 }
0136 pr_debug("%s:%d: port_%u: %s = %lxh\n",
0137 func, line, port_number, strings[i], value);
0138 }
0139 #endif
0140 }
0141
0142 int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
0143 struct vuart_triggers *trig)
0144 {
0145 int result;
0146 u64 size;
0147 u64 val;
0148 u64 tx;
0149
0150 result = lv1_get_virtual_uart_param(dev->port_number,
0151 PARAM_TX_TRIGGER, &tx);
0152 trig->tx = tx;
0153
0154 if (result) {
0155 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
0156 __func__, __LINE__, ps3_result(result));
0157 return result;
0158 }
0159
0160 result = lv1_get_virtual_uart_param(dev->port_number,
0161 PARAM_RX_BUF_SIZE, &size);
0162
0163 if (result) {
0164 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
0165 __func__, __LINE__, ps3_result(result));
0166 return result;
0167 }
0168
0169 result = lv1_get_virtual_uart_param(dev->port_number,
0170 PARAM_RX_TRIGGER, &val);
0171
0172 if (result) {
0173 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
0174 __func__, __LINE__, ps3_result(result));
0175 return result;
0176 }
0177
0178 trig->rx = size - val;
0179
0180 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
0181 trig->tx, trig->rx);
0182
0183 return result;
0184 }
0185
0186 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
0187 unsigned int rx)
0188 {
0189 int result;
0190 u64 size;
0191
0192 result = lv1_set_virtual_uart_param(dev->port_number,
0193 PARAM_TX_TRIGGER, tx);
0194
0195 if (result) {
0196 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
0197 __func__, __LINE__, ps3_result(result));
0198 return result;
0199 }
0200
0201 result = lv1_get_virtual_uart_param(dev->port_number,
0202 PARAM_RX_BUF_SIZE, &size);
0203
0204 if (result) {
0205 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
0206 __func__, __LINE__, ps3_result(result));
0207 return result;
0208 }
0209
0210 result = lv1_set_virtual_uart_param(dev->port_number,
0211 PARAM_RX_TRIGGER, size - rx);
0212
0213 if (result) {
0214 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
0215 __func__, __LINE__, ps3_result(result));
0216 return result;
0217 }
0218
0219 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
0220 tx, rx);
0221
0222 return result;
0223 }
0224
0225 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
0226 u64 *bytes_waiting)
0227 {
0228 int result;
0229
0230 result = lv1_get_virtual_uart_param(dev->port_number,
0231 PARAM_RX_BYTES, bytes_waiting);
0232
0233 if (result)
0234 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
0235 __func__, __LINE__, ps3_result(result));
0236
0237 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
0238 *bytes_waiting);
0239 return result;
0240 }
0241
0242
0243
0244
0245
0246
0247
0248 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
0249 unsigned long mask)
0250 {
0251 int result;
0252 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0253
0254 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
0255
0256 priv->interrupt_mask = mask;
0257
0258 result = lv1_set_virtual_uart_param(dev->port_number,
0259 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
0260
0261 if (result)
0262 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
0263 __func__, __LINE__, ps3_result(result));
0264
0265 return result;
0266 }
0267
0268 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
0269 unsigned long *status)
0270 {
0271 int result;
0272 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0273 u64 tmp;
0274
0275 result = lv1_get_virtual_uart_param(dev->port_number,
0276 PARAM_INTERRUPT_STATUS, &tmp);
0277
0278 if (result)
0279 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
0280 __func__, __LINE__, ps3_result(result));
0281
0282 *status = tmp & priv->interrupt_mask;
0283
0284 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
0285 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
0286
0287 return result;
0288 }
0289
0290 int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
0291 {
0292 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0293
0294 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
0295 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
0296 | INTERRUPT_MASK_TX);
0297 }
0298
0299 int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
0300 {
0301 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0302
0303 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
0304 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
0305 | INTERRUPT_MASK_RX);
0306 }
0307
0308 int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
0309 {
0310 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0311
0312 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
0313 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
0314 | INTERRUPT_MASK_DISCONNECT);
0315 }
0316
0317 int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
0318 {
0319 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0320
0321 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
0322 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
0323 & ~INTERRUPT_MASK_TX) : 0;
0324 }
0325
0326 int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
0327 {
0328 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0329
0330 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
0331 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
0332 & ~INTERRUPT_MASK_RX) : 0;
0333 }
0334
0335 int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
0336 {
0337 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0338
0339 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
0340 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
0341 & ~INTERRUPT_MASK_DISCONNECT) : 0;
0342 }
0343
0344
0345
0346
0347
0348
0349
0350
0351 static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
0352 const void *buf, unsigned int bytes, u64 *bytes_written)
0353 {
0354 int result;
0355 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0356
0357 result = lv1_write_virtual_uart(dev->port_number,
0358 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
0359
0360 if (result) {
0361 dev_warn(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
0362 "%s\n", __func__, __LINE__, ps3_result(result));
0363 return result;
0364 }
0365
0366 priv->stats.bytes_written += *bytes_written;
0367
0368 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
0369 *bytes_written, bytes, priv->stats.bytes_written);
0370
0371 return result;
0372 }
0373
0374
0375
0376
0377
0378
0379
0380
0381 static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
0382 unsigned int bytes, u64 *bytes_read)
0383 {
0384 int result;
0385 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0386
0387 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
0388
0389 result = lv1_read_virtual_uart(dev->port_number,
0390 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
0391
0392 if (result) {
0393 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
0394 __func__, __LINE__, ps3_result(result));
0395 return result;
0396 }
0397
0398 priv->stats.bytes_read += *bytes_read;
0399
0400 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
0401 *bytes_read, bytes, priv->stats.bytes_read);
0402
0403 return result;
0404 }
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414 void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
0415 unsigned int bytes)
0416 {
0417 int result;
0418 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0419 u64 bytes_waiting;
0420 void *tmp;
0421
0422 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
0423
0424 BUG_ON(result);
0425
0426 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
0427
0428 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
0429
0430 if (!bytes)
0431 return;
0432
0433
0434
0435 bytes += 128;
0436
0437 tmp = kmalloc(bytes, GFP_KERNEL);
0438
0439 if (!tmp)
0440 return;
0441
0442 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
0443
0444 kfree(tmp);
0445
0446
0447
0448 priv->stats.bytes_read -= bytes_waiting;
0449 }
0450 EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
0451
0452
0453
0454
0455
0456 struct list_buffer {
0457 struct list_head link;
0458 const unsigned char *head;
0459 const unsigned char *tail;
0460 unsigned long dbg_number;
0461 unsigned char data[];
0462 };
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474 int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
0475 unsigned int bytes)
0476 {
0477 static unsigned long dbg_number;
0478 int result;
0479 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0480 unsigned long flags;
0481 struct list_buffer *lb;
0482
0483 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
0484 bytes, bytes);
0485
0486 spin_lock_irqsave(&priv->tx_list.lock, flags);
0487
0488 if (list_empty(&priv->tx_list.head)) {
0489 u64 bytes_written;
0490
0491 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
0492
0493 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
0494
0495 if (result) {
0496 dev_dbg(&dev->core,
0497 "%s:%d: ps3_vuart_raw_write failed\n",
0498 __func__, __LINE__);
0499 return result;
0500 }
0501
0502 if (bytes_written == bytes) {
0503 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
0504 __func__, __LINE__, bytes);
0505 return 0;
0506 }
0507
0508 bytes -= bytes_written;
0509 buf += bytes_written;
0510 } else
0511 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
0512
0513 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
0514
0515 if (!lb)
0516 return -ENOMEM;
0517
0518 memcpy(lb->data, buf, bytes);
0519 lb->head = lb->data;
0520 lb->tail = lb->data + bytes;
0521 lb->dbg_number = ++dbg_number;
0522
0523 spin_lock_irqsave(&priv->tx_list.lock, flags);
0524 list_add_tail(&lb->link, &priv->tx_list.head);
0525 ps3_vuart_enable_interrupt_tx(dev);
0526 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
0527
0528 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
0529 __func__, __LINE__, lb->dbg_number, bytes);
0530
0531 return 0;
0532 }
0533 EXPORT_SYMBOL_GPL(ps3_vuart_write);
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
0544 u64 *bytes_queued)
0545 {
0546 static unsigned long dbg_number;
0547 int result;
0548 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0549 struct list_buffer *lb;
0550 u64 bytes;
0551
0552 *bytes_queued = 0;
0553
0554 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
0555 BUG_ON(result);
0556
0557 if (result)
0558 return -EIO;
0559
0560 if (!bytes)
0561 return 0;
0562
0563
0564
0565 bytes += 128;
0566
0567 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
0568
0569 if (!lb)
0570 return -ENOMEM;
0571
0572 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
0573
0574 lb->head = lb->data;
0575 lb->tail = lb->data + bytes;
0576 lb->dbg_number = ++dbg_number;
0577
0578 list_add_tail(&lb->link, &priv->rx_list.head);
0579 priv->rx_list.bytes_held += bytes;
0580
0581 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
0582 __func__, __LINE__, lb->dbg_number, bytes);
0583
0584 *bytes_queued = bytes;
0585
0586 return 0;
0587 }
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598 int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
0599 unsigned int bytes)
0600 {
0601 int result;
0602 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0603 unsigned long flags;
0604 struct list_buffer *lb, *n;
0605 unsigned long bytes_read;
0606
0607 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
0608 bytes, bytes);
0609
0610 spin_lock_irqsave(&priv->rx_list.lock, flags);
0611
0612
0613
0614 while (priv->rx_list.bytes_held < bytes) {
0615 u64 tmp;
0616
0617 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
0618 if (result || !tmp) {
0619 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
0620 __func__, __LINE__,
0621 bytes - priv->rx_list.bytes_held);
0622 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0623 return -EAGAIN;
0624 }
0625 }
0626
0627 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
0628 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
0629
0630 memcpy(buf, lb->head, bytes_read);
0631 buf += bytes_read;
0632 bytes -= bytes_read;
0633 priv->rx_list.bytes_held -= bytes_read;
0634
0635 if (bytes_read < lb->tail - lb->head) {
0636 lb->head += bytes_read;
0637 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
0638 "bytes\n", __func__, __LINE__, lb->dbg_number,
0639 bytes_read);
0640 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0641 return 0;
0642 }
0643
0644 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
0645 "bytes\n", __func__, __LINE__, lb->dbg_number,
0646 bytes_read);
0647
0648 list_del(&lb->link);
0649 kfree(lb);
0650 }
0651
0652 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0653 return 0;
0654 }
0655 EXPORT_SYMBOL_GPL(ps3_vuart_read);
0656
0657
0658
0659
0660
0661 static void ps3_vuart_work(struct work_struct *work)
0662 {
0663 struct ps3_system_bus_device *dev =
0664 ps3_vuart_work_to_system_bus_dev(work);
0665 struct ps3_vuart_port_driver *drv =
0666 ps3_system_bus_dev_to_vuart_drv(dev);
0667
0668 BUG_ON(!drv);
0669 drv->work(dev);
0670 }
0671
0672 int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
0673 {
0674 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0675 unsigned long flags;
0676
0677 if (priv->rx_list.work.trigger) {
0678 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
0679 __func__, __LINE__);
0680 return -EAGAIN;
0681 }
0682
0683 BUG_ON(!bytes);
0684
0685 spin_lock_irqsave(&priv->rx_list.lock, flags);
0686 if (priv->rx_list.bytes_held >= bytes) {
0687 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
0688 __func__, __LINE__, bytes);
0689 schedule_work(&priv->rx_list.work.work);
0690 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0691 return 0;
0692 }
0693
0694 priv->rx_list.work.trigger = bytes;
0695 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0696
0697 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
0698 __LINE__, bytes, bytes);
0699
0700 return 0;
0701 }
0702 EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
0703
0704 void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
0705 {
0706 to_port_priv(dev)->rx_list.work.trigger = 0;
0707 }
0708 EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
0719 {
0720 int result = 0;
0721 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0722 unsigned long flags;
0723 struct list_buffer *lb, *n;
0724 unsigned long bytes_total = 0;
0725
0726 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0727
0728 spin_lock_irqsave(&priv->tx_list.lock, flags);
0729
0730 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
0731
0732 u64 bytes_written;
0733
0734 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
0735 &bytes_written);
0736
0737 if (result) {
0738 dev_dbg(&dev->core,
0739 "%s:%d: ps3_vuart_raw_write failed\n",
0740 __func__, __LINE__);
0741 break;
0742 }
0743
0744 bytes_total += bytes_written;
0745
0746 if (bytes_written < lb->tail - lb->head) {
0747 lb->head += bytes_written;
0748 dev_dbg(&dev->core,
0749 "%s:%d cleared buf_%lu, %llxh bytes\n",
0750 __func__, __LINE__, lb->dbg_number,
0751 bytes_written);
0752 goto port_full;
0753 }
0754
0755 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
0756 lb->dbg_number);
0757
0758 list_del(&lb->link);
0759 kfree(lb);
0760 }
0761
0762 ps3_vuart_disable_interrupt_tx(dev);
0763 port_full:
0764 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
0765 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
0766 __func__, __LINE__, bytes_total);
0767 return result;
0768 }
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
0779 {
0780 int result;
0781 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0782 unsigned long flags;
0783 u64 bytes;
0784
0785 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0786
0787 spin_lock_irqsave(&priv->rx_list.lock, flags);
0788 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
0789
0790 if (result) {
0791 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0792 return result;
0793 }
0794
0795 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
0796 >= priv->rx_list.work.trigger) {
0797 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
0798 __func__, __LINE__, priv->rx_list.work.trigger);
0799 priv->rx_list.work.trigger = 0;
0800 schedule_work(&priv->rx_list.work.work);
0801 }
0802
0803 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
0804 return result;
0805 }
0806
0807 static int ps3_vuart_handle_interrupt_disconnect(
0808 struct ps3_system_bus_device *dev)
0809 {
0810 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0811 BUG_ON("no support");
0812 return -1;
0813 }
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
0824 {
0825 int result;
0826 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
0827 unsigned long status;
0828
0829 result = ps3_vuart_get_interrupt_status(dev, &status);
0830
0831 if (result)
0832 return result;
0833
0834 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
0835 status);
0836
0837 if (status & INTERRUPT_MASK_DISCONNECT) {
0838 priv->stats.disconnect_interrupts++;
0839 result = ps3_vuart_handle_interrupt_disconnect(dev);
0840 if (result)
0841 ps3_vuart_disable_interrupt_disconnect(dev);
0842 }
0843
0844 if (status & INTERRUPT_MASK_TX) {
0845 priv->stats.tx_interrupts++;
0846 result = ps3_vuart_handle_interrupt_tx(dev);
0847 if (result)
0848 ps3_vuart_disable_interrupt_tx(dev);
0849 }
0850
0851 if (status & INTERRUPT_MASK_RX) {
0852 priv->stats.rx_interrupts++;
0853 result = ps3_vuart_handle_interrupt_rx(dev);
0854 if (result)
0855 ps3_vuart_disable_interrupt_rx(dev);
0856 }
0857
0858 return 0;
0859 }
0860
0861 static struct vuart_bus_priv {
0862 struct ports_bmp *bmp;
0863 unsigned int virq;
0864 struct mutex probe_mutex;
0865 int use_count;
0866 struct ps3_system_bus_device *devices[PORT_COUNT];
0867 } vuart_bus_priv;
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
0878 {
0879 struct vuart_bus_priv *bus_priv = _private;
0880
0881 BUG_ON(!bus_priv);
0882
0883 while (1) {
0884 unsigned int port;
0885
0886 dump_ports_bmp(bus_priv->bmp);
0887
0888 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
0889
0890 if (port == BITS_PER_LONG)
0891 break;
0892
0893 BUG_ON(port >= PORT_COUNT);
0894 BUG_ON(!bus_priv->devices[port]);
0895
0896 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
0897 }
0898
0899 return IRQ_HANDLED;
0900 }
0901
0902 static int ps3_vuart_bus_interrupt_get(void)
0903 {
0904 int result;
0905
0906 pr_debug(" -> %s:%d\n", __func__, __LINE__);
0907
0908 vuart_bus_priv.use_count++;
0909
0910 BUG_ON(vuart_bus_priv.use_count > 2);
0911
0912 if (vuart_bus_priv.use_count != 1)
0913 return 0;
0914
0915 BUG_ON(vuart_bus_priv.bmp);
0916
0917 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
0918
0919 if (!vuart_bus_priv.bmp) {
0920 result = -ENOMEM;
0921 goto fail_bmp_malloc;
0922 }
0923
0924 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
0925 &vuart_bus_priv.virq);
0926
0927 if (result) {
0928 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
0929 __func__, __LINE__, result);
0930 result = -EPERM;
0931 goto fail_alloc_irq;
0932 }
0933
0934 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
0935 0, "vuart", &vuart_bus_priv);
0936
0937 if (result) {
0938 pr_debug("%s:%d: request_irq failed (%d)\n",
0939 __func__, __LINE__, result);
0940 goto fail_request_irq;
0941 }
0942
0943 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
0944 return result;
0945
0946 fail_request_irq:
0947 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
0948 vuart_bus_priv.virq = 0;
0949 fail_alloc_irq:
0950 kfree(vuart_bus_priv.bmp);
0951 vuart_bus_priv.bmp = NULL;
0952 fail_bmp_malloc:
0953 vuart_bus_priv.use_count--;
0954 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
0955 return result;
0956 }
0957
0958 static int ps3_vuart_bus_interrupt_put(void)
0959 {
0960 pr_debug(" -> %s:%d\n", __func__, __LINE__);
0961
0962 vuart_bus_priv.use_count--;
0963
0964 BUG_ON(vuart_bus_priv.use_count < 0);
0965
0966 if (vuart_bus_priv.use_count != 0)
0967 return 0;
0968
0969 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
0970
0971 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
0972 vuart_bus_priv.virq = 0;
0973
0974 kfree(vuart_bus_priv.bmp);
0975 vuart_bus_priv.bmp = NULL;
0976
0977 pr_debug(" <- %s:%d\n", __func__, __LINE__);
0978 return 0;
0979 }
0980
0981 static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
0982 {
0983 int result;
0984 struct ps3_vuart_port_driver *drv;
0985 struct ps3_vuart_port_priv *priv = NULL;
0986
0987 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0988
0989 drv = ps3_system_bus_dev_to_vuart_drv(dev);
0990 BUG_ON(!drv);
0991
0992 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
0993 drv->core.core.name);
0994
0995 if (dev->port_number >= PORT_COUNT) {
0996 BUG();
0997 return -EINVAL;
0998 }
0999
1000 mutex_lock(&vuart_bus_priv.probe_mutex);
1001
1002 result = ps3_vuart_bus_interrupt_get();
1003
1004 if (result)
1005 goto fail_setup_interrupt;
1006
1007 if (vuart_bus_priv.devices[dev->port_number]) {
1008 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
1009 __LINE__, dev->port_number);
1010 result = -EBUSY;
1011 goto fail_busy;
1012 }
1013
1014 vuart_bus_priv.devices[dev->port_number] = dev;
1015
1016
1017
1018 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1019 GFP_KERNEL);
1020
1021 if (!dev->driver_priv) {
1022 result = -ENOMEM;
1023 goto fail_dev_malloc;
1024 }
1025
1026 priv = to_port_priv(dev);
1027
1028 INIT_LIST_HEAD(&priv->tx_list.head);
1029 spin_lock_init(&priv->tx_list.lock);
1030
1031 INIT_LIST_HEAD(&priv->rx_list.head);
1032 spin_lock_init(&priv->rx_list.lock);
1033
1034 INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work);
1035 priv->rx_list.work.trigger = 0;
1036 priv->rx_list.work.dev = dev;
1037
1038
1039
1040 ps3_vuart_clear_rx_bytes(dev, 0);
1041
1042 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
1043
1044 ps3_vuart_set_triggers(dev, 1, 1);
1045
1046 if (drv->probe)
1047 result = drv->probe(dev);
1048 else {
1049 result = 0;
1050 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1051 __LINE__);
1052 }
1053
1054 if (result) {
1055 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1056 __func__, __LINE__);
1057 goto fail_probe;
1058 }
1059
1060 mutex_unlock(&vuart_bus_priv.probe_mutex);
1061
1062 return result;
1063
1064 fail_probe:
1065 ps3_vuart_set_interrupt_mask(dev, 0);
1066 kfree(dev->driver_priv);
1067 dev->driver_priv = NULL;
1068 fail_dev_malloc:
1069 vuart_bus_priv.devices[dev->port_number] = NULL;
1070 fail_busy:
1071 ps3_vuart_bus_interrupt_put();
1072 fail_setup_interrupt:
1073 mutex_unlock(&vuart_bus_priv.probe_mutex);
1074 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
1075 return result;
1076 }
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087 static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
1088 {
1089 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1090
1091 ps3_vuart_cancel_async(dev);
1092 ps3_vuart_set_interrupt_mask(dev, 0);
1093 ps3_vuart_bus_interrupt_put();
1094 return 0;
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105 static void ps3_vuart_remove(struct ps3_system_bus_device *dev)
1106 {
1107 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1108 struct ps3_vuart_port_driver *drv;
1109
1110 BUG_ON(!dev);
1111
1112 mutex_lock(&vuart_bus_priv.probe_mutex);
1113
1114 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1115 dev->match_id);
1116
1117 if (!dev->core.driver) {
1118 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1119 __LINE__);
1120 mutex_unlock(&vuart_bus_priv.probe_mutex);
1121 return;
1122 }
1123
1124 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1125
1126 BUG_ON(!drv);
1127
1128 if (drv->remove) {
1129 drv->remove(dev);
1130 } else {
1131 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1132 __LINE__);
1133 BUG();
1134 }
1135
1136 ps3_vuart_cleanup(dev);
1137
1138 vuart_bus_priv.devices[dev->port_number] = NULL;
1139 kfree(priv);
1140 priv = NULL;
1141
1142 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1143 mutex_unlock(&vuart_bus_priv.probe_mutex);
1144 }
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 static void ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1157 {
1158 struct ps3_vuart_port_driver *drv;
1159
1160 BUG_ON(!dev);
1161
1162 mutex_lock(&vuart_bus_priv.probe_mutex);
1163
1164 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1165 dev->match_id);
1166
1167 if (!dev->core.driver) {
1168 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1169 __LINE__);
1170 mutex_unlock(&vuart_bus_priv.probe_mutex);
1171 return;
1172 }
1173
1174 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1175
1176 BUG_ON(!drv);
1177
1178 if (drv->shutdown)
1179 drv->shutdown(dev);
1180 else if (drv->remove) {
1181 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1182 __func__, __LINE__);
1183 drv->remove(dev);
1184 } else {
1185 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1186 __LINE__);
1187 BUG();
1188 }
1189
1190 ps3_vuart_cleanup(dev);
1191
1192 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1193
1194 mutex_unlock(&vuart_bus_priv.probe_mutex);
1195 }
1196
1197 static int __init ps3_vuart_bus_init(void)
1198 {
1199 pr_debug("%s:%d:\n", __func__, __LINE__);
1200
1201 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1202 return -ENODEV;
1203
1204 mutex_init(&vuart_bus_priv.probe_mutex);
1205
1206 return 0;
1207 }
1208
1209 static void __exit ps3_vuart_bus_exit(void)
1210 {
1211 pr_debug("%s:%d:\n", __func__, __LINE__);
1212 }
1213
1214 core_initcall(ps3_vuart_bus_init);
1215 module_exit(ps3_vuart_bus_exit);
1216
1217
1218
1219
1220
1221 int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1222 {
1223 int result;
1224
1225 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1226
1227 BUG_ON(!drv->core.match_id);
1228 BUG_ON(!drv->core.core.name);
1229
1230 drv->core.probe = ps3_vuart_probe;
1231 drv->core.remove = ps3_vuart_remove;
1232 drv->core.shutdown = ps3_vuart_shutdown;
1233
1234 result = ps3_system_bus_driver_register(&drv->core);
1235 return result;
1236 }
1237 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1238
1239
1240
1241
1242
1243 void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1244 {
1245 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1246 ps3_system_bus_driver_unregister(&drv->core);
1247 }
1248 EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);