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