| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | * FPrint USB transfer handling | ||
| 3 | * Copyright (C) 2019 Benjamin Berg <bberg@redhat.com> | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | * Lesser General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU Lesser General Public | ||
| 16 | * License along with this library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #pragma once | ||
| 21 | |||
| 22 | #include <gusb.h> | ||
| 23 | #include "fpi-compat.h" | ||
| 24 | #include "fpi-device.h" | ||
| 25 | |||
| 26 | G_BEGIN_DECLS | ||
| 27 | |||
| 28 | #define FPI_TYPE_USB_TRANSFER (fpi_usb_transfer_get_type ()) | ||
| 29 | |||
| 30 | #define FPI_USB_ENDPOINT_IN 0x80 | ||
| 31 | #define FPI_USB_ENDPOINT_OUT 0x00 | ||
| 32 | |||
| 33 | typedef struct _FpiUsbTransfer FpiUsbTransfer; | ||
| 34 | typedef struct _FpiSsm FpiSsm; | ||
| 35 | |||
| 36 | typedef void (*FpiUsbTransferCallback)(FpiUsbTransfer *transfer, | ||
| 37 | FpDevice *dev, | ||
| 38 | gpointer user_data, | ||
| 39 | GError *error); | ||
| 40 | |||
| 41 | /** | ||
| 42 | * FpiTransferType: | ||
| 43 | * @FP_TRANSFER_NONE: Type not set | ||
| 44 | * @FP_TRANSFER_BULK: Bulk transfer | ||
| 45 | * @FP_TRANSFER_CONTROL: Control transfer | ||
| 46 | * @FP_TRANSFER_INTERRUPT: Interrupt transfer | ||
| 47 | * | ||
| 48 | * Type of the transfer. | ||
| 49 | */ | ||
| 50 | typedef enum { | ||
| 51 | FP_TRANSFER_NONE = -1, | ||
| 52 | FP_TRANSFER_CONTROL = 0, | ||
| 53 | FP_TRANSFER_BULK = 2, | ||
| 54 | FP_TRANSFER_INTERRUPT = 3, | ||
| 55 | } FpiTransferType; | ||
| 56 | |||
| 57 | /** | ||
| 58 | * FpiUsbTransfer: | ||
| 59 | * @device: The #FpDevice that the transfer belongs to. | ||
| 60 | * @ssm: Storage slot to associate the transfer with a state machine. | ||
| 61 | * Used by fpi_ssm_usb_transfer_cb() to modify the given state machine. | ||
| 62 | * @length: The requested length of the transfer in bytes. | ||
| 63 | * @actual_length: The actual length of the transfer | ||
| 64 | * (see also fpi_usb_transfer_set_short_error()) | ||
| 65 | * @buffer: The transferred data. | ||
| 66 | * | ||
| 67 | * Helper for handling USB transfers. | ||
| 68 | */ | ||
| 69 | struct _FpiUsbTransfer | ||
| 70 | { | ||
| 71 | /*< public >*/ | ||
| 72 | FpDevice *device; | ||
| 73 | |||
| 74 | FpiSsm *ssm; | ||
| 75 | |||
| 76 | gssize length; | ||
| 77 | gssize actual_length; | ||
| 78 | |||
| 79 | guchar *buffer; | ||
| 80 | |||
| 81 | /*< private >*/ | ||
| 82 | guint ref_count; | ||
| 83 | |||
| 84 | /* USB Transfer information */ | ||
| 85 | FpiTransferType type; | ||
| 86 | guint8 endpoint; | ||
| 87 | |||
| 88 | /* Control Transfer options */ | ||
| 89 | GUsbDeviceDirection direction; | ||
| 90 | GUsbDeviceRequestType request_type; | ||
| 91 | GUsbDeviceRecipient recipient; | ||
| 92 | guint8 request; | ||
| 93 | guint16 value; | ||
| 94 | guint16 idx; | ||
| 95 | |||
| 96 | /* Flags */ | ||
| 97 | gboolean short_is_error; | ||
| 98 | |||
| 99 | /* Callbacks */ | ||
| 100 | gpointer user_data; | ||
| 101 | FpiUsbTransferCallback callback; | ||
| 102 | |||
| 103 | /* Data free function */ | ||
| 104 | GDestroyNotify free_buffer; | ||
| 105 | }; | ||
| 106 | |||
| 107 | GType fpi_usb_transfer_get_type (void) G_GNUC_CONST; | ||
| 108 | FpiUsbTransfer *fpi_usb_transfer_new (FpDevice *device); | ||
| 109 | FpiUsbTransfer *fpi_usb_transfer_ref (FpiUsbTransfer *self); | ||
| 110 | void fpi_usb_transfer_unref (FpiUsbTransfer *self); | ||
| 111 | |||
| 112 | void fpi_usb_transfer_set_short_error (FpiUsbTransfer *transfer, | ||
| 113 | gboolean short_is_error); | ||
| 114 | |||
| 115 | void fpi_usb_transfer_fill_bulk (FpiUsbTransfer *transfer, | ||
| 116 | guint8 endpoint, | ||
| 117 | gsize length); | ||
| 118 | |||
| 119 | FP_GNUC_ACCESS (read_only, 3, 4) | ||
| 120 | void fpi_usb_transfer_fill_bulk_full (FpiUsbTransfer *transfer, | ||
| 121 | guint8 endpoint, | ||
| 122 | guint8 *buffer, | ||
| 123 | gsize length, | ||
| 124 | GDestroyNotify free_func); | ||
| 125 | |||
| 126 | void fpi_usb_transfer_fill_control (FpiUsbTransfer *transfer, | ||
| 127 | GUsbDeviceDirection direction, | ||
| 128 | GUsbDeviceRequestType request_type, | ||
| 129 | GUsbDeviceRecipient recipient, | ||
| 130 | guint8 request, | ||
| 131 | guint16 value, | ||
| 132 | guint16 idx, | ||
| 133 | gsize length); | ||
| 134 | |||
| 135 | void fpi_usb_transfer_fill_interrupt (FpiUsbTransfer *transfer, | ||
| 136 | guint8 endpoint, | ||
| 137 | gsize length); | ||
| 138 | |||
| 139 | FP_GNUC_ACCESS (read_only, 3, 4) | ||
| 140 | void fpi_usb_transfer_fill_interrupt_full (FpiUsbTransfer *transfer, | ||
| 141 | guint8 endpoint, | ||
| 142 | guint8 *buffer, | ||
| 143 | gsize length, | ||
| 144 | GDestroyNotify free_func); | ||
| 145 | |||
| 146 | void fpi_usb_transfer_submit (FpiUsbTransfer *transfer, | ||
| 147 | guint timeout_ms, | ||
| 148 | GCancellable *cancellable, | ||
| 149 | FpiUsbTransferCallback callback, | ||
| 150 | gpointer user_data); | ||
| 151 | |||
| 152 | gboolean fpi_usb_transfer_submit_sync (FpiUsbTransfer *transfer, | ||
| 153 | guint timeout_ms, | ||
| 154 | GError **error); | ||
| 155 | |||
| 156 | |||
| 157 | 3/4✓ Branch 0 (93→94) taken 4 times. ✓ Branch 1 (93→95) taken 2 times. ✓ Branch 2 (98→99) taken 1 times. ✗ Branch 3 (98→100) not taken. | 1053 | G_DEFINE_AUTOPTR_CLEANUP_FUNC (FpiUsbTransfer, fpi_usb_transfer_unref) | 
| 158 | |||
| 159 | G_END_DECLS | ||
| 160 |