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