1 // SPDX-License-Identifier: GPL-2.0+
3 * FireWire Serial driver
5 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/rculist.h>
15 #include <linux/workqueue.h>
16 #include <linux/ratelimit.h>
17 #include <linux/bug.h>
18 #include <linux/uaccess.h>
22 #define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
24 #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
25 #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
27 /* configurable options */
28 static int num_ttys
= 4; /* # of std ttys to create per fw_card */
29 /* - doubles as loopback port index */
30 static bool auto_connect
= true; /* try to VIRT_CABLE to every peer */
31 static bool create_loop_dev
= true; /* create a loopback device for each card */
33 module_param_named(ttys
, num_ttys
, int, 0644);
34 module_param_named(auto, auto_connect
, bool, 0644);
35 module_param_named(loop
, create_loop_dev
, bool, 0644);
38 * Threshold below which the tty is woken for writing
39 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
40 * even if the writer is woken, n_tty_poll() won't set EPOLLOUT until
41 * our fifo is below this level
43 #define WAKEUP_CHARS 256
46 * fwserial_list: list of every fw_serial created for each fw_card
47 * See discussion in fwserial_probe.
49 static LIST_HEAD(fwserial_list
);
50 static DEFINE_MUTEX(fwserial_list_mutex
);
53 * port_table: array of tty ports allocated to each fw_card
55 * tty ports are allocated during probe when an fw_serial is first
56 * created for a given fw_card. Ports are allocated in a contiguous block,
57 * each block consisting of 'num_ports' ports.
59 static struct fwtty_port
*port_table
[MAX_TOTAL_PORTS
];
60 static DEFINE_MUTEX(port_table_lock
);
61 static bool port_table_corrupt
;
62 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
64 #define loop_idx(port) (((port)->index) / num_ports)
65 #define table_idx(loop) ((loop) * num_ports + num_ttys)
67 /* total # of tty ports created per fw_card */
70 /* slab used as pool for struct fwtty_transactions */
71 static struct kmem_cache
*fwtty_txn_cache
;
73 struct tty_driver
*fwtty_driver
;
74 static struct tty_driver
*fwloop_driver
;
76 static struct dentry
*fwserial_debugfs
;
78 struct fwtty_transaction
;
79 typedef void (*fwtty_transaction_cb
)(struct fw_card
*card
, int rcode
,
80 void *data
, size_t length
,
81 struct fwtty_transaction
*txn
);
83 struct fwtty_transaction
{
84 struct fw_transaction fw_txn
;
85 fwtty_transaction_cb callback
;
86 struct fwtty_port
*port
;
88 struct dma_pending dma_pended
;
92 #define to_device(a, b) (a->b)
93 #define fwtty_err(p, fmt, ...) \
94 dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
95 #define fwtty_info(p, fmt, ...) \
96 dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
97 #define fwtty_notice(p, fmt, ...) \
98 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
99 #define fwtty_dbg(p, fmt, ...) \
100 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
101 #define fwtty_err_ratelimited(p, fmt, ...) \
102 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
105 static inline void debug_short_write(struct fwtty_port
*port
, int c
, int n
)
110 spin_lock_bh(&port
->lock
);
111 avail
= dma_fifo_avail(&port
->tx_fifo
);
112 spin_unlock_bh(&port
->lock
);
113 fwtty_dbg(port
, "short write: avail:%d req:%d wrote:%d\n",
118 #define debug_short_write(port, c, n)
121 static struct fwtty_peer
*__fwserial_peer_by_node_id(struct fw_card
*card
,
122 int generation
, int id
);
124 #ifdef FWTTY_PROFILING
126 static void fwtty_profile_fifo(struct fwtty_port
*port
, unsigned int *stat
)
128 spin_lock_bh(&port
->lock
);
129 fwtty_profile_data(stat
, dma_fifo_avail(&port
->tx_fifo
));
130 spin_unlock_bh(&port
->lock
);
133 static void fwtty_dump_profile(struct seq_file
*m
, struct stats
*stats
)
135 /* for each stat, print sum of 0 to 2^k, then individually */
141 snprintf(t
, 10, "< %d", 1 << k
);
142 seq_printf(m
, "\n%14s %6s", " ", t
);
143 for (j
= k
+ 1; j
< DISTRIBUTION_MAX_INDEX
; ++j
)
144 seq_printf(m
, "%6d", 1 << j
);
147 for (j
= 0, sum
= 0; j
<= k
; ++j
)
148 sum
+= stats
->reads
[j
];
149 seq_printf(m
, "\n%14s: %6d", "reads", sum
);
150 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
151 seq_printf(m
, "%6d", stats
->reads
[j
]);
153 for (j
= 0, sum
= 0; j
<= k
; ++j
)
154 sum
+= stats
->writes
[j
];
155 seq_printf(m
, "\n%14s: %6d", "writes", sum
);
156 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
157 seq_printf(m
, "%6d", stats
->writes
[j
]);
159 for (j
= 0, sum
= 0; j
<= k
; ++j
)
160 sum
+= stats
->txns
[j
];
161 seq_printf(m
, "\n%14s: %6d", "txns", sum
);
162 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
163 seq_printf(m
, "%6d", stats
->txns
[j
]);
165 for (j
= 0, sum
= 0; j
<= k
; ++j
)
166 sum
+= stats
->unthrottle
[j
];
167 seq_printf(m
, "\n%14s: %6d", "avail @ unthr", sum
);
168 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
169 seq_printf(m
, "%6d", stats
->unthrottle
[j
]);
173 #define fwtty_profile_fifo(port, stat)
174 #define fwtty_dump_profile(m, stats)
178 * Returns the max receive packet size for the given node
179 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
180 * are required by specification to support max_rec of 8 (512 bytes) or more.
182 static inline int device_max_receive(struct fw_device
*fw_device
)
184 /* see IEEE 1394-2008 table 8-8 */
185 return min(2 << fw_device
->max_rec
, 4096);
188 static void fwtty_log_tx_error(struct fwtty_port
*port
, int rcode
)
191 case RCODE_SEND_ERROR
:
192 fwtty_err_ratelimited(port
, "card busy\n");
194 case RCODE_ADDRESS_ERROR
:
195 fwtty_err_ratelimited(port
, "bad unit addr or write length\n");
197 case RCODE_DATA_ERROR
:
198 fwtty_err_ratelimited(port
, "failed rx\n");
201 fwtty_err_ratelimited(port
, "missing ack\n");
204 fwtty_err_ratelimited(port
, "remote busy\n");
207 fwtty_err_ratelimited(port
, "failed tx: %d\n", rcode
);
211 static void fwtty_common_callback(struct fw_card
*card
, int rcode
,
212 void *payload
, size_t len
, void *cb_data
)
214 struct fwtty_transaction
*txn
= cb_data
;
215 struct fwtty_port
*port
= txn
->port
;
217 if (port
&& rcode
!= RCODE_COMPLETE
)
218 fwtty_log_tx_error(port
, rcode
);
220 txn
->callback(card
, rcode
, payload
, len
, txn
);
221 kmem_cache_free(fwtty_txn_cache
, txn
);
224 static int fwtty_send_data_async(struct fwtty_peer
*peer
, int tcode
,
225 unsigned long long addr
, void *payload
,
226 size_t len
, fwtty_transaction_cb callback
,
227 struct fwtty_port
*port
)
229 struct fwtty_transaction
*txn
;
232 txn
= kmem_cache_alloc(fwtty_txn_cache
, GFP_ATOMIC
);
236 txn
->callback
= callback
;
239 generation
= peer
->generation
;
241 fw_send_request(peer
->serial
->card
, &txn
->fw_txn
, tcode
,
242 peer
->node_id
, generation
, peer
->speed
, addr
, payload
,
243 len
, fwtty_common_callback
, txn
);
247 static void fwtty_send_txn_async(struct fwtty_peer
*peer
,
248 struct fwtty_transaction
*txn
, int tcode
,
249 unsigned long long addr
, void *payload
,
250 size_t len
, fwtty_transaction_cb callback
,
251 struct fwtty_port
*port
)
255 txn
->callback
= callback
;
258 generation
= peer
->generation
;
260 fw_send_request(peer
->serial
->card
, &txn
->fw_txn
, tcode
,
261 peer
->node_id
, generation
, peer
->speed
, addr
, payload
,
262 len
, fwtty_common_callback
, txn
);
265 static void __fwtty_restart_tx(struct fwtty_port
*port
)
269 len
= dma_fifo_out_level(&port
->tx_fifo
);
271 schedule_delayed_work(&port
->drain
, 0);
272 avail
= dma_fifo_avail(&port
->tx_fifo
);
274 fwtty_dbg(port
, "fifo len: %d avail: %d\n", len
, avail
);
277 static void fwtty_restart_tx(struct fwtty_port
*port
)
279 spin_lock_bh(&port
->lock
);
280 __fwtty_restart_tx(port
);
281 spin_unlock_bh(&port
->lock
);
285 * fwtty_update_port_status - decodes & dispatches line status changes
287 * Note: in loopback, the port->lock is being held. Only use functions that
288 * don't attempt to reclaim the port->lock.
290 static void fwtty_update_port_status(struct fwtty_port
*port
,
294 struct tty_struct
*tty
;
296 /* simulated LSR/MSR status from remote */
297 status
&= ~MCTRL_MASK
;
298 delta
= (port
->mstatus
^ status
) & ~MCTRL_MASK
;
299 delta
&= ~(status
& TIOCM_RNG
);
300 port
->mstatus
= status
;
302 if (delta
& TIOCM_RNG
)
304 if (delta
& TIOCM_DSR
)
306 if (delta
& TIOCM_CAR
)
308 if (delta
& TIOCM_CTS
)
311 fwtty_dbg(port
, "status: %x delta: %x\n", status
, delta
);
313 if (delta
& TIOCM_CAR
) {
314 tty
= tty_port_tty_get(&port
->port
);
315 if (tty
&& !C_CLOCAL(tty
)) {
316 if (status
& TIOCM_CAR
)
317 wake_up_interruptible(&port
->port
.open_wait
);
319 schedule_work(&port
->hangup
);
324 if (delta
& TIOCM_CTS
) {
325 tty
= tty_port_tty_get(&port
->port
);
326 if (tty
&& C_CRTSCTS(tty
)) {
327 if (tty
->hw_stopped
) {
328 if (status
& TIOCM_CTS
) {
331 __fwtty_restart_tx(port
);
333 fwtty_restart_tx(port
);
336 if (~status
& TIOCM_CTS
)
342 } else if (delta
& OOB_TX_THROTTLE
) {
343 tty
= tty_port_tty_get(&port
->port
);
345 if (tty
->hw_stopped
) {
346 if (~status
& OOB_TX_THROTTLE
) {
349 __fwtty_restart_tx(port
);
351 fwtty_restart_tx(port
);
354 if (status
& OOB_TX_THROTTLE
)
361 if (delta
& (UART_LSR_BI
<< 24)) {
362 if (status
& (UART_LSR_BI
<< 24)) {
363 port
->break_last
= jiffies
;
364 schedule_delayed_work(&port
->emit_breaks
, 0);
366 /* run emit_breaks one last time (if pending) */
367 mod_delayed_work(system_wq
, &port
->emit_breaks
, 0);
371 if (delta
& (TIOCM_DSR
| TIOCM_CAR
| TIOCM_CTS
| TIOCM_RNG
))
372 wake_up_interruptible(&port
->port
.delta_msr_wait
);
376 * __fwtty_port_line_status - generate 'line status' for indicated port
378 * This function returns a remote 'MSR' state based on the local 'MCR' state,
379 * as if a null modem cable was attached. The actual status is a mangling
380 * of TIOCM_* bits suitable for sending to a peer's status_addr.
382 * Note: caller must be holding port lock
384 static unsigned int __fwtty_port_line_status(struct fwtty_port
*port
)
386 unsigned int status
= 0;
388 /* TODO: add module param to tie RNG to DTR as well */
390 if (port
->mctrl
& TIOCM_DTR
)
391 status
|= TIOCM_DSR
| TIOCM_CAR
;
392 if (port
->mctrl
& TIOCM_RTS
)
394 if (port
->mctrl
& OOB_RX_THROTTLE
)
395 status
|= OOB_TX_THROTTLE
;
396 /* emulate BRK as add'l line status */
398 status
|= UART_LSR_BI
<< 24;
404 * __fwtty_write_port_status - send the port line status to peer
406 * Note: caller must be holding the port lock.
408 static int __fwtty_write_port_status(struct fwtty_port
*port
)
410 struct fwtty_peer
*peer
;
412 unsigned int status
= __fwtty_port_line_status(port
);
415 peer
= rcu_dereference(port
->peer
);
417 err
= fwtty_send_data_async(peer
, TCODE_WRITE_QUADLET_REQUEST
,
418 peer
->status_addr
, &status
,
419 sizeof(status
), NULL
, port
);
427 * fwtty_write_port_status - same as above but locked by port lock
429 static int fwtty_write_port_status(struct fwtty_port
*port
)
433 spin_lock_bh(&port
->lock
);
434 err
= __fwtty_write_port_status(port
);
435 spin_unlock_bh(&port
->lock
);
439 static void fwtty_throttle_port(struct fwtty_port
*port
)
441 struct tty_struct
*tty
;
444 tty
= tty_port_tty_get(&port
->port
);
448 spin_lock_bh(&port
->lock
);
451 port
->mctrl
|= OOB_RX_THROTTLE
;
453 port
->mctrl
&= ~TIOCM_RTS
;
454 if (~old
& OOB_RX_THROTTLE
)
455 __fwtty_write_port_status(port
);
457 spin_unlock_bh(&port
->lock
);
463 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
465 * When the remote has finished tx, and all in-flight rx has been received and
466 * and pushed to the flip buffer, the remote may close its device. This will
467 * drop DTR on the remote which will drop carrier here. Typically, the tty is
468 * hung up when carrier is dropped or lost.
470 * However, there is a race between the hang up and the line discipline
471 * delivering its data to the reader. A hangup will cause the ldisc to flush
472 * (ie., clear) the read buffer and flip buffer. Because of firewire's
473 * relatively high throughput, the ldisc frequently lags well behind the driver,
474 * resulting in lost data (which has already been received and written to
475 * the flip buffer) when the remote closes its end.
477 * Unfortunately, since the flip buffer offers no direct method for determining
478 * if it holds data, ensuring the ldisc has delivered all data is problematic.
481 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
482 static void fwtty_do_hangup(struct work_struct
*work
)
484 struct fwtty_port
*port
= to_port(work
, hangup
);
485 struct tty_struct
*tty
;
487 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
489 tty
= tty_port_tty_get(&port
->port
);
495 static void fwtty_emit_breaks(struct work_struct
*work
)
497 struct fwtty_port
*port
= to_port(to_delayed_work(work
), emit_breaks
);
498 static const char buf
[16];
499 unsigned long now
= jiffies
;
500 unsigned long elapsed
= now
- port
->break_last
;
501 int n
, t
, c
, brk
= 0;
503 /* generate breaks at the line rate (but at least 1) */
504 n
= (elapsed
* port
->cps
) / HZ
+ 1;
505 port
->break_last
= now
;
507 fwtty_dbg(port
, "sending %d brks\n", n
);
511 c
= tty_insert_flip_string_fixed_flag(&port
->port
, buf
,
518 tty_flip_buffer_push(&port
->port
);
520 if (port
->mstatus
& (UART_LSR_BI
<< 24))
521 schedule_delayed_work(&port
->emit_breaks
, FREQ_BREAKS
);
522 port
->icount
.brk
+= brk
;
525 static int fwtty_rx(struct fwtty_port
*port
, unsigned char *data
, size_t len
)
531 fwtty_dbg(port
, "%d\n", n
);
532 fwtty_profile_data(port
->stats
.reads
, n
);
534 if (port
->write_only
) {
539 /* disregard break status; breaks are generated by emit_breaks work */
540 lsr
= (port
->mstatus
>> 24) & ~UART_LSR_BI
;
545 if (lsr
& UART_LSR_OE
)
546 ++port
->icount
.overrun
;
548 lsr
&= port
->status_mask
;
549 if (lsr
& ~port
->ignore_mask
& UART_LSR_OE
) {
550 if (!tty_insert_flip_char(&port
->port
, 0, TTY_OVERRUN
)) {
555 port
->overrun
= false;
557 if (lsr
& port
->ignore_mask
& ~UART_LSR_OE
) {
558 /* TODO: don't drop SAK and Magic SysRq here */
563 c
= tty_insert_flip_string_fixed_flag(&port
->port
, data
, TTY_NORMAL
, n
);
565 tty_flip_buffer_push(&port
->port
);
569 port
->overrun
= true;
571 fwtty_err_ratelimited(port
, "flip buffer overrun\n");
574 /* throttle the sender if remaining flip buffer space has
575 * reached high watermark to avoid losing data which may be
576 * in-flight. Since the AR request context is 32k, that much
577 * data may have _already_ been acked.
579 if (tty_buffer_space_avail(&port
->port
) < HIGH_WATERMARK
)
580 fwtty_throttle_port(port
);
584 port
->icount
.rx
+= len
;
585 port
->stats
.lost
+= n
;
590 * fwtty_port_handler - bus address handler for port reads/writes
591 * @parameters: fw_address_callback_t as specified by firewire core interface
593 * This handler is responsible for handling inbound read/write dma from remotes.
595 static void fwtty_port_handler(struct fw_card
*card
,
596 struct fw_request
*request
,
597 int tcode
, int destination
, int source
,
599 unsigned long long addr
,
600 void *data
, size_t len
,
603 struct fwtty_port
*port
= callback_data
;
604 struct fwtty_peer
*peer
;
608 /* Only accept rx from the peer virtual-cabled to this port */
610 peer
= __fwserial_peer_by_node_id(card
, generation
, source
);
612 if (!peer
|| peer
!= rcu_access_pointer(port
->peer
)) {
613 rcode
= RCODE_ADDRESS_ERROR
;
614 fwtty_err_ratelimited(port
, "ignoring unauthenticated data\n");
619 case TCODE_WRITE_QUADLET_REQUEST
:
620 if (addr
!= port
->rx_handler
.offset
|| len
!= 4) {
621 rcode
= RCODE_ADDRESS_ERROR
;
623 fwtty_update_port_status(port
, *(unsigned int *)data
);
624 rcode
= RCODE_COMPLETE
;
628 case TCODE_WRITE_BLOCK_REQUEST
:
629 if (addr
!= port
->rx_handler
.offset
+ 4 ||
630 len
> port
->rx_handler
.length
- 4) {
631 rcode
= RCODE_ADDRESS_ERROR
;
633 err
= fwtty_rx(port
, data
, len
);
636 rcode
= RCODE_COMPLETE
;
639 rcode
= RCODE_DATA_ERROR
;
642 rcode
= RCODE_CONFLICT_ERROR
;
649 rcode
= RCODE_TYPE_ERROR
;
653 fw_send_response(card
, request
, rcode
);
657 * fwtty_tx_complete - callback for tx dma
658 * @data: ignored, has no meaning for write txns
659 * @length: ignored, has no meaning for write txns
661 * The writer must be woken here if the fifo has been emptied because it
662 * may have slept if chars_in_buffer was != 0
664 static void fwtty_tx_complete(struct fw_card
*card
, int rcode
,
665 void *data
, size_t length
,
666 struct fwtty_transaction
*txn
)
668 struct fwtty_port
*port
= txn
->port
;
671 fwtty_dbg(port
, "rcode: %d\n", rcode
);
675 spin_lock_bh(&port
->lock
);
676 dma_fifo_out_complete(&port
->tx_fifo
, &txn
->dma_pended
);
677 len
= dma_fifo_level(&port
->tx_fifo
);
678 spin_unlock_bh(&port
->lock
);
680 port
->icount
.tx
+= txn
->dma_pended
.len
;
684 /* TODO: implement retries */
685 spin_lock_bh(&port
->lock
);
686 dma_fifo_out_complete(&port
->tx_fifo
, &txn
->dma_pended
);
687 len
= dma_fifo_level(&port
->tx_fifo
);
688 spin_unlock_bh(&port
->lock
);
690 port
->stats
.dropped
+= txn
->dma_pended
.len
;
693 if (len
< WAKEUP_CHARS
)
694 tty_port_tty_wakeup(&port
->port
);
697 static int fwtty_tx(struct fwtty_port
*port
, bool drain
)
699 struct fwtty_peer
*peer
;
700 struct fwtty_transaction
*txn
;
701 struct tty_struct
*tty
;
704 tty
= tty_port_tty_get(&port
->port
);
709 peer
= rcu_dereference(port
->peer
);
715 if (test_and_set_bit(IN_TX
, &port
->flags
)) {
720 /* try to write as many dma transactions out as possible */
722 while (!tty
->stopped
&& !tty
->hw_stopped
&&
723 !test_bit(STOP_TX
, &port
->flags
)) {
724 txn
= kmem_cache_alloc(fwtty_txn_cache
, GFP_ATOMIC
);
730 spin_lock_bh(&port
->lock
);
731 n
= dma_fifo_out_pend(&port
->tx_fifo
, &txn
->dma_pended
);
732 spin_unlock_bh(&port
->lock
);
734 fwtty_dbg(port
, "out: %u rem: %d\n", txn
->dma_pended
.len
, n
);
737 kmem_cache_free(fwtty_txn_cache
, txn
);
739 ++port
->stats
.tx_stall
;
740 } else if (n
== -ENODATA
) {
741 fwtty_profile_data(port
->stats
.txns
, 0);
743 ++port
->stats
.fifo_errs
;
744 fwtty_err_ratelimited(port
, "fifo err: %d\n",
750 fwtty_profile_data(port
->stats
.txns
, txn
->dma_pended
.len
);
752 fwtty_send_txn_async(peer
, txn
, TCODE_WRITE_BLOCK_REQUEST
,
753 peer
->fifo_addr
, txn
->dma_pended
.data
,
754 txn
->dma_pended
.len
, fwtty_tx_complete
,
759 * Stop tx if the 'last view' of the fifo is empty or if
760 * this is the writer and there's not enough data to bother
762 if (n
== 0 || (!drain
&& n
< WRITER_MINIMUM
))
766 if (n
>= 0 || n
== -EAGAIN
|| n
== -ENOMEM
|| n
== -ENODATA
) {
767 spin_lock_bh(&port
->lock
);
768 len
= dma_fifo_out_level(&port
->tx_fifo
);
770 unsigned long delay
= (n
== -ENOMEM
) ? HZ
: 1;
772 schedule_delayed_work(&port
->drain
, delay
);
774 len
= dma_fifo_level(&port
->tx_fifo
);
775 spin_unlock_bh(&port
->lock
);
777 /* wakeup the writer */
778 if (drain
&& len
< WAKEUP_CHARS
)
782 clear_bit(IN_TX
, &port
->flags
);
783 wake_up_interruptible(&port
->wait_tx
);
791 static void fwtty_drain_tx(struct work_struct
*work
)
793 struct fwtty_port
*port
= to_port(to_delayed_work(work
), drain
);
795 fwtty_tx(port
, true);
798 static void fwtty_write_xchar(struct fwtty_port
*port
, char ch
)
800 struct fwtty_peer
*peer
;
802 ++port
->stats
.xchars
;
804 fwtty_dbg(port
, "%02x\n", ch
);
807 peer
= rcu_dereference(port
->peer
);
809 fwtty_send_data_async(peer
, TCODE_WRITE_BLOCK_REQUEST
,
810 peer
->fifo_addr
, &ch
, sizeof(ch
),
816 static struct fwtty_port
*fwtty_port_get(unsigned int index
)
818 struct fwtty_port
*port
;
820 if (index
>= MAX_TOTAL_PORTS
)
823 mutex_lock(&port_table_lock
);
824 port
= port_table
[index
];
826 kref_get(&port
->serial
->kref
);
827 mutex_unlock(&port_table_lock
);
831 static int fwtty_ports_add(struct fw_serial
*serial
)
836 if (port_table_corrupt
)
839 mutex_lock(&port_table_lock
);
840 for (i
= 0; i
+ num_ports
<= MAX_TOTAL_PORTS
; i
+= num_ports
) {
841 if (!port_table
[i
]) {
842 for (j
= 0; j
< num_ports
; ++i
, ++j
) {
843 serial
->ports
[j
]->index
= i
;
844 port_table
[i
] = serial
->ports
[j
];
850 mutex_unlock(&port_table_lock
);
854 static void fwserial_destroy(struct kref
*kref
)
856 struct fw_serial
*serial
= to_serial(kref
, kref
);
857 struct fwtty_port
**ports
= serial
->ports
;
858 int j
, i
= ports
[0]->index
;
862 mutex_lock(&port_table_lock
);
863 for (j
= 0; j
< num_ports
; ++i
, ++j
) {
864 port_table_corrupt
|= port_table
[i
] != ports
[j
];
865 WARN_ONCE(port_table_corrupt
, "port_table[%d]: %p != ports[%d]: %p",
866 i
, port_table
[i
], j
, ports
[j
]);
868 port_table
[i
] = NULL
;
870 mutex_unlock(&port_table_lock
);
872 for (j
= 0; j
< num_ports
; ++j
) {
873 fw_core_remove_address_handler(&ports
[j
]->rx_handler
);
874 tty_port_destroy(&ports
[j
]->port
);
880 static void fwtty_port_put(struct fwtty_port
*port
)
882 kref_put(&port
->serial
->kref
, fwserial_destroy
);
885 static void fwtty_port_dtr_rts(struct tty_port
*tty_port
, int on
)
887 struct fwtty_port
*port
= to_port(tty_port
, port
);
889 fwtty_dbg(port
, "on/off: %d\n", on
);
891 spin_lock_bh(&port
->lock
);
892 /* Don't change carrier state if this is a console */
893 if (!port
->port
.console
) {
895 port
->mctrl
|= TIOCM_DTR
| TIOCM_RTS
;
897 port
->mctrl
&= ~(TIOCM_DTR
| TIOCM_RTS
);
900 __fwtty_write_port_status(port
);
901 spin_unlock_bh(&port
->lock
);
905 * fwtty_port_carrier_raised: required tty_port operation
907 * This port operation is polled after a tty has been opened and is waiting for
908 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
910 static int fwtty_port_carrier_raised(struct tty_port
*tty_port
)
912 struct fwtty_port
*port
= to_port(tty_port
, port
);
915 rc
= (port
->mstatus
& TIOCM_CAR
);
917 fwtty_dbg(port
, "%d\n", rc
);
922 static unsigned int set_termios(struct fwtty_port
*port
, struct tty_struct
*tty
)
924 unsigned int baud
, frame
;
926 baud
= tty_termios_baud_rate(&tty
->termios
);
927 tty_termios_encode_baud_rate(&tty
->termios
, baud
, baud
);
929 /* compute bit count of 2 frames */
930 frame
= 12 + ((C_CSTOPB(tty
)) ? 4 : 2) + ((C_PARENB(tty
)) ? 2 : 0);
932 switch (C_CSIZE(tty
)) {
934 frame
-= (C_CSTOPB(tty
)) ? 1 : 0;
947 port
->cps
= (baud
<< 1) / frame
;
949 port
->status_mask
= UART_LSR_OE
;
950 if (_I_FLAG(tty
, BRKINT
| PARMRK
))
951 port
->status_mask
|= UART_LSR_BI
;
953 port
->ignore_mask
= 0;
955 port
->ignore_mask
|= UART_LSR_BI
;
957 port
->ignore_mask
|= UART_LSR_OE
;
960 port
->write_only
= !C_CREAD(tty
);
962 /* turn off echo and newline xlat if loopback */
963 if (port
->loopback
) {
964 tty
->termios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHOKE
|
965 ECHONL
| ECHOPRT
| ECHOCTL
);
966 tty
->termios
.c_oflag
&= ~ONLCR
;
972 static int fwtty_port_activate(struct tty_port
*tty_port
,
973 struct tty_struct
*tty
)
975 struct fwtty_port
*port
= to_port(tty_port
, port
);
979 set_bit(TTY_IO_ERROR
, &tty
->flags
);
981 err
= dma_fifo_alloc(&port
->tx_fifo
, FWTTY_PORT_TXFIFO_LEN
,
984 FWTTY_PORT_MAX_PEND_DMA
,
989 spin_lock_bh(&port
->lock
);
991 baud
= set_termios(port
, tty
);
993 /* if console, don't change carrier state */
994 if (!port
->port
.console
) {
997 port
->mctrl
= TIOCM_DTR
| TIOCM_RTS
;
1000 if (C_CRTSCTS(tty
) && ~port
->mstatus
& TIOCM_CTS
)
1001 tty
->hw_stopped
= 1;
1003 __fwtty_write_port_status(port
);
1004 spin_unlock_bh(&port
->lock
);
1006 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
1012 * fwtty_port_shutdown
1014 * Note: the tty port core ensures this is not the console and
1015 * manages TTY_IO_ERROR properly
1017 static void fwtty_port_shutdown(struct tty_port
*tty_port
)
1019 struct fwtty_port
*port
= to_port(tty_port
, port
);
1021 /* TODO: cancel outstanding transactions */
1023 cancel_delayed_work_sync(&port
->emit_breaks
);
1024 cancel_delayed_work_sync(&port
->drain
);
1026 spin_lock_bh(&port
->lock
);
1028 port
->break_ctl
= 0;
1030 __fwtty_write_port_status(port
);
1031 dma_fifo_free(&port
->tx_fifo
);
1032 spin_unlock_bh(&port
->lock
);
1035 static int fwtty_open(struct tty_struct
*tty
, struct file
*fp
)
1037 struct fwtty_port
*port
= tty
->driver_data
;
1039 return tty_port_open(&port
->port
, tty
, fp
);
1042 static void fwtty_close(struct tty_struct
*tty
, struct file
*fp
)
1044 struct fwtty_port
*port
= tty
->driver_data
;
1046 tty_port_close(&port
->port
, tty
, fp
);
1049 static void fwtty_hangup(struct tty_struct
*tty
)
1051 struct fwtty_port
*port
= tty
->driver_data
;
1053 tty_port_hangup(&port
->port
);
1056 static void fwtty_cleanup(struct tty_struct
*tty
)
1058 struct fwtty_port
*port
= tty
->driver_data
;
1060 tty
->driver_data
= NULL
;
1061 fwtty_port_put(port
);
1064 static int fwtty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1066 struct fwtty_port
*port
= fwtty_port_get(tty
->index
);
1069 err
= tty_standard_install(driver
, tty
);
1071 tty
->driver_data
= port
;
1073 fwtty_port_put(port
);
1077 static int fwloop_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1079 struct fwtty_port
*port
= fwtty_port_get(table_idx(tty
->index
));
1082 err
= tty_standard_install(driver
, tty
);
1084 tty
->driver_data
= port
;
1086 fwtty_port_put(port
);
1090 static int fwtty_write(struct tty_struct
*tty
, const unsigned char *buf
, int c
)
1092 struct fwtty_port
*port
= tty
->driver_data
;
1095 fwtty_dbg(port
, "%d\n", c
);
1096 fwtty_profile_data(port
->stats
.writes
, c
);
1098 spin_lock_bh(&port
->lock
);
1099 n
= dma_fifo_in(&port
->tx_fifo
, buf
, c
);
1100 len
= dma_fifo_out_level(&port
->tx_fifo
);
1101 if (len
< DRAIN_THRESHOLD
)
1102 schedule_delayed_work(&port
->drain
, 1);
1103 spin_unlock_bh(&port
->lock
);
1105 if (len
>= DRAIN_THRESHOLD
)
1106 fwtty_tx(port
, false);
1108 debug_short_write(port
, c
, n
);
1110 return (n
< 0) ? 0 : n
;
1113 static int fwtty_write_room(struct tty_struct
*tty
)
1115 struct fwtty_port
*port
= tty
->driver_data
;
1118 spin_lock_bh(&port
->lock
);
1119 n
= dma_fifo_avail(&port
->tx_fifo
);
1120 spin_unlock_bh(&port
->lock
);
1122 fwtty_dbg(port
, "%d\n", n
);
1127 static int fwtty_chars_in_buffer(struct tty_struct
*tty
)
1129 struct fwtty_port
*port
= tty
->driver_data
;
1132 spin_lock_bh(&port
->lock
);
1133 n
= dma_fifo_level(&port
->tx_fifo
);
1134 spin_unlock_bh(&port
->lock
);
1136 fwtty_dbg(port
, "%d\n", n
);
1141 static void fwtty_send_xchar(struct tty_struct
*tty
, char ch
)
1143 struct fwtty_port
*port
= tty
->driver_data
;
1145 fwtty_dbg(port
, "%02x\n", ch
);
1147 fwtty_write_xchar(port
, ch
);
1150 static void fwtty_throttle(struct tty_struct
*tty
)
1152 struct fwtty_port
*port
= tty
->driver_data
;
1155 * Ignore throttling (but not unthrottling).
1156 * It only makes sense to throttle when data will no longer be
1157 * accepted by the tty flip buffer. For example, it is
1158 * possible for received data to overflow the tty buffer long
1159 * before the line discipline ever has a chance to throttle the driver.
1160 * Additionally, the driver may have already completed the I/O
1161 * but the tty buffer is still emptying, so the line discipline is
1162 * throttling and unthrottling nothing.
1165 ++port
->stats
.throttled
;
1168 static void fwtty_unthrottle(struct tty_struct
*tty
)
1170 struct fwtty_port
*port
= tty
->driver_data
;
1172 fwtty_dbg(port
, "CRTSCTS: %d\n", C_CRTSCTS(tty
) != 0);
1174 fwtty_profile_fifo(port
, port
->stats
.unthrottle
);
1176 spin_lock_bh(&port
->lock
);
1177 port
->mctrl
&= ~OOB_RX_THROTTLE
;
1179 port
->mctrl
|= TIOCM_RTS
;
1180 __fwtty_write_port_status(port
);
1181 spin_unlock_bh(&port
->lock
);
1184 static int check_msr_delta(struct fwtty_port
*port
, unsigned long mask
,
1185 struct async_icount
*prev
)
1187 struct async_icount now
;
1192 delta
= ((mask
& TIOCM_RNG
&& prev
->rng
!= now
.rng
) ||
1193 (mask
& TIOCM_DSR
&& prev
->dsr
!= now
.dsr
) ||
1194 (mask
& TIOCM_CAR
&& prev
->dcd
!= now
.dcd
) ||
1195 (mask
& TIOCM_CTS
&& prev
->cts
!= now
.cts
));
1202 static int wait_msr_change(struct fwtty_port
*port
, unsigned long mask
)
1204 struct async_icount prev
;
1206 prev
= port
->icount
;
1208 return wait_event_interruptible(port
->port
.delta_msr_wait
,
1209 check_msr_delta(port
, mask
, &prev
));
1212 static int get_serial_info(struct fwtty_port
*port
,
1213 struct serial_struct __user
*info
)
1215 struct serial_struct tmp
;
1217 memset(&tmp
, 0, sizeof(tmp
));
1219 tmp
.type
= PORT_UNKNOWN
;
1220 tmp
.line
= port
->port
.tty
->index
;
1221 tmp
.flags
= port
->port
.flags
;
1222 tmp
.xmit_fifo_size
= FWTTY_PORT_TXFIFO_LEN
;
1223 tmp
.baud_base
= 400000000;
1224 tmp
.close_delay
= port
->port
.close_delay
;
1226 return (copy_to_user(info
, &tmp
, sizeof(*info
))) ? -EFAULT
: 0;
1229 static int set_serial_info(struct fwtty_port
*port
,
1230 struct serial_struct __user
*info
)
1232 struct serial_struct tmp
;
1234 if (copy_from_user(&tmp
, info
, sizeof(tmp
)))
1237 if (tmp
.irq
!= 0 || tmp
.port
!= 0 || tmp
.custom_divisor
!= 0 ||
1238 tmp
.baud_base
!= 400000000)
1241 if (!capable(CAP_SYS_ADMIN
)) {
1242 if (((tmp
.flags
& ~ASYNC_USR_MASK
) !=
1243 (port
->port
.flags
& ~ASYNC_USR_MASK
)))
1246 port
->port
.close_delay
= tmp
.close_delay
* HZ
/ 100;
1252 static int fwtty_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
1255 struct fwtty_port
*port
= tty
->driver_data
;
1260 mutex_lock(&port
->port
.mutex
);
1261 err
= get_serial_info(port
, (void __user
*)arg
);
1262 mutex_unlock(&port
->port
.mutex
);
1266 mutex_lock(&port
->port
.mutex
);
1267 err
= set_serial_info(port
, (void __user
*)arg
);
1268 mutex_unlock(&port
->port
.mutex
);
1272 err
= wait_msr_change(port
, arg
);
1282 static void fwtty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1284 struct fwtty_port
*port
= tty
->driver_data
;
1287 spin_lock_bh(&port
->lock
);
1288 baud
= set_termios(port
, tty
);
1290 if ((baud
== 0) && (old
->c_cflag
& CBAUD
)) {
1291 port
->mctrl
&= ~(TIOCM_DTR
| TIOCM_RTS
);
1292 } else if ((baud
!= 0) && !(old
->c_cflag
& CBAUD
)) {
1293 if (C_CRTSCTS(tty
) || !tty_throttled(tty
))
1294 port
->mctrl
|= TIOCM_DTR
| TIOCM_RTS
;
1296 port
->mctrl
|= TIOCM_DTR
;
1298 __fwtty_write_port_status(port
);
1299 spin_unlock_bh(&port
->lock
);
1301 if (old
->c_cflag
& CRTSCTS
) {
1302 if (!C_CRTSCTS(tty
)) {
1303 tty
->hw_stopped
= 0;
1304 fwtty_restart_tx(port
);
1306 } else if (C_CRTSCTS(tty
) && ~port
->mstatus
& TIOCM_CTS
) {
1307 tty
->hw_stopped
= 1;
1312 * fwtty_break_ctl - start/stop sending breaks
1314 * Signals the remote to start or stop generating simulated breaks.
1315 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1316 * before signalling the break line status. This guarantees any pending rx will
1317 * be queued to the line discipline before break is simulated on the remote.
1318 * Conversely, turning off break_ctl requires signalling the line status change,
1321 static int fwtty_break_ctl(struct tty_struct
*tty
, int state
)
1323 struct fwtty_port
*port
= tty
->driver_data
;
1326 fwtty_dbg(port
, "%d\n", state
);
1329 set_bit(STOP_TX
, &port
->flags
);
1330 ret
= wait_event_interruptible_timeout(port
->wait_tx
,
1331 !test_bit(IN_TX
, &port
->flags
),
1333 if (ret
== 0 || ret
== -ERESTARTSYS
) {
1334 clear_bit(STOP_TX
, &port
->flags
);
1335 fwtty_restart_tx(port
);
1340 spin_lock_bh(&port
->lock
);
1341 port
->break_ctl
= (state
== -1);
1342 __fwtty_write_port_status(port
);
1343 spin_unlock_bh(&port
->lock
);
1346 spin_lock_bh(&port
->lock
);
1347 dma_fifo_reset(&port
->tx_fifo
);
1348 clear_bit(STOP_TX
, &port
->flags
);
1349 spin_unlock_bh(&port
->lock
);
1354 static int fwtty_tiocmget(struct tty_struct
*tty
)
1356 struct fwtty_port
*port
= tty
->driver_data
;
1359 spin_lock_bh(&port
->lock
);
1360 tiocm
= (port
->mctrl
& MCTRL_MASK
) | (port
->mstatus
& ~MCTRL_MASK
);
1361 spin_unlock_bh(&port
->lock
);
1363 fwtty_dbg(port
, "%x\n", tiocm
);
1368 static int fwtty_tiocmset(struct tty_struct
*tty
,
1369 unsigned int set
, unsigned int clear
)
1371 struct fwtty_port
*port
= tty
->driver_data
;
1373 fwtty_dbg(port
, "set: %x clear: %x\n", set
, clear
);
1375 /* TODO: simulate loopback if TIOCM_LOOP set */
1377 spin_lock_bh(&port
->lock
);
1378 port
->mctrl
&= ~(clear
& MCTRL_MASK
& 0xffff);
1379 port
->mctrl
|= set
& MCTRL_MASK
& 0xffff;
1380 __fwtty_write_port_status(port
);
1381 spin_unlock_bh(&port
->lock
);
1385 static int fwtty_get_icount(struct tty_struct
*tty
,
1386 struct serial_icounter_struct
*icount
)
1388 struct fwtty_port
*port
= tty
->driver_data
;
1391 memcpy(&stats
, &port
->stats
, sizeof(stats
));
1392 if (port
->port
.console
)
1393 (*port
->fwcon_ops
->stats
)(&stats
, port
->con_data
);
1395 icount
->cts
= port
->icount
.cts
;
1396 icount
->dsr
= port
->icount
.dsr
;
1397 icount
->rng
= port
->icount
.rng
;
1398 icount
->dcd
= port
->icount
.dcd
;
1399 icount
->rx
= port
->icount
.rx
;
1400 icount
->tx
= port
->icount
.tx
+ stats
.xchars
;
1401 icount
->frame
= port
->icount
.frame
;
1402 icount
->overrun
= port
->icount
.overrun
;
1403 icount
->parity
= port
->icount
.parity
;
1404 icount
->brk
= port
->icount
.brk
;
1405 icount
->buf_overrun
= port
->icount
.overrun
;
1409 static void fwtty_proc_show_port(struct seq_file
*m
, struct fwtty_port
*port
)
1413 memcpy(&stats
, &port
->stats
, sizeof(stats
));
1414 if (port
->port
.console
)
1415 (*port
->fwcon_ops
->stats
)(&stats
, port
->con_data
);
1417 seq_printf(m
, " addr:%012llx tx:%d rx:%d", port
->rx_handler
.offset
,
1418 port
->icount
.tx
+ stats
.xchars
, port
->icount
.rx
);
1419 seq_printf(m
, " cts:%d dsr:%d rng:%d dcd:%d", port
->icount
.cts
,
1420 port
->icount
.dsr
, port
->icount
.rng
, port
->icount
.dcd
);
1421 seq_printf(m
, " fe:%d oe:%d pe:%d brk:%d", port
->icount
.frame
,
1422 port
->icount
.overrun
, port
->icount
.parity
, port
->icount
.brk
);
1425 static void fwtty_debugfs_show_port(struct seq_file
*m
, struct fwtty_port
*port
)
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
, " dr:%d st:%d err:%d lost:%d", stats
.dropped
,
1434 stats
.tx_stall
, stats
.fifo_errs
, stats
.lost
);
1435 seq_printf(m
, " pkts:%d thr:%d", stats
.sent
, stats
.throttled
);
1437 if (port
->port
.console
) {
1439 (*port
->fwcon_ops
->proc_show
)(m
, port
->con_data
);
1442 fwtty_dump_profile(m
, &port
->stats
);
1445 static void fwtty_debugfs_show_peer(struct seq_file
*m
, struct fwtty_peer
*peer
)
1447 int generation
= peer
->generation
;
1450 seq_printf(m
, " %s:", dev_name(&peer
->unit
->device
));
1451 seq_printf(m
, " node:%04x gen:%d", peer
->node_id
, generation
);
1452 seq_printf(m
, " sp:%d max:%d guid:%016llx", peer
->speed
,
1453 peer
->max_payload
, (unsigned long long)peer
->guid
);
1454 seq_printf(m
, " mgmt:%012llx", (unsigned long long)peer
->mgmt_addr
);
1455 seq_printf(m
, " addr:%012llx", (unsigned long long)peer
->status_addr
);
1459 static int fwtty_proc_show(struct seq_file
*m
, void *v
)
1461 struct fwtty_port
*port
;
1464 seq_puts(m
, "fwserinfo: 1.0 driver: 1.0\n");
1465 for (i
= 0; i
< MAX_TOTAL_PORTS
&& (port
= fwtty_port_get(i
)); ++i
) {
1466 seq_printf(m
, "%2d:", i
);
1467 if (capable(CAP_SYS_ADMIN
))
1468 fwtty_proc_show_port(m
, port
);
1469 fwtty_port_put(port
);
1475 static int fwtty_debugfs_stats_show(struct seq_file
*m
, void *v
)
1477 struct fw_serial
*serial
= m
->private;
1478 struct fwtty_port
*port
;
1481 for (i
= 0; i
< num_ports
; ++i
) {
1482 port
= fwtty_port_get(serial
->ports
[i
]->index
);
1484 seq_printf(m
, "%2d:", port
->index
);
1485 fwtty_proc_show_port(m
, port
);
1486 fwtty_debugfs_show_port(m
, port
);
1487 fwtty_port_put(port
);
1494 static int fwtty_debugfs_peers_show(struct seq_file
*m
, void *v
)
1496 struct fw_serial
*serial
= m
->private;
1497 struct fwtty_peer
*peer
;
1500 seq_printf(m
, "card: %s guid: %016llx\n",
1501 dev_name(serial
->card
->device
),
1502 (unsigned long long)serial
->card
->guid
);
1503 list_for_each_entry_rcu(peer
, &serial
->peer_list
, list
)
1504 fwtty_debugfs_show_peer(m
, peer
);
1509 static int fwtty_stats_open(struct inode
*inode
, struct file
*fp
)
1511 return single_open(fp
, fwtty_debugfs_stats_show
, inode
->i_private
);
1514 static int fwtty_peers_open(struct inode
*inode
, struct file
*fp
)
1516 return single_open(fp
, fwtty_debugfs_peers_show
, inode
->i_private
);
1519 static const struct file_operations fwtty_stats_fops
= {
1520 .owner
= THIS_MODULE
,
1521 .open
= fwtty_stats_open
,
1523 .llseek
= seq_lseek
,
1524 .release
= single_release
,
1527 static const struct file_operations fwtty_peers_fops
= {
1528 .owner
= THIS_MODULE
,
1529 .open
= fwtty_peers_open
,
1531 .llseek
= seq_lseek
,
1532 .release
= single_release
,
1535 static const struct tty_port_operations fwtty_port_ops
= {
1536 .dtr_rts
= fwtty_port_dtr_rts
,
1537 .carrier_raised
= fwtty_port_carrier_raised
,
1538 .shutdown
= fwtty_port_shutdown
,
1539 .activate
= fwtty_port_activate
,
1542 static const struct tty_operations fwtty_ops
= {
1544 .close
= fwtty_close
,
1545 .hangup
= fwtty_hangup
,
1546 .cleanup
= fwtty_cleanup
,
1547 .install
= fwtty_install
,
1548 .write
= fwtty_write
,
1549 .write_room
= fwtty_write_room
,
1550 .chars_in_buffer
= fwtty_chars_in_buffer
,
1551 .send_xchar
= fwtty_send_xchar
,
1552 .throttle
= fwtty_throttle
,
1553 .unthrottle
= fwtty_unthrottle
,
1554 .ioctl
= fwtty_ioctl
,
1555 .set_termios
= fwtty_set_termios
,
1556 .break_ctl
= fwtty_break_ctl
,
1557 .tiocmget
= fwtty_tiocmget
,
1558 .tiocmset
= fwtty_tiocmset
,
1559 .get_icount
= fwtty_get_icount
,
1560 .proc_show
= fwtty_proc_show
,
1563 static const struct tty_operations fwloop_ops
= {
1565 .close
= fwtty_close
,
1566 .hangup
= fwtty_hangup
,
1567 .cleanup
= fwtty_cleanup
,
1568 .install
= fwloop_install
,
1569 .write
= fwtty_write
,
1570 .write_room
= fwtty_write_room
,
1571 .chars_in_buffer
= fwtty_chars_in_buffer
,
1572 .send_xchar
= fwtty_send_xchar
,
1573 .throttle
= fwtty_throttle
,
1574 .unthrottle
= fwtty_unthrottle
,
1575 .ioctl
= fwtty_ioctl
,
1576 .set_termios
= fwtty_set_termios
,
1577 .break_ctl
= fwtty_break_ctl
,
1578 .tiocmget
= fwtty_tiocmget
,
1579 .tiocmset
= fwtty_tiocmset
,
1580 .get_icount
= fwtty_get_icount
,
1583 static inline int mgmt_pkt_expected_len(__be16 code
)
1585 static const struct fwserial_mgmt_pkt pkt
;
1587 switch (be16_to_cpu(code
)) {
1588 case FWSC_VIRT_CABLE_PLUG
:
1589 return sizeof(pkt
.hdr
) + sizeof(pkt
.plug_req
);
1591 case FWSC_VIRT_CABLE_PLUG_RSP
: /* | FWSC_RSP_OK */
1592 return sizeof(pkt
.hdr
) + sizeof(pkt
.plug_rsp
);
1594 case FWSC_VIRT_CABLE_UNPLUG
:
1595 case FWSC_VIRT_CABLE_UNPLUG_RSP
:
1596 case FWSC_VIRT_CABLE_PLUG_RSP
| FWSC_RSP_NACK
:
1597 case FWSC_VIRT_CABLE_UNPLUG_RSP
| FWSC_RSP_NACK
:
1598 return sizeof(pkt
.hdr
);
1605 static inline void fill_plug_params(struct virt_plug_params
*params
,
1606 struct fwtty_port
*port
)
1608 u64 status_addr
= port
->rx_handler
.offset
;
1609 u64 fifo_addr
= port
->rx_handler
.offset
+ 4;
1610 size_t fifo_len
= port
->rx_handler
.length
- 4;
1612 params
->status_hi
= cpu_to_be32(status_addr
>> 32);
1613 params
->status_lo
= cpu_to_be32(status_addr
);
1614 params
->fifo_hi
= cpu_to_be32(fifo_addr
>> 32);
1615 params
->fifo_lo
= cpu_to_be32(fifo_addr
);
1616 params
->fifo_len
= cpu_to_be32(fifo_len
);
1619 static inline void fill_plug_req(struct fwserial_mgmt_pkt
*pkt
,
1620 struct fwtty_port
*port
)
1622 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_PLUG
);
1623 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1624 fill_plug_params(&pkt
->plug_req
, port
);
1627 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt
*pkt
,
1628 struct fwtty_port
*port
)
1630 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP
);
1631 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1632 fill_plug_params(&pkt
->plug_rsp
, port
);
1635 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt
*pkt
)
1637 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP
| FWSC_RSP_NACK
);
1638 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1641 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt
*pkt
)
1643 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP
| FWSC_RSP_NACK
);
1644 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1647 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt
*pkt
)
1649 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP
);
1650 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1653 static void fwserial_virt_plug_complete(struct fwtty_peer
*peer
,
1654 struct virt_plug_params
*params
)
1656 struct fwtty_port
*port
= peer
->port
;
1658 peer
->status_addr
= be32_to_u64(params
->status_hi
, params
->status_lo
);
1659 peer
->fifo_addr
= be32_to_u64(params
->fifo_hi
, params
->fifo_lo
);
1660 peer
->fifo_len
= be32_to_cpu(params
->fifo_len
);
1661 peer_set_state(peer
, FWPS_ATTACHED
);
1663 /* reconfigure tx_fifo optimally for this peer */
1664 spin_lock_bh(&port
->lock
);
1665 port
->max_payload
= min(peer
->max_payload
, peer
->fifo_len
);
1666 dma_fifo_change_tx_limit(&port
->tx_fifo
, port
->max_payload
);
1667 spin_unlock_bh(&peer
->port
->lock
);
1669 if (port
->port
.console
&& port
->fwcon_ops
->notify
)
1670 (*port
->fwcon_ops
->notify
)(FWCON_NOTIFY_ATTACH
, port
->con_data
);
1672 fwtty_info(&peer
->unit
, "peer (guid:%016llx) connected on %s\n",
1673 (unsigned long long)peer
->guid
, dev_name(port
->device
));
1676 static inline int fwserial_send_mgmt_sync(struct fwtty_peer
*peer
,
1677 struct fwserial_mgmt_pkt
*pkt
)
1680 int rcode
, tries
= 5;
1683 generation
= peer
->generation
;
1686 rcode
= fw_run_transaction(peer
->serial
->card
,
1687 TCODE_WRITE_BLOCK_REQUEST
,
1689 generation
, peer
->speed
,
1691 pkt
, be16_to_cpu(pkt
->hdr
.len
));
1692 if (rcode
== RCODE_BUSY
|| rcode
== RCODE_SEND_ERROR
||
1693 rcode
== RCODE_GENERATION
) {
1694 fwtty_dbg(&peer
->unit
, "mgmt write error: %d\n", rcode
);
1699 } while (--tries
> 0);
1704 * fwserial_claim_port - attempt to claim port @ index for peer
1706 * Returns ptr to claimed port or error code (as ERR_PTR())
1707 * Can sleep - must be called from process context
1709 static struct fwtty_port
*fwserial_claim_port(struct fwtty_peer
*peer
,
1712 struct fwtty_port
*port
;
1714 if (index
< 0 || index
>= num_ports
)
1715 return ERR_PTR(-EINVAL
);
1717 /* must guarantee that previous port releases have completed */
1720 port
= peer
->serial
->ports
[index
];
1721 spin_lock_bh(&port
->lock
);
1722 if (!rcu_access_pointer(port
->peer
))
1723 rcu_assign_pointer(port
->peer
, peer
);
1725 port
= ERR_PTR(-EBUSY
);
1726 spin_unlock_bh(&port
->lock
);
1732 * fwserial_find_port - find avail port and claim for peer
1734 * Returns ptr to claimed port or NULL if none avail
1735 * Can sleep - must be called from process context
1737 static struct fwtty_port
*fwserial_find_port(struct fwtty_peer
*peer
)
1739 struct fwtty_port
**ports
= peer
->serial
->ports
;
1742 /* must guarantee that previous port releases have completed */
1745 /* TODO: implement optional GUID-to-specific port # matching */
1747 /* find an unattached port (but not the loopback port, if present) */
1748 for (i
= 0; i
< num_ttys
; ++i
) {
1749 spin_lock_bh(&ports
[i
]->lock
);
1750 if (!ports
[i
]->peer
) {
1752 rcu_assign_pointer(ports
[i
]->peer
, peer
);
1753 spin_unlock_bh(&ports
[i
]->lock
);
1756 spin_unlock_bh(&ports
[i
]->lock
);
1761 static void fwserial_release_port(struct fwtty_port
*port
, bool reset
)
1763 /* drop carrier (and all other line status) */
1765 fwtty_update_port_status(port
, 0);
1767 spin_lock_bh(&port
->lock
);
1769 /* reset dma fifo max transmission size back to S100 */
1770 port
->max_payload
= link_speed_to_max_payload(SCODE_100
);
1771 dma_fifo_change_tx_limit(&port
->tx_fifo
, port
->max_payload
);
1773 RCU_INIT_POINTER(port
->peer
, NULL
);
1774 spin_unlock_bh(&port
->lock
);
1776 if (port
->port
.console
&& port
->fwcon_ops
->notify
)
1777 (*port
->fwcon_ops
->notify
)(FWCON_NOTIFY_DETACH
, port
->con_data
);
1780 static void fwserial_plug_timeout(struct timer_list
*t
)
1782 struct fwtty_peer
*peer
= from_timer(peer
, t
, timer
);
1783 struct fwtty_port
*port
;
1785 spin_lock_bh(&peer
->lock
);
1786 if (peer
->state
!= FWPS_PLUG_PENDING
) {
1787 spin_unlock_bh(&peer
->lock
);
1791 port
= peer_revert_state(peer
);
1792 spin_unlock_bh(&peer
->lock
);
1795 fwserial_release_port(port
, false);
1799 * fwserial_connect_peer - initiate virtual cable with peer
1801 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1802 * otherwise error code. Must be called from process context.
1804 static int fwserial_connect_peer(struct fwtty_peer
*peer
)
1806 struct fwtty_port
*port
;
1807 struct fwserial_mgmt_pkt
*pkt
;
1810 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
1814 port
= fwserial_find_port(peer
);
1816 fwtty_err(&peer
->unit
, "avail ports in use\n");
1821 spin_lock_bh(&peer
->lock
);
1823 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1824 if (peer
->state
!= FWPS_NOT_ATTACHED
) {
1830 peer_set_state(peer
, FWPS_PLUG_PENDING
);
1832 fill_plug_req(pkt
, peer
->port
);
1834 mod_timer(&peer
->timer
, jiffies
+ VIRT_CABLE_PLUG_TIMEOUT
);
1835 spin_unlock_bh(&peer
->lock
);
1837 rcode
= fwserial_send_mgmt_sync(peer
, pkt
);
1839 spin_lock_bh(&peer
->lock
);
1840 if (peer
->state
== FWPS_PLUG_PENDING
&& rcode
!= RCODE_COMPLETE
) {
1841 if (rcode
== RCODE_CONFLICT_ERROR
)
1847 spin_unlock_bh(&peer
->lock
);
1853 del_timer(&peer
->timer
);
1854 peer_revert_state(peer
);
1856 spin_unlock_bh(&peer
->lock
);
1857 fwserial_release_port(port
, false);
1864 * fwserial_close_port -
1865 * HUP the tty (if the tty exists) and unregister the tty device.
1866 * Only used by the unit driver upon unit removal to disconnect and
1867 * cleanup all attached ports
1869 * The port reference is put by fwtty_cleanup (if a reference was
1872 static void fwserial_close_port(struct tty_driver
*driver
,
1873 struct fwtty_port
*port
)
1875 struct tty_struct
*tty
;
1877 mutex_lock(&port
->port
.mutex
);
1878 tty
= tty_port_tty_get(&port
->port
);
1883 mutex_unlock(&port
->port
.mutex
);
1885 if (driver
== fwloop_driver
)
1886 tty_unregister_device(driver
, loop_idx(port
));
1888 tty_unregister_device(driver
, port
->index
);
1892 * fwserial_lookup - finds first fw_serial associated with card
1893 * @card: fw_card to match
1895 * NB: caller must be holding fwserial_list_mutex
1897 static struct fw_serial
*fwserial_lookup(struct fw_card
*card
)
1899 struct fw_serial
*serial
;
1901 list_for_each_entry(serial
, &fwserial_list
, list
) {
1902 if (card
== serial
->card
)
1910 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1911 * @card: fw_card to match
1913 * NB: caller must be inside rcu_read_lock() section
1915 static struct fw_serial
*__fwserial_lookup_rcu(struct fw_card
*card
)
1917 struct fw_serial
*serial
;
1919 list_for_each_entry_rcu(serial
, &fwserial_list
, list
) {
1920 if (card
== serial
->card
)
1928 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1930 * If a matching peer could not be found for the specified generation/node id,
1931 * this could be because:
1932 * a) the generation has changed and one of the nodes hasn't updated yet
1933 * b) the remote node has created its remote unit device before this
1934 * local node has created its corresponding remote unit device
1935 * In either case, the remote node should retry
1937 * Note: caller must be in rcu_read_lock() section
1939 static struct fwtty_peer
*__fwserial_peer_by_node_id(struct fw_card
*card
,
1940 int generation
, int id
)
1942 struct fw_serial
*serial
;
1943 struct fwtty_peer
*peer
;
1945 serial
= __fwserial_lookup_rcu(card
);
1948 * Something is very wrong - there should be a matching
1949 * fw_serial structure for every fw_card. Maybe the remote node
1950 * has created its remote unit device before this driver has
1951 * been probed for any unit devices...
1953 fwtty_err(card
, "unknown card (guid %016llx)\n",
1954 (unsigned long long)card
->guid
);
1958 list_for_each_entry_rcu(peer
, &serial
->peer_list
, list
) {
1959 int g
= peer
->generation
;
1962 if (generation
== g
&& id
== peer
->node_id
)
1970 static void __dump_peer_list(struct fw_card
*card
)
1972 struct fw_serial
*serial
;
1973 struct fwtty_peer
*peer
;
1975 serial
= __fwserial_lookup_rcu(card
);
1979 list_for_each_entry_rcu(peer
, &serial
->peer_list
, list
) {
1980 int g
= peer
->generation
;
1983 fwtty_dbg(card
, "peer(%d:%x) guid: %016llx\n",
1984 g
, peer
->node_id
, (unsigned long long)peer
->guid
);
1988 #define __dump_peer_list(s)
1991 static void fwserial_auto_connect(struct work_struct
*work
)
1993 struct fwtty_peer
*peer
= to_peer(to_delayed_work(work
), connect
);
1996 err
= fwserial_connect_peer(peer
);
1997 if (err
== -EAGAIN
&& ++peer
->connect_retries
< MAX_CONNECT_RETRIES
)
1998 schedule_delayed_work(&peer
->connect
, CONNECT_RETRY_DELAY
);
2001 static void fwserial_peer_workfn(struct work_struct
*work
)
2003 struct fwtty_peer
*peer
= to_peer(work
, work
);
2009 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2010 * @serial: aggregate representing the specific fw_card to add the peer to
2011 * @unit: 'peer' to create and add to peer_list of serial
2013 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2014 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2015 * available tty port. This function is called either directly or indirectly
2016 * as a result of a 'serial' unit device being created & probed.
2018 * Note: this function is serialized with fwserial_remove_peer() by the
2019 * fwserial_list_mutex held in fwserial_probe().
2021 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2022 * via the dev_set_drvdata() for the device of the fw_unit.
2024 static int fwserial_add_peer(struct fw_serial
*serial
, struct fw_unit
*unit
)
2026 struct device
*dev
= &unit
->device
;
2027 struct fw_device
*parent
= fw_parent_device(unit
);
2028 struct fwtty_peer
*peer
;
2029 struct fw_csr_iterator ci
;
2033 peer
= kzalloc(sizeof(*peer
), GFP_KERNEL
);
2037 peer_set_state(peer
, FWPS_NOT_ATTACHED
);
2039 dev_set_drvdata(dev
, peer
);
2041 peer
->guid
= (u64
)parent
->config_rom
[3] << 32 | parent
->config_rom
[4];
2042 peer
->speed
= parent
->max_speed
;
2043 peer
->max_payload
= min(device_max_receive(parent
),
2044 link_speed_to_max_payload(peer
->speed
));
2046 generation
= parent
->generation
;
2048 peer
->node_id
= parent
->node_id
;
2050 peer
->generation
= generation
;
2052 /* retrieve the mgmt bus addr from the unit directory */
2053 fw_csr_iterator_init(&ci
, unit
->directory
);
2054 while (fw_csr_iterator_next(&ci
, &key
, &val
)) {
2055 if (key
== (CSR_OFFSET
| CSR_DEPENDENT_INFO
)) {
2056 peer
->mgmt_addr
= CSR_REGISTER_BASE
+ 4 * val
;
2060 if (peer
->mgmt_addr
== 0ULL) {
2062 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2063 * this peer will not be able to attach to a remote
2065 peer_set_state(peer
, FWPS_NO_MGMT_ADDR
);
2068 spin_lock_init(&peer
->lock
);
2071 timer_setup(&peer
->timer
, fwserial_plug_timeout
, 0);
2072 INIT_WORK(&peer
->work
, fwserial_peer_workfn
);
2073 INIT_DELAYED_WORK(&peer
->connect
, fwserial_auto_connect
);
2075 /* associate peer with specific fw_card */
2076 peer
->serial
= serial
;
2077 list_add_rcu(&peer
->list
, &serial
->peer_list
);
2079 fwtty_info(&peer
->unit
, "peer added (guid:%016llx)\n",
2080 (unsigned long long)peer
->guid
);
2082 /* identify the local unit & virt cable to loopback port */
2083 if (parent
->is_local
) {
2084 serial
->self
= peer
;
2085 if (create_loop_dev
) {
2086 struct fwtty_port
*port
;
2088 port
= fwserial_claim_port(peer
, num_ttys
);
2089 if (!IS_ERR(port
)) {
2090 struct virt_plug_params params
;
2092 spin_lock_bh(&peer
->lock
);
2094 fill_plug_params(¶ms
, port
);
2095 fwserial_virt_plug_complete(peer
, ¶ms
);
2096 spin_unlock_bh(&peer
->lock
);
2098 fwtty_write_port_status(port
);
2102 } else if (auto_connect
) {
2103 /* auto-attach to remote units only (if policy allows) */
2104 schedule_delayed_work(&peer
->connect
, 1);
2111 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2113 * Remove a 'peer' from its list of peers. This function is only
2114 * called by fwserial_remove() on bus removal of the unit device.
2116 * Note: this function is serialized with fwserial_add_peer() by the
2117 * fwserial_list_mutex held in fwserial_remove().
2119 static void fwserial_remove_peer(struct fwtty_peer
*peer
)
2121 struct fwtty_port
*port
;
2123 spin_lock_bh(&peer
->lock
);
2124 peer_set_state(peer
, FWPS_GONE
);
2125 spin_unlock_bh(&peer
->lock
);
2127 cancel_delayed_work_sync(&peer
->connect
);
2128 cancel_work_sync(&peer
->work
);
2130 spin_lock_bh(&peer
->lock
);
2131 /* if this unit is the local unit, clear link */
2132 if (peer
== peer
->serial
->self
)
2133 peer
->serial
->self
= NULL
;
2135 /* cancel the request timeout timer (if running) */
2136 del_timer(&peer
->timer
);
2141 list_del_rcu(&peer
->list
);
2143 fwtty_info(&peer
->unit
, "peer removed (guid:%016llx)\n",
2144 (unsigned long long)peer
->guid
);
2146 spin_unlock_bh(&peer
->lock
);
2149 fwserial_release_port(port
, true);
2156 * fwserial_create - init everything to create TTYs for a specific fw_card
2157 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2159 * This function inits the aggregate structure (an fw_serial instance)
2160 * used to manage the TTY ports registered by a specific fw_card. Also, the
2161 * unit device is added as the first 'peer'.
2163 * This unit device may represent a local unit device (as specified by the
2164 * config ROM unit directory) or it may represent a remote unit device
2165 * (as specified by the reading of the remote node's config ROM).
2167 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2168 * value to indicate which error.
2170 static int fwserial_create(struct fw_unit
*unit
)
2172 struct fw_device
*parent
= fw_parent_device(unit
);
2173 struct fw_card
*card
= parent
->card
;
2174 struct fw_serial
*serial
;
2175 struct fwtty_port
*port
;
2176 struct device
*tty_dev
;
2180 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
2184 kref_init(&serial
->kref
);
2185 serial
->card
= card
;
2186 INIT_LIST_HEAD(&serial
->peer_list
);
2188 for (i
= 0; i
< num_ports
; ++i
) {
2189 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
2194 tty_port_init(&port
->port
);
2195 port
->index
= FWTTY_INVALID_INDEX
;
2196 port
->port
.ops
= &fwtty_port_ops
;
2197 port
->serial
= serial
;
2198 tty_buffer_set_limit(&port
->port
, 128 * 1024);
2200 spin_lock_init(&port
->lock
);
2201 INIT_DELAYED_WORK(&port
->drain
, fwtty_drain_tx
);
2202 INIT_DELAYED_WORK(&port
->emit_breaks
, fwtty_emit_breaks
);
2203 INIT_WORK(&port
->hangup
, fwtty_do_hangup
);
2204 init_waitqueue_head(&port
->wait_tx
);
2205 port
->max_payload
= link_speed_to_max_payload(SCODE_100
);
2206 dma_fifo_init(&port
->tx_fifo
);
2208 RCU_INIT_POINTER(port
->peer
, NULL
);
2209 serial
->ports
[i
] = port
;
2211 /* get unique bus addr region for port's status & recv fifo */
2212 port
->rx_handler
.length
= FWTTY_PORT_RXFIFO_LEN
+ 4;
2213 port
->rx_handler
.address_callback
= fwtty_port_handler
;
2214 port
->rx_handler
.callback_data
= port
;
2216 * XXX: use custom memory region above cpu physical memory addrs
2217 * this will ease porting to 64-bit firewire adapters
2219 err
= fw_core_add_address_handler(&port
->rx_handler
,
2220 &fw_high_memory_region
);
2226 /* preserve i for error cleanup */
2228 err
= fwtty_ports_add(serial
);
2230 fwtty_err(&unit
, "no space in port table\n");
2234 for (j
= 0; j
< num_ttys
; ++j
) {
2235 tty_dev
= tty_port_register_device(&serial
->ports
[j
]->port
,
2237 serial
->ports
[j
]->index
,
2239 if (IS_ERR(tty_dev
)) {
2240 err
= PTR_ERR(tty_dev
);
2241 fwtty_err(&unit
, "register tty device error (%d)\n",
2243 goto unregister_ttys
;
2246 serial
->ports
[j
]->device
= tty_dev
;
2248 /* preserve j for error cleanup */
2250 if (create_loop_dev
) {
2251 struct device
*loop_dev
;
2253 loop_dev
= tty_port_register_device(&serial
->ports
[j
]->port
,
2255 loop_idx(serial
->ports
[j
]),
2257 if (IS_ERR(loop_dev
)) {
2258 err
= PTR_ERR(loop_dev
);
2259 fwtty_err(&unit
, "create loop device failed (%d)\n",
2261 goto unregister_ttys
;
2263 serial
->ports
[j
]->device
= loop_dev
;
2264 serial
->ports
[j
]->loopback
= true;
2267 if (!IS_ERR_OR_NULL(fwserial_debugfs
)) {
2268 serial
->debugfs
= debugfs_create_dir(dev_name(&unit
->device
),
2270 if (!IS_ERR_OR_NULL(serial
->debugfs
)) {
2271 debugfs_create_file("peers", 0444, serial
->debugfs
,
2272 serial
, &fwtty_peers_fops
);
2273 debugfs_create_file("stats", 0444, serial
->debugfs
,
2274 serial
, &fwtty_stats_fops
);
2278 list_add_rcu(&serial
->list
, &fwserial_list
);
2280 fwtty_notice(&unit
, "TTY over FireWire on device %s (guid %016llx)\n",
2281 dev_name(card
->device
), (unsigned long long)card
->guid
);
2283 err
= fwserial_add_peer(serial
, unit
);
2287 fwtty_err(&unit
, "unable to add peer unit device (%d)\n", err
);
2289 /* fall-through to error processing */
2290 debugfs_remove_recursive(serial
->debugfs
);
2292 list_del_rcu(&serial
->list
);
2293 if (create_loop_dev
)
2294 tty_unregister_device(fwloop_driver
,
2295 loop_idx(serial
->ports
[j
]));
2297 for (--j
; j
>= 0; --j
)
2298 tty_unregister_device(fwtty_driver
, serial
->ports
[j
]->index
);
2299 kref_put(&serial
->kref
, fwserial_destroy
);
2303 for (--i
; i
>= 0; --i
) {
2304 tty_port_destroy(&serial
->ports
[i
]->port
);
2305 kfree(serial
->ports
[i
]);
2312 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2314 * A 'serial' unit device is created and probed as a result of:
2315 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2316 * 'serial' unit specifier id
2317 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2319 * The firewire core registers unit devices by enumerating unit directories
2320 * of a node's config ROM after reading the config ROM when a new node is
2321 * added to the bus topology after a bus reset.
2323 * The practical implications of this are:
2324 * - this probe is called for both local and remote nodes that have a 'serial'
2325 * unit directory in their config ROM (that matches the specifiers in
2326 * fwserial_id_table).
2327 * - no specific order is enforced for local vs. remote unit devices
2329 * This unit driver copes with the lack of specific order in the same way the
2330 * firewire net driver does -- each probe, for either a local or remote unit
2331 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2332 * first peer created for a given fw_card (tracked by the global fwserial_list)
2333 * creates the underlying TTYs (aggregated in a fw_serial instance).
2335 * NB: an early attempt to differentiate local & remote unit devices by creating
2336 * peers only for remote units and fw_serial instances (with their
2337 * associated TTY devices) only for local units was discarded. Managing
2338 * the peer lifetimes on device removal proved too complicated.
2340 * fwserial_probe/fwserial_remove are effectively serialized by the
2341 * fwserial_list_mutex. This is necessary because the addition of the first peer
2342 * for a given fw_card will trigger the creation of the fw_serial for that
2343 * fw_card, which must not simultaneously contend with the removal of the
2344 * last peer for a given fw_card triggering the destruction of the same
2345 * fw_serial for the same fw_card.
2347 static int fwserial_probe(struct fw_unit
*unit
,
2348 const struct ieee1394_device_id
*id
)
2350 struct fw_serial
*serial
;
2353 mutex_lock(&fwserial_list_mutex
);
2354 serial
= fwserial_lookup(fw_parent_device(unit
)->card
);
2356 err
= fwserial_create(unit
);
2358 err
= fwserial_add_peer(serial
, unit
);
2359 mutex_unlock(&fwserial_list_mutex
);
2364 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2366 * The corresponding 'peer' for this unit device is removed from the list of
2367 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2368 * specific fw_card). If this is the last peer being removed, then trigger
2369 * the destruction of the underlying TTYs.
2371 static void fwserial_remove(struct fw_unit
*unit
)
2373 struct fwtty_peer
*peer
= dev_get_drvdata(&unit
->device
);
2374 struct fw_serial
*serial
= peer
->serial
;
2377 mutex_lock(&fwserial_list_mutex
);
2378 fwserial_remove_peer(peer
);
2380 if (list_empty(&serial
->peer_list
)) {
2381 /* unlink from the fwserial_list here */
2382 list_del_rcu(&serial
->list
);
2384 debugfs_remove_recursive(serial
->debugfs
);
2386 for (i
= 0; i
< num_ttys
; ++i
)
2387 fwserial_close_port(fwtty_driver
, serial
->ports
[i
]);
2388 if (create_loop_dev
)
2389 fwserial_close_port(fwloop_driver
, serial
->ports
[i
]);
2390 kref_put(&serial
->kref
, fwserial_destroy
);
2392 mutex_unlock(&fwserial_list_mutex
);
2396 * fwserial_update: bus update function for 'firewire' serial unit devices
2398 * Updates the new node_id and bus generation for this peer. Note that locking
2399 * is unnecessary; but careful memory barrier usage is important to enforce the
2400 * load and store order of generation & node_id.
2402 * The fw-core orders the write of node_id before generation in the parent
2403 * fw_device to ensure that a stale node_id cannot be used with a current
2404 * bus generation. So the generation value must be read before the node_id.
2406 * In turn, this orders the write of node_id before generation in the peer to
2407 * also ensure a stale node_id cannot be used with a current bus generation.
2409 static void fwserial_update(struct fw_unit
*unit
)
2411 struct fw_device
*parent
= fw_parent_device(unit
);
2412 struct fwtty_peer
*peer
= dev_get_drvdata(&unit
->device
);
2415 generation
= parent
->generation
;
2417 peer
->node_id
= parent
->node_id
;
2419 peer
->generation
= generation
;
2422 static const struct ieee1394_device_id fwserial_id_table
[] = {
2424 .match_flags
= IEEE1394_MATCH_SPECIFIER_ID
|
2425 IEEE1394_MATCH_VERSION
,
2426 .specifier_id
= LINUX_VENDOR_ID
,
2427 .version
= FWSERIAL_VERSION
,
2432 static struct fw_driver fwserial_driver
= {
2434 .owner
= THIS_MODULE
,
2435 .name
= KBUILD_MODNAME
,
2436 .bus
= &fw_bus_type
,
2438 .probe
= fwserial_probe
,
2439 .update
= fwserial_update
,
2440 .remove
= fwserial_remove
,
2441 .id_table
= fwserial_id_table
,
2444 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2445 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2446 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2447 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2448 /* XXX: config ROM definitons could be improved with semi-automated offset
2449 * and length calculation
2451 #define FW_ROM_LEN(quads) ((quads) << 16)
2452 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2454 struct fwserial_unit_directory_data
{
2457 u32 unit_sw_version
;
2458 u32 unit_addr_offset
;
2464 static struct fwserial_unit_directory_data fwserial_unit_directory_data
= {
2465 .len_crc
= FW_ROM_LEN(4),
2466 .unit_specifier
= FW_UNIT_SPECIFIER(LINUX_VENDOR_ID
),
2467 .unit_sw_version
= FW_UNIT_VERSION(FWSERIAL_VERSION
),
2468 .desc1_ofs
= FW_ROM_DESCRIPTOR(1),
2469 .desc1_len_crc
= FW_ROM_LEN(5),
2471 0x00000000, /* type = text */
2472 0x00000000, /* enc = ASCII, lang EN */
2473 0x4c696e75, /* 'Linux TTY' */
2479 static struct fw_descriptor fwserial_unit_directory
= {
2480 .length
= sizeof(fwserial_unit_directory_data
) / sizeof(u32
),
2481 .key
= (CSR_DIRECTORY
| CSR_UNIT
) << 24,
2482 .data
= (u32
*)&fwserial_unit_directory_data
,
2486 * The management address is in the unit space region but above other known
2487 * address users (to keep wild writes from causing havoc)
2489 static const struct fw_address_region fwserial_mgmt_addr_region
= {
2490 .start
= CSR_REGISTER_BASE
+ 0x1e0000ULL
,
2491 .end
= 0x1000000000000ULL
,
2494 static struct fw_address_handler fwserial_mgmt_addr_handler
;
2497 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2498 * @work: ptr to peer->work
2500 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2502 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2503 * already sent to this peer. If so, the collision is resolved by comparing
2504 * guid values; the loser sends the plug response.
2506 * Note: if an error prevents a response, don't do anything -- the
2507 * remote will timeout its request.
2509 static void fwserial_handle_plug_req(struct work_struct
*work
)
2511 struct fwtty_peer
*peer
= to_peer(work
, work
);
2512 struct virt_plug_params
*plug_req
= &peer
->work_params
.plug_req
;
2513 struct fwtty_port
*port
;
2514 struct fwserial_mgmt_pkt
*pkt
;
2517 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
2521 port
= fwserial_find_port(peer
);
2523 spin_lock_bh(&peer
->lock
);
2525 switch (peer
->state
) {
2526 case FWPS_NOT_ATTACHED
:
2528 fwtty_err(&peer
->unit
, "no more ports avail\n");
2529 fill_plug_rsp_nack(pkt
);
2532 fill_plug_rsp_ok(pkt
, peer
->port
);
2533 peer_set_state(peer
, FWPS_PLUG_RESPONDING
);
2534 /* don't release claimed port */
2539 case FWPS_PLUG_PENDING
:
2540 if (peer
->serial
->card
->guid
> peer
->guid
)
2543 /* We lost - hijack the already-claimed port and send ok */
2544 del_timer(&peer
->timer
);
2545 fill_plug_rsp_ok(pkt
, peer
->port
);
2546 peer_set_state(peer
, FWPS_PLUG_RESPONDING
);
2550 fill_plug_rsp_nack(pkt
);
2553 spin_unlock_bh(&peer
->lock
);
2555 fwserial_release_port(port
, false);
2557 rcode
= fwserial_send_mgmt_sync(peer
, pkt
);
2559 spin_lock_bh(&peer
->lock
);
2560 if (peer
->state
== FWPS_PLUG_RESPONDING
) {
2561 if (rcode
== RCODE_COMPLETE
) {
2562 struct fwtty_port
*tmp
= peer
->port
;
2564 fwserial_virt_plug_complete(peer
, plug_req
);
2565 spin_unlock_bh(&peer
->lock
);
2567 fwtty_write_port_status(tmp
);
2568 spin_lock_bh(&peer
->lock
);
2570 fwtty_err(&peer
->unit
, "PLUG_RSP error (%d)\n", rcode
);
2571 port
= peer_revert_state(peer
);
2575 spin_unlock_bh(&peer
->lock
);
2577 fwserial_release_port(port
, false);
2581 static void fwserial_handle_unplug_req(struct work_struct
*work
)
2583 struct fwtty_peer
*peer
= to_peer(work
, work
);
2584 struct fwtty_port
*port
= NULL
;
2585 struct fwserial_mgmt_pkt
*pkt
;
2588 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
2592 spin_lock_bh(&peer
->lock
);
2594 switch (peer
->state
) {
2596 fill_unplug_rsp_ok(pkt
);
2597 peer_set_state(peer
, FWPS_UNPLUG_RESPONDING
);
2600 case FWPS_UNPLUG_PENDING
:
2601 if (peer
->serial
->card
->guid
> peer
->guid
)
2604 /* We lost - send unplug rsp */
2605 del_timer(&peer
->timer
);
2606 fill_unplug_rsp_ok(pkt
);
2607 peer_set_state(peer
, FWPS_UNPLUG_RESPONDING
);
2611 fill_unplug_rsp_nack(pkt
);
2614 spin_unlock_bh(&peer
->lock
);
2616 rcode
= fwserial_send_mgmt_sync(peer
, pkt
);
2618 spin_lock_bh(&peer
->lock
);
2619 if (peer
->state
== FWPS_UNPLUG_RESPONDING
) {
2620 if (rcode
!= RCODE_COMPLETE
)
2621 fwtty_err(&peer
->unit
, "UNPLUG_RSP error (%d)\n",
2623 port
= peer_revert_state(peer
);
2626 spin_unlock_bh(&peer
->lock
);
2628 fwserial_release_port(port
, true);
2632 static int fwserial_parse_mgmt_write(struct fwtty_peer
*peer
,
2633 struct fwserial_mgmt_pkt
*pkt
,
2634 unsigned long long addr
,
2637 struct fwtty_port
*port
= NULL
;
2641 if (addr
!= fwserial_mgmt_addr_handler
.offset
|| len
< sizeof(pkt
->hdr
))
2642 return RCODE_ADDRESS_ERROR
;
2644 if (len
!= be16_to_cpu(pkt
->hdr
.len
) ||
2645 len
!= mgmt_pkt_expected_len(pkt
->hdr
.code
))
2646 return RCODE_DATA_ERROR
;
2648 spin_lock_bh(&peer
->lock
);
2649 if (peer
->state
== FWPS_GONE
) {
2651 * This should never happen - it would mean that the
2652 * remote unit that just wrote this transaction was
2653 * already removed from the bus -- and the removal was
2654 * processed before we rec'd this transaction
2656 fwtty_err(&peer
->unit
, "peer already removed\n");
2657 spin_unlock_bh(&peer
->lock
);
2658 return RCODE_ADDRESS_ERROR
;
2661 rcode
= RCODE_COMPLETE
;
2663 fwtty_dbg(&peer
->unit
, "mgmt: hdr.code: %04hx\n", pkt
->hdr
.code
);
2665 switch (be16_to_cpu(pkt
->hdr
.code
) & FWSC_CODE_MASK
) {
2666 case FWSC_VIRT_CABLE_PLUG
:
2667 if (work_pending(&peer
->work
)) {
2668 fwtty_err(&peer
->unit
, "plug req: busy\n");
2669 rcode
= RCODE_CONFLICT_ERROR
;
2672 peer
->work_params
.plug_req
= pkt
->plug_req
;
2673 peer
->workfn
= fwserial_handle_plug_req
;
2674 queue_work(system_unbound_wq
, &peer
->work
);
2678 case FWSC_VIRT_CABLE_PLUG_RSP
:
2679 if (peer
->state
!= FWPS_PLUG_PENDING
) {
2680 rcode
= RCODE_CONFLICT_ERROR
;
2682 } else if (be16_to_cpu(pkt
->hdr
.code
) & FWSC_RSP_NACK
) {
2683 fwtty_notice(&peer
->unit
, "NACK plug rsp\n");
2684 port
= peer_revert_state(peer
);
2687 struct fwtty_port
*tmp
= peer
->port
;
2689 fwserial_virt_plug_complete(peer
, &pkt
->plug_rsp
);
2690 spin_unlock_bh(&peer
->lock
);
2692 fwtty_write_port_status(tmp
);
2693 spin_lock_bh(&peer
->lock
);
2697 case FWSC_VIRT_CABLE_UNPLUG
:
2698 if (work_pending(&peer
->work
)) {
2699 fwtty_err(&peer
->unit
, "unplug req: busy\n");
2700 rcode
= RCODE_CONFLICT_ERROR
;
2702 peer
->workfn
= fwserial_handle_unplug_req
;
2703 queue_work(system_unbound_wq
, &peer
->work
);
2707 case FWSC_VIRT_CABLE_UNPLUG_RSP
:
2708 if (peer
->state
!= FWPS_UNPLUG_PENDING
) {
2709 rcode
= RCODE_CONFLICT_ERROR
;
2711 if (be16_to_cpu(pkt
->hdr
.code
) & FWSC_RSP_NACK
)
2712 fwtty_notice(&peer
->unit
, "NACK unplug?\n");
2713 port
= peer_revert_state(peer
);
2719 fwtty_err(&peer
->unit
, "unknown mgmt code %d\n",
2720 be16_to_cpu(pkt
->hdr
.code
));
2721 rcode
= RCODE_DATA_ERROR
;
2723 spin_unlock_bh(&peer
->lock
);
2726 fwserial_release_port(port
, reset
);
2732 * fwserial_mgmt_handler: bus address handler for mgmt requests
2733 * @parameters: fw_address_callback_t as specified by firewire core interface
2735 * This handler is responsible for handling virtual cable requests from remotes
2738 static void fwserial_mgmt_handler(struct fw_card
*card
,
2739 struct fw_request
*request
,
2740 int tcode
, int destination
, int source
,
2742 unsigned long long addr
,
2743 void *data
, size_t len
,
2744 void *callback_data
)
2746 struct fwserial_mgmt_pkt
*pkt
= data
;
2747 struct fwtty_peer
*peer
;
2751 peer
= __fwserial_peer_by_node_id(card
, generation
, source
);
2753 fwtty_dbg(card
, "peer(%d:%x) not found\n", generation
, source
);
2754 __dump_peer_list(card
);
2755 rcode
= RCODE_CONFLICT_ERROR
;
2759 case TCODE_WRITE_BLOCK_REQUEST
:
2760 rcode
= fwserial_parse_mgmt_write(peer
, pkt
, addr
, len
);
2764 rcode
= RCODE_TYPE_ERROR
;
2769 fw_send_response(card
, request
, rcode
);
2772 static int __init
fwserial_init(void)
2774 int err
, num_loops
= !!(create_loop_dev
);
2776 /* XXX: placeholder for a "firewire" debugfs node */
2777 fwserial_debugfs
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2779 /* num_ttys/num_ports must not be set above the static alloc avail */
2780 if (num_ttys
+ num_loops
> MAX_CARD_PORTS
)
2781 num_ttys
= MAX_CARD_PORTS
- num_loops
;
2783 num_ports
= num_ttys
+ num_loops
;
2785 fwtty_driver
= tty_alloc_driver(MAX_TOTAL_PORTS
, TTY_DRIVER_REAL_RAW
2786 | TTY_DRIVER_DYNAMIC_DEV
);
2787 if (IS_ERR(fwtty_driver
)) {
2788 err
= PTR_ERR(fwtty_driver
);
2789 goto remove_debugfs
;
2792 fwtty_driver
->driver_name
= KBUILD_MODNAME
;
2793 fwtty_driver
->name
= tty_dev_name
;
2794 fwtty_driver
->major
= 0;
2795 fwtty_driver
->minor_start
= 0;
2796 fwtty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2797 fwtty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2798 fwtty_driver
->init_termios
= tty_std_termios
;
2799 fwtty_driver
->init_termios
.c_cflag
|= CLOCAL
;
2800 tty_set_operations(fwtty_driver
, &fwtty_ops
);
2802 err
= tty_register_driver(fwtty_driver
);
2804 pr_err("register tty driver failed (%d)\n", err
);
2808 if (create_loop_dev
) {
2809 fwloop_driver
= tty_alloc_driver(MAX_TOTAL_PORTS
/ num_ports
,
2811 | TTY_DRIVER_DYNAMIC_DEV
);
2812 if (IS_ERR(fwloop_driver
)) {
2813 err
= PTR_ERR(fwloop_driver
);
2814 goto unregister_driver
;
2817 fwloop_driver
->driver_name
= KBUILD_MODNAME
"_loop";
2818 fwloop_driver
->name
= loop_dev_name
;
2819 fwloop_driver
->major
= 0;
2820 fwloop_driver
->minor_start
= 0;
2821 fwloop_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2822 fwloop_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2823 fwloop_driver
->init_termios
= tty_std_termios
;
2824 fwloop_driver
->init_termios
.c_cflag
|= CLOCAL
;
2825 tty_set_operations(fwloop_driver
, &fwloop_ops
);
2827 err
= tty_register_driver(fwloop_driver
);
2829 pr_err("register loop driver failed (%d)\n", err
);
2834 fwtty_txn_cache
= kmem_cache_create("fwtty_txn_cache",
2835 sizeof(struct fwtty_transaction
),
2837 if (!fwtty_txn_cache
) {
2839 goto unregister_loop
;
2843 * Ideally, this address handler would be registered per local node
2844 * (rather than the same handler for all local nodes). However,
2845 * since the firewire core requires the config rom descriptor *before*
2846 * the local unit device(s) are created, a single management handler
2847 * must suffice for all local serial units.
2849 fwserial_mgmt_addr_handler
.length
= sizeof(struct fwserial_mgmt_pkt
);
2850 fwserial_mgmt_addr_handler
.address_callback
= fwserial_mgmt_handler
;
2852 err
= fw_core_add_address_handler(&fwserial_mgmt_addr_handler
,
2853 &fwserial_mgmt_addr_region
);
2855 pr_err("add management handler failed (%d)\n", err
);
2859 fwserial_unit_directory_data
.unit_addr_offset
=
2860 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler
.offset
);
2861 err
= fw_core_add_descriptor(&fwserial_unit_directory
);
2863 pr_err("add unit descriptor failed (%d)\n", err
);
2864 goto remove_handler
;
2867 err
= driver_register(&fwserial_driver
.driver
);
2869 pr_err("register fwserial driver failed (%d)\n", err
);
2870 goto remove_descriptor
;
2876 fw_core_remove_descriptor(&fwserial_unit_directory
);
2878 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler
);
2880 kmem_cache_destroy(fwtty_txn_cache
);
2882 if (create_loop_dev
)
2883 tty_unregister_driver(fwloop_driver
);
2885 if (create_loop_dev
)
2886 put_tty_driver(fwloop_driver
);
2888 tty_unregister_driver(fwtty_driver
);
2890 put_tty_driver(fwtty_driver
);
2892 debugfs_remove_recursive(fwserial_debugfs
);
2897 static void __exit
fwserial_exit(void)
2899 driver_unregister(&fwserial_driver
.driver
);
2900 fw_core_remove_descriptor(&fwserial_unit_directory
);
2901 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler
);
2902 kmem_cache_destroy(fwtty_txn_cache
);
2903 if (create_loop_dev
) {
2904 tty_unregister_driver(fwloop_driver
);
2905 put_tty_driver(fwloop_driver
);
2907 tty_unregister_driver(fwtty_driver
);
2908 put_tty_driver(fwtty_driver
);
2909 debugfs_remove_recursive(fwserial_debugfs
);
2912 module_init(fwserial_init
);
2913 module_exit(fwserial_exit
);
2915 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2916 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2917 MODULE_LICENSE("GPL");
2918 MODULE_DEVICE_TABLE(ieee1394
, fwserial_id_table
);
2919 MODULE_PARM_DESC(ttys
, "Number of ttys to create for each local firewire node");
2920 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2921 MODULE_PARM_DESC(loop
, "Create a loopback device, fwloop<n>, with ttys");