]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ozwpan/ozusbsvc1.c
50d21bf4f927b2b144ee2b65eec01405abd296db
[karo-tx-linux.git] / drivers / staging / ozwpan / ozusbsvc1.c
1 /* -----------------------------------------------------------------------------
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>
16 #include "ozdbg.h"
17 #include "ozprotocol.h"
18 #include "ozeltbuf.h"
19 #include "ozpd.h"
20 #include "ozproto.h"
21 #include "ozusbif.h"
22 #include "ozhcd.h"
23 #include "ozusbsvc.h"
24 /*------------------------------------------------------------------------------
25  */
26 #define MAX_ISOC_FIXED_DATA     (253-sizeof(struct oz_isoc_fixed))
27 /*------------------------------------------------------------------------------
28  * Context: softirq
29  */
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)
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);
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);
41         if (isoc == 0) {
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;
45         }
46         ret = oz_queue_elt_info(eb, isoc, strid, ei);
47         if (ret)
48                 oz_elt_info_free(eb, ei);
49         spin_unlock_bh(&eb->lock);
50         return ret;
51 }
52 /*------------------------------------------------------------------------------
53  * Context: softirq
54  */
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)
57 {
58         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
59         struct oz_pd *pd = usb_ctx->pd;
60         struct oz_elt *elt;
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);
70         if (len > 200)
71                 len = 200;
72         if (ei == NULL)
73                 return -1;
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;
84         body->index = index;
85         return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
86 }
87 /*------------------------------------------------------------------------------
88  * Context: tasklet
89  */
90 static int oz_usb_set_config_req(void *hpd, u8 req_id, u8 index)
91 {
92         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
93         struct oz_pd *pd = usb_ctx->pd;
94         struct oz_elt *elt;
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;
98         if (ei == NULL)
99                 return -1;
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;
105         body->index = index;
106         return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
107 }
108 /*------------------------------------------------------------------------------
109  * Context: tasklet
110  */
111 static int oz_usb_set_interface_req(void *hpd, u8 req_id, u8 index, u8 alt)
112 {
113         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
114         struct oz_pd *pd = usb_ctx->pd;
115         struct oz_elt *elt;
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;
119         if (ei == NULL)
120                 return -1;
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;
126         body->index = index;
127         body->alternative = alt;
128         return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
129 }
130 /*------------------------------------------------------------------------------
131  * Context: tasklet
132  */
133 static int oz_usb_set_clear_feature_req(void *hpd, u8 req_id, u8 type,
134                         u8 recipient, u8 index, __le16 feature)
135 {
136         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
137         struct oz_pd *pd = usb_ctx->pd;
138         struct oz_elt *elt;
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;
142         if (ei == NULL)
143                 return -1;
144         elt = (struct oz_elt *)ei->data;
145         elt->length = sizeof(struct oz_feature_req);
146         body = (struct oz_feature_req *)(elt+1);
147         body->type = type;
148         body->req_id = req_id;
149         body->recipient = recipient;
150         body->index = index;
151         put_unaligned(feature, &body->feature);
152         return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
153 }
154 /*------------------------------------------------------------------------------
155  * Context: tasklet
156  */
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)
159 {
160         struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd;
161         struct oz_pd *pd = usb_ctx->pd;
162         struct oz_elt *elt;
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;
166         if (ei == NULL)
167                 return -1;
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);
177         if (data_len)
178                 memcpy(body->data, data, data_len);
179         return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0);
180 }
181 /*------------------------------------------------------------------------------
182  * Context: tasklet
183  */
184 int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup,
185                         const u8 *data, int data_len)
186 {
187         unsigned wvalue = le16_to_cpu(setup->wValue);
188         unsigned windex = le16_to_cpu(setup->wIndex);
189         unsigned wlength = le16_to_cpu(setup->wLength);
190         int rc = 0;
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);
197                         break;
198                 case USB_REQ_SET_CONFIGURATION:
199                         rc = oz_usb_set_config_req(hpd, req_id, (u8)wvalue);
200                         break;
201                 case USB_REQ_SET_INTERFACE: {
202                                 u8 if_num = (u8)windex;
203                                 u8 alt = (u8)wvalue;
204                                 rc = oz_usb_set_interface_req(hpd, req_id,
205                                         if_num, alt);
206                         }
207                         break;
208                 case USB_REQ_SET_FEATURE:
209                         rc = oz_usb_set_clear_feature_req(hpd, req_id,
210                                 OZ_SET_FEATURE_REQ,
211                                 setup->bRequestType & 0xf, (u8)windex,
212                                 setup->wValue);
213                         break;
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);
219                         break;
220                 }
221         } else {
222                 rc = oz_usb_vendor_class_req(hpd, req_id, setup->bRequestType,
223                         setup->bRequest, setup->wValue, setup->wIndex,
224                         data, data_len);
225         }
226         return rc;
227 }
228 /*------------------------------------------------------------------------------
229  * Context: softirq
230  */
231 int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb)
232 {
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;
236         int i;
237         int hdr_size;
238         u8 *data;
239         struct usb_iso_packet_descriptor *desc;
240
241         if (pd->mode & OZ_F_ISOC_NO_ELTS) {
242                 for (i = 0; i < urb->number_of_packets; i++) {
243                         u8 *data;
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);
247                 }
248                 return 0;
249         }
250
251         hdr_size = sizeof(struct oz_isoc_fixed) - 1;
252         eb = &pd->elt_buff;
253         i = 0;
254         while (i < urb->number_of_packets) {
255                 struct oz_elt_info *ei = oz_elt_info_alloc(eb);
256                 struct oz_elt *elt;
257                 struct oz_isoc_fixed *body;
258                 int unit_count;
259                 int unit_size;
260                 int rem;
261                 if (ei == NULL)
262                         return -1;
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;
272                 unit_count = 0;
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);
279                                 data += unit_size;
280                                 rem -= unit_size;
281                                 unit_count++;
282                                 desc->status = 0;
283                                 desc->actual_length = desc->length;
284                                 i++;
285                         } else {
286                                 break;
287                         }
288                 }
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);
296         }
297         return 0;
298 }
299 /*------------------------------------------------------------------------------
300  * Context: softirq-serialized
301  */
302 static void oz_usb_handle_ep_data(struct oz_usb_ctx *usb_ctx,
303         struct oz_usb_hdr *usb_hdr, int len)
304 {
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)
312                                 / body->unit_size;
313                         while (n--) {
314                                 oz_hcd_data_ind(usb_ctx->hport, body->endpoint,
315                                         data, body->unit_size);
316                                 data += body->unit_size;
317                         }
318                 }
319                 break;
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;
326                         int count;
327                         int i;
328                         if (!unit_size)
329                                 break;
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);
334                                 data += unit_size;
335                         }
336                 }
337                 break;
338         }
339
340 }
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
345  */
346 void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt)
347 {
348         struct oz_usb_hdr *usb_hdr = (struct oz_usb_hdr *)(elt + 1);
349         struct oz_usb_ctx *usb_ctx;
350
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];
353         if (usb_ctx)
354                 oz_usb_get(usb_ctx);
355         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
356         if (usb_ctx == NULL)
357                 return; /* Context has gone so nothing to do. */
358         if (usb_ctx->stopped)
359                 goto done;
360         /* If sequence number is non-zero then check it is not a duplicate.
361          * Zero sequence numbers are always accepted.
362          */
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. */
366                         goto done;
367         }
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));
376                         u16 total_size =
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);
382                 }
383                 break;
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);
389                 }
390                 break;
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);
396                 }
397                 break;
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);
404                 }
405                 break;
406         case OZ_USB_ENDPOINT_DATA:
407                 oz_usb_handle_ep_data(usb_ctx, usb_hdr, elt->length);
408                 break;
409         }
410 done:
411         oz_usb_put(usb_ctx);
412 }
413 /*------------------------------------------------------------------------------
414  * Context: softirq, process
415  */
416 void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len)
417 {
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];
421         if (usb_ctx)
422                 oz_usb_get(usb_ctx);
423         spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]);
424         if (usb_ctx == NULL)
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);
429         }
430         oz_usb_put(usb_ctx);
431 }