forked from xinyu391/zircon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform-proxy-device.cpp
More file actions
818 lines (707 loc) · 26.1 KB
/
platform-proxy-device.cpp
File metadata and controls
818 lines (707 loc) · 26.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// Copyright 2018 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-proxy-device.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <ddk/binding.h>
#include <ddk/debug.h>
#include <ddk/device.h>
#include <ddk/driver.h>
#include <ddk/protocol/platform-bus.h>
#include <ddk/protocol/platform-device.h>
#include <fbl/auto_call.h>
#include <fbl/unique_ptr.h>
#include <lib/zx/vmar.h>
#include <lib/zx/vmo.h>
#include "platform-proxy.h"
#include "proxy-protocol.h"
// The implementation of the platform bus protocol in this file is for use by
// drivers that exist in a proxy devhost and communicate with the platform bus
// over an RPC channel.
//
// More information can be found at the top of platform-device.cpp.
namespace platform_bus {
zx_status_t ProxyDevice::GpioConfigIn(void* ctx, uint32_t flags) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_CONFIG_IN;
req.index = gpio_ctx->index;
req.flags = flags;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
}
zx_status_t ProxyDevice::GpioConfigOut(void* ctx, uint8_t initial_value) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_CONFIG_OUT;
req.index = gpio_ctx->index;
req.value = initial_value;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
}
zx_status_t ProxyDevice::GpioSetAltFunction(void* ctx, uint64_t function) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_SET_ALT_FUNCTION;
req.index = gpio_ctx->index;
req.alt_function = function;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
}
zx_status_t ProxyDevice::GpioGetInterrupt(void* ctx, uint32_t flags, zx_handle_t* out_handle) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_GET_INTERRUPT;
req.index = gpio_ctx->index;
req.flags = flags;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp),
nullptr, 0, out_handle, 1, nullptr);
}
zx_status_t ProxyDevice::GpioSetPolarity(void* ctx, uint32_t polarity) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_SET_POLARITY;
req.index = gpio_ctx->index;
req.polarity = polarity;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
}
zx_status_t ProxyDevice::GpioReleaseInterrupt(void* ctx) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_RELEASE_INTERRUPT;
req.index = gpio_ctx->index;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
}
zx_status_t ProxyDevice::GpioRead(void* ctx, uint8_t* out_value) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_READ;
req.index = gpio_ctx->index;
auto status = thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
if (status != ZX_OK) {
return status;
}
*out_value = resp.value;
return ZX_OK;
}
zx_status_t ProxyDevice::GpioWrite(void* ctx, uint8_t value) {
auto gpio_ctx = static_cast<GpioCtx*>(ctx);
auto thiz = gpio_ctx->thiz;
rpc_gpio_req_t req = {};
rpc_gpio_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_GPIO;
req.header.op = GPIO_WRITE;
req.index = gpio_ctx->index;
req.value = value;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
}
zx_status_t ProxyDevice::I2cGetMaxTransferSize(void* ctx, size_t* out_size) {
auto i2c_ctx = static_cast<I2cCtx*>(ctx);
auto thiz = i2c_ctx->thiz;
rpc_i2c_req_t req = {};
rpc_i2c_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_I2C;
req.header.op = I2C_GET_MAX_TRANSFER;
req.index = i2c_ctx->index;
auto status = thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp.header,
sizeof(resp));
if (status == ZX_OK) {
*out_size = resp.max_transfer;
}
return status;
}
zx_status_t ProxyDevice::I2cGetInterrupt(void* ctx, uint32_t flags, zx_handle_t* out_handle) {
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::I2cTransact(void* ctx, i2c_op_t* ops, size_t cnt,
i2c_transact_cb transact_cb, void* cookie) {
auto i2c_ctx = static_cast<I2cCtx*>(ctx);
auto thiz = i2c_ctx->thiz;
size_t writes_length = 0;
size_t reads_length = 0;
for (size_t i = 0; i < cnt; ++i) {
if (ops[i].is_read) {
reads_length += ops[i].length;
} else {
writes_length += ops[i].length;
}
}
if (!writes_length && !reads_length) {
return ZX_ERR_INVALID_ARGS;
}
size_t req_length = sizeof(rpc_i2c_req_t) + cnt * sizeof(i2c_rpc_op_t) + writes_length;
if (req_length >= PROXY_MAX_TRANSFER_SIZE) {
return ZX_ERR_INVALID_ARGS;
}
uint8_t req_buffer[PROXY_MAX_TRANSFER_SIZE];
auto req = reinterpret_cast<rpc_i2c_req_t*>(req_buffer);
req->header.proto_id = ZX_PROTOCOL_I2C;
req->header.op = I2C_TRANSACT;
req->index = i2c_ctx->index;
req->cnt = cnt;
req->transact_cb = transact_cb;
req->cookie = cookie;
auto rpc_ops = reinterpret_cast<i2c_rpc_op_t*>(req + 1);
ZX_ASSERT(cnt < I2C_MAX_RW_OPS);
for (size_t i = 0; i < cnt; ++i) {
rpc_ops[i].length = ops[i].length;
rpc_ops[i].is_read = ops[i].is_read;
rpc_ops[i].stop = ops[i].stop;
}
uint8_t* p_writes = reinterpret_cast<uint8_t*>(rpc_ops) + cnt * sizeof(i2c_rpc_op_t);
for (size_t i = 0; i < cnt; ++i) {
if (!ops[i].is_read) {
memcpy(p_writes, ops[i].buf, ops[i].length);
p_writes += ops[i].length;
}
}
const size_t resp_length = sizeof(rpc_i2c_rsp_t) + reads_length;
if (resp_length >= PROXY_MAX_TRANSFER_SIZE) {
return ZX_ERR_INVALID_ARGS;
}
uint8_t resp_buffer[PROXY_MAX_TRANSFER_SIZE];
rpc_i2c_rsp_t* rsp = reinterpret_cast<rpc_i2c_rsp_t*>(resp_buffer);
uint32_t actual;
auto status = thiz->proxy_->Rpc(thiz->device_id_, &req->header,
static_cast<uint32_t>(req_length),
&rsp->header, static_cast<uint32_t>(resp_length),
nullptr, 0, nullptr, 0, &actual);
if (status != ZX_OK) {
return status;
}
// TODO(voydanoff) This proxying code actually implements i2c_transact synchronously
// due to the fact that it is unsafe to respond asynchronously on the devmgr rxrpc channel.
// In the future we may want to redo the plumbing to allow this to be truly asynchronous.
if (actual != resp_length) {
status = ZX_ERR_INTERNAL;
} else {
status = rsp->header.status;
}
if (transact_cb) {
i2c_op_t read_ops[I2C_MAX_RW_OPS];
size_t read_ops_cnt = 0;
uint8_t* p_reads = reinterpret_cast<uint8_t*>(rsp + 1);
for (size_t i = 0; i < cnt; ++i) {
if (ops[i].is_read) {
read_ops[read_ops_cnt] = ops[i];
read_ops[read_ops_cnt].buf = p_reads;
read_ops_cnt++;
p_reads += ops[i].length;
}
}
transact_cb(status, read_ops, read_ops_cnt, rsp->cookie);
}
return ZX_OK;
}
zx_status_t ProxyDevice::ClkEnable(void* ctx, uint32_t index) {
ProxyDevice* thiz = static_cast<ProxyDevice*>(ctx);
rpc_clk_req_t req = {};
platform_proxy_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_CLK;
req.header.op = CLK_ENABLE;
req.index = index;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp, sizeof(resp));
}
zx_status_t ProxyDevice::ClkDisable(void* ctx, uint32_t index) {
ProxyDevice* thiz = static_cast<ProxyDevice*>(ctx);
rpc_clk_req_t req = {};
platform_proxy_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_CLK;
req.header.op = CLK_DISABLE;
req.index = index;
return thiz->proxy_->Rpc(thiz->device_id_, &req.header, sizeof(req), &resp, sizeof(resp));
}
zx_status_t ProxyDevice::GetMmio(uint32_t index, pdev_mmio_t* out_mmio) {
if (index >= mmios_.size()) {
return ZX_ERR_OUT_OF_RANGE;
}
const Mmio& mmio = mmios_[index];
const zx_paddr_t vmo_base = ROUNDDOWN(mmio.base, PAGE_SIZE);
const size_t vmo_size = ROUNDUP(mmio.base + mmio.length - vmo_base, PAGE_SIZE);
zx::vmo vmo;
zx_status_t status = zx_vmo_create_physical(mmio.resource.get(), vmo_base, vmo_size,
vmo.reset_and_get_address());
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: creating vmo failed %d\n", name_, __FUNCTION__, status);
return status;
}
char name[32];
snprintf(name, sizeof(name), "%s mmio %u", name_, index);
status = vmo.set_property(ZX_PROP_NAME, name, sizeof(name));
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: setting vmo name failed %d\n", name_, __FUNCTION__, status);
return status;
}
out_mmio->offset = mmio.base - vmo_base;
out_mmio->vmo = vmo.release();
out_mmio->size = mmio.length;
return ZX_OK;
}
// TODO(surajmalhotra): Remove after migrating all clients off.
zx_status_t ProxyDevice::MapMmio(uint32_t index, uint32_t cache_policy, void** out_vaddr,
size_t* out_size, zx_paddr_t* out_paddr,
zx_handle_t* out_handle) {
if (index >= mmios_.size()) {
return ZX_ERR_OUT_OF_RANGE;
}
const Mmio& mmio = mmios_[index];
const zx_paddr_t vmo_base = ROUNDDOWN(mmio.base, PAGE_SIZE);
const size_t vmo_size = ROUNDUP(mmio.base + mmio.length - vmo_base, PAGE_SIZE);
zx::vmo vmo;
zx_status_t status = zx_vmo_create_physical(mmio.resource.get(), vmo_base, vmo_size,
vmo.reset_and_get_address());
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: creating vmo failed %d\n", name_, __FUNCTION__, status);
return status;
}
char name[32];
snprintf(name, sizeof(name), "%s mmio %u", name_, index);
status = vmo.set_property(ZX_PROP_NAME, name, sizeof(name));
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: setting vmo name failed %d\n", name_, __FUNCTION__, status);
return status;
}
status = vmo.set_cache_policy(cache_policy);
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: setting cache policy failed %d\n", name_, __FUNCTION__, status);
return status;
}
uintptr_t virt;
status = zx::vmar::root_self()->map(0, vmo, 0, vmo_size, ZX_VM_PERM_READ |
ZX_VM_PERM_WRITE | ZX_VM_MAP_RANGE, &virt);
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: mapping vmar failed %d\n", name_, __FUNCTION__, status);
return status;
}
*out_size = mmio.length;
if (out_paddr) {
*out_paddr = mmio.base;
}
*out_vaddr = reinterpret_cast<void*>(virt + (mmio.base - vmo_base));
*out_handle = vmo.release();
return ZX_OK;
}
zx_status_t ProxyDevice::MapInterrupt(uint32_t index, uint32_t flags, zx_handle_t* out_handle) {
if (index >= irqs_.size()) {
return ZX_ERR_OUT_OF_RANGE;
}
Irq* irq = &irqs_[index];
if (flags == 0) {
flags = irq->mode;
}
zx_handle_t handle;
zx_status_t status = zx_interrupt_create(irq->resource.get(), irq->irq, flags, &handle);
if (status != ZX_OK) {
zxlogf(ERROR, "%s %s: creating interrupt failed: %d\n", name_, __FUNCTION__, status);
return status;
}
*out_handle = handle;
return ZX_OK;
}
zx_status_t ProxyDevice::GetBti(uint32_t index, zx_handle_t* out_handle) {
rpc_pdev_req_t req = {};
rpc_pdev_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_GET_BTI;
req.index = index;
return proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp), nullptr, 0,
out_handle, 1, nullptr);
}
zx_status_t ProxyDevice::GetDeviceInfo(pdev_device_info_t* out_info) {
rpc_pdev_req_t req = {};
rpc_pdev_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_GET_DEVICE_INFO;
auto status = proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp));
if (status != ZX_OK) {
return status;
}
memcpy(out_info, &resp.device_info, sizeof(*out_info));
return ZX_OK;
}
zx_status_t ProxyDevice::GetBoardInfo(pdev_board_info_t* out_info) {
rpc_pdev_req_t req = {};
rpc_pdev_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_GET_BOARD_INFO;
auto status = proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp));
if (status != ZX_OK) {
return status;
}
memcpy(out_info, &resp.board_info, sizeof(*out_info));
return ZX_OK;
}
zx_status_t ProxyDevice::DeviceAdd(uint32_t index, device_add_args_t* args, zx_device_t** out) {
rpc_pdev_req_t req = {};
rpc_pdev_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_DEVICE_ADD;
req.index = index;
auto status = proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp));
if (status != ZX_OK) {
return status;
}
return CreateChild(zxdev(), resp.device_id, proxy_, args);
}
zx_status_t ProxyDevice::GetProtocol(uint32_t proto_id, uint32_t index, void* out_protocol) {
// Return the GPIO protocol for the given index.
if (proto_id == ZX_PROTOCOL_GPIO) {
if (index >= gpio_ctxs_.size()) {
return ZX_ERR_OUT_OF_RANGE;
}
auto proto = static_cast<gpio_protocol_t*>(out_protocol);
proto->ops = &gpio_proto_ops_;
proto->ctx = &gpio_ctxs_[index];
return ZX_OK;
}
if (proto_id == ZX_PROTOCOL_I2C) {
if (index >= i2c_ctxs_.size()) {
return ZX_ERR_OUT_OF_RANGE;
}
auto proto = static_cast<i2c_protocol_t*>(out_protocol);
proto->ops = &i2c_proto_ops_;
proto->ctx = &i2c_ctxs_[index];
return ZX_OK;
}
// For other protocols, fall through to DdkGetProtocol if index is zero
if (index != 0) {
return ZX_ERR_OUT_OF_RANGE;
}
return DdkGetProtocol(proto_id, out_protocol);
}
zx_status_t ProxyDevice::CreateRoot(zx_device_t* parent, fbl::RefPtr<PlatformProxy> proxy) {
fbl::AllocChecker ac;
auto dev = fbl::make_unique_checked<ProxyDevice>(&ac,parent, ROOT_DEVICE_ID, proxy);
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
auto status = dev->InitRoot();
if (status != ZX_OK) {
return status;
}
// devmgr is now in charge of the device.
__UNUSED auto* dummy = dev.release();
return ZX_OK;
}
zx_status_t ProxyDevice::CreateChild(zx_device_t* parent, uint32_t device_id,
fbl::RefPtr<PlatformProxy> proxy, device_add_args_t* args) {
fbl::AllocChecker ac;
fbl::unique_ptr<ProxyDevice> dev(new (&ac) platform_bus::ProxyDevice(parent, device_id, proxy));
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
auto status = dev->InitChild(args);
if (status != ZX_OK) {
return status;
}
// devmgr is now in charge of the device.
__UNUSED auto* dummy = dev.release();
return ZX_OK;
}
ProxyDevice::ProxyDevice(zx_device_t* parent, uint32_t device_id,
fbl::RefPtr<PlatformProxy> proxy)
: ProxyDeviceType(parent), device_id_(device_id), proxy_(proxy) {
// Initialize protocol ops
clk_proto_ops_.enable = ClkEnable;
clk_proto_ops_.disable = ClkDisable;
gpio_proto_ops_.config_in = GpioConfigIn;
gpio_proto_ops_.config_out = GpioConfigOut;
gpio_proto_ops_.set_alt_function = GpioSetAltFunction;
gpio_proto_ops_.read = GpioRead;
gpio_proto_ops_.write = GpioWrite;
gpio_proto_ops_.get_interrupt = GpioGetInterrupt;
gpio_proto_ops_.release_interrupt = GpioReleaseInterrupt;
gpio_proto_ops_.set_polarity = GpioSetPolarity;
i2c_proto_ops_.transact = I2cTransact;
i2c_proto_ops_.get_max_transfer_size = I2cGetMaxTransferSize;
i2c_proto_ops_.get_interrupt = I2cGetInterrupt;
}
zx_status_t ProxyDevice::InitCommon() {
pdev_device_info_t info;
auto status = GetDeviceInfo(&info);
if (status != ZX_OK) {
return status;
}
memcpy(name_, info.name, sizeof(name_));
metadata_count_ = info.metadata_count;
fbl::AllocChecker ac;
for (uint32_t i = 0; i < info.mmio_count; i++) {
rpc_pdev_req_t req = {};
rpc_pdev_rsp_t resp = {};
zx_handle_t rsrc_handle;
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_GET_MMIO;
req.index = i;
status = proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp),
NULL, 0, &rsrc_handle, 1, NULL);
if (status != ZX_OK) {
return status;
}
Mmio mmio;
mmio.base = resp.paddr;
mmio.length = resp.length;
mmio.resource.reset(rsrc_handle);
mmios_.push_back(fbl::move(mmio), &ac);
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
zxlogf(SPEW, "%s: received MMIO %u (base %#lx length %#lx handle %#x)\n", name_, i,
mmio.base, mmio.length, mmio.resource.get());
}
for (uint32_t i = 0; i < info.irq_count; i++) {
rpc_pdev_req_t req = {};
rpc_pdev_rsp_t resp = {};
zx_handle_t rsrc_handle;
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_GET_INTERRUPT;
req.index = i;
status = proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.header, sizeof(resp),
NULL, 0, &rsrc_handle, 1, NULL);
if (status != ZX_OK) {
return status;
}
Irq irq;
irq.irq = resp.irq;
irq.mode = resp.mode;
irq.resource.reset(rsrc_handle);
irqs_.push_back(fbl::move(irq), &ac);
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
zxlogf(SPEW, "%s: received IRQ %u (irq %#x handle %#x)\n", name_, i, irq.irq,
irq.resource.get());
}
uint32_t gpio_count = info.gpio_count;
if (gpio_count > 0) {
gpio_ctxs_.reset(new (&ac) GpioCtx[gpio_count], gpio_count);
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
for (uint32_t i = 0; i < info.gpio_count; i++) {
gpio_ctxs_[i].thiz = this;
gpio_ctxs_[i].index = i;
}
}
uint32_t i2c_count = info.i2c_channel_count;
if (i2c_count > 0) {
i2c_ctxs_.reset(new (&ac) I2cCtx[i2c_count], i2c_count);
if (!ac.check()) {
return ZX_ERR_NO_MEMORY;
}
for (uint32_t i = 0; i < i2c_count; i++) {
i2c_ctxs_[i].thiz = this;
i2c_ctxs_[i].index = i;
}
}
return ZX_OK;
}
zx_status_t ProxyDevice::InitRoot() {
auto status = InitCommon();
if (status != ZX_OK) {
return status;
}
return DdkAdd(name_);
}
zx_status_t ProxyDevice::InitChild(device_add_args_t* args) {
auto status = InitCommon();
if (status != ZX_OK) {
return status;
}
ctx_ = args->ctx;
device_ops_ = args->ops;
proto_id_ = args->proto_id;
proto_ops_ = args->proto_ops;
device_add_args_t new_args = *args;
// Replace ctx and device protocol ops with ours so we can intercept device_get_protocol().
new_args.ctx = this;
new_args.ops = &ddk_device_proto_;
if (metadata_count_ == 0) {
return device_add(parent(), &new_args, &zxdev_);
}
new_args.flags |= DEVICE_ADD_INVISIBLE;
status = device_add(parent(), &new_args, &zxdev_);
if (status != ZX_OK) {
return status;
}
// Remove ourselves from the devmgr if something goes wrong.
auto cleanup = fbl::MakeAutoCall([this]() { DdkRemove(); });
for (uint32_t i = 0; i < metadata_count_; i++) {
rpc_pdev_req_t req = {};
rpc_pdev_metadata_rsp_t resp = {};
req.header.proto_id = ZX_PROTOCOL_PLATFORM_DEV;
req.header.op = PDEV_GET_METADATA;
req.index = i;
status = proxy_->Rpc(device_id_, &req.header, sizeof(req), &resp.pdev.header,
sizeof(resp));
if (status != ZX_OK) {
return status;
}
status = DdkAddMetadata(resp.pdev.metadata_type, resp.metadata,
resp.pdev.metadata_length);
if (status != ZX_OK) {
return status;
}
}
cleanup.cancel();
// Make ourselves visible after all metadata has been added successfully.
DdkMakeVisible();
return ZX_OK;
}
zx_status_t ProxyDevice::DdkGetProtocol(uint32_t proto_id, void* out) {
auto* proto = static_cast<ddk::AnyProtocol*>(out);
// Try driver's get_protocol() first, if it is implemented.
if (device_ops_ && device_ops_->get_protocol) {
if (device_ops_->get_protocol(ctx_, proto_id, out) == ZX_OK) {
return ZX_OK;
}
}
// Next try driver's primary protocol.
if (proto_ops_ && proto_id_ == proto_id) {
proto->ops = proto_ops_;
proto->ctx = ctx_;
return ZX_OK;
}
// Finally, protocols provided by platform bus.
proto->ctx = this;
switch (proto_id) {
case ZX_PROTOCOL_PLATFORM_DEV: {
proto->ops = &pdev_proto_ops_;
break;
}
case ZX_PROTOCOL_GPIO: {
auto count = gpio_ctxs_.size();
if (count == 0) {
return ZX_ERR_NOT_SUPPORTED;
} else if (count > 1) {
zxlogf(ERROR, "%s: device has more than one GPIO\n", __func__);
return ZX_ERR_BAD_STATE;
}
// Return zeroth GPIO resource.
proto->ops = &gpio_proto_ops_;
proto->ctx = &gpio_ctxs_[0];
return ZX_OK;
}
case ZX_PROTOCOL_I2C: {
auto count = i2c_ctxs_.size();
if (count == 0) {
return ZX_ERR_NOT_SUPPORTED;
} else if (count > 1) {
zxlogf(ERROR, "%s: device has more than one I2C channel\n", __func__);
return ZX_ERR_BAD_STATE;
}
// Return zeroth I2C resource.
proto->ops = &i2c_proto_ops_;
proto->ctx = &i2c_ctxs_[0];
return ZX_OK;
}
case ZX_PROTOCOL_CLK: {
proto->ops = &clk_proto_ops_;
break;
}
default:
return proxy_->GetProtocol(proto_id, out);;
}
return ZX_OK;
}
zx_status_t ProxyDevice::DdkOpen(zx_device_t** dev_out, uint32_t flags) {
if (device_ops_ && device_ops_->open) {
return device_ops_->open(ctx_, dev_out, flags);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkOpenAt(zx_device_t** dev_out, const char* path, uint32_t flags) {
if (device_ops_ && device_ops_->open_at) {
return device_ops_->open_at(ctx_, dev_out, path, flags);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkClose(uint32_t flags) {
if (device_ops_ && device_ops_->close) {
return device_ops_->close(ctx_, flags);
}
return ZX_ERR_NOT_SUPPORTED;
}
void ProxyDevice::DdkUnbind() {
if (device_ops_ && device_ops_->unbind) {
device_ops_->unbind(ctx_);
}
}
void ProxyDevice::DdkRelease() {
if (device_ops_ && device_ops_->release) {
device_ops_->release(ctx_);
}
delete this;
}
zx_status_t ProxyDevice::DdkRead(void* buf, size_t count, zx_off_t off, size_t* actual) {
if (device_ops_ && device_ops_->read) {
return device_ops_->read(ctx_, buf, count, off, actual);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkWrite(const void* buf, size_t count, zx_off_t off, size_t* actual) {
if (device_ops_ && device_ops_->write) {
return device_ops_->write(ctx_, buf, count, off, actual);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_off_t ProxyDevice::DdkGetSize() {
if (device_ops_ && device_ops_->get_size) {
return device_ops_->get_size(ctx_);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkIoctl(uint32_t op, const void* in_buf, size_t in_len, void* out_buf,
size_t out_len, size_t* actual) {
if (device_ops_ && device_ops_->ioctl) {
return device_ops_->ioctl(ctx_, op, in_buf, in_len, out_buf, out_len, actual);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkSuspend(uint32_t flags) {
if (device_ops_ && device_ops_->suspend) {
return device_ops_->suspend(ctx_, flags);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkResume(uint32_t flags) {
if (device_ops_ && device_ops_->resume) {
return device_ops_->resume(ctx_, flags);
}
return ZX_ERR_NOT_SUPPORTED;
}
zx_status_t ProxyDevice::DdkRxrpc(zx_handle_t channel) {
if (device_ops_ && device_ops_->rxrpc) {
return device_ops_->rxrpc(ctx_, channel);
}
return ZX_ERR_NOT_SUPPORTED;
}
} // namespace platform_bus