Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * UART driver for the Greybus "generic" UART module.
0004  *
0005  * Copyright 2014 Google Inc.
0006  * Copyright 2014 Linaro Ltd.
0007  *
0008  * Heavily based on drivers/usb/class/cdc-acm.c and
0009  * drivers/usb/serial/usb-serial.c.
0010  */
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/errno.h>
0015 #include <linux/module.h>
0016 #include <linux/sched/signal.h>
0017 #include <linux/wait.h>
0018 #include <linux/slab.h>
0019 #include <linux/uaccess.h>
0020 #include <linux/mutex.h>
0021 #include <linux/tty.h>
0022 #include <linux/serial.h>
0023 #include <linux/tty_driver.h>
0024 #include <linux/tty_flip.h>
0025 #include <linux/idr.h>
0026 #include <linux/fs.h>
0027 #include <linux/kdev_t.h>
0028 #include <linux/kfifo.h>
0029 #include <linux/workqueue.h>
0030 #include <linux/completion.h>
0031 #include <linux/greybus.h>
0032 
0033 #include "gbphy.h"
0034 
0035 #define GB_NUM_MINORS   16  /* 16 is more than enough */
0036 #define GB_NAME     "ttyGB"
0037 
0038 #define GB_UART_WRITE_FIFO_SIZE     PAGE_SIZE
0039 #define GB_UART_WRITE_ROOM_MARGIN   1   /* leave some space in fifo */
0040 #define GB_UART_FIRMWARE_CREDITS    4096
0041 #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC    10000
0042 
0043 struct gb_tty {
0044     struct gbphy_device *gbphy_dev;
0045     struct tty_port port;
0046     void *buffer;
0047     size_t buffer_payload_max;
0048     struct gb_connection *connection;
0049     u16 cport_id;
0050     unsigned int minor;
0051     unsigned char clocal;
0052     bool disconnected;
0053     spinlock_t read_lock;
0054     spinlock_t write_lock;
0055     struct async_icount iocount;
0056     struct async_icount oldcount;
0057     wait_queue_head_t wioctl;
0058     struct mutex mutex;
0059     u8 ctrlin;  /* input control lines */
0060     u8 ctrlout; /* output control lines */
0061     struct gb_uart_set_line_coding_request line_coding;
0062     struct work_struct tx_work;
0063     struct kfifo write_fifo;
0064     bool close_pending;
0065     unsigned int credits;
0066     struct completion credits_complete;
0067 };
0068 
0069 static struct tty_driver *gb_tty_driver;
0070 static DEFINE_IDR(tty_minors);
0071 static DEFINE_MUTEX(table_lock);
0072 
0073 static int gb_uart_receive_data_handler(struct gb_operation *op)
0074 {
0075     struct gb_connection *connection = op->connection;
0076     struct gb_tty *gb_tty = gb_connection_get_data(connection);
0077     struct tty_port *port = &gb_tty->port;
0078     struct gb_message *request = op->request;
0079     struct gb_uart_recv_data_request *receive_data;
0080     u16 recv_data_size;
0081     int count;
0082     unsigned long tty_flags = TTY_NORMAL;
0083 
0084     if (request->payload_size < sizeof(*receive_data)) {
0085         dev_err(&gb_tty->gbphy_dev->dev,
0086             "short receive-data request received (%zu < %zu)\n",
0087             request->payload_size, sizeof(*receive_data));
0088         return -EINVAL;
0089     }
0090 
0091     receive_data = op->request->payload;
0092     recv_data_size = le16_to_cpu(receive_data->size);
0093 
0094     if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
0095         dev_err(&gb_tty->gbphy_dev->dev,
0096             "malformed receive-data request received (%u != %zu)\n",
0097             recv_data_size,
0098             request->payload_size - sizeof(*receive_data));
0099         return -EINVAL;
0100     }
0101 
0102     if (!recv_data_size)
0103         return -EINVAL;
0104 
0105     if (receive_data->flags) {
0106         if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
0107             tty_flags = TTY_BREAK;
0108         else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
0109             tty_flags = TTY_PARITY;
0110         else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
0111             tty_flags = TTY_FRAME;
0112 
0113         /* overrun is special, not associated with a char */
0114         if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
0115             tty_insert_flip_char(port, 0, TTY_OVERRUN);
0116     }
0117     count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
0118                           tty_flags, recv_data_size);
0119     if (count != recv_data_size) {
0120         dev_err(&gb_tty->gbphy_dev->dev,
0121             "UART: RX 0x%08x bytes only wrote 0x%08x\n",
0122             recv_data_size, count);
0123     }
0124     if (count)
0125         tty_flip_buffer_push(port);
0126     return 0;
0127 }
0128 
0129 static int gb_uart_serial_state_handler(struct gb_operation *op)
0130 {
0131     struct gb_connection *connection = op->connection;
0132     struct gb_tty *gb_tty = gb_connection_get_data(connection);
0133     struct gb_message *request = op->request;
0134     struct gb_uart_serial_state_request *serial_state;
0135 
0136     if (request->payload_size < sizeof(*serial_state)) {
0137         dev_err(&gb_tty->gbphy_dev->dev,
0138             "short serial-state event received (%zu < %zu)\n",
0139             request->payload_size, sizeof(*serial_state));
0140         return -EINVAL;
0141     }
0142 
0143     serial_state = request->payload;
0144     gb_tty->ctrlin = serial_state->control;
0145 
0146     return 0;
0147 }
0148 
0149 static int gb_uart_receive_credits_handler(struct gb_operation *op)
0150 {
0151     struct gb_connection *connection = op->connection;
0152     struct gb_tty *gb_tty = gb_connection_get_data(connection);
0153     struct gb_message *request = op->request;
0154     struct gb_uart_receive_credits_request *credit_request;
0155     unsigned long flags;
0156     unsigned int incoming_credits;
0157     int ret = 0;
0158 
0159     if (request->payload_size < sizeof(*credit_request)) {
0160         dev_err(&gb_tty->gbphy_dev->dev,
0161             "short receive_credits event received (%zu < %zu)\n",
0162             request->payload_size,
0163             sizeof(*credit_request));
0164         return -EINVAL;
0165     }
0166 
0167     credit_request = request->payload;
0168     incoming_credits = le16_to_cpu(credit_request->count);
0169 
0170     spin_lock_irqsave(&gb_tty->write_lock, flags);
0171     gb_tty->credits += incoming_credits;
0172     if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
0173         gb_tty->credits -= incoming_credits;
0174         ret = -EINVAL;
0175     }
0176     spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0177 
0178     if (ret) {
0179         dev_err(&gb_tty->gbphy_dev->dev,
0180             "invalid number of incoming credits: %d\n",
0181             incoming_credits);
0182         return ret;
0183     }
0184 
0185     if (!gb_tty->close_pending)
0186         schedule_work(&gb_tty->tx_work);
0187 
0188     /*
0189      * the port the tty layer may be waiting for credits
0190      */
0191     tty_port_tty_wakeup(&gb_tty->port);
0192 
0193     if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
0194         complete(&gb_tty->credits_complete);
0195 
0196     return ret;
0197 }
0198 
0199 static int gb_uart_request_handler(struct gb_operation *op)
0200 {
0201     struct gb_connection *connection = op->connection;
0202     struct gb_tty *gb_tty = gb_connection_get_data(connection);
0203     int type = op->type;
0204     int ret;
0205 
0206     switch (type) {
0207     case GB_UART_TYPE_RECEIVE_DATA:
0208         ret = gb_uart_receive_data_handler(op);
0209         break;
0210     case GB_UART_TYPE_SERIAL_STATE:
0211         ret = gb_uart_serial_state_handler(op);
0212         break;
0213     case GB_UART_TYPE_RECEIVE_CREDITS:
0214         ret = gb_uart_receive_credits_handler(op);
0215         break;
0216     default:
0217         dev_err(&gb_tty->gbphy_dev->dev,
0218             "unsupported unsolicited request: 0x%02x\n", type);
0219         ret = -EINVAL;
0220     }
0221 
0222     return ret;
0223 }
0224 
0225 static void  gb_uart_tx_write_work(struct work_struct *work)
0226 {
0227     struct gb_uart_send_data_request *request;
0228     struct gb_tty *gb_tty;
0229     unsigned long flags;
0230     unsigned int send_size;
0231     int ret;
0232 
0233     gb_tty = container_of(work, struct gb_tty, tx_work);
0234     request = gb_tty->buffer;
0235 
0236     while (1) {
0237         if (gb_tty->close_pending)
0238             break;
0239 
0240         spin_lock_irqsave(&gb_tty->write_lock, flags);
0241         send_size = gb_tty->buffer_payload_max;
0242         if (send_size > gb_tty->credits)
0243             send_size = gb_tty->credits;
0244 
0245         send_size = kfifo_out_peek(&gb_tty->write_fifo,
0246                        &request->data[0],
0247                        send_size);
0248         if (!send_size) {
0249             spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0250             break;
0251         }
0252 
0253         gb_tty->credits -= send_size;
0254         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0255 
0256         request->size = cpu_to_le16(send_size);
0257         ret = gb_operation_sync(gb_tty->connection,
0258                     GB_UART_TYPE_SEND_DATA,
0259                     request, sizeof(*request) + send_size,
0260                     NULL, 0);
0261         if (ret) {
0262             dev_err(&gb_tty->gbphy_dev->dev,
0263                 "send data error: %d\n", ret);
0264             spin_lock_irqsave(&gb_tty->write_lock, flags);
0265             gb_tty->credits += send_size;
0266             spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0267             if (!gb_tty->close_pending)
0268                 schedule_work(work);
0269             return;
0270         }
0271 
0272         spin_lock_irqsave(&gb_tty->write_lock, flags);
0273         ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
0274                 send_size);
0275         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0276 
0277         tty_port_tty_wakeup(&gb_tty->port);
0278     }
0279 }
0280 
0281 static int send_line_coding(struct gb_tty *tty)
0282 {
0283     return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
0284                  &tty->line_coding, sizeof(tty->line_coding),
0285                  NULL, 0);
0286 }
0287 
0288 static int send_control(struct gb_tty *gb_tty, u8 control)
0289 {
0290     struct gb_uart_set_control_line_state_request request;
0291 
0292     request.control = control;
0293     return gb_operation_sync(gb_tty->connection,
0294                  GB_UART_TYPE_SET_CONTROL_LINE_STATE,
0295                  &request, sizeof(request), NULL, 0);
0296 }
0297 
0298 static int send_break(struct gb_tty *gb_tty, u8 state)
0299 {
0300     struct gb_uart_set_break_request request;
0301 
0302     if ((state != 0) && (state != 1)) {
0303         dev_err(&gb_tty->gbphy_dev->dev,
0304             "invalid break state of %d\n", state);
0305         return -EINVAL;
0306     }
0307 
0308     request.state = state;
0309     return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
0310                  &request, sizeof(request), NULL, 0);
0311 }
0312 
0313 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
0314 {
0315     int ret;
0316 
0317     if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
0318         return 0;
0319 
0320     ret = wait_for_completion_timeout(&gb_tty->credits_complete,
0321             msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
0322     if (!ret) {
0323         dev_err(&gb_tty->gbphy_dev->dev,
0324             "time out waiting for credits\n");
0325         return -ETIMEDOUT;
0326     }
0327 
0328     return 0;
0329 }
0330 
0331 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
0332 {
0333     struct gb_uart_serial_flush_request request;
0334 
0335     request.flags = flags;
0336     return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
0337                  &request, sizeof(request), NULL, 0);
0338 }
0339 
0340 static struct gb_tty *get_gb_by_minor(unsigned int minor)
0341 {
0342     struct gb_tty *gb_tty;
0343 
0344     mutex_lock(&table_lock);
0345     gb_tty = idr_find(&tty_minors, minor);
0346     if (gb_tty) {
0347         mutex_lock(&gb_tty->mutex);
0348         if (gb_tty->disconnected) {
0349             mutex_unlock(&gb_tty->mutex);
0350             gb_tty = NULL;
0351         } else {
0352             tty_port_get(&gb_tty->port);
0353             mutex_unlock(&gb_tty->mutex);
0354         }
0355     }
0356     mutex_unlock(&table_lock);
0357     return gb_tty;
0358 }
0359 
0360 static int alloc_minor(struct gb_tty *gb_tty)
0361 {
0362     int minor;
0363 
0364     mutex_lock(&table_lock);
0365     minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
0366     mutex_unlock(&table_lock);
0367     if (minor >= 0)
0368         gb_tty->minor = minor;
0369     return minor;
0370 }
0371 
0372 static void release_minor(struct gb_tty *gb_tty)
0373 {
0374     int minor = gb_tty->minor;
0375 
0376     gb_tty->minor = 0;  /* Maybe should use an invalid value instead */
0377     mutex_lock(&table_lock);
0378     idr_remove(&tty_minors, minor);
0379     mutex_unlock(&table_lock);
0380 }
0381 
0382 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
0383 {
0384     struct gb_tty *gb_tty;
0385     int retval;
0386 
0387     gb_tty = get_gb_by_minor(tty->index);
0388     if (!gb_tty)
0389         return -ENODEV;
0390 
0391     retval = tty_standard_install(driver, tty);
0392     if (retval)
0393         goto error;
0394 
0395     tty->driver_data = gb_tty;
0396     return 0;
0397 error:
0398     tty_port_put(&gb_tty->port);
0399     return retval;
0400 }
0401 
0402 static int gb_tty_open(struct tty_struct *tty, struct file *file)
0403 {
0404     struct gb_tty *gb_tty = tty->driver_data;
0405 
0406     return tty_port_open(&gb_tty->port, tty, file);
0407 }
0408 
0409 static void gb_tty_close(struct tty_struct *tty, struct file *file)
0410 {
0411     struct gb_tty *gb_tty = tty->driver_data;
0412 
0413     tty_port_close(&gb_tty->port, tty, file);
0414 }
0415 
0416 static void gb_tty_cleanup(struct tty_struct *tty)
0417 {
0418     struct gb_tty *gb_tty = tty->driver_data;
0419 
0420     tty_port_put(&gb_tty->port);
0421 }
0422 
0423 static void gb_tty_hangup(struct tty_struct *tty)
0424 {
0425     struct gb_tty *gb_tty = tty->driver_data;
0426 
0427     tty_port_hangup(&gb_tty->port);
0428 }
0429 
0430 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
0431             int count)
0432 {
0433     struct gb_tty *gb_tty = tty->driver_data;
0434 
0435     count =  kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
0436                      &gb_tty->write_lock);
0437     if (count && !gb_tty->close_pending)
0438         schedule_work(&gb_tty->tx_work);
0439 
0440     return count;
0441 }
0442 
0443 static unsigned int gb_tty_write_room(struct tty_struct *tty)
0444 {
0445     struct gb_tty *gb_tty = tty->driver_data;
0446     unsigned long flags;
0447     int room;
0448 
0449     spin_lock_irqsave(&gb_tty->write_lock, flags);
0450     room = kfifo_avail(&gb_tty->write_fifo);
0451     spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0452 
0453     room -= GB_UART_WRITE_ROOM_MARGIN;
0454     if (room < 0)
0455         return 0;
0456 
0457     return room;
0458 }
0459 
0460 static unsigned int gb_tty_chars_in_buffer(struct tty_struct *tty)
0461 {
0462     struct gb_tty *gb_tty = tty->driver_data;
0463     unsigned long flags;
0464     unsigned int chars;
0465 
0466     spin_lock_irqsave(&gb_tty->write_lock, flags);
0467     chars = kfifo_len(&gb_tty->write_fifo);
0468     if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
0469         chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
0470     spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0471 
0472     return chars;
0473 }
0474 
0475 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
0476 {
0477     struct gb_tty *gb_tty = tty->driver_data;
0478 
0479     return send_break(gb_tty, state ? 1 : 0);
0480 }
0481 
0482 static void gb_tty_set_termios(struct tty_struct *tty,
0483                    struct ktermios *termios_old)
0484 {
0485     struct gb_uart_set_line_coding_request newline;
0486     struct gb_tty *gb_tty = tty->driver_data;
0487     struct ktermios *termios = &tty->termios;
0488     u8 newctrl = gb_tty->ctrlout;
0489 
0490     newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
0491     newline.format = termios->c_cflag & CSTOPB ?
0492                 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
0493     newline.parity = termios->c_cflag & PARENB ?
0494                 (termios->c_cflag & PARODD ? 1 : 2) +
0495                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
0496 
0497     newline.data_bits = tty_get_char_size(termios->c_cflag);
0498 
0499     /* FIXME: needs to clear unsupported bits in the termios */
0500     gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
0501 
0502     if (C_BAUD(tty) == B0) {
0503         newline.rate = gb_tty->line_coding.rate;
0504         newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
0505     } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
0506         newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
0507     }
0508 
0509     if (newctrl != gb_tty->ctrlout) {
0510         gb_tty->ctrlout = newctrl;
0511         send_control(gb_tty, newctrl);
0512     }
0513 
0514     if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
0515         newline.flow_control = GB_SERIAL_AUTO_RTSCTS_EN;
0516     else
0517         newline.flow_control = 0;
0518 
0519     if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
0520         memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
0521         send_line_coding(gb_tty);
0522     }
0523 }
0524 
0525 static int gb_tty_tiocmget(struct tty_struct *tty)
0526 {
0527     struct gb_tty *gb_tty = tty->driver_data;
0528 
0529     return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
0530            (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
0531            (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
0532            (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
0533            (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
0534            TIOCM_CTS;
0535 }
0536 
0537 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
0538                unsigned int clear)
0539 {
0540     struct gb_tty *gb_tty = tty->driver_data;
0541     u8 newctrl = gb_tty->ctrlout;
0542 
0543     set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
0544           (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
0545     clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
0546         (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
0547 
0548     newctrl = (newctrl & ~clear) | set;
0549     if (gb_tty->ctrlout == newctrl)
0550         return 0;
0551 
0552     gb_tty->ctrlout = newctrl;
0553     return send_control(gb_tty, newctrl);
0554 }
0555 
0556 static void gb_tty_throttle(struct tty_struct *tty)
0557 {
0558     struct gb_tty *gb_tty = tty->driver_data;
0559     unsigned char stop_char;
0560     int retval;
0561 
0562     if (I_IXOFF(tty)) {
0563         stop_char = STOP_CHAR(tty);
0564         retval = gb_tty_write(tty, &stop_char, 1);
0565         if (retval <= 0)
0566             return;
0567     }
0568 
0569     if (tty->termios.c_cflag & CRTSCTS) {
0570         gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
0571         retval = send_control(gb_tty, gb_tty->ctrlout);
0572     }
0573 }
0574 
0575 static void gb_tty_unthrottle(struct tty_struct *tty)
0576 {
0577     struct gb_tty *gb_tty = tty->driver_data;
0578     unsigned char start_char;
0579     int retval;
0580 
0581     if (I_IXOFF(tty)) {
0582         start_char = START_CHAR(tty);
0583         retval = gb_tty_write(tty, &start_char, 1);
0584         if (retval <= 0)
0585             return;
0586     }
0587 
0588     if (tty->termios.c_cflag & CRTSCTS) {
0589         gb_tty->ctrlout |= GB_UART_CTRL_RTS;
0590         retval = send_control(gb_tty, gb_tty->ctrlout);
0591     }
0592 }
0593 
0594 static int get_serial_info(struct tty_struct *tty,
0595                struct serial_struct *ss)
0596 {
0597     struct gb_tty *gb_tty = tty->driver_data;
0598 
0599     ss->line = gb_tty->minor;
0600     ss->close_delay = jiffies_to_msecs(gb_tty->port.close_delay) / 10;
0601     ss->closing_wait =
0602         gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
0603         ASYNC_CLOSING_WAIT_NONE :
0604         jiffies_to_msecs(gb_tty->port.closing_wait) / 10;
0605 
0606     return 0;
0607 }
0608 
0609 static int set_serial_info(struct tty_struct *tty,
0610                struct serial_struct *ss)
0611 {
0612     struct gb_tty *gb_tty = tty->driver_data;
0613     unsigned int closing_wait;
0614     unsigned int close_delay;
0615     int retval = 0;
0616 
0617     close_delay = msecs_to_jiffies(ss->close_delay * 10);
0618     closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
0619             ASYNC_CLOSING_WAIT_NONE :
0620             msecs_to_jiffies(ss->closing_wait * 10);
0621 
0622     mutex_lock(&gb_tty->port.mutex);
0623     if (!capable(CAP_SYS_ADMIN)) {
0624         if ((close_delay != gb_tty->port.close_delay) ||
0625             (closing_wait != gb_tty->port.closing_wait))
0626             retval = -EPERM;
0627     } else {
0628         gb_tty->port.close_delay = close_delay;
0629         gb_tty->port.closing_wait = closing_wait;
0630     }
0631     mutex_unlock(&gb_tty->port.mutex);
0632     return retval;
0633 }
0634 
0635 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
0636 {
0637     int retval = 0;
0638     DECLARE_WAITQUEUE(wait, current);
0639     struct async_icount old;
0640     struct async_icount new;
0641 
0642     if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
0643         return -EINVAL;
0644 
0645     do {
0646         spin_lock_irq(&gb_tty->read_lock);
0647         old = gb_tty->oldcount;
0648         new = gb_tty->iocount;
0649         gb_tty->oldcount = new;
0650         spin_unlock_irq(&gb_tty->read_lock);
0651 
0652         if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
0653             break;
0654         if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
0655             break;
0656         if ((arg & TIOCM_RI) && (old.rng != new.rng))
0657             break;
0658 
0659         add_wait_queue(&gb_tty->wioctl, &wait);
0660         set_current_state(TASK_INTERRUPTIBLE);
0661         schedule();
0662         remove_wait_queue(&gb_tty->wioctl, &wait);
0663         if (gb_tty->disconnected) {
0664             if (arg & TIOCM_CD)
0665                 break;
0666             retval = -ENODEV;
0667         } else if (signal_pending(current)) {
0668             retval = -ERESTARTSYS;
0669         }
0670     } while (!retval);
0671 
0672     return retval;
0673 }
0674 
0675 static int gb_tty_get_icount(struct tty_struct *tty,
0676                  struct serial_icounter_struct *icount)
0677 {
0678     struct gb_tty *gb_tty = tty->driver_data;
0679 
0680     icount->dsr = gb_tty->iocount.dsr;
0681     icount->rng = gb_tty->iocount.rng;
0682     icount->dcd = gb_tty->iocount.dcd;
0683     icount->frame = gb_tty->iocount.frame;
0684     icount->overrun = gb_tty->iocount.overrun;
0685     icount->parity = gb_tty->iocount.parity;
0686     icount->brk = gb_tty->iocount.brk;
0687 
0688     return 0;
0689 }
0690 
0691 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
0692             unsigned long arg)
0693 {
0694     struct gb_tty *gb_tty = tty->driver_data;
0695 
0696     switch (cmd) {
0697     case TIOCMIWAIT:
0698         return wait_serial_change(gb_tty, arg);
0699     }
0700 
0701     return -ENOIOCTLCMD;
0702 }
0703 
0704 static void gb_tty_dtr_rts(struct tty_port *port, int on)
0705 {
0706     struct gb_tty *gb_tty;
0707     u8 newctrl;
0708 
0709     gb_tty = container_of(port, struct gb_tty, port);
0710     newctrl = gb_tty->ctrlout;
0711 
0712     if (on)
0713         newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
0714     else
0715         newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
0716 
0717     gb_tty->ctrlout = newctrl;
0718     send_control(gb_tty, newctrl);
0719 }
0720 
0721 static int gb_tty_port_activate(struct tty_port *port,
0722                 struct tty_struct *tty)
0723 {
0724     struct gb_tty *gb_tty;
0725 
0726     gb_tty = container_of(port, struct gb_tty, port);
0727 
0728     return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
0729 }
0730 
0731 static void gb_tty_port_shutdown(struct tty_port *port)
0732 {
0733     struct gb_tty *gb_tty;
0734     unsigned long flags;
0735     int ret;
0736 
0737     gb_tty = container_of(port, struct gb_tty, port);
0738 
0739     gb_tty->close_pending = true;
0740 
0741     cancel_work_sync(&gb_tty->tx_work);
0742 
0743     spin_lock_irqsave(&gb_tty->write_lock, flags);
0744     kfifo_reset_out(&gb_tty->write_fifo);
0745     spin_unlock_irqrestore(&gb_tty->write_lock, flags);
0746 
0747     if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
0748         goto out;
0749 
0750     ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
0751     if (ret) {
0752         dev_err(&gb_tty->gbphy_dev->dev,
0753             "error flushing transmitter: %d\n", ret);
0754     }
0755 
0756     gb_uart_wait_for_all_credits(gb_tty);
0757 
0758 out:
0759     gb_tty->close_pending = false;
0760 
0761     gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
0762 }
0763 
0764 static void gb_tty_port_destruct(struct tty_port *port)
0765 {
0766     struct gb_tty *gb_tty = container_of(port, struct gb_tty, port);
0767 
0768     if (gb_tty->minor != GB_NUM_MINORS)
0769         release_minor(gb_tty);
0770     kfifo_free(&gb_tty->write_fifo);
0771     kfree(gb_tty->buffer);
0772     kfree(gb_tty);
0773 }
0774 
0775 static const struct tty_operations gb_ops = {
0776     .install =      gb_tty_install,
0777     .open =         gb_tty_open,
0778     .close =        gb_tty_close,
0779     .cleanup =      gb_tty_cleanup,
0780     .hangup =       gb_tty_hangup,
0781     .write =        gb_tty_write,
0782     .write_room =       gb_tty_write_room,
0783     .ioctl =        gb_tty_ioctl,
0784     .throttle =     gb_tty_throttle,
0785     .unthrottle =       gb_tty_unthrottle,
0786     .chars_in_buffer =  gb_tty_chars_in_buffer,
0787     .break_ctl =        gb_tty_break_ctl,
0788     .set_termios =      gb_tty_set_termios,
0789     .tiocmget =     gb_tty_tiocmget,
0790     .tiocmset =     gb_tty_tiocmset,
0791     .get_icount =       gb_tty_get_icount,
0792     .set_serial =       set_serial_info,
0793     .get_serial =       get_serial_info,
0794 };
0795 
0796 static const struct tty_port_operations gb_port_ops = {
0797     .dtr_rts =      gb_tty_dtr_rts,
0798     .activate =     gb_tty_port_activate,
0799     .shutdown =     gb_tty_port_shutdown,
0800     .destruct =     gb_tty_port_destruct,
0801 };
0802 
0803 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
0804              const struct gbphy_device_id *id)
0805 {
0806     struct gb_connection *connection;
0807     size_t max_payload;
0808     struct gb_tty *gb_tty;
0809     struct device *tty_dev;
0810     int retval;
0811     int minor;
0812 
0813     connection = gb_connection_create(gbphy_dev->bundle,
0814                       le16_to_cpu(gbphy_dev->cport_desc->id),
0815                       gb_uart_request_handler);
0816     if (IS_ERR(connection))
0817         return PTR_ERR(connection);
0818 
0819     max_payload = gb_operation_get_payload_size_max(connection);
0820     if (max_payload < sizeof(struct gb_uart_send_data_request)) {
0821         retval = -EINVAL;
0822         goto exit_connection_destroy;
0823     }
0824 
0825     gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
0826     if (!gb_tty) {
0827         retval = -ENOMEM;
0828         goto exit_connection_destroy;
0829     }
0830 
0831     tty_port_init(&gb_tty->port);
0832     gb_tty->port.ops = &gb_port_ops;
0833     gb_tty->minor = GB_NUM_MINORS;
0834 
0835     gb_tty->buffer_payload_max = max_payload -
0836             sizeof(struct gb_uart_send_data_request);
0837 
0838     gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
0839     if (!gb_tty->buffer) {
0840         retval = -ENOMEM;
0841         goto exit_put_port;
0842     }
0843 
0844     INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
0845 
0846     retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
0847                  GFP_KERNEL);
0848     if (retval)
0849         goto exit_put_port;
0850 
0851     gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
0852     init_completion(&gb_tty->credits_complete);
0853 
0854     minor = alloc_minor(gb_tty);
0855     if (minor < 0) {
0856         if (minor == -ENOSPC) {
0857             dev_err(&gbphy_dev->dev,
0858                 "no more free minor numbers\n");
0859             retval = -ENODEV;
0860         } else {
0861             retval = minor;
0862         }
0863         goto exit_put_port;
0864     }
0865 
0866     gb_tty->minor = minor;
0867     spin_lock_init(&gb_tty->write_lock);
0868     spin_lock_init(&gb_tty->read_lock);
0869     init_waitqueue_head(&gb_tty->wioctl);
0870     mutex_init(&gb_tty->mutex);
0871 
0872     gb_tty->connection = connection;
0873     gb_tty->gbphy_dev = gbphy_dev;
0874     gb_connection_set_data(connection, gb_tty);
0875     gb_gbphy_set_data(gbphy_dev, gb_tty);
0876 
0877     retval = gb_connection_enable_tx(connection);
0878     if (retval)
0879         goto exit_put_port;
0880 
0881     send_control(gb_tty, gb_tty->ctrlout);
0882 
0883     /* initialize the uart to be 9600n81 */
0884     gb_tty->line_coding.rate = cpu_to_le32(9600);
0885     gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
0886     gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
0887     gb_tty->line_coding.data_bits = 8;
0888     send_line_coding(gb_tty);
0889 
0890     retval = gb_connection_enable(connection);
0891     if (retval)
0892         goto exit_connection_disable;
0893 
0894     tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
0895                        &gbphy_dev->dev);
0896     if (IS_ERR(tty_dev)) {
0897         retval = PTR_ERR(tty_dev);
0898         goto exit_connection_disable;
0899     }
0900 
0901     gbphy_runtime_put_autosuspend(gbphy_dev);
0902     return 0;
0903 
0904 exit_connection_disable:
0905     gb_connection_disable(connection);
0906 exit_put_port:
0907     tty_port_put(&gb_tty->port);
0908 exit_connection_destroy:
0909     gb_connection_destroy(connection);
0910 
0911     return retval;
0912 }
0913 
0914 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
0915 {
0916     struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
0917     struct gb_connection *connection = gb_tty->connection;
0918     struct tty_struct *tty;
0919     int ret;
0920 
0921     ret = gbphy_runtime_get_sync(gbphy_dev);
0922     if (ret)
0923         gbphy_runtime_get_noresume(gbphy_dev);
0924 
0925     mutex_lock(&gb_tty->mutex);
0926     gb_tty->disconnected = true;
0927 
0928     wake_up_all(&gb_tty->wioctl);
0929     mutex_unlock(&gb_tty->mutex);
0930 
0931     tty = tty_port_tty_get(&gb_tty->port);
0932     if (tty) {
0933         tty_vhangup(tty);
0934         tty_kref_put(tty);
0935     }
0936 
0937     gb_connection_disable_rx(connection);
0938     tty_unregister_device(gb_tty_driver, gb_tty->minor);
0939 
0940     gb_connection_disable(connection);
0941     gb_connection_destroy(connection);
0942 
0943     tty_port_put(&gb_tty->port);
0944 }
0945 
0946 static int gb_tty_init(void)
0947 {
0948     int retval = 0;
0949 
0950     gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
0951     if (IS_ERR(gb_tty_driver)) {
0952         pr_err("Can not allocate tty driver\n");
0953         retval = -ENOMEM;
0954         goto fail_unregister_dev;
0955     }
0956 
0957     gb_tty_driver->driver_name = "gb";
0958     gb_tty_driver->name = GB_NAME;
0959     gb_tty_driver->major = 0;
0960     gb_tty_driver->minor_start = 0;
0961     gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
0962     gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
0963     gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
0964     gb_tty_driver->init_termios = tty_std_termios;
0965     gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
0966         CREAD | HUPCL | CLOCAL;
0967     tty_set_operations(gb_tty_driver, &gb_ops);
0968 
0969     retval = tty_register_driver(gb_tty_driver);
0970     if (retval) {
0971         pr_err("Can not register tty driver: %d\n", retval);
0972         goto fail_put_gb_tty;
0973     }
0974 
0975     return 0;
0976 
0977 fail_put_gb_tty:
0978     tty_driver_kref_put(gb_tty_driver);
0979 fail_unregister_dev:
0980     return retval;
0981 }
0982 
0983 static void gb_tty_exit(void)
0984 {
0985     tty_unregister_driver(gb_tty_driver);
0986     tty_driver_kref_put(gb_tty_driver);
0987     idr_destroy(&tty_minors);
0988 }
0989 
0990 static const struct gbphy_device_id gb_uart_id_table[] = {
0991     { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
0992     { },
0993 };
0994 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
0995 
0996 static struct gbphy_driver uart_driver = {
0997     .name       = "uart",
0998     .probe      = gb_uart_probe,
0999     .remove     = gb_uart_remove,
1000     .id_table   = gb_uart_id_table,
1001 };
1002 
1003 static int gb_uart_driver_init(void)
1004 {
1005     int ret;
1006 
1007     ret = gb_tty_init();
1008     if (ret)
1009         return ret;
1010 
1011     ret = gb_gbphy_register(&uart_driver);
1012     if (ret) {
1013         gb_tty_exit();
1014         return ret;
1015     }
1016 
1017     return 0;
1018 }
1019 module_init(gb_uart_driver_init);
1020 
1021 static void gb_uart_driver_exit(void)
1022 {
1023     gb_gbphy_deregister(&uart_driver);
1024     gb_tty_exit();
1025 }
1026 
1027 module_exit(gb_uart_driver_exit);
1028 MODULE_LICENSE("GPL v2");