usb: gadget: f_fs: Add support for SuperSpeed Mode
Subsystems (2)
v6 cover usb: gadget: f_fs: Add support for SuperSpeed Mode
Changes since v5:
* There is no change in 1st patch.
* 2nd patch from Michal updates ABI and adds new field - flags which lists the descriptors that are present. Driver selects between the schemes based on the magic number passed by client.
With the new scheme, usb_functionfs_descs header is removed and clients can selectively pass descriptors and their count based on the 'flags' field.
Manu Gautam (1):
usb: gadget: f_fs: Add support for SuperSpeed Mode
Michal Nazarewicz (1):
usb: gadget: f_fs: Add flags to descriptors block
drivers/usb/gadget/f_fs.c | 214 ++++++++++++++++++++++++------------ drivers/usb/gadget/u_fs.h | 10 +- include/uapi/linux/usb/functionfs.h | 44 +++++--- 3 files changed, 177 insertions(+), 91 deletions(-)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
PATCH v6 1/2 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.
--- drivers/usb/gadget/f_fs.c | 182 +++++++++++++++++++++++++++--------- drivers/usb/gadget/u_fs.h | 10 +- include/uapi/linux/usb/functionfs.h | 5 + 3 files changed, 147 insertions(+), 50 deletions(-)
drivers/usb/gadget/f_fs.c +136 -46
@@ -122,8 +122,8 @@ struct ffs_ep {struct usb_ep *ep;/* P: ffs->eps_lock */ struct usb_request *req;/* P: epfile->mutex */ -/* [0]: full speed, [1]: high speed */ -struct usb_endpoint_descriptor *descs[2 ]; +/* [0]: full speed, [1]: high speed, [2]: super speed */ +struct usb_endpoint_descriptor *descs[3 ];u8 num;
@@ -1185,10 +1185,11 @@ static void ffs_data_reset(struct ffs_data *ffs) ffs->raw_strings =NULL ; ffs->stringtabs =NULL ; - ffs->raw_descs_length =0 ; - ffs->raw_fs_descs_length =0 ; + ffs->raw_fs_hs_descs_length =0 ; + ffs->raw_ss_descs_length =0 ; ffs->fs_descs_count =0 ; ffs->hs_descs_count =0 ; + ffs->ss_descs_count =0 ; ffs->strings_count =0 ; ffs->interfaces_count =0 ;
@@ -1331,7 +1332,24 @@ static int ffs_func_eps_enable(struct ffs_function *func)spin_lock_irqsave (&func->ffs->eps_lock, flags); do { struct usb_endpoint_descriptor *ds; - ds = ep->descs[ep->descs[1] ? 1 : 0]; + int desc_idx; + + if (ffs->gadget->speed == USB_SPEED_SUPER) + desc_idx = 2; + else if (ffs->gadget->speed == USB_SPEED_HIGH) + desc_idx = 1; + else + desc_idx = 0; + + /* fall-back to lower speed if desc missing for current speed */ + do { + ds = ep->descs[desc_idx]; + } while (!ds && --desc_idx >= 0); + + if (!ds) { + ret = -EINVAL; + break; + } ep->ep->driver_data = ep; ep->ep->desc = ds;
@@ -1466,6 +1484,12 @@ static int __must_check ffs_do_desc(char *data, unsigned len, }break ; +case USB_DT_SS_ENDPOINT_COMP : +pr_vdebug ("EP SS companion descriptor\n" ); +if (length !=sizeof (struct usb_ss_ep_comp_descriptor )) + goto inv_length; +break ; +case USB_DT_OTHER_SPEED_CONFIG :case USB_DT_INTERFACE_POWER :case USB_DT_DEBUG :
@@ -1576,8 +1600,8 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,static int __ffs_data_got_descs (struct ffs_data *ffs,char *const _data,size_t len) { -unsigned fs_count, hs_count; -int fs_len, ret = -EINVAL ; +unsigned fs_count, hs_count, ss_count =0 ; +int fs_len, hs_len, ss_len, ret = -EINVAL ;char *data = _data;ENTER ();
@@ -1588,9 +1612,6 @@ static int __ffs_data_got_descs(struct ffs_data *ffs, fs_count =get_unaligned_le32 (data +8 ); hs_count =get_unaligned_le32 (data +12 ); -if (!fs_count && !hs_count) - goto einval; - data +=16 ; len -=16 ;
@@ -1609,22 +1630,54 @@ static int __ffs_data_got_descs(struct ffs_data *ffs, }if (likely (hs_count)) { - ret =ffs_do_descs (hs_count, data, len, + hs_len =ffs_do_descs (hs_count, data, len, __ffs_data_do_entity, ffs); -if (unlikely (ret <0 )) +if (unlikely (hs_len <0 )) { + ret = hs_len; + goto error; + } + + data += hs_len; + len -= hs_len; + }else { + hs_len =0 ; + } + +if (len >=8 ) { +/* Check SS_MAGIC for presence of ss_descs and get SS_COUNT */ +if (get_unaligned_le32 (data) !=FUNCTIONFS_SS_DESC_MAGIC ) + goto einval; + + ss_count =get_unaligned_le32 (data +4 ); + data +=8 ; + len -=8 ; + } + +if (!fs_count && !hs_count && !ss_count) + goto einval; + +if (ss_count) { + ss_len =ffs_do_descs (ss_count, data, len, + __ffs_data_do_entity, ffs); +if (unlikely (ss_len <0 )) { + ret = ss_len; goto error; + } + ret = ss_len; }else { + ss_len =0 ; ret =0 ; }if (unlikely (len != ret)) goto einval; - ffs->raw_fs_descs_length = fs_len; - ffs->raw_descs_length = fs_len + ret; - ffs->raw_descs = _data; - ffs->fs_descs_count = fs_count; - ffs->hs_descs_count = hs_count; + ffs->raw_fs_hs_descs_length = fs_len + hs_len; + ffs->raw_ss_descs_length = ss_len; + ffs->raw_descs = _data; + ffs->fs_descs_count = fs_count; + ffs->hs_descs_count = hs_count; + ffs->ss_descs_count = ss_count;return 0 ;
@@ -1847,21 +1900,28 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,struct usb_endpoint_descriptor *ds = (void *)desc;struct ffs_function *func = priv;struct ffs_ep *ffs_ep; - -/* -* If hs_descriptors is not NULL then we are reading hs -* descriptors now -*/ -const int isHS = func->function.hs_descriptors !=NULL ; -unsigned idx; +unsigned ep_desc_id, idx; +static const char *speed_names[] = {"full" ,"high" ,"super" };if (type !=FFS_DESCRIPTOR )return 0 ; -if (isHS) +/* +* If ss_descriptors is not NULL, we are reading super speed +* descriptors; if hs_descriptors is not NULL, we are reading high +* speed descriptors; otherwise, we are reading full speed +* descriptors. +*/ +if (func->function.ss_descriptors) { + ep_desc_id =2 ; + func->function.ss_descriptors[(long )valuep] = desc; + }else if (func->function.hs_descriptors) { + ep_desc_id =1 ; func->function.hs_descriptors[(long )valuep] = desc; -else + }else { + ep_desc_id =0 ; func->function.fs_descriptors[(long )valuep] = desc; + }if (!desc || desc->bDescriptorType !=USB_DT_ENDPOINT )return 0 ;
@@ -1869,13 +1929,13 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep, idx = (ds->bEndpointAddress &USB_ENDPOINT_NUMBER_MASK ) -1 ; ffs_ep = func->eps + idx; -if (unlikely (ffs_ep->descs[isHS])) { -pr_vdebug ("two %sspeed descriptors for EP %d\n" , - isHS ?"high" :"full" , +if (unlikely (ffs_ep->descs[ep_desc_id])) { +pr_err ("two %sspeed descriptors for EP %d\n" , + speed_names[ep_desc_id], ds->bEndpointAddress &USB_ENDPOINT_NUMBER_MASK );return -EINVAL ; } - ffs_ep->descs[isHS] = ds; + ffs_ep->descs[ep_desc_id] = ds;ffs_dump_mem (": Original ep desc" , ds, ds->bLength);if (ffs_ep->ep) {
@@ -2019,8 +2079,10 @@ static int _ffs_func_bind(struct usb_configuration *c,const int full = !!func->ffs->fs_descs_count;const int high =gadget_is_dualspeed (func->gadget) && func->ffs->hs_descs_count; +const int super =gadget_is_superspeed (func->gadget) && + func->ffs->ss_descs_count; -int ret; +int fs_len, hs_len, ret;/* Make it a single chunk, less management later on */ vla_group (d);
@@ -2029,15 +2091,17 @@ static int _ffs_func_bind(struct usb_configuration *c, full ? ffs->fs_descs_count + 1 : 0); vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs, high ? ffs->hs_descs_count +1 :0 ); + vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs, + super ? ffs->ss_descs_count +1 :0 );vla_item_with_sz (d, short, inums, ffs->interfaces_count);vla_item_with_sz (d, char, raw_descs, - high ? ffs->raw_descs_length : ffs->raw_fs_descs_length); + ffs->raw_fs_hs_descs_length + ffs->raw_ss_descs_length);char *vlabuf;ENTER (); -/* Only high speed but not supported by gadget? */ -if (unlikely (!(full | high))) +/* Has descriptors only for speeds gadget does not support */ +if (unlikely (!(full | high | super)))return -ENOTSUPP ;/* Allocate a single chunk, less management later on */
@@ -2047,8 +2111,16 @@ static int _ffs_func_bind(struct usb_configuration *c,/* Zero */ memset (vla_ptr (vlabuf, d, eps),0 , d_eps__sz); +/* Copy only raw (hs,fs) descriptors (until ss_magic and ss_count) */ memcpy (vla_ptr (vlabuf, d, raw_descs), ffs->raw_descs +16 , - d_raw_descs__sz); + ffs->raw_fs_hs_descs_length); +/* Copy SS descs present @ header + hs_fs_descs + ss_magic + ss_count */ +if (func->ffs->ss_descs_count) +memcpy (vla_ptr (vlabuf, d, raw_descs) + + ffs->raw_fs_hs_descs_length, + ffs->raw_descs +16 + ffs->raw_fs_hs_descs_length +8 , + ffs->raw_ss_descs_length); +memset (vla_ptr (vlabuf, d, inums),0xff , d_inums__sz);for (ret = ffs->eps_count; ret; --ret) {struct ffs_ep *ptr;
@@ -2070,22 +2142,38 @@ static int _ffs_func_bind(struct usb_configuration *c,*/ if (likely (full)) { func->function.fs_descriptors =vla_ptr (vlabuf, d, fs_descs); - ret =ffs_do_descs (ffs->fs_descs_count, -vla_ptr (vlabuf, d, raw_descs), - d_raw_descs__sz, - __ffs_func_bind_do_descs, func); -if (unlikely (ret <0 )) + fs_len =ffs_do_descs (ffs->fs_descs_count, +vla_ptr (vlabuf, d, raw_descs), + d_raw_descs__sz, + __ffs_func_bind_do_descs, func); +if (unlikely (fs_len <0 )) { + ret = fs_len; goto error; + } }else { - ret =0 ; + fs_len =0 ; }if (likely (high)) { func->function.hs_descriptors =vla_ptr (vlabuf, d, hs_descs); - ret =ffs_do_descs (ffs->hs_descs_count, -vla_ptr (vlabuf, d, raw_descs) + ret, - d_raw_descs__sz - ret, - __ffs_func_bind_do_descs, func); + hs_len =ffs_do_descs (ffs->hs_descs_count, +vla_ptr (vlabuf, d, raw_descs) + fs_len, + d_raw_descs__sz - fs_len, + __ffs_func_bind_do_descs, func); +if (unlikely (hs_len <0 )) { + ret = hs_len; + goto error; + } + }else { + hs_len =0 ; + } + +if (likely (super)) { + func->function.ss_descriptors =vla_ptr (vlabuf, d, ss_descs); + ret =ffs_do_descs (ffs->ss_descs_count, +vla_ptr (vlabuf, d, raw_descs) + fs_len + hs_len, + d_raw_descs__sz - fs_len - hs_len, + __ffs_func_bind_do_descs, func);if (unlikely (ret <0 )) goto error; }
@@ -2096,7 +2184,8 @@ static int _ffs_func_bind(struct usb_configuration *c,* now. */ ret =ffs_do_descs (ffs->fs_descs_count + - (high ? ffs->hs_descs_count :0 ), + (high ? ffs->hs_descs_count :0 ) + + (super ? ffs->ss_descs_count :0 ),vla_ptr (vlabuf, d, raw_descs), d_raw_descs__sz, __ffs_func_bind_do_nums, func);if (unlikely (ret <0 ))
@@ -2443,6 +2532,7 @@ static void ffs_func_unbind(struct usb_configuration *c,*/ func->function.fs_descriptors =NULL ; func->function.hs_descriptors =NULL ; + func->function.ss_descriptors =NULL ; func->interfaces_nums =NULL ;ffs_event_add (ffs,FUNCTIONFS_UNBIND );
drivers/usb/gadget/u_fs.h +6 -4
@@ -212,14 +212,16 @@ struct ffs_data {/* * Real descriptors are 16 bytes after raw_descs (so you need * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the -* first full speed descriptor). raw_descs_length and -* raw_fs_descs_length do not have those 16 bytes added. +* first full speed descriptor). +* raw_fs_hs_descs_length does not have those 16 bytes added. +* ss_descs are 8 bytes (ss_magic + count) pass the hs_descs */ const void *raw_descs; -unsigned raw_descs_length; -unsigned raw_fs_descs_length; +unsigned raw_fs_hs_descs_length; +unsigned raw_ss_descs_length;unsigned fs_descs_count;unsigned hs_descs_count; +unsigned ss_descs_count;unsigned short strings_count;unsigned short interfaces_count;
include/uapi/linux/usb/functionfs.h +5 -0
@@ -13,6 +13,7 @@ enum { FUNCTIONFS_STRINGS_MAGIC = 2 }; +#define FUNCTIONFS_SS_DESC_MAGIC 0x0055DE5C#ifndef __KERNEL__
@@ -50,7 +51,11 @@ struct usb_functionfs_descs_head {* | 12 | hs_count | LE32 | number of high-speed descriptors | * | 16 | fs_descrs | Descriptor[] | list of full-speed descriptors | * | | hs_descrs | Descriptor[] | list of high-speed descriptors | +* | | ss_magic | LE32 | FUNCTIONFS_SS_DESC_MAGIC | +* | | ss_count | LE32 | number of super-speed descriptors | +* | | ss_descrs | Descriptor[] | list of super-speed descriptors | * +* ss_magic: if present then it implies that SS_DESCs are also present * descs are just valid USB descriptors and have the following format: * * | off | name | type | description |
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
PATCH v6 2/2 usb: gadget: f_fs: Add flags to descriptors block
In reply to Manu GautamFrom: Michal Nazarewicz <[email protected]>
This reworks the way SuperSpeed descriptors are added and instead of having a magic after full and high speed descriptors, it reworks the whole descriptors block to include a flags field which lists which descriptors are present and makes future extensions possible.
--- drivers/usb/gadget/f_fs.c | 132 +++++++++++++++--------------------- drivers/usb/gadget/u_fs.h | 12 ++-- include/uapi/linux/usb/functionfs.h | 49 +++++++------ 3 files changed, 91 insertions(+), 102 deletions(-)
drivers/usb/gadget/f_fs.c +56 -76
@@ -1169,7 +1169,7 @@ static void ffs_data_clear(struct ffs_data *ffs)if (ffs->epfiles)ffs_epfiles_destroy (ffs->epfiles, ffs->eps_count); -kfree (ffs->raw_descs); +kfree (ffs->raw_descs_data);kfree (ffs->raw_strings);kfree (ffs->stringtabs); }
@@ -1181,12 +1181,12 @@ static void ffs_data_reset(struct ffs_data *ffs)ffs_data_clear (ffs); ffs->epfiles =NULL ; + ffs->raw_descs_data =NULL ; ffs->raw_descs =NULL ; ffs->raw_strings =NULL ; ffs->stringtabs =NULL ; - ffs->raw_fs_hs_descs_length =0 ; - ffs->raw_ss_descs_length =0 ; + ffs->raw_descs_length =0 ; ffs->fs_descs_count =0 ; ffs->hs_descs_count =0 ; ffs->ss_descs_count =0 ;
@@ -1600,89 +1600,76 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,static int __ffs_data_got_descs (struct ffs_data *ffs,char *const _data,size_t len) { -unsigned fs_count, hs_count, ss_count =0 ; -int fs_len, hs_len, ss_len, ret = -EINVAL ; -char *data = _data; +char *data = _data, *raw_descs; +unsigned counts[3 ], flags; +int ret = -EINVAL , i;ENTER (); -if (unlikely (get_unaligned_le32 (data) !=FUNCTIONFS_DESCRIPTORS_MAGIC || -get_unaligned_le32 (data +4 ) != len)) +if (get_unaligned_le32 (data +4 ) != len) goto error; - fs_count =get_unaligned_le32 (data +8 ); - hs_count =get_unaligned_le32 (data +12 ); - - data +=16 ; - len -=16 ; -if (likely (fs_count)) { - fs_len =ffs_do_descs (fs_count, data, len, - __ffs_data_do_entity, ffs); -if (unlikely (fs_len <0 )) { - ret = fs_len; +switch (get_unaligned_le32 (data)) { +case FUNCTIONFS_DESCRIPTORS_MAGIC : + flags =FUNCTIONFS_HAS_FS_DESC |FUNCTIONFS_HAS_HS_DESC ; + data +=8 ; + len -=8 ; +break ; +case FUNCTIONFS_DESCRIPTORS_MAGIC_V2 : + flags =get_unaligned_le32 (data +8 ); +if (flags & ~(FUNCTIONFS_HAS_FS_DESC | +FUNCTIONFS_HAS_HS_DESC | +FUNCTIONFS_HAS_SS_DESC )) { + ret = -ENOSYS ; goto error; } - - data += fs_len; - len -= fs_len; - }else { - fs_len =0 ; + data +=12 ; + len -=12 ; +break ; +default : + goto error; } -if (likely (hs_count)) { - hs_len =ffs_do_descs (hs_count, data, len, - __ffs_data_do_entity, ffs); -if (unlikely (hs_len <0 )) { - ret = hs_len; +/* Read fs_count, hs_count and ss_count (if present) */ +for (i =0 ; i <3 ; ++i) { +if (!(flags & (1 << i))) { + counts[i] =0 ; + }else if (len <4 ) { goto error; + }else { + counts[i] =get_unaligned_le32 (data); + data +=4 ; + len -=4 ; } - - data += hs_len; - len -= hs_len; - }else { - hs_len =0 ; - } - -if (len >=8 ) { -/* Check SS_MAGIC for presence of ss_descs and get SS_COUNT */ -if (get_unaligned_le32 (data) !=FUNCTIONFS_SS_DESC_MAGIC ) - goto einval; - - ss_count =get_unaligned_le32 (data +4 ); - data +=8 ; - len -=8 ; } -if (!fs_count && !hs_count && !ss_count) - goto einval; - -if (ss_count) { - ss_len =ffs_do_descs (ss_count, data, len, +/* Read descriptors */ + raw_descs = data; +for (i =0 ; i <3 ; ++i) { +if (!counts[i]) +continue ; + ret =ffs_do_descs (counts[i], data, len, __ffs_data_do_entity, ffs); -if (unlikely (ss_len <0 )) { - ret = ss_len; +if (ret <0 ) goto error; - } - ret = ss_len; - }else { - ss_len =0 ; - ret =0 ; + data += ret; + len -= ret; } -if (unlikely (len != ret)) - goto einval; +if (raw_descs == data || len) { + ret = -EINVAL ; + goto error; + } - ffs->raw_fs_hs_descs_length = fs_len + hs_len; - ffs->raw_ss_descs_length = ss_len; - ffs->raw_descs = _data; - ffs->fs_descs_count = fs_count; - ffs->hs_descs_count = hs_count; - ffs->ss_descs_count = ss_count; + ffs->raw_descs_data = _data; + ffs->raw_descs = raw_descs; + ffs->raw_descs_length = data - raw_descs; + ffs->fs_descs_count = counts[0 ]; + ffs->hs_descs_count = counts[1 ]; + ffs->ss_descs_count = counts[2 ];return 0 ; -einval: - ret = -EINVAL ; error:kfree (_data);return ret;
@@ -2094,8 +2081,7 @@ static int _ffs_func_bind(struct usb_configuration *c, vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs, super ? ffs->ss_descs_count +1 :0 );vla_item_with_sz (d, short, inums, ffs->interfaces_count); -vla_item_with_sz (d, char, raw_descs, - ffs->raw_fs_hs_descs_length + ffs->raw_ss_descs_length); +vla_item_with_sz (d, char, raw_descs, ffs->raw_descs_length);char *vlabuf;ENTER ();
@@ -2111,15 +2097,9 @@ static int _ffs_func_bind(struct usb_configuration *c,/* Zero */ memset (vla_ptr (vlabuf, d, eps),0 , d_eps__sz); -/* Copy only raw (hs,fs) descriptors (until ss_magic and ss_count) */ -memcpy (vla_ptr (vlabuf, d, raw_descs), ffs->raw_descs +16 , - ffs->raw_fs_hs_descs_length); -/* Copy SS descs present @ header + hs_fs_descs + ss_magic + ss_count */ -if (func->ffs->ss_descs_count) -memcpy (vla_ptr (vlabuf, d, raw_descs) + - ffs->raw_fs_hs_descs_length, - ffs->raw_descs +16 + ffs->raw_fs_hs_descs_length +8 , - ffs->raw_ss_descs_length); +/* Copy descriptors */ +memcpy (vla_ptr (vlabuf, d, raw_descs), ffs->raw_descs, + ffs->raw_descs_length);memset (vla_ptr (vlabuf, d, inums),0xff , d_inums__sz);for (ret = ffs->eps_count; ret; --ret) {
drivers/usb/gadget/u_fs.h +5 -7
@@ -210,15 +210,13 @@ struct ffs_data {/* filled by __ffs_data_got_descs() */ /* -* Real descriptors are 16 bytes after raw_descs (so you need -* to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the -* first full speed descriptor). -* raw_fs_hs_descs_length does not have those 16 bytes added. -* ss_descs are 8 bytes (ss_magic + count) pass the hs_descs +* raw_descs is what you kfree, real_descs points inside of raw_descs, +* where full speed, high speed and super speed descriptors start. +* real_descs_length is the length of all those descriptors. */ +const void *raw_descs_data;const void *raw_descs; -unsigned raw_fs_hs_descs_length; -unsigned raw_ss_descs_length; +unsigned raw_descs_length;unsigned fs_descs_count;unsigned hs_descs_count;unsigned ss_descs_count;
include/uapi/linux/usb/functionfs.h +30 -19
73 lines not on this page · raw message
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
Quoted 7 lines · Manu Gautam
Changes since v5:
* There is no change in 1st patch.
* 2nd patch from Michal updates ABI and adds new field - flags which lists the descriptors that are present. Driver selects between the schemes based on the magic number passed by client.
With the new scheme,usb_functionfs_descsheader is removed and clients can selectively pass descriptors and their count based on the 'flags' field.
Thanks for testing and fixes! Looks good to me.
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michał “mina86” Nazarewicz (o o) ooo +--<[email protected]>--<xmpp:[email protected]>--ooO--(_)--Ooo--