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