GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / usb / gadget / udc / core.c
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>
25 #include <linux/dma-mapping.h>
26 #include <linux/workqueue.h>
27
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb.h>
31
32 #include "trace.h"
33
34 /**
35  * struct usb_udc - describes one usb device controller
36  * @driver - the gadget driver pointer. For use by the class code
37  * @dev - the child device to the actual controller
38  * @gadget - the gadget. For use by the class code
39  * @list - for use by the udc class driver
40  * @vbus - for udcs who care about vbus status, this value is real vbus status;
41  * for udcs who do not care about vbus status, this value is always true
42  *
43  * This represents the internal data structure which is used by the UDC-class
44  * to hold information about udc driver and gadget together.
45  */
46 struct usb_udc {
47         struct usb_gadget_driver        *driver;
48         struct usb_gadget               *gadget;
49         struct device                   dev;
50         struct list_head                list;
51         bool                            vbus;
52 };
53
54 static struct class *udc_class;
55 static LIST_HEAD(udc_list);
56 static LIST_HEAD(gadget_driver_pending_list);
57 static DEFINE_MUTEX(udc_lock);
58
59 static int udc_bind_to_driver(struct usb_udc *udc,
60                 struct usb_gadget_driver *driver);
61
62 /* ------------------------------------------------------------------------- */
63
64 /**
65  * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
66  * @ep:the endpoint being configured
67  * @maxpacket_limit:value of maximum packet size limit
68  *
69  * This function should be used only in UDC drivers to initialize endpoint
70  * (usually in probe function).
71  */
72 void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
73                                               unsigned maxpacket_limit)
74 {
75         ep->maxpacket_limit = maxpacket_limit;
76         ep->maxpacket = maxpacket_limit;
77
78         trace_usb_ep_set_maxpacket_limit(ep, 0);
79 }
80 EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
81
82 /**
83  * usb_ep_enable - configure endpoint, making it usable
84  * @ep:the endpoint being configured.  may not be the endpoint named "ep0".
85  *      drivers discover endpoints through the ep_list of a usb_gadget.
86  *
87  * When configurations are set, or when interface settings change, the driver
88  * will enable or disable the relevant endpoints.  while it is enabled, an
89  * endpoint may be used for i/o until the driver receives a disconnect() from
90  * the host or until the endpoint is disabled.
91  *
92  * the ep0 implementation (which calls this routine) must ensure that the
93  * hardware capabilities of each endpoint match the descriptor provided
94  * for it.  for example, an endpoint named "ep2in-bulk" would be usable
95  * for interrupt transfers as well as bulk, but it likely couldn't be used
96  * for iso transfers or for endpoint 14.  some endpoints are fully
97  * configurable, with more generic names like "ep-a".  (remember that for
98  * USB, "in" means "towards the USB master".)
99  *
100  * returns zero, or a negative error code.
101  */
102 int usb_ep_enable(struct usb_ep *ep)
103 {
104         int ret = 0;
105
106         if (ep->enabled)
107                 goto out;
108
109         /* UDC drivers can't handle endpoints with maxpacket size 0 */
110         if (usb_endpoint_maxp(ep->desc) == 0) {
111                 /*
112                  * We should log an error message here, but we can't call
113                  * dev_err() because there's no way to find the gadget
114                  * given only ep.
115                  */
116                 ret = -EINVAL;
117                 goto out;
118         }
119
120         ret = ep->ops->enable(ep, ep->desc);
121         if (ret)
122                 goto out;
123
124         ep->enabled = true;
125
126 out:
127         trace_usb_ep_enable(ep, ret);
128
129         return ret;
130 }
131 EXPORT_SYMBOL_GPL(usb_ep_enable);
132
133 /**
134  * usb_ep_disable - endpoint is no longer usable
135  * @ep:the endpoint being unconfigured.  may not be the endpoint named "ep0".
136  *
137  * no other task may be using this endpoint when this is called.
138  * any pending and uncompleted requests will complete with status
139  * indicating disconnect (-ESHUTDOWN) before this call returns.
140  * gadget drivers must call usb_ep_enable() again before queueing
141  * requests to the endpoint.
142  *
143  * returns zero, or a negative error code.
144  */
145 int usb_ep_disable(struct usb_ep *ep)
146 {
147         int ret = 0;
148
149         if (!ep->enabled)
150                 goto out;
151
152         ret = ep->ops->disable(ep);
153         if (ret)
154                 goto out;
155
156         ep->enabled = false;
157
158 out:
159         trace_usb_ep_disable(ep, ret);
160
161         return ret;
162 }
163 EXPORT_SYMBOL_GPL(usb_ep_disable);
164
165 /**
166  * usb_ep_alloc_request - allocate a request object to use with this endpoint
167  * @ep:the endpoint to be used with with the request
168  * @gfp_flags:GFP_* flags to use
169  *
170  * Request objects must be allocated with this call, since they normally
171  * need controller-specific setup and may even need endpoint-specific
172  * resources such as allocation of DMA descriptors.
173  * Requests may be submitted with usb_ep_queue(), and receive a single
174  * completion callback.  Free requests with usb_ep_free_request(), when
175  * they are no longer needed.
176  *
177  * Returns the request, or null if one could not be allocated.
178  */
179 struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
180                                                        gfp_t gfp_flags)
181 {
182         struct usb_request *req = NULL;
183
184         req = ep->ops->alloc_request(ep, gfp_flags);
185
186         trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
187
188         return req;
189 }
190 EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
191
192 /**
193  * usb_ep_free_request - frees a request object
194  * @ep:the endpoint associated with the request
195  * @req:the request being freed
196  *
197  * Reverses the effect of usb_ep_alloc_request().
198  * Caller guarantees the request is not queued, and that it will
199  * no longer be requeued (or otherwise used).
200  */
201 void usb_ep_free_request(struct usb_ep *ep,
202                                        struct usb_request *req)
203 {
204         trace_usb_ep_free_request(ep, req, 0);
205         ep->ops->free_request(ep, req);
206 }
207 EXPORT_SYMBOL_GPL(usb_ep_free_request);
208
209 /**
210  * usb_ep_queue - queues (submits) an I/O request to an endpoint.
211  * @ep:the endpoint associated with the request
212  * @req:the request being submitted
213  * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
214  *      pre-allocate all necessary memory with the request.
215  *
216  * This tells the device controller to perform the specified request through
217  * that endpoint (reading or writing a buffer).  When the request completes,
218  * including being canceled by usb_ep_dequeue(), the request's completion
219  * routine is called to return the request to the driver.  Any endpoint
220  * (except control endpoints like ep0) may have more than one transfer
221  * request queued; they complete in FIFO order.  Once a gadget driver
222  * submits a request, that request may not be examined or modified until it
223  * is given back to that driver through the completion callback.
224  *
225  * Each request is turned into one or more packets.  The controller driver
226  * never merges adjacent requests into the same packet.  OUT transfers
227  * will sometimes use data that's already buffered in the hardware.
228  * Drivers can rely on the fact that the first byte of the request's buffer
229  * always corresponds to the first byte of some USB packet, for both
230  * IN and OUT transfers.
231  *
232  * Bulk endpoints can queue any amount of data; the transfer is packetized
233  * automatically.  The last packet will be short if the request doesn't fill it
234  * out completely.  Zero length packets (ZLPs) should be avoided in portable
235  * protocols since not all usb hardware can successfully handle zero length
236  * packets.  (ZLPs may be explicitly written, and may be implicitly written if
237  * the request 'zero' flag is set.)  Bulk endpoints may also be used
238  * for interrupt transfers; but the reverse is not true, and some endpoints
239  * won't support every interrupt transfer.  (Such as 768 byte packets.)
240  *
241  * Interrupt-only endpoints are less functional than bulk endpoints, for
242  * example by not supporting queueing or not handling buffers that are
243  * larger than the endpoint's maxpacket size.  They may also treat data
244  * toggle differently.
245  *
246  * Control endpoints ... after getting a setup() callback, the driver queues
247  * one response (even if it would be zero length).  That enables the
248  * status ack, after transferring data as specified in the response.  Setup
249  * functions may return negative error codes to generate protocol stalls.
250  * (Note that some USB device controllers disallow protocol stall responses
251  * in some cases.)  When control responses are deferred (the response is
252  * written after the setup callback returns), then usb_ep_set_halt() may be
253  * used on ep0 to trigger protocol stalls.  Depending on the controller,
254  * it may not be possible to trigger a status-stage protocol stall when the
255  * data stage is over, that is, from within the response's completion
256  * routine.
257  *
258  * For periodic endpoints, like interrupt or isochronous ones, the usb host
259  * arranges to poll once per interval, and the gadget driver usually will
260  * have queued some data to transfer at that time.
261  *
262  * Note that @req's ->complete() callback must never be called from
263  * within usb_ep_queue() as that can create deadlock situations.
264  *
265  * Returns zero, or a negative error code.  Endpoints that are not enabled
266  * report errors; errors will also be
267  * reported when the usb peripheral is disconnected.
268  */
269 int usb_ep_queue(struct usb_ep *ep,
270                                struct usb_request *req, gfp_t gfp_flags)
271 {
272         int ret = 0;
273
274         if (WARN_ON_ONCE(!ep->enabled && ep->address)) {
275                 ret = -ESHUTDOWN;
276                 goto out;
277         }
278
279         ret = ep->ops->queue(ep, req, gfp_flags);
280
281 out:
282         trace_usb_ep_queue(ep, req, ret);
283
284         return ret;
285 }
286 EXPORT_SYMBOL_GPL(usb_ep_queue);
287
288 /**
289  * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
290  * @ep:the endpoint associated with the request
291  * @req:the request being canceled
292  *
293  * If the request is still active on the endpoint, it is dequeued and its
294  * completion routine is called (with status -ECONNRESET); else a negative
295  * error code is returned. This is guaranteed to happen before the call to
296  * usb_ep_dequeue() returns.
297  *
298  * Note that some hardware can't clear out write fifos (to unlink the request
299  * at the head of the queue) except as part of disconnecting from usb. Such
300  * restrictions prevent drivers from supporting configuration changes,
301  * even to configuration zero (a "chapter 9" requirement).
302  */
303 int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
304 {
305         int ret;
306
307         ret = ep->ops->dequeue(ep, req);
308         trace_usb_ep_dequeue(ep, req, ret);
309
310         return ret;
311 }
312 EXPORT_SYMBOL_GPL(usb_ep_dequeue);
313
314 /**
315  * usb_ep_set_halt - sets the endpoint halt feature.
316  * @ep: the non-isochronous endpoint being stalled
317  *
318  * Use this to stall an endpoint, perhaps as an error report.
319  * Except for control endpoints,
320  * the endpoint stays halted (will not stream any data) until the host
321  * clears this feature; drivers may need to empty the endpoint's request
322  * queue first, to make sure no inappropriate transfers happen.
323  *
324  * Note that while an endpoint CLEAR_FEATURE will be invisible to the
325  * gadget driver, a SET_INTERFACE will not be.  To reset endpoints for the
326  * current altsetting, see usb_ep_clear_halt().  When switching altsettings,
327  * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
328  *
329  * Returns zero, or a negative error code.  On success, this call sets
330  * underlying hardware state that blocks data transfers.
331  * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
332  * transfer requests are still queued, or if the controller hardware
333  * (usually a FIFO) still holds bytes that the host hasn't collected.
334  */
335 int usb_ep_set_halt(struct usb_ep *ep)
336 {
337         int ret;
338
339         ret = ep->ops->set_halt(ep, 1);
340         trace_usb_ep_set_halt(ep, ret);
341
342         return ret;
343 }
344 EXPORT_SYMBOL_GPL(usb_ep_set_halt);
345
346 /**
347  * usb_ep_clear_halt - clears endpoint halt, and resets toggle
348  * @ep:the bulk or interrupt endpoint being reset
349  *
350  * Use this when responding to the standard usb "set interface" request,
351  * for endpoints that aren't reconfigured, after clearing any other state
352  * in the endpoint's i/o queue.
353  *
354  * Returns zero, or a negative error code.  On success, this call clears
355  * the underlying hardware state reflecting endpoint halt and data toggle.
356  * Note that some hardware can't support this request (like pxa2xx_udc),
357  * and accordingly can't correctly implement interface altsettings.
358  */
359 int usb_ep_clear_halt(struct usb_ep *ep)
360 {
361         int ret;
362
363         ret = ep->ops->set_halt(ep, 0);
364         trace_usb_ep_clear_halt(ep, ret);
365
366         return ret;
367 }
368 EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
369
370 /**
371  * usb_ep_set_wedge - sets the halt feature and ignores clear requests
372  * @ep: the endpoint being wedged
373  *
374  * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
375  * requests. If the gadget driver clears the halt status, it will
376  * automatically unwedge the endpoint.
377  *
378  * Returns zero on success, else negative errno.
379  */
380 int usb_ep_set_wedge(struct usb_ep *ep)
381 {
382         int ret;
383
384         if (ep->ops->set_wedge)
385                 ret = ep->ops->set_wedge(ep);
386         else
387                 ret = ep->ops->set_halt(ep, 1);
388
389         trace_usb_ep_set_wedge(ep, ret);
390
391         return ret;
392 }
393 EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
394
395 /**
396  * usb_ep_fifo_status - returns number of bytes in fifo, or error
397  * @ep: the endpoint whose fifo status is being checked.
398  *
399  * FIFO endpoints may have "unclaimed data" in them in certain cases,
400  * such as after aborted transfers.  Hosts may not have collected all
401  * the IN data written by the gadget driver (and reported by a request
402  * completion).  The gadget driver may not have collected all the data
403  * written OUT to it by the host.  Drivers that need precise handling for
404  * fault reporting or recovery may need to use this call.
405  *
406  * This returns the number of such bytes in the fifo, or a negative
407  * errno if the endpoint doesn't use a FIFO or doesn't support such
408  * precise handling.
409  */
410 int usb_ep_fifo_status(struct usb_ep *ep)
411 {
412         int ret;
413
414         if (ep->ops->fifo_status)
415                 ret = ep->ops->fifo_status(ep);
416         else
417                 ret = -EOPNOTSUPP;
418
419         trace_usb_ep_fifo_status(ep, ret);
420
421         return ret;
422 }
423 EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
424
425 /**
426  * usb_ep_fifo_flush - flushes contents of a fifo
427  * @ep: the endpoint whose fifo is being flushed.
428  *
429  * This call may be used to flush the "unclaimed data" that may exist in
430  * an endpoint fifo after abnormal transaction terminations.  The call
431  * must never be used except when endpoint is not being used for any
432  * protocol translation.
433  */
434 void usb_ep_fifo_flush(struct usb_ep *ep)
435 {
436         if (ep->ops->fifo_flush)
437                 ep->ops->fifo_flush(ep);
438
439         trace_usb_ep_fifo_flush(ep, 0);
440 }
441 EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
442
443 /* ------------------------------------------------------------------------- */
444
445 /**
446  * usb_gadget_frame_number - returns the current frame number
447  * @gadget: controller that reports the frame number
448  *
449  * Returns the usb frame number, normally eleven bits from a SOF packet,
450  * or negative errno if this device doesn't support this capability.
451  */
452 int usb_gadget_frame_number(struct usb_gadget *gadget)
453 {
454         int ret;
455
456         ret = gadget->ops->get_frame(gadget);
457
458         trace_usb_gadget_frame_number(gadget, ret);
459
460         return ret;
461 }
462 EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
463
464 /**
465  * usb_gadget_wakeup - tries to wake up the host connected to this gadget
466  * @gadget: controller used to wake up the host
467  *
468  * Returns zero on success, else negative error code if the hardware
469  * doesn't support such attempts, or its support has not been enabled
470  * by the usb host.  Drivers must return device descriptors that report
471  * their ability to support this, or hosts won't enable it.
472  *
473  * This may also try to use SRP to wake the host and start enumeration,
474  * even if OTG isn't otherwise in use.  OTG devices may also start
475  * remote wakeup even when hosts don't explicitly enable it.
476  */
477 int usb_gadget_wakeup(struct usb_gadget *gadget)
478 {
479         int ret = 0;
480
481         if (!gadget->ops->wakeup) {
482                 ret = -EOPNOTSUPP;
483                 goto out;
484         }
485
486         ret = gadget->ops->wakeup(gadget);
487
488 out:
489         trace_usb_gadget_wakeup(gadget, ret);
490
491         return ret;
492 }
493 EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
494
495 /**
496  * usb_gadget_set_selfpowered - sets the device selfpowered feature.
497  * @gadget:the device being declared as self-powered
498  *
499  * this affects the device status reported by the hardware driver
500  * to reflect that it now has a local power supply.
501  *
502  * returns zero on success, else negative errno.
503  */
504 int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
505 {
506         int ret = 0;
507
508         if (!gadget->ops->set_selfpowered) {
509                 ret = -EOPNOTSUPP;
510                 goto out;
511         }
512
513         ret = gadget->ops->set_selfpowered(gadget, 1);
514
515 out:
516         trace_usb_gadget_set_selfpowered(gadget, ret);
517
518         return ret;
519 }
520 EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
521
522 /**
523  * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
524  * @gadget:the device being declared as bus-powered
525  *
526  * this affects the device status reported by the hardware driver.
527  * some hardware may not support bus-powered operation, in which
528  * case this feature's value can never change.
529  *
530  * returns zero on success, else negative errno.
531  */
532 int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
533 {
534         int ret = 0;
535
536         if (!gadget->ops->set_selfpowered) {
537                 ret = -EOPNOTSUPP;
538                 goto out;
539         }
540
541         ret = gadget->ops->set_selfpowered(gadget, 0);
542
543 out:
544         trace_usb_gadget_clear_selfpowered(gadget, ret);
545
546         return ret;
547 }
548 EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
549
550 /**
551  * usb_gadget_vbus_connect - Notify controller that VBUS is powered
552  * @gadget:The device which now has VBUS power.
553  * Context: can sleep
554  *
555  * This call is used by a driver for an external transceiver (or GPIO)
556  * that detects a VBUS power session starting.  Common responses include
557  * resuming the controller, activating the D+ (or D-) pullup to let the
558  * host detect that a USB device is attached, and starting to draw power
559  * (8mA or possibly more, especially after SET_CONFIGURATION).
560  *
561  * Returns zero on success, else negative errno.
562  */
563 int usb_gadget_vbus_connect(struct usb_gadget *gadget)
564 {
565         int ret = 0;
566
567         if (!gadget->ops->vbus_session) {
568                 ret = -EOPNOTSUPP;
569                 goto out;
570         }
571
572         ret = gadget->ops->vbus_session(gadget, 1);
573
574 out:
575         trace_usb_gadget_vbus_connect(gadget, ret);
576
577         return ret;
578 }
579 EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
580
581 /**
582  * usb_gadget_vbus_draw - constrain controller's VBUS power usage
583  * @gadget:The device whose VBUS usage is being described
584  * @mA:How much current to draw, in milliAmperes.  This should be twice
585  *      the value listed in the configuration descriptor bMaxPower field.
586  *
587  * This call is used by gadget drivers during SET_CONFIGURATION calls,
588  * reporting how much power the device may consume.  For example, this
589  * could affect how quickly batteries are recharged.
590  *
591  * Returns zero on success, else negative errno.
592  */
593 int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
594 {
595         int ret = 0;
596
597         if (!gadget->ops->vbus_draw) {
598                 ret = -EOPNOTSUPP;
599                 goto out;
600         }
601
602         ret = gadget->ops->vbus_draw(gadget, mA);
603         if (!ret)
604                 gadget->mA = mA;
605
606 out:
607         trace_usb_gadget_vbus_draw(gadget, ret);
608
609         return ret;
610 }
611 EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
612
613 /**
614  * usb_gadget_vbus_disconnect - notify controller about VBUS session end
615  * @gadget:the device whose VBUS supply is being described
616  * Context: can sleep
617  *
618  * This call is used by a driver for an external transceiver (or GPIO)
619  * that detects a VBUS power session ending.  Common responses include
620  * reversing everything done in usb_gadget_vbus_connect().
621  *
622  * Returns zero on success, else negative errno.
623  */
624 int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
625 {
626         int ret = 0;
627
628         if (!gadget->ops->vbus_session) {
629                 ret = -EOPNOTSUPP;
630                 goto out;
631         }
632
633         ret = gadget->ops->vbus_session(gadget, 0);
634
635 out:
636         trace_usb_gadget_vbus_disconnect(gadget, ret);
637
638         return ret;
639 }
640 EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
641
642 /**
643  * usb_gadget_connect - software-controlled connect to USB host
644  * @gadget:the peripheral being connected
645  *
646  * Enables the D+ (or potentially D-) pullup.  The host will start
647  * enumerating this gadget when the pullup is active and a VBUS session
648  * is active (the link is powered).  This pullup is always enabled unless
649  * usb_gadget_disconnect() has been used to disable it.
650  *
651  * Returns zero on success, else negative errno.
652  */
653 int usb_gadget_connect(struct usb_gadget *gadget)
654 {
655         int ret = 0;
656
657         if (!gadget->ops->pullup) {
658                 ret = -EOPNOTSUPP;
659                 goto out;
660         }
661
662         if (gadget->deactivated) {
663                 /*
664                  * If gadget is deactivated we only save new state.
665                  * Gadget will be connected automatically after activation.
666                  */
667                 gadget->connected = true;
668                 goto out;
669         }
670
671         ret = gadget->ops->pullup(gadget, 1);
672         if (!ret)
673                 gadget->connected = 1;
674
675 out:
676         trace_usb_gadget_connect(gadget, ret);
677
678         return ret;
679 }
680 EXPORT_SYMBOL_GPL(usb_gadget_connect);
681
682 /**
683  * usb_gadget_disconnect - software-controlled disconnect from USB host
684  * @gadget:the peripheral being disconnected
685  *
686  * Disables the D+ (or potentially D-) pullup, which the host may see
687  * as a disconnect (when a VBUS session is active).  Not all systems
688  * support software pullup controls.
689  *
690  * Returns zero on success, else negative errno.
691  */
692 int usb_gadget_disconnect(struct usb_gadget *gadget)
693 {
694         int ret = 0;
695
696         if (!gadget->ops->pullup) {
697                 ret = -EOPNOTSUPP;
698                 goto out;
699         }
700
701         if (gadget->deactivated) {
702                 /*
703                  * If gadget is deactivated we only save new state.
704                  * Gadget will stay disconnected after activation.
705                  */
706                 gadget->connected = false;
707                 goto out;
708         }
709
710         ret = gadget->ops->pullup(gadget, 0);
711         if (!ret)
712                 gadget->connected = 0;
713
714 out:
715         trace_usb_gadget_disconnect(gadget, ret);
716
717         return ret;
718 }
719 EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
720
721 /**
722  * usb_gadget_deactivate - deactivate function which is not ready to work
723  * @gadget: the peripheral being deactivated
724  *
725  * This routine may be used during the gadget driver bind() call to prevent
726  * the peripheral from ever being visible to the USB host, unless later
727  * usb_gadget_activate() is called.  For example, user mode components may
728  * need to be activated before the system can talk to hosts.
729  *
730  * Returns zero on success, else negative errno.
731  */
732 int usb_gadget_deactivate(struct usb_gadget *gadget)
733 {
734         int ret = 0;
735
736         if (gadget->deactivated)
737                 goto out;
738
739         if (gadget->connected) {
740                 ret = usb_gadget_disconnect(gadget);
741                 if (ret)
742                         goto out;
743
744                 /*
745                  * If gadget was being connected before deactivation, we want
746                  * to reconnect it in usb_gadget_activate().
747                  */
748                 gadget->connected = true;
749         }
750         gadget->deactivated = true;
751
752 out:
753         trace_usb_gadget_deactivate(gadget, ret);
754
755         return ret;
756 }
757 EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
758
759 /**
760  * usb_gadget_activate - activate function which is not ready to work
761  * @gadget: the peripheral being activated
762  *
763  * This routine activates gadget which was previously deactivated with
764  * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
765  *
766  * Returns zero on success, else negative errno.
767  */
768 int usb_gadget_activate(struct usb_gadget *gadget)
769 {
770         int ret = 0;
771
772         if (!gadget->deactivated)
773                 goto out;
774
775         gadget->deactivated = false;
776
777         /*
778          * If gadget has been connected before deactivation, or became connected
779          * while it was being deactivated, we call usb_gadget_connect().
780          */
781         if (gadget->connected)
782                 ret = usb_gadget_connect(gadget);
783
784 out:
785         trace_usb_gadget_activate(gadget, ret);
786
787         return ret;
788 }
789 EXPORT_SYMBOL_GPL(usb_gadget_activate);
790
791 /* ------------------------------------------------------------------------- */
792
793 #ifdef  CONFIG_HAS_DMA
794
795 int usb_gadget_map_request_by_dev(struct device *dev,
796                 struct usb_request *req, int is_in)
797 {
798         if (req->length == 0)
799                 return 0;
800
801         if (req->num_sgs) {
802                 int     mapped;
803
804                 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
805                                 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
806                 if (mapped == 0) {
807                         dev_err(dev, "failed to map SGs\n");
808                         return -EFAULT;
809                 }
810
811                 req->num_mapped_sgs = mapped;
812         } else {
813                 req->dma = dma_map_single(dev, req->buf, req->length,
814                                 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
815
816                 if (dma_mapping_error(dev, req->dma)) {
817                         dev_err(dev, "failed to map buffer\n");
818                         return -EFAULT;
819                 }
820
821                 req->dma_mapped = 1;
822         }
823
824         return 0;
825 }
826 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
827
828 int usb_gadget_map_request(struct usb_gadget *gadget,
829                 struct usb_request *req, int is_in)
830 {
831         return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
832 }
833 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
834
835 void usb_gadget_unmap_request_by_dev(struct device *dev,
836                 struct usb_request *req, int is_in)
837 {
838         if (req->length == 0)
839                 return;
840
841         if (req->num_mapped_sgs) {
842                 dma_unmap_sg(dev, req->sg, req->num_sgs,
843                                 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
844
845                 req->num_mapped_sgs = 0;
846         } else if (req->dma_mapped) {
847                 dma_unmap_single(dev, req->dma, req->length,
848                                 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
849                 req->dma_mapped = 0;
850         }
851 }
852 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
853
854 void usb_gadget_unmap_request(struct usb_gadget *gadget,
855                 struct usb_request *req, int is_in)
856 {
857         usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
858 }
859 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
860
861 #endif  /* CONFIG_HAS_DMA */
862
863 /* ------------------------------------------------------------------------- */
864
865 /**
866  * usb_gadget_giveback_request - give the request back to the gadget layer
867  * Context: in_interrupt()
868  *
869  * This is called by device controller drivers in order to return the
870  * completed request back to the gadget layer.
871  */
872 void usb_gadget_giveback_request(struct usb_ep *ep,
873                 struct usb_request *req)
874 {
875         if (likely(req->status == 0))
876                 usb_led_activity(USB_LED_EVENT_GADGET);
877
878         trace_usb_gadget_giveback_request(ep, req, 0);
879
880         req->complete(ep, req);
881 }
882 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
883
884 /* ------------------------------------------------------------------------- */
885
886 /**
887  * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
888  *      in second parameter or NULL if searched endpoint not found
889  * @g: controller to check for quirk
890  * @name: name of searched endpoint
891  */
892 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
893 {
894         struct usb_ep *ep;
895
896         gadget_for_each_ep(ep, g) {
897                 if (!strcmp(ep->name, name))
898                         return ep;
899         }
900
901         return NULL;
902 }
903 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
904
905 /* ------------------------------------------------------------------------- */
906
907 int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
908                 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
909                 struct usb_ss_ep_comp_descriptor *ep_comp)
910 {
911         u8              type;
912         u16             max;
913         int             num_req_streams = 0;
914
915         /* endpoint already claimed? */
916         if (ep->claimed)
917                 return 0;
918
919         type = usb_endpoint_type(desc);
920         max = 0x7ff & usb_endpoint_maxp(desc);
921
922         if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
923                 return 0;
924         if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
925                 return 0;
926
927         if (max > ep->maxpacket_limit)
928                 return 0;
929
930         /* "high bandwidth" works only at high speed */
931         if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
932                 return 0;
933
934         switch (type) {
935         case USB_ENDPOINT_XFER_CONTROL:
936                 /* only support ep0 for portable CONTROL traffic */
937                 return 0;
938         case USB_ENDPOINT_XFER_ISOC:
939                 if (!ep->caps.type_iso)
940                         return 0;
941                 /* ISO:  limit 1023 bytes full speed, 1024 high/super speed */
942                 if (!gadget_is_dualspeed(gadget) && max > 1023)
943                         return 0;
944                 break;
945         case USB_ENDPOINT_XFER_BULK:
946                 if (!ep->caps.type_bulk)
947                         return 0;
948                 if (ep_comp && gadget_is_superspeed(gadget)) {
949                         /* Get the number of required streams from the
950                          * EP companion descriptor and see if the EP
951                          * matches it
952                          */
953                         num_req_streams = ep_comp->bmAttributes & 0x1f;
954                         if (num_req_streams > ep->max_streams)
955                                 return 0;
956                 }
957                 break;
958         case USB_ENDPOINT_XFER_INT:
959                 /* Bulk endpoints handle interrupt transfers,
960                  * except the toggle-quirky iso-synch kind
961                  */
962                 if (!ep->caps.type_int && !ep->caps.type_bulk)
963                         return 0;
964                 /* INT:  limit 64 bytes full speed, 1024 high/super speed */
965                 if (!gadget_is_dualspeed(gadget) && max > 64)
966                         return 0;
967                 break;
968         }
969
970         return 1;
971 }
972 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
973
974 /* ------------------------------------------------------------------------- */
975
976 static void usb_gadget_state_work(struct work_struct *work)
977 {
978         struct usb_gadget *gadget = work_to_gadget(work);
979         struct usb_udc *udc = gadget->udc;
980
981         if (udc)
982                 sysfs_notify(&udc->dev.kobj, NULL, "state");
983 }
984
985 void usb_gadget_set_state(struct usb_gadget *gadget,
986                 enum usb_device_state state)
987 {
988         gadget->state = state;
989         schedule_work(&gadget->work);
990 }
991 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
992
993 /* ------------------------------------------------------------------------- */
994
995 static void usb_udc_connect_control(struct usb_udc *udc)
996 {
997         if (udc->vbus)
998                 usb_gadget_connect(udc->gadget);
999         else
1000                 usb_gadget_disconnect(udc->gadget);
1001 }
1002
1003 /**
1004  * usb_udc_vbus_handler - updates the udc core vbus status, and try to
1005  * connect or disconnect gadget
1006  * @gadget: The gadget which vbus change occurs
1007  * @status: The vbus status
1008  *
1009  * The udc driver calls it when it wants to connect or disconnect gadget
1010  * according to vbus status.
1011  */
1012 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
1013 {
1014         struct usb_udc *udc = gadget->udc;
1015
1016         if (udc) {
1017                 udc->vbus = status;
1018                 usb_udc_connect_control(udc);
1019         }
1020 }
1021 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1022
1023 /**
1024  * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1025  * @gadget: The gadget which bus reset occurs
1026  * @driver: The gadget driver we want to notify
1027  *
1028  * If the udc driver has bus reset handler, it needs to call this when the bus
1029  * reset occurs, it notifies the gadget driver that the bus reset occurs as
1030  * well as updates gadget state.
1031  */
1032 void usb_gadget_udc_reset(struct usb_gadget *gadget,
1033                 struct usb_gadget_driver *driver)
1034 {
1035         driver->reset(gadget);
1036         usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1037 }
1038 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1039
1040 /**
1041  * usb_gadget_udc_start - tells usb device controller to start up
1042  * @udc: The UDC to be started
1043  *
1044  * This call is issued by the UDC Class driver when it's about
1045  * to register a gadget driver to the device controller, before
1046  * calling gadget driver's bind() method.
1047  *
1048  * It allows the controller to be powered off until strictly
1049  * necessary to have it powered on.
1050  *
1051  * Returns zero on success, else negative errno.
1052  */
1053 static inline int usb_gadget_udc_start(struct usb_udc *udc)
1054 {
1055         return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
1056 }
1057
1058 /**
1059  * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1060  * @gadget: The device we want to stop activity
1061  * @driver: The driver to unbind from @gadget
1062  *
1063  * This call is issued by the UDC Class driver after calling
1064  * gadget driver's unbind() method.
1065  *
1066  * The details are implementation specific, but it can go as
1067  * far as powering off UDC completely and disable its data
1068  * line pullups.
1069  */
1070 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
1071 {
1072         udc->gadget->ops->udc_stop(udc->gadget);
1073 }
1074
1075 /**
1076  * usb_udc_release - release the usb_udc struct
1077  * @dev: the dev member within usb_udc
1078  *
1079  * This is called by driver's core in order to free memory once the last
1080  * reference is released.
1081  */
1082 static void usb_udc_release(struct device *dev)
1083 {
1084         struct usb_udc *udc;
1085
1086         udc = container_of(dev, struct usb_udc, dev);
1087         dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
1088         kfree(udc);
1089 }
1090
1091 static const struct attribute_group *usb_udc_attr_groups[];
1092
1093 static void usb_udc_nop_release(struct device *dev)
1094 {
1095         dev_vdbg(dev, "%s\n", __func__);
1096 }
1097
1098 /* should be called with udc_lock held */
1099 static int check_pending_gadget_drivers(struct usb_udc *udc)
1100 {
1101         struct usb_gadget_driver *driver;
1102         int ret = 0;
1103
1104         list_for_each_entry(driver, &gadget_driver_pending_list, pending)
1105                 if (!driver->udc_name || strcmp(driver->udc_name,
1106                                                 dev_name(&udc->dev)) == 0) {
1107                         ret = udc_bind_to_driver(udc, driver);
1108                         if (ret != -EPROBE_DEFER)
1109                                 list_del(&driver->pending);
1110                         break;
1111                 }
1112
1113         return ret;
1114 }
1115
1116 /**
1117  * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
1118  * @parent: the parent device to this udc. Usually the controller driver's
1119  * device.
1120  * @gadget: the gadget to be added to the list.
1121  * @release: a gadget release function.
1122  *
1123  * Returns zero on success, negative errno otherwise.
1124  */
1125 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
1126                 void (*release)(struct device *dev))
1127 {
1128         struct usb_udc          *udc;
1129         int                     ret = -ENOMEM;
1130
1131         udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1132         if (!udc)
1133                 goto err1;
1134
1135         dev_set_name(&gadget->dev, "gadget");
1136         INIT_WORK(&gadget->work, usb_gadget_state_work);
1137         gadget->dev.parent = parent;
1138
1139         if (release)
1140                 gadget->dev.release = release;
1141         else
1142                 gadget->dev.release = usb_udc_nop_release;
1143
1144         ret = device_register(&gadget->dev);
1145         if (ret)
1146                 goto err2;
1147
1148         device_initialize(&udc->dev);
1149         udc->dev.release = usb_udc_release;
1150         udc->dev.class = udc_class;
1151         udc->dev.groups = usb_udc_attr_groups;
1152         udc->dev.parent = parent;
1153         ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
1154         if (ret)
1155                 goto err3;
1156
1157         udc->gadget = gadget;
1158         gadget->udc = udc;
1159
1160         mutex_lock(&udc_lock);
1161         list_add_tail(&udc->list, &udc_list);
1162
1163         ret = device_add(&udc->dev);
1164         if (ret)
1165                 goto err4;
1166
1167         usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
1168         udc->vbus = true;
1169
1170         /* pick up one of pending gadget drivers */
1171         ret = check_pending_gadget_drivers(udc);
1172         if (ret)
1173                 goto err5;
1174
1175         mutex_unlock(&udc_lock);
1176
1177         return 0;
1178
1179 err5:
1180         device_del(&udc->dev);
1181
1182 err4:
1183         list_del(&udc->list);
1184         mutex_unlock(&udc_lock);
1185
1186 err3:
1187         put_device(&udc->dev);
1188         device_del(&gadget->dev);
1189
1190 err2:
1191         put_device(&gadget->dev);
1192         kfree(udc);
1193
1194 err1:
1195         return ret;
1196 }
1197 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1198
1199 /**
1200  * usb_get_gadget_udc_name - get the name of the first UDC controller
1201  * This functions returns the name of the first UDC controller in the system.
1202  * Please note that this interface is usefull only for legacy drivers which
1203  * assume that there is only one UDC controller in the system and they need to
1204  * get its name before initialization. There is no guarantee that the UDC
1205  * of the returned name will be still available, when gadget driver registers
1206  * itself.
1207  *
1208  * Returns pointer to string with UDC controller name on success, NULL
1209  * otherwise. Caller should kfree() returned string.
1210  */
1211 char *usb_get_gadget_udc_name(void)
1212 {
1213         struct usb_udc *udc;
1214         char *name = NULL;
1215
1216         /* For now we take the first available UDC */
1217         mutex_lock(&udc_lock);
1218         list_for_each_entry(udc, &udc_list, list) {
1219                 if (!udc->driver) {
1220                         name = kstrdup(udc->gadget->name, GFP_KERNEL);
1221                         break;
1222                 }
1223         }
1224         mutex_unlock(&udc_lock);
1225         return name;
1226 }
1227 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1228
1229 /**
1230  * usb_add_gadget_udc - adds a new gadget to the udc class driver list
1231  * @parent: the parent device to this udc. Usually the controller
1232  * driver's device.
1233  * @gadget: the gadget to be added to the list
1234  *
1235  * Returns zero on success, negative errno otherwise.
1236  */
1237 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
1238 {
1239         return usb_add_gadget_udc_release(parent, gadget, NULL);
1240 }
1241 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
1242
1243 static void usb_gadget_remove_driver(struct usb_udc *udc)
1244 {
1245         dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
1246                         udc->driver->function);
1247
1248         kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1249
1250         usb_gadget_disconnect(udc->gadget);
1251         udc->driver->disconnect(udc->gadget);
1252         udc->driver->unbind(udc->gadget);
1253         usb_gadget_udc_stop(udc);
1254
1255         udc->driver = NULL;
1256         udc->gadget->dev.driver = NULL;
1257 }
1258
1259 /**
1260  * usb_del_gadget_udc - deletes @udc from udc_list
1261  * @gadget: the gadget to be removed.
1262  *
1263  * This, will call usb_gadget_unregister_driver() if
1264  * the @udc is still busy.
1265  */
1266 void usb_del_gadget_udc(struct usb_gadget *gadget)
1267 {
1268         struct usb_udc *udc = gadget->udc;
1269
1270         if (!udc)
1271                 return;
1272
1273         dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
1274
1275         mutex_lock(&udc_lock);
1276         list_del(&udc->list);
1277
1278         if (udc->driver) {
1279                 struct usb_gadget_driver *driver = udc->driver;
1280
1281                 usb_gadget_remove_driver(udc);
1282                 list_add(&driver->pending, &gadget_driver_pending_list);
1283         }
1284         mutex_unlock(&udc_lock);
1285
1286         kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
1287         flush_work(&gadget->work);
1288         device_unregister(&udc->dev);
1289         device_unregister(&gadget->dev);
1290 }
1291 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
1292
1293 /* ------------------------------------------------------------------------- */
1294
1295 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
1296 {
1297         int ret;
1298
1299         dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
1300                         driver->function);
1301
1302         udc->driver = driver;
1303         udc->gadget->dev.driver = &driver->driver;
1304
1305         ret = driver->bind(udc->gadget, driver);
1306         if (ret)
1307                 goto err1;
1308         ret = usb_gadget_udc_start(udc);
1309         if (ret) {
1310                 driver->unbind(udc->gadget);
1311                 goto err1;
1312         }
1313         usb_udc_connect_control(udc);
1314
1315         kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1316         return 0;
1317 err1:
1318         if (ret != -EISNAM)
1319                 dev_err(&udc->dev, "failed to start %s: %d\n",
1320                         udc->driver->function, ret);
1321         udc->driver = NULL;
1322         udc->gadget->dev.driver = NULL;
1323         return ret;
1324 }
1325
1326 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
1327 {
1328         struct usb_udc          *udc = NULL;
1329         int                     ret = -ENODEV;
1330
1331         if (!driver || !driver->bind || !driver->setup)
1332                 return -EINVAL;
1333
1334         mutex_lock(&udc_lock);
1335         if (driver->udc_name) {
1336                 list_for_each_entry(udc, &udc_list, list) {
1337                         ret = strcmp(driver->udc_name, dev_name(&udc->dev));
1338                         if (!ret)
1339                                 break;
1340                 }
1341                 if (ret)
1342                         ret = -ENODEV;
1343                 else if (udc->driver)
1344                         ret = -EBUSY;
1345                 else
1346                         goto found;
1347         } else {
1348                 list_for_each_entry(udc, &udc_list, list) {
1349                         /* For now we take the first one */
1350                         if (!udc->driver)
1351                                 goto found;
1352                 }
1353         }
1354
1355         if (!driver->match_existing_only) {
1356                 list_add_tail(&driver->pending, &gadget_driver_pending_list);
1357                 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1358                         driver->function);
1359                 ret = 0;
1360         }
1361
1362         mutex_unlock(&udc_lock);
1363         return ret;
1364 found:
1365         ret = udc_bind_to_driver(udc, driver);
1366         mutex_unlock(&udc_lock);
1367         return ret;
1368 }
1369 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
1370
1371 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1372 {
1373         struct usb_udc          *udc = NULL;
1374         int                     ret = -ENODEV;
1375
1376         if (!driver || !driver->unbind)
1377                 return -EINVAL;
1378
1379         mutex_lock(&udc_lock);
1380         list_for_each_entry(udc, &udc_list, list) {
1381                 if (udc->driver == driver) {
1382                         usb_gadget_remove_driver(udc);
1383                         usb_gadget_set_state(udc->gadget,
1384                                              USB_STATE_NOTATTACHED);
1385
1386                         /* Maybe there is someone waiting for this UDC? */
1387                         check_pending_gadget_drivers(udc);
1388                         /*
1389                          * For now we ignore bind errors as probably it's
1390                          * not a valid reason to fail other's gadget unbind
1391                          */
1392                         ret = 0;
1393                         break;
1394                 }
1395         }
1396
1397         if (ret) {
1398                 list_del(&driver->pending);
1399                 ret = 0;
1400         }
1401         mutex_unlock(&udc_lock);
1402         return ret;
1403 }
1404 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
1405
1406 /* ------------------------------------------------------------------------- */
1407
1408 static ssize_t usb_udc_srp_store(struct device *dev,
1409                 struct device_attribute *attr, const char *buf, size_t n)
1410 {
1411         struct usb_udc          *udc = container_of(dev, struct usb_udc, dev);
1412
1413         if (sysfs_streq(buf, "1"))
1414                 usb_gadget_wakeup(udc->gadget);
1415
1416         return n;
1417 }
1418 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
1419
1420 static ssize_t usb_udc_softconn_store(struct device *dev,
1421                 struct device_attribute *attr, const char *buf, size_t n)
1422 {
1423         struct usb_udc          *udc = container_of(dev, struct usb_udc, dev);
1424         ssize_t                 ret;
1425
1426         mutex_lock(&udc_lock);
1427         if (!udc->driver) {
1428                 dev_err(dev, "soft-connect without a gadget driver\n");
1429                 ret = -EOPNOTSUPP;
1430                 goto out;
1431         }
1432
1433         if (sysfs_streq(buf, "connect")) {
1434                 usb_gadget_udc_start(udc);
1435                 usb_gadget_connect(udc->gadget);
1436         } else if (sysfs_streq(buf, "disconnect")) {
1437                 usb_gadget_disconnect(udc->gadget);
1438                 udc->driver->disconnect(udc->gadget);
1439                 usb_gadget_udc_stop(udc);
1440         } else {
1441                 dev_err(dev, "unsupported command '%s'\n", buf);
1442                 ret = -EINVAL;
1443                 goto out;
1444         }
1445
1446         ret = n;
1447 out:
1448         mutex_unlock(&udc_lock);
1449         return ret;
1450 }
1451 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
1452
1453 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1454                           char *buf)
1455 {
1456         struct usb_udc          *udc = container_of(dev, struct usb_udc, dev);
1457         struct usb_gadget       *gadget = udc->gadget;
1458
1459         return sprintf(buf, "%s\n", usb_state_string(gadget->state));
1460 }
1461 static DEVICE_ATTR_RO(state);
1462
1463 #define USB_UDC_SPEED_ATTR(name, param)                                 \
1464 ssize_t name##_show(struct device *dev,                                 \
1465                 struct device_attribute *attr, char *buf)               \
1466 {                                                                       \
1467         struct usb_udc *udc = container_of(dev, struct usb_udc, dev);   \
1468         return snprintf(buf, PAGE_SIZE, "%s\n",                         \
1469                         usb_speed_string(udc->gadget->param));          \
1470 }                                                                       \
1471 static DEVICE_ATTR_RO(name)
1472
1473 static USB_UDC_SPEED_ATTR(current_speed, speed);
1474 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1475
1476 #define USB_UDC_ATTR(name)                                      \
1477 ssize_t name##_show(struct device *dev,                         \
1478                 struct device_attribute *attr, char *buf)       \
1479 {                                                               \
1480         struct usb_udc          *udc = container_of(dev, struct usb_udc, dev); \
1481         struct usb_gadget       *gadget = udc->gadget;          \
1482                                                                 \
1483         return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name);  \
1484 }                                                               \
1485 static DEVICE_ATTR_RO(name)
1486
1487 static USB_UDC_ATTR(is_otg);
1488 static USB_UDC_ATTR(is_a_peripheral);
1489 static USB_UDC_ATTR(b_hnp_enable);
1490 static USB_UDC_ATTR(a_hnp_support);
1491 static USB_UDC_ATTR(a_alt_hnp_support);
1492 static USB_UDC_ATTR(is_selfpowered);
1493
1494 static struct attribute *usb_udc_attrs[] = {
1495         &dev_attr_srp.attr,
1496         &dev_attr_soft_connect.attr,
1497         &dev_attr_state.attr,
1498         &dev_attr_current_speed.attr,
1499         &dev_attr_maximum_speed.attr,
1500
1501         &dev_attr_is_otg.attr,
1502         &dev_attr_is_a_peripheral.attr,
1503         &dev_attr_b_hnp_enable.attr,
1504         &dev_attr_a_hnp_support.attr,
1505         &dev_attr_a_alt_hnp_support.attr,
1506         &dev_attr_is_selfpowered.attr,
1507         NULL,
1508 };
1509
1510 static const struct attribute_group usb_udc_attr_group = {
1511         .attrs = usb_udc_attrs,
1512 };
1513
1514 static const struct attribute_group *usb_udc_attr_groups[] = {
1515         &usb_udc_attr_group,
1516         NULL,
1517 };
1518
1519 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
1520 {
1521         struct usb_udc          *udc = container_of(dev, struct usb_udc, dev);
1522         int                     ret;
1523
1524         ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
1525         if (ret) {
1526                 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
1527                 return ret;
1528         }
1529
1530         if (udc->driver) {
1531                 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
1532                                 udc->driver->function);
1533                 if (ret) {
1534                         dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1535                         return ret;
1536                 }
1537         }
1538
1539         return 0;
1540 }
1541
1542 static int __init usb_udc_init(void)
1543 {
1544         udc_class = class_create(THIS_MODULE, "udc");
1545         if (IS_ERR(udc_class)) {
1546                 pr_err("failed to create udc class --> %ld\n",
1547                                 PTR_ERR(udc_class));
1548                 return PTR_ERR(udc_class);
1549         }
1550
1551         udc_class->dev_uevent = usb_udc_uevent;
1552         return 0;
1553 }
1554 subsys_initcall(usb_udc_init);
1555
1556 static void __exit usb_udc_exit(void)
1557 {
1558         class_destroy(udc_class);
1559 }
1560 module_exit(usb_udc_exit);
1561
1562 MODULE_DESCRIPTION("UDC Framework");
1563 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1564 MODULE_LICENSE("GPL v2");