1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: LGPL-2.1+ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Copyright (C) 2003 David Brownell 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU Lesser General Public License as published 71da177e4SLinus Torvalds * by the Free Software Foundation; either version 2.1 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/errno.h> 121da177e4SLinus Torvalds #include <linux/kernel.h> 13a84d9e53SSebastian Andrzej Siewior #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/list.h> 151da177e4SLinus Torvalds #include <linux/string.h> 161da177e4SLinus Torvalds #include <linux/device.h> 1786dc243cSAlan Stern #include <linux/nls.h> 181da177e4SLinus Torvalds 195f848137SDavid Brownell #include <linux/usb/ch9.h> 209454a57aSDavid Brownell #include <linux/usb/gadget.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /** 241da177e4SLinus Torvalds * usb_gadget_get_string - fill out a string descriptor 251da177e4SLinus Torvalds * @table: of c strings encoded using UTF-8 261da177e4SLinus Torvalds * @id: string id, from low byte of wValue in get string descriptor 2786dc243cSAlan Stern * @buf: at least 256 bytes, must be 16-bit aligned 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Finds the UTF-8 string matching the ID, and converts it into a 301da177e4SLinus Torvalds * string descriptor in utf16-le. 311da177e4SLinus Torvalds * Returns length of descriptor (always even) or negative errno 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * If your driver needs stings in multiple languages, you'll probably 341da177e4SLinus Torvalds * "switch (wIndex) { ... }" in your ep0 string descriptor logic, 351da177e4SLinus Torvalds * using this routine after choosing which set of UTF-8 strings to use. 361da177e4SLinus Torvalds * Note that US-ASCII is a strict subset of UTF-8; any string bytes with 371da177e4SLinus Torvalds * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1 381da177e4SLinus Torvalds * characters (which are also widely used in C strings). 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds int 411da177e4SLinus Torvalds usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf) 421da177e4SLinus Torvalds { 431da177e4SLinus Torvalds struct usb_string *s; 441da177e4SLinus Torvalds int len; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* descriptor 0 has the language id */ 471da177e4SLinus Torvalds if (id == 0) { 481da177e4SLinus Torvalds buf [0] = 4; 491da177e4SLinus Torvalds buf [1] = USB_DT_STRING; 501da177e4SLinus Torvalds buf [2] = (u8) table->language; 511da177e4SLinus Torvalds buf [3] = (u8) (table->language >> 8); 521da177e4SLinus Torvalds return 4; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds for (s = table->strings; s && s->s; s++) 551da177e4SLinus Torvalds if (s->id == id) 561da177e4SLinus Torvalds break; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* unrecognized: stall. */ 591da177e4SLinus Torvalds if (!s || !s->s) 601da177e4SLinus Torvalds return -EINVAL; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* string descriptors have length, tag, then UTF16-LE text */ 631da177e4SLinus Torvalds len = min ((size_t) 126, strlen (s->s)); 6486dc243cSAlan Stern len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN, 6586dc243cSAlan Stern (wchar_t *) &buf[2], 126); 661da177e4SLinus Torvalds if (len < 0) 671da177e4SLinus Torvalds return -EINVAL; 681da177e4SLinus Torvalds buf [0] = (len + 1) * 2; 691da177e4SLinus Torvalds buf [1] = USB_DT_STRING; 701da177e4SLinus Torvalds return buf [0]; 711da177e4SLinus Torvalds } 72a84d9e53SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(usb_gadget_get_string); 73