x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / usb / wusbcore / wa-nep.c
blob9fdcb6b84abf0b6382717e54240a3e89d29951fd
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
4 * Notification EndPoint support
6 * Copyright (C) 2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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]).
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).
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.
30 * Entry points here are:
32 * wa_nep_[create|destroy]() To initialize/release this subsystem
34 * wa_nep_cb() Callback for the notification
35 * endpoint; when data is ready, this
36 * does the dispatching.
38 #include <linux/workqueue.h>
39 #include <linux/ctype.h>
40 #include <linux/slab.h>
42 #include "wa-hc.h"
43 #include "wusbhc.h"
45 /* Structure for queueing notifications to the workqueue */
46 struct wa_notif_work {
47 struct work_struct work;
48 struct wahc *wa;
49 size_t size;
50 u8 data[];
54 * Process incoming notifications from the WA's Notification EndPoint
55 * [the wuswad daemon, basically]
57 * @_nw: Pointer to a descriptor which has the pointer to the
58 * @wa, the size of the buffer and the work queue
59 * structure (so we can free all when done).
60 * @returns 0 if ok, < 0 errno code on error.
62 * All notifications follow the same format; they need to start with a
63 * 'struct wa_notif_hdr' header, so it is easy to parse through
64 * them. We just break the buffer in individual notifications (the
65 * standard doesn't say if it can be done or is forbidden, so we are
66 * cautious) and dispatch each.
68 * So the handling layers are is:
70 * WA specific notification (from NEP)
71 * Device Notification Received -> wa_handle_notif_dn()
72 * WUSB Device notification generic handling
73 * BPST Adjustment -> wa_handle_notif_bpst_adj()
74 * ... -> ...
76 * @wa has to be referenced
78 static void wa_notif_dispatch(struct work_struct *ws)
80 void *itr;
81 u8 missing = 0;
82 struct wa_notif_work *nw = container_of(ws, struct wa_notif_work,
83 work);
84 struct wahc *wa = nw->wa;
85 struct wa_notif_hdr *notif_hdr;
86 size_t size;
88 struct device *dev = &wa->usb_iface->dev;
90 #if 0
91 /* FIXME: need to check for this??? */
92 if (usb_hcd->state == HC_STATE_QUIESCING) /* Going down? */
93 goto out; /* screw it */
94 #endif
95 atomic_dec(&wa->notifs_queued); /* Throttling ctl */
96 dev = &wa->usb_iface->dev;
97 size = nw->size;
98 itr = nw->data;
100 while (size) {
101 if (size < sizeof(*notif_hdr)) {
102 missing = sizeof(*notif_hdr) - size;
103 goto exhausted_buffer;
105 notif_hdr = itr;
106 if (size < notif_hdr->bLength)
107 goto exhausted_buffer;
108 itr += notif_hdr->bLength;
109 size -= notif_hdr->bLength;
110 /* Dispatch the notification [don't use itr or size!] */
111 switch (notif_hdr->bNotifyType) {
112 case HWA_NOTIF_DN: {
113 struct hwa_notif_dn *hwa_dn;
114 hwa_dn = container_of(notif_hdr, struct hwa_notif_dn,
115 hdr);
116 wusbhc_handle_dn(wa->wusb, hwa_dn->bSourceDeviceAddr,
117 hwa_dn->dndata,
118 notif_hdr->bLength - sizeof(*hwa_dn));
119 break;
121 case WA_NOTIF_TRANSFER:
122 wa_handle_notif_xfer(wa, notif_hdr);
123 break;
124 case HWA_NOTIF_BPST_ADJ:
125 break; /* no action needed for BPST ADJ. */
126 case DWA_NOTIF_RWAKE:
127 case DWA_NOTIF_PORTSTATUS:
128 /* FIXME: unimplemented WA NOTIFs */
129 /* fallthru */
130 default:
131 dev_err(dev, "HWA: unknown notification 0x%x, "
132 "%zu bytes; discarding\n",
133 notif_hdr->bNotifyType,
134 (size_t)notif_hdr->bLength);
135 break;
138 out:
139 wa_put(wa);
140 kfree(nw);
141 return;
143 /* THIS SHOULD NOT HAPPEN
145 * Buffer exahusted with partial data remaining; just warn and
146 * discard the data, as this should not happen.
148 exhausted_buffer:
149 dev_warn(dev, "HWA: device sent short notification, "
150 "%d bytes missing; discarding %d bytes.\n",
151 missing, (int)size);
152 goto out;
156 * Deliver incoming WA notifications to the wusbwa workqueue
158 * @wa: Pointer the Wire Adapter Controller Data Streaming
159 * instance (part of an 'struct usb_hcd').
160 * @size: Size of the received buffer
161 * @returns 0 if ok, < 0 errno code on error.
163 * The input buffer is @wa->nep_buffer, with @size bytes
164 * (guaranteed to fit in the allocated space,
165 * @wa->nep_buffer_size).
167 static int wa_nep_queue(struct wahc *wa, size_t size)
169 int result = 0;
170 struct device *dev = &wa->usb_iface->dev;
171 struct wa_notif_work *nw;
173 /* dev_fnstart(dev, "(wa %p, size %zu)\n", wa, size); */
174 BUG_ON(size > wa->nep_buffer_size);
175 if (size == 0)
176 goto out;
177 if (atomic_read(&wa->notifs_queued) > 200) {
178 if (printk_ratelimit())
179 dev_err(dev, "Too many notifications queued, "
180 "throttling back\n");
181 goto out;
183 nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
184 if (nw == NULL) {
185 if (printk_ratelimit())
186 dev_err(dev, "No memory to queue notification\n");
187 result = -ENOMEM;
188 goto out;
190 INIT_WORK(&nw->work, wa_notif_dispatch);
191 nw->wa = wa_get(wa);
192 nw->size = size;
193 memcpy(nw->data, wa->nep_buffer, size);
194 atomic_inc(&wa->notifs_queued); /* Throttling ctl */
195 queue_work(wusbd, &nw->work);
196 out:
197 /* dev_fnend(dev, "(wa %p, size %zu) = result\n", wa, size, result); */
198 return result;
202 * Callback for the notification event endpoint
204 * Check's that everything is fine and then passes the data to be
205 * queued to the workqueue.
207 static void wa_nep_cb(struct urb *urb)
209 int result;
210 struct wahc *wa = urb->context;
211 struct device *dev = &wa->usb_iface->dev;
213 switch (result = urb->status) {
214 case 0:
215 result = wa_nep_queue(wa, urb->actual_length);
216 if (result < 0)
217 dev_err(dev, "NEP: unable to process notification(s): "
218 "%d\n", result);
219 break;
220 case -ECONNRESET: /* Not an error, but a controlled situation; */
221 case -ENOENT: /* (we killed the URB)...so, no broadcast */
222 case -ESHUTDOWN:
223 dev_dbg(dev, "NEP: going down %d\n", urb->status);
224 goto out;
225 default: /* On general errors, we retry unless it gets ugly */
226 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
227 EDC_ERROR_TIMEFRAME)) {
228 dev_err(dev, "NEP: URB max acceptable errors "
229 "exceeded, resetting device\n");
230 wa_reset_all(wa);
231 goto out;
233 dev_err(dev, "NEP: URB error %d\n", urb->status);
235 result = wa_nep_arm(wa, GFP_ATOMIC);
236 if (result < 0) {
237 dev_err(dev, "NEP: cannot submit URB: %d\n", result);
238 wa_reset_all(wa);
240 out:
241 return;
245 * Initialize @wa's notification and event's endpoint stuff
247 * This includes the allocating the read buffer, the context ID
248 * allocation bitmap, the URB and submitting the URB.
250 int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
252 int result;
253 struct usb_endpoint_descriptor *epd;
254 struct usb_device *usb_dev = interface_to_usbdev(iface);
255 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)
262 goto error_nep_buffer;
263 wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
264 if (wa->nep_urb == NULL)
265 goto error_urb_alloc;
266 usb_fill_int_urb(wa->nep_urb, usb_dev,
267 usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
268 wa->nep_buffer, wa->nep_buffer_size,
269 wa_nep_cb, wa, epd->bInterval);
270 result = wa_nep_arm(wa, GFP_KERNEL);
271 if (result < 0) {
272 dev_err(dev, "Cannot submit notification URB: %d\n", result);
273 goto error_nep_arm;
275 return 0;
277 error_nep_arm:
278 usb_free_urb(wa->nep_urb);
279 error_urb_alloc:
280 kfree(wa->nep_buffer);
281 error_nep_buffer:
282 return -ENOMEM;
285 void wa_nep_destroy(struct wahc *wa)
287 wa_nep_disarm(wa);
288 usb_free_urb(wa->nep_urb);
289 kfree(wa->nep_buffer);