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"
25 /*------------------------------------------------------------------------------
27 #define MAX_ISOC_FIXED_DATA (253-sizeof(struct oz_isoc_fixed))
28 /*------------------------------------------------------------------------------
31 static int oz_usb_submit_elt(struct oz_elt_buf *eb, struct oz_elt_info *ei,
32 struct oz_usb_ctx *usb_ctx, u8 strid, u8 isoc)
35 struct oz_elt *elt = (struct oz_elt *)ei->data;
36 struct oz_app_hdr *app_hdr = (struct oz_app_hdr *)(elt+1);
37 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);
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;
47 ret = oz_queue_elt_info(eb, isoc, strid, ei);
49 oz_elt_info_free(eb, ei);
50 spin_unlock_bh(&eb->lock);
53 /*------------------------------------------------------------------------------
56 int 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)
59 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
60 struct oz_pd *pd = usb_ctx->pd;
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);
65 oz_trace(" req_type = 0x%x\n", req_type);
66 oz_trace(" desc_type = 0x%x\n", desc_type);
67 oz_trace(" index = 0x%x\n", index);
68 oz_trace(" windex = 0x%x\n", windex);
69 oz_trace(" offset = 0x%x\n", offset);
70 oz_trace(" len = 0x%x\n", len);
75 elt = (struct oz_elt *)ei->data;
76 elt->length = sizeof(struct oz_get_desc_req);
77 body = (struct oz_get_desc_req *)(elt+1);
78 body->type = OZ_GET_DESC_REQ;
79 body->req_id = req_id;
80 put_unaligned(cpu_to_le16(offset), &body->offset);
81 put_unaligned(cpu_to_le16(len), &body->size);
82 body->req_type = req_type;
83 body->desc_type = desc_type;
84 body->w_index = windex;
86 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
88 /*------------------------------------------------------------------------------
91 static int oz_usb_set_config_req(void *hpd, u8 req_id, u8 index)
93 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
94 struct oz_pd *pd = usb_ctx->pd;
96 struct oz_elt_buf *eb = &pd->elt_buff;
97 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
98 struct oz_set_config_req *body;
101 elt = (struct oz_elt *)ei->data;
102 elt->length = sizeof(struct oz_set_config_req);
103 body = (struct oz_set_config_req *)(elt+1);
104 body->type = OZ_SET_CONFIG_REQ;
105 body->req_id = req_id;
107 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
109 /*------------------------------------------------------------------------------
112 static int oz_usb_set_interface_req(void *hpd, u8 req_id, u8 index, u8 alt)
114 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
115 struct oz_pd *pd = usb_ctx->pd;
117 struct oz_elt_buf *eb = &pd->elt_buff;
118 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
119 struct oz_set_interface_req *body;
122 elt = (struct oz_elt *)ei->data;
123 elt->length = sizeof(struct oz_set_interface_req);
124 body = (struct oz_set_interface_req *)(elt+1);
125 body->type = OZ_SET_INTERFACE_REQ;
126 body->req_id = req_id;
128 body->alternative = alt;
129 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
131 /*------------------------------------------------------------------------------
134 static int oz_usb_set_clear_feature_req(void *hpd, u8 req_id, u8 type,
135 u8 recipient, u8 index, __le16 feature)
137 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
138 struct oz_pd *pd = usb_ctx->pd;
140 struct oz_elt_buf *eb = &pd->elt_buff;
141 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
142 struct oz_feature_req *body;
145 elt = (struct oz_elt *)ei->data;
146 elt->length = sizeof(struct oz_feature_req);
147 body = (struct oz_feature_req *)(elt+1);
149 body->req_id = req_id;
150 body->recipient = recipient;
152 put_unaligned(feature, &body->feature);
153 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
155 /*------------------------------------------------------------------------------
158 static int oz_usb_vendor_class_req(void *hpd, u8 req_id, u8 req_type,
159 u8 request, __le16 value, __le16 index, const u8 *data, int data_len)
161 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
162 struct oz_pd *pd = usb_ctx->pd;
164 struct oz_elt_buf *eb = &pd->elt_buff;
165 struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff);
166 struct oz_vendor_class_req *body;
169 elt = (struct oz_elt *)ei->data;
170 elt->length = sizeof(struct oz_vendor_class_req) - 1 + data_len;
171 body = (struct oz_vendor_class_req *)(elt+1);
172 body->type = OZ_VENDOR_CLASS_REQ;
173 body->req_id = req_id;
174 body->req_type = req_type;
175 body->request = request;
176 put_unaligned(value, &body->value);
177 put_unaligned(index, &body->index);
179 memcpy(body->data, data, data_len);
180 return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
182 /*------------------------------------------------------------------------------
185 int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup,
186 const u8 *data, int data_len)
188 unsigned wvalue = le16_to_cpu(setup->wValue);
189 unsigned windex = le16_to_cpu(setup->wIndex);
190 unsigned wlength = le16_to_cpu(setup->wLength);
192 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
193 switch (setup->bRequest) {
194 case USB_REQ_GET_DESCRIPTOR:
195 rc = oz_usb_get_desc_req(hpd, req_id,
196 setup->bRequestType, (u8)(wvalue>>8),
197 (u8)wvalue, setup->wIndex, 0, wlength);
199 case USB_REQ_SET_CONFIGURATION:
200 rc = oz_usb_set_config_req(hpd, req_id, (u8)wvalue);
202 case USB_REQ_SET_INTERFACE: {
203 u8 if_num = (u8)windex;
205 rc = oz_usb_set_interface_req(hpd, req_id,
209 case USB_REQ_SET_FEATURE:
210 rc = oz_usb_set_clear_feature_req(hpd, req_id,
212 setup->bRequestType & 0xf, (u8)windex,
215 case USB_REQ_CLEAR_FEATURE:
216 rc = oz_usb_set_clear_feature_req(hpd, req_id,
217 OZ_CLEAR_FEATURE_REQ,
218 setup->bRequestType & 0xf,
219 (u8)windex, setup->wValue);
223 rc = oz_usb_vendor_class_req(hpd, req_id, setup->bRequestType,
224 setup->bRequest, setup->wValue, setup->wIndex,
229 /*------------------------------------------------------------------------------
232 int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb)
234 struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
235 struct oz_pd *pd = usb_ctx->pd;
236 struct oz_elt_buf *eb;
240 struct usb_iso_packet_descriptor *desc;
242 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
243 for (i = 0; i < urb->number_of_packets; i++) {
245 desc = &urb->iso_frame_desc[i];
246 data = ((u8 *)urb->transfer_buffer)+desc->offset;
247 oz_send_isoc_unit(pd, ep_num, data, desc->length);
252 hdr_size = sizeof(struct oz_isoc_fixed) - 1;
255 while (i < urb->number_of_packets) {
256 struct oz_elt_info *ei = oz_elt_info_alloc(eb);
258 struct oz_isoc_fixed *body;
264 rem = MAX_ISOC_FIXED_DATA;
265 elt = (struct oz_elt *)ei->data;
266 body = (struct oz_isoc_fixed *)(elt + 1);
267 body->type = OZ_USB_ENDPOINT_DATA;
268 body->endpoint = ep_num;
269 body->format = OZ_DATA_F_ISOC_FIXED;
270 unit_size = urb->iso_frame_desc[i].length;
271 body->unit_size = (u8)unit_size;
272 data = ((u8 *)(elt+1)) + hdr_size;
274 while (i < urb->number_of_packets) {
275 desc = &urb->iso_frame_desc[i];
276 if ((unit_size == desc->length) &&
277 (desc->length <= rem)) {
278 memcpy(data, ((u8 *)urb->transfer_buffer) +
279 desc->offset, unit_size);
284 desc->actual_length = desc->length;
290 elt->length = hdr_size + MAX_ISOC_FIXED_DATA - rem;
291 /* Store the number of units in body->frame_number for the
292 * moment. This field will be correctly determined before
293 * the element is sent. */
294 body->frame_number = (u8)unit_count;
295 oz_usb_submit_elt(eb, ei, usb_ctx, ep_num,
296 pd->mode & OZ_F_ISOC_ANYTIME);
300 /*------------------------------------------------------------------------------
301 * Context: softirq-serialized
303 static void oz_usb_handle_ep_data(struct oz_usb_ctx *usb_ctx,
304 struct oz_usb_hdr *usb_hdr, int len)
306 struct oz_data *data_hdr = (struct oz_data *)usb_hdr;
307 switch (data_hdr->format) {
308 case OZ_DATA_F_MULTIPLE_FIXED: {
309 struct oz_multiple_fixed *body =
310 (struct oz_multiple_fixed *)data_hdr;
311 u8 *data = body->data;
312 int n = (len - sizeof(struct oz_multiple_fixed)+1)
315 oz_hcd_data_ind(usb_ctx->hport, body->endpoint,
316 data, body->unit_size);
317 data += body->unit_size;
321 case OZ_DATA_F_ISOC_FIXED: {
322 struct oz_isoc_fixed *body =
323 (struct oz_isoc_fixed *)data_hdr;
324 int data_len = len-sizeof(struct oz_isoc_fixed)+1;
325 int unit_size = body->unit_size;
326 u8 *data = body->data;
331 count = data_len/unit_size;
332 for (i = 0; i < count; i++) {
333 oz_hcd_data_ind(usb_ctx->hport,
334 body->endpoint, data, unit_size);
342 /*------------------------------------------------------------------------------
343 * This is called when the PD has received a USB element. The type of element
344 * is determined and is then passed to an appropriate handler function.
345 * Context: softirq-serialized
347 void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt)
349 struct oz_usb_hdr *usb_hdr = (struct oz_usb_hdr *)(elt + 1);
350 struct oz_usb_ctx *usb_ctx;
352 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
353 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
356 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
358 return; /* Context has gone so nothing to do. */
359 if (usb_ctx->stopped)
361 /* If sequence number is non-zero then check it is not a duplicate.
362 * Zero sequence numbers are always accepted.
364 if (usb_hdr->elt_seq_num != 0) {
365 if (((usb_ctx->rx_seq_num - usb_hdr->elt_seq_num) & 0x80) == 0)
366 /* Reject duplicate element. */
369 usb_ctx->rx_seq_num = usb_hdr->elt_seq_num;
370 switch (usb_hdr->type) {
371 case OZ_GET_DESC_RSP: {
372 struct oz_get_desc_rsp *body =
373 (struct oz_get_desc_rsp *)usb_hdr;
374 int data_len = elt->length -
375 sizeof(struct oz_get_desc_rsp) + 1;
376 u16 offs = le16_to_cpu(get_unaligned(&body->offset));
378 le16_to_cpu(get_unaligned(&body->total_size));
379 oz_trace("USB_REQ_GET_DESCRIPTOR - cnf\n");
380 oz_hcd_get_desc_cnf(usb_ctx->hport, body->req_id,
381 body->rcode, body->data,
382 data_len, offs, total_size);
385 case OZ_SET_CONFIG_RSP: {
386 struct oz_set_config_rsp *body =
387 (struct oz_set_config_rsp *)usb_hdr;
388 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
389 body->rcode, NULL, 0);
392 case OZ_SET_INTERFACE_RSP: {
393 struct oz_set_interface_rsp *body =
394 (struct oz_set_interface_rsp *)usb_hdr;
395 oz_hcd_control_cnf(usb_ctx->hport,
396 body->req_id, body->rcode, NULL, 0);
399 case OZ_VENDOR_CLASS_RSP: {
400 struct oz_vendor_class_rsp *body =
401 (struct oz_vendor_class_rsp *)usb_hdr;
402 oz_hcd_control_cnf(usb_ctx->hport, body->req_id,
403 body->rcode, body->data, elt->length-
404 sizeof(struct oz_vendor_class_rsp)+1);
407 case OZ_USB_ENDPOINT_DATA:
408 oz_usb_handle_ep_data(usb_ctx, usb_hdr, elt->length);
414 /*------------------------------------------------------------------------------
415 * Context: softirq, process
417 void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len)
419 struct oz_usb_ctx *usb_ctx;
420 spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]);
421 usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1];
424 spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
426 return; /* Context has gone so nothing to do. */
427 if (!usb_ctx->stopped) {
428 oz_trace("Farewell indicated ep = 0x%x\n", ep_num);
429 oz_hcd_data_ind(usb_ctx->hport, ep_num, data, len);