LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
computil.c
Go to the documentation of this file.
1 /*
2  * LEGO® MINDSTORMS EV3
3  *
4  * Copyright (C) 2010-2013 The LEGO Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 /*
22  * This Composite Utility file is based on and inheritated from
23  * the original file and work done by David Brownell
24  *
25  * >> composite.c - infrastructure for Composite USB Gadgets <<
26  * >> Copyright (C) 2006-2008 David Brownell <<
27  *
28  */
29 
30 #ifndef PCASM
31 #include <linux/kallsyms.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/device.h>
35 
36 #include <linux/usb/composite.h>
37 #else
38 // Keep Eclipse happy
39 #endif
40 
41 /* big enough to hold our biggest descriptor */
42 #define USB_BUFSIZ 1024
43 
44 USB_SPEED UsbSpeed; // We will only be HIGH speed if device is connected to a HIGH
45  // speed HOST (make no sense to use FULL speed for PC connectivity.)
46  // FULL speed is necessary due to the fact, that we only have a 12
47  // Mbit HOST port on the brick (I.e. FULL speed HOST)
48 static USB_SPEED UsbSpeedDefault;
49 static USB_SPEED *pUsbSpeed = &UsbSpeedDefault;
50 /*
51  * The code in this file is utility code, used to build a gadget driver
52  * from one or more "function" drivers, one or more "configuration"
53  * objects, and a "usb_composite_driver" by gluing them together along
54  * with the relevant device-wide data.
55  */
56 
57 static struct usb_composite_driver *composite;
58 
59 /* Some systems will need runtime overrides for the product identifers
60  * published in the device descriptor, either numbers or strings or both.
61  * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
62  */
63 #ifndef PCASM
64 static ushort idVendor;
65 module_param(idVendor, ushort, 0);
66 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
67 
68 static ushort idProduct;
69 module_param(idProduct, ushort, 0);
70 MODULE_PARM_DESC(idProduct, "USB Product ID");
71 
72 static ushort bcdDevice;
73 module_param(bcdDevice, ushort, 0);
74 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
75 
76 static char *iManufacturer;
77 module_param(iManufacturer, charp, 0);
78 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
79 
80 static char *iProduct;
81 module_param(iProduct, charp, 0);
82 MODULE_PARM_DESC(iProduct, "USB Product string");
83 
84 static char *iSerialNumber;
85 module_param(iSerialNumber, charp, 0);
86 MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
87 #else
88 // Keep Eclipse happy
89 #endif
90 
91 /*HIGH SPEED hid descriptor */ /* Size = 29 decimal */
92 static char hs_hid_report_descriptor[] = {
93  0x06, 0x00, 0xFF, // Usage page (vendor defined)
94  0x09, 0x01, // Usage ID (vendor defined)
95  0xA1, 0x01, // Collection (application)
96 
97  // The Input report
98  0x15, 0x00, // Logical Minimum (0)
99  0x26, 0xFF, 0x00, // Logical Maximum (255)
100  0x75, 0x08, // Report Size (8 bits)
101 
102  0x96, 0x00, 0x04, // Report Count (1024 fields)
103  0x09, 0x01, // USAGE (vendor usage 1)
104  0x81, 0x02, // Input (Data, Variable, Absolute)
105 
106  0x96, 0x00, 0x04, // Report Count (1024 fields)
107  0x09, 0x01, // USAGE (vendor usage 1)
108  0x91, 0x02, // Output (Data, Variable, Absolute)
109 
110  0xc0 /* END_COLLECTION */
111 
112 
113 };
114 
115 /*LOW SPEED hid descriptor */ /* Size = 29 decimal */
116 static char fs_hid_report_descriptor[] = {
117  0x06, 0x00, 0xFF, // Usage page (vendor defined)
118  0x09, 0x01, // Usage ID (vendor defined)
119  0xA1, 0x01, // Collection (application)
120 
121  // The Input report
122  0x15, 0x00, // Logical Minimum (0)
123  0x26, 0xFF, 0x00, // Logical Maximum (255)
124  0x75, 0x08, // Report Size (8 bits)
125 
126  0x96, 0x40, 0x00, // Report Count (64 fields)
127  0x09, 0x01, // USAGE (vendor usage 1)
128  0x81, 0x02, // Input (Data, Variable, Absolute)
129 
130  0x96, 0x40, 0x00, // Report Count (64 fields)
131  0x09, 0x01, // USAGE (vendor usage 1)
132  0x91, 0x02, // Output (Data, Variable, Absolute)
133 
134  0xc0 /* END_COLLECTION */
135 
136 };
137 
138 
139 /*-------------------------------------------------------------------------*/
140 
156 int usb_add_function(struct usb_configuration *config,
157  struct usb_function *function)
158 {
159  int value = -EINVAL;
160 
161  DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
162  function->name, function,
163  config->label, config);
164 
165  if (!function->set_alt || !function->disable)
166  goto done;
167 
168  function->config = config;
169  list_add_tail(&function->list, &config->functions);
170 
171  /* REVISIT *require* function->bind? */
172  if (function->bind) {
173  value = function->bind(config, function);
174  if (value < 0) {
175  list_del(&function->list);
176  function->config = NULL;
177  }
178  } else
179  value = 0;
180 
181  /* We allow configurations that don't work at both speeds.
182  * If we run into a lowspeed Linux system, treat it the same
183  * as full speed ... it's the function drivers that will need
184  * to avoid bulk and ISO transfers.
185  */
186  if (!config->fullspeed && function->descriptors)
187  config->fullspeed = true;
188  if (!config->highspeed && function->hs_descriptors)
189  config->highspeed = true;
190 
191 done:
192  if (value)
193  DBG(config->cdev, "adding '%s'/%p --> %d\n",
194  function->name, function, value);
195  return value;
196 }
197 
198 /*
199  * usb_function_deactivate - prevent function and gadget enumeration
200  * @function: the function that isn't yet ready to respond
201  *
202  * Blocks response of the gadget driver to host enumeration by
203  * preventing the data line pullup from being activated. This is
204  * normally called during @bind() processing to change from the
205  * initial "ready to respond" state, or when a required resource
206  * becomes available.
207  *
208  * For example, drivers that serve as a passthrough to a userspace
209  * daemon can block enumeration unless that daemon (such as an OBEX,
210  * MTP, or print server) is ready to handle host requests.
211  *
212  * Not all systems support software control of their USB peripheral
213  * data pullups.
214  *
215  * Returns zero on success, else negative errno.
216  */
217 int usb_function_deactivate(struct usb_function *function)
218 {
219  struct usb_composite_dev *cdev = function->config->cdev;
220  unsigned long flags;
221  int status = 0;
222 
223  spin_lock_irqsave(&cdev->lock, flags);
224 
225  if (cdev->deactivations == 0)
226  status = usb_gadget_disconnect(cdev->gadget);
227  if (status == 0)
228  cdev->deactivations++;
229 
230  spin_unlock_irqrestore(&cdev->lock, flags);
231  return status;
232 }
233 
234 /*
235  * usb_function_activate - allow function and gadget enumeration
236  * @function: function on which usb_function_activate() was called
237  *
238  * Reverses effect of usb_function_deactivate(). If no more functions
239  * are delaying their activation, the gadget driver will respond to
240  * host enumeration procedures.
241  *
242  * Returns zero on success, else negative errno.
243  */
244 int usb_function_activate(struct usb_function *function)
245 {
246  struct usb_composite_dev *cdev = function->config->cdev;
247  int status = 0;
248 
249  spin_lock(&cdev->lock);
250 
251  if (WARN_ON(cdev->deactivations == 0))
252  status = -EINVAL;
253  else {
254  cdev->deactivations--;
255  if (cdev->deactivations == 0)
256  status = usb_gadget_connect(cdev->gadget);
257  }
258 
259  spin_unlock(&cdev->lock);
260  return status;
261 }
262 
287 int usb_interface_id(struct usb_configuration *config,
288  struct usb_function *function)
289 {
290  unsigned id = config->next_interface_id;
291 
292  if (id < MAX_CONFIG_INTERFACES) {
293  config->interface[id] = function;
294  config->next_interface_id = id + 1;
295  return id;
296  }
297  return -ENODEV;
298 }
299 
300 #ifndef PCASM
301 static int config_buf(struct usb_configuration *config,
302  enum usb_device_speed speed, void *buf, u8 type)
303 {
304  struct usb_config_descriptor *c = buf;
305  void *next = buf + USB_DT_CONFIG_SIZE;
306  int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
307  struct usb_function *f;
308  int status;
309 
310  /* write the config descriptor */
311  c = buf;
312  c->bLength = USB_DT_CONFIG_SIZE;
313  c->bDescriptorType = type;
314  /* wTotalLength is written later */
315  c->bNumInterfaces = config->next_interface_id; // CTRL version was set to 2
316  c->bConfigurationValue = config->bConfigurationValue;
317  c->iConfiguration = config->iConfiguration;
318  c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
319  c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
320 
321  /* There may be e.g. OTG descriptors */
322  if (config->descriptors) {
323  status = usb_descriptor_fillbuf(next, len,
324  config->descriptors);
325  if (status < 0)
326  return status;
327  len -= status;
328  next += status;
329  }
330 
331  /* add each function's descriptors */
332 
333  list_for_each_entry(f, &config->functions, list) {
334  struct usb_descriptor_header **descriptors;
335 
336  if (speed == USB_SPEED_HIGH)
337  {
338  descriptors = f->hs_descriptors;
339  }
340  else
341  {
342  descriptors = f->descriptors;
343  }
344 
345  if (!descriptors)
346  continue;
347  status = usb_descriptor_fillbuf(next, len,
348  (const struct usb_descriptor_header **) descriptors);
349  if (status < 0)
350  return status;
351  len -= status;
352  next += status;
353  }
354 
355  len = next - buf;
356  c->wTotalLength = cpu_to_le16(len);
357  return len;
358 }
359 #else
360 // Keep Eclipse happy
361 #endif
362 
363 #ifndef PCASM
364 
365 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
366 {
367  struct usb_gadget *gadget = cdev->gadget;
368  struct usb_configuration *c;
369  u8 type = w_value >> 8;
370  enum usb_device_speed speed = USB_SPEED_UNKNOWN;
371 
372  if (gadget_is_dualspeed(gadget)) {
373  int hs = 0;
374 
375  /*if (gadget->speed == USB_SPEED_HIGH)
376  hs = 1;
377  if (type == USB_DT_OTHER_SPEED_CONFIG)
378  hs = !hs;
379  if (hs)
380  speed = USB_SPEED_HIGH;
381  */
382 
383  if (gadget->speed == USB_SPEED_HIGH)
384  {
385  hs = 1;
386  UsbSpeed.Speed = HIGH_SPEED;
387  //#define DEBUG
388  #undef DEBUG
389  #ifdef DEBUG
390  printk("Speed = HIGH\n\r");
391  #endif
392  }
393  else
394  {
395  hs = 0;
396  UsbSpeed.Speed = FULL_SPEED;
397  #undef DEBUG
398  //#define DEBUG
399  #ifdef DEBUG
400  printk("Speed = LOW\n\r");
401  #endif
402  }
403 
404  if (type == USB_DT_OTHER_SPEED_CONFIG)
405  hs = !hs;
406  if (hs)
407  speed = USB_SPEED_HIGH;
408  }
409 
410  /* This is a lookup by config *INDEX* */
411  w_value &= 0xff;
412  list_for_each_entry(c, &cdev->configs, list) {
413  /* ignore configs that won't work at this speed */
414  if (speed == USB_SPEED_HIGH) {
415  if (!c->highspeed)
416  continue;
417  } else {
418  if (!c->fullspeed)
419  continue;
420  }
421  if (w_value == 0)
422  return config_buf(c, speed, cdev->req->buf, type);
423  w_value--;
424  }
425  return -EINVAL;
426 }
427 #else
428 // Keep Eclipse happy
429 #endif
430 
431 #ifndef PCASM
432 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
433 {
434  struct usb_gadget *gadget = cdev->gadget;
435  struct usb_configuration *c;
436  unsigned count = 0;
437  int hs = 0;
438 
439  if (gadget_is_dualspeed(gadget)) {
440  if (gadget->speed == USB_SPEED_HIGH)
441  hs = 1;
442  if (type == USB_DT_DEVICE_QUALIFIER)
443  hs = !hs;
444  }
445  list_for_each_entry(c, &cdev->configs, list) {
446  /* ignore configs that won't work at this speed */
447  if (hs) {
448  if (!c->highspeed)
449  continue;
450  } else {
451  if (!c->fullspeed)
452  continue;
453  }
454  count++;
455  }
456  return count;
457 }
458 #else
459 // Keep Eclipse happy
460 #endif
461 
462 static void device_qual(struct usb_composite_dev *cdev)
463 {
464  struct usb_qualifier_descriptor *qual = cdev->req->buf;
465 
466  qual->bLength = sizeof(*qual);
467  qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
468  /* POLICY: same bcdUSB and device type info at both speeds */
469  qual->bcdUSB = cdev->desc.bcdUSB;
470  qual->bDeviceClass = cdev->desc.bDeviceClass;
471  qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
472  qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
473  /* ASSUME same EP0 fifo size at both speeds */
474  qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
475  qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
476  qual->bRESERVED = 0;
477 }
478 
479 /*-------------------------------------------------------------------------*/
480 #ifndef PCASM
481 static void reset_config(struct usb_composite_dev *cdev)
482 {
483  struct usb_function *f;
484 
485  DBG(cdev, "reset config\n");
486 
487  list_for_each_entry(f, &cdev->config->functions, list) {
488  if (f->disable)
489  f->disable(f);
490 
491  bitmap_zero(f->endpoints, 32);
492  }
493  cdev->config = NULL;
494 }
495 #else
496 // Keep Eclipse happy
497 #endif
498 
499 #ifndef PCASM
500 static int set_config(struct usb_composite_dev *cdev,
501  const struct usb_ctrlrequest *ctrl, unsigned number)
502 {
503  struct usb_gadget *gadget = cdev->gadget;
504  struct usb_configuration *c = NULL;
505  int result = -EINVAL;
506  unsigned power = gadget_is_otg(gadget) ? 8 : 100;
507  int tmp;
508 
509  if (cdev->config)
510  reset_config(cdev);
511 
512  if (number) {
513  list_for_each_entry(c, &cdev->configs, list) {
514  if (c->bConfigurationValue == number) {
515  result = 0;
516  break;
517  }
518  }
519  if (result < 0)
520  goto done;
521  } else
522  result = 0;
523 
524  /*INFO(cdev, "%s speed config #%d: %s\n",
525  ({ char *speed;
526  switch (gadget->speed) {
527  case USB_SPEED_LOW: speed = "low"; break;
528  case USB_SPEED_FULL: speed = "full"; break;
529  case USB_SPEED_HIGH: speed = "high"; break;
530  default: speed = "?"; break;
531  } ; speed; }), number, c ? c->label : "unconfigured");
532 */
533 
534  if (!c)
535  goto done;
536 
537  cdev->config = c;
538 
539  /* Initialize all interfaces by setting them to altsetting zero. */
540  for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
541  struct usb_function *f = c->interface[tmp];
542  struct usb_descriptor_header **descriptors;
543 
544  if (!f)
545  break;
546 
547  /*
548  * Record which endpoints are used by the function. This is used
549  * to dispatch control requests targeted at that endpoint to the
550  * function's setup callback instead of the current
551  * configuration's setup callback.
552  */
553 
554  if (gadget->speed == USB_SPEED_HIGH)
555  descriptors = f->hs_descriptors;
556  else
557  descriptors = f->descriptors;
558 
559  for (; *descriptors; ++descriptors) {
560  struct usb_endpoint_descriptor *ep;
561  int addr;
562 
563  if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
564  continue;
565 
566  ep = (struct usb_endpoint_descriptor *)*descriptors;
567  addr = ((ep->bEndpointAddress & 0x80) >> 3)
568  | (ep->bEndpointAddress & 0x0f);
569  set_bit(addr, f->endpoints);
570  }
571 
572  result = f->set_alt(f, tmp, 0);
573 
574 
575 
576  if (result < 0) {
577  DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
578  tmp, f->name, f, result);
579 
580  reset_config(cdev);
581 
582  goto done;
583  }
584  }
585 
586  /* when we return, be sure our power usage is valid */
587  power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
588 done:
589  usb_gadget_vbus_draw(gadget, power);
590  return result;
591 }
592 
593 #else
594 // Keep Eclipse happy
595 #endif
596 
597 
613 #ifndef PCASM
614 int usb_add_config(struct usb_composite_dev *cdev,
615  struct usb_configuration *config)
616 {
617  int status = -EINVAL;
618  struct usb_configuration *c;
619 
620  DBG(cdev, "adding config #%u '%s'/%p\n",
621  config->bConfigurationValue,
622  config->label, config);
623 
624  if (!config->bConfigurationValue || !config->bind)
625  goto done;
626 
627  /* Prevent duplicate configuration identifiers */
628  list_for_each_entry(c, &cdev->configs, list) {
629  if (c->bConfigurationValue == config->bConfigurationValue) {
630  status = -EBUSY;
631  goto done;
632  }
633  }
634 
635  config->cdev = cdev;
636  list_add_tail(&config->list, &cdev->configs);
637 
638  INIT_LIST_HEAD(&config->functions);
639  config->next_interface_id = 0;
640 
641  status = config->bind(config);
642  if (status < 0) {
643  list_del(&config->list);
644  config->cdev = NULL;
645  } else {
646  unsigned i;
647 
648  DBG(cdev, "cfg %d/%p speeds:%s%s\n",
649  config->bConfigurationValue, config,
650  config->highspeed ? " high" : "",
651  config->fullspeed
652  ? (gadget_is_dualspeed(cdev->gadget)
653  ? " full"
654  : " full/low")
655  : "");
656 
657  for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
658  struct usb_function *f = config->interface[i];
659 
660  if (!f)
661  continue;
662  DBG(cdev, " interface %d = %s/%p\n",
663  i, f->name, f);
664  }
665  }
666 
667  /* set_alt(), or next config->bind(), sets up
668  * ep->driver_data as needed.
669  */
670  usb_ep_autoconfig_reset(cdev->gadget);
671 
672 done:
673  if (status)
674  DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
675  config->bConfigurationValue, status);
676  return status;
677 }
678 #else
679 // Keep Eclipse happy
680 #endif
681 
682 /*-------------------------------------------------------------------------*/
683 
684 /* We support strings in multiple languages ... string descriptor zero
685  * says which languages are supported. The typical case will be that
686  * only one language (probably English) is used, with I18N handled on
687  * the host side.
688  */
689 
690 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
691 {
692  const struct usb_gadget_strings *s;
693  u16 language;
694  __le16 *tmp;
695 
696  while (*sp) {
697  s = *sp;
698  language = cpu_to_le16(s->language);
699  for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
700  if (*tmp == language)
701  goto repeat;
702  }
703  *tmp++ = language;
704 repeat:
705  sp++;
706  }
707 }
708 
709 static int lookup_string(
710  struct usb_gadget_strings **sp,
711  void *buf,
712  u16 language,
713  int id
714 )
715 {
716  struct usb_gadget_strings *s;
717  int value;
718 
719  while (*sp) {
720  s = *sp++;
721  if (s->language != language)
722  continue;
723  value = usb_gadget_get_string(s, id, buf);
724  if (value > 0)
725  return value;
726  }
727  return -EINVAL;
728 }
729 
730 #ifndef PCASM
731 static int get_string(struct usb_composite_dev *cdev,
732  void *buf, u16 language, int id)
733 {
734  struct usb_configuration *c;
735  struct usb_function *f;
736  int len;
737 
738  /* Yes, not only is USB's I18N support probably more than most
739  * folk will ever care about ... also, it's all supported here.
740  * (Except for UTF8 support for Unicode's "Astral Planes".)
741  */
742 
743  /* 0 == report all available language codes */
744  if (id == 0) {
745  struct usb_string_descriptor *s = buf;
746  struct usb_gadget_strings **sp;
747 
748  memset(s, 0, 256);
749  s->bDescriptorType = USB_DT_STRING;
750 
751  sp = composite->strings;
752  if (sp)
753  collect_langs(sp, s->wData);
754 
755  list_for_each_entry(c, &cdev->configs, list) {
756  sp = c->strings;
757  if (sp)
758  collect_langs(sp, s->wData);
759 
760  list_for_each_entry(f, &c->functions, list) {
761  sp = f->strings;
762  if (sp)
763  collect_langs(sp, s->wData);
764  }
765  }
766 
767  for (len = 0; len <= 126 && s->wData[len]; len++)
768  continue;
769  if (!len)
770  return -EINVAL;
771 
772  s->bLength = 2 * (len + 1);
773  return s->bLength;
774  }
775 
776  /* Otherwise, look up and return a specified string. String IDs
777  * are device-scoped, so we look up each string table we're told
778  * about. These lookups are infrequent; simpler-is-better here.
779  */
780  if (composite->strings) {
781  len = lookup_string(composite->strings, buf, language, id);
782  if (len > 0)
783  return len;
784  }
785  list_for_each_entry(c, &cdev->configs, list) {
786  if (c->strings) {
787  len = lookup_string(c->strings, buf, language, id);
788  if (len > 0)
789  return len;
790  }
791  list_for_each_entry(f, &c->functions, list) {
792  if (!f->strings)
793  continue;
794  len = lookup_string(f->strings, buf, language, id);
795  if (len > 0)
796  return len;
797  }
798  }
799  return -EINVAL;
800 }
801 #else
802 // Keep Eclipse happy
803 #endif
804 
819 int usb_string_id(struct usb_composite_dev *cdev)
820 {
821  if (cdev->next_string_id < 254) {
822  /* string id 0 is reserved */
823  cdev->next_string_id++;
824  return cdev->next_string_id;
825  }
826  return -ENODEV;
827 }
828 
829 /*-------------------------------------------------------------------------*/
830 
831 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
832 {
833  if (req->status || req->actual != req->length)
834  DBG((struct usb_composite_dev *) ep->driver_data,
835  "setup complete --> %d, %d/%d\n",
836  req->status, req->actual, req->length);
837 }
838 
839 /*
840  * The setup() callback implements all the ep0 functionality that's
841  * not handled lower down, in hardware or the hardware driver(like
842  * device and endpoint feature flags, and their status). It's all
843  * housekeeping for the gadget function we're implementing. Most of
844  * the work is in config and function specific setup.
845  */
846 
847 #ifndef PCASM
848 static int
849 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
850 {
851  struct usb_composite_dev *cdev = get_gadget_data(gadget);
852  struct usb_request *req = cdev->req;
853  int value = -EOPNOTSUPP;
854  u16 w_index = le16_to_cpu(ctrl->wIndex);
855  u8 intf = w_index & 0xFF;
856  u16 w_value = le16_to_cpu(ctrl->wValue);
857  u16 w_length = le16_to_cpu(ctrl->wLength);
858  struct usb_function *f = NULL;
859  u8 endp;
860 
861  /* partial re-init of the response message; the function or the
862  * gadget might need to intercept e.g. a control-OUT completion
863  * when we delegate to it.
864  */
865  req->zero = 0;
866  req->complete = composite_setup_complete;
867  req->length = USB_BUFSIZ;
868  gadget->ep0->driver_data = cdev;
869 
870 switch (ctrl->bRequest) {
871  /* we handle all standard USB descriptors */
872  case USB_REQ_GET_DESCRIPTOR:
873 
874  if ((ctrl->bRequestType != USB_DIR_IN) && (ctrl->bRequestType != 0x81))
875  goto unknown;
876 
877  switch (w_value >> 8) {
878 
879  case USB_DT_DEVICE: //
880 
881  cdev->desc.bNumConfigurations =
882  count_configs(cdev, USB_DT_DEVICE);
883  value = min(w_length, (u16) sizeof cdev->desc);
884  memcpy(req->buf, &cdev->desc, value);
885 
886  break;
887  case USB_DT_DEVICE_QUALIFIER:
888 
889  if (!gadget_is_dualspeed(gadget))
890  break;
891  device_qual(cdev);
892  value = min_t(int, w_length,
893  sizeof(struct usb_qualifier_descriptor));
894 
895  break;
896  case USB_DT_OTHER_SPEED_CONFIG:
897 
898  if (!gadget_is_dualspeed(gadget))
899  break;
900  /* FALLTHROUGH */
901  case USB_DT_CONFIG:
902 
903  value = config_desc(cdev, w_value);
904  if (value >= 0)
905  value = min(w_length, (u16) value);
906  break;
907  case USB_DT_STRING:
908 
909  value = get_string(cdev, req->buf,
910  w_index, w_value & 0xff);
911  if (value >= 0)
912  value = min(w_length, (u16) value);
913  break;
914 
915  case 34:
916 
917  //#define DEBUG
918  #undef DEBUG
919  #ifdef DEBUG
920  printk("\nWE are in GET_REPORT_DESC !! SETUP in COMPUTIL.C - case 34");
921  #endif
922 
923  if(UsbSpeed.Speed == HIGH_SPEED)
924  {
925  memcpy(req->buf, hs_hid_report_descriptor, 29);
926 
927  #ifdef DEBUG
928  printk("\nWE are @ HIGH REPORT_DESC !!");
929  #endif
930  }
931  else
932  {
933  memcpy(req->buf, fs_hid_report_descriptor, 29);
934 
935  #ifdef DEBUG
936  printk("\nWE are @ FULL REPORT_DESC !!");
937  #endif
938  }
939  value =29;
940  break;
941  }
942  break;
943 
944  /* any number of configs can work */
945  case USB_REQ_SET_CONFIGURATION:
946 
947  if (ctrl->bRequestType != 0)
948  goto unknown;
949 
950  if (gadget_is_otg(gadget)) {
951 
952  if (gadget->a_hnp_support)
953  DBG(cdev, "HNP available\n");
954  else if (gadget->a_alt_hnp_support)
955  DBG(cdev, "HNP on another port\n");
956  else
957  VDBG(cdev, "HNP inactive\n");
958  }
959 
960  spin_lock(&cdev->lock);
961  value = set_config(cdev, ctrl, w_value);
962 
963  spin_unlock(&cdev->lock);
964  break;
965  case USB_REQ_GET_CONFIGURATION:
966 
967  if (ctrl->bRequestType != USB_DIR_IN)
968  goto unknown;
969 
970  if (cdev->config)
971  *(u8 *)req->buf = cdev->config->bConfigurationValue;
972  else
973  *(u8 *)req->buf = 0;
974  value = min(w_length, (u16) 1);
975  break;
976 
977  /* function drivers must handle get/set altsetting; if there's
978  * no get() method, we know only altsetting zero works.
979  */
980  case USB_REQ_SET_INTERFACE:
981 
982  if (ctrl->bRequestType != USB_RECIP_INTERFACE)
983  goto unknown;
984 
985  if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
986  break;
987 
988  f = cdev->config->interface[intf];
989  if (!f)
990  break;
991 
992  if (w_value && !f->set_alt)
993  break;
994 
995  value = f->set_alt(f, w_index, w_value);
996  break;
997  case USB_REQ_GET_INTERFACE:
998 
999  if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1000  goto unknown;
1001 
1002  if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1003  break;
1004 
1005  f = cdev->config->interface[intf];
1006  if (!f)
1007  break;
1008 
1009  /* lots of interfaces only need altsetting zero... */
1010  value = f->get_alt ? f->get_alt(f, w_index) : 0;
1011  if (value < 0)
1012  break;
1013 
1014  *((u8 *)req->buf) = value;
1015  value = min(w_length, (u16) 1);
1016  break;
1017  default:
1018 unknown:
1019  VDBG(cdev,
1020  "non-core control req%02x.%02x v%04x i%04x l%d\n",
1021  ctrl->bRequestType, ctrl->bRequest,
1022  w_value, w_index, w_length);
1023 
1024  /* functions always handle their interfaces and endpoints...
1025  * punt other recipients (other, WUSB, ...) to the current
1026  * configuration code.
1027  *
1028  * REVISIT it could make sense to let the composite device
1029  * take such requests too, if that's ever needed: to work
1030  * in config 0, etc.
1031  */
1032  switch (ctrl->bRequestType & USB_RECIP_MASK) {
1033 
1034  case USB_RECIP_INTERFACE:
1035 
1036  f = cdev->config->interface[intf];
1037  break;
1038 
1039  case USB_RECIP_ENDPOINT:
1040 
1041  endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1042  list_for_each_entry(f, &cdev->config->functions, list) {
1043  if (test_bit(endp, f->endpoints))
1044  break;
1045  }
1046 
1047  if (&f->list == &cdev->config->functions)
1048  f = NULL;
1049  break;
1050  }
1051 
1052  if (f && f->setup)
1053  value = f->setup(f, ctrl);
1054  else {
1055 
1056  struct usb_configuration *c;
1057 
1058  c = cdev->config;
1059  if (c && c->setup)
1060  value = c->setup(c, ctrl);
1061  }
1062 
1063  goto done;
1064  }
1065 
1066  /* respond with data transfer before status phase? */
1067  if (value >= 0) {
1068 
1069  req->length = value;
1070  req->zero = value < w_length;
1071  value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1072 
1073  if (value < 0) {
1074  DBG(cdev, "ep_queue --> %d\n", value);
1075  req->status = 0;
1076  composite_setup_complete(gadget->ep0, req);
1077  }
1078  }
1079 
1080 done:
1081 
1082  /* device either stalls (value < 0) or reports success */
1083 
1084  return value;
1085 
1086 }
1087 #else
1088 // Keep Eclipse happy
1089 #endif
1090 
1091 
1092 static void composite_disconnect(struct usb_gadget *gadget)
1093 {
1094  struct usb_composite_dev *cdev = get_gadget_data(gadget);
1095  unsigned long flags;
1096 
1097  /* REVISIT: should we have config and device level
1098  * disconnect callbacks?
1099  */
1100 
1101  spin_lock_irqsave(&cdev->lock, flags);
1102  if (cdev->config)
1103  reset_config(cdev);
1104  spin_unlock_irqrestore(&cdev->lock, flags);
1105 }
1106 
1107 /*-------------------------------------------------------------------------*/
1108 #ifndef PCASM
1109 static void /* __init_or_exit */
1110 composite_unbind(struct usb_gadget *gadget)
1111 {
1112  struct usb_composite_dev *cdev = get_gadget_data(gadget);
1113 
1114  /* composite_disconnect() must already have been called
1115  * by the underlying peripheral controller driver!
1116  * so there's no i/o concurrency that could affect the
1117  * state protected by cdev->lock.
1118  */
1119  WARN_ON(cdev->config);
1120 
1121  while (!list_empty(&cdev->configs)) {
1122  struct usb_configuration *c;
1123 
1124  c = list_first_entry(&cdev->configs,
1125  struct usb_configuration, list);
1126  while (!list_empty(&c->functions)) {
1127  struct usb_function *f;
1128 
1129  f = list_first_entry(&c->functions,
1130  struct usb_function, list);
1131  list_del(&f->list);
1132  if (f->unbind) {
1133  DBG(cdev, "unbind function '%s'/%p\n",
1134  f->name, f);
1135  f->unbind(c, f);
1136  /* may free memory for "f" */
1137  }
1138  }
1139  list_del(&c->list);
1140  if (c->unbind) {
1141  DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1142  c->unbind(c);
1143  /* may free memory for "c" */
1144  }
1145  }
1146  if (composite->unbind)
1147  composite->unbind(cdev);
1148 
1149  if (cdev->req) {
1150  kfree(cdev->req->buf);
1151  usb_ep_free_request(gadget->ep0, cdev->req);
1152  }
1153  kfree(cdev);
1154  set_gadget_data(gadget, NULL);
1155  composite = NULL;
1156 }
1157 #else
1158 // Keep Eclipse happy
1159 #endif
1160 
1161 static void
1162 string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
1163 {
1164  struct usb_string *str = tab->strings;
1165 
1166  for (str = tab->strings; str->s; str++) {
1167  if (str->id == id) {
1168  str->s = s;
1169  return;
1170  }
1171  }
1172 }
1173 
1174 static void
1175 string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
1176 {
1177  while (*tab) {
1178  string_override_one(*tab, id, s);
1179  tab++;
1180  }
1181 }
1182 
1183 static int composite_bind(struct usb_gadget *gadget)
1184 {
1185  struct usb_composite_dev *cdev;
1186  int status = -ENOMEM;
1187 
1188  cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1189  if (!cdev)
1190  return status;
1191 
1192  spin_lock_init(&cdev->lock);
1193  cdev->gadget = gadget;
1194  set_gadget_data(gadget, cdev);
1195  INIT_LIST_HEAD(&cdev->configs);
1196 
1197  /* preallocate control response and buffer */
1198  cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1199  if (!cdev->req)
1200  goto fail;
1201  cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1202  if (!cdev->req->buf)
1203  goto fail;
1204  cdev->req->complete = composite_setup_complete;
1205  gadget->ep0->driver_data = cdev;
1206 
1207  cdev->bufsiz = USB_BUFSIZ;
1208  cdev->driver = composite;
1209 
1210  usb_gadget_set_selfpowered(gadget);
1211 
1212  /* interface and string IDs start at zero via kzalloc.
1213  * we force endpoints to start unassigned; few controller
1214  * drivers will zero ep->driver_data.
1215  */
1216  usb_ep_autoconfig_reset(cdev->gadget);
1217 
1218  /* composite gadget needs to assign strings for whole device (like
1219  * serial number), register function drivers, potentially update
1220  * power state and consumption, etc
1221  */
1222  status = composite->bind(cdev);
1223  if (status < 0)
1224  goto fail;
1225 
1226  cdev->desc = *composite->dev;
1227  cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1228 
1229  /* standardized runtime overrides for device ID data */
1230  if (idVendor)
1231  cdev->desc.idVendor = cpu_to_le16(idVendor);
1232  if (idProduct)
1233  cdev->desc.idProduct = cpu_to_le16(idProduct);
1234  if (bcdDevice)
1235  cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1236 
1237  /* strings can't be assigned before bind() allocates the
1238  * releavnt identifiers
1239  */
1240  if (cdev->desc.iManufacturer && iManufacturer)
1241  string_override(composite->strings,
1242  cdev->desc.iManufacturer, iManufacturer);
1243  if (cdev->desc.iProduct && iProduct)
1244  string_override(composite->strings,
1245  cdev->desc.iProduct, iProduct);
1246  if (cdev->desc.iSerialNumber && iSerialNumber)
1247  string_override(composite->strings,
1248  cdev->desc.iSerialNumber, iSerialNumber);
1249 
1250  // INFO(cdev, "%s ready\n", composite->name);
1251  return 0;
1252 
1253 fail:
1254  composite_unbind(gadget);
1255  return status;
1256 }
1257 
1258 /*-------------------------------------------------------------------------*/
1259 
1260 #ifndef PCASM
1261 static void
1262 composite_suspend(struct usb_gadget *gadget)
1263 {
1264  struct usb_composite_dev *cdev = get_gadget_data(gadget);
1265  struct usb_function *f;
1266 
1267  /* REVISIT: should we have config level
1268  * suspend/resume callbacks?
1269  */
1270  DBG(cdev, "suspend\n");
1271  if (cdev->config) {
1272  list_for_each_entry(f, &cdev->config->functions, list) {
1273  if (f->suspend)
1274  f->suspend(f);
1275  }
1276  }
1277  if (composite->suspend)
1278  composite->suspend(cdev);
1279 }
1280 #else
1281 // Keep Eclipse happy
1282 #endif
1283 
1284 #ifndef PCASM
1285 static void
1286 composite_resume(struct usb_gadget *gadget)
1287 {
1288  struct usb_composite_dev *cdev = get_gadget_data(gadget);
1289  struct usb_function *f;
1290 
1291  /* REVISIT: should we have config level
1292  * suspend/resume callbacks?
1293  */
1294  DBG(cdev, "resume\n");
1295  if (composite->resume)
1296  composite->resume(cdev);
1297  if (cdev->config) {
1298  list_for_each_entry(f, &cdev->config->functions, list) {
1299  if (f->resume)
1300  f->resume(f);
1301  }
1302  }
1303 }
1304 #else
1305 // Keep Eclipse happy
1306 #endif
1307 
1308 /*-------------------------------------------------------------------------*/
1309 
1310 static struct usb_gadget_driver composite_driver = {
1311  .speed = USB_SPEED_HIGH,
1312 
1313  .bind = composite_bind,
1314  /* .unbind = __exit_p(composite_unbind), */
1315  .unbind = composite_unbind,
1316 
1317  .setup = composite_setup,
1318  .disconnect = composite_disconnect,
1319 
1320  .suspend = composite_suspend,
1321  .resume = composite_resume,
1322 
1323  .driver = {
1324  .owner = THIS_MODULE,
1325  },
1326 };
1327 
1343 int usb_composite_register(struct usb_composite_driver *driver)
1344 {
1345  if (!driver || !driver->dev || !driver->bind || composite)
1346  return -EINVAL;
1347 
1348  if (!driver->name)
1349  driver->name = "composite";
1350  composite_driver.function = (char *) driver->name;
1351  composite_driver.driver.name = driver->name;
1352  composite = driver;
1353 
1354  return usb_gadget_register_driver(&composite_driver);
1355 }
1356 
1364 void usb_composite_unregister(struct usb_composite_driver *driver)
1365 {
1366  if (composite != driver)
1367  return;
1368  usb_gadget_unregister_driver(&composite_driver);
1369 }
1370 
int usb_function_deactivate(struct usb_function *function)
Definition: computil.c:217
module_param(idVendor, ushort, 0)
int usb_string_id(struct usb_composite_dev *cdev)
Definition: computil.c:819
int usb_function_activate(struct usb_function *function)
Definition: computil.c:244
uint8_t u8
Definition: common.h:160
MODULE_PARM_DESC(idVendor,"USB Vendor ID")
#define USB_BUFSIZ
Definition: computil.c:42
int usb_composite_register(struct usb_composite_driver *driver)
Definition: computil.c:1343
int usb_add_function(struct usb_configuration *config, struct usb_function *function)
Definition: computil.c:156
USB_SPEED UsbSpeed
Definition: computil.c:44
uint16_t u16
Definition: common.h:159
void usb_composite_unregister(struct usb_composite_driver *driver)
Definition: computil.c:1364
int usb_interface_id(struct usb_configuration *config, struct usb_function *function)
Definition: computil.c:287
int usb_add_config(struct usb_composite_dev *cdev, struct usb_configuration *config)
Definition: computil.c:614
USB_SPEED * pUsbSpeed
Definition: c_daisy.c:102
#define NULL