2 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
3 * Notification EndPoint support
5 * Copyright (C) 2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
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.
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]).
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).
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.
44 * Entry points here are:
46 * wa_nep_[create|destroy]() To initialize/release this subsystem
48 * wa_nep_cb() Callback for the notification
49 * endpoint; when data is ready, this
50 * does the dispatching.
52 #include <linux/workqueue.h>
53 #include <linux/ctype.h>
58 /* Structure for queueing notifications to the workqueue */
59 struct wa_notif_work {
60 struct work_struct work;
67 * Process incoming notifications from the WA's Notification EndPoint
68 * [the wuswad daemon, basically]
70 * @_nw: Pointer to a descriptor which has the pointer to the
71 * @wa, the size of the buffer and the work queue
72 * structure (so we can free all when done).
73 * @returns 0 if ok, < 0 errno code on error.
75 * All notifications follow the same format; they need to start with a
76 * 'struct wa_notif_hdr' header, so it is easy to parse through
77 * them. We just break the buffer in individual notifications (the
78 * standard doesn't say if it can be done or is forbidden, so we are
79 * cautious) and dispatch each.
81 * So the handling layers are is:
83 * WA specific notification (from NEP)
84 * Device Notification Received -> wa_handle_notif_dn()
85 * WUSB Device notification generic handling
86 * BPST Adjustment -> wa_handle_notif_bpst_adj()
89 * @wa has to be referenced
91 static void wa_notif_dispatch(struct work_struct *ws)
95 struct wa_notif_work *nw = container_of(ws, struct wa_notif_work, work);
96 struct wahc *wa = nw->wa;
97 struct wa_notif_hdr *notif_hdr;
100 struct device *dev = &wa->usb_iface->dev;
103 /* FIXME: need to check for this??? */
104 if (usb_hcd->state == HC_STATE_QUIESCING) /* Going down? */
105 goto out; /* screw it */
107 atomic_dec(&wa->notifs_queued); /* Throttling ctl */
108 dev = &wa->usb_iface->dev;
113 if (size < sizeof(*notif_hdr)) {
114 missing = sizeof(*notif_hdr) - size;
115 goto exhausted_buffer;
118 if (size < notif_hdr->bLength)
119 goto exhausted_buffer;
120 itr += notif_hdr->bLength;
121 size -= notif_hdr->bLength;
122 /* Dispatch the notification [don't use itr or size!] */
123 switch (notif_hdr->bNotifyType) {
125 struct hwa_notif_dn *hwa_dn;
126 hwa_dn = container_of(notif_hdr, struct hwa_notif_dn,
128 wusbhc_handle_dn(wa->wusb, hwa_dn->bSourceDeviceAddr,
130 notif_hdr->bLength - sizeof(*hwa_dn));
133 case WA_NOTIF_TRANSFER:
134 wa_handle_notif_xfer(wa, notif_hdr);
136 case DWA_NOTIF_RWAKE:
137 case DWA_NOTIF_PORTSTATUS:
138 case HWA_NOTIF_BPST_ADJ:
139 /* FIXME: unimplemented WA NOTIFs */
142 dev_err(dev, "HWA: unknown notification 0x%x, "
143 "%zu bytes; discarding\n",
144 notif_hdr->bNotifyType,
145 (size_t)notif_hdr->bLength);
154 /* THIS SHOULD NOT HAPPEN
156 * Buffer exahusted with partial data remaining; just warn and
157 * discard the data, as this should not happen.
160 dev_warn(dev, "HWA: device sent short notification, "
161 "%d bytes missing; discarding %d bytes.\n",
167 * Deliver incoming WA notifications to the wusbwa workqueue
169 * @wa: Pointer the Wire Adapter Controller Data Streaming
170 * instance (part of an 'struct usb_hcd').
171 * @size: Size of the received buffer
172 * @returns 0 if ok, < 0 errno code on error.
174 * The input buffer is @wa->nep_buffer, with @size bytes
175 * (guaranteed to fit in the allocated space,
176 * @wa->nep_buffer_size).
178 static int wa_nep_queue(struct wahc *wa, size_t size)
181 struct device *dev = &wa->usb_iface->dev;
182 struct wa_notif_work *nw;
184 /* dev_fnstart(dev, "(wa %p, size %zu)\n", wa, size); */
185 BUG_ON(size > wa->nep_buffer_size);
188 if (atomic_read(&wa->notifs_queued) > 200) {
189 if (printk_ratelimit())
190 dev_err(dev, "Too many notifications queued, "
191 "throttling back\n");
194 nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
196 if (printk_ratelimit())
197 dev_err(dev, "No memory to queue notification\n");
200 INIT_WORK(&nw->work, wa_notif_dispatch);
203 memcpy(nw->data, wa->nep_buffer, size);
204 atomic_inc(&wa->notifs_queued); /* Throttling ctl */
205 queue_work(wusbd, &nw->work);
207 /* dev_fnend(dev, "(wa %p, size %zu) = result\n", wa, size, result); */
212 * Callback for the notification event endpoint
214 * Check's that everything is fine and then passes the data to be
215 * queued to the workqueue.
217 static void wa_nep_cb(struct urb *urb)
220 struct wahc *wa = urb->context;
221 struct device *dev = &wa->usb_iface->dev;
223 switch (result = urb->status) {
225 result = wa_nep_queue(wa, urb->actual_length);
227 dev_err(dev, "NEP: unable to process notification(s): "
230 case -ECONNRESET: /* Not an error, but a controlled situation; */
231 case -ENOENT: /* (we killed the URB)...so, no broadcast */
233 dev_dbg(dev, "NEP: going down %d\n", urb->status);
235 default: /* On general errors, we retry unless it gets ugly */
236 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
237 EDC_ERROR_TIMEFRAME)) {
238 dev_err(dev, "NEP: URB max acceptable errors "
239 "exceeded, resetting device\n");
243 dev_err(dev, "NEP: URB error %d\n", urb->status);
245 result = wa_nep_arm(wa, GFP_ATOMIC);
247 dev_err(dev, "NEP: cannot submit URB: %d\n", result);
255 * Initialize @wa's notification and event's endpoint stuff
257 * This includes the allocating the read buffer, the context ID
258 * allocation bitmap, the URB and submitting the URB.
260 int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
263 struct usb_endpoint_descriptor *epd;
264 struct usb_device *usb_dev = interface_to_usbdev(iface);
265 struct device *dev = &iface->dev;
267 edc_init(&wa->nep_edc);
268 epd = &iface->cur_altsetting->endpoint[0].desc;
269 wa->nep_buffer_size = 1024;
270 wa->nep_buffer = kmalloc(wa->nep_buffer_size, GFP_KERNEL);
271 if (wa->nep_buffer == NULL) {
272 dev_err(dev, "Unable to allocate notification's read buffer\n");
273 goto error_nep_buffer;
275 wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
276 if (wa->nep_urb == NULL) {
277 dev_err(dev, "Unable to allocate notification URB\n");
278 goto error_urb_alloc;
280 usb_fill_int_urb(wa->nep_urb, usb_dev,
281 usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
282 wa->nep_buffer, wa->nep_buffer_size,
283 wa_nep_cb, wa, epd->bInterval);
284 result = wa_nep_arm(wa, GFP_KERNEL);
286 dev_err(dev, "Cannot submit notification URB: %d\n", result);
292 usb_free_urb(wa->nep_urb);
294 kfree(wa->nep_buffer);
299 void wa_nep_destroy(struct wahc *wa)
302 usb_free_urb(wa->nep_urb);
303 kfree(wa->nep_buffer);