1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
5 * This file implements the protocol specific parts of the USB service for a PD.
6 * -----------------------------------------------------------------------------
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>
17 #include "ozprotocol.h"
24 /*------------------------------------------------------------------------------
26 #define MAX_ISOC_FIXED_DATA (253-sizeof(struct oz_isoc_fixed))
27 /*------------------------------------------------------------------------------
30 static 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)
34 struct oz_elt *elt = (struct oz_elt *)ei->data;
35 struct oz_app_hdr *app_hdr = (struct oz_app_hdr *)(elt+1);
36 elt->type = OZ_ELT_APP_DATA;
37 ei->app_id = OZ_APPID_USB;
38 ei->length = elt->length + sizeof(struct oz_elt);
39 app_hdr->app_id = OZ_APPID_USB;
40 spin_lock_bh(&eb->lock);
42 app_hdr->elt_seq_num = usb_ctx->tx_seq_num++;
43 if (usb_ctx->tx_seq_num == 0)
44 usb_ctx->tx_seq_num = 1;
46 ret = oz_queue_elt_info(eb, isoc, strid, ei);
48 oz_elt_info_free(eb, ei);
49 spin_unlock_bh(&eb->lock);
52 /*------------------------------------------------------------------------------
55 int oz_usb_get_desc_req(void *hpd, u8 req_id, u8 req_type, u8 desc_type,
56 u8 index, u16 windex, int offset, int len)
58 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
59 struct oz_pd *pd = usb_ctx->pd;
61 struct oz_get_desc_req *body;
62 struct oz_elt_buf *eb = &pd->elt_buff;
63 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
64 oz_dbg(ON, " req_type = 0x%x\n", req_type);
65 oz_dbg(ON, " desc_type = 0x%x\n", desc_type);
66 oz_dbg(ON, " index = 0x%x\n", index);
67 oz_dbg(ON, " windex = 0x%x\n", windex);
68 oz_dbg(ON, " offset = 0x%x\n", offset);
69 oz_dbg(ON, " len = 0x%x\n", len);
74 elt = (struct oz_elt *)ei->data;
75 elt->length = sizeof(struct oz_get_desc_req);
76 body = (struct oz_get_desc_req *)(elt+1);
77 body->type = OZ_GET_DESC_REQ;
78 body->req_id = req_id;
79 put_unaligned(cpu_to_le16(offset), &body->offset);
80 put_unaligned(cpu_to_le16(len), &body->size);
81 body->req_type = req_type;
82 body->desc_type = desc_type;
83 body->w_index = windex;
85 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
87 /*------------------------------------------------------------------------------
90 static int oz_usb_set_config_req(void *hpd, u8 req_id, u8 index)
92 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
93 struct oz_pd *pd = usb_ctx->pd;
95 struct oz_elt_buf *eb = &pd->elt_buff;
96 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
97 struct oz_set_config_req *body;
100 elt = (struct oz_elt *)ei->data;
101 elt->length = sizeof(struct oz_set_config_req);
102 body = (struct oz_set_config_req *)(elt+1);
103 body->type = OZ_SET_CONFIG_REQ;
104 body->req_id = req_id;
106 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
108 /*------------------------------------------------------------------------------
111 static int oz_usb_set_interface_req(void *hpd, u8 req_id, u8 index, u8 alt)
113 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
114 struct oz_pd *pd = usb_ctx->pd;
116 struct oz_elt_buf *eb = &pd->elt_buff;
117 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
118 struct oz_set_interface_req *body;
121 elt = (struct oz_elt *)ei->data;
122 elt->length = sizeof(struct oz_set_interface_req);
123 body = (struct oz_set_interface_req *)(elt+1);
124 body->type = OZ_SET_INTERFACE_REQ;
125 body->req_id = req_id;
127 body->alternative = alt;
128 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
130 /*------------------------------------------------------------------------------
133 static int oz_usb_set_clear_feature_req(void *hpd, u8 req_id, u8 type,
134 u8 recipient, u8 index, __le16 feature)
136 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
137 struct oz_pd *pd = usb_ctx->pd;
139 struct oz_elt_buf *eb = &pd->elt_buff;
140 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
141 struct oz_feature_req *body;
144 elt = (struct oz_elt *)ei->data;
145 elt->length = sizeof(struct oz_feature_req);
146 body = (struct oz_feature_req *)(elt+1);
148 body->req_id = req_id;
149 body->recipient = recipient;
151 put_unaligned(feature, &body->feature);
152 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
154 /*------------------------------------------------------------------------------
157 static int oz_usb_vendor_class_req(void *hpd, u8 req_id, u8 req_type,
158 u8 request, __le16 value, __le16 index, const u8 *data, int data_len)
160 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
161 struct oz_pd *pd = usb_ctx->pd;
163 struct oz_elt_buf *eb = &pd->elt_buff;
164 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
165 struct oz_vendor_class_req *body;
168 elt = (struct oz_elt *)ei->data;
169 elt->length = sizeof(struct oz_vendor_class_req) - 1 + data_len;
170 body = (struct oz_vendor_class_req *)(elt+1);
171 body->type = OZ_VENDOR_CLASS_REQ;
172 body->req_id = req_id;
173 body->req_type = req_type;
174 body->request = request;
175 put_unaligned(value, &body->value);
176 put_unaligned(index, &body->index);
178 memcpy(body->data, data, data_len);
179 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
181 /*------------------------------------------------------------------------------
184 int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup,
185 const u8 *data, int data_len)
187 unsigned wvalue = le16_to_cpu(setup->wValue);
188 unsigned windex = le16_to_cpu(setup->wIndex);
189 unsigned wlength = le16_to_cpu(setup->wLength);
191 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
192 switch (setup->bRequest) {
193 case USB_REQ_GET_DESCRIPTOR:
194 rc = oz_usb_get_desc_req(hpd, req_id,
195 setup->bRequestType, (u8)(wvalue>>8),
196 (u8)wvalue, setup->wIndex, 0, wlength);
198 case USB_REQ_SET_CONFIGURATION:
199 rc = oz_usb_set_config_req(hpd, req_id, (u8)wvalue);
201 case USB_REQ_SET_INTERFACE: {
202 u8 if_num = (u8)windex;
204 rc = oz_usb_set_interface_req(hpd, req_id,
208 case USB_REQ_SET_FEATURE:
209 rc = oz_usb_set_clear_feature_req(hpd, req_id,
211 setup->bRequestType & 0xf, (u8)windex,
214 case USB_REQ_CLEAR_FEATURE:
215 rc = oz_usb_set_clear_feature_req(hpd, req_id,
216 OZ_CLEAR_FEATURE_REQ,
217 setup->bRequestType & 0xf,
218 (u8)windex, setup->wValue);
222 rc = oz_usb_vendor_class_req(hpd, req_id, setup->bRequestType,
223 setup->bRequest, setup->wValue, setup->wIndex,
228 /*------------------------------------------------------------------------------
231 int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb)
233 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
234 struct oz_pd *pd = usb_ctx->pd;
235 struct oz_elt_buf *eb;
239 struct usb_iso_packet_descriptor *desc;
241 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
242 for (i = 0; i < urb->number_of_packets; i++) {
244 desc = &urb->iso_frame_desc[i];
245 data = ((u8 *)urb->transfer_buffer)+desc->offset;
246 oz_send_isoc_unit(pd, ep_num, data, desc->length);
251 hdr_size = sizeof(struct oz_isoc_fixed) - 1;
254 while (i < urb->number_of_packets) {
255 struct oz_elt_info *ei = oz_elt_info_alloc(eb);
257 struct oz_isoc_fixed *body;
263 rem = MAX_ISOC_FIXED_DATA;
264 elt = (struct oz_elt *)ei->data;
265 body = (struct oz_isoc_fixed *)(elt + 1);
266 body->type = OZ_USB_ENDPOINT_DATA;
267 body->endpoint = ep_num;
268 body->format = OZ_DATA_F_ISOC_FIXED;
269 unit_size = urb->iso_frame_desc[i].length;
270 body->unit_size = (u8)unit_size;
271 data = ((u8 *)(elt+1)) + hdr_size;
273 while (i < urb->number_of_packets) {
274 desc = &urb->iso_frame_desc[i];
275 if ((unit_size == desc->length) &&
276 (desc->length <= rem)) {
277 memcpy(data, ((u8 *)urb->transfer_buffer) +
278 desc->offset, unit_size);
283 desc->actual_length = desc->length;
289 elt->length = hdr_size + MAX_ISOC_FIXED_DATA - rem;
290 /* Store the number of units in body->frame_number for the
291 * moment. This field will be correctly determined before
292 * the element is sent. */
293 body->frame_number = (u8)unit_count;
294 oz_usb_submit_elt(eb, ei, usb_ctx, ep_num,
295 pd->mode & OZ_F_ISOC_ANYTIME);
299 /*------------------------------------------------------------------------------
300 * Context: softirq-serialized
302 static void oz_usb_handle_ep_data(struct oz_usb_ctx *usb_ctx,
303 struct oz_usb_hdr *usb_hdr, int len)
305 struct oz_data *data_hdr = (struct oz_data *)usb_hdr;
306 switch (data_hdr->format) {
307 case OZ_DATA_F_MULTIPLE_FIXED: {
308 struct oz_multiple_fixed *body =
309 (struct oz_multiple_fixed *)data_hdr;
310 u8 *data = body->data;
311 int n = (len - sizeof(struct oz_multiple_fixed)+1)
314 oz_hcd_data_ind(usb_ctx->hport, body->endpoint,
315 data, body->unit_size);
316 data += body->unit_size;
320 case OZ_DATA_F_ISOC_FIXED: {
321 struct oz_isoc_fixed *body =
322 (struct oz_isoc_fixed *)data_hdr;
323 int data_len = len-sizeof(struct oz_isoc_fixed)+1;
324 int unit_size = body->unit_size;
325 u8 *data = body->data;
330 count = data_len/unit_size;
331 for (i = 0; i < count; i++) {
332 oz_hcd_data_ind(usb_ctx->hport,
333 body->endpoint, data, unit_size);
341 /*------------------------------------------------------------------------------
342 * This is called when the PD has received a USB element. The type of element
343 * is determined and is then passed to an appropriate handler function.
344 * Context: softirq-serialized
346 void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt)
348 struct oz_usb_hdr *usb_hdr = (struct oz_usb_hdr *)(elt + 1);
349 struct oz_usb_ctx *usb_ctx;
351 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
352 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
355 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
357 return; /* Context has gone so nothing to do. */
358 if (usb_ctx->stopped)
360 /* If sequence number is non-zero then check it is not a duplicate.
361 * Zero sequence numbers are always accepted.
363 if (usb_hdr->elt_seq_num != 0) {
364 if (((usb_ctx->rx_seq_num - usb_hdr->elt_seq_num) & 0x80) == 0)
365 /* Reject duplicate element. */
368 usb_ctx->rx_seq_num = usb_hdr->elt_seq_num;
369 switch (usb_hdr->type) {
370 case OZ_GET_DESC_RSP: {
371 struct oz_get_desc_rsp *body =
372 (struct oz_get_desc_rsp *)usb_hdr;
373 int data_len = elt->length -
374 sizeof(struct oz_get_desc_rsp) + 1;
375 u16 offs = le16_to_cpu(get_unaligned(&body->offset));
377 le16_to_cpu(get_unaligned(&body->total_size));
378 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
379 oz_hcd_get_desc_cnf(usb_ctx->hport, body->req_id,
380 body->rcode, body->data,
381 data_len, offs, total_size);
384 case OZ_SET_CONFIG_RSP: {
385 struct oz_set_config_rsp *body =
386 (struct oz_set_config_rsp *)usb_hdr;
387 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
388 body->rcode, NULL, 0);
391 case OZ_SET_INTERFACE_RSP: {
392 struct oz_set_interface_rsp *body =
393 (struct oz_set_interface_rsp *)usb_hdr;
394 oz_hcd_control_cnf(usb_ctx->hport,
395 body->req_id, body->rcode, NULL, 0);
398 case OZ_VENDOR_CLASS_RSP: {
399 struct oz_vendor_class_rsp *body =
400 (struct oz_vendor_class_rsp *)usb_hdr;
401 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
402 body->rcode, body->data, elt->length-
403 sizeof(struct oz_vendor_class_rsp)+1);
406 case OZ_USB_ENDPOINT_DATA:
407 oz_usb_handle_ep_data(usb_ctx, usb_hdr, elt->length);
413 /*------------------------------------------------------------------------------
414 * Context: softirq, process
416 void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len)
418 struct oz_usb_ctx *usb_ctx;
419 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
420 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
423 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
425 return; /* Context has gone so nothing to do. */
426 if (!usb_ctx->stopped) {
427 oz_dbg(ON, "Farewell indicated ep = 0x%x\n", ep_num);
428 oz_hcd_data_ind(usb_ctx->hport, ep_num, data, len);