blob: ebe3259ac4b7f693f4c01886a3f280e52edcdf79 [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
Joe Perches6e8661e2013-05-28 19:44:24 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Peter Hurley7355ba32012-11-02 08:16:33 -040023#include <linux/sched.h>
24#include <linux/slab.h>
25#include <linux/device.h>
26#include <linux/mod_devicetable.h>
27#include <linux/rculist.h>
28#include <linux/workqueue.h>
29#include <linux/ratelimit.h>
30#include <linux/bug.h>
31#include <linux/uaccess.h>
32
33#include "fwserial.h"
34
35#define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
36
37#define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
38#define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
39
40/* configurable options */
41static int num_ttys = 4; /* # of std ttys to create per fw_card */
42 /* - doubles as loopback port index */
43static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
44static bool create_loop_dev = true; /* create a loopback device for each card */
Peter Hurley7355ba32012-11-02 08:16:33 -040045
46module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
47module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
48module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
Peter Hurley7355ba32012-11-02 08:16:33 -040049
50/*
51 * Threshold below which the tty is woken for writing
52 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
53 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
54 * our fifo is below this level
55 */
56#define WAKEUP_CHARS 256
57
58/**
59 * fwserial_list: list of every fw_serial created for each fw_card
60 * See discussion in fwserial_probe.
61 */
62static LIST_HEAD(fwserial_list);
63static DEFINE_MUTEX(fwserial_list_mutex);
64
65/**
66 * port_table: array of tty ports allocated to each fw_card
67 *
68 * tty ports are allocated during probe when an fw_serial is first
69 * created for a given fw_card. Ports are allocated in a contiguous block,
70 * each block consisting of 'num_ports' ports.
71 */
72static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
73static DEFINE_MUTEX(port_table_lock);
74static bool port_table_corrupt;
75#define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
76
Peter Hurleyfa1da242013-01-28 22:34:38 -050077#define loop_idx(port) (((port)->index) / num_ports)
78#define table_idx(loop) ((loop) * num_ports + num_ttys)
79
Peter Hurley7355ba32012-11-02 08:16:33 -040080/* total # of tty ports created per fw_card */
81static int num_ports;
82
83/* slab used as pool for struct fwtty_transactions */
84static struct kmem_cache *fwtty_txn_cache;
85
Peter Hurleya3d9ad42013-01-28 22:34:37 -050086struct tty_driver *fwtty_driver;
Peter Hurleyfa1da242013-01-28 22:34:38 -050087static struct tty_driver *fwloop_driver;
Peter Hurleya3d9ad42013-01-28 22:34:37 -050088
Peter Hurley4df5bb02013-01-28 22:34:40 -050089static struct dentry *fwserial_debugfs;
90
Peter Hurley7355ba32012-11-02 08:16:33 -040091struct fwtty_transaction;
92typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
93 void *data, size_t length,
94 struct fwtty_transaction *txn);
95
96struct fwtty_transaction {
97 struct fw_transaction fw_txn;
98 fwtty_transaction_cb callback;
99 struct fwtty_port *port;
100 union {
101 struct dma_pending dma_pended;
102 };
103};
104
105#define to_device(a, b) (a->b)
Joe Perches6e8661e2013-05-28 19:44:24 -0700106#define fwtty_err(p, fmt, ...) \
107 dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
108#define fwtty_info(p, fmt, ...) \
109 dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
110#define fwtty_notice(p, fmt, ...) \
111 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
112#define fwtty_dbg(p, fmt, ...) \
113 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
114#define fwtty_err_ratelimited(p, fmt, ...) \
115 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
Peter Hurley7355ba32012-11-02 08:16:33 -0400116
117#ifdef DEBUG
118static inline void debug_short_write(struct fwtty_port *port, int c, int n)
119{
120 int avail;
121
122 if (n < c) {
123 spin_lock_bh(&port->lock);
124 avail = dma_fifo_avail(&port->tx_fifo);
125 spin_unlock_bh(&port->lock);
Joe Perches6e8661e2013-05-28 19:44:24 -0700126 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d\n",
Peter Hurley7355ba32012-11-02 08:16:33 -0400127 avail, c, n);
128 }
129}
130#else
131#define debug_short_write(port, c, n)
132#endif
133
134static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
135 int generation, int id);
136
137#ifdef FWTTY_PROFILING
138
Peter Hurley49bb8402013-11-22 13:06:10 -0500139static void fwtty_profile_fifo(struct fwtty_port *port, unsigned *stat)
Peter Hurley7355ba32012-11-02 08:16:33 -0400140{
141 spin_lock_bh(&port->lock);
Peter Hurley49bb8402013-11-22 13:06:10 -0500142 fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo));
Peter Hurley7355ba32012-11-02 08:16:33 -0400143 spin_unlock_bh(&port->lock);
144}
145
Peter Hurley49bb8402013-11-22 13:06:10 -0500146static void fwtty_dump_profile(struct seq_file *m, struct stats *stats)
Peter Hurley7355ba32012-11-02 08:16:33 -0400147{
148 /* for each stat, print sum of 0 to 2^k, then individually */
149 int k = 4;
150 unsigned sum;
151 int j;
152 char t[10];
153
154 snprintf(t, 10, "< %d", 1 << k);
155 seq_printf(m, "\n%14s %6s", " ", t);
156 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
157 seq_printf(m, "%6d", 1 << j);
158
159 ++k;
160 for (j = 0, sum = 0; j <= k; ++j)
161 sum += stats->reads[j];
162 seq_printf(m, "\n%14s: %6d", "reads", sum);
163 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
164 seq_printf(m, "%6d", stats->reads[j]);
165
166 for (j = 0, sum = 0; j <= k; ++j)
167 sum += stats->writes[j];
168 seq_printf(m, "\n%14s: %6d", "writes", sum);
169 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
170 seq_printf(m, "%6d", stats->writes[j]);
171
172 for (j = 0, sum = 0; j <= k; ++j)
173 sum += stats->txns[j];
174 seq_printf(m, "\n%14s: %6d", "txns", sum);
175 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
176 seq_printf(m, "%6d", stats->txns[j]);
177
178 for (j = 0, sum = 0; j <= k; ++j)
179 sum += stats->unthrottle[j];
180 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
181 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
182 seq_printf(m, "%6d", stats->unthrottle[j]);
183}
184
185#else
Peter Hurley49bb8402013-11-22 13:06:10 -0500186#define fwtty_profile_fifo(port, stat)
187#define fwtty_dump_profile(m, stats)
Peter Hurley7355ba32012-11-02 08:16:33 -0400188#endif
189
Peter Hurley9883a732013-01-28 20:57:48 -0500190/*
191 * Returns the max receive packet size for the given node
192 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
193 * are required by specification to support max_rec of 8 (512 bytes) or more.
194 */
Peter Hurley7355ba32012-11-02 08:16:33 -0400195static inline int device_max_receive(struct fw_device *fw_device)
196{
Peter Hurley9883a732013-01-28 20:57:48 -0500197 /* see IEEE 1394-2008 table 8-8 */
198 return min(2 << fw_device->max_rec, 4096);
Peter Hurley7355ba32012-11-02 08:16:33 -0400199}
200
201static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
202{
203 switch (rcode) {
204 case RCODE_SEND_ERROR:
Joe Perches6e8661e2013-05-28 19:44:24 -0700205 fwtty_err_ratelimited(port, "card busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400206 break;
207 case RCODE_ADDRESS_ERROR:
Joe Perches6e8661e2013-05-28 19:44:24 -0700208 fwtty_err_ratelimited(port, "bad unit addr or write length\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400209 break;
210 case RCODE_DATA_ERROR:
Joe Perches6e8661e2013-05-28 19:44:24 -0700211 fwtty_err_ratelimited(port, "failed rx\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400212 break;
213 case RCODE_NO_ACK:
Joe Perches6e8661e2013-05-28 19:44:24 -0700214 fwtty_err_ratelimited(port, "missing ack\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400215 break;
216 case RCODE_BUSY:
Joe Perches6e8661e2013-05-28 19:44:24 -0700217 fwtty_err_ratelimited(port, "remote busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400218 break;
219 default:
Joe Perches6e8661e2013-05-28 19:44:24 -0700220 fwtty_err_ratelimited(port, "failed tx: %d\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -0400221 }
222}
223
224static void fwtty_txn_constructor(void *this)
225{
226 struct fwtty_transaction *txn = this;
227
228 init_timer(&txn->fw_txn.split_timeout_timer);
229}
230
231static void fwtty_common_callback(struct fw_card *card, int rcode,
232 void *payload, size_t len, void *cb_data)
233{
234 struct fwtty_transaction *txn = cb_data;
235 struct fwtty_port *port = txn->port;
236
237 if (port && rcode != RCODE_COMPLETE)
238 fwtty_log_tx_error(port, rcode);
239 if (txn->callback)
240 txn->callback(card, rcode, payload, len, txn);
241 kmem_cache_free(fwtty_txn_cache, txn);
242}
243
244static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
245 unsigned long long addr, void *payload,
246 size_t len, fwtty_transaction_cb callback,
247 struct fwtty_port *port)
248{
249 struct fwtty_transaction *txn;
250 int generation;
251
252 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
253 if (!txn)
254 return -ENOMEM;
255
256 txn->callback = callback;
257 txn->port = port;
258
259 generation = peer->generation;
260 smp_rmb();
261 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
262 peer->node_id, generation, peer->speed, addr, payload,
263 len, fwtty_common_callback, txn);
264 return 0;
265}
266
267static void fwtty_send_txn_async(struct fwtty_peer *peer,
268 struct fwtty_transaction *txn, int tcode,
269 unsigned long long addr, void *payload,
270 size_t len, fwtty_transaction_cb callback,
271 struct fwtty_port *port)
272{
273 int generation;
274
275 txn->callback = callback;
276 txn->port = port;
277
278 generation = peer->generation;
279 smp_rmb();
280 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
281 peer->node_id, generation, peer->speed, addr, payload,
282 len, fwtty_common_callback, txn);
283}
284
285
286static void __fwtty_restart_tx(struct fwtty_port *port)
287{
288 int len, avail;
289
290 len = dma_fifo_out_level(&port->tx_fifo);
291 if (len)
292 schedule_delayed_work(&port->drain, 0);
293 avail = dma_fifo_avail(&port->tx_fifo);
294
Joe Perches6e8661e2013-05-28 19:44:24 -0700295 fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail);
Peter Hurley7355ba32012-11-02 08:16:33 -0400296}
297
298static void fwtty_restart_tx(struct fwtty_port *port)
299{
300 spin_lock_bh(&port->lock);
301 __fwtty_restart_tx(port);
302 spin_unlock_bh(&port->lock);
303}
304
305/**
306 * fwtty_update_port_status - decodes & dispatches line status changes
307 *
308 * Note: in loopback, the port->lock is being held. Only use functions that
309 * don't attempt to reclaim the port->lock.
310 */
311static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
312{
313 unsigned delta;
314 struct tty_struct *tty;
315
316 /* simulated LSR/MSR status from remote */
317 status &= ~MCTRL_MASK;
318 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
319 delta &= ~(status & TIOCM_RNG);
320 port->mstatus = status;
321
322 if (delta & TIOCM_RNG)
323 ++port->icount.rng;
324 if (delta & TIOCM_DSR)
325 ++port->icount.dsr;
326 if (delta & TIOCM_CAR)
327 ++port->icount.dcd;
328 if (delta & TIOCM_CTS)
329 ++port->icount.cts;
330
Joe Perches6e8661e2013-05-28 19:44:24 -0700331 fwtty_dbg(port, "status: %x delta: %x\n", status, delta);
Peter Hurley7355ba32012-11-02 08:16:33 -0400332
333 if (delta & TIOCM_CAR) {
334 tty = tty_port_tty_get(&port->port);
335 if (tty && !C_CLOCAL(tty)) {
336 if (status & TIOCM_CAR)
337 wake_up_interruptible(&port->port.open_wait);
338 else
339 schedule_work(&port->hangup);
340 }
341 tty_kref_put(tty);
342 }
343
344 if (delta & TIOCM_CTS) {
345 tty = tty_port_tty_get(&port->port);
346 if (tty && C_CRTSCTS(tty)) {
347 if (tty->hw_stopped) {
348 if (status & TIOCM_CTS) {
349 tty->hw_stopped = 0;
350 if (port->loopback)
351 __fwtty_restart_tx(port);
352 else
353 fwtty_restart_tx(port);
354 }
355 } else {
356 if (~status & TIOCM_CTS)
357 tty->hw_stopped = 1;
358 }
359 }
360 tty_kref_put(tty);
361
362 } else if (delta & OOB_TX_THROTTLE) {
363 tty = tty_port_tty_get(&port->port);
364 if (tty) {
365 if (tty->hw_stopped) {
366 if (~status & OOB_TX_THROTTLE) {
367 tty->hw_stopped = 0;
368 if (port->loopback)
369 __fwtty_restart_tx(port);
370 else
371 fwtty_restart_tx(port);
372 }
373 } else {
374 if (status & OOB_TX_THROTTLE)
375 tty->hw_stopped = 1;
376 }
377 }
378 tty_kref_put(tty);
379 }
380
381 if (delta & (UART_LSR_BI << 24)) {
382 if (status & (UART_LSR_BI << 24)) {
383 port->break_last = jiffies;
384 schedule_delayed_work(&port->emit_breaks, 0);
385 } else {
386 /* run emit_breaks one last time (if pending) */
387 mod_delayed_work(system_wq, &port->emit_breaks, 0);
388 }
389 }
390
391 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
392 wake_up_interruptible(&port->port.delta_msr_wait);
393}
394
395/**
396 * __fwtty_port_line_status - generate 'line status' for indicated port
397 *
398 * This function returns a remote 'MSR' state based on the local 'MCR' state,
399 * as if a null modem cable was attached. The actual status is a mangling
400 * of TIOCM_* bits suitable for sending to a peer's status_addr.
401 *
402 * Note: caller must be holding port lock
403 */
404static unsigned __fwtty_port_line_status(struct fwtty_port *port)
405{
406 unsigned status = 0;
407
408 /* TODO: add module param to tie RNG to DTR as well */
409
410 if (port->mctrl & TIOCM_DTR)
411 status |= TIOCM_DSR | TIOCM_CAR;
412 if (port->mctrl & TIOCM_RTS)
413 status |= TIOCM_CTS;
414 if (port->mctrl & OOB_RX_THROTTLE)
415 status |= OOB_TX_THROTTLE;
416 /* emulate BRK as add'l line status */
417 if (port->break_ctl)
418 status |= UART_LSR_BI << 24;
419
420 return status;
421}
422
423/**
424 * __fwtty_write_port_status - send the port line status to peer
425 *
426 * Note: caller must be holding the port lock.
427 */
428static int __fwtty_write_port_status(struct fwtty_port *port)
429{
430 struct fwtty_peer *peer;
431 int err = -ENOENT;
432 unsigned status = __fwtty_port_line_status(port);
433
434 rcu_read_lock();
435 peer = rcu_dereference(port->peer);
436 if (peer) {
437 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
438 peer->status_addr, &status,
439 sizeof(status), NULL, port);
440 }
441 rcu_read_unlock();
442
443 return err;
444}
445
446/**
447 * fwtty_write_port_status - same as above but locked by port lock
448 */
449static int fwtty_write_port_status(struct fwtty_port *port)
450{
451 int err;
452
453 spin_lock_bh(&port->lock);
454 err = __fwtty_write_port_status(port);
455 spin_unlock_bh(&port->lock);
456 return err;
457}
458
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500459static void fwtty_throttle_port(struct fwtty_port *port)
Peter Hurley7355ba32012-11-02 08:16:33 -0400460{
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500461 struct tty_struct *tty;
Peter Hurley7355ba32012-11-02 08:16:33 -0400462 unsigned old;
463
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500464 tty = tty_port_tty_get(&port->port);
465 if (!tty)
466 return;
467
468 spin_lock_bh(&port->lock);
469
Peter Hurley7355ba32012-11-02 08:16:33 -0400470 old = port->mctrl;
471 port->mctrl |= OOB_RX_THROTTLE;
472 if (C_CRTSCTS(tty))
473 port->mctrl &= ~TIOCM_RTS;
474 if (~old & OOB_RX_THROTTLE)
475 __fwtty_write_port_status(port);
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500476
477 spin_unlock_bh(&port->lock);
478
479 tty_kref_put(tty);
Peter Hurley7355ba32012-11-02 08:16:33 -0400480}
481
482/**
483 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
484 *
485 * When the remote has finished tx, and all in-flight rx has been received and
486 * and pushed to the flip buffer, the remote may close its device. This will
487 * drop DTR on the remote which will drop carrier here. Typically, the tty is
488 * hung up when carrier is dropped or lost.
489 *
490 * However, there is a race between the hang up and the line discipline
491 * delivering its data to the reader. A hangup will cause the ldisc to flush
492 * (ie., clear) the read buffer and flip buffer. Because of firewire's
493 * relatively high throughput, the ldisc frequently lags well behind the driver,
494 * resulting in lost data (which has already been received and written to
495 * the flip buffer) when the remote closes its end.
496 *
497 * Unfortunately, since the flip buffer offers no direct method for determining
498 * if it holds data, ensuring the ldisc has delivered all data is problematic.
499 */
500
501/* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
502static void fwtty_do_hangup(struct work_struct *work)
503{
504 struct fwtty_port *port = to_port(work, hangup);
505 struct tty_struct *tty;
506
507 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
508
509 tty = tty_port_tty_get(&port->port);
510 if (tty)
511 tty_vhangup(tty);
512 tty_kref_put(tty);
513}
514
515
516static void fwtty_emit_breaks(struct work_struct *work)
517{
518 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
Peter Hurley7355ba32012-11-02 08:16:33 -0400519 static const char buf[16];
520 unsigned long now = jiffies;
521 unsigned long elapsed = now - port->break_last;
522 int n, t, c, brk = 0;
523
Peter Hurley7355ba32012-11-02 08:16:33 -0400524 /* generate breaks at the line rate (but at least 1) */
525 n = (elapsed * port->cps) / HZ + 1;
526 port->break_last = now;
527
Joe Perches6e8661e2013-05-28 19:44:24 -0700528 fwtty_dbg(port, "sending %d brks\n", n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400529
530 while (n) {
531 t = min(n, 16);
Jiri Slaby2f693352013-01-03 15:53:02 +0100532 c = tty_insert_flip_string_fixed_flag(&port->port, buf,
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +0200533 TTY_BREAK, t);
Peter Hurley7355ba32012-11-02 08:16:33 -0400534 n -= c;
535 brk += c;
536 if (c < t)
537 break;
538 }
Jiri Slaby2e124b42013-01-03 15:53:06 +0100539 tty_flip_buffer_push(&port->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400540
541 if (port->mstatus & (UART_LSR_BI << 24))
542 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
543 port->icount.brk += brk;
544}
545
Peter Hurley7355ba32012-11-02 08:16:33 -0400546static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
547{
Peter Hurley7355ba32012-11-02 08:16:33 -0400548 int c, n = len;
549 unsigned lsr;
550 int err = 0;
551
Joe Perches6e8661e2013-05-28 19:44:24 -0700552 fwtty_dbg(port, "%d\n", n);
Peter Hurley49bb8402013-11-22 13:06:10 -0500553 fwtty_profile_data(port->stats.reads, n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400554
555 if (port->write_only) {
556 n = 0;
557 goto out;
558 }
559
560 /* disregard break status; breaks are generated by emit_breaks work */
561 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
562
563 if (port->overrun)
564 lsr |= UART_LSR_OE;
565
566 if (lsr & UART_LSR_OE)
567 ++port->icount.overrun;
568
569 lsr &= port->status_mask;
570 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100571 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400572 err = -EIO;
573 goto out;
574 }
575 }
576 port->overrun = false;
577
578 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
579 /* TODO: don't drop SAK and Magic SysRq here */
580 n = 0;
581 goto out;
582 }
583
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500584 c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n);
585 if (c > 0)
586 tty_flip_buffer_push(&port->port);
587 n -= c;
Peter Hurley7355ba32012-11-02 08:16:33 -0400588
589 if (n) {
590 port->overrun = true;
591 err = -EIO;
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500592 fwtty_err_ratelimited(port, "flip buffer overrun\n");
593
594 } else {
595 /* throttle the sender if remaining flip buffer space has
596 * reached high watermark to avoid losing data which may be
597 * in-flight. Since the AR request context is 32k, that much
598 * data may have _already_ been acked.
599 */
600 if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK)
601 fwtty_throttle_port(port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400602 }
603
604out:
Peter Hurley7355ba32012-11-02 08:16:33 -0400605 port->icount.rx += len;
606 port->stats.lost += n;
607 return err;
608}
609
610/**
611 * fwtty_port_handler - bus address handler for port reads/writes
612 * @parameters: fw_address_callback_t as specified by firewire core interface
613 *
614 * This handler is responsible for handling inbound read/write dma from remotes.
615 */
616static void fwtty_port_handler(struct fw_card *card,
617 struct fw_request *request,
618 int tcode, int destination, int source,
619 int generation,
620 unsigned long long addr,
621 void *data, size_t len,
622 void *callback_data)
623{
624 struct fwtty_port *port = callback_data;
625 struct fwtty_peer *peer;
626 int err;
627 int rcode;
628
629 /* Only accept rx from the peer virtual-cabled to this port */
630 rcu_read_lock();
631 peer = __fwserial_peer_by_node_id(card, generation, source);
632 rcu_read_unlock();
633 if (!peer || peer != rcu_access_pointer(port->peer)) {
634 rcode = RCODE_ADDRESS_ERROR;
Joe Perches6e8661e2013-05-28 19:44:24 -0700635 fwtty_err_ratelimited(port, "ignoring unauthenticated data\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400636 goto respond;
637 }
638
639 switch (tcode) {
640 case TCODE_WRITE_QUADLET_REQUEST:
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200641 if (addr != port->rx_handler.offset || len != 4) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400642 rcode = RCODE_ADDRESS_ERROR;
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200643 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -0400644 fwtty_update_port_status(port, *(unsigned *)data);
645 rcode = RCODE_COMPLETE;
646 }
647 break;
648
649 case TCODE_WRITE_BLOCK_REQUEST:
650 if (addr != port->rx_handler.offset + 4 ||
651 len > port->rx_handler.length - 4) {
652 rcode = RCODE_ADDRESS_ERROR;
653 } else {
654 err = fwtty_rx(port, data, len);
655 switch (err) {
656 case 0:
657 rcode = RCODE_COMPLETE;
658 break;
659 case -EIO:
660 rcode = RCODE_DATA_ERROR;
661 break;
662 default:
663 rcode = RCODE_CONFLICT_ERROR;
664 break;
665 }
666 }
667 break;
668
669 default:
670 rcode = RCODE_TYPE_ERROR;
671 }
672
673respond:
674 fw_send_response(card, request, rcode);
675}
676
677/**
678 * fwtty_tx_complete - callback for tx dma
679 * @data: ignored, has no meaning for write txns
680 * @length: ignored, has no meaning for write txns
681 *
682 * The writer must be woken here if the fifo has been emptied because it
683 * may have slept if chars_in_buffer was != 0
684 */
685static void fwtty_tx_complete(struct fw_card *card, int rcode,
686 void *data, size_t length,
687 struct fwtty_transaction *txn)
688{
689 struct fwtty_port *port = txn->port;
Peter Hurley7355ba32012-11-02 08:16:33 -0400690 int len;
691
Joe Perches6e8661e2013-05-28 19:44:24 -0700692 fwtty_dbg(port, "rcode: %d\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -0400693
694 switch (rcode) {
695 case RCODE_COMPLETE:
696 spin_lock_bh(&port->lock);
697 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
698 len = dma_fifo_level(&port->tx_fifo);
699 spin_unlock_bh(&port->lock);
700
701 port->icount.tx += txn->dma_pended.len;
702 break;
703
704 default:
705 /* TODO: implement retries */
706 spin_lock_bh(&port->lock);
707 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
708 len = dma_fifo_level(&port->tx_fifo);
709 spin_unlock_bh(&port->lock);
710
711 port->stats.dropped += txn->dma_pended.len;
712 }
713
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100714 if (len < WAKEUP_CHARS)
715 tty_port_tty_wakeup(&port->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400716}
717
718static int fwtty_tx(struct fwtty_port *port, bool drain)
719{
720 struct fwtty_peer *peer;
721 struct fwtty_transaction *txn;
722 struct tty_struct *tty;
723 int n, len;
724
725 tty = tty_port_tty_get(&port->port);
726 if (!tty)
727 return -ENOENT;
728
729 rcu_read_lock();
730 peer = rcu_dereference(port->peer);
731 if (!peer) {
732 n = -EIO;
733 goto out;
734 }
735
736 if (test_and_set_bit(IN_TX, &port->flags)) {
737 n = -EALREADY;
738 goto out;
739 }
740
741 /* try to write as many dma transactions out as possible */
742 n = -EAGAIN;
743 while (!tty->stopped && !tty->hw_stopped &&
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +0200744 !test_bit(STOP_TX, &port->flags)) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400745 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
746 if (!txn) {
747 n = -ENOMEM;
748 break;
749 }
750
751 spin_lock_bh(&port->lock);
752 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
753 spin_unlock_bh(&port->lock);
754
Joe Perches6e8661e2013-05-28 19:44:24 -0700755 fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400756
757 if (n < 0) {
758 kmem_cache_free(fwtty_txn_cache, txn);
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200759 if (n == -EAGAIN) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400760 ++port->stats.tx_stall;
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200761 } else if (n == -ENODATA) {
Peter Hurley49bb8402013-11-22 13:06:10 -0500762 fwtty_profile_data(port->stats.txns, 0);
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200763 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -0400764 ++port->stats.fifo_errs;
Joe Perches6e8661e2013-05-28 19:44:24 -0700765 fwtty_err_ratelimited(port, "fifo err: %d\n",
766 n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400767 }
768 break;
769 }
770
Peter Hurley49bb8402013-11-22 13:06:10 -0500771 fwtty_profile_data(port->stats.txns, txn->dma_pended.len);
Peter Hurley7355ba32012-11-02 08:16:33 -0400772
773 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
774 peer->fifo_addr, txn->dma_pended.data,
775 txn->dma_pended.len, fwtty_tx_complete,
776 port);
777 ++port->stats.sent;
778
779 /*
780 * Stop tx if the 'last view' of the fifo is empty or if
781 * this is the writer and there's not enough data to bother
782 */
783 if (n == 0 || (!drain && n < WRITER_MINIMUM))
784 break;
785 }
786
787 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
788 spin_lock_bh(&port->lock);
789 len = dma_fifo_out_level(&port->tx_fifo);
790 if (len) {
791 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
792 schedule_delayed_work(&port->drain, delay);
793 }
794 len = dma_fifo_level(&port->tx_fifo);
795 spin_unlock_bh(&port->lock);
796
797 /* wakeup the writer */
798 if (drain && len < WAKEUP_CHARS)
799 tty_wakeup(tty);
800 }
801
802 clear_bit(IN_TX, &port->flags);
803 wake_up_interruptible(&port->wait_tx);
804
805out:
806 rcu_read_unlock();
807 tty_kref_put(tty);
808 return n;
809}
810
811static void fwtty_drain_tx(struct work_struct *work)
812{
813 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
814
815 fwtty_tx(port, true);
816}
817
818static void fwtty_write_xchar(struct fwtty_port *port, char ch)
819{
820 struct fwtty_peer *peer;
821
822 ++port->stats.xchars;
823
Joe Perches6e8661e2013-05-28 19:44:24 -0700824 fwtty_dbg(port, "%02x\n", ch);
Peter Hurley7355ba32012-11-02 08:16:33 -0400825
826 rcu_read_lock();
827 peer = rcu_dereference(port->peer);
828 if (peer) {
829 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
830 peer->fifo_addr, &ch, sizeof(ch),
831 NULL, port);
832 }
833 rcu_read_unlock();
834}
835
836struct fwtty_port *fwtty_port_get(unsigned index)
837{
838 struct fwtty_port *port;
839
840 if (index >= MAX_TOTAL_PORTS)
841 return NULL;
842
843 mutex_lock(&port_table_lock);
844 port = port_table[index];
845 if (port)
846 kref_get(&port->serial->kref);
847 mutex_unlock(&port_table_lock);
848 return port;
849}
850EXPORT_SYMBOL(fwtty_port_get);
851
852static int fwtty_ports_add(struct fw_serial *serial)
853{
854 int err = -EBUSY;
855 int i, j;
856
857 if (port_table_corrupt)
858 return err;
859
860 mutex_lock(&port_table_lock);
861 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
862 if (!port_table[i]) {
863 for (j = 0; j < num_ports; ++i, ++j) {
864 serial->ports[j]->index = i;
865 port_table[i] = serial->ports[j];
866 }
867 err = 0;
868 break;
869 }
870 }
871 mutex_unlock(&port_table_lock);
872 return err;
873}
874
875static void fwserial_destroy(struct kref *kref)
876{
877 struct fw_serial *serial = to_serial(kref, kref);
878 struct fwtty_port **ports = serial->ports;
879 int j, i = ports[0]->index;
880
881 synchronize_rcu();
882
883 mutex_lock(&port_table_lock);
884 for (j = 0; j < num_ports; ++i, ++j) {
Peter Hurley49b27462012-11-27 21:37:12 -0500885 port_table_corrupt |= port_table[i] != ports[j];
886 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +0200887 i, port_table[i], j, ports[j]);
Peter Hurley7355ba32012-11-02 08:16:33 -0400888
889 port_table[i] = NULL;
890 }
891 mutex_unlock(&port_table_lock);
892
893 for (j = 0; j < num_ports; ++j) {
894 fw_core_remove_address_handler(&ports[j]->rx_handler);
Peter Hurleya3218462012-11-27 21:37:11 -0500895 tty_port_destroy(&ports[j]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400896 kfree(ports[j]);
897 }
898 kfree(serial);
899}
900
901void fwtty_port_put(struct fwtty_port *port)
902{
903 kref_put(&port->serial->kref, fwserial_destroy);
904}
905EXPORT_SYMBOL(fwtty_port_put);
906
907static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
908{
909 struct fwtty_port *port = to_port(tty_port, port);
910
Joe Perches6e8661e2013-05-28 19:44:24 -0700911 fwtty_dbg(port, "on/off: %d\n", on);
Peter Hurley7355ba32012-11-02 08:16:33 -0400912
913 spin_lock_bh(&port->lock);
914 /* Don't change carrier state if this is a console */
915 if (!port->port.console) {
916 if (on)
917 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
918 else
919 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
920 }
921
922 __fwtty_write_port_status(port);
923 spin_unlock_bh(&port->lock);
924}
925
926/**
927 * fwtty_port_carrier_raised: required tty_port operation
928 *
929 * This port operation is polled after a tty has been opened and is waiting for
930 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
931 */
932static int fwtty_port_carrier_raised(struct tty_port *tty_port)
933{
934 struct fwtty_port *port = to_port(tty_port, port);
935 int rc;
936
937 rc = (port->mstatus & TIOCM_CAR);
938
Joe Perches6e8661e2013-05-28 19:44:24 -0700939 fwtty_dbg(port, "%d\n", rc);
Peter Hurley7355ba32012-11-02 08:16:33 -0400940
941 return rc;
942}
943
944static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
945{
946 unsigned baud, frame;
947
948 baud = tty_termios_baud_rate(&tty->termios);
949 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
950
951 /* compute bit count of 2 frames */
952 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
953
954 switch (C_CSIZE(tty)) {
955 case CS5:
956 frame -= (C_CSTOPB(tty)) ? 1 : 0;
957 break;
958 case CS6:
959 frame += 2;
960 break;
961 case CS7:
962 frame += 4;
963 break;
964 case CS8:
965 frame += 6;
966 break;
967 }
968
969 port->cps = (baud << 1) / frame;
970
971 port->status_mask = UART_LSR_OE;
972 if (_I_FLAG(tty, BRKINT | PARMRK))
973 port->status_mask |= UART_LSR_BI;
974
975 port->ignore_mask = 0;
976 if (I_IGNBRK(tty)) {
977 port->ignore_mask |= UART_LSR_BI;
978 if (I_IGNPAR(tty))
979 port->ignore_mask |= UART_LSR_OE;
980 }
981
982 port->write_only = !C_CREAD(tty);
983
984 /* turn off echo and newline xlat if loopback */
985 if (port->loopback) {
986 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
987 ECHONL | ECHOPRT | ECHOCTL);
988 tty->termios.c_oflag &= ~ONLCR;
989 }
990
991 return baud;
992}
993
994static int fwtty_port_activate(struct tty_port *tty_port,
995 struct tty_struct *tty)
996{
997 struct fwtty_port *port = to_port(tty_port, port);
998 unsigned baud;
999 int err;
1000
1001 set_bit(TTY_IO_ERROR, &tty->flags);
1002
1003 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
1004 cache_line_size(),
1005 port->max_payload,
1006 FWTTY_PORT_MAX_PEND_DMA,
1007 GFP_KERNEL);
1008 if (err)
1009 return err;
1010
1011 spin_lock_bh(&port->lock);
1012
1013 baud = set_termios(port, tty);
1014
1015 /* if console, don't change carrier state */
1016 if (!port->port.console) {
1017 port->mctrl = 0;
1018 if (baud != 0)
1019 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1020 }
1021
1022 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1023 tty->hw_stopped = 1;
1024
1025 __fwtty_write_port_status(port);
1026 spin_unlock_bh(&port->lock);
1027
1028 clear_bit(TTY_IO_ERROR, &tty->flags);
1029
1030 return 0;
1031}
1032
1033/**
1034 * fwtty_port_shutdown
1035 *
1036 * Note: the tty port core ensures this is not the console and
1037 * manages TTY_IO_ERROR properly
1038 */
1039static void fwtty_port_shutdown(struct tty_port *tty_port)
1040{
1041 struct fwtty_port *port = to_port(tty_port, port);
Peter Hurley7355ba32012-11-02 08:16:33 -04001042
1043 /* TODO: cancel outstanding transactions */
1044
1045 cancel_delayed_work_sync(&port->emit_breaks);
1046 cancel_delayed_work_sync(&port->drain);
Peter Hurley7355ba32012-11-02 08:16:33 -04001047
1048 spin_lock_bh(&port->lock);
Peter Hurley7355ba32012-11-02 08:16:33 -04001049 port->flags = 0;
1050 port->break_ctl = 0;
1051 port->overrun = 0;
1052 __fwtty_write_port_status(port);
1053 dma_fifo_free(&port->tx_fifo);
1054 spin_unlock_bh(&port->lock);
1055}
1056
1057static int fwtty_open(struct tty_struct *tty, struct file *fp)
1058{
1059 struct fwtty_port *port = tty->driver_data;
1060
1061 return tty_port_open(&port->port, tty, fp);
1062}
1063
1064static void fwtty_close(struct tty_struct *tty, struct file *fp)
1065{
1066 struct fwtty_port *port = tty->driver_data;
1067
1068 tty_port_close(&port->port, tty, fp);
1069}
1070
1071static void fwtty_hangup(struct tty_struct *tty)
1072{
1073 struct fwtty_port *port = tty->driver_data;
1074
1075 tty_port_hangup(&port->port);
1076}
1077
1078static void fwtty_cleanup(struct tty_struct *tty)
1079{
1080 struct fwtty_port *port = tty->driver_data;
1081
1082 tty->driver_data = NULL;
1083 fwtty_port_put(port);
1084}
1085
1086static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1087{
1088 struct fwtty_port *port = fwtty_port_get(tty->index);
1089 int err;
1090
1091 err = tty_standard_install(driver, tty);
1092 if (!err)
1093 tty->driver_data = port;
1094 else
1095 fwtty_port_put(port);
1096 return err;
1097}
1098
Peter Hurleyfa1da242013-01-28 22:34:38 -05001099static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1100{
1101 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1102 int err;
1103
1104 err = tty_standard_install(driver, tty);
1105 if (!err)
1106 tty->driver_data = port;
1107 else
1108 fwtty_port_put(port);
1109 return err;
1110}
1111
Peter Hurley7355ba32012-11-02 08:16:33 -04001112static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1113{
1114 struct fwtty_port *port = tty->driver_data;
1115 int n, len;
1116
Joe Perches6e8661e2013-05-28 19:44:24 -07001117 fwtty_dbg(port, "%d\n", c);
Peter Hurley49bb8402013-11-22 13:06:10 -05001118 fwtty_profile_data(port->stats.writes, c);
Peter Hurley7355ba32012-11-02 08:16:33 -04001119
1120 spin_lock_bh(&port->lock);
1121 n = dma_fifo_in(&port->tx_fifo, buf, c);
1122 len = dma_fifo_out_level(&port->tx_fifo);
1123 if (len < DRAIN_THRESHOLD)
1124 schedule_delayed_work(&port->drain, 1);
1125 spin_unlock_bh(&port->lock);
1126
1127 if (len >= DRAIN_THRESHOLD)
1128 fwtty_tx(port, false);
1129
1130 debug_short_write(port, c, n);
1131
1132 return (n < 0) ? 0 : n;
1133}
1134
1135static int fwtty_write_room(struct tty_struct *tty)
1136{
1137 struct fwtty_port *port = tty->driver_data;
1138 int n;
1139
1140 spin_lock_bh(&port->lock);
1141 n = dma_fifo_avail(&port->tx_fifo);
1142 spin_unlock_bh(&port->lock);
1143
Joe Perches6e8661e2013-05-28 19:44:24 -07001144 fwtty_dbg(port, "%d\n", n);
Peter Hurley7355ba32012-11-02 08:16:33 -04001145
1146 return n;
1147}
1148
1149static int fwtty_chars_in_buffer(struct tty_struct *tty)
1150{
1151 struct fwtty_port *port = tty->driver_data;
1152 int n;
1153
1154 spin_lock_bh(&port->lock);
1155 n = dma_fifo_level(&port->tx_fifo);
1156 spin_unlock_bh(&port->lock);
1157
Joe Perches6e8661e2013-05-28 19:44:24 -07001158 fwtty_dbg(port, "%d\n", n);
Peter Hurley7355ba32012-11-02 08:16:33 -04001159
1160 return n;
1161}
1162
1163static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1164{
1165 struct fwtty_port *port = tty->driver_data;
1166
Joe Perches6e8661e2013-05-28 19:44:24 -07001167 fwtty_dbg(port, "%02x\n", ch);
Peter Hurley7355ba32012-11-02 08:16:33 -04001168
1169 fwtty_write_xchar(port, ch);
1170}
1171
1172static void fwtty_throttle(struct tty_struct *tty)
1173{
1174 struct fwtty_port *port = tty->driver_data;
1175
1176 /*
1177 * Ignore throttling (but not unthrottling).
1178 * It only makes sense to throttle when data will no longer be
1179 * accepted by the tty flip buffer. For example, it is
1180 * possible for received data to overflow the tty buffer long
1181 * before the line discipline ever has a chance to throttle the driver.
1182 * Additionally, the driver may have already completed the I/O
1183 * but the tty buffer is still emptying, so the line discipline is
1184 * throttling and unthrottling nothing.
1185 */
1186
1187 ++port->stats.throttled;
1188}
1189
1190static void fwtty_unthrottle(struct tty_struct *tty)
1191{
1192 struct fwtty_port *port = tty->driver_data;
1193
Joe Perches6e8661e2013-05-28 19:44:24 -07001194 fwtty_dbg(port, "CRTSCTS: %d\n", (C_CRTSCTS(tty) != 0));
Peter Hurley7355ba32012-11-02 08:16:33 -04001195
Peter Hurley49bb8402013-11-22 13:06:10 -05001196 fwtty_profile_fifo(port, port->stats.unthrottle);
Peter Hurley7355ba32012-11-02 08:16:33 -04001197
Peter Hurley7355ba32012-11-02 08:16:33 -04001198 spin_lock_bh(&port->lock);
1199 port->mctrl &= ~OOB_RX_THROTTLE;
1200 if (C_CRTSCTS(tty))
1201 port->mctrl |= TIOCM_RTS;
1202 __fwtty_write_port_status(port);
1203 spin_unlock_bh(&port->lock);
1204}
1205
1206static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1207 struct async_icount *prev)
1208{
1209 struct async_icount now;
1210 int delta;
1211
1212 now = port->icount;
1213
1214 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1215 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1216 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1217 (mask & TIOCM_CTS && prev->cts != now.cts));
1218
1219 *prev = now;
1220
1221 return delta;
1222}
1223
1224static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1225{
1226 struct async_icount prev;
1227
1228 prev = port->icount;
1229
1230 return wait_event_interruptible(port->port.delta_msr_wait,
1231 check_msr_delta(port, mask, &prev));
1232}
1233
1234static int get_serial_info(struct fwtty_port *port,
1235 struct serial_struct __user *info)
1236{
1237 struct serial_struct tmp;
1238
1239 memset(&tmp, 0, sizeof(tmp));
1240
1241 tmp.type = PORT_UNKNOWN;
1242 tmp.line = port->port.tty->index;
1243 tmp.flags = port->port.flags;
1244 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1245 tmp.baud_base = 400000000;
1246 tmp.close_delay = port->port.close_delay;
1247
1248 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1249}
1250
1251static int set_serial_info(struct fwtty_port *port,
1252 struct serial_struct __user *info)
1253{
1254 struct serial_struct tmp;
1255
1256 if (copy_from_user(&tmp, info, sizeof(tmp)))
1257 return -EFAULT;
1258
1259 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +02001260 tmp.baud_base != 400000000)
Peter Hurley7355ba32012-11-02 08:16:33 -04001261 return -EPERM;
1262
1263 if (!capable(CAP_SYS_ADMIN)) {
1264 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1265 (port->port.flags & ~ASYNC_USR_MASK)))
1266 return -EPERM;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001267 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -04001268 port->port.close_delay = tmp.close_delay * HZ / 100;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001269 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001270
1271 return 0;
1272}
1273
1274static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1275 unsigned long arg)
1276{
1277 struct fwtty_port *port = tty->driver_data;
1278 int err;
1279
1280 switch (cmd) {
1281 case TIOCGSERIAL:
1282 mutex_lock(&port->port.mutex);
1283 err = get_serial_info(port, (void __user *)arg);
1284 mutex_unlock(&port->port.mutex);
1285 break;
1286
1287 case TIOCSSERIAL:
1288 mutex_lock(&port->port.mutex);
1289 err = set_serial_info(port, (void __user *)arg);
1290 mutex_unlock(&port->port.mutex);
1291 break;
1292
1293 case TIOCMIWAIT:
1294 err = wait_msr_change(port, arg);
1295 break;
1296
1297 default:
1298 err = -ENOIOCTLCMD;
1299 }
1300
1301 return err;
1302}
1303
1304static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1305{
1306 struct fwtty_port *port = tty->driver_data;
1307 unsigned baud;
1308
1309 spin_lock_bh(&port->lock);
1310 baud = set_termios(port, tty);
1311
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001312 if ((baud == 0) && (old->c_cflag & CBAUD)) {
Peter Hurley7355ba32012-11-02 08:16:33 -04001313 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001314 } else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
Peter Hurley7355ba32012-11-02 08:16:33 -04001315 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1316 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1317 else
1318 port->mctrl |= TIOCM_DTR;
1319 }
1320 __fwtty_write_port_status(port);
1321 spin_unlock_bh(&port->lock);
1322
1323 if (old->c_cflag & CRTSCTS) {
1324 if (!C_CRTSCTS(tty)) {
1325 tty->hw_stopped = 0;
1326 fwtty_restart_tx(port);
1327 }
1328 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1329 tty->hw_stopped = 1;
1330 }
1331}
1332
1333/**
1334 * fwtty_break_ctl - start/stop sending breaks
1335 *
1336 * Signals the remote to start or stop generating simulated breaks.
1337 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1338 * before signalling the break line status. This guarantees any pending rx will
1339 * be queued to the line discipline before break is simulated on the remote.
1340 * Conversely, turning off break_ctl requires signalling the line status change,
1341 * then enabling tx.
1342 */
1343static int fwtty_break_ctl(struct tty_struct *tty, int state)
1344{
1345 struct fwtty_port *port = tty->driver_data;
1346 long ret;
1347
Joe Perches6e8661e2013-05-28 19:44:24 -07001348 fwtty_dbg(port, "%d\n", state);
Peter Hurley7355ba32012-11-02 08:16:33 -04001349
1350 if (state == -1) {
1351 set_bit(STOP_TX, &port->flags);
1352 ret = wait_event_interruptible_timeout(port->wait_tx,
1353 !test_bit(IN_TX, &port->flags),
1354 10);
1355 if (ret == 0 || ret == -ERESTARTSYS) {
1356 clear_bit(STOP_TX, &port->flags);
1357 fwtty_restart_tx(port);
1358 return -EINTR;
1359 }
1360 }
1361
1362 spin_lock_bh(&port->lock);
1363 port->break_ctl = (state == -1);
1364 __fwtty_write_port_status(port);
1365 spin_unlock_bh(&port->lock);
1366
1367 if (state == 0) {
1368 spin_lock_bh(&port->lock);
1369 dma_fifo_reset(&port->tx_fifo);
1370 clear_bit(STOP_TX, &port->flags);
1371 spin_unlock_bh(&port->lock);
1372 }
1373 return 0;
1374}
1375
1376static int fwtty_tiocmget(struct tty_struct *tty)
1377{
1378 struct fwtty_port *port = tty->driver_data;
1379 unsigned tiocm;
1380
1381 spin_lock_bh(&port->lock);
1382 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1383 spin_unlock_bh(&port->lock);
1384
Joe Perches6e8661e2013-05-28 19:44:24 -07001385 fwtty_dbg(port, "%x\n", tiocm);
Peter Hurley7355ba32012-11-02 08:16:33 -04001386
1387 return tiocm;
1388}
1389
1390static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1391{
1392 struct fwtty_port *port = tty->driver_data;
1393
Joe Perches6e8661e2013-05-28 19:44:24 -07001394 fwtty_dbg(port, "set: %x clear: %x\n", set, clear);
Peter Hurley7355ba32012-11-02 08:16:33 -04001395
1396 /* TODO: simulate loopback if TIOCM_LOOP set */
1397
1398 spin_lock_bh(&port->lock);
1399 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1400 port->mctrl |= set & MCTRL_MASK & 0xffff;
1401 __fwtty_write_port_status(port);
1402 spin_unlock_bh(&port->lock);
1403 return 0;
1404}
1405
1406static int fwtty_get_icount(struct tty_struct *tty,
1407 struct serial_icounter_struct *icount)
1408{
1409 struct fwtty_port *port = tty->driver_data;
1410 struct stats stats;
1411
1412 memcpy(&stats, &port->stats, sizeof(stats));
1413 if (port->port.console)
1414 (*port->fwcon_ops->stats)(&stats, port->con_data);
1415
1416 icount->cts = port->icount.cts;
1417 icount->dsr = port->icount.dsr;
1418 icount->rng = port->icount.rng;
1419 icount->dcd = port->icount.dcd;
1420 icount->rx = port->icount.rx;
1421 icount->tx = port->icount.tx + stats.xchars;
1422 icount->frame = port->icount.frame;
1423 icount->overrun = port->icount.overrun;
1424 icount->parity = port->icount.parity;
1425 icount->brk = port->icount.brk;
1426 icount->buf_overrun = port->icount.overrun;
1427 return 0;
1428}
1429
1430static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1431{
1432 struct stats stats;
1433
1434 memcpy(&stats, &port->stats, sizeof(stats));
1435 if (port->port.console)
1436 (*port->fwcon_ops->stats)(&stats, port->con_data);
1437
Peter Hurleye16d1de2013-01-28 22:34:39 -05001438 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1439 port->icount.tx + stats.xchars, port->icount.rx);
Peter Hurley7355ba32012-11-02 08:16:33 -04001440 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1441 port->icount.dsr, port->icount.rng, port->icount.dcd);
1442 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1443 port->icount.overrun, port->icount.parity, port->icount.brk);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001444}
1445
1446static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1447{
1448 struct stats stats;
1449
1450 memcpy(&stats, &port->stats, sizeof(stats));
1451 if (port->port.console)
1452 (*port->fwcon_ops->stats)(&stats, port->con_data);
1453
Peter Hurley7355ba32012-11-02 08:16:33 -04001454 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1455 stats.tx_stall, stats.fifo_errs, stats.lost);
Peter Hurleyc4a8dab2013-11-22 13:06:08 -05001456 seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled);
Peter Hurley7355ba32012-11-02 08:16:33 -04001457
1458 if (port->port.console) {
Valentin Iliedd7cc7e2013-04-08 13:49:21 +03001459 seq_puts(m, "\n ");
Peter Hurley7355ba32012-11-02 08:16:33 -04001460 (*port->fwcon_ops->proc_show)(m, port->con_data);
1461 }
1462
Peter Hurley49bb8402013-11-22 13:06:10 -05001463 fwtty_dump_profile(m, &port->stats);
Peter Hurley7355ba32012-11-02 08:16:33 -04001464}
1465
Peter Hurleye16d1de2013-01-28 22:34:39 -05001466static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
Peter Hurley7355ba32012-11-02 08:16:33 -04001467{
1468 int generation = peer->generation;
1469
1470 smp_rmb();
1471 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1472 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1473 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1474 peer->max_payload, (unsigned long long) peer->guid);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001475 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
1476 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
Peter Hurley7355ba32012-11-02 08:16:33 -04001477 seq_putc(m, '\n');
1478}
1479
1480static int fwtty_proc_show(struct seq_file *m, void *v)
1481{
1482 struct fwtty_port *port;
Peter Hurley7355ba32012-11-02 08:16:33 -04001483 int i;
1484
1485 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1486 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1487 seq_printf(m, "%2d:", i);
1488 if (capable(CAP_SYS_ADMIN))
1489 fwtty_proc_show_port(m, port);
1490 fwtty_port_put(port);
Valentin Iliedd7cc7e2013-04-08 13:49:21 +03001491 seq_puts(m, "\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04001492 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001493 return 0;
1494}
1495
Peter Hurley4df5bb02013-01-28 22:34:40 -05001496static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
1497{
1498 struct fw_serial *serial = m->private;
1499 struct fwtty_port *port;
1500 int i;
1501
1502 for (i = 0; i < num_ports; ++i) {
1503 port = fwtty_port_get(serial->ports[i]->index);
1504 if (port) {
1505 seq_printf(m, "%2d:", port->index);
1506 fwtty_proc_show_port(m, port);
1507 fwtty_debugfs_show_port(m, port);
1508 fwtty_port_put(port);
Valentin Iliedd7cc7e2013-04-08 13:49:21 +03001509 seq_puts(m, "\n");
Peter Hurley4df5bb02013-01-28 22:34:40 -05001510 }
1511 }
1512 return 0;
1513}
1514
1515static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
1516{
1517 struct fw_serial *serial = m->private;
1518 struct fwtty_peer *peer;
1519
1520 rcu_read_lock();
1521 seq_printf(m, "card: %s guid: %016llx\n",
1522 dev_name(serial->card->device),
1523 (unsigned long long) serial->card->guid);
1524 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1525 fwtty_debugfs_show_peer(m, peer);
1526 rcu_read_unlock();
1527 return 0;
1528}
1529
Peter Hurley7355ba32012-11-02 08:16:33 -04001530static int fwtty_proc_open(struct inode *inode, struct file *fp)
1531{
1532 return single_open(fp, fwtty_proc_show, NULL);
1533}
1534
Peter Hurley4df5bb02013-01-28 22:34:40 -05001535static int fwtty_stats_open(struct inode *inode, struct file *fp)
1536{
1537 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
1538}
1539
1540static int fwtty_peers_open(struct inode *inode, struct file *fp)
1541{
1542 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
1543}
1544
1545static const struct file_operations fwtty_stats_fops = {
1546 .owner = THIS_MODULE,
1547 .open = fwtty_stats_open,
1548 .read = seq_read,
1549 .llseek = seq_lseek,
1550 .release = single_release,
1551};
1552
1553static const struct file_operations fwtty_peers_fops = {
1554 .owner = THIS_MODULE,
1555 .open = fwtty_peers_open,
1556 .read = seq_read,
1557 .llseek = seq_lseek,
1558 .release = single_release,
1559};
1560
Peter Hurley7355ba32012-11-02 08:16:33 -04001561static const struct file_operations fwtty_proc_fops = {
1562 .owner = THIS_MODULE,
1563 .open = fwtty_proc_open,
1564 .read = seq_read,
1565 .llseek = seq_lseek,
1566 .release = single_release,
1567};
1568
1569static const struct tty_port_operations fwtty_port_ops = {
1570 .dtr_rts = fwtty_port_dtr_rts,
1571 .carrier_raised = fwtty_port_carrier_raised,
1572 .shutdown = fwtty_port_shutdown,
1573 .activate = fwtty_port_activate,
1574};
1575
1576static const struct tty_operations fwtty_ops = {
1577 .open = fwtty_open,
1578 .close = fwtty_close,
1579 .hangup = fwtty_hangup,
1580 .cleanup = fwtty_cleanup,
1581 .install = fwtty_install,
1582 .write = fwtty_write,
1583 .write_room = fwtty_write_room,
1584 .chars_in_buffer = fwtty_chars_in_buffer,
1585 .send_xchar = fwtty_send_xchar,
1586 .throttle = fwtty_throttle,
1587 .unthrottle = fwtty_unthrottle,
1588 .ioctl = fwtty_ioctl,
1589 .set_termios = fwtty_set_termios,
1590 .break_ctl = fwtty_break_ctl,
1591 .tiocmget = fwtty_tiocmget,
1592 .tiocmset = fwtty_tiocmset,
1593 .get_icount = fwtty_get_icount,
1594 .proc_fops = &fwtty_proc_fops,
1595};
1596
Peter Hurleyfa1da242013-01-28 22:34:38 -05001597static const struct tty_operations fwloop_ops = {
1598 .open = fwtty_open,
1599 .close = fwtty_close,
1600 .hangup = fwtty_hangup,
1601 .cleanup = fwtty_cleanup,
1602 .install = fwloop_install,
1603 .write = fwtty_write,
1604 .write_room = fwtty_write_room,
1605 .chars_in_buffer = fwtty_chars_in_buffer,
1606 .send_xchar = fwtty_send_xchar,
1607 .throttle = fwtty_throttle,
1608 .unthrottle = fwtty_unthrottle,
1609 .ioctl = fwtty_ioctl,
1610 .set_termios = fwtty_set_termios,
1611 .break_ctl = fwtty_break_ctl,
1612 .tiocmget = fwtty_tiocmget,
1613 .tiocmset = fwtty_tiocmset,
1614 .get_icount = fwtty_get_icount,
1615};
1616
Peter Hurley7355ba32012-11-02 08:16:33 -04001617static inline int mgmt_pkt_expected_len(__be16 code)
1618{
1619 static const struct fwserial_mgmt_pkt pkt;
1620
1621 switch (be16_to_cpu(code)) {
1622 case FWSC_VIRT_CABLE_PLUG:
1623 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1624
1625 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1626 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1627
1628
1629 case FWSC_VIRT_CABLE_UNPLUG:
1630 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1631 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1632 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1633 return sizeof(pkt.hdr);
1634
1635 default:
1636 return -1;
1637 }
1638}
1639
1640static inline void fill_plug_params(struct virt_plug_params *params,
1641 struct fwtty_port *port)
1642{
1643 u64 status_addr = port->rx_handler.offset;
1644 u64 fifo_addr = port->rx_handler.offset + 4;
1645 size_t fifo_len = port->rx_handler.length - 4;
1646
1647 params->status_hi = cpu_to_be32(status_addr >> 32);
1648 params->status_lo = cpu_to_be32(status_addr);
1649 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1650 params->fifo_lo = cpu_to_be32(fifo_addr);
1651 params->fifo_len = cpu_to_be32(fifo_len);
1652}
1653
1654static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1655 struct fwtty_port *port)
1656{
1657 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1658 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1659 fill_plug_params(&pkt->plug_req, port);
1660}
1661
1662static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1663 struct fwtty_port *port)
1664{
1665 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1666 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1667 fill_plug_params(&pkt->plug_rsp, port);
1668}
1669
1670static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1671{
1672 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1673 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1674}
1675
1676static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1677{
1678 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1679 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1680}
1681
1682static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1683{
1684 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1685 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1686}
1687
1688static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1689{
1690 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1691 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1692}
1693
1694static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1695 struct virt_plug_params *params)
1696{
1697 struct fwtty_port *port = peer->port;
1698
1699 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1700 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1701 peer->fifo_len = be32_to_cpu(params->fifo_len);
1702 peer_set_state(peer, FWPS_ATTACHED);
1703
1704 /* reconfigure tx_fifo optimally for this peer */
1705 spin_lock_bh(&port->lock);
Peter Hurley3b1f3152013-01-28 20:57:47 -05001706 port->max_payload = min(peer->max_payload, peer->fifo_len);
Peter Hurley7355ba32012-11-02 08:16:33 -04001707 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1708 spin_unlock_bh(&peer->port->lock);
1709
1710 if (port->port.console && port->fwcon_ops->notify != NULL)
1711 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1712
Joe Perches6e8661e2013-05-28 19:44:24 -07001713 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04001714 (unsigned long long)peer->guid, dev_name(port->device));
1715}
1716
1717static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1718 struct fwserial_mgmt_pkt *pkt)
1719{
1720 int generation;
1721 int rcode, tries = 5;
1722
1723 do {
1724 generation = peer->generation;
1725 smp_rmb();
1726
1727 rcode = fw_run_transaction(peer->serial->card,
1728 TCODE_WRITE_BLOCK_REQUEST,
1729 peer->node_id,
1730 generation, peer->speed,
1731 peer->mgmt_addr,
1732 pkt, be16_to_cpu(pkt->hdr.len));
1733 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1734 rcode == RCODE_GENERATION) {
Joe Perches6e8661e2013-05-28 19:44:24 -07001735 fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -04001736 continue;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001737 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -04001738 break;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001739 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001740 } while (--tries > 0);
1741 return rcode;
1742}
1743
1744/**
1745 * fwserial_claim_port - attempt to claim port @ index for peer
1746 *
1747 * Returns ptr to claimed port or error code (as ERR_PTR())
1748 * Can sleep - must be called from process context
1749 */
1750static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1751 int index)
1752{
1753 struct fwtty_port *port;
1754
1755 if (index < 0 || index >= num_ports)
1756 return ERR_PTR(-EINVAL);
1757
1758 /* must guarantee that previous port releases have completed */
1759 synchronize_rcu();
1760
1761 port = peer->serial->ports[index];
1762 spin_lock_bh(&port->lock);
1763 if (!rcu_access_pointer(port->peer))
1764 rcu_assign_pointer(port->peer, peer);
1765 else
1766 port = ERR_PTR(-EBUSY);
1767 spin_unlock_bh(&port->lock);
1768
1769 return port;
1770}
1771
1772/**
1773 * fwserial_find_port - find avail port and claim for peer
1774 *
1775 * Returns ptr to claimed port or NULL if none avail
1776 * Can sleep - must be called from process context
1777 */
1778static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1779{
1780 struct fwtty_port **ports = peer->serial->ports;
1781 int i;
1782
1783 /* must guarantee that previous port releases have completed */
1784 synchronize_rcu();
1785
1786 /* TODO: implement optional GUID-to-specific port # matching */
1787
1788 /* find an unattached port (but not the loopback port, if present) */
1789 for (i = 0; i < num_ttys; ++i) {
1790 spin_lock_bh(&ports[i]->lock);
1791 if (!ports[i]->peer) {
1792 /* claim port */
1793 rcu_assign_pointer(ports[i]->peer, peer);
1794 spin_unlock_bh(&ports[i]->lock);
1795 return ports[i];
1796 }
1797 spin_unlock_bh(&ports[i]->lock);
1798 }
1799 return NULL;
1800}
1801
Peter Hurleyde321a12013-01-28 22:34:35 -05001802static void fwserial_release_port(struct fwtty_port *port, bool reset)
Peter Hurley7355ba32012-11-02 08:16:33 -04001803{
1804 /* drop carrier (and all other line status) */
Peter Hurleyde321a12013-01-28 22:34:35 -05001805 if (reset)
1806 fwtty_update_port_status(port, 0);
Peter Hurley7355ba32012-11-02 08:16:33 -04001807
1808 spin_lock_bh(&port->lock);
1809
1810 /* reset dma fifo max transmission size back to S100 */
1811 port->max_payload = link_speed_to_max_payload(SCODE_100);
1812 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1813
1814 rcu_assign_pointer(port->peer, NULL);
1815 spin_unlock_bh(&port->lock);
1816
1817 if (port->port.console && port->fwcon_ops->notify != NULL)
1818 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1819}
1820
1821static void fwserial_plug_timeout(unsigned long data)
1822{
Dominique van den Broeckd9492102014-04-12 15:18:14 +02001823 struct fwtty_peer *peer = (struct fwtty_peer *)data;
Peter Hurley7355ba32012-11-02 08:16:33 -04001824 struct fwtty_port *port;
1825
1826 spin_lock_bh(&peer->lock);
1827 if (peer->state != FWPS_PLUG_PENDING) {
1828 spin_unlock_bh(&peer->lock);
1829 return;
1830 }
1831
1832 port = peer_revert_state(peer);
1833 spin_unlock_bh(&peer->lock);
1834
1835 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05001836 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001837}
1838
1839/**
1840 * fwserial_connect_peer - initiate virtual cable with peer
1841 *
1842 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1843 * otherwise error code. Must be called from process context.
1844 */
1845static int fwserial_connect_peer(struct fwtty_peer *peer)
1846{
1847 struct fwtty_port *port;
1848 struct fwserial_mgmt_pkt *pkt;
1849 int err, rcode;
1850
1851 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1852 if (!pkt)
1853 return -ENOMEM;
1854
1855 port = fwserial_find_port(peer);
1856 if (!port) {
Joe Perches6e8661e2013-05-28 19:44:24 -07001857 fwtty_err(&peer->unit, "avail ports in use\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04001858 err = -EBUSY;
1859 goto free_pkt;
1860 }
1861
1862 spin_lock_bh(&peer->lock);
1863
1864 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1865 if (peer->state != FWPS_NOT_ATTACHED) {
1866 err = -EBUSY;
1867 goto release_port;
1868 }
1869
1870 peer->port = port;
1871 peer_set_state(peer, FWPS_PLUG_PENDING);
1872
1873 fill_plug_req(pkt, peer->port);
1874
1875 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1876 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1877 spin_unlock_bh(&peer->lock);
1878
1879 rcode = fwserial_send_mgmt_sync(peer, pkt);
1880
1881 spin_lock_bh(&peer->lock);
1882 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1883 if (rcode == RCODE_CONFLICT_ERROR)
1884 err = -EAGAIN;
1885 else
1886 err = -EIO;
1887 goto cancel_timer;
1888 }
1889 spin_unlock_bh(&peer->lock);
1890
1891 kfree(pkt);
1892 return 0;
1893
1894cancel_timer:
1895 del_timer(&peer->timer);
1896 peer_revert_state(peer);
1897release_port:
1898 spin_unlock_bh(&peer->lock);
Peter Hurleyde321a12013-01-28 22:34:35 -05001899 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001900free_pkt:
1901 kfree(pkt);
1902 return err;
1903}
1904
1905/**
1906 * fwserial_close_port -
1907 * HUP the tty (if the tty exists) and unregister the tty device.
1908 * Only used by the unit driver upon unit removal to disconnect and
1909 * cleanup all attached ports
1910 *
1911 * The port reference is put by fwtty_cleanup (if a reference was
1912 * ever taken).
1913 */
Peter Hurleyfa1da242013-01-28 22:34:38 -05001914static void fwserial_close_port(struct tty_driver *driver,
1915 struct fwtty_port *port)
Peter Hurley7355ba32012-11-02 08:16:33 -04001916{
1917 struct tty_struct *tty;
1918
1919 mutex_lock(&port->port.mutex);
1920 tty = tty_port_tty_get(&port->port);
1921 if (tty) {
1922 tty_vhangup(tty);
1923 tty_kref_put(tty);
1924 }
1925 mutex_unlock(&port->port.mutex);
1926
Peter Hurleyfa1da242013-01-28 22:34:38 -05001927 if (driver == fwloop_driver)
1928 tty_unregister_device(driver, loop_idx(port));
1929 else
1930 tty_unregister_device(driver, port->index);
Peter Hurley7355ba32012-11-02 08:16:33 -04001931}
1932
1933/**
1934 * fwserial_lookup - finds first fw_serial associated with card
1935 * @card: fw_card to match
1936 *
1937 * NB: caller must be holding fwserial_list_mutex
1938 */
1939static struct fw_serial *fwserial_lookup(struct fw_card *card)
1940{
1941 struct fw_serial *serial;
1942
1943 list_for_each_entry(serial, &fwserial_list, list) {
1944 if (card == serial->card)
1945 return serial;
1946 }
1947
1948 return NULL;
1949}
1950
1951/**
1952 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1953 * @card: fw_card to match
1954 *
1955 * NB: caller must be inside rcu_read_lock() section
1956 */
1957static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
1958{
1959 struct fw_serial *serial;
1960
1961 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1962 if (card == serial->card)
1963 return serial;
1964 }
1965
1966 return NULL;
1967}
1968
1969/**
1970 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1971 *
1972 * If a matching peer could not be found for the specified generation/node id,
1973 * this could be because:
1974 * a) the generation has changed and one of the nodes hasn't updated yet
1975 * b) the remote node has created its remote unit device before this
1976 * local node has created its corresponding remote unit device
1977 * In either case, the remote node should retry
1978 *
1979 * Note: caller must be in rcu_read_lock() section
1980 */
1981static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
1982 int generation, int id)
1983{
1984 struct fw_serial *serial;
1985 struct fwtty_peer *peer;
1986
1987 serial = __fwserial_lookup_rcu(card);
1988 if (!serial) {
1989 /*
1990 * Something is very wrong - there should be a matching
1991 * fw_serial structure for every fw_card. Maybe the remote node
1992 * has created its remote unit device before this driver has
1993 * been probed for any unit devices...
1994 */
Joe Perches6e8661e2013-05-28 19:44:24 -07001995 fwtty_err(card, "unknown card (guid %016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04001996 (unsigned long long) card->guid);
1997 return NULL;
1998 }
1999
2000 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2001 int g = peer->generation;
2002 smp_rmb();
2003 if (generation == g && id == peer->node_id)
2004 return peer;
2005 }
2006
2007 return NULL;
2008}
2009
2010#ifdef DEBUG
2011static void __dump_peer_list(struct fw_card *card)
2012{
2013 struct fw_serial *serial;
2014 struct fwtty_peer *peer;
2015
2016 serial = __fwserial_lookup_rcu(card);
2017 if (!serial)
2018 return;
2019
2020 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2021 int g = peer->generation;
2022 smp_rmb();
Joe Perches6e8661e2013-05-28 19:44:24 -07002023 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n",
2024 g, peer->node_id, (unsigned long long) peer->guid);
Peter Hurley7355ba32012-11-02 08:16:33 -04002025 }
2026}
2027#else
2028#define __dump_peer_list(s)
2029#endif
2030
2031static void fwserial_auto_connect(struct work_struct *work)
2032{
2033 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2034 int err;
2035
2036 err = fwserial_connect_peer(peer);
2037 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2038 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2039}
2040
Tejun Heo6c256cb2014-03-07 10:24:50 -05002041static void fwserial_peer_workfn(struct work_struct *work)
2042{
2043 struct fwtty_peer *peer = to_peer(work, work);
2044
2045 peer->workfn(work);
2046}
2047
Peter Hurley7355ba32012-11-02 08:16:33 -04002048/**
2049 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2050 * @serial: aggregate representing the specific fw_card to add the peer to
2051 * @unit: 'peer' to create and add to peer_list of serial
2052 *
2053 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2054 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2055 * available tty port. This function is called either directly or indirectly
2056 * as a result of a 'serial' unit device being created & probed.
2057 *
2058 * Note: this function is serialized with fwserial_remove_peer() by the
2059 * fwserial_list_mutex held in fwserial_probe().
2060 *
2061 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2062 * via the dev_set_drvdata() for the device of the fw_unit.
2063 */
2064static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2065{
2066 struct device *dev = &unit->device;
2067 struct fw_device *parent = fw_parent_device(unit);
2068 struct fwtty_peer *peer;
2069 struct fw_csr_iterator ci;
2070 int key, val;
2071 int generation;
2072
2073 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2074 if (!peer)
2075 return -ENOMEM;
2076
2077 peer_set_state(peer, FWPS_NOT_ATTACHED);
2078
2079 dev_set_drvdata(dev, peer);
2080 peer->unit = unit;
2081 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2082 peer->speed = parent->max_speed;
2083 peer->max_payload = min(device_max_receive(parent),
2084 link_speed_to_max_payload(peer->speed));
2085
2086 generation = parent->generation;
2087 smp_rmb();
2088 peer->node_id = parent->node_id;
2089 smp_wmb();
2090 peer->generation = generation;
2091
2092 /* retrieve the mgmt bus addr from the unit directory */
2093 fw_csr_iterator_init(&ci, unit->directory);
2094 while (fw_csr_iterator_next(&ci, &key, &val)) {
2095 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2096 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2097 break;
2098 }
2099 }
2100 if (peer->mgmt_addr == 0ULL) {
2101 /*
2102 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2103 * this peer will not be able to attach to a remote
2104 */
2105 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2106 }
2107
2108 spin_lock_init(&peer->lock);
2109 peer->port = NULL;
2110
2111 init_timer(&peer->timer);
Tejun Heo6c256cb2014-03-07 10:24:50 -05002112 INIT_WORK(&peer->work, fwserial_peer_workfn);
Peter Hurley7355ba32012-11-02 08:16:33 -04002113 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2114
2115 /* associate peer with specific fw_card */
2116 peer->serial = serial;
2117 list_add_rcu(&peer->list, &serial->peer_list);
2118
Joe Perches6e8661e2013-05-28 19:44:24 -07002119 fwtty_info(&peer->unit, "peer added (guid:%016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002120 (unsigned long long)peer->guid);
2121
2122 /* identify the local unit & virt cable to loopback port */
2123 if (parent->is_local) {
2124 serial->self = peer;
2125 if (create_loop_dev) {
2126 struct fwtty_port *port;
2127 port = fwserial_claim_port(peer, num_ttys);
2128 if (!IS_ERR(port)) {
2129 struct virt_plug_params params;
2130
2131 spin_lock_bh(&peer->lock);
2132 peer->port = port;
2133 fill_plug_params(&params, port);
2134 fwserial_virt_plug_complete(peer, &params);
2135 spin_unlock_bh(&peer->lock);
2136
2137 fwtty_write_port_status(port);
2138 }
2139 }
2140
2141 } else if (auto_connect) {
2142 /* auto-attach to remote units only (if policy allows) */
2143 schedule_delayed_work(&peer->connect, 1);
2144 }
2145
2146 return 0;
2147}
2148
2149/**
2150 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2151 *
2152 * Remove a 'peer' from its list of peers. This function is only
2153 * called by fwserial_remove() on bus removal of the unit device.
2154 *
2155 * Note: this function is serialized with fwserial_add_peer() by the
2156 * fwserial_list_mutex held in fwserial_remove().
2157 */
2158static void fwserial_remove_peer(struct fwtty_peer *peer)
2159{
2160 struct fwtty_port *port;
2161
2162 spin_lock_bh(&peer->lock);
2163 peer_set_state(peer, FWPS_GONE);
2164 spin_unlock_bh(&peer->lock);
2165
2166 cancel_delayed_work_sync(&peer->connect);
2167 cancel_work_sync(&peer->work);
2168
2169 spin_lock_bh(&peer->lock);
2170 /* if this unit is the local unit, clear link */
2171 if (peer == peer->serial->self)
2172 peer->serial->self = NULL;
2173
2174 /* cancel the request timeout timer (if running) */
2175 del_timer(&peer->timer);
2176
2177 port = peer->port;
2178 peer->port = NULL;
2179
2180 list_del_rcu(&peer->list);
2181
Joe Perches6e8661e2013-05-28 19:44:24 -07002182 fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002183 (unsigned long long)peer->guid);
2184
2185 spin_unlock_bh(&peer->lock);
2186
2187 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002188 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002189
2190 synchronize_rcu();
2191 kfree(peer);
2192}
2193
2194/**
Peter Hurley7355ba32012-11-02 08:16:33 -04002195 * fwserial_create - init everything to create TTYs for a specific fw_card
2196 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2197 *
2198 * This function inits the aggregate structure (an fw_serial instance)
2199 * used to manage the TTY ports registered by a specific fw_card. Also, the
2200 * unit device is added as the first 'peer'.
2201 *
2202 * This unit device may represent a local unit device (as specified by the
2203 * config ROM unit directory) or it may represent a remote unit device
2204 * (as specified by the reading of the remote node's config ROM).
2205 *
2206 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2207 * value to indicate which error.
2208 */
2209static int fwserial_create(struct fw_unit *unit)
2210{
2211 struct fw_device *parent = fw_parent_device(unit);
2212 struct fw_card *card = parent->card;
2213 struct fw_serial *serial;
2214 struct fwtty_port *port;
2215 struct device *tty_dev;
2216 int i, j;
2217 int err;
2218
2219 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2220 if (!serial)
2221 return -ENOMEM;
2222
2223 kref_init(&serial->kref);
2224 serial->card = card;
2225 INIT_LIST_HEAD(&serial->peer_list);
2226
2227 for (i = 0; i < num_ports; ++i) {
2228 port = kzalloc(sizeof(*port), GFP_KERNEL);
2229 if (!port) {
2230 err = -ENOMEM;
2231 goto free_ports;
2232 }
2233 tty_port_init(&port->port);
2234 port->index = FWTTY_INVALID_INDEX;
2235 port->port.ops = &fwtty_port_ops;
2236 port->serial = serial;
Peter Hurley2ead3912013-11-22 13:06:09 -05002237 tty_buffer_set_limit(&port->port, 128 * 1024);
Peter Hurley7355ba32012-11-02 08:16:33 -04002238
2239 spin_lock_init(&port->lock);
2240 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2241 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2242 INIT_WORK(&port->hangup, fwtty_do_hangup);
Peter Hurley7355ba32012-11-02 08:16:33 -04002243 init_waitqueue_head(&port->wait_tx);
2244 port->max_payload = link_speed_to_max_payload(SCODE_100);
2245 dma_fifo_init(&port->tx_fifo);
2246
2247 rcu_assign_pointer(port->peer, NULL);
2248 serial->ports[i] = port;
2249
2250 /* get unique bus addr region for port's status & recv fifo */
2251 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2252 port->rx_handler.address_callback = fwtty_port_handler;
2253 port->rx_handler.callback_data = port;
2254 /*
2255 * XXX: use custom memory region above cpu physical memory addrs
2256 * this will ease porting to 64-bit firewire adapters
2257 */
2258 err = fw_core_add_address_handler(&port->rx_handler,
2259 &fw_high_memory_region);
2260 if (err) {
2261 kfree(port);
2262 goto free_ports;
2263 }
2264 }
2265 /* preserve i for error cleanup */
2266
2267 err = fwtty_ports_add(serial);
2268 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002269 fwtty_err(&unit, "no space in port table\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002270 goto free_ports;
2271 }
2272
2273 for (j = 0; j < num_ttys; ++j) {
2274 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2275 fwtty_driver,
2276 serial->ports[j]->index,
2277 card->device);
2278 if (IS_ERR(tty_dev)) {
2279 err = PTR_ERR(tty_dev);
Joe Perches6e8661e2013-05-28 19:44:24 -07002280 fwtty_err(&unit, "register tty device error (%d)\n",
2281 err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002282 goto unregister_ttys;
2283 }
2284
2285 serial->ports[j]->device = tty_dev;
2286 }
2287 /* preserve j for error cleanup */
2288
2289 if (create_loop_dev) {
2290 struct device *loop_dev;
2291
Peter Hurleyfa1da242013-01-28 22:34:38 -05002292 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2293 fwloop_driver,
2294 loop_idx(serial->ports[j]),
2295 card->device);
Peter Hurley7355ba32012-11-02 08:16:33 -04002296 if (IS_ERR(loop_dev)) {
2297 err = PTR_ERR(loop_dev);
Joe Perches6e8661e2013-05-28 19:44:24 -07002298 fwtty_err(&unit, "create loop device failed (%d)\n",
2299 err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002300 goto unregister_ttys;
2301 }
Peter Hurleyfa1da242013-01-28 22:34:38 -05002302 serial->ports[j]->device = loop_dev;
2303 serial->ports[j]->loopback = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002304 }
2305
Peter Hurley4df5bb02013-01-28 22:34:40 -05002306 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2307 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2308 fwserial_debugfs);
2309 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2310 debugfs_create_file("peers", 0444, serial->debugfs,
2311 serial, &fwtty_peers_fops);
2312 debugfs_create_file("stats", 0444, serial->debugfs,
2313 serial, &fwtty_stats_fops);
2314 }
2315 }
2316
Peter Hurley7355ba32012-11-02 08:16:33 -04002317 list_add_rcu(&serial->list, &fwserial_list);
2318
Joe Perches6e8661e2013-05-28 19:44:24 -07002319 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002320 dev_name(card->device), (unsigned long long) card->guid);
2321
2322 err = fwserial_add_peer(serial, unit);
2323 if (!err)
2324 return 0;
2325
Joe Perches6e8661e2013-05-28 19:44:24 -07002326 fwtty_err(&unit, "unable to add peer unit device (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002327
2328 /* fall-through to error processing */
Peter Hurley4df5bb02013-01-28 22:34:40 -05002329 debugfs_remove_recursive(serial->debugfs);
2330
Peter Hurley7355ba32012-11-02 08:16:33 -04002331 list_del_rcu(&serial->list);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002332 if (create_loop_dev)
Jon Bernard9502e2b2013-09-10 18:00:01 -04002333 tty_unregister_device(fwloop_driver,
2334 loop_idx(serial->ports[j]));
Peter Hurley7355ba32012-11-02 08:16:33 -04002335unregister_ttys:
2336 for (--j; j >= 0; --j)
2337 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2338 kref_put(&serial->kref, fwserial_destroy);
2339 return err;
2340
2341free_ports:
Peter Hurleya3218462012-11-27 21:37:11 -05002342 for (--i; i >= 0; --i) {
2343 tty_port_destroy(&serial->ports[i]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -04002344 kfree(serial->ports[i]);
Peter Hurleya3218462012-11-27 21:37:11 -05002345 }
Peter Hurley7355ba32012-11-02 08:16:33 -04002346 kfree(serial);
2347 return err;
2348}
2349
2350/**
2351 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2352 *
2353 * A 'serial' unit device is created and probed as a result of:
2354 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2355 * 'serial' unit specifier id
2356 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2357 *
2358 * The firewire core registers unit devices by enumerating unit directories
2359 * of a node's config ROM after reading the config ROM when a new node is
2360 * added to the bus topology after a bus reset.
2361 *
2362 * The practical implications of this are:
2363 * - this probe is called for both local and remote nodes that have a 'serial'
2364 * unit directory in their config ROM (that matches the specifiers in
2365 * fwserial_id_table).
2366 * - no specific order is enforced for local vs. remote unit devices
2367 *
2368 * This unit driver copes with the lack of specific order in the same way the
2369 * firewire net driver does -- each probe, for either a local or remote unit
2370 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2371 * first peer created for a given fw_card (tracked by the global fwserial_list)
2372 * creates the underlying TTYs (aggregated in a fw_serial instance).
2373 *
2374 * NB: an early attempt to differentiate local & remote unit devices by creating
2375 * peers only for remote units and fw_serial instances (with their
2376 * associated TTY devices) only for local units was discarded. Managing
2377 * the peer lifetimes on device removal proved too complicated.
2378 *
2379 * fwserial_probe/fwserial_remove are effectively serialized by the
2380 * fwserial_list_mutex. This is necessary because the addition of the first peer
2381 * for a given fw_card will trigger the creation of the fw_serial for that
2382 * fw_card, which must not simultaneously contend with the removal of the
2383 * last peer for a given fw_card triggering the destruction of the same
2384 * fw_serial for the same fw_card.
2385 */
Stefan Richter94a87152013-06-09 18:15:00 +02002386static int fwserial_probe(struct fw_unit *unit,
2387 const struct ieee1394_device_id *id)
Peter Hurley7355ba32012-11-02 08:16:33 -04002388{
Peter Hurley7355ba32012-11-02 08:16:33 -04002389 struct fw_serial *serial;
2390 int err;
2391
2392 mutex_lock(&fwserial_list_mutex);
2393 serial = fwserial_lookup(fw_parent_device(unit)->card);
2394 if (!serial)
2395 err = fwserial_create(unit);
2396 else
2397 err = fwserial_add_peer(serial, unit);
2398 mutex_unlock(&fwserial_list_mutex);
2399 return err;
2400}
2401
2402/**
2403 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2404 *
2405 * The corresponding 'peer' for this unit device is removed from the list of
2406 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2407 * specific fw_card). If this is the last peer being removed, then trigger
2408 * the destruction of the underlying TTYs.
2409 */
Stefan Richter94a87152013-06-09 18:15:00 +02002410static void fwserial_remove(struct fw_unit *unit)
Peter Hurley7355ba32012-11-02 08:16:33 -04002411{
Stefan Richter94a87152013-06-09 18:15:00 +02002412 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
Peter Hurley7355ba32012-11-02 08:16:33 -04002413 struct fw_serial *serial = peer->serial;
2414 int i;
2415
2416 mutex_lock(&fwserial_list_mutex);
2417 fwserial_remove_peer(peer);
2418
2419 if (list_empty(&serial->peer_list)) {
2420 /* unlink from the fwserial_list here */
2421 list_del_rcu(&serial->list);
2422
Peter Hurley4df5bb02013-01-28 22:34:40 -05002423 debugfs_remove_recursive(serial->debugfs);
2424
Peter Hurleyfa1da242013-01-28 22:34:38 -05002425 for (i = 0; i < num_ttys; ++i)
2426 fwserial_close_port(fwtty_driver, serial->ports[i]);
2427 if (create_loop_dev)
2428 fwserial_close_port(fwloop_driver, serial->ports[i]);
Peter Hurley7355ba32012-11-02 08:16:33 -04002429 kref_put(&serial->kref, fwserial_destroy);
2430 }
2431 mutex_unlock(&fwserial_list_mutex);
Peter Hurley7355ba32012-11-02 08:16:33 -04002432}
2433
2434/**
2435 * fwserial_update: bus update function for 'firewire' serial unit devices
2436 *
2437 * Updates the new node_id and bus generation for this peer. Note that locking
2438 * is unnecessary; but careful memory barrier usage is important to enforce the
2439 * load and store order of generation & node_id.
2440 *
2441 * The fw-core orders the write of node_id before generation in the parent
2442 * fw_device to ensure that a stale node_id cannot be used with a current
2443 * bus generation. So the generation value must be read before the node_id.
2444 *
2445 * In turn, this orders the write of node_id before generation in the peer to
2446 * also ensure a stale node_id cannot be used with a current bus generation.
2447 */
2448static void fwserial_update(struct fw_unit *unit)
2449{
2450 struct fw_device *parent = fw_parent_device(unit);
2451 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2452 int generation;
2453
2454 generation = parent->generation;
2455 smp_rmb();
2456 peer->node_id = parent->node_id;
2457 smp_wmb();
2458 peer->generation = generation;
2459}
2460
2461static const struct ieee1394_device_id fwserial_id_table[] = {
2462 {
2463 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2464 IEEE1394_MATCH_VERSION,
2465 .specifier_id = LINUX_VENDOR_ID,
2466 .version = FWSERIAL_VERSION,
2467 },
2468 { }
2469};
2470
2471static struct fw_driver fwserial_driver = {
2472 .driver = {
2473 .owner = THIS_MODULE,
2474 .name = KBUILD_MODNAME,
2475 .bus = &fw_bus_type,
Peter Hurley7355ba32012-11-02 08:16:33 -04002476 },
Stefan Richter94a87152013-06-09 18:15:00 +02002477 .probe = fwserial_probe,
Peter Hurley7355ba32012-11-02 08:16:33 -04002478 .update = fwserial_update,
Stefan Richter94a87152013-06-09 18:15:00 +02002479 .remove = fwserial_remove,
Peter Hurley7355ba32012-11-02 08:16:33 -04002480 .id_table = fwserial_id_table,
2481};
2482
2483#define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2484#define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2485#define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2486 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2487/* XXX: config ROM definitons could be improved with semi-automated offset
2488 * and length calculation
2489 */
Peter Hurley612588a2013-01-29 09:10:30 -05002490#define FW_ROM_LEN(quads) ((quads) << 16)
Peter Hurley7355ba32012-11-02 08:16:33 -04002491#define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2492
2493struct fwserial_unit_directory_data {
Peter Hurley612588a2013-01-29 09:10:30 -05002494 u32 len_crc;
Peter Hurley7355ba32012-11-02 08:16:33 -04002495 u32 unit_specifier;
2496 u32 unit_sw_version;
2497 u32 unit_addr_offset;
2498 u32 desc1_ofs;
Peter Hurley612588a2013-01-29 09:10:30 -05002499 u32 desc1_len_crc;
Peter Hurley7355ba32012-11-02 08:16:33 -04002500 u32 desc1_data[5];
2501} __packed;
2502
2503static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
Peter Hurley612588a2013-01-29 09:10:30 -05002504 .len_crc = FW_ROM_LEN(4),
Peter Hurley7355ba32012-11-02 08:16:33 -04002505 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2506 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2507 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
Peter Hurley612588a2013-01-29 09:10:30 -05002508 .desc1_len_crc = FW_ROM_LEN(5),
Peter Hurley7355ba32012-11-02 08:16:33 -04002509 .desc1_data = {
2510 0x00000000, /* type = text */
2511 0x00000000, /* enc = ASCII, lang EN */
2512 0x4c696e75, /* 'Linux TTY' */
2513 0x78205454,
2514 0x59000000,
2515 },
2516};
2517
2518static struct fw_descriptor fwserial_unit_directory = {
2519 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2520 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2521 .data = (u32 *)&fwserial_unit_directory_data,
2522};
2523
2524/*
2525 * The management address is in the unit space region but above other known
2526 * address users (to keep wild writes from causing havoc)
2527 */
Peter Hurleya3d9ad42013-01-28 22:34:37 -05002528static const struct fw_address_region fwserial_mgmt_addr_region = {
Peter Hurley7355ba32012-11-02 08:16:33 -04002529 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2530 .end = 0x1000000000000ULL,
2531};
2532
2533static struct fw_address_handler fwserial_mgmt_addr_handler;
2534
2535/**
2536 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2537 * @work: ptr to peer->work
2538 *
2539 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2540 *
2541 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2542 * already sent to this peer. If so, the collision is resolved by comparing
2543 * guid values; the loser sends the plug response.
2544 *
2545 * Note: if an error prevents a response, don't do anything -- the
2546 * remote will timeout its request.
2547 */
2548static void fwserial_handle_plug_req(struct work_struct *work)
2549{
2550 struct fwtty_peer *peer = to_peer(work, work);
2551 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2552 struct fwtty_port *port;
2553 struct fwserial_mgmt_pkt *pkt;
2554 int rcode;
2555
2556 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2557 if (!pkt)
2558 return;
2559
2560 port = fwserial_find_port(peer);
2561
2562 spin_lock_bh(&peer->lock);
2563
2564 switch (peer->state) {
2565 case FWPS_NOT_ATTACHED:
2566 if (!port) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002567 fwtty_err(&peer->unit, "no more ports avail\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002568 fill_plug_rsp_nack(pkt);
2569 } else {
2570 peer->port = port;
2571 fill_plug_rsp_ok(pkt, peer->port);
2572 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2573 /* don't release claimed port */
2574 port = NULL;
2575 }
2576 break;
2577
2578 case FWPS_PLUG_PENDING:
2579 if (peer->serial->card->guid > peer->guid)
2580 goto cleanup;
2581
2582 /* We lost - hijack the already-claimed port and send ok */
2583 del_timer(&peer->timer);
2584 fill_plug_rsp_ok(pkt, peer->port);
2585 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2586 break;
2587
2588 default:
2589 fill_plug_rsp_nack(pkt);
2590 }
2591
2592 spin_unlock_bh(&peer->lock);
2593 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002594 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002595
2596 rcode = fwserial_send_mgmt_sync(peer, pkt);
2597
2598 spin_lock_bh(&peer->lock);
2599 if (peer->state == FWPS_PLUG_RESPONDING) {
2600 if (rcode == RCODE_COMPLETE) {
2601 struct fwtty_port *tmp = peer->port;
2602
2603 fwserial_virt_plug_complete(peer, plug_req);
2604 spin_unlock_bh(&peer->lock);
2605
2606 fwtty_write_port_status(tmp);
2607 spin_lock_bh(&peer->lock);
2608 } else {
Joe Perches6e8661e2013-05-28 19:44:24 -07002609 fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -04002610 port = peer_revert_state(peer);
2611 }
2612 }
2613cleanup:
2614 spin_unlock_bh(&peer->lock);
2615 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002616 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002617 kfree(pkt);
2618 return;
2619}
2620
2621static void fwserial_handle_unplug_req(struct work_struct *work)
2622{
2623 struct fwtty_peer *peer = to_peer(work, work);
2624 struct fwtty_port *port = NULL;
2625 struct fwserial_mgmt_pkt *pkt;
2626 int rcode;
2627
2628 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2629 if (!pkt)
2630 return;
2631
2632 spin_lock_bh(&peer->lock);
2633
2634 switch (peer->state) {
2635 case FWPS_ATTACHED:
2636 fill_unplug_rsp_ok(pkt);
2637 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2638 break;
2639
2640 case FWPS_UNPLUG_PENDING:
2641 if (peer->serial->card->guid > peer->guid)
2642 goto cleanup;
2643
2644 /* We lost - send unplug rsp */
2645 del_timer(&peer->timer);
2646 fill_unplug_rsp_ok(pkt);
2647 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2648 break;
2649
2650 default:
2651 fill_unplug_rsp_nack(pkt);
2652 }
2653
2654 spin_unlock_bh(&peer->lock);
2655
2656 rcode = fwserial_send_mgmt_sync(peer, pkt);
2657
2658 spin_lock_bh(&peer->lock);
2659 if (peer->state == FWPS_UNPLUG_RESPONDING) {
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002660 if (rcode != RCODE_COMPLETE)
Joe Perches6e8661e2013-05-28 19:44:24 -07002661 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n",
2662 rcode);
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002663 port = peer_revert_state(peer);
Peter Hurley7355ba32012-11-02 08:16:33 -04002664 }
2665cleanup:
2666 spin_unlock_bh(&peer->lock);
2667 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002668 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002669 kfree(pkt);
2670 return;
2671}
2672
2673static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2674 struct fwserial_mgmt_pkt *pkt,
2675 unsigned long long addr,
2676 size_t len)
2677{
2678 struct fwtty_port *port = NULL;
Peter Hurleyde321a12013-01-28 22:34:35 -05002679 bool reset = false;
Peter Hurley7355ba32012-11-02 08:16:33 -04002680 int rcode;
2681
2682 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2683 return RCODE_ADDRESS_ERROR;
2684
2685 if (len != be16_to_cpu(pkt->hdr.len) ||
2686 len != mgmt_pkt_expected_len(pkt->hdr.code))
2687 return RCODE_DATA_ERROR;
2688
2689 spin_lock_bh(&peer->lock);
2690 if (peer->state == FWPS_GONE) {
2691 /*
2692 * This should never happen - it would mean that the
2693 * remote unit that just wrote this transaction was
2694 * already removed from the bus -- and the removal was
2695 * processed before we rec'd this transaction
2696 */
Joe Perches6e8661e2013-05-28 19:44:24 -07002697 fwtty_err(&peer->unit, "peer already removed\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002698 spin_unlock_bh(&peer->lock);
2699 return RCODE_ADDRESS_ERROR;
2700 }
2701
2702 rcode = RCODE_COMPLETE;
2703
Joe Perches6e8661e2013-05-28 19:44:24 -07002704 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx\n", pkt->hdr.code);
Peter Hurley7355ba32012-11-02 08:16:33 -04002705
2706 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2707 case FWSC_VIRT_CABLE_PLUG:
2708 if (work_pending(&peer->work)) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002709 fwtty_err(&peer->unit, "plug req: busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002710 rcode = RCODE_CONFLICT_ERROR;
2711
2712 } else {
2713 peer->work_params.plug_req = pkt->plug_req;
Tejun Heo6c256cb2014-03-07 10:24:50 -05002714 peer->workfn = fwserial_handle_plug_req;
Peter Hurley7355ba32012-11-02 08:16:33 -04002715 queue_work(system_unbound_wq, &peer->work);
2716 }
2717 break;
2718
2719 case FWSC_VIRT_CABLE_PLUG_RSP:
2720 if (peer->state != FWPS_PLUG_PENDING) {
2721 rcode = RCODE_CONFLICT_ERROR;
2722
2723 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002724 fwtty_notice(&peer->unit, "NACK plug rsp\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002725 port = peer_revert_state(peer);
2726
2727 } else {
2728 struct fwtty_port *tmp = peer->port;
2729
2730 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2731 spin_unlock_bh(&peer->lock);
2732
2733 fwtty_write_port_status(tmp);
2734 spin_lock_bh(&peer->lock);
2735 }
2736 break;
2737
2738 case FWSC_VIRT_CABLE_UNPLUG:
2739 if (work_pending(&peer->work)) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002740 fwtty_err(&peer->unit, "unplug req: busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002741 rcode = RCODE_CONFLICT_ERROR;
2742 } else {
Tejun Heo6c256cb2014-03-07 10:24:50 -05002743 peer->workfn = fwserial_handle_unplug_req;
Peter Hurley7355ba32012-11-02 08:16:33 -04002744 queue_work(system_unbound_wq, &peer->work);
2745 }
2746 break;
2747
2748 case FWSC_VIRT_CABLE_UNPLUG_RSP:
Dominique van den Broeckea595e72014-04-12 15:18:13 +02002749 if (peer->state != FWPS_UNPLUG_PENDING) {
Peter Hurley7355ba32012-11-02 08:16:33 -04002750 rcode = RCODE_CONFLICT_ERROR;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02002751 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -04002752 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
Joe Perches6e8661e2013-05-28 19:44:24 -07002753 fwtty_notice(&peer->unit, "NACK unplug?\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002754 port = peer_revert_state(peer);
Peter Hurleyde321a12013-01-28 22:34:35 -05002755 reset = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002756 }
2757 break;
2758
2759 default:
Joe Perches6e8661e2013-05-28 19:44:24 -07002760 fwtty_err(&peer->unit, "unknown mgmt code %d\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002761 be16_to_cpu(pkt->hdr.code));
2762 rcode = RCODE_DATA_ERROR;
2763 }
2764 spin_unlock_bh(&peer->lock);
2765
2766 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002767 fwserial_release_port(port, reset);
Peter Hurley7355ba32012-11-02 08:16:33 -04002768
2769 return rcode;
2770}
2771
2772/**
2773 * fwserial_mgmt_handler: bus address handler for mgmt requests
2774 * @parameters: fw_address_callback_t as specified by firewire core interface
2775 *
2776 * This handler is responsible for handling virtual cable requests from remotes
2777 * for all cards.
2778 */
2779static void fwserial_mgmt_handler(struct fw_card *card,
2780 struct fw_request *request,
2781 int tcode, int destination, int source,
2782 int generation,
2783 unsigned long long addr,
2784 void *data, size_t len,
2785 void *callback_data)
2786{
2787 struct fwserial_mgmt_pkt *pkt = data;
2788 struct fwtty_peer *peer;
2789 int rcode;
2790
2791 rcu_read_lock();
2792 peer = __fwserial_peer_by_node_id(card, generation, source);
2793 if (!peer) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002794 fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source);
Peter Hurley7355ba32012-11-02 08:16:33 -04002795 __dump_peer_list(card);
2796 rcode = RCODE_CONFLICT_ERROR;
2797
2798 } else {
2799 switch (tcode) {
2800 case TCODE_WRITE_BLOCK_REQUEST:
2801 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2802 break;
2803
2804 default:
2805 rcode = RCODE_TYPE_ERROR;
2806 }
2807 }
2808
2809 rcu_read_unlock();
2810 fw_send_response(card, request, rcode);
2811}
2812
2813static int __init fwserial_init(void)
2814{
2815 int err, num_loops = !!(create_loop_dev);
2816
Peter Hurley4df5bb02013-01-28 22:34:40 -05002817 /* XXX: placeholder for a "firewire" debugfs node */
2818 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2819
Peter Hurley7355ba32012-11-02 08:16:33 -04002820 /* num_ttys/num_ports must not be set above the static alloc avail */
2821 if (num_ttys + num_loops > MAX_CARD_PORTS)
2822 num_ttys = MAX_CARD_PORTS - num_loops;
2823 num_ports = num_ttys + num_loops;
2824
Peter Hurley84472c32013-01-28 22:34:41 -05002825 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2826 | TTY_DRIVER_DYNAMIC_DEV);
2827 if (IS_ERR(fwtty_driver)) {
2828 err = PTR_ERR(fwtty_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002829 return err;
2830 }
2831
2832 fwtty_driver->driver_name = KBUILD_MODNAME;
2833 fwtty_driver->name = tty_dev_name;
2834 fwtty_driver->major = 0;
2835 fwtty_driver->minor_start = 0;
2836 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2837 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
Peter Hurley7355ba32012-11-02 08:16:33 -04002838 fwtty_driver->init_termios = tty_std_termios;
2839 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2840 tty_set_operations(fwtty_driver, &fwtty_ops);
2841
2842 err = tty_register_driver(fwtty_driver);
2843 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002844 pr_err("register tty driver failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002845 goto put_tty;
2846 }
2847
Peter Hurleyfa1da242013-01-28 22:34:38 -05002848 if (create_loop_dev) {
Peter Hurley84472c32013-01-28 22:34:41 -05002849 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2850 TTY_DRIVER_REAL_RAW
2851 | TTY_DRIVER_DYNAMIC_DEV);
2852 if (IS_ERR(fwloop_driver)) {
2853 err = PTR_ERR(fwloop_driver);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002854 goto unregister_driver;
2855 }
2856
2857 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2858 fwloop_driver->name = loop_dev_name;
2859 fwloop_driver->major = 0;
2860 fwloop_driver->minor_start = 0;
2861 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2862 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002863 fwloop_driver->init_termios = tty_std_termios;
2864 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2865 tty_set_operations(fwloop_driver, &fwloop_ops);
2866
2867 err = tty_register_driver(fwloop_driver);
2868 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002869 pr_err("register loop driver failed (%d)\n", err);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002870 goto put_loop;
2871 }
2872 }
2873
Peter Hurley7355ba32012-11-02 08:16:33 -04002874 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2875 sizeof(struct fwtty_transaction),
2876 0, 0, fwtty_txn_constructor);
2877 if (!fwtty_txn_cache) {
2878 err = -ENOMEM;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002879 goto unregister_loop;
Peter Hurley7355ba32012-11-02 08:16:33 -04002880 }
2881
2882 /*
2883 * Ideally, this address handler would be registered per local node
2884 * (rather than the same handler for all local nodes). However,
2885 * since the firewire core requires the config rom descriptor *before*
2886 * the local unit device(s) are created, a single management handler
2887 * must suffice for all local serial units.
2888 */
2889 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2890 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2891
2892 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2893 &fwserial_mgmt_addr_region);
2894 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002895 pr_err("add management handler failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002896 goto destroy_cache;
2897 }
2898
2899 fwserial_unit_directory_data.unit_addr_offset =
2900 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2901 err = fw_core_add_descriptor(&fwserial_unit_directory);
2902 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002903 pr_err("add unit descriptor failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002904 goto remove_handler;
2905 }
2906
2907 err = driver_register(&fwserial_driver.driver);
2908 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002909 pr_err("register fwserial driver failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002910 goto remove_descriptor;
2911 }
2912
2913 return 0;
2914
2915remove_descriptor:
2916 fw_core_remove_descriptor(&fwserial_unit_directory);
2917remove_handler:
2918 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2919destroy_cache:
2920 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002921unregister_loop:
2922 if (create_loop_dev)
2923 tty_unregister_driver(fwloop_driver);
2924put_loop:
2925 if (create_loop_dev)
2926 put_tty_driver(fwloop_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002927unregister_driver:
2928 tty_unregister_driver(fwtty_driver);
2929put_tty:
2930 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05002931 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04002932 return err;
2933}
2934
2935static void __exit fwserial_exit(void)
2936{
2937 driver_unregister(&fwserial_driver.driver);
2938 fw_core_remove_descriptor(&fwserial_unit_directory);
2939 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2940 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002941 if (create_loop_dev) {
2942 tty_unregister_driver(fwloop_driver);
2943 put_tty_driver(fwloop_driver);
2944 }
Peter Hurley7355ba32012-11-02 08:16:33 -04002945 tty_unregister_driver(fwtty_driver);
2946 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05002947 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04002948}
2949
2950module_init(fwserial_init);
2951module_exit(fwserial_exit);
2952
2953MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2954MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2955MODULE_LICENSE("GPL");
2956MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
2957MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
2958MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2959MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");