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