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