usb-notif.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_dev_reset_handle()
  55. * i2400mu_rx_kick()
  56. */
  57. #include <linux/usb.h>
  58. #include "i2400m-usb.h"
  59. #define D_SUBMODULE notif
  60. #include "usb-debug-levels.h"
  61. static const
  62. __le32 i2400m_ZERO_BARKER[4] = { 0, 0, 0, 0 };
  63. /*
  64. * Process a received notification
  65. *
  66. * In normal operation mode, we can only receive two types of payloads
  67. * on the notification endpoint:
  68. *
  69. * - a reboot barker, we do a bootstrap (the device has reseted).
  70. *
  71. * - a block of zeroes: there is pending data in the IN endpoint
  72. */
  73. static
  74. int i2400mu_notification_grok(struct i2400mu *i2400mu, const void *buf,
  75. size_t buf_len)
  76. {
  77. int ret;
  78. struct device *dev = &i2400mu->usb_iface->dev;
  79. struct i2400m *i2400m = &i2400mu->i2400m;
  80. d_fnstart(4, dev, "(i2400m %p buf %p buf_len %zu)\n",
  81. i2400mu, buf, buf_len);
  82. ret = -EIO;
  83. if (buf_len < sizeof(i2400m_NBOOT_BARKER))
  84. /* Not a bug, just ignore */
  85. goto error_bad_size;
  86. if (!memcmp(i2400m_NBOOT_BARKER, buf, sizeof(i2400m_NBOOT_BARKER))
  87. || !memcmp(i2400m_SBOOT_BARKER, buf, sizeof(i2400m_SBOOT_BARKER)))
  88. ret = i2400m_dev_reset_handle(i2400m);
  89. else if (!memcmp(i2400m_ZERO_BARKER, buf, sizeof(i2400m_ZERO_BARKER))) {
  90. i2400mu_rx_kick(i2400mu);
  91. ret = 0;
  92. } else { /* Unknown or unexpected data in the notif message */
  93. char prefix[64];
  94. ret = -EIO;
  95. dev_err(dev, "HW BUG? Unknown/unexpected data in notification "
  96. "message (%zu bytes)\n", buf_len);
  97. snprintf(prefix, sizeof(prefix), "%s %s: ",
  98. dev_driver_string(dev), dev_name(dev));
  99. if (buf_len > 64) {
  100. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  101. 8, 4, buf, 64, 0);
  102. printk(KERN_ERR "%s... (only first 64 bytes "
  103. "dumped)\n", prefix);
  104. } else
  105. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  106. 8, 4, buf, buf_len, 0);
  107. }
  108. error_bad_size:
  109. d_fnend(4, dev, "(i2400m %p buf %p buf_len %zu) = %d\n",
  110. i2400mu, buf, buf_len, ret);
  111. return ret;
  112. }
  113. /*
  114. * URB callback for the notification endpoint
  115. *
  116. * @urb: the urb received from the notification endpoint
  117. *
  118. * This function will just process the USB side of the transaction,
  119. * checking everything is fine, pass the processing to
  120. * i2400m_notification_grok() and resubmit the URB.
  121. */
  122. static
  123. void i2400mu_notification_cb(struct urb *urb)
  124. {
  125. int ret;
  126. struct i2400mu *i2400mu = urb->context;
  127. struct device *dev = &i2400mu->usb_iface->dev;
  128. d_fnstart(4, dev, "(urb %p status %d actual_length %d)\n",
  129. urb, urb->status, urb->actual_length);
  130. ret = urb->status;
  131. switch (ret) {
  132. case 0:
  133. ret = i2400mu_notification_grok(i2400mu, urb->transfer_buffer,
  134. urb->actual_length);
  135. if (ret == -EIO && edc_inc(&i2400mu->urb_edc, EDC_MAX_ERRORS,
  136. EDC_ERROR_TIMEFRAME))
  137. goto error_exceeded;
  138. if (ret == -ENOMEM) /* uff...power cycle? shutdown? */
  139. goto error_exceeded;
  140. break;
  141. case -EINVAL: /* while removing driver */
  142. case -ENODEV: /* dev disconnect ... */
  143. case -ENOENT: /* ditto */
  144. case -ESHUTDOWN: /* URB killed */
  145. case -ECONNRESET: /* disconnection */
  146. goto out; /* Notify around */
  147. default: /* Some error? */
  148. if (edc_inc(&i2400mu->urb_edc,
  149. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
  150. goto error_exceeded;
  151. dev_err(dev, "notification: URB error %d, retrying\n",
  152. urb->status);
  153. }
  154. usb_mark_last_busy(i2400mu->usb_dev);
  155. ret = usb_submit_urb(i2400mu->notif_urb, GFP_ATOMIC);
  156. switch (ret) {
  157. case 0:
  158. case -EINVAL: /* while removing driver */
  159. case -ENODEV: /* dev disconnect ... */
  160. case -ENOENT: /* ditto */
  161. case -ESHUTDOWN: /* URB killed */
  162. case -ECONNRESET: /* disconnection */
  163. break; /* just ignore */
  164. default: /* Some error? */
  165. dev_err(dev, "notification: cannot submit URB: %d\n", ret);
  166. goto error_submit;
  167. }
  168. d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
  169. urb, urb->status, urb->actual_length);
  170. return;
  171. error_exceeded:
  172. dev_err(dev, "maximum errors in notification URB exceeded; "
  173. "resetting device\n");
  174. error_submit:
  175. usb_queue_reset_device(i2400mu->usb_iface);
  176. out:
  177. d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
  178. urb, urb->status, urb->actual_length);
  179. return;
  180. }
  181. /*
  182. * setup the notification endpoint
  183. *
  184. * @i2400m: device descriptor
  185. *
  186. * This procedure prepares the notification urb and handler for receiving
  187. * unsolicited barkers from the device.
  188. */
  189. int i2400mu_notification_setup(struct i2400mu *i2400mu)
  190. {
  191. struct device *dev = &i2400mu->usb_iface->dev;
  192. int usb_pipe, ret = 0;
  193. struct usb_endpoint_descriptor *epd;
  194. char *buf;
  195. d_fnstart(4, dev, "(i2400m %p)\n", i2400mu);
  196. buf = kmalloc(I2400MU_MAX_NOTIFICATION_LEN, GFP_KERNEL | GFP_DMA);
  197. if (buf == NULL) {
  198. dev_err(dev, "notification: buffer allocation failed\n");
  199. ret = -ENOMEM;
  200. goto error_buf_alloc;
  201. }
  202. i2400mu->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
  203. if (!i2400mu->notif_urb) {
  204. ret = -ENOMEM;
  205. dev_err(dev, "notification: cannot allocate URB\n");
  206. goto error_alloc_urb;
  207. }
  208. epd = usb_get_epd(i2400mu->usb_iface, I2400MU_EP_NOTIFICATION);
  209. usb_pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  210. usb_fill_int_urb(i2400mu->notif_urb, i2400mu->usb_dev, usb_pipe,
  211. buf, I2400MU_MAX_NOTIFICATION_LEN,
  212. i2400mu_notification_cb, i2400mu, epd->bInterval);
  213. ret = usb_submit_urb(i2400mu->notif_urb, GFP_KERNEL);
  214. if (ret != 0) {
  215. dev_err(dev, "notification: cannot submit URB: %d\n", ret);
  216. goto error_submit;
  217. }
  218. d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
  219. return ret;
  220. error_submit:
  221. usb_free_urb(i2400mu->notif_urb);
  222. error_alloc_urb:
  223. kfree(buf);
  224. error_buf_alloc:
  225. d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
  226. return ret;
  227. }
  228. /*
  229. * Tear down of the notification mechanism
  230. *
  231. * @i2400m: device descriptor
  232. *
  233. * Kill the interrupt endpoint urb, free any allocated resources.
  234. *
  235. * We need to check if we have done it before as for example,
  236. * _suspend() call this; if after a suspend() we get a _disconnect()
  237. * (as the case is when hibernating), nothing bad happens.
  238. */
  239. void i2400mu_notification_release(struct i2400mu *i2400mu)
  240. {
  241. struct device *dev = &i2400mu->usb_iface->dev;
  242. d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
  243. if (i2400mu->notif_urb != NULL) {
  244. usb_kill_urb(i2400mu->notif_urb);
  245. kfree(i2400mu->notif_urb->transfer_buffer);
  246. usb_free_urb(i2400mu->notif_urb);
  247. i2400mu->notif_urb = NULL;
  248. }
  249. d_fnend(4, dev, "(i2400mu %p)\n", i2400mu);
  250. }