merged in all changes done to the trunk up to Aug 28
[pintos-anon] / src / devices / usb.c
1 #include "devices/usb.h"
2 #include "devices/usb_hub.h"
3 #include "threads/synch.h"
4 #include "threads/malloc.h"
5 #include "devices/timer.h"
6 #include <kernel/bitmap.h>
7 #include <string.h>
8 #include <stdio.h>
9
10 #define ADDRS_PER_HOST          127     /* number of configurable addresses */
11 #define ADDR_DEFAULT            0       /* default configuration address */
12 #define ADDR_FIRST              1       /* first configurable address */
13
14 /** sometimes used for ''value' */
15 /* used in desc_header */
16 #define SETUP_DESC_DEVICE       1
17 #define SETUP_DESC_CONFIG       2
18 #define SETUP_DESC_STRING       3
19 #define SETUP_DESC_IFACE        4
20 #define SETUP_DESC_ENDPOINT     5
21 #define SETUP_DESC_DEVQUAL      6
22 #define SETUP_DESC_SPDCONFIG    7
23 #define SETUP_DESC_IFACEPWR     8
24
25 #pragma pack(1)
26 struct desc_header
27 {
28   uint8_t length;
29   uint8_t type;
30 };
31
32 /**
33  * More details can be found in chapter 9 of the usb1.1 spec
34  */
35
36 struct device_descriptor
37 {
38   struct desc_header hdr;
39   uint16_t usb_spec;
40   uint8_t dev_class;
41   uint8_t dev_subclass;
42   uint8_t dev_proto;
43   uint8_t max_pktsz;
44
45   uint16_t vendor_id;
46   uint16_t product_id;
47   uint16_t device_id;
48   uint8_t manufacturer;
49   uint8_t product;
50   uint8_t serial;
51   uint8_t num_configs;
52 };
53
54 struct device_qualifier
55 {
56   uint16_t usb_spec;
57   uint8_t dev_class;
58   uint8_t dev_subclass;
59   uint8_t dev_proto;
60   uint8_t max_pktsz;
61   uint8_t num_configs;
62   uint8_t resv;
63 };
64
65 struct config_descriptor
66 {
67   struct desc_header hdr;
68   uint16_t total_length;
69   uint8_t num_ifaces;
70   uint8_t config_val;
71   uint8_t config_desc;
72   uint8_t attributes;
73   uint8_t max_power;
74 };
75
76 struct interface_descriptor
77 {
78   struct desc_header hdr;
79   uint8_t iface_num;
80   uint8_t alt_setting;
81   uint8_t num_endpoints;
82   uint8_t iface_class;
83   uint8_t iface_subclass;
84   uint8_t iface_proto;
85   uint8_t iface_desc;
86 };
87
88 struct endpoint_descriptor
89 {
90   struct desc_header hdr;
91   /* end point address */
92   uint8_t endpoint_num:4;
93   uint8_t resv1:3;
94   uint8_t direction:1;
95
96   /* attributes */
97   uint8_t transfer:2;
98   uint8_t synch:2;
99   uint8_t usage:2;
100   uint8_t resv2:2;
101
102   uint16_t max_pktsz;
103   uint8_t interval;
104
105 };
106
107 #pragma pack()
108
109 #define USB_CLASS_HUB           0x09
110 struct host
111 {
112   struct usb_host *dev;         /* host driver */
113   host_info info;               /* private to host */
114   struct bitmap *usb_dev_addrs; /* addrs used on device */
115   struct list host_usb_devs;    /* usb devices on host */
116   struct list_elem peers;       /* other hosts on system */
117 };
118
119 struct class
120 {
121   struct usb_class *dev;        /* class driver */
122   struct list usb_ifaces;       /* usb devices on class */
123   struct list_elem peers;
124 };
125
126 static struct list usb_dev_list;        /* list of all usb devices */
127 static struct list class_list;  /* list of all classes */
128 static struct list host_list;   /* list of all hosts on system */
129 static struct lock usb_sys_lock;        /* big usb lock */
130
131 #define usb_lock()              lock_acquire(&usb_sys_lock)
132 #define usb_unlock()            lock_release(&usb_sys_lock)
133
134 static void usb_scan_devices (struct host *h);
135 static struct usb_dev *usb_configure_default (struct host *h);
136 static char *usb_get_string (struct usb_dev *d, int ndx);
137 static struct class *usb_get_class_by_id (int id);
138 static void usb_setup_dev_addr (struct usb_dev *dev);
139 static void usb_config_dev (struct usb_dev *dev, int config_val);
140 static int usb_load_config (struct usb_dev *dev, int idx, void *data,
141                             int dsz);
142 static int usb_tx_all (struct usb_endpoint *eop, void *buf,
143                        int max_bytes, int bailout, bool in);
144 static void usb_attach_interfaces (struct usb_dev *dev);
145 static void usb_apply_class_to_interfaces (struct class *c);
146 static size_t wchar_to_ascii (char *dst, const char *src);
147
148 extern void uhci_init (void);
149 extern void ehci_init (void);
150
151 void
152 usb_init (void)
153 {
154   list_init (&host_list);
155   list_init (&class_list);
156   list_init (&usb_dev_list);
157   lock_init (&usb_sys_lock);
158
159   usb_hub_init ();
160
161   /* add usb hosts */
162   printf ("Initializing EHCI\n");
163   ehci_init ();
164   printf ("Initializing UHCI\n");
165   uhci_init ();
166 }
167
168 /* add host controller device to usb layer */
169 void
170 usb_register_host (struct usb_host *uh, host_info info)
171 {
172   struct host *h;
173
174   h = malloc (sizeof (struct host));
175   h->dev = uh;
176   h->info = info;
177   h->usb_dev_addrs = bitmap_create (ADDRS_PER_HOST);
178   list_init (&h->host_usb_devs);
179
180   usb_lock ();
181   list_push_back (&host_list, &h->peers);
182   usb_unlock ();
183
184   usb_scan_devices (h);
185 }
186
187 int
188 usb_register_class (struct usb_class *uc)
189 {
190   struct class *c;
191
192   usb_lock ();
193
194   /* check to make sure class is not in list */
195   if (usb_get_class_by_id (uc->class_id) != NULL)
196     {
197       usb_unlock ();
198       return -1;
199     }
200
201
202   /* add class to class list */
203   c = malloc (sizeof (struct class));
204   c->dev = uc;
205   list_init (&c->usb_ifaces);
206   list_push_back (&class_list, &c->peers);
207
208   usb_apply_class_to_interfaces (c);
209
210   usb_unlock ();
211
212   return 0;
213 }
214
215 static void
216 usb_scan_devices (struct host *h)
217 {
218   /* scan until there all devices using default pipe are configured */
219
220   printf ("USB: scanning devices...\n");
221   while (1)
222     {
223       struct usb_dev *dev;
224
225       dev = usb_configure_default (h);
226       if (dev == NULL)
227         break;
228
229       if (dev->ignore_device == false)
230         printf ("USB Device %d: %s (%s)\n",
231                 dev->addr, dev->product, dev->manufacturer);
232
233       list_push_back (&h->host_usb_devs, &dev->host_peers);
234       list_push_back (&usb_dev_list, &dev->sys_peers);
235       usb_attach_interfaces (dev);
236     }
237 }
238
239 static int
240 send_status (struct host *h, host_eop_info cfg_eop) 
241 {
242   int sz;
243   int err;
244
245   h->dev->set_toggle (cfg_eop, 1);
246   sz = 0;
247   err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_OUT, NULL, 0, 0, &sz, true);
248   if (err)
249     printf ("error %d in status transaction\n", err);
250   return err;
251 }
252
253 static struct usb_dev *
254 usb_configure_default (struct host *h)
255 {
256   struct usb_dev *dev;
257   struct usb_setup_pkt sp;
258   char data[256];
259   struct device_descriptor *dd;
260   host_dev_info hi;
261   host_eop_info cfg_eop;
262   bool ignore_device = false;
263   int err, sz; 
264   unsigned txed;
265   int config_val;
266
267   hi = h->dev->create_dev_channel (h->info, ADDR_DEFAULT, USB_VERSION_1_1);
268   cfg_eop = h->dev->create_eop (hi, 0, 64);
269
270   /* Get first 8 bytes of device descriptor. */
271   sp.recipient = USB_SETUP_RECIP_DEV;
272   sp.type = USB_SETUP_TYPE_STD;
273   sp.direction = 1;
274   sp.request = REQ_STD_GET_DESC;
275   sp.value = SETUP_DESC_DEVICE << 8;
276   sp.index = 0;
277   sp.length = 8;
278
279   err =
280     h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
281                     true);
282   if (err != USB_HOST_ERR_NONE)
283     goto error;
284
285   dd = (void *) &data;
286   memset (dd, 0, sizeof (data));
287   txed = 0;
288   err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data, 0, 8, &sz, true);
289   if (err != USB_HOST_ERR_NONE)
290     goto error;
291   
292   err = send_status (h, cfg_eop);
293   if (err != USB_HOST_ERR_NONE)
294     return NULL;
295
296   if (dd->usb_spec == USB_VERSION_1_0)
297     {
298       /* USB 1.0 devices have strict schedule requirements not yet supported */
299       printf ("USB 1.0 device detected - skipping\n");
300       goto error;
301     }
302
303   /* Now we know the max packet size. */
304   if (dd->max_pktsz != 8 && dd->max_pktsz != 16
305       && dd->max_pktsz != 32 && dd->max_pktsz != 64)
306     goto error;
307
308   h->dev->remove_eop (cfg_eop);
309   cfg_eop = h->dev->create_eop (hi, 0, dd->max_pktsz);
310
311   /* Get the whole descriptor. */
312   sp.length = sizeof *dd;
313   err =
314     h->dev->tx_pkt (cfg_eop, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL,
315                     true);
316   if (err != USB_HOST_ERR_NONE)
317     goto error;
318
319   txed = 0;
320   while (txed < sizeof *dd)
321     {
322       err = h->dev->tx_pkt (cfg_eop, USB_TOKEN_IN, data + txed, 0,
323                             sizeof *dd - txed, &sz, true);
324       if (err)
325         goto error;
326       txed += sz;
327     }
328   
329   err = send_status (h, cfg_eop);
330   if (err != USB_HOST_ERR_NONE)
331     return NULL;
332
333   if (dd->num_configs == 0)
334     ignore_device = true;
335
336   /* device exists - create device structure */
337   dev = malloc (sizeof (struct usb_dev));
338   memset (dev, 0, sizeof (struct usb_dev));
339   list_init (&dev->interfaces);
340
341   dev->ignore_device = ignore_device;
342   dev->h_dev = hi;
343   dev->h_cfg_eop = cfg_eop;
344
345   dev->cfg_eop.h_eop = cfg_eop;
346
347   dev->usb_version = dd->usb_spec;
348   dev->default_iface.class_id = dd->dev_class;
349   dev->default_iface.subclass_id = dd->dev_subclass;
350   dev->default_iface.iface_num = 0;
351   dev->default_iface.proto = dd->dev_proto;
352   dev->default_iface.class = NULL;
353   dev->default_iface.c_info = NULL;
354   dev->default_iface.dev = dev;
355
356   dev->cfg_eop.iface = &dev->default_iface;
357   dev->cfg_eop.max_pkt = dd->max_pktsz;
358   dev->cfg_eop.eop = 0;
359
360   dev->host = h;
361
362   dev->vendor_id = dd->vendor_id;
363   dev->product_id = dd->product_id;
364   dev->device_id = dd->device_id;
365
366   if (ignore_device == false)
367     {
368       dev->product = usb_get_string (dev, dd->product);
369       dev->manufacturer = usb_get_string (dev, dd->manufacturer);
370     }
371
372   config_val = -123;
373   /* read in configuration data if there are configurations available */
374   /* (which there should be...) */
375   if (dd->num_configs > 0 && ignore_device == false)
376     {
377       config_val = usb_load_config (dev, 0, data, sizeof (data));
378       if (config_val < 0)
379         {
380           printf
381             ("USB: Invalid configuration value %d on '%s (%s)' (%x,%x,%x)\n",
382              config_val, dev->product, dev->manufacturer, dev->vendor_id,
383              dev->product_id, dev->device_id);
384         }
385     }
386
387   usb_setup_dev_addr (dev);
388
389   usb_config_dev (dev, (config_val < 0) ? 1 : config_val);
390
391   return dev;
392
393  error:
394   h->dev->remove_eop (cfg_eop);
395   h->dev->remove_dev_channel (hi);
396   return NULL;
397 }
398
399 /**
400  * Load in data for 'idx' configuration into device structure
401  * XXX support multiple configurations
402  */
403 static int
404 usb_load_config (struct usb_dev *dev, int idx, void *data, int dsz)
405 {
406   struct usb_setup_pkt sp;
407   struct config_descriptor *cd;
408   struct host *h;
409   host_eop_info cfg;
410   void *ptr;
411   int config_val, err, sz;
412   int i;
413
414   h = dev->host;
415   cfg = dev->h_cfg_eop;
416
417   sp.recipient = USB_SETUP_RECIP_DEV;
418   sp.type = USB_SETUP_TYPE_STD;
419   sp.direction = 1;
420   sp.request = REQ_STD_GET_DESC;
421   sp.value = SETUP_DESC_CONFIG << 8 | idx;
422   sp.index = 0;
423   sp.length = dsz;
424   cd = data;
425
426   err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), &sz, true);
427   if (err != USB_HOST_ERR_NONE)
428     {
429       printf ("USB: Could not setup GET descriptor\n");
430       return -err;
431     }
432
433   sz = usb_tx_all (&dev->cfg_eop, cd, dsz,
434                    sizeof (struct config_descriptor), true);
435   if (sz < (int) sizeof (struct config_descriptor))
436     {
437       printf ("USB: Did not rx GET descriptor (%d bytes, expected %d)\n", sz,
438               sizeof (struct config_descriptor));
439       return -err;
440     }
441
442   if (sz == 0 || cd->hdr.type != SETUP_DESC_CONFIG)
443     {
444       printf ("USB: Invalid descriptor\n");
445       return -1;
446     }
447
448   if (sz < cd->total_length)
449     sz += usb_tx_all (&dev->cfg_eop, data+sz, dsz, cd->total_length - sz, true);
450
451   send_status (h, cfg);
452
453   dev->pwr = cd->max_power;
454
455   /* interface information comes right after config data */
456   /* scan interfaces */
457   ptr = data + sizeof (struct config_descriptor);
458
459   for (i = 0; i < cd->num_ifaces; i++)
460     {
461       struct interface_descriptor *iface;
462       struct usb_iface *ui;
463       int j;
464
465       iface = ptr;
466       if (iface->hdr.type != SETUP_DESC_IFACE)
467         {
468           hex_dump (0, iface, 64, false);
469           PANIC ("Expected %d, found %d\n", SETUP_DESC_IFACE,
470                  iface->hdr.type);
471         }
472
473       ui = malloc (sizeof (struct usb_iface));
474       ui->class_id = iface->iface_class;
475       ui->subclass_id = iface->iface_subclass;
476       ui->iface_num = iface->iface_num;
477       ui->proto = iface->iface_proto;
478       ui->class = NULL;
479       ui->c_info = NULL;
480       ui->dev = dev;
481       list_init (&ui->endpoints);
482
483       /* endpoint data comes after interfaces */
484       /* scan endpoints */
485       ptr += sizeof (struct interface_descriptor);
486       for (j = 0; j < iface->num_endpoints;
487            j++, ptr += sizeof (struct endpoint_descriptor))
488         {
489           struct usb_endpoint *ue;
490           struct endpoint_descriptor *ed;
491
492           ed = ptr;
493
494           ue = malloc (sizeof (struct usb_endpoint));
495           ue->eop = ed->endpoint_num;
496           ue->direction = ed->direction;
497           ue->attr = ed->transfer;
498           ue->max_pkt = ed->max_pktsz;
499           ue->interval = ed->interval;
500           ue->iface = ui;
501           ue->h_eop = h->dev->create_eop (dev->h_dev, ue->eop, ue->max_pkt);
502
503           list_push_back (&ui->endpoints, &ue->peers);
504         }
505
506       list_push_back (&dev->interfaces, &ui->peers);
507     }
508
509   config_val = cd->config_val;
510
511   return config_val;
512 }
513
514 /**
515  * Set USB device configuration to desired configuration value
516  */
517 static void
518 usb_config_dev (struct usb_dev *dev, int config_val)
519 {
520   struct usb_setup_pkt sp;
521   struct host *h;
522   host_eop_info cfg;
523   int err;
524
525   h = dev->host;
526   cfg = dev->h_cfg_eop;
527
528   sp.recipient = USB_SETUP_RECIP_DEV;
529   sp.type = USB_SETUP_TYPE_STD;
530   sp.direction = 0;
531   sp.request = REQ_STD_SET_CONFIG;
532   sp.value = config_val;
533   sp.index = 0;
534   sp.length = 0;
535   err = h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
536   if (err != USB_HOST_ERR_NONE)
537     PANIC ("USB: Config setup packet did not tx\n");
538
539   err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
540   if (err != USB_HOST_ERR_NONE)
541     PANIC ("USB: Could not configure device!\n");
542 }
543
544 /**
545  * Set a device address to something other than the default pipe 
546  */
547 static void
548 usb_setup_dev_addr (struct usb_dev *dev)
549 {
550   struct usb_setup_pkt sp;
551   struct host *h;
552   host_eop_info cfg;
553   int err;
554
555   ASSERT (dev->addr == 0);
556
557   h = dev->host;
558   cfg = dev->h_cfg_eop;
559
560   dev->addr = bitmap_scan_and_flip (h->usb_dev_addrs, 1, 1, false);
561
562   sp.recipient = USB_SETUP_RECIP_DEV;
563   sp.type = USB_SETUP_TYPE_STD;
564   sp.direction = 0;
565   sp.request = REQ_STD_SET_ADDRESS;
566   sp.value = dev->addr;
567   sp.index = 0;
568   sp.length = 0;
569   err =
570     h->dev->tx_pkt (cfg, USB_TOKEN_SETUP, &sp, 0, sizeof (sp), NULL, true);
571   if (err != USB_HOST_ERR_NONE)
572     {
573       PANIC ("USB: WHOOPS!!!!!!!\n");
574     }
575   err = h->dev->tx_pkt (cfg, USB_TOKEN_IN, NULL, 0, 0, NULL, true);
576   if (err != USB_HOST_ERR_NONE)
577     {
578       PANIC ("USB: Error on setting device address (err = %d)\n", err);
579     }
580
581   h->dev->modify_dev_channel (dev->h_dev, dev->addr, dev->usb_version);
582 }
583
584 #define MAX_USB_STR     128
585 /* read string by string descriptor index from usb device */
586 static char *
587 usb_get_string (struct usb_dev *udev, int ndx)
588 {
589   struct usb_setup_pkt sp;
590   char str[MAX_USB_STR];
591   char *ret;
592   int sz;
593
594   sp.recipient = USB_SETUP_RECIP_DEV;
595   sp.type = USB_SETUP_TYPE_STD;
596   sp.direction = 1;
597   sp.request = REQ_STD_GET_DESC;
598   sp.value = (SETUP_DESC_STRING << 8) | ndx;
599   sp.index = 0;
600   sp.length = MAX_USB_STR;
601
602   if (udev->host->dev->tx_pkt (udev->h_cfg_eop, USB_TOKEN_SETUP,
603                                &sp, 0, sizeof (sp), NULL, true) 
604                            != USB_HOST_ERR_NONE) 
605     return NULL;
606
607   sz = usb_tx_all (&udev->cfg_eop, &str, MAX_USB_STR, 2, true);
608   sz +=
609     usb_tx_all (&udev->cfg_eop, str + sz, (uint8_t) (str[0]) - sz, 0, true);
610
611   /* string failed to tx? */
612   if (sz == 0)
613     return NULL;
614
615   send_status (udev->host, udev->h_cfg_eop);
616
617   /* some devices don't respect the string descriptor length value (str[0]) 
618    * and just send any old value they want, so we can't use it */
619
620   str[(sz < (MAX_USB_STR - 1)) ? (sz) : (MAX_USB_STR - 1)] = '\0';
621
622   /* usb uses wchars for strings, convert to ASCII */
623   wchar_to_ascii (str, str + 2);
624   ret = malloc (strlen (str) + 1);
625   strlcpy (ret, str, MAX_USB_STR);
626   return ret;
627 }
628
629 static struct class *
630 usb_get_class_by_id (int id)
631 {
632   struct list_elem *li;
633
634   li = list_begin (&class_list);
635   while (li != list_end (&class_list))
636     {
637       struct class *c;
638       c = list_entry (li, struct class, peers);
639       if (c->dev->class_id == id)
640         return c;
641       li = list_next (li);
642     }
643
644   return NULL;
645 }
646
647 /**
648  * Asssociate interfaces on a device with their respective device classes
649  */
650 static void
651 usb_attach_interfaces (struct usb_dev *dev)
652 {
653
654   struct list_elem *li;
655   li = list_begin (&dev->interfaces);
656   while (li != list_end (&dev->interfaces))
657     {
658       struct class *cl;
659       struct usb_iface *ui;
660
661       ui = list_entry (li, struct usb_iface, peers);
662       li = list_next (li);
663       cl = usb_get_class_by_id (ui->class_id);
664
665       /* no matching class? try next interface */
666       if (cl == NULL)
667         continue;
668
669       ui->c_info = cl->dev->attached (ui);
670       /* did class driver initialize it OK? */
671       if (ui->c_info != NULL)
672         {
673           /* yes, add to list of usable interfaces */
674           list_push_back (&cl->usb_ifaces, &ui->class_peers);
675           ui->class = cl;
676         }
677     }
678 }
679
680 /**
681  * Scan all interfaces for interfaces that match class's class id
682  * Attach interfaces that match
683  */
684 static void
685 usb_apply_class_to_interfaces (struct class *c)
686 {
687   struct list_elem *li;
688
689   li = list_begin (&usb_dev_list);
690   while (li != list_end (&usb_dev_list))
691     {
692       struct usb_dev *ud;
693       struct list_elem *lii;
694
695       ud = list_entry (li, struct usb_dev, sys_peers);
696
697       lii = list_begin (&ud->interfaces);
698       while (lii != list_end (&ud->interfaces))
699         {
700           struct usb_iface *ui;
701
702           ui = list_entry (lii, struct usb_iface, peers);
703
704           lii = list_next (lii);
705           if (ui->class_id != c->dev->class_id)
706             continue;
707
708           ui->c_info = c->dev->attached (ui);
709           if (ui->c_info == NULL)
710             continue;
711
712           list_push_back (&c->usb_ifaces, &ui->class_peers);
713           ui->class = c;
714         }
715
716       li = list_next (li);
717     }
718 }
719
720 int
721 usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx)
722 {
723   struct host *h;
724   int err;
725   int token;
726
727   ASSERT (eop != NULL);
728   h = eop->iface->dev->host;
729
730   if (eop->direction == 0)
731     token = USB_TOKEN_OUT;
732   else
733     token = USB_TOKEN_IN;
734
735   err = h->dev->tx_pkt (eop->h_eop, token, buf, sz, sz, tx, true);
736
737   return err;
738 }
739
740 int
741 usb_dev_setup (struct usb_endpoint *eop, bool in,
742                struct usb_setup_pkt *s, void *buf, int sz)
743 {
744   struct host *h;
745   int err;
746
747   ASSERT (eop != NULL);
748   h = eop->iface->dev->host;
749
750   err = h->dev->tx_pkt (eop->h_eop, USB_TOKEN_SETUP, s,
751                         0, sizeof (struct usb_setup_pkt), NULL, true);
752   if (err != USB_HOST_ERR_NONE)
753     {
754       printf ("usb_dev_setup: failed\n");
755       return 0;
756     }
757
758   err = h->dev->tx_pkt (eop->h_eop, (in) ? USB_TOKEN_IN : USB_TOKEN_OUT,
759                         buf, 0, sz, &sz, true);
760
761   return sz;
762 }
763
764 /** convert a wchar string to ascii in place */
765 static size_t
766 wchar_to_ascii (char *dst, const char *src)
767 {
768   size_t sz = 0;
769   for (sz = 0; src[sz * 2] != '\0'; sz++)
770     {
771       dst[sz] = src[sz * 2];
772     }
773   dst[sz] = '\0';
774   return sz;
775 }
776
777 /* this is used for variable sized transfers where a normal bulk transfer would 
778    probably fail, since it expects some minimum size - we just want to 
779    read/write as much to the pipe as we can
780  */
781 static int
782 usb_tx_all (struct usb_endpoint *eop, void *buf,
783             int max_bytes, int bailout, bool in)
784 {
785   int txed;
786   int token;
787   int prev_sz = 0;
788   struct host *h;
789
790   if (max_bytes <= 0)
791     return 0;
792
793   if (bailout == 0)
794     bailout = 512;
795
796   txed = 0;
797   token = (in) ? USB_TOKEN_IN : USB_TOKEN_OUT;
798   h = eop->iface->dev->host;
799   while (txed < max_bytes && txed < bailout)
800     {
801       int sz, err;
802       sz = 0;
803       err = h->dev->tx_pkt (eop->h_eop, token,
804                             buf + txed, 0, max_bytes - txed, &sz, true);
805       if (prev_sz == 0)
806         prev_sz = sz;
807       txed += sz;
808       /* this should probably be using short packet detection */
809       if (err != USB_HOST_ERR_NONE || sz != prev_sz || sz == 0)
810         {
811           return txed;
812         }
813     }
814   return txed;
815 }