aboutsummaryrefslogtreecommitdiff
path: root/hw/usb/dev-mbim.c
blob: 901e551aa95729070a8362148049952b7a0f7f1d (plain)
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
 * CDC MBIM Device emulation
 *
 * Copyright (c) Bjørn Mork <bjorn@mork.no>
 *
 * This code is licensed under the LGPL.
 */

#include "qemu-common.h"
#include "qemu-error.h"
#include "hw/usb.h"
#include "hw/usb/desc.h"

#define DEBUG_MBIM

#ifdef DEBUG_MBIM
#define DPRINTF(fmt, ...) \
	do { printf("usb-mbim: %s:" fmt , __FUNCTION__, ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) do {} while(0)
#endif

#define InterfaceOutClass \
    ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE)<<8)

#define InterfaceInClass  \
    ((USB_DIR_IN  | USB_TYPE_CLASS | USB_RECIP_INTERFACE)<<8)

#define USB_CDC_SEND_ENCAPSULATED_COMMAND	0x00
#define USB_CDC_GET_ENCAPSULATED_RESPONSE	0x01
#define USB_CDC_REQ_SET_LINE_CODING		0x20
#define USB_CDC_REQ_GET_LINE_CODING		0x21
#define USB_CDC_REQ_SET_CONTROL_LINE_STATE	0x22
#define USB_CDC_REQ_SEND_BREAK			0x23
#define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS	0x40
#define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER	0x41
#define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER	0x42
#define USB_CDC_SET_ETHERNET_PACKET_FILTER	0x43
#define USB_CDC_GET_ETHERNET_STATISTIC		0x44
#define USB_CDC_GET_NTB_PARAMETERS		0x80
#define USB_CDC_GET_NET_ADDRESS			0x81
#define USB_CDC_SET_NET_ADDRESS			0x82
#define USB_CDC_GET_NTB_FORMAT			0x83
#define USB_CDC_SET_NTB_FORMAT			0x84
#define USB_CDC_GET_NTB_INPUT_SIZE		0x85
#define USB_CDC_SET_NTB_INPUT_SIZE		0x86
#define USB_CDC_GET_MAX_DATAGRAM_SIZE		0x87
#define USB_CDC_SET_MAX_DATAGRAM_SIZE		0x88
#define USB_CDC_GET_CRC_MODE			0x89
#define USB_CDC_SET_CRC_MODE			0x8a

enum {
	STR_MANUFACTURER = 1,
	STR_PRODUCT_MBIM,
	STR_SERIALNUMBER,
	STR_MACADDRESS,
	STR_COMMANDSET,
};

static const USBDescStrings desc_strings = {
	[STR_MANUFACTURER] = "QEMU",
	[STR_PRODUCT_MBIM] = "QEMU USB CDC MBIM",
	[STR_SERIALNUMBER] = "1",
	[STR_MACADDRESS] = "5254DEADBEEF",
	[STR_COMMANDSET] = "S_NCM_CMD_FUNC",
};

/* CDC functional descriptors: */
static const uint8_t cdc_header[] = {
        0x05,		/* bLength; */
        0x24,		/* bDescriptorType; Functional */
	0x00,		/* bDescriptorSubType */
	0x10, 0x01,	/* bcdCDC 1.10 */
};
static const uint8_t cdc_mbim[] = {
        0x0c,		/* bLength; */
        0x24,		/* bDescriptorType; Functional */
	0x1b,		/* bDescriptorSubType */
	0x00, 0x01,	/* bcdMBIMVersion       1.00 */
	0x00, 0x06,	/* wMaxControlMessage   1536 */
        0x10,		/* bNumberFilters       16 */
        0x28,		/* bMaxFilterSize       40 */
        0x00, 0x10,	/* wMaxSegmentSize      4096 */
        0x20,		/* bmNetworkCapabilities 0x20 */
};
static const uint8_t cdc_union[] = {
        0x05,		/* bLength; */
        0x24,  		/* bDescriptorType; Functional */
	0x06,		/* bDescriptorSubType */
        0x00,		/* bMasterInterface        0 */
        0x01,		/* bSlaveInterface         1 */
};

