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