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> |
| 30 | |
| 31 | /** |
| 32 | * struct usb_udc - describes one usb device controller |
| 33 | * @driver - the gadget driver pointer. For use by the class code |
| 34 | * @dev - the child device to the actual controller |
| 35 | * @gadget - the gadget. For use by the class code |
| 36 | * @list - for use by the udc class driver |
| 37 | * |
| 38 | * This represents the internal data structure which is used by the UDC-class |
| 39 | * to hold information about udc driver and gadget together. |
| 40 | */ |
| 41 | struct usb_udc { |
| 42 | struct usb_gadget_driver *driver; |
| 43 | struct usb_gadget *gadget; |
| 44 | struct device dev; |
| 45 | struct list_head list; |
| 46 | }; |
| 47 | |
| 48 | static struct class *udc_class; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 49 | static LIST_HEAD(udc_list); |
| 50 | static DEFINE_MUTEX(udc_lock); |
| 51 | |
| 52 | /* ------------------------------------------------------------------------- */ |
| 53 | |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 54 | #ifdef CONFIG_HAS_DMA |
| 55 | |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 56 | int usb_gadget_map_request(struct usb_gadget *gadget, |
| 57 | struct usb_request *req, int is_in) |
| 58 | { |
| 59 | if (req->length == 0) |
| 60 | return 0; |
| 61 | |
| 62 | if (req->num_sgs) { |
| 63 | int mapped; |
| 64 | |
| 65 | mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs, |
| 66 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 67 | if (mapped == 0) { |
| 68 | dev_err(&gadget->dev, "failed to map SGs\n"); |
| 69 | return -EFAULT; |
| 70 | } |
| 71 | |
| 72 | req->num_mapped_sgs = mapped; |
| 73 | } else { |
| 74 | req->dma = dma_map_single(&gadget->dev, req->buf, req->length, |
| 75 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 76 | |
| 77 | if (dma_mapping_error(&gadget->dev, req->dma)) { |
| 78 | dev_err(&gadget->dev, "failed to map buffer\n"); |
| 79 | return -EFAULT; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(usb_gadget_map_request); |
| 86 | |
| 87 | void usb_gadget_unmap_request(struct usb_gadget *gadget, |
| 88 | struct usb_request *req, int is_in) |
| 89 | { |
| 90 | if (req->length == 0) |
| 91 | return; |
| 92 | |
| 93 | if (req->num_mapped_sgs) { |
| 94 | dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs, |
| 95 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 96 | |
| 97 | req->num_mapped_sgs = 0; |
| 98 | } else { |
| 99 | dma_unmap_single(&gadget->dev, req->dma, req->length, |
| 100 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 101 | } |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(usb_gadget_unmap_request); |
| 104 | |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 105 | #endif /* CONFIG_HAS_DMA */ |
| 106 | |
Felipe Balbi | a698908 | 2011-12-15 13:31:48 +0200 | [diff] [blame] | 107 | /* ------------------------------------------------------------------------- */ |
| 108 | |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 109 | static void usb_gadget_state_work(struct work_struct *work) |
| 110 | { |
| 111 | struct usb_gadget *gadget = work_to_gadget(work); |
| 112 | |
Greg Kroah-Hartman | 165f606 | 2013-08-13 15:28:01 -0700 | [diff] [blame] | 113 | sysfs_notify(&gadget->dev.kobj, NULL, "state"); |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 114 | } |
| 115 | |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 116 | void usb_gadget_set_state(struct usb_gadget *gadget, |
| 117 | enum usb_device_state state) |
| 118 | { |
| 119 | gadget->state = state; |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 120 | schedule_work(&gadget->work); |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 121 | } |
| 122 | EXPORT_SYMBOL_GPL(usb_gadget_set_state); |
| 123 | |
| 124 | /* ------------------------------------------------------------------------- */ |
| 125 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 126 | /** |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 127 | * usb_gadget_udc_start - tells usb device controller to start up |
| 128 | * @gadget: The gadget we want to get started |
| 129 | * @driver: The driver we want to bind to @gadget |
| 130 | * |
| 131 | * This call is issued by the UDC Class driver when it's about |
| 132 | * to register a gadget driver to the device controller, before |
| 133 | * calling gadget driver's bind() method. |
| 134 | * |
| 135 | * It allows the controller to be powered off until strictly |
| 136 | * necessary to have it powered on. |
| 137 | * |
| 138 | * Returns zero on success, else negative errno. |
| 139 | */ |
| 140 | static inline int usb_gadget_udc_start(struct usb_gadget *gadget, |
| 141 | struct usb_gadget_driver *driver) |
| 142 | { |
| 143 | return gadget->ops->udc_start(gadget, driver); |
| 144 | } |
| 145 | |
| 146 | /** |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 147 | * usb_gadget_udc_stop - tells usb device controller we don't need it anymore |
| 148 | * @gadget: The device we want to stop activity |
| 149 | * @driver: The driver to unbind from @gadget |
| 150 | * |
| 151 | * This call is issued by the UDC Class driver after calling |
| 152 | * gadget driver's unbind() method. |
| 153 | * |
| 154 | * The details are implementation specific, but it can go as |
| 155 | * far as powering off UDC completely and disable its data |
| 156 | * line pullups. |
| 157 | */ |
| 158 | static inline void usb_gadget_udc_stop(struct usb_gadget *gadget, |
| 159 | struct usb_gadget_driver *driver) |
| 160 | { |
| 161 | gadget->ops->udc_stop(gadget, driver); |
| 162 | } |
| 163 | |
| 164 | /** |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 165 | * usb_udc_release - release the usb_udc struct |
| 166 | * @dev: the dev member within usb_udc |
| 167 | * |
| 168 | * This is called by driver's core in order to free memory once the last |
| 169 | * reference is released. |
| 170 | */ |
| 171 | static void usb_udc_release(struct device *dev) |
| 172 | { |
| 173 | struct usb_udc *udc; |
| 174 | |
| 175 | udc = container_of(dev, struct usb_udc, dev); |
| 176 | dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); |
| 177 | kfree(udc); |
| 178 | } |
| 179 | |
Sebastian Andrzej Siewior | 019f976 | 2011-06-23 14:28:38 +0200 | [diff] [blame] | 180 | static const struct attribute_group *usb_udc_attr_groups[]; |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 181 | |
| 182 | static void usb_udc_nop_release(struct device *dev) |
| 183 | { |
| 184 | dev_vdbg(dev, "%s\n", __func__); |
| 185 | } |
| 186 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 187 | /** |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 188 | * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list |
| 189 | * @parent: the parent device to this udc. Usually the controller driver's |
| 190 | * device. |
| 191 | * @gadget: the gadget to be added to the list. |
| 192 | * @release: a gadget release function. |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 193 | * |
| 194 | * Returns zero on success, negative errno otherwise. |
| 195 | */ |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 196 | int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, |
| 197 | void (*release)(struct device *dev)) |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 198 | { |
| 199 | struct usb_udc *udc; |
| 200 | int ret = -ENOMEM; |
| 201 | |
| 202 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); |
| 203 | if (!udc) |
| 204 | goto err1; |
| 205 | |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 206 | dev_set_name(&gadget->dev, "gadget"); |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 207 | INIT_WORK(&gadget->work, usb_gadget_state_work); |
Felipe Balbi | 2ed1432 | 2013-02-26 10:59:41 +0200 | [diff] [blame] | 208 | gadget->dev.parent = parent; |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 209 | |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 210 | #ifdef CONFIG_HAS_DMA |
Felipe Balbi | 658c8cf | 2013-02-26 10:50:42 +0200 | [diff] [blame] | 211 | dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask); |
| 212 | gadget->dev.dma_parms = parent->dma_parms; |
| 213 | gadget->dev.dma_mask = parent->dma_mask; |
Alan Stern | 908b961 | 2013-07-12 11:06:21 -0400 | [diff] [blame] | 214 | #endif |
Felipe Balbi | 658c8cf | 2013-02-26 10:50:42 +0200 | [diff] [blame] | 215 | |
Felipe Balbi | ddf47cc | 2013-02-26 15:25:41 +0200 | [diff] [blame] | 216 | if (release) |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 217 | gadget->dev.release = release; |
Felipe Balbi | ddf47cc | 2013-02-26 15:25:41 +0200 | [diff] [blame] | 218 | else |
| 219 | gadget->dev.release = usb_udc_nop_release; |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 220 | |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 221 | ret = device_register(&gadget->dev); |
| 222 | if (ret) |
| 223 | goto err2; |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 224 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 225 | device_initialize(&udc->dev); |
| 226 | udc->dev.release = usb_udc_release; |
| 227 | udc->dev.class = udc_class; |
Sebastian Andrzej Siewior | 019f976 | 2011-06-23 14:28:38 +0200 | [diff] [blame] | 228 | udc->dev.groups = usb_udc_attr_groups; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 229 | udc->dev.parent = parent; |
| 230 | ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); |
| 231 | if (ret) |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 232 | goto err3; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 233 | |
| 234 | udc->gadget = gadget; |
| 235 | |
| 236 | mutex_lock(&udc_lock); |
| 237 | list_add_tail(&udc->list, &udc_list); |
| 238 | |
| 239 | ret = device_add(&udc->dev); |
| 240 | if (ret) |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 241 | goto err4; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 242 | |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 243 | usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); |
| 244 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 245 | mutex_unlock(&udc_lock); |
| 246 | |
| 247 | return 0; |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 248 | |
| 249 | err4: |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 250 | list_del(&udc->list); |
| 251 | mutex_unlock(&udc_lock); |
| 252 | |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 253 | err3: |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 254 | put_device(&udc->dev); |
| 255 | |
Felipe Balbi | f07bd56b | 2013-01-24 14:52:24 +0200 | [diff] [blame] | 256 | err2: |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 257 | put_device(&gadget->dev); |
Felipe Balbi | c5dbc22 | 2013-04-02 17:06:28 +0300 | [diff] [blame] | 258 | kfree(udc); |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 259 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 260 | err1: |
| 261 | return ret; |
| 262 | } |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 263 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); |
| 264 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 265 | /** |
| 266 | * usb_add_gadget_udc - adds a new gadget to the udc class driver list |
| 267 | * @parent: the parent device to this udc. Usually the controller |
| 268 | * driver's device. |
| 269 | * @gadget: the gadget to be added to the list |
| 270 | * |
| 271 | * Returns zero on success, negative errno otherwise. |
| 272 | */ |
| 273 | int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) |
| 274 | { |
Felipe Balbi | 792bfcf | 2013-02-26 14:47:44 +0200 | [diff] [blame] | 275 | return usb_add_gadget_udc_release(parent, gadget, NULL); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 276 | } |
| 277 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc); |
| 278 | |
| 279 | static void usb_gadget_remove_driver(struct usb_udc *udc) |
| 280 | { |
| 281 | dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n", |
| 282 | udc->gadget->name); |
| 283 | |
| 284 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); |
| 285 | |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 286 | usb_gadget_disconnect(udc->gadget); |
| 287 | udc->driver->disconnect(udc->gadget); |
| 288 | udc->driver->unbind(udc->gadget); |
Alan Stern | 511f3c5 | 2013-03-15 14:02:14 -0400 | [diff] [blame] | 289 | usb_gadget_udc_stop(udc->gadget, NULL); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 290 | |
| 291 | udc->driver = NULL; |
| 292 | udc->dev.driver = NULL; |
Felipe Balbi | 70d3a49 | 2013-02-26 13:51:24 +0200 | [diff] [blame] | 293 | udc->gadget->dev.driver = NULL; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * usb_del_gadget_udc - deletes @udc from udc_list |
| 298 | * @gadget: the gadget to be removed. |
| 299 | * |
| 300 | * This, will call usb_gadget_unregister_driver() if |
| 301 | * the @udc is still busy. |
| 302 | */ |
| 303 | void usb_del_gadget_udc(struct usb_gadget *gadget) |
| 304 | { |
| 305 | struct usb_udc *udc = NULL; |
| 306 | |
| 307 | mutex_lock(&udc_lock); |
| 308 | list_for_each_entry(udc, &udc_list, list) |
| 309 | if (udc->gadget == gadget) |
| 310 | goto found; |
| 311 | |
| 312 | dev_err(gadget->dev.parent, "gadget not registered.\n"); |
| 313 | mutex_unlock(&udc_lock); |
| 314 | |
| 315 | return; |
| 316 | |
| 317 | found: |
| 318 | dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); |
| 319 | |
| 320 | list_del(&udc->list); |
| 321 | mutex_unlock(&udc_lock); |
| 322 | |
| 323 | if (udc->driver) |
| 324 | usb_gadget_remove_driver(udc); |
| 325 | |
| 326 | kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); |
Felipe Balbi | 5702f75 | 2013-07-17 11:09:49 +0300 | [diff] [blame] | 327 | flush_work(&gadget->work); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 328 | device_unregister(&udc->dev); |
Felipe Balbi | 7bce401 | 2013-01-24 17:41:00 +0200 | [diff] [blame] | 329 | device_unregister(&gadget->dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 330 | } |
| 331 | EXPORT_SYMBOL_GPL(usb_del_gadget_udc); |
| 332 | |
| 333 | /* ------------------------------------------------------------------------- */ |
| 334 | |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 335 | 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] | 336 | { |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 337 | int ret; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 338 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 339 | dev_dbg(&udc->dev, "registering UDC driver [%s]\n", |
| 340 | driver->function); |
| 341 | |
| 342 | udc->driver = driver; |
| 343 | udc->dev.driver = &driver->driver; |
Felipe Balbi | 70d3a49 | 2013-02-26 13:51:24 +0200 | [diff] [blame] | 344 | udc->gadget->dev.driver = &driver->driver; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 345 | |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 346 | ret = driver->bind(udc->gadget, driver); |
| 347 | if (ret) |
| 348 | goto err1; |
| 349 | ret = usb_gadget_udc_start(udc->gadget, driver); |
| 350 | if (ret) { |
| 351 | driver->unbind(udc->gadget); |
| 352 | goto err1; |
Sebastian Andrzej Siewior | 352c2dc | 2011-06-23 14:26:15 +0200 | [diff] [blame] | 353 | } |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 354 | usb_gadget_connect(udc->gadget); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 355 | |
| 356 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 357 | return 0; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 358 | err1: |
Fabio Estevam | f8cffc8 | 2013-09-28 00:05:34 -0300 | [diff] [blame] | 359 | if (ret != -EISNAM) |
| 360 | dev_err(&udc->dev, "failed to start %s: %d\n", |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 361 | udc->driver->function, ret); |
| 362 | udc->driver = NULL; |
| 363 | udc->dev.driver = NULL; |
Felipe Balbi | 70d3a49 | 2013-02-26 13:51:24 +0200 | [diff] [blame] | 364 | udc->gadget->dev.driver = NULL; |
Sebastian Andrzej Siewior | 4c49a5f | 2012-12-23 21:10:19 +0100 | [diff] [blame] | 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | int udc_attach_driver(const char *name, struct usb_gadget_driver *driver) |
| 369 | { |
| 370 | struct usb_udc *udc = NULL; |
| 371 | int ret = -ENODEV; |
| 372 | |
| 373 | mutex_lock(&udc_lock); |
| 374 | list_for_each_entry(udc, &udc_list, list) { |
| 375 | ret = strcmp(name, dev_name(&udc->dev)); |
| 376 | if (!ret) |
| 377 | break; |
| 378 | } |
| 379 | if (ret) { |
| 380 | ret = -ENODEV; |
| 381 | goto out; |
| 382 | } |
| 383 | if (udc->driver) { |
| 384 | ret = -EBUSY; |
| 385 | goto out; |
| 386 | } |
| 387 | ret = udc_bind_to_driver(udc, driver); |
| 388 | out: |
| 389 | mutex_unlock(&udc_lock); |
| 390 | return ret; |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(udc_attach_driver); |
| 393 | |
| 394 | int usb_gadget_probe_driver(struct usb_gadget_driver *driver) |
| 395 | { |
| 396 | struct usb_udc *udc = NULL; |
| 397 | int ret; |
| 398 | |
| 399 | if (!driver || !driver->bind || !driver->setup) |
| 400 | return -EINVAL; |
| 401 | |
| 402 | mutex_lock(&udc_lock); |
| 403 | list_for_each_entry(udc, &udc_list, list) { |
| 404 | /* For now we take the first one */ |
| 405 | if (!udc->driver) |
| 406 | goto found; |
| 407 | } |
| 408 | |
| 409 | pr_debug("couldn't find an available UDC\n"); |
| 410 | mutex_unlock(&udc_lock); |
| 411 | return -ENODEV; |
| 412 | found: |
| 413 | ret = udc_bind_to_driver(udc, driver); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 414 | mutex_unlock(&udc_lock); |
| 415 | return ret; |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(usb_gadget_probe_driver); |
| 418 | |
| 419 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 420 | { |
| 421 | struct usb_udc *udc = NULL; |
| 422 | int ret = -ENODEV; |
| 423 | |
| 424 | if (!driver || !driver->unbind) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | mutex_lock(&udc_lock); |
| 428 | list_for_each_entry(udc, &udc_list, list) |
| 429 | if (udc->driver == driver) { |
| 430 | usb_gadget_remove_driver(udc); |
| 431 | ret = 0; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | mutex_unlock(&udc_lock); |
| 436 | return ret; |
| 437 | } |
| 438 | EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); |
| 439 | |
| 440 | /* ------------------------------------------------------------------------- */ |
| 441 | |
| 442 | static ssize_t usb_udc_srp_store(struct device *dev, |
| 443 | struct device_attribute *attr, const char *buf, size_t n) |
| 444 | { |
Felipe Balbi | 1d91a96 | 2011-09-08 14:11:17 +0300 | [diff] [blame] | 445 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 446 | |
| 447 | if (sysfs_streq(buf, "1")) |
| 448 | usb_gadget_wakeup(udc->gadget); |
| 449 | |
| 450 | return n; |
| 451 | } |
| 452 | static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store); |
| 453 | |
| 454 | static ssize_t usb_udc_softconn_store(struct device *dev, |
| 455 | struct device_attribute *attr, const char *buf, size_t n) |
| 456 | { |
Felipe Balbi | 865569b | 2011-09-08 14:12:18 +0300 | [diff] [blame] | 457 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 458 | |
| 459 | if (sysfs_streq(buf, "connect")) { |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 460 | usb_gadget_udc_start(udc->gadget, udc->driver); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 461 | usb_gadget_connect(udc->gadget); |
| 462 | } else if (sysfs_streq(buf, "disconnect")) { |
Felipe Balbi | 83a787a | 2012-04-27 11:02:15 +0300 | [diff] [blame] | 463 | usb_gadget_disconnect(udc->gadget); |
Felipe Balbi | 2d7ebbb | 2013-01-24 11:00:15 +0200 | [diff] [blame] | 464 | usb_gadget_udc_stop(udc->gadget, udc->driver); |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 465 | } else { |
| 466 | dev_err(dev, "unsupported command '%s'\n", buf); |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | |
| 470 | return n; |
| 471 | } |
| 472 | static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store); |
| 473 | |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 474 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 475 | char *buf) |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 476 | { |
| 477 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
| 478 | struct usb_gadget *gadget = udc->gadget; |
| 479 | |
| 480 | return sprintf(buf, "%s\n", usb_state_string(gadget->state)); |
| 481 | } |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 482 | static DEVICE_ATTR_RO(state); |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 483 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 484 | #define USB_UDC_SPEED_ATTR(name, param) \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 485 | ssize_t name##_show(struct device *dev, \ |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 486 | struct device_attribute *attr, char *buf) \ |
| 487 | { \ |
| 488 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ |
| 489 | return snprintf(buf, PAGE_SIZE, "%s\n", \ |
| 490 | usb_speed_string(udc->gadget->param)); \ |
| 491 | } \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 492 | static DEVICE_ATTR_RO(name) |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 493 | |
| 494 | static USB_UDC_SPEED_ATTR(current_speed, speed); |
| 495 | static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); |
| 496 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 497 | #define USB_UDC_ATTR(name) \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 498 | ssize_t name##_show(struct device *dev, \ |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 499 | struct device_attribute *attr, char *buf) \ |
| 500 | { \ |
Sebastian Andrzej Siewior | 019f976 | 2011-06-23 14:28:38 +0200 | [diff] [blame] | 501 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 502 | struct usb_gadget *gadget = udc->gadget; \ |
| 503 | \ |
| 504 | return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ |
| 505 | } \ |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 506 | static DEVICE_ATTR_RO(name) |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 507 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 508 | static USB_UDC_ATTR(is_otg); |
| 509 | static USB_UDC_ATTR(is_a_peripheral); |
| 510 | static USB_UDC_ATTR(b_hnp_enable); |
| 511 | static USB_UDC_ATTR(a_hnp_support); |
| 512 | static USB_UDC_ATTR(a_alt_hnp_support); |
| 513 | |
| 514 | static struct attribute *usb_udc_attrs[] = { |
| 515 | &dev_attr_srp.attr, |
| 516 | &dev_attr_soft_connect.attr, |
Felipe Balbi | 49401f4 | 2011-12-19 12:57:04 +0200 | [diff] [blame] | 517 | &dev_attr_state.attr, |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 518 | &dev_attr_current_speed.attr, |
| 519 | &dev_attr_maximum_speed.attr, |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 520 | |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 521 | &dev_attr_is_otg.attr, |
| 522 | &dev_attr_is_a_peripheral.attr, |
| 523 | &dev_attr_b_hnp_enable.attr, |
| 524 | &dev_attr_a_hnp_support.attr, |
| 525 | &dev_attr_a_alt_hnp_support.attr, |
| 526 | NULL, |
| 527 | }; |
| 528 | |
| 529 | static const struct attribute_group usb_udc_attr_group = { |
| 530 | .attrs = usb_udc_attrs, |
| 531 | }; |
| 532 | |
| 533 | static const struct attribute_group *usb_udc_attr_groups[] = { |
| 534 | &usb_udc_attr_group, |
| 535 | NULL, |
| 536 | }; |
| 537 | |
| 538 | static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 539 | { |
| 540 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
| 541 | int ret; |
| 542 | |
| 543 | ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); |
| 544 | if (ret) { |
| 545 | dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | if (udc->driver) { |
| 550 | ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", |
| 551 | udc->driver->function); |
| 552 | if (ret) { |
| 553 | dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); |
| 554 | return ret; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int __init usb_udc_init(void) |
| 562 | { |
| 563 | udc_class = class_create(THIS_MODULE, "udc"); |
| 564 | if (IS_ERR(udc_class)) { |
| 565 | pr_err("failed to create udc class --> %ld\n", |
| 566 | PTR_ERR(udc_class)); |
| 567 | return PTR_ERR(udc_class); |
| 568 | } |
| 569 | |
| 570 | udc_class->dev_uevent = usb_udc_uevent; |
Felipe Balbi | 2ccea03 | 2011-06-28 16:33:46 +0300 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | subsys_initcall(usb_udc_init); |
| 574 | |
| 575 | static void __exit usb_udc_exit(void) |
| 576 | { |
| 577 | class_destroy(udc_class); |
| 578 | } |
| 579 | module_exit(usb_udc_exit); |
| 580 | |
| 581 | MODULE_DESCRIPTION("UDC Framework"); |
| 582 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
| 583 | MODULE_LICENSE("GPL v2"); |