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>
18 #include "ozprotocol.h"
26 /*------------------------------------------------------------------------------
28 #define MAX_ISOC_FIXED_DATA (253-sizeof(struct oz_isoc_fixed))
29 /*------------------------------------------------------------------------------
32 static int oz_usb_submit_elt(struct oz_elt_buf *eb, struct oz_elt_info *ei,
33 struct oz_usb_ctx *usb_ctx, u8 strid, u8 isoc)
36 struct oz_elt *elt = (struct oz_elt *)ei->data;
37 struct oz_app_hdr *app_hdr = (struct oz_app_hdr *)(elt+1);
38 elt->type = OZ_ELT_APP_DATA;
39 ei->app_id = OZ_APPID_USB;
40 ei->length = elt->length + sizeof(struct oz_elt);
41 app_hdr->app_id = OZ_APPID_USB;
42 spin_lock_bh(&eb->lock);
44 app_hdr->elt_seq_num = usb_ctx->tx_seq_num++;
45 if (usb_ctx->tx_seq_num == 0)
46 usb_ctx->tx_seq_num = 1;
48 ret = oz_queue_elt_info(eb, isoc, strid, ei);
50 oz_elt_info_free(eb, ei);
51 spin_unlock_bh(&eb->lock);
54 /*------------------------------------------------------------------------------
57 int oz_usb_get_desc_req(void *hpd, u8 req_id, u8 req_type, u8 desc_type,
58 u8 index, u16 windex, int offset, int len)
60 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
61 struct oz_pd *pd = usb_ctx->pd;
63 struct oz_get_desc_req *body;
64 struct oz_elt_buf *eb = &pd->elt_buff;
65 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
66 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);
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;
87 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
89 /*------------------------------------------------------------------------------
92 static int oz_usb_set_config_req(void *hpd, u8 req_id, u8 index)
94 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
95 struct oz_pd *pd = usb_ctx->pd;
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;
102 elt = (struct oz_elt *)ei->data;
103 elt->length = sizeof(struct oz_set_config_req);
104 body = (struct oz_set_config_req *)(elt+1);
105 body->type = OZ_SET_CONFIG_REQ;
106 body->req_id = req_id;
108 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
110 /*------------------------------------------------------------------------------
113 static int oz_usb_set_interface_req(void *hpd, u8 req_id, u8 index, u8 alt)
115 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
116 struct oz_pd *pd = usb_ctx->pd;
118 struct oz_elt_buf *eb = &pd->elt_buff;
119 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
120 struct oz_set_interface_req *body;
123 elt = (struct oz_elt *)ei->data;
124 elt->length = sizeof(struct oz_set_interface_req);
125 body = (struct oz_set_interface_req *)(elt+1);
126 body->type = OZ_SET_INTERFACE_REQ;
127 body->req_id = req_id;
129 body->alternative = alt;
130 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
132 /*------------------------------------------------------------------------------
135 static int oz_usb_set_clear_feature_req(void *hpd, u8 req_id, u8 type,
136 u8 recipient, u8 index, __le16 feature)
138 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
139 struct oz_pd *pd = usb_ctx->pd;
141 struct oz_elt_buf *eb = &pd->elt_buff;
142 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
143 struct oz_feature_req *body;
146 elt = (struct oz_elt *)ei->data;
147 elt->length = sizeof(struct oz_feature_req);
148 body = (struct oz_feature_req *)(elt+1);
150 body->req_id = req_id;
151 body->recipient = recipient;
153 put_unaligned(feature, &body->feature);
154 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
156 /*------------------------------------------------------------------------------
159 static int oz_usb_vendor_class_req(void *hpd, u8 req_id, u8 req_type,
160 u8 request, __le16 value, __le16 index, const u8 *data, int data_len)
162 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
163 struct oz_pd *pd = usb_ctx->pd;
165 struct oz_elt_buf *eb = &pd->elt_buff;
166 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
167 struct oz_vendor_class_req *body;
170 elt = (struct oz_elt *)ei->data;
171 elt->length = sizeof(struct oz_vendor_class_req) - 1 + data_len;
172 body = (struct oz_vendor_class_req *)(elt+1);
173 body->type = OZ_VENDOR_CLASS_REQ;
174 body->req_id = req_id;
175 body->req_type = req_type;
176 body->request = request;
177 put_unaligned(value, &body->value);
178 put_unaligned(index, &body->index);
180 memcpy(body->data, data, data_len);
181 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
183 /*------------------------------------------------------------------------------
186 int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup,
187 const u8 *data, int data_len)
189 unsigned wvalue = le16_to_cpu(setup->wValue);
190 unsigned windex = le16_to_cpu(setup->wIndex);
191 unsigned wlength = le16_to_cpu(setup->wLength);
193 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
194 switch (setup->bRequest) {
195 case USB_REQ_GET_DESCRIPTOR:
196 rc = oz_usb_get_desc_req(hpd, req_id,
197 setup->bRequestType, (u8)(wvalue>>8),
198 (u8)wvalue, setup->wIndex, 0, wlength);
200 case USB_REQ_SET_CONFIGURATION:
201 rc = oz_usb_set_config_req(hpd, req_id, (u8)wvalue);
203 case USB_REQ_SET_INTERFACE: {
204 u8 if_num = (u8)windex;
206 rc = oz_usb_set_interface_req(hpd, req_id,
210 case USB_REQ_SET_FEATURE:
211 rc = oz_usb_set_clear_feature_req(hpd, req_id,
213 setup->bRequestType & 0xf, (u8)windex,
216 case USB_REQ_CLEAR_FEATURE:
217 rc = oz_usb_set_clear_feature_req(hpd, req_id,
218 OZ_CLEAR_FEATURE_REQ,
219 setup->bRequestType & 0xf,
220 (u8)windex, setup->wValue);
224 rc = oz_usb_vendor_class_req(hpd, req_id, setup->bRequestType,
225 setup->bRequest, setup->wValue, setup->wIndex,
230 /*------------------------------------------------------------------------------
233 int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb)
235 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
236 struct oz_pd *pd = usb_ctx->pd;
237 struct oz_elt_buf *eb;
241 struct usb_iso_packet_descriptor *desc;
243 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
244 for (i = 0; i < urb->number_of_packets; i++) {
246 desc = &urb->iso_frame_desc[i];
247 data = ((u8 *)urb->transfer_buffer)+desc->offset;
248 oz_send_isoc_unit(pd, ep_num, data, desc->length);
253 hdr_size = sizeof(struct oz_isoc_fixed) - 1;
256 while (i < urb->number_of_packets) {
257 struct oz_elt_info *ei = oz_elt_info_alloc(eb);
259 struct oz_isoc_fixed *body;
265 rem = MAX_ISOC_FIXED_DATA;
266 elt = (struct oz_elt *)ei->data;
267 body = (struct oz_isoc_fixed *)(elt + 1);
268 body->type = OZ_USB_ENDPOINT_DATA;
269 body->endpoint = ep_num;
270 body->format = OZ_DATA_F_ISOC_FIXED;
271 unit_size = urb->iso_frame_desc[i].length;
272 body->unit_size = (u8)unit_size;
273 data = ((u8 *)(elt+1)) + hdr_size;
275 while (i < urb->number_of_packets) {
276 desc = &urb->iso_frame_desc[i];
277 if ((unit_size == desc->length) &&
278 (desc->length <= rem)) {
279 memcpy(data, ((u8 *)urb->transfer_buffer) +
280 desc->offset, unit_size);
285 desc->actual_length = desc->length;
291 elt->length = hdr_size + MAX_ISOC_FIXED_DATA - rem;
292 /* Store the number of units in body->frame_number for the
293 * moment. This field will be correctly determined before
294 * the element is sent. */
295 body->frame_number = (u8)unit_count;
296 oz_usb_submit_elt(eb, ei, usb_ctx, ep_num,
297 pd->mode & OZ_F_ISOC_ANYTIME);
301 /*------------------------------------------------------------------------------
302 * Context: softirq-serialized
304 static void oz_usb_handle_ep_data(struct oz_usb_ctx *usb_ctx,
305 struct oz_usb_hdr *usb_hdr, int len)
307 struct oz_data *data_hdr = (struct oz_data *)usb_hdr;
308 switch (data_hdr->format) {
309 case OZ_DATA_F_MULTIPLE_FIXED: {
310 struct oz_multiple_fixed *body =
311 (struct oz_multiple_fixed *)data_hdr;
312 u8 *data = body->data;
313 int n = (len - sizeof(struct oz_multiple_fixed)+1)
316 oz_hcd_data_ind(usb_ctx->hport, body->endpoint,
317 data, body->unit_size);
318 data += body->unit_size;
322 case OZ_DATA_F_ISOC_FIXED: {
323 struct oz_isoc_fixed *body =
324 (struct oz_isoc_fixed *)data_hdr;
325 int data_len = len-sizeof(struct oz_isoc_fixed)+1;
326 int unit_size = body->unit_size;
327 u8 *data = body->data;
332 count = data_len/unit_size;
333 for (i = 0; i < count; i++) {
334 oz_hcd_data_ind(usb_ctx->hport,
335 body->endpoint, data, unit_size);
343 /*------------------------------------------------------------------------------
344 * This is called when the PD has received a USB element. The type of element
345 * is determined and is then passed to an appropriate handler function.
346 * Context: softirq-serialized
348 void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt)
350 struct oz_usb_hdr *usb_hdr = (struct oz_usb_hdr *)(elt + 1);
351 struct oz_usb_ctx *usb_ctx;
353 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
354 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
357 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
359 return; /* Context has gone so nothing to do. */
360 if (usb_ctx->stopped)
362 /* If sequence number is non-zero then check it is not a duplicate.
363 * Zero sequence numbers are always accepted.
365 if (usb_hdr->elt_seq_num != 0) {
366 if (((usb_ctx->rx_seq_num - usb_hdr->elt_seq_num) & 0x80) == 0)
367 /* Reject duplicate element. */
370 usb_ctx->rx_seq_num = usb_hdr->elt_seq_num;
371 switch (usb_hdr->type) {
372 case OZ_GET_DESC_RSP: {
373 struct oz_get_desc_rsp *body =
374 (struct oz_get_desc_rsp *)usb_hdr;
375 int data_len = elt->length -
376 sizeof(struct oz_get_desc_rsp) + 1;
377 u16 offs = le16_to_cpu(get_unaligned(&body->offset));
379 le16_to_cpu(get_unaligned(&body->total_size));
380 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
381 oz_hcd_get_desc_cnf(usb_ctx->hport, body->req_id,
382 body->rcode, body->data,
383 data_len, offs, total_size);
386 case OZ_SET_CONFIG_RSP: {
387 struct oz_set_config_rsp *body =
388 (struct oz_set_config_rsp *)usb_hdr;
389 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
390 body->rcode, NULL, 0);
393 case OZ_SET_INTERFACE_RSP: {
394 struct oz_set_interface_rsp *body =
395 (struct oz_set_interface_rsp *)usb_hdr;
396 oz_hcd_control_cnf(usb_ctx->hport,
397 body->req_id, body->rcode, NULL, 0);
400 case OZ_VENDOR_CLASS_RSP: {
401 struct oz_vendor_class_rsp *body =
402 (struct oz_vendor_class_rsp *)usb_hdr;
403 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
404 body->rcode, body->data, elt->length-
405 sizeof(struct oz_vendor_class_rsp)+1);
408 case OZ_USB_ENDPOINT_DATA:
409 oz_usb_handle_ep_data(usb_ctx, usb_hdr, elt->length);
415 /*------------------------------------------------------------------------------
416 * Context: softirq, process
418 void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len)
420 struct oz_usb_ctx *usb_ctx;
421 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
422 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
425 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
427 return; /* Context has gone so nothing to do. */
428 if (!usb_ctx->stopped) {
429 oz_dbg(ON, "Farewell indicated ep = 0x%x\n", ep_num);
430 oz_hcd_data_ind(usb_ctx->hport, ep_num, data, len);