USB: ftdi_sio: add Basic Micro ATOM Nano USB2Serial PID
[pandora-kernel.git] / drivers / usb / gadget / f_eem.c
1 /*
2  * f_eem.c -- USB CDC Ethernet (EEM) link function driver
3  *
4  * Copyright (C) 2003-2005,2008 David Brownell
5  * Copyright (C) 2008 Nokia Corporation
6  * Copyright (C) 2009 EF Johnson Technologies
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/etherdevice.h>
17 #include <linux/crc32.h>
18 #include <linux/slab.h>
19
20 #include "u_ether.h"
21
22 #define EEM_HLEN 2
23
24 /*
25  * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
26  * Ethernet link.
27  */
28
29 struct f_eem {
30         struct gether                   port;
31         u8                              ctrl_id;
32 };
33
34 static inline struct f_eem *func_to_eem(struct usb_function *f)
35 {
36         return container_of(f, struct f_eem, port.func);
37 }
38
39 /*-------------------------------------------------------------------------*/
40
41 /* interface descriptor: */
42
43 static struct usb_interface_descriptor eem_intf __initdata = {
44         .bLength =              sizeof eem_intf,
45         .bDescriptorType =      USB_DT_INTERFACE,
46
47         /* .bInterfaceNumber = DYNAMIC */
48         .bNumEndpoints =        2,
49         .bInterfaceClass =      USB_CLASS_COMM,
50         .bInterfaceSubClass =   USB_CDC_SUBCLASS_EEM,
51         .bInterfaceProtocol =   USB_CDC_PROTO_EEM,
52         /* .iInterface = DYNAMIC */
53 };
54
55 /* full speed support: */
56
57 static struct usb_endpoint_descriptor eem_fs_in_desc __initdata = {
58         .bLength =              USB_DT_ENDPOINT_SIZE,
59         .bDescriptorType =      USB_DT_ENDPOINT,
60
61         .bEndpointAddress =     USB_DIR_IN,
62         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
63 };
64
65 static struct usb_endpoint_descriptor eem_fs_out_desc __initdata = {
66         .bLength =              USB_DT_ENDPOINT_SIZE,
67         .bDescriptorType =      USB_DT_ENDPOINT,
68
69         .bEndpointAddress =     USB_DIR_OUT,
70         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
71 };
72
73 static struct usb_descriptor_header *eem_fs_function[] __initdata = {
74         /* CDC EEM control descriptors */
75         (struct usb_descriptor_header *) &eem_intf,
76         (struct usb_descriptor_header *) &eem_fs_in_desc,
77         (struct usb_descriptor_header *) &eem_fs_out_desc,
78         NULL,
79 };
80
81 /* high speed support: */
82
83 static struct usb_endpoint_descriptor eem_hs_in_desc __initdata = {
84         .bLength =              USB_DT_ENDPOINT_SIZE,
85         .bDescriptorType =      USB_DT_ENDPOINT,
86
87         .bEndpointAddress =     USB_DIR_IN,
88         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
89         .wMaxPacketSize =       cpu_to_le16(512),
90 };
91
92 static struct usb_endpoint_descriptor eem_hs_out_desc __initdata = {
93         .bLength =              USB_DT_ENDPOINT_SIZE,
94         .bDescriptorType =      USB_DT_ENDPOINT,
95
96         .bEndpointAddress =     USB_DIR_OUT,
97         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
98         .wMaxPacketSize =       cpu_to_le16(512),
99 };
100
101 static struct usb_descriptor_header *eem_hs_function[] __initdata = {
102         /* CDC EEM control descriptors */
103         (struct usb_descriptor_header *) &eem_intf,
104         (struct usb_descriptor_header *) &eem_hs_in_desc,
105         (struct usb_descriptor_header *) &eem_hs_out_desc,
106         NULL,
107 };
108
109 /* super speed support: */
110
111 static struct usb_endpoint_descriptor eem_ss_in_desc __initdata = {
112         .bLength =              USB_DT_ENDPOINT_SIZE,
113         .bDescriptorType =      USB_DT_ENDPOINT,
114
115         .bEndpointAddress =     USB_DIR_IN,
116         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
117         .wMaxPacketSize =       cpu_to_le16(1024),
118 };
119
120 static struct usb_endpoint_descriptor eem_ss_out_desc __initdata = {
121         .bLength =              USB_DT_ENDPOINT_SIZE,
122         .bDescriptorType =      USB_DT_ENDPOINT,
123
124         .bEndpointAddress =     USB_DIR_OUT,
125         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
126         .wMaxPacketSize =       cpu_to_le16(1024),
127 };
128
129 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc __initdata = {
130         .bLength =              sizeof eem_ss_bulk_comp_desc,
131         .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
132
133         /* the following 2 values can be tweaked if necessary */
134         /* .bMaxBurst =         0, */
135         /* .bmAttributes =      0, */
136 };
137
138 static struct usb_descriptor_header *eem_ss_function[] __initdata = {
139         /* CDC EEM control descriptors */
140         (struct usb_descriptor_header *) &eem_intf,
141         (struct usb_descriptor_header *) &eem_ss_in_desc,
142         (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
143         (struct usb_descriptor_header *) &eem_ss_out_desc,
144         (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
145         NULL,
146 };
147
148 /* string descriptors: */
149
150 static struct usb_string eem_string_defs[] = {
151         [0].s = "CDC Ethernet Emulation Model (EEM)",
152         {  } /* end of list */
153 };
154
155 static struct usb_gadget_strings eem_string_table = {
156         .language =             0x0409, /* en-us */
157         .strings =              eem_string_defs,
158 };
159
160 static struct usb_gadget_strings *eem_strings[] = {
161         &eem_string_table,
162         NULL,
163 };
164
165 /*-------------------------------------------------------------------------*/
166
167 static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
168 {
169         struct usb_composite_dev *cdev = f->config->cdev;
170         int                     value = -EOPNOTSUPP;
171         u16                     w_index = le16_to_cpu(ctrl->wIndex);
172         u16                     w_value = le16_to_cpu(ctrl->wValue);
173         u16                     w_length = le16_to_cpu(ctrl->wLength);
174
175         DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
176                 ctrl->bRequestType, ctrl->bRequest,
177                 w_value, w_index, w_length);
178
179         /* device either stalls (value < 0) or reports success */
180         return value;
181 }
182
183
184 static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
185 {
186         struct f_eem            *eem = func_to_eem(f);
187         struct usb_composite_dev *cdev = f->config->cdev;
188         struct net_device       *net;
189
190         /* we know alt == 0, so this is an activation or a reset */
191         if (alt != 0)
192                 goto fail;
193
194         if (intf == eem->ctrl_id) {
195
196                 if (eem->port.in_ep->driver_data) {
197                         DBG(cdev, "reset eem\n");
198                         gether_disconnect(&eem->port);
199                 }
200
201                 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
202                         DBG(cdev, "init eem\n");
203                         if (config_ep_by_speed(cdev->gadget, f,
204                                                eem->port.in_ep) ||
205                             config_ep_by_speed(cdev->gadget, f,
206                                                eem->port.out_ep)) {
207                                 eem->port.in_ep->desc = NULL;
208                                 eem->port.out_ep->desc = NULL;
209                                 goto fail;
210                         }
211                 }
212
213                 /* zlps should not occur because zero-length EEM packets
214                  * will be inserted in those cases where they would occur
215                  */
216                 eem->port.is_zlp_ok = 1;
217                 eem->port.cdc_filter = DEFAULT_FILTER;
218                 DBG(cdev, "activate eem\n");
219                 net = gether_connect(&eem->port);
220                 if (IS_ERR(net))
221                         return PTR_ERR(net);
222         } else
223                 goto fail;
224
225         return 0;
226 fail:
227         return -EINVAL;
228 }
229
230 static void eem_disable(struct usb_function *f)
231 {
232         struct f_eem            *eem = func_to_eem(f);
233         struct usb_composite_dev *cdev = f->config->cdev;
234
235         DBG(cdev, "eem deactivated\n");
236
237         if (eem->port.in_ep->driver_data)
238                 gether_disconnect(&eem->port);
239 }
240
241 /*-------------------------------------------------------------------------*/
242
243 /* EEM function driver setup/binding */
244
245 static int __init
246 eem_bind(struct usb_configuration *c, struct usb_function *f)
247 {
248         struct usb_composite_dev *cdev = c->cdev;
249         struct f_eem            *eem = func_to_eem(f);
250         int                     status;
251         struct usb_ep           *ep;
252
253         /* allocate instance-specific interface IDs */
254         status = usb_interface_id(c, f);
255         if (status < 0)
256                 goto fail;
257         eem->ctrl_id = status;
258         eem_intf.bInterfaceNumber = status;
259
260         status = -ENODEV;
261
262         /* allocate instance-specific endpoints */
263         ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
264         if (!ep)
265                 goto fail;
266         eem->port.in_ep = ep;
267         ep->driver_data = cdev; /* claim */
268
269         ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
270         if (!ep)
271                 goto fail;
272         eem->port.out_ep = ep;
273         ep->driver_data = cdev; /* claim */
274
275         status = -ENOMEM;
276
277         /* copy descriptors, and track endpoint copies */
278         f->descriptors = usb_copy_descriptors(eem_fs_function);
279         if (!f->descriptors)
280                 goto fail;
281
282         /* support all relevant hardware speeds... we expect that when
283          * hardware is dual speed, all bulk-capable endpoints work at
284          * both speeds
285          */
286         if (gadget_is_dualspeed(c->cdev->gadget)) {
287                 eem_hs_in_desc.bEndpointAddress =
288                                 eem_fs_in_desc.bEndpointAddress;
289                 eem_hs_out_desc.bEndpointAddress =
290                                 eem_fs_out_desc.bEndpointAddress;
291
292                 /* copy descriptors, and track endpoint copies */
293                 f->hs_descriptors = usb_copy_descriptors(eem_hs_function);
294                 if (!f->hs_descriptors)
295                         goto fail;
296         }
297
298         if (gadget_is_superspeed(c->cdev->gadget)) {
299                 eem_ss_in_desc.bEndpointAddress =
300                                 eem_fs_in_desc.bEndpointAddress;
301                 eem_ss_out_desc.bEndpointAddress =
302                                 eem_fs_out_desc.bEndpointAddress;
303
304                 /* copy descriptors, and track endpoint copies */
305                 f->ss_descriptors = usb_copy_descriptors(eem_ss_function);
306                 if (!f->ss_descriptors)
307                         goto fail;
308         }
309
310         DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
311                         gadget_is_superspeed(c->cdev->gadget) ? "super" :
312                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
313                         eem->port.in_ep->name, eem->port.out_ep->name);
314         return 0;
315
316 fail:
317         if (f->descriptors)
318                 usb_free_descriptors(f->descriptors);
319         if (f->hs_descriptors)
320                 usb_free_descriptors(f->hs_descriptors);
321
322         if (eem->port.out_ep)
323                 eem->port.out_ep->driver_data = NULL;
324         if (eem->port.in_ep)
325                 eem->port.in_ep->driver_data = NULL;
326
327         ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
328
329         return status;
330 }
331
332 static void
333 eem_unbind(struct usb_configuration *c, struct usb_function *f)
334 {
335         struct f_eem    *eem = func_to_eem(f);
336
337         DBG(c->cdev, "eem unbind\n");
338
339         if (gadget_is_superspeed(c->cdev->gadget))
340                 usb_free_descriptors(f->ss_descriptors);
341         if (gadget_is_dualspeed(c->cdev->gadget))
342                 usb_free_descriptors(f->hs_descriptors);
343         usb_free_descriptors(f->descriptors);
344         kfree(eem);
345 }
346
347 static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
348 {
349         struct sk_buff *skb = (struct sk_buff *)req->context;
350
351         dev_kfree_skb_any(skb);
352 }
353
354 /*
355  * Add the EEM header and ethernet checksum.
356  * We currently do not attempt to put multiple ethernet frames
357  * into a single USB transfer
358  */
359 static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
360 {
361         struct sk_buff  *skb2 = NULL;
362         struct usb_ep   *in = port->in_ep;
363         int             padlen = 0;
364         u16             len = skb->len;
365
366         if (!skb_cloned(skb)) {
367                 int headroom = skb_headroom(skb);
368                 int tailroom = skb_tailroom(skb);
369
370                 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
371                  * stick two bytes of zero-length EEM packet on the end.
372                  */
373                 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
374                         padlen += 2;
375
376                 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
377                                 (headroom >= EEM_HLEN))
378                         goto done;
379         }
380
381         skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
382         dev_kfree_skb_any(skb);
383         skb = skb2;
384         if (!skb)
385                 return skb;
386
387 done:
388         /* use the "no CRC" option */
389         put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
390
391         /* EEM packet header format:
392          * b0..13:      length of ethernet frame
393          * b14:         bmCRC (0 == sentinel CRC)
394          * b15:         bmType (0 == data)
395          */
396         len = skb->len;
397         put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
398
399         /* add a zero-length EEM packet, if needed */
400         if (padlen)
401                 put_unaligned_le16(0, skb_put(skb, 2));
402
403         return skb;
404 }
405
406 /*
407  * Remove the EEM header.  Note that there can be many EEM packets in a single
408  * USB transfer, so we need to break them out and handle them independently.
409  */
410 static int eem_unwrap(struct gether *port,
411                         struct sk_buff *skb,
412                         struct sk_buff_head *list)
413 {
414         struct usb_composite_dev        *cdev = port->func.config->cdev;
415         int                             status = 0;
416
417         do {
418                 struct sk_buff  *skb2;
419                 u16             header;
420                 u16             len = 0;
421
422                 if (skb->len < EEM_HLEN) {
423                         status = -EINVAL;
424                         DBG(cdev, "invalid EEM header\n");
425                         goto error;
426                 }
427
428                 /* remove the EEM header */
429                 header = get_unaligned_le16(skb->data);
430                 skb_pull(skb, EEM_HLEN);
431
432                 /* EEM packet header format:
433                  * b0..14:      EEM type dependent (data or command)
434                  * b15:         bmType (0 == data, 1 == command)
435                  */
436                 if (header & BIT(15)) {
437                         struct usb_request      *req = cdev->req;
438                         u16                     bmEEMCmd;
439
440                         /* EEM command packet format:
441                          * b0..10:      bmEEMCmdParam
442                          * b11..13:     bmEEMCmd
443                          * b14:         reserved (must be zero)
444                          * b15:         bmType (1 == command)
445                          */
446                         if (header & BIT(14))
447                                 continue;
448
449                         bmEEMCmd = (header >> 11) & 0x7;
450                         switch (bmEEMCmd) {
451                         case 0: /* echo */
452                                 len = header & 0x7FF;
453                                 if (skb->len < len) {
454                                         status = -EOVERFLOW;
455                                         goto error;
456                                 }
457
458                                 skb2 = skb_clone(skb, GFP_ATOMIC);
459                                 if (unlikely(!skb2)) {
460                                         DBG(cdev, "EEM echo response error\n");
461                                         goto next;
462                                 }
463                                 skb_trim(skb2, len);
464                                 put_unaligned_le16(BIT(15) | BIT(11) | len,
465                                                         skb_push(skb2, 2));
466                                 skb_copy_bits(skb2, 0, req->buf, skb2->len);
467                                 req->length = skb2->len;
468                                 req->complete = eem_cmd_complete;
469                                 req->zero = 1;
470                                 req->context = skb2;
471                                 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
472                                         DBG(cdev, "echo response queue fail\n");
473                                 break;
474
475                         case 1:  /* echo response */
476                         case 2:  /* suspend hint */
477                         case 3:  /* response hint */
478                         case 4:  /* response complete hint */
479                         case 5:  /* tickle */
480                         default: /* reserved */
481                                 continue;
482                         }
483                 } else {
484                         u32             crc, crc2;
485                         struct sk_buff  *skb3;
486
487                         /* check for zero-length EEM packet */
488                         if (header == 0)
489                                 continue;
490
491                         /* EEM data packet format:
492                          * b0..13:      length of ethernet frame
493                          * b14:         bmCRC (0 == sentinel, 1 == calculated)
494                          * b15:         bmType (0 == data)
495                          */
496                         len = header & 0x3FFF;
497                         if ((skb->len < len)
498                                         || (len < (ETH_HLEN + ETH_FCS_LEN))) {
499                                 status = -EINVAL;
500                                 goto error;
501                         }
502
503                         /* validate CRC */
504                         if (header & BIT(14)) {
505                                 crc = get_unaligned_le32(skb->data + len
506                                                         - ETH_FCS_LEN);
507                                 crc2 = ~crc32_le(~0,
508                                                 skb->data, len - ETH_FCS_LEN);
509                         } else {
510                                 crc = get_unaligned_be32(skb->data + len
511                                                         - ETH_FCS_LEN);
512                                 crc2 = 0xdeadbeef;
513                         }
514                         if (crc != crc2) {
515                                 DBG(cdev, "invalid EEM CRC\n");
516                                 goto next;
517                         }
518
519                         skb2 = skb_clone(skb, GFP_ATOMIC);
520                         if (unlikely(!skb2)) {
521                                 DBG(cdev, "unable to unframe EEM packet\n");
522                                 continue;
523                         }
524                         skb_trim(skb2, len - ETH_FCS_LEN);
525
526                         skb3 = skb_copy_expand(skb2,
527                                                 NET_IP_ALIGN,
528                                                 0,
529                                                 GFP_ATOMIC);
530                         if (unlikely(!skb3)) {
531                                 DBG(cdev, "unable to realign EEM packet\n");
532                                 dev_kfree_skb_any(skb2);
533                                 continue;
534                         }
535                         dev_kfree_skb_any(skb2);
536                         skb_queue_tail(list, skb3);
537                 }
538 next:
539                 skb_pull(skb, len);
540         } while (skb->len);
541
542 error:
543         dev_kfree_skb_any(skb);
544         return status;
545 }
546
547 /**
548  * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
549  * @c: the configuration to support the network link
550  * Context: single threaded during gadget setup
551  *
552  * Returns zero on success, else negative errno.
553  *
554  * Caller must have called @gether_setup().  Caller is also responsible
555  * for calling @gether_cleanup() before module unload.
556  */
557 int __init eem_bind_config(struct usb_configuration *c)
558 {
559         struct f_eem    *eem;
560         int             status;
561
562         /* maybe allocate device-global string IDs */
563         if (eem_string_defs[0].id == 0) {
564
565                 /* control interface label */
566                 status = usb_string_id(c->cdev);
567                 if (status < 0)
568                         return status;
569                 eem_string_defs[0].id = status;
570                 eem_intf.iInterface = status;
571         }
572
573         /* allocate and initialize one new instance */
574         eem = kzalloc(sizeof *eem, GFP_KERNEL);
575         if (!eem)
576                 return -ENOMEM;
577
578         eem->port.cdc_filter = DEFAULT_FILTER;
579
580         eem->port.func.name = "cdc_eem";
581         eem->port.func.strings = eem_strings;
582         /* descriptors are per-instance copies */
583         eem->port.func.bind = eem_bind;
584         eem->port.func.unbind = eem_unbind;
585         eem->port.func.set_alt = eem_set_alt;
586         eem->port.func.setup = eem_setup;
587         eem->port.func.disable = eem_disable;
588         eem->port.wrap = eem_wrap;
589         eem->port.unwrap = eem_unwrap;
590         eem->port.header_len = EEM_HLEN;
591
592         status = usb_add_function(c, &eem->port.func);
593         if (status)
594                 kfree(eem);
595         return status;
596 }
597