228-more_usb_fixes.patch 27 KB

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