static const uint8_t cdc_ethernet[] = {
        0x0d,		/* bLength; */
        0x24,  		/* bDescriptorType; Functional */
	0x0f,		/* bDescriptorSubType */
        STR_MACADDRESS, /* iMacAddress */
        0x00, 0x00, 0x00, 0x00,	/* bmEthernetStatistics    0x00000000 */
        0xea, 0x05,	/* wMaxSegmentSize               1514 */
        0x40, 0x80,	/* wNumberMCFilters            0x8040 */
        0x01,		/* bNumberPowerFilters              1 */
};
static const uint8_t cdc_ncm[] = {
        0x06,		/* bLength; */
        0x24,  		/* bDescriptorType; Functional */
	0x1a,		/* bDescriptorSubType */
	0x00, 0x01,	/* bcdNcmVersion       1.00 */
        0x1b,		/* bmNetworkCapabilities 0x1b */
};
static const uint8_t cdc_commandset[] = {
        0x16,		/* bLength; */
        0x24,  		/* bDescriptorType; Functional */
	0x16,		/* bDescriptorSubType */
	0x00, 0x01,	/* bcdVersion       1.00 */
        STR_COMMANDSET, /* iCommandSet            13 S_NCM_CMD_FUNC */
	/*       bGUID                {2e23bbae-c188-11df-bcdc-d9c6dfd72085} */
	0x2e, 0x23, 0xbb, 0xae, 0xc1, 0x88, 0x11, 0xdf, 0xbc, 0xdc, 0xd9, 0xc6, 0xdf, 0xd7, 0x20, 0x85,
};

/*
MBIM with NCM 1.0 compatibility setting
=======================================

   I:  If#= 0 Alt= 0 #EPs= 1 Cls=02(comm.) Sub=0d Prot=00 Driver=cdc_ncm
   E:  Ad=81(I) Atr=03(Int.) MxPS=  64 Ivl=2ms
   I:* If#= 0 Alt= 1 #EPs= 1 Cls=02(comm.) Sub=0e Prot=00 Driver=cdc_ncm
   E:  Ad=81(I) Atr=03(Int.) MxPS=  64 Ivl=2ms
   I:  If#= 1 Alt= 0 #EPs= 0 Cls=0a(data ) Sub=00 Prot=01 Driver=cdc_ncm
   I:  If#= 1 Alt= 1 #EPs= 2 Cls=0a(data ) Sub=00 Prot=01 Driver=cdc_ncm
   E:  Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
   E:  Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=4ms
   I:* If#= 1 Alt= 2 #EPs= 2 Cls=0a(data ) Sub=00 Prot=02 Driver=cdc_ncm
   E:  Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
   E:  Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=4ms

*/

static const USBDescIface desc_iface_mbim[] = {
{
	.bInterfaceNumber              = 0,
	.bAlternateSetting             = 0,
	.bNumEndpoints                 = 1,
	.bInterfaceClass               = 0x02,
	.bInterfaceSubClass            = 0x0d, /* NCM */
	.bInterfaceProtocol            = 0x00,
	.ndesc                         = 5,
	.descs = (USBDescOther[]) {
		{
			.data = cdc_header,
		},
		{
			.data = cdc_union,
		},
		{
			.data = cdc_ethernet,
		},
		{
			.data = cdc_ncm,
		},
		{
			.data = cdc_commandset,
		},
	},
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_INT,
			.wMaxPacketSize        = 64,
			.bInterval		= 7,
		},
	}
},{
	.bInterfaceNumber              = 0,
	.bAlternateSetting             = 1,
	.bNumEndpoints                 = 1,
	.bInterfaceClass               = 0x02,
	.bInterfaceSubClass            = 0x0e, /* MBIM */
	.bInterfaceProtocol            = 0x00,
	.ndesc                         = 3,
	.descs = (USBDescOther[]) {
		{
			.data = cdc_header,
		},
		{
			.data = cdc_mbim,
		},
		{
			.data = cdc_union,
		},
	},
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_INT,
			.wMaxPacketSize        = 64,
			.bInterval		= 7,
		},
	}
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 0,
	.bNumEndpoints                 = 0,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x01, /* NCM */
	.eps = (USBDescEndpoint[]) {
    }
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 1,
	.bNumEndpoints                 = 2,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x01, /* NCM */
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},{
			.bEndpointAddress      = USB_DIR_OUT | 0x02,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},
	}
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 2,
	.bNumEndpoints                 = 2,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x02, /* MBIM */
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},{
			.bEndpointAddress      = USB_DIR_OUT | 0x02,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},
	}
}
};

