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