Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.13 2892 lines 77 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * FireWire Serial driver 4 * 5 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com> 6 */ 7 8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10#include <linux/sched.h> 11#include <linux/slab.h> 12#include <linux/device.h> 13#include <linux/mod_devicetable.h> 14#include <linux/rculist.h> 15#include <linux/workqueue.h> 16#include <linux/ratelimit.h> 17#include <linux/bug.h> 18#include <linux/uaccess.h> 19 20#include "fwserial.h" 21 22inline u64 be32_to_u64(__be32 hi, __be32 lo) 23{ 24 return ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo)); 25} 26 27#define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */ 28#define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */ 29 30/* configurable options */ 31static int num_ttys = 4; /* # of std ttys to create per fw_card */ 32 /* - doubles as loopback port index */ 33static bool auto_connect = true; /* try to VIRT_CABLE to every peer */ 34static bool create_loop_dev = true; /* create a loopback device for each card */ 35 36module_param_named(ttys, num_ttys, int, 0644); 37module_param_named(auto, auto_connect, bool, 0644); 38module_param_named(loop, create_loop_dev, bool, 0644); 39 40/* 41 * Threshold below which the tty is woken for writing 42 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because 43 * even if the writer is woken, n_tty_poll() won't set EPOLLOUT until 44 * our fifo is below this level 45 */ 46#define WAKEUP_CHARS 256 47 48/** 49 * fwserial_list: list of every fw_serial created for each fw_card 50 * See discussion in fwserial_probe. 51 */ 52static LIST_HEAD(fwserial_list); 53static DEFINE_MUTEX(fwserial_list_mutex); 54 55/** 56 * port_table: array of tty ports allocated to each fw_card 57 * 58 * tty ports are allocated during probe when an fw_serial is first 59 * created for a given fw_card. Ports are allocated in a contiguous block, 60 * each block consisting of 'num_ports' ports. 61 */ 62static struct fwtty_port *port_table[MAX_TOTAL_PORTS]; 63static DEFINE_MUTEX(port_table_lock); 64static bool port_table_corrupt; 65#define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS 66 67#define loop_idx(port) (((port)->index) / num_ports) 68#define table_idx(loop) ((loop) * num_ports + num_ttys) 69 70/* total # of tty ports created per fw_card */ 71static int num_ports; 72 73/* slab used as pool for struct fwtty_transactions */ 74static struct kmem_cache *fwtty_txn_cache; 75 76struct tty_driver *fwtty_driver; 77static struct tty_driver *fwloop_driver; 78 79static struct dentry *fwserial_debugfs; 80 81struct fwtty_transaction; 82typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode, 83 void *data, size_t length, 84 struct fwtty_transaction *txn); 85 86struct fwtty_transaction { 87 struct fw_transaction fw_txn; 88 fwtty_transaction_cb callback; 89 struct fwtty_port *port; 90 union { 91 struct dma_pending dma_pended; 92 }; 93}; 94 95#define to_device(a, b) (a->b) 96#define fwtty_err(p, fmt, ...) \ 97 dev_err(to_device(p, device), fmt, ##__VA_ARGS__) 98#define fwtty_info(p, fmt, ...) \ 99 dev_info(to_device(p, device), fmt, ##__VA_ARGS__) 100#define fwtty_notice(p, fmt, ...) \ 101 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__) 102#define fwtty_dbg(p, fmt, ...) \ 103 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__) 104#define fwtty_err_ratelimited(p, fmt, ...) \ 105 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__) 106 107#ifdef DEBUG 108static inline void debug_short_write(struct fwtty_port *port, int c, int n) 109{ 110 int avail; 111 112 if (n < c) { 113 spin_lock_bh(&port->lock); 114 avail = dma_fifo_avail(&port->tx_fifo); 115 spin_unlock_bh(&port->lock); 116 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d\n", 117 avail, c, n); 118 } 119} 120#else 121#define debug_short_write(port, c, n) 122#endif 123 124static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card, 125 int generation, int id); 126 127#ifdef FWTTY_PROFILING 128 129static void fwtty_profile_fifo(struct fwtty_port *port, unsigned int *stat) 130{ 131 spin_lock_bh(&port->lock); 132 fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo)); 133 spin_unlock_bh(&port->lock); 134} 135 136static void fwtty_dump_profile(struct seq_file *m, struct stats *stats) 137{ 138 /* for each stat, print sum of 0 to 2^k, then individually */ 139 int k = 4; 140 unsigned int sum; 141 int j; 142 char t[10]; 143 144 snprintf(t, 10, "< %d", 1 << k); 145 seq_printf(m, "\n%14s %6s", " ", t); 146 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j) 147 seq_printf(m, "%6d", 1 << j); 148 149 ++k; 150 for (j = 0, sum = 0; j <= k; ++j) 151 sum += stats->reads[j]; 152 seq_printf(m, "\n%14s: %6d", "reads", sum); 153 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 154 seq_printf(m, "%6d", stats->reads[j]); 155 156 for (j = 0, sum = 0; j <= k; ++j) 157 sum += stats->writes[j]; 158 seq_printf(m, "\n%14s: %6d", "writes", sum); 159 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 160 seq_printf(m, "%6d", stats->writes[j]); 161 162 for (j = 0, sum = 0; j <= k; ++j) 163 sum += stats->txns[j]; 164 seq_printf(m, "\n%14s: %6d", "txns", sum); 165 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 166 seq_printf(m, "%6d", stats->txns[j]); 167 168 for (j = 0, sum = 0; j <= k; ++j) 169 sum += stats->unthrottle[j]; 170 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum); 171 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 172 seq_printf(m, "%6d", stats->unthrottle[j]); 173} 174 175#else 176#define fwtty_profile_fifo(port, stat) 177#define fwtty_dump_profile(m, stats) 178#endif 179 180/* 181 * Returns the max receive packet size for the given node 182 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant 183 * are required by specification to support max_rec of 8 (512 bytes) or more. 184 */ 185static inline int device_max_receive(struct fw_device *fw_device) 186{ 187 /* see IEEE 1394-2008 table 8-8 */ 188 return min(2 << fw_device->max_rec, 4096); 189} 190 191static void fwtty_log_tx_error(struct fwtty_port *port, int rcode) 192{ 193 switch (rcode) { 194 case RCODE_SEND_ERROR: 195 fwtty_err_ratelimited(port, "card busy\n"); 196 break; 197 case RCODE_ADDRESS_ERROR: 198 fwtty_err_ratelimited(port, "bad unit addr or write length\n"); 199 break; 200 case RCODE_DATA_ERROR: 201 fwtty_err_ratelimited(port, "failed rx\n"); 202 break; 203 case RCODE_NO_ACK: 204 fwtty_err_ratelimited(port, "missing ack\n"); 205 break; 206 case RCODE_BUSY: 207 fwtty_err_ratelimited(port, "remote busy\n"); 208 break; 209 default: 210 fwtty_err_ratelimited(port, "failed tx: %d\n", rcode); 211 } 212} 213 214static void fwtty_common_callback(struct fw_card *card, int rcode, 215 void *payload, size_t len, void *cb_data) 216{ 217 struct fwtty_transaction *txn = cb_data; 218 struct fwtty_port *port = txn->port; 219 220 if (port && rcode != RCODE_COMPLETE) 221 fwtty_log_tx_error(port, rcode); 222 if (txn->callback) 223 txn->callback(card, rcode, payload, len, txn); 224 kmem_cache_free(fwtty_txn_cache, txn); 225} 226 227static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode, 228 unsigned long long addr, void *payload, 229 size_t len, fwtty_transaction_cb callback, 230 struct fwtty_port *port) 231{ 232 struct fwtty_transaction *txn; 233 int generation; 234 235 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC); 236 if (!txn) 237 return -ENOMEM; 238 239 txn->callback = callback; 240 txn->port = port; 241 242 generation = peer->generation; 243 smp_rmb(); 244 fw_send_request(peer->serial->card, &txn->fw_txn, tcode, 245 peer->node_id, generation, peer->speed, addr, payload, 246 len, fwtty_common_callback, txn); 247 return 0; 248} 249 250static void fwtty_send_txn_async(struct fwtty_peer *peer, 251 struct fwtty_transaction *txn, int tcode, 252 unsigned long long addr, void *payload, 253 size_t len, fwtty_transaction_cb callback, 254 struct fwtty_port *port) 255{ 256 int generation; 257 258 txn->callback = callback; 259 txn->port = port; 260 261 generation = peer->generation; 262 smp_rmb(); 263 fw_send_request(peer->serial->card, &txn->fw_txn, tcode, 264 peer->node_id, generation, peer->speed, addr, payload, 265 len, fwtty_common_callback, txn); 266} 267 268static void __fwtty_restart_tx(struct fwtty_port *port) 269{ 270 int len, avail; 271 272 len = dma_fifo_out_level(&port->tx_fifo); 273 if (len) 274 schedule_delayed_work(&port->drain, 0); 275 avail = dma_fifo_avail(&port->tx_fifo); 276 277 fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail); 278} 279 280static void fwtty_restart_tx(struct fwtty_port *port) 281{ 282 spin_lock_bh(&port->lock); 283 __fwtty_restart_tx(port); 284 spin_unlock_bh(&port->lock); 285} 286 287/** 288 * fwtty_update_port_status - decodes & dispatches line status changes 289 * 290 * Note: in loopback, the port->lock is being held. Only use functions that 291 * don't attempt to reclaim the port->lock. 292 */ 293static void fwtty_update_port_status(struct fwtty_port *port, 294 unsigned int status) 295{ 296 unsigned int delta; 297 struct tty_struct *tty; 298 299 /* simulated LSR/MSR status from remote */ 300 status &= ~MCTRL_MASK; 301 delta = (port->mstatus ^ status) & ~MCTRL_MASK; 302 delta &= ~(status & TIOCM_RNG); 303 port->mstatus = status; 304 305 if (delta & TIOCM_RNG) 306 ++port->icount.rng; 307 if (delta & TIOCM_DSR) 308 ++port->icount.dsr; 309 if (delta & TIOCM_CAR) 310 ++port->icount.dcd; 311 if (delta & TIOCM_CTS) 312 ++port->icount.cts; 313 314 fwtty_dbg(port, "status: %x delta: %x\n", status, delta); 315 316 if (delta & TIOCM_CAR) { 317 tty = tty_port_tty_get(&port->port); 318 if (tty && !C_CLOCAL(tty)) { 319 if (status & TIOCM_CAR) 320 wake_up_interruptible(&port->port.open_wait); 321 else 322 schedule_work(&port->hangup); 323 } 324 tty_kref_put(tty); 325 } 326 327 if (delta & TIOCM_CTS) { 328 tty = tty_port_tty_get(&port->port); 329 if (tty && C_CRTSCTS(tty)) { 330 if (tty->hw_stopped) { 331 if (status & TIOCM_CTS) { 332 tty->hw_stopped = 0; 333 if (port->loopback) 334 __fwtty_restart_tx(port); 335 else 336 fwtty_restart_tx(port); 337 } 338 } else { 339 if (~status & TIOCM_CTS) 340 tty->hw_stopped = 1; 341 } 342 } 343 tty_kref_put(tty); 344 345 } else if (delta & OOB_TX_THROTTLE) { 346 tty = tty_port_tty_get(&port->port); 347 if (tty) { 348 if (tty->hw_stopped) { 349 if (~status & OOB_TX_THROTTLE) { 350 tty->hw_stopped = 0; 351 if (port->loopback) 352 __fwtty_restart_tx(port); 353 else 354 fwtty_restart_tx(port); 355 } 356 } else { 357 if (status & OOB_TX_THROTTLE) 358 tty->hw_stopped = 1; 359 } 360 } 361 tty_kref_put(tty); 362 } 363 364 if (delta & (UART_LSR_BI << 24)) { 365 if (status & (UART_LSR_BI << 24)) { 366 port->break_last = jiffies; 367 schedule_delayed_work(&port->emit_breaks, 0); 368 } else { 369 /* run emit_breaks one last time (if pending) */ 370 mod_delayed_work(system_wq, &port->emit_breaks, 0); 371 } 372 } 373 374 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG)) 375 wake_up_interruptible(&port->port.delta_msr_wait); 376} 377 378/** 379 * __fwtty_port_line_status - generate 'line status' for indicated port 380 * 381 * This function returns a remote 'MSR' state based on the local 'MCR' state, 382 * as if a null modem cable was attached. The actual status is a mangling 383 * of TIOCM_* bits suitable for sending to a peer's status_addr. 384 * 385 * Note: caller must be holding port lock 386 */ 387static unsigned int __fwtty_port_line_status(struct fwtty_port *port) 388{ 389 unsigned int status = 0; 390 391 /* TODO: add module param to tie RNG to DTR as well */ 392 393 if (port->mctrl & TIOCM_DTR) 394 status |= TIOCM_DSR | TIOCM_CAR; 395 if (port->mctrl & TIOCM_RTS) 396 status |= TIOCM_CTS; 397 if (port->mctrl & OOB_RX_THROTTLE) 398 status |= OOB_TX_THROTTLE; 399 /* emulate BRK as add'l line status */ 400 if (port->break_ctl) 401 status |= UART_LSR_BI << 24; 402 403 return status; 404} 405 406/** 407 * __fwtty_write_port_status - send the port line status to peer 408 * 409 * Note: caller must be holding the port lock. 410 */ 411static int __fwtty_write_port_status(struct fwtty_port *port) 412{ 413 struct fwtty_peer *peer; 414 int err = -ENOENT; 415 unsigned int status = __fwtty_port_line_status(port); 416 417 rcu_read_lock(); 418 peer = rcu_dereference(port->peer); 419 if (peer) { 420 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST, 421 peer->status_addr, &status, 422 sizeof(status), NULL, port); 423 } 424 rcu_read_unlock(); 425 426 return err; 427} 428 429/** 430 * fwtty_write_port_status - same as above but locked by port lock 431 */ 432static int fwtty_write_port_status(struct fwtty_port *port) 433{ 434 int err; 435 436 spin_lock_bh(&port->lock); 437 err = __fwtty_write_port_status(port); 438 spin_unlock_bh(&port->lock); 439 return err; 440} 441 442static void fwtty_throttle_port(struct fwtty_port *port) 443{ 444 struct tty_struct *tty; 445 unsigned int old; 446 447 tty = tty_port_tty_get(&port->port); 448 if (!tty) 449 return; 450 451 spin_lock_bh(&port->lock); 452 453 old = port->mctrl; 454 port->mctrl |= OOB_RX_THROTTLE; 455 if (C_CRTSCTS(tty)) 456 port->mctrl &= ~TIOCM_RTS; 457 if (~old & OOB_RX_THROTTLE) 458 __fwtty_write_port_status(port); 459 460 spin_unlock_bh(&port->lock); 461 462 tty_kref_put(tty); 463} 464 465/** 466 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup 467 * 468 * When the remote has finished tx, and all in-flight rx has been received and 469 * pushed to the flip buffer, the remote may close its device. This will 470 * drop DTR on the remote which will drop carrier here. Typically, the tty is 471 * hung up when carrier is dropped or lost. 472 * 473 * However, there is a race between the hang up and the line discipline 474 * delivering its data to the reader. A hangup will cause the ldisc to flush 475 * (ie., clear) the read buffer and flip buffer. Because of firewire's 476 * relatively high throughput, the ldisc frequently lags well behind the driver, 477 * resulting in lost data (which has already been received and written to 478 * the flip buffer) when the remote closes its end. 479 * 480 * Unfortunately, since the flip buffer offers no direct method for determining 481 * if it holds data, ensuring the ldisc has delivered all data is problematic. 482 */ 483 484/* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */ 485static void fwtty_do_hangup(struct work_struct *work) 486{ 487 struct fwtty_port *port = to_port(work, hangup); 488 struct tty_struct *tty; 489 490 schedule_timeout_uninterruptible(msecs_to_jiffies(50)); 491 492 tty = tty_port_tty_get(&port->port); 493 if (tty) 494 tty_vhangup(tty); 495 tty_kref_put(tty); 496} 497 498static void fwtty_emit_breaks(struct work_struct *work) 499{ 500 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks); 501 static const char buf[16]; 502 unsigned long now = jiffies; 503 unsigned long elapsed = now - port->break_last; 504 int n, t, c, brk = 0; 505 506 /* generate breaks at the line rate (but at least 1) */ 507 n = (elapsed * port->cps) / HZ + 1; 508 port->break_last = now; 509 510 fwtty_dbg(port, "sending %d brks\n", n); 511 512 while (n) { 513 t = min(n, 16); 514 c = tty_insert_flip_string_fixed_flag(&port->port, buf, 515 TTY_BREAK, t); 516 n -= c; 517 brk += c; 518 if (c < t) 519 break; 520 } 521 tty_flip_buffer_push(&port->port); 522 523 if (port->mstatus & (UART_LSR_BI << 24)) 524 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS); 525 port->icount.brk += brk; 526} 527 528static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len) 529{ 530 int c, n = len; 531 unsigned int lsr; 532 int err = 0; 533 534 fwtty_dbg(port, "%d\n", n); 535 fwtty_profile_data(port->stats.reads, n); 536 537 if (port->write_only) { 538 n = 0; 539 goto out; 540 } 541 542 /* disregard break status; breaks are generated by emit_breaks work */ 543 lsr = (port->mstatus >> 24) & ~UART_LSR_BI; 544 545 if (port->overrun) 546 lsr |= UART_LSR_OE; 547 548 if (lsr & UART_LSR_OE) 549 ++port->icount.overrun; 550 551 lsr &= port->status_mask; 552 if (lsr & ~port->ignore_mask & UART_LSR_OE) { 553 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) { 554 err = -EIO; 555 goto out; 556 } 557 } 558 port->overrun = false; 559 560 if (lsr & port->ignore_mask & ~UART_LSR_OE) { 561 /* TODO: don't drop SAK and Magic SysRq here */ 562 n = 0; 563 goto out; 564 } 565 566 c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n); 567 if (c > 0) 568 tty_flip_buffer_push(&port->port); 569 n -= c; 570 571 if (n) { 572 port->overrun = true; 573 err = -EIO; 574 fwtty_err_ratelimited(port, "flip buffer overrun\n"); 575 576 } else { 577 /* throttle the sender if remaining flip buffer space has 578 * reached high watermark to avoid losing data which may be 579 * in-flight. Since the AR request context is 32k, that much 580 * data may have _already_ been acked. 581 */ 582 if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK) 583 fwtty_throttle_port(port); 584 } 585 586out: 587 port->icount.rx += len; 588 port->stats.lost += n; 589 return err; 590} 591 592/** 593 * fwtty_port_handler - bus address handler for port reads/writes 594 * @parameters: fw_address_callback_t as specified by firewire core interface 595 * 596 * This handler is responsible for handling inbound read/write dma from remotes. 597 */ 598static void fwtty_port_handler(struct fw_card *card, 599 struct fw_request *request, 600 int tcode, int destination, int source, 601 int generation, 602 unsigned long long addr, 603 void *data, size_t len, 604 void *callback_data) 605{ 606 struct fwtty_port *port = callback_data; 607 struct fwtty_peer *peer; 608 int err; 609 int rcode; 610 611 /* Only accept rx from the peer virtual-cabled to this port */ 612 rcu_read_lock(); 613 peer = __fwserial_peer_by_node_id(card, generation, source); 614 rcu_read_unlock(); 615 if (!peer || peer != rcu_access_pointer(port->peer)) { 616 rcode = RCODE_ADDRESS_ERROR; 617 fwtty_err_ratelimited(port, "ignoring unauthenticated data\n"); 618 goto respond; 619 } 620 621 switch (tcode) { 622 case TCODE_WRITE_QUADLET_REQUEST: 623 if (addr != port->rx_handler.offset || len != 4) { 624 rcode = RCODE_ADDRESS_ERROR; 625 } else { 626 fwtty_update_port_status(port, *(unsigned int *)data); 627 rcode = RCODE_COMPLETE; 628 } 629 break; 630 631 case TCODE_WRITE_BLOCK_REQUEST: 632 if (addr != port->rx_handler.offset + 4 || 633 len > port->rx_handler.length - 4) { 634 rcode = RCODE_ADDRESS_ERROR; 635 } else { 636 err = fwtty_rx(port, data, len); 637 switch (err) { 638 case 0: 639 rcode = RCODE_COMPLETE; 640 break; 641 case -EIO: 642 rcode = RCODE_DATA_ERROR; 643 break; 644 default: 645 rcode = RCODE_CONFLICT_ERROR; 646 break; 647 } 648 } 649 break; 650 651 default: 652 rcode = RCODE_TYPE_ERROR; 653 } 654 655respond: 656 fw_send_response(card, request, rcode); 657} 658 659/** 660 * fwtty_tx_complete - callback for tx dma 661 * @data: ignored, has no meaning for write txns 662 * @length: ignored, has no meaning for write txns 663 * 664 * The writer must be woken here if the fifo has been emptied because it 665 * may have slept if chars_in_buffer was != 0 666 */ 667static void fwtty_tx_complete(struct fw_card *card, int rcode, 668 void *data, size_t length, 669 struct fwtty_transaction *txn) 670{ 671 struct fwtty_port *port = txn->port; 672 int len; 673 674 fwtty_dbg(port, "rcode: %d\n", rcode); 675 676 switch (rcode) { 677 case RCODE_COMPLETE: 678 spin_lock_bh(&port->lock); 679 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended); 680 len = dma_fifo_level(&port->tx_fifo); 681 spin_unlock_bh(&port->lock); 682 683 port->icount.tx += txn->dma_pended.len; 684 break; 685 686 default: 687 /* TODO: implement retries */ 688 spin_lock_bh(&port->lock); 689 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended); 690 len = dma_fifo_level(&port->tx_fifo); 691 spin_unlock_bh(&port->lock); 692 693 port->stats.dropped += txn->dma_pended.len; 694 } 695 696 if (len < WAKEUP_CHARS) 697 tty_port_tty_wakeup(&port->port); 698} 699 700static int fwtty_tx(struct fwtty_port *port, bool drain) 701{ 702 struct fwtty_peer *peer; 703 struct fwtty_transaction *txn; 704 struct tty_struct *tty; 705 int n, len; 706 707 tty = tty_port_tty_get(&port->port); 708 if (!tty) 709 return -ENOENT; 710 711 rcu_read_lock(); 712 peer = rcu_dereference(port->peer); 713 if (!peer) { 714 n = -EIO; 715 goto out; 716 } 717 718 if (test_and_set_bit(IN_TX, &port->flags)) { 719 n = -EALREADY; 720 goto out; 721 } 722 723 /* try to write as many dma transactions out as possible */ 724 n = -EAGAIN; 725 while (!tty->stopped && !tty->hw_stopped && 726 !test_bit(STOP_TX, &port->flags)) { 727 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC); 728 if (!txn) { 729 n = -ENOMEM; 730 break; 731 } 732 733 spin_lock_bh(&port->lock); 734 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended); 735 spin_unlock_bh(&port->lock); 736 737 fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n); 738 739 if (n < 0) { 740 kmem_cache_free(fwtty_txn_cache, txn); 741 if (n == -EAGAIN) { 742 ++port->stats.tx_stall; 743 } else if (n == -ENODATA) { 744 fwtty_profile_data(port->stats.txns, 0); 745 } else { 746 ++port->stats.fifo_errs; 747 fwtty_err_ratelimited(port, "fifo err: %d\n", 748 n); 749 } 750 break; 751 } 752 753 fwtty_profile_data(port->stats.txns, txn->dma_pended.len); 754 755 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST, 756 peer->fifo_addr, txn->dma_pended.data, 757 txn->dma_pended.len, fwtty_tx_complete, 758 port); 759 ++port->stats.sent; 760 761 /* 762 * Stop tx if the 'last view' of the fifo is empty or if 763 * this is the writer and there's not enough data to bother 764 */ 765 if (n == 0 || (!drain && n < WRITER_MINIMUM)) 766 break; 767 } 768 769 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) { 770 spin_lock_bh(&port->lock); 771 len = dma_fifo_out_level(&port->tx_fifo); 772 if (len) { 773 unsigned long delay = (n == -ENOMEM) ? HZ : 1; 774 775 schedule_delayed_work(&port->drain, delay); 776 } 777 len = dma_fifo_level(&port->tx_fifo); 778 spin_unlock_bh(&port->lock); 779 780 /* wakeup the writer */ 781 if (drain && len < WAKEUP_CHARS) 782 tty_wakeup(tty); 783 } 784 785 clear_bit(IN_TX, &port->flags); 786 wake_up_interruptible(&port->wait_tx); 787 788out: 789 rcu_read_unlock(); 790 tty_kref_put(tty); 791 return n; 792} 793 794static void fwtty_drain_tx(struct work_struct *work) 795{ 796 struct fwtty_port *port = to_port(to_delayed_work(work), drain); 797 798 fwtty_tx(port, true); 799} 800 801static void fwtty_write_xchar(struct fwtty_port *port, char ch) 802{ 803 struct fwtty_peer *peer; 804 805 ++port->stats.xchars; 806 807 fwtty_dbg(port, "%02x\n", ch); 808 809 rcu_read_lock(); 810 peer = rcu_dereference(port->peer); 811 if (peer) { 812 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST, 813 peer->fifo_addr, &ch, sizeof(ch), 814 NULL, port); 815 } 816 rcu_read_unlock(); 817} 818 819static struct fwtty_port *fwtty_port_get(unsigned int index) 820{ 821 struct fwtty_port *port; 822 823 if (index >= MAX_TOTAL_PORTS) 824 return NULL; 825 826 mutex_lock(&port_table_lock); 827 port = port_table[index]; 828 if (port) 829 kref_get(&port->serial->kref); 830 mutex_unlock(&port_table_lock); 831 return port; 832} 833 834static int fwtty_ports_add(struct fw_serial *serial) 835{ 836 int err = -EBUSY; 837 int i, j; 838 839 if (port_table_corrupt) 840 return err; 841 842 mutex_lock(&port_table_lock); 843 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) { 844 if (!port_table[i]) { 845 for (j = 0; j < num_ports; ++i, ++j) { 846 serial->ports[j]->index = i; 847 port_table[i] = serial->ports[j]; 848 } 849 err = 0; 850 break; 851 } 852 } 853 mutex_unlock(&port_table_lock); 854 return err; 855} 856 857static void fwserial_destroy(struct kref *kref) 858{ 859 struct fw_serial *serial = to_serial(kref, kref); 860 struct fwtty_port **ports = serial->ports; 861 int j, i = ports[0]->index; 862 863 synchronize_rcu(); 864 865 mutex_lock(&port_table_lock); 866 for (j = 0; j < num_ports; ++i, ++j) { 867 port_table_corrupt |= port_table[i] != ports[j]; 868 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p", 869 i, port_table[i], j, ports[j]); 870 871 port_table[i] = NULL; 872 } 873 mutex_unlock(&port_table_lock); 874 875 for (j = 0; j < num_ports; ++j) { 876 fw_core_remove_address_handler(&ports[j]->rx_handler); 877 tty_port_destroy(&ports[j]->port); 878 kfree(ports[j]); 879 } 880 kfree(serial); 881} 882 883static void fwtty_port_put(struct fwtty_port *port) 884{ 885 kref_put(&port->serial->kref, fwserial_destroy); 886} 887 888static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on) 889{ 890 struct fwtty_port *port = to_port(tty_port, port); 891 892 fwtty_dbg(port, "on/off: %d\n", on); 893 894 spin_lock_bh(&port->lock); 895 /* Don't change carrier state if this is a console */ 896 if (!port->port.console) { 897 if (on) 898 port->mctrl |= TIOCM_DTR | TIOCM_RTS; 899 else 900 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS); 901 } 902 903 __fwtty_write_port_status(port); 904 spin_unlock_bh(&port->lock); 905} 906 907/** 908 * fwtty_port_carrier_raised: required tty_port operation 909 * 910 * This port operation is polled after a tty has been opened and is waiting for 911 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready(). 912 */ 913static int fwtty_port_carrier_raised(struct tty_port *tty_port) 914{ 915 struct fwtty_port *port = to_port(tty_port, port); 916 int rc; 917 918 rc = (port->mstatus & TIOCM_CAR); 919 920 fwtty_dbg(port, "%d\n", rc); 921 922 return rc; 923} 924 925static unsigned int set_termios(struct fwtty_port *port, struct tty_struct *tty) 926{ 927 unsigned int baud, frame; 928 929 baud = tty_termios_baud_rate(&tty->termios); 930 tty_termios_encode_baud_rate(&tty->termios, baud, baud); 931 932 /* compute bit count of 2 frames */ 933 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0); 934 935 switch (C_CSIZE(tty)) { 936 case CS5: 937 frame -= (C_CSTOPB(tty)) ? 1 : 0; 938 break; 939 case CS6: 940 frame += 2; 941 break; 942 case CS7: 943 frame += 4; 944 break; 945 case CS8: 946 frame += 6; 947 break; 948 } 949 950 port->cps = (baud << 1) / frame; 951 952 port->status_mask = UART_LSR_OE; 953 if (_I_FLAG(tty, BRKINT | PARMRK)) 954 port->status_mask |= UART_LSR_BI; 955 956 port->ignore_mask = 0; 957 if (I_IGNBRK(tty)) { 958 port->ignore_mask |= UART_LSR_BI; 959 if (I_IGNPAR(tty)) 960 port->ignore_mask |= UART_LSR_OE; 961 } 962 963 port->write_only = !C_CREAD(tty); 964 965 /* turn off echo and newline xlat if loopback */ 966 if (port->loopback) { 967 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE | 968 ECHONL | ECHOPRT | ECHOCTL); 969 tty->termios.c_oflag &= ~ONLCR; 970 } 971 972 return baud; 973} 974 975static int fwtty_port_activate(struct tty_port *tty_port, 976 struct tty_struct *tty) 977{ 978 struct fwtty_port *port = to_port(tty_port, port); 979 unsigned int baud; 980 int err; 981 982 set_bit(TTY_IO_ERROR, &tty->flags); 983 984 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN, 985 cache_line_size(), 986 port->max_payload, 987 FWTTY_PORT_MAX_PEND_DMA, 988 GFP_KERNEL); 989 if (err) 990 return err; 991 992 spin_lock_bh(&port->lock); 993 994 baud = set_termios(port, tty); 995 996 /* if console, don't change carrier state */ 997 if (!port->port.console) { 998 port->mctrl = 0; 999 if (baud != 0) 1000 port->mctrl = TIOCM_DTR | TIOCM_RTS; 1001 } 1002 1003 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) 1004 tty->hw_stopped = 1; 1005 1006 __fwtty_write_port_status(port); 1007 spin_unlock_bh(&port->lock); 1008 1009 clear_bit(TTY_IO_ERROR, &tty->flags); 1010 1011 return 0; 1012} 1013 1014/** 1015 * fwtty_port_shutdown 1016 * 1017 * Note: the tty port core ensures this is not the console and 1018 * manages TTY_IO_ERROR properly 1019 */ 1020static void fwtty_port_shutdown(struct tty_port *tty_port) 1021{ 1022 struct fwtty_port *port = to_port(tty_port, port); 1023 1024 /* TODO: cancel outstanding transactions */ 1025 1026 cancel_delayed_work_sync(&port->emit_breaks); 1027 cancel_delayed_work_sync(&port->drain); 1028 1029 spin_lock_bh(&port->lock); 1030 port->flags = 0; 1031 port->break_ctl = 0; 1032 port->overrun = 0; 1033 __fwtty_write_port_status(port); 1034 dma_fifo_free(&port->tx_fifo); 1035 spin_unlock_bh(&port->lock); 1036} 1037 1038static int fwtty_open(struct tty_struct *tty, struct file *fp) 1039{ 1040 struct fwtty_port *port = tty->driver_data; 1041 1042 return tty_port_open(&port->port, tty, fp); 1043} 1044 1045static void fwtty_close(struct tty_struct *tty, struct file *fp) 1046{ 1047 struct fwtty_port *port = tty->driver_data; 1048 1049 tty_port_close(&port->port, tty, fp); 1050} 1051 1052static void fwtty_hangup(struct tty_struct *tty) 1053{ 1054 struct fwtty_port *port = tty->driver_data; 1055 1056 tty_port_hangup(&port->port); 1057} 1058 1059static void fwtty_cleanup(struct tty_struct *tty) 1060{ 1061 struct fwtty_port *port = tty->driver_data; 1062 1063 tty->driver_data = NULL; 1064 fwtty_port_put(port); 1065} 1066 1067static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty) 1068{ 1069 struct fwtty_port *port = fwtty_port_get(tty->index); 1070 int err; 1071 1072 err = tty_standard_install(driver, tty); 1073 if (!err) 1074 tty->driver_data = port; 1075 else 1076 fwtty_port_put(port); 1077 return err; 1078} 1079 1080static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty) 1081{ 1082 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index)); 1083 int err; 1084 1085 err = tty_standard_install(driver, tty); 1086 if (!err) 1087 tty->driver_data = port; 1088 else 1089 fwtty_port_put(port); 1090 return err; 1091} 1092 1093static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c) 1094{ 1095 struct fwtty_port *port = tty->driver_data; 1096 int n, len; 1097 1098 fwtty_dbg(port, "%d\n", c); 1099 fwtty_profile_data(port->stats.writes, c); 1100 1101 spin_lock_bh(&port->lock); 1102 n = dma_fifo_in(&port->tx_fifo, buf, c); 1103 len = dma_fifo_out_level(&port->tx_fifo); 1104 if (len < DRAIN_THRESHOLD) 1105 schedule_delayed_work(&port->drain, 1); 1106 spin_unlock_bh(&port->lock); 1107 1108 if (len >= DRAIN_THRESHOLD) 1109 fwtty_tx(port, false); 1110 1111 debug_short_write(port, c, n); 1112 1113 return (n < 0) ? 0 : n; 1114} 1115 1116static int fwtty_write_room(struct tty_struct *tty) 1117{ 1118 struct fwtty_port *port = tty->driver_data; 1119 int n; 1120 1121 spin_lock_bh(&port->lock); 1122 n = dma_fifo_avail(&port->tx_fifo); 1123 spin_unlock_bh(&port->lock); 1124 1125 fwtty_dbg(port, "%d\n", n); 1126 1127 return n; 1128} 1129 1130static int fwtty_chars_in_buffer(struct tty_struct *tty) 1131{ 1132 struct fwtty_port *port = tty->driver_data; 1133 int n; 1134 1135 spin_lock_bh(&port->lock); 1136 n = dma_fifo_level(&port->tx_fifo); 1137 spin_unlock_bh(&port->lock); 1138 1139 fwtty_dbg(port, "%d\n", n); 1140 1141 return n; 1142} 1143 1144static void fwtty_send_xchar(struct tty_struct *tty, char ch) 1145{ 1146 struct fwtty_port *port = tty->driver_data; 1147 1148 fwtty_dbg(port, "%02x\n", ch); 1149 1150 fwtty_write_xchar(port, ch); 1151} 1152 1153static void fwtty_throttle(struct tty_struct *tty) 1154{ 1155 struct fwtty_port *port = tty->driver_data; 1156 1157 /* 1158 * Ignore throttling (but not unthrottling). 1159 * It only makes sense to throttle when data will no longer be 1160 * accepted by the tty flip buffer. For example, it is 1161 * possible for received data to overflow the tty buffer long 1162 * before the line discipline ever has a chance to throttle the driver. 1163 * Additionally, the driver may have already completed the I/O 1164 * but the tty buffer is still emptying, so the line discipline is 1165 * throttling and unthrottling nothing. 1166 */ 1167 1168 ++port->stats.throttled; 1169} 1170 1171static void fwtty_unthrottle(struct tty_struct *tty) 1172{ 1173 struct fwtty_port *port = tty->driver_data; 1174 1175 fwtty_dbg(port, "CRTSCTS: %d\n", C_CRTSCTS(tty) != 0); 1176 1177 fwtty_profile_fifo(port, port->stats.unthrottle); 1178 1179 spin_lock_bh(&port->lock); 1180 port->mctrl &= ~OOB_RX_THROTTLE; 1181 if (C_CRTSCTS(tty)) 1182 port->mctrl |= TIOCM_RTS; 1183 __fwtty_write_port_status(port); 1184 spin_unlock_bh(&port->lock); 1185} 1186 1187static int check_msr_delta(struct fwtty_port *port, unsigned long mask, 1188 struct async_icount *prev) 1189{ 1190 struct async_icount now; 1191 int delta; 1192 1193 now = port->icount; 1194 1195 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) || 1196 (mask & TIOCM_DSR && prev->dsr != now.dsr) || 1197 (mask & TIOCM_CAR && prev->dcd != now.dcd) || 1198 (mask & TIOCM_CTS && prev->cts != now.cts)); 1199 1200 *prev = now; 1201 1202 return delta; 1203} 1204 1205static int wait_msr_change(struct fwtty_port *port, unsigned long mask) 1206{ 1207 struct async_icount prev; 1208 1209 prev = port->icount; 1210 1211 return wait_event_interruptible(port->port.delta_msr_wait, 1212 check_msr_delta(port, mask, &prev)); 1213} 1214 1215static int get_serial_info(struct tty_struct *tty, 1216 struct serial_struct *ss) 1217{ 1218 struct fwtty_port *port = tty->driver_data; 1219 1220 mutex_lock(&port->port.mutex); 1221 ss->line = port->index; 1222 ss->baud_base = 400000000; 1223 ss->close_delay = jiffies_to_msecs(port->port.close_delay) / 10; 1224 ss->closing_wait = 3000; 1225 mutex_unlock(&port->port.mutex); 1226 1227 return 0; 1228} 1229 1230static int set_serial_info(struct tty_struct *tty, 1231 struct serial_struct *ss) 1232{ 1233 struct fwtty_port *port = tty->driver_data; 1234 unsigned int cdelay; 1235 1236 cdelay = msecs_to_jiffies(ss->close_delay * 10); 1237 1238 mutex_lock(&port->port.mutex); 1239 if (!capable(CAP_SYS_ADMIN)) { 1240 if (cdelay != port->port.close_delay || 1241 ((ss->flags & ~ASYNC_USR_MASK) != 1242 (port->port.flags & ~ASYNC_USR_MASK))) { 1243 mutex_unlock(&port->port.mutex); 1244 return -EPERM; 1245 } 1246 } 1247 port->port.close_delay = cdelay; 1248 mutex_unlock(&port->port.mutex); 1249 1250 return 0; 1251} 1252 1253static int fwtty_ioctl(struct tty_struct *tty, unsigned int cmd, 1254 unsigned long arg) 1255{ 1256 struct fwtty_port *port = tty->driver_data; 1257 int err; 1258 1259 switch (cmd) { 1260 case TIOCMIWAIT: 1261 err = wait_msr_change(port, arg); 1262 break; 1263 1264 default: 1265 err = -ENOIOCTLCMD; 1266 } 1267 1268 return err; 1269} 1270 1271static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old) 1272{ 1273 struct fwtty_port *port = tty->driver_data; 1274 unsigned int baud; 1275 1276 spin_lock_bh(&port->lock); 1277 baud = set_termios(port, tty); 1278 1279 if ((baud == 0) && (old->c_cflag & CBAUD)) { 1280 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS); 1281 } else if ((baud != 0) && !(old->c_cflag & CBAUD)) { 1282 if (C_CRTSCTS(tty) || !tty_throttled(tty)) 1283 port->mctrl |= TIOCM_DTR | TIOCM_RTS; 1284 else 1285 port->mctrl |= TIOCM_DTR; 1286 } 1287 __fwtty_write_port_status(port); 1288 spin_unlock_bh(&port->lock); 1289 1290 if (old->c_cflag & CRTSCTS) { 1291 if (!C_CRTSCTS(tty)) { 1292 tty->hw_stopped = 0; 1293 fwtty_restart_tx(port); 1294 } 1295 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) { 1296 tty->hw_stopped = 1; 1297 } 1298} 1299 1300/** 1301 * fwtty_break_ctl - start/stop sending breaks 1302 * 1303 * Signals the remote to start or stop generating simulated breaks. 1304 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx 1305 * before signalling the break line status. This guarantees any pending rx will 1306 * be queued to the line discipline before break is simulated on the remote. 1307 * Conversely, turning off break_ctl requires signalling the line status change, 1308 * then enabling tx. 1309 */ 1310static int fwtty_break_ctl(struct tty_struct *tty, int state) 1311{ 1312 struct fwtty_port *port = tty->driver_data; 1313 long ret; 1314 1315 fwtty_dbg(port, "%d\n", state); 1316 1317 if (state == -1) { 1318 set_bit(STOP_TX, &port->flags); 1319 ret = wait_event_interruptible_timeout(port->wait_tx, 1320 !test_bit(IN_TX, &port->flags), 1321 10); 1322 if (ret == 0 || ret == -ERESTARTSYS) { 1323 clear_bit(STOP_TX, &port->flags); 1324 fwtty_restart_tx(port); 1325 return -EINTR; 1326 } 1327 } 1328 1329 spin_lock_bh(&port->lock); 1330 port->break_ctl = (state == -1); 1331 __fwtty_write_port_status(port); 1332 spin_unlock_bh(&port->lock); 1333 1334 if (state == 0) { 1335 spin_lock_bh(&port->lock); 1336 dma_fifo_reset(&port->tx_fifo); 1337 clear_bit(STOP_TX, &port->flags); 1338 spin_unlock_bh(&port->lock); 1339 } 1340 return 0; 1341} 1342 1343static int fwtty_tiocmget(struct tty_struct *tty) 1344{ 1345 struct fwtty_port *port = tty->driver_data; 1346 unsigned int tiocm; 1347 1348 spin_lock_bh(&port->lock); 1349 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK); 1350 spin_unlock_bh(&port->lock); 1351 1352 fwtty_dbg(port, "%x\n", tiocm); 1353 1354 return tiocm; 1355} 1356 1357static int fwtty_tiocmset(struct tty_struct *tty, 1358 unsigned int set, unsigned int clear) 1359{ 1360 struct fwtty_port *port = tty->driver_data; 1361 1362 fwtty_dbg(port, "set: %x clear: %x\n", set, clear); 1363 1364 /* TODO: simulate loopback if TIOCM_LOOP set */ 1365 1366 spin_lock_bh(&port->lock); 1367 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff); 1368 port->mctrl |= set & MCTRL_MASK & 0xffff; 1369 __fwtty_write_port_status(port); 1370 spin_unlock_bh(&port->lock); 1371 return 0; 1372} 1373 1374static int fwtty_get_icount(struct tty_struct *tty, 1375 struct serial_icounter_struct *icount) 1376{ 1377 struct fwtty_port *port = tty->driver_data; 1378 struct stats stats; 1379 1380 memcpy(&stats, &port->stats, sizeof(stats)); 1381 if (port->port.console) 1382 (*port->fwcon_ops->stats)(&stats, port->con_data); 1383 1384 icount->cts = port->icount.cts; 1385 icount->dsr = port->icount.dsr; 1386 icount->rng = port->icount.rng; 1387 icount->dcd = port->icount.dcd; 1388 icount->rx = port->icount.rx; 1389 icount->tx = port->icount.tx + stats.xchars; 1390 icount->frame = port->icount.frame; 1391 icount->overrun = port->icount.overrun; 1392 icount->parity = port->icount.parity; 1393 icount->brk = port->icount.brk; 1394 icount->buf_overrun = port->icount.overrun; 1395 return 0; 1396} 1397 1398static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port) 1399{ 1400 struct stats stats; 1401 1402 memcpy(&stats, &port->stats, sizeof(stats)); 1403 if (port->port.console) 1404 (*port->fwcon_ops->stats)(&stats, port->con_data); 1405 1406 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset, 1407 port->icount.tx + stats.xchars, port->icount.rx); 1408 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts, 1409 port->icount.dsr, port->icount.rng, port->icount.dcd); 1410 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame, 1411 port->icount.overrun, port->icount.parity, port->icount.brk); 1412} 1413 1414static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port) 1415{ 1416 struct stats stats; 1417 1418 memcpy(&stats, &port->stats, sizeof(stats)); 1419 if (port->port.console) 1420 (*port->fwcon_ops->stats)(&stats, port->con_data); 1421 1422 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped, 1423 stats.tx_stall, stats.fifo_errs, stats.lost); 1424 seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled); 1425 1426 if (port->port.console) { 1427 seq_puts(m, "\n "); 1428 (*port->fwcon_ops->proc_show)(m, port->con_data); 1429 } 1430 1431 fwtty_dump_profile(m, &port->stats); 1432} 1433 1434static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer) 1435{ 1436 int generation = peer->generation; 1437 1438 smp_rmb(); 1439 seq_printf(m, " %s:", dev_name(&peer->unit->device)); 1440 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation); 1441 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed, 1442 peer->max_payload, (unsigned long long)peer->guid); 1443 seq_printf(m, " mgmt:%012llx", (unsigned long long)peer->mgmt_addr); 1444 seq_printf(m, " addr:%012llx", (unsigned long long)peer->status_addr); 1445 seq_putc(m, '\n'); 1446} 1447 1448static int fwtty_proc_show(struct seq_file *m, void *v) 1449{ 1450 struct fwtty_port *port; 1451 int i; 1452 1453 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n"); 1454 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) { 1455 seq_printf(m, "%2d:", i); 1456 if (capable(CAP_SYS_ADMIN)) 1457 fwtty_proc_show_port(m, port); 1458 fwtty_port_put(port); 1459 seq_puts(m, "\n"); 1460 } 1461 return 0; 1462} 1463 1464static int fwtty_stats_show(struct seq_file *m, void *v) 1465{ 1466 struct fw_serial *serial = m->private; 1467 struct fwtty_port *port; 1468 int i; 1469 1470 for (i = 0; i < num_ports; ++i) { 1471 port = fwtty_port_get(serial->ports[i]->index); 1472 if (port) { 1473 seq_printf(m, "%2d:", port->index); 1474 fwtty_proc_show_port(m, port); 1475 fwtty_debugfs_show_port(m, port); 1476 fwtty_port_put(port); 1477 seq_puts(m, "\n"); 1478 } 1479 } 1480 return 0; 1481} 1482DEFINE_SHOW_ATTRIBUTE(fwtty_stats); 1483 1484static int fwtty_peers_show(struct seq_file *m, void *v) 1485{ 1486 struct fw_serial *serial = m->private; 1487 struct fwtty_peer *peer; 1488 1489 rcu_read_lock(); 1490 seq_printf(m, "card: %s guid: %016llx\n", 1491 dev_name(serial->card->device), 1492 (unsigned long long)serial->card->guid); 1493 list_for_each_entry_rcu(peer, &serial->peer_list, list) 1494 fwtty_debugfs_show_peer(m, peer); 1495 rcu_read_unlock(); 1496 return 0; 1497} 1498DEFINE_SHOW_ATTRIBUTE(fwtty_peers); 1499 1500static const struct tty_port_operations fwtty_port_ops = { 1501 .dtr_rts = fwtty_port_dtr_rts, 1502 .carrier_raised = fwtty_port_carrier_raised, 1503 .shutdown = fwtty_port_shutdown, 1504 .activate = fwtty_port_activate, 1505}; 1506 1507static const struct tty_operations fwtty_ops = { 1508 .open = fwtty_open, 1509 .close = fwtty_close, 1510 .hangup = fwtty_hangup, 1511 .cleanup = fwtty_cleanup, 1512 .install = fwtty_install, 1513 .write = fwtty_write, 1514 .write_room = fwtty_write_room, 1515 .chars_in_buffer = fwtty_chars_in_buffer, 1516 .send_xchar = fwtty_send_xchar, 1517 .throttle = fwtty_throttle, 1518 .unthrottle = fwtty_unthrottle, 1519 .ioctl = fwtty_ioctl, 1520 .set_termios = fwtty_set_termios, 1521 .break_ctl = fwtty_break_ctl, 1522 .tiocmget = fwtty_tiocmget, 1523 .tiocmset = fwtty_tiocmset, 1524 .get_icount = fwtty_get_icount, 1525 .set_serial = set_serial_info, 1526 .get_serial = get_serial_info, 1527 .proc_show = fwtty_proc_show, 1528}; 1529 1530static const struct tty_operations fwloop_ops = { 1531 .open = fwtty_open, 1532 .close = fwtty_close, 1533 .hangup = fwtty_hangup, 1534 .cleanup = fwtty_cleanup, 1535 .install = fwloop_install, 1536 .write = fwtty_write, 1537 .write_room = fwtty_write_room, 1538 .chars_in_buffer = fwtty_chars_in_buffer, 1539 .send_xchar = fwtty_send_xchar, 1540 .throttle = fwtty_throttle, 1541 .unthrottle = fwtty_unthrottle, 1542 .ioctl = fwtty_ioctl, 1543 .set_termios = fwtty_set_termios, 1544 .break_ctl = fwtty_break_ctl, 1545 .tiocmget = fwtty_tiocmget, 1546 .tiocmset = fwtty_tiocmset, 1547 .get_icount = fwtty_get_icount, 1548 .set_serial = set_serial_info, 1549 .get_serial = get_serial_info, 1550}; 1551 1552static inline int mgmt_pkt_expected_len(__be16 code) 1553{ 1554 static const struct fwserial_mgmt_pkt pkt; 1555 1556 switch (be16_to_cpu(code)) { 1557 case FWSC_VIRT_CABLE_PLUG: 1558 return sizeof(pkt.hdr) + sizeof(pkt.plug_req); 1559 1560 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */ 1561 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp); 1562 1563 case FWSC_VIRT_CABLE_UNPLUG: 1564 case FWSC_VIRT_CABLE_UNPLUG_RSP: 1565 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK: 1566 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK: 1567 return sizeof(pkt.hdr); 1568 1569 default: 1570 return -1; 1571 } 1572} 1573 1574static inline void fill_plug_params(struct virt_plug_params *params, 1575 struct fwtty_port *port) 1576{ 1577 u64 status_addr = port->rx_handler.offset; 1578 u64 fifo_addr = port->rx_handler.offset + 4; 1579 size_t fifo_len = port->rx_handler.length - 4; 1580 1581 params->status_hi = cpu_to_be32(status_addr >> 32); 1582 params->status_lo = cpu_to_be32(status_addr); 1583 params->fifo_hi = cpu_to_be32(fifo_addr >> 32); 1584 params->fifo_lo = cpu_to_be32(fifo_addr); 1585 params->fifo_len = cpu_to_be32(fifo_len); 1586} 1587 1588static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt, 1589 struct fwtty_port *port) 1590{ 1591 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG); 1592 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1593 fill_plug_params(&pkt->plug_req, port); 1594} 1595 1596static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt, 1597 struct fwtty_port *port) 1598{ 1599 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP); 1600 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1601 fill_plug_params(&pkt->plug_rsp, port); 1602} 1603 1604static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt) 1605{ 1606 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK); 1607 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1608} 1609 1610static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt) 1611{ 1612 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK); 1613 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1614} 1615 1616static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt) 1617{ 1618 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP); 1619 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1620} 1621 1622static void fwserial_virt_plug_complete(struct fwtty_peer *peer, 1623 struct virt_plug_params *params) 1624{ 1625 struct fwtty_port *port = peer->port; 1626 1627 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo); 1628 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo); 1629 peer->fifo_len = be32_to_cpu(params->fifo_len); 1630 peer_set_state(peer, FWPS_ATTACHED); 1631 1632 /* reconfigure tx_fifo optimally for this peer */ 1633 spin_lock_bh(&port->lock); 1634 port->max_payload = min(peer->max_payload, peer->fifo_len); 1635 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload); 1636 spin_unlock_bh(&peer->port->lock); 1637 1638 if (port->port.console && port->fwcon_ops->notify) 1639 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data); 1640 1641 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n", 1642 (unsigned long long)peer->guid, dev_name(port->device)); 1643} 1644 1645static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer, 1646 struct fwserial_mgmt_pkt *pkt) 1647{ 1648 int generation; 1649 int rcode, tries = 5; 1650 1651 do { 1652 generation = peer->generation; 1653 smp_rmb(); 1654 1655 rcode = fw_run_transaction(peer->serial->card, 1656 TCODE_WRITE_BLOCK_REQUEST, 1657 peer->node_id, 1658 generation, peer->speed, 1659 peer->mgmt_addr, 1660 pkt, be16_to_cpu(pkt->hdr.len)); 1661 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR || 1662 rcode == RCODE_GENERATION) { 1663 fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode); 1664 continue; 1665 } else { 1666 break; 1667 } 1668 } while (--tries > 0); 1669 return rcode; 1670} 1671 1672/** 1673 * fwserial_claim_port - attempt to claim port @ index for peer 1674 * 1675 * Returns ptr to claimed port or error code (as ERR_PTR()) 1676 * Can sleep - must be called from process context 1677 */ 1678static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer, 1679 int index) 1680{ 1681 struct fwtty_port *port; 1682 1683 if (index < 0 || index >= num_ports) 1684 return ERR_PTR(-EINVAL); 1685 1686 /* must guarantee that previous port releases have completed */ 1687 synchronize_rcu(); 1688 1689 port = peer->serial->ports[index]; 1690 spin_lock_bh(&port->lock); 1691 if (!rcu_access_pointer(port->peer)) 1692 rcu_assign_pointer(port->peer, peer); 1693 else 1694 port = ERR_PTR(-EBUSY); 1695 spin_unlock_bh(&port->lock); 1696 1697 return port; 1698} 1699 1700/** 1701 * fwserial_find_port - find avail port and claim for peer 1702 * 1703 * Returns ptr to claimed port or NULL if none avail 1704 * Can sleep - must be called from process context 1705 */ 1706static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer) 1707{ 1708 struct fwtty_port **ports = peer->serial->ports; 1709 int i; 1710 1711 /* must guarantee that previous port releases have completed */ 1712 synchronize_rcu(); 1713 1714 /* TODO: implement optional GUID-to-specific port # matching */ 1715 1716 /* find an unattached port (but not the loopback port, if present) */ 1717 for (i = 0; i < num_ttys; ++i) { 1718 spin_lock_bh(&ports[i]->lock); 1719 if (!ports[i]->peer) { 1720 /* claim port */ 1721 rcu_assign_pointer(ports[i]->peer, peer); 1722 spin_unlock_bh(&ports[i]->lock); 1723 return ports[i]; 1724 } 1725 spin_unlock_bh(&ports[i]->lock); 1726 } 1727 return NULL; 1728} 1729 1730static void fwserial_release_port(struct fwtty_port *port, bool reset) 1731{ 1732 /* drop carrier (and all other line status) */ 1733 if (reset) 1734 fwtty_update_port_status(port, 0); 1735 1736 spin_lock_bh(&port->lock); 1737 1738 /* reset dma fifo max transmission size back to S100 */ 1739 port->max_payload = link_speed_to_max_payload(SCODE_100); 1740 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload); 1741 1742 RCU_INIT_POINTER(port->peer, NULL); 1743 spin_unlock_bh(&port->lock); 1744 1745 if (port->port.console && port->fwcon_ops->notify) 1746 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data); 1747} 1748 1749static void fwserial_plug_timeout(struct timer_list *t) 1750{ 1751 struct fwtty_peer *peer = from_timer(peer, t, timer); 1752 struct fwtty_port *port; 1753 1754 spin_lock_bh(&peer->lock); 1755 if (peer->state != FWPS_PLUG_PENDING) { 1756 spin_unlock_bh(&peer->lock); 1757 return; 1758 } 1759 1760 port = peer_revert_state(peer); 1761 spin_unlock_bh(&peer->lock); 1762 1763 if (port) 1764 fwserial_release_port(port, false); 1765} 1766 1767/** 1768 * fwserial_connect_peer - initiate virtual cable with peer 1769 * 1770 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent, 1771 * otherwise error code. Must be called from process context. 1772 */ 1773static int fwserial_connect_peer(struct fwtty_peer *peer) 1774{ 1775 struct fwtty_port *port; 1776 struct fwserial_mgmt_pkt *pkt; 1777 int err, rcode; 1778 1779 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 1780 if (!pkt) 1781 return -ENOMEM; 1782 1783 port = fwserial_find_port(peer); 1784 if (!port) { 1785 fwtty_err(&peer->unit, "avail ports in use\n"); 1786 err = -EBUSY; 1787 goto free_pkt; 1788 } 1789 1790 spin_lock_bh(&peer->lock); 1791 1792 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */ 1793 if (peer->state != FWPS_NOT_ATTACHED) { 1794 err = -EBUSY; 1795 goto release_port; 1796 } 1797 1798 peer->port = port; 1799 peer_set_state(peer, FWPS_PLUG_PENDING); 1800 1801 fill_plug_req(pkt, peer->port); 1802 1803 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT); 1804 spin_unlock_bh(&peer->lock); 1805 1806 rcode = fwserial_send_mgmt_sync(peer, pkt); 1807 1808 spin_lock_bh(&peer->lock); 1809 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) { 1810 if (rcode == RCODE_CONFLICT_ERROR) 1811 err = -EAGAIN; 1812 else 1813 err = -EIO; 1814 goto cancel_timer; 1815 } 1816 spin_unlock_bh(&peer->lock); 1817 1818 kfree(pkt); 1819 return 0; 1820 1821cancel_timer: 1822 del_timer(&peer->timer); 1823 peer_revert_state(peer); 1824release_port: 1825 spin_unlock_bh(&peer->lock); 1826 fwserial_release_port(port, false); 1827free_pkt: 1828 kfree(pkt); 1829 return err; 1830} 1831 1832/** 1833 * fwserial_close_port - 1834 * HUP the tty (if the tty exists) and unregister the tty device. 1835 * Only used by the unit driver upon unit removal to disconnect and 1836 * cleanup all attached ports 1837 * 1838 * The port reference is put by fwtty_cleanup (if a reference was 1839 * ever taken). 1840 */ 1841static void fwserial_close_port(struct tty_driver *driver, 1842 struct fwtty_port *port) 1843{ 1844 struct tty_struct *tty; 1845 1846 mutex_lock(&port->port.mutex); 1847 tty = tty_port_tty_get(&port->port); 1848 if (tty) { 1849 tty_vhangup(tty); 1850 tty_kref_put(tty); 1851 } 1852 mutex_unlock(&port->port.mutex); 1853 1854 if (driver == fwloop_driver) 1855 tty_unregister_device(driver, loop_idx(port)); 1856 else 1857 tty_unregister_device(driver, port->index); 1858} 1859 1860/** 1861 * fwserial_lookup - finds first fw_serial associated with card 1862 * @card: fw_card to match 1863 * 1864 * NB: caller must be holding fwserial_list_mutex 1865 */ 1866static struct fw_serial *fwserial_lookup(struct fw_card *card) 1867{ 1868 struct fw_serial *serial; 1869 1870 list_for_each_entry(serial, &fwserial_list, list) { 1871 if (card == serial->card) 1872 return serial; 1873 } 1874 1875 return NULL; 1876} 1877 1878/** 1879 * __fwserial_lookup_rcu - finds first fw_serial associated with card 1880 * @card: fw_card to match 1881 * 1882 * NB: caller must be inside rcu_read_lock() section 1883 */ 1884static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card) 1885{ 1886 struct fw_serial *serial; 1887 1888 list_for_each_entry_rcu(serial, &fwserial_list, list) { 1889 if (card == serial->card) 1890 return serial; 1891 } 1892 1893 return NULL; 1894} 1895 1896/** 1897 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id 1898 * 1899 * If a matching peer could not be found for the specified generation/node id, 1900 * this could be because: 1901 * a) the generation has changed and one of the nodes hasn't updated yet 1902 * b) the remote node has created its remote unit device before this 1903 * local node has created its corresponding remote unit device 1904 * In either case, the remote node should retry 1905 * 1906 * Note: caller must be in rcu_read_lock() section 1907 */ 1908static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card, 1909 int generation, int id) 1910{ 1911 struct fw_serial *serial; 1912 struct fwtty_peer *peer; 1913 1914 serial = __fwserial_lookup_rcu(card); 1915 if (!serial) { 1916 /* 1917 * Something is very wrong - there should be a matching 1918 * fw_serial structure for every fw_card. Maybe the remote node 1919 * has created its remote unit device before this driver has 1920 * been probed for any unit devices... 1921 */ 1922 fwtty_err(card, "unknown card (guid %016llx)\n", 1923 (unsigned long long)card->guid); 1924 return NULL; 1925 } 1926 1927 list_for_each_entry_rcu(peer, &serial->peer_list, list) { 1928 int g = peer->generation; 1929 1930 smp_rmb(); 1931 if (generation == g && id == peer->node_id) 1932 return peer; 1933 } 1934 1935 return NULL; 1936} 1937 1938#ifdef DEBUG 1939static void __dump_peer_list(struct fw_card *card) 1940{ 1941 struct fw_serial *serial; 1942 struct fwtty_peer *peer; 1943 1944 serial = __fwserial_lookup_rcu(card); 1945 if (!serial) 1946 return; 1947 1948 list_for_each_entry_rcu(peer, &serial->peer_list, list) { 1949 int g = peer->generation; 1950 1951 smp_rmb(); 1952 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n", 1953 g, peer->node_id, (unsigned long long)peer->guid); 1954 } 1955} 1956#else 1957#define __dump_peer_list(s) 1958#endif 1959 1960static void fwserial_auto_connect(struct work_struct *work) 1961{ 1962 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect); 1963 int err; 1964 1965 err = fwserial_connect_peer(peer); 1966 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES) 1967 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY); 1968} 1969 1970static void fwserial_peer_workfn(struct work_struct *work) 1971{ 1972 struct fwtty_peer *peer = to_peer(work, work); 1973 1974 peer->workfn(work); 1975} 1976 1977/** 1978 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer' 1979 * @serial: aggregate representing the specific fw_card to add the peer to 1980 * @unit: 'peer' to create and add to peer_list of serial 1981 * 1982 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of 1983 * peers for a specific fw_card. Optionally, auto-attach this peer to an 1984 * available tty port. This function is called either directly or indirectly 1985 * as a result of a 'serial' unit device being created & probed. 1986 * 1987 * Note: this function is serialized with fwserial_remove_peer() by the 1988 * fwserial_list_mutex held in fwserial_probe(). 1989 * 1990 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained 1991 * via the dev_set_drvdata() for the device of the fw_unit. 1992 */ 1993static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit) 1994{ 1995 struct device *dev = &unit->device; 1996 struct fw_device *parent = fw_parent_device(unit); 1997 struct fwtty_peer *peer; 1998 struct fw_csr_iterator ci; 1999 int key, val; 2000 int generation; 2001 2002 peer = kzalloc(sizeof(*peer), GFP_KERNEL); 2003 if (!peer) 2004 return -ENOMEM; 2005 2006 peer_set_state(peer, FWPS_NOT_ATTACHED); 2007 2008 dev_set_drvdata(dev, peer); 2009 peer->unit = unit; 2010 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4]; 2011 peer->speed = parent->max_speed; 2012 peer->max_payload = min(device_max_receive(parent), 2013 link_speed_to_max_payload(peer->speed)); 2014 2015 generation = parent->generation; 2016 smp_rmb(); 2017 peer->node_id = parent->node_id; 2018 smp_wmb(); 2019 peer->generation = generation; 2020 2021 /* retrieve the mgmt bus addr from the unit directory */ 2022 fw_csr_iterator_init(&ci, unit->directory); 2023 while (fw_csr_iterator_next(&ci, &key, &val)) { 2024 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) { 2025 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val; 2026 break; 2027 } 2028 } 2029 if (peer->mgmt_addr == 0ULL) { 2030 /* 2031 * No mgmt address effectively disables VIRT_CABLE_PLUG - 2032 * this peer will not be able to attach to a remote 2033 */ 2034 peer_set_state(peer, FWPS_NO_MGMT_ADDR); 2035 } 2036 2037 spin_lock_init(&peer->lock); 2038 peer->port = NULL; 2039 2040 timer_setup(&peer->timer, fwserial_plug_timeout, 0); 2041 INIT_WORK(&peer->work, fwserial_peer_workfn); 2042 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect); 2043 2044 /* associate peer with specific fw_card */ 2045 peer->serial = serial; 2046 list_add_rcu(&peer->list, &serial->peer_list); 2047 2048 fwtty_info(&peer->unit, "peer added (guid:%016llx)\n", 2049 (unsigned long long)peer->guid); 2050 2051 /* identify the local unit & virt cable to loopback port */ 2052 if (parent->is_local) { 2053 serial->self = peer; 2054 if (create_loop_dev) { 2055 struct fwtty_port *port; 2056 2057 port = fwserial_claim_port(peer, num_ttys); 2058 if (!IS_ERR(port)) { 2059 struct virt_plug_params params; 2060 2061 spin_lock_bh(&peer->lock); 2062 peer->port = port; 2063 fill_plug_params(&params, port); 2064 fwserial_virt_plug_complete(peer, &params); 2065 spin_unlock_bh(&peer->lock); 2066 2067 fwtty_write_port_status(port); 2068 } 2069 } 2070 2071 } else if (auto_connect) { 2072 /* auto-attach to remote units only (if policy allows) */ 2073 schedule_delayed_work(&peer->connect, 1); 2074 } 2075 2076 return 0; 2077} 2078 2079/** 2080 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer' 2081 * 2082 * Remove a 'peer' from its list of peers. This function is only 2083 * called by fwserial_remove() on bus removal of the unit device. 2084 * 2085 * Note: this function is serialized with fwserial_add_peer() by the 2086 * fwserial_list_mutex held in fwserial_remove(). 2087 */ 2088static void fwserial_remove_peer(struct fwtty_peer *peer) 2089{ 2090 struct fwtty_port *port; 2091 2092 spin_lock_bh(&peer->lock); 2093 peer_set_state(peer, FWPS_GONE); 2094 spin_unlock_bh(&peer->lock); 2095 2096 cancel_delayed_work_sync(&peer->connect); 2097 cancel_work_sync(&peer->work); 2098 2099 spin_lock_bh(&peer->lock); 2100 /* if this unit is the local unit, clear link */ 2101 if (peer == peer->serial->self) 2102 peer->serial->self = NULL; 2103 2104 /* cancel the request timeout timer (if running) */ 2105 del_timer(&peer->timer); 2106 2107 port = peer->port; 2108 peer->port = NULL; 2109 2110 list_del_rcu(&peer->list); 2111 2112 fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n", 2113 (unsigned long long)peer->guid); 2114 2115 spin_unlock_bh(&peer->lock); 2116 2117 if (port) 2118 fwserial_release_port(port, true); 2119 2120 synchronize_rcu(); 2121 kfree(peer); 2122} 2123 2124/** 2125 * fwserial_create - init everything to create TTYs for a specific fw_card 2126 * @unit: fw_unit for first 'serial' unit device probed for this fw_card 2127 * 2128 * This function inits the aggregate structure (an fw_serial instance) 2129 * used to manage the TTY ports registered by a specific fw_card. Also, the 2130 * unit device is added as the first 'peer'. 2131 * 2132 * This unit device may represent a local unit device (as specified by the 2133 * config ROM unit directory) or it may represent a remote unit device 2134 * (as specified by the reading of the remote node's config ROM). 2135 * 2136 * Returns 0 to indicate "ownership" of the unit device, or a negative errno 2137 * value to indicate which error. 2138 */ 2139static int fwserial_create(struct fw_unit *unit) 2140{ 2141 struct fw_device *parent = fw_parent_device(unit); 2142 struct fw_card *card = parent->card; 2143 struct fw_serial *serial; 2144 struct fwtty_port *port; 2145 struct device *tty_dev; 2146 int i, j; 2147 int err; 2148 2149 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 2150 if (!serial) 2151 return -ENOMEM; 2152 2153 kref_init(&serial->kref); 2154 serial->card = card; 2155 INIT_LIST_HEAD(&serial->peer_list); 2156 2157 for (i = 0; i < num_ports; ++i) { 2158 port = kzalloc(sizeof(*port), GFP_KERNEL); 2159 if (!port) { 2160 err = -ENOMEM; 2161 goto free_ports; 2162 } 2163 tty_port_init(&port->port); 2164 port->index = FWTTY_INVALID_INDEX; 2165 port->port.ops = &fwtty_port_ops; 2166 port->serial = serial; 2167 tty_buffer_set_limit(&port->port, 128 * 1024); 2168 2169 spin_lock_init(&port->lock); 2170 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx); 2171 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks); 2172 INIT_WORK(&port->hangup, fwtty_do_hangup); 2173 init_waitqueue_head(&port->wait_tx); 2174 port->max_payload = link_speed_to_max_payload(SCODE_100); 2175 dma_fifo_init(&port->tx_fifo); 2176 2177 RCU_INIT_POINTER(port->peer, NULL); 2178 serial->ports[i] = port; 2179 2180 /* get unique bus addr region for port's status & recv fifo */ 2181 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4; 2182 port->rx_handler.address_callback = fwtty_port_handler; 2183 port->rx_handler.callback_data = port; 2184 /* 2185 * XXX: use custom memory region above cpu physical memory addrs 2186 * this will ease porting to 64-bit firewire adapters 2187 */ 2188 err = fw_core_add_address_handler(&port->rx_handler, 2189 &fw_high_memory_region); 2190 if (err) { 2191 tty_port_destroy(&port->port); 2192 kfree(port); 2193 goto free_ports; 2194 } 2195 } 2196 /* preserve i for error cleanup */ 2197 2198 err = fwtty_ports_add(serial); 2199 if (err) { 2200 fwtty_err(&unit, "no space in port table\n"); 2201 goto free_ports; 2202 } 2203 2204 for (j = 0; j < num_ttys; ++j) { 2205 tty_dev = tty_port_register_device(&serial->ports[j]->port, 2206 fwtty_driver, 2207 serial->ports[j]->index, 2208 card->device); 2209 if (IS_ERR(tty_dev)) { 2210 err = PTR_ERR(tty_dev); 2211 fwtty_err(&unit, "register tty device error (%d)\n", 2212 err); 2213 goto unregister_ttys; 2214 } 2215 2216 serial->ports[j]->device = tty_dev; 2217 } 2218 /* preserve j for error cleanup */ 2219 2220 if (create_loop_dev) { 2221 struct device *loop_dev; 2222 2223 loop_dev = tty_port_register_device(&serial->ports[j]->port, 2224 fwloop_driver, 2225 loop_idx(serial->ports[j]), 2226 card->device); 2227 if (IS_ERR(loop_dev)) { 2228 err = PTR_ERR(loop_dev); 2229 fwtty_err(&unit, "create loop device failed (%d)\n", 2230 err); 2231 goto unregister_ttys; 2232 } 2233 serial->ports[j]->device = loop_dev; 2234 serial->ports[j]->loopback = true; 2235 } 2236 2237 if (!IS_ERR_OR_NULL(fwserial_debugfs)) { 2238 serial->debugfs = debugfs_create_dir(dev_name(&unit->device), 2239 fwserial_debugfs); 2240 if (!IS_ERR_OR_NULL(serial->debugfs)) { 2241 debugfs_create_file("peers", 0444, serial->debugfs, 2242 serial, &fwtty_peers_fops); 2243 debugfs_create_file("stats", 0444, serial->debugfs, 2244 serial, &fwtty_stats_fops); 2245 } 2246 } 2247 2248 list_add_rcu(&serial->list, &fwserial_list); 2249 2250 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n", 2251 dev_name(card->device), (unsigned long long)card->guid); 2252 2253 err = fwserial_add_peer(serial, unit); 2254 if (!err) 2255 return 0; 2256 2257 fwtty_err(&unit, "unable to add peer unit device (%d)\n", err); 2258 2259 /* fall-through to error processing */ 2260 debugfs_remove_recursive(serial->debugfs); 2261 2262 list_del_rcu(&serial->list); 2263 if (create_loop_dev) 2264 tty_unregister_device(fwloop_driver, 2265 loop_idx(serial->ports[j])); 2266unregister_ttys: 2267 for (--j; j >= 0; --j) 2268 tty_unregister_device(fwtty_driver, serial->ports[j]->index); 2269 kref_put(&serial->kref, fwserial_destroy); 2270 return err; 2271 2272free_ports: 2273 for (--i; i >= 0; --i) { 2274 fw_core_remove_address_handler(&serial->ports[i]->rx_handler); 2275 tty_port_destroy(&serial->ports[i]->port); 2276 kfree(serial->ports[i]); 2277 } 2278 kfree(serial); 2279 return err; 2280} 2281 2282/** 2283 * fwserial_probe: bus probe function for firewire 'serial' unit devices 2284 * 2285 * A 'serial' unit device is created and probed as a result of: 2286 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated 2287 * 'serial' unit specifier id 2288 * - adding a unit directory to the config ROM(s) for a 'serial' unit 2289 * 2290 * The firewire core registers unit devices by enumerating unit directories 2291 * of a node's config ROM after reading the config ROM when a new node is 2292 * added to the bus topology after a bus reset. 2293 * 2294 * The practical implications of this are: 2295 * - this probe is called for both local and remote nodes that have a 'serial' 2296 * unit directory in their config ROM (that matches the specifiers in 2297 * fwserial_id_table). 2298 * - no specific order is enforced for local vs. remote unit devices 2299 * 2300 * This unit driver copes with the lack of specific order in the same way the 2301 * firewire net driver does -- each probe, for either a local or remote unit 2302 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the 2303 * first peer created for a given fw_card (tracked by the global fwserial_list) 2304 * creates the underlying TTYs (aggregated in a fw_serial instance). 2305 * 2306 * NB: an early attempt to differentiate local & remote unit devices by creating 2307 * peers only for remote units and fw_serial instances (with their 2308 * associated TTY devices) only for local units was discarded. Managing 2309 * the peer lifetimes on device removal proved too complicated. 2310 * 2311 * fwserial_probe/fwserial_remove are effectively serialized by the 2312 * fwserial_list_mutex. This is necessary because the addition of the first peer 2313 * for a given fw_card will trigger the creation of the fw_serial for that 2314 * fw_card, which must not simultaneously contend with the removal of the 2315 * last peer for a given fw_card triggering the destruction of the same 2316 * fw_serial for the same fw_card. 2317 */ 2318static int fwserial_probe(struct fw_unit *unit, 2319 const struct ieee1394_device_id *id) 2320{ 2321 struct fw_serial *serial; 2322 int err; 2323 2324 mutex_lock(&fwserial_list_mutex); 2325 serial = fwserial_lookup(fw_parent_device(unit)->card); 2326 if (!serial) 2327 err = fwserial_create(unit); 2328 else 2329 err = fwserial_add_peer(serial, unit); 2330 mutex_unlock(&fwserial_list_mutex); 2331 return err; 2332} 2333 2334/** 2335 * fwserial_remove: bus removal function for firewire 'serial' unit devices 2336 * 2337 * The corresponding 'peer' for this unit device is removed from the list of 2338 * peers for the associated fw_serial (which has a 1:1 correspondence with a 2339 * specific fw_card). If this is the last peer being removed, then trigger 2340 * the destruction of the underlying TTYs. 2341 */ 2342static void fwserial_remove(struct fw_unit *unit) 2343{ 2344 struct fwtty_peer *peer = dev_get_drvdata(&unit->device); 2345 struct fw_serial *serial = peer->serial; 2346 int i; 2347 2348 mutex_lock(&fwserial_list_mutex); 2349 fwserial_remove_peer(peer); 2350 2351 if (list_empty(&serial->peer_list)) { 2352 /* unlink from the fwserial_list here */ 2353 list_del_rcu(&serial->list); 2354 2355 debugfs_remove_recursive(serial->debugfs); 2356 2357 for (i = 0; i < num_ttys; ++i) 2358 fwserial_close_port(fwtty_driver, serial->ports[i]); 2359 if (create_loop_dev) 2360 fwserial_close_port(fwloop_driver, serial->ports[i]); 2361 kref_put(&serial->kref, fwserial_destroy); 2362 } 2363 mutex_unlock(&fwserial_list_mutex); 2364} 2365 2366/** 2367 * fwserial_update: bus update function for 'firewire' serial unit devices 2368 * 2369 * Updates the new node_id and bus generation for this peer. Note that locking 2370 * is unnecessary; but careful memory barrier usage is important to enforce the 2371 * load and store order of generation & node_id. 2372 * 2373 * The fw-core orders the write of node_id before generation in the parent 2374 * fw_device to ensure that a stale node_id cannot be used with a current 2375 * bus generation. So the generation value must be read before the node_id. 2376 * 2377 * In turn, this orders the write of node_id before generation in the peer to 2378 * also ensure a stale node_id cannot be used with a current bus generation. 2379 */ 2380static void fwserial_update(struct fw_unit *unit) 2381{ 2382 struct fw_device *parent = fw_parent_device(unit); 2383 struct fwtty_peer *peer = dev_get_drvdata(&unit->device); 2384 int generation; 2385 2386 generation = parent->generation; 2387 smp_rmb(); 2388 peer->node_id = parent->node_id; 2389 smp_wmb(); 2390 peer->generation = generation; 2391} 2392 2393static const struct ieee1394_device_id fwserial_id_table[] = { 2394 { 2395 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | 2396 IEEE1394_MATCH_VERSION, 2397 .specifier_id = LINUX_VENDOR_ID, 2398 .version = FWSERIAL_VERSION, 2399 }, 2400 { } 2401}; 2402 2403static struct fw_driver fwserial_driver = { 2404 .driver = { 2405 .owner = THIS_MODULE, 2406 .name = KBUILD_MODNAME, 2407 .bus = &fw_bus_type, 2408 }, 2409 .probe = fwserial_probe, 2410 .update = fwserial_update, 2411 .remove = fwserial_remove, 2412 .id_table = fwserial_id_table, 2413}; 2414 2415#define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id)) 2416#define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver)) 2417#define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \ 2418 | (((ofs) - CSR_REGISTER_BASE) >> 2)) 2419/* XXX: config ROM definitons could be improved with semi-automated offset 2420 * and length calculation 2421 */ 2422#define FW_ROM_LEN(quads) ((quads) << 16) 2423#define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs)) 2424 2425struct fwserial_unit_directory_data { 2426 u32 len_crc; 2427 u32 unit_specifier; 2428 u32 unit_sw_version; 2429 u32 unit_addr_offset; 2430 u32 desc1_ofs; 2431 u32 desc1_len_crc; 2432 u32 desc1_data[5]; 2433} __packed; 2434 2435static struct fwserial_unit_directory_data fwserial_unit_directory_data = { 2436 .len_crc = FW_ROM_LEN(4), 2437 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID), 2438 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION), 2439 .desc1_ofs = FW_ROM_DESCRIPTOR(1), 2440 .desc1_len_crc = FW_ROM_LEN(5), 2441 .desc1_data = { 2442 0x00000000, /* type = text */ 2443 0x00000000, /* enc = ASCII, lang EN */ 2444 0x4c696e75, /* 'Linux TTY' */ 2445 0x78205454, 2446 0x59000000, 2447 }, 2448}; 2449 2450static struct fw_descriptor fwserial_unit_directory = { 2451 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32), 2452 .key = (CSR_DIRECTORY | CSR_UNIT) << 24, 2453 .data = (u32 *)&fwserial_unit_directory_data, 2454}; 2455 2456/* 2457 * The management address is in the unit space region but above other known 2458 * address users (to keep wild writes from causing havoc) 2459 */ 2460static const struct fw_address_region fwserial_mgmt_addr_region = { 2461 .start = CSR_REGISTER_BASE + 0x1e0000ULL, 2462 .end = 0x1000000000000ULL, 2463}; 2464 2465static struct fw_address_handler fwserial_mgmt_addr_handler; 2466 2467/** 2468 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work 2469 * @work: ptr to peer->work 2470 * 2471 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer. 2472 * 2473 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was 2474 * already sent to this peer. If so, the collision is resolved by comparing 2475 * guid values; the loser sends the plug response. 2476 * 2477 * Note: if an error prevents a response, don't do anything -- the 2478 * remote will timeout its request. 2479 */ 2480static void fwserial_handle_plug_req(struct work_struct *work) 2481{ 2482 struct fwtty_peer *peer = to_peer(work, work); 2483 struct virt_plug_params *plug_req = &peer->work_params.plug_req; 2484 struct fwtty_port *port; 2485 struct fwserial_mgmt_pkt *pkt; 2486 int rcode; 2487 2488 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 2489 if (!pkt) 2490 return; 2491 2492 port = fwserial_find_port(peer); 2493 2494 spin_lock_bh(&peer->lock); 2495 2496 switch (peer->state) { 2497 case FWPS_NOT_ATTACHED: 2498 if (!port) { 2499 fwtty_err(&peer->unit, "no more ports avail\n"); 2500 fill_plug_rsp_nack(pkt); 2501 } else { 2502 peer->port = port; 2503 fill_plug_rsp_ok(pkt, peer->port); 2504 peer_set_state(peer, FWPS_PLUG_RESPONDING); 2505 /* don't release claimed port */ 2506 port = NULL; 2507 } 2508 break; 2509 2510 case FWPS_PLUG_PENDING: 2511 if (peer->serial->card->guid > peer->guid) 2512 goto cleanup; 2513 2514 /* We lost - hijack the already-claimed port and send ok */ 2515 del_timer(&peer->timer); 2516 fill_plug_rsp_ok(pkt, peer->port); 2517 peer_set_state(peer, FWPS_PLUG_RESPONDING); 2518 break; 2519 2520 default: 2521 fill_plug_rsp_nack(pkt); 2522 } 2523 2524 spin_unlock_bh(&peer->lock); 2525 if (port) 2526 fwserial_release_port(port, false); 2527 2528 rcode = fwserial_send_mgmt_sync(peer, pkt); 2529 2530 spin_lock_bh(&peer->lock); 2531 if (peer->state == FWPS_PLUG_RESPONDING) { 2532 if (rcode == RCODE_COMPLETE) { 2533 struct fwtty_port *tmp = peer->port; 2534 2535 fwserial_virt_plug_complete(peer, plug_req); 2536 spin_unlock_bh(&peer->lock); 2537 2538 fwtty_write_port_status(tmp); 2539 spin_lock_bh(&peer->lock); 2540 } else { 2541 fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode); 2542 port = peer_revert_state(peer); 2543 } 2544 } 2545cleanup: 2546 spin_unlock_bh(&peer->lock); 2547 if (port) 2548 fwserial_release_port(port, false); 2549 kfree(pkt); 2550} 2551 2552static void fwserial_handle_unplug_req(struct work_struct *work) 2553{ 2554 struct fwtty_peer *peer = to_peer(work, work); 2555 struct fwtty_port *port = NULL; 2556 struct fwserial_mgmt_pkt *pkt; 2557 int rcode; 2558 2559 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 2560 if (!pkt) 2561 return; 2562 2563 spin_lock_bh(&peer->lock); 2564 2565 switch (peer->state) { 2566 case FWPS_ATTACHED: 2567 fill_unplug_rsp_ok(pkt); 2568 peer_set_state(peer, FWPS_UNPLUG_RESPONDING); 2569 break; 2570 2571 case FWPS_UNPLUG_PENDING: 2572 if (peer->serial->card->guid > peer->guid) 2573 goto cleanup; 2574 2575 /* We lost - send unplug rsp */ 2576 del_timer(&peer->timer); 2577 fill_unplug_rsp_ok(pkt); 2578 peer_set_state(peer, FWPS_UNPLUG_RESPONDING); 2579 break; 2580 2581 default: 2582 fill_unplug_rsp_nack(pkt); 2583 } 2584 2585 spin_unlock_bh(&peer->lock); 2586 2587 rcode = fwserial_send_mgmt_sync(peer, pkt); 2588 2589 spin_lock_bh(&peer->lock); 2590 if (peer->state == FWPS_UNPLUG_RESPONDING) { 2591 if (rcode != RCODE_COMPLETE) 2592 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n", 2593 rcode); 2594 port = peer_revert_state(peer); 2595 } 2596cleanup: 2597 spin_unlock_bh(&peer->lock); 2598 if (port) 2599 fwserial_release_port(port, true); 2600 kfree(pkt); 2601} 2602 2603static int fwserial_parse_mgmt_write(struct fwtty_peer *peer, 2604 struct fwserial_mgmt_pkt *pkt, 2605 unsigned long long addr, 2606 size_t len) 2607{ 2608 struct fwtty_port *port = NULL; 2609 bool reset = false; 2610 int rcode; 2611 2612 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr)) 2613 return RCODE_ADDRESS_ERROR; 2614 2615 if (len != be16_to_cpu(pkt->hdr.len) || 2616 len != mgmt_pkt_expected_len(pkt->hdr.code)) 2617 return RCODE_DATA_ERROR; 2618 2619 spin_lock_bh(&peer->lock); 2620 if (peer->state == FWPS_GONE) { 2621 /* 2622 * This should never happen - it would mean that the 2623 * remote unit that just wrote this transaction was 2624 * already removed from the bus -- and the removal was 2625 * processed before we rec'd this transaction 2626 */ 2627 fwtty_err(&peer->unit, "peer already removed\n"); 2628 spin_unlock_bh(&peer->lock); 2629 return RCODE_ADDRESS_ERROR; 2630 } 2631 2632 rcode = RCODE_COMPLETE; 2633 2634 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04x\n", pkt->hdr.code); 2635 2636 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) { 2637 case FWSC_VIRT_CABLE_PLUG: 2638 if (work_pending(&peer->work)) { 2639 fwtty_err(&peer->unit, "plug req: busy\n"); 2640 rcode = RCODE_CONFLICT_ERROR; 2641 2642 } else { 2643 peer->work_params.plug_req = pkt->plug_req; 2644 peer->workfn = fwserial_handle_plug_req; 2645 queue_work(system_unbound_wq, &peer->work); 2646 } 2647 break; 2648 2649 case FWSC_VIRT_CABLE_PLUG_RSP: 2650 if (peer->state != FWPS_PLUG_PENDING) { 2651 rcode = RCODE_CONFLICT_ERROR; 2652 2653 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) { 2654 fwtty_notice(&peer->unit, "NACK plug rsp\n"); 2655 port = peer_revert_state(peer); 2656 2657 } else { 2658 struct fwtty_port *tmp = peer->port; 2659 2660 fwserial_virt_plug_complete(peer, &pkt->plug_rsp); 2661 spin_unlock_bh(&peer->lock); 2662 2663 fwtty_write_port_status(tmp); 2664 spin_lock_bh(&peer->lock); 2665 } 2666 break; 2667 2668 case FWSC_VIRT_CABLE_UNPLUG: 2669 if (work_pending(&peer->work)) { 2670 fwtty_err(&peer->unit, "unplug req: busy\n"); 2671 rcode = RCODE_CONFLICT_ERROR; 2672 } else { 2673 peer->workfn = fwserial_handle_unplug_req; 2674 queue_work(system_unbound_wq, &peer->work); 2675 } 2676 break; 2677 2678 case FWSC_VIRT_CABLE_UNPLUG_RSP: 2679 if (peer->state != FWPS_UNPLUG_PENDING) { 2680 rcode = RCODE_CONFLICT_ERROR; 2681 } else { 2682 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) 2683 fwtty_notice(&peer->unit, "NACK unplug?\n"); 2684 port = peer_revert_state(peer); 2685 reset = true; 2686 } 2687 break; 2688 2689 default: 2690 fwtty_err(&peer->unit, "unknown mgmt code %d\n", 2691 be16_to_cpu(pkt->hdr.code)); 2692 rcode = RCODE_DATA_ERROR; 2693 } 2694 spin_unlock_bh(&peer->lock); 2695 2696 if (port) 2697 fwserial_release_port(port, reset); 2698 2699 return rcode; 2700} 2701 2702/** 2703 * fwserial_mgmt_handler: bus address handler for mgmt requests 2704 * @parameters: fw_address_callback_t as specified by firewire core interface 2705 * 2706 * This handler is responsible for handling virtual cable requests from remotes 2707 * for all cards. 2708 */ 2709static void fwserial_mgmt_handler(struct fw_card *card, 2710 struct fw_request *request, 2711 int tcode, int destination, int source, 2712 int generation, 2713 unsigned long long addr, 2714 void *data, size_t len, 2715 void *callback_data) 2716{ 2717 struct fwserial_mgmt_pkt *pkt = data; 2718 struct fwtty_peer *peer; 2719 int rcode; 2720 2721 rcu_read_lock(); 2722 peer = __fwserial_peer_by_node_id(card, generation, source); 2723 if (!peer) { 2724 fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source); 2725 __dump_peer_list(card); 2726 rcode = RCODE_CONFLICT_ERROR; 2727 2728 } else { 2729 switch (tcode) { 2730 case TCODE_WRITE_BLOCK_REQUEST: 2731 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len); 2732 break; 2733 2734 default: 2735 rcode = RCODE_TYPE_ERROR; 2736 } 2737 } 2738 2739 rcu_read_unlock(); 2740 fw_send_response(card, request, rcode); 2741} 2742 2743static int __init fwserial_init(void) 2744{ 2745 int err, num_loops = !!(create_loop_dev); 2746 2747 /* XXX: placeholder for a "firewire" debugfs node */ 2748 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL); 2749 2750 /* num_ttys/num_ports must not be set above the static alloc avail */ 2751 if (num_ttys + num_loops > MAX_CARD_PORTS) 2752 num_ttys = MAX_CARD_PORTS - num_loops; 2753 2754 num_ports = num_ttys + num_loops; 2755 2756 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW 2757 | TTY_DRIVER_DYNAMIC_DEV); 2758 if (IS_ERR(fwtty_driver)) { 2759 err = PTR_ERR(fwtty_driver); 2760 goto remove_debugfs; 2761 } 2762 2763 fwtty_driver->driver_name = KBUILD_MODNAME; 2764 fwtty_driver->name = tty_dev_name; 2765 fwtty_driver->major = 0; 2766 fwtty_driver->minor_start = 0; 2767 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL; 2768 fwtty_driver->subtype = SERIAL_TYPE_NORMAL; 2769 fwtty_driver->init_termios = tty_std_termios; 2770 fwtty_driver->init_termios.c_cflag |= CLOCAL; 2771 tty_set_operations(fwtty_driver, &fwtty_ops); 2772 2773 err = tty_register_driver(fwtty_driver); 2774 if (err) { 2775 pr_err("register tty driver failed (%d)\n", err); 2776 goto put_tty; 2777 } 2778 2779 if (create_loop_dev) { 2780 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports, 2781 TTY_DRIVER_REAL_RAW 2782 | TTY_DRIVER_DYNAMIC_DEV); 2783 if (IS_ERR(fwloop_driver)) { 2784 err = PTR_ERR(fwloop_driver); 2785 goto unregister_driver; 2786 } 2787 2788 fwloop_driver->driver_name = KBUILD_MODNAME "_loop"; 2789 fwloop_driver->name = loop_dev_name; 2790 fwloop_driver->major = 0; 2791 fwloop_driver->minor_start = 0; 2792 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL; 2793 fwloop_driver->subtype = SERIAL_TYPE_NORMAL; 2794 fwloop_driver->init_termios = tty_std_termios; 2795 fwloop_driver->init_termios.c_cflag |= CLOCAL; 2796 tty_set_operations(fwloop_driver, &fwloop_ops); 2797 2798 err = tty_register_driver(fwloop_driver); 2799 if (err) { 2800 pr_err("register loop driver failed (%d)\n", err); 2801 goto put_loop; 2802 } 2803 } 2804 2805 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache", 2806 sizeof(struct fwtty_transaction), 2807 0, 0, NULL); 2808 if (!fwtty_txn_cache) { 2809 err = -ENOMEM; 2810 goto unregister_loop; 2811 } 2812 2813 /* 2814 * Ideally, this address handler would be registered per local node 2815 * (rather than the same handler for all local nodes). However, 2816 * since the firewire core requires the config rom descriptor *before* 2817 * the local unit device(s) are created, a single management handler 2818 * must suffice for all local serial units. 2819 */ 2820 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt); 2821 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler; 2822 2823 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler, 2824 &fwserial_mgmt_addr_region); 2825 if (err) { 2826 pr_err("add management handler failed (%d)\n", err); 2827 goto destroy_cache; 2828 } 2829 2830 fwserial_unit_directory_data.unit_addr_offset = 2831 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset); 2832 err = fw_core_add_descriptor(&fwserial_unit_directory); 2833 if (err) { 2834 pr_err("add unit descriptor failed (%d)\n", err); 2835 goto remove_handler; 2836 } 2837 2838 err = driver_register(&fwserial_driver.driver); 2839 if (err) { 2840 pr_err("register fwserial driver failed (%d)\n", err); 2841 goto remove_descriptor; 2842 } 2843 2844 return 0; 2845 2846remove_descriptor: 2847 fw_core_remove_descriptor(&fwserial_unit_directory); 2848remove_handler: 2849 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler); 2850destroy_cache: 2851 kmem_cache_destroy(fwtty_txn_cache); 2852unregister_loop: 2853 if (create_loop_dev) 2854 tty_unregister_driver(fwloop_driver); 2855put_loop: 2856 if (create_loop_dev) 2857 put_tty_driver(fwloop_driver); 2858unregister_driver: 2859 tty_unregister_driver(fwtty_driver); 2860put_tty: 2861 put_tty_driver(fwtty_driver); 2862remove_debugfs: 2863 debugfs_remove_recursive(fwserial_debugfs); 2864 2865 return err; 2866} 2867 2868static void __exit fwserial_exit(void) 2869{ 2870 driver_unregister(&fwserial_driver.driver); 2871 fw_core_remove_descriptor(&fwserial_unit_directory); 2872 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler); 2873 kmem_cache_destroy(fwtty_txn_cache); 2874 if (create_loop_dev) { 2875 tty_unregister_driver(fwloop_driver); 2876 put_tty_driver(fwloop_driver); 2877 } 2878 tty_unregister_driver(fwtty_driver); 2879 put_tty_driver(fwtty_driver); 2880 debugfs_remove_recursive(fwserial_debugfs); 2881} 2882 2883module_init(fwserial_init); 2884module_exit(fwserial_exit); 2885 2886MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)"); 2887MODULE_DESCRIPTION("FireWire Serial TTY Driver"); 2888MODULE_LICENSE("GPL"); 2889MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table); 2890MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node"); 2891MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered"); 2892MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");