1 #ifndef _FIREWIRE_FWSERIAL_H
2 #define _FIREWIRE_FWSERIAL_H
4 #include <linux/kernel.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>
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)
26 int n = (val) ? min(ilog2(val) + 1, DISTRIBUTION_MAX_INDEX) : 0;
30 #define DISTRIBUTION_MAX_INDEX 0
31 #define profile_size_distrib(st, n)
34 /* Parameters for both VIRT_CABLE_PLUG & VIRT_CABLE_PLUG_RSP mgmt codes */
35 struct virt_plug_params {
43 struct peer_work_params {
45 struct virt_plug_params plug_req;
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)
76 struct fw_serial *serial;
84 /* these are usable only if state == FWSC_ATTACHED */
89 struct list_head list;
93 struct work_struct work;
94 struct peer_work_params work_params;
95 struct timer_list timer;
97 struct delayed_work connect;
100 struct fwtty_port *port;
103 #define to_peer(ptr, field) (container_of(ptr, struct fwtty_peer, field))
105 /* state values for fwtty_peer.state field */
106 enum fwtty_peer_state {
111 FWPS_PLUG_RESPONDING,
113 FWPS_UNPLUG_RESPONDING,
115 FWPS_NO_MGMT_ADDR = -1,
118 #define CONNECT_RETRY_DELAY HZ
119 #define MAX_CONNECT_RETRIES 10
121 /* must be holding peer lock for these state funclets */
122 static inline void peer_set_state(struct fwtty_peer *peer, int new)
127 static inline struct fwtty_port *peer_revert_state(struct fwtty_peer *peer)
129 struct fwtty_port *port = peer->port;
132 peer_set_state(peer, FWPS_NOT_ATTACHED);
136 struct fwserial_mgmt_pkt {
142 struct virt_plug_params plug_req;
143 struct virt_plug_params plug_rsp;
147 /* fwserial_mgmt_packet codes */
148 #define FWSC_RSP_OK 0x0000
149 #define FWSC_RSP_NACK 0x8000
150 #define FWSC_CODE_MASK 0x0fff
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
157 /* 1 min. plug timeout -- suitable for userland authorization */
158 #define VIRT_CABLE_PLUG_TIMEOUT (60 * HZ)
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];
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);
181 /* codes for console ops notify */
182 #define FWCON_NOTIFY_ATTACH 1
183 #define FWCON_NOTIFY_DETACH 2
186 struct list_head list;
188 unsigned char data[0];
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
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
248 struct tty_port port;
249 struct device *device;
251 struct fw_serial *serial;
252 struct fw_address_handler rx_handler;
254 struct fwconsole_ops *fwcon_ops;
257 wait_queue_head_t wait_tx;
258 struct delayed_work emit_breaks;
260 unsigned long break_last;
262 struct work_struct hangup;
268 struct delayed_work drain;
269 struct work_struct push;
270 struct list_head buf_list;
272 struct dma_fifo tx_fifo;
274 unsigned status_mask;
275 unsigned ignore_mask;
276 unsigned break_ctl:1,
282 struct fwtty_peer *peer;
284 struct async_icount icount;
288 #define to_port(ptr, field) (container_of(ptr, struct fwtty_port, field))
290 /* bit #s for flags field */
293 #define BUFFERING_RX 2
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
301 #define MCTRL_MASK (TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 | TIOCM_OUT2 | \
302 TIOCM_LOOP | OOB_RX_THROTTLE | MCTRL_RSRVD)
304 /* XXX even every 1/50th secs. may be unnecessarily accurate */
305 /* delay in jiffies between brk emits */
306 #define FREQ_BREAKS (HZ / 50)
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 */
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 */
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
325 #define FWTTY_PORT_RXFIFO_LEN MAX_ASYNC_PAYLOAD
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
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
338 struct fw_card *card;
341 struct fwtty_peer *self;
343 struct list_head list;
344 struct list_head peer_list;
346 struct fwtty_port *ports[MAX_CARD_PORTS];
349 #define to_serial(ptr, field) (container_of(ptr, struct fw_serial, field))
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;
356 struct tty_driver *fwtty_driver;
358 #define driver_err(s, v...) pr_err(KBUILD_MODNAME ": " s, ##v)
360 struct fwtty_port *fwtty_port_get(unsigned index);
361 void fwtty_port_put(struct fwtty_port *port);
363 static inline void fwtty_bind_console(struct fwtty_port *port,
364 struct fwconsole_ops *fwcon_ops,
367 port->con_data = data;
368 port->fwcon_ops = fwcon_ops;
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
375 static inline int link_speed_to_max_payload(unsigned speed)
377 static const int max_async[] = { 307, 614, 1229, 2458, };
378 BUILD_BUG_ON(ARRAY_SIZE(max_async) - 1 != SCODE_800);
380 speed = clamp(speed, (unsigned) SCODE_100, (unsigned) SCODE_800);
382 return max_async[speed];
384 return 1 << (speed + 9);
387 #endif /* _FIREWIRE_FWSERIAL_H */