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