Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: fwserial: Add TTY-over-Firewire serial driver

This patch provides the kernel driver for high-speed TTY
communication over the IEEE 1394 bus.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Acked-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Peter Hurley and committed by
Greg Kroah-Hartman
7355ba34 fd985e1d

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