wa-nep.c 9.0 KB

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