wa-nep.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. size = nw->size;
  92. itr = nw->data;
  93. while (size) {
  94. if (size < sizeof(*notif_hdr)) {
  95. missing = sizeof(*notif_hdr) - size;
  96. goto exhausted_buffer;
  97. }
  98. notif_hdr = itr;
  99. if (size < notif_hdr->bLength)
  100. goto exhausted_buffer;
  101. itr += notif_hdr->bLength;
  102. size -= notif_hdr->bLength;
  103. /* Dispatch the notification [don't use itr or size!] */
  104. switch (notif_hdr->bNotifyType) {
  105. case HWA_NOTIF_DN: {
  106. struct hwa_notif_dn *hwa_dn;
  107. hwa_dn = container_of(notif_hdr, struct hwa_notif_dn,
  108. hdr);
  109. wusbhc_handle_dn(wa->wusb, hwa_dn->bSourceDeviceAddr,
  110. hwa_dn->dndata,
  111. notif_hdr->bLength - sizeof(*hwa_dn));
  112. break;
  113. }
  114. case WA_NOTIF_TRANSFER:
  115. wa_handle_notif_xfer(wa, notif_hdr);
  116. break;
  117. case HWA_NOTIF_BPST_ADJ:
  118. break; /* no action needed for BPST ADJ. */
  119. case DWA_NOTIF_RWAKE:
  120. case DWA_NOTIF_PORTSTATUS:
  121. /* FIXME: unimplemented WA NOTIFs */
  122. /* fallthru */
  123. default:
  124. dev_err(dev, "HWA: unknown notification 0x%x, "
  125. "%zu bytes; discarding\n",
  126. notif_hdr->bNotifyType,
  127. (size_t)notif_hdr->bLength);
  128. break;
  129. }
  130. }
  131. out:
  132. wa_put(wa);
  133. kfree(nw);
  134. return;
  135. /* THIS SHOULD NOT HAPPEN
  136. *
  137. * Buffer exahusted with partial data remaining; just warn and
  138. * discard the data, as this should not happen.
  139. */
  140. exhausted_buffer:
  141. dev_warn(dev, "HWA: device sent short notification, "
  142. "%d bytes missing; discarding %d bytes.\n",
  143. missing, (int)size);
  144. goto out;
  145. }
  146. /*
  147. * Deliver incoming WA notifications to the wusbwa workqueue
  148. *
  149. * @wa: Pointer the Wire Adapter Controller Data Streaming
  150. * instance (part of an 'struct usb_hcd').
  151. * @size: Size of the received buffer
  152. * @returns 0 if ok, < 0 errno code on error.
  153. *
  154. * The input buffer is @wa->nep_buffer, with @size bytes
  155. * (guaranteed to fit in the allocated space,
  156. * @wa->nep_buffer_size).
  157. */
  158. static int wa_nep_queue(struct wahc *wa, size_t size)
  159. {
  160. int result = 0;
  161. struct device *dev = &wa->usb_iface->dev;
  162. struct wa_notif_work *nw;
  163. /* dev_fnstart(dev, "(wa %p, size %zu)\n", wa, size); */
  164. BUG_ON(size > wa->nep_buffer_size);
  165. if (size == 0)
  166. goto out;
  167. if (atomic_read(&wa->notifs_queued) > 200) {
  168. if (printk_ratelimit())
  169. dev_err(dev, "Too many notifications queued, "
  170. "throttling back\n");
  171. goto out;
  172. }
  173. nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
  174. if (nw == NULL) {
  175. if (printk_ratelimit())
  176. dev_err(dev, "No memory to queue notification\n");
  177. result = -ENOMEM;
  178. goto out;
  179. }
  180. INIT_WORK(&nw->work, wa_notif_dispatch);
  181. nw->wa = wa_get(wa);
  182. nw->size = size;
  183. memcpy(nw->data, wa->nep_buffer, size);
  184. atomic_inc(&wa->notifs_queued); /* Throttling ctl */
  185. queue_work(wusbd, &nw->work);
  186. out:
  187. /* dev_fnend(dev, "(wa %p, size %zu) = result\n", wa, size, result); */
  188. return result;
  189. }
  190. /*
  191. * Callback for the notification event endpoint
  192. *
  193. * Check's that everything is fine and then passes the data to be
  194. * queued to the workqueue.
  195. */
  196. static void wa_nep_cb(struct urb *urb)
  197. {
  198. int result;
  199. struct wahc *wa = urb->context;
  200. struct device *dev = &wa->usb_iface->dev;
  201. switch (result = urb->status) {
  202. case 0:
  203. result = wa_nep_queue(wa, urb->actual_length);
  204. if (result < 0)
  205. dev_err(dev, "NEP: unable to process notification(s): "
  206. "%d\n", result);
  207. break;
  208. case -ECONNRESET: /* Not an error, but a controlled situation; */
  209. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  210. case -ESHUTDOWN:
  211. dev_dbg(dev, "NEP: going down %d\n", urb->status);
  212. goto out;
  213. default: /* On general errors, we retry unless it gets ugly */
  214. if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
  215. EDC_ERROR_TIMEFRAME)) {
  216. dev_err(dev, "NEP: URB max acceptable errors "
  217. "exceeded, resetting device\n");
  218. wa_reset_all(wa);
  219. goto out;
  220. }
  221. dev_err(dev, "NEP: URB error %d\n", urb->status);
  222. }
  223. result = wa_nep_arm(wa, GFP_ATOMIC);
  224. if (result < 0) {
  225. dev_err(dev, "NEP: cannot submit URB: %d\n", result);
  226. wa_reset_all(wa);
  227. }
  228. out:
  229. return;
  230. }
  231. /*
  232. * Initialize @wa's notification and event's endpoint stuff
  233. *
  234. * This includes the allocating the read buffer, the context ID
  235. * allocation bitmap, the URB and submitting the URB.
  236. */
  237. int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
  238. {
  239. int result;
  240. struct usb_endpoint_descriptor *epd;
  241. struct usb_device *usb_dev = interface_to_usbdev(iface);
  242. struct device *dev = &iface->dev;
  243. edc_init(&wa->nep_edc);
  244. epd = &iface->cur_altsetting->endpoint[0].desc;
  245. wa->nep_buffer_size = 1024;
  246. wa->nep_buffer = kmalloc(wa->nep_buffer_size, GFP_KERNEL);
  247. if (!wa->nep_buffer)
  248. goto error_nep_buffer;
  249. wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
  250. if (wa->nep_urb == NULL)
  251. goto error_urb_alloc;
  252. usb_fill_int_urb(wa->nep_urb, usb_dev,
  253. usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
  254. wa->nep_buffer, wa->nep_buffer_size,
  255. wa_nep_cb, wa, epd->bInterval);
  256. result = wa_nep_arm(wa, GFP_KERNEL);
  257. if (result < 0) {
  258. dev_err(dev, "Cannot submit notification URB: %d\n", result);
  259. goto error_nep_arm;
  260. }
  261. return 0;
  262. error_nep_arm:
  263. usb_free_urb(wa->nep_urb);
  264. error_urb_alloc:
  265. kfree(wa->nep_buffer);
  266. error_nep_buffer:
  267. return -ENOMEM;
  268. }
  269. void wa_nep_destroy(struct wahc *wa)
  270. {
  271. wa_nep_disarm(wa);
  272. usb_free_urb(wa->nep_urb);
  273. kfree(wa->nep_buffer);
  274. }