blob: 384758b11e3c15ca6b0c96e07c1c399b23d7d159 [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.
Peter Hurley7355ba32012-11-02 08:16:33 -040015 */
16
Joe Perches6e8661e2013-05-28 19:44:24 -070017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Peter Hurley7355ba32012-11-02 08:16:33 -040019#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/device.h>
22#include <linux/mod_devicetable.h>
23#include <linux/rculist.h>
24#include <linux/workqueue.h>
25#include <linux/ratelimit.h>
26#include <linux/bug.h>
27#include <linux/uaccess.h>
28
29#include "fwserial.h"
30
31#define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
32
33#define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
34#define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
35
36/* configurable options */
37static int num_ttys = 4; /* # of std ttys to create per fw_card */
38 /* - doubles as loopback port index */
39static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
40static bool create_loop_dev = true; /* create a loopback device for each card */
Peter Hurley7355ba32012-11-02 08:16:33 -040041
42module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
43module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
44module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
Peter Hurley7355ba32012-11-02 08:16:33 -040045
46/*
47 * Threshold below which the tty is woken for writing
48 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
49 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
50 * our fifo is below this level
51 */
52#define WAKEUP_CHARS 256
53
54/**
55 * fwserial_list: list of every fw_serial created for each fw_card
56 * See discussion in fwserial_probe.
57 */
58static LIST_HEAD(fwserial_list);
59static DEFINE_MUTEX(fwserial_list_mutex);
60
61/**
62 * port_table: array of tty ports allocated to each fw_card
63 *
64 * tty ports are allocated during probe when an fw_serial is first
65 * created for a given fw_card. Ports are allocated in a contiguous block,
66 * each block consisting of 'num_ports' ports.
67 */
68static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
69static DEFINE_MUTEX(port_table_lock);
70static bool port_table_corrupt;
71#define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
72
Peter Hurleyfa1da242013-01-28 22:34:38 -050073#define loop_idx(port) (((port)->index) / num_ports)
74#define table_idx(loop) ((loop) * num_ports + num_ttys)
75
Peter Hurley7355ba32012-11-02 08:16:33 -040076/* total # of tty ports created per fw_card */
77static int num_ports;
78
79/* slab used as pool for struct fwtty_transactions */
80static struct kmem_cache *fwtty_txn_cache;
81
Peter Hurleya3d9ad42013-01-28 22:34:37 -050082struct tty_driver *fwtty_driver;
Peter Hurleyfa1da242013-01-28 22:34:38 -050083static struct tty_driver *fwloop_driver;
Peter Hurleya3d9ad42013-01-28 22:34:37 -050084
Peter Hurley4df5bb02013-01-28 22:34:40 -050085static struct dentry *fwserial_debugfs;
86
Peter Hurley7355ba32012-11-02 08:16:33 -040087struct fwtty_transaction;
88typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
89 void *data, size_t length,
90 struct fwtty_transaction *txn);
91
92struct fwtty_transaction {
93 struct fw_transaction fw_txn;
94 fwtty_transaction_cb callback;
95 struct fwtty_port *port;
96 union {
97 struct dma_pending dma_pended;
98 };
99};
100
101#define to_device(a, b) (a->b)
Joe Perches6e8661e2013-05-28 19:44:24 -0700102#define fwtty_err(p, fmt, ...) \
103 dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
104#define fwtty_info(p, fmt, ...) \
105 dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
106#define fwtty_notice(p, fmt, ...) \
107 dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
108#define fwtty_dbg(p, fmt, ...) \
109 dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
110#define fwtty_err_ratelimited(p, fmt, ...) \
111 dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
Peter Hurley7355ba32012-11-02 08:16:33 -0400112
113#ifdef DEBUG
114static inline void debug_short_write(struct fwtty_port *port, int c, int n)
115{
116 int avail;
117
118 if (n < c) {
119 spin_lock_bh(&port->lock);
120 avail = dma_fifo_avail(&port->tx_fifo);
121 spin_unlock_bh(&port->lock);
Joe Perches6e8661e2013-05-28 19:44:24 -0700122 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d\n",
Peter Hurley7355ba32012-11-02 08:16:33 -0400123 avail, c, n);
124 }
125}
126#else
127#define debug_short_write(port, c, n)
128#endif
129
130static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
131 int generation, int id);
132
133#ifdef FWTTY_PROFILING
134
Peter Hurley49bb8402013-11-22 13:06:10 -0500135static void fwtty_profile_fifo(struct fwtty_port *port, unsigned *stat)
Peter Hurley7355ba32012-11-02 08:16:33 -0400136{
137 spin_lock_bh(&port->lock);
Peter Hurley49bb8402013-11-22 13:06:10 -0500138 fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo));
Peter Hurley7355ba32012-11-02 08:16:33 -0400139 spin_unlock_bh(&port->lock);
140}
141
Peter Hurley49bb8402013-11-22 13:06:10 -0500142static void fwtty_dump_profile(struct seq_file *m, struct stats *stats)
Peter Hurley7355ba32012-11-02 08:16:33 -0400143{
144 /* for each stat, print sum of 0 to 2^k, then individually */
145 int k = 4;
146 unsigned sum;
147 int j;
148 char t[10];
149
150 snprintf(t, 10, "< %d", 1 << k);
151 seq_printf(m, "\n%14s %6s", " ", t);
152 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
153 seq_printf(m, "%6d", 1 << j);
154
155 ++k;
156 for (j = 0, sum = 0; j <= k; ++j)
157 sum += stats->reads[j];
158 seq_printf(m, "\n%14s: %6d", "reads", sum);
159 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
160 seq_printf(m, "%6d", stats->reads[j]);
161
162 for (j = 0, sum = 0; j <= k; ++j)
163 sum += stats->writes[j];
164 seq_printf(m, "\n%14s: %6d", "writes", sum);
165 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
166 seq_printf(m, "%6d", stats->writes[j]);
167
168 for (j = 0, sum = 0; j <= k; ++j)
169 sum += stats->txns[j];
170 seq_printf(m, "\n%14s: %6d", "txns", sum);
171 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
172 seq_printf(m, "%6d", stats->txns[j]);
173
174 for (j = 0, sum = 0; j <= k; ++j)
175 sum += stats->unthrottle[j];
176 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
177 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
178 seq_printf(m, "%6d", stats->unthrottle[j]);
179}
180
181#else
Peter Hurley49bb8402013-11-22 13:06:10 -0500182#define fwtty_profile_fifo(port, stat)
183#define fwtty_dump_profile(m, stats)
Peter Hurley7355ba32012-11-02 08:16:33 -0400184#endif
185
Peter Hurley9883a732013-01-28 20:57:48 -0500186/*
187 * Returns the max receive packet size for the given node
188 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
189 * are required by specification to support max_rec of 8 (512 bytes) or more.
190 */
Peter Hurley7355ba32012-11-02 08:16:33 -0400191static inline int device_max_receive(struct fw_device *fw_device)
192{
Peter Hurley9883a732013-01-28 20:57:48 -0500193 /* see IEEE 1394-2008 table 8-8 */
194 return min(2 << fw_device->max_rec, 4096);
Peter Hurley7355ba32012-11-02 08:16:33 -0400195}
196
197static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
198{
199 switch (rcode) {
200 case RCODE_SEND_ERROR:
Joe Perches6e8661e2013-05-28 19:44:24 -0700201 fwtty_err_ratelimited(port, "card busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400202 break;
203 case RCODE_ADDRESS_ERROR:
Joe Perches6e8661e2013-05-28 19:44:24 -0700204 fwtty_err_ratelimited(port, "bad unit addr or write length\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400205 break;
206 case RCODE_DATA_ERROR:
Joe Perches6e8661e2013-05-28 19:44:24 -0700207 fwtty_err_ratelimited(port, "failed rx\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400208 break;
209 case RCODE_NO_ACK:
Joe Perches6e8661e2013-05-28 19:44:24 -0700210 fwtty_err_ratelimited(port, "missing ack\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400211 break;
212 case RCODE_BUSY:
Joe Perches6e8661e2013-05-28 19:44:24 -0700213 fwtty_err_ratelimited(port, "remote busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400214 break;
215 default:
Joe Perches6e8661e2013-05-28 19:44:24 -0700216 fwtty_err_ratelimited(port, "failed tx: %d\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -0400217 }
218}
219
220static void fwtty_txn_constructor(void *this)
221{
222 struct fwtty_transaction *txn = this;
223
224 init_timer(&txn->fw_txn.split_timeout_timer);
225}
226
227static void fwtty_common_callback(struct fw_card *card, int rcode,
228 void *payload, size_t len, void *cb_data)
229{
230 struct fwtty_transaction *txn = cb_data;
231 struct fwtty_port *port = txn->port;
232
233 if (port && rcode != RCODE_COMPLETE)
234 fwtty_log_tx_error(port, rcode);
235 if (txn->callback)
236 txn->callback(card, rcode, payload, len, txn);
237 kmem_cache_free(fwtty_txn_cache, txn);
238}
239
240static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
241 unsigned long long addr, void *payload,
242 size_t len, fwtty_transaction_cb callback,
243 struct fwtty_port *port)
244{
245 struct fwtty_transaction *txn;
246 int generation;
247
248 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
249 if (!txn)
250 return -ENOMEM;
251
252 txn->callback = callback;
253 txn->port = port;
254
255 generation = peer->generation;
256 smp_rmb();
257 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
258 peer->node_id, generation, peer->speed, addr, payload,
259 len, fwtty_common_callback, txn);
260 return 0;
261}
262
263static void fwtty_send_txn_async(struct fwtty_peer *peer,
264 struct fwtty_transaction *txn, int tcode,
265 unsigned long long addr, void *payload,
266 size_t len, fwtty_transaction_cb callback,
267 struct fwtty_port *port)
268{
269 int generation;
270
271 txn->callback = callback;
272 txn->port = port;
273
274 generation = peer->generation;
275 smp_rmb();
276 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
277 peer->node_id, generation, peer->speed, addr, payload,
278 len, fwtty_common_callback, txn);
279}
280
281
282static void __fwtty_restart_tx(struct fwtty_port *port)
283{
284 int len, avail;
285
286 len = dma_fifo_out_level(&port->tx_fifo);
287 if (len)
288 schedule_delayed_work(&port->drain, 0);
289 avail = dma_fifo_avail(&port->tx_fifo);
290
Joe Perches6e8661e2013-05-28 19:44:24 -0700291 fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail);
Peter Hurley7355ba32012-11-02 08:16:33 -0400292}
293
294static void fwtty_restart_tx(struct fwtty_port *port)
295{
296 spin_lock_bh(&port->lock);
297 __fwtty_restart_tx(port);
298 spin_unlock_bh(&port->lock);
299}
300
301/**
302 * fwtty_update_port_status - decodes & dispatches line status changes
303 *
304 * Note: in loopback, the port->lock is being held. Only use functions that
305 * don't attempt to reclaim the port->lock.
306 */
307static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
308{
309 unsigned delta;
310 struct tty_struct *tty;
311
312 /* simulated LSR/MSR status from remote */
313 status &= ~MCTRL_MASK;
314 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
315 delta &= ~(status & TIOCM_RNG);
316 port->mstatus = status;
317
318 if (delta & TIOCM_RNG)
319 ++port->icount.rng;
320 if (delta & TIOCM_DSR)
321 ++port->icount.dsr;
322 if (delta & TIOCM_CAR)
323 ++port->icount.dcd;
324 if (delta & TIOCM_CTS)
325 ++port->icount.cts;
326
Joe Perches6e8661e2013-05-28 19:44:24 -0700327 fwtty_dbg(port, "status: %x delta: %x\n", status, delta);
Peter Hurley7355ba32012-11-02 08:16:33 -0400328
329 if (delta & TIOCM_CAR) {
330 tty = tty_port_tty_get(&port->port);
331 if (tty && !C_CLOCAL(tty)) {
332 if (status & TIOCM_CAR)
333 wake_up_interruptible(&port->port.open_wait);
334 else
335 schedule_work(&port->hangup);
336 }
337 tty_kref_put(tty);
338 }
339
340 if (delta & TIOCM_CTS) {
341 tty = tty_port_tty_get(&port->port);
342 if (tty && C_CRTSCTS(tty)) {
343 if (tty->hw_stopped) {
344 if (status & TIOCM_CTS) {
345 tty->hw_stopped = 0;
346 if (port->loopback)
347 __fwtty_restart_tx(port);
348 else
349 fwtty_restart_tx(port);
350 }
351 } else {
352 if (~status & TIOCM_CTS)
353 tty->hw_stopped = 1;
354 }
355 }
356 tty_kref_put(tty);
357
358 } else if (delta & OOB_TX_THROTTLE) {
359 tty = tty_port_tty_get(&port->port);
360 if (tty) {
361 if (tty->hw_stopped) {
362 if (~status & OOB_TX_THROTTLE) {
363 tty->hw_stopped = 0;
364 if (port->loopback)
365 __fwtty_restart_tx(port);
366 else
367 fwtty_restart_tx(port);
368 }
369 } else {
370 if (status & OOB_TX_THROTTLE)
371 tty->hw_stopped = 1;
372 }
373 }
374 tty_kref_put(tty);
375 }
376
377 if (delta & (UART_LSR_BI << 24)) {
378 if (status & (UART_LSR_BI << 24)) {
379 port->break_last = jiffies;
380 schedule_delayed_work(&port->emit_breaks, 0);
381 } else {
382 /* run emit_breaks one last time (if pending) */
383 mod_delayed_work(system_wq, &port->emit_breaks, 0);
384 }
385 }
386
387 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
388 wake_up_interruptible(&port->port.delta_msr_wait);
389}
390
391/**
392 * __fwtty_port_line_status - generate 'line status' for indicated port
393 *
394 * This function returns a remote 'MSR' state based on the local 'MCR' state,
395 * as if a null modem cable was attached. The actual status is a mangling
396 * of TIOCM_* bits suitable for sending to a peer's status_addr.
397 *
398 * Note: caller must be holding port lock
399 */
400static unsigned __fwtty_port_line_status(struct fwtty_port *port)
401{
402 unsigned status = 0;
403
404 /* TODO: add module param to tie RNG to DTR as well */
405
406 if (port->mctrl & TIOCM_DTR)
407 status |= TIOCM_DSR | TIOCM_CAR;
408 if (port->mctrl & TIOCM_RTS)
409 status |= TIOCM_CTS;
410 if (port->mctrl & OOB_RX_THROTTLE)
411 status |= OOB_TX_THROTTLE;
412 /* emulate BRK as add'l line status */
413 if (port->break_ctl)
414 status |= UART_LSR_BI << 24;
415
416 return status;
417}
418
419/**
420 * __fwtty_write_port_status - send the port line status to peer
421 *
422 * Note: caller must be holding the port lock.
423 */
424static int __fwtty_write_port_status(struct fwtty_port *port)
425{
426 struct fwtty_peer *peer;
427 int err = -ENOENT;
428 unsigned status = __fwtty_port_line_status(port);
429
430 rcu_read_lock();
431 peer = rcu_dereference(port->peer);
432 if (peer) {
433 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
434 peer->status_addr, &status,
435 sizeof(status), NULL, port);
436 }
437 rcu_read_unlock();
438
439 return err;
440}
441
442/**
443 * fwtty_write_port_status - same as above but locked by port lock
444 */
445static int fwtty_write_port_status(struct fwtty_port *port)
446{
447 int err;
448
449 spin_lock_bh(&port->lock);
450 err = __fwtty_write_port_status(port);
451 spin_unlock_bh(&port->lock);
452 return err;
453}
454
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500455static void fwtty_throttle_port(struct fwtty_port *port)
Peter Hurley7355ba32012-11-02 08:16:33 -0400456{
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500457 struct tty_struct *tty;
Peter Hurley7355ba32012-11-02 08:16:33 -0400458 unsigned old;
459
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500460 tty = tty_port_tty_get(&port->port);
461 if (!tty)
462 return;
463
464 spin_lock_bh(&port->lock);
465
Peter Hurley7355ba32012-11-02 08:16:33 -0400466 old = port->mctrl;
467 port->mctrl |= OOB_RX_THROTTLE;
468 if (C_CRTSCTS(tty))
469 port->mctrl &= ~TIOCM_RTS;
470 if (~old & OOB_RX_THROTTLE)
471 __fwtty_write_port_status(port);
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500472
473 spin_unlock_bh(&port->lock);
474
475 tty_kref_put(tty);
Peter Hurley7355ba32012-11-02 08:16:33 -0400476}
477
478/**
479 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
480 *
481 * When the remote has finished tx, and all in-flight rx has been received and
482 * and pushed to the flip buffer, the remote may close its device. This will
483 * drop DTR on the remote which will drop carrier here. Typically, the tty is
484 * hung up when carrier is dropped or lost.
485 *
486 * However, there is a race between the hang up and the line discipline
487 * delivering its data to the reader. A hangup will cause the ldisc to flush
488 * (ie., clear) the read buffer and flip buffer. Because of firewire's
489 * relatively high throughput, the ldisc frequently lags well behind the driver,
490 * resulting in lost data (which has already been received and written to
491 * the flip buffer) when the remote closes its end.
492 *
493 * Unfortunately, since the flip buffer offers no direct method for determining
494 * if it holds data, ensuring the ldisc has delivered all data is problematic.
495 */
496
497/* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
498static void fwtty_do_hangup(struct work_struct *work)
499{
500 struct fwtty_port *port = to_port(work, hangup);
501 struct tty_struct *tty;
502
503 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
504
505 tty = tty_port_tty_get(&port->port);
506 if (tty)
507 tty_vhangup(tty);
508 tty_kref_put(tty);
509}
510
511
512static void fwtty_emit_breaks(struct work_struct *work)
513{
514 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
Peter Hurley7355ba32012-11-02 08:16:33 -0400515 static const char buf[16];
516 unsigned long now = jiffies;
517 unsigned long elapsed = now - port->break_last;
518 int n, t, c, brk = 0;
519
Peter Hurley7355ba32012-11-02 08:16:33 -0400520 /* generate breaks at the line rate (but at least 1) */
521 n = (elapsed * port->cps) / HZ + 1;
522 port->break_last = now;
523
Joe Perches6e8661e2013-05-28 19:44:24 -0700524 fwtty_dbg(port, "sending %d brks\n", n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400525
526 while (n) {
527 t = min(n, 16);
Jiri Slaby2f693352013-01-03 15:53:02 +0100528 c = tty_insert_flip_string_fixed_flag(&port->port, buf,
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +0200529 TTY_BREAK, t);
Peter Hurley7355ba32012-11-02 08:16:33 -0400530 n -= c;
531 brk += c;
532 if (c < t)
533 break;
534 }
Jiri Slaby2e124b42013-01-03 15:53:06 +0100535 tty_flip_buffer_push(&port->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400536
537 if (port->mstatus & (UART_LSR_BI << 24))
538 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
539 port->icount.brk += brk;
540}
541
Peter Hurley7355ba32012-11-02 08:16:33 -0400542static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
543{
Peter Hurley7355ba32012-11-02 08:16:33 -0400544 int c, n = len;
545 unsigned lsr;
546 int err = 0;
547
Joe Perches6e8661e2013-05-28 19:44:24 -0700548 fwtty_dbg(port, "%d\n", n);
Peter Hurley49bb8402013-11-22 13:06:10 -0500549 fwtty_profile_data(port->stats.reads, n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400550
551 if (port->write_only) {
552 n = 0;
553 goto out;
554 }
555
556 /* disregard break status; breaks are generated by emit_breaks work */
557 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
558
559 if (port->overrun)
560 lsr |= UART_LSR_OE;
561
562 if (lsr & UART_LSR_OE)
563 ++port->icount.overrun;
564
565 lsr &= port->status_mask;
566 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100567 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400568 err = -EIO;
569 goto out;
570 }
571 }
572 port->overrun = false;
573
574 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
575 /* TODO: don't drop SAK and Magic SysRq here */
576 n = 0;
577 goto out;
578 }
579
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500580 c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n);
581 if (c > 0)
582 tty_flip_buffer_push(&port->port);
583 n -= c;
Peter Hurley7355ba32012-11-02 08:16:33 -0400584
585 if (n) {
586 port->overrun = true;
587 err = -EIO;
Peter Hurleyc4a8dab2013-11-22 13:06:08 -0500588 fwtty_err_ratelimited(port, "flip buffer overrun\n");
589
590 } else {
591 /* throttle the sender if remaining flip buffer space has
592 * reached high watermark to avoid losing data which may be
593 * in-flight. Since the AR request context is 32k, that much
594 * data may have _already_ been acked.
595 */
596 if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK)
597 fwtty_throttle_port(port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400598 }
599
600out:
Peter Hurley7355ba32012-11-02 08:16:33 -0400601 port->icount.rx += len;
602 port->stats.lost += n;
603 return err;
604}
605
606/**
607 * fwtty_port_handler - bus address handler for port reads/writes
608 * @parameters: fw_address_callback_t as specified by firewire core interface
609 *
610 * This handler is responsible for handling inbound read/write dma from remotes.
611 */
612static void fwtty_port_handler(struct fw_card *card,
613 struct fw_request *request,
614 int tcode, int destination, int source,
615 int generation,
616 unsigned long long addr,
617 void *data, size_t len,
618 void *callback_data)
619{
620 struct fwtty_port *port = callback_data;
621 struct fwtty_peer *peer;
622 int err;
623 int rcode;
624
625 /* Only accept rx from the peer virtual-cabled to this port */
626 rcu_read_lock();
627 peer = __fwserial_peer_by_node_id(card, generation, source);
628 rcu_read_unlock();
629 if (!peer || peer != rcu_access_pointer(port->peer)) {
630 rcode = RCODE_ADDRESS_ERROR;
Joe Perches6e8661e2013-05-28 19:44:24 -0700631 fwtty_err_ratelimited(port, "ignoring unauthenticated data\n");
Peter Hurley7355ba32012-11-02 08:16:33 -0400632 goto respond;
633 }
634
635 switch (tcode) {
636 case TCODE_WRITE_QUADLET_REQUEST:
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200637 if (addr != port->rx_handler.offset || len != 4) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400638 rcode = RCODE_ADDRESS_ERROR;
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200639 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -0400640 fwtty_update_port_status(port, *(unsigned *)data);
641 rcode = RCODE_COMPLETE;
642 }
643 break;
644
645 case TCODE_WRITE_BLOCK_REQUEST:
646 if (addr != port->rx_handler.offset + 4 ||
647 len > port->rx_handler.length - 4) {
648 rcode = RCODE_ADDRESS_ERROR;
649 } else {
650 err = fwtty_rx(port, data, len);
651 switch (err) {
652 case 0:
653 rcode = RCODE_COMPLETE;
654 break;
655 case -EIO:
656 rcode = RCODE_DATA_ERROR;
657 break;
658 default:
659 rcode = RCODE_CONFLICT_ERROR;
660 break;
661 }
662 }
663 break;
664
665 default:
666 rcode = RCODE_TYPE_ERROR;
667 }
668
669respond:
670 fw_send_response(card, request, rcode);
671}
672
673/**
674 * fwtty_tx_complete - callback for tx dma
675 * @data: ignored, has no meaning for write txns
676 * @length: ignored, has no meaning for write txns
677 *
678 * The writer must be woken here if the fifo has been emptied because it
679 * may have slept if chars_in_buffer was != 0
680 */
681static void fwtty_tx_complete(struct fw_card *card, int rcode,
682 void *data, size_t length,
683 struct fwtty_transaction *txn)
684{
685 struct fwtty_port *port = txn->port;
Peter Hurley7355ba32012-11-02 08:16:33 -0400686 int len;
687
Joe Perches6e8661e2013-05-28 19:44:24 -0700688 fwtty_dbg(port, "rcode: %d\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -0400689
690 switch (rcode) {
691 case RCODE_COMPLETE:
692 spin_lock_bh(&port->lock);
693 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
694 len = dma_fifo_level(&port->tx_fifo);
695 spin_unlock_bh(&port->lock);
696
697 port->icount.tx += txn->dma_pended.len;
698 break;
699
700 default:
701 /* TODO: implement retries */
702 spin_lock_bh(&port->lock);
703 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
704 len = dma_fifo_level(&port->tx_fifo);
705 spin_unlock_bh(&port->lock);
706
707 port->stats.dropped += txn->dma_pended.len;
708 }
709
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100710 if (len < WAKEUP_CHARS)
711 tty_port_tty_wakeup(&port->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400712}
713
714static int fwtty_tx(struct fwtty_port *port, bool drain)
715{
716 struct fwtty_peer *peer;
717 struct fwtty_transaction *txn;
718 struct tty_struct *tty;
719 int n, len;
720
721 tty = tty_port_tty_get(&port->port);
722 if (!tty)
723 return -ENOENT;
724
725 rcu_read_lock();
726 peer = rcu_dereference(port->peer);
727 if (!peer) {
728 n = -EIO;
729 goto out;
730 }
731
732 if (test_and_set_bit(IN_TX, &port->flags)) {
733 n = -EALREADY;
734 goto out;
735 }
736
737 /* try to write as many dma transactions out as possible */
738 n = -EAGAIN;
739 while (!tty->stopped && !tty->hw_stopped &&
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +0200740 !test_bit(STOP_TX, &port->flags)) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400741 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
742 if (!txn) {
743 n = -ENOMEM;
744 break;
745 }
746
747 spin_lock_bh(&port->lock);
748 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
749 spin_unlock_bh(&port->lock);
750
Joe Perches6e8661e2013-05-28 19:44:24 -0700751 fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400752
753 if (n < 0) {
754 kmem_cache_free(fwtty_txn_cache, txn);
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200755 if (n == -EAGAIN) {
Peter Hurley7355ba32012-11-02 08:16:33 -0400756 ++port->stats.tx_stall;
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200757 } else if (n == -ENODATA) {
Peter Hurley49bb8402013-11-22 13:06:10 -0500758 fwtty_profile_data(port->stats.txns, 0);
Dominique van den Broeckea595e72014-04-12 15:18:13 +0200759 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -0400760 ++port->stats.fifo_errs;
Joe Perches6e8661e2013-05-28 19:44:24 -0700761 fwtty_err_ratelimited(port, "fifo err: %d\n",
762 n);
Peter Hurley7355ba32012-11-02 08:16:33 -0400763 }
764 break;
765 }
766
Peter Hurley49bb8402013-11-22 13:06:10 -0500767 fwtty_profile_data(port->stats.txns, txn->dma_pended.len);
Peter Hurley7355ba32012-11-02 08:16:33 -0400768
769 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
770 peer->fifo_addr, txn->dma_pended.data,
771 txn->dma_pended.len, fwtty_tx_complete,
772 port);
773 ++port->stats.sent;
774
775 /*
776 * Stop tx if the 'last view' of the fifo is empty or if
777 * this is the writer and there's not enough data to bother
778 */
779 if (n == 0 || (!drain && n < WRITER_MINIMUM))
780 break;
781 }
782
783 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
784 spin_lock_bh(&port->lock);
785 len = dma_fifo_out_level(&port->tx_fifo);
786 if (len) {
787 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
788 schedule_delayed_work(&port->drain, delay);
789 }
790 len = dma_fifo_level(&port->tx_fifo);
791 spin_unlock_bh(&port->lock);
792
793 /* wakeup the writer */
794 if (drain && len < WAKEUP_CHARS)
795 tty_wakeup(tty);
796 }
797
798 clear_bit(IN_TX, &port->flags);
799 wake_up_interruptible(&port->wait_tx);
800
801out:
802 rcu_read_unlock();
803 tty_kref_put(tty);
804 return n;
805}
806
807static void fwtty_drain_tx(struct work_struct *work)
808{
809 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
810
811 fwtty_tx(port, true);
812}
813
814static void fwtty_write_xchar(struct fwtty_port *port, char ch)
815{
816 struct fwtty_peer *peer;
817
818 ++port->stats.xchars;
819
Joe Perches6e8661e2013-05-28 19:44:24 -0700820 fwtty_dbg(port, "%02x\n", ch);
Peter Hurley7355ba32012-11-02 08:16:33 -0400821
822 rcu_read_lock();
823 peer = rcu_dereference(port->peer);
824 if (peer) {
825 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
826 peer->fifo_addr, &ch, sizeof(ch),
827 NULL, port);
828 }
829 rcu_read_unlock();
830}
831
832struct fwtty_port *fwtty_port_get(unsigned index)
833{
834 struct fwtty_port *port;
835
836 if (index >= MAX_TOTAL_PORTS)
837 return NULL;
838
839 mutex_lock(&port_table_lock);
840 port = port_table[index];
841 if (port)
842 kref_get(&port->serial->kref);
843 mutex_unlock(&port_table_lock);
844 return port;
845}
846EXPORT_SYMBOL(fwtty_port_get);
847
848static int fwtty_ports_add(struct fw_serial *serial)
849{
850 int err = -EBUSY;
851 int i, j;
852
853 if (port_table_corrupt)
854 return err;
855
856 mutex_lock(&port_table_lock);
857 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
858 if (!port_table[i]) {
859 for (j = 0; j < num_ports; ++i, ++j) {
860 serial->ports[j]->index = i;
861 port_table[i] = serial->ports[j];
862 }
863 err = 0;
864 break;
865 }
866 }
867 mutex_unlock(&port_table_lock);
868 return err;
869}
870
871static void fwserial_destroy(struct kref *kref)
872{
873 struct fw_serial *serial = to_serial(kref, kref);
874 struct fwtty_port **ports = serial->ports;
875 int j, i = ports[0]->index;
876
877 synchronize_rcu();
878
879 mutex_lock(&port_table_lock);
880 for (j = 0; j < num_ports; ++i, ++j) {
Peter Hurley49b27462012-11-27 21:37:12 -0500881 port_table_corrupt |= port_table[i] != ports[j];
882 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +0200883 i, port_table[i], j, ports[j]);
Peter Hurley7355ba32012-11-02 08:16:33 -0400884
885 port_table[i] = NULL;
886 }
887 mutex_unlock(&port_table_lock);
888
889 for (j = 0; j < num_ports; ++j) {
890 fw_core_remove_address_handler(&ports[j]->rx_handler);
Peter Hurleya3218462012-11-27 21:37:11 -0500891 tty_port_destroy(&ports[j]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400892 kfree(ports[j]);
893 }
894 kfree(serial);
895}
896
897void fwtty_port_put(struct fwtty_port *port)
898{
899 kref_put(&port->serial->kref, fwserial_destroy);
900}
901EXPORT_SYMBOL(fwtty_port_put);
902
903static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
904{
905 struct fwtty_port *port = to_port(tty_port, port);
906
Joe Perches6e8661e2013-05-28 19:44:24 -0700907 fwtty_dbg(port, "on/off: %d\n", on);
Peter Hurley7355ba32012-11-02 08:16:33 -0400908
909 spin_lock_bh(&port->lock);
910 /* Don't change carrier state if this is a console */
911 if (!port->port.console) {
912 if (on)
913 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
914 else
915 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
916 }
917
918 __fwtty_write_port_status(port);
919 spin_unlock_bh(&port->lock);
920}
921
922/**
923 * fwtty_port_carrier_raised: required tty_port operation
924 *
925 * This port operation is polled after a tty has been opened and is waiting for
926 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
927 */
928static int fwtty_port_carrier_raised(struct tty_port *tty_port)
929{
930 struct fwtty_port *port = to_port(tty_port, port);
931 int rc;
932
933 rc = (port->mstatus & TIOCM_CAR);
934
Joe Perches6e8661e2013-05-28 19:44:24 -0700935 fwtty_dbg(port, "%d\n", rc);
Peter Hurley7355ba32012-11-02 08:16:33 -0400936
937 return rc;
938}
939
940static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
941{
942 unsigned baud, frame;
943
944 baud = tty_termios_baud_rate(&tty->termios);
945 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
946
947 /* compute bit count of 2 frames */
948 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
949
950 switch (C_CSIZE(tty)) {
951 case CS5:
952 frame -= (C_CSTOPB(tty)) ? 1 : 0;
953 break;
954 case CS6:
955 frame += 2;
956 break;
957 case CS7:
958 frame += 4;
959 break;
960 case CS8:
961 frame += 6;
962 break;
963 }
964
965 port->cps = (baud << 1) / frame;
966
967 port->status_mask = UART_LSR_OE;
968 if (_I_FLAG(tty, BRKINT | PARMRK))
969 port->status_mask |= UART_LSR_BI;
970
971 port->ignore_mask = 0;
972 if (I_IGNBRK(tty)) {
973 port->ignore_mask |= UART_LSR_BI;
974 if (I_IGNPAR(tty))
975 port->ignore_mask |= UART_LSR_OE;
976 }
977
978 port->write_only = !C_CREAD(tty);
979
980 /* turn off echo and newline xlat if loopback */
981 if (port->loopback) {
982 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
983 ECHONL | ECHOPRT | ECHOCTL);
984 tty->termios.c_oflag &= ~ONLCR;
985 }
986
987 return baud;
988}
989
990static int fwtty_port_activate(struct tty_port *tty_port,
991 struct tty_struct *tty)
992{
993 struct fwtty_port *port = to_port(tty_port, port);
994 unsigned baud;
995 int err;
996
997 set_bit(TTY_IO_ERROR, &tty->flags);
998
999 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
1000 cache_line_size(),
1001 port->max_payload,
1002 FWTTY_PORT_MAX_PEND_DMA,
1003 GFP_KERNEL);
1004 if (err)
1005 return err;
1006
1007 spin_lock_bh(&port->lock);
1008
1009 baud = set_termios(port, tty);
1010
1011 /* if console, don't change carrier state */
1012 if (!port->port.console) {
1013 port->mctrl = 0;
1014 if (baud != 0)
1015 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1016 }
1017
1018 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1019 tty->hw_stopped = 1;
1020
1021 __fwtty_write_port_status(port);
1022 spin_unlock_bh(&port->lock);
1023
1024 clear_bit(TTY_IO_ERROR, &tty->flags);
1025
1026 return 0;
1027}
1028
1029/**
1030 * fwtty_port_shutdown
1031 *
1032 * Note: the tty port core ensures this is not the console and
1033 * manages TTY_IO_ERROR properly
1034 */
1035static void fwtty_port_shutdown(struct tty_port *tty_port)
1036{
1037 struct fwtty_port *port = to_port(tty_port, port);
Peter Hurley7355ba32012-11-02 08:16:33 -04001038
1039 /* TODO: cancel outstanding transactions */
1040
1041 cancel_delayed_work_sync(&port->emit_breaks);
1042 cancel_delayed_work_sync(&port->drain);
Peter Hurley7355ba32012-11-02 08:16:33 -04001043
1044 spin_lock_bh(&port->lock);
Peter Hurley7355ba32012-11-02 08:16:33 -04001045 port->flags = 0;
1046 port->break_ctl = 0;
1047 port->overrun = 0;
1048 __fwtty_write_port_status(port);
1049 dma_fifo_free(&port->tx_fifo);
1050 spin_unlock_bh(&port->lock);
1051}
1052
1053static int fwtty_open(struct tty_struct *tty, struct file *fp)
1054{
1055 struct fwtty_port *port = tty->driver_data;
1056
1057 return tty_port_open(&port->port, tty, fp);
1058}
1059
1060static void fwtty_close(struct tty_struct *tty, struct file *fp)
1061{
1062 struct fwtty_port *port = tty->driver_data;
1063
1064 tty_port_close(&port->port, tty, fp);
1065}
1066
1067static void fwtty_hangup(struct tty_struct *tty)
1068{
1069 struct fwtty_port *port = tty->driver_data;
1070
1071 tty_port_hangup(&port->port);
1072}
1073
1074static void fwtty_cleanup(struct tty_struct *tty)
1075{
1076 struct fwtty_port *port = tty->driver_data;
1077
1078 tty->driver_data = NULL;
1079 fwtty_port_put(port);
1080}
1081
1082static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1083{
1084 struct fwtty_port *port = fwtty_port_get(tty->index);
1085 int err;
1086
1087 err = tty_standard_install(driver, tty);
1088 if (!err)
1089 tty->driver_data = port;
1090 else
1091 fwtty_port_put(port);
1092 return err;
1093}
1094
Peter Hurleyfa1da242013-01-28 22:34:38 -05001095static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1096{
1097 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1098 int err;
1099
1100 err = tty_standard_install(driver, tty);
1101 if (!err)
1102 tty->driver_data = port;
1103 else
1104 fwtty_port_put(port);
1105 return err;
1106}
1107
Peter Hurley7355ba32012-11-02 08:16:33 -04001108static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1109{
1110 struct fwtty_port *port = tty->driver_data;
1111 int n, len;
1112
Joe Perches6e8661e2013-05-28 19:44:24 -07001113 fwtty_dbg(port, "%d\n", c);
Peter Hurley49bb8402013-11-22 13:06:10 -05001114 fwtty_profile_data(port->stats.writes, c);
Peter Hurley7355ba32012-11-02 08:16:33 -04001115
1116 spin_lock_bh(&port->lock);
1117 n = dma_fifo_in(&port->tx_fifo, buf, c);
1118 len = dma_fifo_out_level(&port->tx_fifo);
1119 if (len < DRAIN_THRESHOLD)
1120 schedule_delayed_work(&port->drain, 1);
1121 spin_unlock_bh(&port->lock);
1122
1123 if (len >= DRAIN_THRESHOLD)
1124 fwtty_tx(port, false);
1125
1126 debug_short_write(port, c, n);
1127
1128 return (n < 0) ? 0 : n;
1129}
1130
1131static int fwtty_write_room(struct tty_struct *tty)
1132{
1133 struct fwtty_port *port = tty->driver_data;
1134 int n;
1135
1136 spin_lock_bh(&port->lock);
1137 n = dma_fifo_avail(&port->tx_fifo);
1138 spin_unlock_bh(&port->lock);
1139
Joe Perches6e8661e2013-05-28 19:44:24 -07001140 fwtty_dbg(port, "%d\n", n);
Peter Hurley7355ba32012-11-02 08:16:33 -04001141
1142 return n;
1143}
1144
1145static int fwtty_chars_in_buffer(struct tty_struct *tty)
1146{
1147 struct fwtty_port *port = tty->driver_data;
1148 int n;
1149
1150 spin_lock_bh(&port->lock);
1151 n = dma_fifo_level(&port->tx_fifo);
1152 spin_unlock_bh(&port->lock);
1153
Joe Perches6e8661e2013-05-28 19:44:24 -07001154 fwtty_dbg(port, "%d\n", n);
Peter Hurley7355ba32012-11-02 08:16:33 -04001155
1156 return n;
1157}
1158
1159static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1160{
1161 struct fwtty_port *port = tty->driver_data;
1162
Joe Perches6e8661e2013-05-28 19:44:24 -07001163 fwtty_dbg(port, "%02x\n", ch);
Peter Hurley7355ba32012-11-02 08:16:33 -04001164
1165 fwtty_write_xchar(port, ch);
1166}
1167
1168static void fwtty_throttle(struct tty_struct *tty)
1169{
1170 struct fwtty_port *port = tty->driver_data;
1171
1172 /*
1173 * Ignore throttling (but not unthrottling).
1174 * It only makes sense to throttle when data will no longer be
1175 * accepted by the tty flip buffer. For example, it is
1176 * possible for received data to overflow the tty buffer long
1177 * before the line discipline ever has a chance to throttle the driver.
1178 * Additionally, the driver may have already completed the I/O
1179 * but the tty buffer is still emptying, so the line discipline is
1180 * throttling and unthrottling nothing.
1181 */
1182
1183 ++port->stats.throttled;
1184}
1185
1186static void fwtty_unthrottle(struct tty_struct *tty)
1187{
1188 struct fwtty_port *port = tty->driver_data;
1189
Joe Perches6e8661e2013-05-28 19:44:24 -07001190 fwtty_dbg(port, "CRTSCTS: %d\n", (C_CRTSCTS(tty) != 0));
Peter Hurley7355ba32012-11-02 08:16:33 -04001191
Peter Hurley49bb8402013-11-22 13:06:10 -05001192 fwtty_profile_fifo(port, port->stats.unthrottle);
Peter Hurley7355ba32012-11-02 08:16:33 -04001193
Peter Hurley7355ba32012-11-02 08:16:33 -04001194 spin_lock_bh(&port->lock);
1195 port->mctrl &= ~OOB_RX_THROTTLE;
1196 if (C_CRTSCTS(tty))
1197 port->mctrl |= TIOCM_RTS;
1198 __fwtty_write_port_status(port);
1199 spin_unlock_bh(&port->lock);
1200}
1201
1202static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1203 struct async_icount *prev)
1204{
1205 struct async_icount now;
1206 int delta;
1207
1208 now = port->icount;
1209
1210 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1211 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1212 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1213 (mask & TIOCM_CTS && prev->cts != now.cts));
1214
1215 *prev = now;
1216
1217 return delta;
1218}
1219
1220static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1221{
1222 struct async_icount prev;
1223
1224 prev = port->icount;
1225
1226 return wait_event_interruptible(port->port.delta_msr_wait,
1227 check_msr_delta(port, mask, &prev));
1228}
1229
1230static int get_serial_info(struct fwtty_port *port,
1231 struct serial_struct __user *info)
1232{
1233 struct serial_struct tmp;
1234
1235 memset(&tmp, 0, sizeof(tmp));
1236
1237 tmp.type = PORT_UNKNOWN;
1238 tmp.line = port->port.tty->index;
1239 tmp.flags = port->port.flags;
1240 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1241 tmp.baud_base = 400000000;
1242 tmp.close_delay = port->port.close_delay;
1243
1244 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1245}
1246
1247static int set_serial_info(struct fwtty_port *port,
1248 struct serial_struct __user *info)
1249{
1250 struct serial_struct tmp;
1251
1252 if (copy_from_user(&tmp, info, sizeof(tmp)))
1253 return -EFAULT;
1254
1255 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
Dominique van den Broeck340bb3d2014-04-12 15:18:12 +02001256 tmp.baud_base != 400000000)
Peter Hurley7355ba32012-11-02 08:16:33 -04001257 return -EPERM;
1258
1259 if (!capable(CAP_SYS_ADMIN)) {
1260 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1261 (port->port.flags & ~ASYNC_USR_MASK)))
1262 return -EPERM;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001263 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -04001264 port->port.close_delay = tmp.close_delay * HZ / 100;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001265 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001266
1267 return 0;
1268}
1269
1270static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1271 unsigned long arg)
1272{
1273 struct fwtty_port *port = tty->driver_data;
1274 int err;
1275
1276 switch (cmd) {
1277 case TIOCGSERIAL:
1278 mutex_lock(&port->port.mutex);
1279 err = get_serial_info(port, (void __user *)arg);
1280 mutex_unlock(&port->port.mutex);
1281 break;
1282
1283 case TIOCSSERIAL:
1284 mutex_lock(&port->port.mutex);
1285 err = set_serial_info(port, (void __user *)arg);
1286 mutex_unlock(&port->port.mutex);
1287 break;
1288
1289 case TIOCMIWAIT:
1290 err = wait_msr_change(port, arg);
1291 break;
1292
1293 default:
1294 err = -ENOIOCTLCMD;
1295 }
1296
1297 return err;
1298}
1299
1300static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1301{
1302 struct fwtty_port *port = tty->driver_data;
1303 unsigned baud;
1304
1305 spin_lock_bh(&port->lock);
1306 baud = set_termios(port, tty);
1307
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001308 if ((baud == 0) && (old->c_cflag & CBAUD)) {
Peter Hurley7355ba32012-11-02 08:16:33 -04001309 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001310 } else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
Peter Hurley7355ba32012-11-02 08:16:33 -04001311 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1312 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1313 else
1314 port->mctrl |= TIOCM_DTR;
1315 }
1316 __fwtty_write_port_status(port);
1317 spin_unlock_bh(&port->lock);
1318
1319 if (old->c_cflag & CRTSCTS) {
1320 if (!C_CRTSCTS(tty)) {
1321 tty->hw_stopped = 0;
1322 fwtty_restart_tx(port);
1323 }
1324 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1325 tty->hw_stopped = 1;
1326 }
1327}
1328
1329/**
1330 * fwtty_break_ctl - start/stop sending breaks
1331 *
1332 * Signals the remote to start or stop generating simulated breaks.
1333 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1334 * before signalling the break line status. This guarantees any pending rx will
1335 * be queued to the line discipline before break is simulated on the remote.
1336 * Conversely, turning off break_ctl requires signalling the line status change,
1337 * then enabling tx.
1338 */
1339static int fwtty_break_ctl(struct tty_struct *tty, int state)
1340{
1341 struct fwtty_port *port = tty->driver_data;
1342 long ret;
1343
Joe Perches6e8661e2013-05-28 19:44:24 -07001344 fwtty_dbg(port, "%d\n", state);
Peter Hurley7355ba32012-11-02 08:16:33 -04001345
1346 if (state == -1) {
1347 set_bit(STOP_TX, &port->flags);
1348 ret = wait_event_interruptible_timeout(port->wait_tx,
1349 !test_bit(IN_TX, &port->flags),
1350 10);
1351 if (ret == 0 || ret == -ERESTARTSYS) {
1352 clear_bit(STOP_TX, &port->flags);
1353 fwtty_restart_tx(port);
1354 return -EINTR;
1355 }
1356 }
1357
1358 spin_lock_bh(&port->lock);
1359 port->break_ctl = (state == -1);
1360 __fwtty_write_port_status(port);
1361 spin_unlock_bh(&port->lock);
1362
1363 if (state == 0) {
1364 spin_lock_bh(&port->lock);
1365 dma_fifo_reset(&port->tx_fifo);
1366 clear_bit(STOP_TX, &port->flags);
1367 spin_unlock_bh(&port->lock);
1368 }
1369 return 0;
1370}
1371
1372static int fwtty_tiocmget(struct tty_struct *tty)
1373{
1374 struct fwtty_port *port = tty->driver_data;
1375 unsigned tiocm;
1376
1377 spin_lock_bh(&port->lock);
1378 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1379 spin_unlock_bh(&port->lock);
1380
Joe Perches6e8661e2013-05-28 19:44:24 -07001381 fwtty_dbg(port, "%x\n", tiocm);
Peter Hurley7355ba32012-11-02 08:16:33 -04001382
1383 return tiocm;
1384}
1385
1386static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1387{
1388 struct fwtty_port *port = tty->driver_data;
1389
Joe Perches6e8661e2013-05-28 19:44:24 -07001390 fwtty_dbg(port, "set: %x clear: %x\n", set, clear);
Peter Hurley7355ba32012-11-02 08:16:33 -04001391
1392 /* TODO: simulate loopback if TIOCM_LOOP set */
1393
1394 spin_lock_bh(&port->lock);
1395 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1396 port->mctrl |= set & MCTRL_MASK & 0xffff;
1397 __fwtty_write_port_status(port);
1398 spin_unlock_bh(&port->lock);
1399 return 0;
1400}
1401
1402static int fwtty_get_icount(struct tty_struct *tty,
1403 struct serial_icounter_struct *icount)
1404{
1405 struct fwtty_port *port = tty->driver_data;
1406 struct stats stats;
1407
1408 memcpy(&stats, &port->stats, sizeof(stats));
1409 if (port->port.console)
1410 (*port->fwcon_ops->stats)(&stats, port->con_data);
1411
1412 icount->cts = port->icount.cts;
1413 icount->dsr = port->icount.dsr;
1414 icount->rng = port->icount.rng;
1415 icount->dcd = port->icount.dcd;
1416 icount->rx = port->icount.rx;
1417 icount->tx = port->icount.tx + stats.xchars;
1418 icount->frame = port->icount.frame;
1419 icount->overrun = port->icount.overrun;
1420 icount->parity = port->icount.parity;
1421 icount->brk = port->icount.brk;
1422 icount->buf_overrun = port->icount.overrun;
1423 return 0;
1424}
1425
1426static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1427{
1428 struct stats stats;
1429
1430 memcpy(&stats, &port->stats, sizeof(stats));
1431 if (port->port.console)
1432 (*port->fwcon_ops->stats)(&stats, port->con_data);
1433
Peter Hurleye16d1de2013-01-28 22:34:39 -05001434 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1435 port->icount.tx + stats.xchars, port->icount.rx);
Peter Hurley7355ba32012-11-02 08:16:33 -04001436 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1437 port->icount.dsr, port->icount.rng, port->icount.dcd);
1438 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1439 port->icount.overrun, port->icount.parity, port->icount.brk);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001440}
1441
1442static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1443{
1444 struct stats stats;
1445
1446 memcpy(&stats, &port->stats, sizeof(stats));
1447 if (port->port.console)
1448 (*port->fwcon_ops->stats)(&stats, port->con_data);
1449
Peter Hurley7355ba32012-11-02 08:16:33 -04001450 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1451 stats.tx_stall, stats.fifo_errs, stats.lost);
Peter Hurleyc4a8dab2013-11-22 13:06:08 -05001452 seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled);
Peter Hurley7355ba32012-11-02 08:16:33 -04001453
1454 if (port->port.console) {
Valentin Iliedd7cc7e2013-04-08 13:49:21 +03001455 seq_puts(m, "\n ");
Peter Hurley7355ba32012-11-02 08:16:33 -04001456 (*port->fwcon_ops->proc_show)(m, port->con_data);
1457 }
1458
Peter Hurley49bb8402013-11-22 13:06:10 -05001459 fwtty_dump_profile(m, &port->stats);
Peter Hurley7355ba32012-11-02 08:16:33 -04001460}
1461
Peter Hurleye16d1de2013-01-28 22:34:39 -05001462static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
Peter Hurley7355ba32012-11-02 08:16:33 -04001463{
1464 int generation = peer->generation;
1465
1466 smp_rmb();
1467 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1468 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1469 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1470 peer->max_payload, (unsigned long long) peer->guid);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001471 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
1472 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
Peter Hurley7355ba32012-11-02 08:16:33 -04001473 seq_putc(m, '\n');
1474}
1475
1476static int fwtty_proc_show(struct seq_file *m, void *v)
1477{
1478 struct fwtty_port *port;
Peter Hurley7355ba32012-11-02 08:16:33 -04001479 int i;
1480
1481 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1482 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1483 seq_printf(m, "%2d:", i);
1484 if (capable(CAP_SYS_ADMIN))
1485 fwtty_proc_show_port(m, port);
1486 fwtty_port_put(port);
Valentin Iliedd7cc7e2013-04-08 13:49:21 +03001487 seq_puts(m, "\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04001488 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001489 return 0;
1490}
1491
Peter Hurley4df5bb02013-01-28 22:34:40 -05001492static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
1493{
1494 struct fw_serial *serial = m->private;
1495 struct fwtty_port *port;
1496 int i;
1497
1498 for (i = 0; i < num_ports; ++i) {
1499 port = fwtty_port_get(serial->ports[i]->index);
1500 if (port) {
1501 seq_printf(m, "%2d:", port->index);
1502 fwtty_proc_show_port(m, port);
1503 fwtty_debugfs_show_port(m, port);
1504 fwtty_port_put(port);
Valentin Iliedd7cc7e2013-04-08 13:49:21 +03001505 seq_puts(m, "\n");
Peter Hurley4df5bb02013-01-28 22:34:40 -05001506 }
1507 }
1508 return 0;
1509}
1510
1511static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
1512{
1513 struct fw_serial *serial = m->private;
1514 struct fwtty_peer *peer;
1515
1516 rcu_read_lock();
1517 seq_printf(m, "card: %s guid: %016llx\n",
1518 dev_name(serial->card->device),
1519 (unsigned long long) serial->card->guid);
1520 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1521 fwtty_debugfs_show_peer(m, peer);
1522 rcu_read_unlock();
1523 return 0;
1524}
1525
Peter Hurley7355ba32012-11-02 08:16:33 -04001526static int fwtty_proc_open(struct inode *inode, struct file *fp)
1527{
1528 return single_open(fp, fwtty_proc_show, NULL);
1529}
1530
Peter Hurley4df5bb02013-01-28 22:34:40 -05001531static int fwtty_stats_open(struct inode *inode, struct file *fp)
1532{
1533 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
1534}
1535
1536static int fwtty_peers_open(struct inode *inode, struct file *fp)
1537{
1538 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
1539}
1540
1541static const struct file_operations fwtty_stats_fops = {
1542 .owner = THIS_MODULE,
1543 .open = fwtty_stats_open,
1544 .read = seq_read,
1545 .llseek = seq_lseek,
1546 .release = single_release,
1547};
1548
1549static const struct file_operations fwtty_peers_fops = {
1550 .owner = THIS_MODULE,
1551 .open = fwtty_peers_open,
1552 .read = seq_read,
1553 .llseek = seq_lseek,
1554 .release = single_release,
1555};
1556
Peter Hurley7355ba32012-11-02 08:16:33 -04001557static const struct file_operations fwtty_proc_fops = {
1558 .owner = THIS_MODULE,
1559 .open = fwtty_proc_open,
1560 .read = seq_read,
1561 .llseek = seq_lseek,
1562 .release = single_release,
1563};
1564
1565static const struct tty_port_operations fwtty_port_ops = {
1566 .dtr_rts = fwtty_port_dtr_rts,
1567 .carrier_raised = fwtty_port_carrier_raised,
1568 .shutdown = fwtty_port_shutdown,
1569 .activate = fwtty_port_activate,
1570};
1571
1572static const struct tty_operations fwtty_ops = {
1573 .open = fwtty_open,
1574 .close = fwtty_close,
1575 .hangup = fwtty_hangup,
1576 .cleanup = fwtty_cleanup,
1577 .install = fwtty_install,
1578 .write = fwtty_write,
1579 .write_room = fwtty_write_room,
1580 .chars_in_buffer = fwtty_chars_in_buffer,
1581 .send_xchar = fwtty_send_xchar,
1582 .throttle = fwtty_throttle,
1583 .unthrottle = fwtty_unthrottle,
1584 .ioctl = fwtty_ioctl,
1585 .set_termios = fwtty_set_termios,
1586 .break_ctl = fwtty_break_ctl,
1587 .tiocmget = fwtty_tiocmget,
1588 .tiocmset = fwtty_tiocmset,
1589 .get_icount = fwtty_get_icount,
1590 .proc_fops = &fwtty_proc_fops,
1591};
1592
Peter Hurleyfa1da242013-01-28 22:34:38 -05001593static const struct tty_operations fwloop_ops = {
1594 .open = fwtty_open,
1595 .close = fwtty_close,
1596 .hangup = fwtty_hangup,
1597 .cleanup = fwtty_cleanup,
1598 .install = fwloop_install,
1599 .write = fwtty_write,
1600 .write_room = fwtty_write_room,
1601 .chars_in_buffer = fwtty_chars_in_buffer,
1602 .send_xchar = fwtty_send_xchar,
1603 .throttle = fwtty_throttle,
1604 .unthrottle = fwtty_unthrottle,
1605 .ioctl = fwtty_ioctl,
1606 .set_termios = fwtty_set_termios,
1607 .break_ctl = fwtty_break_ctl,
1608 .tiocmget = fwtty_tiocmget,
1609 .tiocmset = fwtty_tiocmset,
1610 .get_icount = fwtty_get_icount,
1611};
1612
Peter Hurley7355ba32012-11-02 08:16:33 -04001613static inline int mgmt_pkt_expected_len(__be16 code)
1614{
1615 static const struct fwserial_mgmt_pkt pkt;
1616
1617 switch (be16_to_cpu(code)) {
1618 case FWSC_VIRT_CABLE_PLUG:
1619 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1620
1621 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1622 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1623
1624
1625 case FWSC_VIRT_CABLE_UNPLUG:
1626 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1627 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1628 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1629 return sizeof(pkt.hdr);
1630
1631 default:
1632 return -1;
1633 }
1634}
1635
1636static inline void fill_plug_params(struct virt_plug_params *params,
1637 struct fwtty_port *port)
1638{
1639 u64 status_addr = port->rx_handler.offset;
1640 u64 fifo_addr = port->rx_handler.offset + 4;
1641 size_t fifo_len = port->rx_handler.length - 4;
1642
1643 params->status_hi = cpu_to_be32(status_addr >> 32);
1644 params->status_lo = cpu_to_be32(status_addr);
1645 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1646 params->fifo_lo = cpu_to_be32(fifo_addr);
1647 params->fifo_len = cpu_to_be32(fifo_len);
1648}
1649
1650static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1651 struct fwtty_port *port)
1652{
1653 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1654 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1655 fill_plug_params(&pkt->plug_req, port);
1656}
1657
1658static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1659 struct fwtty_port *port)
1660{
1661 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1662 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1663 fill_plug_params(&pkt->plug_rsp, port);
1664}
1665
1666static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1667{
1668 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1669 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1670}
1671
1672static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1673{
1674 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1675 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1676}
1677
1678static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1679{
1680 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1681 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1682}
1683
1684static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1685{
1686 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1687 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1688}
1689
1690static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1691 struct virt_plug_params *params)
1692{
1693 struct fwtty_port *port = peer->port;
1694
1695 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1696 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1697 peer->fifo_len = be32_to_cpu(params->fifo_len);
1698 peer_set_state(peer, FWPS_ATTACHED);
1699
1700 /* reconfigure tx_fifo optimally for this peer */
1701 spin_lock_bh(&port->lock);
Peter Hurley3b1f3152013-01-28 20:57:47 -05001702 port->max_payload = min(peer->max_payload, peer->fifo_len);
Peter Hurley7355ba32012-11-02 08:16:33 -04001703 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1704 spin_unlock_bh(&peer->port->lock);
1705
1706 if (port->port.console && port->fwcon_ops->notify != NULL)
1707 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1708
Joe Perches6e8661e2013-05-28 19:44:24 -07001709 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04001710 (unsigned long long)peer->guid, dev_name(port->device));
1711}
1712
1713static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1714 struct fwserial_mgmt_pkt *pkt)
1715{
1716 int generation;
1717 int rcode, tries = 5;
1718
1719 do {
1720 generation = peer->generation;
1721 smp_rmb();
1722
1723 rcode = fw_run_transaction(peer->serial->card,
1724 TCODE_WRITE_BLOCK_REQUEST,
1725 peer->node_id,
1726 generation, peer->speed,
1727 peer->mgmt_addr,
1728 pkt, be16_to_cpu(pkt->hdr.len));
1729 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1730 rcode == RCODE_GENERATION) {
Joe Perches6e8661e2013-05-28 19:44:24 -07001731 fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -04001732 continue;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001733 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -04001734 break;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02001735 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001736 } while (--tries > 0);
1737 return rcode;
1738}
1739
1740/**
1741 * fwserial_claim_port - attempt to claim port @ index for peer
1742 *
1743 * Returns ptr to claimed port or error code (as ERR_PTR())
1744 * Can sleep - must be called from process context
1745 */
1746static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1747 int index)
1748{
1749 struct fwtty_port *port;
1750
1751 if (index < 0 || index >= num_ports)
1752 return ERR_PTR(-EINVAL);
1753
1754 /* must guarantee that previous port releases have completed */
1755 synchronize_rcu();
1756
1757 port = peer->serial->ports[index];
1758 spin_lock_bh(&port->lock);
1759 if (!rcu_access_pointer(port->peer))
1760 rcu_assign_pointer(port->peer, peer);
1761 else
1762 port = ERR_PTR(-EBUSY);
1763 spin_unlock_bh(&port->lock);
1764
1765 return port;
1766}
1767
1768/**
1769 * fwserial_find_port - find avail port and claim for peer
1770 *
1771 * Returns ptr to claimed port or NULL if none avail
1772 * Can sleep - must be called from process context
1773 */
1774static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1775{
1776 struct fwtty_port **ports = peer->serial->ports;
1777 int i;
1778
1779 /* must guarantee that previous port releases have completed */
1780 synchronize_rcu();
1781
1782 /* TODO: implement optional GUID-to-specific port # matching */
1783
1784 /* find an unattached port (but not the loopback port, if present) */
1785 for (i = 0; i < num_ttys; ++i) {
1786 spin_lock_bh(&ports[i]->lock);
1787 if (!ports[i]->peer) {
1788 /* claim port */
1789 rcu_assign_pointer(ports[i]->peer, peer);
1790 spin_unlock_bh(&ports[i]->lock);
1791 return ports[i];
1792 }
1793 spin_unlock_bh(&ports[i]->lock);
1794 }
1795 return NULL;
1796}
1797
Peter Hurleyde321a12013-01-28 22:34:35 -05001798static void fwserial_release_port(struct fwtty_port *port, bool reset)
Peter Hurley7355ba32012-11-02 08:16:33 -04001799{
1800 /* drop carrier (and all other line status) */
Peter Hurleyde321a12013-01-28 22:34:35 -05001801 if (reset)
1802 fwtty_update_port_status(port, 0);
Peter Hurley7355ba32012-11-02 08:16:33 -04001803
1804 spin_lock_bh(&port->lock);
1805
1806 /* reset dma fifo max transmission size back to S100 */
1807 port->max_payload = link_speed_to_max_payload(SCODE_100);
1808 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1809
Monam Agarwale8429b02014-03-24 00:08:22 +05301810 RCU_INIT_POINTER(port->peer, NULL);
Peter Hurley7355ba32012-11-02 08:16:33 -04001811 spin_unlock_bh(&port->lock);
1812
1813 if (port->port.console && port->fwcon_ops->notify != NULL)
1814 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1815}
1816
1817static void fwserial_plug_timeout(unsigned long data)
1818{
Dominique van den Broeckd9492102014-04-12 15:18:14 +02001819 struct fwtty_peer *peer = (struct fwtty_peer *)data;
Peter Hurley7355ba32012-11-02 08:16:33 -04001820 struct fwtty_port *port;
1821
1822 spin_lock_bh(&peer->lock);
1823 if (peer->state != FWPS_PLUG_PENDING) {
1824 spin_unlock_bh(&peer->lock);
1825 return;
1826 }
1827
1828 port = peer_revert_state(peer);
1829 spin_unlock_bh(&peer->lock);
1830
1831 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05001832 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001833}
1834
1835/**
1836 * fwserial_connect_peer - initiate virtual cable with peer
1837 *
1838 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1839 * otherwise error code. Must be called from process context.
1840 */
1841static int fwserial_connect_peer(struct fwtty_peer *peer)
1842{
1843 struct fwtty_port *port;
1844 struct fwserial_mgmt_pkt *pkt;
1845 int err, rcode;
1846
1847 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1848 if (!pkt)
1849 return -ENOMEM;
1850
1851 port = fwserial_find_port(peer);
1852 if (!port) {
Joe Perches6e8661e2013-05-28 19:44:24 -07001853 fwtty_err(&peer->unit, "avail ports in use\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04001854 err = -EBUSY;
1855 goto free_pkt;
1856 }
1857
1858 spin_lock_bh(&peer->lock);
1859
1860 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1861 if (peer->state != FWPS_NOT_ATTACHED) {
1862 err = -EBUSY;
1863 goto release_port;
1864 }
1865
1866 peer->port = port;
1867 peer_set_state(peer, FWPS_PLUG_PENDING);
1868
1869 fill_plug_req(pkt, peer->port);
1870
1871 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1872 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1873 spin_unlock_bh(&peer->lock);
1874
1875 rcode = fwserial_send_mgmt_sync(peer, pkt);
1876
1877 spin_lock_bh(&peer->lock);
1878 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1879 if (rcode == RCODE_CONFLICT_ERROR)
1880 err = -EAGAIN;
1881 else
1882 err = -EIO;
1883 goto cancel_timer;
1884 }
1885 spin_unlock_bh(&peer->lock);
1886
1887 kfree(pkt);
1888 return 0;
1889
1890cancel_timer:
1891 del_timer(&peer->timer);
1892 peer_revert_state(peer);
1893release_port:
1894 spin_unlock_bh(&peer->lock);
Peter Hurleyde321a12013-01-28 22:34:35 -05001895 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001896free_pkt:
1897 kfree(pkt);
1898 return err;
1899}
1900
1901/**
1902 * fwserial_close_port -
1903 * HUP the tty (if the tty exists) and unregister the tty device.
1904 * Only used by the unit driver upon unit removal to disconnect and
1905 * cleanup all attached ports
1906 *
1907 * The port reference is put by fwtty_cleanup (if a reference was
1908 * ever taken).
1909 */
Peter Hurleyfa1da242013-01-28 22:34:38 -05001910static void fwserial_close_port(struct tty_driver *driver,
1911 struct fwtty_port *port)
Peter Hurley7355ba32012-11-02 08:16:33 -04001912{
1913 struct tty_struct *tty;
1914
1915 mutex_lock(&port->port.mutex);
1916 tty = tty_port_tty_get(&port->port);
1917 if (tty) {
1918 tty_vhangup(tty);
1919 tty_kref_put(tty);
1920 }
1921 mutex_unlock(&port->port.mutex);
1922
Peter Hurleyfa1da242013-01-28 22:34:38 -05001923 if (driver == fwloop_driver)
1924 tty_unregister_device(driver, loop_idx(port));
1925 else
1926 tty_unregister_device(driver, port->index);
Peter Hurley7355ba32012-11-02 08:16:33 -04001927}
1928
1929/**
1930 * fwserial_lookup - finds first fw_serial associated with card
1931 * @card: fw_card to match
1932 *
1933 * NB: caller must be holding fwserial_list_mutex
1934 */
1935static struct fw_serial *fwserial_lookup(struct fw_card *card)
1936{
1937 struct fw_serial *serial;
1938
1939 list_for_each_entry(serial, &fwserial_list, list) {
1940 if (card == serial->card)
1941 return serial;
1942 }
1943
1944 return NULL;
1945}
1946
1947/**
1948 * __fwserial_lookup_rcu - finds first fw_serial associated with card
1949 * @card: fw_card to match
1950 *
1951 * NB: caller must be inside rcu_read_lock() section
1952 */
1953static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
1954{
1955 struct fw_serial *serial;
1956
1957 list_for_each_entry_rcu(serial, &fwserial_list, list) {
1958 if (card == serial->card)
1959 return serial;
1960 }
1961
1962 return NULL;
1963}
1964
1965/**
1966 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
1967 *
1968 * If a matching peer could not be found for the specified generation/node id,
1969 * this could be because:
1970 * a) the generation has changed and one of the nodes hasn't updated yet
1971 * b) the remote node has created its remote unit device before this
1972 * local node has created its corresponding remote unit device
1973 * In either case, the remote node should retry
1974 *
1975 * Note: caller must be in rcu_read_lock() section
1976 */
1977static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
1978 int generation, int id)
1979{
1980 struct fw_serial *serial;
1981 struct fwtty_peer *peer;
1982
1983 serial = __fwserial_lookup_rcu(card);
1984 if (!serial) {
1985 /*
1986 * Something is very wrong - there should be a matching
1987 * fw_serial structure for every fw_card. Maybe the remote node
1988 * has created its remote unit device before this driver has
1989 * been probed for any unit devices...
1990 */
Joe Perches6e8661e2013-05-28 19:44:24 -07001991 fwtty_err(card, "unknown card (guid %016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04001992 (unsigned long long) card->guid);
1993 return NULL;
1994 }
1995
1996 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
1997 int g = peer->generation;
1998 smp_rmb();
1999 if (generation == g && id == peer->node_id)
2000 return peer;
2001 }
2002
2003 return NULL;
2004}
2005
2006#ifdef DEBUG
2007static void __dump_peer_list(struct fw_card *card)
2008{
2009 struct fw_serial *serial;
2010 struct fwtty_peer *peer;
2011
2012 serial = __fwserial_lookup_rcu(card);
2013 if (!serial)
2014 return;
2015
2016 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2017 int g = peer->generation;
2018 smp_rmb();
Joe Perches6e8661e2013-05-28 19:44:24 -07002019 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n",
2020 g, peer->node_id, (unsigned long long) peer->guid);
Peter Hurley7355ba32012-11-02 08:16:33 -04002021 }
2022}
2023#else
2024#define __dump_peer_list(s)
2025#endif
2026
2027static void fwserial_auto_connect(struct work_struct *work)
2028{
2029 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2030 int err;
2031
2032 err = fwserial_connect_peer(peer);
2033 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2034 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2035}
2036
Tejun Heo6c256cb2014-03-07 10:24:50 -05002037static void fwserial_peer_workfn(struct work_struct *work)
2038{
2039 struct fwtty_peer *peer = to_peer(work, work);
2040
2041 peer->workfn(work);
2042}
2043
Peter Hurley7355ba32012-11-02 08:16:33 -04002044/**
2045 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2046 * @serial: aggregate representing the specific fw_card to add the peer to
2047 * @unit: 'peer' to create and add to peer_list of serial
2048 *
2049 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2050 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2051 * available tty port. This function is called either directly or indirectly
2052 * as a result of a 'serial' unit device being created & probed.
2053 *
2054 * Note: this function is serialized with fwserial_remove_peer() by the
2055 * fwserial_list_mutex held in fwserial_probe().
2056 *
2057 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2058 * via the dev_set_drvdata() for the device of the fw_unit.
2059 */
2060static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2061{
2062 struct device *dev = &unit->device;
2063 struct fw_device *parent = fw_parent_device(unit);
2064 struct fwtty_peer *peer;
2065 struct fw_csr_iterator ci;
2066 int key, val;
2067 int generation;
2068
2069 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2070 if (!peer)
2071 return -ENOMEM;
2072
2073 peer_set_state(peer, FWPS_NOT_ATTACHED);
2074
2075 dev_set_drvdata(dev, peer);
2076 peer->unit = unit;
2077 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2078 peer->speed = parent->max_speed;
2079 peer->max_payload = min(device_max_receive(parent),
2080 link_speed_to_max_payload(peer->speed));
2081
2082 generation = parent->generation;
2083 smp_rmb();
2084 peer->node_id = parent->node_id;
2085 smp_wmb();
2086 peer->generation = generation;
2087
2088 /* retrieve the mgmt bus addr from the unit directory */
2089 fw_csr_iterator_init(&ci, unit->directory);
2090 while (fw_csr_iterator_next(&ci, &key, &val)) {
2091 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2092 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2093 break;
2094 }
2095 }
2096 if (peer->mgmt_addr == 0ULL) {
2097 /*
2098 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2099 * this peer will not be able to attach to a remote
2100 */
2101 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2102 }
2103
2104 spin_lock_init(&peer->lock);
2105 peer->port = NULL;
2106
2107 init_timer(&peer->timer);
Tejun Heo6c256cb2014-03-07 10:24:50 -05002108 INIT_WORK(&peer->work, fwserial_peer_workfn);
Peter Hurley7355ba32012-11-02 08:16:33 -04002109 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2110
2111 /* associate peer with specific fw_card */
2112 peer->serial = serial;
2113 list_add_rcu(&peer->list, &serial->peer_list);
2114
Joe Perches6e8661e2013-05-28 19:44:24 -07002115 fwtty_info(&peer->unit, "peer added (guid:%016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002116 (unsigned long long)peer->guid);
2117
2118 /* identify the local unit & virt cable to loopback port */
2119 if (parent->is_local) {
2120 serial->self = peer;
2121 if (create_loop_dev) {
2122 struct fwtty_port *port;
2123 port = fwserial_claim_port(peer, num_ttys);
2124 if (!IS_ERR(port)) {
2125 struct virt_plug_params params;
2126
2127 spin_lock_bh(&peer->lock);
2128 peer->port = port;
2129 fill_plug_params(&params, port);
2130 fwserial_virt_plug_complete(peer, &params);
2131 spin_unlock_bh(&peer->lock);
2132
2133 fwtty_write_port_status(port);
2134 }
2135 }
2136
2137 } else if (auto_connect) {
2138 /* auto-attach to remote units only (if policy allows) */
2139 schedule_delayed_work(&peer->connect, 1);
2140 }
2141
2142 return 0;
2143}
2144
2145/**
2146 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2147 *
2148 * Remove a 'peer' from its list of peers. This function is only
2149 * called by fwserial_remove() on bus removal of the unit device.
2150 *
2151 * Note: this function is serialized with fwserial_add_peer() by the
2152 * fwserial_list_mutex held in fwserial_remove().
2153 */
2154static void fwserial_remove_peer(struct fwtty_peer *peer)
2155{
2156 struct fwtty_port *port;
2157
2158 spin_lock_bh(&peer->lock);
2159 peer_set_state(peer, FWPS_GONE);
2160 spin_unlock_bh(&peer->lock);
2161
2162 cancel_delayed_work_sync(&peer->connect);
2163 cancel_work_sync(&peer->work);
2164
2165 spin_lock_bh(&peer->lock);
2166 /* if this unit is the local unit, clear link */
2167 if (peer == peer->serial->self)
2168 peer->serial->self = NULL;
2169
2170 /* cancel the request timeout timer (if running) */
2171 del_timer(&peer->timer);
2172
2173 port = peer->port;
2174 peer->port = NULL;
2175
2176 list_del_rcu(&peer->list);
2177
Joe Perches6e8661e2013-05-28 19:44:24 -07002178 fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002179 (unsigned long long)peer->guid);
2180
2181 spin_unlock_bh(&peer->lock);
2182
2183 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002184 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002185
2186 synchronize_rcu();
2187 kfree(peer);
2188}
2189
2190/**
Peter Hurley7355ba32012-11-02 08:16:33 -04002191 * fwserial_create - init everything to create TTYs for a specific fw_card
2192 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2193 *
2194 * This function inits the aggregate structure (an fw_serial instance)
2195 * used to manage the TTY ports registered by a specific fw_card. Also, the
2196 * unit device is added as the first 'peer'.
2197 *
2198 * This unit device may represent a local unit device (as specified by the
2199 * config ROM unit directory) or it may represent a remote unit device
2200 * (as specified by the reading of the remote node's config ROM).
2201 *
2202 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2203 * value to indicate which error.
2204 */
2205static int fwserial_create(struct fw_unit *unit)
2206{
2207 struct fw_device *parent = fw_parent_device(unit);
2208 struct fw_card *card = parent->card;
2209 struct fw_serial *serial;
2210 struct fwtty_port *port;
2211 struct device *tty_dev;
2212 int i, j;
2213 int err;
2214
2215 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2216 if (!serial)
2217 return -ENOMEM;
2218
2219 kref_init(&serial->kref);
2220 serial->card = card;
2221 INIT_LIST_HEAD(&serial->peer_list);
2222
2223 for (i = 0; i < num_ports; ++i) {
2224 port = kzalloc(sizeof(*port), GFP_KERNEL);
2225 if (!port) {
2226 err = -ENOMEM;
2227 goto free_ports;
2228 }
2229 tty_port_init(&port->port);
2230 port->index = FWTTY_INVALID_INDEX;
2231 port->port.ops = &fwtty_port_ops;
2232 port->serial = serial;
Peter Hurley2ead3912013-11-22 13:06:09 -05002233 tty_buffer_set_limit(&port->port, 128 * 1024);
Peter Hurley7355ba32012-11-02 08:16:33 -04002234
2235 spin_lock_init(&port->lock);
2236 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2237 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2238 INIT_WORK(&port->hangup, fwtty_do_hangup);
Peter Hurley7355ba32012-11-02 08:16:33 -04002239 init_waitqueue_head(&port->wait_tx);
2240 port->max_payload = link_speed_to_max_payload(SCODE_100);
2241 dma_fifo_init(&port->tx_fifo);
2242
Monam Agarwale8429b02014-03-24 00:08:22 +05302243 RCU_INIT_POINTER(port->peer, NULL);
Peter Hurley7355ba32012-11-02 08:16:33 -04002244 serial->ports[i] = port;
2245
2246 /* get unique bus addr region for port's status & recv fifo */
2247 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2248 port->rx_handler.address_callback = fwtty_port_handler;
2249 port->rx_handler.callback_data = port;
2250 /*
2251 * XXX: use custom memory region above cpu physical memory addrs
2252 * this will ease porting to 64-bit firewire adapters
2253 */
2254 err = fw_core_add_address_handler(&port->rx_handler,
2255 &fw_high_memory_region);
2256 if (err) {
2257 kfree(port);
2258 goto free_ports;
2259 }
2260 }
2261 /* preserve i for error cleanup */
2262
2263 err = fwtty_ports_add(serial);
2264 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002265 fwtty_err(&unit, "no space in port table\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002266 goto free_ports;
2267 }
2268
2269 for (j = 0; j < num_ttys; ++j) {
2270 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2271 fwtty_driver,
2272 serial->ports[j]->index,
2273 card->device);
2274 if (IS_ERR(tty_dev)) {
2275 err = PTR_ERR(tty_dev);
Joe Perches6e8661e2013-05-28 19:44:24 -07002276 fwtty_err(&unit, "register tty device error (%d)\n",
2277 err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002278 goto unregister_ttys;
2279 }
2280
2281 serial->ports[j]->device = tty_dev;
2282 }
2283 /* preserve j for error cleanup */
2284
2285 if (create_loop_dev) {
2286 struct device *loop_dev;
2287
Peter Hurleyfa1da242013-01-28 22:34:38 -05002288 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2289 fwloop_driver,
2290 loop_idx(serial->ports[j]),
2291 card->device);
Peter Hurley7355ba32012-11-02 08:16:33 -04002292 if (IS_ERR(loop_dev)) {
2293 err = PTR_ERR(loop_dev);
Joe Perches6e8661e2013-05-28 19:44:24 -07002294 fwtty_err(&unit, "create loop device failed (%d)\n",
2295 err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002296 goto unregister_ttys;
2297 }
Peter Hurleyfa1da242013-01-28 22:34:38 -05002298 serial->ports[j]->device = loop_dev;
2299 serial->ports[j]->loopback = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002300 }
2301
Peter Hurley4df5bb02013-01-28 22:34:40 -05002302 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2303 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2304 fwserial_debugfs);
2305 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2306 debugfs_create_file("peers", 0444, serial->debugfs,
2307 serial, &fwtty_peers_fops);
2308 debugfs_create_file("stats", 0444, serial->debugfs,
2309 serial, &fwtty_stats_fops);
2310 }
2311 }
2312
Peter Hurley7355ba32012-11-02 08:16:33 -04002313 list_add_rcu(&serial->list, &fwserial_list);
2314
Joe Perches6e8661e2013-05-28 19:44:24 -07002315 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002316 dev_name(card->device), (unsigned long long) card->guid);
2317
2318 err = fwserial_add_peer(serial, unit);
2319 if (!err)
2320 return 0;
2321
Joe Perches6e8661e2013-05-28 19:44:24 -07002322 fwtty_err(&unit, "unable to add peer unit device (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002323
2324 /* fall-through to error processing */
Peter Hurley4df5bb02013-01-28 22:34:40 -05002325 debugfs_remove_recursive(serial->debugfs);
2326
Peter Hurley7355ba32012-11-02 08:16:33 -04002327 list_del_rcu(&serial->list);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002328 if (create_loop_dev)
Jon Bernard9502e2b2013-09-10 18:00:01 -04002329 tty_unregister_device(fwloop_driver,
2330 loop_idx(serial->ports[j]));
Peter Hurley7355ba32012-11-02 08:16:33 -04002331unregister_ttys:
2332 for (--j; j >= 0; --j)
2333 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2334 kref_put(&serial->kref, fwserial_destroy);
2335 return err;
2336
2337free_ports:
Peter Hurleya3218462012-11-27 21:37:11 -05002338 for (--i; i >= 0; --i) {
2339 tty_port_destroy(&serial->ports[i]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -04002340 kfree(serial->ports[i]);
Peter Hurleya3218462012-11-27 21:37:11 -05002341 }
Peter Hurley7355ba32012-11-02 08:16:33 -04002342 kfree(serial);
2343 return err;
2344}
2345
2346/**
2347 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2348 *
2349 * A 'serial' unit device is created and probed as a result of:
2350 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2351 * 'serial' unit specifier id
2352 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2353 *
2354 * The firewire core registers unit devices by enumerating unit directories
2355 * of a node's config ROM after reading the config ROM when a new node is
2356 * added to the bus topology after a bus reset.
2357 *
2358 * The practical implications of this are:
2359 * - this probe is called for both local and remote nodes that have a 'serial'
2360 * unit directory in their config ROM (that matches the specifiers in
2361 * fwserial_id_table).
2362 * - no specific order is enforced for local vs. remote unit devices
2363 *
2364 * This unit driver copes with the lack of specific order in the same way the
2365 * firewire net driver does -- each probe, for either a local or remote unit
2366 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2367 * first peer created for a given fw_card (tracked by the global fwserial_list)
2368 * creates the underlying TTYs (aggregated in a fw_serial instance).
2369 *
2370 * NB: an early attempt to differentiate local & remote unit devices by creating
2371 * peers only for remote units and fw_serial instances (with their
2372 * associated TTY devices) only for local units was discarded. Managing
2373 * the peer lifetimes on device removal proved too complicated.
2374 *
2375 * fwserial_probe/fwserial_remove are effectively serialized by the
2376 * fwserial_list_mutex. This is necessary because the addition of the first peer
2377 * for a given fw_card will trigger the creation of the fw_serial for that
2378 * fw_card, which must not simultaneously contend with the removal of the
2379 * last peer for a given fw_card triggering the destruction of the same
2380 * fw_serial for the same fw_card.
2381 */
Stefan Richter94a87152013-06-09 18:15:00 +02002382static int fwserial_probe(struct fw_unit *unit,
2383 const struct ieee1394_device_id *id)
Peter Hurley7355ba32012-11-02 08:16:33 -04002384{
Peter Hurley7355ba32012-11-02 08:16:33 -04002385 struct fw_serial *serial;
2386 int err;
2387
2388 mutex_lock(&fwserial_list_mutex);
2389 serial = fwserial_lookup(fw_parent_device(unit)->card);
2390 if (!serial)
2391 err = fwserial_create(unit);
2392 else
2393 err = fwserial_add_peer(serial, unit);
2394 mutex_unlock(&fwserial_list_mutex);
2395 return err;
2396}
2397
2398/**
2399 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2400 *
2401 * The corresponding 'peer' for this unit device is removed from the list of
2402 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2403 * specific fw_card). If this is the last peer being removed, then trigger
2404 * the destruction of the underlying TTYs.
2405 */
Stefan Richter94a87152013-06-09 18:15:00 +02002406static void fwserial_remove(struct fw_unit *unit)
Peter Hurley7355ba32012-11-02 08:16:33 -04002407{
Stefan Richter94a87152013-06-09 18:15:00 +02002408 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
Peter Hurley7355ba32012-11-02 08:16:33 -04002409 struct fw_serial *serial = peer->serial;
2410 int i;
2411
2412 mutex_lock(&fwserial_list_mutex);
2413 fwserial_remove_peer(peer);
2414
2415 if (list_empty(&serial->peer_list)) {
2416 /* unlink from the fwserial_list here */
2417 list_del_rcu(&serial->list);
2418
Peter Hurley4df5bb02013-01-28 22:34:40 -05002419 debugfs_remove_recursive(serial->debugfs);
2420
Peter Hurleyfa1da242013-01-28 22:34:38 -05002421 for (i = 0; i < num_ttys; ++i)
2422 fwserial_close_port(fwtty_driver, serial->ports[i]);
2423 if (create_loop_dev)
2424 fwserial_close_port(fwloop_driver, serial->ports[i]);
Peter Hurley7355ba32012-11-02 08:16:33 -04002425 kref_put(&serial->kref, fwserial_destroy);
2426 }
2427 mutex_unlock(&fwserial_list_mutex);
Peter Hurley7355ba32012-11-02 08:16:33 -04002428}
2429
2430/**
2431 * fwserial_update: bus update function for 'firewire' serial unit devices
2432 *
2433 * Updates the new node_id and bus generation for this peer. Note that locking
2434 * is unnecessary; but careful memory barrier usage is important to enforce the
2435 * load and store order of generation & node_id.
2436 *
2437 * The fw-core orders the write of node_id before generation in the parent
2438 * fw_device to ensure that a stale node_id cannot be used with a current
2439 * bus generation. So the generation value must be read before the node_id.
2440 *
2441 * In turn, this orders the write of node_id before generation in the peer to
2442 * also ensure a stale node_id cannot be used with a current bus generation.
2443 */
2444static void fwserial_update(struct fw_unit *unit)
2445{
2446 struct fw_device *parent = fw_parent_device(unit);
2447 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2448 int generation;
2449
2450 generation = parent->generation;
2451 smp_rmb();
2452 peer->node_id = parent->node_id;
2453 smp_wmb();
2454 peer->generation = generation;
2455}
2456
2457static const struct ieee1394_device_id fwserial_id_table[] = {
2458 {
2459 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2460 IEEE1394_MATCH_VERSION,
2461 .specifier_id = LINUX_VENDOR_ID,
2462 .version = FWSERIAL_VERSION,
2463 },
2464 { }
2465};
2466
2467static struct fw_driver fwserial_driver = {
2468 .driver = {
2469 .owner = THIS_MODULE,
2470 .name = KBUILD_MODNAME,
2471 .bus = &fw_bus_type,
Peter Hurley7355ba32012-11-02 08:16:33 -04002472 },
Stefan Richter94a87152013-06-09 18:15:00 +02002473 .probe = fwserial_probe,
Peter Hurley7355ba32012-11-02 08:16:33 -04002474 .update = fwserial_update,
Stefan Richter94a87152013-06-09 18:15:00 +02002475 .remove = fwserial_remove,
Peter Hurley7355ba32012-11-02 08:16:33 -04002476 .id_table = fwserial_id_table,
2477};
2478
2479#define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2480#define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2481#define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2482 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2483/* XXX: config ROM definitons could be improved with semi-automated offset
2484 * and length calculation
2485 */
Peter Hurley612588a2013-01-29 09:10:30 -05002486#define FW_ROM_LEN(quads) ((quads) << 16)
Peter Hurley7355ba32012-11-02 08:16:33 -04002487#define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2488
2489struct fwserial_unit_directory_data {
Peter Hurley612588a2013-01-29 09:10:30 -05002490 u32 len_crc;
Peter Hurley7355ba32012-11-02 08:16:33 -04002491 u32 unit_specifier;
2492 u32 unit_sw_version;
2493 u32 unit_addr_offset;
2494 u32 desc1_ofs;
Peter Hurley612588a2013-01-29 09:10:30 -05002495 u32 desc1_len_crc;
Peter Hurley7355ba32012-11-02 08:16:33 -04002496 u32 desc1_data[5];
2497} __packed;
2498
2499static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
Peter Hurley612588a2013-01-29 09:10:30 -05002500 .len_crc = FW_ROM_LEN(4),
Peter Hurley7355ba32012-11-02 08:16:33 -04002501 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2502 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2503 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
Peter Hurley612588a2013-01-29 09:10:30 -05002504 .desc1_len_crc = FW_ROM_LEN(5),
Peter Hurley7355ba32012-11-02 08:16:33 -04002505 .desc1_data = {
2506 0x00000000, /* type = text */
2507 0x00000000, /* enc = ASCII, lang EN */
2508 0x4c696e75, /* 'Linux TTY' */
2509 0x78205454,
2510 0x59000000,
2511 },
2512};
2513
2514static struct fw_descriptor fwserial_unit_directory = {
2515 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2516 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2517 .data = (u32 *)&fwserial_unit_directory_data,
2518};
2519
2520/*
2521 * The management address is in the unit space region but above other known
2522 * address users (to keep wild writes from causing havoc)
2523 */
Peter Hurleya3d9ad42013-01-28 22:34:37 -05002524static const struct fw_address_region fwserial_mgmt_addr_region = {
Peter Hurley7355ba32012-11-02 08:16:33 -04002525 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2526 .end = 0x1000000000000ULL,
2527};
2528
2529static struct fw_address_handler fwserial_mgmt_addr_handler;
2530
2531/**
2532 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2533 * @work: ptr to peer->work
2534 *
2535 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2536 *
2537 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2538 * already sent to this peer. If so, the collision is resolved by comparing
2539 * guid values; the loser sends the plug response.
2540 *
2541 * Note: if an error prevents a response, don't do anything -- the
2542 * remote will timeout its request.
2543 */
2544static void fwserial_handle_plug_req(struct work_struct *work)
2545{
2546 struct fwtty_peer *peer = to_peer(work, work);
2547 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2548 struct fwtty_port *port;
2549 struct fwserial_mgmt_pkt *pkt;
2550 int rcode;
2551
2552 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2553 if (!pkt)
2554 return;
2555
2556 port = fwserial_find_port(peer);
2557
2558 spin_lock_bh(&peer->lock);
2559
2560 switch (peer->state) {
2561 case FWPS_NOT_ATTACHED:
2562 if (!port) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002563 fwtty_err(&peer->unit, "no more ports avail\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002564 fill_plug_rsp_nack(pkt);
2565 } else {
2566 peer->port = port;
2567 fill_plug_rsp_ok(pkt, peer->port);
2568 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2569 /* don't release claimed port */
2570 port = NULL;
2571 }
2572 break;
2573
2574 case FWPS_PLUG_PENDING:
2575 if (peer->serial->card->guid > peer->guid)
2576 goto cleanup;
2577
2578 /* We lost - hijack the already-claimed port and send ok */
2579 del_timer(&peer->timer);
2580 fill_plug_rsp_ok(pkt, peer->port);
2581 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2582 break;
2583
2584 default:
2585 fill_plug_rsp_nack(pkt);
2586 }
2587
2588 spin_unlock_bh(&peer->lock);
2589 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002590 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002591
2592 rcode = fwserial_send_mgmt_sync(peer, pkt);
2593
2594 spin_lock_bh(&peer->lock);
2595 if (peer->state == FWPS_PLUG_RESPONDING) {
2596 if (rcode == RCODE_COMPLETE) {
2597 struct fwtty_port *tmp = peer->port;
2598
2599 fwserial_virt_plug_complete(peer, plug_req);
2600 spin_unlock_bh(&peer->lock);
2601
2602 fwtty_write_port_status(tmp);
2603 spin_lock_bh(&peer->lock);
2604 } else {
Joe Perches6e8661e2013-05-28 19:44:24 -07002605 fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode);
Peter Hurley7355ba32012-11-02 08:16:33 -04002606 port = peer_revert_state(peer);
2607 }
2608 }
2609cleanup:
2610 spin_unlock_bh(&peer->lock);
2611 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002612 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002613 kfree(pkt);
2614 return;
2615}
2616
2617static void fwserial_handle_unplug_req(struct work_struct *work)
2618{
2619 struct fwtty_peer *peer = to_peer(work, work);
2620 struct fwtty_port *port = NULL;
2621 struct fwserial_mgmt_pkt *pkt;
2622 int rcode;
2623
2624 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2625 if (!pkt)
2626 return;
2627
2628 spin_lock_bh(&peer->lock);
2629
2630 switch (peer->state) {
2631 case FWPS_ATTACHED:
2632 fill_unplug_rsp_ok(pkt);
2633 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2634 break;
2635
2636 case FWPS_UNPLUG_PENDING:
2637 if (peer->serial->card->guid > peer->guid)
2638 goto cleanup;
2639
2640 /* We lost - send unplug rsp */
2641 del_timer(&peer->timer);
2642 fill_unplug_rsp_ok(pkt);
2643 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2644 break;
2645
2646 default:
2647 fill_unplug_rsp_nack(pkt);
2648 }
2649
2650 spin_unlock_bh(&peer->lock);
2651
2652 rcode = fwserial_send_mgmt_sync(peer, pkt);
2653
2654 spin_lock_bh(&peer->lock);
2655 if (peer->state == FWPS_UNPLUG_RESPONDING) {
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002656 if (rcode != RCODE_COMPLETE)
Joe Perches6e8661e2013-05-28 19:44:24 -07002657 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n",
2658 rcode);
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002659 port = peer_revert_state(peer);
Peter Hurley7355ba32012-11-02 08:16:33 -04002660 }
2661cleanup:
2662 spin_unlock_bh(&peer->lock);
2663 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002664 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002665 kfree(pkt);
2666 return;
2667}
2668
2669static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2670 struct fwserial_mgmt_pkt *pkt,
2671 unsigned long long addr,
2672 size_t len)
2673{
2674 struct fwtty_port *port = NULL;
Peter Hurleyde321a12013-01-28 22:34:35 -05002675 bool reset = false;
Peter Hurley7355ba32012-11-02 08:16:33 -04002676 int rcode;
2677
2678 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2679 return RCODE_ADDRESS_ERROR;
2680
2681 if (len != be16_to_cpu(pkt->hdr.len) ||
2682 len != mgmt_pkt_expected_len(pkt->hdr.code))
2683 return RCODE_DATA_ERROR;
2684
2685 spin_lock_bh(&peer->lock);
2686 if (peer->state == FWPS_GONE) {
2687 /*
2688 * This should never happen - it would mean that the
2689 * remote unit that just wrote this transaction was
2690 * already removed from the bus -- and the removal was
2691 * processed before we rec'd this transaction
2692 */
Joe Perches6e8661e2013-05-28 19:44:24 -07002693 fwtty_err(&peer->unit, "peer already removed\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002694 spin_unlock_bh(&peer->lock);
2695 return RCODE_ADDRESS_ERROR;
2696 }
2697
2698 rcode = RCODE_COMPLETE;
2699
Joe Perches6e8661e2013-05-28 19:44:24 -07002700 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx\n", pkt->hdr.code);
Peter Hurley7355ba32012-11-02 08:16:33 -04002701
2702 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2703 case FWSC_VIRT_CABLE_PLUG:
2704 if (work_pending(&peer->work)) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002705 fwtty_err(&peer->unit, "plug req: busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002706 rcode = RCODE_CONFLICT_ERROR;
2707
2708 } else {
2709 peer->work_params.plug_req = pkt->plug_req;
Tejun Heo6c256cb2014-03-07 10:24:50 -05002710 peer->workfn = fwserial_handle_plug_req;
Peter Hurley7355ba32012-11-02 08:16:33 -04002711 queue_work(system_unbound_wq, &peer->work);
2712 }
2713 break;
2714
2715 case FWSC_VIRT_CABLE_PLUG_RSP:
2716 if (peer->state != FWPS_PLUG_PENDING) {
2717 rcode = RCODE_CONFLICT_ERROR;
2718
2719 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002720 fwtty_notice(&peer->unit, "NACK plug rsp\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002721 port = peer_revert_state(peer);
2722
2723 } else {
2724 struct fwtty_port *tmp = peer->port;
2725
2726 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2727 spin_unlock_bh(&peer->lock);
2728
2729 fwtty_write_port_status(tmp);
2730 spin_lock_bh(&peer->lock);
2731 }
2732 break;
2733
2734 case FWSC_VIRT_CABLE_UNPLUG:
2735 if (work_pending(&peer->work)) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002736 fwtty_err(&peer->unit, "unplug req: busy\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002737 rcode = RCODE_CONFLICT_ERROR;
2738 } else {
Tejun Heo6c256cb2014-03-07 10:24:50 -05002739 peer->workfn = fwserial_handle_unplug_req;
Peter Hurley7355ba32012-11-02 08:16:33 -04002740 queue_work(system_unbound_wq, &peer->work);
2741 }
2742 break;
2743
2744 case FWSC_VIRT_CABLE_UNPLUG_RSP:
Dominique van den Broeckea595e72014-04-12 15:18:13 +02002745 if (peer->state != FWPS_UNPLUG_PENDING) {
Peter Hurley7355ba32012-11-02 08:16:33 -04002746 rcode = RCODE_CONFLICT_ERROR;
Dominique van den Broeckea595e72014-04-12 15:18:13 +02002747 } else {
Peter Hurley7355ba32012-11-02 08:16:33 -04002748 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
Joe Perches6e8661e2013-05-28 19:44:24 -07002749 fwtty_notice(&peer->unit, "NACK unplug?\n");
Peter Hurley7355ba32012-11-02 08:16:33 -04002750 port = peer_revert_state(peer);
Peter Hurleyde321a12013-01-28 22:34:35 -05002751 reset = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002752 }
2753 break;
2754
2755 default:
Joe Perches6e8661e2013-05-28 19:44:24 -07002756 fwtty_err(&peer->unit, "unknown mgmt code %d\n",
Peter Hurley7355ba32012-11-02 08:16:33 -04002757 be16_to_cpu(pkt->hdr.code));
2758 rcode = RCODE_DATA_ERROR;
2759 }
2760 spin_unlock_bh(&peer->lock);
2761
2762 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002763 fwserial_release_port(port, reset);
Peter Hurley7355ba32012-11-02 08:16:33 -04002764
2765 return rcode;
2766}
2767
2768/**
2769 * fwserial_mgmt_handler: bus address handler for mgmt requests
2770 * @parameters: fw_address_callback_t as specified by firewire core interface
2771 *
2772 * This handler is responsible for handling virtual cable requests from remotes
2773 * for all cards.
2774 */
2775static void fwserial_mgmt_handler(struct fw_card *card,
2776 struct fw_request *request,
2777 int tcode, int destination, int source,
2778 int generation,
2779 unsigned long long addr,
2780 void *data, size_t len,
2781 void *callback_data)
2782{
2783 struct fwserial_mgmt_pkt *pkt = data;
2784 struct fwtty_peer *peer;
2785 int rcode;
2786
2787 rcu_read_lock();
2788 peer = __fwserial_peer_by_node_id(card, generation, source);
2789 if (!peer) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002790 fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source);
Peter Hurley7355ba32012-11-02 08:16:33 -04002791 __dump_peer_list(card);
2792 rcode = RCODE_CONFLICT_ERROR;
2793
2794 } else {
2795 switch (tcode) {
2796 case TCODE_WRITE_BLOCK_REQUEST:
2797 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2798 break;
2799
2800 default:
2801 rcode = RCODE_TYPE_ERROR;
2802 }
2803 }
2804
2805 rcu_read_unlock();
2806 fw_send_response(card, request, rcode);
2807}
2808
2809static int __init fwserial_init(void)
2810{
2811 int err, num_loops = !!(create_loop_dev);
2812
Peter Hurley4df5bb02013-01-28 22:34:40 -05002813 /* XXX: placeholder for a "firewire" debugfs node */
2814 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2815
Peter Hurley7355ba32012-11-02 08:16:33 -04002816 /* num_ttys/num_ports must not be set above the static alloc avail */
2817 if (num_ttys + num_loops > MAX_CARD_PORTS)
2818 num_ttys = MAX_CARD_PORTS - num_loops;
2819 num_ports = num_ttys + num_loops;
2820
Peter Hurley84472c32013-01-28 22:34:41 -05002821 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2822 | TTY_DRIVER_DYNAMIC_DEV);
2823 if (IS_ERR(fwtty_driver)) {
2824 err = PTR_ERR(fwtty_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002825 return err;
2826 }
2827
2828 fwtty_driver->driver_name = KBUILD_MODNAME;
2829 fwtty_driver->name = tty_dev_name;
2830 fwtty_driver->major = 0;
2831 fwtty_driver->minor_start = 0;
2832 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2833 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
Peter Hurley7355ba32012-11-02 08:16:33 -04002834 fwtty_driver->init_termios = tty_std_termios;
2835 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2836 tty_set_operations(fwtty_driver, &fwtty_ops);
2837
2838 err = tty_register_driver(fwtty_driver);
2839 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002840 pr_err("register tty driver failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002841 goto put_tty;
2842 }
2843
Peter Hurleyfa1da242013-01-28 22:34:38 -05002844 if (create_loop_dev) {
Peter Hurley84472c32013-01-28 22:34:41 -05002845 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2846 TTY_DRIVER_REAL_RAW
2847 | TTY_DRIVER_DYNAMIC_DEV);
2848 if (IS_ERR(fwloop_driver)) {
2849 err = PTR_ERR(fwloop_driver);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002850 goto unregister_driver;
2851 }
2852
2853 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2854 fwloop_driver->name = loop_dev_name;
2855 fwloop_driver->major = 0;
2856 fwloop_driver->minor_start = 0;
2857 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2858 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002859 fwloop_driver->init_termios = tty_std_termios;
2860 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2861 tty_set_operations(fwloop_driver, &fwloop_ops);
2862
2863 err = tty_register_driver(fwloop_driver);
2864 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002865 pr_err("register loop driver failed (%d)\n", err);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002866 goto put_loop;
2867 }
2868 }
2869
Peter Hurley7355ba32012-11-02 08:16:33 -04002870 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2871 sizeof(struct fwtty_transaction),
2872 0, 0, fwtty_txn_constructor);
2873 if (!fwtty_txn_cache) {
2874 err = -ENOMEM;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002875 goto unregister_loop;
Peter Hurley7355ba32012-11-02 08:16:33 -04002876 }
2877
2878 /*
2879 * Ideally, this address handler would be registered per local node
2880 * (rather than the same handler for all local nodes). However,
2881 * since the firewire core requires the config rom descriptor *before*
2882 * the local unit device(s) are created, a single management handler
2883 * must suffice for all local serial units.
2884 */
2885 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2886 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2887
2888 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2889 &fwserial_mgmt_addr_region);
2890 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002891 pr_err("add management handler failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002892 goto destroy_cache;
2893 }
2894
2895 fwserial_unit_directory_data.unit_addr_offset =
2896 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2897 err = fw_core_add_descriptor(&fwserial_unit_directory);
2898 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002899 pr_err("add unit descriptor failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002900 goto remove_handler;
2901 }
2902
2903 err = driver_register(&fwserial_driver.driver);
2904 if (err) {
Joe Perches6e8661e2013-05-28 19:44:24 -07002905 pr_err("register fwserial driver failed (%d)\n", err);
Peter Hurley7355ba32012-11-02 08:16:33 -04002906 goto remove_descriptor;
2907 }
2908
2909 return 0;
2910
2911remove_descriptor:
2912 fw_core_remove_descriptor(&fwserial_unit_directory);
2913remove_handler:
2914 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2915destroy_cache:
2916 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002917unregister_loop:
2918 if (create_loop_dev)
2919 tty_unregister_driver(fwloop_driver);
2920put_loop:
2921 if (create_loop_dev)
2922 put_tty_driver(fwloop_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002923unregister_driver:
2924 tty_unregister_driver(fwtty_driver);
2925put_tty:
2926 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05002927 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04002928 return err;
2929}
2930
2931static void __exit fwserial_exit(void)
2932{
2933 driver_unregister(&fwserial_driver.driver);
2934 fw_core_remove_descriptor(&fwserial_unit_directory);
2935 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2936 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002937 if (create_loop_dev) {
2938 tty_unregister_driver(fwloop_driver);
2939 put_tty_driver(fwloop_driver);
2940 }
Peter Hurley7355ba32012-11-02 08:16:33 -04002941 tty_unregister_driver(fwtty_driver);
2942 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05002943 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04002944}
2945
2946module_init(fwserial_init);
2947module_exit(fwserial_exit);
2948
2949MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
2950MODULE_DESCRIPTION("FireWire Serial TTY Driver");
2951MODULE_LICENSE("GPL");
2952MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
2953MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
2954MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
2955MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");