1 // SPDX-License-Identifier: GPL-2.0+
3 * FireWire Serial driver
5 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/rculist.h>
15 #include <linux/workqueue.h>
16 #include <linux/ratelimit.h>
17 #include <linux/bug.h>
18 #include <linux/uaccess.h>
22 inline u64
be32_to_u64(__be32 hi
, __be32 lo
)
24 return ((u64
)be32_to_cpu(hi
) << 32 | be32_to_cpu(lo
));
27 #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
28 #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
30 /* configurable options */
31 static int num_ttys
= 4; /* # of std ttys to create per fw_card */
32 /* - doubles as loopback port index */
33 static bool auto_connect
= true; /* try to VIRT_CABLE to every peer */
34 static bool create_loop_dev
= true; /* create a loopback device for each card */
36 module_param_named(ttys
, num_ttys
, int, 0644);
37 module_param_named(auto, auto_connect
, bool, 0644);
38 module_param_named(loop
, create_loop_dev
, bool, 0644);
41 * Threshold below which the tty is woken for writing
42 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
43 * even if the writer is woken, n_tty_poll() won't set EPOLLOUT until
44 * our fifo is below this level
46 #define WAKEUP_CHARS 256
49 * fwserial_list: list of every fw_serial created for each fw_card
50 * See discussion in fwserial_probe.
52 static LIST_HEAD(fwserial_list
);
53 static DEFINE_MUTEX(fwserial_list_mutex
);
56 * port_table: array of tty ports allocated to each fw_card
58 * tty ports are allocated during probe when an fw_serial is first
59 * created for a given fw_card. Ports are allocated in a contiguous block,
60 * each block consisting of 'num_ports' ports.
62 static struct fwtty_port
*port_table
[MAX_TOTAL_PORTS
];
63 static DEFINE_MUTEX(port_table_lock
);
64 static bool port_table_corrupt
;
65 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
67 #define loop_idx(port) (((port)->index) / num_ports)
68 #define table_idx(loop) ((loop) * num_ports + num_ttys)
70 /* total # of tty ports created per fw_card */
73 /* slab used as pool for struct fwtty_transactions */
74 static struct kmem_cache
*fwtty_txn_cache
;
76 struct tty_driver
*fwtty_driver
;
77 static struct tty_driver
*fwloop_driver
;
79 static struct dentry
*fwserial_debugfs
;
81 struct fwtty_transaction
;
82 typedef void (*fwtty_transaction_cb
)(struct fw_card
*card
, int rcode
,
83 void *data
, size_t length
,
84 struct fwtty_transaction
*txn
);
86 struct fwtty_transaction
{
87 struct fw_transaction fw_txn
;
88 fwtty_transaction_cb callback
;
89 struct fwtty_port
*port
;
91 struct dma_pending dma_pended
;
95 #define to_device(a, b) (a->b)
96 #define fwtty_err(p, fmt, ...) \
97 dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
98 #define fwtty_info(p, fmt, ...) \
99 dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
100 #define fwtty_notice(p, fmt, ...) \
101 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
102 #define fwtty_dbg(p, fmt, ...) \
103 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
104 #define fwtty_err_ratelimited(p, fmt, ...) \
105 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
108 static inline void debug_short_write(struct fwtty_port
*port
, int c
, int n
)
113 spin_lock_bh(&port
->lock
);
114 avail
= dma_fifo_avail(&port
->tx_fifo
);
115 spin_unlock_bh(&port
->lock
);
116 fwtty_dbg(port
, "short write: avail:%d req:%d wrote:%d\n",
121 #define debug_short_write(port, c, n)
124 static struct fwtty_peer
*__fwserial_peer_by_node_id(struct fw_card
*card
,
125 int generation
, int id
);
127 #ifdef FWTTY_PROFILING
129 static void fwtty_profile_fifo(struct fwtty_port
*port
, unsigned int *stat
)
131 spin_lock_bh(&port
->lock
);
132 fwtty_profile_data(stat
, dma_fifo_avail(&port
->tx_fifo
));
133 spin_unlock_bh(&port
->lock
);
136 static void fwtty_dump_profile(struct seq_file
*m
, struct stats
*stats
)
138 /* for each stat, print sum of 0 to 2^k, then individually */
144 snprintf(t
, 10, "< %d", 1 << k
);
145 seq_printf(m
, "\n%14s %6s", " ", t
);
146 for (j
= k
+ 1; j
< DISTRIBUTION_MAX_INDEX
; ++j
)
147 seq_printf(m
, "%6d", 1 << j
);
150 for (j
= 0, sum
= 0; j
<= k
; ++j
)
151 sum
+= stats
->reads
[j
];
152 seq_printf(m
, "\n%14s: %6d", "reads", sum
);
153 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
154 seq_printf(m
, "%6d", stats
->reads
[j
]);
156 for (j
= 0, sum
= 0; j
<= k
; ++j
)
157 sum
+= stats
->writes
[j
];
158 seq_printf(m
, "\n%14s: %6d", "writes", sum
);
159 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
160 seq_printf(m
, "%6d", stats
->writes
[j
]);
162 for (j
= 0, sum
= 0; j
<= k
; ++j
)
163 sum
+= stats
->txns
[j
];
164 seq_printf(m
, "\n%14s: %6d", "txns", sum
);
165 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
166 seq_printf(m
, "%6d", stats
->txns
[j
]);
168 for (j
= 0, sum
= 0; j
<= k
; ++j
)
169 sum
+= stats
->unthrottle
[j
];
170 seq_printf(m
, "\n%14s: %6d", "avail @ unthr", sum
);
171 for (j
= k
+ 1; j
<= DISTRIBUTION_MAX_INDEX
; ++j
)
172 seq_printf(m
, "%6d", stats
->unthrottle
[j
]);
176 #define fwtty_profile_fifo(port, stat)
177 #define fwtty_dump_profile(m, stats)
181 * Returns the max receive packet size for the given node
182 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
183 * are required by specification to support max_rec of 8 (512 bytes) or more.
185 static inline int device_max_receive(struct fw_device
*fw_device
)
187 /* see IEEE 1394-2008 table 8-8 */
188 return min(2 << fw_device
->max_rec
, 4096);
191 static void fwtty_log_tx_error(struct fwtty_port
*port
, int rcode
)
194 case RCODE_SEND_ERROR
:
195 fwtty_err_ratelimited(port
, "card busy\n");
197 case RCODE_ADDRESS_ERROR
:
198 fwtty_err_ratelimited(port
, "bad unit addr or write length\n");
200 case RCODE_DATA_ERROR
:
201 fwtty_err_ratelimited(port
, "failed rx\n");
204 fwtty_err_ratelimited(port
, "missing ack\n");
207 fwtty_err_ratelimited(port
, "remote busy\n");
210 fwtty_err_ratelimited(port
, "failed tx: %d\n", rcode
);
214 static void fwtty_common_callback(struct fw_card
*card
, int rcode
,
215 void *payload
, size_t len
, void *cb_data
)
217 struct fwtty_transaction
*txn
= cb_data
;
218 struct fwtty_port
*port
= txn
->port
;
220 if (port
&& rcode
!= RCODE_COMPLETE
)
221 fwtty_log_tx_error(port
, rcode
);
223 txn
->callback(card
, rcode
, payload
, len
, txn
);
224 kmem_cache_free(fwtty_txn_cache
, txn
);
227 static int fwtty_send_data_async(struct fwtty_peer
*peer
, int tcode
,
228 unsigned long long addr
, void *payload
,
229 size_t len
, fwtty_transaction_cb callback
,
230 struct fwtty_port
*port
)
232 struct fwtty_transaction
*txn
;
235 txn
= kmem_cache_alloc(fwtty_txn_cache
, GFP_ATOMIC
);
239 txn
->callback
= callback
;
242 generation
= peer
->generation
;
244 fw_send_request(peer
->serial
->card
, &txn
->fw_txn
, tcode
,
245 peer
->node_id
, generation
, peer
->speed
, addr
, payload
,
246 len
, fwtty_common_callback
, txn
);
250 static void fwtty_send_txn_async(struct fwtty_peer
*peer
,
251 struct fwtty_transaction
*txn
, int tcode
,
252 unsigned long long addr
, void *payload
,
253 size_t len
, fwtty_transaction_cb callback
,
254 struct fwtty_port
*port
)
258 txn
->callback
= callback
;
261 generation
= peer
->generation
;
263 fw_send_request(peer
->serial
->card
, &txn
->fw_txn
, tcode
,
264 peer
->node_id
, generation
, peer
->speed
, addr
, payload
,
265 len
, fwtty_common_callback
, txn
);
268 static void __fwtty_restart_tx(struct fwtty_port
*port
)
272 len
= dma_fifo_out_level(&port
->tx_fifo
);
274 schedule_delayed_work(&port
->drain
, 0);
275 avail
= dma_fifo_avail(&port
->tx_fifo
);
277 fwtty_dbg(port
, "fifo len: %d avail: %d\n", len
, avail
);
280 static void fwtty_restart_tx(struct fwtty_port
*port
)
282 spin_lock_bh(&port
->lock
);
283 __fwtty_restart_tx(port
);
284 spin_unlock_bh(&port
->lock
);
288 * fwtty_update_port_status - decodes & dispatches line status changes
290 * Note: in loopback, the port->lock is being held. Only use functions that
291 * don't attempt to reclaim the port->lock.
293 static void fwtty_update_port_status(struct fwtty_port
*port
,
297 struct tty_struct
*tty
;
299 /* simulated LSR/MSR status from remote */
300 status
&= ~MCTRL_MASK
;
301 delta
= (port
->mstatus
^ status
) & ~MCTRL_MASK
;
302 delta
&= ~(status
& TIOCM_RNG
);
303 port
->mstatus
= status
;
305 if (delta
& TIOCM_RNG
)
307 if (delta
& TIOCM_DSR
)
309 if (delta
& TIOCM_CAR
)
311 if (delta
& TIOCM_CTS
)
314 fwtty_dbg(port
, "status: %x delta: %x\n", status
, delta
);
316 if (delta
& TIOCM_CAR
) {
317 tty
= tty_port_tty_get(&port
->port
);
318 if (tty
&& !C_CLOCAL(tty
)) {
319 if (status
& TIOCM_CAR
)
320 wake_up_interruptible(&port
->port
.open_wait
);
322 schedule_work(&port
->hangup
);
327 if (delta
& TIOCM_CTS
) {
328 tty
= tty_port_tty_get(&port
->port
);
329 if (tty
&& C_CRTSCTS(tty
)) {
330 if (tty
->hw_stopped
) {
331 if (status
& TIOCM_CTS
) {
334 __fwtty_restart_tx(port
);
336 fwtty_restart_tx(port
);
339 if (~status
& TIOCM_CTS
)
345 } else if (delta
& OOB_TX_THROTTLE
) {
346 tty
= tty_port_tty_get(&port
->port
);
348 if (tty
->hw_stopped
) {
349 if (~status
& OOB_TX_THROTTLE
) {
352 __fwtty_restart_tx(port
);
354 fwtty_restart_tx(port
);
357 if (status
& OOB_TX_THROTTLE
)
364 if (delta
& (UART_LSR_BI
<< 24)) {
365 if (status
& (UART_LSR_BI
<< 24)) {
366 port
->break_last
= jiffies
;
367 schedule_delayed_work(&port
->emit_breaks
, 0);
369 /* run emit_breaks one last time (if pending) */
370 mod_delayed_work(system_wq
, &port
->emit_breaks
, 0);
374 if (delta
& (TIOCM_DSR
| TIOCM_CAR
| TIOCM_CTS
| TIOCM_RNG
))
375 wake_up_interruptible(&port
->port
.delta_msr_wait
);
379 * __fwtty_port_line_status - generate 'line status' for indicated port
381 * This function returns a remote 'MSR' state based on the local 'MCR' state,
382 * as if a null modem cable was attached. The actual status is a mangling
383 * of TIOCM_* bits suitable for sending to a peer's status_addr.
385 * Note: caller must be holding port lock
387 static unsigned int __fwtty_port_line_status(struct fwtty_port
*port
)
389 unsigned int status
= 0;
391 /* TODO: add module param to tie RNG to DTR as well */
393 if (port
->mctrl
& TIOCM_DTR
)
394 status
|= TIOCM_DSR
| TIOCM_CAR
;
395 if (port
->mctrl
& TIOCM_RTS
)
397 if (port
->mctrl
& OOB_RX_THROTTLE
)
398 status
|= OOB_TX_THROTTLE
;
399 /* emulate BRK as add'l line status */
401 status
|= UART_LSR_BI
<< 24;
407 * __fwtty_write_port_status - send the port line status to peer
409 * Note: caller must be holding the port lock.
411 static int __fwtty_write_port_status(struct fwtty_port
*port
)
413 struct fwtty_peer
*peer
;
415 unsigned int status
= __fwtty_port_line_status(port
);
418 peer
= rcu_dereference(port
->peer
);
420 err
= fwtty_send_data_async(peer
, TCODE_WRITE_QUADLET_REQUEST
,
421 peer
->status_addr
, &status
,
422 sizeof(status
), NULL
, port
);
430 * fwtty_write_port_status - same as above but locked by port lock
432 static int fwtty_write_port_status(struct fwtty_port
*port
)
436 spin_lock_bh(&port
->lock
);
437 err
= __fwtty_write_port_status(port
);
438 spin_unlock_bh(&port
->lock
);
442 static void fwtty_throttle_port(struct fwtty_port
*port
)
444 struct tty_struct
*tty
;
447 tty
= tty_port_tty_get(&port
->port
);
451 spin_lock_bh(&port
->lock
);
454 port
->mctrl
|= OOB_RX_THROTTLE
;
456 port
->mctrl
&= ~TIOCM_RTS
;
457 if (~old
& OOB_RX_THROTTLE
)
458 __fwtty_write_port_status(port
);
460 spin_unlock_bh(&port
->lock
);
466 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
468 * When the remote has finished tx, and all in-flight rx has been received and
469 * and pushed to the flip buffer, the remote may close its device. This will
470 * drop DTR on the remote which will drop carrier here. Typically, the tty is
471 * hung up when carrier is dropped or lost.
473 * However, there is a race between the hang up and the line discipline
474 * delivering its data to the reader. A hangup will cause the ldisc to flush
475 * (ie., clear) the read buffer and flip buffer. Because of firewire's
476 * relatively high throughput, the ldisc frequently lags well behind the driver,
477 * resulting in lost data (which has already been received and written to
478 * the flip buffer) when the remote closes its end.
480 * Unfortunately, since the flip buffer offers no direct method for determining
481 * if it holds data, ensuring the ldisc has delivered all data is problematic.
484 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
485 static void fwtty_do_hangup(struct work_struct
*work
)
487 struct fwtty_port
*port
= to_port(work
, hangup
);
488 struct tty_struct
*tty
;
490 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
492 tty
= tty_port_tty_get(&port
->port
);
498 static void fwtty_emit_breaks(struct work_struct
*work
)
500 struct fwtty_port
*port
= to_port(to_delayed_work(work
), emit_breaks
);
501 static const char buf
[16];
502 unsigned long now
= jiffies
;
503 unsigned long elapsed
= now
- port
->break_last
;
504 int n
, t
, c
, brk
= 0;
506 /* generate breaks at the line rate (but at least 1) */
507 n
= (elapsed
* port
->cps
) / HZ
+ 1;
508 port
->break_last
= now
;
510 fwtty_dbg(port
, "sending %d brks\n", n
);
514 c
= tty_insert_flip_string_fixed_flag(&port
->port
, buf
,
521 tty_flip_buffer_push(&port
->port
);
523 if (port
->mstatus
& (UART_LSR_BI
<< 24))
524 schedule_delayed_work(&port
->emit_breaks
, FREQ_BREAKS
);
525 port
->icount
.brk
+= brk
;
528 static int fwtty_rx(struct fwtty_port
*port
, unsigned char *data
, size_t len
)
534 fwtty_dbg(port
, "%d\n", n
);
535 fwtty_profile_data(port
->stats
.reads
, n
);
537 if (port
->write_only
) {
542 /* disregard break status; breaks are generated by emit_breaks work */
543 lsr
= (port
->mstatus
>> 24) & ~UART_LSR_BI
;
548 if (lsr
& UART_LSR_OE
)
549 ++port
->icount
.overrun
;
551 lsr
&= port
->status_mask
;
552 if (lsr
& ~port
->ignore_mask
& UART_LSR_OE
) {
553 if (!tty_insert_flip_char(&port
->port
, 0, TTY_OVERRUN
)) {
558 port
->overrun
= false;
560 if (lsr
& port
->ignore_mask
& ~UART_LSR_OE
) {
561 /* TODO: don't drop SAK and Magic SysRq here */
566 c
= tty_insert_flip_string_fixed_flag(&port
->port
, data
, TTY_NORMAL
, n
);
568 tty_flip_buffer_push(&port
->port
);
572 port
->overrun
= true;
574 fwtty_err_ratelimited(port
, "flip buffer overrun\n");
577 /* throttle the sender if remaining flip buffer space has
578 * reached high watermark to avoid losing data which may be
579 * in-flight. Since the AR request context is 32k, that much
580 * data may have _already_ been acked.
582 if (tty_buffer_space_avail(&port
->port
) < HIGH_WATERMARK
)
583 fwtty_throttle_port(port
);
587 port
->icount
.rx
+= len
;
588 port
->stats
.lost
+= n
;
593 * fwtty_port_handler - bus address handler for port reads/writes
594 * @parameters: fw_address_callback_t as specified by firewire core interface
596 * This handler is responsible for handling inbound read/write dma from remotes.
598 static void fwtty_port_handler(struct fw_card
*card
,
599 struct fw_request
*request
,
600 int tcode
, int destination
, int source
,
602 unsigned long long addr
,
603 void *data
, size_t len
,
606 struct fwtty_port
*port
= callback_data
;
607 struct fwtty_peer
*peer
;
611 /* Only accept rx from the peer virtual-cabled to this port */
613 peer
= __fwserial_peer_by_node_id(card
, generation
, source
);
615 if (!peer
|| peer
!= rcu_access_pointer(port
->peer
)) {
616 rcode
= RCODE_ADDRESS_ERROR
;
617 fwtty_err_ratelimited(port
, "ignoring unauthenticated data\n");
622 case TCODE_WRITE_QUADLET_REQUEST
:
623 if (addr
!= port
->rx_handler
.offset
|| len
!= 4) {
624 rcode
= RCODE_ADDRESS_ERROR
;
626 fwtty_update_port_status(port
, *(unsigned int *)data
);
627 rcode
= RCODE_COMPLETE
;
631 case TCODE_WRITE_BLOCK_REQUEST
:
632 if (addr
!= port
->rx_handler
.offset
+ 4 ||
633 len
> port
->rx_handler
.length
- 4) {
634 rcode
= RCODE_ADDRESS_ERROR
;
636 err
= fwtty_rx(port
, data
, len
);
639 rcode
= RCODE_COMPLETE
;
642 rcode
= RCODE_DATA_ERROR
;
645 rcode
= RCODE_CONFLICT_ERROR
;
652 rcode
= RCODE_TYPE_ERROR
;
656 fw_send_response(card
, request
, rcode
);
660 * fwtty_tx_complete - callback for tx dma
661 * @data: ignored, has no meaning for write txns
662 * @length: ignored, has no meaning for write txns
664 * The writer must be woken here if the fifo has been emptied because it
665 * may have slept if chars_in_buffer was != 0
667 static void fwtty_tx_complete(struct fw_card
*card
, int rcode
,
668 void *data
, size_t length
,
669 struct fwtty_transaction
*txn
)
671 struct fwtty_port
*port
= txn
->port
;
674 fwtty_dbg(port
, "rcode: %d\n", rcode
);
678 spin_lock_bh(&port
->lock
);
679 dma_fifo_out_complete(&port
->tx_fifo
, &txn
->dma_pended
);
680 len
= dma_fifo_level(&port
->tx_fifo
);
681 spin_unlock_bh(&port
->lock
);
683 port
->icount
.tx
+= txn
->dma_pended
.len
;
687 /* TODO: implement retries */
688 spin_lock_bh(&port
->lock
);
689 dma_fifo_out_complete(&port
->tx_fifo
, &txn
->dma_pended
);
690 len
= dma_fifo_level(&port
->tx_fifo
);
691 spin_unlock_bh(&port
->lock
);
693 port
->stats
.dropped
+= txn
->dma_pended
.len
;
696 if (len
< WAKEUP_CHARS
)
697 tty_port_tty_wakeup(&port
->port
);
700 static int fwtty_tx(struct fwtty_port
*port
, bool drain
)
702 struct fwtty_peer
*peer
;
703 struct fwtty_transaction
*txn
;
704 struct tty_struct
*tty
;
707 tty
= tty_port_tty_get(&port
->port
);
712 peer
= rcu_dereference(port
->peer
);
718 if (test_and_set_bit(IN_TX
, &port
->flags
)) {
723 /* try to write as many dma transactions out as possible */
725 while (!tty
->stopped
&& !tty
->hw_stopped
&&
726 !test_bit(STOP_TX
, &port
->flags
)) {
727 txn
= kmem_cache_alloc(fwtty_txn_cache
, GFP_ATOMIC
);
733 spin_lock_bh(&port
->lock
);
734 n
= dma_fifo_out_pend(&port
->tx_fifo
, &txn
->dma_pended
);
735 spin_unlock_bh(&port
->lock
);
737 fwtty_dbg(port
, "out: %u rem: %d\n", txn
->dma_pended
.len
, n
);
740 kmem_cache_free(fwtty_txn_cache
, txn
);
742 ++port
->stats
.tx_stall
;
743 } else if (n
== -ENODATA
) {
744 fwtty_profile_data(port
->stats
.txns
, 0);
746 ++port
->stats
.fifo_errs
;
747 fwtty_err_ratelimited(port
, "fifo err: %d\n",
753 fwtty_profile_data(port
->stats
.txns
, txn
->dma_pended
.len
);
755 fwtty_send_txn_async(peer
, txn
, TCODE_WRITE_BLOCK_REQUEST
,
756 peer
->fifo_addr
, txn
->dma_pended
.data
,
757 txn
->dma_pended
.len
, fwtty_tx_complete
,
762 * Stop tx if the 'last view' of the fifo is empty or if
763 * this is the writer and there's not enough data to bother
765 if (n
== 0 || (!drain
&& n
< WRITER_MINIMUM
))
769 if (n
>= 0 || n
== -EAGAIN
|| n
== -ENOMEM
|| n
== -ENODATA
) {
770 spin_lock_bh(&port
->lock
);
771 len
= dma_fifo_out_level(&port
->tx_fifo
);
773 unsigned long delay
= (n
== -ENOMEM
) ? HZ
: 1;
775 schedule_delayed_work(&port
->drain
, delay
);
777 len
= dma_fifo_level(&port
->tx_fifo
);
778 spin_unlock_bh(&port
->lock
);
780 /* wakeup the writer */
781 if (drain
&& len
< WAKEUP_CHARS
)
785 clear_bit(IN_TX
, &port
->flags
);
786 wake_up_interruptible(&port
->wait_tx
);
794 static void fwtty_drain_tx(struct work_struct
*work
)
796 struct fwtty_port
*port
= to_port(to_delayed_work(work
), drain
);
798 fwtty_tx(port
, true);
801 static void fwtty_write_xchar(struct fwtty_port
*port
, char ch
)
803 struct fwtty_peer
*peer
;
805 ++port
->stats
.xchars
;
807 fwtty_dbg(port
, "%02x\n", ch
);
810 peer
= rcu_dereference(port
->peer
);
812 fwtty_send_data_async(peer
, TCODE_WRITE_BLOCK_REQUEST
,
813 peer
->fifo_addr
, &ch
, sizeof(ch
),
819 static struct fwtty_port
*fwtty_port_get(unsigned int index
)
821 struct fwtty_port
*port
;
823 if (index
>= MAX_TOTAL_PORTS
)
826 mutex_lock(&port_table_lock
);
827 port
= port_table
[index
];
829 kref_get(&port
->serial
->kref
);
830 mutex_unlock(&port_table_lock
);
834 static int fwtty_ports_add(struct fw_serial
*serial
)
839 if (port_table_corrupt
)
842 mutex_lock(&port_table_lock
);
843 for (i
= 0; i
+ num_ports
<= MAX_TOTAL_PORTS
; i
+= num_ports
) {
844 if (!port_table
[i
]) {
845 for (j
= 0; j
< num_ports
; ++i
, ++j
) {
846 serial
->ports
[j
]->index
= i
;
847 port_table
[i
] = serial
->ports
[j
];
853 mutex_unlock(&port_table_lock
);
857 static void fwserial_destroy(struct kref
*kref
)
859 struct fw_serial
*serial
= to_serial(kref
, kref
);
860 struct fwtty_port
**ports
= serial
->ports
;
861 int j
, i
= ports
[0]->index
;
865 mutex_lock(&port_table_lock
);
866 for (j
= 0; j
< num_ports
; ++i
, ++j
) {
867 port_table_corrupt
|= port_table
[i
] != ports
[j
];
868 WARN_ONCE(port_table_corrupt
, "port_table[%d]: %p != ports[%d]: %p",
869 i
, port_table
[i
], j
, ports
[j
]);
871 port_table
[i
] = NULL
;
873 mutex_unlock(&port_table_lock
);
875 for (j
= 0; j
< num_ports
; ++j
) {
876 fw_core_remove_address_handler(&ports
[j
]->rx_handler
);
877 tty_port_destroy(&ports
[j
]->port
);
883 static void fwtty_port_put(struct fwtty_port
*port
)
885 kref_put(&port
->serial
->kref
, fwserial_destroy
);
888 static void fwtty_port_dtr_rts(struct tty_port
*tty_port
, int on
)
890 struct fwtty_port
*port
= to_port(tty_port
, port
);
892 fwtty_dbg(port
, "on/off: %d\n", on
);
894 spin_lock_bh(&port
->lock
);
895 /* Don't change carrier state if this is a console */
896 if (!port
->port
.console
) {
898 port
->mctrl
|= TIOCM_DTR
| TIOCM_RTS
;
900 port
->mctrl
&= ~(TIOCM_DTR
| TIOCM_RTS
);
903 __fwtty_write_port_status(port
);
904 spin_unlock_bh(&port
->lock
);
908 * fwtty_port_carrier_raised: required tty_port operation
910 * This port operation is polled after a tty has been opened and is waiting for
911 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
913 static int fwtty_port_carrier_raised(struct tty_port
*tty_port
)
915 struct fwtty_port
*port
= to_port(tty_port
, port
);
918 rc
= (port
->mstatus
& TIOCM_CAR
);
920 fwtty_dbg(port
, "%d\n", rc
);
925 static unsigned int set_termios(struct fwtty_port
*port
, struct tty_struct
*tty
)
927 unsigned int baud
, frame
;
929 baud
= tty_termios_baud_rate(&tty
->termios
);
930 tty_termios_encode_baud_rate(&tty
->termios
, baud
, baud
);
932 /* compute bit count of 2 frames */
933 frame
= 12 + ((C_CSTOPB(tty
)) ? 4 : 2) + ((C_PARENB(tty
)) ? 2 : 0);
935 switch (C_CSIZE(tty
)) {
937 frame
-= (C_CSTOPB(tty
)) ? 1 : 0;
950 port
->cps
= (baud
<< 1) / frame
;
952 port
->status_mask
= UART_LSR_OE
;
953 if (_I_FLAG(tty
, BRKINT
| PARMRK
))
954 port
->status_mask
|= UART_LSR_BI
;
956 port
->ignore_mask
= 0;
958 port
->ignore_mask
|= UART_LSR_BI
;
960 port
->ignore_mask
|= UART_LSR_OE
;
963 port
->write_only
= !C_CREAD(tty
);
965 /* turn off echo and newline xlat if loopback */
966 if (port
->loopback
) {
967 tty
->termios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHOKE
|
968 ECHONL
| ECHOPRT
| ECHOCTL
);
969 tty
->termios
.c_oflag
&= ~ONLCR
;
975 static int fwtty_port_activate(struct tty_port
*tty_port
,
976 struct tty_struct
*tty
)
978 struct fwtty_port
*port
= to_port(tty_port
, port
);
982 set_bit(TTY_IO_ERROR
, &tty
->flags
);
984 err
= dma_fifo_alloc(&port
->tx_fifo
, FWTTY_PORT_TXFIFO_LEN
,
987 FWTTY_PORT_MAX_PEND_DMA
,
992 spin_lock_bh(&port
->lock
);
994 baud
= set_termios(port
, tty
);
996 /* if console, don't change carrier state */
997 if (!port
->port
.console
) {
1000 port
->mctrl
= TIOCM_DTR
| TIOCM_RTS
;
1003 if (C_CRTSCTS(tty
) && ~port
->mstatus
& TIOCM_CTS
)
1004 tty
->hw_stopped
= 1;
1006 __fwtty_write_port_status(port
);
1007 spin_unlock_bh(&port
->lock
);
1009 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
1015 * fwtty_port_shutdown
1017 * Note: the tty port core ensures this is not the console and
1018 * manages TTY_IO_ERROR properly
1020 static void fwtty_port_shutdown(struct tty_port
*tty_port
)
1022 struct fwtty_port
*port
= to_port(tty_port
, port
);
1024 /* TODO: cancel outstanding transactions */
1026 cancel_delayed_work_sync(&port
->emit_breaks
);
1027 cancel_delayed_work_sync(&port
->drain
);
1029 spin_lock_bh(&port
->lock
);
1031 port
->break_ctl
= 0;
1033 __fwtty_write_port_status(port
);
1034 dma_fifo_free(&port
->tx_fifo
);
1035 spin_unlock_bh(&port
->lock
);
1038 static int fwtty_open(struct tty_struct
*tty
, struct file
*fp
)
1040 struct fwtty_port
*port
= tty
->driver_data
;
1042 return tty_port_open(&port
->port
, tty
, fp
);
1045 static void fwtty_close(struct tty_struct
*tty
, struct file
*fp
)
1047 struct fwtty_port
*port
= tty
->driver_data
;
1049 tty_port_close(&port
->port
, tty
, fp
);
1052 static void fwtty_hangup(struct tty_struct
*tty
)
1054 struct fwtty_port
*port
= tty
->driver_data
;
1056 tty_port_hangup(&port
->port
);
1059 static void fwtty_cleanup(struct tty_struct
*tty
)
1061 struct fwtty_port
*port
= tty
->driver_data
;
1063 tty
->driver_data
= NULL
;
1064 fwtty_port_put(port
);
1067 static int fwtty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1069 struct fwtty_port
*port
= fwtty_port_get(tty
->index
);
1072 err
= tty_standard_install(driver
, tty
);
1074 tty
->driver_data
= port
;
1076 fwtty_port_put(port
);
1080 static int fwloop_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1082 struct fwtty_port
*port
= fwtty_port_get(table_idx(tty
->index
));
1085 err
= tty_standard_install(driver
, tty
);
1087 tty
->driver_data
= port
;
1089 fwtty_port_put(port
);
1093 static int fwtty_write(struct tty_struct
*tty
, const unsigned char *buf
, int c
)
1095 struct fwtty_port
*port
= tty
->driver_data
;
1098 fwtty_dbg(port
, "%d\n", c
);
1099 fwtty_profile_data(port
->stats
.writes
, c
);
1101 spin_lock_bh(&port
->lock
);
1102 n
= dma_fifo_in(&port
->tx_fifo
, buf
, c
);
1103 len
= dma_fifo_out_level(&port
->tx_fifo
);
1104 if (len
< DRAIN_THRESHOLD
)
1105 schedule_delayed_work(&port
->drain
, 1);
1106 spin_unlock_bh(&port
->lock
);
1108 if (len
>= DRAIN_THRESHOLD
)
1109 fwtty_tx(port
, false);
1111 debug_short_write(port
, c
, n
);
1113 return (n
< 0) ? 0 : n
;
1116 static int fwtty_write_room(struct tty_struct
*tty
)
1118 struct fwtty_port
*port
= tty
->driver_data
;
1121 spin_lock_bh(&port
->lock
);
1122 n
= dma_fifo_avail(&port
->tx_fifo
);
1123 spin_unlock_bh(&port
->lock
);
1125 fwtty_dbg(port
, "%d\n", n
);
1130 static int fwtty_chars_in_buffer(struct tty_struct
*tty
)
1132 struct fwtty_port
*port
= tty
->driver_data
;
1135 spin_lock_bh(&port
->lock
);
1136 n
= dma_fifo_level(&port
->tx_fifo
);
1137 spin_unlock_bh(&port
->lock
);
1139 fwtty_dbg(port
, "%d\n", n
);
1144 static void fwtty_send_xchar(struct tty_struct
*tty
, char ch
)
1146 struct fwtty_port
*port
= tty
->driver_data
;
1148 fwtty_dbg(port
, "%02x\n", ch
);
1150 fwtty_write_xchar(port
, ch
);
1153 static void fwtty_throttle(struct tty_struct
*tty
)
1155 struct fwtty_port
*port
= tty
->driver_data
;
1158 * Ignore throttling (but not unthrottling).
1159 * It only makes sense to throttle when data will no longer be
1160 * accepted by the tty flip buffer. For example, it is
1161 * possible for received data to overflow the tty buffer long
1162 * before the line discipline ever has a chance to throttle the driver.
1163 * Additionally, the driver may have already completed the I/O
1164 * but the tty buffer is still emptying, so the line discipline is
1165 * throttling and unthrottling nothing.
1168 ++port
->stats
.throttled
;
1171 static void fwtty_unthrottle(struct tty_struct
*tty
)
1173 struct fwtty_port
*port
= tty
->driver_data
;
1175 fwtty_dbg(port
, "CRTSCTS: %d\n", C_CRTSCTS(tty
) != 0);
1177 fwtty_profile_fifo(port
, port
->stats
.unthrottle
);
1179 spin_lock_bh(&port
->lock
);
1180 port
->mctrl
&= ~OOB_RX_THROTTLE
;
1182 port
->mctrl
|= TIOCM_RTS
;
1183 __fwtty_write_port_status(port
);
1184 spin_unlock_bh(&port
->lock
);
1187 static int check_msr_delta(struct fwtty_port
*port
, unsigned long mask
,
1188 struct async_icount
*prev
)
1190 struct async_icount now
;
1195 delta
= ((mask
& TIOCM_RNG
&& prev
->rng
!= now
.rng
) ||
1196 (mask
& TIOCM_DSR
&& prev
->dsr
!= now
.dsr
) ||
1197 (mask
& TIOCM_CAR
&& prev
->dcd
!= now
.dcd
) ||
1198 (mask
& TIOCM_CTS
&& prev
->cts
!= now
.cts
));
1205 static int wait_msr_change(struct fwtty_port
*port
, unsigned long mask
)
1207 struct async_icount prev
;
1209 prev
= port
->icount
;
1211 return wait_event_interruptible(port
->port
.delta_msr_wait
,
1212 check_msr_delta(port
, mask
, &prev
));
1215 static int get_serial_info(struct tty_struct
*tty
,
1216 struct serial_struct
*ss
)
1218 struct fwtty_port
*port
= tty
->driver_data
;
1220 mutex_lock(&port
->port
.mutex
);
1221 ss
->type
= PORT_UNKNOWN
;
1222 ss
->line
= port
->port
.tty
->index
;
1223 ss
->flags
= port
->port
.flags
;
1224 ss
->xmit_fifo_size
= FWTTY_PORT_TXFIFO_LEN
;
1225 ss
->baud_base
= 400000000;
1226 ss
->close_delay
= port
->port
.close_delay
;
1227 mutex_unlock(&port
->port
.mutex
);
1231 static int set_serial_info(struct tty_struct
*tty
,
1232 struct serial_struct
*ss
)
1234 struct fwtty_port
*port
= tty
->driver_data
;
1236 if (ss
->irq
!= 0 || ss
->port
!= 0 || ss
->custom_divisor
!= 0 ||
1237 ss
->baud_base
!= 400000000)
1240 mutex_lock(&port
->port
.mutex
);
1241 if (!capable(CAP_SYS_ADMIN
)) {
1242 if (((ss
->flags
& ~ASYNC_USR_MASK
) !=
1243 (port
->port
.flags
& ~ASYNC_USR_MASK
))) {
1244 mutex_unlock(&port
->port
.mutex
);
1248 port
->port
.close_delay
= ss
->close_delay
* HZ
/ 100;
1249 mutex_unlock(&port
->port
.mutex
);
1254 static int fwtty_ioctl(struct tty_struct
*tty
, unsigned int cmd
,
1257 struct fwtty_port
*port
= tty
->driver_data
;
1262 err
= wait_msr_change(port
, arg
);
1272 static void fwtty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1274 struct fwtty_port
*port
= tty
->driver_data
;
1277 spin_lock_bh(&port
->lock
);
1278 baud
= set_termios(port
, tty
);
1280 if ((baud
== 0) && (old
->c_cflag
& CBAUD
)) {
1281 port
->mctrl
&= ~(TIOCM_DTR
| TIOCM_RTS
);
1282 } else if ((baud
!= 0) && !(old
->c_cflag
& CBAUD
)) {
1283 if (C_CRTSCTS(tty
) || !tty_throttled(tty
))
1284 port
->mctrl
|= TIOCM_DTR
| TIOCM_RTS
;
1286 port
->mctrl
|= TIOCM_DTR
;
1288 __fwtty_write_port_status(port
);
1289 spin_unlock_bh(&port
->lock
);
1291 if (old
->c_cflag
& CRTSCTS
) {
1292 if (!C_CRTSCTS(tty
)) {
1293 tty
->hw_stopped
= 0;
1294 fwtty_restart_tx(port
);
1296 } else if (C_CRTSCTS(tty
) && ~port
->mstatus
& TIOCM_CTS
) {
1297 tty
->hw_stopped
= 1;
1302 * fwtty_break_ctl - start/stop sending breaks
1304 * Signals the remote to start or stop generating simulated breaks.
1305 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1306 * before signalling the break line status. This guarantees any pending rx will
1307 * be queued to the line discipline before break is simulated on the remote.
1308 * Conversely, turning off break_ctl requires signalling the line status change,
1311 static int fwtty_break_ctl(struct tty_struct
*tty
, int state
)
1313 struct fwtty_port
*port
= tty
->driver_data
;
1316 fwtty_dbg(port
, "%d\n", state
);
1319 set_bit(STOP_TX
, &port
->flags
);
1320 ret
= wait_event_interruptible_timeout(port
->wait_tx
,
1321 !test_bit(IN_TX
, &port
->flags
),
1323 if (ret
== 0 || ret
== -ERESTARTSYS
) {
1324 clear_bit(STOP_TX
, &port
->flags
);
1325 fwtty_restart_tx(port
);
1330 spin_lock_bh(&port
->lock
);
1331 port
->break_ctl
= (state
== -1);
1332 __fwtty_write_port_status(port
);
1333 spin_unlock_bh(&port
->lock
);
1336 spin_lock_bh(&port
->lock
);
1337 dma_fifo_reset(&port
->tx_fifo
);
1338 clear_bit(STOP_TX
, &port
->flags
);
1339 spin_unlock_bh(&port
->lock
);
1344 static int fwtty_tiocmget(struct tty_struct
*tty
)
1346 struct fwtty_port
*port
= tty
->driver_data
;
1349 spin_lock_bh(&port
->lock
);
1350 tiocm
= (port
->mctrl
& MCTRL_MASK
) | (port
->mstatus
& ~MCTRL_MASK
);
1351 spin_unlock_bh(&port
->lock
);
1353 fwtty_dbg(port
, "%x\n", tiocm
);
1358 static int fwtty_tiocmset(struct tty_struct
*tty
,
1359 unsigned int set
, unsigned int clear
)
1361 struct fwtty_port
*port
= tty
->driver_data
;
1363 fwtty_dbg(port
, "set: %x clear: %x\n", set
, clear
);
1365 /* TODO: simulate loopback if TIOCM_LOOP set */
1367 spin_lock_bh(&port
->lock
);
1368 port
->mctrl
&= ~(clear
& MCTRL_MASK
& 0xffff);
1369 port
->mctrl
|= set
& MCTRL_MASK
& 0xffff;
1370 __fwtty_write_port_status(port
);
1371 spin_unlock_bh(&port
->lock
);
1375 static int fwtty_get_icount(struct tty_struct
*tty
,
1376 struct serial_icounter_struct
*icount
)
1378 struct fwtty_port
*port
= tty
->driver_data
;
1381 memcpy(&stats
, &port
->stats
, sizeof(stats
));
1382 if (port
->port
.console
)
1383 (*port
->fwcon_ops
->stats
)(&stats
, port
->con_data
);
1385 icount
->cts
= port
->icount
.cts
;
1386 icount
->dsr
= port
->icount
.dsr
;
1387 icount
->rng
= port
->icount
.rng
;
1388 icount
->dcd
= port
->icount
.dcd
;
1389 icount
->rx
= port
->icount
.rx
;
1390 icount
->tx
= port
->icount
.tx
+ stats
.xchars
;
1391 icount
->frame
= port
->icount
.frame
;
1392 icount
->overrun
= port
->icount
.overrun
;
1393 icount
->parity
= port
->icount
.parity
;
1394 icount
->brk
= port
->icount
.brk
;
1395 icount
->buf_overrun
= port
->icount
.overrun
;
1399 static void fwtty_proc_show_port(struct seq_file
*m
, struct fwtty_port
*port
)
1403 memcpy(&stats
, &port
->stats
, sizeof(stats
));
1404 if (port
->port
.console
)
1405 (*port
->fwcon_ops
->stats
)(&stats
, port
->con_data
);
1407 seq_printf(m
, " addr:%012llx tx:%d rx:%d", port
->rx_handler
.offset
,
1408 port
->icount
.tx
+ stats
.xchars
, port
->icount
.rx
);
1409 seq_printf(m
, " cts:%d dsr:%d rng:%d dcd:%d", port
->icount
.cts
,
1410 port
->icount
.dsr
, port
->icount
.rng
, port
->icount
.dcd
);
1411 seq_printf(m
, " fe:%d oe:%d pe:%d brk:%d", port
->icount
.frame
,
1412 port
->icount
.overrun
, port
->icount
.parity
, port
->icount
.brk
);
1415 static void fwtty_debugfs_show_port(struct seq_file
*m
, struct fwtty_port
*port
)
1419 memcpy(&stats
, &port
->stats
, sizeof(stats
));
1420 if (port
->port
.console
)
1421 (*port
->fwcon_ops
->stats
)(&stats
, port
->con_data
);
1423 seq_printf(m
, " dr:%d st:%d err:%d lost:%d", stats
.dropped
,
1424 stats
.tx_stall
, stats
.fifo_errs
, stats
.lost
);
1425 seq_printf(m
, " pkts:%d thr:%d", stats
.sent
, stats
.throttled
);
1427 if (port
->port
.console
) {
1429 (*port
->fwcon_ops
->proc_show
)(m
, port
->con_data
);
1432 fwtty_dump_profile(m
, &port
->stats
);
1435 static void fwtty_debugfs_show_peer(struct seq_file
*m
, struct fwtty_peer
*peer
)
1437 int generation
= peer
->generation
;
1440 seq_printf(m
, " %s:", dev_name(&peer
->unit
->device
));
1441 seq_printf(m
, " node:%04x gen:%d", peer
->node_id
, generation
);
1442 seq_printf(m
, " sp:%d max:%d guid:%016llx", peer
->speed
,
1443 peer
->max_payload
, (unsigned long long)peer
->guid
);
1444 seq_printf(m
, " mgmt:%012llx", (unsigned long long)peer
->mgmt_addr
);
1445 seq_printf(m
, " addr:%012llx", (unsigned long long)peer
->status_addr
);
1449 static int fwtty_proc_show(struct seq_file
*m
, void *v
)
1451 struct fwtty_port
*port
;
1454 seq_puts(m
, "fwserinfo: 1.0 driver: 1.0\n");
1455 for (i
= 0; i
< MAX_TOTAL_PORTS
&& (port
= fwtty_port_get(i
)); ++i
) {
1456 seq_printf(m
, "%2d:", i
);
1457 if (capable(CAP_SYS_ADMIN
))
1458 fwtty_proc_show_port(m
, port
);
1459 fwtty_port_put(port
);
1465 static int fwtty_stats_show(struct seq_file
*m
, void *v
)
1467 struct fw_serial
*serial
= m
->private;
1468 struct fwtty_port
*port
;
1471 for (i
= 0; i
< num_ports
; ++i
) {
1472 port
= fwtty_port_get(serial
->ports
[i
]->index
);
1474 seq_printf(m
, "%2d:", port
->index
);
1475 fwtty_proc_show_port(m
, port
);
1476 fwtty_debugfs_show_port(m
, port
);
1477 fwtty_port_put(port
);
1483 DEFINE_SHOW_ATTRIBUTE(fwtty_stats
);
1485 static int fwtty_peers_show(struct seq_file
*m
, void *v
)
1487 struct fw_serial
*serial
= m
->private;
1488 struct fwtty_peer
*peer
;
1491 seq_printf(m
, "card: %s guid: %016llx\n",
1492 dev_name(serial
->card
->device
),
1493 (unsigned long long)serial
->card
->guid
);
1494 list_for_each_entry_rcu(peer
, &serial
->peer_list
, list
)
1495 fwtty_debugfs_show_peer(m
, peer
);
1499 DEFINE_SHOW_ATTRIBUTE(fwtty_peers
);
1501 static const struct tty_port_operations fwtty_port_ops
= {
1502 .dtr_rts
= fwtty_port_dtr_rts
,
1503 .carrier_raised
= fwtty_port_carrier_raised
,
1504 .shutdown
= fwtty_port_shutdown
,
1505 .activate
= fwtty_port_activate
,
1508 static const struct tty_operations fwtty_ops
= {
1510 .close
= fwtty_close
,
1511 .hangup
= fwtty_hangup
,
1512 .cleanup
= fwtty_cleanup
,
1513 .install
= fwtty_install
,
1514 .write
= fwtty_write
,
1515 .write_room
= fwtty_write_room
,
1516 .chars_in_buffer
= fwtty_chars_in_buffer
,
1517 .send_xchar
= fwtty_send_xchar
,
1518 .throttle
= fwtty_throttle
,
1519 .unthrottle
= fwtty_unthrottle
,
1520 .ioctl
= fwtty_ioctl
,
1521 .set_termios
= fwtty_set_termios
,
1522 .break_ctl
= fwtty_break_ctl
,
1523 .tiocmget
= fwtty_tiocmget
,
1524 .tiocmset
= fwtty_tiocmset
,
1525 .get_icount
= fwtty_get_icount
,
1526 .set_serial
= set_serial_info
,
1527 .get_serial
= get_serial_info
,
1528 .proc_show
= fwtty_proc_show
,
1531 static const struct tty_operations fwloop_ops
= {
1533 .close
= fwtty_close
,
1534 .hangup
= fwtty_hangup
,
1535 .cleanup
= fwtty_cleanup
,
1536 .install
= fwloop_install
,
1537 .write
= fwtty_write
,
1538 .write_room
= fwtty_write_room
,
1539 .chars_in_buffer
= fwtty_chars_in_buffer
,
1540 .send_xchar
= fwtty_send_xchar
,
1541 .throttle
= fwtty_throttle
,
1542 .unthrottle
= fwtty_unthrottle
,
1543 .ioctl
= fwtty_ioctl
,
1544 .set_termios
= fwtty_set_termios
,
1545 .break_ctl
= fwtty_break_ctl
,
1546 .tiocmget
= fwtty_tiocmget
,
1547 .tiocmset
= fwtty_tiocmset
,
1548 .get_icount
= fwtty_get_icount
,
1549 .set_serial
= set_serial_info
,
1550 .get_serial
= get_serial_info
,
1553 static inline int mgmt_pkt_expected_len(__be16 code
)
1555 static const struct fwserial_mgmt_pkt pkt
;
1557 switch (be16_to_cpu(code
)) {
1558 case FWSC_VIRT_CABLE_PLUG
:
1559 return sizeof(pkt
.hdr
) + sizeof(pkt
.plug_req
);
1561 case FWSC_VIRT_CABLE_PLUG_RSP
: /* | FWSC_RSP_OK */
1562 return sizeof(pkt
.hdr
) + sizeof(pkt
.plug_rsp
);
1564 case FWSC_VIRT_CABLE_UNPLUG
:
1565 case FWSC_VIRT_CABLE_UNPLUG_RSP
:
1566 case FWSC_VIRT_CABLE_PLUG_RSP
| FWSC_RSP_NACK
:
1567 case FWSC_VIRT_CABLE_UNPLUG_RSP
| FWSC_RSP_NACK
:
1568 return sizeof(pkt
.hdr
);
1575 static inline void fill_plug_params(struct virt_plug_params
*params
,
1576 struct fwtty_port
*port
)
1578 u64 status_addr
= port
->rx_handler
.offset
;
1579 u64 fifo_addr
= port
->rx_handler
.offset
+ 4;
1580 size_t fifo_len
= port
->rx_handler
.length
- 4;
1582 params
->status_hi
= cpu_to_be32(status_addr
>> 32);
1583 params
->status_lo
= cpu_to_be32(status_addr
);
1584 params
->fifo_hi
= cpu_to_be32(fifo_addr
>> 32);
1585 params
->fifo_lo
= cpu_to_be32(fifo_addr
);
1586 params
->fifo_len
= cpu_to_be32(fifo_len
);
1589 static inline void fill_plug_req(struct fwserial_mgmt_pkt
*pkt
,
1590 struct fwtty_port
*port
)
1592 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_PLUG
);
1593 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1594 fill_plug_params(&pkt
->plug_req
, port
);
1597 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt
*pkt
,
1598 struct fwtty_port
*port
)
1600 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP
);
1601 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1602 fill_plug_params(&pkt
->plug_rsp
, port
);
1605 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt
*pkt
)
1607 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP
| FWSC_RSP_NACK
);
1608 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1611 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt
*pkt
)
1613 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP
| FWSC_RSP_NACK
);
1614 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1617 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt
*pkt
)
1619 pkt
->hdr
.code
= cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP
);
1620 pkt
->hdr
.len
= cpu_to_be16(mgmt_pkt_expected_len(pkt
->hdr
.code
));
1623 static void fwserial_virt_plug_complete(struct fwtty_peer
*peer
,
1624 struct virt_plug_params
*params
)
1626 struct fwtty_port
*port
= peer
->port
;
1628 peer
->status_addr
= be32_to_u64(params
->status_hi
, params
->status_lo
);
1629 peer
->fifo_addr
= be32_to_u64(params
->fifo_hi
, params
->fifo_lo
);
1630 peer
->fifo_len
= be32_to_cpu(params
->fifo_len
);
1631 peer_set_state(peer
, FWPS_ATTACHED
);
1633 /* reconfigure tx_fifo optimally for this peer */
1634 spin_lock_bh(&port
->lock
);
1635 port
->max_payload
= min(peer
->max_payload
, peer
->fifo_len
);
1636 dma_fifo_change_tx_limit(&port
->tx_fifo
, port
->max_payload
);
1637 spin_unlock_bh(&peer
->port
->lock
);
1639 if (port
->port
.console
&& port
->fwcon_ops
->notify
)
1640 (*port
->fwcon_ops
->notify
)(FWCON_NOTIFY_ATTACH
, port
->con_data
);
1642 fwtty_info(&peer
->unit
, "peer (guid:%016llx) connected on %s\n",
1643 (unsigned long long)peer
->guid
, dev_name(port
->device
));
1646 static inline int fwserial_send_mgmt_sync(struct fwtty_peer
*peer
,
1647 struct fwserial_mgmt_pkt
*pkt
)
1650 int rcode
, tries
= 5;
1653 generation
= peer
->generation
;
1656 rcode
= fw_run_transaction(peer
->serial
->card
,
1657 TCODE_WRITE_BLOCK_REQUEST
,
1659 generation
, peer
->speed
,
1661 pkt
, be16_to_cpu(pkt
->hdr
.len
));
1662 if (rcode
== RCODE_BUSY
|| rcode
== RCODE_SEND_ERROR
||
1663 rcode
== RCODE_GENERATION
) {
1664 fwtty_dbg(&peer
->unit
, "mgmt write error: %d\n", rcode
);
1669 } while (--tries
> 0);
1674 * fwserial_claim_port - attempt to claim port @ index for peer
1676 * Returns ptr to claimed port or error code (as ERR_PTR())
1677 * Can sleep - must be called from process context
1679 static struct fwtty_port
*fwserial_claim_port(struct fwtty_peer
*peer
,
1682 struct fwtty_port
*port
;
1684 if (index
< 0 || index
>= num_ports
)
1685 return ERR_PTR(-EINVAL
);
1687 /* must guarantee that previous port releases have completed */
1690 port
= peer
->serial
->ports
[index
];
1691 spin_lock_bh(&port
->lock
);
1692 if (!rcu_access_pointer(port
->peer
))
1693 rcu_assign_pointer(port
->peer
, peer
);
1695 port
= ERR_PTR(-EBUSY
);
1696 spin_unlock_bh(&port
->lock
);
1702 * fwserial_find_port - find avail port and claim for peer
1704 * Returns ptr to claimed port or NULL if none avail
1705 * Can sleep - must be called from process context
1707 static struct fwtty_port
*fwserial_find_port(struct fwtty_peer
*peer
)
1709 struct fwtty_port
**ports
= peer
->serial
->ports
;
1712 /* must guarantee that previous port releases have completed */
1715 /* TODO: implement optional GUID-to-specific port # matching */
1717 /* find an unattached port (but not the loopback port, if present) */
1718 for (i
= 0; i
< num_ttys
; ++i
) {
1719 spin_lock_bh(&ports
[i
]->lock
);
1720 if (!ports
[i
]->peer
) {
1722 rcu_assign_pointer(ports
[i
]->peer
, peer
);
1723 spin_unlock_bh(&ports
[i
]->lock
);
1726 spin_unlock_bh(&ports
[i
]->lock
);
1731 static void fwserial_release_port(struct fwtty_port
*port
, bool reset
)
1733 /* drop carrier (and all other line status) */
1735 fwtty_update_port_status(port
, 0);
1737 spin_lock_bh(&port
->lock
);
1739 /* reset dma fifo max transmission size back to S100 */
1740 port
->max_payload
= link_speed_to_max_payload(SCODE_100
);
1741 dma_fifo_change_tx_limit(&port
->tx_fifo
, port
->max_payload
);
1743 RCU_INIT_POINTER(port
->peer
, NULL
);
1744 spin_unlock_bh(&port
->lock
);
1746 if (port
->port
.console
&& port
->fwcon_ops
->notify
)
1747 (*port
->fwcon_ops
->notify
)(FWCON_NOTIFY_DETACH
, port
->con_data
);
1750 static void fwserial_plug_timeout(struct timer_list
*t
)
1752 struct fwtty_peer
*peer
= from_timer(peer
, t
, timer
);
1753 struct fwtty_port
*port
;
1755 spin_lock_bh(&peer
->lock
);
1756 if (peer
->state
!= FWPS_PLUG_PENDING
) {
1757 spin_unlock_bh(&peer
->lock
);
1761 port
= peer_revert_state(peer
);
1762 spin_unlock_bh(&peer
->lock
);
1765 fwserial_release_port(port
, false);
1769 * fwserial_connect_peer - initiate virtual cable with peer
1771 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1772 * otherwise error code. Must be called from process context.
1774 static int fwserial_connect_peer(struct fwtty_peer
*peer
)
1776 struct fwtty_port
*port
;
1777 struct fwserial_mgmt_pkt
*pkt
;
1780 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
1784 port
= fwserial_find_port(peer
);
1786 fwtty_err(&peer
->unit
, "avail ports in use\n");
1791 spin_lock_bh(&peer
->lock
);
1793 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1794 if (peer
->state
!= FWPS_NOT_ATTACHED
) {
1800 peer_set_state(peer
, FWPS_PLUG_PENDING
);
1802 fill_plug_req(pkt
, peer
->port
);
1804 mod_timer(&peer
->timer
, jiffies
+ VIRT_CABLE_PLUG_TIMEOUT
);
1805 spin_unlock_bh(&peer
->lock
);
1807 rcode
= fwserial_send_mgmt_sync(peer
, pkt
);
1809 spin_lock_bh(&peer
->lock
);
1810 if (peer
->state
== FWPS_PLUG_PENDING
&& rcode
!= RCODE_COMPLETE
) {
1811 if (rcode
== RCODE_CONFLICT_ERROR
)
1817 spin_unlock_bh(&peer
->lock
);
1823 del_timer(&peer
->timer
);
1824 peer_revert_state(peer
);
1826 spin_unlock_bh(&peer
->lock
);
1827 fwserial_release_port(port
, false);
1834 * fwserial_close_port -
1835 * HUP the tty (if the tty exists) and unregister the tty device.
1836 * Only used by the unit driver upon unit removal to disconnect and
1837 * cleanup all attached ports
1839 * The port reference is put by fwtty_cleanup (if a reference was
1842 static void fwserial_close_port(struct tty_driver
*driver
,
1843 struct fwtty_port
*port
)
1845 struct tty_struct
*tty
;
1847 mutex_lock(&port
->port
.mutex
);
1848 tty
= tty_port_tty_get(&port
->port
);
1853 mutex_unlock(&port
->port
.mutex
);
1855 if (driver
== fwloop_driver
)
1856 tty_unregister_device(driver
, loop_idx(port
));
1858 tty_unregister_device(driver
, port
->index
);
1862 * fwserial_lookup - finds first fw_serial associated with card
1863 * @card: fw_card to match
1865 * NB: caller must be holding fwserial_list_mutex
1867 static struct fw_serial
*fwserial_lookup(struct fw_card
*card
)
1869 struct fw_serial
*serial
;
1871 list_for_each_entry(serial
, &fwserial_list
, list
) {
1872 if (card
== serial
->card
)
1880 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1881 * @card: fw_card to match
1883 * NB: caller must be inside rcu_read_lock() section
1885 static struct fw_serial
*__fwserial_lookup_rcu(struct fw_card
*card
)
1887 struct fw_serial
*serial
;
1889 list_for_each_entry_rcu(serial
, &fwserial_list
, list
) {
1890 if (card
== serial
->card
)
1898 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1900 * If a matching peer could not be found for the specified generation/node id,
1901 * this could be because:
1902 * a) the generation has changed and one of the nodes hasn't updated yet
1903 * b) the remote node has created its remote unit device before this
1904 * local node has created its corresponding remote unit device
1905 * In either case, the remote node should retry
1907 * Note: caller must be in rcu_read_lock() section
1909 static struct fwtty_peer
*__fwserial_peer_by_node_id(struct fw_card
*card
,
1910 int generation
, int id
)
1912 struct fw_serial
*serial
;
1913 struct fwtty_peer
*peer
;
1915 serial
= __fwserial_lookup_rcu(card
);
1918 * Something is very wrong - there should be a matching
1919 * fw_serial structure for every fw_card. Maybe the remote node
1920 * has created its remote unit device before this driver has
1921 * been probed for any unit devices...
1923 fwtty_err(card
, "unknown card (guid %016llx)\n",
1924 (unsigned long long)card
->guid
);
1928 list_for_each_entry_rcu(peer
, &serial
->peer_list
, list
) {
1929 int g
= peer
->generation
;
1932 if (generation
== g
&& id
== peer
->node_id
)
1940 static void __dump_peer_list(struct fw_card
*card
)
1942 struct fw_serial
*serial
;
1943 struct fwtty_peer
*peer
;
1945 serial
= __fwserial_lookup_rcu(card
);
1949 list_for_each_entry_rcu(peer
, &serial
->peer_list
, list
) {
1950 int g
= peer
->generation
;
1953 fwtty_dbg(card
, "peer(%d:%x) guid: %016llx\n",
1954 g
, peer
->node_id
, (unsigned long long)peer
->guid
);
1958 #define __dump_peer_list(s)
1961 static void fwserial_auto_connect(struct work_struct
*work
)
1963 struct fwtty_peer
*peer
= to_peer(to_delayed_work(work
), connect
);
1966 err
= fwserial_connect_peer(peer
);
1967 if (err
== -EAGAIN
&& ++peer
->connect_retries
< MAX_CONNECT_RETRIES
)
1968 schedule_delayed_work(&peer
->connect
, CONNECT_RETRY_DELAY
);
1971 static void fwserial_peer_workfn(struct work_struct
*work
)
1973 struct fwtty_peer
*peer
= to_peer(work
, work
);
1979 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
1980 * @serial: aggregate representing the specific fw_card to add the peer to
1981 * @unit: 'peer' to create and add to peer_list of serial
1983 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
1984 * peers for a specific fw_card. Optionally, auto-attach this peer to an
1985 * available tty port. This function is called either directly or indirectly
1986 * as a result of a 'serial' unit device being created & probed.
1988 * Note: this function is serialized with fwserial_remove_peer() by the
1989 * fwserial_list_mutex held in fwserial_probe().
1991 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
1992 * via the dev_set_drvdata() for the device of the fw_unit.
1994 static int fwserial_add_peer(struct fw_serial
*serial
, struct fw_unit
*unit
)
1996 struct device
*dev
= &unit
->device
;
1997 struct fw_device
*parent
= fw_parent_device(unit
);
1998 struct fwtty_peer
*peer
;
1999 struct fw_csr_iterator ci
;
2003 peer
= kzalloc(sizeof(*peer
), GFP_KERNEL
);
2007 peer_set_state(peer
, FWPS_NOT_ATTACHED
);
2009 dev_set_drvdata(dev
, peer
);
2011 peer
->guid
= (u64
)parent
->config_rom
[3] << 32 | parent
->config_rom
[4];
2012 peer
->speed
= parent
->max_speed
;
2013 peer
->max_payload
= min(device_max_receive(parent
),
2014 link_speed_to_max_payload(peer
->speed
));
2016 generation
= parent
->generation
;
2018 peer
->node_id
= parent
->node_id
;
2020 peer
->generation
= generation
;
2022 /* retrieve the mgmt bus addr from the unit directory */
2023 fw_csr_iterator_init(&ci
, unit
->directory
);
2024 while (fw_csr_iterator_next(&ci
, &key
, &val
)) {
2025 if (key
== (CSR_OFFSET
| CSR_DEPENDENT_INFO
)) {
2026 peer
->mgmt_addr
= CSR_REGISTER_BASE
+ 4 * val
;
2030 if (peer
->mgmt_addr
== 0ULL) {
2032 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2033 * this peer will not be able to attach to a remote
2035 peer_set_state(peer
, FWPS_NO_MGMT_ADDR
);
2038 spin_lock_init(&peer
->lock
);
2041 timer_setup(&peer
->timer
, fwserial_plug_timeout
, 0);
2042 INIT_WORK(&peer
->work
, fwserial_peer_workfn
);
2043 INIT_DELAYED_WORK(&peer
->connect
, fwserial_auto_connect
);
2045 /* associate peer with specific fw_card */
2046 peer
->serial
= serial
;
2047 list_add_rcu(&peer
->list
, &serial
->peer_list
);
2049 fwtty_info(&peer
->unit
, "peer added (guid:%016llx)\n",
2050 (unsigned long long)peer
->guid
);
2052 /* identify the local unit & virt cable to loopback port */
2053 if (parent
->is_local
) {
2054 serial
->self
= peer
;
2055 if (create_loop_dev
) {
2056 struct fwtty_port
*port
;
2058 port
= fwserial_claim_port(peer
, num_ttys
);
2059 if (!IS_ERR(port
)) {
2060 struct virt_plug_params params
;
2062 spin_lock_bh(&peer
->lock
);
2064 fill_plug_params(¶ms
, port
);
2065 fwserial_virt_plug_complete(peer
, ¶ms
);
2066 spin_unlock_bh(&peer
->lock
);
2068 fwtty_write_port_status(port
);
2072 } else if (auto_connect
) {
2073 /* auto-attach to remote units only (if policy allows) */
2074 schedule_delayed_work(&peer
->connect
, 1);
2081 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2083 * Remove a 'peer' from its list of peers. This function is only
2084 * called by fwserial_remove() on bus removal of the unit device.
2086 * Note: this function is serialized with fwserial_add_peer() by the
2087 * fwserial_list_mutex held in fwserial_remove().
2089 static void fwserial_remove_peer(struct fwtty_peer
*peer
)
2091 struct fwtty_port
*port
;
2093 spin_lock_bh(&peer
->lock
);
2094 peer_set_state(peer
, FWPS_GONE
);
2095 spin_unlock_bh(&peer
->lock
);
2097 cancel_delayed_work_sync(&peer
->connect
);
2098 cancel_work_sync(&peer
->work
);
2100 spin_lock_bh(&peer
->lock
);
2101 /* if this unit is the local unit, clear link */
2102 if (peer
== peer
->serial
->self
)
2103 peer
->serial
->self
= NULL
;
2105 /* cancel the request timeout timer (if running) */
2106 del_timer(&peer
->timer
);
2111 list_del_rcu(&peer
->list
);
2113 fwtty_info(&peer
->unit
, "peer removed (guid:%016llx)\n",
2114 (unsigned long long)peer
->guid
);
2116 spin_unlock_bh(&peer
->lock
);
2119 fwserial_release_port(port
, true);
2126 * fwserial_create - init everything to create TTYs for a specific fw_card
2127 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2129 * This function inits the aggregate structure (an fw_serial instance)
2130 * used to manage the TTY ports registered by a specific fw_card. Also, the
2131 * unit device is added as the first 'peer'.
2133 * This unit device may represent a local unit device (as specified by the
2134 * config ROM unit directory) or it may represent a remote unit device
2135 * (as specified by the reading of the remote node's config ROM).
2137 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2138 * value to indicate which error.
2140 static int fwserial_create(struct fw_unit
*unit
)
2142 struct fw_device
*parent
= fw_parent_device(unit
);
2143 struct fw_card
*card
= parent
->card
;
2144 struct fw_serial
*serial
;
2145 struct fwtty_port
*port
;
2146 struct device
*tty_dev
;
2150 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
2154 kref_init(&serial
->kref
);
2155 serial
->card
= card
;
2156 INIT_LIST_HEAD(&serial
->peer_list
);
2158 for (i
= 0; i
< num_ports
; ++i
) {
2159 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
2164 tty_port_init(&port
->port
);
2165 port
->index
= FWTTY_INVALID_INDEX
;
2166 port
->port
.ops
= &fwtty_port_ops
;
2167 port
->serial
= serial
;
2168 tty_buffer_set_limit(&port
->port
, 128 * 1024);
2170 spin_lock_init(&port
->lock
);
2171 INIT_DELAYED_WORK(&port
->drain
, fwtty_drain_tx
);
2172 INIT_DELAYED_WORK(&port
->emit_breaks
, fwtty_emit_breaks
);
2173 INIT_WORK(&port
->hangup
, fwtty_do_hangup
);
2174 init_waitqueue_head(&port
->wait_tx
);
2175 port
->max_payload
= link_speed_to_max_payload(SCODE_100
);
2176 dma_fifo_init(&port
->tx_fifo
);
2178 RCU_INIT_POINTER(port
->peer
, NULL
);
2179 serial
->ports
[i
] = port
;
2181 /* get unique bus addr region for port's status & recv fifo */
2182 port
->rx_handler
.length
= FWTTY_PORT_RXFIFO_LEN
+ 4;
2183 port
->rx_handler
.address_callback
= fwtty_port_handler
;
2184 port
->rx_handler
.callback_data
= port
;
2186 * XXX: use custom memory region above cpu physical memory addrs
2187 * this will ease porting to 64-bit firewire adapters
2189 err
= fw_core_add_address_handler(&port
->rx_handler
,
2190 &fw_high_memory_region
);
2196 /* preserve i for error cleanup */
2198 err
= fwtty_ports_add(serial
);
2200 fwtty_err(&unit
, "no space in port table\n");
2204 for (j
= 0; j
< num_ttys
; ++j
) {
2205 tty_dev
= tty_port_register_device(&serial
->ports
[j
]->port
,
2207 serial
->ports
[j
]->index
,
2209 if (IS_ERR(tty_dev
)) {
2210 err
= PTR_ERR(tty_dev
);
2211 fwtty_err(&unit
, "register tty device error (%d)\n",
2213 goto unregister_ttys
;
2216 serial
->ports
[j
]->device
= tty_dev
;
2218 /* preserve j for error cleanup */
2220 if (create_loop_dev
) {
2221 struct device
*loop_dev
;
2223 loop_dev
= tty_port_register_device(&serial
->ports
[j
]->port
,
2225 loop_idx(serial
->ports
[j
]),
2227 if (IS_ERR(loop_dev
)) {
2228 err
= PTR_ERR(loop_dev
);
2229 fwtty_err(&unit
, "create loop device failed (%d)\n",
2231 goto unregister_ttys
;
2233 serial
->ports
[j
]->device
= loop_dev
;
2234 serial
->ports
[j
]->loopback
= true;
2237 if (!IS_ERR_OR_NULL(fwserial_debugfs
)) {
2238 serial
->debugfs
= debugfs_create_dir(dev_name(&unit
->device
),
2240 if (!IS_ERR_OR_NULL(serial
->debugfs
)) {
2241 debugfs_create_file("peers", 0444, serial
->debugfs
,
2242 serial
, &fwtty_peers_fops
);
2243 debugfs_create_file("stats", 0444, serial
->debugfs
,
2244 serial
, &fwtty_stats_fops
);
2248 list_add_rcu(&serial
->list
, &fwserial_list
);
2250 fwtty_notice(&unit
, "TTY over FireWire on device %s (guid %016llx)\n",
2251 dev_name(card
->device
), (unsigned long long)card
->guid
);
2253 err
= fwserial_add_peer(serial
, unit
);
2257 fwtty_err(&unit
, "unable to add peer unit device (%d)\n", err
);
2259 /* fall-through to error processing */
2260 debugfs_remove_recursive(serial
->debugfs
);
2262 list_del_rcu(&serial
->list
);
2263 if (create_loop_dev
)
2264 tty_unregister_device(fwloop_driver
,
2265 loop_idx(serial
->ports
[j
]));
2267 for (--j
; j
>= 0; --j
)
2268 tty_unregister_device(fwtty_driver
, serial
->ports
[j
]->index
);
2269 kref_put(&serial
->kref
, fwserial_destroy
);
2273 for (--i
; i
>= 0; --i
) {
2274 tty_port_destroy(&serial
->ports
[i
]->port
);
2275 kfree(serial
->ports
[i
]);
2282 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2284 * A 'serial' unit device is created and probed as a result of:
2285 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2286 * 'serial' unit specifier id
2287 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2289 * The firewire core registers unit devices by enumerating unit directories
2290 * of a node's config ROM after reading the config ROM when a new node is
2291 * added to the bus topology after a bus reset.
2293 * The practical implications of this are:
2294 * - this probe is called for both local and remote nodes that have a 'serial'
2295 * unit directory in their config ROM (that matches the specifiers in
2296 * fwserial_id_table).
2297 * - no specific order is enforced for local vs. remote unit devices
2299 * This unit driver copes with the lack of specific order in the same way the
2300 * firewire net driver does -- each probe, for either a local or remote unit
2301 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2302 * first peer created for a given fw_card (tracked by the global fwserial_list)
2303 * creates the underlying TTYs (aggregated in a fw_serial instance).
2305 * NB: an early attempt to differentiate local & remote unit devices by creating
2306 * peers only for remote units and fw_serial instances (with their
2307 * associated TTY devices) only for local units was discarded. Managing
2308 * the peer lifetimes on device removal proved too complicated.
2310 * fwserial_probe/fwserial_remove are effectively serialized by the
2311 * fwserial_list_mutex. This is necessary because the addition of the first peer
2312 * for a given fw_card will trigger the creation of the fw_serial for that
2313 * fw_card, which must not simultaneously contend with the removal of the
2314 * last peer for a given fw_card triggering the destruction of the same
2315 * fw_serial for the same fw_card.
2317 static int fwserial_probe(struct fw_unit
*unit
,
2318 const struct ieee1394_device_id
*id
)
2320 struct fw_serial
*serial
;
2323 mutex_lock(&fwserial_list_mutex
);
2324 serial
= fwserial_lookup(fw_parent_device(unit
)->card
);
2326 err
= fwserial_create(unit
);
2328 err
= fwserial_add_peer(serial
, unit
);
2329 mutex_unlock(&fwserial_list_mutex
);
2334 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2336 * The corresponding 'peer' for this unit device is removed from the list of
2337 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2338 * specific fw_card). If this is the last peer being removed, then trigger
2339 * the destruction of the underlying TTYs.
2341 static void fwserial_remove(struct fw_unit
*unit
)
2343 struct fwtty_peer
*peer
= dev_get_drvdata(&unit
->device
);
2344 struct fw_serial
*serial
= peer
->serial
;
2347 mutex_lock(&fwserial_list_mutex
);
2348 fwserial_remove_peer(peer
);
2350 if (list_empty(&serial
->peer_list
)) {
2351 /* unlink from the fwserial_list here */
2352 list_del_rcu(&serial
->list
);
2354 debugfs_remove_recursive(serial
->debugfs
);
2356 for (i
= 0; i
< num_ttys
; ++i
)
2357 fwserial_close_port(fwtty_driver
, serial
->ports
[i
]);
2358 if (create_loop_dev
)
2359 fwserial_close_port(fwloop_driver
, serial
->ports
[i
]);
2360 kref_put(&serial
->kref
, fwserial_destroy
);
2362 mutex_unlock(&fwserial_list_mutex
);
2366 * fwserial_update: bus update function for 'firewire' serial unit devices
2368 * Updates the new node_id and bus generation for this peer. Note that locking
2369 * is unnecessary; but careful memory barrier usage is important to enforce the
2370 * load and store order of generation & node_id.
2372 * The fw-core orders the write of node_id before generation in the parent
2373 * fw_device to ensure that a stale node_id cannot be used with a current
2374 * bus generation. So the generation value must be read before the node_id.
2376 * In turn, this orders the write of node_id before generation in the peer to
2377 * also ensure a stale node_id cannot be used with a current bus generation.
2379 static void fwserial_update(struct fw_unit
*unit
)
2381 struct fw_device
*parent
= fw_parent_device(unit
);
2382 struct fwtty_peer
*peer
= dev_get_drvdata(&unit
->device
);
2385 generation
= parent
->generation
;
2387 peer
->node_id
= parent
->node_id
;
2389 peer
->generation
= generation
;
2392 static const struct ieee1394_device_id fwserial_id_table
[] = {
2394 .match_flags
= IEEE1394_MATCH_SPECIFIER_ID
|
2395 IEEE1394_MATCH_VERSION
,
2396 .specifier_id
= LINUX_VENDOR_ID
,
2397 .version
= FWSERIAL_VERSION
,
2402 static struct fw_driver fwserial_driver
= {
2404 .owner
= THIS_MODULE
,
2405 .name
= KBUILD_MODNAME
,
2406 .bus
= &fw_bus_type
,
2408 .probe
= fwserial_probe
,
2409 .update
= fwserial_update
,
2410 .remove
= fwserial_remove
,
2411 .id_table
= fwserial_id_table
,
2414 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2415 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2416 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2417 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2418 /* XXX: config ROM definitons could be improved with semi-automated offset
2419 * and length calculation
2421 #define FW_ROM_LEN(quads) ((quads) << 16)
2422 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2424 struct fwserial_unit_directory_data
{
2427 u32 unit_sw_version
;
2428 u32 unit_addr_offset
;
2434 static struct fwserial_unit_directory_data fwserial_unit_directory_data
= {
2435 .len_crc
= FW_ROM_LEN(4),
2436 .unit_specifier
= FW_UNIT_SPECIFIER(LINUX_VENDOR_ID
),
2437 .unit_sw_version
= FW_UNIT_VERSION(FWSERIAL_VERSION
),
2438 .desc1_ofs
= FW_ROM_DESCRIPTOR(1),
2439 .desc1_len_crc
= FW_ROM_LEN(5),
2441 0x00000000, /* type = text */
2442 0x00000000, /* enc = ASCII, lang EN */
2443 0x4c696e75, /* 'Linux TTY' */
2449 static struct fw_descriptor fwserial_unit_directory
= {
2450 .length
= sizeof(fwserial_unit_directory_data
) / sizeof(u32
),
2451 .key
= (CSR_DIRECTORY
| CSR_UNIT
) << 24,
2452 .data
= (u32
*)&fwserial_unit_directory_data
,
2456 * The management address is in the unit space region but above other known
2457 * address users (to keep wild writes from causing havoc)
2459 static const struct fw_address_region fwserial_mgmt_addr_region
= {
2460 .start
= CSR_REGISTER_BASE
+ 0x1e0000ULL
,
2461 .end
= 0x1000000000000ULL
,
2464 static struct fw_address_handler fwserial_mgmt_addr_handler
;
2467 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2468 * @work: ptr to peer->work
2470 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2472 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2473 * already sent to this peer. If so, the collision is resolved by comparing
2474 * guid values; the loser sends the plug response.
2476 * Note: if an error prevents a response, don't do anything -- the
2477 * remote will timeout its request.
2479 static void fwserial_handle_plug_req(struct work_struct
*work
)
2481 struct fwtty_peer
*peer
= to_peer(work
, work
);
2482 struct virt_plug_params
*plug_req
= &peer
->work_params
.plug_req
;
2483 struct fwtty_port
*port
;
2484 struct fwserial_mgmt_pkt
*pkt
;
2487 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
2491 port
= fwserial_find_port(peer
);
2493 spin_lock_bh(&peer
->lock
);
2495 switch (peer
->state
) {
2496 case FWPS_NOT_ATTACHED
:
2498 fwtty_err(&peer
->unit
, "no more ports avail\n");
2499 fill_plug_rsp_nack(pkt
);
2502 fill_plug_rsp_ok(pkt
, peer
->port
);
2503 peer_set_state(peer
, FWPS_PLUG_RESPONDING
);
2504 /* don't release claimed port */
2509 case FWPS_PLUG_PENDING
:
2510 if (peer
->serial
->card
->guid
> peer
->guid
)
2513 /* We lost - hijack the already-claimed port and send ok */
2514 del_timer(&peer
->timer
);
2515 fill_plug_rsp_ok(pkt
, peer
->port
);
2516 peer_set_state(peer
, FWPS_PLUG_RESPONDING
);
2520 fill_plug_rsp_nack(pkt
);
2523 spin_unlock_bh(&peer
->lock
);
2525 fwserial_release_port(port
, false);
2527 rcode
= fwserial_send_mgmt_sync(peer
, pkt
);
2529 spin_lock_bh(&peer
->lock
);
2530 if (peer
->state
== FWPS_PLUG_RESPONDING
) {
2531 if (rcode
== RCODE_COMPLETE
) {
2532 struct fwtty_port
*tmp
= peer
->port
;
2534 fwserial_virt_plug_complete(peer
, plug_req
);
2535 spin_unlock_bh(&peer
->lock
);
2537 fwtty_write_port_status(tmp
);
2538 spin_lock_bh(&peer
->lock
);
2540 fwtty_err(&peer
->unit
, "PLUG_RSP error (%d)\n", rcode
);
2541 port
= peer_revert_state(peer
);
2545 spin_unlock_bh(&peer
->lock
);
2547 fwserial_release_port(port
, false);
2551 static void fwserial_handle_unplug_req(struct work_struct
*work
)
2553 struct fwtty_peer
*peer
= to_peer(work
, work
);
2554 struct fwtty_port
*port
= NULL
;
2555 struct fwserial_mgmt_pkt
*pkt
;
2558 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
2562 spin_lock_bh(&peer
->lock
);
2564 switch (peer
->state
) {
2566 fill_unplug_rsp_ok(pkt
);
2567 peer_set_state(peer
, FWPS_UNPLUG_RESPONDING
);
2570 case FWPS_UNPLUG_PENDING
:
2571 if (peer
->serial
->card
->guid
> peer
->guid
)
2574 /* We lost - send unplug rsp */
2575 del_timer(&peer
->timer
);
2576 fill_unplug_rsp_ok(pkt
);
2577 peer_set_state(peer
, FWPS_UNPLUG_RESPONDING
);
2581 fill_unplug_rsp_nack(pkt
);
2584 spin_unlock_bh(&peer
->lock
);
2586 rcode
= fwserial_send_mgmt_sync(peer
, pkt
);
2588 spin_lock_bh(&peer
->lock
);
2589 if (peer
->state
== FWPS_UNPLUG_RESPONDING
) {
2590 if (rcode
!= RCODE_COMPLETE
)
2591 fwtty_err(&peer
->unit
, "UNPLUG_RSP error (%d)\n",
2593 port
= peer_revert_state(peer
);
2596 spin_unlock_bh(&peer
->lock
);
2598 fwserial_release_port(port
, true);
2602 static int fwserial_parse_mgmt_write(struct fwtty_peer
*peer
,
2603 struct fwserial_mgmt_pkt
*pkt
,
2604 unsigned long long addr
,
2607 struct fwtty_port
*port
= NULL
;
2611 if (addr
!= fwserial_mgmt_addr_handler
.offset
|| len
< sizeof(pkt
->hdr
))
2612 return RCODE_ADDRESS_ERROR
;
2614 if (len
!= be16_to_cpu(pkt
->hdr
.len
) ||
2615 len
!= mgmt_pkt_expected_len(pkt
->hdr
.code
))
2616 return RCODE_DATA_ERROR
;
2618 spin_lock_bh(&peer
->lock
);
2619 if (peer
->state
== FWPS_GONE
) {
2621 * This should never happen - it would mean that the
2622 * remote unit that just wrote this transaction was
2623 * already removed from the bus -- and the removal was
2624 * processed before we rec'd this transaction
2626 fwtty_err(&peer
->unit
, "peer already removed\n");
2627 spin_unlock_bh(&peer
->lock
);
2628 return RCODE_ADDRESS_ERROR
;
2631 rcode
= RCODE_COMPLETE
;
2633 fwtty_dbg(&peer
->unit
, "mgmt: hdr.code: %04hx\n", pkt
->hdr
.code
);
2635 switch (be16_to_cpu(pkt
->hdr
.code
) & FWSC_CODE_MASK
) {
2636 case FWSC_VIRT_CABLE_PLUG
:
2637 if (work_pending(&peer
->work
)) {
2638 fwtty_err(&peer
->unit
, "plug req: busy\n");
2639 rcode
= RCODE_CONFLICT_ERROR
;
2642 peer
->work_params
.plug_req
= pkt
->plug_req
;
2643 peer
->workfn
= fwserial_handle_plug_req
;
2644 queue_work(system_unbound_wq
, &peer
->work
);
2648 case FWSC_VIRT_CABLE_PLUG_RSP
:
2649 if (peer
->state
!= FWPS_PLUG_PENDING
) {
2650 rcode
= RCODE_CONFLICT_ERROR
;
2652 } else if (be16_to_cpu(pkt
->hdr
.code
) & FWSC_RSP_NACK
) {
2653 fwtty_notice(&peer
->unit
, "NACK plug rsp\n");
2654 port
= peer_revert_state(peer
);
2657 struct fwtty_port
*tmp
= peer
->port
;
2659 fwserial_virt_plug_complete(peer
, &pkt
->plug_rsp
);
2660 spin_unlock_bh(&peer
->lock
);
2662 fwtty_write_port_status(tmp
);
2663 spin_lock_bh(&peer
->lock
);
2667 case FWSC_VIRT_CABLE_UNPLUG
:
2668 if (work_pending(&peer
->work
)) {
2669 fwtty_err(&peer
->unit
, "unplug req: busy\n");
2670 rcode
= RCODE_CONFLICT_ERROR
;
2672 peer
->workfn
= fwserial_handle_unplug_req
;
2673 queue_work(system_unbound_wq
, &peer
->work
);
2677 case FWSC_VIRT_CABLE_UNPLUG_RSP
:
2678 if (peer
->state
!= FWPS_UNPLUG_PENDING
) {
2679 rcode
= RCODE_CONFLICT_ERROR
;
2681 if (be16_to_cpu(pkt
->hdr
.code
) & FWSC_RSP_NACK
)
2682 fwtty_notice(&peer
->unit
, "NACK unplug?\n");
2683 port
= peer_revert_state(peer
);
2689 fwtty_err(&peer
->unit
, "unknown mgmt code %d\n",
2690 be16_to_cpu(pkt
->hdr
.code
));
2691 rcode
= RCODE_DATA_ERROR
;
2693 spin_unlock_bh(&peer
->lock
);
2696 fwserial_release_port(port
, reset
);
2702 * fwserial_mgmt_handler: bus address handler for mgmt requests
2703 * @parameters: fw_address_callback_t as specified by firewire core interface
2705 * This handler is responsible for handling virtual cable requests from remotes
2708 static void fwserial_mgmt_handler(struct fw_card
*card
,
2709 struct fw_request
*request
,
2710 int tcode
, int destination
, int source
,
2712 unsigned long long addr
,
2713 void *data
, size_t len
,
2714 void *callback_data
)
2716 struct fwserial_mgmt_pkt
*pkt
= data
;
2717 struct fwtty_peer
*peer
;
2721 peer
= __fwserial_peer_by_node_id(card
, generation
, source
);
2723 fwtty_dbg(card
, "peer(%d:%x) not found\n", generation
, source
);
2724 __dump_peer_list(card
);
2725 rcode
= RCODE_CONFLICT_ERROR
;
2729 case TCODE_WRITE_BLOCK_REQUEST
:
2730 rcode
= fwserial_parse_mgmt_write(peer
, pkt
, addr
, len
);
2734 rcode
= RCODE_TYPE_ERROR
;
2739 fw_send_response(card
, request
, rcode
);
2742 static int __init
fwserial_init(void)
2744 int err
, num_loops
= !!(create_loop_dev
);
2746 /* XXX: placeholder for a "firewire" debugfs node */
2747 fwserial_debugfs
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2749 /* num_ttys/num_ports must not be set above the static alloc avail */
2750 if (num_ttys
+ num_loops
> MAX_CARD_PORTS
)
2751 num_ttys
= MAX_CARD_PORTS
- num_loops
;
2753 num_ports
= num_ttys
+ num_loops
;
2755 fwtty_driver
= tty_alloc_driver(MAX_TOTAL_PORTS
, TTY_DRIVER_REAL_RAW
2756 | TTY_DRIVER_DYNAMIC_DEV
);
2757 if (IS_ERR(fwtty_driver
)) {
2758 err
= PTR_ERR(fwtty_driver
);
2759 goto remove_debugfs
;
2762 fwtty_driver
->driver_name
= KBUILD_MODNAME
;
2763 fwtty_driver
->name
= tty_dev_name
;
2764 fwtty_driver
->major
= 0;
2765 fwtty_driver
->minor_start
= 0;
2766 fwtty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2767 fwtty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2768 fwtty_driver
->init_termios
= tty_std_termios
;
2769 fwtty_driver
->init_termios
.c_cflag
|= CLOCAL
;
2770 tty_set_operations(fwtty_driver
, &fwtty_ops
);
2772 err
= tty_register_driver(fwtty_driver
);
2774 pr_err("register tty driver failed (%d)\n", err
);
2778 if (create_loop_dev
) {
2779 fwloop_driver
= tty_alloc_driver(MAX_TOTAL_PORTS
/ num_ports
,
2781 | TTY_DRIVER_DYNAMIC_DEV
);
2782 if (IS_ERR(fwloop_driver
)) {
2783 err
= PTR_ERR(fwloop_driver
);
2784 goto unregister_driver
;
2787 fwloop_driver
->driver_name
= KBUILD_MODNAME
"_loop";
2788 fwloop_driver
->name
= loop_dev_name
;
2789 fwloop_driver
->major
= 0;
2790 fwloop_driver
->minor_start
= 0;
2791 fwloop_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2792 fwloop_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2793 fwloop_driver
->init_termios
= tty_std_termios
;
2794 fwloop_driver
->init_termios
.c_cflag
|= CLOCAL
;
2795 tty_set_operations(fwloop_driver
, &fwloop_ops
);
2797 err
= tty_register_driver(fwloop_driver
);
2799 pr_err("register loop driver failed (%d)\n", err
);
2804 fwtty_txn_cache
= kmem_cache_create("fwtty_txn_cache",
2805 sizeof(struct fwtty_transaction
),
2807 if (!fwtty_txn_cache
) {
2809 goto unregister_loop
;
2813 * Ideally, this address handler would be registered per local node
2814 * (rather than the same handler for all local nodes). However,
2815 * since the firewire core requires the config rom descriptor *before*
2816 * the local unit device(s) are created, a single management handler
2817 * must suffice for all local serial units.
2819 fwserial_mgmt_addr_handler
.length
= sizeof(struct fwserial_mgmt_pkt
);
2820 fwserial_mgmt_addr_handler
.address_callback
= fwserial_mgmt_handler
;
2822 err
= fw_core_add_address_handler(&fwserial_mgmt_addr_handler
,
2823 &fwserial_mgmt_addr_region
);
2825 pr_err("add management handler failed (%d)\n", err
);
2829 fwserial_unit_directory_data
.unit_addr_offset
=
2830 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler
.offset
);
2831 err
= fw_core_add_descriptor(&fwserial_unit_directory
);
2833 pr_err("add unit descriptor failed (%d)\n", err
);
2834 goto remove_handler
;
2837 err
= driver_register(&fwserial_driver
.driver
);
2839 pr_err("register fwserial driver failed (%d)\n", err
);
2840 goto remove_descriptor
;
2846 fw_core_remove_descriptor(&fwserial_unit_directory
);
2848 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler
);
2850 kmem_cache_destroy(fwtty_txn_cache
);
2852 if (create_loop_dev
)
2853 tty_unregister_driver(fwloop_driver
);
2855 if (create_loop_dev
)
2856 put_tty_driver(fwloop_driver
);
2858 tty_unregister_driver(fwtty_driver
);
2860 put_tty_driver(fwtty_driver
);
2862 debugfs_remove_recursive(fwserial_debugfs
);
2867 static void __exit
fwserial_exit(void)
2869 driver_unregister(&fwserial_driver
.driver
);
2870 fw_core_remove_descriptor(&fwserial_unit_directory
);
2871 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler
);
2872 kmem_cache_destroy(fwtty_txn_cache
);
2873 if (create_loop_dev
) {
2874 tty_unregister_driver(fwloop_driver
);
2875 put_tty_driver(fwloop_driver
);
2877 tty_unregister_driver(fwtty_driver
);
2878 put_tty_driver(fwtty_driver
);
2879 debugfs_remove_recursive(fwserial_debugfs
);
2882 module_init(fwserial_init
);
2883 module_exit(fwserial_exit
);
2885 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2886 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2887 MODULE_LICENSE("GPL");
2888 MODULE_DEVICE_TABLE(ieee1394
, fwserial_id_table
);
2889 MODULE_PARM_DESC(ttys
, "Number of ttys to create for each local firewire node");
2890 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2891 MODULE_PARM_DESC(loop
, "Create a loopback device, fwloop<n>, with ttys");