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