Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 1 | /** |
| 2 | * udc.c - Core UDC Framework |
| 3 | * |
| 4 | * Copyright (C) 2010 Texas Instruments |
| 5 | * Author: Felipe Balbi <balbi@ti.com> |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 of |
| 9 | * the License as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/err.h> |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 27 | |
| 28 | #include <linux/usb/ch9.h> |
| 29 | #include <linux/usb/gadget.h> |
Michal Sojka | 0cfbd32 | 2014-09-24 22:43:21 +0200 | [diff] [blame] | 30 | #include <linux/usb.h> |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * struct usb_udc - describes one usb device controller |
| 34 | * @driver - the gadget driver pointer. For use by the class code |
| 35 | * @dev - the child device to the actual controller |
| 36 | * @gadget - the gadget. For use by the class code |
| 37 | * @list - for use by the udc class driver |
Peter Chen | 628ef0d | 2015-03-06 10:36:03 +0800 | [diff] [blame] | 38 | * @vbus - for udcs who care about vbus status, this value is real vbus status; |
| 39 | * for udcs who do not care about vbus status, this value is always true |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 40 | * |
| 41 | * This represents the internal data structure which is used by the UDC-class |
| 42 | * to hold information about udc driver and gadget together. |
| 43 | */ |
| 44 | struct usb_udc { |
| 45 | struct usb_gadget_driver *driver; |
| 46 | struct usb_gadget *gadget; |
| 47 | struct device dev; |
| 48 | struct list_head list; |
Peter Chen | 628ef0d | 2015-03-06 10:36:03 +0800 | [diff] [blame] | 49 | bool vbus; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static struct class *udc_class; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 53 | static LIST_HEAD(udc_list); |
| 54 | static DEFINE_MUTEX(udc_lock); |
| 55 | |
| 56 | /* ------------------------------------------------------------------------- */ |
| 57 | |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 58 | #ifdef CONFIG_HAS_DMA |
| 59 | |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 60 | int usb_gadget_map_request(struct usb_gadget *gadget, |
| 61 | struct usb_request *req, int is_in) |
| 62 | { |
Yoshihiro Shimoda | 7ace8fc | 2015-07-13 18:10:05 +0900 | [diff] [blame] | 63 | struct device *dev = gadget->dev.parent; |
| 64 | |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 65 | if (req->length == 0) |
| 66 | return 0; |
| 67 | |
| 68 | if (req->num_sgs) { |
| 69 | int mapped; |
| 70 | |
Yoshihiro Shimoda | 7ace8fc | 2015-07-13 18:10:05 +0900 | [diff] [blame] | 71 | mapped = dma_map_sg(dev, req->sg, req->num_sgs, |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 72 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 73 | if (mapped == 0) { |
| 74 | dev_err(&gadget->dev, "failed to map SGs\n"); |
| 75 | return -EFAULT; |
| 76 | } |
| 77 | |
| 78 | req->num_mapped_sgs = mapped; |
| 79 | } else { |
Yoshihiro Shimoda | 7ace8fc | 2015-07-13 18:10:05 +0900 | [diff] [blame] | 80 | req->dma = dma_map_single(dev, req->buf, req->length, |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 81 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 82 | |
Yoshihiro Shimoda | 7ace8fc | 2015-07-13 18:10:05 +0900 | [diff] [blame] | 83 | if (dma_mapping_error(dev, req->dma)) { |
| 84 | dev_err(dev, "failed to map buffer\n"); |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 85 | return -EFAULT; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(usb_gadget_map_request); |
| 92 | |
| 93 | void usb_gadget_unmap_request(struct usb_gadget *gadget, |
| 94 | struct usb_request *req, int is_in) |
| 95 | { |
| 96 | if (req->length == 0) |
| 97 | return; |
| 98 | |
| 99 | if (req->num_mapped_sgs) { |
Yoshihiro Shimoda | 7ace8fc | 2015-07-13 18:10:05 +0900 | [diff] [blame] | 100 | dma_unmap_sg(gadget->dev.parent, req->sg, req->num_mapped_sgs, |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 101 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 102 | |
| 103 | req->num_mapped_sgs = 0; |
| 104 | } else { |
Yoshihiro Shimoda | 7ace8fc | 2015-07-13 18:10:05 +0900 | [diff] [blame] | 105 | dma_unmap_single(gadget->dev.parent, req->dma, req->length, |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 106 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 107 | } |
| 108 | } |
| 109 | EXPORT_SYMBOL_GPL(usb_gadget_unmap_request); |
| 110 | |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 111 | #endif /* CONFIG_HAS_DMA */ |
| 112 | |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 113 | /* ------------------------------------------------------------------------- */ |
| 114 | |
Michal Sojka | 3fc2aa5 | 2014-09-24 22:43:18 +0200 | [diff] [blame] | 115 | /** |
| 116 | * usb_gadget_giveback_request - give the request back to the gadget layer |
| 117 | * Context: in_interrupt() |
| 118 | * |
| 119 | * This is called by device controller drivers in order to return the |
| 120 | * completed request back to the gadget layer. |
| 121 | */ |
| 122 | void usb_gadget_giveback_request(struct usb_ep *ep, |
| 123 | struct usb_request *req) |
| 124 | { |
Michal Sojka | 0cfbd32 | 2014-09-24 22:43:21 +0200 | [diff] [blame] | 125 | if (likely(req->status == 0)) |
| 126 | usb_led_activity(USB_LED_EVENT_GADGET); |
| 127 | |
Michal Sojka | 3fc2aa5 | 2014-09-24 22:43:18 +0200 | [diff] [blame] | 128 | req->complete(ep, req); |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(usb_gadget_giveback_request); |
| 131 | |
| 132 | /* ------------------------------------------------------------------------- */ |
| 133 | |
Robert Baldyga | b0aea00 | 2015-08-06 14:11:12 +0200 | [diff] [blame] | 134 | /** |
| 135 | * gadget_find_ep_by_name - returns ep whose name is the same as sting passed |
| 136 | * in second parameter or NULL if searched endpoint not found |
| 137 | * @g: controller to check for quirk |
| 138 | * @name: name of searched endpoint |
| 139 | */ |
| 140 | struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name) |
| 141 | { |
| 142 | struct usb_ep *ep; |
| 143 | |
| 144 | gadget_for_each_ep(ep, g) { |
| 145 | if (!strcmp(ep->name, name)) |
| 146 | return ep; |
| 147 | } |
| 148 | |
| 149 | return NULL; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(gadget_find_ep_by_name); |
| 152 | |
| 153 | /* ------------------------------------------------------------------------- */ |
| 154 | |
Robert Baldyga | 4278c68 | 2015-08-06 14:11:11 +0200 | [diff] [blame] | 155 | int usb_gadget_ep_match_desc(struct usb_gadget *gadget, |
| 156 | struct usb_ep *ep, struct usb_endpoint_descriptor *desc, |
| 157 | struct usb_ss_ep_comp_descriptor *ep_comp) |
| 158 | { |
| 159 | u8 type; |
| 160 | u16 max; |
| 161 | int num_req_streams = 0; |
| 162 | |
| 163 | /* endpoint already claimed? */ |
| 164 | if (ep->claimed) |
| 165 | return 0; |
| 166 | |
| 167 | type = usb_endpoint_type(desc); |
| 168 | max = 0x7ff & usb_endpoint_maxp(desc); |
| 169 | |
| 170 | if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in) |
| 171 | return 0; |
| 172 | if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out) |
| 173 | return 0; |
| 174 | |
| 175 | if (max > ep->maxpacket_limit) |
| 176 | return 0; |
| 177 | |
| 178 | /* "high bandwidth" works only at high speed */ |
| 179 | if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp(desc) & (3<<11)) |
| 180 | return 0; |
| 181 | |
| 182 | switch (type) { |
| 183 | case USB_ENDPOINT_XFER_CONTROL: |
| 184 | /* only support ep0 for portable CONTROL traffic */ |
| 185 | return 0; |
| 186 | case USB_ENDPOINT_XFER_ISOC: |
| 187 | if (!ep->caps.type_iso) |
| 188 | return 0; |
| 189 | /* ISO: limit 1023 bytes full speed, 1024 high/super speed */ |
| 190 | if (!gadget_is_dualspeed(gadget) && max > 1023) |
| 191 | return 0; |
| 192 | break; |
| 193 | case USB_ENDPOINT_XFER_BULK: |
| 194 | if (!ep->caps.type_bulk) |
| 195 | return 0; |
| 196 | if (ep_comp && gadget_is_superspeed(gadget)) { |
| 197 | /* Get the number of required streams from the |
| 198 | * EP companion descriptor and see if the EP |
| 199 | * matches it |
| 200 | */ |
| 201 | num_req_streams = ep_comp->bmAttributes & 0x1f; |
| 202 | if (num_req_streams > ep->max_streams) |
| 203 | return 0; |
| 204 | } |
| 205 | break; |
| 206 | case USB_ENDPOINT_XFER_INT: |
| 207 | /* Bulk endpoints handle interrupt transfers, |
| 208 | * except the toggle-quirky iso-synch kind |
| 209 | */ |
| 210 | if (!ep->caps.type_int && !ep->caps.type_bulk) |
| 211 | return 0; |
| 212 | /* INT: limit 64 bytes full speed, 1024 high/super speed */ |
| 213 | if (!gadget_is_dualspeed(gadget) && max > 64) |
| 214 | return 0; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | return 1; |
| 219 | } |
| 220 | EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc); |
| 221 | |
| 222 | /* ------------------------------------------------------------------------- */ |
| 223 | |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 224 | static void usb_gadget_state_work(struct work_struct *work) |
| 225 | { |
Peter Chen | dfea9c9 | 2015-03-06 10:36:02 +0800 | [diff] [blame] | 226 | struct usb_gadget *gadget = work_to_gadget(work); |
| 227 | struct usb_udc *udc = gadget->udc; |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 228 | |
Peter Chen | dfea9c9 | 2015-03-06 10:36:02 +0800 | [diff] [blame] | 229 | if (udc) |
| 230 | sysfs_notify(&udc->dev.kobj, NULL, "state"); |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 231 | } |
| 232 | |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 233 | void usb_gadget_set_state(struct usb_gadget *gadget, |
| 234 | enum usb_device_state state) |
| 235 | { |
| 236 | gadget->state = state; |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 237 | schedule_work(&gadget->work); |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(usb_gadget_set_state); |
| 240 | |
| 241 | /* ------------------------------------------------------------------------- */ |
| 242 | |
Peter Chen | 628ef0d | 2015-03-06 10:36:03 +0800 | [diff] [blame] | 243 | static void usb_udc_connect_control(struct usb_udc *udc) |
| 244 | { |
| 245 | if (udc->vbus) |
| 246 | usb_gadget_connect(udc->gadget); |
| 247 | else |
| 248 | usb_gadget_disconnect(udc->gadget); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * usb_udc_vbus_handler - updates the udc core vbus status, and try to |
| 253 | * connect or disconnect gadget |
| 254 | * @gadget: The gadget which vbus change occurs |
| 255 | * @status: The vbus status |
| 256 | * |
| 257 | * The udc driver calls it when it wants to connect or disconnect gadget |
| 258 | * according to vbus status. |
| 259 | */ |
| 260 | void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status) |
| 261 | { |
| 262 | struct usb_udc *udc = gadget->udc; |
| 263 | |
| 264 | if (udc) { |
| 265 | udc->vbus = status; |
| 266 | usb_udc_connect_control(udc); |
| 267 | } |
| 268 | } |
| 269 | EXPORT_SYMBOL_GPL(usb_udc_vbus_handler); |
| 270 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 271 | /** |
Peter Chen | 974a70b | 2014-09-12 09:32:41 +0800 | [diff] [blame] | 272 | * usb_gadget_udc_reset - notifies the udc core that bus reset occurs |
| 273 | * @gadget: The gadget which bus reset occurs |
| 274 | * @driver: The gadget driver we want to notify |
| 275 | * |
| 276 | * If the udc driver has bus reset handler, it needs to call this when the bus |
| 277 | * reset occurs, it notifies the gadget driver that the bus reset occurs as |
| 278 | * well as updates gadget state. |
| 279 | */ |
| 280 | void usb_gadget_udc_reset(struct usb_gadget *gadget, |
| 281 | struct usb_gadget_driver *driver) |
| 282 | { |
| 283 | driver->reset(gadget); |
| 284 | usb_gadget_set_state(gadget, USB_STATE_DEFAULT); |
| 285 | } |
| 286 | EXPORT_SYMBOL_GPL(usb_gadget_udc_reset); |
| 287 | |
| 288 | /** |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 289 | * usb_gadget_udc_start - tells usb device controller to start up |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 290 | * @udc: The UDC to be started |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 291 | * |
| 292 | * This call is issued by the UDC Class driver when it's about |
| 293 | * to register a gadget driver to the device controller, before |
| 294 | * calling gadget driver's bind() method. |
| 295 | * |
| 296 | * It allows the controller to be powered off until strictly |
| 297 | * necessary to have it powered on. |
| 298 | * |
| 299 | * Returns zero on success, else negative errno. |
| 300 | */ |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 301 | static inline int usb_gadget_udc_start(struct usb_udc *udc) |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 302 | { |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 303 | return udc->gadget->ops->udc_start(udc->gadget, udc->driver); |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 307 | * usb_gadget_udc_stop - tells usb device controller we don't need it anymore |
| 308 | * @gadget: The device we want to stop activity |
| 309 | * @driver: The driver to unbind from @gadget |
| 310 | * |
| 311 | * This call is issued by the UDC Class driver after calling |
| 312 | * gadget driver's unbind() method. |
| 313 | * |
| 314 | * The details are implementation specific, but it can go as |
| 315 | * far as powering off UDC completely and disable its data |
| 316 | * line pullups. |
| 317 | */ |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 318 | static inline void usb_gadget_udc_stop(struct usb_udc *udc) |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 319 | { |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 320 | udc->gadget->ops->udc_stop(udc->gadget); |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 324 | * usb_udc_release - release the usb_udc struct |
| 325 | * @dev: the dev member within usb_udc |
| 326 | * |
| 327 | * This is called by driver's core in order to free memory once the last |
| 328 | * reference is released. |
| 329 | */ |
| 330 | static void usb_udc_release(struct device *dev) |
| 331 | { |
| 332 | struct usb_udc *udc; |
| 333 | |
| 334 | udc = container_of(dev, struct usb_udc, dev); |
| 335 | dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); |
| 336 | kfree(udc); |
| 337 | } |
| 338 | |
Sebastian Andrzej Siewior | 019f976 | 2011-06-23 14:28:38 +0200 | [diff] [blame] | 339 | static const struct attribute_group *usb_udc_attr_groups[]; |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 340 | |
| 341 | static void usb_udc_nop_release(struct device *dev) |
| 342 | { |
| 343 | dev_vdbg(dev, "%s\n", __func__); |
| 344 | } |
| 345 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 346 | /** |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 347 | * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list |
| 348 | * @parent: the parent device to this udc. Usually the controller driver's |
| 349 | * device. |
| 350 | * @gadget: the gadget to be added to the list. |
| 351 | * @release: a gadget release function. |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 352 | * |
| 353 | * Returns zero on success, negative errno otherwise. |
| 354 | */ |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 355 | int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, |
| 356 | void (*release)(struct device *dev)) |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 357 | { |
| 358 | struct usb_udc *udc; |
| 359 | int ret = -ENOMEM; |
| 360 | |
| 361 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); |
| 362 | if (!udc) |
| 363 | goto err1; |
| 364 | |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 365 | dev_set_name(&gadget->dev, "gadget"); |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 366 | INIT_WORK(&gadget->work, usb_gadget_state_work); |
Felipe Balbi | 2ed1432 | 2013-02-26 10:59:41 +0200 | [diff] [blame] | 367 | gadget->dev.parent = parent; |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 368 | |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 369 | #ifdef CONFIG_HAS_DMA |
Felipe Balbi | 658c8cf | 2013-02-26 10:50:42 +0200 | [diff] [blame] | 370 | dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask); |
| 371 | gadget->dev.dma_parms = parent->dma_parms; |
| 372 | gadget->dev.dma_mask = parent->dma_mask; |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 373 | #endif |
Felipe Balbi | 658c8cf | 2013-02-26 10:50:42 +0200 | [diff] [blame] | 374 | |
Felipe Balbi | ddf47cc | 2013-02-26 15:25:41 +0200 | [diff] [blame] | 375 | if (release) |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 376 | gadget->dev.release = release; |
Felipe Balbi | ddf47cc | 2013-02-26 15:25:41 +0200 | [diff] [blame] | 377 | else |
| 378 | gadget->dev.release = usb_udc_nop_release; |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 379 | |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 380 | ret = device_register(&gadget->dev); |
| 381 | if (ret) |
| 382 | goto err2; |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 383 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 384 | device_initialize(&udc->dev); |
| 385 | udc->dev.release = usb_udc_release; |
| 386 | udc->dev.class = udc_class; |
Sebastian Andrzej Siewior | 019f976 | 2011-06-23 14:28:38 +0200 | [diff] [blame] | 387 | udc->dev.groups = usb_udc_attr_groups; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 388 | udc->dev.parent = parent; |
| 389 | ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); |
| 390 | if (ret) |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 391 | goto err3; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 392 | |
| 393 | udc->gadget = gadget; |
Peter Chen | dfea9c9 | 2015-03-06 10:36:02 +0800 | [diff] [blame] | 394 | gadget->udc = udc; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 395 | |
| 396 | mutex_lock(&udc_lock); |
| 397 | list_add_tail(&udc->list, &udc_list); |
| 398 | |
| 399 | ret = device_add(&udc->dev); |
| 400 | if (ret) |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 401 | goto err4; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 402 | |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 403 | usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); |
Peter Chen | 628ef0d | 2015-03-06 10:36:03 +0800 | [diff] [blame] | 404 | udc->vbus = true; |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 405 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 406 | mutex_unlock(&udc_lock); |
| 407 | |
| 408 | return 0; |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 409 | |
| 410 | err4: |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 411 | list_del(&udc->list); |
| 412 | mutex_unlock(&udc_lock); |
| 413 | |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 414 | err3: |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 415 | put_device(&udc->dev); |
Alan Stern | c93e64e | 2015-01-16 11:32:51 -0500 | [diff] [blame] | 416 | device_del(&gadget->dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 417 | |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 418 | err2: |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 419 | put_device(&gadget->dev); |
Felipe Balbi | c5dbc22 | 2013-04-02 17:06:28 +0300 | [diff] [blame] | 420 | kfree(udc); |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 421 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 422 | err1: |
| 423 | return ret; |
| 424 | } |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 425 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); |
| 426 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 427 | /** |
| 428 | * usb_add_gadget_udc - adds a new gadget to the udc class driver list |
| 429 | * @parent: the parent device to this udc. Usually the controller |
| 430 | * driver's device. |
| 431 | * @gadget: the gadget to be added to the list |
| 432 | * |
| 433 | * Returns zero on success, negative errno otherwise. |
| 434 | */ |
| 435 | int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) |
| 436 | { |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 437 | return usb_add_gadget_udc_release(parent, gadget, NULL); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 438 | } |
| 439 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc); |
| 440 | |
| 441 | static void usb_gadget_remove_driver(struct usb_udc *udc) |
| 442 | { |
| 443 | dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n", |
Felipe Balbi | 8da9fe8 | 2014-10-17 11:54:46 -0500 | [diff] [blame] | 444 | udc->driver->function); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 445 | |
| 446 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); |
| 447 | |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 448 | usb_gadget_disconnect(udc->gadget); |
| 449 | udc->driver->disconnect(udc->gadget); |
| 450 | udc->driver->unbind(udc->gadget); |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 451 | usb_gadget_udc_stop(udc); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 452 | |
| 453 | udc->driver = NULL; |
| 454 | udc->dev.driver = NULL; |
Felipe Balbi | 70d3a49 | 2013-02-26 13:51:24 +0200 | [diff] [blame] | 455 | udc->gadget->dev.driver = NULL; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| 459 | * usb_del_gadget_udc - deletes @udc from udc_list |
| 460 | * @gadget: the gadget to be removed. |
| 461 | * |
| 462 | * This, will call usb_gadget_unregister_driver() if |
| 463 | * the @udc is still busy. |
| 464 | */ |
| 465 | void usb_del_gadget_udc(struct usb_gadget *gadget) |
| 466 | { |
Peter Chen | dfea9c9 | 2015-03-06 10:36:02 +0800 | [diff] [blame] | 467 | struct usb_udc *udc = gadget->udc; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 468 | |
Peter Chen | dfea9c9 | 2015-03-06 10:36:02 +0800 | [diff] [blame] | 469 | if (!udc) |
| 470 | return; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 471 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 472 | dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); |
| 473 | |
Peter Chen | dfea9c9 | 2015-03-06 10:36:02 +0800 | [diff] [blame] | 474 | mutex_lock(&udc_lock); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 475 | list_del(&udc->list); |
| 476 | mutex_unlock(&udc_lock); |
| 477 | |
| 478 | if (udc->driver) |
| 479 | usb_gadget_remove_driver(udc); |
| 480 | |
| 481 | kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 482 | flush_work(&gadget->work); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 483 | device_unregister(&udc->dev); |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 484 | device_unregister(&gadget->dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 485 | } |
| 486 | EXPORT_SYMBOL_GPL(usb_del_gadget_udc); |
| 487 | |
| 488 | /* ------------------------------------------------------------------------- */ |
| 489 | |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 490 | static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver) |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 491 | { |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 492 | int ret; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 493 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 494 | dev_dbg(&udc->dev, "registering UDC driver [%s]\n", |
| 495 | driver->function); |
| 496 | |
| 497 | udc->driver = driver; |
| 498 | udc->dev.driver = &driver->driver; |
Felipe Balbi | 70d3a49 | 2013-02-26 13:51:24 +0200 | [diff] [blame] | 499 | udc->gadget->dev.driver = &driver->driver; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 500 | |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 501 | ret = driver->bind(udc->gadget, driver); |
| 502 | if (ret) |
| 503 | goto err1; |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 504 | ret = usb_gadget_udc_start(udc); |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 505 | if (ret) { |
| 506 | driver->unbind(udc->gadget); |
| 507 | goto err1; |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 508 | } |
Peter Chen | 628ef0d | 2015-03-06 10:36:03 +0800 | [diff] [blame] | 509 | usb_udc_connect_control(udc); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 510 | |
| 511 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 512 | return 0; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 513 | err1: |
Fabio Estevam | f8cffc8 | 2013-09-28 00:05:34 -0300 | [diff] [blame] | 514 | if (ret != -EISNAM) |
| 515 | dev_err(&udc->dev, "failed to start %s: %d\n", |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 516 | udc->driver->function, ret); |
| 517 | udc->driver = NULL; |
| 518 | udc->dev.driver = NULL; |
Felipe Balbi | 70d3a49 | 2013-02-26 13:51:24 +0200 | [diff] [blame] | 519 | udc->gadget->dev.driver = NULL; |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
Felipe Balbi | 02e8c96 | 2014-10-17 18:57:06 -0500 | [diff] [blame] | 523 | int usb_udc_attach_driver(const char *name, struct usb_gadget_driver *driver) |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 524 | { |
| 525 | struct usb_udc *udc = NULL; |
| 526 | int ret = -ENODEV; |
| 527 | |
| 528 | mutex_lock(&udc_lock); |
| 529 | list_for_each_entry(udc, &udc_list, list) { |
| 530 | ret = strcmp(name, dev_name(&udc->dev)); |
| 531 | if (!ret) |
| 532 | break; |
| 533 | } |
| 534 | if (ret) { |
| 535 | ret = -ENODEV; |
| 536 | goto out; |
| 537 | } |
| 538 | if (udc->driver) { |
| 539 | ret = -EBUSY; |
| 540 | goto out; |
| 541 | } |
| 542 | ret = udc_bind_to_driver(udc, driver); |
| 543 | out: |
| 544 | mutex_unlock(&udc_lock); |
| 545 | return ret; |
| 546 | } |
Felipe Balbi | 02e8c96 | 2014-10-17 18:57:06 -0500 | [diff] [blame] | 547 | EXPORT_SYMBOL_GPL(usb_udc_attach_driver); |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 548 | |
| 549 | int usb_gadget_probe_driver(struct usb_gadget_driver *driver) |
| 550 | { |
| 551 | struct usb_udc *udc = NULL; |
Ruslan Bilovol | 2284b29 | 2015-11-23 09:56:35 +0100 | [diff] [blame^] | 552 | int ret = -ENODEV; |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 553 | |
| 554 | if (!driver || !driver->bind || !driver->setup) |
| 555 | return -EINVAL; |
| 556 | |
| 557 | mutex_lock(&udc_lock); |
Ruslan Bilovol | 2284b29 | 2015-11-23 09:56:35 +0100 | [diff] [blame^] | 558 | if (driver->udc_name) { |
| 559 | list_for_each_entry(udc, &udc_list, list) { |
| 560 | ret = strcmp(driver->udc_name, dev_name(&udc->dev)); |
| 561 | if (!ret) |
| 562 | break; |
| 563 | } |
| 564 | if (ret) |
| 565 | ret = -ENODEV; |
| 566 | else if (udc->driver) |
| 567 | ret = -EBUSY; |
| 568 | else |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 569 | goto found; |
Ruslan Bilovol | 2284b29 | 2015-11-23 09:56:35 +0100 | [diff] [blame^] | 570 | } else { |
| 571 | list_for_each_entry(udc, &udc_list, list) { |
| 572 | /* For now we take the first one */ |
| 573 | if (!udc->driver) |
| 574 | goto found; |
| 575 | } |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | pr_debug("couldn't find an available UDC\n"); |
| 579 | mutex_unlock(&udc_lock); |
Ruslan Bilovol | 2284b29 | 2015-11-23 09:56:35 +0100 | [diff] [blame^] | 580 | return ret; |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 581 | found: |
| 582 | ret = udc_bind_to_driver(udc, driver); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 583 | mutex_unlock(&udc_lock); |
| 584 | return ret; |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(usb_gadget_probe_driver); |
| 587 | |
| 588 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 589 | { |
| 590 | struct usb_udc *udc = NULL; |
| 591 | int ret = -ENODEV; |
| 592 | |
| 593 | if (!driver || !driver->unbind) |
| 594 | return -EINVAL; |
| 595 | |
| 596 | mutex_lock(&udc_lock); |
| 597 | list_for_each_entry(udc, &udc_list, list) |
| 598 | if (udc->driver == driver) { |
| 599 | usb_gadget_remove_driver(udc); |
Peter Chen | b5fb8d0 | 2014-04-29 13:26:29 +0800 | [diff] [blame] | 600 | usb_gadget_set_state(udc->gadget, |
| 601 | USB_STATE_NOTATTACHED); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 602 | ret = 0; |
| 603 | break; |
| 604 | } |
| 605 | |
| 606 | mutex_unlock(&udc_lock); |
| 607 | return ret; |
| 608 | } |
| 609 | EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); |
| 610 | |
| 611 | /* ------------------------------------------------------------------------- */ |
| 612 | |
| 613 | static ssize_t usb_udc_srp_store(struct device *dev, |
| 614 | struct device_attribute *attr, const char *buf, size_t n) |
| 615 | { |
Felipe Balbi | 1d91a96 | 2011-09-08 14:11:17 +0300 | [diff] [blame] | 616 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 617 | |
| 618 | if (sysfs_streq(buf, "1")) |
| 619 | usb_gadget_wakeup(udc->gadget); |
| 620 | |
| 621 | return n; |
| 622 | } |
| 623 | static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store); |
| 624 | |
| 625 | static ssize_t usb_udc_softconn_store(struct device *dev, |
| 626 | struct device_attribute *attr, const char *buf, size_t n) |
| 627 | { |
Felipe Balbi | 865569b | 2011-09-08 14:12:18 +0300 | [diff] [blame] | 628 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 629 | |
Felipe Balbi | bfa6b18 | 2014-10-17 11:10:25 -0500 | [diff] [blame] | 630 | if (!udc->driver) { |
| 631 | dev_err(dev, "soft-connect without a gadget driver\n"); |
| 632 | return -EOPNOTSUPP; |
| 633 | } |
| 634 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 635 | if (sysfs_streq(buf, "connect")) { |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 636 | usb_gadget_udc_start(udc); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 637 | usb_gadget_connect(udc->gadget); |
| 638 | } else if (sysfs_streq(buf, "disconnect")) { |
Felipe Balbi | 83a787a | 2012-04-27 11:02:15 +0300 | [diff] [blame] | 639 | usb_gadget_disconnect(udc->gadget); |
Felipe Balbi | 0abd069 | 2014-10-09 10:12:24 -0500 | [diff] [blame] | 640 | udc->driver->disconnect(udc->gadget); |
Felipe Balbi | 2c68334 | 2014-10-17 11:34:07 -0500 | [diff] [blame] | 641 | usb_gadget_udc_stop(udc); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 642 | } else { |
| 643 | dev_err(dev, "unsupported command '%s'\n", buf); |
| 644 | return -EINVAL; |
| 645 | } |
| 646 | |
| 647 | return n; |
| 648 | } |
| 649 | static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store); |
| 650 | |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 651 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 652 | char *buf) |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 653 | { |
| 654 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
| 655 | struct usb_gadget *gadget = udc->gadget; |
| 656 | |
| 657 | return sprintf(buf, "%s\n", usb_state_string(gadget->state)); |
| 658 | } |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 659 | static DEVICE_ATTR_RO(state); |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 660 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 661 | #define USB_UDC_SPEED_ATTR(name, param) \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 662 | ssize_t name##_show(struct device *dev, \ |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 663 | struct device_attribute *attr, char *buf) \ |
| 664 | { \ |
| 665 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ |
| 666 | return snprintf(buf, PAGE_SIZE, "%s\n", \ |
| 667 | usb_speed_string(udc->gadget->param)); \ |
| 668 | } \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 669 | static DEVICE_ATTR_RO(name) |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 670 | |
| 671 | static USB_UDC_SPEED_ATTR(current_speed, speed); |
| 672 | static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); |
| 673 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 674 | #define USB_UDC_ATTR(name) \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 675 | ssize_t name##_show(struct device *dev, \ |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 676 | struct device_attribute *attr, char *buf) \ |
| 677 | { \ |
Sebastian Andrzej Siewior | 019f976 | 2011-06-23 14:28:38 +0200 | [diff] [blame] | 678 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 679 | struct usb_gadget *gadget = udc->gadget; \ |
| 680 | \ |
| 681 | return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ |
| 682 | } \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 683 | static DEVICE_ATTR_RO(name) |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 684 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 685 | static USB_UDC_ATTR(is_otg); |
| 686 | static USB_UDC_ATTR(is_a_peripheral); |
| 687 | static USB_UDC_ATTR(b_hnp_enable); |
| 688 | static USB_UDC_ATTR(a_hnp_support); |
| 689 | static USB_UDC_ATTR(a_alt_hnp_support); |
Peter Chen | 3f6dd4f | 2015-01-28 16:32:42 +0800 | [diff] [blame] | 690 | static USB_UDC_ATTR(is_selfpowered); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 691 | |
| 692 | static struct attribute *usb_udc_attrs[] = { |
| 693 | &dev_attr_srp.attr, |
| 694 | &dev_attr_soft_connect.attr, |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 695 | &dev_attr_state.attr, |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 696 | &dev_attr_current_speed.attr, |
| 697 | &dev_attr_maximum_speed.attr, |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 698 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 699 | &dev_attr_is_otg.attr, |
| 700 | &dev_attr_is_a_peripheral.attr, |
| 701 | &dev_attr_b_hnp_enable.attr, |
| 702 | &dev_attr_a_hnp_support.attr, |
| 703 | &dev_attr_a_alt_hnp_support.attr, |
Peter Chen | 3f6dd4f | 2015-01-28 16:32:42 +0800 | [diff] [blame] | 704 | &dev_attr_is_selfpowered.attr, |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 705 | NULL, |
| 706 | }; |
| 707 | |
| 708 | static const struct attribute_group usb_udc_attr_group = { |
| 709 | .attrs = usb_udc_attrs, |
| 710 | }; |
| 711 | |
| 712 | static const struct attribute_group *usb_udc_attr_groups[] = { |
| 713 | &usb_udc_attr_group, |
| 714 | NULL, |
| 715 | }; |
| 716 | |
| 717 | static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 718 | { |
| 719 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
| 720 | int ret; |
| 721 | |
| 722 | ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); |
| 723 | if (ret) { |
| 724 | dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | if (udc->driver) { |
| 729 | ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", |
| 730 | udc->driver->function); |
| 731 | if (ret) { |
| 732 | dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); |
| 733 | return ret; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int __init usb_udc_init(void) |
| 741 | { |
| 742 | udc_class = class_create(THIS_MODULE, "udc"); |
| 743 | if (IS_ERR(udc_class)) { |
| 744 | pr_err("failed to create udc class --> %ld\n", |
| 745 | PTR_ERR(udc_class)); |
| 746 | return PTR_ERR(udc_class); |
| 747 | } |
| 748 | |
| 749 | udc_class->dev_uevent = usb_udc_uevent; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | subsys_initcall(usb_udc_init); |
| 753 | |
| 754 | static void __exit usb_udc_exit(void) |
| 755 | { |
| 756 | class_destroy(udc_class); |
| 757 | } |
| 758 | module_exit(usb_udc_exit); |
| 759 | |
| 760 | MODULE_DESCRIPTION("UDC Framework"); |
| 761 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
| 762 | MODULE_LICENSE("GPL v2"); |