-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathascan.c
479 lines (437 loc) · 12.2 KB
/
ascan.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
/**
* AS - the open source Automotive Software on https://github.com/parai
*
* Copyright (C) 2016 AS <[email protected]>
*
* This source code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/* ============================ [ INCLUDES ] ====================================================== */
#include "qemu/osdep.h"
#include "hw/hw.h"
#include "hw/pci/pci.h"
#include "qemu/event_notifier.h"
#include <time.h>
#include <sys/queue.h>
#include <pthread.h>
/* ============================ [ MACROS ] ====================================================== */
#define TYPE_PCI_ASCAN_DEV "pci-ascan"
#define PCI_ASCAN_DEV(obj) OBJECT_CHECK(PCIASCANDevState, (obj), TYPE_PCI_ASCAN_DEV)
/* sizes must be power of 2 in PCI */
#define ASCAN_IO_SIZE 1<<4
#define ASCAN_MMIO_SIZE 1<<6
enum {
FLG_RX = 0x01,
FLG_TX = 0x02,
};
enum{
REG_BUS_NAME = 0x00,
REG_BUSID = 0x04,
REG_PORT = 0x08,
REG_CANID = 0x0C,
REG_CANDLC = 0x10,
REG_CANDATA = 0x14,
REG_CANSTATUS = 0x18,
REG_CMD = 0x1C,
};
/* ============================ [ TYPES ] ====================================================== */
struct Can_Bus_s {
uint32_t busid;
uint32_t canid;
uint32_t candlc;
uint32_t rwpos;
uint8_t data[64];
STAILQ_ENTRY(Can_Bus_s) entry; /* entry for Can_PduQueue_s, sort by CANID queue*/
};
typedef struct PCIASCANDevState {
PCIDevice parent_obj;
/* for PIO */
MemoryRegion io;
/* for MMIO */
MemoryRegion mmio;
/* irq used */
qemu_irq irq;
/* dma buf size */
unsigned int dma_size;
/* buffer copied with the dma operation on RAM */
char *dma_buf;
/* did we throw an interrupt ? */
int threw_irq;
/* id of the device, writable */
int id;
STAILQ_HEAD(,Can_Bus_s) head;
struct Can_Bus_s* curbus; /* current bus for reading data */
char bus_name[64];
uint32_t bn_pos;
uint32_t flag;
uint32_t busid;
uint32_t port;
} PCIASCANDevState;
static Property ascan_properties[] = {
DEFINE_PROP_END_OF_LIST(), };
/* ============================ [ DECLARES ] ====================================================== */
extern void luai_canlib_open(void);
extern void luai_canlib_close(void);
extern int can_open(unsigned long busid,const char* device_name,unsigned long port, unsigned long baudrate);
extern int can_write(unsigned long busid,unsigned long canid,unsigned long dlc,unsigned char* data);
extern int can_read(unsigned long busid,unsigned long canid,unsigned long* p_canid,unsigned long *dlc,unsigned char* data);
static uint64_t ascan_mmioread(void *opaque, hwaddr addr, unsigned size);
static void ascan_mmiowrite(void *opaque, hwaddr addr, uint64_t value,
unsigned size);
static uint64_t ascan_ioread(void *opaque, hwaddr addr, unsigned size);
static void ascan_iowrite(void *opaque, hwaddr addr, uint64_t value,
unsigned size);
static void pci_ascandev_class_init(ObjectClass *klass, void *data);
/* ============================ [ DATAS ] ====================================================== */
/*
* Callbacks called when the Memory Region
* representing the MMIO space is
* accessed.
*/
static const MemoryRegionOps ascan_mmio_ops = {
.read = ascan_mmioread,
.write = ascan_mmiowrite,
.endianness = DEVICE_NATIVE_ENDIAN,
.valid = {
.min_access_size = 4,
.max_access_size = 4,
},
};
/*
* Callbacks called when the Memory Region
* representing the PIO space is
* accessed.
*/
static const MemoryRegionOps ascan_io_ops = {
.read = ascan_ioread,
.write = ascan_iowrite,
.endianness = DEVICE_NATIVE_ENDIAN,
.valid = {
.min_access_size = 4,
.max_access_size = 4,
},
};
/* Contains all the informations of the device
* we are creating.
* class_init will be called when we are defining
* our device.
*/
static const TypeInfo pci_ascan_info = {
.name = TYPE_PCI_ASCAN_DEV,
.parent = TYPE_PCI_DEVICE,
.instance_size = sizeof(PCIASCANDevState),
.class_init = pci_ascandev_class_init,
#ifdef INTERFACE_CONVENTIONAL_PCI_DEVICE
.interfaces = (InterfaceInfo[]) {
{ INTERFACE_CONVENTIONAL_PCI_DEVICE },
{},
},
#endif
};
/* ============================ [ LOCALS ] ====================================================== */
static struct Can_Bus_s* getBus(PCIASCANDevState *d, uint32_t busid)
{
struct Can_Bus_s* b;
STAILQ_FOREACH(b, &d->head, entry)
{
if(b->busid == busid)
{
break;
}
}
return b;
}
static void checkBus(PCIASCANDevState *d)
{
struct Can_Bus_s* b;
STAILQ_FOREACH(b, &d->head, entry)
{
if(d->flag&(FLG_RX<<(4*b->busid)))
{
/* already has message ready for read */
}
else
{
int rv;
rv = can_read(b->busid, -1, (unsigned long*)&b->canid, (unsigned long*)&b->candlc, b->data);
if(TRUE == rv)
{
d->flag |= FLG_RX<<(4*b->busid);
}
}
}
}
static void ascan_iowrite(void *opaque, hwaddr addr, uint64_t value,
unsigned size) {
int i;
PCIASCANDevState *d = (PCIASCANDevState *) opaque;
PCIDevice *pci_dev = (PCIDevice *) opaque;
switch (addr) {
case 0:
if (value) {
/* throw an interrupt */
d->threw_irq = 1;
pci_irq_assert(pci_dev);
} else {
/* ack interrupt */
pci_irq_deassert(pci_dev);
d->threw_irq = 0;
}
break;
case 4:
/* throw a random DMA */
for (i = 0; i < d->dma_size; ++i)
d->dma_buf[i] = rand();
cpu_physical_memory_write(value, (void *) d->dma_buf, d->dma_size);
break;
default:
printf ("%s: Bad register offset 0x%x\n", __func__, (int)addr);
}
}
static uint64_t ascan_ioread(void *opaque, hwaddr addr, unsigned size) {
PCIASCANDevState *d = (PCIASCANDevState *) opaque;
switch (addr) {
case 0:
/* irq status */
return d->threw_irq;
break;
default:
printf ("%s: Bad register offset 0x%x\n", __func__, (int)addr);
return 0x0;
}
}
static uint64_t ascan_mmioread(void *opaque, hwaddr addr, unsigned size) {
PCIASCANDevState *d = (PCIASCANDevState *) opaque;
PCIDevice *pci_dev = (PCIDevice *) opaque;
uint64_t rv;
switch (addr) {
case REG_CANSTATUS:
pci_irq_deassert(pci_dev);
checkBus(d);
rv = d->flag;
d->flag = 0;
return rv;
break;
case REG_CANID:
if(d->curbus) {
return d->curbus->canid;
} else {
printf("%s@%d: invalid CAN bus\n", __func__, __LINE__);
return -1;
}
break;
case REG_CANDLC:
if(d->curbus) {
/* read DLC and then read data */
d->curbus->rwpos = 0;
return d->curbus->candlc;
} else {
printf("%s@%d: invalid CAN bus\n", __func__, __LINE__);
return -1;
}
break;
case REG_CANDATA:
if(NULL == d->curbus) {
printf("%s@%d: invalid CAN bus", __func__, __LINE__);
return -1;
}
if(!(d->curbus->rwpos<sizeof(d->curbus->data))) {
printf("%s@%d: CAN data read overflow\n", __func__, __LINE__);
return -2;
}
return d->curbus->data[d->curbus->rwpos++];
break;
default:
printf ("%s: Bad register offset 0x%x\n", __func__, (int)addr);
return 0x0;
}
}
static void ascan_mmiowrite(void *opaque, hwaddr addr, uint64_t value,
unsigned size) {
PCIASCANDevState *d = (PCIASCANDevState *) opaque;
PCIDevice *pci_dev = (PCIDevice *) opaque;
switch (addr) {
case REG_BUS_NAME:
/* change the id */
if(d->bn_pos < (sizeof(d->bus_name)-1)) {
d->bus_name[d->bn_pos] = value;
d->bn_pos++;
} else {
printf("%s@%d: bus name overflow\n", __func__, __LINE__);
}
break;
case REG_BUSID:
if(value < 8) {
d->busid = value;
d->curbus = getBus(d, d->busid);
} else {
printf("%s@%d: invalid CAN busid\n", __func__, __LINE__);
}
break;
case REG_PORT:
d->port = value;
break;
case REG_CANID:
if(d->curbus) {
d->curbus->canid = value;
} else {
printf("%s@%d: invalid CAN bus\n", __func__, __LINE__);
}
break;
case REG_CANDLC:
if(d->curbus) {
d->curbus->candlc = value;
d->curbus->rwpos = 0;
} else {
printf("%s@%d: invalid CAN bus\n", __func__, __LINE__);
}
break;
case REG_CANDATA:
if(NULL == d->curbus) {
printf("%s@%d: invalid CAN bus\n", __func__, __LINE__);
}
if(!(d->curbus->rwpos < sizeof(d->curbus->data))) {
printf("%s@%d: CAN data write overflow\n", __func__, __LINE__);
}
d->curbus->data[d->curbus->rwpos++] = value;
break;
case REG_CMD:
switch(value)
{
case 0:
d->bn_pos = 0; /* do this command first before config bus name */
memset(d->bus_name, 0, sizeof(d->bus_name));
break;
case 1:
{
int rv = can_open(d->busid, d->bus_name, d->port, 1000000);
if(TRUE == rv)
{
struct Can_Bus_s *p = malloc(sizeof(struct Can_Bus_s));
assert(p);
p->busid = d->busid;
STAILQ_INSERT_TAIL(&d->head, p, entry);
printf("start %s busid(%d) port(%d) okay\n", d->bus_name, d->busid, d->port);
}
else
{
printf("please start up %s for simulation:\n"
"\tsudo modprobe vcan\n"
"\tsudo ip link add dev can%d type vcan\n"
"\tsudo ip link set up can%d\n"
"\tsudo ip link set can%d mtu 72\n",
(0==strcmp(d->bus_name,"socket"))?"vcan":d->bus_name,
d->port,d->port,d->port);
}
}
break;
case 2:
{
int rv;
assert(d->curbus);
rv = can_write(d->busid,d->curbus->canid,d->curbus->candlc,d->curbus->data);
if(TRUE == rv)
{
d->flag |= FLG_TX<<(4*d->busid);
pci_irq_assert(pci_dev);
}
}
break;
default:
printf ("%s: Bad command 0x%x\n", __func__, (int)value);
break;
}
break;
default:
printf ("%s: Bad register offset 0x%x\n", __func__, (int)addr);
}
}
/* Callbacks for MMIO and PIO regions are registered here */
static void ascan_io_setup(PCIASCANDevState *d) {
memory_region_init_io(&d->mmio, OBJECT(d), &ascan_mmio_ops, d, "ascan_mmio",
ASCAN_MMIO_SIZE);
memory_region_init_io(&d->io, OBJECT(d), &ascan_io_ops, d, "ascan_io",
ASCAN_IO_SIZE);
}
/* When device is loaded */
static int pci_ascandev_init(PCIDevice *pci_dev) {
/* init the internal state of the device */
PCIASCANDevState *d = PCI_ASCAN_DEV(pci_dev);
d->dma_size = 0x1ffff * sizeof(char);
d->dma_buf = malloc(d->dma_size);
d->id = 0xcaac;
d->threw_irq = 0;
d->flag = 0;
uint8_t *pci_conf;
STAILQ_INIT(&d->head);
/* create the memory region representing the MMIO and PIO
* of the device
*/
ascan_io_setup(d);
/*
* See linux device driver (Edition 3) for the definition of a bar
* in the PCI bus.
*/
pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
pci_conf = pci_dev->config;
/* also in ldd, a pci device has 4 pin for interrupt
* here we use pin B.
*/
pci_conf[PCI_INTERRUPT_PIN] = 0x02;
return 0;
}
/* When device is unloaded
* Can be useful for hot(un)plugging
*/
static void pci_ascandev_uninit(PCIDevice *dev) {
PCIASCANDevState *d = (PCIASCANDevState *) dev;
free(d->dma_buf);
}
static void qdev_pci_ascandev_reset(DeviceState *dev) {
PCIASCANDevState *d = (PCIASCANDevState *) dev;
while(FALSE == STAILQ_EMPTY(&d->head))
{
struct Can_Bus_s *p = STAILQ_FIRST(&d->head);
STAILQ_REMOVE_HEAD(&d->head,entry);
free(p);
}
luai_canlib_close();
luai_canlib_open();
}
/* Called when the device is defined
* PCI configuration is defined here
* We inherit from PCIDeviceClass
* Also see ldd for the meaning of the different args
*/
static void pci_ascandev_class_init(ObjectClass *klass, void *data) {
DeviceClass *dc = DEVICE_CLASS(klass);
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
k->init = pci_ascandev_init;
k->exit = pci_ascandev_uninit;
/* this identify our device */
k->vendor_id = 0xcaac;
k->device_id = 0x0001;
k->class_id = PCI_CLASS_OTHERS;
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
k->revision = 0x00;
dc->desc = "PCI ASCAN";
/* qemu user things */
dc->props = ascan_properties;
dc->reset = qdev_pci_ascandev_reset;
}
/* function called before the qemu main
* it will define our device
*/
static void pci_ascan_register_types(void) {
type_register_static(&pci_ascan_info);
}
/* ============================ [ FUNCTIONS ] ====================================================== */
/* macro actually defining our device and registering it in qemu*/
type_init(pci_ascan_register_types);