^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) =============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Intel MID PTI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) =============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) The Intel MID PTI project is HW implemented in Intel Atom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) system-on-a-chip designs based on the Parallel Trace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) Interface for MIPI P1149.7 cJTAG standard. The kernel solution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) for this platform involves the following files::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) ./include/linux/pti.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) ./drivers/.../n_tracesink.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) ./drivers/.../n_tracerouter.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) ./drivers/.../n_tracesink.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) ./drivers/.../pti.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) pti.c is the driver that enables various debugging features
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) popular on platforms from certain mobile manufacturers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) n_tracerouter.c and n_tracesink.c allow extra system information to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) be collected and routed to the pti driver, such as trace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) debugging data from a modem. Although n_tracerouter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) and n_tracesink are a part of the complete PTI solution,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) these two line disciplines can work separately from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) pti.c and route any data stream from one /dev/tty node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) to another /dev/tty node via kernel-space. This provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) a stable, reliable connection that will not break unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) the user-space application shuts down (plus avoids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) kernel->user->kernel context switch overheads of routing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) data).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) An example debugging usage for this driver system:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Hook /dev/ttyPTI0 to syslogd. Opening this port will also start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) a console device to further capture debugging messages to PTI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Hook /dev/ttyPTI1 to modem debugging data to write to PTI HW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) This is where n_tracerouter and n_tracesink are used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Hook /dev/pti to a user-level debugging application for writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) to PTI HW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * `Use mipi_` Kernel Driver API in other device drivers for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) debugging to PTI by first requesting a PTI write address via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) mipi_request_masterchannel(1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) Below is example pseudo-code on how a 'privileged' application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) can hook up n_tracerouter and n_tracesink to any tty on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) a system. 'Privileged' means the application has enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) privileges to successfully manipulate the ldisc drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) but is not just blindly executing as 'root'. Keep in mind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) the use of ioctl(,TIOCSETD,) is not specific to the n_tracerouter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) and n_tracesink line discpline drivers but is a generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) operation for a program to use a line discpline driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) on a tty port other than the default n_tty:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /////////// To hook up n_tracerouter and n_tracesink /////////
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) // Note that n_tracerouter depends on n_tracesink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define ONE_TTY "/dev/ttyOne"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define TWO_TTY "/dev/ttyTwo"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) // needed global to hand onto ldisc connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int g_fd_source = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int g_fd_sink = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) // these two vars used to grab LDISC values from loaded ldisc drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) // in OS. Look at /proc/tty/ldiscs to get the right numbers from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) // the ldiscs loaded in the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int source_ldisc_num, sink_ldisc_num = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) g_fd_source = open(ONE_TTY, O_RDWR); // must be R/W
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) g_fd_sink = open(TWO_TTY, O_RDWR); // must be R/W
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (g_fd_source <= 0) || (g_fd_sink <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) // doubt you'll want to use these exact error lines of code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) printf("Error on open(). errno: %d\n",errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) retval = ioctl(g_fd_sink, TIOCSETD, &sink_ldisc_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) printf("Error on ioctl(). errno: %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) retval = ioctl(g_fd_source, TIOCSETD, &source_ldisc_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) printf("Error on ioctl(). errno: %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /////////// To disconnect n_tracerouter and n_tracesink ////////
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) // First make sure data through the ldiscs has stopped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) // Second, disconnect ldiscs. This provides a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) // little cleaner shutdown on tty stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) sink_ldisc_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) source_ldisc_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ioctl(g_fd_uart, TIOCSETD, &sink_ldisc_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ioctl(g_fd_gadget, TIOCSETD, &source_ldisc_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) // Three, program closes connection, and cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) close(g_fd_uart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) close(g_fd_gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) g_fd_uart = g_fd_gadget = NULL;