2018-05-03 12:39:47

by Muni Sekhar

[permalink] [raw]
Subject: serial: custom baud rate

Hi All,

From include/asm-generic/termbits.h , I see baudrate can be one of the
standard values: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000,
576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000,
3500000, 4000000.


If I need to set a custom baud rates(e.g. 14400, 128000, 256000), does
Linux serial framework has any supporting method?



--
Thanks,
Sekhar


2018-05-03 17:54:33

by Theodore Ts'o

[permalink] [raw]
Subject: Re: serial: custom baud rate

On Thu, May 03, 2018 at 06:09:13PM +0530, Muni Sekhar wrote:
> Hi All,
>
> From include/asm-generic/termbits.h , I see baudrate can be one of the
> standard values: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
> 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000,
> 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000,
> 3500000, 4000000.
>
> If I need to set a custom baud rates(e.g. 14400, 128000, 256000), does
> Linux serial framework has any supporting method?

See the setserial man page:t

https://linux.die.net/man/8/setserial

Not all serial devices support the spd_cust and divisor, however. In
general, only devices where the kernel directly programs the
8250/16450/16550 UART directly will support this feature.

- Ted

2018-05-03 18:30:15

by Grant Edwards

[permalink] [raw]
Subject: Re: serial: custom baud rate

On 2018-05-03, Muni Sekhar <[email protected]> wrote:

> If I need to set a custom baud rates(e.g. 14400, 128000, 256000), does
> Linux serial framework has any supporting method?

Sure, use the termios2 structure instead of the termios structure:

#include <linux/termios.h>

struct termios2 t;

ioctl(fd, TCGETS2, &t)

t.c_cflag &= ~CBAUD;
t.c_cflag |= BOTHER;
t.c_ispeed = baud;
t.c_ospeed = baud;

ioctl(fd, TCSETS2, &t)

[Not all devices/drivers support termios2]

--
Grant Edwards grant.b.edwards Yow! Are we live or on
at tape?
gmail.com


2018-05-04 09:05:27

by Muni Sekhar

[permalink] [raw]
Subject: Re: serial: custom baud rate

On Thu, May 3, 2018 at 11:24 PM, Theodore Y. Ts'o <[email protected]> wrote:
> On Thu, May 03, 2018 at 06:09:13PM +0530, Muni Sekhar wrote:
>> Hi All,
>>
>> From include/asm-generic/termbits.h , I see baudrate can be one of the
>> standard values: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
>> 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000,
>> 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000,
>> 3500000, 4000000.
>>
>> If I need to set a custom baud rates(e.g. 14400, 128000, 256000), does
>> Linux serial framework has any supporting method?
>
> See the setserial man page:t
>
> https://linux.die.net/man/8/setserial
>
> Not all serial devices support the spd_cust and divisor, however. In
> general, only devices where the kernel directly programs the
> 8250/16450/16550 UART directly will support this feature.
>
So custom baud's can be set via TIOCSSERIAL IOCtl in kernel mode?
> - Ted



--
Thanks,
Sekhar

2018-05-04 14:02:03

by Theodore Ts'o

[permalink] [raw]
Subject: Re: serial: custom baud rate

On Fri, May 04, 2018 at 02:34:51PM +0530, Muni Sekhar wrote:
> > See the setserial man page:t
> >
> > https://linux.die.net/man/8/setserial
> >
> > Not all serial devices support the spd_cust and divisor, however. In
> > general, only devices where the kernel directly programs the
> > 8250/16450/16550 UART directly will support this feature.
> >
> So custom baud's can be set via TIOCSSERIAL IOCtl in kernel mode?

ioctl's are in general designed for use in userspace. So are the
termios interface.

- Ted

2018-05-13 19:57:58

by Alan Cox

[permalink] [raw]
Subject: Re: serial: custom baud rate

On Thu, 3 May 2018 18:27:14 +0000 (UTC)
Grant Edwards <[email protected]> wrote:

> On 2018-05-03, Muni Sekhar <[email protected]> wrote:
>
> > If I need to set a custom baud rates(e.g. 14400, 128000, 256000), does
> > Linux serial framework has any supporting method?
>
> Sure, use the termios2 structure instead of the termios structure:
>
> #include <linux/termios.h>
>
> struct termios2 t;
>
> ioctl(fd, TCGETS2, &t)
>
> t.c_cflag &= ~CBAUD;
> t.c_cflag |= BOTHER;
> t.c_ispeed = baud;
> t.c_ospeed = baud;
>
> ioctl(fd, TCSETS2, &t)
>
> [Not all devices/drivers support termios2]

That shouldn't be true - all devices get passed ispeed/ospeed and
everything in tree was using the correct fields as far as I could tell
last time I checked this

Alan

2018-05-14 17:20:47

by Grant Edwards

[permalink] [raw]
Subject: Re: serial: custom baud rate

On 2018-05-13, Alan Cox <[email protected]> wrote:
> On Thu, 3 May 2018 18:27:14 +0000 (UTC)
> Grant Edwards <[email protected]> wrote:
>
>> On 2018-05-03, Muni Sekhar <[email protected]> wrote:
>>
>> > If I need to set a custom baud rates(e.g. 14400, 128000, 256000), does
>> > Linux serial framework has any supporting method?
>>
>> Sure, use the termios2 structure instead of the termios structure:
>>
>> #include <linux/termios.h>
>>
>> struct termios2 t;
>>
>> ioctl(fd, TCGETS2, &t)
>>
>> t.c_cflag &= ~CBAUD;
>> t.c_cflag |= BOTHER;
>> t.c_ispeed = baud;
>> t.c_ospeed = baud;
>>
>> ioctl(fd, TCSETS2, &t)
>>
>> [Not all devices/drivers support termios2]
>
> That shouldn't be true - all devices get passed ispeed/ospeed and
> everything in tree was using the correct fields as far as I could
> tell last time I checked this

Yep, my mistake. It looks like the driver where I last ran into this
was an old out-of-tree driver.

--
Grant Edwards grant.b.edwards Yow! Mary Tyler Moore's
at SEVENTH HUSBAND is wearing
gmail.com my DACRON TANK TOP in a
cheap hotel in HONOLULU!


2018-05-14 17:40:37

by Andy Shevchenko

[permalink] [raw]
Subject: Re: serial: custom baud rate

On Thu, May 3, 2018 at 8:54 PM, Theodore Y. Ts'o <[email protected]> wrote:
> On Thu, May 03, 2018 at 06:09:13PM +0530, Muni Sekhar wrote:

> See the setserial man page:t
>
> https://linux.die.net/man/8/setserial
>
> Not all serial devices support the spd_cust and divisor, however. In
> general

Oh, please, do not advertise this old hack.

We have BOTHER that what people should use in new code.

--
With Best Regards,
Andy Shevchenko