Harald Welte | f5e7264 | 2022-10-30 22:20:55 +0100 | [diff] [blame] | 1 | /* (C) 2019 by Harald Welte <laforge@gnumonks.org> |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published |
| 8 | * by the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include <libusb.h> |
| 22 | |
| 23 | #include "idt82v2081.h" |
| 24 | #include "idt82v2081_usb.h" |
| 25 | |
| 26 | /* Adaption layer between idt82 driver and libusb */ |
| 27 | |
| 28 | struct idt82_libusb_infos { |
| 29 | struct libusb_device_handle *devh; |
| 30 | uint8_t ep; |
| 31 | }; |
| 32 | |
| 33 | /* backend function for core idt82 driver */ |
| 34 | int idt82_reg_read(struct idt82 *idt, uint8_t reg) |
| 35 | { |
| 36 | struct idt82_libusb_infos *pi = idt->priv; |
| 37 | int rv; |
| 38 | uint8_t val; |
| 39 | |
| 40 | rv = libusb_control_transfer(pi->devh, 0xc2, 0x02, reg, pi->ep, &val, 1, 1000); |
| 41 | if (rv != 1) |
| 42 | return -EPIPE; |
| 43 | |
| 44 | return val; |
| 45 | } |
| 46 | |
| 47 | /* backend function for core idt82 driver */ |
| 48 | int idt82_reg_write(struct idt82 *idt, uint8_t reg, uint8_t val) |
| 49 | { |
| 50 | struct idt82_libusb_infos *pi = idt->priv; |
| 51 | int rv; |
| 52 | |
| 53 | rv = libusb_control_transfer(pi->devh, 0x42, 0x01, reg, pi->ep, &val, 1, 1000); |
| 54 | if (rv != 1) |
| 55 | return -EPIPE; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int idt82_usb_init(struct idt82 *idt, struct libusb_device_handle *devh, uint8_t ep) |
| 61 | { |
| 62 | struct idt82_libusb_infos *pi; |
| 63 | |
| 64 | idt->priv = pi = malloc(sizeof(struct idt82_libusb_infos)); |
| 65 | |
| 66 | pi->devh = devh; |
| 67 | pi->ep = ep; |
| 68 | |
| 69 | return 0; |
| 70 | } |