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