1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <ddk/driver.h>
8#include <fbl/type_support.h>
9#include <fbl/unique_ptr.h>
10#include <zircon/assert.h>
11
12// DDK audio-codec protocol support
13//
14// :: Mixins ::
15//
16// ddk::AudioCodecProtocol is a mixin class that simplifies writing DDK drivers that
17// interact with the audio-codec protocol. It takes care of implementing the function
18// pointer tables and calling into the object that wraps it.
19//
20// :: Examples ::
21//
22// // A driver that implements a ZX_PROTOCOL_AUDIO_CODEC device
23// class AudioCodecDevice;
24// using AudioCodecDeviceType = ddk::Device<AudioCodecDevice, /* ddk mixins */>;
25//
26// class AudioCodecDevice : public AudioCodecDeviceType,
27//                          public ddk::AudioCodecProtocol<AudioCodecDevice> {
28//   public:
29//     AudioCodecDevice(zx_device_t* parent)
30//       : AudioCodecDeviceType("my-audio-codec-device", parent) {}
31//
32//     zx_status_t Bind() {
33//         DdkAdd();
34//     }
35//
36//     void DdkRelease() {
37//         // Clean up
38//     }
39//
40//     ...
41//   private:
42//     ...
43// };
44
45namespace ddk {
46
47template <typename D>
48class AudioCodecProtocol : public internal::base_protocol {
49public:
50    AudioCodecProtocol() {
51        // Can only inherit from one base_protocol implemenation
52        ZX_ASSERT(ddk_proto_id_ == 0);
53        ddk_proto_id_ = ZX_PROTOCOL_AUDIO_CODEC;
54        ddk_proto_ops_ = &ops_;
55    }
56
57private:
58    // Empty struct to use for ops, so that we do not break the invariant that ddk_proto_ops_ is
59    // non-null for devices with a protocol.
60    struct {
61    } ops_;
62};
63
64} // namespace ddk
65