0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/sched.h>
0011 #include <linux/slab.h>
0012 #include <linux/device.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/rculist.h>
0015 #include <linux/workqueue.h>
0016 #include <linux/ratelimit.h>
0017 #include <linux/bug.h>
0018 #include <linux/uaccess.h>
0019
0020 #include "fwserial.h"
0021
0022 inline u64 be32_to_u64(__be32 hi, __be32 lo)
0023 {
0024 return ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo));
0025 }
0026
0027 #define LINUX_VENDOR_ID 0xd00d1eU
0028 #define FWSERIAL_VERSION 0x00e81cU
0029
0030
0031 static int num_ttys = 4;
0032
0033 static bool auto_connect = true;
0034 static bool create_loop_dev = true;
0035
0036 module_param_named(ttys, num_ttys, int, 0644);
0037 module_param_named(auto, auto_connect, bool, 0644);
0038 module_param_named(loop, create_loop_dev, bool, 0644);
0039
0040
0041
0042
0043
0044
0045
0046 #define WAKEUP_CHARS 256
0047
0048
0049
0050
0051
0052 static LIST_HEAD(fwserial_list);
0053 static DEFINE_MUTEX(fwserial_list_mutex);
0054
0055
0056
0057
0058
0059
0060
0061
0062 static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
0063 static DEFINE_MUTEX(port_table_lock);
0064 static bool port_table_corrupt;
0065 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
0066
0067 #define loop_idx(port) (((port)->index) / num_ports)
0068 #define table_idx(loop) ((loop) * num_ports + num_ttys)
0069
0070
0071 static int num_ports;
0072
0073
0074 static struct kmem_cache *fwtty_txn_cache;
0075
0076 struct tty_driver *fwtty_driver;
0077 static struct tty_driver *fwloop_driver;
0078
0079 static struct dentry *fwserial_debugfs;
0080
0081 struct fwtty_transaction;
0082 typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
0083 void *data, size_t length,
0084 struct fwtty_transaction *txn);
0085
0086 struct fwtty_transaction {
0087 struct fw_transaction fw_txn;
0088 fwtty_transaction_cb callback;
0089 struct fwtty_port *port;
0090 union {
0091 struct dma_pending dma_pended;
0092 };
0093 };
0094
0095 #define to_device(a, b) (a->b)
0096 #define fwtty_err(p, fmt, ...) \
0097 dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
0098 #define fwtty_info(p, fmt, ...) \
0099 dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
0100 #define fwtty_notice(p, fmt, ...) \
0101 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
0102 #define fwtty_dbg(p, fmt, ...) \
0103 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
0104 #define fwtty_err_ratelimited(p, fmt, ...) \
0105 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
0106
0107 #ifdef DEBUG
0108 static inline void debug_short_write(struct fwtty_port *port, int c, int n)
0109 {
0110 int avail;
0111
0112 if (n < c) {
0113 spin_lock_bh(&port->lock);
0114 avail = dma_fifo_avail(&port->tx_fifo);
0115 spin_unlock_bh(&port->lock);
0116 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d\n",
0117 avail, c, n);
0118 }
0119 }
0120 #else
0121 #define debug_short_write(port, c, n)
0122 #endif
0123
0124 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
0125 int generation, int id);
0126
0127 #ifdef FWTTY_PROFILING
0128
0129 static void fwtty_profile_fifo(struct fwtty_port *port, unsigned int *stat)
0130 {
0131 spin_lock_bh(&port->lock);
0132 fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo));
0133 spin_unlock_bh(&port->lock);
0134 }
0135
0136 static void fwtty_dump_profile(struct seq_file *m, struct stats *stats)
0137 {
0138
0139 int k = 4;
0140 unsigned int sum;
0141 int j;
0142 char t[10];
0143
0144 snprintf(t, 10, "< %d", 1 << k);
0145 seq_printf(m, "\n%14s %6s", " ", t);
0146 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
0147 seq_printf(m, "%6d", 1 << j);
0148
0149 ++k;
0150 for (j = 0, sum = 0; j <= k; ++j)
0151 sum += stats->reads[j];
0152 seq_printf(m, "\n%14s: %6d", "reads", sum);
0153 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
0154 seq_printf(m, "%6d", stats->reads[j]);
0155
0156 for (j = 0, sum = 0; j <= k; ++j)
0157 sum += stats->writes[j];
0158 seq_printf(m, "\n%14s: %6d", "writes", sum);
0159 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
0160 seq_printf(m, "%6d", stats->writes[j]);
0161
0162 for (j = 0, sum = 0; j <= k; ++j)
0163 sum += stats->txns[j];
0164 seq_printf(m, "\n%14s: %6d", "txns", sum);
0165 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
0166 seq_printf(m, "%6d", stats->txns[j]);
0167
0168 for (j = 0, sum = 0; j <= k; ++j)
0169 sum += stats->unthrottle[j];
0170 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
0171 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
0172 seq_printf(m, "%6d", stats->unthrottle[j]);
0173 }
0174
0175 #else
0176 #define fwtty_profile_fifo(port, stat)
0177 #define fwtty_dump_profile(m, stats)
0178 #endif
0179
0180
0181
0182
0183
0184
0185 static inline int device_max_receive(struct fw_device *fw_device)
0186 {
0187
0188 return min(2 << fw_device->max_rec, 4096);
0189 }
0190
0191 static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
0192 {
0193 switch (rcode) {
0194 case RCODE_SEND_ERROR:
0195 fwtty_err_ratelimited(port, "card busy\n");
0196 break;
0197 case RCODE_ADDRESS_ERROR:
0198 fwtty_err_ratelimited(port, "bad unit addr or write length\n");
0199 break;
0200 case RCODE_DATA_ERROR:
0201 fwtty_err_ratelimited(port, "failed rx\n");
0202 break;
0203 case RCODE_NO_ACK:
0204 fwtty_err_ratelimited(port, "missing ack\n");
0205 break;
0206 case RCODE_BUSY:
0207 fwtty_err_ratelimited(port, "remote busy\n");
0208 break;
0209 default:
0210 fwtty_err_ratelimited(port, "failed tx: %d\n", rcode);
0211 }
0212 }
0213
0214 static void fwtty_common_callback(struct fw_card *card, int rcode,
0215 void *payload, size_t len, void *cb_data)
0216 {
0217 struct fwtty_transaction *txn = cb_data;
0218 struct fwtty_port *port = txn->port;
0219
0220 if (port && rcode != RCODE_COMPLETE)
0221 fwtty_log_tx_error(port, rcode);
0222 if (txn->callback)
0223 txn->callback(card, rcode, payload, len, txn);
0224 kmem_cache_free(fwtty_txn_cache, txn);
0225 }
0226
0227 static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
0228 unsigned long long addr, void *payload,
0229 size_t len, fwtty_transaction_cb callback,
0230 struct fwtty_port *port)
0231 {
0232 struct fwtty_transaction *txn;
0233 int generation;
0234
0235 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
0236 if (!txn)
0237 return -ENOMEM;
0238
0239 txn->callback = callback;
0240 txn->port = port;
0241
0242 generation = peer->generation;
0243 smp_rmb();
0244 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
0245 peer->node_id, generation, peer->speed, addr, payload,
0246 len, fwtty_common_callback, txn);
0247 return 0;
0248 }
0249
0250 static void fwtty_send_txn_async(struct fwtty_peer *peer,
0251 struct fwtty_transaction *txn, int tcode,
0252 unsigned long long addr, void *payload,
0253 size_t len, fwtty_transaction_cb callback,
0254 struct fwtty_port *port)
0255 {
0256 int generation;
0257
0258 txn->callback = callback;
0259 txn->port = port;
0260
0261 generation = peer->generation;
0262 smp_rmb();
0263 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
0264 peer->node_id, generation, peer->speed, addr, payload,
0265 len, fwtty_common_callback, txn);
0266 }
0267
0268 static void __fwtty_restart_tx(struct fwtty_port *port)
0269 {
0270 int len, avail;
0271
0272 len = dma_fifo_out_level(&port->tx_fifo);
0273 if (len)
0274 schedule_delayed_work(&port->drain, 0);
0275 avail = dma_fifo_avail(&port->tx_fifo);
0276
0277 fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail);
0278 }
0279
0280 static void fwtty_restart_tx(struct fwtty_port *port)
0281 {
0282 spin_lock_bh(&port->lock);
0283 __fwtty_restart_tx(port);
0284 spin_unlock_bh(&port->lock);
0285 }
0286
0287
0288
0289
0290
0291
0292
0293 static void fwtty_update_port_status(struct fwtty_port *port,
0294 unsigned int status)
0295 {
0296 unsigned int delta;
0297 struct tty_struct *tty;
0298
0299
0300 status &= ~MCTRL_MASK;
0301 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
0302 delta &= ~(status & TIOCM_RNG);
0303 port->mstatus = status;
0304
0305 if (delta & TIOCM_RNG)
0306 ++port->icount.rng;
0307 if (delta & TIOCM_DSR)
0308 ++port->icount.dsr;
0309 if (delta & TIOCM_CAR)
0310 ++port->icount.dcd;
0311 if (delta & TIOCM_CTS)
0312 ++port->icount.cts;
0313
0314 fwtty_dbg(port, "status: %x delta: %x\n", status, delta);
0315
0316 if (delta & TIOCM_CAR) {
0317 tty = tty_port_tty_get(&port->port);
0318 if (tty && !C_CLOCAL(tty)) {
0319 if (status & TIOCM_CAR)
0320 wake_up_interruptible(&port->port.open_wait);
0321 else
0322 schedule_work(&port->hangup);
0323 }
0324 tty_kref_put(tty);
0325 }
0326
0327 if (delta & TIOCM_CTS) {
0328 tty = tty_port_tty_get(&port->port);
0329 if (tty && C_CRTSCTS(tty)) {
0330 if (tty->hw_stopped) {
0331 if (status & TIOCM_CTS) {
0332 tty->hw_stopped = 0;
0333 if (port->loopback)
0334 __fwtty_restart_tx(port);
0335 else
0336 fwtty_restart_tx(port);
0337 }
0338 } else {
0339 if (~status & TIOCM_CTS)
0340 tty->hw_stopped = 1;
0341 }
0342 }
0343 tty_kref_put(tty);
0344
0345 } else if (delta & OOB_TX_THROTTLE) {
0346 tty = tty_port_tty_get(&port->port);
0347 if (tty) {
0348 if (tty->hw_stopped) {
0349 if (~status & OOB_TX_THROTTLE) {
0350 tty->hw_stopped = 0;
0351 if (port->loopback)
0352 __fwtty_restart_tx(port);
0353 else
0354 fwtty_restart_tx(port);
0355 }
0356 } else {
0357 if (status & OOB_TX_THROTTLE)
0358 tty->hw_stopped = 1;
0359 }
0360 }
0361 tty_kref_put(tty);
0362 }
0363
0364 if (delta & (UART_LSR_BI << 24)) {
0365 if (status & (UART_LSR_BI << 24)) {
0366 port->break_last = jiffies;
0367 schedule_delayed_work(&port->emit_breaks, 0);
0368 } else {
0369
0370 mod_delayed_work(system_wq, &port->emit_breaks, 0);
0371 }
0372 }
0373
0374 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
0375 wake_up_interruptible(&port->port.delta_msr_wait);
0376 }
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387 static unsigned int __fwtty_port_line_status(struct fwtty_port *port)
0388 {
0389 unsigned int status = 0;
0390
0391
0392
0393 if (port->mctrl & TIOCM_DTR)
0394 status |= TIOCM_DSR | TIOCM_CAR;
0395 if (port->mctrl & TIOCM_RTS)
0396 status |= TIOCM_CTS;
0397 if (port->mctrl & OOB_RX_THROTTLE)
0398 status |= OOB_TX_THROTTLE;
0399
0400 if (port->break_ctl)
0401 status |= UART_LSR_BI << 24;
0402
0403 return status;
0404 }
0405
0406
0407
0408
0409
0410
0411 static int __fwtty_write_port_status(struct fwtty_port *port)
0412 {
0413 struct fwtty_peer *peer;
0414 int err = -ENOENT;
0415 unsigned int status = __fwtty_port_line_status(port);
0416
0417 rcu_read_lock();
0418 peer = rcu_dereference(port->peer);
0419 if (peer) {
0420 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
0421 peer->status_addr, &status,
0422 sizeof(status), NULL, port);
0423 }
0424 rcu_read_unlock();
0425
0426 return err;
0427 }
0428
0429
0430
0431
0432 static int fwtty_write_port_status(struct fwtty_port *port)
0433 {
0434 int err;
0435
0436 spin_lock_bh(&port->lock);
0437 err = __fwtty_write_port_status(port);
0438 spin_unlock_bh(&port->lock);
0439 return err;
0440 }
0441
0442 static void fwtty_throttle_port(struct fwtty_port *port)
0443 {
0444 struct tty_struct *tty;
0445 unsigned int old;
0446
0447 tty = tty_port_tty_get(&port->port);
0448 if (!tty)
0449 return;
0450
0451 spin_lock_bh(&port->lock);
0452
0453 old = port->mctrl;
0454 port->mctrl |= OOB_RX_THROTTLE;
0455 if (C_CRTSCTS(tty))
0456 port->mctrl &= ~TIOCM_RTS;
0457 if (~old & OOB_RX_THROTTLE)
0458 __fwtty_write_port_status(port);
0459
0460 spin_unlock_bh(&port->lock);
0461
0462 tty_kref_put(tty);
0463 }
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485 static void fwtty_do_hangup(struct work_struct *work)
0486 {
0487 struct fwtty_port *port = to_port(work, hangup);
0488 struct tty_struct *tty;
0489
0490 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
0491
0492 tty = tty_port_tty_get(&port->port);
0493 if (tty)
0494 tty_vhangup(tty);
0495 tty_kref_put(tty);
0496 }
0497
0498 static void fwtty_emit_breaks(struct work_struct *work)
0499 {
0500 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
0501 static const char buf[16];
0502 unsigned long now = jiffies;
0503 unsigned long elapsed = now - port->break_last;
0504 int n, t, c, brk = 0;
0505
0506
0507 n = (elapsed * port->cps) / HZ + 1;
0508 port->break_last = now;
0509
0510 fwtty_dbg(port, "sending %d brks\n", n);
0511
0512 while (n) {
0513 t = min(n, 16);
0514 c = tty_insert_flip_string_fixed_flag(&port->port, buf,
0515 TTY_BREAK, t);
0516 n -= c;
0517 brk += c;
0518 if (c < t)
0519 break;
0520 }
0521 tty_flip_buffer_push(&port->port);
0522
0523 if (port->mstatus & (UART_LSR_BI << 24))
0524 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
0525 port->icount.brk += brk;
0526 }
0527
0528 static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
0529 {
0530 int c, n = len;
0531 unsigned int lsr;
0532 int err = 0;
0533
0534 fwtty_dbg(port, "%d\n", n);
0535 fwtty_profile_data(port->stats.reads, n);
0536
0537 if (port->write_only) {
0538 n = 0;
0539 goto out;
0540 }
0541
0542
0543 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
0544
0545 if (port->overrun)
0546 lsr |= UART_LSR_OE;
0547
0548 if (lsr & UART_LSR_OE)
0549 ++port->icount.overrun;
0550
0551 lsr &= port->status_mask;
0552 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
0553 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) {
0554 err = -EIO;
0555 goto out;
0556 }
0557 }
0558 port->overrun = false;
0559
0560 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
0561
0562 n = 0;
0563 goto out;
0564 }
0565
0566 c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n);
0567 if (c > 0)
0568 tty_flip_buffer_push(&port->port);
0569 n -= c;
0570
0571 if (n) {
0572 port->overrun = true;
0573 err = -EIO;
0574 fwtty_err_ratelimited(port, "flip buffer overrun\n");
0575
0576 } else {
0577
0578
0579
0580
0581
0582 if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK)
0583 fwtty_throttle_port(port);
0584 }
0585
0586 out:
0587 port->icount.rx += len;
0588 port->stats.lost += n;
0589 return err;
0590 }
0591
0592
0593
0594
0595
0596
0597 static void fwtty_port_handler(struct fw_card *card,
0598 struct fw_request *request,
0599 int tcode, int destination, int source,
0600 int generation,
0601 unsigned long long addr,
0602 void *data, size_t len,
0603 void *callback_data)
0604 {
0605 struct fwtty_port *port = callback_data;
0606 struct fwtty_peer *peer;
0607 int err;
0608 int rcode;
0609
0610
0611 rcu_read_lock();
0612 peer = __fwserial_peer_by_node_id(card, generation, source);
0613 rcu_read_unlock();
0614 if (!peer || peer != rcu_access_pointer(port->peer)) {
0615 rcode = RCODE_ADDRESS_ERROR;
0616 fwtty_err_ratelimited(port, "ignoring unauthenticated data\n");
0617 goto respond;
0618 }
0619
0620 switch (tcode) {
0621 case TCODE_WRITE_QUADLET_REQUEST:
0622 if (addr != port->rx_handler.offset || len != 4) {
0623 rcode = RCODE_ADDRESS_ERROR;
0624 } else {
0625 fwtty_update_port_status(port, *(unsigned int *)data);
0626 rcode = RCODE_COMPLETE;
0627 }
0628 break;
0629
0630 case TCODE_WRITE_BLOCK_REQUEST:
0631 if (addr != port->rx_handler.offset + 4 ||
0632 len > port->rx_handler.length - 4) {
0633 rcode = RCODE_ADDRESS_ERROR;
0634 } else {
0635 err = fwtty_rx(port, data, len);
0636 switch (err) {
0637 case 0:
0638 rcode = RCODE_COMPLETE;
0639 break;
0640 case -EIO:
0641 rcode = RCODE_DATA_ERROR;
0642 break;
0643 default:
0644 rcode = RCODE_CONFLICT_ERROR;
0645 break;
0646 }
0647 }
0648 break;
0649
0650 default:
0651 rcode = RCODE_TYPE_ERROR;
0652 }
0653
0654 respond:
0655 fw_send_response(card, request, rcode);
0656 }
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666 static void fwtty_tx_complete(struct fw_card *card, int rcode,
0667 void *data, size_t length,
0668 struct fwtty_transaction *txn)
0669 {
0670 struct fwtty_port *port = txn->port;
0671 int len;
0672
0673 fwtty_dbg(port, "rcode: %d\n", rcode);
0674
0675 switch (rcode) {
0676 case RCODE_COMPLETE:
0677 spin_lock_bh(&port->lock);
0678 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
0679 len = dma_fifo_level(&port->tx_fifo);
0680 spin_unlock_bh(&port->lock);
0681
0682 port->icount.tx += txn->dma_pended.len;
0683 break;
0684
0685 default:
0686
0687 spin_lock_bh(&port->lock);
0688 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
0689 len = dma_fifo_level(&port->tx_fifo);
0690 spin_unlock_bh(&port->lock);
0691
0692 port->stats.dropped += txn->dma_pended.len;
0693 }
0694
0695 if (len < WAKEUP_CHARS)
0696 tty_port_tty_wakeup(&port->port);
0697 }
0698
0699 static int fwtty_tx(struct fwtty_port *port, bool drain)
0700 {
0701 struct fwtty_peer *peer;
0702 struct fwtty_transaction *txn;
0703 struct tty_struct *tty;
0704 int n, len;
0705
0706 tty = tty_port_tty_get(&port->port);
0707 if (!tty)
0708 return -ENOENT;
0709
0710 rcu_read_lock();
0711 peer = rcu_dereference(port->peer);
0712 if (!peer) {
0713 n = -EIO;
0714 goto out;
0715 }
0716
0717 if (test_and_set_bit(IN_TX, &port->flags)) {
0718 n = -EALREADY;
0719 goto out;
0720 }
0721
0722
0723 n = -EAGAIN;
0724 while (!tty->flow.stopped && !tty->hw_stopped &&
0725 !test_bit(STOP_TX, &port->flags)) {
0726 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
0727 if (!txn) {
0728 n = -ENOMEM;
0729 break;
0730 }
0731
0732 spin_lock_bh(&port->lock);
0733 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
0734 spin_unlock_bh(&port->lock);
0735
0736 fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n);
0737
0738 if (n < 0) {
0739 kmem_cache_free(fwtty_txn_cache, txn);
0740 if (n == -EAGAIN) {
0741 ++port->stats.tx_stall;
0742 } else if (n == -ENODATA) {
0743 fwtty_profile_data(port->stats.txns, 0);
0744 } else {
0745 ++port->stats.fifo_errs;
0746 fwtty_err_ratelimited(port, "fifo err: %d\n",
0747 n);
0748 }
0749 break;
0750 }
0751
0752 fwtty_profile_data(port->stats.txns, txn->dma_pended.len);
0753
0754 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
0755 peer->fifo_addr, txn->dma_pended.data,
0756 txn->dma_pended.len, fwtty_tx_complete,
0757 port);
0758 ++port->stats.sent;
0759
0760
0761
0762
0763
0764 if (n == 0 || (!drain && n < WRITER_MINIMUM))
0765 break;
0766 }
0767
0768 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
0769 spin_lock_bh(&port->lock);
0770 len = dma_fifo_out_level(&port->tx_fifo);
0771 if (len) {
0772 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
0773
0774 schedule_delayed_work(&port->drain, delay);
0775 }
0776 len = dma_fifo_level(&port->tx_fifo);
0777 spin_unlock_bh(&port->lock);
0778
0779
0780 if (drain && len < WAKEUP_CHARS)
0781 tty_wakeup(tty);
0782 }
0783
0784 clear_bit(IN_TX, &port->flags);
0785 wake_up_interruptible(&port->wait_tx);
0786
0787 out:
0788 rcu_read_unlock();
0789 tty_kref_put(tty);
0790 return n;
0791 }
0792
0793 static void fwtty_drain_tx(struct work_struct *work)
0794 {
0795 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
0796
0797 fwtty_tx(port, true);
0798 }
0799
0800 static void fwtty_write_xchar(struct fwtty_port *port, char ch)
0801 {
0802 struct fwtty_peer *peer;
0803
0804 ++port->stats.xchars;
0805
0806 fwtty_dbg(port, "%02x\n", ch);
0807
0808 rcu_read_lock();
0809 peer = rcu_dereference(port->peer);
0810 if (peer) {
0811 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
0812 peer->fifo_addr, &ch, sizeof(ch),
0813 NULL, port);
0814 }
0815 rcu_read_unlock();
0816 }
0817
0818 static struct fwtty_port *fwtty_port_get(unsigned int index)
0819 {
0820 struct fwtty_port *port;
0821
0822 if (index >= MAX_TOTAL_PORTS)
0823 return NULL;
0824
0825 mutex_lock(&port_table_lock);
0826 port = port_table[index];
0827 if (port)
0828 kref_get(&port->serial->kref);
0829 mutex_unlock(&port_table_lock);
0830 return port;
0831 }
0832
0833 static int fwtty_ports_add(struct fw_serial *serial)
0834 {
0835 int err = -EBUSY;
0836 int i, j;
0837
0838 if (port_table_corrupt)
0839 return err;
0840
0841 mutex_lock(&port_table_lock);
0842 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
0843 if (!port_table[i]) {
0844 for (j = 0; j < num_ports; ++i, ++j) {
0845 serial->ports[j]->index = i;
0846 port_table[i] = serial->ports[j];
0847 }
0848 err = 0;
0849 break;
0850 }
0851 }
0852 mutex_unlock(&port_table_lock);
0853 return err;
0854 }
0855
0856 static void fwserial_destroy(struct kref *kref)
0857 {
0858 struct fw_serial *serial = to_serial(kref, kref);
0859 struct fwtty_port **ports = serial->ports;
0860 int j, i = ports[0]->index;
0861
0862 synchronize_rcu();
0863
0864 mutex_lock(&port_table_lock);
0865 for (j = 0; j < num_ports; ++i, ++j) {
0866 port_table_corrupt |= port_table[i] != ports[j];
0867 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
0868 i, port_table[i], j, ports[j]);
0869
0870 port_table[i] = NULL;
0871 }
0872 mutex_unlock(&port_table_lock);
0873
0874 for (j = 0; j < num_ports; ++j) {
0875 fw_core_remove_address_handler(&ports[j]->rx_handler);
0876 tty_port_destroy(&ports[j]->port);
0877 kfree(ports[j]);
0878 }
0879 kfree(serial);
0880 }
0881
0882 static void fwtty_port_put(struct fwtty_port *port)
0883 {
0884 kref_put(&port->serial->kref, fwserial_destroy);
0885 }
0886
0887 static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
0888 {
0889 struct fwtty_port *port = to_port(tty_port, port);
0890
0891 fwtty_dbg(port, "on/off: %d\n", on);
0892
0893 spin_lock_bh(&port->lock);
0894
0895 if (!port->port.console) {
0896 if (on)
0897 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
0898 else
0899 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
0900 }
0901
0902 __fwtty_write_port_status(port);
0903 spin_unlock_bh(&port->lock);
0904 }
0905
0906
0907
0908
0909
0910
0911
0912 static int fwtty_port_carrier_raised(struct tty_port *tty_port)
0913 {
0914 struct fwtty_port *port = to_port(tty_port, port);
0915 int rc;
0916
0917 rc = (port->mstatus & TIOCM_CAR);
0918
0919 fwtty_dbg(port, "%d\n", rc);
0920
0921 return rc;
0922 }
0923
0924 static unsigned int set_termios(struct fwtty_port *port, struct tty_struct *tty)
0925 {
0926 unsigned int baud, frame;
0927
0928 baud = tty_termios_baud_rate(&tty->termios);
0929 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
0930
0931
0932 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
0933
0934 switch (C_CSIZE(tty)) {
0935 case CS5:
0936 frame -= (C_CSTOPB(tty)) ? 1 : 0;
0937 break;
0938 case CS6:
0939 frame += 2;
0940 break;
0941 case CS7:
0942 frame += 4;
0943 break;
0944 case CS8:
0945 frame += 6;
0946 break;
0947 }
0948
0949 port->cps = (baud << 1) / frame;
0950
0951 port->status_mask = UART_LSR_OE;
0952 if (_I_FLAG(tty, BRKINT | PARMRK))
0953 port->status_mask |= UART_LSR_BI;
0954
0955 port->ignore_mask = 0;
0956 if (I_IGNBRK(tty)) {
0957 port->ignore_mask |= UART_LSR_BI;
0958 if (I_IGNPAR(tty))
0959 port->ignore_mask |= UART_LSR_OE;
0960 }
0961
0962 port->write_only = !C_CREAD(tty);
0963
0964
0965 if (port->loopback) {
0966 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
0967 ECHONL | ECHOPRT | ECHOCTL);
0968 tty->termios.c_oflag &= ~ONLCR;
0969 }
0970
0971 return baud;
0972 }
0973
0974 static int fwtty_port_activate(struct tty_port *tty_port,
0975 struct tty_struct *tty)
0976 {
0977 struct fwtty_port *port = to_port(tty_port, port);
0978 unsigned int baud;
0979 int err;
0980
0981 set_bit(TTY_IO_ERROR, &tty->flags);
0982
0983 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
0984 cache_line_size(),
0985 port->max_payload,
0986 FWTTY_PORT_MAX_PEND_DMA,
0987 GFP_KERNEL);
0988 if (err)
0989 return err;
0990
0991 spin_lock_bh(&port->lock);
0992
0993 baud = set_termios(port, tty);
0994
0995
0996 if (!port->port.console) {
0997 port->mctrl = 0;
0998 if (baud != 0)
0999 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1000 }
1001
1002 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1003 tty->hw_stopped = 1;
1004
1005 __fwtty_write_port_status(port);
1006 spin_unlock_bh(&port->lock);
1007
1008 clear_bit(TTY_IO_ERROR, &tty->flags);
1009
1010 return 0;
1011 }
1012
1013
1014
1015
1016
1017
1018
1019 static void fwtty_port_shutdown(struct tty_port *tty_port)
1020 {
1021 struct fwtty_port *port = to_port(tty_port, port);
1022
1023
1024
1025 cancel_delayed_work_sync(&port->emit_breaks);
1026 cancel_delayed_work_sync(&port->drain);
1027
1028 spin_lock_bh(&port->lock);
1029 port->flags = 0;
1030 port->break_ctl = 0;
1031 port->overrun = 0;
1032 __fwtty_write_port_status(port);
1033 dma_fifo_free(&port->tx_fifo);
1034 spin_unlock_bh(&port->lock);
1035 }
1036
1037 static int fwtty_open(struct tty_struct *tty, struct file *fp)
1038 {
1039 struct fwtty_port *port = tty->driver_data;
1040
1041 return tty_port_open(&port->port, tty, fp);
1042 }
1043
1044 static void fwtty_close(struct tty_struct *tty, struct file *fp)
1045 {
1046 struct fwtty_port *port = tty->driver_data;
1047
1048 tty_port_close(&port->port, tty, fp);
1049 }
1050
1051 static void fwtty_hangup(struct tty_struct *tty)
1052 {
1053 struct fwtty_port *port = tty->driver_data;
1054
1055 tty_port_hangup(&port->port);
1056 }
1057
1058 static void fwtty_cleanup(struct tty_struct *tty)
1059 {
1060 struct fwtty_port *port = tty->driver_data;
1061
1062 tty->driver_data = NULL;
1063 fwtty_port_put(port);
1064 }
1065
1066 static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1067 {
1068 struct fwtty_port *port = fwtty_port_get(tty->index);
1069 int err;
1070
1071 err = tty_standard_install(driver, tty);
1072 if (!err)
1073 tty->driver_data = port;
1074 else
1075 fwtty_port_put(port);
1076 return err;
1077 }
1078
1079 static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1080 {
1081 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1082 int err;
1083
1084 err = tty_standard_install(driver, tty);
1085 if (!err)
1086 tty->driver_data = port;
1087 else
1088 fwtty_port_put(port);
1089 return err;
1090 }
1091
1092 static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1093 {
1094 struct fwtty_port *port = tty->driver_data;
1095 int n, len;
1096
1097 fwtty_dbg(port, "%d\n", c);
1098 fwtty_profile_data(port->stats.writes, c);
1099
1100 spin_lock_bh(&port->lock);
1101 n = dma_fifo_in(&port->tx_fifo, buf, c);
1102 len = dma_fifo_out_level(&port->tx_fifo);
1103 if (len < DRAIN_THRESHOLD)
1104 schedule_delayed_work(&port->drain, 1);
1105 spin_unlock_bh(&port->lock);
1106
1107 if (len >= DRAIN_THRESHOLD)
1108 fwtty_tx(port, false);
1109
1110 debug_short_write(port, c, n);
1111
1112 return (n < 0) ? 0 : n;
1113 }
1114
1115 static unsigned int fwtty_write_room(struct tty_struct *tty)
1116 {
1117 struct fwtty_port *port = tty->driver_data;
1118 unsigned int n;
1119
1120 spin_lock_bh(&port->lock);
1121 n = dma_fifo_avail(&port->tx_fifo);
1122 spin_unlock_bh(&port->lock);
1123
1124 fwtty_dbg(port, "%u\n", n);
1125
1126 return n;
1127 }
1128
1129 static unsigned int fwtty_chars_in_buffer(struct tty_struct *tty)
1130 {
1131 struct fwtty_port *port = tty->driver_data;
1132 unsigned int n;
1133
1134 spin_lock_bh(&port->lock);
1135 n = dma_fifo_level(&port->tx_fifo);
1136 spin_unlock_bh(&port->lock);
1137
1138 fwtty_dbg(port, "%u\n", n);
1139
1140 return n;
1141 }
1142
1143 static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1144 {
1145 struct fwtty_port *port = tty->driver_data;
1146
1147 fwtty_dbg(port, "%02x\n", ch);
1148
1149 fwtty_write_xchar(port, ch);
1150 }
1151
1152 static void fwtty_throttle(struct tty_struct *tty)
1153 {
1154 struct fwtty_port *port = tty->driver_data;
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 ++port->stats.throttled;
1168 }
1169
1170 static void fwtty_unthrottle(struct tty_struct *tty)
1171 {
1172 struct fwtty_port *port = tty->driver_data;
1173
1174 fwtty_dbg(port, "CRTSCTS: %d\n", C_CRTSCTS(tty) != 0);
1175
1176 fwtty_profile_fifo(port, port->stats.unthrottle);
1177
1178 spin_lock_bh(&port->lock);
1179 port->mctrl &= ~OOB_RX_THROTTLE;
1180 if (C_CRTSCTS(tty))
1181 port->mctrl |= TIOCM_RTS;
1182 __fwtty_write_port_status(port);
1183 spin_unlock_bh(&port->lock);
1184 }
1185
1186 static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1187 struct async_icount *prev)
1188 {
1189 struct async_icount now;
1190 int delta;
1191
1192 now = port->icount;
1193
1194 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1195 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1196 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1197 (mask & TIOCM_CTS && prev->cts != now.cts));
1198
1199 *prev = now;
1200
1201 return delta;
1202 }
1203
1204 static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1205 {
1206 struct async_icount prev;
1207
1208 prev = port->icount;
1209
1210 return wait_event_interruptible(port->port.delta_msr_wait,
1211 check_msr_delta(port, mask, &prev));
1212 }
1213
1214 static int get_serial_info(struct tty_struct *tty,
1215 struct serial_struct *ss)
1216 {
1217 struct fwtty_port *port = tty->driver_data;
1218
1219 mutex_lock(&port->port.mutex);
1220 ss->line = port->index;
1221 ss->baud_base = 400000000;
1222 ss->close_delay = jiffies_to_msecs(port->port.close_delay) / 10;
1223 ss->closing_wait = 3000;
1224 mutex_unlock(&port->port.mutex);
1225
1226 return 0;
1227 }
1228
1229 static int set_serial_info(struct tty_struct *tty,
1230 struct serial_struct *ss)
1231 {
1232 struct fwtty_port *port = tty->driver_data;
1233 unsigned int cdelay;
1234
1235 cdelay = msecs_to_jiffies(ss->close_delay * 10);
1236
1237 mutex_lock(&port->port.mutex);
1238 if (!capable(CAP_SYS_ADMIN)) {
1239 if (cdelay != port->port.close_delay ||
1240 ((ss->flags & ~ASYNC_USR_MASK) !=
1241 (port->port.flags & ~ASYNC_USR_MASK))) {
1242 mutex_unlock(&port->port.mutex);
1243 return -EPERM;
1244 }
1245 }
1246 port->port.close_delay = cdelay;
1247 mutex_unlock(&port->port.mutex);
1248
1249 return 0;
1250 }
1251
1252 static int fwtty_ioctl(struct tty_struct *tty, unsigned int cmd,
1253 unsigned long arg)
1254 {
1255 struct fwtty_port *port = tty->driver_data;
1256 int err;
1257
1258 switch (cmd) {
1259 case TIOCMIWAIT:
1260 err = wait_msr_change(port, arg);
1261 break;
1262
1263 default:
1264 err = -ENOIOCTLCMD;
1265 }
1266
1267 return err;
1268 }
1269
1270 static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1271 {
1272 struct fwtty_port *port = tty->driver_data;
1273 unsigned int baud;
1274
1275 spin_lock_bh(&port->lock);
1276 baud = set_termios(port, tty);
1277
1278 if ((baud == 0) && (old->c_cflag & CBAUD)) {
1279 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1280 } else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1281 if (C_CRTSCTS(tty) || !tty_throttled(tty))
1282 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1283 else
1284 port->mctrl |= TIOCM_DTR;
1285 }
1286 __fwtty_write_port_status(port);
1287 spin_unlock_bh(&port->lock);
1288
1289 if (old->c_cflag & CRTSCTS) {
1290 if (!C_CRTSCTS(tty)) {
1291 tty->hw_stopped = 0;
1292 fwtty_restart_tx(port);
1293 }
1294 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1295 tty->hw_stopped = 1;
1296 }
1297 }
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309 static int fwtty_break_ctl(struct tty_struct *tty, int state)
1310 {
1311 struct fwtty_port *port = tty->driver_data;
1312 long ret;
1313
1314 fwtty_dbg(port, "%d\n", state);
1315
1316 if (state == -1) {
1317 set_bit(STOP_TX, &port->flags);
1318 ret = wait_event_interruptible_timeout(port->wait_tx,
1319 !test_bit(IN_TX, &port->flags),
1320 10);
1321 if (ret == 0 || ret == -ERESTARTSYS) {
1322 clear_bit(STOP_TX, &port->flags);
1323 fwtty_restart_tx(port);
1324 return -EINTR;
1325 }
1326 }
1327
1328 spin_lock_bh(&port->lock);
1329 port->break_ctl = (state == -1);
1330 __fwtty_write_port_status(port);
1331 spin_unlock_bh(&port->lock);
1332
1333 if (state == 0) {
1334 spin_lock_bh(&port->lock);
1335 dma_fifo_reset(&port->tx_fifo);
1336 clear_bit(STOP_TX, &port->flags);
1337 spin_unlock_bh(&port->lock);
1338 }
1339 return 0;
1340 }
1341
1342 static int fwtty_tiocmget(struct tty_struct *tty)
1343 {
1344 struct fwtty_port *port = tty->driver_data;
1345 unsigned int tiocm;
1346
1347 spin_lock_bh(&port->lock);
1348 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1349 spin_unlock_bh(&port->lock);
1350
1351 fwtty_dbg(port, "%x\n", tiocm);
1352
1353 return tiocm;
1354 }
1355
1356 static int fwtty_tiocmset(struct tty_struct *tty,
1357 unsigned int set, unsigned int clear)
1358 {
1359 struct fwtty_port *port = tty->driver_data;
1360
1361 fwtty_dbg(port, "set: %x clear: %x\n", set, clear);
1362
1363
1364
1365 spin_lock_bh(&port->lock);
1366 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1367 port->mctrl |= set & MCTRL_MASK & 0xffff;
1368 __fwtty_write_port_status(port);
1369 spin_unlock_bh(&port->lock);
1370 return 0;
1371 }
1372
1373 static int fwtty_get_icount(struct tty_struct *tty,
1374 struct serial_icounter_struct *icount)
1375 {
1376 struct fwtty_port *port = tty->driver_data;
1377 struct stats stats;
1378
1379 memcpy(&stats, &port->stats, sizeof(stats));
1380 if (port->port.console)
1381 (*port->fwcon_ops->stats)(&stats, port->con_data);
1382
1383 icount->cts = port->icount.cts;
1384 icount->dsr = port->icount.dsr;
1385 icount->rng = port->icount.rng;
1386 icount->dcd = port->icount.dcd;
1387 icount->rx = port->icount.rx;
1388 icount->tx = port->icount.tx + stats.xchars;
1389 icount->frame = port->icount.frame;
1390 icount->overrun = port->icount.overrun;
1391 icount->parity = port->icount.parity;
1392 icount->brk = port->icount.brk;
1393 icount->buf_overrun = port->icount.overrun;
1394 return 0;
1395 }
1396
1397 static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1398 {
1399 struct stats stats;
1400
1401 memcpy(&stats, &port->stats, sizeof(stats));
1402 if (port->port.console)
1403 (*port->fwcon_ops->stats)(&stats, port->con_data);
1404
1405 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1406 port->icount.tx + stats.xchars, port->icount.rx);
1407 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1408 port->icount.dsr, port->icount.rng, port->icount.dcd);
1409 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1410 port->icount.overrun, port->icount.parity, port->icount.brk);
1411 }
1412
1413 static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1414 {
1415 struct stats stats;
1416
1417 memcpy(&stats, &port->stats, sizeof(stats));
1418 if (port->port.console)
1419 (*port->fwcon_ops->stats)(&stats, port->con_data);
1420
1421 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1422 stats.tx_stall, stats.fifo_errs, stats.lost);
1423 seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled);
1424
1425 if (port->port.console) {
1426 seq_puts(m, "\n ");
1427 (*port->fwcon_ops->proc_show)(m, port->con_data);
1428 }
1429
1430 fwtty_dump_profile(m, &port->stats);
1431 }
1432
1433 static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
1434 {
1435 int generation = peer->generation;
1436
1437 smp_rmb();
1438 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1439 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1440 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1441 peer->max_payload, (unsigned long long)peer->guid);
1442 seq_printf(m, " mgmt:%012llx", (unsigned long long)peer->mgmt_addr);
1443 seq_printf(m, " addr:%012llx", (unsigned long long)peer->status_addr);
1444 seq_putc(m, '\n');
1445 }
1446
1447 static int fwtty_proc_show(struct seq_file *m, void *v)
1448 {
1449 struct fwtty_port *port;
1450 int i;
1451
1452 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1453 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1454 seq_printf(m, "%2d:", i);
1455 if (capable(CAP_SYS_ADMIN))
1456 fwtty_proc_show_port(m, port);
1457 fwtty_port_put(port);
1458 seq_puts(m, "\n");
1459 }
1460 return 0;
1461 }
1462
1463 static int fwtty_stats_show(struct seq_file *m, void *v)
1464 {
1465 struct fw_serial *serial = m->private;
1466 struct fwtty_port *port;
1467 int i;
1468
1469 for (i = 0; i < num_ports; ++i) {
1470 port = fwtty_port_get(serial->ports[i]->index);
1471 if (port) {
1472 seq_printf(m, "%2d:", port->index);
1473 fwtty_proc_show_port(m, port);
1474 fwtty_debugfs_show_port(m, port);
1475 fwtty_port_put(port);
1476 seq_puts(m, "\n");
1477 }
1478 }
1479 return 0;
1480 }
1481 DEFINE_SHOW_ATTRIBUTE(fwtty_stats);
1482
1483 static int fwtty_peers_show(struct seq_file *m, void *v)
1484 {
1485 struct fw_serial *serial = m->private;
1486 struct fwtty_peer *peer;
1487
1488 rcu_read_lock();
1489 seq_printf(m, "card: %s guid: %016llx\n",
1490 dev_name(serial->card->device),
1491 (unsigned long long)serial->card->guid);
1492 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1493 fwtty_debugfs_show_peer(m, peer);
1494 rcu_read_unlock();
1495 return 0;
1496 }
1497 DEFINE_SHOW_ATTRIBUTE(fwtty_peers);
1498
1499 static const struct tty_port_operations fwtty_port_ops = {
1500 .dtr_rts = fwtty_port_dtr_rts,
1501 .carrier_raised = fwtty_port_carrier_raised,
1502 .shutdown = fwtty_port_shutdown,
1503 .activate = fwtty_port_activate,
1504 };
1505
1506 static const struct tty_operations fwtty_ops = {
1507 .open = fwtty_open,
1508 .close = fwtty_close,
1509 .hangup = fwtty_hangup,
1510 .cleanup = fwtty_cleanup,
1511 .install = fwtty_install,
1512 .write = fwtty_write,
1513 .write_room = fwtty_write_room,
1514 .chars_in_buffer = fwtty_chars_in_buffer,
1515 .send_xchar = fwtty_send_xchar,
1516 .throttle = fwtty_throttle,
1517 .unthrottle = fwtty_unthrottle,
1518 .ioctl = fwtty_ioctl,
1519 .set_termios = fwtty_set_termios,
1520 .break_ctl = fwtty_break_ctl,
1521 .tiocmget = fwtty_tiocmget,
1522 .tiocmset = fwtty_tiocmset,
1523 .get_icount = fwtty_get_icount,
1524 .set_serial = set_serial_info,
1525 .get_serial = get_serial_info,
1526 .proc_show = fwtty_proc_show,
1527 };
1528
1529 static const struct tty_operations fwloop_ops = {
1530 .open = fwtty_open,
1531 .close = fwtty_close,
1532 .hangup = fwtty_hangup,
1533 .cleanup = fwtty_cleanup,
1534 .install = fwloop_install,
1535 .write = fwtty_write,
1536 .write_room = fwtty_write_room,
1537 .chars_in_buffer = fwtty_chars_in_buffer,
1538 .send_xchar = fwtty_send_xchar,
1539 .throttle = fwtty_throttle,
1540 .unthrottle = fwtty_unthrottle,
1541 .ioctl = fwtty_ioctl,
1542 .set_termios = fwtty_set_termios,
1543 .break_ctl = fwtty_break_ctl,
1544 .tiocmget = fwtty_tiocmget,
1545 .tiocmset = fwtty_tiocmset,
1546 .get_icount = fwtty_get_icount,
1547 .set_serial = set_serial_info,
1548 .get_serial = get_serial_info,
1549 };
1550
1551 static inline int mgmt_pkt_expected_len(__be16 code)
1552 {
1553 static const struct fwserial_mgmt_pkt pkt;
1554
1555 switch (be16_to_cpu(code)) {
1556 case FWSC_VIRT_CABLE_PLUG:
1557 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1558
1559 case FWSC_VIRT_CABLE_PLUG_RSP:
1560 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1561
1562 case FWSC_VIRT_CABLE_UNPLUG:
1563 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1564 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1565 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1566 return sizeof(pkt.hdr);
1567
1568 default:
1569 return -1;
1570 }
1571 }
1572
1573 static inline void fill_plug_params(struct virt_plug_params *params,
1574 struct fwtty_port *port)
1575 {
1576 u64 status_addr = port->rx_handler.offset;
1577 u64 fifo_addr = port->rx_handler.offset + 4;
1578 size_t fifo_len = port->rx_handler.length - 4;
1579
1580 params->status_hi = cpu_to_be32(status_addr >> 32);
1581 params->status_lo = cpu_to_be32(status_addr);
1582 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1583 params->fifo_lo = cpu_to_be32(fifo_addr);
1584 params->fifo_len = cpu_to_be32(fifo_len);
1585 }
1586
1587 static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1588 struct fwtty_port *port)
1589 {
1590 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1591 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1592 fill_plug_params(&pkt->plug_req, port);
1593 }
1594
1595 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1596 struct fwtty_port *port)
1597 {
1598 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1599 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1600 fill_plug_params(&pkt->plug_rsp, port);
1601 }
1602
1603 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1604 {
1605 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1606 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1607 }
1608
1609 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1610 {
1611 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1612 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1613 }
1614
1615 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1616 {
1617 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1618 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1619 }
1620
1621 static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1622 struct virt_plug_params *params)
1623 {
1624 struct fwtty_port *port = peer->port;
1625
1626 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1627 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1628 peer->fifo_len = be32_to_cpu(params->fifo_len);
1629 peer_set_state(peer, FWPS_ATTACHED);
1630
1631
1632 spin_lock_bh(&port->lock);
1633 port->max_payload = min(peer->max_payload, peer->fifo_len);
1634 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1635 spin_unlock_bh(&peer->port->lock);
1636
1637 if (port->port.console && port->fwcon_ops->notify)
1638 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1639
1640 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n",
1641 (unsigned long long)peer->guid, dev_name(port->device));
1642 }
1643
1644 static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1645 struct fwserial_mgmt_pkt *pkt)
1646 {
1647 int generation;
1648 int rcode, tries = 5;
1649
1650 do {
1651 generation = peer->generation;
1652 smp_rmb();
1653
1654 rcode = fw_run_transaction(peer->serial->card,
1655 TCODE_WRITE_BLOCK_REQUEST,
1656 peer->node_id,
1657 generation, peer->speed,
1658 peer->mgmt_addr,
1659 pkt, be16_to_cpu(pkt->hdr.len));
1660 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1661 rcode == RCODE_GENERATION) {
1662 fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode);
1663 continue;
1664 } else {
1665 break;
1666 }
1667 } while (--tries > 0);
1668 return rcode;
1669 }
1670
1671
1672
1673
1674
1675
1676
1677 static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1678 int index)
1679 {
1680 struct fwtty_port *port;
1681
1682 if (index < 0 || index >= num_ports)
1683 return ERR_PTR(-EINVAL);
1684
1685
1686 synchronize_rcu();
1687
1688 port = peer->serial->ports[index];
1689 spin_lock_bh(&port->lock);
1690 if (!rcu_access_pointer(port->peer))
1691 rcu_assign_pointer(port->peer, peer);
1692 else
1693 port = ERR_PTR(-EBUSY);
1694 spin_unlock_bh(&port->lock);
1695
1696 return port;
1697 }
1698
1699
1700
1701
1702
1703
1704
1705 static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1706 {
1707 struct fwtty_port **ports = peer->serial->ports;
1708 int i;
1709
1710
1711 synchronize_rcu();
1712
1713
1714
1715
1716 for (i = 0; i < num_ttys; ++i) {
1717 spin_lock_bh(&ports[i]->lock);
1718 if (!ports[i]->peer) {
1719
1720 rcu_assign_pointer(ports[i]->peer, peer);
1721 spin_unlock_bh(&ports[i]->lock);
1722 return ports[i];
1723 }
1724 spin_unlock_bh(&ports[i]->lock);
1725 }
1726 return NULL;
1727 }
1728
1729 static void fwserial_release_port(struct fwtty_port *port, bool reset)
1730 {
1731
1732 if (reset)
1733 fwtty_update_port_status(port, 0);
1734
1735 spin_lock_bh(&port->lock);
1736
1737
1738 port->max_payload = link_speed_to_max_payload(SCODE_100);
1739 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1740
1741 RCU_INIT_POINTER(port->peer, NULL);
1742 spin_unlock_bh(&port->lock);
1743
1744 if (port->port.console && port->fwcon_ops->notify)
1745 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1746 }
1747
1748 static void fwserial_plug_timeout(struct timer_list *t)
1749 {
1750 struct fwtty_peer *peer = from_timer(peer, t, timer);
1751 struct fwtty_port *port;
1752
1753 spin_lock_bh(&peer->lock);
1754 if (peer->state != FWPS_PLUG_PENDING) {
1755 spin_unlock_bh(&peer->lock);
1756 return;
1757 }
1758
1759 port = peer_revert_state(peer);
1760 spin_unlock_bh(&peer->lock);
1761
1762 if (port)
1763 fwserial_release_port(port, false);
1764 }
1765
1766
1767
1768
1769
1770
1771
1772 static int fwserial_connect_peer(struct fwtty_peer *peer)
1773 {
1774 struct fwtty_port *port;
1775 struct fwserial_mgmt_pkt *pkt;
1776 int err, rcode;
1777
1778 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1779 if (!pkt)
1780 return -ENOMEM;
1781
1782 port = fwserial_find_port(peer);
1783 if (!port) {
1784 fwtty_err(&peer->unit, "avail ports in use\n");
1785 err = -EBUSY;
1786 goto free_pkt;
1787 }
1788
1789 spin_lock_bh(&peer->lock);
1790
1791
1792 if (peer->state != FWPS_NOT_ATTACHED) {
1793 err = -EBUSY;
1794 goto release_port;
1795 }
1796
1797 peer->port = port;
1798 peer_set_state(peer, FWPS_PLUG_PENDING);
1799
1800 fill_plug_req(pkt, peer->port);
1801
1802 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1803 spin_unlock_bh(&peer->lock);
1804
1805 rcode = fwserial_send_mgmt_sync(peer, pkt);
1806
1807 spin_lock_bh(&peer->lock);
1808 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1809 if (rcode == RCODE_CONFLICT_ERROR)
1810 err = -EAGAIN;
1811 else
1812 err = -EIO;
1813 goto cancel_timer;
1814 }
1815 spin_unlock_bh(&peer->lock);
1816
1817 kfree(pkt);
1818 return 0;
1819
1820 cancel_timer:
1821 del_timer(&peer->timer);
1822 peer_revert_state(peer);
1823 release_port:
1824 spin_unlock_bh(&peer->lock);
1825 fwserial_release_port(port, false);
1826 free_pkt:
1827 kfree(pkt);
1828 return err;
1829 }
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840 static void fwserial_close_port(struct tty_driver *driver,
1841 struct fwtty_port *port)
1842 {
1843 struct tty_struct *tty;
1844
1845 mutex_lock(&port->port.mutex);
1846 tty = tty_port_tty_get(&port->port);
1847 if (tty) {
1848 tty_vhangup(tty);
1849 tty_kref_put(tty);
1850 }
1851 mutex_unlock(&port->port.mutex);
1852
1853 if (driver == fwloop_driver)
1854 tty_unregister_device(driver, loop_idx(port));
1855 else
1856 tty_unregister_device(driver, port->index);
1857 }
1858
1859
1860
1861
1862
1863
1864
1865 static struct fw_serial *fwserial_lookup(struct fw_card *card)
1866 {
1867 struct fw_serial *serial;
1868
1869 list_for_each_entry(serial, &fwserial_list, list) {
1870 if (card == serial->card)
1871 return serial;
1872 }
1873
1874 return NULL;
1875 }
1876
1877
1878
1879
1880
1881
1882
1883 static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
1884 {
1885 struct fw_serial *serial;
1886
1887 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1888 if (card == serial->card)
1889 return serial;
1890 }
1891
1892 return NULL;
1893 }
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
1908 int generation, int id)
1909 {
1910 struct fw_serial *serial;
1911 struct fwtty_peer *peer;
1912
1913 serial = __fwserial_lookup_rcu(card);
1914 if (!serial) {
1915
1916
1917
1918
1919
1920
1921 fwtty_err(card, "unknown card (guid %016llx)\n",
1922 (unsigned long long)card->guid);
1923 return NULL;
1924 }
1925
1926 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1927 int g = peer->generation;
1928
1929 smp_rmb();
1930 if (generation == g && id == peer->node_id)
1931 return peer;
1932 }
1933
1934 return NULL;
1935 }
1936
1937 #ifdef DEBUG
1938 static void __dump_peer_list(struct fw_card *card)
1939 {
1940 struct fw_serial *serial;
1941 struct fwtty_peer *peer;
1942
1943 serial = __fwserial_lookup_rcu(card);
1944 if (!serial)
1945 return;
1946
1947 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1948 int g = peer->generation;
1949
1950 smp_rmb();
1951 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n",
1952 g, peer->node_id, (unsigned long long)peer->guid);
1953 }
1954 }
1955 #else
1956 #define __dump_peer_list(s)
1957 #endif
1958
1959 static void fwserial_auto_connect(struct work_struct *work)
1960 {
1961 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
1962 int err;
1963
1964 err = fwserial_connect_peer(peer);
1965 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
1966 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
1967 }
1968
1969 static void fwserial_peer_workfn(struct work_struct *work)
1970 {
1971 struct fwtty_peer *peer = to_peer(work, work);
1972
1973 peer->workfn(work);
1974 }
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992 static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
1993 {
1994 struct device *dev = &unit->device;
1995 struct fw_device *parent = fw_parent_device(unit);
1996 struct fwtty_peer *peer;
1997 struct fw_csr_iterator ci;
1998 int key, val;
1999 int generation;
2000
2001 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2002 if (!peer)
2003 return -ENOMEM;
2004
2005 peer_set_state(peer, FWPS_NOT_ATTACHED);
2006
2007 dev_set_drvdata(dev, peer);
2008 peer->unit = unit;
2009 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2010 peer->speed = parent->max_speed;
2011 peer->max_payload = min(device_max_receive(parent),
2012 link_speed_to_max_payload(peer->speed));
2013
2014 generation = parent->generation;
2015 smp_rmb();
2016 peer->node_id = parent->node_id;
2017 smp_wmb();
2018 peer->generation = generation;
2019
2020
2021 fw_csr_iterator_init(&ci, unit->directory);
2022 while (fw_csr_iterator_next(&ci, &key, &val)) {
2023 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2024 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2025 break;
2026 }
2027 }
2028 if (peer->mgmt_addr == 0ULL) {
2029
2030
2031
2032
2033 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2034 }
2035
2036 spin_lock_init(&peer->lock);
2037 peer->port = NULL;
2038
2039 timer_setup(&peer->timer, fwserial_plug_timeout, 0);
2040 INIT_WORK(&peer->work, fwserial_peer_workfn);
2041 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2042
2043
2044 peer->serial = serial;
2045 list_add_rcu(&peer->list, &serial->peer_list);
2046
2047 fwtty_info(&peer->unit, "peer added (guid:%016llx)\n",
2048 (unsigned long long)peer->guid);
2049
2050
2051 if (parent->is_local) {
2052 serial->self = peer;
2053 if (create_loop_dev) {
2054 struct fwtty_port *port;
2055
2056 port = fwserial_claim_port(peer, num_ttys);
2057 if (!IS_ERR(port)) {
2058 struct virt_plug_params params;
2059
2060 spin_lock_bh(&peer->lock);
2061 peer->port = port;
2062 fill_plug_params(¶ms, port);
2063 fwserial_virt_plug_complete(peer, ¶ms);
2064 spin_unlock_bh(&peer->lock);
2065
2066 fwtty_write_port_status(port);
2067 }
2068 }
2069
2070 } else if (auto_connect) {
2071
2072 schedule_delayed_work(&peer->connect, 1);
2073 }
2074
2075 return 0;
2076 }
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087 static void fwserial_remove_peer(struct fwtty_peer *peer)
2088 {
2089 struct fwtty_port *port;
2090
2091 spin_lock_bh(&peer->lock);
2092 peer_set_state(peer, FWPS_GONE);
2093 spin_unlock_bh(&peer->lock);
2094
2095 cancel_delayed_work_sync(&peer->connect);
2096 cancel_work_sync(&peer->work);
2097
2098 spin_lock_bh(&peer->lock);
2099
2100 if (peer == peer->serial->self)
2101 peer->serial->self = NULL;
2102
2103
2104 del_timer(&peer->timer);
2105
2106 port = peer->port;
2107 peer->port = NULL;
2108
2109 list_del_rcu(&peer->list);
2110
2111 fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n",
2112 (unsigned long long)peer->guid);
2113
2114 spin_unlock_bh(&peer->lock);
2115
2116 if (port)
2117 fwserial_release_port(port, true);
2118
2119 synchronize_rcu();
2120 kfree(peer);
2121 }
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138 static int fwserial_create(struct fw_unit *unit)
2139 {
2140 struct fw_device *parent = fw_parent_device(unit);
2141 struct fw_card *card = parent->card;
2142 struct fw_serial *serial;
2143 struct fwtty_port *port;
2144 struct device *tty_dev;
2145 int i, j;
2146 int err;
2147
2148 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2149 if (!serial)
2150 return -ENOMEM;
2151
2152 kref_init(&serial->kref);
2153 serial->card = card;
2154 INIT_LIST_HEAD(&serial->peer_list);
2155
2156 for (i = 0; i < num_ports; ++i) {
2157 port = kzalloc(sizeof(*port), GFP_KERNEL);
2158 if (!port) {
2159 err = -ENOMEM;
2160 goto free_ports;
2161 }
2162 tty_port_init(&port->port);
2163 port->index = FWTTY_INVALID_INDEX;
2164 port->port.ops = &fwtty_port_ops;
2165 port->serial = serial;
2166 tty_buffer_set_limit(&port->port, 128 * 1024);
2167
2168 spin_lock_init(&port->lock);
2169 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2170 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2171 INIT_WORK(&port->hangup, fwtty_do_hangup);
2172 init_waitqueue_head(&port->wait_tx);
2173 port->max_payload = link_speed_to_max_payload(SCODE_100);
2174 dma_fifo_init(&port->tx_fifo);
2175
2176 RCU_INIT_POINTER(port->peer, NULL);
2177 serial->ports[i] = port;
2178
2179
2180 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2181 port->rx_handler.address_callback = fwtty_port_handler;
2182 port->rx_handler.callback_data = port;
2183
2184
2185
2186
2187 err = fw_core_add_address_handler(&port->rx_handler,
2188 &fw_high_memory_region);
2189 if (err) {
2190 tty_port_destroy(&port->port);
2191 kfree(port);
2192 goto free_ports;
2193 }
2194 }
2195
2196
2197 err = fwtty_ports_add(serial);
2198 if (err) {
2199 fwtty_err(&unit, "no space in port table\n");
2200 goto free_ports;
2201 }
2202
2203 for (j = 0; j < num_ttys; ++j) {
2204 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2205 fwtty_driver,
2206 serial->ports[j]->index,
2207 card->device);
2208 if (IS_ERR(tty_dev)) {
2209 err = PTR_ERR(tty_dev);
2210 fwtty_err(&unit, "register tty device error (%d)\n",
2211 err);
2212 goto unregister_ttys;
2213 }
2214
2215 serial->ports[j]->device = tty_dev;
2216 }
2217
2218
2219 if (create_loop_dev) {
2220 struct device *loop_dev;
2221
2222 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2223 fwloop_driver,
2224 loop_idx(serial->ports[j]),
2225 card->device);
2226 if (IS_ERR(loop_dev)) {
2227 err = PTR_ERR(loop_dev);
2228 fwtty_err(&unit, "create loop device failed (%d)\n",
2229 err);
2230 goto unregister_ttys;
2231 }
2232 serial->ports[j]->device = loop_dev;
2233 serial->ports[j]->loopback = true;
2234 }
2235
2236 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2237 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2238 fwserial_debugfs);
2239 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2240 debugfs_create_file("peers", 0444, serial->debugfs,
2241 serial, &fwtty_peers_fops);
2242 debugfs_create_file("stats", 0444, serial->debugfs,
2243 serial, &fwtty_stats_fops);
2244 }
2245 }
2246
2247 list_add_rcu(&serial->list, &fwserial_list);
2248
2249 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n",
2250 dev_name(card->device), (unsigned long long)card->guid);
2251
2252 err = fwserial_add_peer(serial, unit);
2253 if (!err)
2254 return 0;
2255
2256 fwtty_err(&unit, "unable to add peer unit device (%d)\n", err);
2257
2258
2259 debugfs_remove_recursive(serial->debugfs);
2260
2261 list_del_rcu(&serial->list);
2262 if (create_loop_dev)
2263 tty_unregister_device(fwloop_driver,
2264 loop_idx(serial->ports[j]));
2265 unregister_ttys:
2266 for (--j; j >= 0; --j)
2267 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2268 kref_put(&serial->kref, fwserial_destroy);
2269 return err;
2270
2271 free_ports:
2272 for (--i; i >= 0; --i) {
2273 fw_core_remove_address_handler(&serial->ports[i]->rx_handler);
2274 tty_port_destroy(&serial->ports[i]->port);
2275 kfree(serial->ports[i]);
2276 }
2277 kfree(serial);
2278 return err;
2279 }
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317 static int fwserial_probe(struct fw_unit *unit,
2318 const struct ieee1394_device_id *id)
2319 {
2320 struct fw_serial *serial;
2321 int err;
2322
2323 mutex_lock(&fwserial_list_mutex);
2324 serial = fwserial_lookup(fw_parent_device(unit)->card);
2325 if (!serial)
2326 err = fwserial_create(unit);
2327 else
2328 err = fwserial_add_peer(serial, unit);
2329 mutex_unlock(&fwserial_list_mutex);
2330 return err;
2331 }
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341 static void fwserial_remove(struct fw_unit *unit)
2342 {
2343 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2344 struct fw_serial *serial = peer->serial;
2345 int i;
2346
2347 mutex_lock(&fwserial_list_mutex);
2348 fwserial_remove_peer(peer);
2349
2350 if (list_empty(&serial->peer_list)) {
2351
2352 list_del_rcu(&serial->list);
2353
2354 debugfs_remove_recursive(serial->debugfs);
2355
2356 for (i = 0; i < num_ttys; ++i)
2357 fwserial_close_port(fwtty_driver, serial->ports[i]);
2358 if (create_loop_dev)
2359 fwserial_close_port(fwloop_driver, serial->ports[i]);
2360 kref_put(&serial->kref, fwserial_destroy);
2361 }
2362 mutex_unlock(&fwserial_list_mutex);
2363 }
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379 static void fwserial_update(struct fw_unit *unit)
2380 {
2381 struct fw_device *parent = fw_parent_device(unit);
2382 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2383 int generation;
2384
2385 generation = parent->generation;
2386 smp_rmb();
2387 peer->node_id = parent->node_id;
2388 smp_wmb();
2389 peer->generation = generation;
2390 }
2391
2392 static const struct ieee1394_device_id fwserial_id_table[] = {
2393 {
2394 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2395 IEEE1394_MATCH_VERSION,
2396 .specifier_id = LINUX_VENDOR_ID,
2397 .version = FWSERIAL_VERSION,
2398 },
2399 { }
2400 };
2401
2402 static struct fw_driver fwserial_driver = {
2403 .driver = {
2404 .owner = THIS_MODULE,
2405 .name = KBUILD_MODNAME,
2406 .bus = &fw_bus_type,
2407 },
2408 .probe = fwserial_probe,
2409 .update = fwserial_update,
2410 .remove = fwserial_remove,
2411 .id_table = fwserial_id_table,
2412 };
2413
2414 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2415 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2416 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2417 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2418
2419
2420
2421 #define FW_ROM_LEN(quads) ((quads) << 16)
2422 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2423
2424 struct fwserial_unit_directory_data {
2425 u32 len_crc;
2426 u32 unit_specifier;
2427 u32 unit_sw_version;
2428 u32 unit_addr_offset;
2429 u32 desc1_ofs;
2430 u32 desc1_len_crc;
2431 u32 desc1_data[5];
2432 } __packed;
2433
2434 static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
2435 .len_crc = FW_ROM_LEN(4),
2436 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2437 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2438 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
2439 .desc1_len_crc = FW_ROM_LEN(5),
2440 .desc1_data = {
2441 0x00000000,
2442 0x00000000,
2443 0x4c696e75,
2444 0x78205454,
2445 0x59000000,
2446 },
2447 };
2448
2449 static struct fw_descriptor fwserial_unit_directory = {
2450 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2451 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2452 .data = (u32 *)&fwserial_unit_directory_data,
2453 };
2454
2455
2456
2457
2458
2459 static const struct fw_address_region fwserial_mgmt_addr_region = {
2460 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2461 .end = 0x1000000000000ULL,
2462 };
2463
2464 static struct fw_address_handler fwserial_mgmt_addr_handler;
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479 static void fwserial_handle_plug_req(struct work_struct *work)
2480 {
2481 struct fwtty_peer *peer = to_peer(work, work);
2482 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2483 struct fwtty_port *port;
2484 struct fwserial_mgmt_pkt *pkt;
2485 int rcode;
2486
2487 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2488 if (!pkt)
2489 return;
2490
2491 port = fwserial_find_port(peer);
2492
2493 spin_lock_bh(&peer->lock);
2494
2495 switch (peer->state) {
2496 case FWPS_NOT_ATTACHED:
2497 if (!port) {
2498 fwtty_err(&peer->unit, "no more ports avail\n");
2499 fill_plug_rsp_nack(pkt);
2500 } else {
2501 peer->port = port;
2502 fill_plug_rsp_ok(pkt, peer->port);
2503 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2504
2505 port = NULL;
2506 }
2507 break;
2508
2509 case FWPS_PLUG_PENDING:
2510 if (peer->serial->card->guid > peer->guid)
2511 goto cleanup;
2512
2513
2514 del_timer(&peer->timer);
2515 fill_plug_rsp_ok(pkt, peer->port);
2516 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2517 break;
2518
2519 default:
2520 fill_plug_rsp_nack(pkt);
2521 }
2522
2523 spin_unlock_bh(&peer->lock);
2524 if (port)
2525 fwserial_release_port(port, false);
2526
2527 rcode = fwserial_send_mgmt_sync(peer, pkt);
2528
2529 spin_lock_bh(&peer->lock);
2530 if (peer->state == FWPS_PLUG_RESPONDING) {
2531 if (rcode == RCODE_COMPLETE) {
2532 struct fwtty_port *tmp = peer->port;
2533
2534 fwserial_virt_plug_complete(peer, plug_req);
2535 spin_unlock_bh(&peer->lock);
2536
2537 fwtty_write_port_status(tmp);
2538 spin_lock_bh(&peer->lock);
2539 } else {
2540 fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode);
2541 port = peer_revert_state(peer);
2542 }
2543 }
2544 cleanup:
2545 spin_unlock_bh(&peer->lock);
2546 if (port)
2547 fwserial_release_port(port, false);
2548 kfree(pkt);
2549 }
2550
2551 static void fwserial_handle_unplug_req(struct work_struct *work)
2552 {
2553 struct fwtty_peer *peer = to_peer(work, work);
2554 struct fwtty_port *port = NULL;
2555 struct fwserial_mgmt_pkt *pkt;
2556 int rcode;
2557
2558 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2559 if (!pkt)
2560 return;
2561
2562 spin_lock_bh(&peer->lock);
2563
2564 switch (peer->state) {
2565 case FWPS_ATTACHED:
2566 fill_unplug_rsp_ok(pkt);
2567 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2568 break;
2569
2570 case FWPS_UNPLUG_PENDING:
2571 if (peer->serial->card->guid > peer->guid)
2572 goto cleanup;
2573
2574
2575 del_timer(&peer->timer);
2576 fill_unplug_rsp_ok(pkt);
2577 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2578 break;
2579
2580 default:
2581 fill_unplug_rsp_nack(pkt);
2582 }
2583
2584 spin_unlock_bh(&peer->lock);
2585
2586 rcode = fwserial_send_mgmt_sync(peer, pkt);
2587
2588 spin_lock_bh(&peer->lock);
2589 if (peer->state == FWPS_UNPLUG_RESPONDING) {
2590 if (rcode != RCODE_COMPLETE)
2591 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n",
2592 rcode);
2593 port = peer_revert_state(peer);
2594 }
2595 cleanup:
2596 spin_unlock_bh(&peer->lock);
2597 if (port)
2598 fwserial_release_port(port, true);
2599 kfree(pkt);
2600 }
2601
2602 static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2603 struct fwserial_mgmt_pkt *pkt,
2604 unsigned long long addr,
2605 size_t len)
2606 {
2607 struct fwtty_port *port = NULL;
2608 bool reset = false;
2609 int rcode;
2610
2611 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2612 return RCODE_ADDRESS_ERROR;
2613
2614 if (len != be16_to_cpu(pkt->hdr.len) ||
2615 len != mgmt_pkt_expected_len(pkt->hdr.code))
2616 return RCODE_DATA_ERROR;
2617
2618 spin_lock_bh(&peer->lock);
2619 if (peer->state == FWPS_GONE) {
2620
2621
2622
2623
2624
2625
2626 fwtty_err(&peer->unit, "peer already removed\n");
2627 spin_unlock_bh(&peer->lock);
2628 return RCODE_ADDRESS_ERROR;
2629 }
2630
2631 rcode = RCODE_COMPLETE;
2632
2633 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04x\n", pkt->hdr.code);
2634
2635 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2636 case FWSC_VIRT_CABLE_PLUG:
2637 if (work_pending(&peer->work)) {
2638 fwtty_err(&peer->unit, "plug req: busy\n");
2639 rcode = RCODE_CONFLICT_ERROR;
2640
2641 } else {
2642 peer->work_params.plug_req = pkt->plug_req;
2643 peer->workfn = fwserial_handle_plug_req;
2644 queue_work(system_unbound_wq, &peer->work);
2645 }
2646 break;
2647
2648 case FWSC_VIRT_CABLE_PLUG_RSP:
2649 if (peer->state != FWPS_PLUG_PENDING) {
2650 rcode = RCODE_CONFLICT_ERROR;
2651
2652 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2653 fwtty_notice(&peer->unit, "NACK plug rsp\n");
2654 port = peer_revert_state(peer);
2655
2656 } else {
2657 struct fwtty_port *tmp = peer->port;
2658
2659 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2660 spin_unlock_bh(&peer->lock);
2661
2662 fwtty_write_port_status(tmp);
2663 spin_lock_bh(&peer->lock);
2664 }
2665 break;
2666
2667 case FWSC_VIRT_CABLE_UNPLUG:
2668 if (work_pending(&peer->work)) {
2669 fwtty_err(&peer->unit, "unplug req: busy\n");
2670 rcode = RCODE_CONFLICT_ERROR;
2671 } else {
2672 peer->workfn = fwserial_handle_unplug_req;
2673 queue_work(system_unbound_wq, &peer->work);
2674 }
2675 break;
2676
2677 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2678 if (peer->state != FWPS_UNPLUG_PENDING) {
2679 rcode = RCODE_CONFLICT_ERROR;
2680 } else {
2681 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2682 fwtty_notice(&peer->unit, "NACK unplug?\n");
2683 port = peer_revert_state(peer);
2684 reset = true;
2685 }
2686 break;
2687
2688 default:
2689 fwtty_err(&peer->unit, "unknown mgmt code %d\n",
2690 be16_to_cpu(pkt->hdr.code));
2691 rcode = RCODE_DATA_ERROR;
2692 }
2693 spin_unlock_bh(&peer->lock);
2694
2695 if (port)
2696 fwserial_release_port(port, reset);
2697
2698 return rcode;
2699 }
2700
2701
2702
2703
2704
2705
2706
2707 static void fwserial_mgmt_handler(struct fw_card *card,
2708 struct fw_request *request,
2709 int tcode, int destination, int source,
2710 int generation,
2711 unsigned long long addr,
2712 void *data, size_t len,
2713 void *callback_data)
2714 {
2715 struct fwserial_mgmt_pkt *pkt = data;
2716 struct fwtty_peer *peer;
2717 int rcode;
2718
2719 rcu_read_lock();
2720 peer = __fwserial_peer_by_node_id(card, generation, source);
2721 if (!peer) {
2722 fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source);
2723 __dump_peer_list(card);
2724 rcode = RCODE_CONFLICT_ERROR;
2725
2726 } else {
2727 switch (tcode) {
2728 case TCODE_WRITE_BLOCK_REQUEST:
2729 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2730 break;
2731
2732 default:
2733 rcode = RCODE_TYPE_ERROR;
2734 }
2735 }
2736
2737 rcu_read_unlock();
2738 fw_send_response(card, request, rcode);
2739 }
2740
2741 static int __init fwserial_init(void)
2742 {
2743 int err, num_loops = !!(create_loop_dev);
2744
2745
2746 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2747
2748
2749 if (num_ttys + num_loops > MAX_CARD_PORTS)
2750 num_ttys = MAX_CARD_PORTS - num_loops;
2751
2752 num_ports = num_ttys + num_loops;
2753
2754 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2755 | TTY_DRIVER_DYNAMIC_DEV);
2756 if (IS_ERR(fwtty_driver)) {
2757 err = PTR_ERR(fwtty_driver);
2758 goto remove_debugfs;
2759 }
2760
2761 fwtty_driver->driver_name = KBUILD_MODNAME;
2762 fwtty_driver->name = tty_dev_name;
2763 fwtty_driver->major = 0;
2764 fwtty_driver->minor_start = 0;
2765 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2766 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
2767 fwtty_driver->init_termios = tty_std_termios;
2768 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2769 tty_set_operations(fwtty_driver, &fwtty_ops);
2770
2771 err = tty_register_driver(fwtty_driver);
2772 if (err) {
2773 pr_err("register tty driver failed (%d)\n", err);
2774 goto put_tty;
2775 }
2776
2777 if (create_loop_dev) {
2778 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2779 TTY_DRIVER_REAL_RAW
2780 | TTY_DRIVER_DYNAMIC_DEV);
2781 if (IS_ERR(fwloop_driver)) {
2782 err = PTR_ERR(fwloop_driver);
2783 goto unregister_driver;
2784 }
2785
2786 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2787 fwloop_driver->name = loop_dev_name;
2788 fwloop_driver->major = 0;
2789 fwloop_driver->minor_start = 0;
2790 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2791 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
2792 fwloop_driver->init_termios = tty_std_termios;
2793 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2794 tty_set_operations(fwloop_driver, &fwloop_ops);
2795
2796 err = tty_register_driver(fwloop_driver);
2797 if (err) {
2798 pr_err("register loop driver failed (%d)\n", err);
2799 goto put_loop;
2800 }
2801 }
2802
2803 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2804 sizeof(struct fwtty_transaction),
2805 0, 0, NULL);
2806 if (!fwtty_txn_cache) {
2807 err = -ENOMEM;
2808 goto unregister_loop;
2809 }
2810
2811
2812
2813
2814
2815
2816
2817
2818 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2819 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2820
2821 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2822 &fwserial_mgmt_addr_region);
2823 if (err) {
2824 pr_err("add management handler failed (%d)\n", err);
2825 goto destroy_cache;
2826 }
2827
2828 fwserial_unit_directory_data.unit_addr_offset =
2829 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2830 err = fw_core_add_descriptor(&fwserial_unit_directory);
2831 if (err) {
2832 pr_err("add unit descriptor failed (%d)\n", err);
2833 goto remove_handler;
2834 }
2835
2836 err = driver_register(&fwserial_driver.driver);
2837 if (err) {
2838 pr_err("register fwserial driver failed (%d)\n", err);
2839 goto remove_descriptor;
2840 }
2841
2842 return 0;
2843
2844 remove_descriptor:
2845 fw_core_remove_descriptor(&fwserial_unit_directory);
2846 remove_handler:
2847 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2848 destroy_cache:
2849 kmem_cache_destroy(fwtty_txn_cache);
2850 unregister_loop:
2851 if (create_loop_dev)
2852 tty_unregister_driver(fwloop_driver);
2853 put_loop:
2854 if (create_loop_dev)
2855 tty_driver_kref_put(fwloop_driver);
2856 unregister_driver:
2857 tty_unregister_driver(fwtty_driver);
2858 put_tty:
2859 tty_driver_kref_put(fwtty_driver);
2860 remove_debugfs:
2861 debugfs_remove_recursive(fwserial_debugfs);
2862
2863 return err;
2864 }
2865
2866 static void __exit fwserial_exit(void)
2867 {
2868 driver_unregister(&fwserial_driver.driver);
2869 fw_core_remove_descriptor(&fwserial_unit_directory);
2870 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2871 kmem_cache_destroy(fwtty_txn_cache);
2872 if (create_loop_dev) {
2873 tty_unregister_driver(fwloop_driver);
2874 tty_driver_kref_put(fwloop_driver);
2875 }
2876 tty_unregister_driver(fwtty_driver);
2877 tty_driver_kref_put(fwtty_driver);
2878 debugfs_remove_recursive(fwserial_debugfs);
2879 }
2880
2881 module_init(fwserial_init);
2882 module_exit(fwserial_exit);
2883
2884 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2885 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2886 MODULE_LICENSE("GPL");
2887 MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
2888 MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
2889 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2890 MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");