1 // SPDX-License-Identifier: GPL-2.0+
3 * FireWire Serial driver
5 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
22 inline u64 be32_to_u64(__be32 hi, __be32 lo)
24 return ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo));
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 */
30 /* configurable options */
31 static int num_ttys = 4; /* # of std ttys to create per fw_card */
32 /* - doubles as loopback port index */
33 static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
34 static bool create_loop_dev = true; /* create a loopback device for each card */
36 module_param_named(ttys, num_ttys, int, 0644);
37 module_param_named(auto, auto_connect, bool, 0644);
38 module_param_named(loop, create_loop_dev, bool, 0644);
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
46 #define WAKEUP_CHARS 256
49 * fwserial_list: list of every fw_serial created for each fw_card
50 * See discussion in fwserial_probe.
52 static LIST_HEAD(fwserial_list);
53 static DEFINE_MUTEX(fwserial_list_mutex);
56 * port_table: array of tty ports allocated to each fw_card
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.
62 static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
63 static DEFINE_MUTEX(port_table_lock);
64 static bool port_table_corrupt;
65 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
67 #define loop_idx(port) (((port)->index) / num_ports)
68 #define table_idx(loop) ((loop) * num_ports + num_ttys)
70 /* total # of tty ports created per fw_card */
73 /* slab used as pool for struct fwtty_transactions */
74 static struct kmem_cache *fwtty_txn_cache;
76 struct tty_driver *fwtty_driver;
77 static struct tty_driver *fwloop_driver;
79 static struct dentry *fwserial_debugfs;
81 struct fwtty_transaction;
82 typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
83 void *data, size_t length,
84 struct fwtty_transaction *txn);
86 struct fwtty_transaction {
87 struct fw_transaction fw_txn;
88 fwtty_transaction_cb callback;
89 struct fwtty_port *port;
91 struct dma_pending dma_pended;
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__)
108 static inline void debug_short_write(struct fwtty_port *port, int c, int n)
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",
121 #define debug_short_write(port, c, n)
124 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
125 int generation, int id);
127 #ifdef FWTTY_PROFILING
129 static void fwtty_profile_fifo(struct fwtty_port *port, unsigned int *stat)
131 spin_lock_bh(&port->lock);
132 fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo));
133 spin_unlock_bh(&port->lock);
136 static void fwtty_dump_profile(struct seq_file *m, struct stats *stats)
138 /* for each stat, print sum of 0 to 2^k, then individually */
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);
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]);
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]);
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]);
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]);
176 #define fwtty_profile_fifo(port, stat)
177 #define fwtty_dump_profile(m, stats)
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.
185 static inline int device_max_receive(struct fw_device *fw_device)
187 /* see IEEE 1394-2008 table 8-8 */
188 return min(2 << fw_device->max_rec, 4096);
191 static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
194 case RCODE_SEND_ERROR:
195 fwtty_err_ratelimited(port, "card busy\n");
197 case RCODE_ADDRESS_ERROR:
198 fwtty_err_ratelimited(port, "bad unit addr or write length\n");
200 case RCODE_DATA_ERROR:
201 fwtty_err_ratelimited(port, "failed rx\n");
204 fwtty_err_ratelimited(port, "missing ack\n");
207 fwtty_err_ratelimited(port, "remote busy\n");
210 fwtty_err_ratelimited(port, "failed tx: %d\n", rcode);
214 static void fwtty_common_callback(struct fw_card *card, int rcode,
215 void *payload, size_t len, void *cb_data)
217 struct fwtty_transaction *txn = cb_data;
218 struct fwtty_port *port = txn->port;
220 if (port && rcode != RCODE_COMPLETE)
221 fwtty_log_tx_error(port, rcode);
223 txn->callback(card, rcode, payload, len, txn);
224 kmem_cache_free(fwtty_txn_cache, txn);
227 static 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)
232 struct fwtty_transaction *txn;
235 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
239 txn->callback = callback;
242 generation = peer->generation;
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);
250 static 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)
258 txn->callback = callback;
261 generation = peer->generation;
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);
268 static void __fwtty_restart_tx(struct fwtty_port *port)
272 len = dma_fifo_out_level(&port->tx_fifo);
274 schedule_delayed_work(&port->drain, 0);
275 avail = dma_fifo_avail(&port->tx_fifo);
277 fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail);
280 static void fwtty_restart_tx(struct fwtty_port *port)
282 spin_lock_bh(&port->lock);
283 __fwtty_restart_tx(port);
284 spin_unlock_bh(&port->lock);
288 * fwtty_update_port_status - decodes & dispatches line status changes
290 * Note: in loopback, the port->lock is being held. Only use functions that
291 * don't attempt to reclaim the port->lock.
293 static void fwtty_update_port_status(struct fwtty_port *port,
297 struct tty_struct *tty;
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;
305 if (delta & TIOCM_RNG)
307 if (delta & TIOCM_DSR)
309 if (delta & TIOCM_CAR)
311 if (delta & TIOCM_CTS)
314 fwtty_dbg(port, "status: %x delta: %x\n", status, delta);
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);
322 schedule_work(&port->hangup);
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) {
334 __fwtty_restart_tx(port);
336 fwtty_restart_tx(port);
339 if (~status & TIOCM_CTS)
345 } else if (delta & OOB_TX_THROTTLE) {
346 tty = tty_port_tty_get(&port->port);
348 if (tty->hw_stopped) {
349 if (~status & OOB_TX_THROTTLE) {
352 __fwtty_restart_tx(port);
354 fwtty_restart_tx(port);
357 if (status & OOB_TX_THROTTLE)
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);
369 /* run emit_breaks one last time (if pending) */
370 mod_delayed_work(system_wq, &port->emit_breaks, 0);
374 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
375 wake_up_interruptible(&port->port.delta_msr_wait);
379 * __fwtty_port_line_status - generate 'line status' for indicated port
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.
385 * Note: caller must be holding port lock
387 static unsigned int __fwtty_port_line_status(struct fwtty_port *port)
389 unsigned int status = 0;
391 /* TODO: add module param to tie RNG to DTR as well */
393 if (port->mctrl & TIOCM_DTR)
394 status |= TIOCM_DSR | TIOCM_CAR;
395 if (port->mctrl & TIOCM_RTS)
397 if (port->mctrl & OOB_RX_THROTTLE)
398 status |= OOB_TX_THROTTLE;
399 /* emulate BRK as add'l line status */
401 status |= UART_LSR_BI << 24;
407 * __fwtty_write_port_status - send the port line status to peer
409 * Note: caller must be holding the port lock.
411 static int __fwtty_write_port_status(struct fwtty_port *port)
413 struct fwtty_peer *peer;
415 unsigned int status = __fwtty_port_line_status(port);
418 peer = rcu_dereference(port->peer);
420 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
421 peer->status_addr, &status,
422 sizeof(status), NULL, port);
430 * fwtty_write_port_status - same as above but locked by port lock
432 static int fwtty_write_port_status(struct fwtty_port *port)
436 spin_lock_bh(&port->lock);
437 err = __fwtty_write_port_status(port);
438 spin_unlock_bh(&port->lock);
442 static void fwtty_throttle_port(struct fwtty_port *port)
444 struct tty_struct *tty;
447 tty = tty_port_tty_get(&port->port);
451 spin_lock_bh(&port->lock);
454 port->mctrl |= OOB_RX_THROTTLE;
456 port->mctrl &= ~TIOCM_RTS;
457 if (~old & OOB_RX_THROTTLE)
458 __fwtty_write_port_status(port);
460 spin_unlock_bh(&port->lock);
466 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
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.
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.
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.
484 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
485 static void fwtty_do_hangup(struct work_struct *work)
487 struct fwtty_port *port = to_port(work, hangup);
488 struct tty_struct *tty;
490 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
492 tty = tty_port_tty_get(&port->port);
498 static void fwtty_emit_breaks(struct work_struct *work)
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;
506 /* generate breaks at the line rate (but at least 1) */
507 n = (elapsed * port->cps) / HZ + 1;
508 port->break_last = now;
510 fwtty_dbg(port, "sending %d brks\n", n);
514 c = tty_insert_flip_string_fixed_flag(&port->port, buf,
521 tty_flip_buffer_push(&port->port);
523 if (port->mstatus & (UART_LSR_BI << 24))
524 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
525 port->icount.brk += brk;
528 static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
534 fwtty_dbg(port, "%d\n", n);
535 fwtty_profile_data(port->stats.reads, n);
537 if (port->write_only) {
542 /* disregard break status; breaks are generated by emit_breaks work */
543 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
548 if (lsr & UART_LSR_OE)
549 ++port->icount.overrun;
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)) {
558 port->overrun = false;
560 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
561 /* TODO: don't drop SAK and Magic SysRq here */
566 c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n);
568 tty_flip_buffer_push(&port->port);
572 port->overrun = true;
574 fwtty_err_ratelimited(port, "flip buffer overrun\n");
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.
582 if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK)
583 fwtty_throttle_port(port);
587 port->icount.rx += len;
588 port->stats.lost += n;
593 * fwtty_port_handler - bus address handler for port reads/writes
595 * This handler is responsible for handling inbound read/write dma from remotes.
597 static void fwtty_port_handler(struct fw_card *card,
598 struct fw_request *request,
599 int tcode, int destination, int source,
601 unsigned long long addr,
602 void *data, size_t len,
605 struct fwtty_port *port = callback_data;
606 struct fwtty_peer *peer;
610 /* Only accept rx from the peer virtual-cabled to this port */
612 peer = __fwserial_peer_by_node_id(card, generation, source);
614 if (!peer || peer != rcu_access_pointer(port->peer)) {
615 rcode = RCODE_ADDRESS_ERROR;
616 fwtty_err_ratelimited(port, "ignoring unauthenticated data\n");
621 case TCODE_WRITE_QUADLET_REQUEST:
622 if (addr != port->rx_handler.offset || len != 4) {
623 rcode = RCODE_ADDRESS_ERROR;
625 fwtty_update_port_status(port, *(unsigned int *)data);
626 rcode = RCODE_COMPLETE;
630 case TCODE_WRITE_BLOCK_REQUEST:
631 if (addr != port->rx_handler.offset + 4 ||
632 len > port->rx_handler.length - 4) {
633 rcode = RCODE_ADDRESS_ERROR;
635 err = fwtty_rx(port, data, len);
638 rcode = RCODE_COMPLETE;
641 rcode = RCODE_DATA_ERROR;
644 rcode = RCODE_CONFLICT_ERROR;
651 rcode = RCODE_TYPE_ERROR;
655 fw_send_response(card, request, rcode);
659 * fwtty_tx_complete - callback for tx dma
660 * @data: ignored, has no meaning for write txns
661 * @length: ignored, has no meaning for write txns
663 * The writer must be woken here if the fifo has been emptied because it
664 * may have slept if chars_in_buffer was != 0
666 static void fwtty_tx_complete(struct fw_card *card, int rcode,
667 void *data, size_t length,
668 struct fwtty_transaction *txn)
670 struct fwtty_port *port = txn->port;
673 fwtty_dbg(port, "rcode: %d\n", rcode);
677 spin_lock_bh(&port->lock);
678 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
679 len = dma_fifo_level(&port->tx_fifo);
680 spin_unlock_bh(&port->lock);
682 port->icount.tx += txn->dma_pended.len;
686 /* TODO: implement retries */
687 spin_lock_bh(&port->lock);
688 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
689 len = dma_fifo_level(&port->tx_fifo);
690 spin_unlock_bh(&port->lock);
692 port->stats.dropped += txn->dma_pended.len;
695 if (len < WAKEUP_CHARS)
696 tty_port_tty_wakeup(&port->port);
699 static int fwtty_tx(struct fwtty_port *port, bool drain)
701 struct fwtty_peer *peer;
702 struct fwtty_transaction *txn;
703 struct tty_struct *tty;
706 tty = tty_port_tty_get(&port->port);
711 peer = rcu_dereference(port->peer);
717 if (test_and_set_bit(IN_TX, &port->flags)) {
722 /* try to write as many dma transactions out as possible */
724 while (!tty->flow.stopped && !tty->hw_stopped &&
725 !test_bit(STOP_TX, &port->flags)) {
726 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
732 spin_lock_bh(&port->lock);
733 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
734 spin_unlock_bh(&port->lock);
736 fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n);
739 kmem_cache_free(fwtty_txn_cache, txn);
741 ++port->stats.tx_stall;
742 } else if (n == -ENODATA) {
743 fwtty_profile_data(port->stats.txns, 0);
745 ++port->stats.fifo_errs;
746 fwtty_err_ratelimited(port, "fifo err: %d\n",
752 fwtty_profile_data(port->stats.txns, txn->dma_pended.len);
754 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
755 peer->fifo_addr, txn->dma_pended.data,
756 txn->dma_pended.len, fwtty_tx_complete,
761 * Stop tx if the 'last view' of the fifo is empty or if
762 * this is the writer and there's not enough data to bother
764 if (n == 0 || (!drain && n < WRITER_MINIMUM))
768 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
769 spin_lock_bh(&port->lock);
770 len = dma_fifo_out_level(&port->tx_fifo);
772 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
774 schedule_delayed_work(&port->drain, delay);
776 len = dma_fifo_level(&port->tx_fifo);
777 spin_unlock_bh(&port->lock);
779 /* wakeup the writer */
780 if (drain && len < WAKEUP_CHARS)
784 clear_bit(IN_TX, &port->flags);
785 wake_up_interruptible(&port->wait_tx);
793 static void fwtty_drain_tx(struct work_struct *work)
795 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
797 fwtty_tx(port, true);
800 static void fwtty_write_xchar(struct fwtty_port *port, char ch)
802 struct fwtty_peer *peer;
804 ++port->stats.xchars;
806 fwtty_dbg(port, "%02x\n", ch);
809 peer = rcu_dereference(port->peer);
811 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
812 peer->fifo_addr, &ch, sizeof(ch),
818 static struct fwtty_port *fwtty_port_get(unsigned int index)
820 struct fwtty_port *port;
822 if (index >= MAX_TOTAL_PORTS)
825 mutex_lock(&port_table_lock);
826 port = port_table[index];
828 kref_get(&port->serial->kref);
829 mutex_unlock(&port_table_lock);
833 static int fwtty_ports_add(struct fw_serial *serial)
838 if (port_table_corrupt)
841 mutex_lock(&port_table_lock);
842 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
843 if (!port_table[i]) {
844 for (j = 0; j < num_ports; ++i, ++j) {
845 serial->ports[j]->index = i;
846 port_table[i] = serial->ports[j];
852 mutex_unlock(&port_table_lock);
856 static void fwserial_destroy(struct kref *kref)
858 struct fw_serial *serial = to_serial(kref, kref);
859 struct fwtty_port **ports = serial->ports;
860 int j, i = ports[0]->index;
864 mutex_lock(&port_table_lock);
865 for (j = 0; j < num_ports; ++i, ++j) {
866 port_table_corrupt |= port_table[i] != ports[j];
867 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
868 i, port_table[i], j, ports[j]);
870 port_table[i] = NULL;
872 mutex_unlock(&port_table_lock);
874 for (j = 0; j < num_ports; ++j) {
875 fw_core_remove_address_handler(&ports[j]->rx_handler);
876 tty_port_destroy(&ports[j]->port);
882 static void fwtty_port_put(struct fwtty_port *port)
884 kref_put(&port->serial->kref, fwserial_destroy);
887 static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
889 struct fwtty_port *port = to_port(tty_port, port);
891 fwtty_dbg(port, "on/off: %d\n", on);
893 spin_lock_bh(&port->lock);
894 /* Don't change carrier state if this is a console */
895 if (!port->port.console) {
897 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
899 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
902 __fwtty_write_port_status(port);
903 spin_unlock_bh(&port->lock);
907 * fwtty_port_carrier_raised: required tty_port operation
909 * This port operation is polled after a tty has been opened and is waiting for
910 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
912 static int fwtty_port_carrier_raised(struct tty_port *tty_port)
914 struct fwtty_port *port = to_port(tty_port, port);
917 rc = (port->mstatus & TIOCM_CAR);
919 fwtty_dbg(port, "%d\n", rc);
924 static unsigned int set_termios(struct fwtty_port *port, struct tty_struct *tty)
926 unsigned int baud, frame;
928 baud = tty_termios_baud_rate(&tty->termios);
929 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
931 /* compute bit count of 2 frames */
932 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
934 switch (C_CSIZE(tty)) {
936 frame -= (C_CSTOPB(tty)) ? 1 : 0;
949 port->cps = (baud << 1) / frame;
951 port->status_mask = UART_LSR_OE;
952 if (_I_FLAG(tty, BRKINT | PARMRK))
953 port->status_mask |= UART_LSR_BI;
955 port->ignore_mask = 0;
957 port->ignore_mask |= UART_LSR_BI;
959 port->ignore_mask |= UART_LSR_OE;
962 port->write_only = !C_CREAD(tty);
964 /* turn off echo and newline xlat if loopback */
965 if (port->loopback) {
966 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
967 ECHONL | ECHOPRT | ECHOCTL);
968 tty->termios.c_oflag &= ~ONLCR;
974 static int fwtty_port_activate(struct tty_port *tty_port,
975 struct tty_struct *tty)
977 struct fwtty_port *port = to_port(tty_port, port);
981 set_bit(TTY_IO_ERROR, &tty->flags);
983 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
986 FWTTY_PORT_MAX_PEND_DMA,
991 spin_lock_bh(&port->lock);
993 baud = set_termios(port, tty);
995 /* if console, don't change carrier state */
996 if (!port->port.console) {
999 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1002 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1003 tty->hw_stopped = 1;
1005 __fwtty_write_port_status(port);
1006 spin_unlock_bh(&port->lock);
1008 clear_bit(TTY_IO_ERROR, &tty->flags);
1014 * fwtty_port_shutdown
1016 * Note: the tty port core ensures this is not the console and
1017 * manages TTY_IO_ERROR properly
1019 static void fwtty_port_shutdown(struct tty_port *tty_port)
1021 struct fwtty_port *port = to_port(tty_port, port);
1023 /* TODO: cancel outstanding transactions */
1025 cancel_delayed_work_sync(&port->emit_breaks);
1026 cancel_delayed_work_sync(&port->drain);
1028 spin_lock_bh(&port->lock);
1030 port->break_ctl = 0;
1032 __fwtty_write_port_status(port);
1033 dma_fifo_free(&port->tx_fifo);
1034 spin_unlock_bh(&port->lock);
1037 static int fwtty_open(struct tty_struct *tty, struct file *fp)
1039 struct fwtty_port *port = tty->driver_data;
1041 return tty_port_open(&port->port, tty, fp);
1044 static void fwtty_close(struct tty_struct *tty, struct file *fp)
1046 struct fwtty_port *port = tty->driver_data;
1048 tty_port_close(&port->port, tty, fp);
1051 static void fwtty_hangup(struct tty_struct *tty)
1053 struct fwtty_port *port = tty->driver_data;
1055 tty_port_hangup(&port->port);
1058 static void fwtty_cleanup(struct tty_struct *tty)
1060 struct fwtty_port *port = tty->driver_data;
1062 tty->driver_data = NULL;
1063 fwtty_port_put(port);
1066 static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1068 struct fwtty_port *port = fwtty_port_get(tty->index);
1071 err = tty_standard_install(driver, tty);
1073 tty->driver_data = port;
1075 fwtty_port_put(port);
1079 static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1081 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1084 err = tty_standard_install(driver, tty);
1086 tty->driver_data = port;
1088 fwtty_port_put(port);
1092 static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1094 struct fwtty_port *port = tty->driver_data;
1097 fwtty_dbg(port, "%d\n", c);
1098 fwtty_profile_data(port->stats.writes, c);
1100 spin_lock_bh(&port->lock);
1101 n = dma_fifo_in(&port->tx_fifo, buf, c);
1102 len = dma_fifo_out_level(&port->tx_fifo);
1103 if (len < DRAIN_THRESHOLD)
1104 schedule_delayed_work(&port->drain, 1);
1105 spin_unlock_bh(&port->lock);
1107 if (len >= DRAIN_THRESHOLD)
1108 fwtty_tx(port, false);
1110 debug_short_write(port, c, n);
1112 return (n < 0) ? 0 : n;
1115 static unsigned int fwtty_write_room(struct tty_struct *tty)
1117 struct fwtty_port *port = tty->driver_data;
1120 spin_lock_bh(&port->lock);
1121 n = dma_fifo_avail(&port->tx_fifo);
1122 spin_unlock_bh(&port->lock);
1124 fwtty_dbg(port, "%u\n", n);
1129 static unsigned int fwtty_chars_in_buffer(struct tty_struct *tty)
1131 struct fwtty_port *port = tty->driver_data;
1134 spin_lock_bh(&port->lock);
1135 n = dma_fifo_level(&port->tx_fifo);
1136 spin_unlock_bh(&port->lock);
1138 fwtty_dbg(port, "%u\n", n);
1143 static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1145 struct fwtty_port *port = tty->driver_data;
1147 fwtty_dbg(port, "%02x\n", ch);
1149 fwtty_write_xchar(port, ch);
1152 static void fwtty_throttle(struct tty_struct *tty)
1154 struct fwtty_port *port = tty->driver_data;
1157 * Ignore throttling (but not unthrottling).
1158 * It only makes sense to throttle when data will no longer be
1159 * accepted by the tty flip buffer. For example, it is
1160 * possible for received data to overflow the tty buffer long
1161 * before the line discipline ever has a chance to throttle the driver.
1162 * Additionally, the driver may have already completed the I/O
1163 * but the tty buffer is still emptying, so the line discipline is
1164 * throttling and unthrottling nothing.
1167 ++port->stats.throttled;
1170 static void fwtty_unthrottle(struct tty_struct *tty)
1172 struct fwtty_port *port = tty->driver_data;
1174 fwtty_dbg(port, "CRTSCTS: %d\n", C_CRTSCTS(tty) != 0);
1176 fwtty_profile_fifo(port, port->stats.unthrottle);
1178 spin_lock_bh(&port->lock);
1179 port->mctrl &= ~OOB_RX_THROTTLE;
1181 port->mctrl |= TIOCM_RTS;
1182 __fwtty_write_port_status(port);
1183 spin_unlock_bh(&port->lock);
1186 static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1187 struct async_icount *prev)
1189 struct async_icount now;
1194 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1195 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1196 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1197 (mask & TIOCM_CTS && prev->cts != now.cts));
1204 static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1206 struct async_icount prev;
1208 prev = port->icount;
1210 return wait_event_interruptible(port->port.delta_msr_wait,
1211 check_msr_delta(port, mask, &prev));
1214 static int get_serial_info(struct tty_struct *tty,
1215 struct serial_struct *ss)
1217 struct fwtty_port *port = tty->driver_data;
1219 mutex_lock(&port->port.mutex);
1220 ss->line = port->index;
1221 ss->baud_base = 400000000;
1222 ss->close_delay = jiffies_to_msecs(port->port.close_delay) / 10;
1223 ss->closing_wait = 3000;
1224 mutex_unlock(&port->port.mutex);
1229 static int set_serial_info(struct tty_struct *tty,
1230 struct serial_struct *ss)
1232 struct fwtty_port *port = tty->driver_data;
1233 unsigned int cdelay;
1235 cdelay = msecs_to_jiffies(ss->close_delay * 10);
1237 mutex_lock(&port->port.mutex);
1238 if (!capable(CAP_SYS_ADMIN)) {
1239 if (cdelay != port->port.close_delay ||
1240 ((ss->flags & ~ASYNC_USR_MASK) !=
1241 (port->port.flags & ~ASYNC_USR_MASK))) {
1242 mutex_unlock(&port->port.mutex);
1246 port->port.close_delay = cdelay;
1247 mutex_unlock(&port->port.mutex);
1252 static int fwtty_ioctl(struct tty_struct *tty, unsigned int cmd,
1255 struct fwtty_port *port = tty->driver_data;
1260 err = wait_msr_change(port, arg);
1270 static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1272 struct fwtty_port *port = tty->driver_data;
1275 spin_lock_bh(&port->lock);
1276 baud = set_termios(port, tty);
1278 if ((baud == 0) && (old->c_cflag & CBAUD)) {
1279 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1280 } else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1281 if (C_CRTSCTS(tty) || !tty_throttled(tty))
1282 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1284 port->mctrl |= TIOCM_DTR;
1286 __fwtty_write_port_status(port);
1287 spin_unlock_bh(&port->lock);
1289 if (old->c_cflag & CRTSCTS) {
1290 if (!C_CRTSCTS(tty)) {
1291 tty->hw_stopped = 0;
1292 fwtty_restart_tx(port);
1294 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1295 tty->hw_stopped = 1;
1300 * fwtty_break_ctl - start/stop sending breaks
1302 * Signals the remote to start or stop generating simulated breaks.
1303 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1304 * before signalling the break line status. This guarantees any pending rx will
1305 * be queued to the line discipline before break is simulated on the remote.
1306 * Conversely, turning off break_ctl requires signalling the line status change,
1309 static int fwtty_break_ctl(struct tty_struct *tty, int state)
1311 struct fwtty_port *port = tty->driver_data;
1314 fwtty_dbg(port, "%d\n", state);
1317 set_bit(STOP_TX, &port->flags);
1318 ret = wait_event_interruptible_timeout(port->wait_tx,
1319 !test_bit(IN_TX, &port->flags),
1321 if (ret == 0 || ret == -ERESTARTSYS) {
1322 clear_bit(STOP_TX, &port->flags);
1323 fwtty_restart_tx(port);
1328 spin_lock_bh(&port->lock);
1329 port->break_ctl = (state == -1);
1330 __fwtty_write_port_status(port);
1331 spin_unlock_bh(&port->lock);
1334 spin_lock_bh(&port->lock);
1335 dma_fifo_reset(&port->tx_fifo);
1336 clear_bit(STOP_TX, &port->flags);
1337 spin_unlock_bh(&port->lock);
1342 static int fwtty_tiocmget(struct tty_struct *tty)
1344 struct fwtty_port *port = tty->driver_data;
1347 spin_lock_bh(&port->lock);
1348 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1349 spin_unlock_bh(&port->lock);
1351 fwtty_dbg(port, "%x\n", tiocm);
1356 static int fwtty_tiocmset(struct tty_struct *tty,
1357 unsigned int set, unsigned int clear)
1359 struct fwtty_port *port = tty->driver_data;
1361 fwtty_dbg(port, "set: %x clear: %x\n", set, clear);
1363 /* TODO: simulate loopback if TIOCM_LOOP set */
1365 spin_lock_bh(&port->lock);
1366 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1367 port->mctrl |= set & MCTRL_MASK & 0xffff;
1368 __fwtty_write_port_status(port);
1369 spin_unlock_bh(&port->lock);
1373 static int fwtty_get_icount(struct tty_struct *tty,
1374 struct serial_icounter_struct *icount)
1376 struct fwtty_port *port = tty->driver_data;
1379 memcpy(&stats, &port->stats, sizeof(stats));
1380 if (port->port.console)
1381 (*port->fwcon_ops->stats)(&stats, port->con_data);
1383 icount->cts = port->icount.cts;
1384 icount->dsr = port->icount.dsr;
1385 icount->rng = port->icount.rng;
1386 icount->dcd = port->icount.dcd;
1387 icount->rx = port->icount.rx;
1388 icount->tx = port->icount.tx + stats.xchars;
1389 icount->frame = port->icount.frame;
1390 icount->overrun = port->icount.overrun;
1391 icount->parity = port->icount.parity;
1392 icount->brk = port->icount.brk;
1393 icount->buf_overrun = port->icount.overrun;
1397 static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1401 memcpy(&stats, &port->stats, sizeof(stats));
1402 if (port->port.console)
1403 (*port->fwcon_ops->stats)(&stats, port->con_data);
1405 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1406 port->icount.tx + stats.xchars, port->icount.rx);
1407 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1408 port->icount.dsr, port->icount.rng, port->icount.dcd);
1409 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1410 port->icount.overrun, port->icount.parity, port->icount.brk);
1413 static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1417 memcpy(&stats, &port->stats, sizeof(stats));
1418 if (port->port.console)
1419 (*port->fwcon_ops->stats)(&stats, port->con_data);
1421 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1422 stats.tx_stall, stats.fifo_errs, stats.lost);
1423 seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled);
1425 if (port->port.console) {
1427 (*port->fwcon_ops->proc_show)(m, port->con_data);
1430 fwtty_dump_profile(m, &port->stats);
1433 static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
1435 int generation = peer->generation;
1438 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1439 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1440 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1441 peer->max_payload, (unsigned long long)peer->guid);
1442 seq_printf(m, " mgmt:%012llx", (unsigned long long)peer->mgmt_addr);
1443 seq_printf(m, " addr:%012llx", (unsigned long long)peer->status_addr);
1447 static int fwtty_proc_show(struct seq_file *m, void *v)
1449 struct fwtty_port *port;
1452 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1453 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1454 seq_printf(m, "%2d:", i);
1455 if (capable(CAP_SYS_ADMIN))
1456 fwtty_proc_show_port(m, port);
1457 fwtty_port_put(port);
1463 static int fwtty_stats_show(struct seq_file *m, void *v)
1465 struct fw_serial *serial = m->private;
1466 struct fwtty_port *port;
1469 for (i = 0; i < num_ports; ++i) {
1470 port = fwtty_port_get(serial->ports[i]->index);
1472 seq_printf(m, "%2d:", port->index);
1473 fwtty_proc_show_port(m, port);
1474 fwtty_debugfs_show_port(m, port);
1475 fwtty_port_put(port);
1481 DEFINE_SHOW_ATTRIBUTE(fwtty_stats);
1483 static int fwtty_peers_show(struct seq_file *m, void *v)
1485 struct fw_serial *serial = m->private;
1486 struct fwtty_peer *peer;
1489 seq_printf(m, "card: %s guid: %016llx\n",
1490 dev_name(serial->card->device),
1491 (unsigned long long)serial->card->guid);
1492 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1493 fwtty_debugfs_show_peer(m, peer);
1497 DEFINE_SHOW_ATTRIBUTE(fwtty_peers);
1499 static const struct tty_port_operations fwtty_port_ops = {
1500 .dtr_rts = fwtty_port_dtr_rts,
1501 .carrier_raised = fwtty_port_carrier_raised,
1502 .shutdown = fwtty_port_shutdown,
1503 .activate = fwtty_port_activate,
1506 static const struct tty_operations fwtty_ops = {
1508 .close = fwtty_close,
1509 .hangup = fwtty_hangup,
1510 .cleanup = fwtty_cleanup,
1511 .install = fwtty_install,
1512 .write = fwtty_write,
1513 .write_room = fwtty_write_room,
1514 .chars_in_buffer = fwtty_chars_in_buffer,
1515 .send_xchar = fwtty_send_xchar,
1516 .throttle = fwtty_throttle,
1517 .unthrottle = fwtty_unthrottle,
1518 .ioctl = fwtty_ioctl,
1519 .set_termios = fwtty_set_termios,
1520 .break_ctl = fwtty_break_ctl,
1521 .tiocmget = fwtty_tiocmget,
1522 .tiocmset = fwtty_tiocmset,
1523 .get_icount = fwtty_get_icount,
1524 .set_serial = set_serial_info,
1525 .get_serial = get_serial_info,
1526 .proc_show = fwtty_proc_show,
1529 static const struct tty_operations fwloop_ops = {
1531 .close = fwtty_close,
1532 .hangup = fwtty_hangup,
1533 .cleanup = fwtty_cleanup,
1534 .install = fwloop_install,
1535 .write = fwtty_write,
1536 .write_room = fwtty_write_room,
1537 .chars_in_buffer = fwtty_chars_in_buffer,
1538 .send_xchar = fwtty_send_xchar,
1539 .throttle = fwtty_throttle,
1540 .unthrottle = fwtty_unthrottle,
1541 .ioctl = fwtty_ioctl,
1542 .set_termios = fwtty_set_termios,
1543 .break_ctl = fwtty_break_ctl,
1544 .tiocmget = fwtty_tiocmget,
1545 .tiocmset = fwtty_tiocmset,
1546 .get_icount = fwtty_get_icount,
1547 .set_serial = set_serial_info,
1548 .get_serial = get_serial_info,
1551 static inline int mgmt_pkt_expected_len(__be16 code)
1553 static const struct fwserial_mgmt_pkt pkt;
1555 switch (be16_to_cpu(code)) {
1556 case FWSC_VIRT_CABLE_PLUG:
1557 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1559 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1560 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1562 case FWSC_VIRT_CABLE_UNPLUG:
1563 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1564 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1565 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1566 return sizeof(pkt.hdr);
1573 static inline void fill_plug_params(struct virt_plug_params *params,
1574 struct fwtty_port *port)
1576 u64 status_addr = port->rx_handler.offset;
1577 u64 fifo_addr = port->rx_handler.offset + 4;
1578 size_t fifo_len = port->rx_handler.length - 4;
1580 params->status_hi = cpu_to_be32(status_addr >> 32);
1581 params->status_lo = cpu_to_be32(status_addr);
1582 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1583 params->fifo_lo = cpu_to_be32(fifo_addr);
1584 params->fifo_len = cpu_to_be32(fifo_len);
1587 static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1588 struct fwtty_port *port)
1590 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1591 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1592 fill_plug_params(&pkt->plug_req, port);
1595 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1596 struct fwtty_port *port)
1598 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1599 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1600 fill_plug_params(&pkt->plug_rsp, port);
1603 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1605 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1606 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1609 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1611 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1612 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1615 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1617 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1618 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1621 static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1622 struct virt_plug_params *params)
1624 struct fwtty_port *port = peer->port;
1626 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1627 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1628 peer->fifo_len = be32_to_cpu(params->fifo_len);
1629 peer_set_state(peer, FWPS_ATTACHED);
1631 /* reconfigure tx_fifo optimally for this peer */
1632 spin_lock_bh(&port->lock);
1633 port->max_payload = min(peer->max_payload, peer->fifo_len);
1634 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1635 spin_unlock_bh(&peer->port->lock);
1637 if (port->port.console && port->fwcon_ops->notify)
1638 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1640 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n",
1641 (unsigned long long)peer->guid, dev_name(port->device));
1644 static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1645 struct fwserial_mgmt_pkt *pkt)
1648 int rcode, tries = 5;
1651 generation = peer->generation;
1654 rcode = fw_run_transaction(peer->serial->card,
1655 TCODE_WRITE_BLOCK_REQUEST,
1657 generation, peer->speed,
1659 pkt, be16_to_cpu(pkt->hdr.len));
1660 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1661 rcode == RCODE_GENERATION) {
1662 fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode);
1667 } while (--tries > 0);
1672 * fwserial_claim_port - attempt to claim port @ index for peer
1674 * Returns ptr to claimed port or error code (as ERR_PTR())
1675 * Can sleep - must be called from process context
1677 static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1680 struct fwtty_port *port;
1682 if (index < 0 || index >= num_ports)
1683 return ERR_PTR(-EINVAL);
1685 /* must guarantee that previous port releases have completed */
1688 port = peer->serial->ports[index];
1689 spin_lock_bh(&port->lock);
1690 if (!rcu_access_pointer(port->peer))
1691 rcu_assign_pointer(port->peer, peer);
1693 port = ERR_PTR(-EBUSY);
1694 spin_unlock_bh(&port->lock);
1700 * fwserial_find_port - find avail port and claim for peer
1702 * Returns ptr to claimed port or NULL if none avail
1703 * Can sleep - must be called from process context
1705 static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1707 struct fwtty_port **ports = peer->serial->ports;
1710 /* must guarantee that previous port releases have completed */
1713 /* TODO: implement optional GUID-to-specific port # matching */
1715 /* find an unattached port (but not the loopback port, if present) */
1716 for (i = 0; i < num_ttys; ++i) {
1717 spin_lock_bh(&ports[i]->lock);
1718 if (!ports[i]->peer) {
1720 rcu_assign_pointer(ports[i]->peer, peer);
1721 spin_unlock_bh(&ports[i]->lock);
1724 spin_unlock_bh(&ports[i]->lock);
1729 static void fwserial_release_port(struct fwtty_port *port, bool reset)
1731 /* drop carrier (and all other line status) */
1733 fwtty_update_port_status(port, 0);
1735 spin_lock_bh(&port->lock);
1737 /* reset dma fifo max transmission size back to S100 */
1738 port->max_payload = link_speed_to_max_payload(SCODE_100);
1739 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1741 RCU_INIT_POINTER(port->peer, NULL);
1742 spin_unlock_bh(&port->lock);
1744 if (port->port.console && port->fwcon_ops->notify)
1745 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1748 static void fwserial_plug_timeout(struct timer_list *t)
1750 struct fwtty_peer *peer = from_timer(peer, t, timer);
1751 struct fwtty_port *port;
1753 spin_lock_bh(&peer->lock);
1754 if (peer->state != FWPS_PLUG_PENDING) {
1755 spin_unlock_bh(&peer->lock);
1759 port = peer_revert_state(peer);
1760 spin_unlock_bh(&peer->lock);
1763 fwserial_release_port(port, false);
1767 * fwserial_connect_peer - initiate virtual cable with peer
1769 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1770 * otherwise error code. Must be called from process context.
1772 static int fwserial_connect_peer(struct fwtty_peer *peer)
1774 struct fwtty_port *port;
1775 struct fwserial_mgmt_pkt *pkt;
1778 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1782 port = fwserial_find_port(peer);
1784 fwtty_err(&peer->unit, "avail ports in use\n");
1789 spin_lock_bh(&peer->lock);
1791 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1792 if (peer->state != FWPS_NOT_ATTACHED) {
1798 peer_set_state(peer, FWPS_PLUG_PENDING);
1800 fill_plug_req(pkt, peer->port);
1802 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1803 spin_unlock_bh(&peer->lock);
1805 rcode = fwserial_send_mgmt_sync(peer, pkt);
1807 spin_lock_bh(&peer->lock);
1808 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1809 if (rcode == RCODE_CONFLICT_ERROR)
1815 spin_unlock_bh(&peer->lock);
1821 del_timer(&peer->timer);
1822 peer_revert_state(peer);
1824 spin_unlock_bh(&peer->lock);
1825 fwserial_release_port(port, false);
1832 * fwserial_close_port -
1833 * HUP the tty (if the tty exists) and unregister the tty device.
1834 * Only used by the unit driver upon unit removal to disconnect and
1835 * cleanup all attached ports
1837 * The port reference is put by fwtty_cleanup (if a reference was
1840 static void fwserial_close_port(struct tty_driver *driver,
1841 struct fwtty_port *port)
1843 struct tty_struct *tty;
1845 mutex_lock(&port->port.mutex);
1846 tty = tty_port_tty_get(&port->port);
1851 mutex_unlock(&port->port.mutex);
1853 if (driver == fwloop_driver)
1854 tty_unregister_device(driver, loop_idx(port));
1856 tty_unregister_device(driver, port->index);
1860 * fwserial_lookup - finds first fw_serial associated with card
1861 * @card: fw_card to match
1863 * NB: caller must be holding fwserial_list_mutex
1865 static struct fw_serial *fwserial_lookup(struct fw_card *card)
1867 struct fw_serial *serial;
1869 list_for_each_entry(serial, &fwserial_list, list) {
1870 if (card == serial->card)
1878 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1879 * @card: fw_card to match
1881 * NB: caller must be inside rcu_read_lock() section
1883 static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
1885 struct fw_serial *serial;
1887 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1888 if (card == serial->card)
1896 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1898 * If a matching peer could not be found for the specified generation/node id,
1899 * this could be because:
1900 * a) the generation has changed and one of the nodes hasn't updated yet
1901 * b) the remote node has created its remote unit device before this
1902 * local node has created its corresponding remote unit device
1903 * In either case, the remote node should retry
1905 * Note: caller must be in rcu_read_lock() section
1907 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
1908 int generation, int id)
1910 struct fw_serial *serial;
1911 struct fwtty_peer *peer;
1913 serial = __fwserial_lookup_rcu(card);
1916 * Something is very wrong - there should be a matching
1917 * fw_serial structure for every fw_card. Maybe the remote node
1918 * has created its remote unit device before this driver has
1919 * been probed for any unit devices...
1921 fwtty_err(card, "unknown card (guid %016llx)\n",
1922 (unsigned long long)card->guid);
1926 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1927 int g = peer->generation;
1930 if (generation == g && id == peer->node_id)
1938 static void __dump_peer_list(struct fw_card *card)
1940 struct fw_serial *serial;
1941 struct fwtty_peer *peer;
1943 serial = __fwserial_lookup_rcu(card);
1947 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1948 int g = peer->generation;
1951 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n",
1952 g, peer->node_id, (unsigned long long)peer->guid);
1956 #define __dump_peer_list(s)
1959 static void fwserial_auto_connect(struct work_struct *work)
1961 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
1964 err = fwserial_connect_peer(peer);
1965 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
1966 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
1969 static void fwserial_peer_workfn(struct work_struct *work)
1971 struct fwtty_peer *peer = to_peer(work, work);
1977 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
1978 * @serial: aggregate representing the specific fw_card to add the peer to
1979 * @unit: 'peer' to create and add to peer_list of serial
1981 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
1982 * peers for a specific fw_card. Optionally, auto-attach this peer to an
1983 * available tty port. This function is called either directly or indirectly
1984 * as a result of a 'serial' unit device being created & probed.
1986 * Note: this function is serialized with fwserial_remove_peer() by the
1987 * fwserial_list_mutex held in fwserial_probe().
1989 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
1990 * via the dev_set_drvdata() for the device of the fw_unit.
1992 static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
1994 struct device *dev = &unit->device;
1995 struct fw_device *parent = fw_parent_device(unit);
1996 struct fwtty_peer *peer;
1997 struct fw_csr_iterator ci;
2001 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2005 peer_set_state(peer, FWPS_NOT_ATTACHED);
2007 dev_set_drvdata(dev, peer);
2009 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2010 peer->speed = parent->max_speed;
2011 peer->max_payload = min(device_max_receive(parent),
2012 link_speed_to_max_payload(peer->speed));
2014 generation = parent->generation;
2016 peer->node_id = parent->node_id;
2018 peer->generation = generation;
2020 /* retrieve the mgmt bus addr from the unit directory */
2021 fw_csr_iterator_init(&ci, unit->directory);
2022 while (fw_csr_iterator_next(&ci, &key, &val)) {
2023 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2024 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2028 if (peer->mgmt_addr == 0ULL) {
2030 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2031 * this peer will not be able to attach to a remote
2033 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2036 spin_lock_init(&peer->lock);
2039 timer_setup(&peer->timer, fwserial_plug_timeout, 0);
2040 INIT_WORK(&peer->work, fwserial_peer_workfn);
2041 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2043 /* associate peer with specific fw_card */
2044 peer->serial = serial;
2045 list_add_rcu(&peer->list, &serial->peer_list);
2047 fwtty_info(&peer->unit, "peer added (guid:%016llx)\n",
2048 (unsigned long long)peer->guid);
2050 /* identify the local unit & virt cable to loopback port */
2051 if (parent->is_local) {
2052 serial->self = peer;
2053 if (create_loop_dev) {
2054 struct fwtty_port *port;
2056 port = fwserial_claim_port(peer, num_ttys);
2057 if (!IS_ERR(port)) {
2058 struct virt_plug_params params;
2060 spin_lock_bh(&peer->lock);
2062 fill_plug_params(¶ms, port);
2063 fwserial_virt_plug_complete(peer, ¶ms);
2064 spin_unlock_bh(&peer->lock);
2066 fwtty_write_port_status(port);
2070 } else if (auto_connect) {
2071 /* auto-attach to remote units only (if policy allows) */
2072 schedule_delayed_work(&peer->connect, 1);
2079 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2081 * Remove a 'peer' from its list of peers. This function is only
2082 * called by fwserial_remove() on bus removal of the unit device.
2084 * Note: this function is serialized with fwserial_add_peer() by the
2085 * fwserial_list_mutex held in fwserial_remove().
2087 static void fwserial_remove_peer(struct fwtty_peer *peer)
2089 struct fwtty_port *port;
2091 spin_lock_bh(&peer->lock);
2092 peer_set_state(peer, FWPS_GONE);
2093 spin_unlock_bh(&peer->lock);
2095 cancel_delayed_work_sync(&peer->connect);
2096 cancel_work_sync(&peer->work);
2098 spin_lock_bh(&peer->lock);
2099 /* if this unit is the local unit, clear link */
2100 if (peer == peer->serial->self)
2101 peer->serial->self = NULL;
2103 /* cancel the request timeout timer (if running) */
2104 del_timer(&peer->timer);
2109 list_del_rcu(&peer->list);
2111 fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n",
2112 (unsigned long long)peer->guid);
2114 spin_unlock_bh(&peer->lock);
2117 fwserial_release_port(port, true);
2124 * fwserial_create - init everything to create TTYs for a specific fw_card
2125 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2127 * This function inits the aggregate structure (an fw_serial instance)
2128 * used to manage the TTY ports registered by a specific fw_card. Also, the
2129 * unit device is added as the first 'peer'.
2131 * This unit device may represent a local unit device (as specified by the
2132 * config ROM unit directory) or it may represent a remote unit device
2133 * (as specified by the reading of the remote node's config ROM).
2135 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2136 * value to indicate which error.
2138 static int fwserial_create(struct fw_unit *unit)
2140 struct fw_device *parent = fw_parent_device(unit);
2141 struct fw_card *card = parent->card;
2142 struct fw_serial *serial;
2143 struct fwtty_port *port;
2144 struct device *tty_dev;
2148 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2152 kref_init(&serial->kref);
2153 serial->card = card;
2154 INIT_LIST_HEAD(&serial->peer_list);
2156 for (i = 0; i < num_ports; ++i) {
2157 port = kzalloc(sizeof(*port), GFP_KERNEL);
2162 tty_port_init(&port->port);
2163 port->index = FWTTY_INVALID_INDEX;
2164 port->port.ops = &fwtty_port_ops;
2165 port->serial = serial;
2166 tty_buffer_set_limit(&port->port, 128 * 1024);
2168 spin_lock_init(&port->lock);
2169 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2170 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2171 INIT_WORK(&port->hangup, fwtty_do_hangup);
2172 init_waitqueue_head(&port->wait_tx);
2173 port->max_payload = link_speed_to_max_payload(SCODE_100);
2174 dma_fifo_init(&port->tx_fifo);
2176 RCU_INIT_POINTER(port->peer, NULL);
2177 serial->ports[i] = port;
2179 /* get unique bus addr region for port's status & recv fifo */
2180 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2181 port->rx_handler.address_callback = fwtty_port_handler;
2182 port->rx_handler.callback_data = port;
2184 * XXX: use custom memory region above cpu physical memory addrs
2185 * this will ease porting to 64-bit firewire adapters
2187 err = fw_core_add_address_handler(&port->rx_handler,
2188 &fw_high_memory_region);
2190 tty_port_destroy(&port->port);
2195 /* preserve i for error cleanup */
2197 err = fwtty_ports_add(serial);
2199 fwtty_err(&unit, "no space in port table\n");
2203 for (j = 0; j < num_ttys; ++j) {
2204 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2206 serial->ports[j]->index,
2208 if (IS_ERR(tty_dev)) {
2209 err = PTR_ERR(tty_dev);
2210 fwtty_err(&unit, "register tty device error (%d)\n",
2212 goto unregister_ttys;
2215 serial->ports[j]->device = tty_dev;
2217 /* preserve j for error cleanup */
2219 if (create_loop_dev) {
2220 struct device *loop_dev;
2222 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2224 loop_idx(serial->ports[j]),
2226 if (IS_ERR(loop_dev)) {
2227 err = PTR_ERR(loop_dev);
2228 fwtty_err(&unit, "create loop device failed (%d)\n",
2230 goto unregister_ttys;
2232 serial->ports[j]->device = loop_dev;
2233 serial->ports[j]->loopback = true;
2236 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2237 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2239 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2240 debugfs_create_file("peers", 0444, serial->debugfs,
2241 serial, &fwtty_peers_fops);
2242 debugfs_create_file("stats", 0444, serial->debugfs,
2243 serial, &fwtty_stats_fops);
2247 list_add_rcu(&serial->list, &fwserial_list);
2249 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n",
2250 dev_name(card->device), (unsigned long long)card->guid);
2252 err = fwserial_add_peer(serial, unit);
2256 fwtty_err(&unit, "unable to add peer unit device (%d)\n", err);
2258 /* fall-through to error processing */
2259 debugfs_remove_recursive(serial->debugfs);
2261 list_del_rcu(&serial->list);
2262 if (create_loop_dev)
2263 tty_unregister_device(fwloop_driver,
2264 loop_idx(serial->ports[j]));
2266 for (--j; j >= 0; --j)
2267 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2268 kref_put(&serial->kref, fwserial_destroy);
2272 for (--i; i >= 0; --i) {
2273 fw_core_remove_address_handler(&serial->ports[i]->rx_handler);
2274 tty_port_destroy(&serial->ports[i]->port);
2275 kfree(serial->ports[i]);
2282 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2284 * A 'serial' unit device is created and probed as a result of:
2285 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2286 * 'serial' unit specifier id
2287 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2289 * The firewire core registers unit devices by enumerating unit directories
2290 * of a node's config ROM after reading the config ROM when a new node is
2291 * added to the bus topology after a bus reset.
2293 * The practical implications of this are:
2294 * - this probe is called for both local and remote nodes that have a 'serial'
2295 * unit directory in their config ROM (that matches the specifiers in
2296 * fwserial_id_table).
2297 * - no specific order is enforced for local vs. remote unit devices
2299 * This unit driver copes with the lack of specific order in the same way the
2300 * firewire net driver does -- each probe, for either a local or remote unit
2301 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2302 * first peer created for a given fw_card (tracked by the global fwserial_list)
2303 * creates the underlying TTYs (aggregated in a fw_serial instance).
2305 * NB: an early attempt to differentiate local & remote unit devices by creating
2306 * peers only for remote units and fw_serial instances (with their
2307 * associated TTY devices) only for local units was discarded. Managing
2308 * the peer lifetimes on device removal proved too complicated.
2310 * fwserial_probe/fwserial_remove are effectively serialized by the
2311 * fwserial_list_mutex. This is necessary because the addition of the first peer
2312 * for a given fw_card will trigger the creation of the fw_serial for that
2313 * fw_card, which must not simultaneously contend with the removal of the
2314 * last peer for a given fw_card triggering the destruction of the same
2315 * fw_serial for the same fw_card.
2317 static int fwserial_probe(struct fw_unit *unit,
2318 const struct ieee1394_device_id *id)
2320 struct fw_serial *serial;
2323 mutex_lock(&fwserial_list_mutex);
2324 serial = fwserial_lookup(fw_parent_device(unit)->card);
2326 err = fwserial_create(unit);
2328 err = fwserial_add_peer(serial, unit);
2329 mutex_unlock(&fwserial_list_mutex);
2334 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2336 * The corresponding 'peer' for this unit device is removed from the list of
2337 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2338 * specific fw_card). If this is the last peer being removed, then trigger
2339 * the destruction of the underlying TTYs.
2341 static void fwserial_remove(struct fw_unit *unit)
2343 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2344 struct fw_serial *serial = peer->serial;
2347 mutex_lock(&fwserial_list_mutex);
2348 fwserial_remove_peer(peer);
2350 if (list_empty(&serial->peer_list)) {
2351 /* unlink from the fwserial_list here */
2352 list_del_rcu(&serial->list);
2354 debugfs_remove_recursive(serial->debugfs);
2356 for (i = 0; i < num_ttys; ++i)
2357 fwserial_close_port(fwtty_driver, serial->ports[i]);
2358 if (create_loop_dev)
2359 fwserial_close_port(fwloop_driver, serial->ports[i]);
2360 kref_put(&serial->kref, fwserial_destroy);
2362 mutex_unlock(&fwserial_list_mutex);
2366 * fwserial_update: bus update function for 'firewire' serial unit devices
2368 * Updates the new node_id and bus generation for this peer. Note that locking
2369 * is unnecessary; but careful memory barrier usage is important to enforce the
2370 * load and store order of generation & node_id.
2372 * The fw-core orders the write of node_id before generation in the parent
2373 * fw_device to ensure that a stale node_id cannot be used with a current
2374 * bus generation. So the generation value must be read before the node_id.
2376 * In turn, this orders the write of node_id before generation in the peer to
2377 * also ensure a stale node_id cannot be used with a current bus generation.
2379 static void fwserial_update(struct fw_unit *unit)
2381 struct fw_device *parent = fw_parent_device(unit);
2382 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2385 generation = parent->generation;
2387 peer->node_id = parent->node_id;
2389 peer->generation = generation;
2392 static const struct ieee1394_device_id fwserial_id_table[] = {
2394 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2395 IEEE1394_MATCH_VERSION,
2396 .specifier_id = LINUX_VENDOR_ID,
2397 .version = FWSERIAL_VERSION,
2402 static struct fw_driver fwserial_driver = {
2404 .owner = THIS_MODULE,
2405 .name = KBUILD_MODNAME,
2406 .bus = &fw_bus_type,
2408 .probe = fwserial_probe,
2409 .update = fwserial_update,
2410 .remove = fwserial_remove,
2411 .id_table = fwserial_id_table,
2414 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2415 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2416 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2417 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2418 /* XXX: config ROM definitons could be improved with semi-automated offset
2419 * and length calculation
2421 #define FW_ROM_LEN(quads) ((quads) << 16)
2422 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2424 struct fwserial_unit_directory_data {
2427 u32 unit_sw_version;
2428 u32 unit_addr_offset;
2434 static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
2435 .len_crc = FW_ROM_LEN(4),
2436 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2437 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2438 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
2439 .desc1_len_crc = FW_ROM_LEN(5),
2441 0x00000000, /* type = text */
2442 0x00000000, /* enc = ASCII, lang EN */
2443 0x4c696e75, /* 'Linux TTY' */
2449 static struct fw_descriptor fwserial_unit_directory = {
2450 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2451 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2452 .data = (u32 *)&fwserial_unit_directory_data,
2456 * The management address is in the unit space region but above other known
2457 * address users (to keep wild writes from causing havoc)
2459 static const struct fw_address_region fwserial_mgmt_addr_region = {
2460 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2461 .end = 0x1000000000000ULL,
2464 static struct fw_address_handler fwserial_mgmt_addr_handler;
2467 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2468 * @work: ptr to peer->work
2470 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2472 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2473 * already sent to this peer. If so, the collision is resolved by comparing
2474 * guid values; the loser sends the plug response.
2476 * Note: if an error prevents a response, don't do anything -- the
2477 * remote will timeout its request.
2479 static void fwserial_handle_plug_req(struct work_struct *work)
2481 struct fwtty_peer *peer = to_peer(work, work);
2482 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2483 struct fwtty_port *port;
2484 struct fwserial_mgmt_pkt *pkt;
2487 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2491 port = fwserial_find_port(peer);
2493 spin_lock_bh(&peer->lock);
2495 switch (peer->state) {
2496 case FWPS_NOT_ATTACHED:
2498 fwtty_err(&peer->unit, "no more ports avail\n");
2499 fill_plug_rsp_nack(pkt);
2502 fill_plug_rsp_ok(pkt, peer->port);
2503 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2504 /* don't release claimed port */
2509 case FWPS_PLUG_PENDING:
2510 if (peer->serial->card->guid > peer->guid)
2513 /* We lost - hijack the already-claimed port and send ok */
2514 del_timer(&peer->timer);
2515 fill_plug_rsp_ok(pkt, peer->port);
2516 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2520 fill_plug_rsp_nack(pkt);
2523 spin_unlock_bh(&peer->lock);
2525 fwserial_release_port(port, false);
2527 rcode = fwserial_send_mgmt_sync(peer, pkt);
2529 spin_lock_bh(&peer->lock);
2530 if (peer->state == FWPS_PLUG_RESPONDING) {
2531 if (rcode == RCODE_COMPLETE) {
2532 struct fwtty_port *tmp = peer->port;
2534 fwserial_virt_plug_complete(peer, plug_req);
2535 spin_unlock_bh(&peer->lock);
2537 fwtty_write_port_status(tmp);
2538 spin_lock_bh(&peer->lock);
2540 fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode);
2541 port = peer_revert_state(peer);
2545 spin_unlock_bh(&peer->lock);
2547 fwserial_release_port(port, false);
2551 static void fwserial_handle_unplug_req(struct work_struct *work)
2553 struct fwtty_peer *peer = to_peer(work, work);
2554 struct fwtty_port *port = NULL;
2555 struct fwserial_mgmt_pkt *pkt;
2558 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2562 spin_lock_bh(&peer->lock);
2564 switch (peer->state) {
2566 fill_unplug_rsp_ok(pkt);
2567 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2570 case FWPS_UNPLUG_PENDING:
2571 if (peer->serial->card->guid > peer->guid)
2574 /* We lost - send unplug rsp */
2575 del_timer(&peer->timer);
2576 fill_unplug_rsp_ok(pkt);
2577 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2581 fill_unplug_rsp_nack(pkt);
2584 spin_unlock_bh(&peer->lock);
2586 rcode = fwserial_send_mgmt_sync(peer, pkt);
2588 spin_lock_bh(&peer->lock);
2589 if (peer->state == FWPS_UNPLUG_RESPONDING) {
2590 if (rcode != RCODE_COMPLETE)
2591 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n",
2593 port = peer_revert_state(peer);
2596 spin_unlock_bh(&peer->lock);
2598 fwserial_release_port(port, true);
2602 static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2603 struct fwserial_mgmt_pkt *pkt,
2604 unsigned long long addr,
2607 struct fwtty_port *port = NULL;
2611 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2612 return RCODE_ADDRESS_ERROR;
2614 if (len != be16_to_cpu(pkt->hdr.len) ||
2615 len != mgmt_pkt_expected_len(pkt->hdr.code))
2616 return RCODE_DATA_ERROR;
2618 spin_lock_bh(&peer->lock);
2619 if (peer->state == FWPS_GONE) {
2621 * This should never happen - it would mean that the
2622 * remote unit that just wrote this transaction was
2623 * already removed from the bus -- and the removal was
2624 * processed before we rec'd this transaction
2626 fwtty_err(&peer->unit, "peer already removed\n");
2627 spin_unlock_bh(&peer->lock);
2628 return RCODE_ADDRESS_ERROR;
2631 rcode = RCODE_COMPLETE;
2633 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04x\n", pkt->hdr.code);
2635 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2636 case FWSC_VIRT_CABLE_PLUG:
2637 if (work_pending(&peer->work)) {
2638 fwtty_err(&peer->unit, "plug req: busy\n");
2639 rcode = RCODE_CONFLICT_ERROR;
2642 peer->work_params.plug_req = pkt->plug_req;
2643 peer->workfn = fwserial_handle_plug_req;
2644 queue_work(system_unbound_wq, &peer->work);
2648 case FWSC_VIRT_CABLE_PLUG_RSP:
2649 if (peer->state != FWPS_PLUG_PENDING) {
2650 rcode = RCODE_CONFLICT_ERROR;
2652 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2653 fwtty_notice(&peer->unit, "NACK plug rsp\n");
2654 port = peer_revert_state(peer);
2657 struct fwtty_port *tmp = peer->port;
2659 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2660 spin_unlock_bh(&peer->lock);
2662 fwtty_write_port_status(tmp);
2663 spin_lock_bh(&peer->lock);
2667 case FWSC_VIRT_CABLE_UNPLUG:
2668 if (work_pending(&peer->work)) {
2669 fwtty_err(&peer->unit, "unplug req: busy\n");
2670 rcode = RCODE_CONFLICT_ERROR;
2672 peer->workfn = fwserial_handle_unplug_req;
2673 queue_work(system_unbound_wq, &peer->work);
2677 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2678 if (peer->state != FWPS_UNPLUG_PENDING) {
2679 rcode = RCODE_CONFLICT_ERROR;
2681 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2682 fwtty_notice(&peer->unit, "NACK unplug?\n");
2683 port = peer_revert_state(peer);
2689 fwtty_err(&peer->unit, "unknown mgmt code %d\n",
2690 be16_to_cpu(pkt->hdr.code));
2691 rcode = RCODE_DATA_ERROR;
2693 spin_unlock_bh(&peer->lock);
2696 fwserial_release_port(port, reset);
2702 * fwserial_mgmt_handler: bus address handler for mgmt requests
2704 * This handler is responsible for handling virtual cable requests from remotes
2707 static void fwserial_mgmt_handler(struct fw_card *card,
2708 struct fw_request *request,
2709 int tcode, int destination, int source,
2711 unsigned long long addr,
2712 void *data, size_t len,
2713 void *callback_data)
2715 struct fwserial_mgmt_pkt *pkt = data;
2716 struct fwtty_peer *peer;
2720 peer = __fwserial_peer_by_node_id(card, generation, source);
2722 fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source);
2723 __dump_peer_list(card);
2724 rcode = RCODE_CONFLICT_ERROR;
2728 case TCODE_WRITE_BLOCK_REQUEST:
2729 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2733 rcode = RCODE_TYPE_ERROR;
2738 fw_send_response(card, request, rcode);
2741 static int __init fwserial_init(void)
2743 int err, num_loops = !!(create_loop_dev);
2745 /* XXX: placeholder for a "firewire" debugfs node */
2746 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2748 /* num_ttys/num_ports must not be set above the static alloc avail */
2749 if (num_ttys + num_loops > MAX_CARD_PORTS)
2750 num_ttys = MAX_CARD_PORTS - num_loops;
2752 num_ports = num_ttys + num_loops;
2754 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2755 | TTY_DRIVER_DYNAMIC_DEV);
2756 if (IS_ERR(fwtty_driver)) {
2757 err = PTR_ERR(fwtty_driver);
2758 goto remove_debugfs;
2761 fwtty_driver->driver_name = KBUILD_MODNAME;
2762 fwtty_driver->name = tty_dev_name;
2763 fwtty_driver->major = 0;
2764 fwtty_driver->minor_start = 0;
2765 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2766 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
2767 fwtty_driver->init_termios = tty_std_termios;
2768 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2769 tty_set_operations(fwtty_driver, &fwtty_ops);
2771 err = tty_register_driver(fwtty_driver);
2773 pr_err("register tty driver failed (%d)\n", err);
2777 if (create_loop_dev) {
2778 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2780 | TTY_DRIVER_DYNAMIC_DEV);
2781 if (IS_ERR(fwloop_driver)) {
2782 err = PTR_ERR(fwloop_driver);
2783 goto unregister_driver;
2786 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2787 fwloop_driver->name = loop_dev_name;
2788 fwloop_driver->major = 0;
2789 fwloop_driver->minor_start = 0;
2790 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2791 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
2792 fwloop_driver->init_termios = tty_std_termios;
2793 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2794 tty_set_operations(fwloop_driver, &fwloop_ops);
2796 err = tty_register_driver(fwloop_driver);
2798 pr_err("register loop driver failed (%d)\n", err);
2803 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2804 sizeof(struct fwtty_transaction),
2806 if (!fwtty_txn_cache) {
2808 goto unregister_loop;
2812 * Ideally, this address handler would be registered per local node
2813 * (rather than the same handler for all local nodes). However,
2814 * since the firewire core requires the config rom descriptor *before*
2815 * the local unit device(s) are created, a single management handler
2816 * must suffice for all local serial units.
2818 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2819 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2821 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2822 &fwserial_mgmt_addr_region);
2824 pr_err("add management handler failed (%d)\n", err);
2828 fwserial_unit_directory_data.unit_addr_offset =
2829 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2830 err = fw_core_add_descriptor(&fwserial_unit_directory);
2832 pr_err("add unit descriptor failed (%d)\n", err);
2833 goto remove_handler;
2836 err = driver_register(&fwserial_driver.driver);
2838 pr_err("register fwserial driver failed (%d)\n", err);
2839 goto remove_descriptor;
2845 fw_core_remove_descriptor(&fwserial_unit_directory);
2847 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2849 kmem_cache_destroy(fwtty_txn_cache);
2851 if (create_loop_dev)
2852 tty_unregister_driver(fwloop_driver);
2854 if (create_loop_dev)
2855 put_tty_driver(fwloop_driver);
2857 tty_unregister_driver(fwtty_driver);
2859 put_tty_driver(fwtty_driver);
2861 debugfs_remove_recursive(fwserial_debugfs);
2866 static void __exit fwserial_exit(void)
2868 driver_unregister(&fwserial_driver.driver);
2869 fw_core_remove_descriptor(&fwserial_unit_directory);
2870 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2871 kmem_cache_destroy(fwtty_txn_cache);
2872 if (create_loop_dev) {
2873 tty_unregister_driver(fwloop_driver);
2874 put_tty_driver(fwloop_driver);
2876 tty_unregister_driver(fwtty_driver);
2877 put_tty_driver(fwtty_driver);
2878 debugfs_remove_recursive(fwserial_debugfs);
2881 module_init(fwserial_init);
2882 module_exit(fwserial_exit);
2884 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2885 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2886 MODULE_LICENSE("GPL");
2887 MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
2888 MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
2889 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2890 MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");