forked from xinyu391/zircon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform-device.cpp
More file actions
637 lines (564 loc) · 22.1 KB
/
platform-device.cpp
File metadata and controls
637 lines (564 loc) · 22.1 KB
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
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform-device.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ddk/binding.h>
#include <ddk/debug.h>
#include <ddk/device.h>
#include <ddk/driver.h>
#include <ddk/metadata.h>
#include <ddk/platform-defs.h>
#include <fbl/function.h>
#include <zircon/syscalls/resource.h>
#include "platform-bus.h"
namespace platform_bus {
zx_status_t PlatformDevice::Create(const pbus_dev_t* pdev, zx_device_t* parent, PlatformBus* bus,
fbl::unique_ptr<platform_bus::PlatformDevice>* out) {
fbl::AllocChecker ac;
fbl::unique_ptr<platform_bus::PlatformDevice> dev(new (&ac)
platform_bus::PlatformDevice(parent, bus, pdev));
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
auto status = dev->Init(pdev);
if (status != ZX_OK) {
return status;
}
out->swap(dev);
return ZX_OK;
}
PlatformDevice::PlatformDevice(zx_device_t* parent, PlatformBus* bus, const pbus_dev_t* pdev)
: PlatformDeviceType(parent), bus_(bus), vid_(pdev->vid), pid_(pdev->pid),
did_(pdev->did), resource_tree_(ROOT_DEVICE_ID) {
strlcpy(name_, pdev->name, sizeof(name_));
}
zx_status_t PlatformDevice::Init(const pbus_dev_t* pdev) {
uint32_t next_device_id = ROOT_DEVICE_ID + 1;
auto status = resource_tree_.Init(pdev, &next_device_id);
if (status != ZX_OK) {
return status;
}
fbl::AllocChecker ac;
device_index_.reserve(resource_tree_.DeviceCount(), &ac);
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
resource_tree_.BuildDeviceIndex(&device_index_);
return ZX_OK;
}
// Create a resource and pass it back to the proxy along with necessary metadata
// to create/map the VMO in the driver process.
zx_status_t PlatformDevice::RpcGetMmio(const DeviceResources* dr, uint32_t index, zx_paddr_t* out_paddr,
size_t* out_length, zx_handle_t* out_handle,
uint32_t* out_handle_count) {
if (index >= dr->mmio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
const pbus_mmio_t& mmio = dr->mmio(index);
zx_handle_t handle;
char rsrc_name[ZX_MAX_NAME_LEN];
snprintf(rsrc_name, ZX_MAX_NAME_LEN - 1, "%s.pbus[%u]", name_, index);
zx_status_t status = zx_resource_create(bus_->GetResource(), ZX_RSRC_KIND_MMIO, mmio.base,
mmio.length, rsrc_name, sizeof(rsrc_name), &handle);
if (status != ZX_OK) {
zxlogf(ERROR, "%s: pdev_rpc_get_mmio: zx_resource_create failed: %d\n", name_, status);
return status;
}
*out_paddr = mmio.base;
*out_length = mmio.length;
*out_handle_count = 1;
*out_handle = handle;
return ZX_OK;
}
// Create a resource and pass it back to the proxy along with necessary metadata
// to create the IRQ in the driver process.
zx_status_t PlatformDevice::RpcGetInterrupt(const DeviceResources* dr, uint32_t index,
uint32_t* out_irq, uint32_t* out_mode,
zx_handle_t* out_handle, uint32_t* out_handle_count) {
if (index >= dr->irq_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
zx_handle_t handle;
const pbus_irq_t& irq = dr->irq(index);
uint32_t options = ZX_RSRC_KIND_IRQ | ZX_RSRC_FLAG_EXCLUSIVE;
char rsrc_name[ZX_MAX_NAME_LEN];
snprintf(rsrc_name, ZX_MAX_NAME_LEN - 1, "%s.pbus[%u]", name_, index);
zx_status_t status = zx_resource_create(bus_->GetResource(), options, irq.irq, 1, rsrc_name,
sizeof(rsrc_name), &handle);
if (status != ZX_OK) {
return status;
}
*out_irq = irq.irq;
*out_mode = irq.mode;
*out_handle_count = 1;
*out_handle = handle;
return status;
}
zx_status_t PlatformDevice::RpcGetBti(const DeviceResources* dr, uint32_t index,
zx_handle_t* out_handle, uint32_t* out_handle_count) {
if (index >= dr->bti_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
const pbus_bti_t& bti = dr->bti(index);
zx_status_t status = bus_->IommuGetBti(bti.iommu_index, bti.bti_id, out_handle);
if (status == ZX_OK) {
*out_handle_count = 1;
}
return status;
}
zx_status_t PlatformDevice::RpcGetDeviceInfo(const DeviceResources* dr, pdev_device_info_t* out_info) {
pdev_device_info_t info = {
.vid = vid_,
.pid = pid_,
.did = did_,
.mmio_count = static_cast<uint32_t>(dr->mmio_count()),
.irq_count = static_cast<uint32_t>(dr->irq_count()),
.gpio_count = static_cast<uint32_t>(dr->gpio_count()),
.i2c_channel_count = static_cast<uint32_t>(dr->i2c_channel_count()),
.clk_count = static_cast<uint32_t>(dr->clk_count()),
.bti_count = static_cast<uint32_t>(dr->bti_count()),
.metadata_count = static_cast<uint32_t>(dr->metadata_count() + dr->boot_metadata_count()),
.reserved = {},
.name = {},
};
static_assert(sizeof(info.name) == sizeof(name_), "");
memcpy(info.name, name_, sizeof(out_info->name));
memcpy(out_info, &info, sizeof(info));
return ZX_OK;
}
zx_status_t PlatformDevice::RpcDeviceAdd(const DeviceResources* dr, uint32_t index,
uint32_t* out_device_id) {
if (index >= dr->child_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
// TODO(voydanoff) verify that this device has not already been added?
*out_device_id = dr->child_index(index);
return ZX_OK;
}
zx_status_t PlatformDevice::RpcGetMetadata(const DeviceResources* dr, uint32_t index,
uint32_t* out_type, uint8_t* buf, uint32_t buf_size,
uint32_t* actual) {
if (index >= dr->metadata_count() + dr->boot_metadata_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
if (index < dr->metadata_count()) {
auto& metadata = dr->metadata(index);
if (metadata.len > buf_size) {
return ZX_ERR_BUFFER_TOO_SMALL;
}
memcpy(buf, metadata.data, metadata.len);
*out_type = metadata.type;
*actual = metadata.len;
return ZX_OK;
} else {
// boot_metadata indices follow metadata indices.
index -= static_cast<uint32_t>(dr->metadata_count());
auto& metadata = dr->boot_metadata(index);
const void* data;
uint32_t length;
auto status = bus_->GetZbiMetadata(metadata.zbi_type, metadata.zbi_extra, &data, &length);
if (status == ZX_OK) {
if (length > buf_size) {
return ZX_ERR_BUFFER_TOO_SMALL;
}
memcpy(buf, data, length);
*out_type = metadata.zbi_type;
*actual = length;
}
return status;
}
}
zx_status_t PlatformDevice::RpcGetProtocols(const DeviceResources* dr, uint32_t* out_protocols,
uint32_t* out_protocol_count) {
auto count = dr->protocol_count();
memcpy(out_protocols, dr->protocols(), count * sizeof(*out_protocols));
*out_protocol_count = static_cast<uint32_t>(count);
return ZX_OK;
}
zx_status_t PlatformDevice::RpcGpioConfigIn(const DeviceResources* dr, uint32_t index, uint32_t flags) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->ConfigIn(dr->gpio(index).gpio, flags);
}
zx_status_t PlatformDevice::RpcGpioConfigOut(const DeviceResources* dr, uint32_t index,
uint8_t initial_value) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->ConfigOut(dr->gpio(index).gpio, initial_value);
}
zx_status_t PlatformDevice::RpcGpioSetAltFunction(const DeviceResources* dr, uint32_t index,
uint64_t function) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->SetAltFunction(dr->gpio(index).gpio, function);
}
zx_status_t PlatformDevice::RpcGpioRead(const DeviceResources* dr, uint32_t index,
uint8_t* out_value) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->Read(dr->gpio(index).gpio, out_value);
}
zx_status_t PlatformDevice::RpcGpioWrite(const DeviceResources* dr, uint32_t index, uint8_t value) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->Write(dr->gpio(index).gpio, value);
}
zx_status_t PlatformDevice::RpcGpioGetInterrupt(const DeviceResources* dr, uint32_t index,
uint32_t flags, zx_handle_t* out_handle,
uint32_t* out_handle_count) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
zx_status_t status = bus_->gpio()->GetInterrupt(dr->gpio(index).gpio, flags, out_handle);
if (status == ZX_OK) {
*out_handle_count = 1;
}
return status;
}
zx_status_t PlatformDevice::RpcGpioReleaseInterrupt(const DeviceResources* dr, uint32_t index) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->ReleaseInterrupt(dr->gpio(index).gpio);
}
zx_status_t PlatformDevice::RpcGpioSetPolarity(const DeviceResources* dr, uint32_t index,
uint32_t flags) {
if (bus_->gpio() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->gpio_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->gpio()->SetPolarity(dr->gpio(index).gpio, flags);
}
zx_status_t PlatformDevice::RpcI2cTransact(const DeviceResources* dr, uint32_t txid,
rpc_i2c_req_t* req, zx_handle_t channel) {
if (bus_->i2c() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
uint32_t index = req->index;
if (index >= dr->i2c_channel_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
const pbus_i2c_channel_t& pdev_channel = dr->i2c_channel(index);
return bus_->I2cTransact(txid, req, &pdev_channel, channel);
}
zx_status_t PlatformDevice::RpcI2cGetMaxTransferSize(const DeviceResources* dr, uint32_t index,
size_t* out_size) {
if (bus_->i2c() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->i2c_channel_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
const pbus_i2c_channel_t& pdev_channel = dr->i2c_channel(index);
return bus_->i2c()->GetMaxTransferSize(pdev_channel.bus_id, out_size);
}
zx_status_t PlatformDevice::RpcClkEnable(const DeviceResources* dr, uint32_t index) {
if (bus_->clk() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->clk_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->clk()->Enable(dr->clk(index).clk);
}
zx_status_t PlatformDevice::RpcClkDisable(const DeviceResources* dr, uint32_t index) {
if (bus_->clk() == nullptr) {
return ZX_ERR_NOT_SUPPORTED;
}
if (index >= dr->clk_count()) {
return ZX_ERR_OUT_OF_RANGE;
}
return bus_->clk()->Disable(dr->clk(index).clk);
}
zx_status_t PlatformDevice::DdkRxrpc(zx_handle_t channel) {
if (channel == ZX_HANDLE_INVALID) {
// proxy device has connected
return ZX_OK;
}
uint8_t req_buf[PROXY_MAX_TRANSFER_SIZE];
uint8_t resp_buf[PROXY_MAX_TRANSFER_SIZE];
auto* req_header = reinterpret_cast<platform_proxy_req_t*>(&req_buf);
auto* resp_header = reinterpret_cast<platform_proxy_rsp_t*>(&resp_buf);
uint32_t actual;
zx_handle_t req_handles[ZX_CHANNEL_MAX_MSG_HANDLES];
zx_handle_t resp_handles[ZX_CHANNEL_MAX_MSG_HANDLES];
uint32_t req_handle_count;
uint32_t resp_handle_count = 0;
auto status = zx_channel_read(channel, 0, &req_buf, req_handles, sizeof(req_buf),
fbl::count_of(req_handles), &actual, &req_handle_count);
if (status != ZX_OK) {
zxlogf(ERROR, "platform_dev_rxrpc: zx_channel_read failed %d\n", status);
return status;
}
const uint32_t index = req_header->device_id;
if (index >= device_index_.size()) {
return ZX_ERR_OUT_OF_RANGE;
}
const DeviceResources* dr = device_index_[index];
resp_header->txid = req_header->txid;
uint32_t resp_len;
switch (req_header->proto_id) {
case ZX_PROTOCOL_PLATFORM_DEV: {
auto req = reinterpret_cast<rpc_pdev_req_t*>(&req_buf);
if (actual < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu\n", __FUNCTION__, actual, sizeof(*req));
return ZX_ERR_INTERNAL;
}
auto resp = reinterpret_cast<rpc_pdev_rsp_t*>(&resp_buf);
resp_len = sizeof(*resp);
switch (req_header->op) {
case PDEV_GET_MMIO:
status = RpcGetMmio(dr, req->index, &resp->paddr, &resp->length, resp_handles,
&resp_handle_count);
break;
case PDEV_GET_INTERRUPT:
status = RpcGetInterrupt(dr, req->index, &resp->irq, &resp->mode, resp_handles,
&resp_handle_count);
break;
case PDEV_GET_BTI:
status = RpcGetBti(dr, req->index, resp_handles, &resp_handle_count);
break;
case PDEV_GET_DEVICE_INFO:
status = RpcGetDeviceInfo(dr, &resp->device_info);
break;
case PDEV_GET_BOARD_INFO:
status = bus_->GetBoardInfo(&resp->board_info);
break;
case PDEV_DEVICE_ADD:
status = RpcDeviceAdd(dr, req->index, &resp->device_id);
break;
case PDEV_GET_METADATA: {
auto resp = reinterpret_cast<rpc_pdev_metadata_rsp_t*>(resp_buf);
static_assert(sizeof(*resp) == sizeof(resp_buf), "");
auto buf_size = static_cast<uint32_t>(sizeof(resp_buf) - sizeof(*resp_header));
status = RpcGetMetadata(dr, req->index, &resp->pdev.metadata_type, resp->metadata,
buf_size, &resp->pdev.metadata_length);
resp_len += resp->pdev.metadata_length;
break;
}
case PDEV_GET_PROTOCOLS: {
auto protos = reinterpret_cast<uint32_t*>(&resp[1]);
status = RpcGetProtocols(dr, protos, &resp->protocol_count);
resp_len += static_cast<uint32_t>(resp->protocol_count * sizeof(*protos));
break;
}
default:
zxlogf(ERROR, "%s: unknown pdev op %u\n", __func__, req_header->op);
return ZX_ERR_INTERNAL;
}
break;
}
case ZX_PROTOCOL_GPIO: {
auto req = reinterpret_cast<rpc_gpio_req_t*>(&req_buf);
if (actual < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu\n", __FUNCTION__, actual, sizeof(*req));
return ZX_ERR_INTERNAL;
}
auto resp = reinterpret_cast<rpc_gpio_rsp_t*>(&resp_buf);
resp_len = sizeof(*resp);
switch (req_header->op) {
case GPIO_CONFIG_IN:
status = RpcGpioConfigIn(dr, req->index, req->flags);
break;
case GPIO_CONFIG_OUT:
status = RpcGpioConfigOut(dr, req->index, req->value);
break;
case GPIO_SET_ALT_FUNCTION:
status = RpcGpioSetAltFunction(dr, req->index, req->alt_function);
break;
case GPIO_READ:
status = RpcGpioRead(dr, req->index, &resp->value);
break;
case GPIO_WRITE:
status = RpcGpioWrite(dr, req->index, req->value);
break;
case GPIO_GET_INTERRUPT:
status = RpcGpioGetInterrupt(dr, req->index, req->flags, resp_handles,
&resp_handle_count);
break;
case GPIO_RELEASE_INTERRUPT:
status = RpcGpioReleaseInterrupt(dr, req->index);
break;
case GPIO_SET_POLARITY:
status = RpcGpioSetPolarity(dr, req->index, req->polarity);
break;
default:
zxlogf(ERROR, "%s: unknown GPIO op %u\n", __func__, req_header->op);
return ZX_ERR_INTERNAL;
}
break;
}
case ZX_PROTOCOL_I2C: {
auto req = reinterpret_cast<rpc_i2c_req_t*>(&req_buf);
if (actual < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu\n", __FUNCTION__, actual, sizeof(*req));
return ZX_ERR_INTERNAL;
}
auto resp = reinterpret_cast<rpc_i2c_rsp_t*>(&resp_buf);
resp_len = sizeof(*resp);
switch (req_header->op) {
case I2C_GET_MAX_TRANSFER:
status = RpcI2cGetMaxTransferSize(dr, req->index, &resp->max_transfer);
break;
case I2C_TRANSACT: {
status = RpcI2cTransact(dr, req_header->txid, req, channel);
if (status == ZX_OK) {
// If platform_i2c_transact succeeds, we return immmediately instead of calling
// zx_channel_write below. Instead we will respond in platform_i2c_complete().
return ZX_OK;
}
break;
}
default:
zxlogf(ERROR, "%s: unknown I2C op %u\n", __func__, req_header->op);
return ZX_ERR_INTERNAL;
}
break;
}
case ZX_PROTOCOL_CLK: {
auto req = reinterpret_cast<rpc_clk_req_t*>(&req_buf);
if (actual < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu\n", __FUNCTION__, actual, sizeof(*req));
return ZX_ERR_INTERNAL;
}
resp_len = sizeof(*resp_header);
switch (req_header->op) {
case CLK_ENABLE:
status = RpcClkEnable(dr, req->index);
break;
case CLK_DISABLE:
status = RpcClkDisable(dr, req->index);
break;
default:
zxlogf(ERROR, "%s: unknown clk op %u\n", __func__, req_header->op);
return ZX_ERR_INTERNAL;
}
break;
}
default: {
platform_proxy_args_t args = {
.req = req_header,
.req_size = actual,
.resp = resp_header,
.resp_size = sizeof(resp_buf),
.req_handles = req_handles,
.req_handle_count = req_handle_count,
.resp_handles = resp_handles,
.resp_handle_count = fbl::count_of(resp_handles),
.resp_actual_size = 0,
.resp_actual_handles = 0,
};
status = bus_->Proxy(&args);
if (status == ZX_OK) {
status = args.resp->status;
}
resp_len = args.resp_actual_size;
resp_handle_count = args.resp_actual_handles;
break;
}
}
// set op to match request so zx_channel_write will return our response
resp_header->status = status;
status = zx_channel_write(channel, 0, resp_header, resp_len,
(resp_handle_count ? resp_handles : nullptr), resp_handle_count);
if (status != ZX_OK) {
zxlogf(ERROR, "platform_dev_rxrpc: zx_channel_write failed %d\n", status);
}
return status;
}
void PlatformDevice::DdkRelease() {
delete this;
}
zx_status_t PlatformDevice::Start() {
char name[ZX_DEVICE_NAME_MAX];
if (vid_ == PDEV_VID_GENERIC && pid_ == PDEV_PID_GENERIC && did_ == PDEV_DID_KPCI) {
strlcpy(name, "pci", sizeof(name));
} else {
snprintf(name, sizeof(name), "%02x:%02x:%01x", vid_, pid_, did_);
}
char argstr[64];
snprintf(argstr, sizeof(argstr), "pdev:%s,", name);
// Platform devices run in their own devhosts.
uint32_t device_add_flags = DEVICE_ADD_MUST_ISOLATE;
const DeviceResources* dr = device_index_[ROOT_DEVICE_ID];
const size_t metadata_count = dr->metadata_count();
const size_t boot_metadata_count = dr->boot_metadata_count();
if (metadata_count > 0 || boot_metadata_count > 0) {
// Keep device invisible until after we add its metadata.
device_add_flags |= DEVICE_ADD_INVISIBLE;
}
zx_status_t status;
if (dr->protocol_count() > 0) {
// PlatformDevice::Start with protocols
status = DdkAdd(name, device_add_flags, nullptr, 0, ZX_PROTOCOL_PLATFORM_PROXY, argstr);
} else {
zx_device_prop_t props[] = {
{BIND_PLATFORM_DEV_VID, 0, vid_},
{BIND_PLATFORM_DEV_PID, 0, pid_},
{BIND_PLATFORM_DEV_DID, 0, did_},
};
status = DdkAdd(name, device_add_flags, props, fbl::count_of(props), ZX_PROTOCOL_PLATFORM_DEV,
argstr);
}
if (status != ZX_OK) {
return status;
}
if (metadata_count > 0 || boot_metadata_count > 0) {
for (size_t i = 0; i < metadata_count; i++) {
const auto& metadata = dr->metadata(i);
status = DdkAddMetadata(metadata.type, metadata.data, metadata.len);
if (status != ZX_OK) {
DdkRemove();
return status;
}
}
for (size_t i = 0; i < boot_metadata_count; i++) {
const auto& metadata = dr->boot_metadata(i);
const void* data;
uint32_t length;
status = bus_->GetZbiMetadata(metadata.zbi_type, metadata.zbi_extra, &data, &length);
if (status == ZX_OK) {
status = DdkAddMetadata(metadata.zbi_type, data, length);
}
if (status != ZX_OK) {
DdkRemove();
return status;
}
}
DdkMakeVisible();
}
return ZX_OK;
}
} // namespace platform_bus