1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * UART driver for the Greybus "generic" UART module.
4  *
5  * Copyright 2014 Google Inc.
6  * Copyright 2014 Linaro Ltd.
7  *
8  * Heavily based on drivers/usb/class/cdc-acm.c and
9  * drivers/usb/serial/usb-serial.c.
10  */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched/signal.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <linux/mutex.h>
21 #include <linux/tty.h>
22 #include <linux/serial.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/idr.h>
26 #include <linux/fs.h>
27 #include <linux/kdev_t.h>
28 #include <linux/kfifo.h>
29 #include <linux/workqueue.h>
30 #include <linux/completion.h>
31 
32 #include "greybus.h"
33 #include "gbphy.h"
34 
35 #define GB_NUM_MINORS	16	/* 16 is more than enough */
36 #define GB_NAME		"ttyGB"
37 
38 #define GB_UART_WRITE_FIFO_SIZE		PAGE_SIZE
39 #define GB_UART_WRITE_ROOM_MARGIN	1	/* leave some space in fifo */
40 #define GB_UART_FIRMWARE_CREDITS	4096
41 #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC	10000
42 
43 struct gb_tty_line_coding {
44 	__le32	rate;
45 	__u8	format;
46 	__u8	parity;
47 	__u8	data_bits;
48 	__u8	flow_control;
49 };
50 
51 struct gb_tty {
52 	struct gbphy_device *gbphy_dev;
53 	struct tty_port port;
54 	void *buffer;
55 	size_t buffer_payload_max;
56 	struct gb_connection *connection;
57 	u16 cport_id;
58 	unsigned int minor;
59 	unsigned char clocal;
60 	bool disconnected;
61 	spinlock_t read_lock;
62 	spinlock_t write_lock;
63 	struct async_icount iocount;
64 	struct async_icount oldcount;
65 	wait_queue_head_t wioctl;
66 	struct mutex mutex;
67 	u8 ctrlin;	/* input control lines */
68 	u8 ctrlout;	/* output control lines */
69 	struct gb_tty_line_coding line_coding;
70 	struct work_struct tx_work;
71 	struct kfifo write_fifo;
72 	bool close_pending;
73 	unsigned int credits;
74 	struct completion credits_complete;
75 };
76 
77 static struct tty_driver *gb_tty_driver;
78 static DEFINE_IDR(tty_minors);
79 static DEFINE_MUTEX(table_lock);
80 
gb_uart_receive_data_handler(struct gb_operation * op)81 static int gb_uart_receive_data_handler(struct gb_operation *op)
82 {
83 	struct gb_connection *connection = op->connection;
84 	struct gb_tty *gb_tty = gb_connection_get_data(connection);
85 	struct tty_port *port = &gb_tty->port;
86 	struct gb_message *request = op->request;
87 	struct gb_uart_recv_data_request *receive_data;
88 	u16 recv_data_size;
89 	int count;
90 	unsigned long tty_flags = TTY_NORMAL;
91 
92 	if (request->payload_size < sizeof(*receive_data)) {
93 		dev_err(&gb_tty->gbphy_dev->dev,
94 			"short receive-data request received (%zu < %zu)\n",
95 			request->payload_size, sizeof(*receive_data));
96 		return -EINVAL;
97 	}
98 
99 	receive_data = op->request->payload;
100 	recv_data_size = le16_to_cpu(receive_data->size);
101 
102 	if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
103 		dev_err(&gb_tty->gbphy_dev->dev,
104 			"malformed receive-data request received (%u != %zu)\n",
105 			recv_data_size,
106 			request->payload_size - sizeof(*receive_data));
107 		return -EINVAL;
108 	}
109 
110 	if (!recv_data_size)
111 		return -EINVAL;
112 
113 	if (receive_data->flags) {
114 		if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
115 			tty_flags = TTY_BREAK;
116 		else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
117 			tty_flags = TTY_PARITY;
118 		else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
119 			tty_flags = TTY_FRAME;
120 
121 		/* overrun is special, not associated with a char */
122 		if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
123 			tty_insert_flip_char(port, 0, TTY_OVERRUN);
124 	}
125 	count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
126 						  tty_flags, recv_data_size);
127 	if (count != recv_data_size) {
128 		dev_err(&gb_tty->gbphy_dev->dev,
129 			"UART: RX 0x%08x bytes only wrote 0x%08x\n",
130 			recv_data_size, count);
131 	}
132 	if (count)
133 		tty_flip_buffer_push(port);
134 	return 0;
135 }
136 
gb_uart_serial_state_handler(struct gb_operation * op)137 static int gb_uart_serial_state_handler(struct gb_operation *op)
138 {
139 	struct gb_connection *connection = op->connection;
140 	struct gb_tty *gb_tty = gb_connection_get_data(connection);
141 	struct gb_message *request = op->request;
142 	struct gb_uart_serial_state_request *serial_state;
143 
144 	if (request->payload_size < sizeof(*serial_state)) {
145 		dev_err(&gb_tty->gbphy_dev->dev,
146 			"short serial-state event received (%zu < %zu)\n",
147 			request->payload_size, sizeof(*serial_state));
148 		return -EINVAL;
149 	}
150 
151 	serial_state = request->payload;
152 	gb_tty->ctrlin = serial_state->control;
153 
154 	return 0;
155 }
156 
gb_uart_receive_credits_handler(struct gb_operation * op)157 static int gb_uart_receive_credits_handler(struct gb_operation *op)
158 {
159 	struct gb_connection *connection = op->connection;
160 	struct gb_tty *gb_tty = gb_connection_get_data(connection);
161 	struct gb_message *request = op->request;
162 	struct gb_uart_receive_credits_request *credit_request;
163 	unsigned long flags;
164 	unsigned int incoming_credits;
165 	int ret = 0;
166 
167 	if (request->payload_size < sizeof(*credit_request)) {
168 		dev_err(&gb_tty->gbphy_dev->dev,
169 			"short receive_credits event received (%zu < %zu)\n",
170 			request->payload_size,
171 			sizeof(*credit_request));
172 		return -EINVAL;
173 	}
174 
175 	credit_request = request->payload;
176 	incoming_credits = le16_to_cpu(credit_request->count);
177 
178 	spin_lock_irqsave(&gb_tty->write_lock, flags);
179 	gb_tty->credits += incoming_credits;
180 	if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
181 		gb_tty->credits -= incoming_credits;
182 		ret = -EINVAL;
183 	}
184 	spin_unlock_irqrestore(&gb_tty->write_lock, flags);
185 
186 	if (ret) {
187 		dev_err(&gb_tty->gbphy_dev->dev,
188 			"invalid number of incoming credits: %d\n",
189 			incoming_credits);
190 		return ret;
191 	}
192 
193 	if (!gb_tty->close_pending)
194 		schedule_work(&gb_tty->tx_work);
195 
196 	/*
197 	 * the port the tty layer may be waiting for credits
198 	 */
199 	tty_port_tty_wakeup(&gb_tty->port);
200 
201 	if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
202 		complete(&gb_tty->credits_complete);
203 
204 	return ret;
205 }
206 
gb_uart_request_handler(struct gb_operation * op)207 static int gb_uart_request_handler(struct gb_operation *op)
208 {
209 	struct gb_connection *connection = op->connection;
210 	struct gb_tty *gb_tty = gb_connection_get_data(connection);
211 	int type = op->type;
212 	int ret;
213 
214 	switch (type) {
215 	case GB_UART_TYPE_RECEIVE_DATA:
216 		ret = gb_uart_receive_data_handler(op);
217 		break;
218 	case GB_UART_TYPE_SERIAL_STATE:
219 		ret = gb_uart_serial_state_handler(op);
220 		break;
221 	case GB_UART_TYPE_RECEIVE_CREDITS:
222 		ret = gb_uart_receive_credits_handler(op);
223 		break;
224 	default:
225 		dev_err(&gb_tty->gbphy_dev->dev,
226 			"unsupported unsolicited request: 0x%02x\n", type);
227 		ret = -EINVAL;
228 	}
229 
230 	return ret;
231 }
232 
gb_uart_tx_write_work(struct work_struct * work)233 static void  gb_uart_tx_write_work(struct work_struct *work)
234 {
235 	struct gb_uart_send_data_request *request;
236 	struct gb_tty *gb_tty;
237 	unsigned long flags;
238 	unsigned int send_size;
239 	int ret;
240 
241 	gb_tty = container_of(work, struct gb_tty, tx_work);
242 	request = gb_tty->buffer;
243 
244 	while (1) {
245 		if (gb_tty->close_pending)
246 			break;
247 
248 		spin_lock_irqsave(&gb_tty->write_lock, flags);
249 		send_size = gb_tty->buffer_payload_max;
250 		if (send_size > gb_tty->credits)
251 			send_size = gb_tty->credits;
252 
253 		send_size = kfifo_out_peek(&gb_tty->write_fifo,
254 					   &request->data[0],
255 					   send_size);
256 		if (!send_size) {
257 			spin_unlock_irqrestore(&gb_tty->write_lock, flags);
258 			break;
259 		}
260 
261 		gb_tty->credits -= send_size;
262 		spin_unlock_irqrestore(&gb_tty->write_lock, flags);
263 
264 		request->size = cpu_to_le16(send_size);
265 		ret = gb_operation_sync(gb_tty->connection,
266 					GB_UART_TYPE_SEND_DATA,
267 					request, sizeof(*request) + send_size,
268 					NULL, 0);
269 		if (ret) {
270 			dev_err(&gb_tty->gbphy_dev->dev,
271 				"send data error: %d\n", ret);
272 			spin_lock_irqsave(&gb_tty->write_lock, flags);
273 			gb_tty->credits += send_size;
274 			spin_unlock_irqrestore(&gb_tty->write_lock, flags);
275 			if (!gb_tty->close_pending)
276 				schedule_work(work);
277 			return;
278 		}
279 
280 		spin_lock_irqsave(&gb_tty->write_lock, flags);
281 		ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
282 				send_size);
283 		spin_unlock_irqrestore(&gb_tty->write_lock, flags);
284 
285 		tty_port_tty_wakeup(&gb_tty->port);
286 	}
287 }
288 
send_line_coding(struct gb_tty * tty)289 static int send_line_coding(struct gb_tty *tty)
290 {
291 	struct gb_uart_set_line_coding_request request;
292 
293 	memcpy(&request, &tty->line_coding,
294 	       sizeof(tty->line_coding));
295 	return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
296 				 &request, sizeof(request), NULL, 0);
297 }
298 
send_control(struct gb_tty * gb_tty,u8 control)299 static int send_control(struct gb_tty *gb_tty, u8 control)
300 {
301 	struct gb_uart_set_control_line_state_request request;
302 
303 	request.control = control;
304 	return gb_operation_sync(gb_tty->connection,
305 				 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
306 				 &request, sizeof(request), NULL, 0);
307 }
308 
send_break(struct gb_tty * gb_tty,u8 state)309 static int send_break(struct gb_tty *gb_tty, u8 state)
310 {
311 	struct gb_uart_set_break_request request;
312 
313 	if ((state != 0) && (state != 1)) {
314 		dev_err(&gb_tty->gbphy_dev->dev,
315 			"invalid break state of %d\n", state);
316 		return -EINVAL;
317 	}
318 
319 	request.state = state;
320 	return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
321 				 &request, sizeof(request), NULL, 0);
322 }
323 
gb_uart_wait_for_all_credits(struct gb_tty * gb_tty)324 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
325 {
326 	int ret;
327 
328 	if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
329 		return 0;
330 
331 	ret = wait_for_completion_timeout(&gb_tty->credits_complete,
332 			msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
333 	if (!ret) {
334 		dev_err(&gb_tty->gbphy_dev->dev,
335 			"time out waiting for credits\n");
336 		return -ETIMEDOUT;
337 	}
338 
339 	return 0;
340 }
341 
gb_uart_flush(struct gb_tty * gb_tty,u8 flags)342 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
343 {
344 	struct gb_uart_serial_flush_request request;
345 
346 	request.flags = flags;
347 	return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
348 				 &request, sizeof(request), NULL, 0);
349 }
350 
get_gb_by_minor(unsigned int minor)351 static struct gb_tty *get_gb_by_minor(unsigned int minor)
352 {
353 	struct gb_tty *gb_tty;
354 
355 	mutex_lock(&table_lock);
356 	gb_tty = idr_find(&tty_minors, minor);
357 	if (gb_tty) {
358 		mutex_lock(&gb_tty->mutex);
359 		if (gb_tty->disconnected) {
360 			mutex_unlock(&gb_tty->mutex);
361 			gb_tty = NULL;
362 		} else {
363 			tty_port_get(&gb_tty->port);
364 			mutex_unlock(&gb_tty->mutex);
365 		}
366 	}
367 	mutex_unlock(&table_lock);
368 	return gb_tty;
369 }
370 
alloc_minor(struct gb_tty * gb_tty)371 static int alloc_minor(struct gb_tty *gb_tty)
372 {
373 	int minor;
374 
375 	mutex_lock(&table_lock);
376 	minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
377 	mutex_unlock(&table_lock);
378 	if (minor >= 0)
379 		gb_tty->minor = minor;
380 	return minor;
381 }
382 
release_minor(struct gb_tty * gb_tty)383 static void release_minor(struct gb_tty *gb_tty)
384 {
385 	int minor = gb_tty->minor;
386 
387 	gb_tty->minor = 0;	/* Maybe should use an invalid value instead */
388 	mutex_lock(&table_lock);
389 	idr_remove(&tty_minors, minor);
390 	mutex_unlock(&table_lock);
391 }
392 
gb_tty_install(struct tty_driver * driver,struct tty_struct * tty)393 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
394 {
395 	struct gb_tty *gb_tty;
396 	int retval;
397 
398 	gb_tty = get_gb_by_minor(tty->index);
399 	if (!gb_tty)
400 		return -ENODEV;
401 
402 	retval = tty_standard_install(driver, tty);
403 	if (retval)
404 		goto error;
405 
406 	tty->driver_data = gb_tty;
407 	return 0;
408 error:
409 	tty_port_put(&gb_tty->port);
410 	return retval;
411 }
412 
gb_tty_open(struct tty_struct * tty,struct file * file)413 static int gb_tty_open(struct tty_struct *tty, struct file *file)
414 {
415 	struct gb_tty *gb_tty = tty->driver_data;
416 
417 	return tty_port_open(&gb_tty->port, tty, file);
418 }
419 
gb_tty_close(struct tty_struct * tty,struct file * file)420 static void gb_tty_close(struct tty_struct *tty, struct file *file)
421 {
422 	struct gb_tty *gb_tty = tty->driver_data;
423 
424 	tty_port_close(&gb_tty->port, tty, file);
425 }
426 
gb_tty_cleanup(struct tty_struct * tty)427 static void gb_tty_cleanup(struct tty_struct *tty)
428 {
429 	struct gb_tty *gb_tty = tty->driver_data;
430 
431 	tty_port_put(&gb_tty->port);
432 }
433 
gb_tty_hangup(struct tty_struct * tty)434 static void gb_tty_hangup(struct tty_struct *tty)
435 {
436 	struct gb_tty *gb_tty = tty->driver_data;
437 
438 	tty_port_hangup(&gb_tty->port);
439 }
440 
gb_tty_write(struct tty_struct * tty,const unsigned char * buf,int count)441 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
442 			int count)
443 {
444 	struct gb_tty *gb_tty = tty->driver_data;
445 
446 	count =  kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
447 				     &gb_tty->write_lock);
448 	if (count && !gb_tty->close_pending)
449 		schedule_work(&gb_tty->tx_work);
450 
451 	return count;
452 }
453 
gb_tty_write_room(struct tty_struct * tty)454 static int gb_tty_write_room(struct tty_struct *tty)
455 {
456 	struct gb_tty *gb_tty = tty->driver_data;
457 	unsigned long flags;
458 	int room;
459 
460 	spin_lock_irqsave(&gb_tty->write_lock, flags);
461 	room = kfifo_avail(&gb_tty->write_fifo);
462 	spin_unlock_irqrestore(&gb_tty->write_lock, flags);
463 
464 	room -= GB_UART_WRITE_ROOM_MARGIN;
465 	if (room < 0)
466 		return 0;
467 
468 	return room;
469 }
470 
gb_tty_chars_in_buffer(struct tty_struct * tty)471 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
472 {
473 	struct gb_tty *gb_tty = tty->driver_data;
474 	unsigned long flags;
475 	int chars;
476 
477 	spin_lock_irqsave(&gb_tty->write_lock, flags);
478 	chars = kfifo_len(&gb_tty->write_fifo);
479 	if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
480 		chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
481 	spin_unlock_irqrestore(&gb_tty->write_lock, flags);
482 
483 	return chars;
484 }
485 
gb_tty_break_ctl(struct tty_struct * tty,int state)486 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
487 {
488 	struct gb_tty *gb_tty = tty->driver_data;
489 
490 	return send_break(gb_tty, state ? 1 : 0);
491 }
492 
gb_tty_set_termios(struct tty_struct * tty,struct ktermios * termios_old)493 static void gb_tty_set_termios(struct tty_struct *tty,
494 			       struct ktermios *termios_old)
495 {
496 	struct gb_tty *gb_tty = tty->driver_data;
497 	struct ktermios *termios = &tty->termios;
498 	struct gb_tty_line_coding newline;
499 	u8 newctrl = gb_tty->ctrlout;
500 
501 	newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
502 	newline.format = termios->c_cflag & CSTOPB ?
503 				GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
504 	newline.parity = termios->c_cflag & PARENB ?
505 				(termios->c_cflag & PARODD ? 1 : 2) +
506 				(termios->c_cflag & CMSPAR ? 2 : 0) : 0;
507 
508 	switch (termios->c_cflag & CSIZE) {
509 	case CS5:
510 		newline.data_bits = 5;
511 		break;
512 	case CS6:
513 		newline.data_bits = 6;
514 		break;
515 	case CS7:
516 		newline.data_bits = 7;
517 		break;
518 	case CS8:
519 	default:
520 		newline.data_bits = 8;
521 		break;
522 	}
523 
524 	/* FIXME: needs to clear unsupported bits in the termios */
525 	gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
526 
527 	if (C_BAUD(tty) == B0) {
528 		newline.rate = gb_tty->line_coding.rate;
529 		newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
530 	} else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
531 		newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
532 	}
533 
534 	if (newctrl != gb_tty->ctrlout) {
535 		gb_tty->ctrlout = newctrl;
536 		send_control(gb_tty, newctrl);
537 	}
538 
539 	if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
540 		newline.flow_control |= GB_SERIAL_AUTO_RTSCTS_EN;
541 	else
542 		newline.flow_control &= ~GB_SERIAL_AUTO_RTSCTS_EN;
543 
544 	if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
545 		memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
546 		send_line_coding(gb_tty);
547 	}
548 }
549 
gb_tty_tiocmget(struct tty_struct * tty)550 static int gb_tty_tiocmget(struct tty_struct *tty)
551 {
552 	struct gb_tty *gb_tty = tty->driver_data;
553 
554 	return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
555 	       (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
556 	       (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
557 	       (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
558 	       (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
559 	       TIOCM_CTS;
560 }
561 
gb_tty_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)562 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
563 			   unsigned int clear)
564 {
565 	struct gb_tty *gb_tty = tty->driver_data;
566 	u8 newctrl = gb_tty->ctrlout;
567 
568 	set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
569 	      (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
570 	clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
571 		(clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
572 
573 	newctrl = (newctrl & ~clear) | set;
574 	if (gb_tty->ctrlout == newctrl)
575 		return 0;
576 
577 	gb_tty->ctrlout = newctrl;
578 	return send_control(gb_tty, newctrl);
579 }
580 
gb_tty_throttle(struct tty_struct * tty)581 static void gb_tty_throttle(struct tty_struct *tty)
582 {
583 	struct gb_tty *gb_tty = tty->driver_data;
584 	unsigned char stop_char;
585 	int retval;
586 
587 	if (I_IXOFF(tty)) {
588 		stop_char = STOP_CHAR(tty);
589 		retval = gb_tty_write(tty, &stop_char, 1);
590 		if (retval <= 0)
591 			return;
592 	}
593 
594 	if (tty->termios.c_cflag & CRTSCTS) {
595 		gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
596 		retval = send_control(gb_tty, gb_tty->ctrlout);
597 	}
598 }
599 
gb_tty_unthrottle(struct tty_struct * tty)600 static void gb_tty_unthrottle(struct tty_struct *tty)
601 {
602 	struct gb_tty *gb_tty = tty->driver_data;
603 	unsigned char start_char;
604 	int retval;
605 
606 	if (I_IXOFF(tty)) {
607 		start_char = START_CHAR(tty);
608 		retval = gb_tty_write(tty, &start_char, 1);
609 		if (retval <= 0)
610 			return;
611 	}
612 
613 	if (tty->termios.c_cflag & CRTSCTS) {
614 		gb_tty->ctrlout |= GB_UART_CTRL_RTS;
615 		retval = send_control(gb_tty, gb_tty->ctrlout);
616 	}
617 }
618 
get_serial_info(struct gb_tty * gb_tty,struct serial_struct __user * info)619 static int get_serial_info(struct gb_tty *gb_tty,
620 			   struct serial_struct __user *info)
621 {
622 	struct serial_struct tmp;
623 
624 	memset(&tmp, 0, sizeof(tmp));
625 	tmp.type = PORT_16550A;
626 	tmp.line = gb_tty->minor;
627 	tmp.xmit_fifo_size = 16;
628 	tmp.baud_base = 9600;
629 	tmp.close_delay = gb_tty->port.close_delay / 10;
630 	tmp.closing_wait =
631 		gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
632 		ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
633 
634 	if (copy_to_user(info, &tmp, sizeof(tmp)))
635 		return -EFAULT;
636 	return 0;
637 }
638 
set_serial_info(struct gb_tty * gb_tty,struct serial_struct __user * newinfo)639 static int set_serial_info(struct gb_tty *gb_tty,
640 			   struct serial_struct __user *newinfo)
641 {
642 	struct serial_struct new_serial;
643 	unsigned int closing_wait;
644 	unsigned int close_delay;
645 	int retval = 0;
646 
647 	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
648 		return -EFAULT;
649 
650 	close_delay = new_serial.close_delay * 10;
651 	closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
652 			ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
653 
654 	mutex_lock(&gb_tty->port.mutex);
655 	if (!capable(CAP_SYS_ADMIN)) {
656 		if ((close_delay != gb_tty->port.close_delay) ||
657 		    (closing_wait != gb_tty->port.closing_wait))
658 			retval = -EPERM;
659 		else
660 			retval = -EOPNOTSUPP;
661 	} else {
662 		gb_tty->port.close_delay = close_delay;
663 		gb_tty->port.closing_wait = closing_wait;
664 	}
665 	mutex_unlock(&gb_tty->port.mutex);
666 	return retval;
667 }
668 
wait_serial_change(struct gb_tty * gb_tty,unsigned long arg)669 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
670 {
671 	int retval = 0;
672 	DECLARE_WAITQUEUE(wait, current);
673 	struct async_icount old;
674 	struct async_icount new;
675 
676 	if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
677 		return -EINVAL;
678 
679 	do {
680 		spin_lock_irq(&gb_tty->read_lock);
681 		old = gb_tty->oldcount;
682 		new = gb_tty->iocount;
683 		gb_tty->oldcount = new;
684 		spin_unlock_irq(&gb_tty->read_lock);
685 
686 		if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
687 			break;
688 		if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
689 			break;
690 		if ((arg & TIOCM_RI) && (old.rng != new.rng))
691 			break;
692 
693 		add_wait_queue(&gb_tty->wioctl, &wait);
694 		set_current_state(TASK_INTERRUPTIBLE);
695 		schedule();
696 		remove_wait_queue(&gb_tty->wioctl, &wait);
697 		if (gb_tty->disconnected) {
698 			if (arg & TIOCM_CD)
699 				break;
700 			retval = -ENODEV;
701 		} else if (signal_pending(current)) {
702 			retval = -ERESTARTSYS;
703 		}
704 	} while (!retval);
705 
706 	return retval;
707 }
708 
gb_tty_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)709 static int gb_tty_get_icount(struct tty_struct *tty,
710 			     struct serial_icounter_struct *icount)
711 {
712 	struct gb_tty *gb_tty = tty->driver_data;
713 
714 	icount->dsr = gb_tty->iocount.dsr;
715 	icount->rng = gb_tty->iocount.rng;
716 	icount->dcd = gb_tty->iocount.dcd;
717 	icount->frame = gb_tty->iocount.frame;
718 	icount->overrun = gb_tty->iocount.overrun;
719 	icount->parity = gb_tty->iocount.parity;
720 	icount->brk = gb_tty->iocount.brk;
721 
722 	return 0;
723 }
724 
gb_tty_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)725 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
726 			unsigned long arg)
727 {
728 	struct gb_tty *gb_tty = tty->driver_data;
729 
730 	switch (cmd) {
731 	case TIOCGSERIAL:
732 		return get_serial_info(gb_tty,
733 				       (struct serial_struct __user *)arg);
734 	case TIOCSSERIAL:
735 		return set_serial_info(gb_tty,
736 				       (struct serial_struct __user *)arg);
737 	case TIOCMIWAIT:
738 		return wait_serial_change(gb_tty, arg);
739 	}
740 
741 	return -ENOIOCTLCMD;
742 }
743 
gb_tty_dtr_rts(struct tty_port * port,int on)744 static void gb_tty_dtr_rts(struct tty_port *port, int on)
745 {
746 	struct gb_tty *gb_tty;
747 	u8 newctrl;
748 
749 	gb_tty = container_of(port, struct gb_tty, port);
750 	newctrl = gb_tty->ctrlout;
751 
752 	if (on)
753 		newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
754 	else
755 		newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
756 
757 	gb_tty->ctrlout = newctrl;
758 	send_control(gb_tty, newctrl);
759 }
760 
gb_tty_port_activate(struct tty_port * port,struct tty_struct * tty)761 static int gb_tty_port_activate(struct tty_port *port,
762 				struct tty_struct *tty)
763 {
764 	struct gb_tty *gb_tty;
765 
766 	gb_tty = container_of(port, struct gb_tty, port);
767 
768 	return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
769 }
770 
gb_tty_port_shutdown(struct tty_port * port)771 static void gb_tty_port_shutdown(struct tty_port *port)
772 {
773 	struct gb_tty *gb_tty;
774 	unsigned long flags;
775 	int ret;
776 
777 	gb_tty = container_of(port, struct gb_tty, port);
778 
779 	gb_tty->close_pending = true;
780 
781 	cancel_work_sync(&gb_tty->tx_work);
782 
783 	spin_lock_irqsave(&gb_tty->write_lock, flags);
784 	kfifo_reset_out(&gb_tty->write_fifo);
785 	spin_unlock_irqrestore(&gb_tty->write_lock, flags);
786 
787 	if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
788 		goto out;
789 
790 	ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
791 	if (ret) {
792 		dev_err(&gb_tty->gbphy_dev->dev,
793 			"error flushing transmitter: %d\n", ret);
794 	}
795 
796 	gb_uart_wait_for_all_credits(gb_tty);
797 
798 out:
799 	gb_tty->close_pending = false;
800 
801 	gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
802 }
803 
804 static const struct tty_operations gb_ops = {
805 	.install =		gb_tty_install,
806 	.open =			gb_tty_open,
807 	.close =		gb_tty_close,
808 	.cleanup =		gb_tty_cleanup,
809 	.hangup =		gb_tty_hangup,
810 	.write =		gb_tty_write,
811 	.write_room =		gb_tty_write_room,
812 	.ioctl =		gb_tty_ioctl,
813 	.throttle =		gb_tty_throttle,
814 	.unthrottle =		gb_tty_unthrottle,
815 	.chars_in_buffer =	gb_tty_chars_in_buffer,
816 	.break_ctl =		gb_tty_break_ctl,
817 	.set_termios =		gb_tty_set_termios,
818 	.tiocmget =		gb_tty_tiocmget,
819 	.tiocmset =		gb_tty_tiocmset,
820 	.get_icount =		gb_tty_get_icount,
821 };
822 
823 static const struct tty_port_operations gb_port_ops = {
824 	.dtr_rts =		gb_tty_dtr_rts,
825 	.activate =		gb_tty_port_activate,
826 	.shutdown =		gb_tty_port_shutdown,
827 };
828 
gb_uart_probe(struct gbphy_device * gbphy_dev,const struct gbphy_device_id * id)829 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
830 			 const struct gbphy_device_id *id)
831 {
832 	struct gb_connection *connection;
833 	size_t max_payload;
834 	struct gb_tty *gb_tty;
835 	struct device *tty_dev;
836 	int retval;
837 	int minor;
838 
839 	gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
840 	if (!gb_tty)
841 		return -ENOMEM;
842 
843 	connection = gb_connection_create(gbphy_dev->bundle,
844 					  le16_to_cpu(gbphy_dev->cport_desc->id),
845 					  gb_uart_request_handler);
846 	if (IS_ERR(connection)) {
847 		retval = PTR_ERR(connection);
848 		goto exit_tty_free;
849 	}
850 
851 	max_payload = gb_operation_get_payload_size_max(connection);
852 	if (max_payload < sizeof(struct gb_uart_send_data_request)) {
853 		retval = -EINVAL;
854 		goto exit_connection_destroy;
855 	}
856 
857 	gb_tty->buffer_payload_max = max_payload -
858 			sizeof(struct gb_uart_send_data_request);
859 
860 	gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
861 	if (!gb_tty->buffer) {
862 		retval = -ENOMEM;
863 		goto exit_connection_destroy;
864 	}
865 
866 	INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
867 
868 	retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
869 			     GFP_KERNEL);
870 	if (retval)
871 		goto exit_buf_free;
872 
873 	gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
874 	init_completion(&gb_tty->credits_complete);
875 
876 	minor = alloc_minor(gb_tty);
877 	if (minor < 0) {
878 		if (minor == -ENOSPC) {
879 			dev_err(&gbphy_dev->dev,
880 				"no more free minor numbers\n");
881 			retval = -ENODEV;
882 		} else {
883 			retval = minor;
884 		}
885 		goto exit_kfifo_free;
886 	}
887 
888 	gb_tty->minor = minor;
889 	spin_lock_init(&gb_tty->write_lock);
890 	spin_lock_init(&gb_tty->read_lock);
891 	init_waitqueue_head(&gb_tty->wioctl);
892 	mutex_init(&gb_tty->mutex);
893 
894 	tty_port_init(&gb_tty->port);
895 	gb_tty->port.ops = &gb_port_ops;
896 
897 	gb_tty->connection = connection;
898 	gb_tty->gbphy_dev = gbphy_dev;
899 	gb_connection_set_data(connection, gb_tty);
900 	gb_gbphy_set_data(gbphy_dev, gb_tty);
901 
902 	retval = gb_connection_enable_tx(connection);
903 	if (retval)
904 		goto exit_release_minor;
905 
906 	send_control(gb_tty, gb_tty->ctrlout);
907 
908 	/* initialize the uart to be 9600n81 */
909 	gb_tty->line_coding.rate = cpu_to_le32(9600);
910 	gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
911 	gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
912 	gb_tty->line_coding.data_bits = 8;
913 	send_line_coding(gb_tty);
914 
915 	retval = gb_connection_enable(connection);
916 	if (retval)
917 		goto exit_connection_disable;
918 
919 	tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
920 					   &gbphy_dev->dev);
921 	if (IS_ERR(tty_dev)) {
922 		retval = PTR_ERR(tty_dev);
923 		goto exit_connection_disable;
924 	}
925 
926 	gbphy_runtime_put_autosuspend(gbphy_dev);
927 	return 0;
928 
929 exit_connection_disable:
930 	gb_connection_disable(connection);
931 exit_release_minor:
932 	release_minor(gb_tty);
933 exit_kfifo_free:
934 	kfifo_free(&gb_tty->write_fifo);
935 exit_buf_free:
936 	kfree(gb_tty->buffer);
937 exit_connection_destroy:
938 	gb_connection_destroy(connection);
939 exit_tty_free:
940 	kfree(gb_tty);
941 
942 	return retval;
943 }
944 
gb_uart_remove(struct gbphy_device * gbphy_dev)945 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
946 {
947 	struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
948 	struct gb_connection *connection = gb_tty->connection;
949 	struct tty_struct *tty;
950 	int ret;
951 
952 	ret = gbphy_runtime_get_sync(gbphy_dev);
953 	if (ret)
954 		gbphy_runtime_get_noresume(gbphy_dev);
955 
956 	mutex_lock(&gb_tty->mutex);
957 	gb_tty->disconnected = true;
958 
959 	wake_up_all(&gb_tty->wioctl);
960 	mutex_unlock(&gb_tty->mutex);
961 
962 	tty = tty_port_tty_get(&gb_tty->port);
963 	if (tty) {
964 		tty_vhangup(tty);
965 		tty_kref_put(tty);
966 	}
967 
968 	gb_connection_disable_rx(connection);
969 	tty_unregister_device(gb_tty_driver, gb_tty->minor);
970 
971 	/* FIXME - free transmit / receive buffers */
972 
973 	gb_connection_disable(connection);
974 	tty_port_destroy(&gb_tty->port);
975 	gb_connection_destroy(connection);
976 	release_minor(gb_tty);
977 	kfifo_free(&gb_tty->write_fifo);
978 	kfree(gb_tty->buffer);
979 	kfree(gb_tty);
980 }
981 
gb_tty_init(void)982 static int gb_tty_init(void)
983 {
984 	int retval = 0;
985 
986 	gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
987 	if (IS_ERR(gb_tty_driver)) {
988 		pr_err("Can not allocate tty driver\n");
989 		retval = -ENOMEM;
990 		goto fail_unregister_dev;
991 	}
992 
993 	gb_tty_driver->driver_name = "gb";
994 	gb_tty_driver->name = GB_NAME;
995 	gb_tty_driver->major = 0;
996 	gb_tty_driver->minor_start = 0;
997 	gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
998 	gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
999 	gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1000 	gb_tty_driver->init_termios = tty_std_termios;
1001 	gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
1002 		CREAD | HUPCL | CLOCAL;
1003 	tty_set_operations(gb_tty_driver, &gb_ops);
1004 
1005 	retval = tty_register_driver(gb_tty_driver);
1006 	if (retval) {
1007 		pr_err("Can not register tty driver: %d\n", retval);
1008 		goto fail_put_gb_tty;
1009 	}
1010 
1011 	return 0;
1012 
1013 fail_put_gb_tty:
1014 	put_tty_driver(gb_tty_driver);
1015 fail_unregister_dev:
1016 	return retval;
1017 }
1018 
gb_tty_exit(void)1019 static void gb_tty_exit(void)
1020 {
1021 	tty_unregister_driver(gb_tty_driver);
1022 	put_tty_driver(gb_tty_driver);
1023 	idr_destroy(&tty_minors);
1024 }
1025 
1026 static const struct gbphy_device_id gb_uart_id_table[] = {
1027 	{ GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
1028 	{ },
1029 };
1030 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
1031 
1032 static struct gbphy_driver uart_driver = {
1033 	.name		= "uart",
1034 	.probe		= gb_uart_probe,
1035 	.remove		= gb_uart_remove,
1036 	.id_table	= gb_uart_id_table,
1037 };
1038 
gb_uart_driver_init(void)1039 static int gb_uart_driver_init(void)
1040 {
1041 	int ret;
1042 
1043 	ret = gb_tty_init();
1044 	if (ret)
1045 		return ret;
1046 
1047 	ret = gb_gbphy_register(&uart_driver);
1048 	if (ret) {
1049 		gb_tty_exit();
1050 		return ret;
1051 	}
1052 
1053 	return 0;
1054 }
1055 module_init(gb_uart_driver_init);
1056 
gb_uart_driver_exit(void)1057 static void gb_uart_driver_exit(void)
1058 {
1059 	gb_gbphy_deregister(&uart_driver);
1060 	gb_tty_exit();
1061 }
1062 
1063 module_exit(gb_uart_driver_exit);
1064 MODULE_LICENSE("GPL v2");
1065