-/* CP2103 ioctls */
-#define IOCTL_GPIOGET 0x8000 /* Get gpio bits */
-#define IOCTL_GPIOSET 0x8001 /* Set gpio bits */
-#define IOCTL_GPIOBIC 0x8002 /* Clear specific gpio bit(s) */
-#define IOCTL_GPIOBIS 0x8003 /* Set specific gpio bit(s) */
-
-/* CP210x ioctls principally used during initial device configuration */
-#define IOCTL_DEVICERESET 0x8004 /* Reset the cp210x */
-#define IOCTL_PORTCONFGET 0x8005 /* Get port configuration */
-#define IOCTL_PORTCONFSET 0x8006 /* Set port configuration */
-#define IOCTL_SETVID 0x8007 /* Set vendor id */
-#define IOCTL_SETPID 0x8008 /* Set product id */
-#define IOCTL_SETMFG 0x8009 /* Set manufacturer string */
-#define IOCTL_SETPRODUCT 0x800a /* Set product string */
-#define IOCTL_SETSERIAL 0x800b /* Set serial number string */
-#define IOCTL_SETDEVVER 0x800c /* set device version id */
-/* FIXME: where is IOCTL_SETMFG? */
-
-/* CP2103 GPIO */
-#define GPIO_0 0x01
-#define GPIO_1 0x02
-#define GPIO_2 0x04
-#define GPIO_3 0x08
-#define GPIO_MASK (GPIO_0|GPIO_1|GPIO_2|GPIO_3)
-
-/* GetDeviceVersion() return codes */
-#define CP210x_CP2101_VERSION 0x01
-#define CP210x_CP2102_VERSION 0x02
-#define CP210x_CP2103_VERSION 0x03
-
-/* Return codes */
-#define CP210x_SUCCESS 0x00
-#define CP210x_DEVICE_NOT_FOUND 0xFF
-#define CP210x_INVALID_HANDLE 0x01
-#define CP210x_INVALID_PARAMETER 0x02
-#define CP210x_DEVICE_IO_FAILED 0x03
-#define CP210x_FUNCTION_NOT_SUPPORTED 0x04
-#define CP210x_GLOBAL_DATA_ERROR 0x05
-#define CP210x_FILE_ERROR 0x06
-#define CP210x_COMMAND_FAILED 0x08
-#define CP210x_INVALID_ACCESS_TYPE 0x09
-
-/* USB descriptor sizes */
-#define CP210x_MAX_DEVICE_STRLEN 256
-#define CP210x_MAX_PRODUCT_STRLEN 126
-#define CP210x_MAX_SERIAL_STRLEN 63
-#define CP210x_MAX_MAXPOWER 250
-
-/* Used to pass variable sized buffers between user and kernel space (ioctls) */
-typedef struct {
- char* buf;
- size_t len;
-} cp2101_buffer_t;
-
-/* Port config definitions */
-typedef struct {
- uint16_t mode; /* Push-pull = 1, Open-drain = 0 */
- uint16_t lowPower;
- uint16_t latch; /* Logic high = 1, Logic low = 0 */
-} cp2101_port_state_t;
-
-typedef struct {
- cp2101_port_state_t reset;
- cp2101_port_state_t suspend;
- uint8_t enhancedFxn;
-} cp2101_port_config_t;
-
-#define PORT_CONFIG_LEN 13 /* Because sizeof() will pad to full words */
+/* cp210x_get_partnum() return codes */
+#define CP210x_PART_UNKNOWN 0x00
+#define CP210x_PART_CP2101 0x01
+#define CP210x_PART_CP2102 0x02
+#define CP210x_PART_CP2103 0x03
+
+/* Taken from drivers/usb/gadget/usbstring.c */
+static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len)
+{
+ int count = 0;
+ u8 c;
+ u16 uchar;
+
+ /* this insists on correct encodings, though not minimal ones.
+ * BUT it currently rejects legit 4-byte UTF-8 code points,
+ * which need surrogate pairs. (Unicode 3.1 can use them.)
+ */
+ while (len != 0 && (c = (u8) *s++) != 0) {
+ if (unlikely(c & 0x80)) {
+ // 2-byte sequence:
+ // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
+ if ((c & 0xe0) == 0xc0) {
+ uchar = (c & 0x1f) << 6;
+
+ c = (u8) *s++;
+ if ((c & 0xc0) != 0x80)
+ goto fail;
+ c &= 0x3f;
+ uchar |= c;
+
+ // 3-byte sequence (most CJKV characters):
+ // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
+ } else if ((c & 0xf0) == 0xe0) {
+ uchar = (c & 0x0f) << 12;
+
+ c = (u8) *s++;
+ if ((c & 0xc0) != 0x80)
+ goto fail;
+ c &= 0x3f;
+ uchar |= c << 6;
+
+ c = (u8) *s++;
+ if ((c & 0xc0) != 0x80)
+ goto fail;
+ c &= 0x3f;
+ uchar |= c;
+
+ /* no bogus surrogates */
+ if (0xd800 <= uchar && uchar <= 0xdfff)
+ goto fail;
+
+ // 4-byte sequence (surrogate pairs, currently rare):
+ // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
+ // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
+ // (uuuuu = wwww + 1)
+ // FIXME accept the surrogate code points (only)
+
+ } else
+ goto fail;
+ } else
+ uchar = c;
+ *cp++ = uchar;
+ count++;
+ len--;
+ }
+ return count;
+fail:
+ return -1;
+}
+