| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 | /** @addtogroup libusbdev
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * Common USB types and functions.
|
|---|
| 33 | */
|
|---|
| 34 | #ifndef LIBUSBDEV_DEVICE_CONNECTION_H_
|
|---|
| 35 | #define LIBUSBDEV_DEVICE_CONNECTION_H_
|
|---|
| 36 |
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <devman.h>
|
|---|
| 39 | #include <usb/usb.h>
|
|---|
| 40 | #include <usb/hc.h>
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | /** Abstraction of a physical connection to the device.
|
|---|
| 44 | * This type is an abstraction of the USB wire that connects the host and
|
|---|
| 45 | * the function (device).
|
|---|
| 46 | */
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | /** Connection to the host controller device is connected to. */
|
|---|
| 49 | usb_hc_connection_t *hc_connection;
|
|---|
| 50 | /** Address of the device. */
|
|---|
| 51 | usb_address_t address;
|
|---|
| 52 | } usb_device_connection_t;
|
|---|
| 53 |
|
|---|
| 54 | /** Initialize device connection. Set address and hc connection.
|
|---|
| 55 | * @param instance Structure to initialize.
|
|---|
| 56 | * @param hc_connection. Host controller connection to use.
|
|---|
| 57 | * @param address USB address.
|
|---|
| 58 | * @return Error code.
|
|---|
| 59 | */
|
|---|
| 60 | static inline int usb_device_connection_initialize(
|
|---|
| 61 | usb_device_connection_t *instance, usb_hc_connection_t *hc_connection,
|
|---|
| 62 | usb_address_t address)
|
|---|
| 63 | {
|
|---|
| 64 | assert(instance);
|
|---|
| 65 | if (hc_connection == NULL)
|
|---|
| 66 | return EBADMEM;
|
|---|
| 67 | if (!usb_address_is_valid(address))
|
|---|
| 68 | return EINVAL;
|
|---|
| 69 |
|
|---|
| 70 | instance->hc_connection = hc_connection;
|
|---|
| 71 | instance->address = address;
|
|---|
| 72 | return EOK;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | /** Get data from the device.
|
|---|
| 77 | * @param[in] instance device connection structure to use.
|
|---|
| 78 | * @param[in] ep target endpoint's number.
|
|---|
| 79 | * @param[in] setup Setup stage data (control transfers).
|
|---|
| 80 | * @param[in] data data buffer.
|
|---|
| 81 | * @param[in] size size of the data buffer.
|
|---|
| 82 | * @param[out] rsize bytes actually copied to the buffer.
|
|---|
| 83 | * @return Error code.
|
|---|
| 84 | */
|
|---|
| 85 | static inline int usb_device_control_read(usb_device_connection_t *instance,
|
|---|
| 86 | usb_endpoint_t ep, uint64_t setup, void *data, size_t size, size_t *rsize)
|
|---|
| 87 | {
|
|---|
| 88 | assert(instance);
|
|---|
| 89 | return usb_hc_read(instance->hc_connection,
|
|---|
| 90 | instance->address, ep, setup, data, size, rsize);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /** Send data to the device.
|
|---|
| 94 | * @param instance device connection structure to use.
|
|---|
| 95 | * @param ep target endpoint's number.
|
|---|
| 96 | * @param setup Setup stage data (control transfers).
|
|---|
| 97 | * @param data data buffer.
|
|---|
| 98 | * @param size size of the data buffer.
|
|---|
| 99 | * @return Error code.
|
|---|
| 100 | */
|
|---|
| 101 | static inline int usb_device_control_write(usb_device_connection_t *instance,
|
|---|
| 102 | usb_endpoint_t ep, uint64_t setup, const void *data, size_t size)
|
|---|
| 103 | {
|
|---|
| 104 | assert(instance);
|
|---|
| 105 | return usb_hc_write(instance->hc_connection,
|
|---|
| 106 | instance->address, ep, setup, data, size);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Wrapper for read calls with no setup stage.
|
|---|
| 110 | * @param[in] instance device connection structure.
|
|---|
| 111 | * @param[in] address USB device address.
|
|---|
| 112 | * @param[in] endpoint USB device endpoint.
|
|---|
| 113 | * @param[in] data Data buffer.
|
|---|
| 114 | * @param[in] size Size of the buffer.
|
|---|
| 115 | * @param[out] real_size Size of the transferred data.
|
|---|
| 116 | * @return Error code.
|
|---|
| 117 | */
|
|---|
| 118 | static inline int usb_device_read(usb_device_connection_t *instance,
|
|---|
| 119 | usb_endpoint_t ep, void *data, size_t size, size_t *real_size)
|
|---|
| 120 | {
|
|---|
| 121 | return usb_device_control_read(instance, ep, 0, data, size, real_size);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Wrapper for write calls with no setup stage.
|
|---|
| 125 | * @param instance device connection structure.
|
|---|
| 126 | * @param address USB device address.
|
|---|
| 127 | * @param endpoint USB device endpoint.
|
|---|
| 128 | * @param data Data buffer.
|
|---|
| 129 | * @param size Size of the buffer.
|
|---|
| 130 | * @return Error code.
|
|---|
| 131 | */
|
|---|
| 132 | static inline int usb_device_write(usb_device_connection_t *instance,
|
|---|
| 133 | usb_endpoint_t ep, const void *data, size_t size)
|
|---|
| 134 | {
|
|---|
| 135 | return usb_device_control_write(instance, ep, 0, data, size);
|
|---|
| 136 | }
|
|---|
| 137 | #endif
|
|---|
| 138 | /**
|
|---|
| 139 | * @}
|
|---|
| 140 | */
|
|---|