]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ozwpan/ozusbsvc.c
Merge branch 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.git] / drivers / staging / ozwpan / ozusbsvc.c
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 #include <linux/init.h>
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 "ozconfig.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 "oztrace.h"
29 #include "ozusbsvc.h"
30 /*------------------------------------------------------------------------------
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 {
36         return oz_hcd_init();
37 }
38 /*------------------------------------------------------------------------------
39  * This is called once when the driver is unloaded to terminate the USB service.
40  * Context: process
41  */
42 void oz_usb_term(void)
43 {
44         oz_hcd_term();
45 }
46 /*------------------------------------------------------------------------------
47  * This is called when the USB service is started or resumed for a PD.
48  * Context: softirq
49  */
50 int oz_usb_start(struct oz_pd *pd, int resume)
51 {
52         int rc = 0;
53         struct oz_usb_ctx *usb_ctx;
54         struct oz_usb_ctx *old_ctx;
55         if (resume) {
56                 oz_trace("USB service resumed.\n");
57                 return 0;
58         }
59         oz_trace("USB service started.\n");
60         /* Create a USB context in case we need one. If we find the PD already
61          * has a USB context then we will destroy it.
62          */
63         usb_ctx = kzalloc(sizeof(struct oz_usb_ctx), GFP_ATOMIC);
64         if (usb_ctx == NULL)
65                 return -ENOMEM;
66         atomic_set(&usb_ctx->ref_count, 1);
67         usb_ctx->pd = pd;
68         usb_ctx->stopped = 0;
69         /* Install the USB context if the PD doesn't already have one.
70          * If it does already have one then destroy the one we have just
71          * created.
72          */
73         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
74         old_ctx = pd->app_ctx[OZ_APPID_USB-1];
75         if (old_ctx == NULL)
76                 pd->app_ctx[OZ_APPID_USB-1] = usb_ctx;
77         oz_usb_get(pd->app_ctx[OZ_APPID_USB-1]);
78         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
79         if (old_ctx) {
80                 oz_trace("Already have USB context.\n");
81                 kfree(usb_ctx);
82                 usb_ctx = old_ctx;
83         } else if (usb_ctx) {
84                 /* Take a reference to the PD. This will be released when
85                  * the USB context is destroyed.
86                  */
87                 oz_pd_get(pd);
88         }
89         /* If we already had a USB context and had obtained a port from
90          * the USB HCD then just reset the port. If we didn't have a port
91          * then report the arrival to the USB HCD so we get one.
92          */
93         if (usb_ctx->hport) {
94                 oz_hcd_pd_reset(usb_ctx, usb_ctx->hport);
95         } else {
96                 usb_ctx->hport = oz_hcd_pd_arrived(usb_ctx);
97                 if (usb_ctx->hport == NULL) {
98                         oz_trace("USB hub returned null port.\n");
99                         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
100                         pd->app_ctx[OZ_APPID_USB-1] = NULL;
101                         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
102                         oz_usb_put(usb_ctx);
103                         rc = -1;
104                 }
105         }
106         oz_usb_put(usb_ctx);
107         return rc;
108 }
109 /*------------------------------------------------------------------------------
110  * This is called when the USB service is stopped or paused for a PD.
111  * Context: softirq or process
112  */
113 void oz_usb_stop(struct oz_pd *pd, int pause)
114 {
115         struct oz_usb_ctx *usb_ctx;
116         if (pause) {
117                 oz_trace("USB service paused.\n");
118                 return;
119         }
120         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
121         usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
122         pd->app_ctx[OZ_APPID_USB-1] = NULL;
123         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
124         if (usb_ctx) {
125                 unsigned long tout = jiffies + HZ;
126                 oz_trace("USB service stopping...\n");
127                 usb_ctx->stopped = 1;
128                 /* At this point the reference count on the usb context should
129                  * be 2 - one from when we created it and one from the hcd
130                  * which claims a reference. Since stopped = 1 no one else
131                  * should get in but someone may already be in. So wait
132                  * until they leave but timeout after 1 second.
133                  */
134                 while ((atomic_read(&usb_ctx->ref_count) > 2) &&
135                         time_before(jiffies, tout))
136                         ;
137                 oz_trace("USB service stopped.\n");
138                 oz_hcd_pd_departed(usb_ctx->hport);
139                 /* Release the reference taken in oz_usb_start.
140                  */
141                 oz_usb_put(usb_ctx);
142         }
143 }
144 /*------------------------------------------------------------------------------
145  * This increments the reference count of the context area for a specific PD.
146  * This ensures this context area does not disappear while still in use.
147  * Context: softirq
148  */
149 void oz_usb_get(void *hpd)
150 {
151         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
152         atomic_inc(&usb_ctx->ref_count);
153 }
154 /*------------------------------------------------------------------------------
155  * This decrements the reference count of the context area for a specific PD
156  * and destroys the context area if the reference count becomes zero.
157  * Context: softirq or process
158  */
159 void oz_usb_put(void *hpd)
160 {
161         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
162         if (atomic_dec_and_test(&usb_ctx->ref_count)) {
163                 oz_trace("Dealloc USB context.\n");
164                 oz_pd_put(usb_ctx->pd);
165                 kfree(usb_ctx);
166         }
167 }
168 /*------------------------------------------------------------------------------
169  * Context: softirq
170  */
171 int oz_usb_heartbeat(struct oz_pd *pd)
172 {
173         struct oz_usb_ctx *usb_ctx;
174         int rc = 0;
175         spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
176         usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
177         if (usb_ctx)
178                 oz_usb_get(usb_ctx);
179         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
180         if (usb_ctx == NULL)
181                 return rc;
182         if (usb_ctx->stopped)
183                 goto done;
184         if (usb_ctx->hport)
185                 if (oz_hcd_heartbeat(usb_ctx->hport))
186                         rc = 1;
187 done:
188         oz_usb_put(usb_ctx);
189         return rc;
190 }
191 /*------------------------------------------------------------------------------
192  * Context: softirq
193  */
194 int oz_usb_stream_create(void *hpd, u8 ep_num)
195 {
196         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
197         struct oz_pd *pd = usb_ctx->pd;
198         oz_trace("oz_usb_stream_create(0x%x)\n", ep_num);
199         if (pd->mode & OZ_F_ISOC_NO_ELTS) {
200                 oz_isoc_stream_create(pd, ep_num);
201         } else {
202                 oz_pd_get(pd);
203                 if (oz_elt_stream_create(&pd->elt_buff, ep_num,
204                         4*pd->max_tx_size)) {
205                         oz_pd_put(pd);
206                         return -1;
207                 }
208         }
209         return 0;
210 }
211 /*------------------------------------------------------------------------------
212  * Context: softirq
213  */
214 int oz_usb_stream_delete(void *hpd, u8 ep_num)
215 {
216         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
217         if (usb_ctx) {
218                 struct oz_pd *pd = usb_ctx->pd;
219                 if (pd) {
220                         oz_trace("oz_usb_stream_delete(0x%x)\n", ep_num);
221                         if (pd->mode & OZ_F_ISOC_NO_ELTS) {
222                                 oz_isoc_stream_delete(pd, ep_num);
223                         } else {
224                                 if (oz_elt_stream_delete(&pd->elt_buff, ep_num))
225                                         return -1;
226                                 oz_pd_put(pd);
227                         }
228                 }
229         }
230         return 0;
231 }
232 /*------------------------------------------------------------------------------
233  * Context: softirq or process
234  */
235 void oz_usb_request_heartbeat(void *hpd)
236 {
237         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
238         if (usb_ctx && usb_ctx->pd)
239                 oz_pd_request_heartbeat(usb_ctx->pd);
240 }