usb-notif.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m over USB
  3. * Notification handling
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. *
  35. * Intel Corporation <linux-wimax@intel.com>
  36. * Yanir Lubetkin <yanirx.lubetkin@intel.com>
  37. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  38. * - Initial implementation
  39. *
  40. *
  41. * The notification endpoint is active when the device is not in boot
  42. * mode; in here we just read and get notifications; based on those,
  43. * we act to either reinitialize the device after a reboot or to
  44. * submit a RX request.
  45. *
  46. * ROADMAP
  47. *
  48. * i2400mu_usb_notification_setup()
  49. *
  50. * i2400mu_usb_notification_release()
  51. *
  52. * i2400mu_usb_notification_cb() Called when a URB is ready
  53. * i2400mu_notif_grok()
  54. * i2400m_is_boot_barker()
  55. * i2400m_dev_reset_handle()
  56. * i2400mu_rx_kick()
  57. */
  58. #include <linux/usb.h>
  59. #include <linux/slab.h>
  60. #include "i2400m-usb.h"
  61. #define D_SUBMODULE notif
  62. #include "usb-debug-levels.h"
  63. static const
  64. __le32 i2400m_ZERO_BARKER[4] = { 0, 0, 0, 0 };
  65. /*
  66. * Process a received notification
  67. *
  68. * In normal operation mode, we can only receive two types of payloads
  69. * on the notification endpoint:
  70. *
  71. * - a reboot barker, we do a bootstrap (the device has reseted).
  72. *
  73. * - a block of zeroes: there is pending data in the IN endpoint
  74. */
  75. static
  76. int i2400mu_notification_grok(struct i2400mu *i2400mu, const void *buf,
  77. size_t buf_len)
  78. {
  79. int ret;
  80. struct device *dev = &i2400mu->usb_iface->dev;
  81. struct i2400m *i2400m = &i2400mu->i2400m;
  82. d_fnstart(4, dev, "(i2400m %p buf %p buf_len %zu)\n",
  83. i2400mu, buf, buf_len);
  84. ret = -EIO;
  85. if (buf_len < sizeof(i2400m_ZERO_BARKER))
  86. /* Not a bug, just ignore */
  87. goto error_bad_size;
  88. ret = 0;
  89. if (!memcmp(i2400m_ZERO_BARKER, buf, sizeof(i2400m_ZERO_BARKER))) {
  90. i2400mu_rx_kick(i2400mu);
  91. goto out;
  92. }
  93. ret = i2400m_is_boot_barker(i2400m, buf, buf_len);
  94. if (unlikely(ret >= 0))
  95. ret = i2400m_dev_reset_handle(i2400m, "device rebooted");
  96. else /* Unknown or unexpected data in the notif message */
  97. i2400m_unknown_barker(i2400m, buf, buf_len);
  98. error_bad_size:
  99. out:
  100. d_fnend(4, dev, "(i2400m %p buf %p buf_len %zu) = %d\n",
  101. i2400mu, buf, buf_len, ret);
  102. return ret;
  103. }
  104. /*
  105. * URB callback for the notification endpoint
  106. *
  107. * @urb: the urb received from the notification endpoint
  108. *
  109. * This function will just process the USB side of the transaction,
  110. * checking everything is fine, pass the processing to
  111. * i2400m_notification_grok() and resubmit the URB.
  112. */
  113. static
  114. void i2400mu_notification_cb(struct urb *urb)
  115. {
  116. int ret;
  117. struct i2400mu *i2400mu = urb->context;
  118. struct device *dev = &i2400mu->usb_iface->dev;
  119. d_fnstart(4, dev, "(urb %p status %d actual_length %d)\n",
  120. urb, urb->status, urb->actual_length);
  121. ret = urb->status;
  122. switch (ret) {
  123. case 0:
  124. ret = i2400mu_notification_grok(i2400mu, urb->transfer_buffer,
  125. urb->actual_length);
  126. if (ret == -EIO && edc_inc(&i2400mu->urb_edc, EDC_MAX_ERRORS,
  127. EDC_ERROR_TIMEFRAME))
  128. goto error_exceeded;
  129. if (ret == -ENOMEM) /* uff...power cycle? shutdown? */
  130. goto error_exceeded;
  131. break;
  132. case -EINVAL: /* while removing driver */
  133. case -ENODEV: /* dev disconnect ... */
  134. case -ENOENT: /* ditto */
  135. case -ESHUTDOWN: /* URB killed */
  136. case -ECONNRESET: /* disconnection */
  137. goto out; /* Notify around */
  138. default: /* Some error? */
  139. if (edc_inc(&i2400mu->urb_edc,
  140. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
  141. goto error_exceeded;
  142. dev_err(dev, "notification: URB error %d, retrying\n",
  143. urb->status);
  144. }
  145. usb_mark_last_busy(i2400mu->usb_dev);
  146. ret = usb_submit_urb(i2400mu->notif_urb, GFP_ATOMIC);
  147. switch (ret) {
  148. case 0:
  149. case -EINVAL: /* while removing driver */
  150. case -ENODEV: /* dev disconnect ... */
  151. case -ENOENT: /* ditto */
  152. case -ESHUTDOWN: /* URB killed */
  153. case -ECONNRESET: /* disconnection */
  154. break; /* just ignore */
  155. default: /* Some error? */
  156. dev_err(dev, "notification: cannot submit URB: %d\n", ret);
  157. goto error_submit;
  158. }
  159. d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
  160. urb, urb->status, urb->actual_length);
  161. return;
  162. error_exceeded:
  163. dev_err(dev, "maximum errors in notification URB exceeded; "
  164. "resetting device\n");
  165. error_submit:
  166. usb_queue_reset_device(i2400mu->usb_iface);
  167. out:
  168. d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
  169. urb, urb->status, urb->actual_length);
  170. }
  171. /*
  172. * setup the notification endpoint
  173. *
  174. * @i2400m: device descriptor
  175. *
  176. * This procedure prepares the notification urb and handler for receiving
  177. * unsolicited barkers from the device.
  178. */
  179. int i2400mu_notification_setup(struct i2400mu *i2400mu)
  180. {
  181. struct device *dev = &i2400mu->usb_iface->dev;
  182. int usb_pipe, ret = 0;
  183. struct usb_endpoint_descriptor *epd;
  184. char *buf;
  185. d_fnstart(4, dev, "(i2400m %p)\n", i2400mu);
  186. buf = kmalloc(I2400MU_MAX_NOTIFICATION_LEN, GFP_KERNEL | GFP_DMA);
  187. if (buf == NULL) {
  188. ret = -ENOMEM;
  189. goto error_buf_alloc;
  190. }
  191. i2400mu->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
  192. if (!i2400mu->notif_urb) {
  193. ret = -ENOMEM;
  194. goto error_alloc_urb;
  195. }
  196. epd = usb_get_epd(i2400mu->usb_iface,
  197. i2400mu->endpoint_cfg.notification);
  198. usb_pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  199. usb_fill_int_urb(i2400mu->notif_urb, i2400mu->usb_dev, usb_pipe,
  200. buf, I2400MU_MAX_NOTIFICATION_LEN,
  201. i2400mu_notification_cb, i2400mu, epd->bInterval);
  202. ret = usb_submit_urb(i2400mu->notif_urb, GFP_KERNEL);
  203. if (ret != 0) {
  204. dev_err(dev, "notification: cannot submit URB: %d\n", ret);
  205. goto error_submit;
  206. }
  207. d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
  208. return ret;
  209. error_submit:
  210. usb_free_urb(i2400mu->notif_urb);
  211. error_alloc_urb:
  212. kfree(buf);
  213. error_buf_alloc:
  214. d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
  215. return ret;
  216. }
  217. /*
  218. * Tear down of the notification mechanism
  219. *
  220. * @i2400m: device descriptor
  221. *
  222. * Kill the interrupt endpoint urb, free any allocated resources.
  223. *
  224. * We need to check if we have done it before as for example,
  225. * _suspend() call this; if after a suspend() we get a _disconnect()
  226. * (as the case is when hibernating), nothing bad happens.
  227. */
  228. void i2400mu_notification_release(struct i2400mu *i2400mu)
  229. {
  230. struct device *dev = &i2400mu->usb_iface->dev;
  231. d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
  232. if (i2400mu->notif_urb != NULL) {
  233. usb_kill_urb(i2400mu->notif_urb);
  234. kfree(i2400mu->notif_urb->transfer_buffer);
  235. usb_free_urb(i2400mu->notif_urb);
  236. i2400mu->notif_urb = NULL;
  237. }
  238. d_fnend(4, dev, "(i2400mu %p)\n", i2400mu);
  239. }