blob: 2d19d397fb7264984c222439796053cf7f2f4349 [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 Bolivar7fabc8842014-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 Bolivar7fabc8842014-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;
46 u32 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;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -080058 u8 version_major;
59 u8 version_minor;
60 unsigned int ctrlin; /* input control lines */
61 unsigned int ctrlout; /* output control lines */
Bryan O'Donoghue11fca142015-06-02 13:40:45 +010062 struct gb_tty_line_coding line_coding;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080063};
64
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080065static struct tty_driver *gb_tty_driver;
66static DEFINE_IDR(tty_minors);
67static DEFINE_MUTEX(table_lock);
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +080068static atomic_t reference_count = ATOMIC_INIT(0);
69
Viresh Kumar36e79de2015-01-21 18:12:36 +053070/* Define get_version() routine */
71define_get_version(gb_tty, UART);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -080072
Bryan O'Donoghue1c087012015-06-02 13:40:50 +010073static int gb_uart_request_recv(u8 type, struct gb_operation *op)
74{
75 struct gb_connection *connection = op->connection;
76 struct gb_tty *gb_tty = connection->private;
77 struct gb_message *request = op->request;
78 struct gb_uart_recv_data_request *receive_data;
79 struct gb_uart_serial_state_request *serial_state;
80 struct tty_port *port = &gb_tty->port;
81 int count;
82 int ret = 0;
83
84 switch (type) {
85 case GB_UART_TYPE_RECEIVE_DATA:
86 receive_data = request->payload;
87 count = gb_tty->buffer_payload_max - sizeof(*receive_data);
88 if (!receive_data->size || receive_data->size > count)
89 return -EINVAL;
90
91 count = tty_insert_flip_string(port, receive_data->data,
92 receive_data->size);
93 if (count != receive_data->size) {
94 dev_err(&connection->dev,
95 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
96 receive_data->size, count);
97 }
98 if (count)
99 tty_flip_buffer_push(port);
100 break;
101 case GB_UART_TYPE_SERIAL_STATE:
102 serial_state = request->payload;
103 /* TODO: Parse state change and translate to tty API. */
104 break;
105 default:
106 dev_err(&connection->dev,
107 "unsupported unsolicited request: %02x\n", type);
108 ret = -EINVAL;
109 }
110
111 return ret;
112}
113
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800114static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
115{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800116 struct gb_uart_send_data_request *request;
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100117 int ret;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800118
119 if (!data || !size)
120 return 0;
121
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100122 if (size > tty->buffer_payload_max)
123 size = tty->buffer_payload_max;
124 request = tty->buffer;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800125 request->size = cpu_to_le16(size);
126 memcpy(&request->data[0], data, size);
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100127 ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
128 request, sizeof(*request) + size, NULL, 0);
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100129 if (ret)
130 return ret;
131 else
132 return size;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800133}
134
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800135static int send_line_coding(struct gb_tty *tty)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800136{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800137 struct gb_uart_set_line_coding_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800138
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100139 memcpy(&request, &tty->line_coding,
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800140 sizeof(tty->line_coding));
Viresh Kumar530430b2015-01-21 18:12:35 +0530141 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800142 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800143}
144
145static int send_control(struct gb_tty *tty, u16 control)
146{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800147 struct gb_uart_set_control_line_state_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800148
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800149 request.control = cpu_to_le16(control);
150 return gb_operation_sync(tty->connection,
Viresh Kumar530430b2015-01-21 18:12:35 +0530151 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800152 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800153}
154
155static int send_break(struct gb_tty *tty, u8 state)
156{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800157 struct gb_uart_set_break_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800158
159 if ((state != 0) && (state != 1)) {
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800160 dev_err(&tty->connection->dev,
161 "invalid break state of %d\n", state);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800162 return -EINVAL;
163 }
164
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800165 request.state = state;
Viresh Kumar530430b2015-01-21 18:12:35 +0530166 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_BREAK,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800167 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800168}
169
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800170
171static struct gb_tty *get_gb_by_minor(unsigned minor)
172{
173 struct gb_tty *gb_tty;
174
175 mutex_lock(&table_lock);
176 gb_tty = idr_find(&tty_minors, minor);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800177 if (gb_tty) {
178 mutex_lock(&gb_tty->mutex);
179 if (gb_tty->disconnected) {
180 mutex_unlock(&gb_tty->mutex);
181 gb_tty = NULL;
182 } else {
183 tty_port_get(&gb_tty->port);
184 mutex_unlock(&gb_tty->mutex);
185 }
186 }
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800187 mutex_unlock(&table_lock);
188 return gb_tty;
189}
190
191static int alloc_minor(struct gb_tty *gb_tty)
192{
193 int minor;
194
195 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500196 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800197 mutex_unlock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500198 if (minor >= 0)
199 gb_tty->minor = minor;
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800200 return minor;
201}
202
203static void release_minor(struct gb_tty *gb_tty)
204{
Alex Elderff5f0b32014-08-18 18:25:11 -0500205 int minor = gb_tty->minor;
206
207 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800208 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500209 idr_remove(&tty_minors, minor);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800210 mutex_unlock(&table_lock);
211}
212
213static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
214{
215 struct gb_tty *gb_tty;
216 int retval;
217
218 gb_tty = get_gb_by_minor(tty->index);
219 if (!gb_tty)
220 return -ENODEV;
221
222 retval = tty_standard_install(driver, tty);
223 if (retval)
224 goto error;
225
226 tty->driver_data = gb_tty;
227 return 0;
228error:
229 tty_port_put(&gb_tty->port);
230 return retval;
231}
232
233static int gb_tty_open(struct tty_struct *tty, struct file *file)
234{
235 struct gb_tty *gb_tty = tty->driver_data;
236
237 return tty_port_open(&gb_tty->port, tty, file);
238}
239
240static void gb_tty_close(struct tty_struct *tty, struct file *file)
241{
242 struct gb_tty *gb_tty = tty->driver_data;
243
244 tty_port_close(&gb_tty->port, tty, file);
245}
246
247static void gb_tty_cleanup(struct tty_struct *tty)
248{
249 struct gb_tty *gb_tty = tty->driver_data;
250
251 tty_port_put(&gb_tty->port);
252}
253
254static void gb_tty_hangup(struct tty_struct *tty)
255{
256 struct gb_tty *gb_tty = tty->driver_data;
257
258 tty_port_hangup(&gb_tty->port);
259}
260
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800261static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
262 int count)
263{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800264 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800265
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800266 return send_data(gb_tty, count, buf);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800267}
268
269static int gb_tty_write_room(struct tty_struct *tty)
270{
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100271 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800272
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100273 return gb_tty->buffer_payload_max;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800274}
275
276static int gb_tty_chars_in_buffer(struct tty_struct *tty)
277{
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800278 return 0;
279}
280
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800281static int gb_tty_break_ctl(struct tty_struct *tty, int state)
282{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800283 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800284
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800285 return send_break(gb_tty, state ? 1 : 0);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800286}
287
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800288static void gb_tty_set_termios(struct tty_struct *tty,
289 struct ktermios *termios_old)
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800290{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800291 struct gb_tty *gb_tty = tty->driver_data;
292 struct ktermios *termios = &tty->termios;
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100293 struct gb_tty_line_coding newline;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800294 int newctrl = gb_tty->ctrlout;
295
296 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
297 newline.format = termios->c_cflag & CSTOPB ? 2 : 0;
298 newline.parity = termios->c_cflag & PARENB ?
299 (termios->c_cflag & PARODD ? 1 : 2) +
300 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
301
302 switch (termios->c_cflag & CSIZE) {
303 case CS5:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100304 newline.data_bits = 5;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800305 break;
306 case CS6:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100307 newline.data_bits = 6;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800308 break;
309 case CS7:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100310 newline.data_bits = 7;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800311 break;
312 case CS8:
313 default:
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100314 newline.data_bits = 8;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800315 break;
316 }
317
318 /* FIXME: needs to clear unsupported bits in the termios */
319 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
320
321 if (C_BAUD(tty) == B0) {
322 newline.rate = gb_tty->line_coding.rate;
323 newctrl &= GB_UART_CTRL_DTR;
324 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
325 newctrl |= GB_UART_CTRL_DTR;
326 }
327
328 if (newctrl != gb_tty->ctrlout) {
329 gb_tty->ctrlout = newctrl;
330 send_control(gb_tty, newctrl);
331 }
332
333 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
334 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800335 send_line_coding(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800336 }
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800337}
338
339static int gb_tty_tiocmget(struct tty_struct *tty)
340{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800341 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800342
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800343 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
344 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
345 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
346 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
347 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
348 TIOCM_CTS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800349}
350
351static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
352 unsigned int clear)
353{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800354 struct gb_tty *gb_tty = tty->driver_data;
355 unsigned int newctrl = gb_tty->ctrlout;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800356
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800357 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
358 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
359 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
360 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
361
362 newctrl = (newctrl & ~clear) | set;
363 if (gb_tty->ctrlout == newctrl)
364 return 0;
365
366 gb_tty->ctrlout = newctrl;
367 return send_control(gb_tty, newctrl);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800368}
369
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800370static void gb_tty_throttle(struct tty_struct *tty)
371{
372 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800373 unsigned char stop_char;
374 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800375
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800376 if (I_IXOFF(tty)) {
377 stop_char = STOP_CHAR(tty);
378 retval = gb_tty_write(tty, &stop_char, 1);
379 if (retval <= 0)
380 return;
381 }
382
383 if (tty->termios.c_cflag & CRTSCTS) {
384 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
385 retval = send_control(gb_tty, gb_tty->ctrlout);
386 }
387
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800388}
389
390static void gb_tty_unthrottle(struct tty_struct *tty)
391{
392 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800393 unsigned char start_char;
394 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800395
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800396 if (I_IXOFF(tty)) {
397 start_char = START_CHAR(tty);
398 retval = gb_tty_write(tty, &start_char, 1);
399 if (retval <= 0)
400 return;
401 }
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800402
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800403 if (tty->termios.c_cflag & CRTSCTS) {
404 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
405 retval = send_control(gb_tty, gb_tty->ctrlout);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800406 }
407}
408
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800409static int get_serial_info(struct gb_tty *gb_tty,
410 struct serial_struct __user *info)
411{
412 struct serial_struct tmp;
413
414 if (!info)
415 return -EINVAL;
416
417 memset(&tmp, 0, sizeof(tmp));
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800418 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
419 tmp.type = PORT_16550A;
420 tmp.line = gb_tty->minor;
421 tmp.xmit_fifo_size = 16;
422 tmp.baud_base = 9600;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800423 tmp.close_delay = gb_tty->port.close_delay / 10;
424 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
425 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
426
427 if (copy_to_user(info, &tmp, sizeof(tmp)))
428 return -EFAULT;
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700429 return 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800430}
431
432static int set_serial_info(struct gb_tty *gb_tty,
433 struct serial_struct __user *newinfo)
434{
435 struct serial_struct new_serial;
436 unsigned int closing_wait;
437 unsigned int close_delay;
Alex Elder69f93ab2014-09-22 18:53:02 -0500438 int retval = 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800439
440 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
441 return -EFAULT;
442
443 close_delay = new_serial.close_delay * 10;
444 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
445 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
446
447 mutex_lock(&gb_tty->port.mutex);
448 if (!capable(CAP_SYS_ADMIN)) {
449 if ((close_delay != gb_tty->port.close_delay) ||
450 (closing_wait != gb_tty->port.closing_wait))
451 retval = -EPERM;
452 else
453 retval = -EOPNOTSUPP;
454 } else {
455 gb_tty->port.close_delay = close_delay;
456 gb_tty->port.closing_wait = closing_wait;
457 }
458 mutex_unlock(&gb_tty->port.mutex);
459 return retval;
460}
461
462static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
463{
464 int retval = 0;
465 DECLARE_WAITQUEUE(wait, current);
466 struct async_icount old;
467 struct async_icount new;
468
Alex Eldercaaa8a82014-08-18 18:25:12 -0500469 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800470 return -EINVAL;
471
472 do {
473 spin_lock_irq(&gb_tty->read_lock);
474 old = gb_tty->oldcount;
475 new = gb_tty->iocount;
476 gb_tty->oldcount = new;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500477 spin_unlock_irq(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800478
479 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
480 break;
481 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
482 break;
483 if ((arg & TIOCM_RI) && (old.rng != new.rng))
484 break;
485
486 add_wait_queue(&gb_tty->wioctl, &wait);
487 set_current_state(TASK_INTERRUPTIBLE);
488 schedule();
489 remove_wait_queue(&gb_tty->wioctl, &wait);
490 if (gb_tty->disconnected) {
491 if (arg & TIOCM_CD)
492 break;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500493 retval = -ENODEV;
494 } else if (signal_pending(current)) {
495 retval = -ERESTARTSYS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800496 }
497 } while (!retval);
498
499 return retval;
500}
501
502static int get_serial_usage(struct gb_tty *gb_tty,
503 struct serial_icounter_struct __user *count)
504{
505 struct serial_icounter_struct icount;
506 int retval = 0;
507
508 memset(&icount, 0, sizeof(icount));
509 icount.dsr = gb_tty->iocount.dsr;
510 icount.rng = gb_tty->iocount.rng;
511 icount.dcd = gb_tty->iocount.dcd;
512 icount.frame = gb_tty->iocount.frame;
513 icount.overrun = gb_tty->iocount.overrun;
514 icount.parity = gb_tty->iocount.parity;
515 icount.brk = gb_tty->iocount.brk;
516
517 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
518 retval = -EFAULT;
519
520 return retval;
521}
522
523static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
524 unsigned long arg)
525{
526 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800527
528 switch (cmd) {
529 case TIOCGSERIAL:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700530 return get_serial_info(gb_tty,
531 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800532 case TIOCSSERIAL:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700533 return set_serial_info(gb_tty,
534 (struct serial_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800535 case TIOCMIWAIT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700536 return wait_serial_change(gb_tty, arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800537 case TIOCGICOUNT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700538 return get_serial_usage(gb_tty,
539 (struct serial_icounter_struct __user *)arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800540 }
541
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700542 return -ENOIOCTLCMD;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800543}
544
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800545
546static const struct tty_operations gb_ops = {
547 .install = gb_tty_install,
548 .open = gb_tty_open,
549 .close = gb_tty_close,
550 .cleanup = gb_tty_cleanup,
551 .hangup = gb_tty_hangup,
552 .write = gb_tty_write,
553 .write_room = gb_tty_write_room,
554 .ioctl = gb_tty_ioctl,
555 .throttle = gb_tty_throttle,
556 .unthrottle = gb_tty_unthrottle,
557 .chars_in_buffer = gb_tty_chars_in_buffer,
558 .break_ctl = gb_tty_break_ctl,
559 .set_termios = gb_tty_set_termios,
560 .tiocmget = gb_tty_tiocmget,
561 .tiocmset = gb_tty_tiocmset,
562};
563
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100564static struct tty_port_operations null_ops = { };
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800565
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800566static int gb_tty_init(void);
567static void gb_tty_exit(void);
568
Greg Kroah-Hartman059b0932014-10-28 09:49:33 +0800569static int gb_uart_connection_init(struct gb_connection *connection)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800570{
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800571 struct gb_tty *gb_tty;
572 struct device *tty_dev;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800573 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800574 int minor;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800575
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800576 /* First time here, initialize the tty structures */
577 if (atomic_inc_return(&reference_count) == 1) {
578 retval = gb_tty_init();
579 if (retval) {
580 atomic_dec(&reference_count);
581 return retval;
582 }
583 }
584
Greg Kroah-Hartman8bf23e82014-08-30 17:18:04 -0700585 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800586 if (!gb_tty)
587 return -ENOMEM;
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100588
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100589 gb_tty->buffer_payload_max =
590 gb_operation_get_payload_size_max(connection);
591 if (!gb_tty->buffer_payload_max) {
592 kfree(gb_tty);
593 return -EINVAL;
594 }
595
596 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
597 if (!gb_tty->buffer) {
598 kfree(gb_tty);
599 return -ENOMEM;
600 }
601
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800602 gb_tty->connection = connection;
Alex Elder93bbe852014-12-03 12:27:42 -0600603 connection->private = gb_tty;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800604
605 /* Check for compatible protocol version */
606 retval = get_version(gb_tty);
607 if (retval)
608 goto error_version;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800609
610 minor = alloc_minor(gb_tty);
Alex Elderff5f0b32014-08-18 18:25:11 -0500611 if (minor < 0) {
612 if (minor == -ENOSPC) {
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800613 dev_err(&connection->dev,
614 "no more free minor numbers\n");
Phong Tran55f22912015-06-01 22:19:45 +0700615 retval = -ENODEV;
616 goto error_version;
Alex Elderff5f0b32014-08-18 18:25:11 -0500617 }
Phong Tran55f22912015-06-01 22:19:45 +0700618 retval = minor;
619 goto error_version;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800620 }
621
622 gb_tty->minor = minor;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800623 spin_lock_init(&gb_tty->write_lock);
624 spin_lock_init(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800625 init_waitqueue_head(&gb_tty->wioctl);
626 mutex_init(&gb_tty->mutex);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800627
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100628 tty_port_init(&gb_tty->port);
629 gb_tty->port.ops = &null_ops;
630
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800631 send_control(gb_tty, gb_tty->ctrlout);
632
633 /* initialize the uart to be 9600n81 */
634 gb_tty->line_coding.rate = cpu_to_le32(9600);
635 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
636 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100637 gb_tty->line_coding.data_bits = 8;
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800638 send_line_coding(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800639
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800640 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800641 &connection->dev);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800642 if (IS_ERR(tty_dev)) {
643 retval = PTR_ERR(tty_dev);
644 goto error;
645 }
646
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800647 return 0;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800648error:
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100649 tty_port_destroy(&gb_tty->port);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800650 release_minor(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800651error_version:
652 connection->private = NULL;
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100653 kfree(gb_tty->buffer);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800654 kfree(gb_tty);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800655 return retval;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800656}
657
Greg Kroah-Hartman059b0932014-10-28 09:49:33 +0800658static void gb_uart_connection_exit(struct gb_connection *connection)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800659{
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800660 struct gb_tty *gb_tty = connection->private;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800661 struct tty_struct *tty;
662
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800663 if (!gb_tty)
664 return;
665
666 mutex_lock(&gb_tty->mutex);
667 gb_tty->disconnected = true;
668
669 wake_up_all(&gb_tty->wioctl);
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800670 connection->private = NULL;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800671 mutex_unlock(&gb_tty->mutex);
672
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800673 tty = tty_port_tty_get(&gb_tty->port);
674 if (tty) {
675 tty_vhangup(tty);
676 tty_kref_put(tty);
677 }
678 /* FIXME - stop all traffic */
679
680 tty_unregister_device(gb_tty_driver, gb_tty->minor);
681
Viresh Kumar696e0cc2014-11-21 11:26:30 +0530682 /* FIXME - free transmit / receive buffers */
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800683
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800684 tty_port_put(&gb_tty->port);
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100685 tty_port_destroy(&gb_tty->port);
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100686 kfree(gb_tty->buffer);
Greg Kroah-Hartmane5f167f2014-08-30 17:11:41 -0700687 kfree(gb_tty);
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800688
689 /* If last device is gone, tear down the tty structures */
690 if (atomic_dec_return(&reference_count) == 0)
691 gb_tty_exit();
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800692}
693
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800694static int gb_tty_init(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800695{
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400696 int retval = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800697
Marti Bolivarf8089c02014-09-05 23:56:09 -0400698 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400699 if (IS_ERR(gb_tty_driver)) {
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700700 pr_err("Can not allocate tty driver\n");
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400701 retval = -ENOMEM;
702 goto fail_unregister_dev;
703 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800704
705 gb_tty_driver->driver_name = "gb";
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400706 gb_tty_driver->name = GB_NAME;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700707 gb_tty_driver->major = 0;
708 gb_tty_driver->minor_start = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800709 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
710 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
711 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
712 gb_tty_driver->init_termios = tty_std_termios;
713 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
714 tty_set_operations(gb_tty_driver, &gb_ops);
715
716 retval = tty_register_driver(gb_tty_driver);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700717 if (retval) {
718 pr_err("Can not register tty driver: %d\n", retval);
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400719 goto fail_put_gb_tty;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700720 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800721
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400722 return 0;
723
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700724fail_put_gb_tty:
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400725 put_tty_driver(gb_tty_driver);
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700726fail_unregister_dev:
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800727 return retval;
728}
729
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800730static void gb_tty_exit(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800731{
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400732 int major = MAJOR(gb_tty_driver->major);
733 int minor = gb_tty_driver->minor_start;
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700734
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800735 tty_unregister_driver(gb_tty_driver);
736 put_tty_driver(gb_tty_driver);
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400737 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800738}
Alex Elder3689f972014-10-27 06:04:30 -0500739
Alex Elder19d03de2014-11-05 16:12:53 -0600740static struct gb_protocol uart_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800741 .name = "uart",
Alex Elder19d03de2014-11-05 16:12:53 -0600742 .id = GREYBUS_PROTOCOL_UART,
743 .major = 0,
744 .minor = 1,
Alex Elder5d9fd7e2014-11-05 16:12:54 -0600745 .connection_init = gb_uart_connection_init,
746 .connection_exit = gb_uart_connection_exit,
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100747 .request_recv = gb_uart_request_recv,
Alex Elder19d03de2014-11-05 16:12:53 -0600748};
749
Viresh Kumarbdac5992015-05-20 17:20:10 +0530750gb_gpbridge_protocol_driver(uart_protocol);