blob: 52cc9d581ca4921118ff800b5a4b5f53b0cdba9d [file] [log] [blame]
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08001/*
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +08002 * UART driver for the Greybus "generic" UART module.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08003 *
4 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06005 * Copyright 2014 Linaro Ltd.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08006 *
7 * Released under the GPLv2 only.
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +08008 *
9 * Heavily based on drivers/usb/class/cdc-acm.c and
10 * drivers/usb/serial/usb-serial.c.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080011 */
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080013
14#include <linux/kernel.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080015#include <linux/errno.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080016#include <linux/module.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080017#include <linux/sched.h>
18#include <linux/wait.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080019#include <linux/slab.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080020#include <linux/uaccess.h>
21#include <linux/mutex.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080022#include <linux/tty.h>
23#include <linux/serial.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080026#include <linux/serial.h>
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080027#include <linux/idr.h>
Marti Bolivar7fabc882014-09-05 23:56:10 -040028#include <linux/fs.h>
29#include <linux/kdev_t.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050030
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080031#include "greybus.h"
32
Bryan O'Donoghuef5537d42015-06-02 13:40:46 +010033#define GB_NUM_MINORS 16 /* 16 is is more than enough */
Marti Bolivar7fabc882014-09-05 23:56:10 -040034#define GB_NAME "ttyGB"
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080035
Bryan O'Donoghue11fca142015-06-02 13:40:45 +010036struct gb_tty_line_coding {
37 __le32 rate;
38 __u8 format;
39 __u8 parity;
40 __u8 data_bits;
41};
42
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080043struct gb_tty {
44 struct tty_port port;
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +010045 void *buffer;
Greg Kroah-Hartman7f29ade2016-02-22 18:14:46 -080046 size_t buffer_payload_max;
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +080047 struct gb_connection *connection;
Alex Elder1cfc6672014-09-30 19:25:21 -050048 u16 cport_id;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080049 unsigned int minor;
50 unsigned char clocal;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080051 bool disconnected;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +080052 spinlock_t read_lock;
53 spinlock_t write_lock;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080054 struct async_icount iocount;
55 struct async_icount oldcount;
56 wait_queue_head_t wioctl;
57 struct mutex mutex;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +010058 u8 ctrlin; /* input control lines */
59 u8 ctrlout; /* output control lines */
Bryan O'Donoghue11fca142015-06-02 13:40:45 +010060 struct gb_tty_line_coding line_coding;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080061};
62
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080063static struct tty_driver *gb_tty_driver;
64static DEFINE_IDR(tty_minors);
65static DEFINE_MUTEX(table_lock);
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +080066static atomic_t reference_count = ATOMIC_INIT(0);
67
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +010068static int gb_uart_receive_data(struct gb_tty *gb_tty,
69 struct gb_connection *connection,
70 struct gb_uart_recv_data_request *receive_data)
71{
72 struct tty_port *port = &gb_tty->port;
73 u16 recv_data_size;
74 int count;
75 unsigned long tty_flags = TTY_NORMAL;
76
77 count = gb_tty->buffer_payload_max - sizeof(*receive_data);
78 recv_data_size = le16_to_cpu(receive_data->size);
79 if (!recv_data_size || recv_data_size > count)
80 return -EINVAL;
81
82 if (receive_data->flags) {
83 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
84 tty_flags = TTY_BREAK;
85 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
86 tty_flags = TTY_PARITY;
87 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
88 tty_flags = TTY_FRAME;
89
90 /* overrun is special, not associated with a char */
91 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
92 tty_insert_flip_char(port, 0, TTY_OVERRUN);
93 }
94 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
95 tty_flags, recv_data_size);
96 if (count != recv_data_size) {
Greg Kroah-Hartman4f30bf32015-10-14 11:15:12 -070097 dev_err(&connection->bundle->dev,
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +010098 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
99 recv_data_size, count);
100 }
101 if (count)
102 tty_flip_buffer_push(port);
103 return 0;
104}
105
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100106static int gb_uart_request_recv(u8 type, struct gb_operation *op)
107{
108 struct gb_connection *connection = op->connection;
109 struct gb_tty *gb_tty = connection->private;
110 struct gb_message *request = op->request;
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100111 struct gb_uart_serial_state_request *serial_state;
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100112 int ret = 0;
113
114 switch (type) {
115 case GB_UART_TYPE_RECEIVE_DATA:
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +0100116 ret = gb_uart_receive_data(gb_tty, connection,
117 request->payload);
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100118 break;
119 case GB_UART_TYPE_SERIAL_STATE:
120 serial_state = request->payload;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100121 gb_tty->ctrlin = serial_state->control;
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100122 break;
123 default:
Greg Kroah-Hartman4f30bf32015-10-14 11:15:12 -0700124 dev_err(&connection->bundle->dev,
Viresh Kumarb933fa42015-12-04 21:30:10 +0530125 "unsupported unsolicited request: 0x%02x\n", type);
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100126 ret = -EINVAL;
127 }
128
129 return ret;
130}
131
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800132static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
133{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800134 struct gb_uart_send_data_request *request;
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100135 int ret;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800136
137 if (!data || !size)
138 return 0;
139
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100140 if (size > tty->buffer_payload_max)
141 size = tty->buffer_payload_max;
142 request = tty->buffer;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800143 request->size = cpu_to_le16(size);
144 memcpy(&request->data[0], data, size);
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100145 ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
146 request, sizeof(*request) + size, NULL, 0);
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100147 if (ret)
148 return ret;
149 else
150 return size;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800151}
152
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800153static int send_line_coding(struct gb_tty *tty)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800154{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800155 struct gb_uart_set_line_coding_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800156
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100157 memcpy(&request, &tty->line_coding,
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800158 sizeof(tty->line_coding));
Viresh Kumar530430b2015-01-21 18:12:35 +0530159 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800160 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800161}
162
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100163static int send_control(struct gb_tty *gb_tty, u8 control)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800164{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800165 struct gb_uart_set_control_line_state_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800166
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100167 request.control = control;
Bryan O'Donoghue62229a12015-06-02 13:40:51 +0100168 return gb_operation_sync(gb_tty->connection,
Viresh Kumar530430b2015-01-21 18:12:35 +0530169 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800170 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800171}
172
Bryan O'Donoghue62229a12015-06-02 13:40:51 +0100173static int send_break(struct gb_tty *gb_tty, u8 state)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800174{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800175 struct gb_uart_set_break_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800176
177 if ((state != 0) && (state != 1)) {
Greg Kroah-Hartman4f30bf32015-10-14 11:15:12 -0700178 dev_err(&gb_tty->connection->bundle->dev,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800179 "invalid break state of %d\n", state);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800180 return -EINVAL;
181 }
182
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800183 request.state = state;
Bryan O'Donoghuea5192032015-07-14 02:10:18 +0100184 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800185 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800186}
187
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800188
189static struct gb_tty *get_gb_by_minor(unsigned minor)
190{
191 struct gb_tty *gb_tty;
192
193 mutex_lock(&table_lock);
194 gb_tty = idr_find(&tty_minors, minor);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800195 if (gb_tty) {
196 mutex_lock(&gb_tty->mutex);
197 if (gb_tty->disconnected) {
198 mutex_unlock(&gb_tty->mutex);
199 gb_tty = NULL;
200 } else {
201 tty_port_get(&gb_tty->port);
202 mutex_unlock(&gb_tty->mutex);
203 }
204 }
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800205 mutex_unlock(&table_lock);
206 return gb_tty;
207}
208
209static int alloc_minor(struct gb_tty *gb_tty)
210{
211 int minor;
212
213 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500214 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800215 mutex_unlock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500216 if (minor >= 0)
217 gb_tty->minor = minor;
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800218 return minor;
219}
220
221static void release_minor(struct gb_tty *gb_tty)
222{
Alex Elderff5f0b32014-08-18 18:25:11 -0500223 int minor = gb_tty->minor;
224
225 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800226 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500227 idr_remove(&tty_minors, minor);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800228 mutex_unlock(&table_lock);
229}
230
231static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
232{
233 struct gb_tty *gb_tty;
234 int retval;
235
236 gb_tty = get_gb_by_minor(tty->index);
237 if (!gb_tty)
238 return -ENODEV;
239
240 retval = tty_standard_install(driver, tty);
241 if (retval)
242 goto error;
243
244 tty->driver_data = gb_tty;
245 return 0;
246error:
247 tty_port_put(&gb_tty->port);
248 return retval;
249}
250
251static int gb_tty_open(struct tty_struct *tty, struct file *file)
252{
253 struct gb_tty *gb_tty = tty->driver_data;
254
255 return tty_port_open(&gb_tty->port, tty, file);
256}
257
258static void gb_tty_close(struct tty_struct *tty, struct file *file)
259{
260 struct gb_tty *gb_tty = tty->driver_data;
261
262 tty_port_close(&gb_tty->port, tty, file);
263}
264
265static void gb_tty_cleanup(struct tty_struct *tty)
266{
267 struct gb_tty *gb_tty = tty->driver_data;
268
269 tty_port_put(&gb_tty->port);
270}
271
272static void gb_tty_hangup(struct tty_struct *tty)
273{
274 struct gb_tty *gb_tty = tty->driver_data;
275
276 tty_port_hangup(&gb_tty->port);
277}
278
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800279static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
280 int count)
281{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800282 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800283
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800284 return send_data(gb_tty, count, buf);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800285}
286
287static int gb_tty_write_room(struct tty_struct *tty)
288{
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100289 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800290
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100291 return gb_tty->buffer_payload_max;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800292}
293
294static int gb_tty_chars_in_buffer(struct tty_struct *tty)
295{
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800296 return 0;
297}
298
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800299static int gb_tty_break_ctl(struct tty_struct *tty, int state)
300{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800301 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800302
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800303 return send_break(gb_tty, state ? 1 : 0);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800304}
305
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800306static void gb_tty_set_termios(struct tty_struct *tty,
307 struct ktermios *termios_old)
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800308{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800309 struct gb_tty *gb_tty = tty->driver_data;
310 struct ktermios *termios = &tty->termios;
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100311 struct gb_tty_line_coding newline;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100312 u8 newctrl = gb_tty->ctrlout;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800313
314 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
Bryan O'Donoghue62229a12015-06-02 13:40:51 +0100315 newline.format = termios->c_cflag & CSTOPB ?
316 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800317 newline.parity = termios->c_cflag & PARENB ?
318 (termios->c_cflag & PARODD ? 1 : 2) +
319 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
320
321 switch (termios->c_cflag & CSIZE) {
322 case CS5:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100323 newline.data_bits = 5;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800324 break;
325 case CS6:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100326 newline.data_bits = 6;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800327 break;
328 case CS7:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100329 newline.data_bits = 7;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800330 break;
331 case CS8:
332 default:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100333 newline.data_bits = 8;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800334 break;
335 }
336
337 /* FIXME: needs to clear unsupported bits in the termios */
338 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
339
340 if (C_BAUD(tty) == B0) {
341 newline.rate = gb_tty->line_coding.rate;
342 newctrl &= GB_UART_CTRL_DTR;
343 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
344 newctrl |= GB_UART_CTRL_DTR;
345 }
346
347 if (newctrl != gb_tty->ctrlout) {
348 gb_tty->ctrlout = newctrl;
349 send_control(gb_tty, newctrl);
350 }
351
352 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
353 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800354 send_line_coding(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800355 }
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800356}
357
358static int gb_tty_tiocmget(struct tty_struct *tty)
359{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800360 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800361
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800362 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
363 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
364 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
365 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
366 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
367 TIOCM_CTS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800368}
369
370static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
371 unsigned int clear)
372{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800373 struct gb_tty *gb_tty = tty->driver_data;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100374 u8 newctrl = gb_tty->ctrlout;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800375
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800376 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
377 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
378 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
379 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
380
381 newctrl = (newctrl & ~clear) | set;
382 if (gb_tty->ctrlout == newctrl)
383 return 0;
384
385 gb_tty->ctrlout = newctrl;
386 return send_control(gb_tty, newctrl);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800387}
388
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800389static void gb_tty_throttle(struct tty_struct *tty)
390{
391 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800392 unsigned char stop_char;
393 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800394
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800395 if (I_IXOFF(tty)) {
396 stop_char = STOP_CHAR(tty);
397 retval = gb_tty_write(tty, &stop_char, 1);
398 if (retval <= 0)
399 return;
400 }
401
402 if (tty->termios.c_cflag & CRTSCTS) {
403 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
404 retval = send_control(gb_tty, gb_tty->ctrlout);
405 }
406
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800407}
408
409static void gb_tty_unthrottle(struct tty_struct *tty)
410{
411 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800412 unsigned char start_char;
413 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800414
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800415 if (I_IXOFF(tty)) {
416 start_char = START_CHAR(tty);
417 retval = gb_tty_write(tty, &start_char, 1);
418 if (retval <= 0)
419 return;
420 }
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800421
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800422 if (tty->termios.c_cflag & CRTSCTS) {
423 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
424 retval = send_control(gb_tty, gb_tty->ctrlout);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800425 }
426}
427
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800428static int get_serial_info(struct gb_tty *gb_tty,
429 struct serial_struct __user *info)
430{
431 struct serial_struct tmp;
432
433 if (!info)
434 return -EINVAL;
435
436 memset(&tmp, 0, sizeof(tmp));
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800437 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
438 tmp.type = PORT_16550A;
439 tmp.line = gb_tty->minor;
440 tmp.xmit_fifo_size = 16;
441 tmp.baud_base = 9600;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800442 tmp.close_delay = gb_tty->port.close_delay / 10;
443 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
444 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
445
446 if (copy_to_user(info, &tmp, sizeof(tmp)))
447 return -EFAULT;
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700448 return 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800449}
450
451static int set_serial_info(struct gb_tty *gb_tty,
452 struct serial_struct __user *newinfo)
453{
454 struct serial_struct new_serial;
455 unsigned int closing_wait;
456 unsigned int close_delay;
Alex Elder69f93ab2014-09-22 18:53:02 -0500457 int retval = 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800458
459 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
460 return -EFAULT;
461
462 close_delay = new_serial.close_delay * 10;
463 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
464 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
465
466 mutex_lock(&gb_tty->port.mutex);
467 if (!capable(CAP_SYS_ADMIN)) {
468 if ((close_delay != gb_tty->port.close_delay) ||
469 (closing_wait != gb_tty->port.closing_wait))
470 retval = -EPERM;
471 else
472 retval = -EOPNOTSUPP;
473 } else {
474 gb_tty->port.close_delay = close_delay;
475 gb_tty->port.closing_wait = closing_wait;
476 }
477 mutex_unlock(&gb_tty->port.mutex);
478 return retval;
479}
480
481static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
482{
483 int retval = 0;
484 DECLARE_WAITQUEUE(wait, current);
485 struct async_icount old;
486 struct async_icount new;
487
Alex Eldercaaa8a82014-08-18 18:25:12 -0500488 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800489 return -EINVAL;
490
491 do {
492 spin_lock_irq(&gb_tty->read_lock);
493 old = gb_tty->oldcount;
494 new = gb_tty->iocount;
495 gb_tty->oldcount = new;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500496 spin_unlock_irq(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800497
498 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
499 break;
500 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
501 break;
502 if ((arg & TIOCM_RI) && (old.rng != new.rng))
503 break;
504
505 add_wait_queue(&gb_tty->wioctl, &wait);
506 set_current_state(TASK_INTERRUPTIBLE);
507 schedule();
508 remove_wait_queue(&gb_tty->wioctl, &wait);
509 if (gb_tty->disconnected) {
510 if (arg & TIOCM_CD)
511 break;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500512 retval = -ENODEV;
513 } else if (signal_pending(current)) {
514 retval = -ERESTARTSYS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800515 }
516 } while (!retval);
517
518 return retval;
519}
520
521static int get_serial_usage(struct gb_tty *gb_tty,
522 struct serial_icounter_struct __user *count)
523{
524 struct serial_icounter_struct icount;
525 int retval = 0;
526
527 memset(&icount, 0, sizeof(icount));
528 icount.dsr = gb_tty->iocount.dsr;
529 icount.rng = gb_tty->iocount.rng;
530 icount.dcd = gb_tty->iocount.dcd;
531 icount.frame = gb_tty->iocount.frame;
532 icount.overrun = gb_tty->iocount.overrun;
533 icount.parity = gb_tty->iocount.parity;
534 icount.brk = gb_tty->iocount.brk;
535
536 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
537 retval = -EFAULT;
538
539 return retval;
540}
541
542static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
543 unsigned long arg)
544{
545 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800546
547 switch (cmd) {
548 case TIOCGSERIAL:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700549 return get_serial_info(gb_tty,
550 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800551 case TIOCSSERIAL:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700552 return set_serial_info(gb_tty,
553 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800554 case TIOCMIWAIT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700555 return wait_serial_change(gb_tty, arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800556 case TIOCGICOUNT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700557 return get_serial_usage(gb_tty,
558 (struct serial_icounter_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800559 }
560
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700561 return -ENOIOCTLCMD;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800562}
563
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800564
565static const struct tty_operations gb_ops = {
566 .install = gb_tty_install,
567 .open = gb_tty_open,
568 .close = gb_tty_close,
569 .cleanup = gb_tty_cleanup,
570 .hangup = gb_tty_hangup,
571 .write = gb_tty_write,
572 .write_room = gb_tty_write_room,
573 .ioctl = gb_tty_ioctl,
574 .throttle = gb_tty_throttle,
575 .unthrottle = gb_tty_unthrottle,
576 .chars_in_buffer = gb_tty_chars_in_buffer,
577 .break_ctl = gb_tty_break_ctl,
578 .set_termios = gb_tty_set_termios,
579 .tiocmget = gb_tty_tiocmget,
580 .tiocmset = gb_tty_tiocmset,
581};
582
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100583static struct tty_port_operations null_ops = { };
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800584
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800585static int gb_tty_init(void);
586static void gb_tty_exit(void);
587
Greg Kroah-Hartman059b0932014-10-28 09:49:33 +0800588static int gb_uart_connection_init(struct gb_connection *connection)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800589{
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800590 struct gb_tty *gb_tty;
591 struct device *tty_dev;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800592 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800593 int minor;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800594
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800595 /* First time here, initialize the tty structures */
596 if (atomic_inc_return(&reference_count) == 1) {
597 retval = gb_tty_init();
598 if (retval) {
599 atomic_dec(&reference_count);
600 return retval;
601 }
602 }
603
Greg Kroah-Hartman8bf23e82014-08-30 17:18:04 -0700604 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
Phong Tran55a8e352015-06-10 21:03:17 +0700605 if (!gb_tty) {
606 retval = -ENOMEM;
607 goto error_alloc;
608 }
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100609
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100610 gb_tty->buffer_payload_max =
Greg Kroah-Hartman7f29ade2016-02-22 18:14:46 -0800611 gb_operation_get_payload_size_max(connection) -
612 sizeof(struct gb_uart_send_data_request);
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100613
614 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
615 if (!gb_tty->buffer) {
Phong Tran55a8e352015-06-10 21:03:17 +0700616 retval = -ENOMEM;
617 goto error_payload;
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100618 }
619
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800620 gb_tty->connection = connection;
Alex Elder93bbe852014-12-03 12:27:42 -0600621 connection->private = gb_tty;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800622
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800623 minor = alloc_minor(gb_tty);
Alex Elderff5f0b32014-08-18 18:25:11 -0500624 if (minor < 0) {
625 if (minor == -ENOSPC) {
Greg Kroah-Hartman4f30bf32015-10-14 11:15:12 -0700626 dev_err(&connection->bundle->dev,
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800627 "no more free minor numbers\n");
Phong Tran55f22912015-06-01 22:19:45 +0700628 retval = -ENODEV;
Viresh Kumara94e1442015-08-11 07:36:11 +0530629 goto error_minor;
Alex Elderff5f0b32014-08-18 18:25:11 -0500630 }
Phong Tran55f22912015-06-01 22:19:45 +0700631 retval = minor;
Viresh Kumara94e1442015-08-11 07:36:11 +0530632 goto error_minor;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800633 }
634
635 gb_tty->minor = minor;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800636 spin_lock_init(&gb_tty->write_lock);
637 spin_lock_init(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800638 init_waitqueue_head(&gb_tty->wioctl);
639 mutex_init(&gb_tty->mutex);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800640
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100641 tty_port_init(&gb_tty->port);
642 gb_tty->port.ops = &null_ops;
643
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800644 send_control(gb_tty, gb_tty->ctrlout);
645
646 /* initialize the uart to be 9600n81 */
647 gb_tty->line_coding.rate = cpu_to_le32(9600);
648 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
649 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100650 gb_tty->line_coding.data_bits = 8;
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800651 send_line_coding(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800652
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800653 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
Greg Kroah-Hartman4f30bf32015-10-14 11:15:12 -0700654 &connection->bundle->dev);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800655 if (IS_ERR(tty_dev)) {
656 retval = PTR_ERR(tty_dev);
657 goto error;
658 }
659
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800660 return 0;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800661error:
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100662 tty_port_destroy(&gb_tty->port);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800663 release_minor(gb_tty);
Viresh Kumara94e1442015-08-11 07:36:11 +0530664error_minor:
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800665 connection->private = NULL;
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100666 kfree(gb_tty->buffer);
Phong Tran55a8e352015-06-10 21:03:17 +0700667error_payload:
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800668 kfree(gb_tty);
Phong Tran55a8e352015-06-10 21:03:17 +0700669error_alloc:
670 if (atomic_dec_return(&reference_count) == 0)
671 gb_tty_exit();
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800672 return retval;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800673}
674
Greg Kroah-Hartman059b0932014-10-28 09:49:33 +0800675static void gb_uart_connection_exit(struct gb_connection *connection)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800676{
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800677 struct gb_tty *gb_tty = connection->private;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800678 struct tty_struct *tty;
679
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800680 if (!gb_tty)
681 return;
682
683 mutex_lock(&gb_tty->mutex);
684 gb_tty->disconnected = true;
685
686 wake_up_all(&gb_tty->wioctl);
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800687 connection->private = NULL;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800688 mutex_unlock(&gb_tty->mutex);
689
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800690 tty = tty_port_tty_get(&gb_tty->port);
691 if (tty) {
692 tty_vhangup(tty);
693 tty_kref_put(tty);
694 }
695 /* FIXME - stop all traffic */
696
697 tty_unregister_device(gb_tty_driver, gb_tty->minor);
698
Viresh Kumar696e0cc2014-11-21 11:26:30 +0530699 /* FIXME - free transmit / receive buffers */
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800700
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100701 tty_port_destroy(&gb_tty->port);
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100702 kfree(gb_tty->buffer);
Greg Kroah-Hartmane5f167f2014-08-30 17:11:41 -0700703 kfree(gb_tty);
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800704
705 /* If last device is gone, tear down the tty structures */
706 if (atomic_dec_return(&reference_count) == 0)
707 gb_tty_exit();
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800708}
709
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800710static int gb_tty_init(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800711{
Marti Bolivar7fabc882014-09-05 23:56:10 -0400712 int retval = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800713
Marti Bolivarf8089c02014-09-05 23:56:09 -0400714 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
Marti Bolivar7fabc882014-09-05 23:56:10 -0400715 if (IS_ERR(gb_tty_driver)) {
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700716 pr_err("Can not allocate tty driver\n");
Marti Bolivar7fabc882014-09-05 23:56:10 -0400717 retval = -ENOMEM;
718 goto fail_unregister_dev;
719 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800720
721 gb_tty_driver->driver_name = "gb";
Marti Bolivar7fabc882014-09-05 23:56:10 -0400722 gb_tty_driver->name = GB_NAME;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700723 gb_tty_driver->major = 0;
724 gb_tty_driver->minor_start = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800725 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
726 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
727 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
728 gb_tty_driver->init_termios = tty_std_termios;
729 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
730 tty_set_operations(gb_tty_driver, &gb_ops);
731
732 retval = tty_register_driver(gb_tty_driver);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700733 if (retval) {
734 pr_err("Can not register tty driver: %d\n", retval);
Marti Bolivar7fabc882014-09-05 23:56:10 -0400735 goto fail_put_gb_tty;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700736 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800737
Marti Bolivar7fabc882014-09-05 23:56:10 -0400738 return 0;
739
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700740fail_put_gb_tty:
Marti Bolivar7fabc882014-09-05 23:56:10 -0400741 put_tty_driver(gb_tty_driver);
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700742fail_unregister_dev:
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800743 return retval;
744}
745
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800746static void gb_tty_exit(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800747{
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800748 tty_unregister_driver(gb_tty_driver);
749 put_tty_driver(gb_tty_driver);
Greg Kroah-Hartman5c1ac692015-07-08 10:44:09 -0700750 idr_destroy(&tty_minors);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800751}
Alex Elder3689f972014-10-27 06:04:30 -0500752
Alex Elder19d03de2014-11-05 16:12:53 -0600753static struct gb_protocol uart_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800754 .name = "uart",
Alex Elder19d03de2014-11-05 16:12:53 -0600755 .id = GREYBUS_PROTOCOL_UART,
Viresh Kumar9475fb52015-08-08 10:25:39 +0530756 .major = GB_UART_VERSION_MAJOR,
757 .minor = GB_UART_VERSION_MINOR,
Alex Elder5d9fd7e2014-11-05 16:12:54 -0600758 .connection_init = gb_uart_connection_init,
759 .connection_exit = gb_uart_connection_exit,
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100760 .request_recv = gb_uart_request_recv,
Alex Elder19d03de2014-11-05 16:12:53 -0600761};
762
Viresh Kumare18822e2015-07-01 12:13:52 +0530763gb_builtin_protocol_driver(uart_protocol);