From b0aa30f33b6a4e0b755321e631eb5f870678eae6 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 24 May 2018 17:49:34 +0300 Subject: usb: gadget: uvc: configfs: Don't wrap groups unnecessarily Various configfs groups (represented by config_group) are wrapped in structures that they're the only member of. This allows adding other data fields to groups, but it unnecessarily makes the code more complex. Remove the outer structures and use config_group directly to simplify the code. Groups can still be wrapped individually in the future if other data fields need to be added. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_configfs.c | 302 +++++++++++------------------ 1 file changed, 117 insertions(+), 185 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index b51f0d278826..1df94b25abe1 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -162,9 +162,7 @@ static void uvcg_control_header_drop(struct config_group *group, } /* control/header */ -static struct uvcg_control_header_grp { - struct config_group group; -} uvcg_control_header_grp; +static struct config_group uvcg_control_header_grp; static struct configfs_group_operations uvcg_control_header_grp_ops = { .make_item = uvcg_control_header_make, @@ -177,31 +175,22 @@ static const struct config_item_type uvcg_control_header_grp_type = { }; /* control/processing/default */ -static struct uvcg_default_processing { - struct config_group group; -} uvcg_default_processing; - -static inline struct uvcg_default_processing -*to_uvcg_default_processing(struct config_item *item) -{ - return container_of(to_config_group(item), - struct uvcg_default_processing, group); -} +static struct config_group uvcg_default_processing_grp; #define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_processing_##cname##_show( \ struct config_item *item, char *page) \ { \ - struct uvcg_default_processing *dp = to_uvcg_default_processing(item); \ + struct config_group *group = to_config_group(item); \ struct f_uvc_opts *opts; \ struct config_item *opts_item; \ - struct mutex *su_mutex = &dp->group.cg_subsys->su_mutex; \ + struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ struct uvc_processing_unit_descriptor *pd; \ int result; \ \ mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ \ - opts_item = dp->group.cg_item.ci_parent->ci_parent->ci_parent; \ + opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; \ opts = to_f_uvc_opts(opts_item); \ pd = &opts->uvc_processing; \ \ @@ -229,17 +218,17 @@ UVCG_DEFAULT_PROCESSING_ATTR(i_processing, iProcessing, identity_conv); static ssize_t uvcg_default_processing_bm_controls_show( struct config_item *item, char *page) { - struct uvcg_default_processing *dp = to_uvcg_default_processing(item); + struct config_group *group = to_config_group(item); struct f_uvc_opts *opts; struct config_item *opts_item; - struct mutex *su_mutex = &dp->group.cg_subsys->su_mutex; + struct mutex *su_mutex = &group->cg_subsys->su_mutex; struct uvc_processing_unit_descriptor *pd; int result, i; char *pg = page; mutex_lock(su_mutex); /* for navigating configfs hierarchy */ - opts_item = dp->group.cg_item.ci_parent->ci_parent->ci_parent; + opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; opts = to_f_uvc_opts(opts_item); pd = &opts->uvc_processing; @@ -274,40 +263,29 @@ static const struct config_item_type uvcg_default_processing_type = { /* struct uvcg_processing {}; */ /* control/processing */ -static struct uvcg_processing_grp { - struct config_group group; -} uvcg_processing_grp; +static struct config_group uvcg_processing_grp; static const struct config_item_type uvcg_processing_grp_type = { .ct_owner = THIS_MODULE, }; /* control/terminal/camera/default */ -static struct uvcg_default_camera { - struct config_group group; -} uvcg_default_camera; - -static inline struct uvcg_default_camera -*to_uvcg_default_camera(struct config_item *item) -{ - return container_of(to_config_group(item), - struct uvcg_default_camera, group); -} +static struct config_group uvcg_default_camera_grp; #define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_camera_##cname##_show( \ struct config_item *item, char *page) \ { \ - struct uvcg_default_camera *dc = to_uvcg_default_camera(item); \ + struct config_group *group = to_config_group(item); \ struct f_uvc_opts *opts; \ struct config_item *opts_item; \ - struct mutex *su_mutex = &dc->group.cg_subsys->su_mutex; \ + struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ struct uvc_camera_terminal_descriptor *cd; \ int result; \ \ mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ \ - opts_item = dc->group.cg_item.ci_parent->ci_parent->ci_parent-> \ + opts_item = group->cg_item.ci_parent->ci_parent->ci_parent-> \ ci_parent; \ opts = to_f_uvc_opts(opts_item); \ cd = &opts->uvc_camera_terminal; \ @@ -343,17 +321,17 @@ UVCG_DEFAULT_CAMERA_ATTR(w_ocular_focal_length, wOcularFocalLength, static ssize_t uvcg_default_camera_bm_controls_show( struct config_item *item, char *page) { - struct uvcg_default_camera *dc = to_uvcg_default_camera(item); + struct config_group *group = to_config_group(item); struct f_uvc_opts *opts; struct config_item *opts_item; - struct mutex *su_mutex = &dc->group.cg_subsys->su_mutex; + struct mutex *su_mutex = &group->cg_subsys->su_mutex; struct uvc_camera_terminal_descriptor *cd; int result, i; char *pg = page; mutex_lock(su_mutex); /* for navigating configfs hierarchy */ - opts_item = dc->group.cg_item.ci_parent->ci_parent->ci_parent-> + opts_item = group->cg_item.ci_parent->ci_parent->ci_parent-> ci_parent; opts = to_f_uvc_opts(opts_item); cd = &opts->uvc_camera_terminal; @@ -391,40 +369,29 @@ static const struct config_item_type uvcg_default_camera_type = { /* struct uvcg_camera {}; */ /* control/terminal/camera */ -static struct uvcg_camera_grp { - struct config_group group; -} uvcg_camera_grp; +static struct config_group uvcg_camera_grp; static const struct config_item_type uvcg_camera_grp_type = { .ct_owner = THIS_MODULE, }; /* control/terminal/output/default */ -static struct uvcg_default_output { - struct config_group group; -} uvcg_default_output; - -static inline struct uvcg_default_output -*to_uvcg_default_output(struct config_item *item) -{ - return container_of(to_config_group(item), - struct uvcg_default_output, group); -} +static struct config_group uvcg_default_output_grp; #define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_output_##cname##_show( \ - struct config_item *item, char *page) \ + struct config_item *item, char *page) \ { \ - struct uvcg_default_output *dout = to_uvcg_default_output(item); \ + struct config_group *group = to_config_group(item); \ struct f_uvc_opts *opts; \ struct config_item *opts_item; \ - struct mutex *su_mutex = &dout->group.cg_subsys->su_mutex; \ + struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ struct uvc_output_terminal_descriptor *cd; \ int result; \ \ mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ \ - opts_item = dout->group.cg_item.ci_parent->ci_parent-> \ + opts_item = group->cg_item.ci_parent->ci_parent-> \ ci_parent->ci_parent; \ opts = to_f_uvc_opts(opts_item); \ cd = &opts->uvc_output_terminal; \ @@ -469,39 +436,32 @@ static const struct config_item_type uvcg_default_output_type = { /* struct uvcg_output {}; */ /* control/terminal/output */ -static struct uvcg_output_grp { - struct config_group group; -} uvcg_output_grp; +static struct config_group uvcg_output_grp; static const struct config_item_type uvcg_output_grp_type = { .ct_owner = THIS_MODULE, }; /* control/terminal */ -static struct uvcg_terminal_grp { - struct config_group group; -} uvcg_terminal_grp; +static struct config_group uvcg_terminal_grp; static const struct config_item_type uvcg_terminal_grp_type = { .ct_owner = THIS_MODULE, }; /* control/class/{fs} */ -static struct uvcg_control_class { - struct config_group group; -} uvcg_control_class_fs, uvcg_control_class_ss; - +static struct config_group uvcg_control_class_fs_grp; +static struct config_group uvcg_control_class_ss_grp; static inline struct uvc_descriptor_header **uvcg_get_ctl_class_arr(struct config_item *i, struct f_uvc_opts *o) { - struct uvcg_control_class *cl = container_of(to_config_group(i), - struct uvcg_control_class, group); + struct config_group *group = to_config_group(i); - if (cl == &uvcg_control_class_fs) + if (group == &uvcg_control_class_fs_grp) return o->uvc_fs_control_cls; - if (cl == &uvcg_control_class_ss) + if (group == &uvcg_control_class_ss_grp) return o->uvc_ss_control_cls; return NULL; @@ -593,36 +553,28 @@ static const struct config_item_type uvcg_control_class_type = { }; /* control/class */ -static struct uvcg_control_class_grp { - struct config_group group; -} uvcg_control_class_grp; +static struct config_group uvcg_control_class_grp; static const struct config_item_type uvcg_control_class_grp_type = { .ct_owner = THIS_MODULE, }; /* control */ -static struct uvcg_control_grp { - struct config_group group; -} uvcg_control_grp; +static struct config_group uvcg_control_grp; static const struct config_item_type uvcg_control_grp_type = { .ct_owner = THIS_MODULE, }; /* streaming/uncompressed */ -static struct uvcg_uncompressed_grp { - struct config_group group; -} uvcg_uncompressed_grp; +static struct config_group uvcg_uncompressed_grp; /* streaming/mjpeg */ -static struct uvcg_mjpeg_grp { - struct config_group group; -} uvcg_mjpeg_grp; +static struct config_group uvcg_mjpeg_grp; static struct config_item *fmt_parent[] = { - &uvcg_uncompressed_grp.group.cg_item, - &uvcg_mjpeg_grp.group.cg_item, + &uvcg_uncompressed_grp.cg_item, + &uvcg_mjpeg_grp.cg_item, }; enum uvcg_format_type { @@ -893,9 +845,7 @@ static void uvcg_streaming_header_drop(struct config_group *group, } /* streaming/header */ -static struct uvcg_streaming_header_grp { - struct config_group group; -} uvcg_streaming_header_grp; +static struct config_group uvcg_streaming_header_grp; static struct configfs_group_operations uvcg_streaming_header_grp_ops = { .make_item = uvcg_streaming_header_make, @@ -1670,32 +1620,22 @@ static const struct config_item_type uvcg_mjpeg_grp_type = { }; /* streaming/color_matching/default */ -static struct uvcg_default_color_matching { - struct config_group group; -} uvcg_default_color_matching; - -static inline struct uvcg_default_color_matching -*to_uvcg_default_color_matching(struct config_item *item) -{ - return container_of(to_config_group(item), - struct uvcg_default_color_matching, group); -} +static struct config_group uvcg_default_color_matching_grp; #define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_color_matching_##cname##_show( \ - struct config_item *item, char *page) \ + struct config_item *item, char *page) \ { \ - struct uvcg_default_color_matching *dc = \ - to_uvcg_default_color_matching(item); \ + struct config_group *group = to_config_group(item); \ struct f_uvc_opts *opts; \ struct config_item *opts_item; \ - struct mutex *su_mutex = &dc->group.cg_subsys->su_mutex; \ + struct mutex *su_mutex = &group->cg_subsys->su_mutex; \ struct uvc_color_matching_descriptor *cd; \ int result; \ \ mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ \ - opts_item = dc->group.cg_item.ci_parent->ci_parent->ci_parent; \ + opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; \ opts = to_f_uvc_opts(opts_item); \ cd = &opts->uvc_color_matching; \ \ @@ -1737,33 +1677,29 @@ static const struct config_item_type uvcg_default_color_matching_type = { /* struct uvcg_color_matching {}; */ /* streaming/color_matching */ -static struct uvcg_color_matching_grp { - struct config_group group; -} uvcg_color_matching_grp; +static struct config_group uvcg_color_matching_grp; static const struct config_item_type uvcg_color_matching_grp_type = { .ct_owner = THIS_MODULE, }; /* streaming/class/{fs|hs|ss} */ -static struct uvcg_streaming_class { - struct config_group group; -} uvcg_streaming_class_fs, uvcg_streaming_class_hs, uvcg_streaming_class_ss; - +static struct config_group uvcg_streaming_class_fs_grp; +static struct config_group uvcg_streaming_class_hs_grp; +static struct config_group uvcg_streaming_class_ss_grp; static inline struct uvc_descriptor_header ***__uvcg_get_stream_class_arr(struct config_item *i, struct f_uvc_opts *o) { - struct uvcg_streaming_class *cl = container_of(to_config_group(i), - struct uvcg_streaming_class, group); + struct config_group *group = to_config_group(i); - if (cl == &uvcg_streaming_class_fs) + if (group == &uvcg_streaming_class_fs_grp) return &o->uvc_fs_streaming_cls; - if (cl == &uvcg_streaming_class_hs) + if (group == &uvcg_streaming_class_hs_grp) return &o->uvc_hs_streaming_cls; - if (cl == &uvcg_streaming_class_ss) + if (group == &uvcg_streaming_class_ss_grp) return &o->uvc_ss_streaming_cls; return NULL; @@ -2092,18 +2028,14 @@ static const struct config_item_type uvcg_streaming_class_type = { }; /* streaming/class */ -static struct uvcg_streaming_class_grp { - struct config_group group; -} uvcg_streaming_class_grp; +static struct config_group uvcg_streaming_class_grp; static const struct config_item_type uvcg_streaming_class_grp_type = { .ct_owner = THIS_MODULE, }; /* streaming */ -static struct uvcg_streaming_grp { - struct config_group group; -} uvcg_streaming_grp; +static struct config_group uvcg_streaming_grp; static const struct config_item_type uvcg_streaming_grp_type = { .ct_owner = THIS_MODULE, @@ -2193,114 +2125,114 @@ static const struct config_item_type uvc_func_type = { int uvcg_attach_configfs(struct f_uvc_opts *opts) { - config_group_init_type_name(&uvcg_control_header_grp.group, + config_group_init_type_name(&uvcg_control_header_grp, "header", &uvcg_control_header_grp_type); - config_group_init_type_name(&uvcg_default_processing.group, + config_group_init_type_name(&uvcg_default_processing_grp, "default", &uvcg_default_processing_type); - config_group_init_type_name(&uvcg_processing_grp.group, + config_group_init_type_name(&uvcg_processing_grp, "processing", &uvcg_processing_grp_type); - configfs_add_default_group(&uvcg_default_processing.group, - &uvcg_processing_grp.group); + configfs_add_default_group(&uvcg_default_processing_grp, + &uvcg_processing_grp); - config_group_init_type_name(&uvcg_default_camera.group, + config_group_init_type_name(&uvcg_default_camera_grp, "default", &uvcg_default_camera_type); - config_group_init_type_name(&uvcg_camera_grp.group, + config_group_init_type_name(&uvcg_camera_grp, "camera", &uvcg_camera_grp_type); - configfs_add_default_group(&uvcg_default_camera.group, - &uvcg_camera_grp.group); + configfs_add_default_group(&uvcg_default_camera_grp, + &uvcg_camera_grp); - config_group_init_type_name(&uvcg_default_output.group, + config_group_init_type_name(&uvcg_default_output_grp, "default", &uvcg_default_output_type); - config_group_init_type_name(&uvcg_output_grp.group, + config_group_init_type_name(&uvcg_output_grp, "output", &uvcg_output_grp_type); - configfs_add_default_group(&uvcg_default_output.group, - &uvcg_output_grp.group); + configfs_add_default_group(&uvcg_default_output_grp, + &uvcg_output_grp); - config_group_init_type_name(&uvcg_terminal_grp.group, + config_group_init_type_name(&uvcg_terminal_grp, "terminal", &uvcg_terminal_grp_type); - configfs_add_default_group(&uvcg_camera_grp.group, - &uvcg_terminal_grp.group); - configfs_add_default_group(&uvcg_output_grp.group, - &uvcg_terminal_grp.group); + configfs_add_default_group(&uvcg_camera_grp, + &uvcg_terminal_grp); + configfs_add_default_group(&uvcg_output_grp, + &uvcg_terminal_grp); - config_group_init_type_name(&uvcg_control_class_fs.group, + config_group_init_type_name(&uvcg_control_class_fs_grp, "fs", &uvcg_control_class_type); - config_group_init_type_name(&uvcg_control_class_ss.group, + config_group_init_type_name(&uvcg_control_class_ss_grp, "ss", &uvcg_control_class_type); - config_group_init_type_name(&uvcg_control_class_grp.group, + config_group_init_type_name(&uvcg_control_class_grp, "class", &uvcg_control_class_grp_type); - configfs_add_default_group(&uvcg_control_class_fs.group, - &uvcg_control_class_grp.group); - configfs_add_default_group(&uvcg_control_class_ss.group, - &uvcg_control_class_grp.group); + configfs_add_default_group(&uvcg_control_class_fs_grp, + &uvcg_control_class_grp); + configfs_add_default_group(&uvcg_control_class_ss_grp, + &uvcg_control_class_grp); - config_group_init_type_name(&uvcg_control_grp.group, + config_group_init_type_name(&uvcg_control_grp, "control", &uvcg_control_grp_type); - configfs_add_default_group(&uvcg_control_header_grp.group, - &uvcg_control_grp.group); - configfs_add_default_group(&uvcg_processing_grp.group, - &uvcg_control_grp.group); - configfs_add_default_group(&uvcg_terminal_grp.group, - &uvcg_control_grp.group); - configfs_add_default_group(&uvcg_control_class_grp.group, - &uvcg_control_grp.group); - - config_group_init_type_name(&uvcg_streaming_header_grp.group, + configfs_add_default_group(&uvcg_control_header_grp, + &uvcg_control_grp); + configfs_add_default_group(&uvcg_processing_grp, + &uvcg_control_grp); + configfs_add_default_group(&uvcg_terminal_grp, + &uvcg_control_grp); + configfs_add_default_group(&uvcg_control_class_grp, + &uvcg_control_grp); + + config_group_init_type_name(&uvcg_streaming_header_grp, "header", &uvcg_streaming_header_grp_type); - config_group_init_type_name(&uvcg_uncompressed_grp.group, + config_group_init_type_name(&uvcg_uncompressed_grp, "uncompressed", &uvcg_uncompressed_grp_type); - config_group_init_type_name(&uvcg_mjpeg_grp.group, + config_group_init_type_name(&uvcg_mjpeg_grp, "mjpeg", &uvcg_mjpeg_grp_type); - config_group_init_type_name(&uvcg_default_color_matching.group, + config_group_init_type_name(&uvcg_default_color_matching_grp, "default", &uvcg_default_color_matching_type); - config_group_init_type_name(&uvcg_color_matching_grp.group, + config_group_init_type_name(&uvcg_color_matching_grp, "color_matching", &uvcg_color_matching_grp_type); - configfs_add_default_group(&uvcg_default_color_matching.group, - &uvcg_color_matching_grp.group); + configfs_add_default_group(&uvcg_default_color_matching_grp, + &uvcg_color_matching_grp); - config_group_init_type_name(&uvcg_streaming_class_fs.group, + config_group_init_type_name(&uvcg_streaming_class_fs_grp, "fs", &uvcg_streaming_class_type); - config_group_init_type_name(&uvcg_streaming_class_hs.group, + config_group_init_type_name(&uvcg_streaming_class_hs_grp, "hs", &uvcg_streaming_class_type); - config_group_init_type_name(&uvcg_streaming_class_ss.group, + config_group_init_type_name(&uvcg_streaming_class_ss_grp, "ss", &uvcg_streaming_class_type); - config_group_init_type_name(&uvcg_streaming_class_grp.group, + config_group_init_type_name(&uvcg_streaming_class_grp, "class", &uvcg_streaming_class_grp_type); - configfs_add_default_group(&uvcg_streaming_class_fs.group, - &uvcg_streaming_class_grp.group); - configfs_add_default_group(&uvcg_streaming_class_hs.group, - &uvcg_streaming_class_grp.group); - configfs_add_default_group(&uvcg_streaming_class_ss.group, - &uvcg_streaming_class_grp.group); - - config_group_init_type_name(&uvcg_streaming_grp.group, + configfs_add_default_group(&uvcg_streaming_class_fs_grp, + &uvcg_streaming_class_grp); + configfs_add_default_group(&uvcg_streaming_class_hs_grp, + &uvcg_streaming_class_grp); + configfs_add_default_group(&uvcg_streaming_class_ss_grp, + &uvcg_streaming_class_grp); + + config_group_init_type_name(&uvcg_streaming_grp, "streaming", &uvcg_streaming_grp_type); - configfs_add_default_group(&uvcg_streaming_header_grp.group, - &uvcg_streaming_grp.group); - configfs_add_default_group(&uvcg_uncompressed_grp.group, - &uvcg_streaming_grp.group); - configfs_add_default_group(&uvcg_mjpeg_grp.group, - &uvcg_streaming_grp.group); - configfs_add_default_group(&uvcg_color_matching_grp.group, - &uvcg_streaming_grp.group); - configfs_add_default_group(&uvcg_streaming_class_grp.group, - &uvcg_streaming_grp.group); + configfs_add_default_group(&uvcg_streaming_header_grp, + &uvcg_streaming_grp); + configfs_add_default_group(&uvcg_uncompressed_grp, + &uvcg_streaming_grp); + configfs_add_default_group(&uvcg_mjpeg_grp, + &uvcg_streaming_grp); + configfs_add_default_group(&uvcg_color_matching_grp, + &uvcg_streaming_grp); + configfs_add_default_group(&uvcg_streaming_class_grp, + &uvcg_streaming_grp); config_group_init_type_name(&opts->func_inst.group, "", &uvc_func_type); - configfs_add_default_group(&uvcg_control_grp.group, + configfs_add_default_group(&uvcg_control_grp, &opts->func_inst.group); - configfs_add_default_group(&uvcg_streaming_grp.group, + configfs_add_default_group(&uvcg_streaming_grp, &opts->func_inst.group); return 0; -- cgit v1.2.1 From f7d8109e31bbe785b13c8cfdafc56e9351bccef1 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 24 May 2018 17:49:34 +0300 Subject: usb: gadget: uvc: configfs: Add section header comments The UVC configfs implementation is large and difficult to navigate. Add a bit more air to the code to make it easier to read. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_configfs.c | 120 ++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 29 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 1df94b25abe1..dbc95c9558de 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -12,6 +12,10 @@ #include "u_uvc.h" #include "uvc_configfs.h" +/* ----------------------------------------------------------------------------- + * Global Utility Structures and Macros + */ + #define UVCG_STREAMING_CONTROL_SIZE 1 #define UVC_ATTR(prefix, cname, aname) \ @@ -37,7 +41,11 @@ static inline struct f_uvc_opts *to_f_uvc_opts(struct config_item *item) func_inst.group); } -/* control/header/ */ +/* ----------------------------------------------------------------------------- + * control/header/ + * control/header + */ + DECLARE_UVC_HEADER_DESCRIPTOR(1); struct uvcg_control_header { @@ -161,7 +169,6 @@ static void uvcg_control_header_drop(struct config_group *group, kfree(h); } -/* control/header */ static struct config_group uvcg_control_header_grp; static struct configfs_group_operations uvcg_control_header_grp_ops = { @@ -174,7 +181,10 @@ static const struct config_item_type uvcg_control_header_grp_type = { .ct_owner = THIS_MODULE, }; -/* control/processing/default */ +/* ----------------------------------------------------------------------------- + * control/processing/default + */ + static struct config_group uvcg_default_processing_grp; #define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, conv) \ @@ -260,16 +270,20 @@ static const struct config_item_type uvcg_default_processing_type = { .ct_owner = THIS_MODULE, }; -/* struct uvcg_processing {}; */ +/* ----------------------------------------------------------------------------- + * control/processing + */ -/* control/processing */ static struct config_group uvcg_processing_grp; static const struct config_item_type uvcg_processing_grp_type = { .ct_owner = THIS_MODULE, }; -/* control/terminal/camera/default */ +/* ----------------------------------------------------------------------------- + * control/terminal/camera/default + */ + static struct config_group uvcg_default_camera_grp; #define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, conv) \ @@ -366,16 +380,20 @@ static const struct config_item_type uvcg_default_camera_type = { .ct_owner = THIS_MODULE, }; -/* struct uvcg_camera {}; */ +/* ----------------------------------------------------------------------------- + * control/terminal/camera + */ -/* control/terminal/camera */ static struct config_group uvcg_camera_grp; static const struct config_item_type uvcg_camera_grp_type = { .ct_owner = THIS_MODULE, }; -/* control/terminal/output/default */ +/* ----------------------------------------------------------------------------- + * control/terminal/output/default + */ + static struct config_group uvcg_default_output_grp; #define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, conv) \ @@ -433,23 +451,30 @@ static const struct config_item_type uvcg_default_output_type = { .ct_owner = THIS_MODULE, }; -/* struct uvcg_output {}; */ +/* ----------------------------------------------------------------------------- + * control/terminal/output + */ -/* control/terminal/output */ static struct config_group uvcg_output_grp; static const struct config_item_type uvcg_output_grp_type = { .ct_owner = THIS_MODULE, }; -/* control/terminal */ +/* ----------------------------------------------------------------------------- + * control/terminal + */ + static struct config_group uvcg_terminal_grp; static const struct config_item_type uvcg_terminal_grp_type = { .ct_owner = THIS_MODULE, }; -/* control/class/{fs} */ +/* ----------------------------------------------------------------------------- + * control/class/{fs|ss} + */ + static struct config_group uvcg_control_class_fs_grp; static struct config_group uvcg_control_class_ss_grp; @@ -552,24 +577,32 @@ static const struct config_item_type uvcg_control_class_type = { .ct_owner = THIS_MODULE, }; -/* control/class */ +/* ----------------------------------------------------------------------------- + * control/class + */ + static struct config_group uvcg_control_class_grp; static const struct config_item_type uvcg_control_class_grp_type = { .ct_owner = THIS_MODULE, }; -/* control */ +/* ----------------------------------------------------------------------------- + * control + */ + static struct config_group uvcg_control_grp; static const struct config_item_type uvcg_control_grp_type = { .ct_owner = THIS_MODULE, }; -/* streaming/uncompressed */ -static struct config_group uvcg_uncompressed_grp; +/* ----------------------------------------------------------------------------- + * streaming/uncompressed + * streaming/mjpeg + */ -/* streaming/mjpeg */ +static struct config_group uvcg_uncompressed_grp; static struct config_group uvcg_mjpeg_grp; static struct config_item *fmt_parent[] = { @@ -658,7 +691,11 @@ struct uvcg_format_ptr { struct list_head entry; }; -/* streaming/header/ */ +/* ----------------------------------------------------------------------------- + * streaming/header/ + * streaming/header + */ + struct uvcg_streaming_header { struct config_item item; struct uvc_input_header_descriptor desc; @@ -844,7 +881,6 @@ static void uvcg_streaming_header_drop(struct config_group *group, kfree(h); } -/* streaming/header */ static struct config_group uvcg_streaming_header_grp; static struct configfs_group_operations uvcg_streaming_header_grp_ops = { @@ -857,7 +893,10 @@ static const struct config_item_type uvcg_streaming_header_grp_type = { .ct_owner = THIS_MODULE, }; -/* streaming/// */ +/* ----------------------------------------------------------------------------- + * streaming/// + */ + struct uvcg_frame { struct { u8 b_length; @@ -1168,7 +1207,10 @@ static void uvcg_frame_drop(struct config_group *group, struct config_item *item mutex_unlock(&opts->lock); } -/* streaming/uncompressed/ */ +/* ----------------------------------------------------------------------------- + * streaming/uncompressed/ + */ + struct uvcg_uncompressed { struct uvcg_format fmt; struct uvc_format_uncompressed desc; @@ -1425,7 +1467,10 @@ static const struct config_item_type uvcg_uncompressed_grp_type = { .ct_owner = THIS_MODULE, }; -/* streaming/mjpeg/ */ +/* ----------------------------------------------------------------------------- + * streaming/mjpeg/ + */ + struct uvcg_mjpeg { struct uvcg_format fmt; struct uvc_format_mjpeg desc; @@ -1619,7 +1664,10 @@ static const struct config_item_type uvcg_mjpeg_grp_type = { .ct_owner = THIS_MODULE, }; -/* streaming/color_matching/default */ +/* ----------------------------------------------------------------------------- + * streaming/color_matching/default + */ + static struct config_group uvcg_default_color_matching_grp; #define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, conv) \ @@ -1674,16 +1722,20 @@ static const struct config_item_type uvcg_default_color_matching_type = { .ct_owner = THIS_MODULE, }; -/* struct uvcg_color_matching {}; */ +/* ----------------------------------------------------------------------------- + * streaming/color_matching + */ -/* streaming/color_matching */ static struct config_group uvcg_color_matching_grp; static const struct config_item_type uvcg_color_matching_grp_type = { .ct_owner = THIS_MODULE, }; -/* streaming/class/{fs|hs|ss} */ +/* ----------------------------------------------------------------------------- + * streaming/class/{fs|hs|ss} + */ + static struct config_group uvcg_streaming_class_fs_grp; static struct config_group uvcg_streaming_class_hs_grp; static struct config_group uvcg_streaming_class_ss_grp; @@ -2027,20 +2079,30 @@ static const struct config_item_type uvcg_streaming_class_type = { .ct_owner = THIS_MODULE, }; -/* streaming/class */ +/* ----------------------------------------------------------------------------- + * streaming/class + */ + static struct config_group uvcg_streaming_class_grp; static const struct config_item_type uvcg_streaming_class_grp_type = { .ct_owner = THIS_MODULE, }; -/* streaming */ +/* ----------------------------------------------------------------------------- + * streaming + */ + static struct config_group uvcg_streaming_grp; static const struct config_item_type uvcg_streaming_grp_type = { .ct_owner = THIS_MODULE, }; +/* ----------------------------------------------------------------------------- + * UVC function + */ + static void uvc_attr_release(struct config_item *item) { struct f_uvc_opts *opts = to_f_uvc_opts(item); -- cgit v1.2.1 From 86f3daed59bceb4fa7981d85e89f63ebbae1d561 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 2 Aug 2018 00:14:00 +0300 Subject: usb: gadget: uvc: configfs: Drop leaked references to config items Some of the .allow_link() and .drop_link() operations implementations call config_group_find_item() and then leak the reference to the returned item. Fix this by dropping those references where needed. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_configfs.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index dbc95c9558de..8d513cc6fb8c 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -529,6 +529,7 @@ static int uvcg_control_class_allow_link(struct config_item *src, unlock: mutex_unlock(&opts->lock); out: + config_item_put(header); mutex_unlock(su_mutex); return ret; } @@ -564,6 +565,7 @@ static void uvcg_control_class_drop_link(struct config_item *src, unlock: mutex_unlock(&opts->lock); out: + config_item_put(header); mutex_unlock(su_mutex); } @@ -2026,6 +2028,7 @@ static int uvcg_streaming_class_allow_link(struct config_item *src, unlock: mutex_unlock(&opts->lock); out: + config_item_put(header); mutex_unlock(su_mutex); return ret; } @@ -2066,6 +2069,7 @@ static void uvcg_streaming_class_drop_link(struct config_item *src, unlock: mutex_unlock(&opts->lock); out: + config_item_put(header); mutex_unlock(su_mutex); } -- cgit v1.2.1 From efbf0af70b4f7ee6ed1533ed0d905255c0545e08 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 24 May 2018 17:49:34 +0300 Subject: usb: gadget: uvc: configfs: Allocate groups dynamically The UVC configfs implementation creates all groups as global static variables. This prevents creation of multiple UVC function instances, as they would all require their own configfs group instances. Fix this by allocating all groups dynamically. To avoid duplicating code around, extend the config_item_type structure with group name and children, and implement helper functions to create children automatically for most groups. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/f_uvc.c | 8 +- drivers/usb/gadget/function/uvc_configfs.c | 581 ++++++++++++++++------------- 2 files changed, 338 insertions(+), 251 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c index d8ce7868fe22..95cb1b5f5ffe 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -792,6 +792,7 @@ static struct usb_function_instance *uvc_alloc_inst(void) struct uvc_output_terminal_descriptor *od; struct uvc_color_matching_descriptor *md; struct uvc_descriptor_header **ctl_cls; + int ret; opts = kzalloc(sizeof(*opts), GFP_KERNEL); if (!opts) @@ -868,7 +869,12 @@ static struct usb_function_instance *uvc_alloc_inst(void) opts->streaming_interval = 1; opts->streaming_maxpacket = 1024; - uvcg_attach_configfs(opts); + ret = uvcg_attach_configfs(opts); + if (ret < 0) { + kfree(opts); + return ERR_PTR(ret); + } + return &opts->func_inst; } diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 8d513cc6fb8c..ae722549eabc 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -41,6 +41,71 @@ static inline struct f_uvc_opts *to_f_uvc_opts(struct config_item *item) func_inst.group); } +struct uvcg_config_group_type { + struct config_item_type type; + const char *name; + const struct uvcg_config_group_type **children; + int (*create_children)(struct config_group *group); +}; + +static void uvcg_config_item_release(struct config_item *item) +{ + struct config_group *group = to_config_group(item); + + kfree(group); +} + +static struct configfs_item_operations uvcg_config_item_ops = { + .release = uvcg_config_item_release, +}; + +static int uvcg_config_create_group(struct config_group *parent, + const struct uvcg_config_group_type *type); + +static int uvcg_config_create_children(struct config_group *group, + const struct uvcg_config_group_type *type) +{ + const struct uvcg_config_group_type **child; + int ret; + + if (type->create_children) + return type->create_children(group); + + for (child = type->children; child && *child; ++child) { + ret = uvcg_config_create_group(group, *child); + if (ret < 0) + return ret; + } + + return 0; +} + +static int uvcg_config_create_group(struct config_group *parent, + const struct uvcg_config_group_type *type) +{ + struct config_group *group; + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) + return -ENOMEM; + + config_group_init_type_name(group, type->name, &type->type); + configfs_add_default_group(group, parent); + + return uvcg_config_create_children(group, type); +} + +static void uvcg_config_remove_children(struct config_group *group) +{ + struct config_group *child, *n; + + list_for_each_entry_safe(child, n, &group->default_groups, group_entry) { + list_del(&child->group_entry); + uvcg_config_remove_children(child); + config_item_put(&child->cg_item); + } +} + /* ----------------------------------------------------------------------------- * control/header/ * control/header @@ -137,6 +202,7 @@ static struct configfs_attribute *uvcg_control_header_attrs[] = { }; static const struct config_item_type uvcg_control_header_type = { + .ct_item_ops = &uvcg_config_item_ops, .ct_attrs = uvcg_control_header_attrs, .ct_owner = THIS_MODULE, }; @@ -161,32 +227,23 @@ static struct config_item *uvcg_control_header_make(struct config_group *group, return &h->item; } -static void uvcg_control_header_drop(struct config_group *group, - struct config_item *item) -{ - struct uvcg_control_header *h = to_uvcg_control_header(item); - - kfree(h); -} - -static struct config_group uvcg_control_header_grp; - static struct configfs_group_operations uvcg_control_header_grp_ops = { .make_item = uvcg_control_header_make, - .drop_item = uvcg_control_header_drop, }; -static const struct config_item_type uvcg_control_header_grp_type = { - .ct_group_ops = &uvcg_control_header_grp_ops, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_control_header_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_group_ops = &uvcg_control_header_grp_ops, + .ct_owner = THIS_MODULE, + }, + .name = "header", }; /* ----------------------------------------------------------------------------- * control/processing/default */ -static struct config_group uvcg_default_processing_grp; - #define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_processing_##cname##_show( \ struct config_item *item, char *page) \ @@ -265,27 +322,35 @@ static struct configfs_attribute *uvcg_default_processing_attrs[] = { NULL, }; -static const struct config_item_type uvcg_default_processing_type = { - .ct_attrs = uvcg_default_processing_attrs, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_default_processing_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_attrs = uvcg_default_processing_attrs, + .ct_owner = THIS_MODULE, + }, + .name = "default", }; /* ----------------------------------------------------------------------------- * control/processing */ -static struct config_group uvcg_processing_grp; - -static const struct config_item_type uvcg_processing_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_processing_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "processing", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_default_processing_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- * control/terminal/camera/default */ -static struct config_group uvcg_default_camera_grp; - #define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_camera_##cname##_show( \ struct config_item *item, char *page) \ @@ -375,27 +440,35 @@ static struct configfs_attribute *uvcg_default_camera_attrs[] = { NULL, }; -static const struct config_item_type uvcg_default_camera_type = { - .ct_attrs = uvcg_default_camera_attrs, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_default_camera_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_attrs = uvcg_default_camera_attrs, + .ct_owner = THIS_MODULE, + }, + .name = "default", }; /* ----------------------------------------------------------------------------- * control/terminal/camera */ -static struct config_group uvcg_camera_grp; - -static const struct config_item_type uvcg_camera_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_camera_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "camera", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_default_camera_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- * control/terminal/output/default */ -static struct config_group uvcg_default_output_grp; - #define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_output_##cname##_show( \ struct config_item *item, char *page) \ @@ -446,47 +519,68 @@ static struct configfs_attribute *uvcg_default_output_attrs[] = { NULL, }; -static const struct config_item_type uvcg_default_output_type = { - .ct_attrs = uvcg_default_output_attrs, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_default_output_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_attrs = uvcg_default_output_attrs, + .ct_owner = THIS_MODULE, + }, + .name = "default", }; /* ----------------------------------------------------------------------------- * control/terminal/output */ -static struct config_group uvcg_output_grp; - -static const struct config_item_type uvcg_output_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_output_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "output", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_default_output_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- * control/terminal */ -static struct config_group uvcg_terminal_grp; - -static const struct config_item_type uvcg_terminal_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_terminal_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "terminal", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_camera_grp_type, + &uvcg_output_grp_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- * control/class/{fs|ss} */ -static struct config_group uvcg_control_class_fs_grp; -static struct config_group uvcg_control_class_ss_grp; +struct uvcg_control_class_group { + struct config_group group; + const char *name; +}; static inline struct uvc_descriptor_header **uvcg_get_ctl_class_arr(struct config_item *i, struct f_uvc_opts *o) { - struct config_group *group = to_config_group(i); + struct uvcg_control_class_group *group = + container_of(i, struct uvcg_control_class_group, + group.cg_item); - if (group == &uvcg_control_class_fs_grp) + if (!strcmp(group->name, "fs")) return o->uvc_fs_control_cls; - if (group == &uvcg_control_class_ss_grp) + if (!strcmp(group->name, "ss")) return o->uvc_ss_control_cls; return NULL; @@ -570,6 +664,7 @@ out: } static struct configfs_item_operations uvcg_control_class_item_ops = { + .release = uvcg_config_item_release, .allow_link = uvcg_control_class_allow_link, .drop_link = uvcg_control_class_drop_link, }; @@ -583,20 +678,54 @@ static const struct config_item_type uvcg_control_class_type = { * control/class */ -static struct config_group uvcg_control_class_grp; +static int uvcg_control_class_create_children(struct config_group *parent) +{ + static const char * const names[] = { "fs", "ss" }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(names); ++i) { + struct uvcg_control_class_group *group; -static const struct config_item_type uvcg_control_class_grp_type = { - .ct_owner = THIS_MODULE, + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) + return -ENOMEM; + + group->name = names[i]; + + config_group_init_type_name(&group->group, group->name, + &uvcg_control_class_type); + configfs_add_default_group(&group->group, parent); + } + + return 0; +} + +static const struct uvcg_config_group_type uvcg_control_class_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "class", + .create_children = uvcg_control_class_create_children, }; /* ----------------------------------------------------------------------------- * control */ -static struct config_group uvcg_control_grp; - -static const struct config_item_type uvcg_control_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_control_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "control", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_control_header_grp_type, + &uvcg_processing_grp_type, + &uvcg_terminal_grp_type, + &uvcg_control_class_grp_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- @@ -604,12 +733,9 @@ static const struct config_item_type uvcg_control_grp_type = { * streaming/mjpeg */ -static struct config_group uvcg_uncompressed_grp; -static struct config_group uvcg_mjpeg_grp; - -static struct config_item *fmt_parent[] = { - &uvcg_uncompressed_grp.cg_item, - &uvcg_mjpeg_grp.cg_item, +static const char * const uvcg_format_names[] = { + "uncompressed", + "mjpeg", }; enum uvcg_format_type { @@ -735,10 +861,22 @@ static int uvcg_streaming_header_allow_link(struct config_item *src, goto out; } - for (i = 0; i < ARRAY_SIZE(fmt_parent); ++i) - if (target->ci_parent == fmt_parent[i]) + /* + * Linking is only allowed to direct children of the format nodes + * (streaming/uncompressed or streaming/mjpeg nodes). First check that + * the grand-parent of the target matches the grand-parent of the source + * (the streaming node), and then verify that the target parent is a + * format node. + */ + if (src->ci_parent->ci_parent != target->ci_parent->ci_parent) + goto out; + + for (i = 0; i < ARRAY_SIZE(uvcg_format_names); ++i) { + if (!strcmp(target->ci_parent->ci_name, uvcg_format_names[i])) break; - if (i == ARRAY_SIZE(fmt_parent)) + } + + if (i == ARRAY_SIZE(uvcg_format_names)) goto out; target_fmt = container_of(to_config_group(target), struct uvcg_format, @@ -798,8 +936,9 @@ out: } static struct configfs_item_operations uvcg_streaming_header_item_ops = { - .allow_link = uvcg_streaming_header_allow_link, - .drop_link = uvcg_streaming_header_drop_link, + .release = uvcg_config_item_release, + .allow_link = uvcg_streaming_header_allow_link, + .drop_link = uvcg_streaming_header_drop_link, }; #define UVCG_STREAMING_HEADER_ATTR(cname, aname, conv) \ @@ -875,24 +1014,17 @@ static struct config_item return &h->item; } -static void uvcg_streaming_header_drop(struct config_group *group, - struct config_item *item) -{ - struct uvcg_streaming_header *h = to_uvcg_streaming_header(item); - - kfree(h); -} - -static struct config_group uvcg_streaming_header_grp; - static struct configfs_group_operations uvcg_streaming_header_grp_ops = { .make_item = uvcg_streaming_header_make, - .drop_item = uvcg_streaming_header_drop, }; -static const struct config_item_type uvcg_streaming_header_grp_type = { - .ct_group_ops = &uvcg_streaming_header_grp_ops, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_streaming_header_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_group_ops = &uvcg_streaming_header_grp_ops, + .ct_owner = THIS_MODULE, + }, + .name = "header", }; /* ----------------------------------------------------------------------------- @@ -900,6 +1032,8 @@ static const struct config_item_type uvcg_streaming_header_grp_type = { */ struct uvcg_frame { + struct config_item item; + enum uvcg_format_type fmt_type; struct { u8 b_length; u8 b_descriptor_type; @@ -915,8 +1049,6 @@ struct uvcg_frame { u8 b_frame_interval_type; } __attribute__((packed)) frame; u32 *dw_frame_interval; - enum uvcg_format_type fmt_type; - struct config_item item; }; static struct uvcg_frame *to_uvcg_frame(struct config_item *item) @@ -1143,6 +1275,7 @@ static struct configfs_attribute *uvcg_frame_attrs[] = { }; static const struct config_item_type uvcg_frame_type = { + .ct_item_ops = &uvcg_config_item_ops, .ct_attrs = uvcg_frame_attrs, .ct_owner = THIS_MODULE, }; @@ -1194,7 +1327,6 @@ static struct config_item *uvcg_frame_make(struct config_group *group, static void uvcg_frame_drop(struct config_group *group, struct config_item *item) { - struct uvcg_frame *h = to_uvcg_frame(item); struct uvcg_format *fmt; struct f_uvc_opts *opts; struct config_item *opts_item; @@ -1205,8 +1337,9 @@ static void uvcg_frame_drop(struct config_group *group, struct config_item *item mutex_lock(&opts->lock); fmt = to_uvcg_format(&group->cg_item); --fmt->num_frames; - kfree(h); mutex_unlock(&opts->lock); + + config_item_put(item); } /* ----------------------------------------------------------------------------- @@ -1415,6 +1548,7 @@ static struct configfs_attribute *uvcg_uncompressed_attrs[] = { }; static const struct config_item_type uvcg_uncompressed_type = { + .ct_item_ops = &uvcg_config_item_ops, .ct_group_ops = &uvcg_uncompressed_group_ops, .ct_attrs = uvcg_uncompressed_attrs, .ct_owner = THIS_MODULE, @@ -1451,22 +1585,17 @@ static struct config_group *uvcg_uncompressed_make(struct config_group *group, return &h->fmt.group; } -static void uvcg_uncompressed_drop(struct config_group *group, - struct config_item *item) -{ - struct uvcg_uncompressed *h = to_uvcg_uncompressed(item); - - kfree(h); -} - static struct configfs_group_operations uvcg_uncompressed_grp_ops = { .make_group = uvcg_uncompressed_make, - .drop_item = uvcg_uncompressed_drop, }; -static const struct config_item_type uvcg_uncompressed_grp_type = { - .ct_group_ops = &uvcg_uncompressed_grp_ops, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_uncompressed_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_group_ops = &uvcg_uncompressed_grp_ops, + .ct_owner = THIS_MODULE, + }, + .name = "uncompressed", }; /* ----------------------------------------------------------------------------- @@ -1618,6 +1747,7 @@ static struct configfs_attribute *uvcg_mjpeg_attrs[] = { }; static const struct config_item_type uvcg_mjpeg_type = { + .ct_item_ops = &uvcg_config_item_ops, .ct_group_ops = &uvcg_mjpeg_group_ops, .ct_attrs = uvcg_mjpeg_attrs, .ct_owner = THIS_MODULE, @@ -1648,30 +1778,23 @@ static struct config_group *uvcg_mjpeg_make(struct config_group *group, return &h->fmt.group; } -static void uvcg_mjpeg_drop(struct config_group *group, - struct config_item *item) -{ - struct uvcg_mjpeg *h = to_uvcg_mjpeg(item); - - kfree(h); -} - static struct configfs_group_operations uvcg_mjpeg_grp_ops = { .make_group = uvcg_mjpeg_make, - .drop_item = uvcg_mjpeg_drop, }; -static const struct config_item_type uvcg_mjpeg_grp_type = { - .ct_group_ops = &uvcg_mjpeg_grp_ops, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_mjpeg_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_group_ops = &uvcg_mjpeg_grp_ops, + .ct_owner = THIS_MODULE, + }, + .name = "mjpeg", }; /* ----------------------------------------------------------------------------- * streaming/color_matching/default */ -static struct config_group uvcg_default_color_matching_grp; - #define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, conv) \ static ssize_t uvcg_default_color_matching_##cname##_show( \ struct config_item *item, char *page) \ @@ -1719,41 +1842,54 @@ static struct configfs_attribute *uvcg_default_color_matching_attrs[] = { NULL, }; -static const struct config_item_type uvcg_default_color_matching_type = { - .ct_attrs = uvcg_default_color_matching_attrs, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_default_color_matching_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_attrs = uvcg_default_color_matching_attrs, + .ct_owner = THIS_MODULE, + }, + .name = "default", }; /* ----------------------------------------------------------------------------- * streaming/color_matching */ -static struct config_group uvcg_color_matching_grp; - -static const struct config_item_type uvcg_color_matching_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_color_matching_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "color_matching", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_default_color_matching_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- * streaming/class/{fs|hs|ss} */ -static struct config_group uvcg_streaming_class_fs_grp; -static struct config_group uvcg_streaming_class_hs_grp; -static struct config_group uvcg_streaming_class_ss_grp; +struct uvcg_streaming_class_group { + struct config_group group; + const char *name; +}; static inline struct uvc_descriptor_header ***__uvcg_get_stream_class_arr(struct config_item *i, struct f_uvc_opts *o) { - struct config_group *group = to_config_group(i); + struct uvcg_streaming_class_group *group = + container_of(i, struct uvcg_streaming_class_group, + group.cg_item); - if (group == &uvcg_streaming_class_fs_grp) + if (!strcmp(group->name, "fs")) return &o->uvc_fs_streaming_cls; - if (group == &uvcg_streaming_class_hs_grp) + if (!strcmp(group->name, "hs")) return &o->uvc_hs_streaming_cls; - if (group == &uvcg_streaming_class_ss_grp) + if (!strcmp(group->name, "ss")) return &o->uvc_ss_streaming_cls; return NULL; @@ -2074,6 +2210,7 @@ out: } static struct configfs_item_operations uvcg_streaming_class_item_ops = { + .release = uvcg_config_item_release, .allow_link = uvcg_streaming_class_allow_link, .drop_link = uvcg_streaming_class_drop_link, }; @@ -2087,35 +2224,71 @@ static const struct config_item_type uvcg_streaming_class_type = { * streaming/class */ -static struct config_group uvcg_streaming_class_grp; +static int uvcg_streaming_class_create_children(struct config_group *parent) +{ + static const char * const names[] = { "fs", "hs", "ss" }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(names); ++i) { + struct uvcg_streaming_class_group *group; + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) + return -ENOMEM; + + group->name = names[i]; -static const struct config_item_type uvcg_streaming_class_grp_type = { - .ct_owner = THIS_MODULE, + config_group_init_type_name(&group->group, group->name, + &uvcg_streaming_class_type); + configfs_add_default_group(&group->group, parent); + } + + return 0; +} + +static const struct uvcg_config_group_type uvcg_streaming_class_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "class", + .create_children = uvcg_streaming_class_create_children, }; /* ----------------------------------------------------------------------------- * streaming */ -static struct config_group uvcg_streaming_grp; - -static const struct config_item_type uvcg_streaming_grp_type = { - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvcg_streaming_grp_type = { + .type = { + .ct_item_ops = &uvcg_config_item_ops, + .ct_owner = THIS_MODULE, + }, + .name = "streaming", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_streaming_header_grp_type, + &uvcg_uncompressed_grp_type, + &uvcg_mjpeg_grp_type, + &uvcg_color_matching_grp_type, + &uvcg_streaming_class_grp_type, + NULL, + }, }; /* ----------------------------------------------------------------------------- * UVC function */ -static void uvc_attr_release(struct config_item *item) +static void uvc_func_item_release(struct config_item *item) { struct f_uvc_opts *opts = to_f_uvc_opts(item); + uvcg_config_remove_children(to_config_group(item)); usb_put_function_instance(&opts->func_inst); } -static struct configfs_item_operations uvc_item_ops = { - .release = uvc_attr_release, +static struct configfs_item_operations uvc_func_item_ops = { + .release = uvc_func_item_release, }; #define UVCG_OPTS_ATTR(cname, aname, conv, str2u, uxx, vnoc, limit) \ @@ -2183,123 +2356,31 @@ static struct configfs_attribute *uvc_attrs[] = { NULL, }; -static const struct config_item_type uvc_func_type = { - .ct_item_ops = &uvc_item_ops, - .ct_attrs = uvc_attrs, - .ct_owner = THIS_MODULE, +static const struct uvcg_config_group_type uvc_func_type = { + .type = { + .ct_item_ops = &uvc_func_item_ops, + .ct_attrs = uvc_attrs, + .ct_owner = THIS_MODULE, + }, + .name = "", + .children = (const struct uvcg_config_group_type*[]) { + &uvcg_control_grp_type, + &uvcg_streaming_grp_type, + NULL, + }, }; int uvcg_attach_configfs(struct f_uvc_opts *opts) { - config_group_init_type_name(&uvcg_control_header_grp, - "header", - &uvcg_control_header_grp_type); - - config_group_init_type_name(&uvcg_default_processing_grp, - "default", &uvcg_default_processing_type); - config_group_init_type_name(&uvcg_processing_grp, - "processing", &uvcg_processing_grp_type); - configfs_add_default_group(&uvcg_default_processing_grp, - &uvcg_processing_grp); - - config_group_init_type_name(&uvcg_default_camera_grp, - "default", &uvcg_default_camera_type); - config_group_init_type_name(&uvcg_camera_grp, - "camera", &uvcg_camera_grp_type); - configfs_add_default_group(&uvcg_default_camera_grp, - &uvcg_camera_grp); - - config_group_init_type_name(&uvcg_default_output_grp, - "default", &uvcg_default_output_type); - config_group_init_type_name(&uvcg_output_grp, - "output", &uvcg_output_grp_type); - configfs_add_default_group(&uvcg_default_output_grp, - &uvcg_output_grp); - - config_group_init_type_name(&uvcg_terminal_grp, - "terminal", &uvcg_terminal_grp_type); - configfs_add_default_group(&uvcg_camera_grp, - &uvcg_terminal_grp); - configfs_add_default_group(&uvcg_output_grp, - &uvcg_terminal_grp); - - config_group_init_type_name(&uvcg_control_class_fs_grp, - "fs", &uvcg_control_class_type); - config_group_init_type_name(&uvcg_control_class_ss_grp, - "ss", &uvcg_control_class_type); - config_group_init_type_name(&uvcg_control_class_grp, - "class", - &uvcg_control_class_grp_type); - configfs_add_default_group(&uvcg_control_class_fs_grp, - &uvcg_control_class_grp); - configfs_add_default_group(&uvcg_control_class_ss_grp, - &uvcg_control_class_grp); - - config_group_init_type_name(&uvcg_control_grp, - "control", - &uvcg_control_grp_type); - configfs_add_default_group(&uvcg_control_header_grp, - &uvcg_control_grp); - configfs_add_default_group(&uvcg_processing_grp, - &uvcg_control_grp); - configfs_add_default_group(&uvcg_terminal_grp, - &uvcg_control_grp); - configfs_add_default_group(&uvcg_control_class_grp, - &uvcg_control_grp); - - config_group_init_type_name(&uvcg_streaming_header_grp, - "header", - &uvcg_streaming_header_grp_type); - config_group_init_type_name(&uvcg_uncompressed_grp, - "uncompressed", - &uvcg_uncompressed_grp_type); - config_group_init_type_name(&uvcg_mjpeg_grp, - "mjpeg", - &uvcg_mjpeg_grp_type); - config_group_init_type_name(&uvcg_default_color_matching_grp, - "default", - &uvcg_default_color_matching_type); - config_group_init_type_name(&uvcg_color_matching_grp, - "color_matching", - &uvcg_color_matching_grp_type); - configfs_add_default_group(&uvcg_default_color_matching_grp, - &uvcg_color_matching_grp); - - config_group_init_type_name(&uvcg_streaming_class_fs_grp, - "fs", &uvcg_streaming_class_type); - config_group_init_type_name(&uvcg_streaming_class_hs_grp, - "hs", &uvcg_streaming_class_type); - config_group_init_type_name(&uvcg_streaming_class_ss_grp, - "ss", &uvcg_streaming_class_type); - config_group_init_type_name(&uvcg_streaming_class_grp, - "class", &uvcg_streaming_class_grp_type); - configfs_add_default_group(&uvcg_streaming_class_fs_grp, - &uvcg_streaming_class_grp); - configfs_add_default_group(&uvcg_streaming_class_hs_grp, - &uvcg_streaming_class_grp); - configfs_add_default_group(&uvcg_streaming_class_ss_grp, - &uvcg_streaming_class_grp); - - config_group_init_type_name(&uvcg_streaming_grp, - "streaming", &uvcg_streaming_grp_type); - configfs_add_default_group(&uvcg_streaming_header_grp, - &uvcg_streaming_grp); - configfs_add_default_group(&uvcg_uncompressed_grp, - &uvcg_streaming_grp); - configfs_add_default_group(&uvcg_mjpeg_grp, - &uvcg_streaming_grp); - configfs_add_default_group(&uvcg_color_matching_grp, - &uvcg_streaming_grp); - configfs_add_default_group(&uvcg_streaming_class_grp, - &uvcg_streaming_grp); - - config_group_init_type_name(&opts->func_inst.group, - "", - &uvc_func_type); - configfs_add_default_group(&uvcg_control_grp, - &opts->func_inst.group); - configfs_add_default_group(&uvcg_streaming_grp, - &opts->func_inst.group); + int ret; - return 0; + config_group_init_type_name(&opts->func_inst.group, uvc_func_type.name, + &uvc_func_type.type); + + ret = uvcg_config_create_children(&opts->func_inst.group, + &uvc_func_type); + if (ret < 0) + config_group_put(&opts->func_inst.group); + + return ret; } -- cgit v1.2.1 From bf71544883a1ccb20021eb5139475496dbd8abd9 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 23 May 2018 18:47:56 +0300 Subject: usb: gadget: uvc: configfs: Add interface number attributes The video control and video streaming interface numbers are needed in the UVC gadget userspace stack to reply to UVC requests. They are hardcoded to fixed values at the moment, preventing configurations with multiple functions. To fix this, make them dynamically discoverable by userspace through read-only configfs attributes in /control/bInterfaceNumber and /streaming/bInterfaceNumber respectively. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/f_uvc.c | 2 + drivers/usb/gadget/function/u_uvc.h | 3 ++ drivers/usb/gadget/function/uvc_configfs.c | 62 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c index 95cb1b5f5ffe..4ea987741e6e 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -699,12 +699,14 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f) uvc_iad.bFirstInterface = ret; uvc_control_intf.bInterfaceNumber = ret; uvc->control_intf = ret; + opts->control_interface = ret; if ((ret = usb_interface_id(c, f)) < 0) goto error; uvc_streaming_intf_alt0.bInterfaceNumber = ret; uvc_streaming_intf_alt1.bInterfaceNumber = ret; uvc->streaming_intf = ret; + opts->streaming_interface = ret; /* Copy descriptors */ f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); diff --git a/drivers/usb/gadget/function/u_uvc.h b/drivers/usb/gadget/function/u_uvc.h index 2ed292e94fbc..5242d489e20a 100644 --- a/drivers/usb/gadget/function/u_uvc.h +++ b/drivers/usb/gadget/function/u_uvc.h @@ -25,6 +25,9 @@ struct f_uvc_opts { unsigned int streaming_maxpacket; unsigned int streaming_maxburst; + unsigned int control_interface; + unsigned int streaming_interface; + /* * Control descriptors array pointers for full-/high-speed and * super-speed. They point by default to the uvc_fs_control_cls and diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index ae722549eabc..fa8d2e1f54ba 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -713,9 +713,40 @@ static const struct uvcg_config_group_type uvcg_control_class_grp_type = { * control */ +static ssize_t uvcg_default_control_b_interface_number_show( + struct config_item *item, char *page) +{ + struct config_group *group = to_config_group(item); + struct mutex *su_mutex = &group->cg_subsys->su_mutex; + struct config_item *opts_item; + struct f_uvc_opts *opts; + int result = 0; + + mutex_lock(su_mutex); /* for navigating configfs hierarchy */ + + opts_item = item->ci_parent; + opts = to_f_uvc_opts(opts_item); + + mutex_lock(&opts->lock); + result += sprintf(page, "%u\n", opts->control_interface); + mutex_unlock(&opts->lock); + + mutex_unlock(su_mutex); + + return result; +} + +UVC_ATTR_RO(uvcg_default_control_, b_interface_number, bInterfaceNumber); + +static struct configfs_attribute *uvcg_default_control_attrs[] = { + &uvcg_default_control_attr_b_interface_number, + NULL, +}; + static const struct uvcg_config_group_type uvcg_control_grp_type = { .type = { .ct_item_ops = &uvcg_config_item_ops, + .ct_attrs = uvcg_default_control_attrs, .ct_owner = THIS_MODULE, }, .name = "control", @@ -2259,9 +2290,40 @@ static const struct uvcg_config_group_type uvcg_streaming_class_grp_type = { * streaming */ +static ssize_t uvcg_default_streaming_b_interface_number_show( + struct config_item *item, char *page) +{ + struct config_group *group = to_config_group(item); + struct mutex *su_mutex = &group->cg_subsys->su_mutex; + struct config_item *opts_item; + struct f_uvc_opts *opts; + int result = 0; + + mutex_lock(su_mutex); /* for navigating configfs hierarchy */ + + opts_item = item->ci_parent; + opts = to_f_uvc_opts(opts_item); + + mutex_lock(&opts->lock); + result += sprintf(page, "%u\n", opts->streaming_interface); + mutex_unlock(&opts->lock); + + mutex_unlock(su_mutex); + + return result; +} + +UVC_ATTR_RO(uvcg_default_streaming_, b_interface_number, bInterfaceNumber); + +static struct configfs_attribute *uvcg_default_streaming_attrs[] = { + &uvcg_default_streaming_attr_b_interface_number, + NULL, +}; + static const struct uvcg_config_group_type uvcg_streaming_grp_type = { .type = { .ct_item_ops = &uvcg_config_item_ops, + .ct_attrs = uvcg_default_streaming_attrs, .ct_owner = THIS_MODULE, }, .name = "streaming", -- cgit v1.2.1 From 61ff10e0ea0cb39c737eab7e4fc5f0ae4d0fff33 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 27 May 2018 00:51:57 +0300 Subject: usb: gadget: uvc: configfs: Add bFormatIndex attributes The UVC format description are numbered using the descriptor's bFormatIndex field. The index is used in UVC requests, and is thus needed to handle requests in userspace. Make it dynamically discoverable by exposing it in a bFormatIndex configfs attribute of the uncompressed and mjpeg format config items. The bFormatIndex value exposed through the attribute is stored in the config item private data. However, that value is never set: the driver instead computes the bFormatIndex value when linking the stream class header in the configfs hierarchy and stores it directly in the class descriptors in a separate structure. In order to expose the value through the configfs attribute, store it in the config item private data as well. This results in a small code simplification. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_configfs.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index fa8d2e1f54ba..5cee8aca3734 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -1538,6 +1538,7 @@ UVC_ATTR(uvcg_uncompressed_, cname, aname); #define identity_conv(x) (x) +UVCG_UNCOMPRESSED_ATTR_RO(b_format_index, bFormatIndex, identity_conv); UVCG_UNCOMPRESSED_ATTR(b_bits_per_pixel, bBitsPerPixel, identity_conv); UVCG_UNCOMPRESSED_ATTR(b_default_frame_index, bDefaultFrameIndex, identity_conv); @@ -1568,6 +1569,7 @@ uvcg_uncompressed_bma_controls_store(struct config_item *item, UVC_ATTR(uvcg_uncompressed_, bma_controls, bmaControls); static struct configfs_attribute *uvcg_uncompressed_attrs[] = { + &uvcg_uncompressed_attr_b_format_index, &uvcg_uncompressed_attr_guid_format, &uvcg_uncompressed_attr_b_bits_per_pixel, &uvcg_uncompressed_attr_b_default_frame_index, @@ -1738,6 +1740,7 @@ UVC_ATTR(uvcg_mjpeg_, cname, aname) #define identity_conv(x) (x) +UVCG_MJPEG_ATTR_RO(b_format_index, bFormatIndex, identity_conv); UVCG_MJPEG_ATTR(b_default_frame_index, bDefaultFrameIndex, identity_conv); UVCG_MJPEG_ATTR_RO(bm_flags, bmFlags, identity_conv); @@ -1768,6 +1771,7 @@ uvcg_mjpeg_bma_controls_store(struct config_item *item, UVC_ATTR(uvcg_mjpeg_, bma_controls, bmaControls); static struct configfs_attribute *uvcg_mjpeg_attrs[] = { + &uvcg_mjpeg_attr_b_format_index, &uvcg_mjpeg_attr_b_default_frame_index, &uvcg_mjpeg_attr_bm_flags, &uvcg_mjpeg_attr_b_aspect_ratio_x, @@ -2079,24 +2083,22 @@ static int __uvcg_fill_strm(void *priv1, void *priv2, void *priv3, int n, struct uvcg_format *fmt = priv1; if (fmt->type == UVCG_UNCOMPRESSED) { - struct uvc_format_uncompressed *unc = *dest; struct uvcg_uncompressed *u = container_of(fmt, struct uvcg_uncompressed, fmt); + u->desc.bFormatIndex = n + 1; + u->desc.bNumFrameDescriptors = fmt->num_frames; memcpy(*dest, &u->desc, sizeof(u->desc)); *dest += sizeof(u->desc); - unc->bNumFrameDescriptors = fmt->num_frames; - unc->bFormatIndex = n + 1; } else if (fmt->type == UVCG_MJPEG) { - struct uvc_format_mjpeg *mjp = *dest; struct uvcg_mjpeg *m = container_of(fmt, struct uvcg_mjpeg, fmt); + m->desc.bFormatIndex = n + 1; + m->desc.bNumFrameDescriptors = fmt->num_frames; memcpy(*dest, &m->desc, sizeof(m->desc)); *dest += sizeof(m->desc); - mjp->bNumFrameDescriptors = fmt->num_frames; - mjp->bFormatIndex = n + 1; } else { return -EINVAL; } -- cgit v1.2.1 From b206548be6459ea5ffa82b9f1175915b225a89a1 Mon Sep 17 00:00:00 2001 From: Joel Pepper Date: Tue, 29 May 2018 21:02:13 +0200 Subject: usb: gadget: uvc: configfs: Add bFrameIndex attributes - Add bFrameIndex as a UVCG_FRAME_ATTR_RO for each frame size. - Automatically assign ascending bFrameIndex to each frame in a format. Before all "bFrameindex" attributes were set to "1" with no way to configure the gadget otherwise. This resulted in the host always negotiating for bFrameIndex 1 (i.e. the first frame size of the gadget). After the negotiation the host driver will set the user or application selected frame size, while the gadget is actually set to the first frame size. Now, when the containing format is linked into the streaming header, iterate over all child frame descriptors and assign ascending indices. The automatically assigned indices can be read from the new read only bFrameIndex configfs attribute in each frame descriptor item. Signed-off-by: Joel Pepper [Simplified documentation, renamed function, blank space update] Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_configfs.c | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 5cee8aca3734..b8763343dcae 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -868,6 +868,8 @@ static struct uvcg_streaming_header *to_uvcg_streaming_header(struct config_item return container_of(item, struct uvcg_streaming_header, item); } +static void uvcg_format_set_indices(struct config_group *fmt); + static int uvcg_streaming_header_allow_link(struct config_item *src, struct config_item *target) { @@ -915,6 +917,8 @@ static int uvcg_streaming_header_allow_link(struct config_item *src, if (!target_fmt) goto out; + uvcg_format_set_indices(to_config_group(target)); + format_ptr = kzalloc(sizeof(*format_ptr), GFP_KERNEL); if (!format_ptr) { ret = -ENOMEM; @@ -1146,6 +1150,41 @@ end: \ \ UVC_ATTR(uvcg_frame_, cname, aname); +static ssize_t uvcg_frame_b_frame_index_show(struct config_item *item, + char *page) +{ + struct uvcg_frame *f = to_uvcg_frame(item); + struct uvcg_format *fmt; + struct f_uvc_opts *opts; + struct config_item *opts_item; + struct config_item *fmt_item; + struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex; + int result; + + mutex_lock(su_mutex); /* for navigating configfs hierarchy */ + + fmt_item = f->item.ci_parent; + fmt = to_uvcg_format(fmt_item); + + if (!fmt->linked) { + result = -EBUSY; + goto out; + } + + opts_item = fmt_item->ci_parent->ci_parent->ci_parent; + opts = to_f_uvc_opts(opts_item); + + mutex_lock(&opts->lock); + result = sprintf(page, "%d\n", f->frame.b_frame_index); + mutex_unlock(&opts->lock); + +out: + mutex_unlock(su_mutex); + return result; +} + +UVC_ATTR_RO(uvcg_frame_, b_frame_index, bFrameIndex); + #define noop_conversion(x) (x) UVCG_FRAME_ATTR(bm_capabilities, bmCapabilities, noop_conversion, @@ -1294,6 +1333,7 @@ end: UVC_ATTR(uvcg_frame_, dw_frame_interval, dwFrameInterval); static struct configfs_attribute *uvcg_frame_attrs[] = { + &uvcg_frame_attr_b_frame_index, &uvcg_frame_attr_bm_capabilities, &uvcg_frame_attr_w_width, &uvcg_frame_attr_w_height, @@ -1373,6 +1413,22 @@ static void uvcg_frame_drop(struct config_group *group, struct config_item *item config_item_put(item); } +static void uvcg_format_set_indices(struct config_group *fmt) +{ + struct config_item *ci; + unsigned int i = 1; + + list_for_each_entry(ci, &fmt->cg_children, ci_entry) { + struct uvcg_frame *frm; + + if (ci->ci_type != &uvcg_frame_type) + continue; + + frm = to_uvcg_frame(ci); + frm->frame.b_frame_index = i++; + } +} + /* ----------------------------------------------------------------------------- * streaming/uncompressed/ */ -- cgit v1.2.1 From cb2200f7af8341aaf0c6abd7ba37e4c667c41639 Mon Sep 17 00:00:00 2001 From: Joel Pepper Date: Tue, 29 May 2018 21:02:12 +0200 Subject: usb: gadget: uvc: configfs: Prevent format changes after linking header While checks are in place to avoid attributes and children of a format being manipulated after the format is linked into the streaming header, the linked flag was never actually set, invalidating the protections. Update the flag as appropriate in the header link calls. Signed-off-by: Joel Pepper Reviewed-by: Kieran Bingham Signed-off-by: Laurent Pinchart --- drivers/usb/gadget/function/uvc_configfs.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index b8763343dcae..799dc32c5bc7 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -928,6 +928,7 @@ static int uvcg_streaming_header_allow_link(struct config_item *src, format_ptr->fmt = target_fmt; list_add_tail(&format_ptr->entry, &src_hdr->formats); ++src_hdr->num_fmt; + ++target_fmt->linked; out: mutex_unlock(&opts->lock); @@ -965,6 +966,8 @@ static void uvcg_streaming_header_drop_link(struct config_item *src, break; } + --target_fmt->linked; + out: mutex_unlock(&opts->lock); mutex_unlock(su_mutex); -- cgit v1.2.1 From 89969a842e72b1b653140a4bbddd927b242736d0 Mon Sep 17 00:00:00 2001 From: Paul Elder Date: Sun, 2 Sep 2018 19:46:03 -0400 Subject: usb: gadget: uvc: configfs: Sort frame intervals upon writing There is an issue where the host is unable to tell the gadget what frame rate it wants if the dwFrameIntervals in the interface descriptors are not in ascending order. This means that when instantiating a uvc gadget via configfs the user must make sure the dwFrameIntervals are in ascending order. Instead of silently failing the breaking of this rule, we sort the dwFrameIntervals upon writing to configfs. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- drivers/usb/gadget/function/uvc_configfs.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 799dc32c5bc7..6031467f1868 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -9,6 +9,9 @@ * * Author: Andrzej Pietrasiewicz */ + +#include + #include "u_uvc.h" #include "uvc_configfs.h" @@ -35,6 +38,14 @@ static struct configfs_attribute prefix##attr_##cname = { \ .show = prefix##cname##_show, \ } +static int uvcg_config_compare_u32(const void *l, const void *r) +{ + u32 li = *(const u32 *)l; + u32 ri = *(const u32 *)r; + + return li < ri ? -1 : li == ri ? 0 : 1; +} + static inline struct f_uvc_opts *to_f_uvc_opts(struct config_item *item) { return container_of(to_config_group(item), struct f_uvc_opts, @@ -1325,6 +1336,8 @@ static ssize_t uvcg_frame_dw_frame_interval_store(struct config_item *item, kfree(ch->dw_frame_interval); ch->dw_frame_interval = frm_intrv; ch->frame.b_frame_interval_type = n; + sort(ch->dw_frame_interval, n, sizeof(*ch->dw_frame_interval), + uvcg_config_compare_u32, NULL); ret = len; end: -- cgit v1.2.1 From 9d1ff5dcb3cd3390b1e56f1c24ae42c72257c4a3 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 10 Aug 2018 15:42:03 +0300 Subject: usb: gadget: uvc: Factor out video USB request queueing USB requests for video data are queued from two different locations in the driver, with the same code block occurring twice. Factor it out to a function. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Tested-by: Paul Elder Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_video.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index d3567b90343a..a95c8e2364ed 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -125,6 +125,19 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, * Request handling */ +static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) +{ + int ret; + + ret = usb_ep_queue(video->ep, req, GFP_ATOMIC); + if (ret < 0) { + printk(KERN_INFO "Failed to queue request (%d).\n", ret); + usb_ep_set_halt(video->ep); + } + + return ret; +} + /* * I somehow feel that synchronisation won't be easy to achieve here. We have * three events that control USB requests submission: @@ -189,14 +202,13 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req) video->encode(req, video, buf); - if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) { - printk(KERN_INFO "Failed to queue request (%d).\n", ret); - usb_ep_set_halt(ep); - spin_unlock_irqrestore(&video->queue.irqlock, flags); + ret = uvcg_video_ep_queue(video, req); + spin_unlock_irqrestore(&video->queue.irqlock, flags); + + if (ret < 0) { uvcg_queue_cancel(queue, 0); goto requeue; } - spin_unlock_irqrestore(&video->queue.irqlock, flags); return; @@ -316,15 +328,13 @@ int uvcg_video_pump(struct uvc_video *video) video->encode(req, video, buf); /* Queue the USB request */ - ret = usb_ep_queue(video->ep, req, GFP_ATOMIC); + ret = uvcg_video_ep_queue(video, req); + spin_unlock_irqrestore(&queue->irqlock, flags); + if (ret < 0) { - printk(KERN_INFO "Failed to queue request (%d)\n", ret); - usb_ep_set_halt(video->ep); - spin_unlock_irqrestore(&queue->irqlock, flags); uvcg_queue_cancel(queue, 0); break; } - spin_unlock_irqrestore(&queue->irqlock, flags); } spin_lock_irqsave(&video->req_lock, flags); -- cgit v1.2.1 From 8dbf9c7abefd5c1434a956d5c6b25e11183061a3 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 10 Aug 2018 15:44:57 +0300 Subject: usb: gadget: uvc: Only halt video streaming endpoint in bulk mode When USB requests for video data fail to be submitted, the driver signals a problem to the host by halting the video streaming endpoint. This is only valid in bulk mode, as isochronous transfers have no handshake phase and can't thus report a stall. The usb_ep_set_halt() call returns an error when using isochronous endpoints, which we happily ignore, but some UDCs complain in the kernel log. Fix this by only trying to halt the endpoint in bulk mode. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Tested-by: Paul Elder Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/uvc_video.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index a95c8e2364ed..2c9821ec836e 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -132,7 +132,9 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) ret = usb_ep_queue(video->ep, req, GFP_ATOMIC); if (ret < 0) { printk(KERN_INFO "Failed to queue request (%d).\n", ret); - usb_ep_set_halt(video->ep); + /* Isochronous endpoints can't be halted. */ + if (usb_endpoint_xfer_bulk(video->ep->desc)) + usb_ep_set_halt(video->ep); } return ret; -- cgit v1.2.1 From dc0f755b421d5aac9052f43c9d0e7285607d446c Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 10 Aug 2018 15:48:02 +0300 Subject: usb: gadget: uvc: Replace plain printk() with dev_*() Adding device context to the kernel log messages make them more useful. Add new uvcg_* macros based on dev_*() that print both the gadget device name and the function name. While at it, remove a commented out printk statement and an unused printk-based macro. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Tested-by: Paul Elder Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/f_uvc.c | 41 ++++++++++++++------------------- drivers/usb/gadget/function/uvc.h | 16 ++++++------- drivers/usb/gadget/function/uvc_v4l2.c | 4 ++-- drivers/usb/gadget/function/uvc_video.c | 18 +++++++++------ drivers/usb/gadget/function/uvc_video.h | 2 +- 5 files changed, 39 insertions(+), 42 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c index 4ea987741e6e..0cc4a6220050 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -232,13 +232,8 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) struct v4l2_event v4l2_event; struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; - /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n", - * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue), - * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength)); - */ - if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) { - INFO(f->config->cdev, "invalid request type\n"); + uvcg_info(f, "invalid request type\n"); return -EINVAL; } @@ -272,7 +267,7 @@ uvc_function_get_alt(struct usb_function *f, unsigned interface) { struct uvc_device *uvc = to_uvc(f); - INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface); + uvcg_info(f, "%s(%u)\n", __func__, interface); if (interface == uvc->control_intf) return 0; @@ -291,13 +286,13 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) struct uvc_event *uvc_event = (void *)&v4l2_event.u.data; int ret; - INFO(cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt); + uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt); if (interface == uvc->control_intf) { if (alt) return -EINVAL; - INFO(cdev, "reset UVC Control\n"); + uvcg_info(f, "reset UVC Control\n"); usb_ep_disable(uvc->control_ep); if (!uvc->control_ep->desc) @@ -348,7 +343,7 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt) if (!uvc->video.ep) return -EINVAL; - INFO(cdev, "reset UVC\n"); + uvcg_info(f, "reset UVC\n"); usb_ep_disable(uvc->video.ep); ret = config_ep_by_speed(f->config->cdev->gadget, @@ -373,7 +368,7 @@ uvc_function_disable(struct usb_function *f) struct uvc_device *uvc = to_uvc(f); struct v4l2_event v4l2_event; - INFO(f->config->cdev, "uvc_function_disable\n"); + uvcg_info(f, "%s()\n", __func__); memset(&v4l2_event, 0, sizeof(v4l2_event)); v4l2_event.type = UVC_EVENT_DISCONNECT; @@ -392,21 +387,19 @@ uvc_function_disable(struct usb_function *f) void uvc_function_connect(struct uvc_device *uvc) { - struct usb_composite_dev *cdev = uvc->func.config->cdev; int ret; if ((ret = usb_function_activate(&uvc->func)) < 0) - INFO(cdev, "UVC connect failed with %d\n", ret); + uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret); } void uvc_function_disconnect(struct uvc_device *uvc) { - struct usb_composite_dev *cdev = uvc->func.config->cdev; int ret; if ((ret = usb_function_deactivate(&uvc->func)) < 0) - INFO(cdev, "UVC disconnect failed with %d\n", ret); + uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret); } /* -------------------------------------------------------------------------- @@ -605,7 +598,7 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f) struct f_uvc_opts *opts; int ret = -EINVAL; - INFO(cdev, "uvc_function_bind\n"); + uvcg_info(f, "%s()\n", __func__); opts = fi_to_f_uvc_opts(f->fi); /* Sanity check the streaming endpoint module parameters. @@ -618,8 +611,8 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f) if (opts->streaming_maxburst && (opts->streaming_maxpacket % 1024) != 0) { opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024); - INFO(cdev, "overriding streaming_maxpacket to %d\n", - opts->streaming_maxpacket); + uvcg_info(f, "overriding streaming_maxpacket to %d\n", + opts->streaming_maxpacket); } /* Fill in the FS/HS/SS Video Streaming specific descriptors from the @@ -658,7 +651,7 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f) /* Allocate endpoints. */ ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep); if (!ep) { - INFO(cdev, "Unable to allocate control EP\n"); + uvcg_info(f, "Unable to allocate control EP\n"); goto error; } uvc->control_ep = ep; @@ -672,7 +665,7 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f) ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); if (!ep) { - INFO(cdev, "Unable to allocate streaming EP\n"); + uvcg_info(f, "Unable to allocate streaming EP\n"); goto error; } uvc->video.ep = ep; @@ -745,19 +738,19 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f) uvc->control_req->context = uvc; if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) { - printk(KERN_INFO "v4l2_device_register failed\n"); + uvcg_err(f, "failed to register V4L2 device\n"); goto error; } /* Initialise video. */ - ret = uvcg_video_init(&uvc->video); + ret = uvcg_video_init(&uvc->video, uvc); if (ret < 0) goto error; /* Register a V4L2 device. */ ret = uvc_register_video(uvc); if (ret < 0) { - printk(KERN_INFO "Unable to register video device\n"); + uvcg_err(f, "failed to register video device\n"); goto error; } @@ -894,7 +887,7 @@ static void uvc_unbind(struct usb_configuration *c, struct usb_function *f) struct usb_composite_dev *cdev = c->cdev; struct uvc_device *uvc = to_uvc(f); - INFO(cdev, "%s\n", __func__); + uvcg_info(f, "%s\n", __func__); device_remove_file(&uvc->vdev.dev, &dev_attr_function_name); video_unregister_device(&uvc->vdev); diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h index 93cf78b420fe..099d650082e5 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -24,6 +24,7 @@ struct usb_ep; struct usb_request; struct uvc_descriptor_header; +struct uvc_device; /* ------------------------------------------------------------------------ * Debugging, printing and logging @@ -51,14 +52,12 @@ extern unsigned int uvc_gadget_trace_param; printk(KERN_DEBUG "uvcvideo: " msg); \ } while (0) -#define uvc_warn_once(dev, warn, msg...) \ - do { \ - if (!test_and_set_bit(warn, &dev->warnings)) \ - printk(KERN_INFO "uvcvideo: " msg); \ - } while (0) - -#define uvc_printk(level, msg...) \ - printk(level "uvcvideo: " msg) +#define uvcg_dbg(f, fmt, args...) \ + dev_dbg(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args) +#define uvcg_info(f, fmt, args...) \ + dev_info(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args) +#define uvcg_err(f, fmt, args...) \ + dev_err(&(f)->config->cdev->gadget->dev, "%s: " fmt, (f)->name, ##args) /* ------------------------------------------------------------------------ * Driver specific constants @@ -73,6 +72,7 @@ extern unsigned int uvc_gadget_trace_param; */ struct uvc_video { + struct uvc_device *uvc; struct usb_ep *ep; /* Frame parameters */ diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c index 7f1ca3b57823..a1183eccee22 100644 --- a/drivers/usb/gadget/function/uvc_v4l2.c +++ b/drivers/usb/gadget/function/uvc_v4l2.c @@ -115,8 +115,8 @@ uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt) } if (i == ARRAY_SIZE(uvc_formats)) { - printk(KERN_INFO "Unsupported format 0x%08x.\n", - fmt->fmt.pix.pixelformat); + uvcg_info(&uvc->func, "Unsupported format 0x%08x.\n", + fmt->fmt.pix.pixelformat); return -EINVAL; } diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index 2c9821ec836e..5c042f380708 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -131,7 +131,9 @@ static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) ret = usb_ep_queue(video->ep, req, GFP_ATOMIC); if (ret < 0) { - printk(KERN_INFO "Failed to queue request (%d).\n", ret); + uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n", + ret); + /* Isochronous endpoints can't be halted. */ if (usb_endpoint_xfer_bulk(video->ep->desc)) usb_ep_set_halt(video->ep); @@ -184,13 +186,14 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req) break; case -ESHUTDOWN: /* disconnect from host. */ - printk(KERN_DEBUG "VS request cancelled.\n"); + uvcg_dbg(&video->uvc->func, "VS request cancelled.\n"); uvcg_queue_cancel(queue, 1); goto requeue; default: - printk(KERN_INFO "VS request completed with status %d.\n", - req->status); + uvcg_info(&video->uvc->func, + "VS request completed with status %d.\n", + req->status); uvcg_queue_cancel(queue, 0); goto requeue; } @@ -354,8 +357,8 @@ int uvcg_video_enable(struct uvc_video *video, int enable) int ret; if (video->ep == NULL) { - printk(KERN_INFO "Video enable failed, device is " - "uninitialized.\n"); + uvcg_info(&video->uvc->func, + "Video enable failed, device is uninitialized.\n"); return -ENODEV; } @@ -387,11 +390,12 @@ int uvcg_video_enable(struct uvc_video *video, int enable) /* * Initialize the UVC video stream. */ -int uvcg_video_init(struct uvc_video *video) +int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc) { INIT_LIST_HEAD(&video->req_free); spin_lock_init(&video->req_lock); + video->uvc = uvc; video->fcc = V4L2_PIX_FMT_YUYV; video->bpp = 16; video->width = 320; diff --git a/drivers/usb/gadget/function/uvc_video.h b/drivers/usb/gadget/function/uvc_video.h index 7d77122b0ff9..278dc52c7604 100644 --- a/drivers/usb/gadget/function/uvc_video.h +++ b/drivers/usb/gadget/function/uvc_video.h @@ -18,6 +18,6 @@ int uvcg_video_pump(struct uvc_video *video); int uvcg_video_enable(struct uvc_video *video, int enable); -int uvcg_video_init(struct uvc_video *video); +int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc); #endif /* __UVC_VIDEO_H__ */ -- cgit v1.2.1 From d865d00db9e69eb2e6f7c4166e33e5047de497bc Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 18 Sep 2018 14:26:47 +0300 Subject: usb: gadget: uvc: Remove uvc_set_trace_param() function The function is never called, remove it. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- drivers/usb/gadget/function/f_uvc.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c index 0cc4a6220050..8c99392df593 100644 --- a/drivers/usb/gadget/function/f_uvc.c +++ b/drivers/usb/gadget/function/f_uvc.c @@ -197,12 +197,6 @@ static const struct usb_descriptor_header * const uvc_ss_streaming[] = { NULL, }; -void uvc_set_trace_param(unsigned int trace) -{ - uvc_gadget_trace_param = trace; -} -EXPORT_SYMBOL(uvc_set_trace_param); - /* -------------------------------------------------------------------------- * Control requests */ -- cgit v1.2.1 From 78c9e7ce00c3fabf5c6dd3a6eab3a3dcf21dd213 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 18 Sep 2018 14:40:58 +0300 Subject: usb: gadget: uvc: configfs: Fix operation on big endian platforms USB descriptors are stored in little endian, requiring the use of conversion macros. Those macros are incorrectly used for values stored in native endian structures within the driver. Operation on big endian platforms is thus broken. Fix it by removing the conversion macros where they're not needed. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Tested-by: Paul Elder --- drivers/usb/gadget/function/uvc_configfs.c | 59 ++++++++++++------------------ 1 file changed, 24 insertions(+), 35 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 6031467f1868..522cb7be9850 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -1105,7 +1105,7 @@ static struct uvcg_frame *to_uvcg_frame(struct config_item *item) return container_of(item, struct uvcg_frame, item); } -#define UVCG_FRAME_ATTR(cname, aname, to_cpu_endian, to_little_endian, bits) \ +#define UVCG_FRAME_ATTR(cname, aname, bits) \ static ssize_t uvcg_frame_##cname##_show(struct config_item *item, char *page)\ { \ struct uvcg_frame *f = to_uvcg_frame(item); \ @@ -1120,7 +1120,7 @@ static ssize_t uvcg_frame_##cname##_show(struct config_item *item, char *page)\ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", to_cpu_endian(f->frame.cname)); \ + result = sprintf(page, "%d\n", f->frame.cname); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1154,7 +1154,7 @@ static ssize_t uvcg_frame_##cname##_store(struct config_item *item, \ goto end; \ } \ \ - f->frame.cname = to_little_endian(num); \ + f->frame.cname = num; \ ret = len; \ end: \ mutex_unlock(&opts->lock); \ @@ -1199,20 +1199,13 @@ out: UVC_ATTR_RO(uvcg_frame_, b_frame_index, bFrameIndex); -#define noop_conversion(x) (x) - -UVCG_FRAME_ATTR(bm_capabilities, bmCapabilities, noop_conversion, - noop_conversion, 8); -UVCG_FRAME_ATTR(w_width, wWidth, le16_to_cpu, cpu_to_le16, 16); -UVCG_FRAME_ATTR(w_height, wHeight, le16_to_cpu, cpu_to_le16, 16); -UVCG_FRAME_ATTR(dw_min_bit_rate, dwMinBitRate, le32_to_cpu, cpu_to_le32, 32); -UVCG_FRAME_ATTR(dw_max_bit_rate, dwMaxBitRate, le32_to_cpu, cpu_to_le32, 32); -UVCG_FRAME_ATTR(dw_max_video_frame_buffer_size, dwMaxVideoFrameBufferSize, - le32_to_cpu, cpu_to_le32, 32); -UVCG_FRAME_ATTR(dw_default_frame_interval, dwDefaultFrameInterval, - le32_to_cpu, cpu_to_le32, 32); - -#undef noop_conversion +UVCG_FRAME_ATTR(bm_capabilities, bmCapabilities, 8); +UVCG_FRAME_ATTR(w_width, wWidth, 16); +UVCG_FRAME_ATTR(w_height, wHeight, 16); +UVCG_FRAME_ATTR(dw_min_bit_rate, dwMinBitRate, 32); +UVCG_FRAME_ATTR(dw_max_bit_rate, dwMaxBitRate, 32); +UVCG_FRAME_ATTR(dw_max_video_frame_buffer_size, dwMaxVideoFrameBufferSize, 32); +UVCG_FRAME_ATTR(dw_default_frame_interval, dwDefaultFrameInterval, 32); #undef UVCG_FRAME_ATTR @@ -1233,8 +1226,7 @@ static ssize_t uvcg_frame_dw_frame_interval_show(struct config_item *item, mutex_lock(&opts->lock); for (result = 0, i = 0; i < frm->frame.b_frame_interval_type; ++i) { - result += sprintf(pg, "%d\n", - le32_to_cpu(frm->dw_frame_interval[i])); + result += sprintf(pg, "%d\n", frm->dw_frame_interval[i]); pg = page + result; } mutex_unlock(&opts->lock); @@ -1259,7 +1251,7 @@ static inline int __uvcg_fill_frm_intrv(char *buf, void *priv) return ret; interv = priv; - **interv = cpu_to_le32(num); + **interv = num; ++*interv; return 0; @@ -1381,12 +1373,12 @@ static struct config_item *uvcg_frame_make(struct config_group *group, h->frame.b_descriptor_type = USB_DT_CS_INTERFACE; h->frame.b_frame_index = 1; - h->frame.w_width = cpu_to_le16(640); - h->frame.w_height = cpu_to_le16(360); - h->frame.dw_min_bit_rate = cpu_to_le32(18432000); - h->frame.dw_max_bit_rate = cpu_to_le32(55296000); - h->frame.dw_max_video_frame_buffer_size = cpu_to_le32(460800); - h->frame.dw_default_frame_interval = cpu_to_le32(666666); + h->frame.w_width = 640; + h->frame.w_height = 360; + h->frame.dw_min_bit_rate = 18432000; + h->frame.dw_max_bit_rate = 55296000; + h->frame.dw_max_video_frame_buffer_size = 460800; + h->frame.dw_default_frame_interval = 666666; opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; opts = to_f_uvc_opts(opts_item); @@ -2427,7 +2419,7 @@ static struct configfs_item_operations uvc_func_item_ops = { .release = uvc_func_item_release, }; -#define UVCG_OPTS_ATTR(cname, aname, conv, str2u, uxx, vnoc, limit) \ +#define UVCG_OPTS_ATTR(cname, aname, str2u, uxx, limit) \ static ssize_t f_uvc_opts_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -2435,7 +2427,7 @@ static ssize_t f_uvc_opts_##cname##_show( \ int result; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(opts->cname)); \ + result = sprintf(page, "%d\n", opts->cname); \ mutex_unlock(&opts->lock); \ \ return result; \ @@ -2463,7 +2455,7 @@ f_uvc_opts_##cname##_store(struct config_item *item, \ ret = -EINVAL; \ goto end; \ } \ - opts->cname = vnoc(num); \ + opts->cname = num; \ ret = len; \ end: \ mutex_unlock(&opts->lock); \ @@ -2474,12 +2466,9 @@ UVC_ATTR(f_uvc_opts_, cname, cname) #define identity_conv(x) (x) -UVCG_OPTS_ATTR(streaming_interval, streaming_interval, identity_conv, - kstrtou8, u8, identity_conv, 16); -UVCG_OPTS_ATTR(streaming_maxpacket, streaming_maxpacket, le16_to_cpu, - kstrtou16, u16, le16_to_cpu, 3072); -UVCG_OPTS_ATTR(streaming_maxburst, streaming_maxburst, identity_conv, - kstrtou8, u8, identity_conv, 15); +UVCG_OPTS_ATTR(streaming_interval, streaming_interval, kstrtou8, u8, 16); +UVCG_OPTS_ATTR(streaming_maxpacket, streaming_maxpacket, kstrtou16, u16, 3072); +UVCG_OPTS_ATTR(streaming_maxburst, streaming_maxburst, kstrtou8, u8, 15); #undef identity_conv -- cgit v1.2.1 From 4f2a6552c2888b317984e64b292f0f1ca1832d44 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 18 Sep 2018 15:04:16 +0300 Subject: usb: gadget: uvc: configfs: Simplify attributes macros Several macros used to define attributes and their access functions take multiple arguments to specify endianness and string conversion functions, based on the size of the attribute. This can be simplified by passing the number of bits explicitly, and constructing the name of the functions internally. The UVCG_OPTS_ATTR macro can be simplified further as all fields it deals with are unsigned int. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Tested-by: Paul Elder --- drivers/usb/gadget/function/uvc_configfs.c | 184 ++++++++++++----------------- 1 file changed, 74 insertions(+), 110 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 522cb7be9850..36f8f03e25e4 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -38,6 +38,9 @@ static struct configfs_attribute prefix##attr_##cname = { \ .show = prefix##cname##_show, \ } +#define le8_to_cpu(x) (x) +#define cpu_to_le8(x) (x) + static int uvcg_config_compare_u32(const void *l, const void *r) { u32 li = *(const u32 *)l; @@ -135,9 +138,9 @@ static struct uvcg_control_header *to_uvcg_control_header(struct config_item *it return container_of(item, struct uvcg_control_header, item); } -#define UVCG_CTRL_HDR_ATTR(cname, aname, conv, str2u, uxx, vnoc, limit) \ +#define UVCG_CTRL_HDR_ATTR(cname, aname, bits, limit) \ static ssize_t uvcg_control_header_##cname##_show( \ - struct config_item *item, char *page) \ + struct config_item *item, char *page) \ { \ struct uvcg_control_header *ch = to_uvcg_control_header(item); \ struct f_uvc_opts *opts; \ @@ -151,7 +154,7 @@ static ssize_t uvcg_control_header_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(ch->desc.aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(ch->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -167,7 +170,7 @@ uvcg_control_header_##cname##_store(struct config_item *item, \ struct config_item *opts_item; \ struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex;\ int ret; \ - uxx num; \ + u##bits num; \ \ mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \ \ @@ -180,7 +183,7 @@ uvcg_control_header_##cname##_store(struct config_item *item, \ goto end; \ } \ \ - ret = str2u(page, 0, &num); \ + ret = kstrtou##bits(page, 0, &num); \ if (ret) \ goto end; \ \ @@ -188,7 +191,7 @@ uvcg_control_header_##cname##_store(struct config_item *item, \ ret = -EINVAL; \ goto end; \ } \ - ch->desc.aname = vnoc(num); \ + ch->desc.aname = cpu_to_le##bits(num); \ ret = len; \ end: \ mutex_unlock(&opts->lock); \ @@ -198,11 +201,9 @@ end: \ \ UVC_ATTR(uvcg_control_header_, cname, aname) -UVCG_CTRL_HDR_ATTR(bcd_uvc, bcdUVC, le16_to_cpu, kstrtou16, u16, cpu_to_le16, - 0xffff); +UVCG_CTRL_HDR_ATTR(bcd_uvc, bcdUVC, 16, 0xffff); -UVCG_CTRL_HDR_ATTR(dw_clock_frequency, dwClockFrequency, le32_to_cpu, kstrtou32, - u32, cpu_to_le32, 0x7fffffff); +UVCG_CTRL_HDR_ATTR(dw_clock_frequency, dwClockFrequency, 32, 0x7fffffff); #undef UVCG_CTRL_HDR_ATTR @@ -255,7 +256,7 @@ static const struct uvcg_config_group_type uvcg_control_header_grp_type = { * control/processing/default */ -#define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, conv) \ +#define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, bits) \ static ssize_t uvcg_default_processing_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -273,7 +274,7 @@ static ssize_t uvcg_default_processing_##cname##_show( \ pd = &opts->uvc_processing; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(pd->aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(pd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -282,14 +283,10 @@ static ssize_t uvcg_default_processing_##cname##_show( \ \ UVC_ATTR_RO(uvcg_default_processing_, cname, aname) -#define identity_conv(x) (x) - -UVCG_DEFAULT_PROCESSING_ATTR(b_unit_id, bUnitID, identity_conv); -UVCG_DEFAULT_PROCESSING_ATTR(b_source_id, bSourceID, identity_conv); -UVCG_DEFAULT_PROCESSING_ATTR(w_max_multiplier, wMaxMultiplier, le16_to_cpu); -UVCG_DEFAULT_PROCESSING_ATTR(i_processing, iProcessing, identity_conv); - -#undef identity_conv +UVCG_DEFAULT_PROCESSING_ATTR(b_unit_id, bUnitID, 8); +UVCG_DEFAULT_PROCESSING_ATTR(b_source_id, bSourceID, 8); +UVCG_DEFAULT_PROCESSING_ATTR(w_max_multiplier, wMaxMultiplier, 16); +UVCG_DEFAULT_PROCESSING_ATTR(i_processing, iProcessing, 8); #undef UVCG_DEFAULT_PROCESSING_ATTR @@ -362,7 +359,7 @@ static const struct uvcg_config_group_type uvcg_processing_grp_type = { * control/terminal/camera/default */ -#define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, conv) \ +#define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, bits) \ static ssize_t uvcg_default_camera_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -381,7 +378,7 @@ static ssize_t uvcg_default_camera_##cname##_show( \ cd = &opts->uvc_camera_terminal; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(cd->aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(cd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -391,20 +388,16 @@ static ssize_t uvcg_default_camera_##cname##_show( \ \ UVC_ATTR_RO(uvcg_default_camera_, cname, aname) -#define identity_conv(x) (x) - -UVCG_DEFAULT_CAMERA_ATTR(b_terminal_id, bTerminalID, identity_conv); -UVCG_DEFAULT_CAMERA_ATTR(w_terminal_type, wTerminalType, le16_to_cpu); -UVCG_DEFAULT_CAMERA_ATTR(b_assoc_terminal, bAssocTerminal, identity_conv); -UVCG_DEFAULT_CAMERA_ATTR(i_terminal, iTerminal, identity_conv); +UVCG_DEFAULT_CAMERA_ATTR(b_terminal_id, bTerminalID, 8); +UVCG_DEFAULT_CAMERA_ATTR(w_terminal_type, wTerminalType, 16); +UVCG_DEFAULT_CAMERA_ATTR(b_assoc_terminal, bAssocTerminal, 8); +UVCG_DEFAULT_CAMERA_ATTR(i_terminal, iTerminal, 8); UVCG_DEFAULT_CAMERA_ATTR(w_objective_focal_length_min, wObjectiveFocalLengthMin, - le16_to_cpu); + 16); UVCG_DEFAULT_CAMERA_ATTR(w_objective_focal_length_max, wObjectiveFocalLengthMax, - le16_to_cpu); + 16); UVCG_DEFAULT_CAMERA_ATTR(w_ocular_focal_length, wOcularFocalLength, - le16_to_cpu); - -#undef identity_conv + 16); #undef UVCG_DEFAULT_CAMERA_ATTR @@ -480,7 +473,7 @@ static const struct uvcg_config_group_type uvcg_camera_grp_type = { * control/terminal/output/default */ -#define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, conv) \ +#define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, bits) \ static ssize_t uvcg_default_output_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -499,7 +492,7 @@ static ssize_t uvcg_default_output_##cname##_show( \ cd = &opts->uvc_output_terminal; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(cd->aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(cd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -509,15 +502,11 @@ static ssize_t uvcg_default_output_##cname##_show( \ \ UVC_ATTR_RO(uvcg_default_output_, cname, aname) -#define identity_conv(x) (x) - -UVCG_DEFAULT_OUTPUT_ATTR(b_terminal_id, bTerminalID, identity_conv); -UVCG_DEFAULT_OUTPUT_ATTR(w_terminal_type, wTerminalType, le16_to_cpu); -UVCG_DEFAULT_OUTPUT_ATTR(b_assoc_terminal, bAssocTerminal, identity_conv); -UVCG_DEFAULT_OUTPUT_ATTR(b_source_id, bSourceID, identity_conv); -UVCG_DEFAULT_OUTPUT_ATTR(i_terminal, iTerminal, identity_conv); - -#undef identity_conv +UVCG_DEFAULT_OUTPUT_ATTR(b_terminal_id, bTerminalID, 8); +UVCG_DEFAULT_OUTPUT_ATTR(w_terminal_type, wTerminalType, 16); +UVCG_DEFAULT_OUTPUT_ATTR(b_assoc_terminal, bAssocTerminal, 8); +UVCG_DEFAULT_OUTPUT_ATTR(b_source_id, bSourceID, 8); +UVCG_DEFAULT_OUTPUT_ATTR(i_terminal, iTerminal, 8); #undef UVCG_DEFAULT_OUTPUT_ATTR @@ -990,9 +979,9 @@ static struct configfs_item_operations uvcg_streaming_header_item_ops = { .drop_link = uvcg_streaming_header_drop_link, }; -#define UVCG_STREAMING_HEADER_ATTR(cname, aname, conv) \ +#define UVCG_STREAMING_HEADER_ATTR(cname, aname, bits) \ static ssize_t uvcg_streaming_header_##cname##_show( \ - struct config_item *item, char *page) \ + struct config_item *item, char *page) \ { \ struct uvcg_streaming_header *sh = to_uvcg_streaming_header(item); \ struct f_uvc_opts *opts; \ @@ -1006,7 +995,7 @@ static ssize_t uvcg_streaming_header_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(sh->desc.aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(sh->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1015,16 +1004,11 @@ static ssize_t uvcg_streaming_header_##cname##_show( \ \ UVC_ATTR_RO(uvcg_streaming_header_, cname, aname) -#define identity_conv(x) (x) - -UVCG_STREAMING_HEADER_ATTR(bm_info, bmInfo, identity_conv); -UVCG_STREAMING_HEADER_ATTR(b_terminal_link, bTerminalLink, identity_conv); -UVCG_STREAMING_HEADER_ATTR(b_still_capture_method, bStillCaptureMethod, - identity_conv); -UVCG_STREAMING_HEADER_ATTR(b_trigger_support, bTriggerSupport, identity_conv); -UVCG_STREAMING_HEADER_ATTR(b_trigger_usage, bTriggerUsage, identity_conv); - -#undef identity_conv +UVCG_STREAMING_HEADER_ATTR(bm_info, bmInfo, 8); +UVCG_STREAMING_HEADER_ATTR(b_terminal_link, bTerminalLink, 8); +UVCG_STREAMING_HEADER_ATTR(b_still_capture_method, bStillCaptureMethod, 8); +UVCG_STREAMING_HEADER_ATTR(b_trigger_support, bTriggerSupport, 8); +UVCG_STREAMING_HEADER_ATTR(b_trigger_usage, bTriggerUsage, 8); #undef UVCG_STREAMING_HEADER_ATTR @@ -1135,8 +1119,8 @@ static ssize_t uvcg_frame_##cname##_store(struct config_item *item, \ struct config_item *opts_item; \ struct uvcg_format *fmt; \ struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex;\ + typeof(f->frame.cname) num; \ int ret; \ - u##bits num; \ \ ret = kstrtou##bits(page, 0, &num); \ if (ret) \ @@ -1512,7 +1496,7 @@ end: UVC_ATTR(uvcg_uncompressed_, guid_format, guidFormat); -#define UVCG_UNCOMPRESSED_ATTR_RO(cname, aname, conv) \ +#define UVCG_UNCOMPRESSED_ATTR_RO(cname, aname, bits) \ static ssize_t uvcg_uncompressed_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -1528,7 +1512,7 @@ static ssize_t uvcg_uncompressed_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(u->desc.aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1537,7 +1521,7 @@ static ssize_t uvcg_uncompressed_##cname##_show( \ \ UVC_ATTR_RO(uvcg_uncompressed_, cname, aname); -#define UVCG_UNCOMPRESSED_ATTR(cname, aname, conv) \ +#define UVCG_UNCOMPRESSED_ATTR(cname, aname, bits) \ static ssize_t uvcg_uncompressed_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -1553,7 +1537,7 @@ static ssize_t uvcg_uncompressed_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(u->desc.aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1600,17 +1584,12 @@ end: \ \ UVC_ATTR(uvcg_uncompressed_, cname, aname); -#define identity_conv(x) (x) - -UVCG_UNCOMPRESSED_ATTR_RO(b_format_index, bFormatIndex, identity_conv); -UVCG_UNCOMPRESSED_ATTR(b_bits_per_pixel, bBitsPerPixel, identity_conv); -UVCG_UNCOMPRESSED_ATTR(b_default_frame_index, bDefaultFrameIndex, - identity_conv); -UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, identity_conv); -UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, identity_conv); -UVCG_UNCOMPRESSED_ATTR_RO(bm_interface_flags, bmInterfaceFlags, identity_conv); - -#undef identity_conv +UVCG_UNCOMPRESSED_ATTR_RO(b_format_index, bFormatIndex, 8); +UVCG_UNCOMPRESSED_ATTR(b_bits_per_pixel, bBitsPerPixel, 8); +UVCG_UNCOMPRESSED_ATTR(b_default_frame_index, bDefaultFrameIndex, 8); +UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, 8); +UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, 8); +UVCG_UNCOMPRESSED_ATTR_RO(bm_interface_flags, bmInterfaceFlags, 8); #undef UVCG_UNCOMPRESSED_ATTR #undef UVCG_UNCOMPRESSED_ATTR_RO @@ -1716,7 +1695,7 @@ static struct configfs_group_operations uvcg_mjpeg_group_ops = { .drop_item = uvcg_frame_drop, }; -#define UVCG_MJPEG_ATTR_RO(cname, aname, conv) \ +#define UVCG_MJPEG_ATTR_RO(cname, aname, bits) \ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ { \ struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \ @@ -1731,7 +1710,7 @@ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(u->desc.aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1740,7 +1719,7 @@ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ \ UVC_ATTR_RO(uvcg_mjpeg_, cname, aname) -#define UVCG_MJPEG_ATTR(cname, aname, conv) \ +#define UVCG_MJPEG_ATTR(cname, aname, bits) \ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ { \ struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \ @@ -1755,7 +1734,7 @@ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(u->desc.aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1802,17 +1781,12 @@ end: \ \ UVC_ATTR(uvcg_mjpeg_, cname, aname) -#define identity_conv(x) (x) - -UVCG_MJPEG_ATTR_RO(b_format_index, bFormatIndex, identity_conv); -UVCG_MJPEG_ATTR(b_default_frame_index, bDefaultFrameIndex, - identity_conv); -UVCG_MJPEG_ATTR_RO(bm_flags, bmFlags, identity_conv); -UVCG_MJPEG_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, identity_conv); -UVCG_MJPEG_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, identity_conv); -UVCG_MJPEG_ATTR_RO(bm_interface_flags, bmInterfaceFlags, identity_conv); - -#undef identity_conv +UVCG_MJPEG_ATTR_RO(b_format_index, bFormatIndex, 8); +UVCG_MJPEG_ATTR(b_default_frame_index, bDefaultFrameIndex, 8); +UVCG_MJPEG_ATTR_RO(bm_flags, bmFlags, 8); +UVCG_MJPEG_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, 8); +UVCG_MJPEG_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, 8); +UVCG_MJPEG_ATTR_RO(bm_interface_flags, bmInterfaceFlags, 8); #undef UVCG_MJPEG_ATTR #undef UVCG_MJPEG_ATTR_RO @@ -1894,9 +1868,9 @@ static const struct uvcg_config_group_type uvcg_mjpeg_grp_type = { * streaming/color_matching/default */ -#define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, conv) \ +#define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, bits) \ static ssize_t uvcg_default_color_matching_##cname##_show( \ - struct config_item *item, char *page) \ + struct config_item *item, char *page) \ { \ struct config_group *group = to_config_group(item); \ struct f_uvc_opts *opts; \ @@ -1912,7 +1886,7 @@ static ssize_t uvcg_default_color_matching_##cname##_show( \ cd = &opts->uvc_color_matching; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", conv(cd->aname)); \ + result = sprintf(page, "%d\n", le##bits##_to_cpu(cd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1921,16 +1895,10 @@ static ssize_t uvcg_default_color_matching_##cname##_show( \ \ UVC_ATTR_RO(uvcg_default_color_matching_, cname, aname) -#define identity_conv(x) (x) - -UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_color_primaries, bColorPrimaries, - identity_conv); +UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_color_primaries, bColorPrimaries, 8); UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_transfer_characteristics, - bTransferCharacteristics, identity_conv); -UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_matrix_coefficients, bMatrixCoefficients, - identity_conv); - -#undef identity_conv + bTransferCharacteristics, 8); +UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_matrix_coefficients, bMatrixCoefficients, 8); #undef UVCG_DEFAULT_COLOR_MATCHING_ATTR @@ -2419,7 +2387,7 @@ static struct configfs_item_operations uvc_func_item_ops = { .release = uvc_func_item_release, }; -#define UVCG_OPTS_ATTR(cname, aname, str2u, uxx, limit) \ +#define UVCG_OPTS_ATTR(cname, aname, limit) \ static ssize_t f_uvc_opts_##cname##_show( \ struct config_item *item, char *page) \ { \ @@ -2438,8 +2406,8 @@ f_uvc_opts_##cname##_store(struct config_item *item, \ const char *page, size_t len) \ { \ struct f_uvc_opts *opts = to_f_uvc_opts(item); \ + unsigned int num; \ int ret; \ - uxx num; \ \ mutex_lock(&opts->lock); \ if (opts->refcnt) { \ @@ -2447,7 +2415,7 @@ f_uvc_opts_##cname##_store(struct config_item *item, \ goto end; \ } \ \ - ret = str2u(page, 0, &num); \ + ret = kstrtouint(page, 0, &num); \ if (ret) \ goto end; \ \ @@ -2464,13 +2432,9 @@ end: \ \ UVC_ATTR(f_uvc_opts_, cname, cname) -#define identity_conv(x) (x) - -UVCG_OPTS_ATTR(streaming_interval, streaming_interval, kstrtou8, u8, 16); -UVCG_OPTS_ATTR(streaming_maxpacket, streaming_maxpacket, kstrtou16, u16, 3072); -UVCG_OPTS_ATTR(streaming_maxburst, streaming_maxburst, kstrtou8, u8, 15); - -#undef identity_conv +UVCG_OPTS_ATTR(streaming_interval, streaming_interval, 16); +UVCG_OPTS_ATTR(streaming_maxpacket, streaming_maxpacket, 3072); +UVCG_OPTS_ATTR(streaming_maxburst, streaming_maxburst, 15); #undef UVCG_OPTS_ATTR -- cgit v1.2.1 From 3fb2fd76eda265ce5421318de38dd9b9f7c54737 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 18 Sep 2018 15:04:16 +0300 Subject: usb: gadget: uvc: configfs: Use %u to print unsigned int values The driver uses the %d format to print unsigned int values. The correct format is %u. Fix it. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Tested-by: Paul Elder --- drivers/usb/gadget/function/uvc_configfs.c | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c index 36f8f03e25e4..bc1e2af566c3 100644 --- a/drivers/usb/gadget/function/uvc_configfs.c +++ b/drivers/usb/gadget/function/uvc_configfs.c @@ -154,7 +154,7 @@ static ssize_t uvcg_control_header_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(ch->desc.aname));\ + result = sprintf(page, "%u\n", le##bits##_to_cpu(ch->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -274,7 +274,7 @@ static ssize_t uvcg_default_processing_##cname##_show( \ pd = &opts->uvc_processing; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(pd->aname)); \ + result = sprintf(page, "%u\n", le##bits##_to_cpu(pd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -309,7 +309,7 @@ static ssize_t uvcg_default_processing_bm_controls_show( mutex_lock(&opts->lock); for (result = 0, i = 0; i < pd->bControlSize; ++i) { - result += sprintf(pg, "%d\n", pd->bmControls[i]); + result += sprintf(pg, "%u\n", pd->bmControls[i]); pg = page + result; } mutex_unlock(&opts->lock); @@ -378,7 +378,7 @@ static ssize_t uvcg_default_camera_##cname##_show( \ cd = &opts->uvc_camera_terminal; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(cd->aname)); \ + result = sprintf(page, "%u\n", le##bits##_to_cpu(cd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -421,7 +421,7 @@ static ssize_t uvcg_default_camera_bm_controls_show( mutex_lock(&opts->lock); for (result = 0, i = 0; i < cd->bControlSize; ++i) { - result += sprintf(pg, "%d\n", cd->bmControls[i]); + result += sprintf(pg, "%u\n", cd->bmControls[i]); pg = page + result; } mutex_unlock(&opts->lock); @@ -492,7 +492,7 @@ static ssize_t uvcg_default_output_##cname##_show( \ cd = &opts->uvc_output_terminal; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(cd->aname)); \ + result = sprintf(page, "%u\n", le##bits##_to_cpu(cd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -995,7 +995,7 @@ static ssize_t uvcg_streaming_header_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(sh->desc.aname));\ + result = sprintf(page, "%u\n", le##bits##_to_cpu(sh->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1104,7 +1104,7 @@ static ssize_t uvcg_frame_##cname##_show(struct config_item *item, char *page)\ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", f->frame.cname); \ + result = sprintf(page, "%u\n", f->frame.cname); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1173,7 +1173,7 @@ static ssize_t uvcg_frame_b_frame_index_show(struct config_item *item, opts = to_f_uvc_opts(opts_item); mutex_lock(&opts->lock); - result = sprintf(page, "%d\n", f->frame.b_frame_index); + result = sprintf(page, "%u\n", f->frame.b_frame_index); mutex_unlock(&opts->lock); out: @@ -1210,7 +1210,7 @@ static ssize_t uvcg_frame_dw_frame_interval_show(struct config_item *item, mutex_lock(&opts->lock); for (result = 0, i = 0; i < frm->frame.b_frame_interval_type; ++i) { - result += sprintf(pg, "%d\n", frm->dw_frame_interval[i]); + result += sprintf(pg, "%u\n", frm->dw_frame_interval[i]); pg = page + result; } mutex_unlock(&opts->lock); @@ -1512,7 +1512,7 @@ static ssize_t uvcg_uncompressed_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ + result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1537,7 +1537,7 @@ static ssize_t uvcg_uncompressed_##cname##_show( \ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ + result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1710,7 +1710,7 @@ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ + result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1734,7 +1734,7 @@ static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\ opts = to_f_uvc_opts(opts_item); \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(u->desc.aname));\ + result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -1886,7 +1886,7 @@ static ssize_t uvcg_default_color_matching_##cname##_show( \ cd = &opts->uvc_color_matching; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", le##bits##_to_cpu(cd->aname)); \ + result = sprintf(page, "%u\n", le##bits##_to_cpu(cd->aname)); \ mutex_unlock(&opts->lock); \ \ mutex_unlock(su_mutex); \ @@ -2395,7 +2395,7 @@ static ssize_t f_uvc_opts_##cname##_show( \ int result; \ \ mutex_lock(&opts->lock); \ - result = sprintf(page, "%d\n", opts->cname); \ + result = sprintf(page, "%u\n", opts->cname); \ mutex_unlock(&opts->lock); \ \ return result; \ -- cgit v1.2.1 From 3fa4eaa6c08206b5fa6a8ba49b891d6aab243f52 Mon Sep 17 00:00:00 2001 From: Andreas Pape Date: Thu, 21 Jun 2018 17:22:51 +0200 Subject: usb: gadget: f_uac2: disable IN/OUT ep if unused Via p_chmask/c_chmask the user can define whether uac2 shall support playback and/or capture. This has only effect on the created ALSA device, but not on the USB descriptor. This patch adds playback/capture descriptors dependent on that parameter. Signed-off-by: Andreas Pape Signed-off-by: Eugeniu Rosca Signed-off-by: Felipe Balbi --- drivers/usb/gadget/function/f_uac2.c | 216 ++++++++++++++++++++++++++++------- 1 file changed, 172 insertions(+), 44 deletions(-) (limited to 'drivers/usb/gadget/function') diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c index d582921f7257..db2d4980cb35 100644 --- a/drivers/usb/gadget/function/f_uac2.c +++ b/drivers/usb/gadget/function/f_uac2.c @@ -22,12 +22,8 @@ * controlled by two clock sources : * CLK_5 := c_srate, and CLK_6 := p_srate */ -#define USB_OUT_IT_ID 1 -#define IO_IN_IT_ID 2 -#define IO_OUT_OT_ID 3 -#define USB_IN_OT_ID 4 -#define USB_OUT_CLK_ID 5 -#define USB_IN_CLK_ID 6 +#define USB_OUT_CLK_ID (out_clk_src_desc.bClockID) +#define USB_IN_CLK_ID (in_clk_src_desc.bClockID) #define CONTROL_ABSENT 0 #define CONTROL_RDONLY 1 @@ -43,6 +39,9 @@ #define UNFLW_CTRL 8 #define OVFLW_CTRL 10 +#define EPIN_EN(_opts) ((_opts)->p_chmask != 0) +#define EPOUT_EN(_opts) ((_opts)->c_chmask != 0) + struct f_uac2 { struct g_audio g_audio; u8 ac_intf, as_in_intf, as_out_intf; @@ -135,7 +134,7 @@ static struct uac_clock_source_descriptor in_clk_src_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC2_CLOCK_SOURCE, - .bClockID = USB_IN_CLK_ID, + /* .bClockID = DYNAMIC */ .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), .bAssocTerminal = 0, @@ -147,7 +146,7 @@ static struct uac_clock_source_descriptor out_clk_src_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC2_CLOCK_SOURCE, - .bClockID = USB_OUT_CLK_ID, + /* .bClockID = DYNAMIC */ .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED, .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL), .bAssocTerminal = 0, @@ -159,10 +158,10 @@ static struct uac2_input_terminal_descriptor usb_out_it_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC_INPUT_TERMINAL, - .bTerminalID = USB_OUT_IT_ID, + /* .bTerminalID = DYNAMIC */ .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), .bAssocTerminal = 0, - .bCSourceID = USB_OUT_CLK_ID, + /* .bCSourceID = DYNAMIC */ .iChannelNames = 0, .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), }; @@ -173,10 +172,10 @@ static struct uac2_input_terminal_descriptor io_in_it_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC_INPUT_TERMINAL, - .bTerminalID = IO_IN_IT_ID, + /* .bTerminalID = DYNAMIC */ .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED), .bAssocTerminal = 0, - .bCSourceID = USB_IN_CLK_ID, + /* .bCSourceID = DYNAMIC */ .iChannelNames = 0, .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), }; @@ -187,11 +186,11 @@ static struct uac2_output_terminal_descriptor usb_in_ot_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, - .bTerminalID = USB_IN_OT_ID, + /* .bTerminalID = DYNAMIC */ .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), .bAssocTerminal = 0, - .bSourceID = IO_IN_IT_ID, - .bCSourceID = USB_IN_CLK_ID, + /* .bSourceID = DYNAMIC */ + /* .bCSourceID = DYNAMIC */ .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), }; @@ -201,11 +200,11 @@ static struct uac2_output_terminal_descriptor io_out_ot_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, - .bTerminalID = IO_OUT_OT_ID, + /* .bTerminalID = DYNAMIC */ .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED), .bAssocTerminal = 0, - .bSourceID = USB_OUT_IT_ID, - .bCSourceID = USB_OUT_CLK_ID, + /* .bSourceID = DYNAMIC */ + /* .bCSourceID = DYNAMIC */ .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL), }; @@ -253,7 +252,7 @@ static struct uac2_as_header_descriptor as_out_hdr_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC_AS_GENERAL, - .bTerminalLink = USB_OUT_IT_ID, + /* .bTerminalLink = DYNAMIC */ .bmControls = 0, .bFormatType = UAC_FORMAT_TYPE_I, .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), @@ -330,7 +329,7 @@ static struct uac2_as_header_descriptor as_in_hdr_desc = { .bDescriptorType = USB_DT_CS_INTERFACE, .bDescriptorSubtype = UAC_AS_GENERAL, - .bTerminalLink = USB_IN_OT_ID, + /* .bTerminalLink = DYNAMIC */ .bmControls = 0, .bFormatType = UAC_FORMAT_TYPE_I, .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM), @@ -471,6 +470,125 @@ static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, le16_to_cpu(ep_desc->wMaxPacketSize))); } +/* Use macro to overcome line length limitation */ +#define USBDHDR(p) (struct usb_descriptor_header *)(p) + +static void setup_descriptor(struct f_uac2_opts *opts) +{ + /* patch descriptors */ + int i = 1; /* ID's start with 1 */ + + if (EPOUT_EN(opts)) + usb_out_it_desc.bTerminalID = i++; + if (EPIN_EN(opts)) + io_in_it_desc.bTerminalID = i++; + if (EPOUT_EN(opts)) + io_out_ot_desc.bTerminalID = i++; + if (EPIN_EN(opts)) + usb_in_ot_desc.bTerminalID = i++; + if (EPOUT_EN(opts)) + out_clk_src_desc.bClockID = i++; + if (EPIN_EN(opts)) + in_clk_src_desc.bClockID = i++; + + usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID; + usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID; + usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID; + io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID; + io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID; + io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID; + as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID; + as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID; + + iad_desc.bInterfaceCount = 1; + ac_hdr_desc.wTotalLength = 0; + + if (EPIN_EN(opts)) { + u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength); + + len += sizeof(in_clk_src_desc); + len += sizeof(usb_in_ot_desc); + len += sizeof(io_in_it_desc); + ac_hdr_desc.wTotalLength = cpu_to_le16(len); + iad_desc.bInterfaceCount++; + } + if (EPOUT_EN(opts)) { + u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength); + + len += sizeof(out_clk_src_desc); + len += sizeof(usb_out_it_desc); + len += sizeof(io_out_ot_desc); + ac_hdr_desc.wTotalLength = cpu_to_le16(len); + iad_desc.bInterfaceCount++; + } + + i = 0; + fs_audio_desc[i++] = USBDHDR(&iad_desc); + fs_audio_desc[i++] = USBDHDR(&std_ac_if_desc); + fs_audio_desc[i++] = USBDHDR(&ac_hdr_desc); + if (EPIN_EN(opts)) + fs_audio_desc[i++] = USBDHDR(&in_clk_src_desc); + if (EPOUT_EN(opts)) { + fs_audio_desc[i++] = USBDHDR(&out_clk_src_desc); + fs_audio_desc[i++] = USBDHDR(&usb_out_it_desc); + } + if (EPIN_EN(opts)) { + fs_audio_desc[i++] = USBDHDR(&io_in_it_desc); + fs_audio_desc[i++] = USBDHDR(&usb_in_ot_desc); + } + if (EPOUT_EN(opts)) { + fs_audio_desc[i++] = USBDHDR(&io_out_ot_desc); + fs_audio_desc[i++] = USBDHDR(&std_as_out_if0_desc); + fs_audio_desc[i++] = USBDHDR(&std_as_out_if1_desc); + fs_audio_desc[i++] = USBDHDR(&as_out_hdr_desc); + fs_audio_desc[i++] = USBDHDR(&as_out_fmt1_desc); + fs_audio_desc[i++] = USBDHDR(&fs_epout_desc); + fs_audio_desc[i++] = USBDHDR(&as_iso_out_desc); + } + if (EPIN_EN(opts)) { + fs_audio_desc[i++] = USBDHDR(&std_as_in_if0_desc); + fs_audio_desc[i++] = USBDHDR(&std_as_in_if1_desc); + fs_audio_desc[i++] = USBDHDR(&as_in_hdr_desc); + fs_audio_desc[i++] = USBDHDR(&as_in_fmt1_desc); + fs_audio_desc[i++] = USBDHDR(&fs_epin_desc); + fs_audio_desc[i++] = USBDHDR(&as_iso_in_desc); + } + fs_audio_desc[i] = NULL; + + i = 0; + hs_audio_desc[i++] = USBDHDR(&iad_desc); + hs_audio_desc[i++] = USBDHDR(&std_ac_if_desc); + hs_audio_desc[i++] = USBDHDR(&ac_hdr_desc); + if (EPIN_EN(opts)) + hs_audio_desc[i++] = USBDHDR(&in_clk_src_desc); + if (EPOUT_EN(opts)) { + hs_audio_desc[i++] = USBDHDR(&out_clk_src_desc); + hs_audio_desc[i++] = USBDHDR(&usb_out_it_desc); + } + if (EPIN_EN(opts)) { + hs_audio_desc[i++] = USBDHDR(&io_in_it_desc); + hs_audio_desc[i++] = USBDHDR(&usb_in_ot_desc); + } + if (EPOUT_EN(opts)) { + hs_audio_desc[i++] = USBDHDR(&io_out_ot_desc); + hs_audio_desc[i++] = USBDHDR(&std_as_out_if0_desc); + hs_audio_desc[i++] = USBDHDR(&std_as_out_if1_desc); + hs_audio_desc[i++] = USBDHDR(&as_out_hdr_desc); + hs_audio_desc[i++] = USBDHDR(&as_out_fmt1_desc); + hs_audio_desc[i++] = USBDHDR(&hs_epout_desc); + hs_audio_desc[i++] = USBDHDR(&as_iso_out_desc); + } + if (EPIN_EN(opts)) { + hs_audio_desc[i++] = USBDHDR(&std_as_in_if0_desc); + hs_audio_desc[i++] = USBDHDR(&std_as_in_if1_desc); + hs_audio_desc[i++] = USBDHDR(&as_in_hdr_desc); + hs_audio_desc[i++] = USBDHDR(&as_in_fmt1_desc); + hs_audio_desc[i++] = USBDHDR(&hs_epin_desc); + hs_audio_desc[i++] = USBDHDR(&as_iso_in_desc); + } + hs_audio_desc[i] = NULL; +} + static int afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) { @@ -530,25 +648,29 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) uac2->ac_intf = ret; uac2->ac_alt = 0; - ret = usb_interface_id(cfg, fn); - if (ret < 0) { - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); - return ret; + if (EPOUT_EN(uac2_opts)) { + ret = usb_interface_id(cfg, fn); + if (ret < 0) { + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); + return ret; + } + std_as_out_if0_desc.bInterfaceNumber = ret; + std_as_out_if1_desc.bInterfaceNumber = ret; + uac2->as_out_intf = ret; + uac2->as_out_alt = 0; } - std_as_out_if0_desc.bInterfaceNumber = ret; - std_as_out_if1_desc.bInterfaceNumber = ret; - uac2->as_out_intf = ret; - uac2->as_out_alt = 0; - ret = usb_interface_id(cfg, fn); - if (ret < 0) { - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); - return ret; + if (EPIN_EN(uac2_opts)) { + ret = usb_interface_id(cfg, fn); + if (ret < 0) { + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); + return ret; + } + std_as_in_if0_desc.bInterfaceNumber = ret; + std_as_in_if1_desc.bInterfaceNumber = ret; + uac2->as_in_intf = ret; + uac2->as_in_alt = 0; } - std_as_in_if0_desc.bInterfaceNumber = ret; - std_as_in_if1_desc.bInterfaceNumber = ret; - uac2->as_in_intf = ret; - uac2->as_in_alt = 0; /* Calculate wMaxPacketSize according to audio bandwidth */ set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true); @@ -556,16 +678,20 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true); set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false); - agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); - if (!agdev->out_ep) { - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); - return -ENODEV; + if (EPOUT_EN(uac2_opts)) { + agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); + if (!agdev->out_ep) { + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); + return -ENODEV; + } } - agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); - if (!agdev->in_ep) { - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); - return -ENODEV; + if (EPIN_EN(uac2_opts)) { + agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); + if (!agdev->in_ep) { + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); + return -ENODEV; + } } agdev->in_ep_maxpsize = max_t(u16, @@ -578,6 +704,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; + setup_descriptor(uac2_opts); + ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL, NULL); if (ret) -- cgit v1.2.1