of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / staging / fwserial / fwserial.c
blobb3ea4bb54e2c7a80f25f86f5e01008712d22142a
1 /*
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/device.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/rculist.h>
24 #include <linux/workqueue.h>
25 #include <linux/ratelimit.h>
26 #include <linux/bug.h>
27 #include <linux/uaccess.h>
29 #include "fwserial.h"
31 #define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
33 #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
34 #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
36 /* configurable options */
37 static int num_ttys = 4; /* # of std ttys to create per fw_card */
38 /* - doubles as loopback port index */
39 static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
40 static bool create_loop_dev = true; /* create a loopback device for each card */
42 module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
43 module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
44 module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
47 * Threshold below which the tty is woken for writing
48 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
49 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
50 * our fifo is below this level
52 #define WAKEUP_CHARS 256
54 /**
55 * fwserial_list: list of every fw_serial created for each fw_card
56 * See discussion in fwserial_probe.
58 static LIST_HEAD(fwserial_list);
59 static DEFINE_MUTEX(fwserial_list_mutex);
61 /**
62 * port_table: array of tty ports allocated to each fw_card
64 * tty ports are allocated during probe when an fw_serial is first
65 * created for a given fw_card. Ports are allocated in a contiguous block,
66 * each block consisting of 'num_ports' ports.
68 static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
69 static DEFINE_MUTEX(port_table_lock);
70 static bool port_table_corrupt;
71 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
73 #define loop_idx(port) (((port)->index) / num_ports)
74 #define table_idx(loop) ((loop) * num_ports + num_ttys)
76 /* total # of tty ports created per fw_card */
77 static int num_ports;
79 /* slab used as pool for struct fwtty_transactions */
80 static struct kmem_cache *fwtty_txn_cache;
82 struct tty_driver *fwtty_driver;
83 static struct tty_driver *fwloop_driver;
85 static struct dentry *fwserial_debugfs;
87 struct fwtty_transaction;
88 typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
89 void *data, size_t length,
90 struct fwtty_transaction *txn);
92 struct fwtty_transaction {
93 struct fw_transaction fw_txn;
94 fwtty_transaction_cb callback;
95 struct fwtty_port *port;
96 union {
97 struct dma_pending dma_pended;
101 #define to_device(a, b) (a->b)
102 #define fwtty_err(p, fmt, ...) \
103 dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
104 #define fwtty_info(p, fmt, ...) \
105 dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
106 #define fwtty_notice(p, fmt, ...) \
107 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
108 #define fwtty_dbg(p, fmt, ...) \
109 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
110 #define fwtty_err_ratelimited(p, fmt, ...) \
111 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
113 #ifdef DEBUG
114 static inline void debug_short_write(struct fwtty_port *port, int c, int n)
116 int avail;
118 if (n < c) {
119 spin_lock_bh(&port->lock);
120 avail = dma_fifo_avail(&port->tx_fifo);
121 spin_unlock_bh(&port->lock);
122 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d\n",
123 avail, c, n);
126 #else
127 #define debug_short_write(port, c, n)
128 #endif
130 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
131 int generation, int id);
133 #ifdef FWTTY_PROFILING
135 static void fwtty_profile_fifo(struct fwtty_port *port, unsigned *stat)
137 spin_lock_bh(&port->lock);
138 fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo));
139 spin_unlock_bh(&port->lock);
142 static void fwtty_dump_profile(struct seq_file *m, struct stats *stats)
144 /* for each stat, print sum of 0 to 2^k, then individually */
145 int k = 4;
146 unsigned sum;
147 int j;
148 char t[10];
150 snprintf(t, 10, "< %d", 1 << k);
151 seq_printf(m, "\n%14s %6s", " ", t);
152 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
153 seq_printf(m, "%6d", 1 << j);
155 ++k;
156 for (j = 0, sum = 0; j <= k; ++j)
157 sum += stats->reads[j];
158 seq_printf(m, "\n%14s: %6d", "reads", sum);
159 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
160 seq_printf(m, "%6d", stats->reads[j]);
162 for (j = 0, sum = 0; j <= k; ++j)
163 sum += stats->writes[j];
164 seq_printf(m, "\n%14s: %6d", "writes", sum);
165 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
166 seq_printf(m, "%6d", stats->writes[j]);
168 for (j = 0, sum = 0; j <= k; ++j)
169 sum += stats->txns[j];
170 seq_printf(m, "\n%14s: %6d", "txns", sum);
171 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
172 seq_printf(m, "%6d", stats->txns[j]);
174 for (j = 0, sum = 0; j <= k; ++j)
175 sum += stats->unthrottle[j];
176 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
177 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
178 seq_printf(m, "%6d", stats->unthrottle[j]);
181 #else
182 #define fwtty_profile_fifo(port, stat)
183 #define fwtty_dump_profile(m, stats)
184 #endif
187 * Returns the max receive packet size for the given node
188 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
189 * are required by specification to support max_rec of 8 (512 bytes) or more.
191 static inline int device_max_receive(struct fw_device *fw_device)
193 /* see IEEE 1394-2008 table 8-8 */
194 return min(2 << fw_device->max_rec, 4096);
197 static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
199 switch (rcode) {
200 case RCODE_SEND_ERROR:
201 fwtty_err_ratelimited(port, "card busy\n");
202 break;
203 case RCODE_ADDRESS_ERROR:
204 fwtty_err_ratelimited(port, "bad unit addr or write length\n");
205 break;
206 case RCODE_DATA_ERROR:
207 fwtty_err_ratelimited(port, "failed rx\n");
208 break;
209 case RCODE_NO_ACK:
210 fwtty_err_ratelimited(port, "missing ack\n");
211 break;
212 case RCODE_BUSY:
213 fwtty_err_ratelimited(port, "remote busy\n");
214 break;
215 default:
216 fwtty_err_ratelimited(port, "failed tx: %d\n", rcode);
220 static void fwtty_txn_constructor(void *this)
222 struct fwtty_transaction *txn = this;
224 init_timer(&txn->fw_txn.split_timeout_timer);
227 static void fwtty_common_callback(struct fw_card *card, int rcode,
228 void *payload, size_t len, void *cb_data)
230 struct fwtty_transaction *txn = cb_data;
231 struct fwtty_port *port = txn->port;
233 if (port && rcode != RCODE_COMPLETE)
234 fwtty_log_tx_error(port, rcode);
235 if (txn->callback)
236 txn->callback(card, rcode, payload, len, txn);
237 kmem_cache_free(fwtty_txn_cache, txn);
240 static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
241 unsigned long long addr, void *payload,
242 size_t len, fwtty_transaction_cb callback,
243 struct fwtty_port *port)
245 struct fwtty_transaction *txn;
246 int generation;
248 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
249 if (!txn)
250 return -ENOMEM;
252 txn->callback = callback;
253 txn->port = port;
255 generation = peer->generation;
256 smp_rmb();
257 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
258 peer->node_id, generation, peer->speed, addr, payload,
259 len, fwtty_common_callback, txn);
260 return 0;
263 static void fwtty_send_txn_async(struct fwtty_peer *peer,
264 struct fwtty_transaction *txn, int tcode,
265 unsigned long long addr, void *payload,
266 size_t len, fwtty_transaction_cb callback,
267 struct fwtty_port *port)
269 int generation;
271 txn->callback = callback;
272 txn->port = port;
274 generation = peer->generation;
275 smp_rmb();
276 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
277 peer->node_id, generation, peer->speed, addr, payload,
278 len, fwtty_common_callback, txn);
281 static void __fwtty_restart_tx(struct fwtty_port *port)
283 int len, avail;
285 len = dma_fifo_out_level(&port->tx_fifo);
286 if (len)
287 schedule_delayed_work(&port->drain, 0);
288 avail = dma_fifo_avail(&port->tx_fifo);
290 fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail);
293 static void fwtty_restart_tx(struct fwtty_port *port)
295 spin_lock_bh(&port->lock);
296 __fwtty_restart_tx(port);
297 spin_unlock_bh(&port->lock);
301 * fwtty_update_port_status - decodes & dispatches line status changes
303 * Note: in loopback, the port->lock is being held. Only use functions that
304 * don't attempt to reclaim the port->lock.
306 static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
308 unsigned delta;
309 struct tty_struct *tty;
311 /* simulated LSR/MSR status from remote */
312 status &= ~MCTRL_MASK;
313 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
314 delta &= ~(status & TIOCM_RNG);
315 port->mstatus = status;
317 if (delta & TIOCM_RNG)
318 ++port->icount.rng;
319 if (delta & TIOCM_DSR)
320 ++port->icount.dsr;
321 if (delta & TIOCM_CAR)
322 ++port->icount.dcd;
323 if (delta & TIOCM_CTS)
324 ++port->icount.cts;
326 fwtty_dbg(port, "status: %x delta: %x\n", status, delta);
328 if (delta & TIOCM_CAR) {
329 tty = tty_port_tty_get(&port->port);
330 if (tty && !C_CLOCAL(tty)) {
331 if (status & TIOCM_CAR)
332 wake_up_interruptible(&port->port.open_wait);
333 else
334 schedule_work(&port->hangup);
336 tty_kref_put(tty);
339 if (delta & TIOCM_CTS) {
340 tty = tty_port_tty_get(&port->port);
341 if (tty && C_CRTSCTS(tty)) {
342 if (tty->hw_stopped) {
343 if (status & TIOCM_CTS) {
344 tty->hw_stopped = 0;
345 if (port->loopback)
346 __fwtty_restart_tx(port);
347 else
348 fwtty_restart_tx(port);
350 } else {
351 if (~status & TIOCM_CTS)
352 tty->hw_stopped = 1;
355 tty_kref_put(tty);
357 } else if (delta & OOB_TX_THROTTLE) {
358 tty = tty_port_tty_get(&port->port);
359 if (tty) {
360 if (tty->hw_stopped) {
361 if (~status & OOB_TX_THROTTLE) {
362 tty->hw_stopped = 0;
363 if (port->loopback)
364 __fwtty_restart_tx(port);
365 else
366 fwtty_restart_tx(port);
368 } else {
369 if (status & OOB_TX_THROTTLE)
370 tty->hw_stopped = 1;
373 tty_kref_put(tty);
376 if (delta & (UART_LSR_BI << 24)) {
377 if (status & (UART_LSR_BI << 24)) {
378 port->break_last = jiffies;
379 schedule_delayed_work(&port->emit_breaks, 0);
380 } else {
381 /* run emit_breaks one last time (if pending) */
382 mod_delayed_work(system_wq, &port->emit_breaks, 0);
386 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
387 wake_up_interruptible(&port->port.delta_msr_wait);
391 * __fwtty_port_line_status - generate 'line status' for indicated port
393 * This function returns a remote 'MSR' state based on the local 'MCR' state,
394 * as if a null modem cable was attached. The actual status is a mangling
395 * of TIOCM_* bits suitable for sending to a peer's status_addr.
397 * Note: caller must be holding port lock
399 static unsigned __fwtty_port_line_status(struct fwtty_port *port)
401 unsigned status = 0;
403 /* TODO: add module param to tie RNG to DTR as well */
405 if (port->mctrl & TIOCM_DTR)
406 status |= TIOCM_DSR | TIOCM_CAR;
407 if (port->mctrl & TIOCM_RTS)
408 status |= TIOCM_CTS;
409 if (port->mctrl & OOB_RX_THROTTLE)
410 status |= OOB_TX_THROTTLE;
411 /* emulate BRK as add'l line status */
412 if (port->break_ctl)
413 status |= UART_LSR_BI << 24;
415 return status;
419 * __fwtty_write_port_status - send the port line status to peer
421 * Note: caller must be holding the port lock.
423 static int __fwtty_write_port_status(struct fwtty_port *port)
425 struct fwtty_peer *peer;
426 int err = -ENOENT;
427 unsigned status = __fwtty_port_line_status(port);
429 rcu_read_lock();
430 peer = rcu_dereference(port->peer);
431 if (peer) {
432 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
433 peer->status_addr, &status,
434 sizeof(status), NULL, port);
436 rcu_read_unlock();
438 return err;
442 * fwtty_write_port_status - same as above but locked by port lock
444 static int fwtty_write_port_status(struct fwtty_port *port)
446 int err;
448 spin_lock_bh(&port->lock);
449 err = __fwtty_write_port_status(port);
450 spin_unlock_bh(&port->lock);
451 return err;
454 static void fwtty_throttle_port(struct fwtty_port *port)
456 struct tty_struct *tty;
457 unsigned old;
459 tty = tty_port_tty_get(&port->port);
460 if (!tty)
461 return;
463 spin_lock_bh(&port->lock);
465 old = port->mctrl;
466 port->mctrl |= OOB_RX_THROTTLE;
467 if (C_CRTSCTS(tty))
468 port->mctrl &= ~TIOCM_RTS;
469 if (~old & OOB_RX_THROTTLE)
470 __fwtty_write_port_status(port);
472 spin_unlock_bh(&port->lock);
474 tty_kref_put(tty);
478 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
480 * When the remote has finished tx, and all in-flight rx has been received and
481 * and pushed to the flip buffer, the remote may close its device. This will
482 * drop DTR on the remote which will drop carrier here. Typically, the tty is
483 * hung up when carrier is dropped or lost.
485 * However, there is a race between the hang up and the line discipline
486 * delivering its data to the reader. A hangup will cause the ldisc to flush
487 * (ie., clear) the read buffer and flip buffer. Because of firewire's
488 * relatively high throughput, the ldisc frequently lags well behind the driver,
489 * resulting in lost data (which has already been received and written to
490 * the flip buffer) when the remote closes its end.
492 * Unfortunately, since the flip buffer offers no direct method for determining
493 * if it holds data, ensuring the ldisc has delivered all data is problematic.
496 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
497 static void fwtty_do_hangup(struct work_struct *work)
499 struct fwtty_port *port = to_port(work, hangup);
500 struct tty_struct *tty;
502 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
504 tty = tty_port_tty_get(&port->port);
505 if (tty)
506 tty_vhangup(tty);
507 tty_kref_put(tty);
510 static void fwtty_emit_breaks(struct work_struct *work)
512 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
513 static const char buf[16];
514 unsigned long now = jiffies;
515 unsigned long elapsed = now - port->break_last;
516 int n, t, c, brk = 0;
518 /* generate breaks at the line rate (but at least 1) */
519 n = (elapsed * port->cps) / HZ + 1;
520 port->break_last = now;
522 fwtty_dbg(port, "sending %d brks\n", n);
524 while (n) {
525 t = min(n, 16);
526 c = tty_insert_flip_string_fixed_flag(&port->port, buf,
527 TTY_BREAK, t);
528 n -= c;
529 brk += c;
530 if (c < t)
531 break;
533 tty_flip_buffer_push(&port->port);
535 if (port->mstatus & (UART_LSR_BI << 24))
536 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
537 port->icount.brk += brk;
540 static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
542 int c, n = len;
543 unsigned lsr;
544 int err = 0;
546 fwtty_dbg(port, "%d\n", n);
547 fwtty_profile_data(port->stats.reads, n);
549 if (port->write_only) {
550 n = 0;
551 goto out;
554 /* disregard break status; breaks are generated by emit_breaks work */
555 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
557 if (port->overrun)
558 lsr |= UART_LSR_OE;
560 if (lsr & UART_LSR_OE)
561 ++port->icount.overrun;
563 lsr &= port->status_mask;
564 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
565 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) {
566 err = -EIO;
567 goto out;
570 port->overrun = false;
572 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
573 /* TODO: don't drop SAK and Magic SysRq here */
574 n = 0;
575 goto out;
578 c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n);
579 if (c > 0)
580 tty_flip_buffer_push(&port->port);
581 n -= c;
583 if (n) {
584 port->overrun = true;
585 err = -EIO;
586 fwtty_err_ratelimited(port, "flip buffer overrun\n");
588 } else {
589 /* throttle the sender if remaining flip buffer space has
590 * reached high watermark to avoid losing data which may be
591 * in-flight. Since the AR request context is 32k, that much
592 * data may have _already_ been acked.
594 if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK)
595 fwtty_throttle_port(port);
598 out:
599 port->icount.rx += len;
600 port->stats.lost += n;
601 return err;
605 * fwtty_port_handler - bus address handler for port reads/writes
606 * @parameters: fw_address_callback_t as specified by firewire core interface
608 * This handler is responsible for handling inbound read/write dma from remotes.
610 static void fwtty_port_handler(struct fw_card *card,
611 struct fw_request *request,
612 int tcode, int destination, int source,
613 int generation,
614 unsigned long long addr,
615 void *data, size_t len,
616 void *callback_data)
618 struct fwtty_port *port = callback_data;
619 struct fwtty_peer *peer;
620 int err;
621 int rcode;
623 /* Only accept rx from the peer virtual-cabled to this port */
624 rcu_read_lock();
625 peer = __fwserial_peer_by_node_id(card, generation, source);
626 rcu_read_unlock();
627 if (!peer || peer != rcu_access_pointer(port->peer)) {
628 rcode = RCODE_ADDRESS_ERROR;
629 fwtty_err_ratelimited(port, "ignoring unauthenticated data\n");
630 goto respond;
633 switch (tcode) {
634 case TCODE_WRITE_QUADLET_REQUEST:
635 if (addr != port->rx_handler.offset || len != 4) {
636 rcode = RCODE_ADDRESS_ERROR;
637 } else {
638 fwtty_update_port_status(port, *(unsigned *)data);
639 rcode = RCODE_COMPLETE;
641 break;
643 case TCODE_WRITE_BLOCK_REQUEST:
644 if (addr != port->rx_handler.offset + 4 ||
645 len > port->rx_handler.length - 4) {
646 rcode = RCODE_ADDRESS_ERROR;
647 } else {
648 err = fwtty_rx(port, data, len);
649 switch (err) {
650 case 0:
651 rcode = RCODE_COMPLETE;
652 break;
653 case -EIO:
654 rcode = RCODE_DATA_ERROR;
655 break;
656 default:
657 rcode = RCODE_CONFLICT_ERROR;
658 break;
661 break;
663 default:
664 rcode = RCODE_TYPE_ERROR;
667 respond:
668 fw_send_response(card, request, rcode);
672 * fwtty_tx_complete - callback for tx dma
673 * @data: ignored, has no meaning for write txns
674 * @length: ignored, has no meaning for write txns
676 * The writer must be woken here if the fifo has been emptied because it
677 * may have slept if chars_in_buffer was != 0
679 static void fwtty_tx_complete(struct fw_card *card, int rcode,
680 void *data, size_t length,
681 struct fwtty_transaction *txn)
683 struct fwtty_port *port = txn->port;
684 int len;
686 fwtty_dbg(port, "rcode: %d\n", rcode);
688 switch (rcode) {
689 case RCODE_COMPLETE:
690 spin_lock_bh(&port->lock);
691 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
692 len = dma_fifo_level(&port->tx_fifo);
693 spin_unlock_bh(&port->lock);
695 port->icount.tx += txn->dma_pended.len;
696 break;
698 default:
699 /* TODO: implement retries */
700 spin_lock_bh(&port->lock);
701 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
702 len = dma_fifo_level(&port->tx_fifo);
703 spin_unlock_bh(&port->lock);
705 port->stats.dropped += txn->dma_pended.len;
708 if (len < WAKEUP_CHARS)
709 tty_port_tty_wakeup(&port->port);
712 static int fwtty_tx(struct fwtty_port *port, bool drain)
714 struct fwtty_peer *peer;
715 struct fwtty_transaction *txn;
716 struct tty_struct *tty;
717 int n, len;
719 tty = tty_port_tty_get(&port->port);
720 if (!tty)
721 return -ENOENT;
723 rcu_read_lock();
724 peer = rcu_dereference(port->peer);
725 if (!peer) {
726 n = -EIO;
727 goto out;
730 if (test_and_set_bit(IN_TX, &port->flags)) {
731 n = -EALREADY;
732 goto out;
735 /* try to write as many dma transactions out as possible */
736 n = -EAGAIN;
737 while (!tty->stopped && !tty->hw_stopped &&
738 !test_bit(STOP_TX, &port->flags)) {
739 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
740 if (!txn) {
741 n = -ENOMEM;
742 break;
745 spin_lock_bh(&port->lock);
746 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
747 spin_unlock_bh(&port->lock);
749 fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n);
751 if (n < 0) {
752 kmem_cache_free(fwtty_txn_cache, txn);
753 if (n == -EAGAIN) {
754 ++port->stats.tx_stall;
755 } else if (n == -ENODATA) {
756 fwtty_profile_data(port->stats.txns, 0);
757 } else {
758 ++port->stats.fifo_errs;
759 fwtty_err_ratelimited(port, "fifo err: %d\n",
762 break;
765 fwtty_profile_data(port->stats.txns, txn->dma_pended.len);
767 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
768 peer->fifo_addr, txn->dma_pended.data,
769 txn->dma_pended.len, fwtty_tx_complete,
770 port);
771 ++port->stats.sent;
774 * Stop tx if the 'last view' of the fifo is empty or if
775 * this is the writer and there's not enough data to bother
777 if (n == 0 || (!drain && n < WRITER_MINIMUM))
778 break;
781 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
782 spin_lock_bh(&port->lock);
783 len = dma_fifo_out_level(&port->tx_fifo);
784 if (len) {
785 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
787 schedule_delayed_work(&port->drain, delay);
789 len = dma_fifo_level(&port->tx_fifo);
790 spin_unlock_bh(&port->lock);
792 /* wakeup the writer */
793 if (drain && len < WAKEUP_CHARS)
794 tty_wakeup(tty);
797 clear_bit(IN_TX, &port->flags);
798 wake_up_interruptible(&port->wait_tx);
800 out:
801 rcu_read_unlock();
802 tty_kref_put(tty);
803 return n;
806 static void fwtty_drain_tx(struct work_struct *work)
808 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
810 fwtty_tx(port, true);
813 static void fwtty_write_xchar(struct fwtty_port *port, char ch)
815 struct fwtty_peer *peer;
817 ++port->stats.xchars;
819 fwtty_dbg(port, "%02x\n", ch);
821 rcu_read_lock();
822 peer = rcu_dereference(port->peer);
823 if (peer) {
824 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
825 peer->fifo_addr, &ch, sizeof(ch),
826 NULL, port);
828 rcu_read_unlock();
831 struct fwtty_port *fwtty_port_get(unsigned index)
833 struct fwtty_port *port;
835 if (index >= MAX_TOTAL_PORTS)
836 return NULL;
838 mutex_lock(&port_table_lock);
839 port = port_table[index];
840 if (port)
841 kref_get(&port->serial->kref);
842 mutex_unlock(&port_table_lock);
843 return port;
845 EXPORT_SYMBOL(fwtty_port_get);
847 static int fwtty_ports_add(struct fw_serial *serial)
849 int err = -EBUSY;
850 int i, j;
852 if (port_table_corrupt)
853 return err;
855 mutex_lock(&port_table_lock);
856 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
857 if (!port_table[i]) {
858 for (j = 0; j < num_ports; ++i, ++j) {
859 serial->ports[j]->index = i;
860 port_table[i] = serial->ports[j];
862 err = 0;
863 break;
866 mutex_unlock(&port_table_lock);
867 return err;
870 static void fwserial_destroy(struct kref *kref)
872 struct fw_serial *serial = to_serial(kref, kref);
873 struct fwtty_port **ports = serial->ports;
874 int j, i = ports[0]->index;
876 synchronize_rcu();
878 mutex_lock(&port_table_lock);
879 for (j = 0; j < num_ports; ++i, ++j) {
880 port_table_corrupt |= port_table[i] != ports[j];
881 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
882 i, port_table[i], j, ports[j]);
884 port_table[i] = NULL;
886 mutex_unlock(&port_table_lock);
888 for (j = 0; j < num_ports; ++j) {
889 fw_core_remove_address_handler(&ports[j]->rx_handler);
890 tty_port_destroy(&ports[j]->port);
891 kfree(ports[j]);
893 kfree(serial);
896 void fwtty_port_put(struct fwtty_port *port)
898 kref_put(&port->serial->kref, fwserial_destroy);
900 EXPORT_SYMBOL(fwtty_port_put);
902 static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
904 struct fwtty_port *port = to_port(tty_port, port);
906 fwtty_dbg(port, "on/off: %d\n", on);
908 spin_lock_bh(&port->lock);
909 /* Don't change carrier state if this is a console */
910 if (!port->port.console) {
911 if (on)
912 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
913 else
914 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
917 __fwtty_write_port_status(port);
918 spin_unlock_bh(&port->lock);
922 * fwtty_port_carrier_raised: required tty_port operation
924 * This port operation is polled after a tty has been opened and is waiting for
925 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
927 static int fwtty_port_carrier_raised(struct tty_port *tty_port)
929 struct fwtty_port *port = to_port(tty_port, port);
930 int rc;
932 rc = (port->mstatus & TIOCM_CAR);
934 fwtty_dbg(port, "%d\n", rc);
936 return rc;
939 static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
941 unsigned baud, frame;
943 baud = tty_termios_baud_rate(&tty->termios);
944 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
946 /* compute bit count of 2 frames */
947 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
949 switch (C_CSIZE(tty)) {
950 case CS5:
951 frame -= (C_CSTOPB(tty)) ? 1 : 0;
952 break;
953 case CS6:
954 frame += 2;
955 break;
956 case CS7:
957 frame += 4;
958 break;
959 case CS8:
960 frame += 6;
961 break;
964 port->cps = (baud << 1) / frame;
966 port->status_mask = UART_LSR_OE;
967 if (_I_FLAG(tty, BRKINT | PARMRK))
968 port->status_mask |= UART_LSR_BI;
970 port->ignore_mask = 0;
971 if (I_IGNBRK(tty)) {
972 port->ignore_mask |= UART_LSR_BI;
973 if (I_IGNPAR(tty))
974 port->ignore_mask |= UART_LSR_OE;
977 port->write_only = !C_CREAD(tty);
979 /* turn off echo and newline xlat if loopback */
980 if (port->loopback) {
981 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
982 ECHONL | ECHOPRT | ECHOCTL);
983 tty->termios.c_oflag &= ~ONLCR;
986 return baud;
989 static int fwtty_port_activate(struct tty_port *tty_port,
990 struct tty_struct *tty)
992 struct fwtty_port *port = to_port(tty_port, port);
993 unsigned baud;
994 int err;
996 set_bit(TTY_IO_ERROR, &tty->flags);
998 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
999 cache_line_size(),
1000 port->max_payload,
1001 FWTTY_PORT_MAX_PEND_DMA,
1002 GFP_KERNEL);
1003 if (err)
1004 return err;
1006 spin_lock_bh(&port->lock);
1008 baud = set_termios(port, tty);
1010 /* if console, don't change carrier state */
1011 if (!port->port.console) {
1012 port->mctrl = 0;
1013 if (baud != 0)
1014 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1017 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1018 tty->hw_stopped = 1;
1020 __fwtty_write_port_status(port);
1021 spin_unlock_bh(&port->lock);
1023 clear_bit(TTY_IO_ERROR, &tty->flags);
1025 return 0;
1029 * fwtty_port_shutdown
1031 * Note: the tty port core ensures this is not the console and
1032 * manages TTY_IO_ERROR properly
1034 static void fwtty_port_shutdown(struct tty_port *tty_port)
1036 struct fwtty_port *port = to_port(tty_port, port);
1038 /* TODO: cancel outstanding transactions */
1040 cancel_delayed_work_sync(&port->emit_breaks);
1041 cancel_delayed_work_sync(&port->drain);
1043 spin_lock_bh(&port->lock);
1044 port->flags = 0;
1045 port->break_ctl = 0;
1046 port->overrun = 0;
1047 __fwtty_write_port_status(port);
1048 dma_fifo_free(&port->tx_fifo);
1049 spin_unlock_bh(&port->lock);
1052 static int fwtty_open(struct tty_struct *tty, struct file *fp)
1054 struct fwtty_port *port = tty->driver_data;
1056 return tty_port_open(&port->port, tty, fp);
1059 static void fwtty_close(struct tty_struct *tty, struct file *fp)
1061 struct fwtty_port *port = tty->driver_data;
1063 tty_port_close(&port->port, tty, fp);
1066 static void fwtty_hangup(struct tty_struct *tty)
1068 struct fwtty_port *port = tty->driver_data;
1070 tty_port_hangup(&port->port);
1073 static void fwtty_cleanup(struct tty_struct *tty)
1075 struct fwtty_port *port = tty->driver_data;
1077 tty->driver_data = NULL;
1078 fwtty_port_put(port);
1081 static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1083 struct fwtty_port *port = fwtty_port_get(tty->index);
1084 int err;
1086 err = tty_standard_install(driver, tty);
1087 if (!err)
1088 tty->driver_data = port;
1089 else
1090 fwtty_port_put(port);
1091 return err;
1094 static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1096 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1097 int err;
1099 err = tty_standard_install(driver, tty);
1100 if (!err)
1101 tty->driver_data = port;
1102 else
1103 fwtty_port_put(port);
1104 return err;
1107 static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1109 struct fwtty_port *port = tty->driver_data;
1110 int n, len;
1112 fwtty_dbg(port, "%d\n", c);
1113 fwtty_profile_data(port->stats.writes, c);
1115 spin_lock_bh(&port->lock);
1116 n = dma_fifo_in(&port->tx_fifo, buf, c);
1117 len = dma_fifo_out_level(&port->tx_fifo);
1118 if (len < DRAIN_THRESHOLD)
1119 schedule_delayed_work(&port->drain, 1);
1120 spin_unlock_bh(&port->lock);
1122 if (len >= DRAIN_THRESHOLD)
1123 fwtty_tx(port, false);
1125 debug_short_write(port, c, n);
1127 return (n < 0) ? 0 : n;
1130 static int fwtty_write_room(struct tty_struct *tty)
1132 struct fwtty_port *port = tty->driver_data;
1133 int n;
1135 spin_lock_bh(&port->lock);
1136 n = dma_fifo_avail(&port->tx_fifo);
1137 spin_unlock_bh(&port->lock);
1139 fwtty_dbg(port, "%d\n", n);
1141 return n;
1144 static int fwtty_chars_in_buffer(struct tty_struct *tty)
1146 struct fwtty_port *port = tty->driver_data;
1147 int n;
1149 spin_lock_bh(&port->lock);
1150 n = dma_fifo_level(&port->tx_fifo);
1151 spin_unlock_bh(&port->lock);
1153 fwtty_dbg(port, "%d\n", n);
1155 return n;
1158 static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1160 struct fwtty_port *port = tty->driver_data;
1162 fwtty_dbg(port, "%02x\n", ch);
1164 fwtty_write_xchar(port, ch);
1167 static void fwtty_throttle(struct tty_struct *tty)
1169 struct fwtty_port *port = tty->driver_data;
1172 * Ignore throttling (but not unthrottling).
1173 * It only makes sense to throttle when data will no longer be
1174 * accepted by the tty flip buffer. For example, it is
1175 * possible for received data to overflow the tty buffer long
1176 * before the line discipline ever has a chance to throttle the driver.
1177 * Additionally, the driver may have already completed the I/O
1178 * but the tty buffer is still emptying, so the line discipline is
1179 * throttling and unthrottling nothing.
1182 ++port->stats.throttled;
1185 static void fwtty_unthrottle(struct tty_struct *tty)
1187 struct fwtty_port *port = tty->driver_data;
1189 fwtty_dbg(port, "CRTSCTS: %d\n", C_CRTSCTS(tty) != 0);
1191 fwtty_profile_fifo(port, port->stats.unthrottle);
1193 spin_lock_bh(&port->lock);
1194 port->mctrl &= ~OOB_RX_THROTTLE;
1195 if (C_CRTSCTS(tty))
1196 port->mctrl |= TIOCM_RTS;
1197 __fwtty_write_port_status(port);
1198 spin_unlock_bh(&port->lock);
1201 static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1202 struct async_icount *prev)
1204 struct async_icount now;
1205 int delta;
1207 now = port->icount;
1209 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1210 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1211 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1212 (mask & TIOCM_CTS && prev->cts != now.cts));
1214 *prev = now;
1216 return delta;
1219 static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1221 struct async_icount prev;
1223 prev = port->icount;
1225 return wait_event_interruptible(port->port.delta_msr_wait,
1226 check_msr_delta(port, mask, &prev));
1229 static int get_serial_info(struct fwtty_port *port,
1230 struct serial_struct __user *info)
1232 struct serial_struct tmp;
1234 memset(&tmp, 0, sizeof(tmp));
1236 tmp.type = PORT_UNKNOWN;
1237 tmp.line = port->port.tty->index;
1238 tmp.flags = port->port.flags;
1239 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1240 tmp.baud_base = 400000000;
1241 tmp.close_delay = port->port.close_delay;
1243 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1246 static int set_serial_info(struct fwtty_port *port,
1247 struct serial_struct __user *info)
1249 struct serial_struct tmp;
1251 if (copy_from_user(&tmp, info, sizeof(tmp)))
1252 return -EFAULT;
1254 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
1255 tmp.baud_base != 400000000)
1256 return -EPERM;
1258 if (!capable(CAP_SYS_ADMIN)) {
1259 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1260 (port->port.flags & ~ASYNC_USR_MASK)))
1261 return -EPERM;
1262 } else {
1263 port->port.close_delay = tmp.close_delay * HZ / 100;
1266 return 0;
1269 static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1270 unsigned long arg)
1272 struct fwtty_port *port = tty->driver_data;
1273 int err;
1275 switch (cmd) {
1276 case TIOCGSERIAL:
1277 mutex_lock(&port->port.mutex);
1278 err = get_serial_info(port, (void __user *)arg);
1279 mutex_unlock(&port->port.mutex);
1280 break;
1282 case TIOCSSERIAL:
1283 mutex_lock(&port->port.mutex);
1284 err = set_serial_info(port, (void __user *)arg);
1285 mutex_unlock(&port->port.mutex);
1286 break;
1288 case TIOCMIWAIT:
1289 err = wait_msr_change(port, arg);
1290 break;
1292 default:
1293 err = -ENOIOCTLCMD;
1296 return err;
1299 static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1301 struct fwtty_port *port = tty->driver_data;
1302 unsigned baud;
1304 spin_lock_bh(&port->lock);
1305 baud = set_termios(port, tty);
1307 if ((baud == 0) && (old->c_cflag & CBAUD)) {
1308 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1309 } else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1310 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1311 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1312 else
1313 port->mctrl |= TIOCM_DTR;
1315 __fwtty_write_port_status(port);
1316 spin_unlock_bh(&port->lock);
1318 if (old->c_cflag & CRTSCTS) {
1319 if (!C_CRTSCTS(tty)) {
1320 tty->hw_stopped = 0;
1321 fwtty_restart_tx(port);
1323 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1324 tty->hw_stopped = 1;
1329 * fwtty_break_ctl - start/stop sending breaks
1331 * Signals the remote to start or stop generating simulated breaks.
1332 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1333 * before signalling the break line status. This guarantees any pending rx will
1334 * be queued to the line discipline before break is simulated on the remote.
1335 * Conversely, turning off break_ctl requires signalling the line status change,
1336 * then enabling tx.
1338 static int fwtty_break_ctl(struct tty_struct *tty, int state)
1340 struct fwtty_port *port = tty->driver_data;
1341 long ret;
1343 fwtty_dbg(port, "%d\n", state);
1345 if (state == -1) {
1346 set_bit(STOP_TX, &port->flags);
1347 ret = wait_event_interruptible_timeout(port->wait_tx,
1348 !test_bit(IN_TX, &port->flags),
1349 10);
1350 if (ret == 0 || ret == -ERESTARTSYS) {
1351 clear_bit(STOP_TX, &port->flags);
1352 fwtty_restart_tx(port);
1353 return -EINTR;
1357 spin_lock_bh(&port->lock);
1358 port->break_ctl = (state == -1);
1359 __fwtty_write_port_status(port);
1360 spin_unlock_bh(&port->lock);
1362 if (state == 0) {
1363 spin_lock_bh(&port->lock);
1364 dma_fifo_reset(&port->tx_fifo);
1365 clear_bit(STOP_TX, &port->flags);
1366 spin_unlock_bh(&port->lock);
1368 return 0;
1371 static int fwtty_tiocmget(struct tty_struct *tty)
1373 struct fwtty_port *port = tty->driver_data;
1374 unsigned tiocm;
1376 spin_lock_bh(&port->lock);
1377 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1378 spin_unlock_bh(&port->lock);
1380 fwtty_dbg(port, "%x\n", tiocm);
1382 return tiocm;
1385 static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1387 struct fwtty_port *port = tty->driver_data;
1389 fwtty_dbg(port, "set: %x clear: %x\n", set, clear);
1391 /* TODO: simulate loopback if TIOCM_LOOP set */
1393 spin_lock_bh(&port->lock);
1394 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1395 port->mctrl |= set & MCTRL_MASK & 0xffff;
1396 __fwtty_write_port_status(port);
1397 spin_unlock_bh(&port->lock);
1398 return 0;
1401 static int fwtty_get_icount(struct tty_struct *tty,
1402 struct serial_icounter_struct *icount)
1404 struct fwtty_port *port = tty->driver_data;
1405 struct stats stats;
1407 memcpy(&stats, &port->stats, sizeof(stats));
1408 if (port->port.console)
1409 (*port->fwcon_ops->stats)(&stats, port->con_data);
1411 icount->cts = port->icount.cts;
1412 icount->dsr = port->icount.dsr;
1413 icount->rng = port->icount.rng;
1414 icount->dcd = port->icount.dcd;
1415 icount->rx = port->icount.rx;
1416 icount->tx = port->icount.tx + stats.xchars;
1417 icount->frame = port->icount.frame;
1418 icount->overrun = port->icount.overrun;
1419 icount->parity = port->icount.parity;
1420 icount->brk = port->icount.brk;
1421 icount->buf_overrun = port->icount.overrun;
1422 return 0;
1425 static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1427 struct stats stats;
1429 memcpy(&stats, &port->stats, sizeof(stats));
1430 if (port->port.console)
1431 (*port->fwcon_ops->stats)(&stats, port->con_data);
1433 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1434 port->icount.tx + stats.xchars, port->icount.rx);
1435 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1436 port->icount.dsr, port->icount.rng, port->icount.dcd);
1437 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1438 port->icount.overrun, port->icount.parity, port->icount.brk);
1441 static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1443 struct stats stats;
1445 memcpy(&stats, &port->stats, sizeof(stats));
1446 if (port->port.console)
1447 (*port->fwcon_ops->stats)(&stats, port->con_data);
1449 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1450 stats.tx_stall, stats.fifo_errs, stats.lost);
1451 seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled);
1453 if (port->port.console) {
1454 seq_puts(m, "\n ");
1455 (*port->fwcon_ops->proc_show)(m, port->con_data);
1458 fwtty_dump_profile(m, &port->stats);
1461 static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
1463 int generation = peer->generation;
1465 smp_rmb();
1466 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1467 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1468 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1469 peer->max_payload, (unsigned long long) peer->guid);
1470 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
1471 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
1472 seq_putc(m, '\n');
1475 static int fwtty_proc_show(struct seq_file *m, void *v)
1477 struct fwtty_port *port;
1478 int i;
1480 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1481 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1482 seq_printf(m, "%2d:", i);
1483 if (capable(CAP_SYS_ADMIN))
1484 fwtty_proc_show_port(m, port);
1485 fwtty_port_put(port);
1486 seq_puts(m, "\n");
1488 return 0;
1491 static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
1493 struct fw_serial *serial = m->private;
1494 struct fwtty_port *port;
1495 int i;
1497 for (i = 0; i < num_ports; ++i) {
1498 port = fwtty_port_get(serial->ports[i]->index);
1499 if (port) {
1500 seq_printf(m, "%2d:", port->index);
1501 fwtty_proc_show_port(m, port);
1502 fwtty_debugfs_show_port(m, port);
1503 fwtty_port_put(port);
1504 seq_puts(m, "\n");
1507 return 0;
1510 static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
1512 struct fw_serial *serial = m->private;
1513 struct fwtty_peer *peer;
1515 rcu_read_lock();
1516 seq_printf(m, "card: %s guid: %016llx\n",
1517 dev_name(serial->card->device),
1518 (unsigned long long) serial->card->guid);
1519 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1520 fwtty_debugfs_show_peer(m, peer);
1521 rcu_read_unlock();
1522 return 0;
1525 static int fwtty_proc_open(struct inode *inode, struct file *fp)
1527 return single_open(fp, fwtty_proc_show, NULL);
1530 static int fwtty_stats_open(struct inode *inode, struct file *fp)
1532 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
1535 static int fwtty_peers_open(struct inode *inode, struct file *fp)
1537 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
1540 static const struct file_operations fwtty_stats_fops = {
1541 .owner = THIS_MODULE,
1542 .open = fwtty_stats_open,
1543 .read = seq_read,
1544 .llseek = seq_lseek,
1545 .release = single_release,
1548 static const struct file_operations fwtty_peers_fops = {
1549 .owner = THIS_MODULE,
1550 .open = fwtty_peers_open,
1551 .read = seq_read,
1552 .llseek = seq_lseek,
1553 .release = single_release,
1556 static const struct file_operations fwtty_proc_fops = {
1557 .owner = THIS_MODULE,
1558 .open = fwtty_proc_open,
1559 .read = seq_read,
1560 .llseek = seq_lseek,
1561 .release = single_release,
1564 static const struct tty_port_operations fwtty_port_ops = {
1565 .dtr_rts = fwtty_port_dtr_rts,
1566 .carrier_raised = fwtty_port_carrier_raised,
1567 .shutdown = fwtty_port_shutdown,
1568 .activate = fwtty_port_activate,
1571 static const struct tty_operations fwtty_ops = {
1572 .open = fwtty_open,
1573 .close = fwtty_close,
1574 .hangup = fwtty_hangup,
1575 .cleanup = fwtty_cleanup,
1576 .install = fwtty_install,
1577 .write = fwtty_write,
1578 .write_room = fwtty_write_room,
1579 .chars_in_buffer = fwtty_chars_in_buffer,
1580 .send_xchar = fwtty_send_xchar,
1581 .throttle = fwtty_throttle,
1582 .unthrottle = fwtty_unthrottle,
1583 .ioctl = fwtty_ioctl,
1584 .set_termios = fwtty_set_termios,
1585 .break_ctl = fwtty_break_ctl,
1586 .tiocmget = fwtty_tiocmget,
1587 .tiocmset = fwtty_tiocmset,
1588 .get_icount = fwtty_get_icount,
1589 .proc_fops = &fwtty_proc_fops,
1592 static const struct tty_operations fwloop_ops = {
1593 .open = fwtty_open,
1594 .close = fwtty_close,
1595 .hangup = fwtty_hangup,
1596 .cleanup = fwtty_cleanup,
1597 .install = fwloop_install,
1598 .write = fwtty_write,
1599 .write_room = fwtty_write_room,
1600 .chars_in_buffer = fwtty_chars_in_buffer,
1601 .send_xchar = fwtty_send_xchar,
1602 .throttle = fwtty_throttle,
1603 .unthrottle = fwtty_unthrottle,
1604 .ioctl = fwtty_ioctl,
1605 .set_termios = fwtty_set_termios,
1606 .break_ctl = fwtty_break_ctl,
1607 .tiocmget = fwtty_tiocmget,
1608 .tiocmset = fwtty_tiocmset,
1609 .get_icount = fwtty_get_icount,
1612 static inline int mgmt_pkt_expected_len(__be16 code)
1614 static const struct fwserial_mgmt_pkt pkt;
1616 switch (be16_to_cpu(code)) {
1617 case FWSC_VIRT_CABLE_PLUG:
1618 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1620 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1621 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1623 case FWSC_VIRT_CABLE_UNPLUG:
1624 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1625 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1626 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1627 return sizeof(pkt.hdr);
1629 default:
1630 return -1;
1634 static inline void fill_plug_params(struct virt_plug_params *params,
1635 struct fwtty_port *port)
1637 u64 status_addr = port->rx_handler.offset;
1638 u64 fifo_addr = port->rx_handler.offset + 4;
1639 size_t fifo_len = port->rx_handler.length - 4;
1641 params->status_hi = cpu_to_be32(status_addr >> 32);
1642 params->status_lo = cpu_to_be32(status_addr);
1643 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1644 params->fifo_lo = cpu_to_be32(fifo_addr);
1645 params->fifo_len = cpu_to_be32(fifo_len);
1648 static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1649 struct fwtty_port *port)
1651 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1652 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1653 fill_plug_params(&pkt->plug_req, port);
1656 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1657 struct fwtty_port *port)
1659 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1660 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1661 fill_plug_params(&pkt->plug_rsp, port);
1664 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1666 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1667 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1670 static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1672 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1673 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1676 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1678 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1679 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1682 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1684 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1685 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1688 static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1689 struct virt_plug_params *params)
1691 struct fwtty_port *port = peer->port;
1693 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1694 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1695 peer->fifo_len = be32_to_cpu(params->fifo_len);
1696 peer_set_state(peer, FWPS_ATTACHED);
1698 /* reconfigure tx_fifo optimally for this peer */
1699 spin_lock_bh(&port->lock);
1700 port->max_payload = min(peer->max_payload, peer->fifo_len);
1701 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1702 spin_unlock_bh(&peer->port->lock);
1704 if (port->port.console && port->fwcon_ops->notify != NULL)
1705 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1707 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n",
1708 (unsigned long long)peer->guid, dev_name(port->device));
1711 static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1712 struct fwserial_mgmt_pkt *pkt)
1714 int generation;
1715 int rcode, tries = 5;
1717 do {
1718 generation = peer->generation;
1719 smp_rmb();
1721 rcode = fw_run_transaction(peer->serial->card,
1722 TCODE_WRITE_BLOCK_REQUEST,
1723 peer->node_id,
1724 generation, peer->speed,
1725 peer->mgmt_addr,
1726 pkt, be16_to_cpu(pkt->hdr.len));
1727 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1728 rcode == RCODE_GENERATION) {
1729 fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode);
1730 continue;
1731 } else {
1732 break;
1734 } while (--tries > 0);
1735 return rcode;
1739 * fwserial_claim_port - attempt to claim port @ index for peer
1741 * Returns ptr to claimed port or error code (as ERR_PTR())
1742 * Can sleep - must be called from process context
1744 static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1745 int index)
1747 struct fwtty_port *port;
1749 if (index < 0 || index >= num_ports)
1750 return ERR_PTR(-EINVAL);
1752 /* must guarantee that previous port releases have completed */
1753 synchronize_rcu();
1755 port = peer->serial->ports[index];
1756 spin_lock_bh(&port->lock);
1757 if (!rcu_access_pointer(port->peer))
1758 rcu_assign_pointer(port->peer, peer);
1759 else
1760 port = ERR_PTR(-EBUSY);
1761 spin_unlock_bh(&port->lock);
1763 return port;
1767 * fwserial_find_port - find avail port and claim for peer
1769 * Returns ptr to claimed port or NULL if none avail
1770 * Can sleep - must be called from process context
1772 static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1774 struct fwtty_port **ports = peer->serial->ports;
1775 int i;
1777 /* must guarantee that previous port releases have completed */
1778 synchronize_rcu();
1780 /* TODO: implement optional GUID-to-specific port # matching */
1782 /* find an unattached port (but not the loopback port, if present) */
1783 for (i = 0; i < num_ttys; ++i) {
1784 spin_lock_bh(&ports[i]->lock);
1785 if (!ports[i]->peer) {
1786 /* claim port */
1787 rcu_assign_pointer(ports[i]->peer, peer);
1788 spin_unlock_bh(&ports[i]->lock);
1789 return ports[i];
1791 spin_unlock_bh(&ports[i]->lock);
1793 return NULL;
1796 static void fwserial_release_port(struct fwtty_port *port, bool reset)
1798 /* drop carrier (and all other line status) */
1799 if (reset)
1800 fwtty_update_port_status(port, 0);
1802 spin_lock_bh(&port->lock);
1804 /* reset dma fifo max transmission size back to S100 */
1805 port->max_payload = link_speed_to_max_payload(SCODE_100);
1806 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1808 RCU_INIT_POINTER(port->peer, NULL);
1809 spin_unlock_bh(&port->lock);
1811 if (port->port.console && port->fwcon_ops->notify != NULL)
1812 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1815 static void fwserial_plug_timeout(unsigned long data)
1817 struct fwtty_peer *peer = (struct fwtty_peer *)data;
1818 struct fwtty_port *port;
1820 spin_lock_bh(&peer->lock);
1821 if (peer->state != FWPS_PLUG_PENDING) {
1822 spin_unlock_bh(&peer->lock);
1823 return;
1826 port = peer_revert_state(peer);
1827 spin_unlock_bh(&peer->lock);
1829 if (port)
1830 fwserial_release_port(port, false);
1834 * fwserial_connect_peer - initiate virtual cable with peer
1836 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1837 * otherwise error code. Must be called from process context.
1839 static int fwserial_connect_peer(struct fwtty_peer *peer)
1841 struct fwtty_port *port;
1842 struct fwserial_mgmt_pkt *pkt;
1843 int err, rcode;
1845 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1846 if (!pkt)
1847 return -ENOMEM;
1849 port = fwserial_find_port(peer);
1850 if (!port) {
1851 fwtty_err(&peer->unit, "avail ports in use\n");
1852 err = -EBUSY;
1853 goto free_pkt;
1856 spin_lock_bh(&peer->lock);
1858 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1859 if (peer->state != FWPS_NOT_ATTACHED) {
1860 err = -EBUSY;
1861 goto release_port;
1864 peer->port = port;
1865 peer_set_state(peer, FWPS_PLUG_PENDING);
1867 fill_plug_req(pkt, peer->port);
1869 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1870 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1871 spin_unlock_bh(&peer->lock);
1873 rcode = fwserial_send_mgmt_sync(peer, pkt);
1875 spin_lock_bh(&peer->lock);
1876 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1877 if (rcode == RCODE_CONFLICT_ERROR)
1878 err = -EAGAIN;
1879 else
1880 err = -EIO;
1881 goto cancel_timer;
1883 spin_unlock_bh(&peer->lock);
1885 kfree(pkt);
1886 return 0;
1888 cancel_timer:
1889 del_timer(&peer->timer);
1890 peer_revert_state(peer);
1891 release_port:
1892 spin_unlock_bh(&peer->lock);
1893 fwserial_release_port(port, false);
1894 free_pkt:
1895 kfree(pkt);
1896 return err;
1900 * fwserial_close_port -
1901 * HUP the tty (if the tty exists) and unregister the tty device.
1902 * Only used by the unit driver upon unit removal to disconnect and
1903 * cleanup all attached ports
1905 * The port reference is put by fwtty_cleanup (if a reference was
1906 * ever taken).
1908 static void fwserial_close_port(struct tty_driver *driver,
1909 struct fwtty_port *port)
1911 struct tty_struct *tty;
1913 mutex_lock(&port->port.mutex);
1914 tty = tty_port_tty_get(&port->port);
1915 if (tty) {
1916 tty_vhangup(tty);
1917 tty_kref_put(tty);
1919 mutex_unlock(&port->port.mutex);
1921 if (driver == fwloop_driver)
1922 tty_unregister_device(driver, loop_idx(port));
1923 else
1924 tty_unregister_device(driver, port->index);
1928 * fwserial_lookup - finds first fw_serial associated with card
1929 * @card: fw_card to match
1931 * NB: caller must be holding fwserial_list_mutex
1933 static struct fw_serial *fwserial_lookup(struct fw_card *card)
1935 struct fw_serial *serial;
1937 list_for_each_entry(serial, &fwserial_list, list) {
1938 if (card == serial->card)
1939 return serial;
1942 return NULL;
1946 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1947 * @card: fw_card to match
1949 * NB: caller must be inside rcu_read_lock() section
1951 static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
1953 struct fw_serial *serial;
1955 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1956 if (card == serial->card)
1957 return serial;
1960 return NULL;
1964 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1966 * If a matching peer could not be found for the specified generation/node id,
1967 * this could be because:
1968 * a) the generation has changed and one of the nodes hasn't updated yet
1969 * b) the remote node has created its remote unit device before this
1970 * local node has created its corresponding remote unit device
1971 * In either case, the remote node should retry
1973 * Note: caller must be in rcu_read_lock() section
1975 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
1976 int generation, int id)
1978 struct fw_serial *serial;
1979 struct fwtty_peer *peer;
1981 serial = __fwserial_lookup_rcu(card);
1982 if (!serial) {
1984 * Something is very wrong - there should be a matching
1985 * fw_serial structure for every fw_card. Maybe the remote node
1986 * has created its remote unit device before this driver has
1987 * been probed for any unit devices...
1989 fwtty_err(card, "unknown card (guid %016llx)\n",
1990 (unsigned long long) card->guid);
1991 return NULL;
1994 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1995 int g = peer->generation;
1997 smp_rmb();
1998 if (generation == g && id == peer->node_id)
1999 return peer;
2002 return NULL;
2005 #ifdef DEBUG
2006 static void __dump_peer_list(struct fw_card *card)
2008 struct fw_serial *serial;
2009 struct fwtty_peer *peer;
2011 serial = __fwserial_lookup_rcu(card);
2012 if (!serial)
2013 return;
2015 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2016 int g = peer->generation;
2018 smp_rmb();
2019 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n",
2020 g, peer->node_id, (unsigned long long) peer->guid);
2023 #else
2024 #define __dump_peer_list(s)
2025 #endif
2027 static void fwserial_auto_connect(struct work_struct *work)
2029 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2030 int err;
2032 err = fwserial_connect_peer(peer);
2033 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2034 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2037 static void fwserial_peer_workfn(struct work_struct *work)
2039 struct fwtty_peer *peer = to_peer(work, work);
2041 peer->workfn(work);
2045 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2046 * @serial: aggregate representing the specific fw_card to add the peer to
2047 * @unit: 'peer' to create and add to peer_list of serial
2049 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2050 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2051 * available tty port. This function is called either directly or indirectly
2052 * as a result of a 'serial' unit device being created & probed.
2054 * Note: this function is serialized with fwserial_remove_peer() by the
2055 * fwserial_list_mutex held in fwserial_probe().
2057 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2058 * via the dev_set_drvdata() for the device of the fw_unit.
2060 static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2062 struct device *dev = &unit->device;
2063 struct fw_device *parent = fw_parent_device(unit);
2064 struct fwtty_peer *peer;
2065 struct fw_csr_iterator ci;
2066 int key, val;
2067 int generation;
2069 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2070 if (!peer)
2071 return -ENOMEM;
2073 peer_set_state(peer, FWPS_NOT_ATTACHED);
2075 dev_set_drvdata(dev, peer);
2076 peer->unit = unit;
2077 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2078 peer->speed = parent->max_speed;
2079 peer->max_payload = min(device_max_receive(parent),
2080 link_speed_to_max_payload(peer->speed));
2082 generation = parent->generation;
2083 smp_rmb();
2084 peer->node_id = parent->node_id;
2085 smp_wmb();
2086 peer->generation = generation;
2088 /* retrieve the mgmt bus addr from the unit directory */
2089 fw_csr_iterator_init(&ci, unit->directory);
2090 while (fw_csr_iterator_next(&ci, &key, &val)) {
2091 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2092 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2093 break;
2096 if (peer->mgmt_addr == 0ULL) {
2098 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2099 * this peer will not be able to attach to a remote
2101 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2104 spin_lock_init(&peer->lock);
2105 peer->port = NULL;
2107 init_timer(&peer->timer);
2108 INIT_WORK(&peer->work, fwserial_peer_workfn);
2109 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2111 /* associate peer with specific fw_card */
2112 peer->serial = serial;
2113 list_add_rcu(&peer->list, &serial->peer_list);
2115 fwtty_info(&peer->unit, "peer added (guid:%016llx)\n",
2116 (unsigned long long)peer->guid);
2118 /* identify the local unit & virt cable to loopback port */
2119 if (parent->is_local) {
2120 serial->self = peer;
2121 if (create_loop_dev) {
2122 struct fwtty_port *port;
2124 port = fwserial_claim_port(peer, num_ttys);
2125 if (!IS_ERR(port)) {
2126 struct virt_plug_params params;
2128 spin_lock_bh(&peer->lock);
2129 peer->port = port;
2130 fill_plug_params(&params, port);
2131 fwserial_virt_plug_complete(peer, &params);
2132 spin_unlock_bh(&peer->lock);
2134 fwtty_write_port_status(port);
2138 } else if (auto_connect) {
2139 /* auto-attach to remote units only (if policy allows) */
2140 schedule_delayed_work(&peer->connect, 1);
2143 return 0;
2147 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2149 * Remove a 'peer' from its list of peers. This function is only
2150 * called by fwserial_remove() on bus removal of the unit device.
2152 * Note: this function is serialized with fwserial_add_peer() by the
2153 * fwserial_list_mutex held in fwserial_remove().
2155 static void fwserial_remove_peer(struct fwtty_peer *peer)
2157 struct fwtty_port *port;
2159 spin_lock_bh(&peer->lock);
2160 peer_set_state(peer, FWPS_GONE);
2161 spin_unlock_bh(&peer->lock);
2163 cancel_delayed_work_sync(&peer->connect);
2164 cancel_work_sync(&peer->work);
2166 spin_lock_bh(&peer->lock);
2167 /* if this unit is the local unit, clear link */
2168 if (peer == peer->serial->self)
2169 peer->serial->self = NULL;
2171 /* cancel the request timeout timer (if running) */
2172 del_timer(&peer->timer);
2174 port = peer->port;
2175 peer->port = NULL;
2177 list_del_rcu(&peer->list);
2179 fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n",
2180 (unsigned long long)peer->guid);
2182 spin_unlock_bh(&peer->lock);
2184 if (port)
2185 fwserial_release_port(port, true);
2187 synchronize_rcu();
2188 kfree(peer);
2192 * fwserial_create - init everything to create TTYs for a specific fw_card
2193 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2195 * This function inits the aggregate structure (an fw_serial instance)
2196 * used to manage the TTY ports registered by a specific fw_card. Also, the
2197 * unit device is added as the first 'peer'.
2199 * This unit device may represent a local unit device (as specified by the
2200 * config ROM unit directory) or it may represent a remote unit device
2201 * (as specified by the reading of the remote node's config ROM).
2203 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2204 * value to indicate which error.
2206 static int fwserial_create(struct fw_unit *unit)
2208 struct fw_device *parent = fw_parent_device(unit);
2209 struct fw_card *card = parent->card;
2210 struct fw_serial *serial;
2211 struct fwtty_port *port;
2212 struct device *tty_dev;
2213 int i, j;
2214 int err;
2216 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2217 if (!serial)
2218 return -ENOMEM;
2220 kref_init(&serial->kref);
2221 serial->card = card;
2222 INIT_LIST_HEAD(&serial->peer_list);
2224 for (i = 0; i < num_ports; ++i) {
2225 port = kzalloc(sizeof(*port), GFP_KERNEL);
2226 if (!port) {
2227 err = -ENOMEM;
2228 goto free_ports;
2230 tty_port_init(&port->port);
2231 port->index = FWTTY_INVALID_INDEX;
2232 port->port.ops = &fwtty_port_ops;
2233 port->serial = serial;
2234 tty_buffer_set_limit(&port->port, 128 * 1024);
2236 spin_lock_init(&port->lock);
2237 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2238 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2239 INIT_WORK(&port->hangup, fwtty_do_hangup);
2240 init_waitqueue_head(&port->wait_tx);
2241 port->max_payload = link_speed_to_max_payload(SCODE_100);
2242 dma_fifo_init(&port->tx_fifo);
2244 RCU_INIT_POINTER(port->peer, NULL);
2245 serial->ports[i] = port;
2247 /* get unique bus addr region for port's status & recv fifo */
2248 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2249 port->rx_handler.address_callback = fwtty_port_handler;
2250 port->rx_handler.callback_data = port;
2252 * XXX: use custom memory region above cpu physical memory addrs
2253 * this will ease porting to 64-bit firewire adapters
2255 err = fw_core_add_address_handler(&port->rx_handler,
2256 &fw_high_memory_region);
2257 if (err) {
2258 kfree(port);
2259 goto free_ports;
2262 /* preserve i for error cleanup */
2264 err = fwtty_ports_add(serial);
2265 if (err) {
2266 fwtty_err(&unit, "no space in port table\n");
2267 goto free_ports;
2270 for (j = 0; j < num_ttys; ++j) {
2271 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2272 fwtty_driver,
2273 serial->ports[j]->index,
2274 card->device);
2275 if (IS_ERR(tty_dev)) {
2276 err = PTR_ERR(tty_dev);
2277 fwtty_err(&unit, "register tty device error (%d)\n",
2278 err);
2279 goto unregister_ttys;
2282 serial->ports[j]->device = tty_dev;
2284 /* preserve j for error cleanup */
2286 if (create_loop_dev) {
2287 struct device *loop_dev;
2289 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2290 fwloop_driver,
2291 loop_idx(serial->ports[j]),
2292 card->device);
2293 if (IS_ERR(loop_dev)) {
2294 err = PTR_ERR(loop_dev);
2295 fwtty_err(&unit, "create loop device failed (%d)\n",
2296 err);
2297 goto unregister_ttys;
2299 serial->ports[j]->device = loop_dev;
2300 serial->ports[j]->loopback = true;
2303 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2304 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2305 fwserial_debugfs);
2306 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2307 debugfs_create_file("peers", 0444, serial->debugfs,
2308 serial, &fwtty_peers_fops);
2309 debugfs_create_file("stats", 0444, serial->debugfs,
2310 serial, &fwtty_stats_fops);
2314 list_add_rcu(&serial->list, &fwserial_list);
2316 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n",
2317 dev_name(card->device), (unsigned long long) card->guid);
2319 err = fwserial_add_peer(serial, unit);
2320 if (!err)
2321 return 0;
2323 fwtty_err(&unit, "unable to add peer unit device (%d)\n", err);
2325 /* fall-through to error processing */
2326 debugfs_remove_recursive(serial->debugfs);
2328 list_del_rcu(&serial->list);
2329 if (create_loop_dev)
2330 tty_unregister_device(fwloop_driver,
2331 loop_idx(serial->ports[j]));
2332 unregister_ttys:
2333 for (--j; j >= 0; --j)
2334 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2335 kref_put(&serial->kref, fwserial_destroy);
2336 return err;
2338 free_ports:
2339 for (--i; i >= 0; --i) {
2340 tty_port_destroy(&serial->ports[i]->port);
2341 kfree(serial->ports[i]);
2343 kfree(serial);
2344 return err;
2348 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2350 * A 'serial' unit device is created and probed as a result of:
2351 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2352 * 'serial' unit specifier id
2353 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2355 * The firewire core registers unit devices by enumerating unit directories
2356 * of a node's config ROM after reading the config ROM when a new node is
2357 * added to the bus topology after a bus reset.
2359 * The practical implications of this are:
2360 * - this probe is called for both local and remote nodes that have a 'serial'
2361 * unit directory in their config ROM (that matches the specifiers in
2362 * fwserial_id_table).
2363 * - no specific order is enforced for local vs. remote unit devices
2365 * This unit driver copes with the lack of specific order in the same way the
2366 * firewire net driver does -- each probe, for either a local or remote unit
2367 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2368 * first peer created for a given fw_card (tracked by the global fwserial_list)
2369 * creates the underlying TTYs (aggregated in a fw_serial instance).
2371 * NB: an early attempt to differentiate local & remote unit devices by creating
2372 * peers only for remote units and fw_serial instances (with their
2373 * associated TTY devices) only for local units was discarded. Managing
2374 * the peer lifetimes on device removal proved too complicated.
2376 * fwserial_probe/fwserial_remove are effectively serialized by the
2377 * fwserial_list_mutex. This is necessary because the addition of the first peer
2378 * for a given fw_card will trigger the creation of the fw_serial for that
2379 * fw_card, which must not simultaneously contend with the removal of the
2380 * last peer for a given fw_card triggering the destruction of the same
2381 * fw_serial for the same fw_card.
2383 static int fwserial_probe(struct fw_unit *unit,
2384 const struct ieee1394_device_id *id)
2386 struct fw_serial *serial;
2387 int err;
2389 mutex_lock(&fwserial_list_mutex);
2390 serial = fwserial_lookup(fw_parent_device(unit)->card);
2391 if (!serial)
2392 err = fwserial_create(unit);
2393 else
2394 err = fwserial_add_peer(serial, unit);
2395 mutex_unlock(&fwserial_list_mutex);
2396 return err;
2400 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2402 * The corresponding 'peer' for this unit device is removed from the list of
2403 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2404 * specific fw_card). If this is the last peer being removed, then trigger
2405 * the destruction of the underlying TTYs.
2407 static void fwserial_remove(struct fw_unit *unit)
2409 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2410 struct fw_serial *serial = peer->serial;
2411 int i;
2413 mutex_lock(&fwserial_list_mutex);
2414 fwserial_remove_peer(peer);
2416 if (list_empty(&serial->peer_list)) {
2417 /* unlink from the fwserial_list here */
2418 list_del_rcu(&serial->list);
2420 debugfs_remove_recursive(serial->debugfs);
2422 for (i = 0; i < num_ttys; ++i)
2423 fwserial_close_port(fwtty_driver, serial->ports[i]);
2424 if (create_loop_dev)
2425 fwserial_close_port(fwloop_driver, serial->ports[i]);
2426 kref_put(&serial->kref, fwserial_destroy);
2428 mutex_unlock(&fwserial_list_mutex);
2432 * fwserial_update: bus update function for 'firewire' serial unit devices
2434 * Updates the new node_id and bus generation for this peer. Note that locking
2435 * is unnecessary; but careful memory barrier usage is important to enforce the
2436 * load and store order of generation & node_id.
2438 * The fw-core orders the write of node_id before generation in the parent
2439 * fw_device to ensure that a stale node_id cannot be used with a current
2440 * bus generation. So the generation value must be read before the node_id.
2442 * In turn, this orders the write of node_id before generation in the peer to
2443 * also ensure a stale node_id cannot be used with a current bus generation.
2445 static void fwserial_update(struct fw_unit *unit)
2447 struct fw_device *parent = fw_parent_device(unit);
2448 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2449 int generation;
2451 generation = parent->generation;
2452 smp_rmb();
2453 peer->node_id = parent->node_id;
2454 smp_wmb();
2455 peer->generation = generation;
2458 static const struct ieee1394_device_id fwserial_id_table[] = {
2460 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2461 IEEE1394_MATCH_VERSION,
2462 .specifier_id = LINUX_VENDOR_ID,
2463 .version = FWSERIAL_VERSION,
2468 static struct fw_driver fwserial_driver = {
2469 .driver = {
2470 .owner = THIS_MODULE,
2471 .name = KBUILD_MODNAME,
2472 .bus = &fw_bus_type,
2474 .probe = fwserial_probe,
2475 .update = fwserial_update,
2476 .remove = fwserial_remove,
2477 .id_table = fwserial_id_table,
2480 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2481 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2482 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2483 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2484 /* XXX: config ROM definitons could be improved with semi-automated offset
2485 * and length calculation
2487 #define FW_ROM_LEN(quads) ((quads) << 16)
2488 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2490 struct fwserial_unit_directory_data {
2491 u32 len_crc;
2492 u32 unit_specifier;
2493 u32 unit_sw_version;
2494 u32 unit_addr_offset;
2495 u32 desc1_ofs;
2496 u32 desc1_len_crc;
2497 u32 desc1_data[5];
2498 } __packed;
2500 static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
2501 .len_crc = FW_ROM_LEN(4),
2502 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2503 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2504 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
2505 .desc1_len_crc = FW_ROM_LEN(5),
2506 .desc1_data = {
2507 0x00000000, /* type = text */
2508 0x00000000, /* enc = ASCII, lang EN */
2509 0x4c696e75, /* 'Linux TTY' */
2510 0x78205454,
2511 0x59000000,
2515 static struct fw_descriptor fwserial_unit_directory = {
2516 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2517 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2518 .data = (u32 *)&fwserial_unit_directory_data,
2522 * The management address is in the unit space region but above other known
2523 * address users (to keep wild writes from causing havoc)
2525 static const struct fw_address_region fwserial_mgmt_addr_region = {
2526 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2527 .end = 0x1000000000000ULL,
2530 static struct fw_address_handler fwserial_mgmt_addr_handler;
2533 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2534 * @work: ptr to peer->work
2536 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2538 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2539 * already sent to this peer. If so, the collision is resolved by comparing
2540 * guid values; the loser sends the plug response.
2542 * Note: if an error prevents a response, don't do anything -- the
2543 * remote will timeout its request.
2545 static void fwserial_handle_plug_req(struct work_struct *work)
2547 struct fwtty_peer *peer = to_peer(work, work);
2548 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2549 struct fwtty_port *port;
2550 struct fwserial_mgmt_pkt *pkt;
2551 int rcode;
2553 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2554 if (!pkt)
2555 return;
2557 port = fwserial_find_port(peer);
2559 spin_lock_bh(&peer->lock);
2561 switch (peer->state) {
2562 case FWPS_NOT_ATTACHED:
2563 if (!port) {
2564 fwtty_err(&peer->unit, "no more ports avail\n");
2565 fill_plug_rsp_nack(pkt);
2566 } else {
2567 peer->port = port;
2568 fill_plug_rsp_ok(pkt, peer->port);
2569 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2570 /* don't release claimed port */
2571 port = NULL;
2573 break;
2575 case FWPS_PLUG_PENDING:
2576 if (peer->serial->card->guid > peer->guid)
2577 goto cleanup;
2579 /* We lost - hijack the already-claimed port and send ok */
2580 del_timer(&peer->timer);
2581 fill_plug_rsp_ok(pkt, peer->port);
2582 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2583 break;
2585 default:
2586 fill_plug_rsp_nack(pkt);
2589 spin_unlock_bh(&peer->lock);
2590 if (port)
2591 fwserial_release_port(port, false);
2593 rcode = fwserial_send_mgmt_sync(peer, pkt);
2595 spin_lock_bh(&peer->lock);
2596 if (peer->state == FWPS_PLUG_RESPONDING) {
2597 if (rcode == RCODE_COMPLETE) {
2598 struct fwtty_port *tmp = peer->port;
2600 fwserial_virt_plug_complete(peer, plug_req);
2601 spin_unlock_bh(&peer->lock);
2603 fwtty_write_port_status(tmp);
2604 spin_lock_bh(&peer->lock);
2605 } else {
2606 fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode);
2607 port = peer_revert_state(peer);
2610 cleanup:
2611 spin_unlock_bh(&peer->lock);
2612 if (port)
2613 fwserial_release_port(port, false);
2614 kfree(pkt);
2617 static void fwserial_handle_unplug_req(struct work_struct *work)
2619 struct fwtty_peer *peer = to_peer(work, work);
2620 struct fwtty_port *port = NULL;
2621 struct fwserial_mgmt_pkt *pkt;
2622 int rcode;
2624 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2625 if (!pkt)
2626 return;
2628 spin_lock_bh(&peer->lock);
2630 switch (peer->state) {
2631 case FWPS_ATTACHED:
2632 fill_unplug_rsp_ok(pkt);
2633 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2634 break;
2636 case FWPS_UNPLUG_PENDING:
2637 if (peer->serial->card->guid > peer->guid)
2638 goto cleanup;
2640 /* We lost - send unplug rsp */
2641 del_timer(&peer->timer);
2642 fill_unplug_rsp_ok(pkt);
2643 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2644 break;
2646 default:
2647 fill_unplug_rsp_nack(pkt);
2650 spin_unlock_bh(&peer->lock);
2652 rcode = fwserial_send_mgmt_sync(peer, pkt);
2654 spin_lock_bh(&peer->lock);
2655 if (peer->state == FWPS_UNPLUG_RESPONDING) {
2656 if (rcode != RCODE_COMPLETE)
2657 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n",
2658 rcode);
2659 port = peer_revert_state(peer);
2661 cleanup:
2662 spin_unlock_bh(&peer->lock);
2663 if (port)
2664 fwserial_release_port(port, true);
2665 kfree(pkt);
2668 static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2669 struct fwserial_mgmt_pkt *pkt,
2670 unsigned long long addr,
2671 size_t len)
2673 struct fwtty_port *port = NULL;
2674 bool reset = false;
2675 int rcode;
2677 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2678 return RCODE_ADDRESS_ERROR;
2680 if (len != be16_to_cpu(pkt->hdr.len) ||
2681 len != mgmt_pkt_expected_len(pkt->hdr.code))
2682 return RCODE_DATA_ERROR;
2684 spin_lock_bh(&peer->lock);
2685 if (peer->state == FWPS_GONE) {
2687 * This should never happen - it would mean that the
2688 * remote unit that just wrote this transaction was
2689 * already removed from the bus -- and the removal was
2690 * processed before we rec'd this transaction
2692 fwtty_err(&peer->unit, "peer already removed\n");
2693 spin_unlock_bh(&peer->lock);
2694 return RCODE_ADDRESS_ERROR;
2697 rcode = RCODE_COMPLETE;
2699 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx\n", pkt->hdr.code);
2701 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2702 case FWSC_VIRT_CABLE_PLUG:
2703 if (work_pending(&peer->work)) {
2704 fwtty_err(&peer->unit, "plug req: busy\n");
2705 rcode = RCODE_CONFLICT_ERROR;
2707 } else {
2708 peer->work_params.plug_req = pkt->plug_req;
2709 peer->workfn = fwserial_handle_plug_req;
2710 queue_work(system_unbound_wq, &peer->work);
2712 break;
2714 case FWSC_VIRT_CABLE_PLUG_RSP:
2715 if (peer->state != FWPS_PLUG_PENDING) {
2716 rcode = RCODE_CONFLICT_ERROR;
2718 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2719 fwtty_notice(&peer->unit, "NACK plug rsp\n");
2720 port = peer_revert_state(peer);
2722 } else {
2723 struct fwtty_port *tmp = peer->port;
2725 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2726 spin_unlock_bh(&peer->lock);
2728 fwtty_write_port_status(tmp);
2729 spin_lock_bh(&peer->lock);
2731 break;
2733 case FWSC_VIRT_CABLE_UNPLUG:
2734 if (work_pending(&peer->work)) {
2735 fwtty_err(&peer->unit, "unplug req: busy\n");
2736 rcode = RCODE_CONFLICT_ERROR;
2737 } else {
2738 peer->workfn = fwserial_handle_unplug_req;
2739 queue_work(system_unbound_wq, &peer->work);
2741 break;
2743 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2744 if (peer->state != FWPS_UNPLUG_PENDING) {
2745 rcode = RCODE_CONFLICT_ERROR;
2746 } else {
2747 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2748 fwtty_notice(&peer->unit, "NACK unplug?\n");
2749 port = peer_revert_state(peer);
2750 reset = true;
2752 break;
2754 default:
2755 fwtty_err(&peer->unit, "unknown mgmt code %d\n",
2756 be16_to_cpu(pkt->hdr.code));
2757 rcode = RCODE_DATA_ERROR;
2759 spin_unlock_bh(&peer->lock);
2761 if (port)
2762 fwserial_release_port(port, reset);
2764 return rcode;
2768 * fwserial_mgmt_handler: bus address handler for mgmt requests
2769 * @parameters: fw_address_callback_t as specified by firewire core interface
2771 * This handler is responsible for handling virtual cable requests from remotes
2772 * for all cards.
2774 static void fwserial_mgmt_handler(struct fw_card *card,
2775 struct fw_request *request,
2776 int tcode, int destination, int source,
2777 int generation,
2778 unsigned long long addr,
2779 void *data, size_t len,
2780 void *callback_data)
2782 struct fwserial_mgmt_pkt *pkt = data;
2783 struct fwtty_peer *peer;
2784 int rcode;
2786 rcu_read_lock();
2787 peer = __fwserial_peer_by_node_id(card, generation, source);
2788 if (!peer) {
2789 fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source);
2790 __dump_peer_list(card);
2791 rcode = RCODE_CONFLICT_ERROR;
2793 } else {
2794 switch (tcode) {
2795 case TCODE_WRITE_BLOCK_REQUEST:
2796 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2797 break;
2799 default:
2800 rcode = RCODE_TYPE_ERROR;
2804 rcu_read_unlock();
2805 fw_send_response(card, request, rcode);
2808 static int __init fwserial_init(void)
2810 int err, num_loops = !!(create_loop_dev);
2812 /* XXX: placeholder for a "firewire" debugfs node */
2813 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2815 /* num_ttys/num_ports must not be set above the static alloc avail */
2816 if (num_ttys + num_loops > MAX_CARD_PORTS)
2817 num_ttys = MAX_CARD_PORTS - num_loops;
2819 num_ports = num_ttys + num_loops;
2821 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2822 | TTY_DRIVER_DYNAMIC_DEV);
2823 if (IS_ERR(fwtty_driver)) {
2824 err = PTR_ERR(fwtty_driver);
2825 goto remove_debugfs;
2828 fwtty_driver->driver_name = KBUILD_MODNAME;
2829 fwtty_driver->name = tty_dev_name;
2830 fwtty_driver->major = 0;
2831 fwtty_driver->minor_start = 0;
2832 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2833 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
2834 fwtty_driver->init_termios = tty_std_termios;
2835 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2836 tty_set_operations(fwtty_driver, &fwtty_ops);
2838 err = tty_register_driver(fwtty_driver);
2839 if (err) {
2840 pr_err("register tty driver failed (%d)\n", err);
2841 goto put_tty;
2844 if (create_loop_dev) {
2845 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2846 TTY_DRIVER_REAL_RAW
2847 | TTY_DRIVER_DYNAMIC_DEV);
2848 if (IS_ERR(fwloop_driver)) {
2849 err = PTR_ERR(fwloop_driver);
2850 goto unregister_driver;
2853 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2854 fwloop_driver->name = loop_dev_name;
2855 fwloop_driver->major = 0;
2856 fwloop_driver->minor_start = 0;
2857 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2858 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
2859 fwloop_driver->init_termios = tty_std_termios;
2860 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2861 tty_set_operations(fwloop_driver, &fwloop_ops);
2863 err = tty_register_driver(fwloop_driver);
2864 if (err) {
2865 pr_err("register loop driver failed (%d)\n", err);
2866 goto put_loop;
2870 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2871 sizeof(struct fwtty_transaction),
2872 0, 0, fwtty_txn_constructor);
2873 if (!fwtty_txn_cache) {
2874 err = -ENOMEM;
2875 goto unregister_loop;
2879 * Ideally, this address handler would be registered per local node
2880 * (rather than the same handler for all local nodes). However,
2881 * since the firewire core requires the config rom descriptor *before*
2882 * the local unit device(s) are created, a single management handler
2883 * must suffice for all local serial units.
2885 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2886 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2888 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2889 &fwserial_mgmt_addr_region);
2890 if (err) {
2891 pr_err("add management handler failed (%d)\n", err);
2892 goto destroy_cache;
2895 fwserial_unit_directory_data.unit_addr_offset =
2896 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2897 err = fw_core_add_descriptor(&fwserial_unit_directory);
2898 if (err) {
2899 pr_err("add unit descriptor failed (%d)\n", err);
2900 goto remove_handler;
2903 err = driver_register(&fwserial_driver.driver);
2904 if (err) {
2905 pr_err("register fwserial driver failed (%d)\n", err);
2906 goto remove_descriptor;
2909 return 0;
2911 remove_descriptor:
2912 fw_core_remove_descriptor(&fwserial_unit_directory);
2913 remove_handler:
2914 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2915 destroy_cache:
2916 kmem_cache_destroy(fwtty_txn_cache);
2917 unregister_loop:
2918 if (create_loop_dev)
2919 tty_unregister_driver(fwloop_driver);
2920 put_loop:
2921 if (create_loop_dev)
2922 put_tty_driver(fwloop_driver);
2923 unregister_driver:
2924 tty_unregister_driver(fwtty_driver);
2925 put_tty:
2926 put_tty_driver(fwtty_driver);
2927 remove_debugfs:
2928 debugfs_remove_recursive(fwserial_debugfs);
2930 return err;
2933 static void __exit fwserial_exit(void)
2935 driver_unregister(&fwserial_driver.driver);
2936 fw_core_remove_descriptor(&fwserial_unit_directory);
2937 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2938 kmem_cache_destroy(fwtty_txn_cache);
2939 if (create_loop_dev) {
2940 tty_unregister_driver(fwloop_driver);
2941 put_tty_driver(fwloop_driver);
2943 tty_unregister_driver(fwtty_driver);
2944 put_tty_driver(fwtty_driver);
2945 debugfs_remove_recursive(fwserial_debugfs);
2948 module_init(fwserial_init);
2949 module_exit(fwserial_exit);
2951 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2952 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2953 MODULE_LICENSE("GPL");
2954 MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
2955 MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
2956 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2957 MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");