blob: dc4ed0ff1ae277fc0041276f3340aecc6ec0059e [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08002/*
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +08003 * UART driver for the Greybus "generic" UART module.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08004 *
5 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06006 * Copyright 2014 Linaro Ltd.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +08007 *
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +08008 * Heavily based on drivers/usb/class/cdc-acm.c and
9 * drivers/usb/serial/usb-serial.c.
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080010 */
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080012
13#include <linux/kernel.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080014#include <linux/errno.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080015#include <linux/module.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010016#include <linux/sched/signal.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080017#include <linux/wait.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080018#include <linux/slab.h>
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080019#include <linux/uaccess.h>
20#include <linux/mutex.h>
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080021#include <linux/tty.h>
22#include <linux/serial.h>
23#include <linux/tty_driver.h>
24#include <linux/tty_flip.h>
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080025#include <linux/idr.h>
Marti Bolivar7fabc8842014-09-05 23:56:10 -040026#include <linux/fs.h>
27#include <linux/kdev_t.h>
Axel Haslam8d6fbe92016-05-31 14:36:10 +020028#include <linux/kfifo.h>
29#include <linux/workqueue.h>
Axel Haslam5dad5c32016-05-31 14:36:13 +020030#include <linux/completion.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020031#include <linux/greybus.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050032
Sandeep Patile54b1062016-05-19 08:52:39 -070033#include "gbphy.h"
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080034
simran singhal4452b092017-03-04 23:11:00 +053035#define GB_NUM_MINORS 16 /* 16 is more than enough */
Marti Bolivar7fabc8842014-09-05 23:56:10 -040036#define GB_NAME "ttyGB"
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080037
Axel Haslam8d6fbe92016-05-31 14:36:10 +020038#define GB_UART_WRITE_FIFO_SIZE PAGE_SIZE
39#define GB_UART_WRITE_ROOM_MARGIN 1 /* leave some space in fifo */
Axel Haslama8bc00f2016-05-31 14:36:11 +020040#define GB_UART_FIRMWARE_CREDITS 4096
Axel Haslam5dad5c32016-05-31 14:36:13 +020041#define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC 10000
Axel Haslam8d6fbe92016-05-31 14:36:10 +020042
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080043struct gb_tty {
Sandeep Patile54b1062016-05-19 08:52:39 -070044 struct gbphy_device *gbphy_dev;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080045 struct tty_port port;
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +010046 void *buffer;
Greg Kroah-Hartman7f29ade2016-02-22 18:14:46 -080047 size_t buffer_payload_max;
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +080048 struct gb_connection *connection;
Alex Elder1cfc6672014-09-30 19:25:21 -050049 u16 cport_id;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080050 unsigned int minor;
51 unsigned char clocal;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080052 bool disconnected;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +080053 spinlock_t read_lock;
54 spinlock_t write_lock;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +080055 struct async_icount iocount;
56 struct async_icount oldcount;
57 wait_queue_head_t wioctl;
58 struct mutex mutex;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +010059 u8 ctrlin; /* input control lines */
60 u8 ctrlout; /* output control lines */
Johan Hovold6064aee2020-05-14 09:05:48 +020061 struct gb_uart_set_line_coding_request line_coding;
Axel Haslam8d6fbe92016-05-31 14:36:10 +020062 struct work_struct tx_work;
63 struct kfifo write_fifo;
64 bool close_pending;
Axel Haslama8bc00f2016-05-31 14:36:11 +020065 unsigned int credits;
Axel Haslam5dad5c32016-05-31 14:36:13 +020066 struct completion credits_complete;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +080067};
68
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +080069static struct tty_driver *gb_tty_driver;
70static DEFINE_IDR(tty_minors);
71static DEFINE_MUTEX(table_lock);
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +080072
Johan Hovoldc5f338c2016-02-24 16:11:51 +010073static int gb_uart_receive_data_handler(struct gb_operation *op)
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +010074{
Johan Hovoldc5f338c2016-02-24 16:11:51 +010075 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -040076 struct gb_tty *gb_tty = gb_connection_get_data(connection);
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +010077 struct tty_port *port = &gb_tty->port;
Johan Hovoldc5f338c2016-02-24 16:11:51 +010078 struct gb_message *request = op->request;
79 struct gb_uart_recv_data_request *receive_data;
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +010080 u16 recv_data_size;
81 int count;
82 unsigned long tty_flags = TTY_NORMAL;
83
Johan Hovoldc5f338c2016-02-24 16:11:51 +010084 if (request->payload_size < sizeof(*receive_data)) {
Sandeep Patile54b1062016-05-19 08:52:39 -070085 dev_err(&gb_tty->gbphy_dev->dev,
Chaehyun Lim319b78c2016-09-22 10:56:44 +090086 "short receive-data request received (%zu < %zu)\n",
87 request->payload_size, sizeof(*receive_data));
Johan Hovoldc5f338c2016-02-24 16:11:51 +010088 return -EINVAL;
89 }
90
91 receive_data = op->request->payload;
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +010092 recv_data_size = le16_to_cpu(receive_data->size);
Johan Hovoldc5f338c2016-02-24 16:11:51 +010093
94 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
Sandeep Patile54b1062016-05-19 08:52:39 -070095 dev_err(&gb_tty->gbphy_dev->dev,
Chaehyun Lim319b78c2016-09-22 10:56:44 +090096 "malformed receive-data request received (%u != %zu)\n",
97 recv_data_size,
98 request->payload_size - sizeof(*receive_data));
Johan Hovoldc5f338c2016-02-24 16:11:51 +010099 return -EINVAL;
100 }
101
102 if (!recv_data_size)
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +0100103 return -EINVAL;
104
105 if (receive_data->flags) {
106 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
107 tty_flags = TTY_BREAK;
108 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
109 tty_flags = TTY_PARITY;
110 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
111 tty_flags = TTY_FRAME;
112
113 /* overrun is special, not associated with a char */
114 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
115 tty_insert_flip_char(port, 0, TTY_OVERRUN);
116 }
117 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
118 tty_flags, recv_data_size);
119 if (count != recv_data_size) {
Sandeep Patile54b1062016-05-19 08:52:39 -0700120 dev_err(&gb_tty->gbphy_dev->dev,
Bryan O'Donoghue4c025cf2015-06-29 18:09:14 +0100121 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
122 recv_data_size, count);
123 }
124 if (count)
125 tty_flip_buffer_push(port);
126 return 0;
127}
128
Johan Hovoldcb7f00b2016-02-24 16:11:50 +0100129static int gb_uart_serial_state_handler(struct gb_operation *op)
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100130{
131 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400132 struct gb_tty *gb_tty = gb_connection_get_data(connection);
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100133 struct gb_message *request = op->request;
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100134 struct gb_uart_serial_state_request *serial_state;
Johan Hovoldcb7f00b2016-02-24 16:11:50 +0100135
136 if (request->payload_size < sizeof(*serial_state)) {
Sandeep Patile54b1062016-05-19 08:52:39 -0700137 dev_err(&gb_tty->gbphy_dev->dev,
Chaehyun Lim319b78c2016-09-22 10:56:44 +0900138 "short serial-state event received (%zu < %zu)\n",
139 request->payload_size, sizeof(*serial_state));
Johan Hovoldcb7f00b2016-02-24 16:11:50 +0100140 return -EINVAL;
141 }
142
143 serial_state = request->payload;
144 gb_tty->ctrlin = serial_state->control;
145
146 return 0;
147}
148
Axel Haslama8bc00f2016-05-31 14:36:11 +0200149static int gb_uart_receive_credits_handler(struct gb_operation *op)
150{
151 struct gb_connection *connection = op->connection;
152 struct gb_tty *gb_tty = gb_connection_get_data(connection);
153 struct gb_message *request = op->request;
154 struct gb_uart_receive_credits_request *credit_request;
155 unsigned long flags;
156 unsigned int incoming_credits;
157 int ret = 0;
158
159 if (request->payload_size < sizeof(*credit_request)) {
160 dev_err(&gb_tty->gbphy_dev->dev,
Chaehyun Lim319b78c2016-09-22 10:56:44 +0900161 "short receive_credits event received (%zu < %zu)\n",
162 request->payload_size,
163 sizeof(*credit_request));
Axel Haslama8bc00f2016-05-31 14:36:11 +0200164 return -EINVAL;
165 }
166
167 credit_request = request->payload;
168 incoming_credits = le16_to_cpu(credit_request->count);
169
170 spin_lock_irqsave(&gb_tty->write_lock, flags);
171 gb_tty->credits += incoming_credits;
172 if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
173 gb_tty->credits -= incoming_credits;
174 ret = -EINVAL;
175 }
176 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
177
178 if (ret) {
179 dev_err(&gb_tty->gbphy_dev->dev,
180 "invalid number of incoming credits: %d\n",
181 incoming_credits);
182 return ret;
183 }
184
185 if (!gb_tty->close_pending)
186 schedule_work(&gb_tty->tx_work);
187
188 /*
189 * the port the tty layer may be waiting for credits
190 */
191 tty_port_tty_wakeup(&gb_tty->port);
192
Axel Haslam5dad5c32016-05-31 14:36:13 +0200193 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
194 complete(&gb_tty->credits_complete);
195
Axel Haslama8bc00f2016-05-31 14:36:11 +0200196 return ret;
197}
198
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530199static int gb_uart_request_handler(struct gb_operation *op)
Johan Hovoldcb7f00b2016-02-24 16:11:50 +0100200{
201 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530202 struct gb_tty *gb_tty = gb_connection_get_data(connection);
203 int type = op->type;
Johan Hovoldcb7f00b2016-02-24 16:11:50 +0100204 int ret;
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100205
206 switch (type) {
207 case GB_UART_TYPE_RECEIVE_DATA:
Johan Hovoldc5f338c2016-02-24 16:11:51 +0100208 ret = gb_uart_receive_data_handler(op);
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100209 break;
210 case GB_UART_TYPE_SERIAL_STATE:
Johan Hovoldcb7f00b2016-02-24 16:11:50 +0100211 ret = gb_uart_serial_state_handler(op);
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100212 break;
Axel Haslama8bc00f2016-05-31 14:36:11 +0200213 case GB_UART_TYPE_RECEIVE_CREDITS:
214 ret = gb_uart_receive_credits_handler(op);
215 break;
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100216 default:
Sandeep Patile54b1062016-05-19 08:52:39 -0700217 dev_err(&gb_tty->gbphy_dev->dev,
Viresh Kumarb933fa42015-12-04 21:30:10 +0530218 "unsupported unsolicited request: 0x%02x\n", type);
Bryan O'Donoghue1c087012015-06-02 13:40:50 +0100219 ret = -EINVAL;
220 }
221
222 return ret;
223}
224
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200225static void gb_uart_tx_write_work(struct work_struct *work)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800226{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800227 struct gb_uart_send_data_request *request;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200228 struct gb_tty *gb_tty;
229 unsigned long flags;
230 unsigned int send_size;
Bryan O'Donoghue563bd792015-06-02 13:40:48 +0100231 int ret;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800232
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200233 gb_tty = container_of(work, struct gb_tty, tx_work);
234 request = gb_tty->buffer;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800235
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200236 while (1) {
237 if (gb_tty->close_pending)
238 break;
239
240 spin_lock_irqsave(&gb_tty->write_lock, flags);
Axel Haslama8bc00f2016-05-31 14:36:11 +0200241 send_size = gb_tty->buffer_payload_max;
242 if (send_size > gb_tty->credits)
243 send_size = gb_tty->credits;
244
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200245 send_size = kfifo_out_peek(&gb_tty->write_fifo,
Chaehyun Lim319b78c2016-09-22 10:56:44 +0900246 &request->data[0],
247 send_size);
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200248 if (!send_size) {
249 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
250 break;
251 }
252
Axel Haslama8bc00f2016-05-31 14:36:11 +0200253 gb_tty->credits -= send_size;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200254 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
255
256 request->size = cpu_to_le16(send_size);
257 ret = gb_operation_sync(gb_tty->connection,
258 GB_UART_TYPE_SEND_DATA,
259 request, sizeof(*request) + send_size,
260 NULL, 0);
261 if (ret) {
262 dev_err(&gb_tty->gbphy_dev->dev,
263 "send data error: %d\n", ret);
Axel Haslama8bc00f2016-05-31 14:36:11 +0200264 spin_lock_irqsave(&gb_tty->write_lock, flags);
265 gb_tty->credits += send_size;
266 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200267 if (!gb_tty->close_pending)
268 schedule_work(work);
269 return;
270 }
271
272 spin_lock_irqsave(&gb_tty->write_lock, flags);
273 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
274 send_size);
275 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
276
277 tty_port_tty_wakeup(&gb_tty->port);
278 }
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800279}
280
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800281static int send_line_coding(struct gb_tty *tty)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800282{
Viresh Kumar530430b2015-01-21 18:12:35 +0530283 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
Johan Hovold6064aee2020-05-14 09:05:48 +0200284 &tty->line_coding, sizeof(tty->line_coding),
285 NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800286}
287
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100288static int send_control(struct gb_tty *gb_tty, u8 control)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800289{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800290 struct gb_uart_set_control_line_state_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800291
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100292 request.control = control;
Bryan O'Donoghue62229a12015-06-02 13:40:51 +0100293 return gb_operation_sync(gb_tty->connection,
Viresh Kumar530430b2015-01-21 18:12:35 +0530294 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800295 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800296}
297
Bryan O'Donoghue62229a12015-06-02 13:40:51 +0100298static int send_break(struct gb_tty *gb_tty, u8 state)
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800299{
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800300 struct gb_uart_set_break_request request;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800301
302 if ((state != 0) && (state != 1)) {
Sandeep Patile54b1062016-05-19 08:52:39 -0700303 dev_err(&gb_tty->gbphy_dev->dev,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800304 "invalid break state of %d\n", state);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800305 return -EINVAL;
306 }
307
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800308 request.state = state;
Bryan O'Donoghuea5192032015-07-14 02:10:18 +0100309 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
Greg Kroah-Hartmane51f1d12014-11-23 17:45:21 -0800310 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800311}
312
Axel Haslam5dad5c32016-05-31 14:36:13 +0200313static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
314{
315 int ret;
316
317 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
318 return 0;
319
320 ret = wait_for_completion_timeout(&gb_tty->credits_complete,
321 msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
322 if (!ret) {
323 dev_err(&gb_tty->gbphy_dev->dev,
324 "time out waiting for credits\n");
325 return -ETIMEDOUT;
326 }
327
328 return 0;
329}
330
Axel Haslam2b3b87f2016-05-31 14:36:12 +0200331static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
332{
333 struct gb_uart_serial_flush_request request;
334
335 request.flags = flags;
336 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
337 &request, sizeof(request), NULL, 0);
338}
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800339
Chaehyun Lim26746a32016-09-22 10:56:43 +0900340static struct gb_tty *get_gb_by_minor(unsigned int minor)
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800341{
342 struct gb_tty *gb_tty;
343
344 mutex_lock(&table_lock);
345 gb_tty = idr_find(&tty_minors, minor);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800346 if (gb_tty) {
347 mutex_lock(&gb_tty->mutex);
348 if (gb_tty->disconnected) {
349 mutex_unlock(&gb_tty->mutex);
350 gb_tty = NULL;
351 } else {
352 tty_port_get(&gb_tty->port);
353 mutex_unlock(&gb_tty->mutex);
354 }
355 }
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800356 mutex_unlock(&table_lock);
357 return gb_tty;
358}
359
360static int alloc_minor(struct gb_tty *gb_tty)
361{
362 int minor;
363
364 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500365 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800366 mutex_unlock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500367 if (minor >= 0)
368 gb_tty->minor = minor;
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800369 return minor;
370}
371
372static void release_minor(struct gb_tty *gb_tty)
373{
Alex Elderff5f0b32014-08-18 18:25:11 -0500374 int minor = gb_tty->minor;
375
376 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800377 mutex_lock(&table_lock);
Alex Elderff5f0b32014-08-18 18:25:11 -0500378 idr_remove(&tty_minors, minor);
Greg Kroah-Hartmanff45c262014-08-15 18:33:33 +0800379 mutex_unlock(&table_lock);
380}
381
382static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
383{
384 struct gb_tty *gb_tty;
385 int retval;
386
387 gb_tty = get_gb_by_minor(tty->index);
388 if (!gb_tty)
389 return -ENODEV;
390
391 retval = tty_standard_install(driver, tty);
392 if (retval)
393 goto error;
394
395 tty->driver_data = gb_tty;
396 return 0;
397error:
398 tty_port_put(&gb_tty->port);
399 return retval;
400}
401
402static int gb_tty_open(struct tty_struct *tty, struct file *file)
403{
404 struct gb_tty *gb_tty = tty->driver_data;
405
406 return tty_port_open(&gb_tty->port, tty, file);
407}
408
409static void gb_tty_close(struct tty_struct *tty, struct file *file)
410{
411 struct gb_tty *gb_tty = tty->driver_data;
412
413 tty_port_close(&gb_tty->port, tty, file);
414}
415
416static void gb_tty_cleanup(struct tty_struct *tty)
417{
418 struct gb_tty *gb_tty = tty->driver_data;
419
420 tty_port_put(&gb_tty->port);
421}
422
423static void gb_tty_hangup(struct tty_struct *tty)
424{
425 struct gb_tty *gb_tty = tty->driver_data;
426
427 tty_port_hangup(&gb_tty->port);
428}
429
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800430static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
431 int count)
432{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800433 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800434
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200435 count = kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
Chaehyun Lim319b78c2016-09-22 10:56:44 +0900436 &gb_tty->write_lock);
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200437 if (count && !gb_tty->close_pending)
438 schedule_work(&gb_tty->tx_work);
439
440 return count;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800441}
442
Jiri Slaby03b3b1a2021-05-05 11:19:15 +0200443static unsigned int gb_tty_write_room(struct tty_struct *tty)
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800444{
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100445 struct gb_tty *gb_tty = tty->driver_data;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200446 unsigned long flags;
447 int room;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800448
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200449 spin_lock_irqsave(&gb_tty->write_lock, flags);
450 room = kfifo_avail(&gb_tty->write_fifo);
451 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
452
453 room -= GB_UART_WRITE_ROOM_MARGIN;
454 if (room < 0)
455 return 0;
456
457 return room;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800458}
459
Jiri Slabyfff4ef12021-05-05 11:19:19 +0200460static unsigned int gb_tty_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800461{
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200462 struct gb_tty *gb_tty = tty->driver_data;
463 unsigned long flags;
Jiri Slabyfff4ef12021-05-05 11:19:19 +0200464 unsigned int chars;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200465
466 spin_lock_irqsave(&gb_tty->write_lock, flags);
467 chars = kfifo_len(&gb_tty->write_fifo);
Axel Haslama8bc00f2016-05-31 14:36:11 +0200468 if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
469 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200470 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
471
472 return chars;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800473}
474
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800475static int gb_tty_break_ctl(struct tty_struct *tty, int state)
476{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800477 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800478
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800479 return send_break(gb_tty, state ? 1 : 0);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800480}
481
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800482static void gb_tty_set_termios(struct tty_struct *tty,
483 struct ktermios *termios_old)
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800484{
Johan Hovold6064aee2020-05-14 09:05:48 +0200485 struct gb_uart_set_line_coding_request newline;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800486 struct gb_tty *gb_tty = tty->driver_data;
487 struct ktermios *termios = &tty->termios;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100488 u8 newctrl = gb_tty->ctrlout;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800489
490 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
Bryan O'Donoghue62229a12015-06-02 13:40:51 +0100491 newline.format = termios->c_cflag & CSTOPB ?
492 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800493 newline.parity = termios->c_cflag & PARENB ?
494 (termios->c_cflag & PARODD ? 1 : 2) +
495 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
496
Jiri Slaby3ec2ff32021-06-10 11:02:47 +0200497 newline.data_bits = tty_get_char_size(termios->c_cflag);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800498
499 /* FIXME: needs to clear unsupported bits in the termios */
500 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
501
502 if (C_BAUD(tty) == B0) {
503 newline.rate = gb_tty->line_coding.rate;
Axel Haslame55c2522016-05-31 14:36:08 +0200504 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800505 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
Axel Haslame55c2522016-05-31 14:36:08 +0200506 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800507 }
508
509 if (newctrl != gb_tty->ctrlout) {
510 gb_tty->ctrlout = newctrl;
511 send_control(gb_tty, newctrl);
512 }
513
Axel Haslame55c2522016-05-31 14:36:08 +0200514 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
Oscar Carter34625c12020-05-10 12:14:26 +0200515 newline.flow_control = GB_SERIAL_AUTO_RTSCTS_EN;
Axel Haslame55c2522016-05-31 14:36:08 +0200516 else
Oscar Carter34625c12020-05-10 12:14:26 +0200517 newline.flow_control = 0;
Axel Haslame55c2522016-05-31 14:36:08 +0200518
Axel Haslamb4b1b7e2016-04-20 20:47:20 +0200519 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800520 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800521 send_line_coding(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800522 }
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800523}
524
525static int gb_tty_tiocmget(struct tty_struct *tty)
526{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800527 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800528
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800529 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
530 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
531 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
532 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
533 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
534 TIOCM_CTS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800535}
536
537static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
538 unsigned int clear)
539{
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800540 struct gb_tty *gb_tty = tty->driver_data;
Bryan O'Donoghueba4b0992015-06-29 18:09:15 +0100541 u8 newctrl = gb_tty->ctrlout;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800542
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800543 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
544 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
545 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
546 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
547
548 newctrl = (newctrl & ~clear) | set;
549 if (gb_tty->ctrlout == newctrl)
550 return 0;
551
552 gb_tty->ctrlout = newctrl;
553 return send_control(gb_tty, newctrl);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800554}
555
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800556static void gb_tty_throttle(struct tty_struct *tty)
557{
558 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800559 unsigned char stop_char;
560 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800561
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800562 if (I_IXOFF(tty)) {
563 stop_char = STOP_CHAR(tty);
564 retval = gb_tty_write(tty, &stop_char, 1);
565 if (retval <= 0)
566 return;
567 }
568
569 if (tty->termios.c_cflag & CRTSCTS) {
570 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
571 retval = send_control(gb_tty, gb_tty->ctrlout);
572 }
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800573}
574
575static void gb_tty_unthrottle(struct tty_struct *tty)
576{
577 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800578 unsigned char start_char;
579 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800580
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800581 if (I_IXOFF(tty)) {
582 start_char = START_CHAR(tty);
583 retval = gb_tty_write(tty, &start_char, 1);
584 if (retval <= 0)
585 return;
586 }
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800587
Greg Kroah-Hartman980c7c52014-11-19 14:23:00 -0800588 if (tty->termios.c_cflag & CRTSCTS) {
589 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
590 retval = send_control(gb_tty, gb_tty->ctrlout);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800591 }
592}
593
Al Viro0aad5ad2018-09-11 21:58:45 -0400594static int get_serial_info(struct tty_struct *tty,
595 struct serial_struct *ss)
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800596{
Al Viro0aad5ad2018-09-11 21:58:45 -0400597 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800598
Al Viro0aad5ad2018-09-11 21:58:45 -0400599 ss->line = gb_tty->minor;
Johan Hovoldb71e5712021-04-07 12:23:23 +0200600 ss->close_delay = jiffies_to_msecs(gb_tty->port.close_delay) / 10;
Al Viro0aad5ad2018-09-11 21:58:45 -0400601 ss->closing_wait =
Gioh Kim461ab802017-02-09 17:30:11 +0100602 gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Johan Hovoldb71e5712021-04-07 12:23:23 +0200603 ASYNC_CLOSING_WAIT_NONE :
604 jiffies_to_msecs(gb_tty->port.closing_wait) / 10;
605
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700606 return 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800607}
608
Al Viro0aad5ad2018-09-11 21:58:45 -0400609static int set_serial_info(struct tty_struct *tty,
610 struct serial_struct *ss)
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800611{
Al Viro0aad5ad2018-09-11 21:58:45 -0400612 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800613 unsigned int closing_wait;
614 unsigned int close_delay;
Alex Elder69f93ab2014-09-22 18:53:02 -0500615 int retval = 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800616
Johan Hovoldb71e5712021-04-07 12:23:23 +0200617 close_delay = msecs_to_jiffies(ss->close_delay * 10);
Al Viro0aad5ad2018-09-11 21:58:45 -0400618 closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
Johan Hovoldb71e5712021-04-07 12:23:23 +0200619 ASYNC_CLOSING_WAIT_NONE :
620 msecs_to_jiffies(ss->closing_wait * 10);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800621
622 mutex_lock(&gb_tty->port.mutex);
623 if (!capable(CAP_SYS_ADMIN)) {
624 if ((close_delay != gb_tty->port.close_delay) ||
625 (closing_wait != gb_tty->port.closing_wait))
626 retval = -EPERM;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800627 } else {
628 gb_tty->port.close_delay = close_delay;
629 gb_tty->port.closing_wait = closing_wait;
630 }
631 mutex_unlock(&gb_tty->port.mutex);
632 return retval;
633}
634
635static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
636{
637 int retval = 0;
638 DECLARE_WAITQUEUE(wait, current);
639 struct async_icount old;
640 struct async_icount new;
641
Alex Eldercaaa8a82014-08-18 18:25:12 -0500642 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800643 return -EINVAL;
644
645 do {
Johan Hovold2aae92b2016-06-27 20:07:12 +0200646 spin_lock_irq(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800647 old = gb_tty->oldcount;
648 new = gb_tty->iocount;
649 gb_tty->oldcount = new;
Johan Hovold2aae92b2016-06-27 20:07:12 +0200650 spin_unlock_irq(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800651
652 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
653 break;
654 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
655 break;
656 if ((arg & TIOCM_RI) && (old.rng != new.rng))
657 break;
658
659 add_wait_queue(&gb_tty->wioctl, &wait);
660 set_current_state(TASK_INTERRUPTIBLE);
661 schedule();
662 remove_wait_queue(&gb_tty->wioctl, &wait);
663 if (gb_tty->disconnected) {
664 if (arg & TIOCM_CD)
665 break;
Alex Eldercaaa8a82014-08-18 18:25:12 -0500666 retval = -ENODEV;
667 } else if (signal_pending(current)) {
668 retval = -ERESTARTSYS;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800669 }
670 } while (!retval);
671
672 return retval;
673}
674
Johan Hovold424a4b52016-11-08 13:31:19 +0100675static int gb_tty_get_icount(struct tty_struct *tty,
676 struct serial_icounter_struct *icount)
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800677{
Johan Hovold424a4b52016-11-08 13:31:19 +0100678 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800679
Johan Hovold424a4b52016-11-08 13:31:19 +0100680 icount->dsr = gb_tty->iocount.dsr;
681 icount->rng = gb_tty->iocount.rng;
682 icount->dcd = gb_tty->iocount.dcd;
683 icount->frame = gb_tty->iocount.frame;
684 icount->overrun = gb_tty->iocount.overrun;
685 icount->parity = gb_tty->iocount.parity;
686 icount->brk = gb_tty->iocount.brk;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800687
Johan Hovold424a4b52016-11-08 13:31:19 +0100688 return 0;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800689}
690
691static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
692 unsigned long arg)
693{
694 struct gb_tty *gb_tty = tty->driver_data;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800695
696 switch (cmd) {
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800697 case TIOCMIWAIT:
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700698 return wait_serial_change(gb_tty, arg);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800699 }
700
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700701 return -ENOIOCTLCMD;
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800702}
703
Axel Haslam219ffcf2016-05-31 14:36:09 +0200704static void gb_tty_dtr_rts(struct tty_port *port, int on)
705{
706 struct gb_tty *gb_tty;
707 u8 newctrl;
708
709 gb_tty = container_of(port, struct gb_tty, port);
710 newctrl = gb_tty->ctrlout;
711
712 if (on)
713 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
714 else
715 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
716
717 gb_tty->ctrlout = newctrl;
718 send_control(gb_tty, newctrl);
719}
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800720
Axel Haslam22b87082016-07-14 15:13:00 -0500721static int gb_tty_port_activate(struct tty_port *port,
722 struct tty_struct *tty)
723{
724 struct gb_tty *gb_tty;
725
726 gb_tty = container_of(port, struct gb_tty, port);
727
728 return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
729}
730
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200731static void gb_tty_port_shutdown(struct tty_port *port)
732{
733 struct gb_tty *gb_tty;
734 unsigned long flags;
Axel Haslam2b3b87f2016-05-31 14:36:12 +0200735 int ret;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200736
737 gb_tty = container_of(port, struct gb_tty, port);
738
739 gb_tty->close_pending = true;
740
741 cancel_work_sync(&gb_tty->tx_work);
742
743 spin_lock_irqsave(&gb_tty->write_lock, flags);
744 kfifo_reset_out(&gb_tty->write_fifo);
745 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
746
Axel Haslam5dad5c32016-05-31 14:36:13 +0200747 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
748 goto out;
749
Axel Haslam2b3b87f2016-05-31 14:36:12 +0200750 ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
751 if (ret) {
752 dev_err(&gb_tty->gbphy_dev->dev,
753 "error flushing transmitter: %d\n", ret);
754 }
755
Axel Haslam5dad5c32016-05-31 14:36:13 +0200756 gb_uart_wait_for_all_credits(gb_tty);
757
758out:
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200759 gb_tty->close_pending = false;
Axel Haslam22b87082016-07-14 15:13:00 -0500760
761 gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200762}
763
Johan Hovold92dc0b12021-09-06 14:45:38 +0200764static void gb_tty_port_destruct(struct tty_port *port)
765{
766 struct gb_tty *gb_tty = container_of(port, struct gb_tty, port);
767
768 if (gb_tty->minor != GB_NUM_MINORS)
769 release_minor(gb_tty);
770 kfifo_free(&gb_tty->write_fifo);
771 kfree(gb_tty->buffer);
772 kfree(gb_tty);
773}
774
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800775static const struct tty_operations gb_ops = {
776 .install = gb_tty_install,
777 .open = gb_tty_open,
778 .close = gb_tty_close,
779 .cleanup = gb_tty_cleanup,
780 .hangup = gb_tty_hangup,
781 .write = gb_tty_write,
782 .write_room = gb_tty_write_room,
783 .ioctl = gb_tty_ioctl,
784 .throttle = gb_tty_throttle,
785 .unthrottle = gb_tty_unthrottle,
786 .chars_in_buffer = gb_tty_chars_in_buffer,
787 .break_ctl = gb_tty_break_ctl,
788 .set_termios = gb_tty_set_termios,
789 .tiocmget = gb_tty_tiocmget,
790 .tiocmset = gb_tty_tiocmset,
Johan Hovold424a4b52016-11-08 13:31:19 +0100791 .get_icount = gb_tty_get_icount,
Ioannis Valasakis327f27692018-11-09 13:54:23 +0000792 .set_serial = set_serial_info,
793 .get_serial = get_serial_info,
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800794};
795
Bhumika Goyal4d79f432016-10-15 12:28:01 +0530796static const struct tty_port_operations gb_port_ops = {
Axel Haslam219ffcf2016-05-31 14:36:09 +0200797 .dtr_rts = gb_tty_dtr_rts,
Axel Haslam22b87082016-07-14 15:13:00 -0500798 .activate = gb_tty_port_activate,
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200799 .shutdown = gb_tty_port_shutdown,
Johan Hovold92dc0b12021-09-06 14:45:38 +0200800 .destruct = gb_tty_port_destruct,
Axel Haslam219ffcf2016-05-31 14:36:09 +0200801};
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800802
Sandeep Patile54b1062016-05-19 08:52:39 -0700803static int gb_uart_probe(struct gbphy_device *gbphy_dev,
804 const struct gbphy_device_id *id)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800805{
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530806 struct gb_connection *connection;
Johan Hovold066f9502016-02-24 16:11:49 +0100807 size_t max_payload;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800808 struct gb_tty *gb_tty;
809 struct device *tty_dev;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800810 int retval;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800811 int minor;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800812
Sandeep Patile54b1062016-05-19 08:52:39 -0700813 connection = gb_connection_create(gbphy_dev->bundle,
814 le16_to_cpu(gbphy_dev->cport_desc->id),
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530815 gb_uart_request_handler);
Johan Hovold92dc0b12021-09-06 14:45:38 +0200816 if (IS_ERR(connection))
817 return PTR_ERR(connection);
Bryan O'Donoghuef95ad782015-06-02 13:40:47 +0100818
Johan Hovold066f9502016-02-24 16:11:49 +0100819 max_payload = gb_operation_get_payload_size_max(connection);
820 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
821 retval = -EINVAL;
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530822 goto exit_connection_destroy;
Johan Hovold066f9502016-02-24 16:11:49 +0100823 }
824
Johan Hovold92dc0b12021-09-06 14:45:38 +0200825 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
826 if (!gb_tty) {
827 retval = -ENOMEM;
828 goto exit_connection_destroy;
829 }
830
831 tty_port_init(&gb_tty->port);
832 gb_tty->port.ops = &gb_port_ops;
833 gb_tty->minor = GB_NUM_MINORS;
834
Johan Hovold066f9502016-02-24 16:11:49 +0100835 gb_tty->buffer_payload_max = max_payload -
Greg Kroah-Hartman7f29ade2016-02-22 18:14:46 -0800836 sizeof(struct gb_uart_send_data_request);
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100837
838 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
839 if (!gb_tty->buffer) {
Phong Tran55a8e352015-06-10 21:03:17 +0700840 retval = -ENOMEM;
Johan Hovold92dc0b12021-09-06 14:45:38 +0200841 goto exit_put_port;
Bryan O'Donoghuedd1c64e2015-06-02 13:40:49 +0100842 }
843
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200844 INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
845
846 retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
Chaehyun Lim319b78c2016-09-22 10:56:44 +0900847 GFP_KERNEL);
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200848 if (retval)
Johan Hovold92dc0b12021-09-06 14:45:38 +0200849 goto exit_put_port;
Axel Haslam8d6fbe92016-05-31 14:36:10 +0200850
Axel Haslama8bc00f2016-05-31 14:36:11 +0200851 gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
Axel Haslam5dad5c32016-05-31 14:36:13 +0200852 init_completion(&gb_tty->credits_complete);
Axel Haslama8bc00f2016-05-31 14:36:11 +0200853
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800854 minor = alloc_minor(gb_tty);
Alex Elderff5f0b32014-08-18 18:25:11 -0500855 if (minor < 0) {
856 if (minor == -ENOSPC) {
Viresh Kumar4fa58912016-10-12 08:02:22 +0530857 dev_err(&gbphy_dev->dev,
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800858 "no more free minor numbers\n");
Phong Tran55f22912015-06-01 22:19:45 +0700859 retval = -ENODEV;
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530860 } else {
861 retval = minor;
Alex Elderff5f0b32014-08-18 18:25:11 -0500862 }
Johan Hovold92dc0b12021-09-06 14:45:38 +0200863 goto exit_put_port;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800864 }
865
866 gb_tty->minor = minor;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800867 spin_lock_init(&gb_tty->write_lock);
868 spin_lock_init(&gb_tty->read_lock);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800869 init_waitqueue_head(&gb_tty->wioctl);
870 mutex_init(&gb_tty->mutex);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800871
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530872 gb_tty->connection = connection;
Sandeep Patile54b1062016-05-19 08:52:39 -0700873 gb_tty->gbphy_dev = gbphy_dev;
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530874 gb_connection_set_data(connection, gb_tty);
Sandeep Patile54b1062016-05-19 08:52:39 -0700875 gb_gbphy_set_data(gbphy_dev, gb_tty);
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530876
877 retval = gb_connection_enable_tx(connection);
878 if (retval)
Johan Hovold92dc0b12021-09-06 14:45:38 +0200879 goto exit_put_port;
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530880
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800881 send_control(gb_tty, gb_tty->ctrlout);
882
883 /* initialize the uart to be 9600n81 */
884 gb_tty->line_coding.rate = cpu_to_le32(9600);
885 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
886 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
Bryan O'Donoghue11fca142015-06-02 13:40:45 +0100887 gb_tty->line_coding.data_bits = 8;
Greg Kroah-Hartman9f240f22014-11-24 13:52:25 -0800888 send_line_coding(gb_tty);
Greg Kroah-Hartmanb4be4042014-11-15 18:30:00 -0800889
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530890 retval = gb_connection_enable(connection);
891 if (retval)
892 goto exit_connection_disable;
893
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800894 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
Sandeep Patile54b1062016-05-19 08:52:39 -0700895 &gbphy_dev->dev);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800896 if (IS_ERR(tty_dev)) {
897 retval = PTR_ERR(tty_dev);
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530898 goto exit_connection_disable;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800899 }
900
Axel Haslam22b87082016-07-14 15:13:00 -0500901 gbphy_runtime_put_autosuspend(gbphy_dev);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800902 return 0;
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530903
904exit_connection_disable:
905 gb_connection_disable(connection);
Johan Hovold92dc0b12021-09-06 14:45:38 +0200906exit_put_port:
907 tty_port_put(&gb_tty->port);
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530908exit_connection_destroy:
909 gb_connection_destroy(connection);
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530910
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800911 return retval;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800912}
913
Sandeep Patile54b1062016-05-19 08:52:39 -0700914static void gb_uart_remove(struct gbphy_device *gbphy_dev)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800915{
Sandeep Patile54b1062016-05-19 08:52:39 -0700916 struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530917 struct gb_connection *connection = gb_tty->connection;
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800918 struct tty_struct *tty;
Axel Haslam22b87082016-07-14 15:13:00 -0500919 int ret;
920
921 ret = gbphy_runtime_get_sync(gbphy_dev);
922 if (ret)
923 gbphy_runtime_get_noresume(gbphy_dev);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800924
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800925 mutex_lock(&gb_tty->mutex);
926 gb_tty->disconnected = true;
927
928 wake_up_all(&gb_tty->wioctl);
Greg Kroah-Hartmane68453e2014-08-15 19:44:32 +0800929 mutex_unlock(&gb_tty->mutex);
930
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800931 tty = tty_port_tty_get(&gb_tty->port);
932 if (tty) {
933 tty_vhangup(tty);
934 tty_kref_put(tty);
935 }
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800936
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530937 gb_connection_disable_rx(connection);
Greg Kroah-Hartmana18e1512014-08-15 18:54:11 +0800938 tty_unregister_device(gb_tty_driver, gb_tty->minor);
939
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530940 gb_connection_disable(connection);
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530941 gb_connection_destroy(connection);
Johan Hovold92dc0b12021-09-06 14:45:38 +0200942
943 tty_port_put(&gb_tty->port);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800944}
945
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800946static int gb_tty_init(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800947{
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400948 int retval = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800949
Marti Bolivarf8089c02014-09-05 23:56:09 -0400950 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400951 if (IS_ERR(gb_tty_driver)) {
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700952 pr_err("Can not allocate tty driver\n");
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400953 retval = -ENOMEM;
954 goto fail_unregister_dev;
955 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800956
957 gb_tty_driver->driver_name = "gb";
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400958 gb_tty_driver->name = GB_NAME;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700959 gb_tty_driver->major = 0;
960 gb_tty_driver->minor_start = 0;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800961 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
962 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
963 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
964 gb_tty_driver->init_termios = tty_std_termios;
Gioh Kim461ab802017-02-09 17:30:11 +0100965 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
966 CREAD | HUPCL | CLOCAL;
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800967 tty_set_operations(gb_tty_driver, &gb_ops);
968
969 retval = tty_register_driver(gb_tty_driver);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700970 if (retval) {
971 pr_err("Can not register tty driver: %d\n", retval);
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400972 goto fail_put_gb_tty;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700973 }
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800974
Marti Bolivar7fabc8842014-09-05 23:56:10 -0400975 return 0;
976
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700977fail_put_gb_tty:
Jiri Slaby9f90a4d2021-07-23 09:43:16 +0200978 tty_driver_kref_put(gb_tty_driver);
Greg Kroah-Hartman543b8ed2014-09-13 17:02:47 -0700979fail_unregister_dev:
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800980 return retval;
981}
982
Greg Kroah-Hartman4d980982014-10-27 17:42:45 +0800983static void gb_tty_exit(void)
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800984{
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800985 tty_unregister_driver(gb_tty_driver);
Jiri Slaby9f90a4d2021-07-23 09:43:16 +0200986 tty_driver_kref_put(gb_tty_driver);
Greg Kroah-Hartman5c1ac692015-07-08 10:44:09 -0700987 idr_destroy(&tty_minors);
Greg Kroah-Hartman79c822b2014-08-15 16:01:23 +0800988}
Alex Elder3689f972014-10-27 06:04:30 -0500989
Sandeep Patile54b1062016-05-19 08:52:39 -0700990static const struct gbphy_device_id gb_uart_id_table[] = {
991 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530992 { },
Alex Elder19d03de2014-11-05 16:12:53 -0600993};
Sandeep Patile54b1062016-05-19 08:52:39 -0700994MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
Alex Elder19d03de2014-11-05 16:12:53 -0600995
Sandeep Patile54b1062016-05-19 08:52:39 -0700996static struct gbphy_driver uart_driver = {
Greg Kroah-Hartman7dbe1f42016-05-05 14:32:30 +0530997 .name = "uart",
998 .probe = gb_uart_probe,
999 .remove = gb_uart_remove,
1000 .id_table = gb_uart_id_table,
1001};
Viresh Kumar73d292d2016-05-05 14:32:38 +05301002
Viresh Kumar66b9e092016-05-09 18:15:11 +05301003static int gb_uart_driver_init(void)
Viresh Kumar73d292d2016-05-05 14:32:38 +05301004{
1005 int ret;
1006
1007 ret = gb_tty_init();
1008 if (ret)
1009 return ret;
1010
Sandeep Patile54b1062016-05-19 08:52:39 -07001011 ret = gb_gbphy_register(&uart_driver);
Viresh Kumar73d292d2016-05-05 14:32:38 +05301012 if (ret) {
1013 gb_tty_exit();
1014 return ret;
1015 }
1016
1017 return 0;
1018}
Viresh Kumar66b9e092016-05-09 18:15:11 +05301019module_init(gb_uart_driver_init);
Viresh Kumar73d292d2016-05-05 14:32:38 +05301020
Viresh Kumar66b9e092016-05-09 18:15:11 +05301021static void gb_uart_driver_exit(void)
Viresh Kumar73d292d2016-05-05 14:32:38 +05301022{
Sandeep Patile54b1062016-05-19 08:52:39 -07001023 gb_gbphy_deregister(&uart_driver);
Viresh Kumar73d292d2016-05-05 14:32:38 +05301024 gb_tty_exit();
1025}
Viresh Kumar66b9e092016-05-09 18:15:11 +05301026
1027module_exit(gb_uart_driver_exit);
1028MODULE_LICENSE("GPL v2");