static const USBDescIface desc_iface_ncm[] = {
{
	.bInterfaceNumber              = 0,
	.bAlternateSetting             = 0,
	.bNumEndpoints                 = 1,
	.bInterfaceClass               = 0x02,
	.bInterfaceSubClass            = 0x0d, /* NCM */
	.bInterfaceProtocol            = 0x00,
	.ndesc                         = 5,
	.descs = (USBDescOther[]) {
		{
			.data = cdc_header,
		},
		{
			.data = cdc_union,
		},
		{
			.data = cdc_ethernet,
		},
		{
			.data = cdc_ncm,
		},
		{
			.data = cdc_commandset,
		},
	},
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_INT,
			.wMaxPacketSize        = 64,
			.bInterval		= 7,
		},
	}
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 0,
	.bNumEndpoints                 = 0,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x01, /* NCM */
	.eps = (USBDescEndpoint[]) {
    }
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 1,
	.bNumEndpoints                 = 2,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x01, /* NCM */
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},{
			.bEndpointAddress      = USB_DIR_OUT | 0x02,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},
	}
}
};

static const USBDescIface desc_iface_mbim_nocompat[] = {
{
	.bInterfaceNumber              = 0,
	.bAlternateSetting             = 0,
	.bNumEndpoints                 = 1,
	.bInterfaceClass               = 0x02,
	.bInterfaceSubClass            = 0x0e, /* MBIM */
	.bInterfaceProtocol            = 0x00,
	.ndesc                         = 3,
	.descs = (USBDescOther[]) {
		{
			.data = cdc_header,
		},
		{
			.data = cdc_mbim,
		},
		{
			.data = cdc_union,
		},
	},
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_INT,
			.wMaxPacketSize        = 64,
			.bInterval		= 7,
		},
	}
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 0,
	.bNumEndpoints                 = 0,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x02, /* MBIM */
	.eps = (USBDescEndpoint[]) {
    }
},{
	.bInterfaceNumber              = 1,
	.bAlternateSetting             = 1,
	.bNumEndpoints                 = 2,
	.bInterfaceClass               = 0x0a, /* CDC Data */
	.bInterfaceSubClass            = 0x00,
	.bInterfaceProtocol            = 0x02, /* MBIM */
	.eps = (USBDescEndpoint[]) {
		{
			.bEndpointAddress      = USB_DIR_IN | 0x01,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},{
			.bEndpointAddress      = USB_DIR_OUT | 0x02,
			.bmAttributes          = USB_ENDPOINT_XFER_BULK,
			.wMaxPacketSize        = 64,
		},
	}
}
};

static const USBDescDevice desc_device_mbim = {
	.bcdUSB                        = 0x0200,
	.bMaxPacketSize0               = 8,
	.bNumConfigurations            = 1,
	.confs = (USBDescConfig[]) {
		{
			.bNumInterfaces        = 2,
			.bConfigurationValue   = 1,
			.bmAttributes          = 0x80,
			.bMaxPower             = 50,
			.nif = ARRAY_SIZE(desc_iface_mbim),
			.ifs = desc_iface_mbim,
		},
	},
};
static const USBDescDevice desc_device_ncm = {
	.bcdUSB                        = 0x0200,
	.bMaxPacketSize0               = 8,
	.bNumConfigurations            = 1,
	.confs = (USBDescConfig[]) {
		{
			.bNumInterfaces        = 2,
			.bConfigurationValue   = 1,
			.bmAttributes          = 0x80,
			.bMaxPower             = 50,
			.nif = ARRAY_SIZE(desc_iface_ncm),
			.ifs = desc_iface_ncm,
		},
	},
};
static const USBDescDevice desc_device_nocompat = {
	.bcdUSB                        = 0x0200,
	.bMaxPacketSize0               = 8,
	.bNumConfigurations            = 1,
	.confs = (USBDescConfig[]) {
		{
			.bNumInterfaces        = 2,
			.bConfigurationValue   = 1,
			.bmAttributes          = 0x80,
			.bMaxPower             = 50,
			.nif = ARRAY_SIZE(desc_iface_mbim_nocompat),
			.ifs = desc_iface_mbim_nocompat,
		},
	},
};

