0001
0002
0003
0004
0005 #include <linux/kernel.h>
0006 #include <linux/serdev.h>
0007 #include <linux/tty.h>
0008 #include <linux/tty_driver.h>
0009 #include <linux/poll.h>
0010
0011 #define SERPORT_ACTIVE 1
0012
0013 struct serport {
0014 struct tty_port *port;
0015 struct tty_struct *tty;
0016 struct tty_driver *tty_drv;
0017 int tty_idx;
0018 unsigned long flags;
0019 };
0020
0021
0022
0023
0024
0025 static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
0026 const unsigned char *fp, size_t count)
0027 {
0028 struct serdev_controller *ctrl = port->client_data;
0029 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0030 int ret;
0031
0032 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
0033 return 0;
0034
0035 ret = serdev_controller_receive_buf(ctrl, cp, count);
0036
0037 dev_WARN_ONCE(&ctrl->dev, ret < 0 || ret > count,
0038 "receive_buf returns %d (count = %zu)\n",
0039 ret, count);
0040 if (ret < 0)
0041 return 0;
0042 else if (ret > count)
0043 return count;
0044
0045 return ret;
0046 }
0047
0048 static void ttyport_write_wakeup(struct tty_port *port)
0049 {
0050 struct serdev_controller *ctrl = port->client_data;
0051 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0052 struct tty_struct *tty;
0053
0054 tty = tty_port_tty_get(port);
0055 if (!tty)
0056 return;
0057
0058 if (test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
0059 test_bit(SERPORT_ACTIVE, &serport->flags))
0060 serdev_controller_write_wakeup(ctrl);
0061
0062
0063 wake_up_interruptible(&tty->write_wait);
0064
0065 tty_kref_put(tty);
0066 }
0067
0068 static const struct tty_port_client_operations client_ops = {
0069 .receive_buf = ttyport_receive_buf,
0070 .write_wakeup = ttyport_write_wakeup,
0071 };
0072
0073
0074
0075
0076
0077 static int ttyport_write_buf(struct serdev_controller *ctrl, const unsigned char *data, size_t len)
0078 {
0079 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0080 struct tty_struct *tty = serport->tty;
0081
0082 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
0083 return 0;
0084
0085 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0086 return tty->ops->write(serport->tty, data, len);
0087 }
0088
0089 static void ttyport_write_flush(struct serdev_controller *ctrl)
0090 {
0091 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0092 struct tty_struct *tty = serport->tty;
0093
0094 tty_driver_flush_buffer(tty);
0095 }
0096
0097 static int ttyport_write_room(struct serdev_controller *ctrl)
0098 {
0099 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0100 struct tty_struct *tty = serport->tty;
0101
0102 return tty_write_room(tty);
0103 }
0104
0105 static int ttyport_open(struct serdev_controller *ctrl)
0106 {
0107 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0108 struct tty_struct *tty;
0109 struct ktermios ktermios;
0110 int ret;
0111
0112 tty = tty_init_dev(serport->tty_drv, serport->tty_idx);
0113 if (IS_ERR(tty))
0114 return PTR_ERR(tty);
0115 serport->tty = tty;
0116
0117 if (!tty->ops->open || !tty->ops->close) {
0118 ret = -ENODEV;
0119 goto err_unlock;
0120 }
0121
0122 ret = tty->ops->open(serport->tty, NULL);
0123 if (ret)
0124 goto err_close;
0125
0126 tty_unlock(serport->tty);
0127
0128
0129 ktermios = tty->termios;
0130 ktermios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
0131 INLCR | IGNCR | ICRNL | IXON);
0132 ktermios.c_oflag &= ~OPOST;
0133 ktermios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
0134 ktermios.c_cflag &= ~(CSIZE | PARENB);
0135 ktermios.c_cflag |= CS8;
0136 ktermios.c_cflag |= CRTSCTS;
0137
0138 ktermios.c_cflag |= CLOCAL;
0139 tty_set_termios(tty, &ktermios);
0140
0141 set_bit(SERPORT_ACTIVE, &serport->flags);
0142
0143 return 0;
0144
0145 err_close:
0146 tty->ops->close(tty, NULL);
0147 err_unlock:
0148 tty_unlock(tty);
0149 tty_release_struct(tty, serport->tty_idx);
0150
0151 return ret;
0152 }
0153
0154 static void ttyport_close(struct serdev_controller *ctrl)
0155 {
0156 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0157 struct tty_struct *tty = serport->tty;
0158
0159 clear_bit(SERPORT_ACTIVE, &serport->flags);
0160
0161 tty_lock(tty);
0162 if (tty->ops->close)
0163 tty->ops->close(tty, NULL);
0164 tty_unlock(tty);
0165
0166 tty_release_struct(tty, serport->tty_idx);
0167 }
0168
0169 static unsigned int ttyport_set_baudrate(struct serdev_controller *ctrl, unsigned int speed)
0170 {
0171 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0172 struct tty_struct *tty = serport->tty;
0173 struct ktermios ktermios = tty->termios;
0174
0175 ktermios.c_cflag &= ~CBAUD;
0176 tty_termios_encode_baud_rate(&ktermios, speed, speed);
0177
0178
0179 tty_set_termios(tty, &ktermios);
0180 return ktermios.c_ospeed;
0181 }
0182
0183 static void ttyport_set_flow_control(struct serdev_controller *ctrl, bool enable)
0184 {
0185 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0186 struct tty_struct *tty = serport->tty;
0187 struct ktermios ktermios = tty->termios;
0188
0189 if (enable)
0190 ktermios.c_cflag |= CRTSCTS;
0191 else
0192 ktermios.c_cflag &= ~CRTSCTS;
0193
0194 tty_set_termios(tty, &ktermios);
0195 }
0196
0197 static int ttyport_set_parity(struct serdev_controller *ctrl,
0198 enum serdev_parity parity)
0199 {
0200 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0201 struct tty_struct *tty = serport->tty;
0202 struct ktermios ktermios = tty->termios;
0203
0204 ktermios.c_cflag &= ~(PARENB | PARODD | CMSPAR);
0205 if (parity != SERDEV_PARITY_NONE) {
0206 ktermios.c_cflag |= PARENB;
0207 if (parity == SERDEV_PARITY_ODD)
0208 ktermios.c_cflag |= PARODD;
0209 }
0210
0211 tty_set_termios(tty, &ktermios);
0212
0213 if ((tty->termios.c_cflag & (PARENB | PARODD | CMSPAR)) !=
0214 (ktermios.c_cflag & (PARENB | PARODD | CMSPAR)))
0215 return -EINVAL;
0216
0217 return 0;
0218 }
0219
0220 static void ttyport_wait_until_sent(struct serdev_controller *ctrl, long timeout)
0221 {
0222 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0223 struct tty_struct *tty = serport->tty;
0224
0225 tty_wait_until_sent(tty, timeout);
0226 }
0227
0228 static int ttyport_get_tiocm(struct serdev_controller *ctrl)
0229 {
0230 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0231 struct tty_struct *tty = serport->tty;
0232
0233 if (!tty->ops->tiocmget)
0234 return -ENOTSUPP;
0235
0236 return tty->ops->tiocmget(tty);
0237 }
0238
0239 static int ttyport_set_tiocm(struct serdev_controller *ctrl, unsigned int set, unsigned int clear)
0240 {
0241 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0242 struct tty_struct *tty = serport->tty;
0243
0244 if (!tty->ops->tiocmset)
0245 return -ENOTSUPP;
0246
0247 return tty->ops->tiocmset(tty, set, clear);
0248 }
0249
0250 static const struct serdev_controller_ops ctrl_ops = {
0251 .write_buf = ttyport_write_buf,
0252 .write_flush = ttyport_write_flush,
0253 .write_room = ttyport_write_room,
0254 .open = ttyport_open,
0255 .close = ttyport_close,
0256 .set_flow_control = ttyport_set_flow_control,
0257 .set_parity = ttyport_set_parity,
0258 .set_baudrate = ttyport_set_baudrate,
0259 .wait_until_sent = ttyport_wait_until_sent,
0260 .get_tiocm = ttyport_get_tiocm,
0261 .set_tiocm = ttyport_set_tiocm,
0262 };
0263
0264 struct device *serdev_tty_port_register(struct tty_port *port,
0265 struct device *parent,
0266 struct tty_driver *drv, int idx)
0267 {
0268 struct serdev_controller *ctrl;
0269 struct serport *serport;
0270 int ret;
0271
0272 if (!port || !drv || !parent)
0273 return ERR_PTR(-ENODEV);
0274
0275 ctrl = serdev_controller_alloc(parent, sizeof(struct serport));
0276 if (!ctrl)
0277 return ERR_PTR(-ENOMEM);
0278 serport = serdev_controller_get_drvdata(ctrl);
0279
0280 serport->port = port;
0281 serport->tty_idx = idx;
0282 serport->tty_drv = drv;
0283
0284 ctrl->ops = &ctrl_ops;
0285
0286 port->client_ops = &client_ops;
0287 port->client_data = ctrl;
0288
0289 ret = serdev_controller_add(ctrl);
0290 if (ret)
0291 goto err_reset_data;
0292
0293 dev_info(&ctrl->dev, "tty port %s%d registered\n", drv->name, idx);
0294 return &ctrl->dev;
0295
0296 err_reset_data:
0297 port->client_data = NULL;
0298 port->client_ops = &tty_port_default_client_ops;
0299 serdev_controller_put(ctrl);
0300
0301 return ERR_PTR(ret);
0302 }
0303
0304 int serdev_tty_port_unregister(struct tty_port *port)
0305 {
0306 struct serdev_controller *ctrl = port->client_data;
0307 struct serport *serport = serdev_controller_get_drvdata(ctrl);
0308
0309 if (!serport)
0310 return -ENODEV;
0311
0312 serdev_controller_remove(ctrl);
0313 port->client_data = NULL;
0314 port->client_ops = &tty_port_default_client_ops;
0315 serdev_controller_put(ctrl);
0316
0317 return 0;
0318 }