]>
Commit | Line | Data |
---|---|---|
b3147863 CK |
1 | /* ----------------------------------------------------------------------------- |
2 | * Copyright (c) 2011 Ozmo Inc | |
3 | * Released under the GNU General Public License Version 2 (GPLv2). | |
4 | * | |
5 | * This file provides protocol independent part of the implementation of the USB | |
6 | * service for a PD. | |
7 | * The implementation of this service is split into two parts the first of which | |
8 | * is protocol independent and the second contains protocol specific details. | |
9 | * This split is to allow alternative protocols to be defined. | |
8dc24597 | 10 | * The implementation of this service uses ozhcd.c to implement a USB HCD. |
b3147863 CK |
11 | * ----------------------------------------------------------------------------- |
12 | */ | |
05f608f2 | 13 | |
b3147863 CK |
14 | #include <linux/module.h> |
15 | #include <linux/timer.h> | |
16 | #include <linux/sched.h> | |
17 | #include <linux/netdevice.h> | |
18 | #include <linux/errno.h> | |
19 | #include <linux/input.h> | |
20 | #include <asm/unaligned.h> | |
f724b584 | 21 | #include "ozdbg.h" |
b3147863 CK |
22 | #include "ozprotocol.h" |
23 | #include "ozeltbuf.h" | |
24 | #include "ozpd.h" | |
25 | #include "ozproto.h" | |
26 | #include "ozusbif.h" | |
27 | #include "ozhcd.h" | |
b3147863 | 28 | #include "ozusbsvc.h" |
05f608f2 | 29 | |
4e7fb829 | 30 | /* |
b3147863 CK |
31 | * This is called once when the driver is loaded to initialise the USB service. |
32 | * Context: process | |
33 | */ | |
34 | int oz_usb_init(void) | |
35 | { | |
b3147863 CK |
36 | return oz_hcd_init(); |
37 | } | |
6e244a83 | 38 | |
4e7fb829 | 39 | /* |
b3147863 CK |
40 | * This is called once when the driver is unloaded to terminate the USB service. |
41 | * Context: process | |
42 | */ | |
43 | void oz_usb_term(void) | |
44 | { | |
b3147863 CK |
45 | oz_hcd_term(); |
46 | } | |
6e244a83 | 47 | |
4e7fb829 | 48 | /* |
b3147863 CK |
49 | * This is called when the USB service is started or resumed for a PD. |
50 | * Context: softirq | |
51 | */ | |
52 | int oz_usb_start(struct oz_pd *pd, int resume) | |
53 | { | |
54 | int rc = 0; | |
55 | struct oz_usb_ctx *usb_ctx; | |
ba346a43 | 56 | struct oz_usb_ctx *old_ctx; |
18f8191e | 57 | |
b3147863 | 58 | if (resume) { |
f724b584 | 59 | oz_dbg(ON, "USB service resumed\n"); |
b3147863 CK |
60 | return 0; |
61 | } | |
f724b584 | 62 | oz_dbg(ON, "USB service started\n"); |
b3147863 CK |
63 | /* Create a USB context in case we need one. If we find the PD already |
64 | * has a USB context then we will destroy it. | |
65 | */ | |
1ec41a31 | 66 | usb_ctx = kzalloc(sizeof(struct oz_usb_ctx), GFP_ATOMIC); |
ba346a43 | 67 | if (usb_ctx == NULL) |
1ec41a31 | 68 | return -ENOMEM; |
b3147863 CK |
69 | atomic_set(&usb_ctx->ref_count, 1); |
70 | usb_ctx->pd = pd; | |
71 | usb_ctx->stopped = 0; | |
72 | /* Install the USB context if the PD doesn't already have one. | |
73 | * If it does already have one then destroy the one we have just | |
74 | * created. | |
75 | */ | |
76 | spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); | |
77 | old_ctx = pd->app_ctx[OZ_APPID_USB-1]; | |
ba346a43 | 78 | if (old_ctx == NULL) |
b3147863 CK |
79 | pd->app_ctx[OZ_APPID_USB-1] = usb_ctx; |
80 | oz_usb_get(pd->app_ctx[OZ_APPID_USB-1]); | |
81 | spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); | |
82 | if (old_ctx) { | |
f724b584 | 83 | oz_dbg(ON, "Already have USB context\n"); |
1ec41a31 | 84 | kfree(usb_ctx); |
b3147863 CK |
85 | usb_ctx = old_ctx; |
86 | } else if (usb_ctx) { | |
87 | /* Take a reference to the PD. This will be released when | |
88 | * the USB context is destroyed. | |
89 | */ | |
90 | oz_pd_get(pd); | |
91 | } | |
92 | /* If we already had a USB context and had obtained a port from | |
93 | * the USB HCD then just reset the port. If we didn't have a port | |
94 | * then report the arrival to the USB HCD so we get one. | |
95 | */ | |
96 | if (usb_ctx->hport) { | |
97 | oz_hcd_pd_reset(usb_ctx, usb_ctx->hport); | |
98 | } else { | |
99 | usb_ctx->hport = oz_hcd_pd_arrived(usb_ctx); | |
ba346a43 | 100 | if (usb_ctx->hport == NULL) { |
f724b584 | 101 | oz_dbg(ON, "USB hub returned null port\n"); |
b3147863 | 102 | spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); |
ba346a43 | 103 | pd->app_ctx[OZ_APPID_USB-1] = NULL; |
b3147863 CK |
104 | spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); |
105 | oz_usb_put(usb_ctx); | |
106 | rc = -1; | |
107 | } | |
108 | } | |
109 | oz_usb_put(usb_ctx); | |
110 | return rc; | |
111 | } | |
6e244a83 | 112 | |
4e7fb829 | 113 | /* |
b3147863 CK |
114 | * This is called when the USB service is stopped or paused for a PD. |
115 | * Context: softirq or process | |
116 | */ | |
117 | void oz_usb_stop(struct oz_pd *pd, int pause) | |
118 | { | |
119 | struct oz_usb_ctx *usb_ctx; | |
18f8191e | 120 | |
b3147863 | 121 | if (pause) { |
f724b584 | 122 | oz_dbg(ON, "USB service paused\n"); |
b3147863 CK |
123 | return; |
124 | } | |
125 | spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); | |
126 | usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1]; | |
ba346a43 | 127 | pd->app_ctx[OZ_APPID_USB-1] = NULL; |
b3147863 CK |
128 | spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); |
129 | if (usb_ctx) { | |
8fd07007 RG |
130 | struct timespec ts, now; |
131 | getnstimeofday(&ts); | |
f724b584 | 132 | oz_dbg(ON, "USB service stopping...\n"); |
b3147863 CK |
133 | usb_ctx->stopped = 1; |
134 | /* At this point the reference count on the usb context should | |
135 | * be 2 - one from when we created it and one from the hcd | |
136 | * which claims a reference. Since stopped = 1 no one else | |
137 | * should get in but someone may already be in. So wait | |
138 | * until they leave but timeout after 1 second. | |
139 | */ | |
8fd07007 RG |
140 | while ((atomic_read(&usb_ctx->ref_count) > 2)) { |
141 | getnstimeofday(&now); | |
142 | /*Approx 1 Sec. this is not perfect calculation*/ | |
143 | if (now.tv_sec != ts.tv_sec) | |
144 | break; | |
145 | } | |
f724b584 | 146 | oz_dbg(ON, "USB service stopped\n"); |
b3147863 CK |
147 | oz_hcd_pd_departed(usb_ctx->hport); |
148 | /* Release the reference taken in oz_usb_start. | |
149 | */ | |
150 | oz_usb_put(usb_ctx); | |
151 | } | |
152 | } | |
6e244a83 | 153 | |
4e7fb829 | 154 | /* |
b3147863 CK |
155 | * This increments the reference count of the context area for a specific PD. |
156 | * This ensures this context area does not disappear while still in use. | |
157 | * Context: softirq | |
158 | */ | |
159 | void oz_usb_get(void *hpd) | |
160 | { | |
161 | struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; | |
18f8191e | 162 | |
b3147863 CK |
163 | atomic_inc(&usb_ctx->ref_count); |
164 | } | |
6e244a83 | 165 | |
4e7fb829 | 166 | /* |
b3147863 CK |
167 | * This decrements the reference count of the context area for a specific PD |
168 | * and destroys the context area if the reference count becomes zero. | |
8fd07007 | 169 | * Context: irq or process |
b3147863 CK |
170 | */ |
171 | void oz_usb_put(void *hpd) | |
172 | { | |
173 | struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; | |
18f8191e | 174 | |
b3147863 | 175 | if (atomic_dec_and_test(&usb_ctx->ref_count)) { |
f724b584 | 176 | oz_dbg(ON, "Dealloc USB context\n"); |
b3147863 | 177 | oz_pd_put(usb_ctx->pd); |
1ec41a31 | 178 | kfree(usb_ctx); |
b3147863 CK |
179 | } |
180 | } | |
6e244a83 | 181 | |
4e7fb829 | 182 | /* |
b3147863 CK |
183 | * Context: softirq |
184 | */ | |
185 | int oz_usb_heartbeat(struct oz_pd *pd) | |
186 | { | |
187 | struct oz_usb_ctx *usb_ctx; | |
188 | int rc = 0; | |
18f8191e | 189 | |
b3147863 CK |
190 | spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); |
191 | usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1]; | |
192 | if (usb_ctx) | |
193 | oz_usb_get(usb_ctx); | |
194 | spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); | |
ba346a43 | 195 | if (usb_ctx == NULL) |
b3147863 CK |
196 | return rc; |
197 | if (usb_ctx->stopped) | |
198 | goto done; | |
199 | if (usb_ctx->hport) | |
200 | if (oz_hcd_heartbeat(usb_ctx->hport)) | |
201 | rc = 1; | |
202 | done: | |
203 | oz_usb_put(usb_ctx); | |
204 | return rc; | |
205 | } | |
6e244a83 | 206 | |
4e7fb829 | 207 | /* |
b3147863 CK |
208 | * Context: softirq |
209 | */ | |
210 | int oz_usb_stream_create(void *hpd, u8 ep_num) | |
211 | { | |
212 | struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; | |
213 | struct oz_pd *pd = usb_ctx->pd; | |
18f8191e | 214 | |
f724b584 | 215 | oz_dbg(ON, "%s: (0x%x)\n", __func__, ep_num); |
b3147863 CK |
216 | if (pd->mode & OZ_F_ISOC_NO_ELTS) { |
217 | oz_isoc_stream_create(pd, ep_num); | |
218 | } else { | |
219 | oz_pd_get(pd); | |
220 | if (oz_elt_stream_create(&pd->elt_buff, ep_num, | |
221 | 4*pd->max_tx_size)) { | |
222 | oz_pd_put(pd); | |
223 | return -1; | |
224 | } | |
225 | } | |
226 | return 0; | |
227 | } | |
6e244a83 | 228 | |
4e7fb829 | 229 | /* |
b3147863 CK |
230 | * Context: softirq |
231 | */ | |
232 | int oz_usb_stream_delete(void *hpd, u8 ep_num) | |
233 | { | |
234 | struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; | |
18f8191e | 235 | |
b3147863 CK |
236 | if (usb_ctx) { |
237 | struct oz_pd *pd = usb_ctx->pd; | |
238 | if (pd) { | |
f724b584 | 239 | oz_dbg(ON, "%s: (0x%x)\n", __func__, ep_num); |
b3147863 CK |
240 | if (pd->mode & OZ_F_ISOC_NO_ELTS) { |
241 | oz_isoc_stream_delete(pd, ep_num); | |
242 | } else { | |
243 | if (oz_elt_stream_delete(&pd->elt_buff, ep_num)) | |
244 | return -1; | |
245 | oz_pd_put(pd); | |
246 | } | |
247 | } | |
248 | } | |
249 | return 0; | |
250 | } | |
6e244a83 | 251 | |
4e7fb829 | 252 | /* |
b3147863 CK |
253 | * Context: softirq or process |
254 | */ | |
255 | void oz_usb_request_heartbeat(void *hpd) | |
256 | { | |
257 | struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; | |
18f8191e | 258 | |
b3147863 CK |
259 | if (usb_ctx && usb_ctx->pd) |
260 | oz_pd_request_heartbeat(usb_ctx->pd); | |
261 | } |