| .. | .. |
|---|
| 1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
|---|
| 2 | | -/** |
|---|
| 2 | +/* |
|---|
| 3 | 3 | * udc.c - Core UDC Framework |
|---|
| 4 | 4 | * |
|---|
| 5 | 5 | * Copyright (C) 2010 Texas Instruments |
|---|
| .. | .. |
|---|
| 23 | 23 | |
|---|
| 24 | 24 | /** |
|---|
| 25 | 25 | * struct usb_udc - describes one usb device controller |
|---|
| 26 | | - * @driver - the gadget driver pointer. For use by the class code |
|---|
| 27 | | - * @dev - the child device to the actual controller |
|---|
| 28 | | - * @gadget - the gadget. For use by the class code |
|---|
| 29 | | - * @list - for use by the udc class driver |
|---|
| 30 | | - * @vbus - for udcs who care about vbus status, this value is real vbus status; |
|---|
| 26 | + * @driver: the gadget driver pointer. For use by the class code |
|---|
| 27 | + * @dev: the child device to the actual controller |
|---|
| 28 | + * @gadget: the gadget. For use by the class code |
|---|
| 29 | + * @list: for use by the udc class driver |
|---|
| 30 | + * @vbus: for udcs who care about vbus status, this value is real vbus status; |
|---|
| 31 | 31 | * for udcs who do not care about vbus status, this value is always true |
|---|
| 32 | + * @started: the UDC's started state. True if the UDC had started. |
|---|
| 32 | 33 | * |
|---|
| 33 | 34 | * This represents the internal data structure which is used by the UDC-class |
|---|
| 34 | 35 | * to hold information about udc driver and gadget together. |
|---|
| .. | .. |
|---|
| 39 | 40 | struct device dev; |
|---|
| 40 | 41 | struct list_head list; |
|---|
| 41 | 42 | bool vbus; |
|---|
| 43 | + bool started; |
|---|
| 42 | 44 | }; |
|---|
| 43 | 45 | |
|---|
| 44 | 46 | static struct class *udc_class; |
|---|
| .. | .. |
|---|
| 85 | 87 | * for interrupt transfers as well as bulk, but it likely couldn't be used |
|---|
| 86 | 88 | * for iso transfers or for endpoint 14. some endpoints are fully |
|---|
| 87 | 89 | * configurable, with more generic names like "ep-a". (remember that for |
|---|
| 88 | | - * USB, "in" means "towards the USB master".) |
|---|
| 90 | + * USB, "in" means "towards the USB host".) |
|---|
| 89 | 91 | * |
|---|
| 90 | | - * This routine must be called in process context. |
|---|
| 92 | + * This routine may be called in an atomic (interrupt) context. |
|---|
| 91 | 93 | * |
|---|
| 92 | 94 | * returns zero, or a negative error code. |
|---|
| 93 | 95 | */ |
|---|
| .. | .. |
|---|
| 132 | 134 | * gadget drivers must call usb_ep_enable() again before queueing |
|---|
| 133 | 135 | * requests to the endpoint. |
|---|
| 134 | 136 | * |
|---|
| 135 | | - * This routine must be called in process context. |
|---|
| 137 | + * This routine may be called in an atomic (interrupt) context. |
|---|
| 136 | 138 | * |
|---|
| 137 | 139 | * returns zero, or a negative error code. |
|---|
| 138 | 140 | */ |
|---|
| .. | .. |
|---|
| 292 | 294 | * @ep:the endpoint associated with the request |
|---|
| 293 | 295 | * @req:the request being canceled |
|---|
| 294 | 296 | * |
|---|
| 295 | | - * If the request is still active on the endpoint, it is dequeued and its |
|---|
| 296 | | - * completion routine is called (with status -ECONNRESET); else a negative |
|---|
| 297 | | - * error code is returned. This is guaranteed to happen before the call to |
|---|
| 298 | | - * usb_ep_dequeue() returns. |
|---|
| 297 | + * If the request is still active on the endpoint, it is dequeued and |
|---|
| 298 | + * eventually its completion routine is called (with status -ECONNRESET); |
|---|
| 299 | + * else a negative error code is returned. This routine is asynchronous, |
|---|
| 300 | + * that is, it may return before the completion routine runs. |
|---|
| 299 | 301 | * |
|---|
| 300 | 302 | * Note that some hardware can't clear out write fifos (to unlink the request |
|---|
| 301 | 303 | * at the head of the queue) except as part of disconnecting from usb. Such |
|---|
| .. | .. |
|---|
| 507 | 509 | EXPORT_SYMBOL_GPL(usb_gadget_wakeup); |
|---|
| 508 | 510 | |
|---|
| 509 | 511 | /** |
|---|
| 510 | | - * usb_gsi_ep_op - performs operation on GSI accelerated EP based on EP op code |
|---|
| 511 | | - * |
|---|
| 512 | | - * Operations such as EP configuration, TRB allocation, StartXfer etc. |
|---|
| 513 | | - * See gsi_ep_op for more details. |
|---|
| 514 | | - */ |
|---|
| 515 | | -int usb_gsi_ep_op(struct usb_ep *ep, |
|---|
| 516 | | - struct usb_gsi_request *req, enum gsi_ep_op op) |
|---|
| 517 | | -{ |
|---|
| 518 | | - if (ep && ep->ops && ep->ops->gsi_ep_op) |
|---|
| 519 | | - return ep->ops->gsi_ep_op(ep, req, op); |
|---|
| 520 | | - |
|---|
| 521 | | - return -EOPNOTSUPP; |
|---|
| 522 | | -} |
|---|
| 523 | | -EXPORT_SYMBOL_GPL(usb_gsi_ep_op); |
|---|
| 524 | | - |
|---|
| 525 | | -/** |
|---|
| 526 | | - * usb_gadget_func_wakeup - send a function remote wakeup up notification |
|---|
| 527 | | - * to the host connected to this gadget |
|---|
| 528 | | - * @gadget: controller used to wake up the host |
|---|
| 529 | | - * @interface_id: the interface which triggered the remote wakeup event |
|---|
| 530 | | - * |
|---|
| 531 | | - * Returns zero on success. Otherwise, negative error code is returned. |
|---|
| 532 | | - */ |
|---|
| 533 | | -int usb_gadget_func_wakeup(struct usb_gadget *gadget, |
|---|
| 534 | | - int interface_id) |
|---|
| 535 | | -{ |
|---|
| 536 | | - if (!gadget || (gadget->speed != USB_SPEED_SUPER)) |
|---|
| 537 | | - return -EOPNOTSUPP; |
|---|
| 538 | | - |
|---|
| 539 | | - if (!gadget->ops || !gadget->ops->func_wakeup) |
|---|
| 540 | | - return -EOPNOTSUPP; |
|---|
| 541 | | - |
|---|
| 542 | | - return gadget->ops->func_wakeup(gadget, interface_id); |
|---|
| 543 | | -} |
|---|
| 544 | | -EXPORT_SYMBOL_GPL(usb_gadget_func_wakeup); |
|---|
| 545 | | - |
|---|
| 546 | | -/** |
|---|
| 547 | 512 | * usb_gadget_set_selfpowered - sets the device selfpowered feature. |
|---|
| 548 | 513 | * @gadget:the device being declared as self-powered |
|---|
| 549 | 514 | * |
|---|
| .. | .. |
|---|
| 738 | 703 | * as a disconnect (when a VBUS session is active). Not all systems |
|---|
| 739 | 704 | * support software pullup controls. |
|---|
| 740 | 705 | * |
|---|
| 706 | + * Following a successful disconnect, invoke the ->disconnect() callback |
|---|
| 707 | + * for the current gadget driver so that UDC drivers don't need to. |
|---|
| 708 | + * |
|---|
| 741 | 709 | * Returns zero on success, else negative errno. |
|---|
| 742 | 710 | */ |
|---|
| 743 | 711 | int usb_gadget_disconnect(struct usb_gadget *gadget) |
|---|
| .. | .. |
|---|
| 749 | 717 | goto out; |
|---|
| 750 | 718 | } |
|---|
| 751 | 719 | |
|---|
| 720 | + if (!gadget->connected) |
|---|
| 721 | + goto out; |
|---|
| 722 | + |
|---|
| 752 | 723 | if (gadget->deactivated) { |
|---|
| 753 | 724 | /* |
|---|
| 754 | 725 | * If gadget is deactivated we only save new state. |
|---|
| .. | .. |
|---|
| 759 | 730 | } |
|---|
| 760 | 731 | |
|---|
| 761 | 732 | ret = gadget->ops->pullup(gadget, 0); |
|---|
| 762 | | - if (!ret) |
|---|
| 733 | + if (!ret) { |
|---|
| 763 | 734 | gadget->connected = 0; |
|---|
| 735 | + if (gadget->udc->driver) |
|---|
| 736 | + gadget->udc->driver->disconnect(gadget); |
|---|
| 737 | + } |
|---|
| 764 | 738 | |
|---|
| 765 | 739 | out: |
|---|
| 766 | 740 | trace_usb_gadget_disconnect(gadget, ret); |
|---|
| .. | .. |
|---|
| 787 | 761 | if (!gadget || gadget->deactivated) |
|---|
| 788 | 762 | goto out; |
|---|
| 789 | 763 | |
|---|
| 790 | | - if (gadget->connected && !gadget->uvc_enabled) { |
|---|
| 764 | + if (gadget->connected) { |
|---|
| 791 | 765 | ret = usb_gadget_disconnect(gadget); |
|---|
| 792 | 766 | if (ret) |
|---|
| 793 | 767 | goto out; |
|---|
| .. | .. |
|---|
| 923 | 897 | |
|---|
| 924 | 898 | /** |
|---|
| 925 | 899 | * usb_gadget_giveback_request - give the request back to the gadget layer |
|---|
| 900 | + * @ep: the endpoint to be used with with the request |
|---|
| 901 | + * @req: the request being given back |
|---|
| 902 | + * |
|---|
| 926 | 903 | * Context: in_interrupt() |
|---|
| 927 | 904 | * |
|---|
| 928 | 905 | * This is called by device controller drivers in order to return the |
|---|
| .. | .. |
|---|
| 1030 | 1007 | } |
|---|
| 1031 | 1008 | EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc); |
|---|
| 1032 | 1009 | |
|---|
| 1010 | +/** |
|---|
| 1011 | + * usb_gadget_check_config - checks if the UDC can support the binded |
|---|
| 1012 | + * configuration |
|---|
| 1013 | + * @gadget: controller to check the USB configuration |
|---|
| 1014 | + * |
|---|
| 1015 | + * Ensure that a UDC is able to support the requested resources by a |
|---|
| 1016 | + * configuration, and that there are no resource limitations, such as |
|---|
| 1017 | + * internal memory allocated to all requested endpoints. |
|---|
| 1018 | + * |
|---|
| 1019 | + * Returns zero on success, else a negative errno. |
|---|
| 1020 | + */ |
|---|
| 1021 | +int usb_gadget_check_config(struct usb_gadget *gadget) |
|---|
| 1022 | +{ |
|---|
| 1023 | + if (gadget->ops->check_config) |
|---|
| 1024 | + return gadget->ops->check_config(gadget); |
|---|
| 1025 | + return 0; |
|---|
| 1026 | +} |
|---|
| 1027 | +EXPORT_SYMBOL_GPL(usb_gadget_check_config); |
|---|
| 1028 | + |
|---|
| 1033 | 1029 | /* ------------------------------------------------------------------------- */ |
|---|
| 1034 | 1030 | |
|---|
| 1035 | 1031 | static void usb_gadget_state_work(struct work_struct *work) |
|---|
| .. | .. |
|---|
| 1111 | 1107 | */ |
|---|
| 1112 | 1108 | static inline int usb_gadget_udc_start(struct usb_udc *udc) |
|---|
| 1113 | 1109 | { |
|---|
| 1114 | | - return udc->gadget->ops->udc_start(udc->gadget, udc->driver); |
|---|
| 1110 | + int ret; |
|---|
| 1111 | + |
|---|
| 1112 | + if (udc->started) { |
|---|
| 1113 | + dev_err(&udc->dev, "UDC had already started\n"); |
|---|
| 1114 | + return -EBUSY; |
|---|
| 1115 | + } |
|---|
| 1116 | + |
|---|
| 1117 | + ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver); |
|---|
| 1118 | + if (!ret) |
|---|
| 1119 | + udc->started = true; |
|---|
| 1120 | + |
|---|
| 1121 | + return ret; |
|---|
| 1115 | 1122 | } |
|---|
| 1116 | 1123 | |
|---|
| 1117 | 1124 | /** |
|---|
| 1118 | 1125 | * usb_gadget_udc_stop - tells usb device controller we don't need it anymore |
|---|
| 1119 | | - * @gadget: The device we want to stop activity |
|---|
| 1120 | | - * @driver: The driver to unbind from @gadget |
|---|
| 1126 | + * @udc: The UDC to be stopped |
|---|
| 1121 | 1127 | * |
|---|
| 1122 | 1128 | * This call is issued by the UDC Class driver after calling |
|---|
| 1123 | 1129 | * gadget driver's unbind() method. |
|---|
| .. | .. |
|---|
| 1128 | 1134 | */ |
|---|
| 1129 | 1135 | static inline void usb_gadget_udc_stop(struct usb_udc *udc) |
|---|
| 1130 | 1136 | { |
|---|
| 1137 | + if (!udc->started) { |
|---|
| 1138 | + dev_err(&udc->dev, "UDC had already stopped\n"); |
|---|
| 1139 | + return; |
|---|
| 1140 | + } |
|---|
| 1141 | + |
|---|
| 1131 | 1142 | udc->gadget->ops->udc_stop(udc->gadget); |
|---|
| 1143 | + udc->started = false; |
|---|
| 1132 | 1144 | } |
|---|
| 1133 | 1145 | |
|---|
| 1134 | 1146 | /** |
|---|
| .. | .. |
|---|
| 1144 | 1156 | static inline void usb_gadget_udc_set_speed(struct usb_udc *udc, |
|---|
| 1145 | 1157 | enum usb_device_speed speed) |
|---|
| 1146 | 1158 | { |
|---|
| 1147 | | - if (udc->gadget->ops->udc_set_speed) { |
|---|
| 1148 | | - enum usb_device_speed s; |
|---|
| 1159 | + struct usb_gadget *gadget = udc->gadget; |
|---|
| 1160 | + enum usb_device_speed s; |
|---|
| 1149 | 1161 | |
|---|
| 1150 | | - s = min(speed, udc->gadget->max_speed); |
|---|
| 1151 | | - udc->gadget->ops->udc_set_speed(udc->gadget, s); |
|---|
| 1152 | | - } |
|---|
| 1162 | + if (speed == USB_SPEED_UNKNOWN) |
|---|
| 1163 | + s = gadget->max_speed; |
|---|
| 1164 | + else |
|---|
| 1165 | + s = min(speed, gadget->max_speed); |
|---|
| 1166 | + |
|---|
| 1167 | + if (s == USB_SPEED_SUPER_PLUS && gadget->ops->udc_set_ssp_rate) |
|---|
| 1168 | + gadget->ops->udc_set_ssp_rate(gadget, gadget->max_ssp_rate); |
|---|
| 1169 | + else if (gadget->ops->udc_set_speed) |
|---|
| 1170 | + gadget->ops->udc_set_speed(gadget, s); |
|---|
| 1171 | +} |
|---|
| 1172 | + |
|---|
| 1173 | +/** |
|---|
| 1174 | + * usb_gadget_enable_async_callbacks - tell usb device controller to enable asynchronous callbacks |
|---|
| 1175 | + * @udc: The UDC which should enable async callbacks |
|---|
| 1176 | + * |
|---|
| 1177 | + * This routine is used when binding gadget drivers. It undoes the effect |
|---|
| 1178 | + * of usb_gadget_disable_async_callbacks(); the UDC driver should enable IRQs |
|---|
| 1179 | + * (if necessary) and resume issuing callbacks. |
|---|
| 1180 | + * |
|---|
| 1181 | + * This routine will always be called in process context. |
|---|
| 1182 | + */ |
|---|
| 1183 | +static inline void usb_gadget_enable_async_callbacks(struct usb_udc *udc) |
|---|
| 1184 | +{ |
|---|
| 1185 | + struct usb_gadget *gadget = udc->gadget; |
|---|
| 1186 | + |
|---|
| 1187 | + if (gadget->ops->udc_async_callbacks) |
|---|
| 1188 | + gadget->ops->udc_async_callbacks(gadget, true); |
|---|
| 1189 | +} |
|---|
| 1190 | + |
|---|
| 1191 | +/** |
|---|
| 1192 | + * usb_gadget_disable_async_callbacks - tell usb device controller to disable asynchronous callbacks |
|---|
| 1193 | + * @udc: The UDC which should disable async callbacks |
|---|
| 1194 | + * |
|---|
| 1195 | + * This routine is used when unbinding gadget drivers. It prevents a race: |
|---|
| 1196 | + * The UDC driver doesn't know when the gadget driver's ->unbind callback |
|---|
| 1197 | + * runs, so unless it is told to disable asynchronous callbacks, it might |
|---|
| 1198 | + * issue a callback (such as ->disconnect) after the unbind has completed. |
|---|
| 1199 | + * |
|---|
| 1200 | + * After this function runs, the UDC driver must suppress all ->suspend, |
|---|
| 1201 | + * ->resume, ->disconnect, ->reset, and ->setup callbacks to the gadget driver |
|---|
| 1202 | + * until async callbacks are again enabled. A simple-minded but effective |
|---|
| 1203 | + * way to accomplish this is to tell the UDC hardware not to generate any |
|---|
| 1204 | + * more IRQs. |
|---|
| 1205 | + * |
|---|
| 1206 | + * Request completion callbacks must still be issued. However, it's okay |
|---|
| 1207 | + * to defer them until the request is cancelled, since the pull-up will be |
|---|
| 1208 | + * turned off during the time period when async callbacks are disabled. |
|---|
| 1209 | + * |
|---|
| 1210 | + * This routine will always be called in process context. |
|---|
| 1211 | + */ |
|---|
| 1212 | +static inline void usb_gadget_disable_async_callbacks(struct usb_udc *udc) |
|---|
| 1213 | +{ |
|---|
| 1214 | + struct usb_gadget *gadget = udc->gadget; |
|---|
| 1215 | + |
|---|
| 1216 | + if (gadget->ops->udc_async_callbacks) |
|---|
| 1217 | + gadget->ops->udc_async_callbacks(gadget, false); |
|---|
| 1153 | 1218 | } |
|---|
| 1154 | 1219 | |
|---|
| 1155 | 1220 | /** |
|---|
| .. | .. |
|---|
| 1194 | 1259 | } |
|---|
| 1195 | 1260 | |
|---|
| 1196 | 1261 | /** |
|---|
| 1197 | | - * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list |
|---|
| 1262 | + * usb_initialize_gadget - initialize a gadget and its embedded struct device |
|---|
| 1198 | 1263 | * @parent: the parent device to this udc. Usually the controller driver's |
|---|
| 1199 | 1264 | * device. |
|---|
| 1200 | | - * @gadget: the gadget to be added to the list. |
|---|
| 1265 | + * @gadget: the gadget to be initialized. |
|---|
| 1201 | 1266 | * @release: a gadget release function. |
|---|
| 1202 | 1267 | * |
|---|
| 1203 | 1268 | * Returns zero on success, negative errno otherwise. |
|---|
| 1204 | 1269 | * Calls the gadget release function in the latter case. |
|---|
| 1205 | 1270 | */ |
|---|
| 1206 | | -int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, |
|---|
| 1271 | +void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget, |
|---|
| 1207 | 1272 | void (*release)(struct device *dev)) |
|---|
| 1208 | 1273 | { |
|---|
| 1209 | | - struct usb_udc *udc; |
|---|
| 1210 | | - int ret = -ENOMEM; |
|---|
| 1211 | | - |
|---|
| 1212 | 1274 | dev_set_name(&gadget->dev, "gadget"); |
|---|
| 1213 | 1275 | INIT_WORK(&gadget->work, usb_gadget_state_work); |
|---|
| 1214 | 1276 | gadget->dev.parent = parent; |
|---|
| .. | .. |
|---|
| 1219 | 1281 | gadget->dev.release = usb_udc_nop_release; |
|---|
| 1220 | 1282 | |
|---|
| 1221 | 1283 | device_initialize(&gadget->dev); |
|---|
| 1284 | +} |
|---|
| 1285 | +EXPORT_SYMBOL_GPL(usb_initialize_gadget); |
|---|
| 1286 | + |
|---|
| 1287 | +/** |
|---|
| 1288 | + * usb_add_gadget - adds a new gadget to the udc class driver list |
|---|
| 1289 | + * @gadget: the gadget to be added to the list. |
|---|
| 1290 | + * |
|---|
| 1291 | + * Returns zero on success, negative errno otherwise. |
|---|
| 1292 | + * Does not do a final usb_put_gadget() if an error occurs. |
|---|
| 1293 | + */ |
|---|
| 1294 | +int usb_add_gadget(struct usb_gadget *gadget) |
|---|
| 1295 | +{ |
|---|
| 1296 | + struct usb_udc *udc; |
|---|
| 1297 | + int ret = -ENOMEM; |
|---|
| 1222 | 1298 | |
|---|
| 1223 | 1299 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); |
|---|
| 1224 | 1300 | if (!udc) |
|---|
| 1225 | | - goto err_put_gadget; |
|---|
| 1301 | + goto error; |
|---|
| 1226 | 1302 | |
|---|
| 1227 | 1303 | device_initialize(&udc->dev); |
|---|
| 1228 | 1304 | udc->dev.release = usb_udc_release; |
|---|
| 1229 | 1305 | udc->dev.class = udc_class; |
|---|
| 1230 | 1306 | udc->dev.groups = usb_udc_attr_groups; |
|---|
| 1231 | | - udc->dev.parent = parent; |
|---|
| 1232 | | - ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); |
|---|
| 1307 | + udc->dev.parent = gadget->dev.parent; |
|---|
| 1308 | + ret = dev_set_name(&udc->dev, "%s", |
|---|
| 1309 | + kobject_name(&gadget->dev.parent->kobj)); |
|---|
| 1233 | 1310 | if (ret) |
|---|
| 1234 | 1311 | goto err_put_udc; |
|---|
| 1235 | 1312 | |
|---|
| .. | .. |
|---|
| 1239 | 1316 | |
|---|
| 1240 | 1317 | udc->gadget = gadget; |
|---|
| 1241 | 1318 | gadget->udc = udc; |
|---|
| 1319 | + |
|---|
| 1320 | + udc->started = false; |
|---|
| 1242 | 1321 | |
|---|
| 1243 | 1322 | mutex_lock(&udc_lock); |
|---|
| 1244 | 1323 | list_add_tail(&udc->list, &udc_list); |
|---|
| .. | .. |
|---|
| 1260 | 1339 | return 0; |
|---|
| 1261 | 1340 | |
|---|
| 1262 | 1341 | err_del_udc: |
|---|
| 1342 | + flush_work(&gadget->work); |
|---|
| 1263 | 1343 | device_del(&udc->dev); |
|---|
| 1264 | 1344 | |
|---|
| 1265 | 1345 | err_unlist_udc: |
|---|
| .. | .. |
|---|
| 1271 | 1351 | err_put_udc: |
|---|
| 1272 | 1352 | put_device(&udc->dev); |
|---|
| 1273 | 1353 | |
|---|
| 1274 | | - err_put_gadget: |
|---|
| 1275 | | - put_device(&gadget->dev); |
|---|
| 1354 | + error: |
|---|
| 1355 | + return ret; |
|---|
| 1356 | +} |
|---|
| 1357 | +EXPORT_SYMBOL_GPL(usb_add_gadget); |
|---|
| 1358 | + |
|---|
| 1359 | +/** |
|---|
| 1360 | + * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list |
|---|
| 1361 | + * @parent: the parent device to this udc. Usually the controller driver's |
|---|
| 1362 | + * device. |
|---|
| 1363 | + * @gadget: the gadget to be added to the list. |
|---|
| 1364 | + * @release: a gadget release function. |
|---|
| 1365 | + * |
|---|
| 1366 | + * Returns zero on success, negative errno otherwise. |
|---|
| 1367 | + * Calls the gadget release function in the latter case. |
|---|
| 1368 | + */ |
|---|
| 1369 | +int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, |
|---|
| 1370 | + void (*release)(struct device *dev)) |
|---|
| 1371 | +{ |
|---|
| 1372 | + int ret; |
|---|
| 1373 | + |
|---|
| 1374 | + usb_initialize_gadget(parent, gadget, release); |
|---|
| 1375 | + ret = usb_add_gadget(gadget); |
|---|
| 1376 | + if (ret) |
|---|
| 1377 | + usb_put_gadget(gadget); |
|---|
| 1276 | 1378 | return ret; |
|---|
| 1277 | 1379 | } |
|---|
| 1278 | 1380 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); |
|---|
| .. | .. |
|---|
| 1329 | 1431 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); |
|---|
| 1330 | 1432 | |
|---|
| 1331 | 1433 | usb_gadget_disconnect(udc->gadget); |
|---|
| 1332 | | - udc->driver->disconnect(udc->gadget); |
|---|
| 1434 | + usb_gadget_disable_async_callbacks(udc); |
|---|
| 1435 | + if (udc->gadget->irq) |
|---|
| 1436 | + synchronize_irq(udc->gadget->irq); |
|---|
| 1333 | 1437 | udc->driver->unbind(udc->gadget); |
|---|
| 1334 | 1438 | usb_gadget_udc_stop(udc); |
|---|
| 1335 | 1439 | |
|---|
| 1336 | 1440 | udc->driver = NULL; |
|---|
| 1337 | | - udc->dev.driver = NULL; |
|---|
| 1338 | 1441 | udc->gadget->dev.driver = NULL; |
|---|
| 1339 | 1442 | } |
|---|
| 1340 | 1443 | |
|---|
| 1341 | 1444 | /** |
|---|
| 1342 | | - * usb_del_gadget_udc - deletes @udc from udc_list |
|---|
| 1445 | + * usb_del_gadget - deletes @udc from udc_list |
|---|
| 1343 | 1446 | * @gadget: the gadget to be removed. |
|---|
| 1344 | 1447 | * |
|---|
| 1345 | | - * This, will call usb_gadget_unregister_driver() if |
|---|
| 1448 | + * This will call usb_gadget_unregister_driver() if |
|---|
| 1346 | 1449 | * the @udc is still busy. |
|---|
| 1450 | + * It will not do a final usb_put_gadget(). |
|---|
| 1347 | 1451 | */ |
|---|
| 1348 | | -void usb_del_gadget_udc(struct usb_gadget *gadget) |
|---|
| 1452 | +void usb_del_gadget(struct usb_gadget *gadget) |
|---|
| 1349 | 1453 | { |
|---|
| 1350 | 1454 | struct usb_udc *udc = gadget->udc; |
|---|
| 1351 | 1455 | |
|---|
| .. | .. |
|---|
| 1368 | 1472 | kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); |
|---|
| 1369 | 1473 | flush_work(&gadget->work); |
|---|
| 1370 | 1474 | device_unregister(&udc->dev); |
|---|
| 1371 | | - device_unregister(&gadget->dev); |
|---|
| 1372 | | - memset(&gadget->dev, 0x00, sizeof(gadget->dev)); |
|---|
| 1475 | + device_del(&gadget->dev); |
|---|
| 1476 | +} |
|---|
| 1477 | +EXPORT_SYMBOL_GPL(usb_del_gadget); |
|---|
| 1478 | + |
|---|
| 1479 | +/** |
|---|
| 1480 | + * usb_del_gadget_udc - deletes @udc from udc_list |
|---|
| 1481 | + * @gadget: the gadget to be removed. |
|---|
| 1482 | + * |
|---|
| 1483 | + * Calls usb_del_gadget() and does a final usb_put_gadget(). |
|---|
| 1484 | + */ |
|---|
| 1485 | +void usb_del_gadget_udc(struct usb_gadget *gadget) |
|---|
| 1486 | +{ |
|---|
| 1487 | + usb_del_gadget(gadget); |
|---|
| 1488 | + usb_put_gadget(gadget); |
|---|
| 1373 | 1489 | } |
|---|
| 1374 | 1490 | EXPORT_SYMBOL_GPL(usb_del_gadget_udc); |
|---|
| 1375 | 1491 | |
|---|
| .. | .. |
|---|
| 1383 | 1499 | driver->function); |
|---|
| 1384 | 1500 | |
|---|
| 1385 | 1501 | udc->driver = driver; |
|---|
| 1386 | | - udc->dev.driver = &driver->driver; |
|---|
| 1387 | 1502 | udc->gadget->dev.driver = &driver->driver; |
|---|
| 1388 | 1503 | |
|---|
| 1389 | 1504 | usb_gadget_udc_set_speed(udc, driver->max_speed); |
|---|
| .. | .. |
|---|
| 1396 | 1511 | driver->unbind(udc->gadget); |
|---|
| 1397 | 1512 | goto err1; |
|---|
| 1398 | 1513 | } |
|---|
| 1514 | + usb_gadget_enable_async_callbacks(udc); |
|---|
| 1399 | 1515 | usb_udc_connect_control(udc); |
|---|
| 1400 | 1516 | |
|---|
| 1401 | 1517 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); |
|---|
| .. | .. |
|---|
| 1405 | 1521 | dev_err(&udc->dev, "failed to start %s: %d\n", |
|---|
| 1406 | 1522 | udc->driver->function, ret); |
|---|
| 1407 | 1523 | udc->driver = NULL; |
|---|
| 1408 | | - udc->dev.driver = NULL; |
|---|
| 1409 | 1524 | udc->gadget->dev.driver = NULL; |
|---|
| 1410 | 1525 | return ret; |
|---|
| 1411 | 1526 | } |
|---|
| .. | .. |
|---|
| 1447 | 1562 | } |
|---|
| 1448 | 1563 | |
|---|
| 1449 | 1564 | mutex_unlock(&udc_lock); |
|---|
| 1565 | + if (ret) |
|---|
| 1566 | + pr_warn("udc-core: couldn't find an available UDC or it's busy\n"); |
|---|
| 1450 | 1567 | return ret; |
|---|
| 1451 | 1568 | found: |
|---|
| 1452 | 1569 | ret = udc_bind_to_driver(udc, driver); |
|---|
| .. | .. |
|---|
| 1522 | 1639 | usb_gadget_connect(udc->gadget); |
|---|
| 1523 | 1640 | } else if (sysfs_streq(buf, "disconnect")) { |
|---|
| 1524 | 1641 | usb_gadget_disconnect(udc->gadget); |
|---|
| 1525 | | - udc->driver->disconnect(udc->gadget); |
|---|
| 1526 | 1642 | usb_gadget_udc_stop(udc); |
|---|
| 1527 | 1643 | } else { |
|---|
| 1528 | 1644 | dev_err(dev, "unsupported command '%s'\n", buf); |
|---|