diff options
author | Krzysztof Opasiak <k.opasiak@samsung.com> | 2016-05-13 10:39:43 +0200 |
---|---|---|
committer | Seung-Woo Kim <sw0312.kim@samsung.com> | 2016-07-25 14:39:49 +0900 |
commit | 0cad2e199664c9cb316c39e17b63dc979174cd3f (patch) | |
tree | e3ed998f5018ab768080d43731b57dbe53a0d07c | |
parent | 5aa6ebc9046376519cf9c90a2fdef4abd2fb4ed6 (diff) | |
download | lthor-0cad2e199664c9cb316c39e17b63dc979174cd3f.tar.gz lthor-0cad2e199664c9cb316c39e17b63dc979174cd3f.tar.bz2 lthor-0cad2e199664c9cb316c39e17b63dc979174cd3f.zip |
Add cdc-acm related implementation of thor protocol
Thor protocol "emulates" cdc-acm class. This commit adds
some helper functions which are required to properly
setup communication on cdc-acm protocol level.
Change-Id: Iffb79c813f97bc3f3c4260f6fd713d0ea4d99353
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | libthor/thor_acm.c | 113 | ||||
-rw-r--r-- | libthor/thor_internal.h | 2 |
2 files changed, 115 insertions, 0 deletions
diff --git a/libthor/thor_acm.c b/libthor/thor_acm.c new file mode 100644 index 0000000..69eae53 --- /dev/null +++ b/libthor/thor_acm.c @@ -0,0 +1,113 @@ +/* + * libthor - Tizen Thor communication protocol + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <sys/types.h> +#include <stdio.h> +#include <endian.h> +#ifdef __linux__ +#include <linux/usb/cdc.h> +#else +#include <stdint.h> + +#ifdef __APPLE__ +#include <libkern/OSByteOrder.h> + +#define htole32(x) OSSwapHostToLittleInt32(x) +#endif + +struct usb_cdc_line_coding { + uint32_t dwDTERate; /* little endian */ + uint8_t bCharFormat; +#define USB_CDC_1_STOP_BITS 0 + + uint8_t bParityType; +#define USB_CDC_NO_PARITY 0 + + uint8_t bDataBits; +} __attribute__ ((packed)); + +#define USB_CDC_REQ_SET_LINE_CODING 0x20 +#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22 +#endif + +#include <libusb-1.0/libusb.h> + +#include "thor_internal.h" + +static int acm_set_control_line_state(struct thor_device_handle *th, int state) +{ + int ret; + + ret = libusb_control_transfer(th->devh, + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + USB_CDC_REQ_SET_CONTROL_LINE_STATE, + state ? 0x3 : 0, + (uint16_t)th->control_interface_id, + NULL, + 0, + DEFAULT_TIMEOUT); + + if (ret < 0) + return ret; + + return 0; +} + +static int acm_set_line_coding(struct thor_device_handle *th) +{ + struct usb_cdc_line_coding default_thor_line_coding = { + .dwDTERate = htole32(9600), + .bCharFormat = USB_CDC_1_STOP_BITS, + .bParityType = USB_CDC_NO_PARITY, + .bDataBits = 8, + }; + int ret; + + ret = libusb_control_transfer(th->devh, + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + USB_CDC_REQ_SET_LINE_CODING, + 0, + (uint16_t)th->control_interface_id, + (unsigned char *)&default_thor_line_coding, + sizeof(default_thor_line_coding), + DEFAULT_TIMEOUT); + + if (ret < 0) + return ret; + + return 0; +} + + +int t_acm_prepare_device(struct thor_device_handle *th) +{ + int ret; + + ret = acm_set_control_line_state(th, 0); + if (ret < 0) + return ret; + + ret = acm_set_line_coding(th); + if (ret < 0) + return ret; + + ret = acm_set_control_line_state(th, 1); + + return ret; +} + diff --git a/libthor/thor_internal.h b/libthor/thor_internal.h index 20085b4..70ccfd4 100644 --- a/libthor/thor_internal.h +++ b/libthor/thor_internal.h @@ -65,5 +65,7 @@ int t_usb_find_device(struct thor_device_id *dev_id, int wait, void t_usb_close_device(struct thor_device_handle *th); +int t_acm_prepare_device(struct thor_device_handle *th); + #endif /* THOR_INTERNAL_H__ */ |