Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: device/serial/serial_io_handler_posix.cc

Issue 1131953003: Use Linux custom baud rate selection based on struct termios2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/serial/serial_io_handler_posix.h" 5 #include "device/serial/serial_io_handler_posix.h"
6 6
7 #include <sys/ioctl.h> 7 #include <sys/ioctl.h>
8 #include <termios.h> 8 #include <termios.h>
9 9
10 #include "base/posix/eintr_wrapper.h" 10 #include "base/posix/eintr_wrapper.h"
11 11
12 #if defined(OS_LINUX) 12 #if defined(OS_LINUX)
13 #include <linux/serial.h> 13 #include <linux/serial.h>
14 #if defined(OS_CHROMEOS) 14 #if defined(OS_CHROMEOS)
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/sys_info.h" 16 #include "base/sys_info.h"
17 #include "chromeos/dbus/dbus_thread_manager.h" 17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/permission_broker_client.h" 18 #include "chromeos/dbus/permission_broker_client.h"
19 #endif // defined(OS_CHROMEOS) 19 #endif // defined(OS_CHROMEOS)
20
21 // The definition of struct termios2 is copied from asm-generic/termbits.h
22 // because including that header directly conflicts with termios.h.
23 extern "C" {
24 struct termios2 {
25 tcflag_t c_iflag; // input mode flags
26 tcflag_t c_oflag; // output mode flags
27 tcflag_t c_cflag; // control mode flags
28 tcflag_t c_lflag; // local mode flags
29 cc_t c_line; // line discipline
30 cc_t c_cc[19]; // control characters
31 speed_t c_ispeed; // input speed
32 speed_t c_ospeed; // output speed
33 };
34 }
35
20 #endif // defined(OS_LINUX) 36 #endif // defined(OS_LINUX)
21 37
22 namespace { 38 namespace {
23 39
24 // Convert an integral bit rate to a nominal one. Returns |true| 40 // Convert an integral bit rate to a nominal one. Returns |true|
25 // if the conversion was successful and |false| otherwise. 41 // if the conversion was successful and |false| otherwise.
26 bool BitrateToSpeedConstant(int bitrate, speed_t* speed) { 42 bool BitrateToSpeedConstant(int bitrate, speed_t* speed) {
27 #define BITRATE_TO_SPEED_CASE(x) \ 43 #define BITRATE_TO_SPEED_CASE(x) \
28 case x: \ 44 case x: \
29 *speed = B##x; \ 45 *speed = B##x; \
30 return true; 46 return true;
31 switch (bitrate) { 47 switch (bitrate) {
32 BITRATE_TO_SPEED_CASE(0) 48 BITRATE_TO_SPEED_CASE(0)
33 BITRATE_TO_SPEED_CASE(50) 49 BITRATE_TO_SPEED_CASE(50)
34 BITRATE_TO_SPEED_CASE(75) 50 BITRATE_TO_SPEED_CASE(75)
35 BITRATE_TO_SPEED_CASE(110) 51 BITRATE_TO_SPEED_CASE(110)
36 BITRATE_TO_SPEED_CASE(134) 52 BITRATE_TO_SPEED_CASE(134)
37 BITRATE_TO_SPEED_CASE(150) 53 BITRATE_TO_SPEED_CASE(150)
38 BITRATE_TO_SPEED_CASE(200) 54 BITRATE_TO_SPEED_CASE(200)
39 BITRATE_TO_SPEED_CASE(300) 55 BITRATE_TO_SPEED_CASE(300)
40 BITRATE_TO_SPEED_CASE(600) 56 BITRATE_TO_SPEED_CASE(600)
41 BITRATE_TO_SPEED_CASE(1200) 57 BITRATE_TO_SPEED_CASE(1200)
42 BITRATE_TO_SPEED_CASE(1800) 58 BITRATE_TO_SPEED_CASE(1800)
43 BITRATE_TO_SPEED_CASE(2400) 59 BITRATE_TO_SPEED_CASE(2400)
44 BITRATE_TO_SPEED_CASE(4800) 60 BITRATE_TO_SPEED_CASE(4800)
45 BITRATE_TO_SPEED_CASE(9600) 61 BITRATE_TO_SPEED_CASE(9600)
46 BITRATE_TO_SPEED_CASE(19200) 62 BITRATE_TO_SPEED_CASE(19200)
47 BITRATE_TO_SPEED_CASE(38400) 63 BITRATE_TO_SPEED_CASE(38400)
48 #if defined(OS_POSIX) && !defined(OS_MACOSX) 64 #if !defined(OS_MACOSX)
49 BITRATE_TO_SPEED_CASE(57600) 65 BITRATE_TO_SPEED_CASE(57600)
50 BITRATE_TO_SPEED_CASE(115200) 66 BITRATE_TO_SPEED_CASE(115200)
51 BITRATE_TO_SPEED_CASE(230400) 67 BITRATE_TO_SPEED_CASE(230400)
52 BITRATE_TO_SPEED_CASE(460800) 68 BITRATE_TO_SPEED_CASE(460800)
53 BITRATE_TO_SPEED_CASE(576000) 69 BITRATE_TO_SPEED_CASE(576000)
54 BITRATE_TO_SPEED_CASE(921600) 70 BITRATE_TO_SPEED_CASE(921600)
55 #endif 71 #endif
56 default: 72 default:
57 return false; 73 return false;
58 } 74 }
(...skipping 17 matching lines...) Expand all
76 SPEED_TO_BITRATE_CASE(200) 92 SPEED_TO_BITRATE_CASE(200)
77 SPEED_TO_BITRATE_CASE(300) 93 SPEED_TO_BITRATE_CASE(300)
78 SPEED_TO_BITRATE_CASE(600) 94 SPEED_TO_BITRATE_CASE(600)
79 SPEED_TO_BITRATE_CASE(1200) 95 SPEED_TO_BITRATE_CASE(1200)
80 SPEED_TO_BITRATE_CASE(1800) 96 SPEED_TO_BITRATE_CASE(1800)
81 SPEED_TO_BITRATE_CASE(2400) 97 SPEED_TO_BITRATE_CASE(2400)
82 SPEED_TO_BITRATE_CASE(4800) 98 SPEED_TO_BITRATE_CASE(4800)
83 SPEED_TO_BITRATE_CASE(9600) 99 SPEED_TO_BITRATE_CASE(9600)
84 SPEED_TO_BITRATE_CASE(19200) 100 SPEED_TO_BITRATE_CASE(19200)
85 SPEED_TO_BITRATE_CASE(38400) 101 SPEED_TO_BITRATE_CASE(38400)
86 #if defined(OS_POSIX) && !defined(OS_MACOSX) 102 #if !defined(OS_MACOSX)
87 SPEED_TO_BITRATE_CASE(57600) 103 SPEED_TO_BITRATE_CASE(57600)
88 SPEED_TO_BITRATE_CASE(115200) 104 SPEED_TO_BITRATE_CASE(115200)
89 SPEED_TO_BITRATE_CASE(230400) 105 SPEED_TO_BITRATE_CASE(230400)
90 SPEED_TO_BITRATE_CASE(460800) 106 SPEED_TO_BITRATE_CASE(460800)
91 SPEED_TO_BITRATE_CASE(576000) 107 SPEED_TO_BITRATE_CASE(576000)
92 SPEED_TO_BITRATE_CASE(921600) 108 SPEED_TO_BITRATE_CASE(921600)
93 #endif 109 #endif
94 default: 110 default:
95 return false; 111 return false;
96 } 112 }
97 #undef SPEED_TO_BITRATE_CASE 113 #undef SPEED_TO_BITRATE_CASE
98 } 114 }
99 115
100 bool SetCustomBitrate(base::PlatformFile file, 116 bool SetCustomBitrate(base::PlatformFile file,
101 struct termios* config, 117 struct termios* config,
102 int bitrate) { 118 int bitrate) {
103 #if defined(OS_LINUX) 119 #if defined(OS_LINUX)
104 struct serial_struct serial; 120 struct termios2 tio;
105 if (ioctl(file, TIOCGSERIAL, &serial) < 0) { 121 if (ioctl(file, TCGETS2, &tio) < 0) {
122 VPLOG(1) << "Failed to get parameters to set custom bitrate";
106 return false; 123 return false;
107 } 124 }
108 serial.flags = (serial.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST; 125 tio.c_cflag &= ~CBAUD;
109 serial.custom_divisor = serial.baud_base / bitrate; 126 tio.c_cflag |= CBAUDEX;
110 if (serial.custom_divisor < 1) { 127 tio.c_ispeed = bitrate;
111 serial.custom_divisor = 1; 128 tio.c_ospeed = bitrate;
129 if (ioctl(file, TCSETS2, &tio) < 0) {
130 VPLOG(1) << "Failed to set custom bitrate";
131 return false;
112 } 132 }
113 cfsetispeed(config, B38400); 133 return true;
114 cfsetospeed(config, B38400);
115 return ioctl(file, TIOCSSERIAL, &serial) >= 0;
116 #elif defined(OS_MACOSX) 134 #elif defined(OS_MACOSX)
117 speed_t speed = static_cast<speed_t>(bitrate); 135 speed_t speed = static_cast<speed_t>(bitrate);
118 cfsetispeed(config, speed); 136 cfsetispeed(config, speed);
119 cfsetospeed(config, speed); 137 cfsetospeed(config, speed);
120 return true; 138 return true;
121 #else 139 #else
122 return false; 140 return false;
123 #endif 141 #endif
124 } 142 }
125 143
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 (config.c_cflag & CSTOPB) ? serial::STOP_BITS_TWO : serial::STOP_BITS_ONE; 470 (config.c_cflag & CSTOPB) ? serial::STOP_BITS_TWO : serial::STOP_BITS_ONE;
453 info->cts_flow_control = (config.c_cflag & CRTSCTS) != 0; 471 info->cts_flow_control = (config.c_cflag & CRTSCTS) != 0;
454 return info.Pass(); 472 return info.Pass();
455 } 473 }
456 474
457 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { 475 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) {
458 return port_name; 476 return port_name;
459 } 477 }
460 478
461 } // namespace device 479 } // namespace device
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698