wa-nep.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
  3. * Notification EndPoint support
  4. *
  5. * Copyright (C) 2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * This part takes care of getting the notification from the hw
  24. * only and dispatching through wusbwad into
  25. * wa_notif_dispatch. Handling is done there.
  26. *
  27. * WA notifications are limited in size; most of them are three or
  28. * four bytes long, and the longest is the HWA Device Notification,
  29. * which would not exceed 38 bytes (DNs are limited in payload to 32
  30. * bytes plus 3 bytes header (WUSB1.0[7.6p2]), plus 3 bytes HWA
  31. * header (WUSB1.0[8.5.4.2]).
  32. *
  33. * It is not clear if more than one Device Notification can be packed
  34. * in a HWA Notification, I assume no because of the wording in
  35. * WUSB1.0[8.5.4.2]. In any case, the bigger any notification could
  36. * get is 256 bytes (as the bLength field is a byte).
  37. *
  38. * So what we do is we have this buffer and read into it; when a
  39. * notification arrives we schedule work to a specific, single thread
  40. * workqueue (so notifications are serialized) and copy the
  41. * notification data. After scheduling the work, we rearm the read from
  42. * the notification endpoint.
  43. *
  44. * Entry points here are:
  45. *
  46. * wa_nep_[create|destroy]() To initialize/release this subsystem
  47. *
  48. * wa_nep_cb() Callback for the notification
  49. * endpoint; when data is ready, this
  50. * does the dispatching.
  51. */
  52. #include <linux/workqueue.h>
  53. #include <linux/ctype.h>
  54. #include <linux/uwb/debug.h>
  55. #include "wa-hc.h"
  56. #include "wusbhc.h"
  57. /* Structure for queueing notifications to the workqueue */
  58. struct wa_notif_work {
  59. struct work_struct work;
  60. struct wahc *wa;
  61. size_t size;
  62. u8 data[];
  63. };
  64. /*
  65. * Process incoming notifications from the WA's Notification EndPoint
  66. * [the wuswad daemon, basically]
  67. *
  68. * @_nw: Pointer to a descriptor which has the pointer to the
  69. * @wa, the size of the buffer and the work queue
  70. * structure (so we can free all when done).
  71. * @returns 0 if ok, < 0 errno code on error.
  72. *
  73. * All notifications follow the same format; they need to start with a
  74. * 'struct wa_notif_hdr' header, so it is easy to parse through
  75. * them. We just break the buffer in individual notifications (the
  76. * standard doesn't say if it can be done or is forbidden, so we are
  77. * cautious) and dispatch each.
  78. *
  79. * So the handling layers are is:
  80. *
  81. * WA specific notification (from NEP)
  82. * Device Notification Received -> wa_handle_notif_dn()
  83. * WUSB Device notification generic handling
  84. * BPST Adjustment -> wa_handle_notif_bpst_adj()
  85. * ... -> ...
  86. *
  87. * @wa has to be referenced
  88. */
  89. static void wa_notif_dispatch(struct work_struct *ws)
  90. {
  91. void *itr;
  92. u8 missing = 0;
  93. struct wa_notif_work *nw = container_of(ws, struct wa_notif_work, work);
  94. struct wahc *wa = nw->wa;
  95. struct wa_notif_hdr *notif_hdr;
  96. size_t size;
  97. struct device *dev = &wa->usb_iface->dev;
  98. #if 0
  99. /* FIXME: need to check for this??? */
  100. if (usb_hcd->state == HC_STATE_QUIESCING) /* Going down? */
  101. goto out; /* screw it */
  102. #endif
  103. atomic_dec(&wa->notifs_queued); /* Throttling ctl */
  104. dev = &wa->usb_iface->dev;
  105. size = nw->size;
  106. itr = nw->data;
  107. while (size) {
  108. if (size < sizeof(*notif_hdr)) {
  109. missing = sizeof(*notif_hdr) - size;
  110. goto exhausted_buffer;
  111. }
  112. notif_hdr = itr;
  113. if (size < notif_hdr->bLength)
  114. goto exhausted_buffer;
  115. itr += notif_hdr->bLength;
  116. size -= notif_hdr->bLength;
  117. /* Dispatch the notification [don't use itr or size!] */
  118. switch (notif_hdr->bNotifyType) {
  119. case HWA_NOTIF_DN: {
  120. struct hwa_notif_dn *hwa_dn;
  121. hwa_dn = container_of(notif_hdr, struct hwa_notif_dn,
  122. hdr);
  123. wusbhc_handle_dn(wa->wusb, hwa_dn->bSourceDeviceAddr,
  124. hwa_dn->dndata,
  125. notif_hdr->bLength - sizeof(*hwa_dn));
  126. break;
  127. }
  128. case WA_NOTIF_TRANSFER:
  129. wa_handle_notif_xfer(wa, notif_hdr);
  130. break;
  131. case DWA_NOTIF_RWAKE:
  132. case DWA_NOTIF_PORTSTATUS:
  133. case HWA_NOTIF_BPST_ADJ:
  134. /* FIXME: unimplemented WA NOTIFs */
  135. /* fallthru */
  136. default:
  137. if (printk_ratelimit()) {
  138. dev_err(dev, "HWA: unknown notification 0x%x, "
  139. "%zu bytes; discarding\n",
  140. notif_hdr->bNotifyType,
  141. (size_t)notif_hdr->bLength);
  142. dump_bytes(dev, notif_hdr, 16);
  143. }
  144. break;
  145. }
  146. }
  147. out:
  148. wa_put(wa);
  149. kfree(nw);
  150. return;
  151. /* THIS SHOULD NOT HAPPEN
  152. *
  153. * Buffer exahusted with partial data remaining; just warn and
  154. * discard the data, as this should not happen.
  155. */
  156. exhausted_buffer:
  157. if (!printk_ratelimit())
  158. goto out;
  159. dev_warn(dev, "HWA: device sent short notification, "
  160. "%d bytes missing; discarding %d bytes.\n",
  161. missing, (int)size);
  162. dump_bytes(dev, itr, size);
  163. goto out;
  164. }
  165. /*
  166. * Deliver incoming WA notifications to the wusbwa workqueue
  167. *
  168. * @wa: Pointer the Wire Adapter Controller Data Streaming
  169. * instance (part of an 'struct usb_hcd').
  170. * @size: Size of the received buffer
  171. * @returns 0 if ok, < 0 errno code on error.
  172. *
  173. * The input buffer is @wa->nep_buffer, with @size bytes
  174. * (guaranteed to fit in the allocated space,
  175. * @wa->nep_buffer_size).
  176. */
  177. static int wa_nep_queue(struct wahc *wa, size_t size)
  178. {
  179. int result = 0;
  180. struct device *dev = &wa->usb_iface->dev;
  181. struct wa_notif_work *nw;
  182. /* dev_fnstart(dev, "(wa %p, size %zu)\n", wa, size); */
  183. BUG_ON(size > wa->nep_buffer_size);
  184. if (size == 0)
  185. goto out;
  186. if (atomic_read(&wa->notifs_queued) > 200) {
  187. if (printk_ratelimit())
  188. dev_err(dev, "Too many notifications queued, "
  189. "throttling back\n");
  190. goto out;
  191. }
  192. nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
  193. if (nw == NULL) {
  194. if (printk_ratelimit())
  195. dev_err(dev, "No memory to queue notification\n");
  196. goto out;
  197. }
  198. INIT_WORK(&nw->work, wa_notif_dispatch);
  199. nw->wa = wa_get(wa);
  200. nw->size = size;
  201. memcpy(nw->data, wa->nep_buffer, size);
  202. atomic_inc(&wa->notifs_queued); /* Throttling ctl */
  203. queue_work(wusbd, &nw->work);
  204. out:
  205. /* dev_fnend(dev, "(wa %p, size %zu) = result\n", wa, size, result); */
  206. return result;
  207. }
  208. /*
  209. * Callback for the notification event endpoint
  210. *
  211. * Check's that everything is fine and then passes the data to be
  212. * queued to the workqueue.
  213. */
  214. static void wa_nep_cb(struct urb *urb)
  215. {
  216. int result;
  217. struct wahc *wa = urb->context;
  218. struct device *dev = &wa->usb_iface->dev;
  219. switch (result = urb->status) {
  220. case 0:
  221. result = wa_nep_queue(wa, urb->actual_length);
  222. if (result < 0)
  223. dev_err(dev, "NEP: unable to process notification(s): "
  224. "%d\n", result);
  225. break;
  226. case -ECONNRESET: /* Not an error, but a controlled situation; */
  227. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  228. case -ESHUTDOWN:
  229. dev_dbg(dev, "NEP: going down %d\n", urb->status);
  230. goto out;
  231. default: /* On general errors, we retry unless it gets ugly */
  232. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  233. EDC_ERROR_TIMEFRAME)) {
  234. dev_err(dev, "NEP: URB max acceptable errors "
  235. "exceeded, resetting device\n");
  236. wa_reset_all(wa);
  237. goto out;
  238. }
  239. dev_err(dev, "NEP: URB error %d\n", urb->status);
  240. }
  241. result = wa_nep_arm(wa, GFP_ATOMIC);
  242. if (result < 0) {
  243. dev_err(dev, "NEP: cannot submit URB: %d\n", result);
  244. wa_reset_all(wa);
  245. }
  246. out:
  247. return;
  248. }
  249. /*
  250. * Initialize @wa's notification and event's endpoint stuff
  251. *
  252. * This includes the allocating the read buffer, the context ID
  253. * allocation bitmap, the URB and submitting the URB.
  254. */
  255. int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
  256. {
  257. int result;
  258. struct usb_endpoint_descriptor *epd;
  259. struct usb_device *usb_dev = interface_to_usbdev(iface);
  260. struct device *dev = &iface->dev;
  261. edc_init(&wa->nep_edc);
  262. epd = &iface->cur_altsetting->endpoint[0].desc;
  263. wa->nep_buffer_size = 1024;
  264. wa->nep_buffer = kmalloc(wa->nep_buffer_size, GFP_KERNEL);
  265. if (wa->nep_buffer == NULL) {
  266. dev_err(dev, "Unable to allocate notification's read buffer\n");
  267. goto error_nep_buffer;
  268. }
  269. wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
  270. if (wa->nep_urb == NULL) {
  271. dev_err(dev, "Unable to allocate notification URB\n");
  272. goto error_urb_alloc;
  273. }
  274. usb_fill_int_urb(wa->nep_urb, usb_dev,
  275. usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
  276. wa->nep_buffer, wa->nep_buffer_size,
  277. wa_nep_cb, wa, epd->bInterval);
  278. result = wa_nep_arm(wa, GFP_KERNEL);
  279. if (result < 0) {
  280. dev_err(dev, "Cannot submit notification URB: %d\n", result);
  281. goto error_nep_arm;
  282. }
  283. return 0;
  284. error_nep_arm:
  285. usb_free_urb(wa->nep_urb);
  286. error_urb_alloc:
  287. kfree(wa->nep_buffer);
  288. error_nep_buffer:
  289. return -ENOMEM;
  290. }
  291. void wa_nep_destroy(struct wahc *wa)
  292. {
  293. wa_nep_disarm(wa);
  294. usb_free_urb(wa->nep_urb);
  295. kfree(wa->nep_buffer);
  296. }