12045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle/*
22045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Copyright (C) 2010 The Android Open Source Project
32045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
42045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Licensed under the Apache License, Version 2.0 (the "License");
52045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * you may not use this file except in compliance with the License.
62045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * You may obtain a copy of the License at
72045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
82045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *      http://www.apache.org/licenses/LICENSE-2.0
92045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
102045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Unless required by applicable law or agreed to in writing, software
112045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * distributed under the License is distributed on an "AS IS" BASIS,
122045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * See the License for the specific language governing permissions and
142045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * limitations under the License.
152045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle */
162045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
172045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle#ifndef UBLOCK_UBLOCK_H
182045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle#define UBLOCK_UBLOCK_H
192045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
202045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle#include <stdint.h>
212045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle#include <sys/types.h>
222045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
232045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle#define UBLOCK_VERSION 0
242045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
252045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttlestruct ublock_ctx;
262045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
272045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttlestruct ublock_ops {
282045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle  int (*read)(char *buf, uint64_t length, uint64_t offset);
292045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle  int (*write)(const char *buf, uint64_t length, uint64_t offset);
302045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle};
312045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
322045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle/*
332045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Initializes a ublock block device.
342045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
352045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * May call your read and write functions as the kernel scans partition
362045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * tables.  Will return once the kernel has finished adding the block device.
372045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
382045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Returns 0 on success,
392045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *   -ENOMEM if we ran out of memory,
402045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *   -ENOENT if the kernel appears to lack ublock support, and
412045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *   -EPROTO if the kernel did something unexpected.
422045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle */
432045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttleint ublock_init(struct ublock_ctx **ub, struct ublock_ops *ops, uint64_t size);
442045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
452045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle/*
462045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Returns the index of a ublock block device.
472045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
482045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Returns -EFAULT if the context is NULL,
492045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *         -EINVAL if the context is invalid.
502045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle */
512045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttleint ublock_index(struct ublock_ctx *ub);
522045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
532045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle/*
542045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Runs a loop waiting for ublock requests and calling the ops callbacks.
552045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *
562045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Returns 0 if the loop was stopped by ublock_stop,
572045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle *   -EPROTO if the loop was stopped by a protocol error,
582045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle */
592045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttleint ublock_run(struct ublock_ctx *ub);
602045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
612045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle/*
622045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Stops ublock_run, if it is running.  Should be called from one of the ops
632045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * callbacks.
642045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle */
652045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttlevoid ublock_stop(struct ublock_ctx *ub);
662045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
672045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle/*
682045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle * Destroys a ublock block device.
692045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle */
702045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttlevoid ublock_destroy(struct ublock_ctx *ub);
712045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle
722045e6e39d6dbfed1e46c333c2910c66e788cd36Thomas Tuttle#endif
73