blob: 35eb257783de0a21158a5bf2e73da925ec6c01c6 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* drivers/usb/function/usb_function.h
2 *
3 * USB Function Device Interface
4 *
5 * Copyright (C) 2007 Google, Inc.
6 * Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
7 * Author: Brian Swetland <swetland@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20#ifndef _DRIVERS_USB_FUNCTION_USB_FUNCTION_H_
21#define _DRIVERS_USB_FUNCTION_USB_FUNCTION_H_
22
23#include <linux/list.h>
24#include <linux/usb/ch9.h>
25
26#define EPT_BULK_IN 1
27#define EPT_BULK_OUT 2
28#define EPT_INT_IN 3
29
30#define USB_CONFIG_ATT_SELFPOWER_POS (6) /* self powered */
31#define USB_CONFIG_ATT_WAKEUP_POS (5) /* can wakeup */
32
33struct usb_endpoint {
34 struct usb_info *ui;
35 struct msm_request *req; /* head of pending requests */
36 struct msm_request *last;
37 unsigned flags;
38
39 /* bit number (0-31) in various status registers
40 ** as well as the index into the usb_info's array
41 ** of all endpoints
42 */
43 unsigned char bit;
44 unsigned char num;
45
46 unsigned short max_pkt;
47
48 unsigned ept_halted;
49
50 /* pointers to DMA transfer list area */
51 /* these are allocated from the usb_info dma space */
52 struct ept_queue_head *head;
53 struct usb_endpoint_descriptor *ep_descriptor;
54 unsigned int alloced;
55};
56
57struct usb_request {
58 void *buf; /* pointer to associated data buffer */
59 unsigned length; /* requested transfer length */
60 int status; /* status upon completion */
61 unsigned actual; /* actual bytes transferred */
62
63 void (*complete)(struct usb_endpoint *ep, struct usb_request *req);
64 void *context;
65
66 void *device;
67
68 struct list_head list;
69};
70
71struct usb_function {
72 /* bind() is called once when the function has had its endpoints
73 ** allocated, but before the bus is active.
74 **
75 ** might be a good place to allocate some usb_request objects
76 */
77 void (*bind)(void *);
78
79 /* unbind() is called when the function is being removed.
80 ** it is illegal to call and usb_ept_* hooks at this point
81 ** and all endpoints must be released.
82 */
83 void (*unbind)(void *);
84
85 /* configure() is called when the usb client has been configured
86 ** by the host and again when the device is unconfigured (or
87 ** when the client is detached)
88 **
89 ** currently called from interrupt context.
90 */
91 void (*configure)(int configured, void *);
92 void (*disconnect)(void *);
93
94 /* setup() is called to allow functions to handle class and vendor
95 ** setup requests. If the request is unsupported or can not be handled,
96 ** setup() should return -1.
97 ** For OUT requests, buf will point to a buffer to data received in the
98 ** request's data phase, and len will contain the length of the data.
99 ** setup() should return 0 after handling an OUT request successfully.
100 ** for IN requests, buf will contain a pointer to a buffer for setup()
101 ** to write data to, and len will be the maximum size of the data to
102 ** be written back to the host.
103 ** After successfully handling an IN request, setup() should return
104 ** the number of bytes written to buf that should be sent in the
105 ** response to the host.
106 */
107 int (*setup)(struct usb_ctrlrequest *req, void *buf,
108 int len, void *);
109
110 int (*set_interface)(int ifc_num, int alt_set, void *_ctxt);
111 int (*get_interface)(int ifc_num, void *ctxt);
112 /* driver name */
113 const char *name;
114 void *context;
115
116 /* interface class/subclass/protocol for descriptor */
117 unsigned char ifc_class;
118 unsigned char ifc_subclass;
119 unsigned char ifc_protocol;
120
121 /* name string for descriptor */
122 const char *ifc_name;
123
124 /* number of needed endpoints and their types */
125 unsigned char ifc_ept_count;
126 unsigned char ifc_ept_type[8];
127
128 /* if the endpoint is disabled, its interface will not be
129 ** included in the configuration descriptor
130 */
131 unsigned char disabled;
132
133 struct usb_descriptor_header **fs_descriptors;
134 struct usb_descriptor_header **hs_descriptors;
135
136 struct usb_request *ep0_out_req, *ep0_in_req;
137 struct usb_endpoint *ep0_out, *ep0_in;
138};
139
140int usb_function_register(struct usb_function *driver);
141int usb_function_unregister(struct usb_function *driver);
142
143int usb_msm_get_speed(void);
144void usb_configure_endpoint(struct usb_endpoint *ep,
145 struct usb_endpoint_descriptor *ep_desc);
146int usb_remote_wakeup(void);
147/* To allocate endpoint from function driver*/
148struct usb_endpoint *usb_alloc_endpoint(unsigned direction);
149int usb_free_endpoint(struct usb_endpoint *ept);
150/* To enable endpoint from frunction driver*/
151void usb_ept_enable(struct usb_endpoint *ept, int yes);
152int usb_msm_get_next_ifc_number(struct usb_function *);
153int usb_msm_get_next_strdesc_id(char *);
154void usb_msm_enable_iad(void);
155
156void usb_function_enable(const char *function, int enable);
157
158/* Allocate a USB request.
159** Must be called from a context that can sleep.
160** If bufsize is nonzero, req->buf will be allocated for
161** you and free'd when the request is free'd. Otherwise
162** it is your responsibility to provide.
163*/
164struct usb_request *usb_ept_alloc_req(struct usb_endpoint *ept, unsigned bufsize);
165void usb_ept_free_req(struct usb_endpoint *ept, struct usb_request *req);
166
167/* safely callable from any context
168** returns 0 if successfully queued and sets req->status = -EBUSY
169** req->status will change to a different value upon completion
170** (0 for success, -EIO, -ENODEV, etc for error)
171*/
172int usb_ept_queue_xfer(struct usb_endpoint *ept, struct usb_request *req);
173int usb_ept_flush(struct usb_endpoint *ept);
174int usb_ept_get_max_packet(struct usb_endpoint *ept);
175int usb_ept_cancel_xfer(struct usb_endpoint *ept, struct usb_request *_req);
176void usb_ept_fifo_flush(struct usb_endpoint *ept);
177int usb_ept_set_halt(struct usb_endpoint *ept);
178int usb_ept_clear_halt(struct usb_endpoint *ept);
179struct device *usb_get_device(void);
180struct usb_endpoint *usb_ept_find(struct usb_endpoint **ept, int type);
181struct usb_function *usb_ept_get_function(struct usb_endpoint *ept);
182int usb_ept_is_stalled(struct usb_endpoint *ept);
183void usb_request_set_buffer(struct usb_request *req, void *buf, dma_addr_t dma);
184void usb_free_endpoint_all_req(struct usb_endpoint *ep);
185void usb_remove_function_driver(struct usb_function *func);
186int usb_remote_wakeup(void);
187#endif