finally move buildroot-ng to trunk
[openwrt.git] / target / linux / generic-2.4 / patches / 228-more_usb_fixes.patch
1 diff -ur linux.old/drivers/scsi/hosts.c linux.dev/drivers/scsi/hosts.c
2 --- linux.old/drivers/scsi/hosts.c      2003-06-13 16:51:36.000000000 +0200
3 +++ linux.dev/drivers/scsi/hosts.c      2006-07-30 12:34:30.000000000 +0200
4 @@ -107,8 +107,21 @@
5      if (shn) shn->host_registered = 0;
6      /* else {} : This should not happen, we should panic here... */
7      
8 +    /* If we are removing the last host registered, it is safe to reuse
9 +     * its host number (this avoids "holes" at boot time) (DB) 
10 +     * It is also safe to reuse those of numbers directly below which have
11 +     * been released earlier (to avoid some holes in numbering).
12 +     */
13 +    if(sh->host_no == max_scsi_hosts - 1) {
14 +       while(--max_scsi_hosts >= next_scsi_host) {
15 +           shpnt = scsi_hostlist;
16 +           while(shpnt && shpnt->host_no != max_scsi_hosts - 1)
17 +               shpnt = shpnt->next;
18 +           if(shpnt)
19 +               break;
20 +       }
21 +    }
22      next_scsi_host--;
23 -
24      kfree((char *) sh);
25  }
26  
27 diff -ur linux.old/drivers/usb/hcd.c linux.dev/drivers/usb/hcd.c
28 --- linux.old/drivers/usb/hcd.c 2004-04-14 15:05:32.000000000 +0200
29 +++ linux.dev/drivers/usb/hcd.c 2006-07-30 11:49:06.000000000 +0200
30 @@ -1105,7 +1105,8 @@
31                 break;
32         case PIPE_BULK:
33                 allowed |= USB_DISABLE_SPD | USB_QUEUE_BULK
34 -                               | USB_ZERO_PACKET | URB_NO_INTERRUPT;
35 +                               | USB_ZERO_PACKET | URB_NO_INTERRUPT
36 +                       | URB_NO_TRANSFER_DMA_MAP;
37                 break;
38         case PIPE_INTERRUPT:
39                 allowed |= USB_DISABLE_SPD;
40 @@ -1212,7 +1213,8 @@
41                                         urb->setup_packet,
42                                         sizeof (struct usb_ctrlrequest),
43                                         PCI_DMA_TODEVICE);
44 -               if (urb->transfer_buffer_length != 0)
45 +               if (urb->transfer_buffer_length != 0
46 +                       && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
47                         urb->transfer_dma = pci_map_single (
48                                         hcd->pdev,
49                                         urb->transfer_buffer,
50 diff -ur linux.old/drivers/usb/host/ehci-hcd.c linux.dev/drivers/usb/host/ehci-hcd.c
51 --- linux.old/drivers/usb/host/ehci-hcd.c       2006-07-30 11:31:57.000000000 +0200
52 +++ linux.dev/drivers/usb/host/ehci-hcd.c       2006-07-30 11:48:14.000000000 +0200
53 @@ -399,6 +399,27 @@
54                 ehci_mem_cleanup (ehci);
55                 return retval;
56         }
57 +
58 +{
59 +       int misc_reg;
60 +       u32 vendor_id;
61 +       
62 +       pci_read_config_dword (ehci->hcd.pdev, PCI_VENDOR_ID, &vendor_id);
63 +       if (vendor_id == 0x31041106) {
64 +               /* VIA 6212 */
65 +               printk(KERN_INFO "EHCI: Enabling VIA 6212 workarounds\n", misc_reg);
66 +               pci_read_config_byte(ehci->hcd.pdev, 0x49, &misc_reg);
67 +               misc_reg &= ~0x20;
68 +               pci_write_config_byte(ehci->hcd.pdev, 0x49, misc_reg);
69 +               pci_read_config_byte(ehci->hcd.pdev, 0x49, &misc_reg);
70 +
71 +               pci_read_config_byte(ehci->hcd.pdev, 0x4b, &misc_reg);
72 +               misc_reg |= 0x20;
73 +               pci_write_config_byte(ehci->hcd.pdev, 0x4b, misc_reg);
74 +               pci_read_config_byte(ehci->hcd.pdev, 0x4b, &misc_reg);
75 +       }
76 +}
77 +
78         writel (INTR_MASK, &ehci->regs->intr_enable);
79         writel (ehci->periodic_dma, &ehci->regs->frame_list);
80  
81 diff -ur linux.old/drivers/usb/host/ehci-q.c linux.dev/drivers/usb/host/ehci-q.c
82 --- linux.old/drivers/usb/host/ehci-q.c 2006-07-30 11:31:57.000000000 +0200
83 +++ linux.dev/drivers/usb/host/ehci-q.c 2006-07-30 12:10:15.000000000 +0200
84 @@ -791,6 +791,8 @@
85                         writel (cmd, &ehci->regs->command);
86                         ehci->hcd.state = USB_STATE_RUNNING;
87                         /* posted write need not be known to HC yet ... */
88 +                       
89 +                       timer_action (ehci, TIMER_IO_WATCHDOG);
90                 }
91         }
92  
93 diff -ur linux.old/drivers/usb/host/usb-uhci.c linux.dev/drivers/usb/host/usb-uhci.c
94 --- linux.old/drivers/usb/host/usb-uhci.c       2004-11-17 12:54:21.000000000 +0100
95 +++ linux.dev/drivers/usb/host/usb-uhci.c       2006-07-30 12:10:16.000000000 +0200
96 @@ -2491,7 +2491,7 @@
97                         ((urb_priv_t*)urb->hcpriv)->flags=0;                                    
98                 }
99                 
100 -               if ((urb->status != -ECONNABORTED) && (urb->status != ECONNRESET) &&
101 +               if ((urb->status != -ECONNABORTED) && (urb->status != -ECONNRESET) &&
102                             (urb->status != -ENOENT)) {
103  
104                         urb->status = -EINPROGRESS;
105 @@ -3034,6 +3034,21 @@
106         
107         pci_set_master(dev);
108  
109 +       {
110 +               u8 misc_reg;
111 +               u32 vendor_id;
112 +               
113 +               pci_read_config_dword (dev, PCI_VENDOR_ID, &vendor_id);
114 +               if (vendor_id == 0x30381106) {
115 +                       /* VIA 6212 */
116 +                       printk(KERN_INFO "UHCI: Enabling VIA 6212 workarounds\n");
117 +                       pci_read_config_byte(dev, 0x41, &misc_reg);
118 +                       misc_reg &= ~0x10;
119 +                       pci_write_config_byte(dev, 0x41, misc_reg);
120 +                       pci_read_config_byte(dev, 0x41, &misc_reg);
121 +               }
122 +       }
123 +       
124         /* Search for the IO base address.. */
125         for (i = 0; i < 6; i++) {
126  
127 diff -ur linux.old/drivers/usb/storage/transport.c linux.dev/drivers/usb/storage/transport.c
128 --- linux.old/drivers/usb/storage/transport.c   2005-04-04 03:42:19.000000000 +0200
129 +++ linux.dev/drivers/usb/storage/transport.c   2006-07-30 12:22:56.000000000 +0200
130 @@ -54,6 +54,22 @@
131  #include <linux/sched.h>
132  #include <linux/errno.h>
133  #include <linux/slab.h>
134 +#include <linux/pci.h>
135 +#include "../hcd.h"
136 +
137 +/* These definitions mirror those in pci.h, so they can be used
138 + * interchangeably with their PCI_ counterparts */
139 +enum dma_data_direction {
140 +       DMA_BIDIRECTIONAL = 0,
141 +       DMA_TO_DEVICE = 1,
142 +       DMA_FROM_DEVICE = 2,
143 +       DMA_NONE = 3,
144 +};
145 +
146 +#define dma_map_sg(d,s,n,dir) pci_map_sg(d,s,n,dir)
147 +#define dma_unmap_sg(d,s,n,dir) pci_unmap_sg(d,s,n,dir)
148 +
149 +
150  
151  /***********************************************************************
152   * Helper routines
153 @@ -554,6 +570,543 @@
154         return US_BULK_TRANSFER_SHORT;
155  }
156  
157 +/*-------------------------------------------------------------------*/
158 +/**
159 + * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
160 + * @dev: device to which the scatterlist will be mapped
161 + * @pipe: endpoint defining the mapping direction
162 + * @sg: the scatterlist to unmap
163 + * @n_hw_ents: the positive return value from usb_buffer_map_sg
164 + *
165 + * Reverses the effect of usb_buffer_map_sg().
166 + */
167 +static void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
168 +               struct scatterlist *sg, int n_hw_ents)
169 +{
170 +       struct usb_bus *bus;
171 +       struct usb_hcd *hcd;
172 +       struct pci_dev *pdev;
173 +
174 +       if (!dev
175 +                       || !(bus = dev->bus)
176 +                       || !(hcd = bus->hcpriv)
177 +                       || !(pdev = hcd->pdev)
178 +                       || !pdev->dma_mask)
179 +               return;
180 +
181 +       dma_unmap_sg (pdev, sg, n_hw_ents,
182 +                       usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
183 +}
184 +
185 +/**
186 + * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
187 + * @dev: device to which the scatterlist will be mapped
188 + * @pipe: endpoint defining the mapping direction
189 + * @sg: the scatterlist to map
190 + * @nents: the number of entries in the scatterlist
191 + *
192 + * Return value is either < 0 (indicating no buffers could be mapped), or
193 + * the number of DMA mapping array entries in the scatterlist.
194 + *
195 + * The caller is responsible for placing the resulting DMA addresses from
196 + * the scatterlist into URB transfer buffer pointers, and for setting the
197 + * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
198 + *
199 + * Top I/O rates come from queuing URBs, instead of waiting for each one
200 + * to complete before starting the next I/O.   This is particularly easy
201 + * to do with scatterlists.  Just allocate and submit one URB for each DMA
202 + * mapping entry returned, stopping on the first error or when all succeed.
203 + * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
204 + *
205 + * This call would normally be used when translating scatterlist requests,
206 + * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
207 + * may be able to coalesce mappings for improved I/O efficiency.
208 + *
209 + * Reverse the effect of this call with usb_buffer_unmap_sg().
210 + */
211 +static int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
212 +               struct scatterlist *sg, int nents)
213 +{
214 +       struct usb_bus          *bus;
215 +       struct usb_hcd *hcd;
216 +       struct pci_dev *pdev;
217 +
218 +       if (!dev
219 +                       || usb_pipecontrol (pipe)
220 +                       || !(bus = dev->bus)
221 +                       || !(hcd = bus->hcpriv)
222 +                       || !(pdev = hcd->pdev)
223 +                       || !pdev->dma_mask)
224 +               return -1;
225 +
226 +       // FIXME generic api broken like pci, can't report errors
227 +       return dma_map_sg (pdev, sg, nents,
228 +                       usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
229 +}
230 +
231 +static void sg_clean (struct usb_sg_request *io)
232 +{
233 +       struct usb_hcd *hcd = io->dev->bus->hcpriv;
234 +       struct pci_dev *pdev = hcd->pdev;
235 +
236 +       if (io->urbs) {
237 +               while (io->entries--)
238 +                       usb_free_urb (io->urbs [io->entries]);
239 +               kfree (io->urbs);
240 +               io->urbs = 0;
241 +       }
242 +       if (pdev->dma_mask != 0)
243 +               usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents);
244 +       io->dev = 0;
245 +}
246 +
247 +static void sg_complete (struct urb *urb)
248 +{
249 +       struct usb_sg_request   *io = (struct usb_sg_request *) urb->context;
250 +
251 +       spin_lock (&io->lock);
252 +
253 +       /* In 2.5 we require hcds' endpoint queues not to progress after fault
254 +        * reports, until the completion callback (this!) returns.  That lets
255 +        * device driver code (like this routine) unlink queued urbs first,
256 +        * if it needs to, since the HC won't work on them at all.  So it's
257 +        * not possible for page N+1 to overwrite page N, and so on.
258 +        *
259 +        * That's only for "hard" faults; "soft" faults (unlinks) sometimes
260 +        * complete before the HCD can get requests away from hardware,
261 +        * though never during cleanup after a hard fault.
262 +        */
263 +       if (io->status
264 +                       && (io->status != -ECONNRESET
265 +                               || urb->status != -ECONNRESET)
266 +                       && urb->actual_length) {
267 +               US_DEBUGP("Error: %s ep%d%s scatterlist error %d/%d\n",
268 +                       io->dev->devpath,
269 +                       usb_pipeendpoint (urb->pipe),
270 +                       usb_pipein (urb->pipe) ? "in" : "out",
271 +                       urb->status, io->status);
272 +               // BUG ();
273 +       }
274 +
275 +       if (urb->status && urb->status != -ECONNRESET) {
276 +               int             i, found, status;
277 +
278 +               io->status = urb->status;
279 +
280 +               /* the previous urbs, and this one, completed already.
281 +                * unlink pending urbs so they won't rx/tx bad data.
282 +                */
283 +               for (i = 0, found = 0; i < io->entries; i++) {
284 +                       if (!io->urbs [i])
285 +                               continue;
286 +                       if (found) {
287 +                               status = usb_unlink_urb (io->urbs [i]);
288 +                               if (status != -EINPROGRESS && status != -EBUSY)
289 +                                       US_DEBUGP("Error: %s, unlink --> %d\n", __FUNCTION__, status);
290 +                       } else if (urb == io->urbs [i])
291 +                               found = 1;
292 +               }
293 +       }
294 +       urb->dev = 0;
295 +
296 +       /* on the last completion, signal usb_sg_wait() */
297 +       io->bytes += urb->actual_length;
298 +       io->count--;
299 +       if (!io->count)
300 +               complete (&io->complete);
301 +
302 +       spin_unlock (&io->lock);
303 +}
304 +
305 +/**
306 + * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
307 + * @io: request block being initialized.  until usb_sg_wait() returns,
308 + *     treat this as a pointer to an opaque block of memory,
309 + * @dev: the usb device that will send or receive the data
310 + * @pipe: endpoint "pipe" used to transfer the data
311 + * @period: polling rate for interrupt endpoints, in frames or
312 + *     (for high speed endpoints) microframes; ignored for bulk
313 + * @sg: scatterlist entries
314 + * @nents: how many entries in the scatterlist
315 + * @length: how many bytes to send from the scatterlist, or zero to
316 + *     send every byte identified in the list.
317 + * @mem_flags: SLAB_* flags affecting memory allocations in this call
318 + *
319 + * Returns zero for success, else a negative errno value.  This initializes a
320 + * scatter/gather request, allocating resources such as I/O mappings and urb
321 + * memory (except maybe memory used by USB controller drivers).
322 + *
323 + * The request must be issued using usb_sg_wait(), which waits for the I/O to
324 + * complete (or to be canceled) and then cleans up all resources allocated by
325 + * usb_sg_init().
326 + *
327 + * The request may be canceled with usb_sg_cancel(), either before or after
328 + * usb_sg_wait() is called.
329 + */
330 +int usb_sg_init (
331 +       struct usb_sg_request   *io,
332 +       struct usb_device       *dev,
333 +       unsigned                pipe, 
334 +       unsigned                period,
335 +       struct scatterlist      *sg,
336 +       int                     nents,
337 +       size_t                  length,
338 +       int                     mem_flags
339 +)
340 +{
341 +       int                     i;
342 +       int                     urb_flags;
343 +       int                     dma;
344 +       struct usb_hcd *hcd;
345 +
346 +       hcd = dev->bus->hcpriv;
347 +
348 +       if (!io || !dev || !sg
349 +                       || usb_pipecontrol (pipe)
350 +                       || usb_pipeisoc (pipe)
351 +                       || nents <= 0)
352 +               return -EINVAL;
353 +
354 +       spin_lock_init (&io->lock);
355 +       io->dev = dev;
356 +       io->pipe = pipe;
357 +       io->sg = sg;
358 +       io->nents = nents;
359 +
360 +       /* not all host controllers use DMA (like the mainstream pci ones);
361 +        * they can use PIO (sl811) or be software over another transport.
362 +        */
363 +       dma = (hcd->pdev->dma_mask != 0);
364 +       if (dma)
365 +               io->entries = usb_buffer_map_sg (dev, pipe, sg, nents);
366 +       else
367 +               io->entries = nents;
368 +
369 +       /* initialize all the urbs we'll use */
370 +       if (io->entries <= 0)
371 +               return io->entries;
372 +
373 +       io->count = 0;
374 +       io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
375 +       if (!io->urbs)
376 +               goto nomem;
377 +
378 +       urb_flags = USB_ASYNC_UNLINK | URB_NO_INTERRUPT | URB_NO_TRANSFER_DMA_MAP;
379 +       if (usb_pipein (pipe))
380 +               urb_flags |= URB_SHORT_NOT_OK;
381 +
382 +       for (i = 0; i < io->entries; i++, io->count = i) {
383 +               unsigned                len;
384 +
385 +               io->urbs [i] = usb_alloc_urb (0);
386 +               if (!io->urbs [i]) {
387 +                       io->entries = i;
388 +                       goto nomem;
389 +               }
390 +
391 +               io->urbs [i]->dev = 0;
392 +               io->urbs [i]->pipe = pipe;
393 +               io->urbs [i]->interval = period;
394 +               io->urbs [i]->transfer_flags = urb_flags;
395 +
396 +               io->urbs [i]->complete = sg_complete;
397 +               io->urbs [i]->context = io;
398 +               io->urbs [i]->status = -EINPROGRESS;
399 +               io->urbs [i]->actual_length = 0;
400 +
401 +               if (dma) {
402 +                       /* hc may use _only_ transfer_dma */
403 +                       io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
404 +                       len = sg_dma_len (sg + i);
405 +               } else {
406 +                       /* hc may use _only_ transfer_buffer */
407 +                       io->urbs [i]->transfer_buffer =
408 +                               page_address (sg [i].page) + sg [i].offset;
409 +                       len = sg [i].length;
410 +               }
411 +
412 +               if (length) {
413 +                       len = min_t (unsigned, len, length);
414 +                       length -= len;
415 +                       if (length == 0)
416 +                               io->entries = i + 1;
417 +               }
418 +               io->urbs [i]->transfer_buffer_length = len;
419 +       }
420 +       io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
421 +
422 +       /* transaction state */
423 +       io->status = 0;
424 +       io->bytes = 0;
425 +       init_completion (&io->complete);
426 +       return 0;
427 +
428 +nomem:
429 +       sg_clean (io);
430 +       return -ENOMEM;
431 +}
432 +
433 +/**
434 + * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
435 + * @io: request block, initialized with usb_sg_init()
436 + *
437 + * This stops a request after it has been started by usb_sg_wait().
438 + * It can also prevents one initialized by usb_sg_init() from starting,
439 + * so that call just frees resources allocated to the request.
440 + */
441 +void usb_sg_cancel (struct usb_sg_request *io)
442 +{
443 +       unsigned long   flags;
444 +
445 +       spin_lock_irqsave (&io->lock, flags);
446 +
447 +       /* shut everything down, if it didn't already */
448 +       if (!io->status) {
449 +               int     i;
450 +
451 +               io->status = -ECONNRESET;
452 +               for (i = 0; i < io->entries; i++) {
453 +                       int     retval;
454 +
455 +                       if (!io->urbs [i]->dev)
456 +                               continue;
457 +                       retval = usb_unlink_urb (io->urbs [i]);
458 +                       if (retval != -EINPROGRESS && retval != -EBUSY)
459 +                               US_DEBUGP("WARNING: %s, unlink --> %d\n", __FUNCTION__, retval);
460 +               }
461 +       }
462 +       spin_unlock_irqrestore (&io->lock, flags);
463 +}
464 +
465 +/**
466 + * usb_sg_wait - synchronously execute scatter/gather request
467 + * @io: request block handle, as initialized with usb_sg_init().
468 + *     some fields become accessible when this call returns.
469 + * Context: !in_interrupt ()
470 + *
471 + * This function blocks until the specified I/O operation completes.  It
472 + * leverages the grouping of the related I/O requests to get good transfer
473 + * rates, by queueing the requests.  At higher speeds, such queuing can
474 + * significantly improve USB throughput.
475 + *
476 + * There are three kinds of completion for this function.
477 + * (1) success, where io->status is zero.  The number of io->bytes
478 + *     transferred is as requested.
479 + * (2) error, where io->status is a negative errno value.  The number
480 + *     of io->bytes transferred before the error is usually less
481 + *     than requested, and can be nonzero.
482 + * (3) cancelation, a type of error with status -ECONNRESET that
483 + *     is initiated by usb_sg_cancel().
484 + *
485 + * When this function returns, all memory allocated through usb_sg_init() or
486 + * this call will have been freed.  The request block parameter may still be
487 + * passed to usb_sg_cancel(), or it may be freed.  It could also be
488 + * reinitialized and then reused.
489 + *
490 + * Data Transfer Rates:
491 + *
492 + * Bulk transfers are valid for full or high speed endpoints.
493 + * The best full speed data rate is 19 packets of 64 bytes each
494 + * per frame, or 1216 bytes per millisecond.
495 + * The best high speed data rate is 13 packets of 512 bytes each
496 + * per microframe, or 52 KBytes per millisecond.
497 + *
498 + * The reason to use interrupt transfers through this API would most likely
499 + * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
500 + * could be transferred.  That capability is less useful for low or full
501 + * speed interrupt endpoints, which allow at most one packet per millisecond,
502 + * of at most 8 or 64 bytes (respectively).
503 + */
504 +void usb_sg_wait (struct usb_sg_request *io)
505 +{
506 +       int             i, entries = io->entries;
507 +
508 +       /* queue the urbs.  */
509 +       spin_lock_irq (&io->lock);
510 +       for (i = 0; i < entries && !io->status; i++) {
511 +               int     retval;
512 +
513 +               io->urbs [i]->dev = io->dev;
514 +               retval = usb_submit_urb (io->urbs [i]);
515 +
516 +               /* after we submit, let completions or cancelations fire;
517 +                * we handshake using io->status.
518 +                */
519 +               spin_unlock_irq (&io->lock);
520 +               switch (retval) {
521 +                       /* maybe we retrying will recover */
522 +               case -ENXIO:    // hc didn't queue this one
523 +               case -EAGAIN:
524 +               case -ENOMEM:
525 +                       io->urbs [i]->dev = 0;
526 +                       retval = 0;
527 +                       i--;
528 +                       yield ();
529 +                       break;
530 +
531 +                       /* no error? continue immediately.
532 +                        *
533 +                        * NOTE: to work better with UHCI (4K I/O buffer may
534 +                        * need 3K of TDs) it may be good to limit how many
535 +                        * URBs are queued at once; N milliseconds?
536 +                        */
537 +               case 0:
538 +                       cpu_relax ();
539 +                       break;
540 +
541 +                       /* fail any uncompleted urbs */
542 +               default:
543 +                       spin_lock_irq (&io->lock);
544 +                       io->count -= entries - i;
545 +                       if (io->status == -EINPROGRESS)
546 +                               io->status = retval;
547 +                       if (io->count == 0)
548 +                               complete (&io->complete);
549 +                       spin_unlock_irq (&io->lock);
550 +
551 +                       io->urbs [i]->dev = 0;
552 +                       io->urbs [i]->status = retval;
553 +                       
554 +                       US_DEBUGP("%s, submit --> %d\n", __FUNCTION__, retval);
555 +                       usb_sg_cancel (io);
556 +               }
557 +               spin_lock_irq (&io->lock);
558 +               if (retval && io->status == -ECONNRESET)
559 +                       io->status = retval;
560 +       }
561 +       spin_unlock_irq (&io->lock);
562 +
563 +       /* OK, yes, this could be packaged as non-blocking.
564 +        * So could the submit loop above ... but it's easier to
565 +        * solve neither problem than to solve both!
566 +        */
567 +       wait_for_completion (&io->complete);
568 +
569 +       sg_clean (io);
570 +}
571 +
572 +/*
573 + * Interpret the results of a URB transfer
574 + *
575 + * This function prints appropriate debugging messages, clears halts on
576 + * non-control endpoints, and translates the status to the corresponding
577 + * USB_STOR_XFER_xxx return code.
578 + */
579 +static int interpret_urb_result(struct us_data *us, unsigned int pipe,
580 +               unsigned int length, int result, unsigned int partial)
581 +{
582 +       US_DEBUGP("Status code %d; transferred %u/%u\n",
583 +                       result, partial, length);
584 +       switch (result) {
585 +
586 +       /* no error code; did we send all the data? */
587 +       case 0:
588 +               if (partial != length) {
589 +                       US_DEBUGP("-- short transfer\n");
590 +                       return USB_STOR_XFER_SHORT;
591 +               }
592 +
593 +               US_DEBUGP("-- transfer complete\n");
594 +               return USB_STOR_XFER_GOOD;
595 +
596 +       /* stalled */
597 +       case -EPIPE:
598 +               /* for control endpoints, (used by CB[I]) a stall indicates
599 +                * a failed command */
600 +               if (usb_pipecontrol(pipe)) {
601 +                       US_DEBUGP("-- stall on control pipe\n");
602 +                       return USB_STOR_XFER_STALLED;
603 +               }
604 +
605 +               /* for other sorts of endpoint, clear the stall */
606 +               US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
607 +               if (usb_stor_clear_halt(us, pipe) < 0)
608 +                       return USB_STOR_XFER_ERROR;
609 +               return USB_STOR_XFER_STALLED;
610 +
611 +       /* timeout or excessively long NAK */
612 +       case -ETIMEDOUT:
613 +               US_DEBUGP("-- timeout or NAK\n");
614 +               return USB_STOR_XFER_ERROR;
615 +
616 +       /* babble - the device tried to send more than we wanted to read */
617 +       case -EOVERFLOW:
618 +               US_DEBUGP("-- babble\n");
619 +               return USB_STOR_XFER_LONG;
620 +
621 +       /* the transfer was cancelled by abort, disconnect, or timeout */
622 +       case -ECONNRESET:
623 +               US_DEBUGP("-- transfer cancelled\n");
624 +               return USB_STOR_XFER_ERROR;
625 +
626 +       /* short scatter-gather read transfer */
627 +       case -EREMOTEIO:
628 +               US_DEBUGP("-- short read transfer\n");
629 +               return USB_STOR_XFER_SHORT;
630 +
631 +       /* abort or disconnect in progress */
632 +       case -EIO:
633 +               US_DEBUGP("-- abort or disconnect in progress\n");
634 +               return USB_STOR_XFER_ERROR;
635 +
636 +       /* the catch-all error case */
637 +       default:
638 +               US_DEBUGP("-- unknown error\n");
639 +               return USB_STOR_XFER_ERROR;
640 +       }
641 +}
642 +
643 +/*
644 + * Transfer a scatter-gather list via bulk transfer
645 + *
646 + * This function does basically the same thing as usb_stor_bulk_msg()
647 + * above, but it uses the usbcore scatter-gather library.
648 + */
649 +int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
650 +               struct scatterlist *sg, int num_sg, unsigned int length,
651 +               unsigned int *act_len)
652 +{
653 +       int result;
654 +
655 +       /* don't submit s-g requests during abort/disconnect processing */
656 +       if (us->flags & ABORTING_OR_DISCONNECTING)
657 +               return USB_STOR_XFER_ERROR;
658 +
659 +       /* initialize the scatter-gather request block */
660 +       US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
661 +                       length, num_sg);
662 +       result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
663 +                       sg, num_sg, length, SLAB_NOIO);
664 +       if (result) {
665 +               US_DEBUGP("usb_sg_init returned %d\n", result);
666 +               return USB_STOR_XFER_ERROR;
667 +       }
668 +
669 +       /* since the block has been initialized successfully, it's now
670 +        * okay to cancel it */
671 +       set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
672 +
673 +       /* did an abort/disconnect occur during the submission? */
674 +       if (us->flags & ABORTING_OR_DISCONNECTING) {
675 +
676 +               /* cancel the request, if it hasn't been cancelled already */
677 +               if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
678 +                       US_DEBUGP("-- cancelling sg request\n");
679 +                       usb_sg_cancel(&us->current_sg);
680 +               }
681 +       }
682 +
683 +       /* wait for the completion of the transfer */
684 +       usb_sg_wait(&us->current_sg);
685 +       clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
686 +
687 +       result = us->current_sg.status;
688 +       if (act_len)
689 +               *act_len = us->current_sg.bytes;
690 +       return interpret_urb_result(us, pipe, length, result,
691 +                       us->current_sg.bytes);
692 +}
693 +
694  /*
695   * Transfer an entire SCSI command's worth of data payload over the bulk
696   * pipe.
697 @@ -569,6 +1122,8 @@
698         struct scatterlist *sg;
699         unsigned int total_transferred = 0;
700         unsigned int transfer_amount;
701 +       unsigned int partial;
702 +       unsigned int pipe;
703  
704         /* calculate how much we want to transfer */
705         transfer_amount = usb_stor_transfer_length(srb);
706 @@ -585,23 +1140,34 @@
707                  * make the appropriate requests for each, until done
708                  */
709                 sg = (struct scatterlist *) srb->request_buffer;
710 -               for (i = 0; i < srb->use_sg; i++) {
711 -
712 -                       /* transfer the lesser of the next buffer or the
713 -                        * remaining data */
714 -                       if (transfer_amount - total_transferred >= 
715 -                                       sg[i].length) {
716 -                               result = usb_stor_transfer_partial(us,
717 -                                               sg[i].address, sg[i].length);
718 -                               total_transferred += sg[i].length;
719 -                       } else
720 -                               result = usb_stor_transfer_partial(us,
721 -                                               sg[i].address,
722 -                                               transfer_amount - total_transferred);
723 -
724 -                       /* if we get an error, end the loop here */
725 -                       if (result)
726 -                               break;
727 +               if (us->pusb_dev->speed == USB_SPEED_HIGH) {
728 +                       /* calculate the appropriate pipe information */
729 +                       if (us->srb->sc_data_direction == SCSI_DATA_READ)
730 +                               pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
731 +                       else
732 +                               pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
733 +                       /* use the usb core scatter-gather primitives */
734 +                       result = usb_stor_bulk_transfer_sglist(us, pipe,
735 +                                       sg, srb->use_sg, transfer_amount, &partial);
736 +               } else {
737 +                       for (i = 0; i < srb->use_sg; i++) {
738 +
739 +                               /* transfer the lesser of the next buffer or the
740 +                                * remaining data */
741 +                               if (transfer_amount - total_transferred >= 
742 +                                               sg[i].length) {
743 +                                       result = usb_stor_transfer_partial(us,
744 +                                                       sg[i].address, sg[i].length);
745 +                                       total_transferred += sg[i].length;
746 +                               } else
747 +                                       result = usb_stor_transfer_partial(us,
748 +                                                       sg[i].address,
749 +                                                       transfer_amount - total_transferred);
750 +
751 +                               /* if we get an error, end the loop here */
752 +                               if (result)
753 +                                       break;
754 +                       }
755                 }
756         }
757         else
758 diff -ur linux.old/drivers/usb/storage/transport.h linux.dev/drivers/usb/storage/transport.h
759 --- linux.old/drivers/usb/storage/transport.h   2003-08-25 13:44:42.000000000 +0200
760 +++ linux.dev/drivers/usb/storage/transport.h   2006-07-30 12:10:16.000000000 +0200
761 @@ -127,6 +127,16 @@
762  #define US_BULK_TRANSFER_ABORTED       3  /* transfer canceled             */
763  
764  /*
765 + * usb_stor_bulk_transfer_xxx() return codes, in order of severity
766 + */
767 +
768 +#define USB_STOR_XFER_GOOD             0       /* good transfer                 */
769 +#define USB_STOR_XFER_SHORT            1       /* transferred less than expected */
770 +#define USB_STOR_XFER_STALLED  2       /* endpoint stalled              */
771 +#define USB_STOR_XFER_LONG             3       /* device tried to send too much */
772 +#define USB_STOR_XFER_ERROR            4       /* transfer died in the middle   */
773 +
774 +/*
775   * Transport return codes
776   */
777  
778 diff -ur linux.old/drivers/usb/storage/usb.h linux.dev/drivers/usb/storage/usb.h
779 --- linux.old/drivers/usb/storage/usb.h 2005-04-04 03:42:20.000000000 +0200
780 +++ linux.dev/drivers/usb/storage/usb.h 2006-07-30 12:11:06.000000000 +0200
781 @@ -111,6 +111,60 @@
782  typedef void (*proto_cmnd)(Scsi_Cmnd*, struct us_data*);
783  typedef void (*extra_data_destructor)(void *);  /* extra data destructor   */
784  
785 +/* Dynamic flag definitions: used in set_bit() etc. */
786 +#define US_FLIDX_URB_ACTIVE    18  /* 0x00040000  current_urb is in use  */
787 +#define US_FLIDX_SG_ACTIVE     19  /* 0x00080000  current_sg is in use   */
788 +#define US_FLIDX_ABORTING      20  /* 0x00100000  abort is in progress   */
789 +#define US_FLIDX_DISCONNECTING 21  /* 0x00200000  disconnect in progress */
790 +#define ABORTING_OR_DISCONNECTING      ((1UL << US_FLIDX_ABORTING) | \
791 +                                        (1UL << US_FLIDX_DISCONNECTING))
792 +#define US_FLIDX_RESETTING     22  /* 0x00400000  device reset in progress */
793 +
794 +/* processing state machine states */
795 +#define US_STATE_IDLE          1
796 +#define US_STATE_RUNNING       2
797 +#define US_STATE_RESETTING     3
798 +#define US_STATE_ABORTING      4
799 +
800 +/**
801 + * struct usb_sg_request - support for scatter/gather I/O
802 + * @status: zero indicates success, else negative errno
803 + * @bytes: counts bytes transferred.
804 + *
805 + * These requests are initialized using usb_sg_init(), and then are used
806 + * as request handles passed to usb_sg_wait() or usb_sg_cancel().  Most
807 + * members of the request object aren't for driver access.
808 + *
809 + * The status and bytecount values are valid only after usb_sg_wait()
810 + * returns.  If the status is zero, then the bytecount matches the total
811 + * from the request.
812 + *
813 + * After an error completion, drivers may need to clear a halt condition
814 + * on the endpoint.
815 + */
816 +struct usb_sg_request {
817 +       int                     status;
818 +       size_t                  bytes;
819 +
820 +       /* 
821 +        * members below are private to usbcore,
822 +        * and are not provided for driver access!
823 +        */
824 +       spinlock_t              lock;
825 +
826 +       struct usb_device       *dev;
827 +       int                     pipe;
828 +       struct scatterlist      *sg;
829 +       int                     nents;
830 +
831 +       int                     entries;
832 +       struct urb              **urbs;
833 +
834 +       int                     count;
835 +       struct completion       complete;
836 +};
837 +
838 +
839  /* we allocate one of these for every device that we remember */
840  struct us_data {
841         struct us_data          *next;           /* next device */
842 @@ -171,6 +225,7 @@
843         struct urb              *current_urb;    /* non-int USB requests */
844         struct completion       current_done;    /* the done flag        */
845         unsigned int            tag;             /* tag for bulk CBW/CSW */
846 +       struct usb_sg_request   current_sg;  /* scatter-gather req.  */
847  
848         /* the semaphore for sleeping the control thread */
849         struct semaphore        sema;            /* to sleep thread on   */
850 diff -ur linux.old/include/linux/usb.h linux.dev/include/linux/usb.h
851 --- linux.old/include/linux/usb.h       2004-11-17 12:54:22.000000000 +0100
852 +++ linux.dev/include/linux/usb.h       2006-07-30 12:19:19.000000000 +0200
853 @@ -483,6 +483,8 @@
854  #define URB_NO_INTERRUPT       0x0080  /* HINT: no non-error interrupt needed */
855                                         /* ... less overhead for QUEUE_BULK */
856  #define USB_TIMEOUT_KILLED     0x1000  // only set by HCD!
857 +#define URB_NO_TRANSFER_DMA_MAP        0x0400  /* urb->transfer_dma valid on submit */
858 +#define URB_NO_SETUP_DMA_MAP   0x0800  /* urb->setup_dma valid on submit */
859  
860  struct iso_packet_descriptor
861  {