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