11c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull/*
21c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * Copyright (C) 2017 The Android Open Source Project
31c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull *
41c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * Licensed under the Apache License, Version 2.0 (the "License");
51c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * you may not use this file except in compliance with the License.
61c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * You may obtain a copy of the License at
71c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull *
81c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull *      http://www.apache.org/licenses/LICENSE-2.0
91c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull *
101c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * Unless required by applicable law or agreed to in writing, software
111c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * distributed under the License is distributed on an "AS IS" BASIS,
121c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * See the License for the specific language governing permissions and
141c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * limitations under the License.
151c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull */
161c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#ifndef NOS_DEVICE_H
171c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#define NOS_DEVICE_H
181c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
191c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#include <stdint.h>
201c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
211c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#ifdef __cplusplus
221c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scullextern "C" {
231c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#endif
241c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
251c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull/* Max data size for read/write.
261c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull * TODO: Yes, it's a magic number. */
271c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#define MAX_DEVICE_TRANSFER 2044
281c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
291c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scullstruct nos_device_ops {
3036ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  /**
3136ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   * Read a datagram from the device.
3236ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   *
3336ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   * Return 0 on success and a negative value on failure.
3436ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   */
3536ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  int (*read)(void* ctx, uint32_t command, uint8_t *buf, uint32_t len);
361c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
3736ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  /**
3836ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   * Write a datagram to the device.
3936ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   *
4036ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   * Return 0 on success and a negative value on failure.
4136ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   */
4236ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  int (*write)(void *ctx, uint32_t command, const uint8_t *buf, uint32_t len);
4396517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull
4436ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  /**
45d1727636df79205f215b3112369cc01bebe4419dBill Richardson   * Block until an event has happened on the device, or until timed out.
46d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *
47d1727636df79205f215b3112369cc01bebe4419dBill Richardson   * Values for msecs
48d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *  <0 wait forever
49d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *   0 return immediately (why?)
50d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *  >0 timeout after this many milliseconds
51d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *
52d1727636df79205f215b3112369cc01bebe4419dBill Richardson   * Returns:
53d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *  <0 on error
54d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *   0 timed out
55d1727636df79205f215b3112369cc01bebe4419dBill Richardson   *  >0 interrupt occurred
5643aa2e90b53932dc6670e05b6e12fb9d3f178615Allen Webb   */
57d1727636df79205f215b3112369cc01bebe4419dBill Richardson  int (*wait_for_interrupt)(void *ctx, int msecs);
5843aa2e90b53932dc6670e05b6e12fb9d3f178615Allen Webb
5943aa2e90b53932dc6670e05b6e12fb9d3f178615Allen Webb  /**
608408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull   * Reset the device.
618408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull   *
628408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull   * Return 0 on success and a negative value on failure.
638408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull   */
648408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull  int (*reset)(void *ctx);
658408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull
668408937412be9c6d7659e19df67d94f2f7d82015Andrew Scull  /**
6736ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   * Close the connection to the device.
6836ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   *
6936ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   * The device must not be used after closing.
7036ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull   */
7136ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  void (*close)(void *ctx);
721c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull};
731c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
741c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scullstruct nos_device {
7536ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  void *ctx;
7636ebf2dbe695e70de53f97537ea07f5745d3d5dbAndrew Scull  struct nos_device_ops ops;
771c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull};
781c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
7996517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull/*
8096517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull * Open a connection to a Nugget device.
8196517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull *
8296517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull * The name parameter identifies which Nugget device to connect to. Passing
8396517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull * NULL connects to the default device.
8496517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull *
8596517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull * This function is implemented by the host specific variants of this library.
8696517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull *
8796517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull * Returns 0 on success or negative on failure.
8896517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull */
8996517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scullint nos_device_open(const char *name, struct nos_device *device);
9096517d86f7e9e17a6e038880828393f00eca4cf2Andrew Scull
911c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#ifdef __cplusplus
921c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull}
931c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#endif
941c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull
951c021e8e850ed1ac7c5595309bbcef80697ad920Andrew Scull#endif /* NOS_DEVICE_H */
96