Merge tag 'ftracetest-3.19' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[cascardo/linux.git] / Documentation / serial / driver
1
2                         Low Level Serial API
3                         --------------------
4
5
6 This document is meant as a brief overview of some aspects of the new serial
7 driver.  It is not complete, any questions you have should be directed to
8 <rmk@arm.linux.org.uk>
9
10 The reference implementation is contained within amba_pl011.c.
11
12
13
14 Low Level Serial Hardware Driver
15 --------------------------------
16
17 The low level serial hardware driver is responsible for supplying port
18 information (defined by uart_port) and a set of control methods (defined
19 by uart_ops) to the core serial driver.  The low level driver is also
20 responsible for handling interrupts for the port, and providing any
21 console support.
22
23
24 Console Support
25 ---------------
26
27 The serial core provides a few helper functions.  This includes identifing
28 the correct port structure (via uart_get_console) and decoding command line
29 arguments (uart_parse_options).
30
31 There is also a helper function (uart_write_console) which performs a
32 character by character write, translating newlines to CRLF sequences.
33 Driver writers are recommended to use this function rather than implementing
34 their own version.
35
36
37 Locking
38 -------
39
40 It is the responsibility of the low level hardware driver to perform the
41 necessary locking using port->lock.  There are some exceptions (which
42 are described in the uart_ops listing below.)
43
44 There are three locks.  A per-port spinlock, a per-port tmpbuf semaphore,
45 and an overall semaphore.
46
47 From the core driver perspective, the port->lock locks the following
48 data:
49
50         port->mctrl
51         port->icount
52         info->xmit.head (circ->head)
53         info->xmit.tail (circ->tail)
54
55 The low level driver is free to use this lock to provide any additional
56 locking.
57
58 The core driver uses the info->tmpbuf_sem lock to prevent multi-threaded
59 access to the info->tmpbuf bouncebuffer used for port writes.
60
61 The port_sem semaphore is used to protect against ports being added/
62 removed or reconfigured at inappropriate times.
63
64
65 uart_ops
66 --------
67
68 The uart_ops structure is the main interface between serial_core and the
69 hardware specific driver.  It contains all the methods to control the
70 hardware.
71
72   tx_empty(port)
73         This function tests whether the transmitter fifo and shifter
74         for the port described by 'port' is empty.  If it is empty,
75         this function should return TIOCSER_TEMT, otherwise return 0.
76         If the port does not support this operation, then it should
77         return TIOCSER_TEMT.
78
79         Locking: none.
80         Interrupts: caller dependent.
81         This call must not sleep
82
83   set_mctrl(port, mctrl)
84         This function sets the modem control lines for port described
85         by 'port' to the state described by mctrl.  The relevant bits
86         of mctrl are:
87                 - TIOCM_RTS     RTS signal.
88                 - TIOCM_DTR     DTR signal.
89                 - TIOCM_OUT1    OUT1 signal.
90                 - TIOCM_OUT2    OUT2 signal.
91                 - TIOCM_LOOP    Set the port into loopback mode.
92         If the appropriate bit is set, the signal should be driven
93         active.  If the bit is clear, the signal should be driven
94         inactive.
95
96         Locking: port->lock taken.
97         Interrupts: locally disabled.
98         This call must not sleep
99
100   get_mctrl(port)
101         Returns the current state of modem control inputs.  The state
102         of the outputs should not be returned, since the core keeps
103         track of their state.  The state information should include:
104                 - TIOCM_CAR     state of DCD signal
105                 - TIOCM_CTS     state of CTS signal
106                 - TIOCM_DSR     state of DSR signal
107                 - TIOCM_RI      state of RI signal
108         The bit is set if the signal is currently driven active.  If
109         the port does not support CTS, DCD or DSR, the driver should
110         indicate that the signal is permanently active.  If RI is
111         not available, the signal should not be indicated as active.
112
113         Locking: port->lock taken.
114         Interrupts: locally disabled.
115         This call must not sleep
116
117   stop_tx(port)
118         Stop transmitting characters.  This might be due to the CTS
119         line becoming inactive or the tty layer indicating we want
120         to stop transmission due to an XOFF character.
121
122         The driver should stop transmitting characters as soon as
123         possible.
124
125         Locking: port->lock taken.
126         Interrupts: locally disabled.
127         This call must not sleep
128
129   start_tx(port)
130         Start transmitting characters.
131
132         Locking: port->lock taken.
133         Interrupts: locally disabled.
134         This call must not sleep
135
136   send_xchar(port,ch)
137         Transmit a high priority character, even if the port is stopped.
138         This is used to implement XON/XOFF flow control and tcflow().  If
139         the serial driver does not implement this function, the tty core
140         will append the character to the circular buffer and then call
141         start_tx() / stop_tx() to flush the data out.
142
143         Do not transmit if ch == '\0' (__DISABLED_CHAR).
144
145         Locking: none.
146         Interrupts: caller dependent.
147
148   stop_rx(port)
149         Stop receiving characters; the port is in the process of
150         being closed.
151
152         Locking: port->lock taken.
153         Interrupts: locally disabled.
154         This call must not sleep
155
156   enable_ms(port)
157         Enable the modem status interrupts.
158
159         This method may be called multiple times.  Modem status
160         interrupts should be disabled when the shutdown method is
161         called.
162
163         Locking: port->lock taken.
164         Interrupts: locally disabled.
165         This call must not sleep
166
167   break_ctl(port,ctl)
168         Control the transmission of a break signal.  If ctl is
169         nonzero, the break signal should be transmitted.  The signal
170         should be terminated when another call is made with a zero
171         ctl.
172
173         Locking: none.
174         Interrupts: caller dependent.
175         This call must not sleep
176
177   startup(port)
178         Grab any interrupt resources and initialise any low level driver
179         state.  Enable the port for reception.  It should not activate
180         RTS nor DTR; this will be done via a separate call to set_mctrl.
181
182         This method will only be called when the port is initially opened.
183
184         Locking: port_sem taken.
185         Interrupts: globally disabled.
186
187   shutdown(port)
188         Disable the port, disable any break condition that may be in
189         effect, and free any interrupt resources.  It should not disable
190         RTS nor DTR; this will have already been done via a separate
191         call to set_mctrl.
192
193         Drivers must not access port->info once this call has completed.
194
195         This method will only be called when there are no more users of
196         this port.
197
198         Locking: port_sem taken.
199         Interrupts: caller dependent.
200
201   flush_buffer(port)
202         Flush any write buffers, reset any DMA state and stop any
203         ongoing DMA transfers.
204
205         This will be called whenever the port->info->xmit circular
206         buffer is cleared.
207
208         Locking: port->lock taken.
209         Interrupts: locally disabled.
210         This call must not sleep
211
212   set_termios(port,termios,oldtermios)
213         Change the port parameters, including word length, parity, stop
214         bits.  Update read_status_mask and ignore_status_mask to indicate
215         the types of events we are interested in receiving.  Relevant
216         termios->c_cflag bits are:
217                 CSIZE   - word size
218                 CSTOPB  - 2 stop bits
219                 PARENB  - parity enable
220                 PARODD  - odd parity (when PARENB is in force)
221                 CREAD   - enable reception of characters (if not set,
222                           still receive characters from the port, but
223                           throw them away.
224                 CRTSCTS - if set, enable CTS status change reporting
225                 CLOCAL  - if not set, enable modem status change
226                           reporting.
227         Relevant termios->c_iflag bits are:
228                 INPCK   - enable frame and parity error events to be
229                           passed to the TTY layer.
230                 BRKINT
231                 PARMRK  - both of these enable break events to be
232                           passed to the TTY layer.
233
234                 IGNPAR  - ignore parity and framing errors
235                 IGNBRK  - ignore break errors,  If IGNPAR is also
236                           set, ignore overrun errors as well.
237         The interaction of the iflag bits is as follows (parity error
238         given as an example):
239         Parity error    INPCK   IGNPAR
240         n/a             0       n/a     character received, marked as
241                                         TTY_NORMAL
242         None            1       n/a     character received, marked as
243                                         TTY_NORMAL
244         Yes             1       0       character received, marked as
245                                         TTY_PARITY
246         Yes             1       1       character discarded
247
248         Other flags may be used (eg, xon/xoff characters) if your
249         hardware supports hardware "soft" flow control.
250
251         Locking: none.
252         Interrupts: caller dependent.
253         This call must not sleep
254
255   pm(port,state,oldstate)
256         Perform any power management related activities on the specified
257         port.  State indicates the new state (defined by
258         enum uart_pm_state), oldstate indicates the previous state.
259
260         This function should not be used to grab any resources.
261
262         This will be called when the port is initially opened and finally
263         closed, except when the port is also the system console.  This
264         will occur even if CONFIG_PM is not set.
265
266         Locking: none.
267         Interrupts: caller dependent.
268
269   type(port)
270         Return a pointer to a string constant describing the specified
271         port, or return NULL, in which case the string 'unknown' is
272         substituted.
273
274         Locking: none.
275         Interrupts: caller dependent.
276
277   release_port(port)
278         Release any memory and IO region resources currently in use by
279         the port.
280
281         Locking: none.
282         Interrupts: caller dependent.
283
284   request_port(port)
285         Request any memory and IO region resources required by the port.
286         If any fail, no resources should be registered when this function
287         returns, and it should return -EBUSY on failure.
288
289         Locking: none.
290         Interrupts: caller dependent.
291
292   config_port(port,type)
293         Perform any autoconfiguration steps required for the port.  `type`
294         contains a bit mask of the required configuration.  UART_CONFIG_TYPE
295         indicates that the port requires detection and identification.
296         port->type should be set to the type found, or PORT_UNKNOWN if
297         no port was detected.
298
299         UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
300         which should be probed using standard kernel autoprobing techniques.
301         This is not necessary on platforms where ports have interrupts
302         internally hard wired (eg, system on a chip implementations).
303
304         Locking: none.
305         Interrupts: caller dependent.
306
307   verify_port(port,serinfo)
308         Verify the new serial port information contained within serinfo is
309         suitable for this port type.
310
311         Locking: none.
312         Interrupts: caller dependent.
313
314   ioctl(port,cmd,arg)
315         Perform any port specific IOCTLs.  IOCTL commands must be defined
316         using the standard numbering system found in <asm/ioctl.h>
317
318         Locking: none.
319         Interrupts: caller dependent.
320
321   poll_init(port)
322         Called by kgdb to perform the minimal hardware initialization needed
323         to support poll_put_char() and poll_get_char().  Unlike ->startup()
324         this should not request interrupts.
325
326         Locking: tty_mutex and tty_port->mutex taken.
327         Interrupts: n/a.
328
329   poll_put_char(port,ch)
330         Called by kgdb to write a single character directly to the serial
331         port.  It can and should block until there is space in the TX FIFO.
332
333         Locking: none.
334         Interrupts: caller dependent.
335         This call must not sleep
336
337   poll_get_char(port)
338         Called by kgdb to read a single character directly from the serial
339         port.  If data is available, it should be returned; otherwise
340         the function should return NO_POLL_CHAR immediately.
341
342         Locking: none.
343         Interrupts: caller dependent.
344         This call must not sleep
345
346 Other functions
347 ---------------
348
349 uart_update_timeout(port,cflag,baud)
350         Update the FIFO drain timeout, port->timeout, according to the
351         number of bits, parity, stop bits and baud rate.
352
353         Locking: caller is expected to take port->lock
354         Interrupts: n/a
355
356 uart_get_baud_rate(port,termios,old,min,max)
357         Return the numeric baud rate for the specified termios, taking
358         account of the special 38400 baud "kludge".  The B0 baud rate
359         is mapped to 9600 baud.
360
361         If the baud rate is not within min..max, then if old is non-NULL,
362         the original baud rate will be tried.  If that exceeds the
363         min..max constraint, 9600 baud will be returned.  termios will
364         be updated to the baud rate in use.
365
366         Note: min..max must always allow 9600 baud to be selected.
367
368         Locking: caller dependent.
369         Interrupts: n/a
370
371 uart_get_divisor(port,baud)
372         Return the divsor (baud_base / baud) for the specified baud
373         rate, appropriately rounded.
374
375         If 38400 baud and custom divisor is selected, return the
376         custom divisor instead.
377
378         Locking: caller dependent.
379         Interrupts: n/a
380
381 uart_match_port(port1,port2)
382         This utility function can be used to determine whether two
383         uart_port structures describe the same port.
384
385         Locking: n/a
386         Interrupts: n/a
387
388 uart_write_wakeup(port)
389         A driver is expected to call this function when the number of
390         characters in the transmit buffer have dropped below a threshold.
391
392         Locking: port->lock should be held.
393         Interrupts: n/a
394
395 uart_register_driver(drv)
396         Register a uart driver with the core driver.  We in turn register
397         with the tty layer, and initialise the core driver per-port state.
398
399         drv->port should be NULL, and the per-port structures should be
400         registered using uart_add_one_port after this call has succeeded.
401
402         Locking: none
403         Interrupts: enabled
404
405 uart_unregister_driver()
406         Remove all references to a driver from the core driver.  The low
407         level driver must have removed all its ports via the
408         uart_remove_one_port() if it registered them with uart_add_one_port().
409
410         Locking: none
411         Interrupts: enabled
412
413 uart_suspend_port()
414
415 uart_resume_port()
416
417 uart_add_one_port()
418
419 uart_remove_one_port()
420
421 Other notes
422 -----------
423
424 It is intended some day to drop the 'unused' entries from uart_port, and
425 allow low level drivers to register their own individual uart_port's with
426 the core.  This will allow drivers to use uart_port as a pointer to a
427 structure containing both the uart_port entry with their own extensions,
428 thus:
429
430         struct my_port {
431                 struct uart_port        port;
432                 int                     my_stuff;
433         };
434
435 Modem control lines via GPIO
436 ----------------------------
437
438 Some helpers are provided in order to set/get modem control lines via GPIO.
439
440 mctrl_gpio_init(dev, idx):
441         This will get the {cts,rts,...}-gpios from device tree if they are
442         present and request them, set direction etc, and return an
443         allocated structure. devm_* functions are used, so there's no need
444         to call mctrl_gpio_free().
445
446 mctrl_gpio_free(dev, gpios):
447         This will free the requested gpios in mctrl_gpio_init().
448         As devm_* function are used, there's generally no need to call
449         this function.
450
451 mctrl_gpio_to_gpiod(gpios, gidx)
452         This returns the gpio structure associated to the modem line index.
453
454 mctrl_gpio_set(gpios, mctrl):
455         This will sets the gpios according to the mctrl state.
456
457 mctrl_gpio_get(gpios, mctrl):
458         This will update mctrl with the gpios values.