Skip to content

Commit 8d4e897

Browse files
Manu GautamFelipe Balbi
authored andcommitted
usb: gadget: f_fs: Add support for SuperSpeed Mode
Allow userspace to pass SuperSpeed descriptors and handle them in the driver accordingly. This change doesn't modify existing desc_header and thereby keeps the ABI changes backward compatible i.e. existing userspace drivers compiled with old header (functionfs.h) would continue to work with the updated kernel. Signed-off-by: Manu Gautam <mgautam@codeaurora.org> Acked-by: Michal Nazarewicz <mina86@mina86.com> Signed-off-by: Felipe Balbi <balbi@ti.com>
1 parent d8eb6c6 commit 8d4e897

3 files changed

Lines changed: 147 additions & 50 deletions

File tree

‎drivers/usb/gadget/f_fs.c‎

Lines changed: 136 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ struct ffs_ep {
134134
struct usb_ep *ep; /* P: ffs->eps_lock */
135135
struct usb_request *req; /* P: epfile->mutex */
136136

137-
/* [0]: full speed, [1]: high speed */
138-
struct usb_endpoint_descriptor *descs[2];
137+
/* [0]: full speed, [1]: high speed, [2]: super speed */
138+
struct usb_endpoint_descriptor *descs[3];
139139

140140
u8 num;
141141

@@ -1450,10 +1450,11 @@ static void ffs_data_reset(struct ffs_data *ffs)
14501450
ffs->raw_strings = NULL;
14511451
ffs->stringtabs = NULL;
14521452

1453-
ffs->raw_descs_length = 0;
1454-
ffs->raw_fs_descs_length = 0;
1453+
ffs->raw_fs_hs_descs_length = 0;
1454+
ffs->raw_ss_descs_length = 0;
14551455
ffs->fs_descs_count = 0;
14561456
ffs->hs_descs_count = 0;
1457+
ffs->ss_descs_count = 0;
14571458

14581459
ffs->strings_count = 0;
14591460
ffs->interfaces_count = 0;
@@ -1596,7 +1597,24 @@ static int ffs_func_eps_enable(struct ffs_function *func)
15961597
spin_lock_irqsave(&func->ffs->eps_lock, flags);
15971598
do {
15981599
struct usb_endpoint_descriptor *ds;
1599-
ds = ep->descs[ep->descs[1] ? 1 : 0];
1600+
int desc_idx;
1601+
1602+
if (ffs->gadget->speed == USB_SPEED_SUPER)
1603+
desc_idx = 2;
1604+
else if (ffs->gadget->speed == USB_SPEED_HIGH)
1605+
desc_idx = 1;
1606+
else
1607+
desc_idx = 0;
1608+
1609+
/* fall-back to lower speed if desc missing for current speed */
1610+
do {
1611+
ds = ep->descs[desc_idx];
1612+
} while (!ds && --desc_idx >= 0);
1613+
1614+
if (!ds) {
1615+
ret = -EINVAL;
1616+
break;
1617+
}
16001618

16011619
ep->ep->driver_data = ep;
16021620
ep->ep->desc = ds;
@@ -1731,6 +1749,12 @@ static int __must_check ffs_do_desc(char *data, unsigned len,
17311749
}
17321750
break;
17331751

1752+
case USB_DT_SS_ENDPOINT_COMP:
1753+
pr_vdebug("EP SS companion descriptor\n");
1754+
if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1755+
goto inv_length;
1756+
break;
1757+
17341758
case USB_DT_OTHER_SPEED_CONFIG:
17351759
case USB_DT_INTERFACE_POWER:
17361760
case USB_DT_DEBUG:
@@ -1841,8 +1865,8 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
18411865
static int __ffs_data_got_descs(struct ffs_data *ffs,
18421866
char *const _data, size_t len)
18431867
{
1844-
unsigned fs_count, hs_count;
1845-
int fs_len, ret = -EINVAL;
1868+
unsigned fs_count, hs_count, ss_count = 0;
1869+
int fs_len, hs_len, ss_len, ret = -EINVAL;
18461870
char *data = _data;
18471871

18481872
ENTER();
@@ -1853,9 +1877,6 @@ static int __ffs_data_got_descs(struct ffs_data *ffs,
18531877
fs_count = get_unaligned_le32(data + 8);
18541878
hs_count = get_unaligned_le32(data + 12);
18551879

1856-
if (!fs_count && !hs_count)
1857-
goto einval;
1858-
18591880
data += 16;
18601881
len -= 16;
18611882

@@ -1874,22 +1895,54 @@ static int __ffs_data_got_descs(struct ffs_data *ffs,
18741895
}
18751896

18761897
if (likely(hs_count)) {
1877-
ret = ffs_do_descs(hs_count, data, len,
1898+
hs_len = ffs_do_descs(hs_count, data, len,
18781899
__ffs_data_do_entity, ffs);
1879-
if (unlikely(ret < 0))
1900+
if (unlikely(hs_len < 0)) {
1901+
ret = hs_len;
1902+
goto error;
1903+
}
1904+
1905+
data += hs_len;
1906+
len -= hs_len;
1907+
} else {
1908+
hs_len = 0;
1909+
}
1910+
1911+
if (len >= 8) {
1912+
/* Check SS_MAGIC for presence of ss_descs and get SS_COUNT */
1913+
if (get_unaligned_le32(data) != FUNCTIONFS_SS_DESC_MAGIC)
1914+
goto einval;
1915+
1916+
ss_count = get_unaligned_le32(data + 4);
1917+
data += 8;
1918+
len -= 8;
1919+
}
1920+
1921+
if (!fs_count && !hs_count && !ss_count)
1922+
goto einval;
1923+
1924+
if (ss_count) {
1925+
ss_len = ffs_do_descs(ss_count, data, len,
1926+
__ffs_data_do_entity, ffs);
1927+
if (unlikely(ss_len < 0)) {
1928+
ret = ss_len;
18801929
goto error;
1930+
}
1931+
ret = ss_len;
18811932
} else {
1933+
ss_len = 0;
18821934
ret = 0;
18831935
}
18841936

18851937
if (unlikely(len != ret))
18861938
goto einval;
18871939

1888-
ffs->raw_fs_descs_length = fs_len;
1889-
ffs->raw_descs_length = fs_len + ret;
1890-
ffs->raw_descs = _data;
1891-
ffs->fs_descs_count = fs_count;
1892-
ffs->hs_descs_count = hs_count;
1940+
ffs->raw_fs_hs_descs_length = fs_len + hs_len;
1941+
ffs->raw_ss_descs_length = ss_len;
1942+
ffs->raw_descs = _data;
1943+
ffs->fs_descs_count = fs_count;
1944+
ffs->hs_descs_count = hs_count;
1945+
ffs->ss_descs_count = ss_count;
18931946

18941947
return 0;
18951948

@@ -2112,35 +2165,42 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
21122165
struct usb_endpoint_descriptor *ds = (void *)desc;
21132166
struct ffs_function *func = priv;
21142167
struct ffs_ep *ffs_ep;
2115-
2116-
/*
2117-
* If hs_descriptors is not NULL then we are reading hs
2118-
* descriptors now
2119-
*/
2120-
const int isHS = func->function.hs_descriptors != NULL;
2121-
unsigned idx;
2168+
unsigned ep_desc_id, idx;
2169+
static const char *speed_names[] = { "full", "high", "super" };
21222170

21232171
if (type != FFS_DESCRIPTOR)
21242172
return 0;
21252173

2126-
if (isHS)
2174+
/*
2175+
* If ss_descriptors is not NULL, we are reading super speed
2176+
* descriptors; if hs_descriptors is not NULL, we are reading high
2177+
* speed descriptors; otherwise, we are reading full speed
2178+
* descriptors.
2179+
*/
2180+
if (func->function.ss_descriptors) {
2181+
ep_desc_id = 2;
2182+
func->function.ss_descriptors[(long)valuep] = desc;
2183+
} else if (func->function.hs_descriptors) {
2184+
ep_desc_id = 1;
21272185
func->function.hs_descriptors[(long)valuep] = desc;
2128-
else
2186+
} else {
2187+
ep_desc_id = 0;
21292188
func->function.fs_descriptors[(long)valuep] = desc;
2189+
}
21302190

21312191
if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
21322192
return 0;
21332193

21342194
idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
21352195
ffs_ep = func->eps + idx;
21362196

2137-
if (unlikely(ffs_ep->descs[isHS])) {
2138-
pr_vdebug("two %sspeed descriptors for EP %d\n",
2139-
isHS ? "high" : "full",
2197+
if (unlikely(ffs_ep->descs[ep_desc_id])) {
2198+
pr_err("two %sspeed descriptors for EP %d\n",
2199+
speed_names[ep_desc_id],
21402200
ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
21412201
return -EINVAL;
21422202
}
2143-
ffs_ep->descs[isHS] = ds;
2203+
ffs_ep->descs[ep_desc_id] = ds;
21442204

21452205
ffs_dump_mem(": Original ep desc", ds, ds->bLength);
21462206
if (ffs_ep->ep) {
@@ -2284,8 +2344,10 @@ static int _ffs_func_bind(struct usb_configuration *c,
22842344
const int full = !!func->ffs->fs_descs_count;
22852345
const int high = gadget_is_dualspeed(func->gadget) &&
22862346
func->ffs->hs_descs_count;
2347+
const int super = gadget_is_superspeed(func->gadget) &&
2348+
func->ffs->ss_descs_count;
22872349

2288-
int ret;
2350+
int fs_len, hs_len, ret;
22892351

22902352
/* Make it a single chunk, less management later on */
22912353
vla_group(d);
@@ -2294,15 +2356,17 @@ static int _ffs_func_bind(struct usb_configuration *c,
22942356
full ? ffs->fs_descs_count + 1 : 0);
22952357
vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
22962358
high ? ffs->hs_descs_count + 1 : 0);
2359+
vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2360+
super ? ffs->ss_descs_count + 1 : 0);
22972361
vla_item_with_sz(d, short, inums, ffs->interfaces_count);
22982362
vla_item_with_sz(d, char, raw_descs,
2299-
high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
2363+
ffs->raw_fs_hs_descs_length + ffs->raw_ss_descs_length);
23002364
char *vlabuf;
23012365

23022366
ENTER();
23032367

2304-
/* Only high speed but not supported by gadget? */
2305-
if (unlikely(!(full | high)))
2368+
/* Has descriptors only for speeds gadget does not support */
2369+
if (unlikely(!(full | high | super)))
23062370
return -ENOTSUPP;
23072371

23082372
/* Allocate a single chunk, less management later on */
@@ -2312,8 +2376,16 @@ static int _ffs_func_bind(struct usb_configuration *c,
23122376

23132377
/* Zero */
23142378
memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2379+
/* Copy only raw (hs,fs) descriptors (until ss_magic and ss_count) */
23152380
memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16,
2316-
d_raw_descs__sz);
2381+
ffs->raw_fs_hs_descs_length);
2382+
/* Copy SS descs present @ header + hs_fs_descs + ss_magic + ss_count */
2383+
if (func->ffs->ss_descs_count)
2384+
memcpy(vla_ptr(vlabuf, d, raw_descs) +
2385+
ffs->raw_fs_hs_descs_length,
2386+
ffs->raw_descs + 16 + ffs->raw_fs_hs_descs_length + 8,
2387+
ffs->raw_ss_descs_length);
2388+
23172389
memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
23182390
for (ret = ffs->eps_count; ret; --ret) {
23192391
struct ffs_ep *ptr;
@@ -2335,22 +2407,38 @@ static int _ffs_func_bind(struct usb_configuration *c,
23352407
*/
23362408
if (likely(full)) {
23372409
func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
2338-
ret = ffs_do_descs(ffs->fs_descs_count,
2339-
vla_ptr(vlabuf, d, raw_descs),
2340-
d_raw_descs__sz,
2341-
__ffs_func_bind_do_descs, func);
2342-
if (unlikely(ret < 0))
2410+
fs_len = ffs_do_descs(ffs->fs_descs_count,
2411+
vla_ptr(vlabuf, d, raw_descs),
2412+
d_raw_descs__sz,
2413+
__ffs_func_bind_do_descs, func);
2414+
if (unlikely(fs_len < 0)) {
2415+
ret = fs_len;
23432416
goto error;
2417+
}
23442418
} else {
2345-
ret = 0;
2419+
fs_len = 0;
23462420
}
23472421

23482422
if (likely(high)) {
23492423
func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
2350-
ret = ffs_do_descs(ffs->hs_descs_count,
2351-
vla_ptr(vlabuf, d, raw_descs) + ret,
2352-
d_raw_descs__sz - ret,
2353-
__ffs_func_bind_do_descs, func);
2424+
hs_len = ffs_do_descs(ffs->hs_descs_count,
2425+
vla_ptr(vlabuf, d, raw_descs) + fs_len,
2426+
d_raw_descs__sz - fs_len,
2427+
__ffs_func_bind_do_descs, func);
2428+
if (unlikely(hs_len < 0)) {
2429+
ret = hs_len;
2430+
goto error;
2431+
}
2432+
} else {
2433+
hs_len = 0;
2434+
}
2435+
2436+
if (likely(super)) {
2437+
func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2438+
ret = ffs_do_descs(ffs->ss_descs_count,
2439+
vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2440+
d_raw_descs__sz - fs_len - hs_len,
2441+
__ffs_func_bind_do_descs, func);
23542442
if (unlikely(ret < 0))
23552443
goto error;
23562444
}
@@ -2361,7 +2449,8 @@ static int _ffs_func_bind(struct usb_configuration *c,
23612449
* now.
23622450
*/
23632451
ret = ffs_do_descs(ffs->fs_descs_count +
2364-
(high ? ffs->hs_descs_count : 0),
2452+
(high ? ffs->hs_descs_count : 0) +
2453+
(super ? ffs->ss_descs_count : 0),
23652454
vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
23662455
__ffs_func_bind_do_nums, func);
23672456
if (unlikely(ret < 0))
@@ -2708,6 +2797,7 @@ static void ffs_func_unbind(struct usb_configuration *c,
27082797
*/
27092798
func->function.fs_descriptors = NULL;
27102799
func->function.hs_descriptors = NULL;
2800+
func->function.ss_descriptors = NULL;
27112801
func->interfaces_nums = NULL;
27122802

27132803
ffs_event_add(ffs, FUNCTIONFS_UNBIND);

‎drivers/usb/gadget/u_fs.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,16 @@ struct ffs_data {
208208
/*
209209
* Real descriptors are 16 bytes after raw_descs (so you need
210210
* to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
211-
* first full speed descriptor). raw_descs_length and
212-
* raw_fs_descs_length do not have those 16 bytes added.
211+
* first full speed descriptor).
212+
* raw_fs_hs_descs_length does not have those 16 bytes added.
213+
* ss_descs are 8 bytes (ss_magic + count) pass the hs_descs
213214
*/
214215
const void *raw_descs;
215-
unsigned raw_descs_length;
216-
unsigned raw_fs_descs_length;
216+
unsigned raw_fs_hs_descs_length;
217+
unsigned raw_ss_descs_length;
217218
unsigned fs_descs_count;
218219
unsigned hs_descs_count;
220+
unsigned ss_descs_count;
219221

220222
unsigned short strings_count;
221223
unsigned short interfaces_count;

‎include/uapi/linux/usb/functionfs.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum {
1313
FUNCTIONFS_STRINGS_MAGIC = 2
1414
};
1515

16+
#define FUNCTIONFS_SS_DESC_MAGIC 0x0055DE5C
1617

1718
#ifndef __KERNEL__
1819

@@ -50,7 +51,11 @@ struct usb_functionfs_descs_head {
5051
* | 12 | hs_count | LE32 | number of high-speed descriptors |
5152
* | 16 | fs_descrs | Descriptor[] | list of full-speed descriptors |
5253
* | | hs_descrs | Descriptor[] | list of high-speed descriptors |
54+
* | | ss_magic | LE32 | FUNCTIONFS_SS_DESC_MAGIC |
55+
* | | ss_count | LE32 | number of super-speed descriptors |
56+
* | | ss_descrs | Descriptor[] | list of super-speed descriptors |
5357
*
58+
* ss_magic: if present then it implies that SS_DESCs are also present
5459
* descs are just valid USB descriptors and have the following format:
5560
*
5661
* | off | name | type | description |

0 commit comments

Comments
 (0)