wa-nep.c 8.3 KB

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