1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <common.h>
30#include <command.h>
31#include <asm/processor.h>
32#include <linux/compiler.h>
33#include <linux/ctype.h>
34#include <asm/byteorder.h>
35#include <asm/unaligned.h>
36#include <compiler.h>
37
38#include <usb.h>
39#ifdef CONFIG_4xx
40#include <asm/4xx_pci.h>
41#endif
42
43#define USB_BUFSIZ 512
44
45static struct usb_device usb_dev[USB_MAX_DEVICE];
46static int dev_index;
47static int asynch_allowed;
48
49char usb_started;
50
51#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
52#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
53#endif
54
55
56
57
58int usb_init(void)
59{
60 void *ctrl;
61 struct usb_device *dev;
62 int i, start_index = 0;
63
64 dev_index = 0;
65 asynch_allowed = 1;
66 usb_hub_reset();
67
68
69 for (i = 0; i < USB_MAX_DEVICE; i++) {
70 memset(&usb_dev[i], 0, sizeof(struct usb_device));
71 usb_dev[i].devnum = -1;
72 }
73
74
75 for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
76
77 printf("USB%d: ", i);
78 if (usb_lowlevel_init(i, USB_INIT_HOST, &ctrl)) {
79 puts("lowlevel init failed\n");
80 continue;
81 }
82
83
84
85
86 start_index = dev_index;
87 printf("scanning bus %d for devices... ", i);
88 dev = usb_alloc_new_device(ctrl);
89
90
91
92
93 if (dev)
94 usb_new_device(dev);
95
96 if (start_index == dev_index)
97 puts("No USB Device found\n");
98 else
99 printf("%d USB Device(s) found\n",
100 dev_index - start_index);
101
102 usb_started = 1;
103 }
104
105 debug("scan end\n");
106
107 if (!usb_started) {
108 puts("USB error: all controllers failed lowlevel init\n");
109 return -1;
110 }
111
112 return 0;
113}
114
115
116
117
118int usb_stop(void)
119{
120 int i;
121
122 if (usb_started) {
123 asynch_allowed = 1;
124 usb_started = 0;
125 usb_hub_reset();
126
127 for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
128 if (usb_lowlevel_stop(i))
129 printf("failed to stop USB controller %d\n", i);
130 }
131 }
132
133 return 0;
134}
135
136
137
138
139
140
141int usb_disable_asynch(int disable)
142{
143 int old_value = asynch_allowed;
144
145 asynch_allowed = !disable;
146 return old_value;
147}
148
149
150
151
152
153
154
155
156
157
158int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
159 void *buffer, int transfer_len, int interval)
160{
161 return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
162}
163
164
165
166
167
168
169
170
171
172
173int usb_control_msg(struct usb_device *dev, unsigned int pipe,
174 unsigned char request, unsigned char requesttype,
175 unsigned short value, unsigned short index,
176 void *data, unsigned short size, int timeout)
177{
178 ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1);
179
180 if ((timeout == 0) && (!asynch_allowed)) {
181
182 return -1;
183 }
184
185
186 setup_packet->requesttype = requesttype;
187 setup_packet->request = request;
188 setup_packet->value = cpu_to_le16(value);
189 setup_packet->index = cpu_to_le16(index);
190 setup_packet->length = cpu_to_le16(size);
191 debug("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
192 "value 0x%X index 0x%X length 0x%X\n",
193 request, requesttype, value, index, size);
194 dev->status = USB_ST_NOT_PROC;
195
196 if (submit_control_msg(dev, pipe, data, size, setup_packet) < 0)
197 return -1;
198 if (timeout == 0)
199 return (int)size;
200
201
202
203
204
205
206 while (timeout--) {
207 if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
208 break;
209 mdelay(1);
210 }
211 if (dev->status)
212 return -1;
213
214 return dev->act_len;
215
216}
217
218
219
220
221
222
223int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
224 void *data, int len, int *actual_length, int timeout)
225{
226 if (len < 0)
227 return -1;
228 dev->status = USB_ST_NOT_PROC;
229 if (submit_bulk_msg(dev, pipe, data, len) < 0)
230 return -1;
231 while (timeout--) {
232 if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
233 break;
234 mdelay(1);
235 }
236 *actual_length = dev->act_len;
237 if (dev->status == 0)
238 return 0;
239 else
240 return -1;
241}
242
243
244
245
246
247
248
249
250
251
252int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
253{
254
255 if ((pipe & USB_DIR_IN) == 0)
256 return dev->epmaxpacketout[((pipe>>15) & 0xf)];
257 else
258 return dev->epmaxpacketin[((pipe>>15) & 0xf)];
259}
260
261
262
263
264
265
266
267
268
269
270
271
272static void noinline
273usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx)
274{
275 int b;
276 struct usb_endpoint_descriptor *ep;
277 u16 ep_wMaxPacketSize;
278
279 ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx];
280
281 b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
282 ep_wMaxPacketSize = get_unaligned(&ep->wMaxPacketSize);
283
284 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
285 USB_ENDPOINT_XFER_CONTROL) {
286
287 dev->epmaxpacketout[b] = ep_wMaxPacketSize;
288 dev->epmaxpacketin[b] = ep_wMaxPacketSize;
289 debug("##Control EP epmaxpacketout/in[%d] = %d\n",
290 b, dev->epmaxpacketin[b]);
291 } else {
292 if ((ep->bEndpointAddress & 0x80) == 0) {
293
294 if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) {
295 dev->epmaxpacketout[b] = ep_wMaxPacketSize;
296 debug("##EP epmaxpacketout[%d] = %d\n",
297 b, dev->epmaxpacketout[b]);
298 }
299 } else {
300
301 if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) {
302 dev->epmaxpacketin[b] = ep_wMaxPacketSize;
303 debug("##EP epmaxpacketin[%d] = %d\n",
304 b, dev->epmaxpacketin[b]);
305 }
306 }
307 }
308}
309
310
311
312
313static int usb_set_maxpacket(struct usb_device *dev)
314{
315 int i, ii;
316
317 for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
318 for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
319 usb_set_maxpacket_ep(dev, i, ii);
320
321 return 0;
322}
323
324
325
326
327
328
329static int usb_parse_config(struct usb_device *dev,
330 unsigned char *buffer, int cfgno)
331{
332 struct usb_descriptor_header *head;
333 int index, ifno, epno, curr_if_num;
334 u16 ep_wMaxPacketSize;
335 struct usb_interface *if_desc = NULL;
336
337 ifno = -1;
338 epno = -1;
339 curr_if_num = -1;
340
341 dev->configno = cfgno;
342 head = (struct usb_descriptor_header *) &buffer[0];
343 if (head->bDescriptorType != USB_DT_CONFIG) {
344 printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
345 head->bDescriptorType);
346 return -1;
347 }
348 if (head->bLength != USB_DT_CONFIG_SIZE) {
349 printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength);
350 return -1;
351 }
352 memcpy(&dev->config, head, USB_DT_CONFIG_SIZE);
353 dev->config.no_of_if = 0;
354
355 index = dev->config.desc.bLength;
356
357
358 head = (struct usb_descriptor_header *) &buffer[index];
359 while (index + 1 < dev->config.desc.wTotalLength && head->bLength) {
360 switch (head->bDescriptorType) {
361 case USB_DT_INTERFACE:
362 if (head->bLength != USB_DT_INTERFACE_SIZE) {
363 printf("ERROR: Invalid USB IF length (%d)\n",
364 head->bLength);
365 break;
366 }
367 if (index + USB_DT_INTERFACE_SIZE >
368 dev->config.desc.wTotalLength) {
369 puts("USB IF descriptor overflowed buffer!\n");
370 break;
371 }
372 if (((struct usb_interface_descriptor *) \
373 head)->bInterfaceNumber != curr_if_num) {
374
375 ifno = dev->config.no_of_if;
376 if (ifno >= USB_MAXINTERFACES) {
377 puts("Too many USB interfaces!\n");
378
379 return 1;
380 }
381 if_desc = &dev->config.if_desc[ifno];
382 dev->config.no_of_if++;
383 memcpy(if_desc, head,
384 USB_DT_INTERFACE_SIZE);
385 if_desc->no_of_ep = 0;
386 if_desc->num_altsetting = 1;
387 curr_if_num =
388 if_desc->desc.bInterfaceNumber;
389 } else {
390
391 if (ifno >= 0) {
392 if_desc = &dev->config.if_desc[ifno];
393 if_desc->num_altsetting++;
394 }
395 }
396 break;
397 case USB_DT_ENDPOINT:
398 if (head->bLength != USB_DT_ENDPOINT_SIZE) {
399 printf("ERROR: Invalid USB EP length (%d)\n",
400 head->bLength);
401 break;
402 }
403 if (index + USB_DT_ENDPOINT_SIZE >
404 dev->config.desc.wTotalLength) {
405 puts("USB EP descriptor overflowed buffer!\n");
406 break;
407 }
408 if (ifno < 0) {
409 puts("Endpoint descriptor out of order!\n");
410 break;
411 }
412 epno = dev->config.if_desc[ifno].no_of_ep;
413 if_desc = &dev->config.if_desc[ifno];
414 if (epno > USB_MAXENDPOINTS) {
415 printf("Interface %d has too many endpoints!\n",
416 if_desc->desc.bInterfaceNumber);
417 return 1;
418 }
419
420 if_desc->no_of_ep++;
421 memcpy(&if_desc->ep_desc[epno], head,
422 USB_DT_ENDPOINT_SIZE);
423 ep_wMaxPacketSize = get_unaligned(&dev->config.\
424 if_desc[ifno].\
425 ep_desc[epno].\
426 wMaxPacketSize);
427 put_unaligned(le16_to_cpu(ep_wMaxPacketSize),
428 &dev->config.\
429 if_desc[ifno].\
430 ep_desc[epno].\
431 wMaxPacketSize);
432 debug("if %d, ep %d\n", ifno, epno);
433 break;
434 case USB_DT_SS_ENDPOINT_COMP:
435 if (head->bLength != USB_DT_SS_EP_COMP_SIZE) {
436 printf("ERROR: Invalid USB EPC length (%d)\n",
437 head->bLength);
438 break;
439 }
440 if (index + USB_DT_SS_EP_COMP_SIZE >
441 dev->config.desc.wTotalLength) {
442 puts("USB EPC descriptor overflowed buffer!\n");
443 break;
444 }
445 if (ifno < 0 || epno < 0) {
446 puts("EPC descriptor out of order!\n");
447 break;
448 }
449 if_desc = &dev->config.if_desc[ifno];
450 memcpy(&if_desc->ss_ep_comp_desc[epno], head,
451 USB_DT_SS_EP_COMP_SIZE);
452 break;
453 default:
454 if (head->bLength == 0)
455 return 1;
456
457 debug("unknown Description Type : %x\n",
458 head->bDescriptorType);
459
460#ifdef DEBUG
461 {
462 unsigned char *ch = (unsigned char *)head;
463 int i;
464
465 for (i = 0; i < head->bLength; i++)
466 debug("%02X ", *ch++);
467 debug("\n\n\n");
468 }
469#endif
470 break;
471 }
472 index += head->bLength;
473 head = (struct usb_descriptor_header *)&buffer[index];
474 }
475 return 1;
476}
477
478
479
480
481
482
483int usb_clear_halt(struct usb_device *dev, int pipe)
484{
485 int result;
486 int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
487
488 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
489 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
490 endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
491
492
493 if (result < 0)
494 return result;
495
496
497
498
499
500
501 usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
502
503
504 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
505 return 0;
506}
507
508
509
510
511
512static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
513 unsigned char index, void *buf, int size)
514{
515 int res;
516 res = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
517 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
518 (type << 8) + index, 0,
519 buf, size, USB_CNTL_TIMEOUT);
520 return res;
521}
522
523
524
525
526int usb_get_configuration_no(struct usb_device *dev,
527 unsigned char *buffer, int cfgno)
528{
529 int result;
530 unsigned int length;
531 struct usb_config_descriptor *config;
532
533 config = (struct usb_config_descriptor *)&buffer[0];
534 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
535 if (result < 9) {
536 if (result < 0)
537 printf("unable to get descriptor, error %lX\n",
538 dev->status);
539 else
540 printf("config descriptor too short " \
541 "(expected %i, got %i)\n", 9, result);
542 return -1;
543 }
544 length = le16_to_cpu(config->wTotalLength);
545
546 if (length > USB_BUFSIZ) {
547 printf("%s: failed to get descriptor - too long: %d\n",
548 __func__, length);
549 return -1;
550 }
551
552 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length);
553 debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result, length);
554 config->wTotalLength = length;
555
556 return result;
557}
558
559
560
561
562
563static int usb_set_address(struct usb_device *dev)
564{
565 int res;
566
567 debug("set address %d\n", dev->devnum);
568 res = usb_control_msg(dev, usb_snddefctrl(dev),
569 USB_REQ_SET_ADDRESS, 0,
570 (dev->devnum), 0,
571 NULL, 0, USB_CNTL_TIMEOUT);
572 return res;
573}
574
575
576
577
578int usb_set_interface(struct usb_device *dev, int interface, int alternate)
579{
580 struct usb_interface *if_face = NULL;
581 int ret, i;
582
583 for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
584 if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
585 if_face = &dev->config.if_desc[i];
586 break;
587 }
588 }
589 if (!if_face) {
590 printf("selecting invalid interface %d", interface);
591 return -1;
592 }
593
594
595
596
597
598
599
600 if (if_face->num_altsetting == 1)
601 return 0;
602
603 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
604 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
605 alternate, interface, NULL, 0,
606 USB_CNTL_TIMEOUT * 5);
607 if (ret < 0)
608 return ret;
609
610 return 0;
611}
612
613
614
615
616static int usb_set_configuration(struct usb_device *dev, int configuration)
617{
618 int res;
619 debug("set configuration %d\n", configuration);
620
621 res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
622 USB_REQ_SET_CONFIGURATION, 0,
623 configuration, 0,
624 NULL, 0, USB_CNTL_TIMEOUT);
625 if (res == 0) {
626 dev->toggle[0] = 0;
627 dev->toggle[1] = 0;
628 return 0;
629 } else
630 return -1;
631}
632
633
634
635
636int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
637{
638 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
639 USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
640 protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
641}
642
643
644
645
646int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
647{
648 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
649 USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
650 (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
651}
652
653
654
655
656int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
657 unsigned char id, void *buf, int size)
658{
659 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
660 USB_REQ_GET_REPORT,
661 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
662 (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
663}
664
665
666
667
668int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
669 unsigned char type, unsigned char id, void *buf, int size)
670{
671 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
672 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
673 (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
674}
675
676
677
678
679static int usb_get_string(struct usb_device *dev, unsigned short langid,
680 unsigned char index, void *buf, int size)
681{
682 int i;
683 int result;
684
685 for (i = 0; i < 3; ++i) {
686
687 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
688 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
689 (USB_DT_STRING << 8) + index, langid, buf, size,
690 USB_CNTL_TIMEOUT);
691
692 if (result > 0)
693 break;
694 }
695
696 return result;
697}
698
699
700static void usb_try_string_workarounds(unsigned char *buf, int *length)
701{
702 int newlength, oldlength = *length;
703
704 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
705 if (!isprint(buf[newlength]) || buf[newlength + 1])
706 break;
707
708 if (newlength > 2) {
709 buf[0] = newlength;
710 *length = newlength;
711 }
712}
713
714
715static int usb_string_sub(struct usb_device *dev, unsigned int langid,
716 unsigned int index, unsigned char *buf)
717{
718 int rc;
719
720
721
722 rc = usb_get_string(dev, langid, index, buf, 255);
723
724
725
726 if (rc < 2) {
727 rc = usb_get_string(dev, langid, index, buf, 2);
728 if (rc == 2)
729 rc = usb_get_string(dev, langid, index, buf, buf[0]);
730 }
731
732 if (rc >= 2) {
733 if (!buf[0] && !buf[1])
734 usb_try_string_workarounds(buf, &rc);
735
736
737 if (buf[0] < rc)
738 rc = buf[0];
739
740 rc = rc - (rc & 1);
741 }
742
743 if (rc < 2)
744 rc = -1;
745
746 return rc;
747}
748
749
750
751
752
753
754
755int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
756{
757 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
758 unsigned char *tbuf;
759 int err;
760 unsigned int u, idx;
761
762 if (size <= 0 || !buf || !index)
763 return -1;
764 buf[0] = 0;
765 tbuf = &mybuf[0];
766
767
768 if (!dev->have_langid) {
769 err = usb_string_sub(dev, 0, 0, tbuf);
770 if (err < 0) {
771 debug("error getting string descriptor 0 " \
772 "(error=%lx)\n", dev->status);
773 return -1;
774 } else if (tbuf[0] < 4) {
775 debug("string descriptor 0 too short\n");
776 return -1;
777 } else {
778 dev->have_langid = -1;
779 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
780
781 debug("USB device number %d default " \
782 "language ID 0x%x\n",
783 dev->devnum, dev->string_langid);
784 }
785 }
786
787 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
788 if (err < 0)
789 return err;
790
791 size--;
792 for (idx = 0, u = 2; u < err; u += 2) {
793 if (idx >= size)
794 break;
795 if (tbuf[u+1])
796 buf[idx++] = '?';
797 else
798 buf[idx++] = tbuf[u];
799 }
800 buf[idx] = 0;
801 err = idx;
802 return err;
803}
804
805
806
807
808
809
810
811
812
813
814
815struct usb_device *usb_get_dev_index(int index)
816{
817 if (usb_dev[index].devnum == -1)
818 return NULL;
819 else
820 return &usb_dev[index];
821}
822
823
824
825
826struct usb_device *usb_alloc_new_device(void *controller)
827{
828 int i;
829 debug("New Device %d\n", dev_index);
830 if (dev_index == USB_MAX_DEVICE) {
831 printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE);
832 return NULL;
833 }
834
835 usb_dev[dev_index].devnum = dev_index + 1;
836 usb_dev[dev_index].maxchild = 0;
837 for (i = 0; i < USB_MAXCHILDREN; i++)
838 usb_dev[dev_index].children[i] = NULL;
839 usb_dev[dev_index].parent = NULL;
840 usb_dev[dev_index].controller = controller;
841 dev_index++;
842 return &usb_dev[dev_index - 1];
843}
844
845
846
847
848
849
850void usb_free_device(void)
851{
852 dev_index--;
853 debug("Freeing device node: %d\n", dev_index);
854 memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
855 usb_dev[dev_index].devnum = -1;
856}
857
858
859
860
861
862
863
864__weak int usb_alloc_device(struct usb_device *udev)
865{
866 return 0;
867}
868
869
870
871
872
873
874
875int usb_new_device(struct usb_device *dev)
876{
877 int addr, err;
878 int tmp;
879 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
880
881
882
883
884
885
886
887 if (usb_alloc_device(dev)) {
888 printf("Cannot allocate device context to get SLOT_ID\n");
889 return -1;
890 }
891
892
893 addr = dev->devnum;
894 dev->devnum = 0;
895
896#ifdef CONFIG_LEGACY_USB_INIT_SEQ
897
898
899
900
901
902 dev->descriptor.bMaxPacketSize0 = 8;
903 dev->maxpacketsize = PACKET_SIZE_8;
904 dev->epmaxpacketin[0] = 8;
905 dev->epmaxpacketout[0] = 8;
906
907 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, tmpbuf, 8);
908 if (err < 8) {
909 printf("\n USB device not responding, " \
910 "giving up (status=%lX)\n", dev->status);
911 return 1;
912 }
913 memcpy(&dev->descriptor, tmpbuf, 8);
914#else
915
916
917
918
919
920
921
922 __maybe_unused struct usb_device_descriptor *desc;
923 int port = -1;
924 struct usb_device *parent = dev->parent;
925 unsigned short portstatus;
926
927
928
929
930
931
932 desc = (struct usb_device_descriptor *)tmpbuf;
933 dev->descriptor.bMaxPacketSize0 = 64;
934
935 dev->maxpacketsize = PACKET_SIZE_64;
936 dev->epmaxpacketin[0] = 64;
937 dev->epmaxpacketout[0] = 64;
938
939
940
941
942
943
944
945#ifndef CONFIG_USB_XHCI
946 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
947 if (err < 0) {
948 debug("usb_new_device: usb_get_descriptor() failed\n");
949 return 1;
950 }
951
952 dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0;
953
954
955
956
957 dev->descriptor.bDeviceClass = desc->bDeviceClass;
958#endif
959
960 if (parent) {
961 int j;
962
963
964 for (j = 0; j < parent->maxchild; j++) {
965 if (parent->children[j] == dev) {
966 port = j;
967 break;
968 }
969 }
970 if (port < 0) {
971 printf("usb_new_device:cannot locate device's port.\n");
972 return 1;
973 }
974
975
976 err = hub_port_reset(dev->parent, port, &portstatus);
977 if (err < 0) {
978 printf("\n Couldn't reset port %i\n", port);
979 return 1;
980 }
981 }
982#endif
983
984 dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
985 dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
986 switch (dev->descriptor.bMaxPacketSize0) {
987 case 8:
988 dev->maxpacketsize = PACKET_SIZE_8;
989 break;
990 case 16:
991 dev->maxpacketsize = PACKET_SIZE_16;
992 break;
993 case 32:
994 dev->maxpacketsize = PACKET_SIZE_32;
995 break;
996 case 64:
997 dev->maxpacketsize = PACKET_SIZE_64;
998 break;
999 }
1000 dev->devnum = addr;
1001
1002 err = usb_set_address(dev);
1003
1004 if (err < 0) {
1005 printf("\n USB device not accepting new address " \
1006 "(error=%lX)\n", dev->status);
1007 return 1;
1008 }
1009
1010 mdelay(10);
1011
1012 tmp = sizeof(dev->descriptor);
1013
1014 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
1015 tmpbuf, sizeof(dev->descriptor));
1016 if (err < tmp) {
1017 if (err < 0)
1018 printf("unable to get device descriptor (error=%d)\n",
1019 err);
1020 else
1021 printf("USB device descriptor short read " \
1022 "(expected %i, got %i)\n", tmp, err);
1023 return 1;
1024 }
1025 memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
1026
1027 le16_to_cpus(&dev->descriptor.bcdUSB);
1028 le16_to_cpus(&dev->descriptor.idVendor);
1029 le16_to_cpus(&dev->descriptor.idProduct);
1030 le16_to_cpus(&dev->descriptor.bcdDevice);
1031
1032 err = usb_get_configuration_no(dev, tmpbuf, 0);
1033 if (err < 0) {
1034 printf("usb_new_device: Cannot read configuration, " \
1035 "skipping device %04x:%04x\n",
1036 dev->descriptor.idVendor, dev->descriptor.idProduct);
1037 return -1;
1038 }
1039 usb_parse_config(dev, tmpbuf, 0);
1040 usb_set_maxpacket(dev);
1041
1042 if (usb_set_configuration(dev, dev->config.desc.bConfigurationValue)) {
1043 printf("failed to set default configuration " \
1044 "len %d, status %lX\n", dev->act_len, dev->status);
1045 return -1;
1046 }
1047 debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1048 dev->descriptor.iManufacturer, dev->descriptor.iProduct,
1049 dev->descriptor.iSerialNumber);
1050 memset(dev->mf, 0, sizeof(dev->mf));
1051 memset(dev->prod, 0, sizeof(dev->prod));
1052 memset(dev->serial, 0, sizeof(dev->serial));
1053 if (dev->descriptor.iManufacturer)
1054 usb_string(dev, dev->descriptor.iManufacturer,
1055 dev->mf, sizeof(dev->mf));
1056 if (dev->descriptor.iProduct)
1057 usb_string(dev, dev->descriptor.iProduct,
1058 dev->prod, sizeof(dev->prod));
1059 if (dev->descriptor.iSerialNumber)
1060 usb_string(dev, dev->descriptor.iSerialNumber,
1061 dev->serial, sizeof(dev->serial));
1062 debug("Manufacturer %s\n", dev->mf);
1063 debug("Product %s\n", dev->prod);
1064 debug("SerialNumber %s\n", dev->serial);
1065
1066 usb_hub_probe(dev, 0);
1067 return 0;
1068}
1069
1070__weak
1071int board_usb_init(int index, enum usb_init_type init)
1072{
1073 return 0;
1074}
1075
1076