forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvirtio-serial.c
600 lines (490 loc) · 18.2 KB
/
virtio-serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/****************************************************************************
* drivers/virtio/virtio-serial.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <errno.h>
#include <stdio.h>
#include <nuttx/serial/serial.h>
#include <nuttx/virtio/virtio.h>
#include "virtio-serial.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define VIRTIO_SERIAL_RX 0
#define VIRTIO_SERIAL_TX 1
#define VIRTIO_SERIAL_NUM 2
/****************************************************************************
* Private Types
****************************************************************************/
struct virtio_serial_priv_s
{
/* Virtio device informations */
FAR struct virtio_device *vdev;
/* Nuttx uart device informations */
FAR struct uart_dev_s udev;
char name[NAME_MAX];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Uart operation functions */
static int virtio_serial_setup(FAR struct uart_dev_s *dev);
static void virtio_serial_shutdown(FAR struct uart_dev_s *dev);
static int virtio_serial_attach(FAR struct uart_dev_s *dev);
static void virtio_serial_detach(FAR struct uart_dev_s *dev);
static int virtio_serial_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
static void virtio_serial_rxint(FAR struct uart_dev_s *dev, bool enable);
static void virtio_serial_send(FAR struct uart_dev_s *dev, int ch);
static void virtio_serial_txint(FAR struct uart_dev_s *dev, bool enable);
static bool virtio_serial_txready(FAR struct uart_dev_s *dev);
static bool virtio_serial_txempty(FAR struct uart_dev_s *dev);
static void virtio_serial_dmasend(FAR struct uart_dev_s *dev);
static void virtio_serial_dmatxavail(FAR struct uart_dev_s *dev);
static void virtio_serial_dmareceive(FAR struct uart_dev_s *dev);
static void virtio_serial_dmarxfree(FAR struct uart_dev_s *dev);
/* Other functions */
static void virtio_serial_rxready(FAR struct virtqueue *vq);
static void virtio_serial_txdone(FAR struct virtqueue *vq);
static int virtio_serial_probe(FAR struct virtio_device *vdev);
static void virtio_serial_remove(FAR struct virtio_device *vdev);
/****************************************************************************
* Private Data
****************************************************************************/
static struct virtio_driver g_virtio_serial_driver =
{
LIST_INITIAL_VALUE(g_virtio_serial_driver.node), /* node */
VIRTIO_ID_CONSOLE, /* device id */
virtio_serial_probe, /* probe */
virtio_serial_remove, /* remove */
};
static struct virtio_driver g_virtio_rprocserial_driver =
{
LIST_INITIAL_VALUE(g_virtio_rprocserial_driver.node), /* node */
VIRTIO_ID_RPROC_SERIAL, /* device id */
virtio_serial_probe, /* probe */
virtio_serial_remove, /* remove */
};
static const struct uart_ops_s g_virtio_serial_ops =
{
virtio_serial_setup, /* setup */
virtio_serial_shutdown, /* shutdown */
virtio_serial_attach, /* attach */
virtio_serial_detach, /* detach */
virtio_serial_ioctl, /* ioctl */
NULL, /* receive */
virtio_serial_rxint, /* rxint */
NULL, /* rxavailable */
#ifdef CONFIG_SERIAL_IFLOWCONTROL
NULL, /* rxflowcontrol */
#endif
virtio_serial_dmasend, /* dmasend */
virtio_serial_dmareceive, /* dmareceive */
virtio_serial_dmarxfree, /* dmarxfree */
virtio_serial_dmatxavail, /* dmatxavail */
virtio_serial_send, /* send */
virtio_serial_txint, /* txint */
virtio_serial_txready, /* txready */
virtio_serial_txempty, /* txempty */
};
static int g_virtio_serial_idx = 0;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: virtio_serial_setup
*
* Description:
* Configure the UART baud, bits, parity, fifos, etc. This
* method is called the first time that the serial port is
* opened.
*
****************************************************************************/
static int virtio_serial_setup(FAR struct uart_dev_s *dev)
{
return OK;
}
/****************************************************************************
* Name: virtio_serial_shutdown
*
* Description:
* Disable the UART. This method is called when the serial
* port is closed
*
****************************************************************************/
static void virtio_serial_shutdown(FAR struct uart_dev_s *dev)
{
/* Nothing */
}
/****************************************************************************
* Name: virtio_serial_attach
*
* Description:
* Configure the UART to operation in interrupt driven mode. This method
* is called when the serial port is opened. Normally, this is just after
* the setup() method is called, however, the serial console may operate in
* a non-interrupt driven mode during the boot phase.
*
* RX and TX interrupts are not enabled when by the attach method (unless
* the hardware supports multiple levels of interrupt enabling). The RX
* and TX interrupts are not enabled until the txint() and rxint() methods
* are called.
*
****************************************************************************/
static int virtio_serial_attach(FAR struct uart_dev_s *dev)
{
FAR struct virtio_serial_priv_s *priv = dev->priv;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_SERIAL_RX].vq;
virtqueue_enable_cb(vq);
return 0;
}
/****************************************************************************
* Name: virtio_serial_detach
*
* Description:
* Detach UART interrupts. This method is called when the serial port is
* closed normally just before the shutdown method is called. The
* exception is the serial console which is never shutdown.
*
****************************************************************************/
static void virtio_serial_detach(FAR struct uart_dev_s *dev)
{
FAR struct virtio_serial_priv_s *priv = dev->priv;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_SERIAL_RX].vq;
virtqueue_disable_cb(vq);
}
/****************************************************************************
* Name: virtio_serial_ioctl
*
* Description:
* All ioctl calls will be routed through this method
*
****************************************************************************/
static int virtio_serial_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
{
return -ENOTTY;
}
/****************************************************************************
* Name: virtio_serial_rxint
*
* Description:
* Call to enable or disable RX interrupts
*
****************************************************************************/
static void virtio_serial_rxint(FAR struct uart_dev_s *dev, bool enable)
{
}
/****************************************************************************
* Name: virtio_serial_send
*
* Description:
* This method will send one byte on the UART
*
****************************************************************************/
static void virtio_serial_send(FAR struct uart_dev_s *dev, int ch)
{
int nexthead;
nexthead = dev->xmit.head + 1;
if (nexthead >= dev->xmit.size)
{
nexthead = 0;
}
if (nexthead != dev->xmit.tail)
{
/* No.. not full. Add the character to the TX buffer and return. */
dev->xmit.buffer[dev->xmit.head] = ch;
dev->xmit.head = nexthead;
}
uart_dmatxavail(dev);
}
/****************************************************************************
* Name: virtio_serial_txint
*
* Description:
* Call to enable or disable TX interrupts
*
****************************************************************************/
static void virtio_serial_txint(FAR struct uart_dev_s *dev, bool enable)
{
}
/****************************************************************************
* Name: uart_txready
*
* Description:
* Return true if the tranmsit fifo is not full
*
****************************************************************************/
static bool virtio_serial_txready(FAR struct uart_dev_s *dev)
{
int nexthead = dev->xmit.head + 1;
if (nexthead >= dev->xmit.size)
{
nexthead = 0;
}
return nexthead != dev->xmit.tail;
}
/****************************************************************************
* Name: virtio_serial_txempty
*
* Description:
* Return true if the transmit fifo is empty
*
****************************************************************************/
static bool virtio_serial_txempty(FAR struct uart_dev_s *dev)
{
return true;
}
/****************************************************************************
* Name: virtio_serial_dmasend
****************************************************************************/
static void virtio_serial_dmasend(FAR struct uart_dev_s *dev)
{
FAR struct virtio_serial_priv_s *priv = dev->priv;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_SERIAL_TX].vq;
FAR struct uart_dmaxfer_s *xfer = &dev->dmatx;
struct virtqueue_buf vb[2];
uintptr_t len;
int num = 1;
/* Get the total send length */
len = xfer->length + xfer->nlength;
/* Set the virtqueue buffer */
vb[0].buf = xfer->buffer;
vb[0].len = xfer->length;
if (xfer->nlength != 0)
{
vb[1].buf = xfer->nbuffer;
vb[1].len = xfer->nlength;
num = 2;
}
/* Add buffer to TX virtiqueue and notify the other size */
virtqueue_add_buffer(vq, vb, num, 0, (FAR void *)len);
virtqueue_kick(vq);
}
/****************************************************************************
* Name: virtio_serial_dmatxavail
****************************************************************************/
static void virtio_serial_dmatxavail(FAR struct uart_dev_s *dev)
{
if (dev->dmatx.length == 0)
{
uart_xmitchars_dma(dev);
}
}
/****************************************************************************
* Name: virtio_serial_dmareceive
****************************************************************************/
static void virtio_serial_dmareceive(FAR struct uart_dev_s *dev)
{
FAR struct virtio_serial_priv_s *priv = dev->priv;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_SERIAL_RX].vq;
FAR struct uart_dmaxfer_s *xfer = &dev->dmarx;
struct virtqueue_buf vb[2];
int num = 1;
vb[0].buf = xfer->buffer;
vb[0].len = xfer->length;
if (xfer->nlength != 0)
{
vb[num].buf = xfer->nbuffer;
vb[num].len = xfer->nlength;
num = 2;
}
/* Add buffer to the RX virtqueue and notify the device side */
virtqueue_add_buffer(vq, vb, 0, num, xfer);
virtqueue_kick(vq);
}
/****************************************************************************
* Name: virtio_serial_dmarxfree
****************************************************************************/
static void virtio_serial_dmarxfree(FAR struct uart_dev_s *dev)
{
if (dev->dmarx.length == 0)
{
uart_recvchars_dma(dev);
}
}
/****************************************************************************
* Name: virtio_serial_rxready
*
* Description:
* The virt serial receive virtqueue callback funtion
*
****************************************************************************/
static void virtio_serial_rxready(FAR struct virtqueue *vq)
{
FAR struct virtio_serial_priv_s *priv = vq->vq_dev->priv;
FAR struct uart_dmaxfer_s *xfer;
uint32_t len;
/* Received some data, call uart_recvchars_done() */
xfer = virtqueue_get_buffer(vq, &len, NULL);
if (xfer == NULL)
{
return;
}
xfer->nbytes = len;
uart_recvchars_done(&priv->udev);
uart_dmarxfree(&priv->udev);
}
/****************************************************************************
* Name: virtio_serial_txdone
*
* Description:
* The virt serial transimit virtqueue callback funtion
*
****************************************************************************/
static void virtio_serial_txdone(FAR struct virtqueue *vq)
{
FAR struct virtio_serial_priv_s *priv = vq->vq_dev->priv;
uintptr_t len;
/* Call uart_xmitchars_done to notify the upperhalf */
len = (uintptr_t)virtqueue_get_buffer(vq, NULL, NULL);
priv->udev.dmatx.nbytes = len;
uart_xmitchars_done(&priv->udev);
uart_dmatxavail(&priv->udev);
}
/****************************************************************************
* Name: virtio_serial_init
****************************************************************************/
static int virtio_serial_init(FAR struct virtio_serial_priv_s *priv,
FAR struct virtio_device *vdev)
{
FAR const char *vqnames[VIRTIO_SERIAL_NUM];
vq_callback callbacks[VIRTIO_SERIAL_NUM];
FAR struct uart_dev_s *udev;
int ret;
priv->vdev = vdev;
vdev->priv = priv;
/* Uart device buffer and ops init */
udev = &priv->udev;
udev->priv = priv;
udev->ops = &g_virtio_serial_ops;
udev->recv.size = CONFIG_DRIVERS_VIRTIO_SERIAL_BUFSIZE;
udev->recv.buffer = virtio_zalloc_buf(vdev, udev->recv.size, 16);
if (udev->recv.buffer == NULL)
{
vrterr("No enough memory\n");
return -ENOMEM;
}
udev->xmit.size = CONFIG_DRIVERS_VIRTIO_SERIAL_BUFSIZE;
udev->xmit.buffer = virtio_zalloc_buf(vdev, udev->xmit.size, 16);
if (udev->xmit.buffer == NULL)
{
vrterr("No enough memory\n");
ret = -ENOMEM;
goto err_with_recv;
}
//// TinyEMU needs NuttX to echo the keypress and change CR to NL
udev->isconsole = true; ////
/* Initialize the virtio device */
virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER);
virtio_set_features(vdev, 0);
virtio_set_status(vdev, VIRTIO_CONFIG_FEATURES_OK);
vqnames[VIRTIO_SERIAL_RX] = "virtio_serial_rx";
vqnames[VIRTIO_SERIAL_TX] = "virtio_serial_tx";
callbacks[VIRTIO_SERIAL_RX] = virtio_serial_rxready;
callbacks[VIRTIO_SERIAL_TX] = virtio_serial_txdone;
ret = virtio_create_virtqueues(vdev, 0, VIRTIO_SERIAL_NUM, vqnames,
callbacks);
if (ret < 0)
{
vrterr("virtio_device_create_virtqueue failed, ret=%d\n", ret);
goto err_with_xmit;
}
virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
return OK;
err_with_xmit:
virtio_free_buf(vdev, udev->xmit.buffer);
err_with_recv:
virtio_free_buf(vdev, udev->recv.buffer);
virtio_reset_device(vdev);
return ret;
}
/****************************************************************************
* Name: virtio_serial_uninit
****************************************************************************/
static void virtio_serial_uninit(FAR struct virtio_serial_priv_s *priv)
{
FAR struct virtio_device *vdev = priv->vdev;
virtio_reset_device(vdev);
virtio_delete_virtqueues(vdev);
virtio_free_buf(vdev, priv->udev.xmit.buffer);
virtio_free_buf(vdev, priv->udev.recv.buffer);
}
/****************************************************************************
* Name: virtio_serial_probe
****************************************************************************/
static int virtio_serial_probe(FAR struct virtio_device *vdev)
{
FAR struct virtio_serial_priv_s *priv;
int ret;
/* Alloc the virtio serial driver and uart buffer */
priv = kmm_zalloc(sizeof(*priv));
if (priv == NULL)
{
vrterr("No enough memory\n");
return -ENOMEM;
}
ret = virtio_serial_init(priv, vdev);
if (ret < 0)
{
vrterr("virtio_serial_init failed, ret=%d\n", ret);
goto err_with_priv;
}
/* Uart driver register */
snprintf(priv->name, NAME_MAX, "/dev/console"); //// Previously: /dev/ttyV0
ret = uart_register(priv->name, &priv->udev);
if (ret < 0)
{
vrterr("uart_register failed, ret=%d\n", ret);
goto err_with_init;
}
g_virtio_serial_idx++;
return ret;
err_with_init:
virtio_serial_uninit(priv);
err_with_priv:
kmm_free(priv);
return ret;
}
/****************************************************************************
* Name: virtio_serial_remove
****************************************************************************/
static void virtio_serial_remove(FAR struct virtio_device *vdev)
{
FAR struct virtio_serial_priv_s *priv = vdev->priv;
unregister_driver(priv->name);
virtio_serial_uninit(priv);
kmm_free(priv);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: virtio_register_serial_driver
****************************************************************************/
int virtio_register_serial_driver(void)
{
int ret1 = virtio_register_driver(&g_virtio_serial_driver);
int ret2 = virtio_register_driver(&g_virtio_rprocserial_driver);
return ret1 < 0 ? ret1 : ret2;
}