2 * FireWire Serial driver
4 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/device.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/rculist.h>
26 #include <linux/workqueue.h>
27 #include <linux/ratelimit.h>
28 #include <linux/bug.h>
29 #include <linux/uaccess.h>
33 #define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
35 #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
36 #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
38 /* configurable options */
39 static int num_ttys = 4; /* # of std ttys to create per fw_card */
40 /* - doubles as loopback port index */
41 static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
42 static bool create_loop_dev = true; /* create a loopback device for each card */
44 module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
45 module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
46 module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
49 * Threshold below which the tty is woken for writing
50 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
51 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
52 * our fifo is below this level
54 #define WAKEUP_CHARS 256
57 * fwserial_list: list of every fw_serial created for each fw_card
58 * See discussion in fwserial_probe.
60 static LIST_HEAD(fwserial_list);
61 static DEFINE_MUTEX(fwserial_list_mutex);
64 * port_table: array of tty ports allocated to each fw_card
66 * tty ports are allocated during probe when an fw_serial is first
67 * created for a given fw_card. Ports are allocated in a contiguous block,
68 * each block consisting of 'num_ports' ports.
70 static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
71 static DEFINE_MUTEX(port_table_lock);
72 static bool port_table_corrupt;
73 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
75 /* total # of tty ports created per fw_card */
78 /* slab used as pool for struct fwtty_transactions */
79 static struct kmem_cache *fwtty_txn_cache;
81 struct tty_driver *fwtty_driver;
83 struct fwtty_transaction;
84 typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
85 void *data, size_t length,
86 struct fwtty_transaction *txn);
88 struct fwtty_transaction {
89 struct fw_transaction fw_txn;
90 fwtty_transaction_cb callback;
91 struct fwtty_port *port;
93 struct dma_pending dma_pended;
97 #define to_device(a, b) (a->b)
98 #define fwtty_err(p, s, v...) dev_err(to_device(p, device), s, ##v)
99 #define fwtty_info(p, s, v...) dev_info(to_device(p, device), s, ##v)
100 #define fwtty_notice(p, s, v...) dev_notice(to_device(p, device), s, ##v)
101 #define fwtty_dbg(p, s, v...) \
102 dev_dbg(to_device(p, device), "%s: " s, __func__, ##v)
103 #define fwtty_err_ratelimited(p, s, v...) \
104 dev_err_ratelimited(to_device(p, device), s, ##v)
107 static inline void debug_short_write(struct fwtty_port *port, int c, int n)
112 spin_lock_bh(&port->lock);
113 avail = dma_fifo_avail(&port->tx_fifo);
114 spin_unlock_bh(&port->lock);
115 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d",
120 #define debug_short_write(port, c, n)
123 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
124 int generation, int id);
126 #ifdef FWTTY_PROFILING
128 static void profile_fifo_avail(struct fwtty_port *port, unsigned *stat)
130 spin_lock_bh(&port->lock);
131 profile_size_distrib(stat, dma_fifo_avail(&port->tx_fifo));
132 spin_unlock_bh(&port->lock);
135 static void dump_profile(struct seq_file *m, struct stats *stats)
137 /* for each stat, print sum of 0 to 2^k, then individually */
143 snprintf(t, 10, "< %d", 1 << k);
144 seq_printf(m, "\n%14s %6s", " ", t);
145 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
146 seq_printf(m, "%6d", 1 << j);
149 for (j = 0, sum = 0; j <= k; ++j)
150 sum += stats->reads[j];
151 seq_printf(m, "\n%14s: %6d", "reads", sum);
152 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
153 seq_printf(m, "%6d", stats->reads[j]);
155 for (j = 0, sum = 0; j <= k; ++j)
156 sum += stats->writes[j];
157 seq_printf(m, "\n%14s: %6d", "writes", sum);
158 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
159 seq_printf(m, "%6d", stats->writes[j]);
161 for (j = 0, sum = 0; j <= k; ++j)
162 sum += stats->txns[j];
163 seq_printf(m, "\n%14s: %6d", "txns", sum);
164 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
165 seq_printf(m, "%6d", stats->txns[j]);
167 for (j = 0, sum = 0; j <= k; ++j)
168 sum += stats->unthrottle[j];
169 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
170 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
171 seq_printf(m, "%6d", stats->unthrottle[j]);
175 #define profile_fifo_avail(port, stat)
176 #define dump_profile(m, stats)
180 * Returns the max receive packet size for the given node
181 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
182 * are required by specification to support max_rec of 8 (512 bytes) or more.
184 static inline int device_max_receive(struct fw_device *fw_device)
186 /* see IEEE 1394-2008 table 8-8 */
187 return min(2 << fw_device->max_rec, 4096);
190 static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
193 case RCODE_SEND_ERROR:
194 fwtty_err_ratelimited(port, "card busy");
196 case RCODE_ADDRESS_ERROR:
197 fwtty_err_ratelimited(port, "bad unit addr or write length");
199 case RCODE_DATA_ERROR:
200 fwtty_err_ratelimited(port, "failed rx");
203 fwtty_err_ratelimited(port, "missing ack");
206 fwtty_err_ratelimited(port, "remote busy");
209 fwtty_err_ratelimited(port, "failed tx: %d", rcode);
213 static void fwtty_txn_constructor(void *this)
215 struct fwtty_transaction *txn = this;
217 init_timer(&txn->fw_txn.split_timeout_timer);
220 static void fwtty_common_callback(struct fw_card *card, int rcode,
221 void *payload, size_t len, void *cb_data)
223 struct fwtty_transaction *txn = cb_data;
224 struct fwtty_port *port = txn->port;
226 if (port && rcode != RCODE_COMPLETE)
227 fwtty_log_tx_error(port, rcode);
229 txn->callback(card, rcode, payload, len, txn);
230 kmem_cache_free(fwtty_txn_cache, txn);
233 static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
234 unsigned long long addr, void *payload,
235 size_t len, fwtty_transaction_cb callback,
236 struct fwtty_port *port)
238 struct fwtty_transaction *txn;
241 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
245 txn->callback = callback;
248 generation = peer->generation;
250 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
251 peer->node_id, generation, peer->speed, addr, payload,
252 len, fwtty_common_callback, txn);
256 static void fwtty_send_txn_async(struct fwtty_peer *peer,
257 struct fwtty_transaction *txn, int tcode,
258 unsigned long long addr, void *payload,
259 size_t len, fwtty_transaction_cb callback,
260 struct fwtty_port *port)
264 txn->callback = callback;
267 generation = peer->generation;
269 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
270 peer->node_id, generation, peer->speed, addr, payload,
271 len, fwtty_common_callback, txn);
275 static void __fwtty_restart_tx(struct fwtty_port *port)
279 len = dma_fifo_out_level(&port->tx_fifo);
281 schedule_delayed_work(&port->drain, 0);
282 avail = dma_fifo_avail(&port->tx_fifo);
284 fwtty_dbg(port, "fifo len: %d avail: %d", len, avail);
287 static void fwtty_restart_tx(struct fwtty_port *port)
289 spin_lock_bh(&port->lock);
290 __fwtty_restart_tx(port);
291 spin_unlock_bh(&port->lock);
295 * fwtty_update_port_status - decodes & dispatches line status changes
297 * Note: in loopback, the port->lock is being held. Only use functions that
298 * don't attempt to reclaim the port->lock.
300 static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
303 struct tty_struct *tty;
305 /* simulated LSR/MSR status from remote */
306 status &= ~MCTRL_MASK;
307 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
308 delta &= ~(status & TIOCM_RNG);
309 port->mstatus = status;
311 if (delta & TIOCM_RNG)
313 if (delta & TIOCM_DSR)
315 if (delta & TIOCM_CAR)
317 if (delta & TIOCM_CTS)
320 fwtty_dbg(port, "status: %x delta: %x", status, delta);
322 if (delta & TIOCM_CAR) {
323 tty = tty_port_tty_get(&port->port);
324 if (tty && !C_CLOCAL(tty)) {
325 if (status & TIOCM_CAR)
326 wake_up_interruptible(&port->port.open_wait);
328 schedule_work(&port->hangup);
333 if (delta & TIOCM_CTS) {
334 tty = tty_port_tty_get(&port->port);
335 if (tty && C_CRTSCTS(tty)) {
336 if (tty->hw_stopped) {
337 if (status & TIOCM_CTS) {
340 __fwtty_restart_tx(port);
342 fwtty_restart_tx(port);
345 if (~status & TIOCM_CTS)
351 } else if (delta & OOB_TX_THROTTLE) {
352 tty = tty_port_tty_get(&port->port);
354 if (tty->hw_stopped) {
355 if (~status & OOB_TX_THROTTLE) {
358 __fwtty_restart_tx(port);
360 fwtty_restart_tx(port);
363 if (status & OOB_TX_THROTTLE)
370 if (delta & (UART_LSR_BI << 24)) {
371 if (status & (UART_LSR_BI << 24)) {
372 port->break_last = jiffies;
373 schedule_delayed_work(&port->emit_breaks, 0);
375 /* run emit_breaks one last time (if pending) */
376 mod_delayed_work(system_wq, &port->emit_breaks, 0);
380 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
381 wake_up_interruptible(&port->port.delta_msr_wait);
385 * __fwtty_port_line_status - generate 'line status' for indicated port
387 * This function returns a remote 'MSR' state based on the local 'MCR' state,
388 * as if a null modem cable was attached. The actual status is a mangling
389 * of TIOCM_* bits suitable for sending to a peer's status_addr.
391 * Note: caller must be holding port lock
393 static unsigned __fwtty_port_line_status(struct fwtty_port *port)
397 /* TODO: add module param to tie RNG to DTR as well */
399 if (port->mctrl & TIOCM_DTR)
400 status |= TIOCM_DSR | TIOCM_CAR;
401 if (port->mctrl & TIOCM_RTS)
403 if (port->mctrl & OOB_RX_THROTTLE)
404 status |= OOB_TX_THROTTLE;
405 /* emulate BRK as add'l line status */
407 status |= UART_LSR_BI << 24;
413 * __fwtty_write_port_status - send the port line status to peer
415 * Note: caller must be holding the port lock.
417 static int __fwtty_write_port_status(struct fwtty_port *port)
419 struct fwtty_peer *peer;
421 unsigned status = __fwtty_port_line_status(port);
424 peer = rcu_dereference(port->peer);
426 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
427 peer->status_addr, &status,
428 sizeof(status), NULL, port);
436 * fwtty_write_port_status - same as above but locked by port lock
438 static int fwtty_write_port_status(struct fwtty_port *port)
442 spin_lock_bh(&port->lock);
443 err = __fwtty_write_port_status(port);
444 spin_unlock_bh(&port->lock);
448 static void __fwtty_throttle(struct fwtty_port *port, struct tty_struct *tty)
453 port->mctrl |= OOB_RX_THROTTLE;
455 port->mctrl &= ~TIOCM_RTS;
456 if (~old & OOB_RX_THROTTLE)
457 __fwtty_write_port_status(port);
461 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
463 * When the remote has finished tx, and all in-flight rx has been received and
464 * and pushed to the flip buffer, the remote may close its device. This will
465 * drop DTR on the remote which will drop carrier here. Typically, the tty is
466 * hung up when carrier is dropped or lost.
468 * However, there is a race between the hang up and the line discipline
469 * delivering its data to the reader. A hangup will cause the ldisc to flush
470 * (ie., clear) the read buffer and flip buffer. Because of firewire's
471 * relatively high throughput, the ldisc frequently lags well behind the driver,
472 * resulting in lost data (which has already been received and written to
473 * the flip buffer) when the remote closes its end.
475 * Unfortunately, since the flip buffer offers no direct method for determining
476 * if it holds data, ensuring the ldisc has delivered all data is problematic.
479 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
480 static void fwtty_do_hangup(struct work_struct *work)
482 struct fwtty_port *port = to_port(work, hangup);
483 struct tty_struct *tty;
485 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
487 tty = tty_port_tty_get(&port->port);
494 static void fwtty_emit_breaks(struct work_struct *work)
496 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
497 struct tty_struct *tty;
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;
503 tty = tty_port_tty_get(&port->port);
507 /* generate breaks at the line rate (but at least 1) */
508 n = (elapsed * port->cps) / HZ + 1;
509 port->break_last = now;
511 fwtty_dbg(port, "sending %d brks", n);
515 c = tty_insert_flip_string_fixed_flag(tty, buf, TTY_BREAK, t);
521 tty_flip_buffer_push(tty);
525 if (port->mstatus & (UART_LSR_BI << 24))
526 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
527 port->icount.brk += brk;
530 static void fwtty_pushrx(struct work_struct *work)
532 struct fwtty_port *port = to_port(work, push);
533 struct tty_struct *tty;
534 struct buffered_rx *buf, *next;
537 tty = tty_port_tty_get(&port->port);
541 spin_lock_bh(&port->lock);
542 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
543 n = tty_insert_flip_string_fixed_flag(tty, buf->data,
549 memmove(buf->data, buf->data + n, buf->n - n);
552 __fwtty_throttle(port, tty);
555 list_del(&buf->list);
560 tty_flip_buffer_push(tty);
562 if (list_empty(&port->buf_list))
563 clear_bit(BUFFERING_RX, &port->flags);
564 spin_unlock_bh(&port->lock);
569 static int fwtty_buffer_rx(struct fwtty_port *port, unsigned char *d, size_t n)
571 struct buffered_rx *buf;
572 size_t size = (n + sizeof(struct buffered_rx) + 0xFF) & ~0xFF;
574 if (port->buffered + n > HIGH_WATERMARK)
576 buf = kmalloc(size, GFP_ATOMIC);
579 INIT_LIST_HEAD(&buf->list);
581 memcpy(buf->data, d, n);
583 spin_lock_bh(&port->lock);
584 list_add_tail(&buf->list, &port->buf_list);
586 if (port->buffered > port->stats.watermark)
587 port->stats.watermark = port->buffered;
588 set_bit(BUFFERING_RX, &port->flags);
589 spin_unlock_bh(&port->lock);
594 static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
596 struct tty_struct *tty;
601 tty = tty_port_tty_get(&port->port);
605 fwtty_dbg(port, "%d", n);
606 profile_size_distrib(port->stats.reads, n);
608 if (port->write_only) {
613 /* disregard break status; breaks are generated by emit_breaks work */
614 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
619 if (lsr & UART_LSR_OE)
620 ++port->icount.overrun;
622 lsr &= port->status_mask;
623 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
624 if (!tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
629 port->overrun = false;
631 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
632 /* TODO: don't drop SAK and Magic SysRq here */
637 if (!test_bit(BUFFERING_RX, &port->flags)) {
638 c = tty_insert_flip_string_fixed_flag(tty, data, TTY_NORMAL, n);
640 tty_flip_buffer_push(tty);
644 /* start buffering and throttling */
645 n -= fwtty_buffer_rx(port, &data[c], n);
647 spin_lock_bh(&port->lock);
648 __fwtty_throttle(port, tty);
649 spin_unlock_bh(&port->lock);
652 n -= fwtty_buffer_rx(port, data, n);
655 port->overrun = true;
662 port->icount.rx += len;
663 port->stats.lost += n;
668 * fwtty_port_handler - bus address handler for port reads/writes
669 * @parameters: fw_address_callback_t as specified by firewire core interface
671 * This handler is responsible for handling inbound read/write dma from remotes.
673 static void fwtty_port_handler(struct fw_card *card,
674 struct fw_request *request,
675 int tcode, int destination, int source,
677 unsigned long long addr,
678 void *data, size_t len,
681 struct fwtty_port *port = callback_data;
682 struct fwtty_peer *peer;
686 /* Only accept rx from the peer virtual-cabled to this port */
688 peer = __fwserial_peer_by_node_id(card, generation, source);
690 if (!peer || peer != rcu_access_pointer(port->peer)) {
691 rcode = RCODE_ADDRESS_ERROR;
692 fwtty_err_ratelimited(port, "ignoring unauthenticated data");
697 case TCODE_WRITE_QUADLET_REQUEST:
698 if (addr != port->rx_handler.offset || len != 4)
699 rcode = RCODE_ADDRESS_ERROR;
701 fwtty_update_port_status(port, *(unsigned *)data);
702 rcode = RCODE_COMPLETE;
706 case TCODE_WRITE_BLOCK_REQUEST:
707 if (addr != port->rx_handler.offset + 4 ||
708 len > port->rx_handler.length - 4) {
709 rcode = RCODE_ADDRESS_ERROR;
711 err = fwtty_rx(port, data, len);
714 rcode = RCODE_COMPLETE;
717 rcode = RCODE_DATA_ERROR;
720 rcode = RCODE_CONFLICT_ERROR;
727 rcode = RCODE_TYPE_ERROR;
731 fw_send_response(card, request, rcode);
735 * fwtty_tx_complete - callback for tx dma
736 * @data: ignored, has no meaning for write txns
737 * @length: ignored, has no meaning for write txns
739 * The writer must be woken here if the fifo has been emptied because it
740 * may have slept if chars_in_buffer was != 0
742 static void fwtty_tx_complete(struct fw_card *card, int rcode,
743 void *data, size_t length,
744 struct fwtty_transaction *txn)
746 struct fwtty_port *port = txn->port;
747 struct tty_struct *tty;
750 fwtty_dbg(port, "rcode: %d", rcode);
754 spin_lock_bh(&port->lock);
755 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
756 len = dma_fifo_level(&port->tx_fifo);
757 spin_unlock_bh(&port->lock);
759 port->icount.tx += txn->dma_pended.len;
763 /* TODO: implement retries */
764 spin_lock_bh(&port->lock);
765 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
766 len = dma_fifo_level(&port->tx_fifo);
767 spin_unlock_bh(&port->lock);
769 port->stats.dropped += txn->dma_pended.len;
772 if (len < WAKEUP_CHARS) {
773 tty = tty_port_tty_get(&port->port);
781 static int fwtty_tx(struct fwtty_port *port, bool drain)
783 struct fwtty_peer *peer;
784 struct fwtty_transaction *txn;
785 struct tty_struct *tty;
788 tty = tty_port_tty_get(&port->port);
793 peer = rcu_dereference(port->peer);
799 if (test_and_set_bit(IN_TX, &port->flags)) {
804 /* try to write as many dma transactions out as possible */
806 while (!tty->stopped && !tty->hw_stopped &&
807 !test_bit(STOP_TX, &port->flags)) {
808 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
814 spin_lock_bh(&port->lock);
815 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
816 spin_unlock_bh(&port->lock);
818 fwtty_dbg(port, "out: %u rem: %d", txn->dma_pended.len, n);
821 kmem_cache_free(fwtty_txn_cache, txn);
823 ++port->stats.tx_stall;
824 else if (n == -ENODATA)
825 profile_size_distrib(port->stats.txns, 0);
827 ++port->stats.fifo_errs;
828 fwtty_err_ratelimited(port, "fifo err: %d", n);
833 profile_size_distrib(port->stats.txns, txn->dma_pended.len);
835 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
836 peer->fifo_addr, txn->dma_pended.data,
837 txn->dma_pended.len, fwtty_tx_complete,
842 * Stop tx if the 'last view' of the fifo is empty or if
843 * this is the writer and there's not enough data to bother
845 if (n == 0 || (!drain && n < WRITER_MINIMUM))
849 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
850 spin_lock_bh(&port->lock);
851 len = dma_fifo_out_level(&port->tx_fifo);
853 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
854 schedule_delayed_work(&port->drain, delay);
856 len = dma_fifo_level(&port->tx_fifo);
857 spin_unlock_bh(&port->lock);
859 /* wakeup the writer */
860 if (drain && len < WAKEUP_CHARS)
864 clear_bit(IN_TX, &port->flags);
865 wake_up_interruptible(&port->wait_tx);
873 static void fwtty_drain_tx(struct work_struct *work)
875 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
877 fwtty_tx(port, true);
880 static void fwtty_write_xchar(struct fwtty_port *port, char ch)
882 struct fwtty_peer *peer;
884 ++port->stats.xchars;
886 fwtty_dbg(port, "%02x", ch);
889 peer = rcu_dereference(port->peer);
891 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
892 peer->fifo_addr, &ch, sizeof(ch),
898 struct fwtty_port *fwtty_port_get(unsigned index)
900 struct fwtty_port *port;
902 if (index >= MAX_TOTAL_PORTS)
905 mutex_lock(&port_table_lock);
906 port = port_table[index];
908 kref_get(&port->serial->kref);
909 mutex_unlock(&port_table_lock);
912 EXPORT_SYMBOL(fwtty_port_get);
914 static int fwtty_ports_add(struct fw_serial *serial)
919 if (port_table_corrupt)
922 mutex_lock(&port_table_lock);
923 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
924 if (!port_table[i]) {
925 for (j = 0; j < num_ports; ++i, ++j) {
926 serial->ports[j]->index = i;
927 port_table[i] = serial->ports[j];
933 mutex_unlock(&port_table_lock);
937 static void fwserial_destroy(struct kref *kref)
939 struct fw_serial *serial = to_serial(kref, kref);
940 struct fwtty_port **ports = serial->ports;
941 int j, i = ports[0]->index;
945 mutex_lock(&port_table_lock);
946 for (j = 0; j < num_ports; ++i, ++j) {
947 port_table_corrupt |= port_table[i] != ports[j];
948 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
949 i, port_table[i], j, ports[j]);
951 port_table[i] = NULL;
953 mutex_unlock(&port_table_lock);
955 for (j = 0; j < num_ports; ++j) {
956 fw_core_remove_address_handler(&ports[j]->rx_handler);
957 tty_port_destroy(&ports[j]->port);
963 void fwtty_port_put(struct fwtty_port *port)
965 kref_put(&port->serial->kref, fwserial_destroy);
967 EXPORT_SYMBOL(fwtty_port_put);
969 static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
971 struct fwtty_port *port = to_port(tty_port, port);
973 fwtty_dbg(port, "on/off: %d", on);
975 spin_lock_bh(&port->lock);
976 /* Don't change carrier state if this is a console */
977 if (!port->port.console) {
979 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
981 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
984 __fwtty_write_port_status(port);
985 spin_unlock_bh(&port->lock);
989 * fwtty_port_carrier_raised: required tty_port operation
991 * This port operation is polled after a tty has been opened and is waiting for
992 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
994 static int fwtty_port_carrier_raised(struct tty_port *tty_port)
996 struct fwtty_port *port = to_port(tty_port, port);
999 rc = (port->mstatus & TIOCM_CAR);
1001 fwtty_dbg(port, "%d", rc);
1006 static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
1008 unsigned baud, frame;
1010 baud = tty_termios_baud_rate(&tty->termios);
1011 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
1013 /* compute bit count of 2 frames */
1014 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
1016 switch (C_CSIZE(tty)) {
1018 frame -= (C_CSTOPB(tty)) ? 1 : 0;
1031 port->cps = (baud << 1) / frame;
1033 port->status_mask = UART_LSR_OE;
1034 if (_I_FLAG(tty, BRKINT | PARMRK))
1035 port->status_mask |= UART_LSR_BI;
1037 port->ignore_mask = 0;
1038 if (I_IGNBRK(tty)) {
1039 port->ignore_mask |= UART_LSR_BI;
1041 port->ignore_mask |= UART_LSR_OE;
1044 port->write_only = !C_CREAD(tty);
1046 /* turn off echo and newline xlat if loopback */
1047 if (port->loopback) {
1048 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
1049 ECHONL | ECHOPRT | ECHOCTL);
1050 tty->termios.c_oflag &= ~ONLCR;
1056 static int fwtty_port_activate(struct tty_port *tty_port,
1057 struct tty_struct *tty)
1059 struct fwtty_port *port = to_port(tty_port, port);
1063 set_bit(TTY_IO_ERROR, &tty->flags);
1065 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
1068 FWTTY_PORT_MAX_PEND_DMA,
1073 spin_lock_bh(&port->lock);
1075 baud = set_termios(port, tty);
1077 /* if console, don't change carrier state */
1078 if (!port->port.console) {
1081 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1084 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1085 tty->hw_stopped = 1;
1087 __fwtty_write_port_status(port);
1088 spin_unlock_bh(&port->lock);
1090 clear_bit(TTY_IO_ERROR, &tty->flags);
1096 * fwtty_port_shutdown
1098 * Note: the tty port core ensures this is not the console and
1099 * manages TTY_IO_ERROR properly
1101 static void fwtty_port_shutdown(struct tty_port *tty_port)
1103 struct fwtty_port *port = to_port(tty_port, port);
1104 struct buffered_rx *buf, *next;
1106 /* TODO: cancel outstanding transactions */
1108 cancel_delayed_work_sync(&port->emit_breaks);
1109 cancel_delayed_work_sync(&port->drain);
1110 cancel_work_sync(&port->push);
1112 spin_lock_bh(&port->lock);
1113 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
1114 list_del(&buf->list);
1119 port->break_ctl = 0;
1121 __fwtty_write_port_status(port);
1122 dma_fifo_free(&port->tx_fifo);
1123 spin_unlock_bh(&port->lock);
1126 static int fwtty_open(struct tty_struct *tty, struct file *fp)
1128 struct fwtty_port *port = tty->driver_data;
1130 return tty_port_open(&port->port, tty, fp);
1133 static void fwtty_close(struct tty_struct *tty, struct file *fp)
1135 struct fwtty_port *port = tty->driver_data;
1137 tty_port_close(&port->port, tty, fp);
1140 static void fwtty_hangup(struct tty_struct *tty)
1142 struct fwtty_port *port = tty->driver_data;
1144 tty_port_hangup(&port->port);
1147 static void fwtty_cleanup(struct tty_struct *tty)
1149 struct fwtty_port *port = tty->driver_data;
1151 tty->driver_data = NULL;
1152 fwtty_port_put(port);
1155 static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1157 struct fwtty_port *port = fwtty_port_get(tty->index);
1160 err = tty_standard_install(driver, tty);
1162 tty->driver_data = port;
1164 fwtty_port_put(port);
1168 static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1170 struct fwtty_port *port = tty->driver_data;
1173 fwtty_dbg(port, "%d", c);
1174 profile_size_distrib(port->stats.writes, c);
1176 spin_lock_bh(&port->lock);
1177 n = dma_fifo_in(&port->tx_fifo, buf, c);
1178 len = dma_fifo_out_level(&port->tx_fifo);
1179 if (len < DRAIN_THRESHOLD)
1180 schedule_delayed_work(&port->drain, 1);
1181 spin_unlock_bh(&port->lock);
1183 if (len >= DRAIN_THRESHOLD)
1184 fwtty_tx(port, false);
1186 debug_short_write(port, c, n);
1188 return (n < 0) ? 0 : n;
1191 static int fwtty_write_room(struct tty_struct *tty)
1193 struct fwtty_port *port = tty->driver_data;
1196 spin_lock_bh(&port->lock);
1197 n = dma_fifo_avail(&port->tx_fifo);
1198 spin_unlock_bh(&port->lock);
1200 fwtty_dbg(port, "%d", n);
1205 static int fwtty_chars_in_buffer(struct tty_struct *tty)
1207 struct fwtty_port *port = tty->driver_data;
1210 spin_lock_bh(&port->lock);
1211 n = dma_fifo_level(&port->tx_fifo);
1212 spin_unlock_bh(&port->lock);
1214 fwtty_dbg(port, "%d", n);
1219 static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1221 struct fwtty_port *port = tty->driver_data;
1223 fwtty_dbg(port, "%02x", ch);
1225 fwtty_write_xchar(port, ch);
1228 static void fwtty_throttle(struct tty_struct *tty)
1230 struct fwtty_port *port = tty->driver_data;
1233 * Ignore throttling (but not unthrottling).
1234 * It only makes sense to throttle when data will no longer be
1235 * accepted by the tty flip buffer. For example, it is
1236 * possible for received data to overflow the tty buffer long
1237 * before the line discipline ever has a chance to throttle the driver.
1238 * Additionally, the driver may have already completed the I/O
1239 * but the tty buffer is still emptying, so the line discipline is
1240 * throttling and unthrottling nothing.
1243 ++port->stats.throttled;
1246 static void fwtty_unthrottle(struct tty_struct *tty)
1248 struct fwtty_port *port = tty->driver_data;
1250 fwtty_dbg(port, "CRTSCTS: %d", (C_CRTSCTS(tty) != 0));
1252 profile_fifo_avail(port, port->stats.unthrottle);
1254 schedule_work(&port->push);
1256 spin_lock_bh(&port->lock);
1257 port->mctrl &= ~OOB_RX_THROTTLE;
1259 port->mctrl |= TIOCM_RTS;
1260 __fwtty_write_port_status(port);
1261 spin_unlock_bh(&port->lock);
1264 static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1265 struct async_icount *prev)
1267 struct async_icount now;
1272 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1273 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1274 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1275 (mask & TIOCM_CTS && prev->cts != now.cts));
1282 static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1284 struct async_icount prev;
1286 prev = port->icount;
1288 return wait_event_interruptible(port->port.delta_msr_wait,
1289 check_msr_delta(port, mask, &prev));
1292 static int get_serial_info(struct fwtty_port *port,
1293 struct serial_struct __user *info)
1295 struct serial_struct tmp;
1297 memset(&tmp, 0, sizeof(tmp));
1299 tmp.type = PORT_UNKNOWN;
1300 tmp.line = port->port.tty->index;
1301 tmp.flags = port->port.flags;
1302 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1303 tmp.baud_base = 400000000;
1304 tmp.close_delay = port->port.close_delay;
1306 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1309 static int set_serial_info(struct fwtty_port *port,
1310 struct serial_struct __user *info)
1312 struct serial_struct tmp;
1314 if (copy_from_user(&tmp, info, sizeof(tmp)))
1317 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
1318 tmp.baud_base != 400000000)
1321 if (!capable(CAP_SYS_ADMIN)) {
1322 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1323 (port->port.flags & ~ASYNC_USR_MASK)))
1326 port->port.close_delay = tmp.close_delay * HZ / 100;
1331 static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1334 struct fwtty_port *port = tty->driver_data;
1339 mutex_lock(&port->port.mutex);
1340 err = get_serial_info(port, (void __user *)arg);
1341 mutex_unlock(&port->port.mutex);
1345 mutex_lock(&port->port.mutex);
1346 err = set_serial_info(port, (void __user *)arg);
1347 mutex_unlock(&port->port.mutex);
1351 err = wait_msr_change(port, arg);
1361 static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1363 struct fwtty_port *port = tty->driver_data;
1366 spin_lock_bh(&port->lock);
1367 baud = set_termios(port, tty);
1369 if ((baud == 0) && (old->c_cflag & CBAUD))
1370 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1371 else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1372 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1373 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1375 port->mctrl |= TIOCM_DTR;
1377 __fwtty_write_port_status(port);
1378 spin_unlock_bh(&port->lock);
1380 if (old->c_cflag & CRTSCTS) {
1381 if (!C_CRTSCTS(tty)) {
1382 tty->hw_stopped = 0;
1383 fwtty_restart_tx(port);
1385 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1386 tty->hw_stopped = 1;
1391 * fwtty_break_ctl - start/stop sending breaks
1393 * Signals the remote to start or stop generating simulated breaks.
1394 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1395 * before signalling the break line status. This guarantees any pending rx will
1396 * be queued to the line discipline before break is simulated on the remote.
1397 * Conversely, turning off break_ctl requires signalling the line status change,
1400 static int fwtty_break_ctl(struct tty_struct *tty, int state)
1402 struct fwtty_port *port = tty->driver_data;
1405 fwtty_dbg(port, "%d", state);
1408 set_bit(STOP_TX, &port->flags);
1409 ret = wait_event_interruptible_timeout(port->wait_tx,
1410 !test_bit(IN_TX, &port->flags),
1412 if (ret == 0 || ret == -ERESTARTSYS) {
1413 clear_bit(STOP_TX, &port->flags);
1414 fwtty_restart_tx(port);
1419 spin_lock_bh(&port->lock);
1420 port->break_ctl = (state == -1);
1421 __fwtty_write_port_status(port);
1422 spin_unlock_bh(&port->lock);
1425 spin_lock_bh(&port->lock);
1426 dma_fifo_reset(&port->tx_fifo);
1427 clear_bit(STOP_TX, &port->flags);
1428 spin_unlock_bh(&port->lock);
1433 static int fwtty_tiocmget(struct tty_struct *tty)
1435 struct fwtty_port *port = tty->driver_data;
1438 spin_lock_bh(&port->lock);
1439 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1440 spin_unlock_bh(&port->lock);
1442 fwtty_dbg(port, "%x", tiocm);
1447 static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1449 struct fwtty_port *port = tty->driver_data;
1451 fwtty_dbg(port, "set: %x clear: %x", set, clear);
1453 /* TODO: simulate loopback if TIOCM_LOOP set */
1455 spin_lock_bh(&port->lock);
1456 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1457 port->mctrl |= set & MCTRL_MASK & 0xffff;
1458 __fwtty_write_port_status(port);
1459 spin_unlock_bh(&port->lock);
1463 static int fwtty_get_icount(struct tty_struct *tty,
1464 struct serial_icounter_struct *icount)
1466 struct fwtty_port *port = tty->driver_data;
1469 memcpy(&stats, &port->stats, sizeof(stats));
1470 if (port->port.console)
1471 (*port->fwcon_ops->stats)(&stats, port->con_data);
1473 icount->cts = port->icount.cts;
1474 icount->dsr = port->icount.dsr;
1475 icount->rng = port->icount.rng;
1476 icount->dcd = port->icount.dcd;
1477 icount->rx = port->icount.rx;
1478 icount->tx = port->icount.tx + stats.xchars;
1479 icount->frame = port->icount.frame;
1480 icount->overrun = port->icount.overrun;
1481 icount->parity = port->icount.parity;
1482 icount->brk = port->icount.brk;
1483 icount->buf_overrun = port->icount.overrun;
1487 static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1491 memcpy(&stats, &port->stats, sizeof(stats));
1492 if (port->port.console)
1493 (*port->fwcon_ops->stats)(&stats, port->con_data);
1495 seq_printf(m, " tx:%d rx:%d", port->icount.tx + stats.xchars,
1497 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1498 port->icount.dsr, port->icount.rng, port->icount.dcd);
1499 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1500 port->icount.overrun, port->icount.parity, port->icount.brk);
1501 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1502 stats.tx_stall, stats.fifo_errs, stats.lost);
1503 seq_printf(m, " pkts:%d thr:%d wtrmk:%d", stats.sent, stats.throttled,
1505 seq_printf(m, " addr:%012llx", port->rx_handler.offset);
1507 if (port->port.console) {
1508 seq_printf(m, "\n ");
1509 (*port->fwcon_ops->proc_show)(m, port->con_data);
1512 dump_profile(m, &port->stats);
1515 static void fwtty_proc_show_peer(struct seq_file *m, struct fwtty_peer *peer)
1517 int generation = peer->generation;
1520 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1521 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1522 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1523 peer->max_payload, (unsigned long long) peer->guid);
1525 if (capable(CAP_SYS_ADMIN)) {
1526 seq_printf(m, " mgmt:%012llx",
1527 (unsigned long long) peer->mgmt_addr);
1528 seq_printf(m, " addr:%012llx",
1529 (unsigned long long) peer->status_addr);
1534 static int fwtty_proc_show(struct seq_file *m, void *v)
1536 struct fwtty_port *port;
1537 struct fw_serial *serial;
1538 struct fwtty_peer *peer;
1541 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1542 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1543 seq_printf(m, "%2d:", i);
1544 if (capable(CAP_SYS_ADMIN))
1545 fwtty_proc_show_port(m, port);
1546 fwtty_port_put(port);
1547 seq_printf(m, "\n");
1552 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1553 seq_printf(m, "card: %s guid: %016llx\n",
1554 dev_name(serial->card->device),
1555 (unsigned long long) serial->card->guid);
1556 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1557 fwtty_proc_show_peer(m, peer);
1563 static int fwtty_proc_open(struct inode *inode, struct file *fp)
1565 return single_open(fp, fwtty_proc_show, NULL);
1568 static const struct file_operations fwtty_proc_fops = {
1569 .owner = THIS_MODULE,
1570 .open = fwtty_proc_open,
1572 .llseek = seq_lseek,
1573 .release = single_release,
1576 static const struct tty_port_operations fwtty_port_ops = {
1577 .dtr_rts = fwtty_port_dtr_rts,
1578 .carrier_raised = fwtty_port_carrier_raised,
1579 .shutdown = fwtty_port_shutdown,
1580 .activate = fwtty_port_activate,
1583 static const struct tty_operations fwtty_ops = {
1585 .close = fwtty_close,
1586 .hangup = fwtty_hangup,
1587 .cleanup = fwtty_cleanup,
1588 .install = fwtty_install,
1589 .write = fwtty_write,
1590 .write_room = fwtty_write_room,
1591 .chars_in_buffer = fwtty_chars_in_buffer,
1592 .send_xchar = fwtty_send_xchar,
1593 .throttle = fwtty_throttle,
1594 .unthrottle = fwtty_unthrottle,
1595 .ioctl = fwtty_ioctl,
1596 .set_termios = fwtty_set_termios,
1597 .break_ctl = fwtty_break_ctl,
1598 .tiocmget = fwtty_tiocmget,
1599 .tiocmset = fwtty_tiocmset,
1600 .get_icount = fwtty_get_icount,
1601 .proc_fops = &fwtty_proc_fops,
1604 static inline int mgmt_pkt_expected_len(__be16 code)
1606 static const struct fwserial_mgmt_pkt pkt;
1608 switch (be16_to_cpu(code)) {
1609 case FWSC_VIRT_CABLE_PLUG:
1610 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1612 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1613 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1616 case FWSC_VIRT_CABLE_UNPLUG:
1617 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1618 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1619 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1620 return sizeof(pkt.hdr);
1627 static inline void fill_plug_params(struct virt_plug_params *params,
1628 struct fwtty_port *port)
1630 u64 status_addr = port->rx_handler.offset;
1631 u64 fifo_addr = port->rx_handler.offset + 4;
1632 size_t fifo_len = port->rx_handler.length - 4;
1634 params->status_hi = cpu_to_be32(status_addr >> 32);
1635 params->status_lo = cpu_to_be32(status_addr);
1636 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1637 params->fifo_lo = cpu_to_be32(fifo_addr);
1638 params->fifo_len = cpu_to_be32(fifo_len);
1641 static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1642 struct fwtty_port *port)
1644 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1645 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1646 fill_plug_params(&pkt->plug_req, port);
1649 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1650 struct fwtty_port *port)
1652 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1653 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1654 fill_plug_params(&pkt->plug_rsp, port);
1657 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1659 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1660 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1663 static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1665 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1666 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1669 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1671 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1672 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1675 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1677 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1678 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1681 static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1682 struct virt_plug_params *params)
1684 struct fwtty_port *port = peer->port;
1686 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1687 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1688 peer->fifo_len = be32_to_cpu(params->fifo_len);
1689 peer_set_state(peer, FWPS_ATTACHED);
1691 /* reconfigure tx_fifo optimally for this peer */
1692 spin_lock_bh(&port->lock);
1693 port->max_payload = min(peer->max_payload, peer->fifo_len);
1694 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1695 spin_unlock_bh(&peer->port->lock);
1697 if (port->port.console && port->fwcon_ops->notify != NULL)
1698 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1700 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s",
1701 (unsigned long long)peer->guid, dev_name(port->device));
1704 static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1705 struct fwserial_mgmt_pkt *pkt)
1708 int rcode, tries = 5;
1711 generation = peer->generation;
1714 rcode = fw_run_transaction(peer->serial->card,
1715 TCODE_WRITE_BLOCK_REQUEST,
1717 generation, peer->speed,
1719 pkt, be16_to_cpu(pkt->hdr.len));
1720 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1721 rcode == RCODE_GENERATION) {
1722 fwtty_dbg(&peer->unit, "mgmt write error: %d", rcode);
1726 } while (--tries > 0);
1731 * fwserial_claim_port - attempt to claim port @ index for peer
1733 * Returns ptr to claimed port or error code (as ERR_PTR())
1734 * Can sleep - must be called from process context
1736 static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1739 struct fwtty_port *port;
1741 if (index < 0 || index >= num_ports)
1742 return ERR_PTR(-EINVAL);
1744 /* must guarantee that previous port releases have completed */
1747 port = peer->serial->ports[index];
1748 spin_lock_bh(&port->lock);
1749 if (!rcu_access_pointer(port->peer))
1750 rcu_assign_pointer(port->peer, peer);
1752 port = ERR_PTR(-EBUSY);
1753 spin_unlock_bh(&port->lock);
1759 * fwserial_find_port - find avail port and claim for peer
1761 * Returns ptr to claimed port or NULL if none avail
1762 * Can sleep - must be called from process context
1764 static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1766 struct fwtty_port **ports = peer->serial->ports;
1769 /* must guarantee that previous port releases have completed */
1772 /* TODO: implement optional GUID-to-specific port # matching */
1774 /* find an unattached port (but not the loopback port, if present) */
1775 for (i = 0; i < num_ttys; ++i) {
1776 spin_lock_bh(&ports[i]->lock);
1777 if (!ports[i]->peer) {
1779 rcu_assign_pointer(ports[i]->peer, peer);
1780 spin_unlock_bh(&ports[i]->lock);
1783 spin_unlock_bh(&ports[i]->lock);
1788 static void fwserial_release_port(struct fwtty_port *port, bool reset)
1790 /* drop carrier (and all other line status) */
1792 fwtty_update_port_status(port, 0);
1794 spin_lock_bh(&port->lock);
1796 /* reset dma fifo max transmission size back to S100 */
1797 port->max_payload = link_speed_to_max_payload(SCODE_100);
1798 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1800 rcu_assign_pointer(port->peer, NULL);
1801 spin_unlock_bh(&port->lock);
1803 if (port->port.console && port->fwcon_ops->notify != NULL)
1804 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1807 static void fwserial_plug_timeout(unsigned long data)
1809 struct fwtty_peer *peer = (struct fwtty_peer *) data;
1810 struct fwtty_port *port;
1812 spin_lock_bh(&peer->lock);
1813 if (peer->state != FWPS_PLUG_PENDING) {
1814 spin_unlock_bh(&peer->lock);
1818 port = peer_revert_state(peer);
1819 spin_unlock_bh(&peer->lock);
1822 fwserial_release_port(port, false);
1826 * fwserial_connect_peer - initiate virtual cable with peer
1828 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1829 * otherwise error code. Must be called from process context.
1831 static int fwserial_connect_peer(struct fwtty_peer *peer)
1833 struct fwtty_port *port;
1834 struct fwserial_mgmt_pkt *pkt;
1837 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1841 port = fwserial_find_port(peer);
1843 fwtty_err(&peer->unit, "avail ports in use");
1848 spin_lock_bh(&peer->lock);
1850 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1851 if (peer->state != FWPS_NOT_ATTACHED) {
1857 peer_set_state(peer, FWPS_PLUG_PENDING);
1859 fill_plug_req(pkt, peer->port);
1861 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1862 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1863 spin_unlock_bh(&peer->lock);
1865 rcode = fwserial_send_mgmt_sync(peer, pkt);
1867 spin_lock_bh(&peer->lock);
1868 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1869 if (rcode == RCODE_CONFLICT_ERROR)
1875 spin_unlock_bh(&peer->lock);
1881 del_timer(&peer->timer);
1882 peer_revert_state(peer);
1884 spin_unlock_bh(&peer->lock);
1885 fwserial_release_port(port, false);
1892 * fwserial_close_port -
1893 * HUP the tty (if the tty exists) and unregister the tty device.
1894 * Only used by the unit driver upon unit removal to disconnect and
1895 * cleanup all attached ports
1897 * The port reference is put by fwtty_cleanup (if a reference was
1900 static void fwserial_close_port(struct fwtty_port *port)
1902 struct tty_struct *tty;
1904 mutex_lock(&port->port.mutex);
1905 tty = tty_port_tty_get(&port->port);
1910 mutex_unlock(&port->port.mutex);
1912 tty_unregister_device(fwtty_driver, port->index);
1916 * fwserial_lookup - finds first fw_serial associated with card
1917 * @card: fw_card to match
1919 * NB: caller must be holding fwserial_list_mutex
1921 static struct fw_serial *fwserial_lookup(struct fw_card *card)
1923 struct fw_serial *serial;
1925 list_for_each_entry(serial, &fwserial_list, list) {
1926 if (card == serial->card)
1934 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1935 * @card: fw_card to match
1937 * NB: caller must be inside rcu_read_lock() section
1939 static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
1941 struct fw_serial *serial;
1943 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1944 if (card == serial->card)
1952 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1954 * If a matching peer could not be found for the specified generation/node id,
1955 * this could be because:
1956 * a) the generation has changed and one of the nodes hasn't updated yet
1957 * b) the remote node has created its remote unit device before this
1958 * local node has created its corresponding remote unit device
1959 * In either case, the remote node should retry
1961 * Note: caller must be in rcu_read_lock() section
1963 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
1964 int generation, int id)
1966 struct fw_serial *serial;
1967 struct fwtty_peer *peer;
1969 serial = __fwserial_lookup_rcu(card);
1972 * Something is very wrong - there should be a matching
1973 * fw_serial structure for every fw_card. Maybe the remote node
1974 * has created its remote unit device before this driver has
1975 * been probed for any unit devices...
1977 fwtty_err(card, "unknown card (guid %016llx)",
1978 (unsigned long long) card->guid);
1982 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1983 int g = peer->generation;
1985 if (generation == g && id == peer->node_id)
1993 static void __dump_peer_list(struct fw_card *card)
1995 struct fw_serial *serial;
1996 struct fwtty_peer *peer;
1998 serial = __fwserial_lookup_rcu(card);
2002 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2003 int g = peer->generation;
2005 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n", g,
2006 peer->node_id, (unsigned long long) peer->guid);
2010 #define __dump_peer_list(s)
2013 static void fwserial_auto_connect(struct work_struct *work)
2015 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2018 err = fwserial_connect_peer(peer);
2019 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2020 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2024 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2025 * @serial: aggregate representing the specific fw_card to add the peer to
2026 * @unit: 'peer' to create and add to peer_list of serial
2028 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2029 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2030 * available tty port. This function is called either directly or indirectly
2031 * as a result of a 'serial' unit device being created & probed.
2033 * Note: this function is serialized with fwserial_remove_peer() by the
2034 * fwserial_list_mutex held in fwserial_probe().
2036 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2037 * via the dev_set_drvdata() for the device of the fw_unit.
2039 static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2041 struct device *dev = &unit->device;
2042 struct fw_device *parent = fw_parent_device(unit);
2043 struct fwtty_peer *peer;
2044 struct fw_csr_iterator ci;
2048 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2052 peer_set_state(peer, FWPS_NOT_ATTACHED);
2054 dev_set_drvdata(dev, peer);
2056 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2057 peer->speed = parent->max_speed;
2058 peer->max_payload = min(device_max_receive(parent),
2059 link_speed_to_max_payload(peer->speed));
2061 generation = parent->generation;
2063 peer->node_id = parent->node_id;
2065 peer->generation = generation;
2067 /* retrieve the mgmt bus addr from the unit directory */
2068 fw_csr_iterator_init(&ci, unit->directory);
2069 while (fw_csr_iterator_next(&ci, &key, &val)) {
2070 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2071 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2075 if (peer->mgmt_addr == 0ULL) {
2077 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2078 * this peer will not be able to attach to a remote
2080 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2083 spin_lock_init(&peer->lock);
2086 init_timer(&peer->timer);
2087 INIT_WORK(&peer->work, NULL);
2088 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2090 /* associate peer with specific fw_card */
2091 peer->serial = serial;
2092 list_add_rcu(&peer->list, &serial->peer_list);
2094 fwtty_info(&peer->unit, "peer added (guid:%016llx)",
2095 (unsigned long long)peer->guid);
2097 /* identify the local unit & virt cable to loopback port */
2098 if (parent->is_local) {
2099 serial->self = peer;
2100 if (create_loop_dev) {
2101 struct fwtty_port *port;
2102 port = fwserial_claim_port(peer, num_ttys);
2103 if (!IS_ERR(port)) {
2104 struct virt_plug_params params;
2106 spin_lock_bh(&peer->lock);
2108 fill_plug_params(¶ms, port);
2109 fwserial_virt_plug_complete(peer, ¶ms);
2110 spin_unlock_bh(&peer->lock);
2112 fwtty_write_port_status(port);
2116 } else if (auto_connect) {
2117 /* auto-attach to remote units only (if policy allows) */
2118 schedule_delayed_work(&peer->connect, 1);
2125 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2127 * Remove a 'peer' from its list of peers. This function is only
2128 * called by fwserial_remove() on bus removal of the unit device.
2130 * Note: this function is serialized with fwserial_add_peer() by the
2131 * fwserial_list_mutex held in fwserial_remove().
2133 static void fwserial_remove_peer(struct fwtty_peer *peer)
2135 struct fwtty_port *port;
2137 spin_lock_bh(&peer->lock);
2138 peer_set_state(peer, FWPS_GONE);
2139 spin_unlock_bh(&peer->lock);
2141 cancel_delayed_work_sync(&peer->connect);
2142 cancel_work_sync(&peer->work);
2144 spin_lock_bh(&peer->lock);
2145 /* if this unit is the local unit, clear link */
2146 if (peer == peer->serial->self)
2147 peer->serial->self = NULL;
2149 /* cancel the request timeout timer (if running) */
2150 del_timer(&peer->timer);
2155 list_del_rcu(&peer->list);
2157 fwtty_info(&peer->unit, "peer removed (guid:%016llx)",
2158 (unsigned long long)peer->guid);
2160 spin_unlock_bh(&peer->lock);
2163 fwserial_release_port(port, true);
2170 * create_loop_device - create a loopback tty device
2171 * @tty_driver: tty_driver to own loopback device
2172 * @prototype: ptr to already-assigned 'prototype' tty port
2173 * @index: index to associate this device with the tty port
2174 * @parent: device to child to
2176 * HACK - this is basically tty_port_register_device() with an
2177 * alternate naming scheme. Suggest tty_port_register_named_device()
2180 * Creates a loopback tty device named 'fwloop<n>' which is attached to
2181 * the local unit in fwserial_add_peer(). Note that <n> in the device
2182 * name advances in increments of port allocation blocks, ie., for port
2183 * indices 0..3, the device name will be 'fwloop0'; for 4..7, 'fwloop1',
2186 * Only one loopback device should be created per fw_card.
2188 static void release_loop_device(struct device *dev)
2193 static struct device *create_loop_device(struct tty_driver *driver,
2194 struct fwtty_port *prototype,
2195 struct fwtty_port *port,
2196 struct device *parent)
2199 int index = port->index;
2200 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
2201 struct device *dev = NULL;
2204 if (index >= fwtty_driver->num)
2205 return ERR_PTR(-EINVAL);
2207 snprintf(name, 64, "%s%d", loop_dev_name, index / num_ports);
2209 tty_port_link_device(&port->port, driver, index);
2211 cdev_init(&driver->cdevs[index], driver->cdevs[prototype->index].ops);
2212 driver->cdevs[index].owner = driver->owner;
2213 err = cdev_add(&driver->cdevs[index], devt, 1);
2215 return ERR_PTR(err);
2217 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2219 cdev_del(&driver->cdevs[index]);
2220 return ERR_PTR(-ENOMEM);
2224 dev->class = prototype->device->class;
2225 dev->parent = parent;
2226 dev->release = release_loop_device;
2227 dev_set_name(dev, "%s", name);
2229 dev_set_drvdata(dev, NULL);
2231 err = device_register(dev);
2234 cdev_del(&driver->cdevs[index]);
2235 return ERR_PTR(err);
2242 * fwserial_create - init everything to create TTYs for a specific fw_card
2243 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2245 * This function inits the aggregate structure (an fw_serial instance)
2246 * used to manage the TTY ports registered by a specific fw_card. Also, the
2247 * unit device is added as the first 'peer'.
2249 * This unit device may represent a local unit device (as specified by the
2250 * config ROM unit directory) or it may represent a remote unit device
2251 * (as specified by the reading of the remote node's config ROM).
2253 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2254 * value to indicate which error.
2256 static int fwserial_create(struct fw_unit *unit)
2258 struct fw_device *parent = fw_parent_device(unit);
2259 struct fw_card *card = parent->card;
2260 struct fw_serial *serial;
2261 struct fwtty_port *port;
2262 struct device *tty_dev;
2266 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2270 kref_init(&serial->kref);
2271 serial->card = card;
2272 INIT_LIST_HEAD(&serial->peer_list);
2274 for (i = 0; i < num_ports; ++i) {
2275 port = kzalloc(sizeof(*port), GFP_KERNEL);
2280 tty_port_init(&port->port);
2281 port->index = FWTTY_INVALID_INDEX;
2282 port->port.ops = &fwtty_port_ops;
2283 port->serial = serial;
2285 spin_lock_init(&port->lock);
2286 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2287 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2288 INIT_WORK(&port->hangup, fwtty_do_hangup);
2289 INIT_WORK(&port->push, fwtty_pushrx);
2290 INIT_LIST_HEAD(&port->buf_list);
2291 init_waitqueue_head(&port->wait_tx);
2292 port->max_payload = link_speed_to_max_payload(SCODE_100);
2293 dma_fifo_init(&port->tx_fifo);
2295 rcu_assign_pointer(port->peer, NULL);
2296 serial->ports[i] = port;
2298 /* get unique bus addr region for port's status & recv fifo */
2299 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2300 port->rx_handler.address_callback = fwtty_port_handler;
2301 port->rx_handler.callback_data = port;
2303 * XXX: use custom memory region above cpu physical memory addrs
2304 * this will ease porting to 64-bit firewire adapters
2306 err = fw_core_add_address_handler(&port->rx_handler,
2307 &fw_high_memory_region);
2313 /* preserve i for error cleanup */
2315 err = fwtty_ports_add(serial);
2317 fwtty_err(&unit, "no space in port table");
2321 for (j = 0; j < num_ttys; ++j) {
2322 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2324 serial->ports[j]->index,
2326 if (IS_ERR(tty_dev)) {
2327 err = PTR_ERR(tty_dev);
2328 fwtty_err(&unit, "register tty device error (%d)", err);
2329 goto unregister_ttys;
2332 serial->ports[j]->device = tty_dev;
2334 /* preserve j for error cleanup */
2336 if (create_loop_dev) {
2337 struct device *loop_dev;
2339 loop_dev = create_loop_device(fwtty_driver,
2341 serial->ports[num_ttys],
2343 if (IS_ERR(loop_dev)) {
2344 err = PTR_ERR(loop_dev);
2345 fwtty_err(&unit, "create loop device failed (%d)", err);
2346 goto unregister_ttys;
2348 serial->ports[num_ttys]->device = loop_dev;
2349 serial->ports[num_ttys]->loopback = true;
2352 list_add_rcu(&serial->list, &fwserial_list);
2354 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)",
2355 dev_name(card->device), (unsigned long long) card->guid);
2357 err = fwserial_add_peer(serial, unit);
2361 fwtty_err(&unit, "unable to add peer unit device (%d)", err);
2363 /* fall-through to error processing */
2364 list_del_rcu(&serial->list);
2366 for (--j; j >= 0; --j)
2367 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2368 kref_put(&serial->kref, fwserial_destroy);
2372 for (--i; i >= 0; --i) {
2373 tty_port_destroy(&serial->ports[i]->port);
2374 kfree(serial->ports[i]);
2381 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2383 * A 'serial' unit device is created and probed as a result of:
2384 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2385 * 'serial' unit specifier id
2386 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2388 * The firewire core registers unit devices by enumerating unit directories
2389 * of a node's config ROM after reading the config ROM when a new node is
2390 * added to the bus topology after a bus reset.
2392 * The practical implications of this are:
2393 * - this probe is called for both local and remote nodes that have a 'serial'
2394 * unit directory in their config ROM (that matches the specifiers in
2395 * fwserial_id_table).
2396 * - no specific order is enforced for local vs. remote unit devices
2398 * This unit driver copes with the lack of specific order in the same way the
2399 * firewire net driver does -- each probe, for either a local or remote unit
2400 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2401 * first peer created for a given fw_card (tracked by the global fwserial_list)
2402 * creates the underlying TTYs (aggregated in a fw_serial instance).
2404 * NB: an early attempt to differentiate local & remote unit devices by creating
2405 * peers only for remote units and fw_serial instances (with their
2406 * associated TTY devices) only for local units was discarded. Managing
2407 * the peer lifetimes on device removal proved too complicated.
2409 * fwserial_probe/fwserial_remove are effectively serialized by the
2410 * fwserial_list_mutex. This is necessary because the addition of the first peer
2411 * for a given fw_card will trigger the creation of the fw_serial for that
2412 * fw_card, which must not simultaneously contend with the removal of the
2413 * last peer for a given fw_card triggering the destruction of the same
2414 * fw_serial for the same fw_card.
2416 static int fwserial_probe(struct device *dev)
2418 struct fw_unit *unit = fw_unit(dev);
2419 struct fw_serial *serial;
2422 mutex_lock(&fwserial_list_mutex);
2423 serial = fwserial_lookup(fw_parent_device(unit)->card);
2425 err = fwserial_create(unit);
2427 err = fwserial_add_peer(serial, unit);
2428 mutex_unlock(&fwserial_list_mutex);
2433 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2435 * The corresponding 'peer' for this unit device is removed from the list of
2436 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2437 * specific fw_card). If this is the last peer being removed, then trigger
2438 * the destruction of the underlying TTYs.
2440 static int fwserial_remove(struct device *dev)
2442 struct fwtty_peer *peer = dev_get_drvdata(dev);
2443 struct fw_serial *serial = peer->serial;
2446 mutex_lock(&fwserial_list_mutex);
2447 fwserial_remove_peer(peer);
2449 if (list_empty(&serial->peer_list)) {
2450 /* unlink from the fwserial_list here */
2451 list_del_rcu(&serial->list);
2453 for (i = 0; i < num_ports; ++i)
2454 fwserial_close_port(serial->ports[i]);
2455 kref_put(&serial->kref, fwserial_destroy);
2457 mutex_unlock(&fwserial_list_mutex);
2463 * fwserial_update: bus update function for 'firewire' serial unit devices
2465 * Updates the new node_id and bus generation for this peer. Note that locking
2466 * is unnecessary; but careful memory barrier usage is important to enforce the
2467 * load and store order of generation & node_id.
2469 * The fw-core orders the write of node_id before generation in the parent
2470 * fw_device to ensure that a stale node_id cannot be used with a current
2471 * bus generation. So the generation value must be read before the node_id.
2473 * In turn, this orders the write of node_id before generation in the peer to
2474 * also ensure a stale node_id cannot be used with a current bus generation.
2476 static void fwserial_update(struct fw_unit *unit)
2478 struct fw_device *parent = fw_parent_device(unit);
2479 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2482 generation = parent->generation;
2484 peer->node_id = parent->node_id;
2486 peer->generation = generation;
2489 static const struct ieee1394_device_id fwserial_id_table[] = {
2491 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2492 IEEE1394_MATCH_VERSION,
2493 .specifier_id = LINUX_VENDOR_ID,
2494 .version = FWSERIAL_VERSION,
2499 static struct fw_driver fwserial_driver = {
2501 .owner = THIS_MODULE,
2502 .name = KBUILD_MODNAME,
2503 .bus = &fw_bus_type,
2504 .probe = fwserial_probe,
2505 .remove = fwserial_remove,
2507 .update = fwserial_update,
2508 .id_table = fwserial_id_table,
2511 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2512 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2513 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2514 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2515 /* XXX: config ROM definitons could be improved with semi-automated offset
2516 * and length calculation
2518 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2520 struct fwserial_unit_directory_data {
2524 u32 unit_sw_version;
2525 u32 unit_addr_offset;
2532 static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
2534 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2535 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2536 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
2539 0x00000000, /* type = text */
2540 0x00000000, /* enc = ASCII, lang EN */
2541 0x4c696e75, /* 'Linux TTY' */
2547 static struct fw_descriptor fwserial_unit_directory = {
2548 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2549 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2550 .data = (u32 *)&fwserial_unit_directory_data,
2554 * The management address is in the unit space region but above other known
2555 * address users (to keep wild writes from causing havoc)
2557 static const struct fw_address_region fwserial_mgmt_addr_region = {
2558 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2559 .end = 0x1000000000000ULL,
2562 static struct fw_address_handler fwserial_mgmt_addr_handler;
2565 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2566 * @work: ptr to peer->work
2568 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2570 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2571 * already sent to this peer. If so, the collision is resolved by comparing
2572 * guid values; the loser sends the plug response.
2574 * Note: if an error prevents a response, don't do anything -- the
2575 * remote will timeout its request.
2577 static void fwserial_handle_plug_req(struct work_struct *work)
2579 struct fwtty_peer *peer = to_peer(work, work);
2580 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2581 struct fwtty_port *port;
2582 struct fwserial_mgmt_pkt *pkt;
2585 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2589 port = fwserial_find_port(peer);
2591 spin_lock_bh(&peer->lock);
2593 switch (peer->state) {
2594 case FWPS_NOT_ATTACHED:
2596 fwtty_err(&peer->unit, "no more ports avail");
2597 fill_plug_rsp_nack(pkt);
2600 fill_plug_rsp_ok(pkt, peer->port);
2601 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2602 /* don't release claimed port */
2607 case FWPS_PLUG_PENDING:
2608 if (peer->serial->card->guid > peer->guid)
2611 /* We lost - hijack the already-claimed port and send ok */
2612 del_timer(&peer->timer);
2613 fill_plug_rsp_ok(pkt, peer->port);
2614 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2618 fill_plug_rsp_nack(pkt);
2621 spin_unlock_bh(&peer->lock);
2623 fwserial_release_port(port, false);
2625 rcode = fwserial_send_mgmt_sync(peer, pkt);
2627 spin_lock_bh(&peer->lock);
2628 if (peer->state == FWPS_PLUG_RESPONDING) {
2629 if (rcode == RCODE_COMPLETE) {
2630 struct fwtty_port *tmp = peer->port;
2632 fwserial_virt_plug_complete(peer, plug_req);
2633 spin_unlock_bh(&peer->lock);
2635 fwtty_write_port_status(tmp);
2636 spin_lock_bh(&peer->lock);
2638 fwtty_err(&peer->unit, "PLUG_RSP error (%d)", rcode);
2639 port = peer_revert_state(peer);
2643 spin_unlock_bh(&peer->lock);
2645 fwserial_release_port(port, false);
2650 static void fwserial_handle_unplug_req(struct work_struct *work)
2652 struct fwtty_peer *peer = to_peer(work, work);
2653 struct fwtty_port *port = NULL;
2654 struct fwserial_mgmt_pkt *pkt;
2657 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2661 spin_lock_bh(&peer->lock);
2663 switch (peer->state) {
2665 fill_unplug_rsp_ok(pkt);
2666 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2669 case FWPS_UNPLUG_PENDING:
2670 if (peer->serial->card->guid > peer->guid)
2673 /* We lost - send unplug rsp */
2674 del_timer(&peer->timer);
2675 fill_unplug_rsp_ok(pkt);
2676 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2680 fill_unplug_rsp_nack(pkt);
2683 spin_unlock_bh(&peer->lock);
2685 rcode = fwserial_send_mgmt_sync(peer, pkt);
2687 spin_lock_bh(&peer->lock);
2688 if (peer->state == FWPS_UNPLUG_RESPONDING) {
2689 if (rcode != RCODE_COMPLETE)
2690 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)", rcode);
2691 port = peer_revert_state(peer);
2694 spin_unlock_bh(&peer->lock);
2696 fwserial_release_port(port, true);
2701 static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2702 struct fwserial_mgmt_pkt *pkt,
2703 unsigned long long addr,
2706 struct fwtty_port *port = NULL;
2710 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2711 return RCODE_ADDRESS_ERROR;
2713 if (len != be16_to_cpu(pkt->hdr.len) ||
2714 len != mgmt_pkt_expected_len(pkt->hdr.code))
2715 return RCODE_DATA_ERROR;
2717 spin_lock_bh(&peer->lock);
2718 if (peer->state == FWPS_GONE) {
2720 * This should never happen - it would mean that the
2721 * remote unit that just wrote this transaction was
2722 * already removed from the bus -- and the removal was
2723 * processed before we rec'd this transaction
2725 fwtty_err(&peer->unit, "peer already removed");
2726 spin_unlock_bh(&peer->lock);
2727 return RCODE_ADDRESS_ERROR;
2730 rcode = RCODE_COMPLETE;
2732 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx", pkt->hdr.code);
2734 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2735 case FWSC_VIRT_CABLE_PLUG:
2736 if (work_pending(&peer->work)) {
2737 fwtty_err(&peer->unit, "plug req: busy");
2738 rcode = RCODE_CONFLICT_ERROR;
2741 peer->work_params.plug_req = pkt->plug_req;
2742 PREPARE_WORK(&peer->work, fwserial_handle_plug_req);
2743 queue_work(system_unbound_wq, &peer->work);
2747 case FWSC_VIRT_CABLE_PLUG_RSP:
2748 if (peer->state != FWPS_PLUG_PENDING) {
2749 rcode = RCODE_CONFLICT_ERROR;
2751 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2752 fwtty_notice(&peer->unit, "NACK plug rsp");
2753 port = peer_revert_state(peer);
2756 struct fwtty_port *tmp = peer->port;
2758 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2759 spin_unlock_bh(&peer->lock);
2761 fwtty_write_port_status(tmp);
2762 spin_lock_bh(&peer->lock);
2766 case FWSC_VIRT_CABLE_UNPLUG:
2767 if (work_pending(&peer->work)) {
2768 fwtty_err(&peer->unit, "unplug req: busy");
2769 rcode = RCODE_CONFLICT_ERROR;
2771 PREPARE_WORK(&peer->work, fwserial_handle_unplug_req);
2772 queue_work(system_unbound_wq, &peer->work);
2776 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2777 if (peer->state != FWPS_UNPLUG_PENDING)
2778 rcode = RCODE_CONFLICT_ERROR;
2780 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2781 fwtty_notice(&peer->unit, "NACK unplug?");
2782 port = peer_revert_state(peer);
2788 fwtty_err(&peer->unit, "unknown mgmt code %d",
2789 be16_to_cpu(pkt->hdr.code));
2790 rcode = RCODE_DATA_ERROR;
2792 spin_unlock_bh(&peer->lock);
2795 fwserial_release_port(port, reset);
2801 * fwserial_mgmt_handler: bus address handler for mgmt requests
2802 * @parameters: fw_address_callback_t as specified by firewire core interface
2804 * This handler is responsible for handling virtual cable requests from remotes
2807 static void fwserial_mgmt_handler(struct fw_card *card,
2808 struct fw_request *request,
2809 int tcode, int destination, int source,
2811 unsigned long long addr,
2812 void *data, size_t len,
2813 void *callback_data)
2815 struct fwserial_mgmt_pkt *pkt = data;
2816 struct fwtty_peer *peer;
2820 peer = __fwserial_peer_by_node_id(card, generation, source);
2822 fwtty_dbg(card, "peer(%d:%x) not found", generation, source);
2823 __dump_peer_list(card);
2824 rcode = RCODE_CONFLICT_ERROR;
2828 case TCODE_WRITE_BLOCK_REQUEST:
2829 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2833 rcode = RCODE_TYPE_ERROR;
2838 fw_send_response(card, request, rcode);
2841 static int __init fwserial_init(void)
2843 int err, num_loops = !!(create_loop_dev);
2845 /* num_ttys/num_ports must not be set above the static alloc avail */
2846 if (num_ttys + num_loops > MAX_CARD_PORTS)
2847 num_ttys = MAX_CARD_PORTS - num_loops;
2848 num_ports = num_ttys + num_loops;
2850 fwtty_driver = alloc_tty_driver(MAX_TOTAL_PORTS);
2851 if (!fwtty_driver) {
2856 fwtty_driver->driver_name = KBUILD_MODNAME;
2857 fwtty_driver->name = tty_dev_name;
2858 fwtty_driver->major = 0;
2859 fwtty_driver->minor_start = 0;
2860 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2861 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
2862 fwtty_driver->flags = TTY_DRIVER_REAL_RAW |
2863 TTY_DRIVER_DYNAMIC_DEV;
2865 fwtty_driver->init_termios = tty_std_termios;
2866 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2867 tty_set_operations(fwtty_driver, &fwtty_ops);
2869 err = tty_register_driver(fwtty_driver);
2871 driver_err("register tty driver failed (%d)", err);
2875 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2876 sizeof(struct fwtty_transaction),
2877 0, 0, fwtty_txn_constructor);
2878 if (!fwtty_txn_cache) {
2880 goto unregister_driver;
2884 * Ideally, this address handler would be registered per local node
2885 * (rather than the same handler for all local nodes). However,
2886 * since the firewire core requires the config rom descriptor *before*
2887 * the local unit device(s) are created, a single management handler
2888 * must suffice for all local serial units.
2890 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2891 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2893 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2894 &fwserial_mgmt_addr_region);
2896 driver_err("add management handler failed (%d)", err);
2900 fwserial_unit_directory_data.unit_addr_offset =
2901 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2902 err = fw_core_add_descriptor(&fwserial_unit_directory);
2904 driver_err("add unit descriptor failed (%d)", err);
2905 goto remove_handler;
2908 err = driver_register(&fwserial_driver.driver);
2910 driver_err("register fwserial driver failed (%d)", err);
2911 goto remove_descriptor;
2917 fw_core_remove_descriptor(&fwserial_unit_directory);
2919 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2921 kmem_cache_destroy(fwtty_txn_cache);
2923 tty_unregister_driver(fwtty_driver);
2925 put_tty_driver(fwtty_driver);
2929 static void __exit fwserial_exit(void)
2931 driver_unregister(&fwserial_driver.driver);
2932 fw_core_remove_descriptor(&fwserial_unit_directory);
2933 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2934 kmem_cache_destroy(fwtty_txn_cache);
2935 tty_unregister_driver(fwtty_driver);
2936 put_tty_driver(fwtty_driver);
2939 module_init(fwserial_init);
2940 module_exit(fwserial_exit);
2942 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2943 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2944 MODULE_LICENSE("GPL");
2945 MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
2946 MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
2947 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2948 MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");