Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Provides code common for host and device side USB.
0004  *
0005  * If either host side (ie. CONFIG_USB=y) or device side USB stack
0006  * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
0007  * compiled-in as well.  Otherwise, if either of the two stacks is
0008  * compiled as module, this file is compiled as module as well.
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/usb/ch9.h>
0015 #include <linux/usb/of.h>
0016 #include <linux/usb/otg.h>
0017 #include <linux/of_platform.h>
0018 #include <linux/debugfs.h>
0019 #include "common.h"
0020 
0021 static const char *const ep_type_names[] = {
0022     [USB_ENDPOINT_XFER_CONTROL] = "ctrl",
0023     [USB_ENDPOINT_XFER_ISOC] = "isoc",
0024     [USB_ENDPOINT_XFER_BULK] = "bulk",
0025     [USB_ENDPOINT_XFER_INT] = "intr",
0026 };
0027 
0028 /**
0029  * usb_ep_type_string() - Returns human readable-name of the endpoint type.
0030  * @ep_type: The endpoint type to return human-readable name for.  If it's not
0031  *   any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT},
0032  *   usually got by usb_endpoint_type(), the string 'unknown' will be returned.
0033  */
0034 const char *usb_ep_type_string(int ep_type)
0035 {
0036     if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
0037         return "unknown";
0038 
0039     return ep_type_names[ep_type];
0040 }
0041 EXPORT_SYMBOL_GPL(usb_ep_type_string);
0042 
0043 const char *usb_otg_state_string(enum usb_otg_state state)
0044 {
0045     static const char *const names[] = {
0046         [OTG_STATE_A_IDLE] = "a_idle",
0047         [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
0048         [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
0049         [OTG_STATE_A_HOST] = "a_host",
0050         [OTG_STATE_A_SUSPEND] = "a_suspend",
0051         [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
0052         [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
0053         [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
0054         [OTG_STATE_B_IDLE] = "b_idle",
0055         [OTG_STATE_B_SRP_INIT] = "b_srp_init",
0056         [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
0057         [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
0058         [OTG_STATE_B_HOST] = "b_host",
0059     };
0060 
0061     if (state < 0 || state >= ARRAY_SIZE(names))
0062         return "UNDEFINED";
0063 
0064     return names[state];
0065 }
0066 EXPORT_SYMBOL_GPL(usb_otg_state_string);
0067 
0068 static const char *const speed_names[] = {
0069     [USB_SPEED_UNKNOWN] = "UNKNOWN",
0070     [USB_SPEED_LOW] = "low-speed",
0071     [USB_SPEED_FULL] = "full-speed",
0072     [USB_SPEED_HIGH] = "high-speed",
0073     [USB_SPEED_WIRELESS] = "wireless",
0074     [USB_SPEED_SUPER] = "super-speed",
0075     [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
0076 };
0077 
0078 static const char *const ssp_rate[] = {
0079     [USB_SSP_GEN_UNKNOWN] = "UNKNOWN",
0080     [USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1",
0081     [USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2",
0082     [USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2",
0083 };
0084 
0085 /**
0086  * usb_speed_string() - Returns human readable-name of the speed.
0087  * @speed: The speed to return human-readable name for.  If it's not
0088  *   any of the speeds defined in usb_device_speed enum, string for
0089  *   USB_SPEED_UNKNOWN will be returned.
0090  */
0091 const char *usb_speed_string(enum usb_device_speed speed)
0092 {
0093     if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
0094         speed = USB_SPEED_UNKNOWN;
0095     return speed_names[speed];
0096 }
0097 EXPORT_SYMBOL_GPL(usb_speed_string);
0098 
0099 /**
0100  * usb_get_maximum_speed - Get maximum requested speed for a given USB
0101  * controller.
0102  * @dev: Pointer to the given USB controller device
0103  *
0104  * The function gets the maximum speed string from property "maximum-speed",
0105  * and returns the corresponding enum usb_device_speed.
0106  */
0107 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
0108 {
0109     const char *maximum_speed;
0110     int ret;
0111 
0112     ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
0113     if (ret < 0)
0114         return USB_SPEED_UNKNOWN;
0115 
0116     ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
0117     if (ret > 0)
0118         return USB_SPEED_SUPER_PLUS;
0119 
0120     ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
0121     return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
0122 }
0123 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
0124 
0125 /**
0126  * usb_get_maximum_ssp_rate - Get the signaling rate generation and lane count
0127  *  of a SuperSpeed Plus capable device.
0128  * @dev: Pointer to the given USB controller device
0129  *
0130  * If the string from "maximum-speed" property is super-speed-plus-genXxY where
0131  * 'X' is the generation number and 'Y' is the number of lanes, then this
0132  * function returns the corresponding enum usb_ssp_rate.
0133  */
0134 enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
0135 {
0136     const char *maximum_speed;
0137     int ret;
0138 
0139     ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
0140     if (ret < 0)
0141         return USB_SSP_GEN_UNKNOWN;
0142 
0143     ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
0144     return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
0145 }
0146 EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
0147 
0148 /**
0149  * usb_state_string - Returns human readable name for the state.
0150  * @state: The state to return a human-readable name for. If it's not
0151  *  any of the states devices in usb_device_state_string enum,
0152  *  the string UNKNOWN will be returned.
0153  */
0154 const char *usb_state_string(enum usb_device_state state)
0155 {
0156     static const char *const names[] = {
0157         [USB_STATE_NOTATTACHED] = "not attached",
0158         [USB_STATE_ATTACHED] = "attached",
0159         [USB_STATE_POWERED] = "powered",
0160         [USB_STATE_RECONNECTING] = "reconnecting",
0161         [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
0162         [USB_STATE_DEFAULT] = "default",
0163         [USB_STATE_ADDRESS] = "addressed",
0164         [USB_STATE_CONFIGURED] = "configured",
0165         [USB_STATE_SUSPENDED] = "suspended",
0166     };
0167 
0168     if (state < 0 || state >= ARRAY_SIZE(names))
0169         return "UNKNOWN";
0170 
0171     return names[state];
0172 }
0173 EXPORT_SYMBOL_GPL(usb_state_string);
0174 
0175 static const char *const usb_dr_modes[] = {
0176     [USB_DR_MODE_UNKNOWN]       = "",
0177     [USB_DR_MODE_HOST]      = "host",
0178     [USB_DR_MODE_PERIPHERAL]    = "peripheral",
0179     [USB_DR_MODE_OTG]       = "otg",
0180 };
0181 
0182 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
0183 {
0184     int ret;
0185 
0186     ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
0187     return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
0188 }
0189 
0190 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
0191 {
0192     const char *dr_mode;
0193     int err;
0194 
0195     err = device_property_read_string(dev, "dr_mode", &dr_mode);
0196     if (err < 0)
0197         return USB_DR_MODE_UNKNOWN;
0198 
0199     return usb_get_dr_mode_from_string(dr_mode);
0200 }
0201 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
0202 
0203 /**
0204  * usb_get_role_switch_default_mode - Get default mode for given device
0205  * @dev: Pointer to the given device
0206  *
0207  * The function gets string from property 'role-switch-default-mode',
0208  * and returns the corresponding enum usb_dr_mode.
0209  */
0210 enum usb_dr_mode usb_get_role_switch_default_mode(struct device *dev)
0211 {
0212     const char *str;
0213     int ret;
0214 
0215     ret = device_property_read_string(dev, "role-switch-default-mode", &str);
0216     if (ret < 0)
0217         return USB_DR_MODE_UNKNOWN;
0218 
0219     return usb_get_dr_mode_from_string(str);
0220 }
0221 EXPORT_SYMBOL_GPL(usb_get_role_switch_default_mode);
0222 
0223 /**
0224  * usb_decode_interval - Decode bInterval into the time expressed in 1us unit
0225  * @epd: The descriptor of the endpoint
0226  * @speed: The speed that the endpoint works as
0227  *
0228  * Function returns the interval expressed in 1us unit for servicing
0229  * endpoint for data transfers.
0230  */
0231 unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd,
0232                  enum usb_device_speed speed)
0233 {
0234     unsigned int interval = 0;
0235 
0236     switch (usb_endpoint_type(epd)) {
0237     case USB_ENDPOINT_XFER_CONTROL:
0238         /* uframes per NAK */
0239         if (speed == USB_SPEED_HIGH)
0240             interval = epd->bInterval;
0241         break;
0242     case USB_ENDPOINT_XFER_ISOC:
0243         interval = 1 << (epd->bInterval - 1);
0244         break;
0245     case USB_ENDPOINT_XFER_BULK:
0246         /* uframes per NAK */
0247         if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd))
0248             interval = epd->bInterval;
0249         break;
0250     case USB_ENDPOINT_XFER_INT:
0251         if (speed >= USB_SPEED_HIGH)
0252             interval = 1 << (epd->bInterval - 1);
0253         else
0254             interval = epd->bInterval;
0255         break;
0256     }
0257 
0258     interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000;
0259 
0260     return interval;
0261 }
0262 EXPORT_SYMBOL_GPL(usb_decode_interval);
0263 
0264 #ifdef CONFIG_OF
0265 /**
0266  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
0267  * which is associated with the given phy device_node
0268  * @np: Pointer to the given phy device_node
0269  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
0270  *        phys which do not have phy-cells
0271  *
0272  * In dts a usb controller associates with phy devices.  The function gets
0273  * the string from property 'dr_mode' of the controller associated with the
0274  * given phy device node, and returns the correspondig enum usb_dr_mode.
0275  */
0276 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
0277 {
0278     struct device_node *controller = NULL;
0279     struct of_phandle_args args;
0280     const char *dr_mode;
0281     int index;
0282     int err;
0283 
0284     do {
0285         controller = of_find_node_with_property(controller, "phys");
0286         if (!of_device_is_available(controller))
0287             continue;
0288         index = 0;
0289         do {
0290             if (arg0 == -1) {
0291                 args.np = of_parse_phandle(controller, "phys",
0292                             index);
0293                 args.args_count = 0;
0294             } else {
0295                 err = of_parse_phandle_with_args(controller,
0296                             "phys", "#phy-cells",
0297                             index, &args);
0298                 if (err)
0299                     break;
0300             }
0301 
0302             of_node_put(args.np);
0303             if (args.np == np && (args.args_count == 0 ||
0304                           args.args[0] == arg0))
0305                 goto finish;
0306             index++;
0307         } while (args.np);
0308     } while (controller);
0309 
0310 finish:
0311     err = of_property_read_string(controller, "dr_mode", &dr_mode);
0312     of_node_put(controller);
0313 
0314     if (err < 0)
0315         return USB_DR_MODE_UNKNOWN;
0316 
0317     return usb_get_dr_mode_from_string(dr_mode);
0318 }
0319 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
0320 
0321 /**
0322  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
0323  * for given targeted hosts (non-PC hosts)
0324  * @np: Pointer to the given device_node
0325  *
0326  * The function gets if the targeted hosts support TPL or not
0327  */
0328 bool of_usb_host_tpl_support(struct device_node *np)
0329 {
0330     return of_property_read_bool(np, "tpl-support");
0331 }
0332 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
0333 
0334 /**
0335  * of_usb_update_otg_caps - to update usb otg capabilities according to
0336  * the passed properties in DT.
0337  * @np: Pointer to the given device_node
0338  * @otg_caps: Pointer to the target usb_otg_caps to be set
0339  *
0340  * The function updates the otg capabilities
0341  */
0342 int of_usb_update_otg_caps(struct device_node *np,
0343             struct usb_otg_caps *otg_caps)
0344 {
0345     u32 otg_rev;
0346 
0347     if (!otg_caps)
0348         return -EINVAL;
0349 
0350     if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
0351         switch (otg_rev) {
0352         case 0x0100:
0353         case 0x0120:
0354         case 0x0130:
0355         case 0x0200:
0356             /* Choose the lesser one if it's already been set */
0357             if (otg_caps->otg_rev)
0358                 otg_caps->otg_rev = min_t(u16, otg_rev,
0359                             otg_caps->otg_rev);
0360             else
0361                 otg_caps->otg_rev = otg_rev;
0362             break;
0363         default:
0364             pr_err("%pOF: unsupported otg-rev: 0x%x\n",
0365                         np, otg_rev);
0366             return -EINVAL;
0367         }
0368     } else {
0369         /*
0370          * otg-rev is mandatory for otg properties, if not passed
0371          * we set it to be 0 and assume it's a legacy otg device.
0372          * Non-dt platform can set it afterwards.
0373          */
0374         otg_caps->otg_rev = 0;
0375     }
0376 
0377     if (of_property_read_bool(np, "hnp-disable"))
0378         otg_caps->hnp_support = false;
0379     if (of_property_read_bool(np, "srp-disable"))
0380         otg_caps->srp_support = false;
0381     if (of_property_read_bool(np, "adp-disable") ||
0382                 (otg_caps->otg_rev < 0x0200))
0383         otg_caps->adp_support = false;
0384 
0385     return 0;
0386 }
0387 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
0388 
0389 /**
0390  * usb_of_get_companion_dev - Find the companion device
0391  * @dev: the device pointer to find a companion
0392  *
0393  * Find the companion device from platform bus.
0394  *
0395  * Takes a reference to the returned struct device which needs to be dropped
0396  * after use.
0397  *
0398  * Return: On success, a pointer to the companion device, %NULL on failure.
0399  */
0400 struct device *usb_of_get_companion_dev(struct device *dev)
0401 {
0402     struct device_node *node;
0403     struct platform_device *pdev = NULL;
0404 
0405     node = of_parse_phandle(dev->of_node, "companion", 0);
0406     if (node)
0407         pdev = of_find_device_by_node(node);
0408 
0409     of_node_put(node);
0410 
0411     return pdev ? &pdev->dev : NULL;
0412 }
0413 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
0414 #endif
0415 
0416 struct dentry *usb_debug_root;
0417 EXPORT_SYMBOL_GPL(usb_debug_root);
0418 
0419 static int __init usb_common_init(void)
0420 {
0421     usb_debug_root = debugfs_create_dir("usb", NULL);
0422     ledtrig_usb_init();
0423     return 0;
0424 }
0425 
0426 static void __exit usb_common_exit(void)
0427 {
0428     ledtrig_usb_exit();
0429     debugfs_remove_recursive(usb_debug_root);
0430 }
0431 
0432 subsys_initcall(usb_common_init);
0433 module_exit(usb_common_exit);
0434 
0435 MODULE_LICENSE("GPL");