static const USBDesc desc_mbim = {
	.id = {
		.idVendor          = 0xff03,
		.idProduct         = 0xf001,
		.bcdDevice         = 0x0100,
		.iManufacturer     = STR_MANUFACTURER,
		.iProduct          = 0,
		.iSerialNumber     = STR_SERIALNUMBER,
	},
	.full = &desc_device_mbim,
	.str  = desc_strings,
};

static const USBDesc desc_ncm = {
	.id = {
		.idVendor          = 0xff03,
		.idProduct         = 0xf002,
		.bcdDevice         = 0x0100,
		.iManufacturer     = STR_MANUFACTURER,
		.iProduct          = 0,
		.iSerialNumber     = STR_SERIALNUMBER,
	},
	.full = &desc_device_ncm,
	.str  = desc_strings,
};
static const USBDesc desc_mbim_nocompat = {
	.id = {
		.idVendor          = 0xff03,
		.idProduct         = 0xf003,
		.bcdDevice         = 0x0100,
		.iManufacturer     = STR_MANUFACTURER,
		.iProduct          = 0,
		.iSerialNumber     = STR_SERIALNUMBER,
	},
	.full = &desc_device_nocompat,
	.str  = desc_strings,
};

static const uint8_t ncm_ntb_parameters[] = {
	0x1c, 0x00,		/* __le16	wLength; */
	0x00, 0x00,		/* __le16	bmNtbFormatsSupported; */
	0x00, 0x00, 0x02, 0x00,	/* __le32	dwNtbInMaxSize; */
	0x20, 0x00,		/* __le16	wNdpInDivisor; */
	0x00, 0x00,		/* __le16	wNdpInPayloadRemainder; */
	0x20, 0x00,		/* __le16	wNdpInAlignment; */
	0x00, 0x00,		/* __le16	wPadding1; */
	0x00, 0x80, 0x00, 0x00,	/* __le32	dwNtbOutMaxSize; */
	0x04, 0x00,		/* __le16	wNdpOutDivisor; */
	0x00, 0x00,		/* __le16	wNdpOutPayloadRemainder; */
	0x20, 0x00,		/* __le16	wNdpOutAlignment; */
	0x00, 0x00,		/* __le16	wNtbOutMaxDatagrams; */
};

static void usb_mbim_handle_reset(USBDevice *dev)
{

	DPRINTF("Reset\n");
}

static int usb_mbim_handle_control(USBDevice *dev, USBPacket *p,
				int request, int value, int index, int length, uint8_t *data)
{
	int ret;

	DPRINTF("got control %x, value %x\n",request, value);
	ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
	if (ret >= 0) {
		return ret;
	}
	
	ret = 0;

	/* Class specific requests.  */
	switch (request) {
	case InterfaceOutClass | USB_CDC_SET_NTB_INPUT_SIZE:
		DPRINTF("USB_CDC_SET_NTB_INPUT_SIZE is not implemented\n");
		goto fail;
	case InterfaceOutClass | USB_CDC_SET_CRC_MODE:
		DPRINTF("USB_CDC_SET_CRC_MODE is not implemented\n");
		goto fail;
	case InterfaceOutClass | USB_CDC_SET_NTB_FORMAT:
		DPRINTF("USB_CDC_SET_NTB_FORMAT is not implemented\n");
		goto fail;
	case InterfaceOutClass | USB_CDC_SET_MAX_DATAGRAM_SIZE:
		DPRINTF("USB_CDC_SET_MAX_DATAGRAM_SIZE is not implemented\n");
		goto fail;
	case InterfaceInClass | USB_CDC_GET_NTB_PARAMETERS:
		DPRINTF("USB_CDC_GET_NTB_PARAMETERS, length=%d, sizeof(ncm_ntb_parameters)=%zu\n", length, sizeof(ncm_ntb_parameters));
		if (length == sizeof(ncm_ntb_parameters))
			memcpy(data, ncm_ntb_parameters, sizeof(ncm_ntb_parameters));
		else
			goto fail;
		ret = sizeof(ncm_ntb_parameters);
		break;
	case InterfaceInClass | USB_CDC_GET_MAX_DATAGRAM_SIZE:
		DPRINTF("USB_CDC_GET_MAX_DATAGRAM_SIZE is not implemented\n");
		goto fail;
	default:
		DPRINTF("got unsupported/bogus control %x, value %x\n", request, value);
	fail:
		ret = USB_RET_STALL;
		break;
	}
	return ret;
}

