proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / usb / wusbcore / wa-nep.c
blobf67f7f1e6df9502680868d2bfa5e0493df0cf36d
1 /*
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
20 * 02110-1301, USA.
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>
54 #include <linux/slab.h>
56 #include "wa-hc.h"
57 #include "wusbhc.h"
59 /* Structure for queueing notifications to the workqueue */
60 struct wa_notif_work {
61 struct work_struct work;
62 struct wahc *wa;
63 size_t size;
64 u8 data[];
68 * Process incoming notifications from the WA's Notification EndPoint
69 * [the wuswad daemon, basically]
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.
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.
82 * So the handling layers are is:
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 * ... -> ...
90 * @wa has to be referenced
92 static void wa_notif_dispatch(struct work_struct *ws)
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;
101 struct device *dev = &wa->usb_iface->dev;
103 #if 0
104 /* FIXME: need to check for this??? */
105 if (usb_hcd->state == HC_STATE_QUIESCING) /* Going down? */
106 goto out; /* screw it */
107 #endif
108 atomic_dec(&wa->notifs_queued); /* Throttling ctl */
109 dev = &wa->usb_iface->dev;
110 size = nw->size;
111 itr = nw->data;
113 while (size) {
114 if (size < sizeof(*notif_hdr)) {
115 missing = sizeof(*notif_hdr) - size;
116 goto exhausted_buffer;
118 notif_hdr = itr;
119 if (size < notif_hdr->bLength)
120 goto exhausted_buffer;
121 itr += notif_hdr->bLength;
122 size -= notif_hdr->bLength;
123 /* Dispatch the notification [don't use itr or size!] */
124 switch (notif_hdr->bNotifyType) {
125 case HWA_NOTIF_DN: {
126 struct hwa_notif_dn *hwa_dn;
127 hwa_dn = container_of(notif_hdr, struct hwa_notif_dn,
128 hdr);
129 wusbhc_handle_dn(wa->wusb, hwa_dn->bSourceDeviceAddr,
130 hwa_dn->dndata,
131 notif_hdr->bLength - sizeof(*hwa_dn));
132 break;
134 case WA_NOTIF_TRANSFER:
135 wa_handle_notif_xfer(wa, notif_hdr);
136 break;
137 case DWA_NOTIF_RWAKE:
138 case DWA_NOTIF_PORTSTATUS:
139 case HWA_NOTIF_BPST_ADJ:
140 /* FIXME: unimplemented WA NOTIFs */
141 /* fallthru */
142 default:
143 dev_err(dev, "HWA: unknown notification 0x%x, "
144 "%zu bytes; discarding\n",
145 notif_hdr->bNotifyType,
146 (size_t)notif_hdr->bLength);
147 break;
150 out:
151 wa_put(wa);
152 kfree(nw);
153 return;
155 /* THIS SHOULD NOT HAPPEN
157 * Buffer exahusted with partial data remaining; just warn and
158 * discard the data, as this should not happen.
160 exhausted_buffer:
161 dev_warn(dev, "HWA: device sent short notification, "
162 "%d bytes missing; discarding %d bytes.\n",
163 missing, (int)size);
164 goto out;
168 * Deliver incoming WA notifications to the wusbwa workqueue
170 * @wa: Pointer the Wire Adapter Controller Data Streaming
171 * instance (part of an 'struct usb_hcd').
172 * @size: Size of the received buffer
173 * @returns 0 if ok, < 0 errno code on error.
175 * The input buffer is @wa->nep_buffer, with @size bytes
176 * (guaranteed to fit in the allocated space,
177 * @wa->nep_buffer_size).
179 static int wa_nep_queue(struct wahc *wa, size_t size)
181 int result = 0;
182 struct device *dev = &wa->usb_iface->dev;
183 struct wa_notif_work *nw;
185 /* dev_fnstart(dev, "(wa %p, size %zu)\n", wa, size); */
186 BUG_ON(size > wa->nep_buffer_size);
187 if (size == 0)
188 goto out;
189 if (atomic_read(&wa->notifs_queued) > 200) {
190 if (printk_ratelimit())
191 dev_err(dev, "Too many notifications queued, "
192 "throttling back\n");
193 goto out;
195 nw = kzalloc(sizeof(*nw) + size, GFP_ATOMIC);
196 if (nw == NULL) {
197 if (printk_ratelimit())
198 dev_err(dev, "No memory to queue notification\n");
199 goto out;
201 INIT_WORK(&nw->work, wa_notif_dispatch);
202 nw->wa = wa_get(wa);
203 nw->size = size;
204 memcpy(nw->data, wa->nep_buffer, size);
205 atomic_inc(&wa->notifs_queued); /* Throttling ctl */
206 queue_work(wusbd, &nw->work);
207 out:
208 /* dev_fnend(dev, "(wa %p, size %zu) = result\n", wa, size, result); */
209 return result;
213 * Callback for the notification event endpoint
215 * Check's that everything is fine and then passes the data to be
216 * queued to the workqueue.
218 static void wa_nep_cb(struct urb *urb)
220 int result;
221 struct wahc *wa = urb->context;
222 struct device *dev = &wa->usb_iface->dev;
224 switch (result = urb->status) {
225 case 0:
226 result = wa_nep_queue(wa, urb->actual_length);
227 if (result < 0)
228 dev_err(dev, "NEP: unable to process notification(s): "
229 "%d\n", result);
230 break;
231 case -ECONNRESET: /* Not an error, but a controlled situation; */
232 case -ENOENT: /* (we killed the URB)...so, no broadcast */
233 case -ESHUTDOWN:
234 dev_dbg(dev, "NEP: going down %d\n", urb->status);
235 goto out;
236 default: /* On general errors, we retry unless it gets ugly */
237 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
238 EDC_ERROR_TIMEFRAME)) {
239 dev_err(dev, "NEP: URB max acceptable errors "
240 "exceeded, resetting device\n");
241 wa_reset_all(wa);
242 goto out;
244 dev_err(dev, "NEP: URB error %d\n", urb->status);
246 result = wa_nep_arm(wa, GFP_ATOMIC);
247 if (result < 0) {
248 dev_err(dev, "NEP: cannot submit URB: %d\n", result);
249 wa_reset_all(wa);
251 out:
252 return;
256 * Initialize @wa's notification and event's endpoint stuff
258 * This includes the allocating the read buffer, the context ID
259 * allocation bitmap, the URB and submitting the URB.
261 int wa_nep_create(struct wahc *wa, struct usb_interface *iface)
263 int result;
264 struct usb_endpoint_descriptor *epd;
265 struct usb_device *usb_dev = interface_to_usbdev(iface);
266 struct device *dev = &iface->dev;
268 edc_init(&wa->nep_edc);
269 epd = &iface->cur_altsetting->endpoint[0].desc;
270 wa->nep_buffer_size = 1024;
271 wa->nep_buffer = kmalloc(wa->nep_buffer_size, GFP_KERNEL);
272 if (wa->nep_buffer == NULL) {
273 dev_err(dev, "Unable to allocate notification's read buffer\n");
274 goto error_nep_buffer;
276 wa->nep_urb = usb_alloc_urb(0, GFP_KERNEL);
277 if (wa->nep_urb == NULL) {
278 dev_err(dev, "Unable to allocate notification URB\n");
279 goto error_urb_alloc;
281 usb_fill_int_urb(wa->nep_urb, usb_dev,
282 usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
283 wa->nep_buffer, wa->nep_buffer_size,
284 wa_nep_cb, wa, epd->bInterval);
285 result = wa_nep_arm(wa, GFP_KERNEL);
286 if (result < 0) {
287 dev_err(dev, "Cannot submit notification URB: %d\n", result);
288 goto error_nep_arm;
290 return 0;
292 error_nep_arm:
293 usb_free_urb(wa->nep_urb);
294 error_urb_alloc:
295 kfree(wa->nep_buffer);
296 error_nep_buffer:
297 return -ENOMEM;
300 void wa_nep_destroy(struct wahc *wa)
302 wa_nep_disarm(wa);
303 usb_free_urb(wa->nep_urb);
304 kfree(wa->nep_buffer);