blob: cad9e98e0e6ab572cdbd98413a83d222d1530cf4 [file] [log] [blame]
Peter Hurley7355ba32012-11-02 08:16:33 -04001/*
2 * FireWire Serial driver
3 *
4 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/device.h>
24#include <linux/mod_devicetable.h>
25#include <linux/rculist.h>
26#include <linux/workqueue.h>
27#include <linux/ratelimit.h>
28#include <linux/bug.h>
29#include <linux/uaccess.h>
30
31#include "fwserial.h"
32
33#define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
34
35#define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
36#define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
37
38/* configurable options */
39static int num_ttys = 4; /* # of std ttys to create per fw_card */
40 /* - doubles as loopback port index */
41static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
42static bool create_loop_dev = true; /* create a loopback device for each card */
Peter Hurley7355ba32012-11-02 08:16:33 -040043
44module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
45module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
46module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
Peter Hurley7355ba32012-11-02 08:16:33 -040047
48/*
49 * Threshold below which the tty is woken for writing
50 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
51 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
52 * our fifo is below this level
53 */
54#define WAKEUP_CHARS 256
55
56/**
57 * fwserial_list: list of every fw_serial created for each fw_card
58 * See discussion in fwserial_probe.
59 */
60static LIST_HEAD(fwserial_list);
61static DEFINE_MUTEX(fwserial_list_mutex);
62
63/**
64 * port_table: array of tty ports allocated to each fw_card
65 *
66 * tty ports are allocated during probe when an fw_serial is first
67 * created for a given fw_card. Ports are allocated in a contiguous block,
68 * each block consisting of 'num_ports' ports.
69 */
70static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
71static DEFINE_MUTEX(port_table_lock);
72static bool port_table_corrupt;
73#define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
74
Peter Hurleyfa1da242013-01-28 22:34:38 -050075#define loop_idx(port) (((port)->index) / num_ports)
76#define table_idx(loop) ((loop) * num_ports + num_ttys)
77
Peter Hurley7355ba32012-11-02 08:16:33 -040078/* total # of tty ports created per fw_card */
79static int num_ports;
80
81/* slab used as pool for struct fwtty_transactions */
82static struct kmem_cache *fwtty_txn_cache;
83
Peter Hurleya3d9ad42013-01-28 22:34:37 -050084struct tty_driver *fwtty_driver;
Peter Hurleyfa1da242013-01-28 22:34:38 -050085static struct tty_driver *fwloop_driver;
Peter Hurleya3d9ad42013-01-28 22:34:37 -050086
Peter Hurley4df5bb02013-01-28 22:34:40 -050087static struct dentry *fwserial_debugfs;
88
Peter Hurley7355ba32012-11-02 08:16:33 -040089struct fwtty_transaction;
90typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
91 void *data, size_t length,
92 struct fwtty_transaction *txn);
93
94struct fwtty_transaction {
95 struct fw_transaction fw_txn;
96 fwtty_transaction_cb callback;
97 struct fwtty_port *port;
98 union {
99 struct dma_pending dma_pended;
100 };
101};
102
103#define to_device(a, b) (a->b)
104#define fwtty_err(p, s, v...) dev_err(to_device(p, device), s, ##v)
105#define fwtty_info(p, s, v...) dev_info(to_device(p, device), s, ##v)
106#define fwtty_notice(p, s, v...) dev_notice(to_device(p, device), s, ##v)
107#define fwtty_dbg(p, s, v...) \
108 dev_dbg(to_device(p, device), "%s: " s, __func__, ##v)
109#define fwtty_err_ratelimited(p, s, v...) \
110 dev_err_ratelimited(to_device(p, device), s, ##v)
111
112#ifdef DEBUG
113static inline void debug_short_write(struct fwtty_port *port, int c, int n)
114{
115 int avail;
116
117 if (n < c) {
118 spin_lock_bh(&port->lock);
119 avail = dma_fifo_avail(&port->tx_fifo);
120 spin_unlock_bh(&port->lock);
121 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d",
122 avail, c, n);
123 }
124}
125#else
126#define debug_short_write(port, c, n)
127#endif
128
129static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
130 int generation, int id);
131
132#ifdef FWTTY_PROFILING
133
134static void profile_fifo_avail(struct fwtty_port *port, unsigned *stat)
135{
136 spin_lock_bh(&port->lock);
137 profile_size_distrib(stat, dma_fifo_avail(&port->tx_fifo));
138 spin_unlock_bh(&port->lock);
139}
140
141static void dump_profile(struct seq_file *m, struct stats *stats)
142{
143 /* for each stat, print sum of 0 to 2^k, then individually */
144 int k = 4;
145 unsigned sum;
146 int j;
147 char t[10];
148
149 snprintf(t, 10, "< %d", 1 << k);
150 seq_printf(m, "\n%14s %6s", " ", t);
151 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
152 seq_printf(m, "%6d", 1 << j);
153
154 ++k;
155 for (j = 0, sum = 0; j <= k; ++j)
156 sum += stats->reads[j];
157 seq_printf(m, "\n%14s: %6d", "reads", sum);
158 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
159 seq_printf(m, "%6d", stats->reads[j]);
160
161 for (j = 0, sum = 0; j <= k; ++j)
162 sum += stats->writes[j];
163 seq_printf(m, "\n%14s: %6d", "writes", sum);
164 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
165 seq_printf(m, "%6d", stats->writes[j]);
166
167 for (j = 0, sum = 0; j <= k; ++j)
168 sum += stats->txns[j];
169 seq_printf(m, "\n%14s: %6d", "txns", sum);
170 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
171 seq_printf(m, "%6d", stats->txns[j]);
172
173 for (j = 0, sum = 0; j <= k; ++j)
174 sum += stats->unthrottle[j];
175 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
176 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
177 seq_printf(m, "%6d", stats->unthrottle[j]);
178}
179
180#else
181#define profile_fifo_avail(port, stat)
182#define dump_profile(m, stats)
183#endif
184
Peter Hurley9883a732013-01-28 20:57:48 -0500185/*
186 * Returns the max receive packet size for the given node
187 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
188 * are required by specification to support max_rec of 8 (512 bytes) or more.
189 */
Peter Hurley7355ba32012-11-02 08:16:33 -0400190static inline int device_max_receive(struct fw_device *fw_device)
191{
Peter Hurley9883a732013-01-28 20:57:48 -0500192 /* see IEEE 1394-2008 table 8-8 */
193 return min(2 << fw_device->max_rec, 4096);
Peter Hurley7355ba32012-11-02 08:16:33 -0400194}
195
196static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
197{
198 switch (rcode) {
199 case RCODE_SEND_ERROR:
200 fwtty_err_ratelimited(port, "card busy");
201 break;
202 case RCODE_ADDRESS_ERROR:
203 fwtty_err_ratelimited(port, "bad unit addr or write length");
204 break;
205 case RCODE_DATA_ERROR:
206 fwtty_err_ratelimited(port, "failed rx");
207 break;
208 case RCODE_NO_ACK:
209 fwtty_err_ratelimited(port, "missing ack");
210 break;
211 case RCODE_BUSY:
212 fwtty_err_ratelimited(port, "remote busy");
213 break;
214 default:
215 fwtty_err_ratelimited(port, "failed tx: %d", rcode);
216 }
217}
218
219static void fwtty_txn_constructor(void *this)
220{
221 struct fwtty_transaction *txn = this;
222
223 init_timer(&txn->fw_txn.split_timeout_timer);
224}
225
226static void fwtty_common_callback(struct fw_card *card, int rcode,
227 void *payload, size_t len, void *cb_data)
228{
229 struct fwtty_transaction *txn = cb_data;
230 struct fwtty_port *port = txn->port;
231
232 if (port && rcode != RCODE_COMPLETE)
233 fwtty_log_tx_error(port, rcode);
234 if (txn->callback)
235 txn->callback(card, rcode, payload, len, txn);
236 kmem_cache_free(fwtty_txn_cache, txn);
237}
238
239static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
240 unsigned long long addr, void *payload,
241 size_t len, fwtty_transaction_cb callback,
242 struct fwtty_port *port)
243{
244 struct fwtty_transaction *txn;
245 int generation;
246
247 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
248 if (!txn)
249 return -ENOMEM;
250
251 txn->callback = callback;
252 txn->port = port;
253
254 generation = peer->generation;
255 smp_rmb();
256 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
257 peer->node_id, generation, peer->speed, addr, payload,
258 len, fwtty_common_callback, txn);
259 return 0;
260}
261
262static void fwtty_send_txn_async(struct fwtty_peer *peer,
263 struct fwtty_transaction *txn, int tcode,
264 unsigned long long addr, void *payload,
265 size_t len, fwtty_transaction_cb callback,
266 struct fwtty_port *port)
267{
268 int generation;
269
270 txn->callback = callback;
271 txn->port = port;
272
273 generation = peer->generation;
274 smp_rmb();
275 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
276 peer->node_id, generation, peer->speed, addr, payload,
277 len, fwtty_common_callback, txn);
278}
279
280
281static void __fwtty_restart_tx(struct fwtty_port *port)
282{
283 int len, avail;
284
285 len = dma_fifo_out_level(&port->tx_fifo);
286 if (len)
287 schedule_delayed_work(&port->drain, 0);
288 avail = dma_fifo_avail(&port->tx_fifo);
289
290 fwtty_dbg(port, "fifo len: %d avail: %d", len, avail);
291}
292
293static void fwtty_restart_tx(struct fwtty_port *port)
294{
295 spin_lock_bh(&port->lock);
296 __fwtty_restart_tx(port);
297 spin_unlock_bh(&port->lock);
298}
299
300/**
301 * fwtty_update_port_status - decodes & dispatches line status changes
302 *
303 * Note: in loopback, the port->lock is being held. Only use functions that
304 * don't attempt to reclaim the port->lock.
305 */
306static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
307{
308 unsigned delta;
309 struct tty_struct *tty;
310
311 /* simulated LSR/MSR status from remote */
312 status &= ~MCTRL_MASK;
313 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
314 delta &= ~(status & TIOCM_RNG);
315 port->mstatus = status;
316
317 if (delta & TIOCM_RNG)
318 ++port->icount.rng;
319 if (delta & TIOCM_DSR)
320 ++port->icount.dsr;
321 if (delta & TIOCM_CAR)
322 ++port->icount.dcd;
323 if (delta & TIOCM_CTS)
324 ++port->icount.cts;
325
326 fwtty_dbg(port, "status: %x delta: %x", status, delta);
327
328 if (delta & TIOCM_CAR) {
329 tty = tty_port_tty_get(&port->port);
330 if (tty && !C_CLOCAL(tty)) {
331 if (status & TIOCM_CAR)
332 wake_up_interruptible(&port->port.open_wait);
333 else
334 schedule_work(&port->hangup);
335 }
336 tty_kref_put(tty);
337 }
338
339 if (delta & TIOCM_CTS) {
340 tty = tty_port_tty_get(&port->port);
341 if (tty && C_CRTSCTS(tty)) {
342 if (tty->hw_stopped) {
343 if (status & TIOCM_CTS) {
344 tty->hw_stopped = 0;
345 if (port->loopback)
346 __fwtty_restart_tx(port);
347 else
348 fwtty_restart_tx(port);
349 }
350 } else {
351 if (~status & TIOCM_CTS)
352 tty->hw_stopped = 1;
353 }
354 }
355 tty_kref_put(tty);
356
357 } else if (delta & OOB_TX_THROTTLE) {
358 tty = tty_port_tty_get(&port->port);
359 if (tty) {
360 if (tty->hw_stopped) {
361 if (~status & OOB_TX_THROTTLE) {
362 tty->hw_stopped = 0;
363 if (port->loopback)
364 __fwtty_restart_tx(port);
365 else
366 fwtty_restart_tx(port);
367 }
368 } else {
369 if (status & OOB_TX_THROTTLE)
370 tty->hw_stopped = 1;
371 }
372 }
373 tty_kref_put(tty);
374 }
375
376 if (delta & (UART_LSR_BI << 24)) {
377 if (status & (UART_LSR_BI << 24)) {
378 port->break_last = jiffies;
379 schedule_delayed_work(&port->emit_breaks, 0);
380 } else {
381 /* run emit_breaks one last time (if pending) */
382 mod_delayed_work(system_wq, &port->emit_breaks, 0);
383 }
384 }
385
386 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
387 wake_up_interruptible(&port->port.delta_msr_wait);
388}
389
390/**
391 * __fwtty_port_line_status - generate 'line status' for indicated port
392 *
393 * This function returns a remote 'MSR' state based on the local 'MCR' state,
394 * as if a null modem cable was attached. The actual status is a mangling
395 * of TIOCM_* bits suitable for sending to a peer's status_addr.
396 *
397 * Note: caller must be holding port lock
398 */
399static unsigned __fwtty_port_line_status(struct fwtty_port *port)
400{
401 unsigned status = 0;
402
403 /* TODO: add module param to tie RNG to DTR as well */
404
405 if (port->mctrl & TIOCM_DTR)
406 status |= TIOCM_DSR | TIOCM_CAR;
407 if (port->mctrl & TIOCM_RTS)
408 status |= TIOCM_CTS;
409 if (port->mctrl & OOB_RX_THROTTLE)
410 status |= OOB_TX_THROTTLE;
411 /* emulate BRK as add'l line status */
412 if (port->break_ctl)
413 status |= UART_LSR_BI << 24;
414
415 return status;
416}
417
418/**
419 * __fwtty_write_port_status - send the port line status to peer
420 *
421 * Note: caller must be holding the port lock.
422 */
423static int __fwtty_write_port_status(struct fwtty_port *port)
424{
425 struct fwtty_peer *peer;
426 int err = -ENOENT;
427 unsigned status = __fwtty_port_line_status(port);
428
429 rcu_read_lock();
430 peer = rcu_dereference(port->peer);
431 if (peer) {
432 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
433 peer->status_addr, &status,
434 sizeof(status), NULL, port);
435 }
436 rcu_read_unlock();
437
438 return err;
439}
440
441/**
442 * fwtty_write_port_status - same as above but locked by port lock
443 */
444static int fwtty_write_port_status(struct fwtty_port *port)
445{
446 int err;
447
448 spin_lock_bh(&port->lock);
449 err = __fwtty_write_port_status(port);
450 spin_unlock_bh(&port->lock);
451 return err;
452}
453
454static void __fwtty_throttle(struct fwtty_port *port, struct tty_struct *tty)
455{
456 unsigned old;
457
458 old = port->mctrl;
459 port->mctrl |= OOB_RX_THROTTLE;
460 if (C_CRTSCTS(tty))
461 port->mctrl &= ~TIOCM_RTS;
462 if (~old & OOB_RX_THROTTLE)
463 __fwtty_write_port_status(port);
464}
465
466/**
467 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
468 *
469 * When the remote has finished tx, and all in-flight rx has been received and
470 * and pushed to the flip buffer, the remote may close its device. This will
471 * drop DTR on the remote which will drop carrier here. Typically, the tty is
472 * hung up when carrier is dropped or lost.
473 *
474 * However, there is a race between the hang up and the line discipline
475 * delivering its data to the reader. A hangup will cause the ldisc to flush
476 * (ie., clear) the read buffer and flip buffer. Because of firewire's
477 * relatively high throughput, the ldisc frequently lags well behind the driver,
478 * resulting in lost data (which has already been received and written to
479 * the flip buffer) when the remote closes its end.
480 *
481 * Unfortunately, since the flip buffer offers no direct method for determining
482 * if it holds data, ensuring the ldisc has delivered all data is problematic.
483 */
484
485/* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
486static void fwtty_do_hangup(struct work_struct *work)
487{
488 struct fwtty_port *port = to_port(work, hangup);
489 struct tty_struct *tty;
490
491 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
492
493 tty = tty_port_tty_get(&port->port);
494 if (tty)
495 tty_vhangup(tty);
496 tty_kref_put(tty);
497}
498
499
500static void fwtty_emit_breaks(struct work_struct *work)
501{
502 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
503 struct tty_struct *tty;
504 static const char buf[16];
505 unsigned long now = jiffies;
506 unsigned long elapsed = now - port->break_last;
507 int n, t, c, brk = 0;
508
509 tty = tty_port_tty_get(&port->port);
510 if (!tty)
511 return;
512
513 /* generate breaks at the line rate (but at least 1) */
514 n = (elapsed * port->cps) / HZ + 1;
515 port->break_last = now;
516
517 fwtty_dbg(port, "sending %d brks", n);
518
519 while (n) {
520 t = min(n, 16);
521 c = tty_insert_flip_string_fixed_flag(tty, buf, TTY_BREAK, t);
522 n -= c;
523 brk += c;
524 if (c < t)
525 break;
526 }
527 tty_flip_buffer_push(tty);
528
529 tty_kref_put(tty);
530
531 if (port->mstatus & (UART_LSR_BI << 24))
532 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
533 port->icount.brk += brk;
534}
535
536static void fwtty_pushrx(struct work_struct *work)
537{
538 struct fwtty_port *port = to_port(work, push);
539 struct tty_struct *tty;
540 struct buffered_rx *buf, *next;
541 int n, c = 0;
542
543 tty = tty_port_tty_get(&port->port);
544 if (!tty)
545 return;
546
547 spin_lock_bh(&port->lock);
548 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
549 n = tty_insert_flip_string_fixed_flag(tty, buf->data,
550 TTY_NORMAL, buf->n);
551 c += n;
552 port->buffered -= n;
553 if (n < buf->n) {
554 if (n > 0) {
555 memmove(buf->data, buf->data + n, buf->n - n);
556 buf->n -= n;
557 }
558 __fwtty_throttle(port, tty);
559 break;
560 } else {
561 list_del(&buf->list);
562 kfree(buf);
563 }
564 }
565 if (c > 0)
566 tty_flip_buffer_push(tty);
567
568 if (list_empty(&port->buf_list))
569 clear_bit(BUFFERING_RX, &port->flags);
570 spin_unlock_bh(&port->lock);
571
572 tty_kref_put(tty);
573}
574
575static int fwtty_buffer_rx(struct fwtty_port *port, unsigned char *d, size_t n)
576{
577 struct buffered_rx *buf;
578 size_t size = (n + sizeof(struct buffered_rx) + 0xFF) & ~0xFF;
579
Peter Hurleyef34dd182013-01-28 22:34:43 -0500580 if (port->buffered + n > HIGH_WATERMARK) {
581 fwtty_err_ratelimited(port, "overflowed rx buffer: buffered: %d new: %ld wtrmk: %d",
582 port->buffered, n, HIGH_WATERMARK);
Peter Hurley7355ba32012-11-02 08:16:33 -0400583 return 0;
Peter Hurleyef34dd182013-01-28 22:34:43 -0500584 }
Peter Hurley7355ba32012-11-02 08:16:33 -0400585 buf = kmalloc(size, GFP_ATOMIC);
586 if (!buf)
587 return 0;
588 INIT_LIST_HEAD(&buf->list);
589 buf->n = n;
590 memcpy(buf->data, d, n);
591
592 spin_lock_bh(&port->lock);
593 list_add_tail(&buf->list, &port->buf_list);
594 port->buffered += n;
595 if (port->buffered > port->stats.watermark)
596 port->stats.watermark = port->buffered;
597 set_bit(BUFFERING_RX, &port->flags);
598 spin_unlock_bh(&port->lock);
599
600 return n;
601}
602
603static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
604{
605 struct tty_struct *tty;
606 int c, n = len;
607 unsigned lsr;
608 int err = 0;
609
610 tty = tty_port_tty_get(&port->port);
611 if (!tty)
612 return -ENOENT;
613
614 fwtty_dbg(port, "%d", n);
615 profile_size_distrib(port->stats.reads, n);
616
617 if (port->write_only) {
618 n = 0;
619 goto out;
620 }
621
622 /* disregard break status; breaks are generated by emit_breaks work */
623 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
624
625 if (port->overrun)
626 lsr |= UART_LSR_OE;
627
628 if (lsr & UART_LSR_OE)
629 ++port->icount.overrun;
630
631 lsr &= port->status_mask;
632 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
633 if (!tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
634 err = -EIO;
635 goto out;
636 }
637 }
638 port->overrun = false;
639
640 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
641 /* TODO: don't drop SAK and Magic SysRq here */
642 n = 0;
643 goto out;
644 }
645
646 if (!test_bit(BUFFERING_RX, &port->flags)) {
647 c = tty_insert_flip_string_fixed_flag(tty, data, TTY_NORMAL, n);
648 if (c > 0)
649 tty_flip_buffer_push(tty);
650 n -= c;
651
652 if (n) {
653 /* start buffering and throttling */
654 n -= fwtty_buffer_rx(port, &data[c], n);
655
656 spin_lock_bh(&port->lock);
657 __fwtty_throttle(port, tty);
658 spin_unlock_bh(&port->lock);
659 }
660 } else
661 n -= fwtty_buffer_rx(port, data, n);
662
663 if (n) {
664 port->overrun = true;
665 err = -EIO;
666 }
667
668out:
669 tty_kref_put(tty);
670
671 port->icount.rx += len;
672 port->stats.lost += n;
673 return err;
674}
675
676/**
677 * fwtty_port_handler - bus address handler for port reads/writes
678 * @parameters: fw_address_callback_t as specified by firewire core interface
679 *
680 * This handler is responsible for handling inbound read/write dma from remotes.
681 */
682static void fwtty_port_handler(struct fw_card *card,
683 struct fw_request *request,
684 int tcode, int destination, int source,
685 int generation,
686 unsigned long long addr,
687 void *data, size_t len,
688 void *callback_data)
689{
690 struct fwtty_port *port = callback_data;
691 struct fwtty_peer *peer;
692 int err;
693 int rcode;
694
695 /* Only accept rx from the peer virtual-cabled to this port */
696 rcu_read_lock();
697 peer = __fwserial_peer_by_node_id(card, generation, source);
698 rcu_read_unlock();
699 if (!peer || peer != rcu_access_pointer(port->peer)) {
700 rcode = RCODE_ADDRESS_ERROR;
701 fwtty_err_ratelimited(port, "ignoring unauthenticated data");
702 goto respond;
703 }
704
705 switch (tcode) {
706 case TCODE_WRITE_QUADLET_REQUEST:
707 if (addr != port->rx_handler.offset || len != 4)
708 rcode = RCODE_ADDRESS_ERROR;
709 else {
710 fwtty_update_port_status(port, *(unsigned *)data);
711 rcode = RCODE_COMPLETE;
712 }
713 break;
714
715 case TCODE_WRITE_BLOCK_REQUEST:
716 if (addr != port->rx_handler.offset + 4 ||
717 len > port->rx_handler.length - 4) {
718 rcode = RCODE_ADDRESS_ERROR;
719 } else {
720 err = fwtty_rx(port, data, len);
721 switch (err) {
722 case 0:
723 rcode = RCODE_COMPLETE;
724 break;
725 case -EIO:
726 rcode = RCODE_DATA_ERROR;
727 break;
728 default:
729 rcode = RCODE_CONFLICT_ERROR;
730 break;
731 }
732 }
733 break;
734
735 default:
736 rcode = RCODE_TYPE_ERROR;
737 }
738
739respond:
740 fw_send_response(card, request, rcode);
741}
742
743/**
744 * fwtty_tx_complete - callback for tx dma
745 * @data: ignored, has no meaning for write txns
746 * @length: ignored, has no meaning for write txns
747 *
748 * The writer must be woken here if the fifo has been emptied because it
749 * may have slept if chars_in_buffer was != 0
750 */
751static void fwtty_tx_complete(struct fw_card *card, int rcode,
752 void *data, size_t length,
753 struct fwtty_transaction *txn)
754{
755 struct fwtty_port *port = txn->port;
756 struct tty_struct *tty;
757 int len;
758
759 fwtty_dbg(port, "rcode: %d", rcode);
760
761 switch (rcode) {
762 case RCODE_COMPLETE:
763 spin_lock_bh(&port->lock);
764 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
765 len = dma_fifo_level(&port->tx_fifo);
766 spin_unlock_bh(&port->lock);
767
768 port->icount.tx += txn->dma_pended.len;
769 break;
770
771 default:
772 /* TODO: implement retries */
773 spin_lock_bh(&port->lock);
774 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
775 len = dma_fifo_level(&port->tx_fifo);
776 spin_unlock_bh(&port->lock);
777
778 port->stats.dropped += txn->dma_pended.len;
779 }
780
781 if (len < WAKEUP_CHARS) {
782 tty = tty_port_tty_get(&port->port);
783 if (tty) {
784 tty_wakeup(tty);
785 tty_kref_put(tty);
786 }
787 }
788}
789
790static int fwtty_tx(struct fwtty_port *port, bool drain)
791{
792 struct fwtty_peer *peer;
793 struct fwtty_transaction *txn;
794 struct tty_struct *tty;
795 int n, len;
796
797 tty = tty_port_tty_get(&port->port);
798 if (!tty)
799 return -ENOENT;
800
801 rcu_read_lock();
802 peer = rcu_dereference(port->peer);
803 if (!peer) {
804 n = -EIO;
805 goto out;
806 }
807
808 if (test_and_set_bit(IN_TX, &port->flags)) {
809 n = -EALREADY;
810 goto out;
811 }
812
813 /* try to write as many dma transactions out as possible */
814 n = -EAGAIN;
815 while (!tty->stopped && !tty->hw_stopped &&
816 !test_bit(STOP_TX, &port->flags)) {
817 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
818 if (!txn) {
819 n = -ENOMEM;
820 break;
821 }
822
823 spin_lock_bh(&port->lock);
824 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
825 spin_unlock_bh(&port->lock);
826
827 fwtty_dbg(port, "out: %u rem: %d", txn->dma_pended.len, n);
828
829 if (n < 0) {
830 kmem_cache_free(fwtty_txn_cache, txn);
831 if (n == -EAGAIN)
832 ++port->stats.tx_stall;
833 else if (n == -ENODATA)
834 profile_size_distrib(port->stats.txns, 0);
835 else {
836 ++port->stats.fifo_errs;
837 fwtty_err_ratelimited(port, "fifo err: %d", n);
838 }
839 break;
840 }
841
842 profile_size_distrib(port->stats.txns, txn->dma_pended.len);
843
844 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
845 peer->fifo_addr, txn->dma_pended.data,
846 txn->dma_pended.len, fwtty_tx_complete,
847 port);
848 ++port->stats.sent;
849
850 /*
851 * Stop tx if the 'last view' of the fifo is empty or if
852 * this is the writer and there's not enough data to bother
853 */
854 if (n == 0 || (!drain && n < WRITER_MINIMUM))
855 break;
856 }
857
858 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
859 spin_lock_bh(&port->lock);
860 len = dma_fifo_out_level(&port->tx_fifo);
861 if (len) {
862 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
863 schedule_delayed_work(&port->drain, delay);
864 }
865 len = dma_fifo_level(&port->tx_fifo);
866 spin_unlock_bh(&port->lock);
867
868 /* wakeup the writer */
869 if (drain && len < WAKEUP_CHARS)
870 tty_wakeup(tty);
871 }
872
873 clear_bit(IN_TX, &port->flags);
874 wake_up_interruptible(&port->wait_tx);
875
876out:
877 rcu_read_unlock();
878 tty_kref_put(tty);
879 return n;
880}
881
882static void fwtty_drain_tx(struct work_struct *work)
883{
884 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
885
886 fwtty_tx(port, true);
887}
888
889static void fwtty_write_xchar(struct fwtty_port *port, char ch)
890{
891 struct fwtty_peer *peer;
892
893 ++port->stats.xchars;
894
895 fwtty_dbg(port, "%02x", ch);
896
897 rcu_read_lock();
898 peer = rcu_dereference(port->peer);
899 if (peer) {
900 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
901 peer->fifo_addr, &ch, sizeof(ch),
902 NULL, port);
903 }
904 rcu_read_unlock();
905}
906
907struct fwtty_port *fwtty_port_get(unsigned index)
908{
909 struct fwtty_port *port;
910
911 if (index >= MAX_TOTAL_PORTS)
912 return NULL;
913
914 mutex_lock(&port_table_lock);
915 port = port_table[index];
916 if (port)
917 kref_get(&port->serial->kref);
918 mutex_unlock(&port_table_lock);
919 return port;
920}
921EXPORT_SYMBOL(fwtty_port_get);
922
923static int fwtty_ports_add(struct fw_serial *serial)
924{
925 int err = -EBUSY;
926 int i, j;
927
928 if (port_table_corrupt)
929 return err;
930
931 mutex_lock(&port_table_lock);
932 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
933 if (!port_table[i]) {
934 for (j = 0; j < num_ports; ++i, ++j) {
935 serial->ports[j]->index = i;
936 port_table[i] = serial->ports[j];
937 }
938 err = 0;
939 break;
940 }
941 }
942 mutex_unlock(&port_table_lock);
943 return err;
944}
945
946static void fwserial_destroy(struct kref *kref)
947{
948 struct fw_serial *serial = to_serial(kref, kref);
949 struct fwtty_port **ports = serial->ports;
950 int j, i = ports[0]->index;
951
952 synchronize_rcu();
953
954 mutex_lock(&port_table_lock);
955 for (j = 0; j < num_ports; ++i, ++j) {
Peter Hurley49b27462012-11-27 21:37:12 -0500956 port_table_corrupt |= port_table[i] != ports[j];
957 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
958 i, port_table[i], j, ports[j]);
Peter Hurley7355ba32012-11-02 08:16:33 -0400959
960 port_table[i] = NULL;
961 }
962 mutex_unlock(&port_table_lock);
963
964 for (j = 0; j < num_ports; ++j) {
965 fw_core_remove_address_handler(&ports[j]->rx_handler);
Peter Hurleya3218462012-11-27 21:37:11 -0500966 tty_port_destroy(&ports[j]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400967 kfree(ports[j]);
968 }
969 kfree(serial);
970}
971
972void fwtty_port_put(struct fwtty_port *port)
973{
974 kref_put(&port->serial->kref, fwserial_destroy);
975}
976EXPORT_SYMBOL(fwtty_port_put);
977
978static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
979{
980 struct fwtty_port *port = to_port(tty_port, port);
981
982 fwtty_dbg(port, "on/off: %d", on);
983
984 spin_lock_bh(&port->lock);
985 /* Don't change carrier state if this is a console */
986 if (!port->port.console) {
987 if (on)
988 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
989 else
990 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
991 }
992
993 __fwtty_write_port_status(port);
994 spin_unlock_bh(&port->lock);
995}
996
997/**
998 * fwtty_port_carrier_raised: required tty_port operation
999 *
1000 * This port operation is polled after a tty has been opened and is waiting for
1001 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
1002 */
1003static int fwtty_port_carrier_raised(struct tty_port *tty_port)
1004{
1005 struct fwtty_port *port = to_port(tty_port, port);
1006 int rc;
1007
1008 rc = (port->mstatus & TIOCM_CAR);
1009
1010 fwtty_dbg(port, "%d", rc);
1011
1012 return rc;
1013}
1014
1015static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
1016{
1017 unsigned baud, frame;
1018
1019 baud = tty_termios_baud_rate(&tty->termios);
1020 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
1021
1022 /* compute bit count of 2 frames */
1023 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
1024
1025 switch (C_CSIZE(tty)) {
1026 case CS5:
1027 frame -= (C_CSTOPB(tty)) ? 1 : 0;
1028 break;
1029 case CS6:
1030 frame += 2;
1031 break;
1032 case CS7:
1033 frame += 4;
1034 break;
1035 case CS8:
1036 frame += 6;
1037 break;
1038 }
1039
1040 port->cps = (baud << 1) / frame;
1041
1042 port->status_mask = UART_LSR_OE;
1043 if (_I_FLAG(tty, BRKINT | PARMRK))
1044 port->status_mask |= UART_LSR_BI;
1045
1046 port->ignore_mask = 0;
1047 if (I_IGNBRK(tty)) {
1048 port->ignore_mask |= UART_LSR_BI;
1049 if (I_IGNPAR(tty))
1050 port->ignore_mask |= UART_LSR_OE;
1051 }
1052
1053 port->write_only = !C_CREAD(tty);
1054
1055 /* turn off echo and newline xlat if loopback */
1056 if (port->loopback) {
1057 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
1058 ECHONL | ECHOPRT | ECHOCTL);
1059 tty->termios.c_oflag &= ~ONLCR;
1060 }
1061
1062 return baud;
1063}
1064
1065static int fwtty_port_activate(struct tty_port *tty_port,
1066 struct tty_struct *tty)
1067{
1068 struct fwtty_port *port = to_port(tty_port, port);
1069 unsigned baud;
1070 int err;
1071
1072 set_bit(TTY_IO_ERROR, &tty->flags);
1073
1074 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
1075 cache_line_size(),
1076 port->max_payload,
1077 FWTTY_PORT_MAX_PEND_DMA,
1078 GFP_KERNEL);
1079 if (err)
1080 return err;
1081
1082 spin_lock_bh(&port->lock);
1083
1084 baud = set_termios(port, tty);
1085
1086 /* if console, don't change carrier state */
1087 if (!port->port.console) {
1088 port->mctrl = 0;
1089 if (baud != 0)
1090 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1091 }
1092
1093 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1094 tty->hw_stopped = 1;
1095
1096 __fwtty_write_port_status(port);
1097 spin_unlock_bh(&port->lock);
1098
1099 clear_bit(TTY_IO_ERROR, &tty->flags);
1100
1101 return 0;
1102}
1103
1104/**
1105 * fwtty_port_shutdown
1106 *
1107 * Note: the tty port core ensures this is not the console and
1108 * manages TTY_IO_ERROR properly
1109 */
1110static void fwtty_port_shutdown(struct tty_port *tty_port)
1111{
1112 struct fwtty_port *port = to_port(tty_port, port);
1113 struct buffered_rx *buf, *next;
1114
1115 /* TODO: cancel outstanding transactions */
1116
1117 cancel_delayed_work_sync(&port->emit_breaks);
1118 cancel_delayed_work_sync(&port->drain);
1119 cancel_work_sync(&port->push);
1120
1121 spin_lock_bh(&port->lock);
1122 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
1123 list_del(&buf->list);
1124 kfree(buf);
1125 }
1126 port->buffered = 0;
1127 port->flags = 0;
1128 port->break_ctl = 0;
1129 port->overrun = 0;
1130 __fwtty_write_port_status(port);
1131 dma_fifo_free(&port->tx_fifo);
1132 spin_unlock_bh(&port->lock);
1133}
1134
1135static int fwtty_open(struct tty_struct *tty, struct file *fp)
1136{
1137 struct fwtty_port *port = tty->driver_data;
1138
1139 return tty_port_open(&port->port, tty, fp);
1140}
1141
1142static void fwtty_close(struct tty_struct *tty, struct file *fp)
1143{
1144 struct fwtty_port *port = tty->driver_data;
1145
1146 tty_port_close(&port->port, tty, fp);
1147}
1148
1149static void fwtty_hangup(struct tty_struct *tty)
1150{
1151 struct fwtty_port *port = tty->driver_data;
1152
1153 tty_port_hangup(&port->port);
1154}
1155
1156static void fwtty_cleanup(struct tty_struct *tty)
1157{
1158 struct fwtty_port *port = tty->driver_data;
1159
1160 tty->driver_data = NULL;
1161 fwtty_port_put(port);
1162}
1163
1164static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1165{
1166 struct fwtty_port *port = fwtty_port_get(tty->index);
1167 int err;
1168
1169 err = tty_standard_install(driver, tty);
1170 if (!err)
1171 tty->driver_data = port;
1172 else
1173 fwtty_port_put(port);
1174 return err;
1175}
1176
Peter Hurleyfa1da242013-01-28 22:34:38 -05001177static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1178{
1179 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1180 int err;
1181
1182 err = tty_standard_install(driver, tty);
1183 if (!err)
1184 tty->driver_data = port;
1185 else
1186 fwtty_port_put(port);
1187 return err;
1188}
1189
Peter Hurley7355ba32012-11-02 08:16:33 -04001190static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1191{
1192 struct fwtty_port *port = tty->driver_data;
1193 int n, len;
1194
1195 fwtty_dbg(port, "%d", c);
1196 profile_size_distrib(port->stats.writes, c);
1197
1198 spin_lock_bh(&port->lock);
1199 n = dma_fifo_in(&port->tx_fifo, buf, c);
1200 len = dma_fifo_out_level(&port->tx_fifo);
1201 if (len < DRAIN_THRESHOLD)
1202 schedule_delayed_work(&port->drain, 1);
1203 spin_unlock_bh(&port->lock);
1204
1205 if (len >= DRAIN_THRESHOLD)
1206 fwtty_tx(port, false);
1207
1208 debug_short_write(port, c, n);
1209
1210 return (n < 0) ? 0 : n;
1211}
1212
1213static int fwtty_write_room(struct tty_struct *tty)
1214{
1215 struct fwtty_port *port = tty->driver_data;
1216 int n;
1217
1218 spin_lock_bh(&port->lock);
1219 n = dma_fifo_avail(&port->tx_fifo);
1220 spin_unlock_bh(&port->lock);
1221
1222 fwtty_dbg(port, "%d", n);
1223
1224 return n;
1225}
1226
1227static int fwtty_chars_in_buffer(struct tty_struct *tty)
1228{
1229 struct fwtty_port *port = tty->driver_data;
1230 int n;
1231
1232 spin_lock_bh(&port->lock);
1233 n = dma_fifo_level(&port->tx_fifo);
1234 spin_unlock_bh(&port->lock);
1235
1236 fwtty_dbg(port, "%d", n);
1237
1238 return n;
1239}
1240
1241static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1242{
1243 struct fwtty_port *port = tty->driver_data;
1244
1245 fwtty_dbg(port, "%02x", ch);
1246
1247 fwtty_write_xchar(port, ch);
1248}
1249
1250static void fwtty_throttle(struct tty_struct *tty)
1251{
1252 struct fwtty_port *port = tty->driver_data;
1253
1254 /*
1255 * Ignore throttling (but not unthrottling).
1256 * It only makes sense to throttle when data will no longer be
1257 * accepted by the tty flip buffer. For example, it is
1258 * possible for received data to overflow the tty buffer long
1259 * before the line discipline ever has a chance to throttle the driver.
1260 * Additionally, the driver may have already completed the I/O
1261 * but the tty buffer is still emptying, so the line discipline is
1262 * throttling and unthrottling nothing.
1263 */
1264
1265 ++port->stats.throttled;
1266}
1267
1268static void fwtty_unthrottle(struct tty_struct *tty)
1269{
1270 struct fwtty_port *port = tty->driver_data;
1271
1272 fwtty_dbg(port, "CRTSCTS: %d", (C_CRTSCTS(tty) != 0));
1273
1274 profile_fifo_avail(port, port->stats.unthrottle);
1275
1276 schedule_work(&port->push);
1277
1278 spin_lock_bh(&port->lock);
1279 port->mctrl &= ~OOB_RX_THROTTLE;
1280 if (C_CRTSCTS(tty))
1281 port->mctrl |= TIOCM_RTS;
1282 __fwtty_write_port_status(port);
1283 spin_unlock_bh(&port->lock);
1284}
1285
1286static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1287 struct async_icount *prev)
1288{
1289 struct async_icount now;
1290 int delta;
1291
1292 now = port->icount;
1293
1294 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1295 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1296 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1297 (mask & TIOCM_CTS && prev->cts != now.cts));
1298
1299 *prev = now;
1300
1301 return delta;
1302}
1303
1304static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1305{
1306 struct async_icount prev;
1307
1308 prev = port->icount;
1309
1310 return wait_event_interruptible(port->port.delta_msr_wait,
1311 check_msr_delta(port, mask, &prev));
1312}
1313
1314static int get_serial_info(struct fwtty_port *port,
1315 struct serial_struct __user *info)
1316{
1317 struct serial_struct tmp;
1318
1319 memset(&tmp, 0, sizeof(tmp));
1320
1321 tmp.type = PORT_UNKNOWN;
1322 tmp.line = port->port.tty->index;
1323 tmp.flags = port->port.flags;
1324 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1325 tmp.baud_base = 400000000;
1326 tmp.close_delay = port->port.close_delay;
1327
1328 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1329}
1330
1331static int set_serial_info(struct fwtty_port *port,
1332 struct serial_struct __user *info)
1333{
1334 struct serial_struct tmp;
1335
1336 if (copy_from_user(&tmp, info, sizeof(tmp)))
1337 return -EFAULT;
1338
1339 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
1340 tmp.baud_base != 400000000)
1341 return -EPERM;
1342
1343 if (!capable(CAP_SYS_ADMIN)) {
1344 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1345 (port->port.flags & ~ASYNC_USR_MASK)))
1346 return -EPERM;
1347 } else
1348 port->port.close_delay = tmp.close_delay * HZ / 100;
1349
1350 return 0;
1351}
1352
1353static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1354 unsigned long arg)
1355{
1356 struct fwtty_port *port = tty->driver_data;
1357 int err;
1358
1359 switch (cmd) {
1360 case TIOCGSERIAL:
1361 mutex_lock(&port->port.mutex);
1362 err = get_serial_info(port, (void __user *)arg);
1363 mutex_unlock(&port->port.mutex);
1364 break;
1365
1366 case TIOCSSERIAL:
1367 mutex_lock(&port->port.mutex);
1368 err = set_serial_info(port, (void __user *)arg);
1369 mutex_unlock(&port->port.mutex);
1370 break;
1371
1372 case TIOCMIWAIT:
1373 err = wait_msr_change(port, arg);
1374 break;
1375
1376 default:
1377 err = -ENOIOCTLCMD;
1378 }
1379
1380 return err;
1381}
1382
1383static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1384{
1385 struct fwtty_port *port = tty->driver_data;
1386 unsigned baud;
1387
1388 spin_lock_bh(&port->lock);
1389 baud = set_termios(port, tty);
1390
1391 if ((baud == 0) && (old->c_cflag & CBAUD))
1392 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1393 else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1394 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1395 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1396 else
1397 port->mctrl |= TIOCM_DTR;
1398 }
1399 __fwtty_write_port_status(port);
1400 spin_unlock_bh(&port->lock);
1401
1402 if (old->c_cflag & CRTSCTS) {
1403 if (!C_CRTSCTS(tty)) {
1404 tty->hw_stopped = 0;
1405 fwtty_restart_tx(port);
1406 }
1407 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1408 tty->hw_stopped = 1;
1409 }
1410}
1411
1412/**
1413 * fwtty_break_ctl - start/stop sending breaks
1414 *
1415 * Signals the remote to start or stop generating simulated breaks.
1416 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1417 * before signalling the break line status. This guarantees any pending rx will
1418 * be queued to the line discipline before break is simulated on the remote.
1419 * Conversely, turning off break_ctl requires signalling the line status change,
1420 * then enabling tx.
1421 */
1422static int fwtty_break_ctl(struct tty_struct *tty, int state)
1423{
1424 struct fwtty_port *port = tty->driver_data;
1425 long ret;
1426
1427 fwtty_dbg(port, "%d", state);
1428
1429 if (state == -1) {
1430 set_bit(STOP_TX, &port->flags);
1431 ret = wait_event_interruptible_timeout(port->wait_tx,
1432 !test_bit(IN_TX, &port->flags),
1433 10);
1434 if (ret == 0 || ret == -ERESTARTSYS) {
1435 clear_bit(STOP_TX, &port->flags);
1436 fwtty_restart_tx(port);
1437 return -EINTR;
1438 }
1439 }
1440
1441 spin_lock_bh(&port->lock);
1442 port->break_ctl = (state == -1);
1443 __fwtty_write_port_status(port);
1444 spin_unlock_bh(&port->lock);
1445
1446 if (state == 0) {
1447 spin_lock_bh(&port->lock);
1448 dma_fifo_reset(&port->tx_fifo);
1449 clear_bit(STOP_TX, &port->flags);
1450 spin_unlock_bh(&port->lock);
1451 }
1452 return 0;
1453}
1454
1455static int fwtty_tiocmget(struct tty_struct *tty)
1456{
1457 struct fwtty_port *port = tty->driver_data;
1458 unsigned tiocm;
1459
1460 spin_lock_bh(&port->lock);
1461 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1462 spin_unlock_bh(&port->lock);
1463
1464 fwtty_dbg(port, "%x", tiocm);
1465
1466 return tiocm;
1467}
1468
1469static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1470{
1471 struct fwtty_port *port = tty->driver_data;
1472
1473 fwtty_dbg(port, "set: %x clear: %x", set, clear);
1474
1475 /* TODO: simulate loopback if TIOCM_LOOP set */
1476
1477 spin_lock_bh(&port->lock);
1478 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1479 port->mctrl |= set & MCTRL_MASK & 0xffff;
1480 __fwtty_write_port_status(port);
1481 spin_unlock_bh(&port->lock);
1482 return 0;
1483}
1484
1485static int fwtty_get_icount(struct tty_struct *tty,
1486 struct serial_icounter_struct *icount)
1487{
1488 struct fwtty_port *port = tty->driver_data;
1489 struct stats stats;
1490
1491 memcpy(&stats, &port->stats, sizeof(stats));
1492 if (port->port.console)
1493 (*port->fwcon_ops->stats)(&stats, port->con_data);
1494
1495 icount->cts = port->icount.cts;
1496 icount->dsr = port->icount.dsr;
1497 icount->rng = port->icount.rng;
1498 icount->dcd = port->icount.dcd;
1499 icount->rx = port->icount.rx;
1500 icount->tx = port->icount.tx + stats.xchars;
1501 icount->frame = port->icount.frame;
1502 icount->overrun = port->icount.overrun;
1503 icount->parity = port->icount.parity;
1504 icount->brk = port->icount.brk;
1505 icount->buf_overrun = port->icount.overrun;
1506 return 0;
1507}
1508
1509static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1510{
1511 struct stats stats;
1512
1513 memcpy(&stats, &port->stats, sizeof(stats));
1514 if (port->port.console)
1515 (*port->fwcon_ops->stats)(&stats, port->con_data);
1516
Peter Hurleye16d1de2013-01-28 22:34:39 -05001517 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1518 port->icount.tx + stats.xchars, port->icount.rx);
Peter Hurley7355ba32012-11-02 08:16:33 -04001519 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1520 port->icount.dsr, port->icount.rng, port->icount.dcd);
1521 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1522 port->icount.overrun, port->icount.parity, port->icount.brk);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001523}
1524
1525static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1526{
1527 struct stats stats;
1528
1529 memcpy(&stats, &port->stats, sizeof(stats));
1530 if (port->port.console)
1531 (*port->fwcon_ops->stats)(&stats, port->con_data);
1532
Peter Hurley7355ba32012-11-02 08:16:33 -04001533 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1534 stats.tx_stall, stats.fifo_errs, stats.lost);
1535 seq_printf(m, " pkts:%d thr:%d wtrmk:%d", stats.sent, stats.throttled,
1536 stats.watermark);
Peter Hurley7355ba32012-11-02 08:16:33 -04001537
1538 if (port->port.console) {
1539 seq_printf(m, "\n ");
1540 (*port->fwcon_ops->proc_show)(m, port->con_data);
1541 }
1542
1543 dump_profile(m, &port->stats);
1544}
1545
Peter Hurleye16d1de2013-01-28 22:34:39 -05001546static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
Peter Hurley7355ba32012-11-02 08:16:33 -04001547{
1548 int generation = peer->generation;
1549
1550 smp_rmb();
1551 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1552 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1553 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1554 peer->max_payload, (unsigned long long) peer->guid);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001555 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
1556 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
Peter Hurley7355ba32012-11-02 08:16:33 -04001557 seq_putc(m, '\n');
1558}
1559
1560static int fwtty_proc_show(struct seq_file *m, void *v)
1561{
1562 struct fwtty_port *port;
Peter Hurley7355ba32012-11-02 08:16:33 -04001563 int i;
1564
1565 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1566 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1567 seq_printf(m, "%2d:", i);
1568 if (capable(CAP_SYS_ADMIN))
1569 fwtty_proc_show_port(m, port);
1570 fwtty_port_put(port);
1571 seq_printf(m, "\n");
1572 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001573 return 0;
1574}
1575
Peter Hurley4df5bb02013-01-28 22:34:40 -05001576static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
1577{
1578 struct fw_serial *serial = m->private;
1579 struct fwtty_port *port;
1580 int i;
1581
1582 for (i = 0; i < num_ports; ++i) {
1583 port = fwtty_port_get(serial->ports[i]->index);
1584 if (port) {
1585 seq_printf(m, "%2d:", port->index);
1586 fwtty_proc_show_port(m, port);
1587 fwtty_debugfs_show_port(m, port);
1588 fwtty_port_put(port);
1589 seq_printf(m, "\n");
1590 }
1591 }
1592 return 0;
1593}
1594
1595static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
1596{
1597 struct fw_serial *serial = m->private;
1598 struct fwtty_peer *peer;
1599
1600 rcu_read_lock();
1601 seq_printf(m, "card: %s guid: %016llx\n",
1602 dev_name(serial->card->device),
1603 (unsigned long long) serial->card->guid);
1604 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1605 fwtty_debugfs_show_peer(m, peer);
1606 rcu_read_unlock();
1607 return 0;
1608}
1609
Peter Hurley7355ba32012-11-02 08:16:33 -04001610static int fwtty_proc_open(struct inode *inode, struct file *fp)
1611{
1612 return single_open(fp, fwtty_proc_show, NULL);
1613}
1614
Peter Hurley4df5bb02013-01-28 22:34:40 -05001615static int fwtty_stats_open(struct inode *inode, struct file *fp)
1616{
1617 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
1618}
1619
1620static int fwtty_peers_open(struct inode *inode, struct file *fp)
1621{
1622 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
1623}
1624
1625static const struct file_operations fwtty_stats_fops = {
1626 .owner = THIS_MODULE,
1627 .open = fwtty_stats_open,
1628 .read = seq_read,
1629 .llseek = seq_lseek,
1630 .release = single_release,
1631};
1632
1633static const struct file_operations fwtty_peers_fops = {
1634 .owner = THIS_MODULE,
1635 .open = fwtty_peers_open,
1636 .read = seq_read,
1637 .llseek = seq_lseek,
1638 .release = single_release,
1639};
1640
Peter Hurley7355ba32012-11-02 08:16:33 -04001641static const struct file_operations fwtty_proc_fops = {
1642 .owner = THIS_MODULE,
1643 .open = fwtty_proc_open,
1644 .read = seq_read,
1645 .llseek = seq_lseek,
1646 .release = single_release,
1647};
1648
1649static const struct tty_port_operations fwtty_port_ops = {
1650 .dtr_rts = fwtty_port_dtr_rts,
1651 .carrier_raised = fwtty_port_carrier_raised,
1652 .shutdown = fwtty_port_shutdown,
1653 .activate = fwtty_port_activate,
1654};
1655
1656static const struct tty_operations fwtty_ops = {
1657 .open = fwtty_open,
1658 .close = fwtty_close,
1659 .hangup = fwtty_hangup,
1660 .cleanup = fwtty_cleanup,
1661 .install = fwtty_install,
1662 .write = fwtty_write,
1663 .write_room = fwtty_write_room,
1664 .chars_in_buffer = fwtty_chars_in_buffer,
1665 .send_xchar = fwtty_send_xchar,
1666 .throttle = fwtty_throttle,
1667 .unthrottle = fwtty_unthrottle,
1668 .ioctl = fwtty_ioctl,
1669 .set_termios = fwtty_set_termios,
1670 .break_ctl = fwtty_break_ctl,
1671 .tiocmget = fwtty_tiocmget,
1672 .tiocmset = fwtty_tiocmset,
1673 .get_icount = fwtty_get_icount,
1674 .proc_fops = &fwtty_proc_fops,
1675};
1676
Peter Hurleyfa1da242013-01-28 22:34:38 -05001677static const struct tty_operations fwloop_ops = {
1678 .open = fwtty_open,
1679 .close = fwtty_close,
1680 .hangup = fwtty_hangup,
1681 .cleanup = fwtty_cleanup,
1682 .install = fwloop_install,
1683 .write = fwtty_write,
1684 .write_room = fwtty_write_room,
1685 .chars_in_buffer = fwtty_chars_in_buffer,
1686 .send_xchar = fwtty_send_xchar,
1687 .throttle = fwtty_throttle,
1688 .unthrottle = fwtty_unthrottle,
1689 .ioctl = fwtty_ioctl,
1690 .set_termios = fwtty_set_termios,
1691 .break_ctl = fwtty_break_ctl,
1692 .tiocmget = fwtty_tiocmget,
1693 .tiocmset = fwtty_tiocmset,
1694 .get_icount = fwtty_get_icount,
1695};
1696
Peter Hurley7355ba32012-11-02 08:16:33 -04001697static inline int mgmt_pkt_expected_len(__be16 code)
1698{
1699 static const struct fwserial_mgmt_pkt pkt;
1700
1701 switch (be16_to_cpu(code)) {
1702 case FWSC_VIRT_CABLE_PLUG:
1703 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1704
1705 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1706 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1707
1708
1709 case FWSC_VIRT_CABLE_UNPLUG:
1710 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1711 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1712 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1713 return sizeof(pkt.hdr);
1714
1715 default:
1716 return -1;
1717 }
1718}
1719
1720static inline void fill_plug_params(struct virt_plug_params *params,
1721 struct fwtty_port *port)
1722{
1723 u64 status_addr = port->rx_handler.offset;
1724 u64 fifo_addr = port->rx_handler.offset + 4;
1725 size_t fifo_len = port->rx_handler.length - 4;
1726
1727 params->status_hi = cpu_to_be32(status_addr >> 32);
1728 params->status_lo = cpu_to_be32(status_addr);
1729 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1730 params->fifo_lo = cpu_to_be32(fifo_addr);
1731 params->fifo_len = cpu_to_be32(fifo_len);
1732}
1733
1734static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1735 struct fwtty_port *port)
1736{
1737 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1738 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1739 fill_plug_params(&pkt->plug_req, port);
1740}
1741
1742static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1743 struct fwtty_port *port)
1744{
1745 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1746 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1747 fill_plug_params(&pkt->plug_rsp, port);
1748}
1749
1750static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1751{
1752 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1753 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1754}
1755
1756static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1757{
1758 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1759 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1760}
1761
1762static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1763{
1764 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1765 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1766}
1767
1768static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1769{
1770 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1771 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1772}
1773
1774static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1775 struct virt_plug_params *params)
1776{
1777 struct fwtty_port *port = peer->port;
1778
1779 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1780 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1781 peer->fifo_len = be32_to_cpu(params->fifo_len);
1782 peer_set_state(peer, FWPS_ATTACHED);
1783
1784 /* reconfigure tx_fifo optimally for this peer */
1785 spin_lock_bh(&port->lock);
Peter Hurley3b1f3152013-01-28 20:57:47 -05001786 port->max_payload = min(peer->max_payload, peer->fifo_len);
Peter Hurley7355ba32012-11-02 08:16:33 -04001787 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1788 spin_unlock_bh(&peer->port->lock);
1789
1790 if (port->port.console && port->fwcon_ops->notify != NULL)
1791 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1792
1793 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s",
1794 (unsigned long long)peer->guid, dev_name(port->device));
1795}
1796
1797static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1798 struct fwserial_mgmt_pkt *pkt)
1799{
1800 int generation;
1801 int rcode, tries = 5;
1802
1803 do {
1804 generation = peer->generation;
1805 smp_rmb();
1806
1807 rcode = fw_run_transaction(peer->serial->card,
1808 TCODE_WRITE_BLOCK_REQUEST,
1809 peer->node_id,
1810 generation, peer->speed,
1811 peer->mgmt_addr,
1812 pkt, be16_to_cpu(pkt->hdr.len));
1813 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1814 rcode == RCODE_GENERATION) {
1815 fwtty_dbg(&peer->unit, "mgmt write error: %d", rcode);
1816 continue;
1817 } else
1818 break;
1819 } while (--tries > 0);
1820 return rcode;
1821}
1822
1823/**
1824 * fwserial_claim_port - attempt to claim port @ index for peer
1825 *
1826 * Returns ptr to claimed port or error code (as ERR_PTR())
1827 * Can sleep - must be called from process context
1828 */
1829static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1830 int index)
1831{
1832 struct fwtty_port *port;
1833
1834 if (index < 0 || index >= num_ports)
1835 return ERR_PTR(-EINVAL);
1836
1837 /* must guarantee that previous port releases have completed */
1838 synchronize_rcu();
1839
1840 port = peer->serial->ports[index];
1841 spin_lock_bh(&port->lock);
1842 if (!rcu_access_pointer(port->peer))
1843 rcu_assign_pointer(port->peer, peer);
1844 else
1845 port = ERR_PTR(-EBUSY);
1846 spin_unlock_bh(&port->lock);
1847
1848 return port;
1849}
1850
1851/**
1852 * fwserial_find_port - find avail port and claim for peer
1853 *
1854 * Returns ptr to claimed port or NULL if none avail
1855 * Can sleep - must be called from process context
1856 */
1857static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1858{
1859 struct fwtty_port **ports = peer->serial->ports;
1860 int i;
1861
1862 /* must guarantee that previous port releases have completed */
1863 synchronize_rcu();
1864
1865 /* TODO: implement optional GUID-to-specific port # matching */
1866
1867 /* find an unattached port (but not the loopback port, if present) */
1868 for (i = 0; i < num_ttys; ++i) {
1869 spin_lock_bh(&ports[i]->lock);
1870 if (!ports[i]->peer) {
1871 /* claim port */
1872 rcu_assign_pointer(ports[i]->peer, peer);
1873 spin_unlock_bh(&ports[i]->lock);
1874 return ports[i];
1875 }
1876 spin_unlock_bh(&ports[i]->lock);
1877 }
1878 return NULL;
1879}
1880
Peter Hurleyde321a12013-01-28 22:34:35 -05001881static void fwserial_release_port(struct fwtty_port *port, bool reset)
Peter Hurley7355ba32012-11-02 08:16:33 -04001882{
1883 /* drop carrier (and all other line status) */
Peter Hurleyde321a12013-01-28 22:34:35 -05001884 if (reset)
1885 fwtty_update_port_status(port, 0);
Peter Hurley7355ba32012-11-02 08:16:33 -04001886
1887 spin_lock_bh(&port->lock);
1888
1889 /* reset dma fifo max transmission size back to S100 */
1890 port->max_payload = link_speed_to_max_payload(SCODE_100);
1891 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1892
1893 rcu_assign_pointer(port->peer, NULL);
1894 spin_unlock_bh(&port->lock);
1895
1896 if (port->port.console && port->fwcon_ops->notify != NULL)
1897 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1898}
1899
1900static void fwserial_plug_timeout(unsigned long data)
1901{
1902 struct fwtty_peer *peer = (struct fwtty_peer *) data;
1903 struct fwtty_port *port;
1904
1905 spin_lock_bh(&peer->lock);
1906 if (peer->state != FWPS_PLUG_PENDING) {
1907 spin_unlock_bh(&peer->lock);
1908 return;
1909 }
1910
1911 port = peer_revert_state(peer);
1912 spin_unlock_bh(&peer->lock);
1913
1914 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05001915 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001916}
1917
1918/**
1919 * fwserial_connect_peer - initiate virtual cable with peer
1920 *
1921 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1922 * otherwise error code. Must be called from process context.
1923 */
1924static int fwserial_connect_peer(struct fwtty_peer *peer)
1925{
1926 struct fwtty_port *port;
1927 struct fwserial_mgmt_pkt *pkt;
1928 int err, rcode;
1929
1930 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1931 if (!pkt)
1932 return -ENOMEM;
1933
1934 port = fwserial_find_port(peer);
1935 if (!port) {
1936 fwtty_err(&peer->unit, "avail ports in use");
1937 err = -EBUSY;
1938 goto free_pkt;
1939 }
1940
1941 spin_lock_bh(&peer->lock);
1942
1943 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1944 if (peer->state != FWPS_NOT_ATTACHED) {
1945 err = -EBUSY;
1946 goto release_port;
1947 }
1948
1949 peer->port = port;
1950 peer_set_state(peer, FWPS_PLUG_PENDING);
1951
1952 fill_plug_req(pkt, peer->port);
1953
1954 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1955 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1956 spin_unlock_bh(&peer->lock);
1957
1958 rcode = fwserial_send_mgmt_sync(peer, pkt);
1959
1960 spin_lock_bh(&peer->lock);
1961 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1962 if (rcode == RCODE_CONFLICT_ERROR)
1963 err = -EAGAIN;
1964 else
1965 err = -EIO;
1966 goto cancel_timer;
1967 }
1968 spin_unlock_bh(&peer->lock);
1969
1970 kfree(pkt);
1971 return 0;
1972
1973cancel_timer:
1974 del_timer(&peer->timer);
1975 peer_revert_state(peer);
1976release_port:
1977 spin_unlock_bh(&peer->lock);
Peter Hurleyde321a12013-01-28 22:34:35 -05001978 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001979free_pkt:
1980 kfree(pkt);
1981 return err;
1982}
1983
1984/**
1985 * fwserial_close_port -
1986 * HUP the tty (if the tty exists) and unregister the tty device.
1987 * Only used by the unit driver upon unit removal to disconnect and
1988 * cleanup all attached ports
1989 *
1990 * The port reference is put by fwtty_cleanup (if a reference was
1991 * ever taken).
1992 */
Peter Hurleyfa1da242013-01-28 22:34:38 -05001993static void fwserial_close_port(struct tty_driver *driver,
1994 struct fwtty_port *port)
Peter Hurley7355ba32012-11-02 08:16:33 -04001995{
1996 struct tty_struct *tty;
1997
1998 mutex_lock(&port->port.mutex);
1999 tty = tty_port_tty_get(&port->port);
2000 if (tty) {
2001 tty_vhangup(tty);
2002 tty_kref_put(tty);
2003 }
2004 mutex_unlock(&port->port.mutex);
2005
Peter Hurleyfa1da242013-01-28 22:34:38 -05002006 if (driver == fwloop_driver)
2007 tty_unregister_device(driver, loop_idx(port));
2008 else
2009 tty_unregister_device(driver, port->index);
Peter Hurley7355ba32012-11-02 08:16:33 -04002010}
2011
2012/**
2013 * fwserial_lookup - finds first fw_serial associated with card
2014 * @card: fw_card to match
2015 *
2016 * NB: caller must be holding fwserial_list_mutex
2017 */
2018static struct fw_serial *fwserial_lookup(struct fw_card *card)
2019{
2020 struct fw_serial *serial;
2021
2022 list_for_each_entry(serial, &fwserial_list, list) {
2023 if (card == serial->card)
2024 return serial;
2025 }
2026
2027 return NULL;
2028}
2029
2030/**
2031 * __fwserial_lookup_rcu - finds first fw_serial associated with card
2032 * @card: fw_card to match
2033 *
2034 * NB: caller must be inside rcu_read_lock() section
2035 */
2036static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
2037{
2038 struct fw_serial *serial;
2039
2040 list_for_each_entry_rcu(serial, &fwserial_list, list) {
2041 if (card == serial->card)
2042 return serial;
2043 }
2044
2045 return NULL;
2046}
2047
2048/**
2049 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
2050 *
2051 * If a matching peer could not be found for the specified generation/node id,
2052 * this could be because:
2053 * a) the generation has changed and one of the nodes hasn't updated yet
2054 * b) the remote node has created its remote unit device before this
2055 * local node has created its corresponding remote unit device
2056 * In either case, the remote node should retry
2057 *
2058 * Note: caller must be in rcu_read_lock() section
2059 */
2060static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
2061 int generation, int id)
2062{
2063 struct fw_serial *serial;
2064 struct fwtty_peer *peer;
2065
2066 serial = __fwserial_lookup_rcu(card);
2067 if (!serial) {
2068 /*
2069 * Something is very wrong - there should be a matching
2070 * fw_serial structure for every fw_card. Maybe the remote node
2071 * has created its remote unit device before this driver has
2072 * been probed for any unit devices...
2073 */
2074 fwtty_err(card, "unknown card (guid %016llx)",
2075 (unsigned long long) card->guid);
2076 return NULL;
2077 }
2078
2079 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2080 int g = peer->generation;
2081 smp_rmb();
2082 if (generation == g && id == peer->node_id)
2083 return peer;
2084 }
2085
2086 return NULL;
2087}
2088
2089#ifdef DEBUG
2090static void __dump_peer_list(struct fw_card *card)
2091{
2092 struct fw_serial *serial;
2093 struct fwtty_peer *peer;
2094
2095 serial = __fwserial_lookup_rcu(card);
2096 if (!serial)
2097 return;
2098
2099 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2100 int g = peer->generation;
2101 smp_rmb();
2102 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n", g,
2103 peer->node_id, (unsigned long long) peer->guid);
2104 }
2105}
2106#else
2107#define __dump_peer_list(s)
2108#endif
2109
2110static void fwserial_auto_connect(struct work_struct *work)
2111{
2112 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2113 int err;
2114
2115 err = fwserial_connect_peer(peer);
2116 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2117 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2118}
2119
2120/**
2121 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2122 * @serial: aggregate representing the specific fw_card to add the peer to
2123 * @unit: 'peer' to create and add to peer_list of serial
2124 *
2125 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2126 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2127 * available tty port. This function is called either directly or indirectly
2128 * as a result of a 'serial' unit device being created & probed.
2129 *
2130 * Note: this function is serialized with fwserial_remove_peer() by the
2131 * fwserial_list_mutex held in fwserial_probe().
2132 *
2133 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2134 * via the dev_set_drvdata() for the device of the fw_unit.
2135 */
2136static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2137{
2138 struct device *dev = &unit->device;
2139 struct fw_device *parent = fw_parent_device(unit);
2140 struct fwtty_peer *peer;
2141 struct fw_csr_iterator ci;
2142 int key, val;
2143 int generation;
2144
2145 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2146 if (!peer)
2147 return -ENOMEM;
2148
2149 peer_set_state(peer, FWPS_NOT_ATTACHED);
2150
2151 dev_set_drvdata(dev, peer);
2152 peer->unit = unit;
2153 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2154 peer->speed = parent->max_speed;
2155 peer->max_payload = min(device_max_receive(parent),
2156 link_speed_to_max_payload(peer->speed));
2157
2158 generation = parent->generation;
2159 smp_rmb();
2160 peer->node_id = parent->node_id;
2161 smp_wmb();
2162 peer->generation = generation;
2163
2164 /* retrieve the mgmt bus addr from the unit directory */
2165 fw_csr_iterator_init(&ci, unit->directory);
2166 while (fw_csr_iterator_next(&ci, &key, &val)) {
2167 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2168 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2169 break;
2170 }
2171 }
2172 if (peer->mgmt_addr == 0ULL) {
2173 /*
2174 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2175 * this peer will not be able to attach to a remote
2176 */
2177 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2178 }
2179
2180 spin_lock_init(&peer->lock);
2181 peer->port = NULL;
2182
2183 init_timer(&peer->timer);
2184 INIT_WORK(&peer->work, NULL);
2185 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2186
2187 /* associate peer with specific fw_card */
2188 peer->serial = serial;
2189 list_add_rcu(&peer->list, &serial->peer_list);
2190
2191 fwtty_info(&peer->unit, "peer added (guid:%016llx)",
2192 (unsigned long long)peer->guid);
2193
2194 /* identify the local unit & virt cable to loopback port */
2195 if (parent->is_local) {
2196 serial->self = peer;
2197 if (create_loop_dev) {
2198 struct fwtty_port *port;
2199 port = fwserial_claim_port(peer, num_ttys);
2200 if (!IS_ERR(port)) {
2201 struct virt_plug_params params;
2202
2203 spin_lock_bh(&peer->lock);
2204 peer->port = port;
2205 fill_plug_params(&params, port);
2206 fwserial_virt_plug_complete(peer, &params);
2207 spin_unlock_bh(&peer->lock);
2208
2209 fwtty_write_port_status(port);
2210 }
2211 }
2212
2213 } else if (auto_connect) {
2214 /* auto-attach to remote units only (if policy allows) */
2215 schedule_delayed_work(&peer->connect, 1);
2216 }
2217
2218 return 0;
2219}
2220
2221/**
2222 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2223 *
2224 * Remove a 'peer' from its list of peers. This function is only
2225 * called by fwserial_remove() on bus removal of the unit device.
2226 *
2227 * Note: this function is serialized with fwserial_add_peer() by the
2228 * fwserial_list_mutex held in fwserial_remove().
2229 */
2230static void fwserial_remove_peer(struct fwtty_peer *peer)
2231{
2232 struct fwtty_port *port;
2233
2234 spin_lock_bh(&peer->lock);
2235 peer_set_state(peer, FWPS_GONE);
2236 spin_unlock_bh(&peer->lock);
2237
2238 cancel_delayed_work_sync(&peer->connect);
2239 cancel_work_sync(&peer->work);
2240
2241 spin_lock_bh(&peer->lock);
2242 /* if this unit is the local unit, clear link */
2243 if (peer == peer->serial->self)
2244 peer->serial->self = NULL;
2245
2246 /* cancel the request timeout timer (if running) */
2247 del_timer(&peer->timer);
2248
2249 port = peer->port;
2250 peer->port = NULL;
2251
2252 list_del_rcu(&peer->list);
2253
2254 fwtty_info(&peer->unit, "peer removed (guid:%016llx)",
2255 (unsigned long long)peer->guid);
2256
2257 spin_unlock_bh(&peer->lock);
2258
2259 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002260 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002261
2262 synchronize_rcu();
2263 kfree(peer);
2264}
2265
2266/**
Peter Hurley7355ba32012-11-02 08:16:33 -04002267 * fwserial_create - init everything to create TTYs for a specific fw_card
2268 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2269 *
2270 * This function inits the aggregate structure (an fw_serial instance)
2271 * used to manage the TTY ports registered by a specific fw_card. Also, the
2272 * unit device is added as the first 'peer'.
2273 *
2274 * This unit device may represent a local unit device (as specified by the
2275 * config ROM unit directory) or it may represent a remote unit device
2276 * (as specified by the reading of the remote node's config ROM).
2277 *
2278 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2279 * value to indicate which error.
2280 */
2281static int fwserial_create(struct fw_unit *unit)
2282{
2283 struct fw_device *parent = fw_parent_device(unit);
2284 struct fw_card *card = parent->card;
2285 struct fw_serial *serial;
2286 struct fwtty_port *port;
2287 struct device *tty_dev;
2288 int i, j;
2289 int err;
2290
2291 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2292 if (!serial)
2293 return -ENOMEM;
2294
2295 kref_init(&serial->kref);
2296 serial->card = card;
2297 INIT_LIST_HEAD(&serial->peer_list);
2298
2299 for (i = 0; i < num_ports; ++i) {
2300 port = kzalloc(sizeof(*port), GFP_KERNEL);
2301 if (!port) {
2302 err = -ENOMEM;
2303 goto free_ports;
2304 }
2305 tty_port_init(&port->port);
2306 port->index = FWTTY_INVALID_INDEX;
2307 port->port.ops = &fwtty_port_ops;
2308 port->serial = serial;
2309
2310 spin_lock_init(&port->lock);
2311 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2312 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2313 INIT_WORK(&port->hangup, fwtty_do_hangup);
2314 INIT_WORK(&port->push, fwtty_pushrx);
2315 INIT_LIST_HEAD(&port->buf_list);
2316 init_waitqueue_head(&port->wait_tx);
2317 port->max_payload = link_speed_to_max_payload(SCODE_100);
2318 dma_fifo_init(&port->tx_fifo);
2319
2320 rcu_assign_pointer(port->peer, NULL);
2321 serial->ports[i] = port;
2322
2323 /* get unique bus addr region for port's status & recv fifo */
2324 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2325 port->rx_handler.address_callback = fwtty_port_handler;
2326 port->rx_handler.callback_data = port;
2327 /*
2328 * XXX: use custom memory region above cpu physical memory addrs
2329 * this will ease porting to 64-bit firewire adapters
2330 */
2331 err = fw_core_add_address_handler(&port->rx_handler,
2332 &fw_high_memory_region);
2333 if (err) {
2334 kfree(port);
2335 goto free_ports;
2336 }
2337 }
2338 /* preserve i for error cleanup */
2339
2340 err = fwtty_ports_add(serial);
2341 if (err) {
2342 fwtty_err(&unit, "no space in port table");
2343 goto free_ports;
2344 }
2345
2346 for (j = 0; j < num_ttys; ++j) {
2347 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2348 fwtty_driver,
2349 serial->ports[j]->index,
2350 card->device);
2351 if (IS_ERR(tty_dev)) {
2352 err = PTR_ERR(tty_dev);
2353 fwtty_err(&unit, "register tty device error (%d)", err);
2354 goto unregister_ttys;
2355 }
2356
2357 serial->ports[j]->device = tty_dev;
2358 }
2359 /* preserve j for error cleanup */
2360
2361 if (create_loop_dev) {
2362 struct device *loop_dev;
2363
Peter Hurleyfa1da242013-01-28 22:34:38 -05002364 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2365 fwloop_driver,
2366 loop_idx(serial->ports[j]),
2367 card->device);
Peter Hurley7355ba32012-11-02 08:16:33 -04002368 if (IS_ERR(loop_dev)) {
2369 err = PTR_ERR(loop_dev);
2370 fwtty_err(&unit, "create loop device failed (%d)", err);
2371 goto unregister_ttys;
2372 }
Peter Hurleyfa1da242013-01-28 22:34:38 -05002373 serial->ports[j]->device = loop_dev;
2374 serial->ports[j]->loopback = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002375 }
2376
Peter Hurley4df5bb02013-01-28 22:34:40 -05002377 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2378 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2379 fwserial_debugfs);
2380 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2381 debugfs_create_file("peers", 0444, serial->debugfs,
2382 serial, &fwtty_peers_fops);
2383 debugfs_create_file("stats", 0444, serial->debugfs,
2384 serial, &fwtty_stats_fops);
2385 }
2386 }
2387
Peter Hurley7355ba32012-11-02 08:16:33 -04002388 list_add_rcu(&serial->list, &fwserial_list);
2389
2390 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)",
2391 dev_name(card->device), (unsigned long long) card->guid);
2392
2393 err = fwserial_add_peer(serial, unit);
2394 if (!err)
2395 return 0;
2396
2397 fwtty_err(&unit, "unable to add peer unit device (%d)", err);
2398
2399 /* fall-through to error processing */
Peter Hurley4df5bb02013-01-28 22:34:40 -05002400 debugfs_remove_recursive(serial->debugfs);
2401
Peter Hurley7355ba32012-11-02 08:16:33 -04002402 list_del_rcu(&serial->list);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002403 if (create_loop_dev)
2404 tty_unregister_device(fwloop_driver, loop_idx(serial->ports[j]));
Peter Hurley7355ba32012-11-02 08:16:33 -04002405unregister_ttys:
2406 for (--j; j >= 0; --j)
2407 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2408 kref_put(&serial->kref, fwserial_destroy);
2409 return err;
2410
2411free_ports:
Peter Hurleya3218462012-11-27 21:37:11 -05002412 for (--i; i >= 0; --i) {
2413 tty_port_destroy(&serial->ports[i]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -04002414 kfree(serial->ports[i]);
Peter Hurleya3218462012-11-27 21:37:11 -05002415 }
Peter Hurley7355ba32012-11-02 08:16:33 -04002416 kfree(serial);
2417 return err;
2418}
2419
2420/**
2421 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2422 *
2423 * A 'serial' unit device is created and probed as a result of:
2424 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2425 * 'serial' unit specifier id
2426 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2427 *
2428 * The firewire core registers unit devices by enumerating unit directories
2429 * of a node's config ROM after reading the config ROM when a new node is
2430 * added to the bus topology after a bus reset.
2431 *
2432 * The practical implications of this are:
2433 * - this probe is called for both local and remote nodes that have a 'serial'
2434 * unit directory in their config ROM (that matches the specifiers in
2435 * fwserial_id_table).
2436 * - no specific order is enforced for local vs. remote unit devices
2437 *
2438 * This unit driver copes with the lack of specific order in the same way the
2439 * firewire net driver does -- each probe, for either a local or remote unit
2440 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2441 * first peer created for a given fw_card (tracked by the global fwserial_list)
2442 * creates the underlying TTYs (aggregated in a fw_serial instance).
2443 *
2444 * NB: an early attempt to differentiate local & remote unit devices by creating
2445 * peers only for remote units and fw_serial instances (with their
2446 * associated TTY devices) only for local units was discarded. Managing
2447 * the peer lifetimes on device removal proved too complicated.
2448 *
2449 * fwserial_probe/fwserial_remove are effectively serialized by the
2450 * fwserial_list_mutex. This is necessary because the addition of the first peer
2451 * for a given fw_card will trigger the creation of the fw_serial for that
2452 * fw_card, which must not simultaneously contend with the removal of the
2453 * last peer for a given fw_card triggering the destruction of the same
2454 * fw_serial for the same fw_card.
2455 */
2456static int fwserial_probe(struct device *dev)
2457{
2458 struct fw_unit *unit = fw_unit(dev);
2459 struct fw_serial *serial;
2460 int err;
2461
2462 mutex_lock(&fwserial_list_mutex);
2463 serial = fwserial_lookup(fw_parent_device(unit)->card);
2464 if (!serial)
2465 err = fwserial_create(unit);
2466 else
2467 err = fwserial_add_peer(serial, unit);
2468 mutex_unlock(&fwserial_list_mutex);
2469 return err;
2470}
2471
2472/**
2473 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2474 *
2475 * The corresponding 'peer' for this unit device is removed from the list of
2476 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2477 * specific fw_card). If this is the last peer being removed, then trigger
2478 * the destruction of the underlying TTYs.
2479 */
2480static int fwserial_remove(struct device *dev)
2481{
2482 struct fwtty_peer *peer = dev_get_drvdata(dev);
2483 struct fw_serial *serial = peer->serial;
2484 int i;
2485
2486 mutex_lock(&fwserial_list_mutex);
2487 fwserial_remove_peer(peer);
2488
2489 if (list_empty(&serial->peer_list)) {
2490 /* unlink from the fwserial_list here */
2491 list_del_rcu(&serial->list);
2492
Peter Hurley4df5bb02013-01-28 22:34:40 -05002493 debugfs_remove_recursive(serial->debugfs);
2494
Peter Hurleyfa1da242013-01-28 22:34:38 -05002495 for (i = 0; i < num_ttys; ++i)
2496 fwserial_close_port(fwtty_driver, serial->ports[i]);
2497 if (create_loop_dev)
2498 fwserial_close_port(fwloop_driver, serial->ports[i]);
Peter Hurley7355ba32012-11-02 08:16:33 -04002499 kref_put(&serial->kref, fwserial_destroy);
2500 }
2501 mutex_unlock(&fwserial_list_mutex);
2502
2503 return 0;
2504}
2505
2506/**
2507 * fwserial_update: bus update function for 'firewire' serial unit devices
2508 *
2509 * Updates the new node_id and bus generation for this peer. Note that locking
2510 * is unnecessary; but careful memory barrier usage is important to enforce the
2511 * load and store order of generation & node_id.
2512 *
2513 * The fw-core orders the write of node_id before generation in the parent
2514 * fw_device to ensure that a stale node_id cannot be used with a current
2515 * bus generation. So the generation value must be read before the node_id.
2516 *
2517 * In turn, this orders the write of node_id before generation in the peer to
2518 * also ensure a stale node_id cannot be used with a current bus generation.
2519 */
2520static void fwserial_update(struct fw_unit *unit)
2521{
2522 struct fw_device *parent = fw_parent_device(unit);
2523 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2524 int generation;
2525
2526 generation = parent->generation;
2527 smp_rmb();
2528 peer->node_id = parent->node_id;
2529 smp_wmb();
2530 peer->generation = generation;
2531}
2532
2533static const struct ieee1394_device_id fwserial_id_table[] = {
2534 {
2535 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2536 IEEE1394_MATCH_VERSION,
2537 .specifier_id = LINUX_VENDOR_ID,
2538 .version = FWSERIAL_VERSION,
2539 },
2540 { }
2541};
2542
2543static struct fw_driver fwserial_driver = {
2544 .driver = {
2545 .owner = THIS_MODULE,
2546 .name = KBUILD_MODNAME,
2547 .bus = &fw_bus_type,
2548 .probe = fwserial_probe,
2549 .remove = fwserial_remove,
2550 },
2551 .update = fwserial_update,
2552 .id_table = fwserial_id_table,
2553};
2554
2555#define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2556#define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2557#define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2558 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2559/* XXX: config ROM definitons could be improved with semi-automated offset
2560 * and length calculation
2561 */
Peter Hurley612588a2013-01-29 09:10:30 -05002562#define FW_ROM_LEN(quads) ((quads) << 16)
Peter Hurley7355ba32012-11-02 08:16:33 -04002563#define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2564
2565struct fwserial_unit_directory_data {
Peter Hurley612588a2013-01-29 09:10:30 -05002566 u32 len_crc;
Peter Hurley7355ba32012-11-02 08:16:33 -04002567 u32 unit_specifier;
2568 u32 unit_sw_version;
2569 u32 unit_addr_offset;
2570 u32 desc1_ofs;
Peter Hurley612588a2013-01-29 09:10:30 -05002571 u32 desc1_len_crc;
Peter Hurley7355ba32012-11-02 08:16:33 -04002572 u32 desc1_data[5];
2573} __packed;
2574
2575static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
Peter Hurley612588a2013-01-29 09:10:30 -05002576 .len_crc = FW_ROM_LEN(4),
Peter Hurley7355ba32012-11-02 08:16:33 -04002577 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2578 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2579 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
Peter Hurley612588a2013-01-29 09:10:30 -05002580 .desc1_len_crc = FW_ROM_LEN(5),
Peter Hurley7355ba32012-11-02 08:16:33 -04002581 .desc1_data = {
2582 0x00000000, /* type = text */
2583 0x00000000, /* enc = ASCII, lang EN */
2584 0x4c696e75, /* 'Linux TTY' */
2585 0x78205454,
2586 0x59000000,
2587 },
2588};
2589
2590static struct fw_descriptor fwserial_unit_directory = {
2591 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2592 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2593 .data = (u32 *)&fwserial_unit_directory_data,
2594};
2595
2596/*
2597 * The management address is in the unit space region but above other known
2598 * address users (to keep wild writes from causing havoc)
2599 */
Peter Hurleya3d9ad42013-01-28 22:34:37 -05002600static const struct fw_address_region fwserial_mgmt_addr_region = {
Peter Hurley7355ba32012-11-02 08:16:33 -04002601 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2602 .end = 0x1000000000000ULL,
2603};
2604
2605static struct fw_address_handler fwserial_mgmt_addr_handler;
2606
2607/**
2608 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2609 * @work: ptr to peer->work
2610 *
2611 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2612 *
2613 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2614 * already sent to this peer. If so, the collision is resolved by comparing
2615 * guid values; the loser sends the plug response.
2616 *
2617 * Note: if an error prevents a response, don't do anything -- the
2618 * remote will timeout its request.
2619 */
2620static void fwserial_handle_plug_req(struct work_struct *work)
2621{
2622 struct fwtty_peer *peer = to_peer(work, work);
2623 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2624 struct fwtty_port *port;
2625 struct fwserial_mgmt_pkt *pkt;
2626 int rcode;
2627
2628 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2629 if (!pkt)
2630 return;
2631
2632 port = fwserial_find_port(peer);
2633
2634 spin_lock_bh(&peer->lock);
2635
2636 switch (peer->state) {
2637 case FWPS_NOT_ATTACHED:
2638 if (!port) {
2639 fwtty_err(&peer->unit, "no more ports avail");
2640 fill_plug_rsp_nack(pkt);
2641 } else {
2642 peer->port = port;
2643 fill_plug_rsp_ok(pkt, peer->port);
2644 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2645 /* don't release claimed port */
2646 port = NULL;
2647 }
2648 break;
2649
2650 case FWPS_PLUG_PENDING:
2651 if (peer->serial->card->guid > peer->guid)
2652 goto cleanup;
2653
2654 /* We lost - hijack the already-claimed port and send ok */
2655 del_timer(&peer->timer);
2656 fill_plug_rsp_ok(pkt, peer->port);
2657 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2658 break;
2659
2660 default:
2661 fill_plug_rsp_nack(pkt);
2662 }
2663
2664 spin_unlock_bh(&peer->lock);
2665 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002666 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002667
2668 rcode = fwserial_send_mgmt_sync(peer, pkt);
2669
2670 spin_lock_bh(&peer->lock);
2671 if (peer->state == FWPS_PLUG_RESPONDING) {
2672 if (rcode == RCODE_COMPLETE) {
2673 struct fwtty_port *tmp = peer->port;
2674
2675 fwserial_virt_plug_complete(peer, plug_req);
2676 spin_unlock_bh(&peer->lock);
2677
2678 fwtty_write_port_status(tmp);
2679 spin_lock_bh(&peer->lock);
2680 } else {
2681 fwtty_err(&peer->unit, "PLUG_RSP error (%d)", rcode);
2682 port = peer_revert_state(peer);
2683 }
2684 }
2685cleanup:
2686 spin_unlock_bh(&peer->lock);
2687 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002688 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002689 kfree(pkt);
2690 return;
2691}
2692
2693static void fwserial_handle_unplug_req(struct work_struct *work)
2694{
2695 struct fwtty_peer *peer = to_peer(work, work);
2696 struct fwtty_port *port = NULL;
2697 struct fwserial_mgmt_pkt *pkt;
2698 int rcode;
2699
2700 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2701 if (!pkt)
2702 return;
2703
2704 spin_lock_bh(&peer->lock);
2705
2706 switch (peer->state) {
2707 case FWPS_ATTACHED:
2708 fill_unplug_rsp_ok(pkt);
2709 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2710 break;
2711
2712 case FWPS_UNPLUG_PENDING:
2713 if (peer->serial->card->guid > peer->guid)
2714 goto cleanup;
2715
2716 /* We lost - send unplug rsp */
2717 del_timer(&peer->timer);
2718 fill_unplug_rsp_ok(pkt);
2719 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2720 break;
2721
2722 default:
2723 fill_unplug_rsp_nack(pkt);
2724 }
2725
2726 spin_unlock_bh(&peer->lock);
2727
2728 rcode = fwserial_send_mgmt_sync(peer, pkt);
2729
2730 spin_lock_bh(&peer->lock);
2731 if (peer->state == FWPS_UNPLUG_RESPONDING) {
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002732 if (rcode != RCODE_COMPLETE)
Peter Hurley7355ba32012-11-02 08:16:33 -04002733 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)", rcode);
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002734 port = peer_revert_state(peer);
Peter Hurley7355ba32012-11-02 08:16:33 -04002735 }
2736cleanup:
2737 spin_unlock_bh(&peer->lock);
2738 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002739 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002740 kfree(pkt);
2741 return;
2742}
2743
2744static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2745 struct fwserial_mgmt_pkt *pkt,
2746 unsigned long long addr,
2747 size_t len)
2748{
2749 struct fwtty_port *port = NULL;
Peter Hurleyde321a12013-01-28 22:34:35 -05002750 bool reset = false;
Peter Hurley7355ba32012-11-02 08:16:33 -04002751 int rcode;
2752
2753 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2754 return RCODE_ADDRESS_ERROR;
2755
2756 if (len != be16_to_cpu(pkt->hdr.len) ||
2757 len != mgmt_pkt_expected_len(pkt->hdr.code))
2758 return RCODE_DATA_ERROR;
2759
2760 spin_lock_bh(&peer->lock);
2761 if (peer->state == FWPS_GONE) {
2762 /*
2763 * This should never happen - it would mean that the
2764 * remote unit that just wrote this transaction was
2765 * already removed from the bus -- and the removal was
2766 * processed before we rec'd this transaction
2767 */
2768 fwtty_err(&peer->unit, "peer already removed");
2769 spin_unlock_bh(&peer->lock);
2770 return RCODE_ADDRESS_ERROR;
2771 }
2772
2773 rcode = RCODE_COMPLETE;
2774
2775 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx", pkt->hdr.code);
2776
2777 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2778 case FWSC_VIRT_CABLE_PLUG:
2779 if (work_pending(&peer->work)) {
2780 fwtty_err(&peer->unit, "plug req: busy");
2781 rcode = RCODE_CONFLICT_ERROR;
2782
2783 } else {
2784 peer->work_params.plug_req = pkt->plug_req;
2785 PREPARE_WORK(&peer->work, fwserial_handle_plug_req);
2786 queue_work(system_unbound_wq, &peer->work);
2787 }
2788 break;
2789
2790 case FWSC_VIRT_CABLE_PLUG_RSP:
2791 if (peer->state != FWPS_PLUG_PENDING) {
2792 rcode = RCODE_CONFLICT_ERROR;
2793
2794 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2795 fwtty_notice(&peer->unit, "NACK plug rsp");
2796 port = peer_revert_state(peer);
2797
2798 } else {
2799 struct fwtty_port *tmp = peer->port;
2800
2801 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2802 spin_unlock_bh(&peer->lock);
2803
2804 fwtty_write_port_status(tmp);
2805 spin_lock_bh(&peer->lock);
2806 }
2807 break;
2808
2809 case FWSC_VIRT_CABLE_UNPLUG:
2810 if (work_pending(&peer->work)) {
2811 fwtty_err(&peer->unit, "unplug req: busy");
2812 rcode = RCODE_CONFLICT_ERROR;
2813 } else {
2814 PREPARE_WORK(&peer->work, fwserial_handle_unplug_req);
2815 queue_work(system_unbound_wq, &peer->work);
2816 }
2817 break;
2818
2819 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2820 if (peer->state != FWPS_UNPLUG_PENDING)
2821 rcode = RCODE_CONFLICT_ERROR;
2822 else {
2823 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2824 fwtty_notice(&peer->unit, "NACK unplug?");
2825 port = peer_revert_state(peer);
Peter Hurleyde321a12013-01-28 22:34:35 -05002826 reset = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002827 }
2828 break;
2829
2830 default:
2831 fwtty_err(&peer->unit, "unknown mgmt code %d",
2832 be16_to_cpu(pkt->hdr.code));
2833 rcode = RCODE_DATA_ERROR;
2834 }
2835 spin_unlock_bh(&peer->lock);
2836
2837 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002838 fwserial_release_port(port, reset);
Peter Hurley7355ba32012-11-02 08:16:33 -04002839
2840 return rcode;
2841}
2842
2843/**
2844 * fwserial_mgmt_handler: bus address handler for mgmt requests
2845 * @parameters: fw_address_callback_t as specified by firewire core interface
2846 *
2847 * This handler is responsible for handling virtual cable requests from remotes
2848 * for all cards.
2849 */
2850static void fwserial_mgmt_handler(struct fw_card *card,
2851 struct fw_request *request,
2852 int tcode, int destination, int source,
2853 int generation,
2854 unsigned long long addr,
2855 void *data, size_t len,
2856 void *callback_data)
2857{
2858 struct fwserial_mgmt_pkt *pkt = data;
2859 struct fwtty_peer *peer;
2860 int rcode;
2861
2862 rcu_read_lock();
2863 peer = __fwserial_peer_by_node_id(card, generation, source);
2864 if (!peer) {
2865 fwtty_dbg(card, "peer(%d:%x) not found", generation, source);
2866 __dump_peer_list(card);
2867 rcode = RCODE_CONFLICT_ERROR;
2868
2869 } else {
2870 switch (tcode) {
2871 case TCODE_WRITE_BLOCK_REQUEST:
2872 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2873 break;
2874
2875 default:
2876 rcode = RCODE_TYPE_ERROR;
2877 }
2878 }
2879
2880 rcu_read_unlock();
2881 fw_send_response(card, request, rcode);
2882}
2883
2884static int __init fwserial_init(void)
2885{
2886 int err, num_loops = !!(create_loop_dev);
2887
Peter Hurley4df5bb02013-01-28 22:34:40 -05002888 /* XXX: placeholder for a "firewire" debugfs node */
2889 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2890
Peter Hurley7355ba32012-11-02 08:16:33 -04002891 /* num_ttys/num_ports must not be set above the static alloc avail */
2892 if (num_ttys + num_loops > MAX_CARD_PORTS)
2893 num_ttys = MAX_CARD_PORTS - num_loops;
2894 num_ports = num_ttys + num_loops;
2895
Peter Hurley84472c32013-01-28 22:34:41 -05002896 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2897 | TTY_DRIVER_DYNAMIC_DEV);
2898 if (IS_ERR(fwtty_driver)) {
2899 err = PTR_ERR(fwtty_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002900 return err;
2901 }
2902
2903 fwtty_driver->driver_name = KBUILD_MODNAME;
2904 fwtty_driver->name = tty_dev_name;
2905 fwtty_driver->major = 0;
2906 fwtty_driver->minor_start = 0;
2907 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2908 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
Peter Hurley7355ba32012-11-02 08:16:33 -04002909 fwtty_driver->init_termios = tty_std_termios;
2910 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2911 tty_set_operations(fwtty_driver, &fwtty_ops);
2912
2913 err = tty_register_driver(fwtty_driver);
2914 if (err) {
2915 driver_err("register tty driver failed (%d)", err);
2916 goto put_tty;
2917 }
2918
Peter Hurleyfa1da242013-01-28 22:34:38 -05002919 if (create_loop_dev) {
Peter Hurley84472c32013-01-28 22:34:41 -05002920 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2921 TTY_DRIVER_REAL_RAW
2922 | TTY_DRIVER_DYNAMIC_DEV);
2923 if (IS_ERR(fwloop_driver)) {
2924 err = PTR_ERR(fwloop_driver);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002925 goto unregister_driver;
2926 }
2927
2928 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2929 fwloop_driver->name = loop_dev_name;
2930 fwloop_driver->major = 0;
2931 fwloop_driver->minor_start = 0;
2932 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2933 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002934 fwloop_driver->init_termios = tty_std_termios;
2935 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2936 tty_set_operations(fwloop_driver, &fwloop_ops);
2937
2938 err = tty_register_driver(fwloop_driver);
2939 if (err) {
2940 driver_err("register loop driver failed (%d)", err);
2941 goto put_loop;
2942 }
2943 }
2944
Peter Hurley7355ba32012-11-02 08:16:33 -04002945 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2946 sizeof(struct fwtty_transaction),
2947 0, 0, fwtty_txn_constructor);
2948 if (!fwtty_txn_cache) {
2949 err = -ENOMEM;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002950 goto unregister_loop;
Peter Hurley7355ba32012-11-02 08:16:33 -04002951 }
2952
2953 /*
2954 * Ideally, this address handler would be registered per local node
2955 * (rather than the same handler for all local nodes). However,
2956 * since the firewire core requires the config rom descriptor *before*
2957 * the local unit device(s) are created, a single management handler
2958 * must suffice for all local serial units.
2959 */
2960 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2961 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2962
2963 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2964 &fwserial_mgmt_addr_region);
2965 if (err) {
2966 driver_err("add management handler failed (%d)", err);
2967 goto destroy_cache;
2968 }
2969
2970 fwserial_unit_directory_data.unit_addr_offset =
2971 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2972 err = fw_core_add_descriptor(&fwserial_unit_directory);
2973 if (err) {
2974 driver_err("add unit descriptor failed (%d)", err);
2975 goto remove_handler;
2976 }
2977
2978 err = driver_register(&fwserial_driver.driver);
2979 if (err) {
2980 driver_err("register fwserial driver failed (%d)", err);
2981 goto remove_descriptor;
2982 }
2983
2984 return 0;
2985
2986remove_descriptor:
2987 fw_core_remove_descriptor(&fwserial_unit_directory);
2988remove_handler:
2989 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2990destroy_cache:
2991 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002992unregister_loop:
2993 if (create_loop_dev)
2994 tty_unregister_driver(fwloop_driver);
2995put_loop:
2996 if (create_loop_dev)
2997 put_tty_driver(fwloop_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002998unregister_driver:
2999 tty_unregister_driver(fwtty_driver);
3000put_tty:
3001 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05003002 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04003003 return err;
3004}
3005
3006static void __exit fwserial_exit(void)
3007{
3008 driver_unregister(&fwserial_driver.driver);
3009 fw_core_remove_descriptor(&fwserial_unit_directory);
3010 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
3011 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05003012 if (create_loop_dev) {
3013 tty_unregister_driver(fwloop_driver);
3014 put_tty_driver(fwloop_driver);
3015 }
Peter Hurley7355ba32012-11-02 08:16:33 -04003016 tty_unregister_driver(fwtty_driver);
3017 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05003018 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04003019}
3020
3021module_init(fwserial_init);
3022module_exit(fwserial_exit);
3023
3024MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
3025MODULE_DESCRIPTION("FireWire Serial TTY Driver");
3026MODULE_LICENSE("GPL");
3027MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
3028MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
3029MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
3030MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");