2014-08-15 08:01:23 +00:00
|
|
|
/*
|
2014-08-15 11:44:32 +00:00
|
|
|
* UART driver for the Greybus "generic" UART module.
|
2014-08-15 08:01:23 +00:00
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 18:08:42 +00:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-08-15 08:01:23 +00:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
2014-08-15 11:44:32 +00:00
|
|
|
*
|
|
|
|
* Heavily based on drivers/usb/class/cdc-acm.c and
|
|
|
|
* drivers/usb/serial/usb-serial.c.
|
2014-08-15 08:01:23 +00:00
|
|
|
*/
|
2014-09-13 23:15:52 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2014-08-15 08:01:23 +00:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2014-08-15 11:44:32 +00:00
|
|
|
#include <linux/errno.h>
|
2014-08-15 08:01:23 +00:00
|
|
|
#include <linux/module.h>
|
2014-08-15 11:44:32 +00:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/wait.h>
|
2014-08-15 08:01:23 +00:00
|
|
|
#include <linux/slab.h>
|
2014-08-15 11:44:32 +00:00
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/mutex.h>
|
2014-08-15 08:01:23 +00:00
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/serial.h>
|
|
|
|
#include <linux/tty_driver.h>
|
|
|
|
#include <linux/tty_flip.h>
|
2014-08-15 11:44:32 +00:00
|
|
|
#include <linux/serial.h>
|
2014-08-15 10:33:33 +00:00
|
|
|
#include <linux/idr.h>
|
2014-09-06 03:56:10 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/kdev_t.h>
|
2014-10-02 02:54:11 +00:00
|
|
|
|
2014-08-15 08:01:23 +00:00
|
|
|
#include "greybus.h"
|
2016-02-28 22:42:54 +00:00
|
|
|
#include "gpbridge.h"
|
2014-08-15 08:01:23 +00:00
|
|
|
|
2015-06-02 12:40:46 +00:00
|
|
|
#define GB_NUM_MINORS 16 /* 16 is is more than enough */
|
2014-09-06 03:56:10 +00:00
|
|
|
#define GB_NAME "ttyGB"
|
2014-08-15 08:01:23 +00:00
|
|
|
|
2015-06-02 12:40:45 +00:00
|
|
|
struct gb_tty_line_coding {
|
|
|
|
__le32 rate;
|
|
|
|
__u8 format;
|
|
|
|
__u8 parity;
|
|
|
|
__u8 data_bits;
|
|
|
|
};
|
|
|
|
|
2014-08-15 08:01:23 +00:00
|
|
|
struct gb_tty {
|
2016-05-05 09:02:30 +00:00
|
|
|
struct gpbridge_device *gpbdev;
|
2014-08-15 08:01:23 +00:00
|
|
|
struct tty_port port;
|
2015-06-02 12:40:49 +00:00
|
|
|
void *buffer;
|
2016-02-23 02:14:46 +00:00
|
|
|
size_t buffer_payload_max;
|
2014-10-27 09:32:34 +00:00
|
|
|
struct gb_connection *connection;
|
2014-10-01 00:25:21 +00:00
|
|
|
u16 cport_id;
|
2014-08-15 08:01:23 +00:00
|
|
|
unsigned int minor;
|
|
|
|
unsigned char clocal;
|
2014-08-15 11:44:32 +00:00
|
|
|
bool disconnected;
|
2014-08-15 10:54:11 +00:00
|
|
|
spinlock_t read_lock;
|
|
|
|
spinlock_t write_lock;
|
2014-08-15 11:44:32 +00:00
|
|
|
struct async_icount iocount;
|
|
|
|
struct async_icount oldcount;
|
|
|
|
wait_queue_head_t wioctl;
|
|
|
|
struct mutex mutex;
|
2015-06-29 17:09:15 +00:00
|
|
|
u8 ctrlin; /* input control lines */
|
|
|
|
u8 ctrlout; /* output control lines */
|
2015-06-02 12:40:45 +00:00
|
|
|
struct gb_tty_line_coding line_coding;
|
2014-08-15 08:01:23 +00:00
|
|
|
};
|
|
|
|
|
2014-08-15 10:33:33 +00:00
|
|
|
static struct tty_driver *gb_tty_driver;
|
|
|
|
static DEFINE_IDR(tty_minors);
|
|
|
|
static DEFINE_MUTEX(table_lock);
|
2014-10-27 09:42:45 +00:00
|
|
|
static atomic_t reference_count = ATOMIC_INIT(0);
|
|
|
|
|
2016-02-24 15:11:51 +00:00
|
|
|
static int gb_uart_receive_data_handler(struct gb_operation *op)
|
2015-06-29 17:09:14 +00:00
|
|
|
{
|
2016-02-24 15:11:51 +00:00
|
|
|
struct gb_connection *connection = op->connection;
|
2016-03-22 18:30:35 +00:00
|
|
|
struct gb_tty *gb_tty = gb_connection_get_data(connection);
|
2015-06-29 17:09:14 +00:00
|
|
|
struct tty_port *port = &gb_tty->port;
|
2016-02-24 15:11:51 +00:00
|
|
|
struct gb_message *request = op->request;
|
|
|
|
struct gb_uart_recv_data_request *receive_data;
|
2015-06-29 17:09:14 +00:00
|
|
|
u16 recv_data_size;
|
|
|
|
int count;
|
|
|
|
unsigned long tty_flags = TTY_NORMAL;
|
|
|
|
|
2016-02-24 15:11:51 +00:00
|
|
|
if (request->payload_size < sizeof(*receive_data)) {
|
2016-05-05 09:02:30 +00:00
|
|
|
dev_err(&gb_tty->gpbdev->dev,
|
2016-02-24 15:11:51 +00:00
|
|
|
"short receive-data request received (%zu < %zu)\n",
|
|
|
|
request->payload_size, sizeof(*receive_data));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
receive_data = op->request->payload;
|
2015-06-29 17:09:14 +00:00
|
|
|
recv_data_size = le16_to_cpu(receive_data->size);
|
2016-02-24 15:11:51 +00:00
|
|
|
|
|
|
|
if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
|
2016-05-05 09:02:30 +00:00
|
|
|
dev_err(&gb_tty->gpbdev->dev,
|
2016-02-24 15:11:51 +00:00
|
|
|
"malformed receive-data request received (%u != %zu)\n",
|
|
|
|
recv_data_size,
|
|
|
|
request->payload_size - sizeof(*receive_data));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recv_data_size)
|
2015-06-29 17:09:14 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (receive_data->flags) {
|
|
|
|
if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
|
|
|
|
tty_flags = TTY_BREAK;
|
|
|
|
else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
|
|
|
|
tty_flags = TTY_PARITY;
|
|
|
|
else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
|
|
|
|
tty_flags = TTY_FRAME;
|
|
|
|
|
|
|
|
/* overrun is special, not associated with a char */
|
|
|
|
if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
|
|
|
|
tty_insert_flip_char(port, 0, TTY_OVERRUN);
|
|
|
|
}
|
|
|
|
count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
|
|
|
|
tty_flags, recv_data_size);
|
|
|
|
if (count != recv_data_size) {
|
2016-05-05 09:02:30 +00:00
|
|
|
dev_err(&gb_tty->gpbdev->dev,
|
2015-06-29 17:09:14 +00:00
|
|
|
"UART: RX 0x%08x bytes only wrote 0x%08x\n",
|
|
|
|
recv_data_size, count);
|
|
|
|
}
|
|
|
|
if (count)
|
|
|
|
tty_flip_buffer_push(port);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-24 15:11:50 +00:00
|
|
|
static int gb_uart_serial_state_handler(struct gb_operation *op)
|
2015-06-02 12:40:50 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection = op->connection;
|
2016-03-22 18:30:35 +00:00
|
|
|
struct gb_tty *gb_tty = gb_connection_get_data(connection);
|
2015-06-02 12:40:50 +00:00
|
|
|
struct gb_message *request = op->request;
|
|
|
|
struct gb_uart_serial_state_request *serial_state;
|
2016-02-24 15:11:50 +00:00
|
|
|
|
|
|
|
if (request->payload_size < sizeof(*serial_state)) {
|
2016-05-05 09:02:30 +00:00
|
|
|
dev_err(&gb_tty->gpbdev->dev,
|
2016-02-24 15:11:50 +00:00
|
|
|
"short serial-state event received (%zu < %zu)\n",
|
|
|
|
request->payload_size, sizeof(*serial_state));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
serial_state = request->payload;
|
|
|
|
gb_tty->ctrlin = serial_state->control;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
static int gb_uart_request_handler(struct gb_operation *op)
|
2016-02-24 15:11:50 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection = op->connection;
|
2016-05-05 09:02:30 +00:00
|
|
|
struct gb_tty *gb_tty = gb_connection_get_data(connection);
|
|
|
|
int type = op->type;
|
2016-02-24 15:11:50 +00:00
|
|
|
int ret;
|
2015-06-02 12:40:50 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GB_UART_TYPE_RECEIVE_DATA:
|
2016-02-24 15:11:51 +00:00
|
|
|
ret = gb_uart_receive_data_handler(op);
|
2015-06-02 12:40:50 +00:00
|
|
|
break;
|
|
|
|
case GB_UART_TYPE_SERIAL_STATE:
|
2016-02-24 15:11:50 +00:00
|
|
|
ret = gb_uart_serial_state_handler(op);
|
2015-06-02 12:40:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-05-05 09:02:30 +00:00
|
|
|
dev_err(&gb_tty->gpbdev->dev,
|
2015-12-04 16:00:10 +00:00
|
|
|
"unsupported unsolicited request: 0x%02x\n", type);
|
2015-06-02 12:40:50 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
|
|
|
|
{
|
|
|
|
struct gb_uart_send_data_request *request;
|
2015-06-02 12:40:48 +00:00
|
|
|
int ret;
|
2014-11-16 02:30:00 +00:00
|
|
|
|
|
|
|
if (!data || !size)
|
|
|
|
return 0;
|
|
|
|
|
2015-06-02 12:40:49 +00:00
|
|
|
if (size > tty->buffer_payload_max)
|
|
|
|
size = tty->buffer_payload_max;
|
|
|
|
request = tty->buffer;
|
2014-11-16 02:30:00 +00:00
|
|
|
request->size = cpu_to_le16(size);
|
|
|
|
memcpy(&request->data[0], data, size);
|
2015-06-02 12:40:48 +00:00
|
|
|
ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
|
|
|
|
request, sizeof(*request) + size, NULL, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return size;
|
2014-11-16 02:30:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:52:25 +00:00
|
|
|
static int send_line_coding(struct gb_tty *tty)
|
2014-11-16 02:30:00 +00:00
|
|
|
{
|
2014-11-24 01:45:21 +00:00
|
|
|
struct gb_uart_set_line_coding_request request;
|
2014-11-16 02:30:00 +00:00
|
|
|
|
2015-06-02 12:40:45 +00:00
|
|
|
memcpy(&request, &tty->line_coding,
|
2014-11-24 21:52:25 +00:00
|
|
|
sizeof(tty->line_coding));
|
2015-01-21 12:42:35 +00:00
|
|
|
return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
|
2014-11-24 01:45:21 +00:00
|
|
|
&request, sizeof(request), NULL, 0);
|
2014-11-16 02:30:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 17:09:15 +00:00
|
|
|
static int send_control(struct gb_tty *gb_tty, u8 control)
|
2014-11-16 02:30:00 +00:00
|
|
|
{
|
2014-11-24 01:45:21 +00:00
|
|
|
struct gb_uart_set_control_line_state_request request;
|
2014-11-16 02:30:00 +00:00
|
|
|
|
2015-06-29 17:09:15 +00:00
|
|
|
request.control = control;
|
2015-06-02 12:40:51 +00:00
|
|
|
return gb_operation_sync(gb_tty->connection,
|
2015-01-21 12:42:35 +00:00
|
|
|
GB_UART_TYPE_SET_CONTROL_LINE_STATE,
|
2014-11-24 01:45:21 +00:00
|
|
|
&request, sizeof(request), NULL, 0);
|
2014-11-16 02:30:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 12:40:51 +00:00
|
|
|
static int send_break(struct gb_tty *gb_tty, u8 state)
|
2014-11-16 02:30:00 +00:00
|
|
|
{
|
2014-11-24 01:45:21 +00:00
|
|
|
struct gb_uart_set_break_request request;
|
2014-11-16 02:30:00 +00:00
|
|
|
|
|
|
|
if ((state != 0) && (state != 1)) {
|
2016-05-05 09:02:30 +00:00
|
|
|
dev_err(&gb_tty->gpbdev->dev,
|
2014-11-24 01:45:21 +00:00
|
|
|
"invalid break state of %d\n", state);
|
2014-11-16 02:30:00 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:45:21 +00:00
|
|
|
request.state = state;
|
2015-07-14 01:10:18 +00:00
|
|
|
return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
|
2014-11-24 01:45:21 +00:00
|
|
|
&request, sizeof(request), NULL, 0);
|
2014-11-16 02:30:00 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 10:33:33 +00:00
|
|
|
|
|
|
|
static struct gb_tty *get_gb_by_minor(unsigned minor)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty;
|
|
|
|
|
|
|
|
mutex_lock(&table_lock);
|
|
|
|
gb_tty = idr_find(&tty_minors, minor);
|
2014-08-15 11:44:32 +00:00
|
|
|
if (gb_tty) {
|
|
|
|
mutex_lock(&gb_tty->mutex);
|
|
|
|
if (gb_tty->disconnected) {
|
|
|
|
mutex_unlock(&gb_tty->mutex);
|
|
|
|
gb_tty = NULL;
|
|
|
|
} else {
|
|
|
|
tty_port_get(&gb_tty->port);
|
|
|
|
mutex_unlock(&gb_tty->mutex);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 10:33:33 +00:00
|
|
|
mutex_unlock(&table_lock);
|
|
|
|
return gb_tty;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int alloc_minor(struct gb_tty *gb_tty)
|
|
|
|
{
|
|
|
|
int minor;
|
|
|
|
|
|
|
|
mutex_lock(&table_lock);
|
greybus: uart-gb: improve minor device number error checking
When alloc_minor() finds an available minor device number it
does not constrain the highest number desired. Instead, it
relies on its caller, tty_gb_probe() to see if the returned
number indicates all minor numbers have been exhausted.
There are a couple problems with this--or rather with this
code.
First, if an allocation is attempted *after* GB_NUM_MINORS
is returned, a new number greater than (but not equal to)
GB_NUM_MINORS will be allocated, and that won't produce
any error condition.
Second, alloc_minor() can return an error code (like -ENOMEM). And
its caller is only checking for GB_NUM_MINORS. If an error code
is returned, tty_gb_probe() simply uses it.
Change alloc_minor() so it requests minor device numbers in the
range 0..(GB_NUM_MINORS-1), and use an error return to detect
when the minor device numbers have been exhausted.
If alloc_minor() returns -ENOSPC (from idr_alloc()), translate that
to -ENODEV. The only other error we might see is -ENOMEM, and if
we get that, return it.
Finally, zero gb_tty->minor when it's released. (If this is
actually important a reserved value like GB_NUM_MINORS should
be used instead to signify a gb_tty with no minor assigned.)
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-08-18 23:25:11 +00:00
|
|
|
minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
|
2014-08-15 10:33:33 +00:00
|
|
|
mutex_unlock(&table_lock);
|
greybus: uart-gb: improve minor device number error checking
When alloc_minor() finds an available minor device number it
does not constrain the highest number desired. Instead, it
relies on its caller, tty_gb_probe() to see if the returned
number indicates all minor numbers have been exhausted.
There are a couple problems with this--or rather with this
code.
First, if an allocation is attempted *after* GB_NUM_MINORS
is returned, a new number greater than (but not equal to)
GB_NUM_MINORS will be allocated, and that won't produce
any error condition.
Second, alloc_minor() can return an error code (like -ENOMEM). And
its caller is only checking for GB_NUM_MINORS. If an error code
is returned, tty_gb_probe() simply uses it.
Change alloc_minor() so it requests minor device numbers in the
range 0..(GB_NUM_MINORS-1), and use an error return to detect
when the minor device numbers have been exhausted.
If alloc_minor() returns -ENOSPC (from idr_alloc()), translate that
to -ENODEV. The only other error we might see is -ENOMEM, and if
we get that, return it.
Finally, zero gb_tty->minor when it's released. (If this is
actually important a reserved value like GB_NUM_MINORS should
be used instead to signify a gb_tty with no minor assigned.)
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-08-18 23:25:11 +00:00
|
|
|
if (minor >= 0)
|
|
|
|
gb_tty->minor = minor;
|
2014-08-15 10:33:33 +00:00
|
|
|
return minor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_minor(struct gb_tty *gb_tty)
|
|
|
|
{
|
greybus: uart-gb: improve minor device number error checking
When alloc_minor() finds an available minor device number it
does not constrain the highest number desired. Instead, it
relies on its caller, tty_gb_probe() to see if the returned
number indicates all minor numbers have been exhausted.
There are a couple problems with this--or rather with this
code.
First, if an allocation is attempted *after* GB_NUM_MINORS
is returned, a new number greater than (but not equal to)
GB_NUM_MINORS will be allocated, and that won't produce
any error condition.
Second, alloc_minor() can return an error code (like -ENOMEM). And
its caller is only checking for GB_NUM_MINORS. If an error code
is returned, tty_gb_probe() simply uses it.
Change alloc_minor() so it requests minor device numbers in the
range 0..(GB_NUM_MINORS-1), and use an error return to detect
when the minor device numbers have been exhausted.
If alloc_minor() returns -ENOSPC (from idr_alloc()), translate that
to -ENODEV. The only other error we might see is -ENOMEM, and if
we get that, return it.
Finally, zero gb_tty->minor when it's released. (If this is
actually important a reserved value like GB_NUM_MINORS should
be used instead to signify a gb_tty with no minor assigned.)
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-08-18 23:25:11 +00:00
|
|
|
int minor = gb_tty->minor;
|
|
|
|
|
|
|
|
gb_tty->minor = 0; /* Maybe should use an invalid value instead */
|
2014-08-15 10:33:33 +00:00
|
|
|
mutex_lock(&table_lock);
|
greybus: uart-gb: improve minor device number error checking
When alloc_minor() finds an available minor device number it
does not constrain the highest number desired. Instead, it
relies on its caller, tty_gb_probe() to see if the returned
number indicates all minor numbers have been exhausted.
There are a couple problems with this--or rather with this
code.
First, if an allocation is attempted *after* GB_NUM_MINORS
is returned, a new number greater than (but not equal to)
GB_NUM_MINORS will be allocated, and that won't produce
any error condition.
Second, alloc_minor() can return an error code (like -ENOMEM). And
its caller is only checking for GB_NUM_MINORS. If an error code
is returned, tty_gb_probe() simply uses it.
Change alloc_minor() so it requests minor device numbers in the
range 0..(GB_NUM_MINORS-1), and use an error return to detect
when the minor device numbers have been exhausted.
If alloc_minor() returns -ENOSPC (from idr_alloc()), translate that
to -ENODEV. The only other error we might see is -ENOMEM, and if
we get that, return it.
Finally, zero gb_tty->minor when it's released. (If this is
actually important a reserved value like GB_NUM_MINORS should
be used instead to signify a gb_tty with no minor assigned.)
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-08-18 23:25:11 +00:00
|
|
|
idr_remove(&tty_minors, minor);
|
2014-08-15 10:33:33 +00:00
|
|
|
mutex_unlock(&table_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
gb_tty = get_gb_by_minor(tty->index);
|
|
|
|
if (!gb_tty)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
retval = tty_standard_install(driver, tty);
|
|
|
|
if (retval)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
tty->driver_data = gb_tty;
|
|
|
|
return 0;
|
|
|
|
error:
|
|
|
|
tty_port_put(&gb_tty->port);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_open(struct tty_struct *tty, struct file *file)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
|
|
|
|
|
|
|
return tty_port_open(&gb_tty->port, tty, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_tty_close(struct tty_struct *tty, struct file *file)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
|
|
|
|
|
|
|
tty_port_close(&gb_tty->port, tty, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_tty_cleanup(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
|
|
|
|
|
|
|
tty_port_put(&gb_tty->port);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_tty_hangup(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
|
|
|
|
|
|
|
tty_port_hangup(&gb_tty->port);
|
|
|
|
}
|
|
|
|
|
2014-08-15 10:54:11 +00:00
|
|
|
static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
|
|
|
|
int count)
|
|
|
|
{
|
2014-11-16 02:30:00 +00:00
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2014-08-15 10:54:11 +00:00
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
return send_data(gb_tty, count, buf);
|
2014-08-15 10:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_write_room(struct tty_struct *tty)
|
|
|
|
{
|
2015-06-02 12:40:49 +00:00
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2014-08-15 10:54:11 +00:00
|
|
|
|
2015-06-02 12:40:49 +00:00
|
|
|
return gb_tty->buffer_payload_max;
|
2014-08-15 10:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_chars_in_buffer(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-15 11:44:32 +00:00
|
|
|
static int gb_tty_break_ctl(struct tty_struct *tty, int state)
|
|
|
|
{
|
2014-11-16 02:30:00 +00:00
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2014-08-15 11:44:32 +00:00
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
return send_break(gb_tty, state ? 1 : 0);
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
static void gb_tty_set_termios(struct tty_struct *tty,
|
|
|
|
struct ktermios *termios_old)
|
2014-08-15 11:44:32 +00:00
|
|
|
{
|
2014-11-16 02:30:00 +00:00
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
|
|
|
struct ktermios *termios = &tty->termios;
|
2015-06-02 12:40:45 +00:00
|
|
|
struct gb_tty_line_coding newline;
|
2015-06-29 17:09:15 +00:00
|
|
|
u8 newctrl = gb_tty->ctrlout;
|
2014-11-16 02:30:00 +00:00
|
|
|
|
|
|
|
newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
|
2015-06-02 12:40:51 +00:00
|
|
|
newline.format = termios->c_cflag & CSTOPB ?
|
|
|
|
GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
|
2014-11-16 02:30:00 +00:00
|
|
|
newline.parity = termios->c_cflag & PARENB ?
|
|
|
|
(termios->c_cflag & PARODD ? 1 : 2) +
|
|
|
|
(termios->c_cflag & CMSPAR ? 2 : 0) : 0;
|
|
|
|
|
|
|
|
switch (termios->c_cflag & CSIZE) {
|
|
|
|
case CS5:
|
2015-06-02 12:40:45 +00:00
|
|
|
newline.data_bits = 5;
|
2014-11-16 02:30:00 +00:00
|
|
|
break;
|
|
|
|
case CS6:
|
2015-06-02 12:40:45 +00:00
|
|
|
newline.data_bits = 6;
|
2014-11-16 02:30:00 +00:00
|
|
|
break;
|
|
|
|
case CS7:
|
2015-06-02 12:40:45 +00:00
|
|
|
newline.data_bits = 7;
|
2014-11-16 02:30:00 +00:00
|
|
|
break;
|
|
|
|
case CS8:
|
|
|
|
default:
|
2015-06-02 12:40:45 +00:00
|
|
|
newline.data_bits = 8;
|
2014-11-16 02:30:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: needs to clear unsupported bits in the termios */
|
|
|
|
gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
|
|
|
|
|
|
|
|
if (C_BAUD(tty) == B0) {
|
|
|
|
newline.rate = gb_tty->line_coding.rate;
|
|
|
|
newctrl &= GB_UART_CTRL_DTR;
|
|
|
|
} else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
|
|
|
|
newctrl |= GB_UART_CTRL_DTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newctrl != gb_tty->ctrlout) {
|
|
|
|
gb_tty->ctrlout = newctrl;
|
|
|
|
send_control(gb_tty, newctrl);
|
|
|
|
}
|
|
|
|
|
2016-04-20 18:47:20 +00:00
|
|
|
if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
|
2014-11-16 02:30:00 +00:00
|
|
|
memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
|
2014-11-24 21:52:25 +00:00
|
|
|
send_line_coding(gb_tty);
|
2014-11-16 02:30:00 +00:00
|
|
|
}
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_tiocmget(struct tty_struct *tty)
|
|
|
|
{
|
2014-11-16 02:30:00 +00:00
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2014-08-15 11:44:32 +00:00
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
|
|
|
|
(gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
|
|
|
|
(gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
|
|
|
|
(gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
|
|
|
|
(gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
|
|
|
|
TIOCM_CTS;
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
|
|
|
|
unsigned int clear)
|
|
|
|
{
|
2014-11-16 02:30:00 +00:00
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2015-06-29 17:09:15 +00:00
|
|
|
u8 newctrl = gb_tty->ctrlout;
|
2014-08-15 11:44:32 +00:00
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
|
|
|
|
(set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
|
|
|
|
clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
|
|
|
|
(clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
|
|
|
|
|
|
|
|
newctrl = (newctrl & ~clear) | set;
|
|
|
|
if (gb_tty->ctrlout == newctrl)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
gb_tty->ctrlout = newctrl;
|
|
|
|
return send_control(gb_tty, newctrl);
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 10:54:11 +00:00
|
|
|
static void gb_tty_throttle(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2014-11-19 22:23:00 +00:00
|
|
|
unsigned char stop_char;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (I_IXOFF(tty)) {
|
|
|
|
stop_char = STOP_CHAR(tty);
|
|
|
|
retval = gb_tty_write(tty, &stop_char, 1);
|
|
|
|
if (retval <= 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tty->termios.c_cflag & CRTSCTS) {
|
|
|
|
gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
|
|
|
|
retval = send_control(gb_tty, gb_tty->ctrlout);
|
|
|
|
}
|
2014-08-15 10:54:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_tty_unthrottle(struct tty_struct *tty)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
2014-11-19 22:23:00 +00:00
|
|
|
unsigned char start_char;
|
|
|
|
int retval;
|
2014-08-15 10:54:11 +00:00
|
|
|
|
2014-11-19 22:23:00 +00:00
|
|
|
if (I_IXOFF(tty)) {
|
|
|
|
start_char = START_CHAR(tty);
|
|
|
|
retval = gb_tty_write(tty, &start_char, 1);
|
|
|
|
if (retval <= 0)
|
|
|
|
return;
|
|
|
|
}
|
2014-08-15 10:54:11 +00:00
|
|
|
|
2014-11-19 22:23:00 +00:00
|
|
|
if (tty->termios.c_cflag & CRTSCTS) {
|
|
|
|
gb_tty->ctrlout |= GB_UART_CTRL_RTS;
|
|
|
|
retval = send_control(gb_tty, gb_tty->ctrlout);
|
2014-08-15 10:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 11:44:32 +00:00
|
|
|
static int get_serial_info(struct gb_tty *gb_tty,
|
|
|
|
struct serial_struct __user *info)
|
|
|
|
{
|
|
|
|
struct serial_struct tmp;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset(&tmp, 0, sizeof(tmp));
|
2014-11-19 22:23:00 +00:00
|
|
|
tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
|
|
|
|
tmp.type = PORT_16550A;
|
|
|
|
tmp.line = gb_tty->minor;
|
|
|
|
tmp.xmit_fifo_size = 16;
|
|
|
|
tmp.baud_base = 9600;
|
2014-08-15 11:44:32 +00:00
|
|
|
tmp.close_delay = gb_tty->port.close_delay / 10;
|
|
|
|
tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
|
|
|
|
ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
|
|
|
|
|
|
|
|
if (copy_to_user(info, &tmp, sizeof(tmp)))
|
|
|
|
return -EFAULT;
|
2014-09-02 02:10:06 +00:00
|
|
|
return 0;
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int set_serial_info(struct gb_tty *gb_tty,
|
|
|
|
struct serial_struct __user *newinfo)
|
|
|
|
{
|
|
|
|
struct serial_struct new_serial;
|
|
|
|
unsigned int closing_wait;
|
|
|
|
unsigned int close_delay;
|
2014-09-22 23:53:02 +00:00
|
|
|
int retval = 0;
|
2014-08-15 11:44:32 +00:00
|
|
|
|
|
|
|
if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
close_delay = new_serial.close_delay * 10;
|
|
|
|
closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
|
|
|
|
ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
|
|
|
|
|
|
|
|
mutex_lock(&gb_tty->port.mutex);
|
|
|
|
if (!capable(CAP_SYS_ADMIN)) {
|
|
|
|
if ((close_delay != gb_tty->port.close_delay) ||
|
|
|
|
(closing_wait != gb_tty->port.closing_wait))
|
|
|
|
retval = -EPERM;
|
|
|
|
else
|
|
|
|
retval = -EOPNOTSUPP;
|
|
|
|
} else {
|
|
|
|
gb_tty->port.close_delay = close_delay;
|
|
|
|
gb_tty->port.closing_wait = closing_wait;
|
|
|
|
}
|
|
|
|
mutex_unlock(&gb_tty->port.mutex);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
struct async_icount old;
|
|
|
|
struct async_icount new;
|
|
|
|
|
2014-08-18 23:25:12 +00:00
|
|
|
if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
|
2014-08-15 11:44:32 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
spin_lock_irq(&gb_tty->read_lock);
|
|
|
|
old = gb_tty->oldcount;
|
|
|
|
new = gb_tty->iocount;
|
|
|
|
gb_tty->oldcount = new;
|
2014-08-18 23:25:12 +00:00
|
|
|
spin_unlock_irq(&gb_tty->read_lock);
|
2014-08-15 11:44:32 +00:00
|
|
|
|
|
|
|
if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
|
|
|
|
break;
|
|
|
|
if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
|
|
|
|
break;
|
|
|
|
if ((arg & TIOCM_RI) && (old.rng != new.rng))
|
|
|
|
break;
|
|
|
|
|
|
|
|
add_wait_queue(&gb_tty->wioctl, &wait);
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
schedule();
|
|
|
|
remove_wait_queue(&gb_tty->wioctl, &wait);
|
|
|
|
if (gb_tty->disconnected) {
|
|
|
|
if (arg & TIOCM_CD)
|
|
|
|
break;
|
2014-08-18 23:25:12 +00:00
|
|
|
retval = -ENODEV;
|
|
|
|
} else if (signal_pending(current)) {
|
|
|
|
retval = -ERESTARTSYS;
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
} while (!retval);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_serial_usage(struct gb_tty *gb_tty,
|
|
|
|
struct serial_icounter_struct __user *count)
|
|
|
|
{
|
|
|
|
struct serial_icounter_struct icount;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
memset(&icount, 0, sizeof(icount));
|
|
|
|
icount.dsr = gb_tty->iocount.dsr;
|
|
|
|
icount.rng = gb_tty->iocount.rng;
|
|
|
|
icount.dcd = gb_tty->iocount.dcd;
|
|
|
|
icount.frame = gb_tty->iocount.frame;
|
|
|
|
icount.overrun = gb_tty->iocount.overrun;
|
|
|
|
icount.parity = gb_tty->iocount.parity;
|
|
|
|
icount.brk = gb_tty->iocount.brk;
|
|
|
|
|
|
|
|
if (copy_to_user(count, &icount, sizeof(icount)) > 0)
|
|
|
|
retval = -EFAULT;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
struct gb_tty *gb_tty = tty->driver_data;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case TIOCGSERIAL:
|
2014-08-30 23:20:22 +00:00
|
|
|
return get_serial_info(gb_tty,
|
|
|
|
(struct serial_struct __user *)arg);
|
2014-08-15 11:44:32 +00:00
|
|
|
case TIOCSSERIAL:
|
2014-08-30 23:20:22 +00:00
|
|
|
return set_serial_info(gb_tty,
|
|
|
|
(struct serial_struct __user *)arg);
|
2014-08-15 11:44:32 +00:00
|
|
|
case TIOCMIWAIT:
|
2014-08-30 23:20:22 +00:00
|
|
|
return wait_serial_change(gb_tty, arg);
|
2014-08-15 11:44:32 +00:00
|
|
|
case TIOCGICOUNT:
|
2014-08-30 23:20:22 +00:00
|
|
|
return get_serial_usage(gb_tty,
|
|
|
|
(struct serial_icounter_struct __user *)arg);
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 23:20:22 +00:00
|
|
|
return -ENOIOCTLCMD;
|
2014-08-15 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:01:23 +00:00
|
|
|
|
|
|
|
static const struct tty_operations gb_ops = {
|
|
|
|
.install = gb_tty_install,
|
|
|
|
.open = gb_tty_open,
|
|
|
|
.close = gb_tty_close,
|
|
|
|
.cleanup = gb_tty_cleanup,
|
|
|
|
.hangup = gb_tty_hangup,
|
|
|
|
.write = gb_tty_write,
|
|
|
|
.write_room = gb_tty_write_room,
|
|
|
|
.ioctl = gb_tty_ioctl,
|
|
|
|
.throttle = gb_tty_throttle,
|
|
|
|
.unthrottle = gb_tty_unthrottle,
|
|
|
|
.chars_in_buffer = gb_tty_chars_in_buffer,
|
|
|
|
.break_ctl = gb_tty_break_ctl,
|
|
|
|
.set_termios = gb_tty_set_termios,
|
|
|
|
.tiocmget = gb_tty_tiocmget,
|
|
|
|
.tiocmset = gb_tty_tiocmset,
|
|
|
|
};
|
|
|
|
|
2015-06-02 12:40:47 +00:00
|
|
|
static struct tty_port_operations null_ops = { };
|
2014-08-15 08:01:23 +00:00
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
static int gb_tty_init(void);
|
|
|
|
static void gb_tty_exit(void);
|
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
static int gb_uart_probe(struct gpbridge_device *gpbdev,
|
|
|
|
const struct gpbridge_device_id *id)
|
2014-08-15 08:01:23 +00:00
|
|
|
{
|
2016-05-05 09:02:30 +00:00
|
|
|
struct gb_connection *connection;
|
2016-02-24 15:11:49 +00:00
|
|
|
size_t max_payload;
|
2014-08-15 10:54:11 +00:00
|
|
|
struct gb_tty *gb_tty;
|
|
|
|
struct device *tty_dev;
|
2014-08-15 08:01:23 +00:00
|
|
|
int retval;
|
2014-08-15 10:54:11 +00:00
|
|
|
int minor;
|
|
|
|
|
2014-10-27 09:42:45 +00:00
|
|
|
/* First time here, initialize the tty structures */
|
|
|
|
if (atomic_inc_return(&reference_count) == 1) {
|
|
|
|
retval = gb_tty_init();
|
|
|
|
if (retval) {
|
|
|
|
atomic_dec(&reference_count);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 00:18:04 +00:00
|
|
|
gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
|
2015-06-10 14:03:17 +00:00
|
|
|
if (!gb_tty) {
|
|
|
|
retval = -ENOMEM;
|
2016-05-05 09:02:30 +00:00
|
|
|
goto exit_tty;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection = gb_connection_create(gpbdev->bundle,
|
|
|
|
le16_to_cpu(gpbdev->cport_desc->id),
|
|
|
|
gb_uart_request_handler);
|
|
|
|
if (IS_ERR(connection)) {
|
|
|
|
retval = PTR_ERR(connection);
|
|
|
|
goto exit_tty_free;
|
2015-06-10 14:03:17 +00:00
|
|
|
}
|
2015-06-02 12:40:47 +00:00
|
|
|
|
2016-02-24 15:11:49 +00:00
|
|
|
max_payload = gb_operation_get_payload_size_max(connection);
|
|
|
|
if (max_payload < sizeof(struct gb_uart_send_data_request)) {
|
|
|
|
retval = -EINVAL;
|
2016-05-05 09:02:30 +00:00
|
|
|
goto exit_connection_destroy;
|
2016-02-24 15:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gb_tty->buffer_payload_max = max_payload -
|
2016-02-23 02:14:46 +00:00
|
|
|
sizeof(struct gb_uart_send_data_request);
|
2015-06-02 12:40:49 +00:00
|
|
|
|
|
|
|
gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
|
|
|
|
if (!gb_tty->buffer) {
|
2015-06-10 14:03:17 +00:00
|
|
|
retval = -ENOMEM;
|
2016-05-05 09:02:30 +00:00
|
|
|
goto exit_connection_destroy;
|
2015-06-02 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 10:54:11 +00:00
|
|
|
minor = alloc_minor(gb_tty);
|
greybus: uart-gb: improve minor device number error checking
When alloc_minor() finds an available minor device number it
does not constrain the highest number desired. Instead, it
relies on its caller, tty_gb_probe() to see if the returned
number indicates all minor numbers have been exhausted.
There are a couple problems with this--or rather with this
code.
First, if an allocation is attempted *after* GB_NUM_MINORS
is returned, a new number greater than (but not equal to)
GB_NUM_MINORS will be allocated, and that won't produce
any error condition.
Second, alloc_minor() can return an error code (like -ENOMEM). And
its caller is only checking for GB_NUM_MINORS. If an error code
is returned, tty_gb_probe() simply uses it.
Change alloc_minor() so it requests minor device numbers in the
range 0..(GB_NUM_MINORS-1), and use an error return to detect
when the minor device numbers have been exhausted.
If alloc_minor() returns -ENOSPC (from idr_alloc()), translate that
to -ENODEV. The only other error we might see is -ENOMEM, and if
we get that, return it.
Finally, zero gb_tty->minor when it's released. (If this is
actually important a reserved value like GB_NUM_MINORS should
be used instead to signify a gb_tty with no minor assigned.)
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-08-18 23:25:11 +00:00
|
|
|
if (minor < 0) {
|
|
|
|
if (minor == -ENOSPC) {
|
2015-10-14 18:15:12 +00:00
|
|
|
dev_err(&connection->bundle->dev,
|
2014-11-16 02:30:00 +00:00
|
|
|
"no more free minor numbers\n");
|
2015-06-01 15:19:45 +00:00
|
|
|
retval = -ENODEV;
|
2016-05-05 09:02:30 +00:00
|
|
|
} else {
|
|
|
|
retval = minor;
|
greybus: uart-gb: improve minor device number error checking
When alloc_minor() finds an available minor device number it
does not constrain the highest number desired. Instead, it
relies on its caller, tty_gb_probe() to see if the returned
number indicates all minor numbers have been exhausted.
There are a couple problems with this--or rather with this
code.
First, if an allocation is attempted *after* GB_NUM_MINORS
is returned, a new number greater than (but not equal to)
GB_NUM_MINORS will be allocated, and that won't produce
any error condition.
Second, alloc_minor() can return an error code (like -ENOMEM). And
its caller is only checking for GB_NUM_MINORS. If an error code
is returned, tty_gb_probe() simply uses it.
Change alloc_minor() so it requests minor device numbers in the
range 0..(GB_NUM_MINORS-1), and use an error return to detect
when the minor device numbers have been exhausted.
If alloc_minor() returns -ENOSPC (from idr_alloc()), translate that
to -ENODEV. The only other error we might see is -ENOMEM, and if
we get that, return it.
Finally, zero gb_tty->minor when it's released. (If this is
actually important a reserved value like GB_NUM_MINORS should
be used instead to signify a gb_tty with no minor assigned.)
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-08-18 23:25:11 +00:00
|
|
|
}
|
2016-05-05 09:02:30 +00:00
|
|
|
goto exit_buf_free;
|
2014-08-15 10:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gb_tty->minor = minor;
|
|
|
|
spin_lock_init(&gb_tty->write_lock);
|
|
|
|
spin_lock_init(&gb_tty->read_lock);
|
2014-08-15 11:44:32 +00:00
|
|
|
init_waitqueue_head(&gb_tty->wioctl);
|
|
|
|
mutex_init(&gb_tty->mutex);
|
2014-08-15 10:54:11 +00:00
|
|
|
|
2015-06-02 12:40:47 +00:00
|
|
|
tty_port_init(&gb_tty->port);
|
|
|
|
gb_tty->port.ops = &null_ops;
|
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
gb_tty->connection = connection;
|
|
|
|
gb_tty->gpbdev = gpbdev;
|
|
|
|
gb_connection_set_data(connection, gb_tty);
|
|
|
|
gb_gpbridge_set_data(gpbdev, gb_tty);
|
|
|
|
|
|
|
|
retval = gb_connection_enable_tx(connection);
|
|
|
|
if (retval)
|
|
|
|
goto exit_release_minor;
|
|
|
|
|
|
|
|
retval = gb_gpbridge_get_version(connection);
|
|
|
|
if (retval)
|
|
|
|
goto exit_connection_disable;
|
|
|
|
|
2014-11-16 02:30:00 +00:00
|
|
|
send_control(gb_tty, gb_tty->ctrlout);
|
|
|
|
|
|
|
|
/* initialize the uart to be 9600n81 */
|
|
|
|
gb_tty->line_coding.rate = cpu_to_le32(9600);
|
|
|
|
gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
|
|
|
|
gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
|
2015-06-02 12:40:45 +00:00
|
|
|
gb_tty->line_coding.data_bits = 8;
|
2014-11-24 21:52:25 +00:00
|
|
|
send_line_coding(gb_tty);
|
2014-11-16 02:30:00 +00:00
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
retval = gb_connection_enable(connection);
|
|
|
|
if (retval)
|
|
|
|
goto exit_connection_disable;
|
|
|
|
|
2014-08-15 10:54:11 +00:00
|
|
|
tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
|
2016-05-05 09:02:30 +00:00
|
|
|
&gpbdev->dev);
|
2014-08-15 10:54:11 +00:00
|
|
|
if (IS_ERR(tty_dev)) {
|
|
|
|
retval = PTR_ERR(tty_dev);
|
2016-05-05 09:02:30 +00:00
|
|
|
goto exit_connection_disable;
|
2014-08-15 10:54:11 +00:00
|
|
|
}
|
2014-08-15 08:01:23 +00:00
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
|
2014-08-15 08:01:23 +00:00
|
|
|
return 0;
|
2016-05-05 09:02:30 +00:00
|
|
|
|
|
|
|
exit_connection_disable:
|
|
|
|
gb_connection_disable(connection);
|
|
|
|
exit_release_minor:
|
2014-08-15 10:54:11 +00:00
|
|
|
release_minor(gb_tty);
|
2016-05-05 09:02:30 +00:00
|
|
|
exit_buf_free:
|
2015-06-02 12:40:49 +00:00
|
|
|
kfree(gb_tty->buffer);
|
2016-05-05 09:02:30 +00:00
|
|
|
exit_connection_destroy:
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
exit_tty_free:
|
2014-11-16 02:30:00 +00:00
|
|
|
kfree(gb_tty);
|
2016-05-05 09:02:30 +00:00
|
|
|
exit_tty:
|
2015-06-10 14:03:17 +00:00
|
|
|
if (atomic_dec_return(&reference_count) == 0)
|
|
|
|
gb_tty_exit();
|
2016-05-05 09:02:30 +00:00
|
|
|
|
2014-08-15 10:54:11 +00:00
|
|
|
return retval;
|
2014-08-15 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
static void gb_uart_remove(struct gpbridge_device *gpbdev)
|
2014-08-15 08:01:23 +00:00
|
|
|
{
|
2016-05-05 09:02:30 +00:00
|
|
|
struct gb_tty *gb_tty = gb_gpbridge_get_data(gpbdev);
|
|
|
|
struct gb_connection *connection = gb_tty->connection;
|
2014-08-15 10:54:11 +00:00
|
|
|
struct tty_struct *tty;
|
|
|
|
|
2014-08-15 11:44:32 +00:00
|
|
|
mutex_lock(&gb_tty->mutex);
|
|
|
|
gb_tty->disconnected = true;
|
|
|
|
|
|
|
|
wake_up_all(&gb_tty->wioctl);
|
|
|
|
mutex_unlock(&gb_tty->mutex);
|
|
|
|
|
2014-08-15 10:54:11 +00:00
|
|
|
tty = tty_port_tty_get(&gb_tty->port);
|
|
|
|
if (tty) {
|
|
|
|
tty_vhangup(tty);
|
|
|
|
tty_kref_put(tty);
|
|
|
|
}
|
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
gb_connection_disable_rx(connection);
|
2014-08-15 10:54:11 +00:00
|
|
|
tty_unregister_device(gb_tty_driver, gb_tty->minor);
|
|
|
|
|
2014-11-21 05:56:30 +00:00
|
|
|
/* FIXME - free transmit / receive buffers */
|
2014-08-15 11:44:32 +00:00
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
gb_connection_disable(connection);
|
2015-06-02 12:40:47 +00:00
|
|
|
tty_port_destroy(&gb_tty->port);
|
2016-05-05 09:02:30 +00:00
|
|
|
gb_connection_destroy(connection);
|
2015-06-02 12:40:49 +00:00
|
|
|
kfree(gb_tty->buffer);
|
2014-08-31 00:11:41 +00:00
|
|
|
kfree(gb_tty);
|
2014-10-27 09:42:45 +00:00
|
|
|
|
|
|
|
/* If last device is gone, tear down the tty structures */
|
|
|
|
if (atomic_dec_return(&reference_count) == 0)
|
|
|
|
gb_tty_exit();
|
2014-08-15 08:01:23 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 09:42:45 +00:00
|
|
|
static int gb_tty_init(void)
|
2014-08-15 08:01:23 +00:00
|
|
|
{
|
2014-09-06 03:56:10 +00:00
|
|
|
int retval = 0;
|
2014-08-15 08:01:23 +00:00
|
|
|
|
2014-09-06 03:56:09 +00:00
|
|
|
gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
|
2014-09-06 03:56:10 +00:00
|
|
|
if (IS_ERR(gb_tty_driver)) {
|
2014-09-13 23:15:52 +00:00
|
|
|
pr_err("Can not allocate tty driver\n");
|
2014-09-06 03:56:10 +00:00
|
|
|
retval = -ENOMEM;
|
|
|
|
goto fail_unregister_dev;
|
|
|
|
}
|
2014-08-15 08:01:23 +00:00
|
|
|
|
|
|
|
gb_tty_driver->driver_name = "gb";
|
2014-09-06 03:56:10 +00:00
|
|
|
gb_tty_driver->name = GB_NAME;
|
2014-09-13 23:15:52 +00:00
|
|
|
gb_tty_driver->major = 0;
|
|
|
|
gb_tty_driver->minor_start = 0;
|
2014-08-15 08:01:23 +00:00
|
|
|
gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
|
|
|
|
gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
|
|
|
|
gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
|
|
|
|
gb_tty_driver->init_termios = tty_std_termios;
|
|
|
|
gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
|
|
|
|
tty_set_operations(gb_tty_driver, &gb_ops);
|
|
|
|
|
|
|
|
retval = tty_register_driver(gb_tty_driver);
|
2014-09-13 23:15:52 +00:00
|
|
|
if (retval) {
|
|
|
|
pr_err("Can not register tty driver: %d\n", retval);
|
2014-09-06 03:56:10 +00:00
|
|
|
goto fail_put_gb_tty;
|
2014-09-13 23:15:52 +00:00
|
|
|
}
|
2014-08-15 08:01:23 +00:00
|
|
|
|
2014-09-06 03:56:10 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-09-14 00:02:47 +00:00
|
|
|
fail_put_gb_tty:
|
2014-09-06 03:56:10 +00:00
|
|
|
put_tty_driver(gb_tty_driver);
|
2014-09-14 00:02:47 +00:00
|
|
|
fail_unregister_dev:
|
2014-08-15 08:01:23 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-10-27 09:42:45 +00:00
|
|
|
static void gb_tty_exit(void)
|
2014-08-15 08:01:23 +00:00
|
|
|
{
|
|
|
|
tty_unregister_driver(gb_tty_driver);
|
|
|
|
put_tty_driver(gb_tty_driver);
|
2015-07-08 17:44:09 +00:00
|
|
|
idr_destroy(&tty_minors);
|
2014-08-15 08:01:23 +00:00
|
|
|
}
|
2014-10-27 11:04:30 +00:00
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
static const struct gpbridge_device_id gb_uart_id_table[] = {
|
|
|
|
{ GPBRIDGE_PROTOCOL(GREYBUS_PROTOCOL_UART) },
|
|
|
|
{ },
|
2014-11-05 22:12:53 +00:00
|
|
|
};
|
|
|
|
|
2016-05-05 09:02:30 +00:00
|
|
|
static struct gpbridge_driver uart_driver = {
|
|
|
|
.name = "uart",
|
|
|
|
.probe = gb_uart_probe,
|
|
|
|
.remove = gb_uart_remove,
|
|
|
|
.id_table = gb_uart_id_table,
|
|
|
|
};
|
|
|
|
gb_gpbridge_builtin_driver(uart_driver);
|