5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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.
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
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/ioctl.h>
41 #define CONNMAN_API_SUBJECT_TO_CHANGE
42 #include <connman/plugin.h>
43 #include <connman/log.h>
45 #define TIST_SYSFS_INSTALL "/sys/devices/platform/kim/install"
46 #define TIST_SYSFS_UART "/sys/devices/platform/kim/dev_name"
47 #define TIST_SYSFS_BAUD "/sys/devices/platform/kim/baud_rate"
49 /* Shared transport line discipline */
52 static GIOChannel *install_channel = NULL;
53 static GIOChannel *uart_channel = NULL;
54 static char uart_dev_name[32];
55 static unsigned long baud_rate = 0;
57 static guint install_watch = 0;
58 static guint uart_watch = 0;
60 static int install_count = 0;
64 tcflag_t c_iflag; /* input mode flags */
65 tcflag_t c_oflag; /* output mode flags */
66 tcflag_t c_cflag; /* control mode flags */
67 tcflag_t c_lflag; /* local mode flags */
68 cc_t c_line; /* line discipline */
69 cc_t c_cc[NCCS2]; /* control characters */
70 speed_t c_ispeed; /* input speed */
71 speed_t c_ospeed; /* output speed */
74 #define BOTHER 0x00001000
77 #define HCI_HDR_OPCODE 0xff36
78 #define HCI_COMMAND_PKT 0x01
79 #define HCI_EVENT_PKT 0x04
80 #define EVT_CMD_COMPLETE 0x0E
82 /* HCI Command structure to set the target baud rate */
83 struct speed_change_cmd {
88 } __attribute__ ((packed));
90 /* HCI Event structure to set the cusrom baud rate*/
99 } __attribute__ ((packed));
101 static int read_baud_rate(unsigned long *baud)
108 f = fopen(TIST_SYSFS_BAUD, "r");
112 err = fscanf(f, "%lu", baud);
115 DBG("baud rate %lu", *baud);
120 static int read_uart_name(char uart_name[], size_t uart_name_len)
127 memset(uart_name, 0, uart_name_len);
129 f = fopen(TIST_SYSFS_UART, "r");
133 err = fscanf(f, "%s", uart_name);
136 DBG("UART name %s", uart_name);
141 static int read_hci_event(int fd, unsigned char *buf, int size)
143 int prefix_len, param_len;
148 /* First 3 bytes are prefix, event and param length */
149 prefix_len = read(fd, buf, 3);
153 if (prefix_len < 3) {
154 connman_error("Truncated HCI prefix %d bytes 0x%x",
159 DBG("type 0x%x event 0x%x param len %d", buf[0], buf[1], buf[2]);
162 if (param_len > size - 3) {
163 connman_error("Buffer is too small %d", size);
167 return read(fd, buf + 3, param_len);
170 static int read_command_complete(int fd, unsigned short opcode)
172 struct cmd_complete resp;
177 err = read_hci_event(fd, (unsigned char *)&resp, sizeof(resp));
181 DBG("HCI event %d bytes", err);
183 if (resp.uart_prefix != HCI_EVENT_PKT) {
184 connman_error("Not an event packet");
188 if (resp.evt != EVT_CMD_COMPLETE) {
189 connman_error("Not a cmd complete event");
194 connman_error("HCI header length %d", resp.plen);
198 if (resp.opcode != (unsigned short) opcode) {
199 connman_error("opcode 0x%04x 0x%04x", resp.opcode, opcode);
206 /* The default baud rate is 115200 */
207 static int set_default_baud_rate(int fd)
214 err = tcflush(fd, TCIOFLUSH);
218 err = tcgetattr(fd, &ti);
225 ti.c_cflag |= CRTSCTS;
227 err = tcsetattr(fd, TCSANOW, &ti);
231 cfsetospeed(&ti, B115200);
232 cfsetispeed(&ti, B115200);
234 err = tcsetattr(fd, TCSANOW, &ti);
238 err = tcflush(fd, TCIOFLUSH);
245 connman_error("%s", strerror(errno));
250 static int set_custom_baud_rate(int fd, unsigned long cus_baud_rate, int flow_ctrl)
256 DBG("baud rate %lu flow_ctrl %d", cus_baud_rate, flow_ctrl);
258 err = tcflush(fd, TCIOFLUSH);
262 err = tcgetattr(fd, &ti);
267 ti.c_cflag |= CRTSCTS;
269 ti.c_cflag &= ~CRTSCTS;
272 * Set the parameters associated with the UART
273 * The change will occur immediately by using TCSANOW.
275 err = tcsetattr(fd, TCSANOW, &ti);
279 err = tcflush(fd, TCIOFLUSH);
283 /* Set the actual baud rate */
284 err = ioctl(fd, TCGETS2, &ti2);
288 ti2.c_cflag &= ~CBAUD;
289 ti2.c_cflag |= BOTHER;
290 ti2.c_ospeed = cus_baud_rate;
292 err = ioctl(fd, TCSETS2, &ti2);
299 DBG("%s", strerror(errno));
304 static gboolean uart_event(GIOChannel *channel,
305 GIOCondition cond, gpointer data)
311 uart_fd = g_io_channel_unix_get_fd(channel);
313 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
314 connman_error("UART event 0x%x", cond);
316 g_source_remove(uart_watch);
321 if (read_command_complete(uart_fd, HCI_HDR_OPCODE) < 0)
324 if (set_custom_baud_rate(uart_fd, baud_rate, 1) < 0)
328 if (ioctl(uart_fd, TIOCSETD, &ldisc) < 0)
332 __sync_synchronize();
338 __sync_synchronize();
340 g_io_channel_shutdown(channel, TRUE, NULL);
341 g_io_channel_unref(channel);
346 static int install_ldisc(GIOChannel *channel, bool install)
349 struct speed_change_cmd cmd;
352 DBG("%d %p", install, uart_channel);
356 __sync_synchronize();
359 DBG("UART channel is NULL");
363 g_io_channel_shutdown(uart_channel, TRUE, NULL);
364 g_io_channel_unref(uart_channel);
372 g_io_channel_shutdown(uart_channel, TRUE, NULL);
373 g_io_channel_unref(uart_channel);
377 DBG("opening %s custom baud %lu", uart_dev_name, baud_rate);
379 uart_fd = open(uart_dev_name, O_RDWR | O_CLOEXEC);
383 uart_channel = g_io_channel_unix_new(uart_fd);
384 g_io_channel_set_close_on_unref(uart_channel, TRUE);
386 g_io_channel_set_encoding(uart_channel, NULL, NULL);
387 g_io_channel_set_buffered(uart_channel, FALSE);
389 flags = g_io_channel_get_flags(uart_channel);
390 flags |= G_IO_FLAG_NONBLOCK;
391 g_io_channel_set_flags(uart_channel, flags, NULL);
393 err = set_default_baud_rate(uart_fd);
395 g_io_channel_shutdown(uart_channel, TRUE, NULL);
396 g_io_channel_unref(uart_channel);
402 if (baud_rate == 115200) {
406 if (ioctl(uart_fd, TIOCSETD, &ldisc) < 0) {
407 g_io_channel_shutdown(uart_channel, TRUE, NULL);
408 g_io_channel_unref(uart_channel);
413 __sync_synchronize();
418 cmd.uart_prefix = HCI_COMMAND_PKT;
419 cmd.opcode = HCI_HDR_OPCODE;
420 cmd.plen = sizeof(unsigned long);
421 cmd.speed = baud_rate;
423 uart_watch = g_io_add_watch(uart_channel,
424 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
427 err = write(uart_fd, &cmd, sizeof(cmd));
429 connman_error("Write failed %d", err);
431 g_io_channel_shutdown(uart_channel, TRUE, NULL);
432 g_io_channel_unref(uart_channel);
440 static gboolean install_event(GIOChannel *channel,
441 GIOCondition cond, gpointer data)
443 GIOStatus status = G_IO_STATUS_NORMAL;
444 unsigned int install_state;
451 if (cond & (G_IO_HUP | G_IO_NVAL)) {
452 connman_error("install event 0x%x", cond);
456 __sync_synchronize();
457 if (install_count != 0) {
458 status = g_io_channel_seek_position(channel, 0, G_SEEK_SET, NULL);
459 if (status != G_IO_STATUS_NORMAL) {
460 g_io_channel_shutdown(channel, TRUE, NULL);
461 g_io_channel_unref(channel);
465 /* Read the install value */
466 status = g_io_channel_read_chars(channel, (gchar *) buf,
468 if (status != G_IO_STATUS_NORMAL) {
469 g_io_channel_shutdown(channel, TRUE, NULL);
470 g_io_channel_unref(channel);
474 install_state = atoi(buf);
475 DBG("install event while installing %d %c", install_state, buf[0]);
480 __sync_synchronize();
483 status = g_io_channel_seek_position(channel, 0, G_SEEK_SET, NULL);
484 if (status != G_IO_STATUS_NORMAL) {
485 g_io_channel_shutdown(channel, TRUE, NULL);
486 g_io_channel_unref(channel);
490 /* Read the install value */
491 status = g_io_channel_read_chars(channel, (gchar *) buf, 8, &len, NULL);
492 if (status != G_IO_STATUS_NORMAL) {
493 g_io_channel_shutdown(channel, TRUE, NULL);
494 g_io_channel_unref(channel);
498 install_state = atoi(buf);
500 DBG("install state %d", install_state);
502 install = !!install_state;
504 if (install_ldisc(channel, install) < 0) {
505 connman_error("ldisc installation failed");
507 __sync_synchronize();
515 static int tist_init(void)
517 GIOStatus status = G_IO_STATUS_NORMAL;
519 unsigned int install_state;
524 err = read_uart_name(uart_dev_name, sizeof(uart_dev_name));
526 connman_error("Could not read the UART name");
530 err = read_baud_rate(&baud_rate);
532 connman_error("Could not read the baud rate");
536 fd = open(TIST_SYSFS_INSTALL, O_RDONLY | O_CLOEXEC);
538 connman_error("Failed to open TI ST sysfs install file");
542 install_channel = g_io_channel_unix_new(fd);
543 g_io_channel_set_close_on_unref(install_channel, TRUE);
545 g_io_channel_set_encoding(install_channel, NULL, NULL);
546 g_io_channel_set_buffered(install_channel, FALSE);
548 flags = g_io_channel_get_flags(install_channel);
549 flags |= G_IO_FLAG_NONBLOCK;
550 g_io_channel_set_flags(install_channel, flags, NULL);
552 status = g_io_channel_read_chars(install_channel, (gchar *) buf, 8,
554 if (status != G_IO_STATUS_NORMAL) {
555 g_io_channel_shutdown(install_channel, TRUE, NULL);
556 g_io_channel_unref(install_channel);
560 status = g_io_channel_seek_position(install_channel, 0, G_SEEK_SET, NULL);
561 if (status != G_IO_STATUS_NORMAL) {
562 connman_error("Initial seek failed");
563 g_io_channel_shutdown(install_channel, TRUE, NULL);
564 g_io_channel_unref(install_channel);
568 install_state = atoi(buf);
570 DBG("Initial state %d", install_state);
572 install_watch = g_io_add_watch_full(install_channel, G_PRIORITY_HIGH,
574 install_event, NULL, NULL);
578 __sync_synchronize();
580 err = install_ldisc(install_channel, true);
582 connman_error("ldisc installtion failed");
591 static void tist_exit(void)
594 if (install_watch > 0)
595 g_source_remove(install_watch);
597 DBG("uart_channel %p", uart_channel);
599 g_io_channel_shutdown(install_channel, TRUE, NULL);
600 g_io_channel_unref(install_channel);
603 g_io_channel_shutdown(uart_channel, TRUE, NULL);
604 g_io_channel_unref(uart_channel);
610 CONNMAN_PLUGIN_DEFINE(tist, "TI shared transport support", VERSION,
611 CONNMAN_PLUGIN_PRIORITY_DEFAULT, tist_init, tist_exit)