1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef NOS_DEVICE_H
17 #define NOS_DEVICE_H
18 
19 #include <stdint.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* Max data size for read/write.
26  * Yes, it's a magic number. See b/37675056#comment8. */
27 #define MAX_DEVICE_TRANSFER 2044
28 
29 struct nos_device_ops {
30   /**
31    * Read a datagram from the device.
32    *
33    * Return 0 on success and a negative value on failure.
34    */
35   int (*read)(void* ctx, uint32_t command, uint8_t *buf, uint32_t len);
36 
37   /**
38    * Write a datagram to the device.
39    *
40    * Return 0 on success and a negative value on failure.
41    */
42   int (*write)(void *ctx, uint32_t command, const uint8_t *buf, uint32_t len);
43 
44   /**
45    * Block until an event has happened on the device, or until timed out.
46    *
47    * Values for msecs
48    *  <0 wait forever
49    *   0 return immediately (why?)
50    *  >0 timeout after this many milliseconds
51    *
52    * Returns:
53    *  <0 on error
54    *   0 timed out
55    *  >0 interrupt occurred
56    */
57   int (*wait_for_interrupt)(void *ctx, int msecs);
58 
59   /**
60    * Reset the device.
61    *
62    * Return 0 on success and a negative value on failure.
63    */
64   int (*reset)(void *ctx);
65 
66   /**
67    * Close the connection to the device.
68    *
69    * The device must not be used after closing.
70    */
71   void (*close)(void *ctx);
72 };
73 
74 struct nos_device {
75   void *ctx;
76   struct nos_device_ops ops;
77 };
78 
79 /*
80  * Open a connection to a Nugget device.
81  *
82  * The name parameter identifies which Nugget device to connect to. Passing
83  * NULL connects to the default device.
84  *
85  * This function is implemented by the host specific variants of this library.
86  *
87  * Returns 0 on success or negative on failure.
88  */
89 int nos_device_open(const char *name, struct nos_device *device);
90 
91 #ifdef __cplusplus
92 }
93 #endif
94 
95 #endif /* NOS_DEVICE_H */
96