ozusbsvc.c 6.5 KB

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