• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
10  * The implementation of this service uses ozhcd.c to implement a USB HCD.
11  * -----------------------------------------------------------------------------
12  */
13 
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>
21 #include "ozdbg.h"
22 #include "ozprotocol.h"
23 #include "ozeltbuf.h"
24 #include "ozpd.h"
25 #include "ozproto.h"
26 #include "ozusbif.h"
27 #include "ozhcd.h"
28 #include "ozusbsvc.h"
29 
30 /*
31  * This is called once when the driver is loaded to initialise the USB service.
32  * Context: process
33  */
oz_usb_init(void)34 int oz_usb_init(void)
35 {
36 	return oz_hcd_init();
37 }
38 
39 /*
40  * This is called once when the driver is unloaded to terminate the USB service.
41  * Context: process
42  */
oz_usb_term(void)43 void oz_usb_term(void)
44 {
45 	oz_hcd_term();
46 }
47 
48 /*
49  * This is called when the USB service is started or resumed for a PD.
50  * Context: softirq
51  */
oz_usb_start(struct oz_pd * pd,int resume)52 int oz_usb_start(struct oz_pd *pd, int resume)
53 {
54 	int rc = 0;
55 	struct oz_usb_ctx *usb_ctx;
56 	struct oz_usb_ctx *old_ctx;
57 
58 	if (resume) {
59 		oz_dbg(ON, "USB service resumed\n");
60 		return 0;
61 	}
62 	oz_dbg(ON, "USB service started\n");
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 	 */
66 	usb_ctx = kzalloc(sizeof(struct oz_usb_ctx), GFP_ATOMIC);
67 	if (usb_ctx == NULL)
68 		return -ENOMEM;
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]);
77 	old_ctx = pd->app_ctx[OZ_APPID_USB];
78 	if (old_ctx == NULL)
79 		pd->app_ctx[OZ_APPID_USB] = usb_ctx;
80 	oz_usb_get(pd->app_ctx[OZ_APPID_USB]);
81 	spin_unlock_bh(&pd->app_lock[OZ_APPID_USB]);
82 	if (old_ctx) {
83 		oz_dbg(ON, "Already have USB context\n");
84 		kfree(usb_ctx);
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);
100 		if (usb_ctx->hport == NULL) {
101 			oz_dbg(ON, "USB hub returned null port\n");
102 			spin_lock_bh(&pd->app_lock[OZ_APPID_USB]);
103 			pd->app_ctx[OZ_APPID_USB] = NULL;
104 			spin_unlock_bh(&pd->app_lock[OZ_APPID_USB]);
105 			oz_usb_put(usb_ctx);
106 			rc = -1;
107 		}
108 	}
109 	oz_usb_put(usb_ctx);
110 	return rc;
111 }
112 
113 /*
114  * This is called when the USB service is stopped or paused for a PD.
115  * Context: softirq or process
116  */
oz_usb_stop(struct oz_pd * pd,int pause)117 void oz_usb_stop(struct oz_pd *pd, int pause)
118 {
119 	struct oz_usb_ctx *usb_ctx;
120 
121 	if (pause) {
122 		oz_dbg(ON, "USB service paused\n");
123 		return;
124 	}
125 	spin_lock_bh(&pd->app_lock[OZ_APPID_USB]);
126 	usb_ctx = (struct oz_usb_ctx *) pd->app_ctx[OZ_APPID_USB];
127 	pd->app_ctx[OZ_APPID_USB] = NULL;
128 	spin_unlock_bh(&pd->app_lock[OZ_APPID_USB]);
129 	if (usb_ctx) {
130 		struct timespec ts, now;
131 
132 		getnstimeofday(&ts);
133 		oz_dbg(ON, "USB service stopping...\n");
134 		usb_ctx->stopped = 1;
135 		/* At this point the reference count on the usb context should
136 		 * be 2 - one from when we created it and one from the hcd
137 		 * which claims a reference. Since stopped = 1 no one else
138 		 * should get in but someone may already be in. So wait
139 		 * until they leave but timeout after 1 second.
140 		 */
141 		while ((atomic_read(&usb_ctx->ref_count) > 2)) {
142 			getnstimeofday(&now);
143 			/*Approx 1 Sec. this is not perfect calculation*/
144 			if (now.tv_sec != ts.tv_sec)
145 				break;
146 		}
147 		oz_dbg(ON, "USB service stopped\n");
148 		oz_hcd_pd_departed(usb_ctx->hport);
149 		/* Release the reference taken in oz_usb_start.
150 		 */
151 		oz_usb_put(usb_ctx);
152 	}
153 }
154 
155 /*
156  * This increments the reference count of the context area for a specific PD.
157  * This ensures this context area does not disappear while still in use.
158  * Context: softirq
159  */
oz_usb_get(void * hpd)160 void oz_usb_get(void *hpd)
161 {
162 	struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
163 
164 	atomic_inc(&usb_ctx->ref_count);
165 }
166 
167 /*
168  * This decrements the reference count of the context area for a specific PD
169  * and destroys the context area if the reference count becomes zero.
170  * Context: irq or process
171  */
oz_usb_put(void * hpd)172 void oz_usb_put(void *hpd)
173 {
174 	struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
175 
176 	if (atomic_dec_and_test(&usb_ctx->ref_count)) {
177 		oz_dbg(ON, "Dealloc USB context\n");
178 		oz_pd_put(usb_ctx->pd);
179 		kfree(usb_ctx);
180 	}
181 }
182 
183 /*
184  * Context: softirq
185  */
oz_usb_heartbeat(struct oz_pd * pd)186 int oz_usb_heartbeat(struct oz_pd *pd)
187 {
188 	struct oz_usb_ctx *usb_ctx;
189 	int rc = 0;
190 
191 	spin_lock_bh(&pd->app_lock[OZ_APPID_USB]);
192 	usb_ctx = (struct oz_usb_ctx *) pd->app_ctx[OZ_APPID_USB];
193 	if (usb_ctx)
194 		oz_usb_get(usb_ctx);
195 	spin_unlock_bh(&pd->app_lock[OZ_APPID_USB]);
196 	if (usb_ctx == NULL)
197 		return rc;
198 	if (usb_ctx->stopped)
199 		goto done;
200 	if (usb_ctx->hport)
201 		if (oz_hcd_heartbeat(usb_ctx->hport))
202 			rc = 1;
203 done:
204 	oz_usb_put(usb_ctx);
205 	return rc;
206 }
207 
208 /*
209  * Context: softirq
210  */
oz_usb_stream_create(void * hpd,u8 ep_num)211 int oz_usb_stream_create(void *hpd, u8 ep_num)
212 {
213 	struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
214 	struct oz_pd *pd = usb_ctx->pd;
215 
216 	oz_dbg(ON, "%s: (0x%x)\n", __func__, ep_num);
217 	if (pd->mode & OZ_F_ISOC_NO_ELTS) {
218 		oz_isoc_stream_create(pd, ep_num);
219 	} else {
220 		oz_pd_get(pd);
221 		if (oz_elt_stream_create(&pd->elt_buff, ep_num,
222 			4*pd->max_tx_size)) {
223 			oz_pd_put(pd);
224 			return -1;
225 		}
226 	}
227 	return 0;
228 }
229 
230 /*
231  * Context: softirq
232  */
oz_usb_stream_delete(void * hpd,u8 ep_num)233 int oz_usb_stream_delete(void *hpd, u8 ep_num)
234 {
235 	struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
236 
237 	if (usb_ctx) {
238 		struct oz_pd *pd = usb_ctx->pd;
239 
240 		if (pd) {
241 			oz_dbg(ON, "%s: (0x%x)\n", __func__, ep_num);
242 			if (pd->mode & OZ_F_ISOC_NO_ELTS) {
243 				oz_isoc_stream_delete(pd, ep_num);
244 			} else {
245 				if (oz_elt_stream_delete(&pd->elt_buff, ep_num))
246 					return -1;
247 				oz_pd_put(pd);
248 			}
249 		}
250 	}
251 	return 0;
252 }
253 
254 /*
255  * Context: softirq or process
256  */
oz_usb_request_heartbeat(void * hpd)257 void oz_usb_request_heartbeat(void *hpd)
258 {
259 	struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
260 
261 	if (usb_ctx && usb_ctx->pd)
262 		oz_pd_request_heartbeat(usb_ctx->pd);
263 }
264