static int usb_mbim_handle_data(USBDevice *dev, USBPacket *p)
{
	int ret = 0;
	uint8_t devep = p->ep->nr;

	DPRINTF("ep=0x%02x\n",devep);
	
	switch (p->pid) {
	case USB_TOKEN_OUT:
		break;
		
	case USB_TOKEN_IN:
		break;
		
	default:
		DPRINTF("Bad token\n");
		ret = USB_RET_STALL;
		break;
	}
	
	return ret;
}

static void usb_mbim_handle_destroy(USBDevice *dev)
{
    DPRINTF("\n");
}

static int usb_mbim_initfn(USBDevice *dev)
{
    DPRINTF("\n");
    usb_desc_create_serial(dev);
    usb_desc_init(dev);
    usb_mbim_handle_reset(dev);
    return 0;
}

static const VMStateDescription vmstate_usb_mbim = {
    .name = "usb-mbim",
    .unmigratable = 1,
};

static const VMStateDescription vmstate_usb_ncm = {
    .name = "usb-ncm",
    .unmigratable = 1,
};
static const VMStateDescription vmstate_usb_mbim_nocompat = {
    .name = "usb-mbim-nocompat",
    .unmigratable = 1,
};

static void usb_mbim_class_initfn(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);

    uc->init = usb_mbim_initfn;
    uc->product_desc   = "QEMU USB CDC MBIM";
    uc->usb_desc       = &desc_mbim;
    uc->handle_reset   = usb_mbim_handle_reset;
    uc->handle_control = usb_mbim_handle_control;
    uc->handle_data    = usb_mbim_handle_data;
    uc->handle_destroy = usb_mbim_handle_destroy;
    dc->vmsd = &vmstate_usb_mbim;
}

static void usb_ncm_class_initfn(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);

    uc->init = usb_mbim_initfn;
    uc->product_desc   = "QEMU USB CDC NCM";
    uc->usb_desc       = &desc_ncm;
    uc->handle_reset   = usb_mbim_handle_reset;
    uc->handle_control = usb_mbim_handle_control;
    uc->handle_data    = usb_mbim_handle_data;
    uc->handle_destroy = usb_mbim_handle_destroy;
    dc->vmsd = &vmstate_usb_mbim;
}

static void usb_mbim_nocompat_class_initfn(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);

    uc->init = usb_mbim_initfn;
    uc->product_desc   = "QEMU USB CDC MBIM";
    uc->usb_desc       = &desc_mbim_nocompat;
    uc->handle_reset   = usb_mbim_handle_reset;
    uc->handle_control = usb_mbim_handle_control;
    uc->handle_data    = usb_mbim_handle_data;
    uc->handle_destroy = usb_mbim_handle_destroy;
    dc->vmsd = &vmstate_usb_mbim;
}

static TypeInfo mbim_info = {
    .name          = "usb-mbim",
    .parent        = TYPE_USB_DEVICE,
    .instance_size = sizeof(USBDevice),
    .class_init    = usb_mbim_class_initfn,
};

static TypeInfo ncm_info = {
    .name          = "usb-ncm",
    .parent        = TYPE_USB_DEVICE,
    .instance_size = sizeof(USBDevice),
    .class_init    = usb_ncm_class_initfn,
};

static TypeInfo mbim_nocompat_info = {
    .name          = "usb-mbim-nocompat",
    .parent        = TYPE_USB_DEVICE,
    .instance_size = sizeof(USBDevice),
    .class_init    = usb_mbim_nocompat_class_initfn,
};

static void usb_mbim_register_types(void)
{
    type_register_static(&mbim_info);
    usb_legacy_register("usb-mbim", "mbim", NULL);
    type_register_static(&ncm_info);
    usb_legacy_register("usb-ncm", "ncm", NULL);
    type_register_static(&mbim_nocompat_info);
    usb_legacy_register("usb-mbim-nocompat", "mbim-nocompat", NULL);
}

type_init(usb_mbim_register_types)