Newer
Older
static int hvsi_chars_in_buffer(struct tty_struct *tty)
{
return hp->n_outbuf;
}
static int hvsi_write(struct tty_struct *tty,
const unsigned char *buf, int count)
{
struct hvsi_struct *hp = tty->driver_data;
const char *source = buf;
unsigned long flags;
int total = 0;
int origcount = count;
spin_lock_irqsave(&hp->lock, flags);
pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
if (!is_open(hp)) {
/* we're either closing or not yet open; don't accept data */
pr_debug("%s: not open\n", __func__);
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
goto out;
}
/*
* when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
* and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
* will see there is no room in outbuf and return.
*/
while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
int chunksize = min(count, hvsi_write_room(hp->tty));
BUG_ON(hp->n_outbuf < 0);
memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
hp->n_outbuf += chunksize;
total += chunksize;
source += chunksize;
count -= chunksize;
hvsi_push(hp);
}
if (hp->n_outbuf > 0) {
/*
* we weren't able to write it all to the hypervisor.
* schedule another push attempt.
*/
schedule_delayed_work(&hp->writer, 10);
}
out:
spin_unlock_irqrestore(&hp->lock, flags);
if (total != origcount)
pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
total);
return total;
}
/*
* I have never seen throttle or unthrottle called, so this little throttle
* buffering scheme may or may not work.
*/
static void hvsi_throttle(struct tty_struct *tty)
{
pr_debug("%s\n", __func__);
h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
}
static void hvsi_unthrottle(struct tty_struct *tty)
{
pr_debug("%s\n", __func__);
spin_lock_irqsave(&hp->lock, flags);
if (hp->n_throttle) {
hvsi_send_overflow(hp);
shouldflip = 1;
}
spin_unlock_irqrestore(&hp->lock, flags);
if (shouldflip)
tty_flip_buffer_push(hp->tty);
h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
}
static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
{
hvsi_get_mctrl(hp);
return hp->mctrl;
}
static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
unsigned int set, unsigned int clear)
{
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
unsigned long flags;
uint16_t new_mctrl;
/* we can only alter DTR */
clear &= TIOCM_DTR;
set &= TIOCM_DTR;
spin_lock_irqsave(&hp->lock, flags);
new_mctrl = (hp->mctrl & ~clear) | set;
if (hp->mctrl != new_mctrl) {
hvsi_set_mctrl(hp, new_mctrl);
hp->mctrl = new_mctrl;
}
spin_unlock_irqrestore(&hp->lock, flags);
return 0;
}
static const struct tty_operations hvsi_ops = {
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
.open = hvsi_open,
.close = hvsi_close,
.write = hvsi_write,
.hangup = hvsi_hangup,
.write_room = hvsi_write_room,
.chars_in_buffer = hvsi_chars_in_buffer,
.throttle = hvsi_throttle,
.unthrottle = hvsi_unthrottle,
.tiocmget = hvsi_tiocmget,
.tiocmset = hvsi_tiocmset,
};
static int __init hvsi_init(void)
{
int i;
hvsi_driver = alloc_tty_driver(hvsi_count);
if (!hvsi_driver)
return -ENOMEM;
hvsi_driver->owner = THIS_MODULE;
hvsi_driver->driver_name = "hvsi";
hvsi_driver->name = "hvsi";
hvsi_driver->major = HVSI_MAJOR;
hvsi_driver->minor_start = HVSI_MINOR;
hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
hvsi_driver->init_termios = tty_std_termios;
hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
hvsi_driver->init_termios.c_ispeed = 9600;
hvsi_driver->init_termios.c_ospeed = 9600;
hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
tty_set_operations(hvsi_driver, &hvsi_ops);
for (i=0; i < hvsi_count; i++) {
struct hvsi_struct *hp = &hvsi_ports[i];
int ret = 1;
ret = request_irq(hp->virq, hvsi_interrupt, IRQF_DISABLED, "hvsi", hp);
if (ret)
printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
hp->virq, ret);
}
hvsi_wait = wait_for_state; /* irqs active now */
if (tty_register_driver(hvsi_driver))
panic("Couldn't register hvsi console driver\n");
printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
return 0;
}
device_initcall(hvsi_init);
/***** console (not tty) code: *****/
static void hvsi_console_print(struct console *console, const char *buf,
unsigned int count)
{
struct hvsi_struct *hp = &hvsi_ports[console->index];
char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
unsigned int i = 0, n = 0;
int ret, donecr = 0;
mb();
if (!is_open(hp))
return;
/*
* ugh, we have to translate LF -> CRLF ourselves, in place.
* copied from hvc_console.c:
*/
while (count > 0 || i > 0) {
if (count > 0 && i < sizeof(c)) {
if (buf[n] == '\n' && !donecr) {
c[i++] = '\r';
donecr = 1;
} else {
c[i++] = buf[n++];
donecr = 0;
--count;
}
} else {
ret = hvsi_put_chars(hp, c, i);
if (ret < 0)
i = 0;
i -= ret;
}
}
}
static struct tty_driver *hvsi_console_device(struct console *console,
int *index)
{
*index = console->index;
return hvsi_driver;
}
static int __init hvsi_console_setup(struct console *console, char *options)
{
struct hvsi_struct *hp;
int ret;
if (console->index < 0 || console->index >= hvsi_count)
return -1;
hp = &hvsi_ports[console->index];
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
/* give the FSP a chance to change the baud rate when we re-open */
hvsi_close_protocol(hp);
ret = hvsi_handshake(hp);
if (ret < 0)
return ret;
ret = hvsi_get_mctrl(hp);
if (ret < 0)
return ret;
ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
if (ret < 0)
return ret;
hp->flags |= HVSI_CONSOLE;
return 0;
}
static struct console hvsi_console = {
.name = "hvsi",
.write = hvsi_console_print,
.device = hvsi_console_device,
.setup = hvsi_console_setup,
.flags = CON_PRINTBUFFER,
.index = -1,
};
static int __init hvsi_console_init(void)
{
struct device_node *vty;
hvsi_wait = poll_for_state; /* no irqs yet; must poll */
/* search device tree for vty nodes */
for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
vty != NULL;
vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
struct hvsi_struct *hp;
const uint32_t *vtermno, *irq;
vtermno = of_get_property(vty, "reg", NULL);
irq = of_get_property(vty, "interrupts", NULL);
if (!vtermno || !irq)
continue;
if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
of_node_put(vty);
break;
}
hp = &hvsi_ports[hvsi_count];
INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
INIT_WORK(&hp->handshaker, hvsi_handshaker);
init_waitqueue_head(&hp->emptyq);
init_waitqueue_head(&hp->stateq);
spin_lock_init(&hp->lock);
hp->index = hvsi_count;
hp->inbuf_end = hp->inbuf;
hp->state = HVSI_CLOSED;
hp->vtermno = *vtermno;
hp->virq = irq_create_mapping(NULL, irq[0]);
if (hp->virq == NO_IRQ) {
printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
__func__, irq[0]);
Benjamin Herrenschmidt
committed
}
register_console(&hvsi_console);
return 0;
}
console_initcall(hvsi_console_init);