blob: 34133efddb4291b41a25f1f95a64527426351ed9 [file] [log] [blame]
Chris Kellyb3147862012-02-20 21:12:08 +00001/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 *
5 * This file implements the protocol specific parts of the USB service for a PD.
6 * -----------------------------------------------------------------------------
7 */
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/timer.h>
11#include <linux/sched.h>
12#include <linux/netdevice.h>
13#include <linux/errno.h>
14#include <linux/input.h>
15#include <asm/unaligned.h>
Joe Perchesf724b582013-07-23 13:45:00 +010016#include "ozdbg.h"
Chris Kellyb3147862012-02-20 21:12:08 +000017#include "ozprotocol.h"
18#include "ozeltbuf.h"
19#include "ozpd.h"
20#include "ozproto.h"
21#include "ozusbif.h"
22#include "ozhcd.h"
Chris Kellyb3147862012-02-20 21:12:08 +000023#include "ozusbsvc.h"
Chris Kellyb3147862012-02-20 21:12:08 +000024/*------------------------------------------------------------------------------
25 */
26#define MAX_ISOC_FIXED_DATA (253-sizeof(struct oz_isoc_fixed))
27/*------------------------------------------------------------------------------
28 * Context: softirq
29 */
30static int oz_usb_submit_elt(struct oz_elt_buf *eb, struct oz_elt_info *ei,
31 struct oz_usb_ctx *usb_ctx, u8 strid, u8 isoc)
32{
33 int ret;
34 struct oz_elt *elt = (struct oz_elt *)ei->data;
35 struct oz_app_hdr *app_hdr = (struct oz_app_hdr *)(elt+1);
Rupesh Gujare18f81912013-08-13 18:24:21 +010036
Chris Kellyb3147862012-02-20 21:12:08 +000037 elt->type = OZ_ELT_APP_DATA;
38 ei->app_id = OZ_APPID_USB;
39 ei->length = elt->length + sizeof(struct oz_elt);
40 app_hdr->app_id = OZ_APPID_USB;
41 spin_lock_bh(&eb->lock);
42 if (isoc == 0) {
43 app_hdr->elt_seq_num = usb_ctx->tx_seq_num++;
44 if (usb_ctx->tx_seq_num == 0)
45 usb_ctx->tx_seq_num = 1;
46 }
47 ret = oz_queue_elt_info(eb, isoc, strid, ei);
48 if (ret)
49 oz_elt_info_free(eb, ei);
50 spin_unlock_bh(&eb->lock);
51 return ret;
52}
53/*------------------------------------------------------------------------------
54 * Context: softirq
55 */
56int oz_usb_get_desc_req(void *hpd, u8 req_id, u8 req_type, u8 desc_type,
57 u8 index, u16 windex, int offset, int len)
58{
59 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
60 struct oz_pd *pd = usb_ctx->pd;
61 struct oz_elt *elt;
62 struct oz_get_desc_req *body;
63 struct oz_elt_buf *eb = &pd->elt_buff;
64 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
Rupesh Gujare18f81912013-08-13 18:24:21 +010065
Joe Perchesf724b582013-07-23 13:45:00 +010066 oz_dbg(ON, " req_type = 0x%x\n", req_type);
67 oz_dbg(ON, " desc_type = 0x%x\n", desc_type);
68 oz_dbg(ON, " index = 0x%x\n", index);
69 oz_dbg(ON, " windex = 0x%x\n", windex);
70 oz_dbg(ON, " offset = 0x%x\n", offset);
71 oz_dbg(ON, " len = 0x%x\n", len);
Chris Kellyb3147862012-02-20 21:12:08 +000072 if (len > 200)
73 len = 200;
Peter Huewe4d1b2fb2013-02-15 15:22:23 +010074 if (ei == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +000075 return -1;
76 elt = (struct oz_elt *)ei->data;
77 elt->length = sizeof(struct oz_get_desc_req);
78 body = (struct oz_get_desc_req *)(elt+1);
79 body->type = OZ_GET_DESC_REQ;
80 body->req_id = req_id;
81 put_unaligned(cpu_to_le16(offset), &body->offset);
82 put_unaligned(cpu_to_le16(len), &body->size);
83 body->req_type = req_type;
84 body->desc_type = desc_type;
85 body->w_index = windex;
86 body->index = index;
87 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
88}
89/*------------------------------------------------------------------------------
90 * Context: tasklet
91 */
92static int oz_usb_set_config_req(void *hpd, u8 req_id, u8 index)
93{
94 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
95 struct oz_pd *pd = usb_ctx->pd;
96 struct oz_elt *elt;
97 struct oz_elt_buf *eb = &pd->elt_buff;
98 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
99 struct oz_set_config_req *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100100
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100101 if (ei == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000102 return -1;
103 elt = (struct oz_elt *)ei->data;
104 elt->length = sizeof(struct oz_set_config_req);
105 body = (struct oz_set_config_req *)(elt+1);
106 body->type = OZ_SET_CONFIG_REQ;
107 body->req_id = req_id;
108 body->index = index;
109 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
110}
111/*------------------------------------------------------------------------------
112 * Context: tasklet
113 */
114static int oz_usb_set_interface_req(void *hpd, u8 req_id, u8 index, u8 alt)
115{
116 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
117 struct oz_pd *pd = usb_ctx->pd;
118 struct oz_elt *elt;
119 struct oz_elt_buf *eb = &pd->elt_buff;
120 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
121 struct oz_set_interface_req *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100122
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100123 if (ei == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000124 return -1;
125 elt = (struct oz_elt *)ei->data;
126 elt->length = sizeof(struct oz_set_interface_req);
127 body = (struct oz_set_interface_req *)(elt+1);
128 body->type = OZ_SET_INTERFACE_REQ;
129 body->req_id = req_id;
130 body->index = index;
131 body->alternative = alt;
132 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
133}
134/*------------------------------------------------------------------------------
135 * Context: tasklet
136 */
137static int oz_usb_set_clear_feature_req(void *hpd, u8 req_id, u8 type,
138 u8 recipient, u8 index, __le16 feature)
139{
140 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
141 struct oz_pd *pd = usb_ctx->pd;
142 struct oz_elt *elt;
143 struct oz_elt_buf *eb = &pd->elt_buff;
144 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
145 struct oz_feature_req *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100146
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100147 if (ei == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000148 return -1;
149 elt = (struct oz_elt *)ei->data;
150 elt->length = sizeof(struct oz_feature_req);
151 body = (struct oz_feature_req *)(elt+1);
152 body->type = type;
153 body->req_id = req_id;
154 body->recipient = recipient;
155 body->index = index;
156 put_unaligned(feature, &body->feature);
157 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
158}
159/*------------------------------------------------------------------------------
160 * Context: tasklet
161 */
162static int oz_usb_vendor_class_req(void *hpd, u8 req_id, u8 req_type,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100163 u8 request, __le16 value, __le16 index, const u8 *data, int data_len)
Chris Kellyb3147862012-02-20 21:12:08 +0000164{
165 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
166 struct oz_pd *pd = usb_ctx->pd;
167 struct oz_elt *elt;
168 struct oz_elt_buf *eb = &pd->elt_buff;
169 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
170 struct oz_vendor_class_req *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100171
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100172 if (ei == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000173 return -1;
174 elt = (struct oz_elt *)ei->data;
175 elt->length = sizeof(struct oz_vendor_class_req) - 1 + data_len;
176 body = (struct oz_vendor_class_req *)(elt+1);
177 body->type = OZ_VENDOR_CLASS_REQ;
178 body->req_id = req_id;
179 body->req_type = req_type;
180 body->request = request;
181 put_unaligned(value, &body->value);
182 put_unaligned(index, &body->index);
183 if (data_len)
184 memcpy(body->data, data, data_len);
185 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
186}
187/*------------------------------------------------------------------------------
188 * Context: tasklet
189 */
190int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100191 const u8 *data, int data_len)
Chris Kellyb3147862012-02-20 21:12:08 +0000192{
193 unsigned wvalue = le16_to_cpu(setup->wValue);
194 unsigned windex = le16_to_cpu(setup->wIndex);
195 unsigned wlength = le16_to_cpu(setup->wLength);
196 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100197
Chris Kellyb3147862012-02-20 21:12:08 +0000198 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
199 switch (setup->bRequest) {
200 case USB_REQ_GET_DESCRIPTOR:
201 rc = oz_usb_get_desc_req(hpd, req_id,
202 setup->bRequestType, (u8)(wvalue>>8),
203 (u8)wvalue, setup->wIndex, 0, wlength);
204 break;
205 case USB_REQ_SET_CONFIGURATION:
206 rc = oz_usb_set_config_req(hpd, req_id, (u8)wvalue);
207 break;
208 case USB_REQ_SET_INTERFACE: {
209 u8 if_num = (u8)windex;
210 u8 alt = (u8)wvalue;
211 rc = oz_usb_set_interface_req(hpd, req_id,
212 if_num, alt);
213 }
214 break;
215 case USB_REQ_SET_FEATURE:
216 rc = oz_usb_set_clear_feature_req(hpd, req_id,
217 OZ_SET_FEATURE_REQ,
218 setup->bRequestType & 0xf, (u8)windex,
219 setup->wValue);
220 break;
221 case USB_REQ_CLEAR_FEATURE:
222 rc = oz_usb_set_clear_feature_req(hpd, req_id,
223 OZ_CLEAR_FEATURE_REQ,
224 setup->bRequestType & 0xf,
225 (u8)windex, setup->wValue);
226 break;
227 }
228 } else {
229 rc = oz_usb_vendor_class_req(hpd, req_id, setup->bRequestType,
230 setup->bRequest, setup->wValue, setup->wIndex,
231 data, data_len);
232 }
233 return rc;
234}
235/*------------------------------------------------------------------------------
236 * Context: softirq
237 */
238int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb)
239{
240 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
241 struct oz_pd *pd = usb_ctx->pd;
242 struct oz_elt_buf *eb;
243 int i;
244 int hdr_size;
245 u8 *data;
246 struct usb_iso_packet_descriptor *desc;
247
248 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
249 for (i = 0; i < urb->number_of_packets; i++) {
250 u8 *data;
251 desc = &urb->iso_frame_desc[i];
252 data = ((u8 *)urb->transfer_buffer)+desc->offset;
253 oz_send_isoc_unit(pd, ep_num, data, desc->length);
254 }
255 return 0;
256 }
257
258 hdr_size = sizeof(struct oz_isoc_fixed) - 1;
259 eb = &pd->elt_buff;
260 i = 0;
261 while (i < urb->number_of_packets) {
262 struct oz_elt_info *ei = oz_elt_info_alloc(eb);
263 struct oz_elt *elt;
264 struct oz_isoc_fixed *body;
265 int unit_count;
266 int unit_size;
267 int rem;
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100268 if (ei == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000269 return -1;
270 rem = MAX_ISOC_FIXED_DATA;
271 elt = (struct oz_elt *)ei->data;
272 body = (struct oz_isoc_fixed *)(elt + 1);
273 body->type = OZ_USB_ENDPOINT_DATA;
274 body->endpoint = ep_num;
275 body->format = OZ_DATA_F_ISOC_FIXED;
276 unit_size = urb->iso_frame_desc[i].length;
277 body->unit_size = (u8)unit_size;
278 data = ((u8 *)(elt+1)) + hdr_size;
279 unit_count = 0;
280 while (i < urb->number_of_packets) {
281 desc = &urb->iso_frame_desc[i];
282 if ((unit_size == desc->length) &&
283 (desc->length <= rem)) {
284 memcpy(data, ((u8 *)urb->transfer_buffer) +
285 desc->offset, unit_size);
286 data += unit_size;
287 rem -= unit_size;
288 unit_count++;
289 desc->status = 0;
290 desc->actual_length = desc->length;
291 i++;
292 } else {
293 break;
294 }
295 }
296 elt->length = hdr_size + MAX_ISOC_FIXED_DATA - rem;
297 /* Store the number of units in body->frame_number for the
298 * moment. This field will be correctly determined before
299 * the element is sent. */
300 body->frame_number = (u8)unit_count;
301 oz_usb_submit_elt(eb, ei, usb_ctx, ep_num,
302 pd->mode & OZ_F_ISOC_ANYTIME);
303 }
304 return 0;
305}
306/*------------------------------------------------------------------------------
307 * Context: softirq-serialized
308 */
Peter Huewea7f74c32013-02-15 21:17:22 +0100309static void oz_usb_handle_ep_data(struct oz_usb_ctx *usb_ctx,
Chris Kellyb3147862012-02-20 21:12:08 +0000310 struct oz_usb_hdr *usb_hdr, int len)
311{
312 struct oz_data *data_hdr = (struct oz_data *)usb_hdr;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100313
Chris Kellyb3147862012-02-20 21:12:08 +0000314 switch (data_hdr->format) {
315 case OZ_DATA_F_MULTIPLE_FIXED: {
316 struct oz_multiple_fixed *body =
317 (struct oz_multiple_fixed *)data_hdr;
318 u8 *data = body->data;
319 int n = (len - sizeof(struct oz_multiple_fixed)+1)
320 / body->unit_size;
321 while (n--) {
322 oz_hcd_data_ind(usb_ctx->hport, body->endpoint,
323 data, body->unit_size);
324 data += body->unit_size;
325 }
326 }
327 break;
328 case OZ_DATA_F_ISOC_FIXED: {
329 struct oz_isoc_fixed *body =
330 (struct oz_isoc_fixed *)data_hdr;
331 int data_len = len-sizeof(struct oz_isoc_fixed)+1;
332 int unit_size = body->unit_size;
333 u8 *data = body->data;
334 int count;
335 int i;
336 if (!unit_size)
337 break;
338 count = data_len/unit_size;
339 for (i = 0; i < count; i++) {
340 oz_hcd_data_ind(usb_ctx->hport,
341 body->endpoint, data, unit_size);
342 data += unit_size;
343 }
344 }
345 break;
346 }
347
348}
349/*------------------------------------------------------------------------------
350 * This is called when the PD has received a USB element. The type of element
351 * is determined and is then passed to an appropriate handler function.
352 * Context: softirq-serialized
353 */
354void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt)
355{
356 struct oz_usb_hdr *usb_hdr = (struct oz_usb_hdr *)(elt + 1);
357 struct oz_usb_ctx *usb_ctx;
358
359 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
360 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
361 if (usb_ctx)
362 oz_usb_get(usb_ctx);
363 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100364 if (usb_ctx == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000365 return; /* Context has gone so nothing to do. */
366 if (usb_ctx->stopped)
367 goto done;
368 /* If sequence number is non-zero then check it is not a duplicate.
369 * Zero sequence numbers are always accepted.
370 */
371 if (usb_hdr->elt_seq_num != 0) {
372 if (((usb_ctx->rx_seq_num - usb_hdr->elt_seq_num) & 0x80) == 0)
373 /* Reject duplicate element. */
374 goto done;
375 }
376 usb_ctx->rx_seq_num = usb_hdr->elt_seq_num;
377 switch (usb_hdr->type) {
378 case OZ_GET_DESC_RSP: {
379 struct oz_get_desc_rsp *body =
380 (struct oz_get_desc_rsp *)usb_hdr;
381 int data_len = elt->length -
382 sizeof(struct oz_get_desc_rsp) + 1;
383 u16 offs = le16_to_cpu(get_unaligned(&body->offset));
384 u16 total_size =
385 le16_to_cpu(get_unaligned(&body->total_size));
Joe Perchesf724b582013-07-23 13:45:00 +0100386 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
Chris Kellyb3147862012-02-20 21:12:08 +0000387 oz_hcd_get_desc_cnf(usb_ctx->hport, body->req_id,
388 body->rcode, body->data,
389 data_len, offs, total_size);
390 }
391 break;
392 case OZ_SET_CONFIG_RSP: {
393 struct oz_set_config_rsp *body =
394 (struct oz_set_config_rsp *)usb_hdr;
395 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100396 body->rcode, NULL, 0);
Chris Kellyb3147862012-02-20 21:12:08 +0000397 }
398 break;
399 case OZ_SET_INTERFACE_RSP: {
400 struct oz_set_interface_rsp *body =
401 (struct oz_set_interface_rsp *)usb_hdr;
402 oz_hcd_control_cnf(usb_ctx->hport,
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100403 body->req_id, body->rcode, NULL, 0);
Chris Kellyb3147862012-02-20 21:12:08 +0000404 }
405 break;
406 case OZ_VENDOR_CLASS_RSP: {
407 struct oz_vendor_class_rsp *body =
408 (struct oz_vendor_class_rsp *)usb_hdr;
409 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
410 body->rcode, body->data, elt->length-
411 sizeof(struct oz_vendor_class_rsp)+1);
412 }
413 break;
414 case OZ_USB_ENDPOINT_DATA:
415 oz_usb_handle_ep_data(usb_ctx, usb_hdr, elt->length);
416 break;
417 }
418done:
419 oz_usb_put(usb_ctx);
420}
421/*------------------------------------------------------------------------------
422 * Context: softirq, process
423 */
424void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len)
425{
426 struct oz_usb_ctx *usb_ctx;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100427
Chris Kellyb3147862012-02-20 21:12:08 +0000428 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
429 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
430 if (usb_ctx)
431 oz_usb_get(usb_ctx);
432 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
Peter Huewe4d1b2fb2013-02-15 15:22:23 +0100433 if (usb_ctx == NULL)
Chris Kellyb3147862012-02-20 21:12:08 +0000434 return; /* Context has gone so nothing to do. */
435 if (!usb_ctx->stopped) {
Joe Perchesf724b582013-07-23 13:45:00 +0100436 oz_dbg(ON, "Farewell indicated ep = 0x%x\n", ep_num);
Chris Kellyb3147862012-02-20 21:12:08 +0000437 oz_hcd_data_ind(usb_ctx->hport, ep_num, data, len);
438 }
439 oz_usb_put(usb_ctx);
440}