• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/usb/wusbcore/
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
56#include "wa-hc.h"
57#include "wusbhc.h"
58
59/* Structure for queueing notifications to the workqueue */
60struct wa_notif_work {
61	struct work_struct work;
62	struct wahc *wa;
63	size_t size;
64	u8 data[];
65};
66
67/*
68 * Process incoming notifications from the WA's Notification EndPoint
69 * [the wuswad daemon, basically]
70 *
71 * @_nw:	Pointer to a descriptor which has the pointer to the
72 * 		@wa, the size of the buffer and the work queue
73 * 		structure (so we can free all when done).
74 * @returns     0 if ok, < 0 errno code on error.
75 *
76 * All notifications follow the same format; they need to start with a
77 * 'struct wa_notif_hdr' header, so it is easy to parse through
78 * them. We just break the buffer in individual notifications (the
79 * standard doesn't say if it can be done or is forbidden, so we are
80 * cautious) and dispatch each.
81 *
82 * So the handling layers are is:
83 *
84 *   WA specific notification (from NEP)
85 *      Device Notification Received -> wa_handle_notif_dn()
86 *        WUSB Device notification generic handling
87 *      BPST Adjustment -> wa_handle_notif_bpst_adj()
88 *      ... -> ...
89 *
90 * @wa has to be referenced
91 */
92static void wa_notif_dispatch(struct work_struct *ws)
93{
94	void *itr;
95	u8 missing = 0;
96	struct wa_notif_work *nw = container_of(ws, struct wa_notif_work, work);
97	struct wahc *wa = nw->wa;
98	struct wa_notif_hdr *notif_hdr;
99	size_t size;
100
101	struct device *dev = &wa->usb_iface->dev;
102
103	atomic_dec(&wa->notifs_queued);		/* Throttling ctl */
104	dev = &wa->usb_iface->dev;
105	size = nw->size;
106	itr = nw->data;
107
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 DWA_NOTIF_RWAKE:
133		case DWA_NOTIF_PORTSTATUS:
134		case HWA_NOTIF_BPST_ADJ:
135			/* fallthru */
136		default:
137			dev_err(dev, "HWA: unknown notification 0x%x, "
138				"%zu bytes; discarding\n",
139				notif_hdr->bNotifyType,
140				(size_t)notif_hdr->bLength);
141			break;
142		}
143	}
144out:
145	wa_put(wa);
146	kfree(nw);
147	return;
148
149	/* THIS SHOULD NOT HAPPEN
150	 *
151	 * Buffer exahusted with partial data remaining; just warn and
152	 * discard the data, as this should not happen.
153	 */
154exhausted_buffer:
155	dev_warn(dev, "HWA: device sent short notification, "
156		 "%d bytes missing; discarding %d bytes.\n",
157		 missing, (int)size);
158	goto out;
159}
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 */
173static 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
179	/* dev_fnstart(dev, "(wa %p, size %zu)\n", wa, size); */
180	BUG_ON(size > wa->nep_buffer_size);
181	if (size == 0)
182		goto out;
183	if (atomic_read(&wa->notifs_queued) > 200) {
184		if (printk_ratelimit())
185			dev_err(dev, "Too many notifications queued, "
186				"throttling back\n");
187		goto out;
188	}
189	nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
190	if (nw == NULL) {
191		if (printk_ratelimit())
192			dev_err(dev, "No memory to queue notification\n");
193		goto out;
194	}
195	INIT_WORK(&nw->work, wa_notif_dispatch);
196	nw->wa = wa_get(wa);
197	nw->size = size;
198	memcpy(nw->data, wa->nep_buffer, size);
199	atomic_inc(&wa->notifs_queued);		/* Throttling ctl */
200	queue_work(wusbd, &nw->work);
201out:
202	/* dev_fnend(dev, "(wa %p, size %zu) = result\n", wa, size, result); */
203	return result;
204}
205
206/*
207 * Callback for the notification event endpoint
208 *
209 * Check's that everything is fine and then passes the data to be
210 * queued to the workqueue.
211 */
212static void wa_nep_cb(struct urb *urb)
213{
214	int result;
215	struct wahc *wa = urb->context;
216	struct device *dev = &wa->usb_iface->dev;
217
218	switch (result = urb->status) {
219	case 0:
220		result = wa_nep_queue(wa, urb->actual_length);
221		if (result < 0)
222			dev_err(dev, "NEP: unable to process notification(s): "
223				"%d\n", result);
224		break;
225	case -ECONNRESET:	/* Not an error, but a controlled situation; */
226	case -ENOENT:		/* (we killed the URB)...so, no broadcast */
227	case -ESHUTDOWN:
228		dev_dbg(dev, "NEP: going down %d\n", urb->status);
229		goto out;
230	default:	/* On general errors, we retry unless it gets ugly */
231		if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
232			    EDC_ERROR_TIMEFRAME)) {
233			dev_err(dev, "NEP: URB max acceptable errors "
234				"exceeded, resetting device\n");
235			wa_reset_all(wa);
236			goto out;
237		}
238		dev_err(dev, "NEP: URB error %d\n", urb->status);
239	}
240	result = wa_nep_arm(wa, GFP_ATOMIC);
241	if (result < 0) {
242		dev_err(dev, "NEP: cannot submit URB: %d\n", result);
243		wa_reset_all(wa);
244	}
245out:
246	return;
247}
248
249/*
250 * Initialize @wa's notification and event's endpoint stuff
251 *
252 * This includes the allocating the read buffer, the context ID
253 * allocation bitmap, the URB and submitting the URB.
254 */
255int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
256{
257	int result;
258	struct usb_endpoint_descriptor *epd;
259	struct usb_device *usb_dev = interface_to_usbdev(iface);
260	struct device *dev = &iface->dev;
261
262	edc_init(&wa->nep_edc);
263	epd = &iface->cur_altsetting->endpoint[0].desc;
264	wa->nep_buffer_size = 1024;
265	wa->nep_buffer = kmalloc(wa->nep_buffer_size, GFP_KERNEL);
266	if (wa->nep_buffer == NULL) {
267		dev_err(dev, "Unable to allocate notification's read buffer\n");
268		goto error_nep_buffer;
269	}
270	wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
271	if (wa->nep_urb == NULL) {
272		dev_err(dev, "Unable to allocate notification URB\n");
273		goto error_urb_alloc;
274	}
275	usb_fill_int_urb(wa->nep_urb, usb_dev,
276			 usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
277			 wa->nep_buffer, wa->nep_buffer_size,
278			 wa_nep_cb, wa, epd->bInterval);
279	result = wa_nep_arm(wa, GFP_KERNEL);
280	if (result < 0) {
281		dev_err(dev, "Cannot submit notification URB: %d\n", result);
282		goto error_nep_arm;
283	}
284	return 0;
285
286error_nep_arm:
287	usb_free_urb(wa->nep_urb);
288error_urb_alloc:
289	kfree(wa->nep_buffer);
290error_nep_buffer:
291	return -ENOMEM;
292}
293
294void wa_nep_destroy(struct wahc *wa)
295{
296	wa_nep_disarm(wa);
297	usb_free_urb(wa->nep_urb);
298	kfree(wa->nep_buffer);
299}
300