usbtrans.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* usbtrans.c - USB Transfers and Transactions. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/dma.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/usb.h>
  24. #include <grub/usbtrans.h>
  25. #include <grub/time.h>
  26. #include <grub/cache.h>
  27. static inline unsigned int
  28. grub_usb_bulk_maxpacket (grub_usb_device_t dev,
  29. struct grub_usb_desc_endp *endpoint)
  30. {
  31. /* Use the maximum packet size given in the endpoint descriptor. */
  32. if (dev->initialized && endpoint && (unsigned int) endpoint->maxpacket)
  33. return endpoint->maxpacket;
  34. return 64;
  35. }
  36. static grub_usb_err_t
  37. grub_usb_execute_and_wait_transfer (grub_usb_device_t dev,
  38. grub_usb_transfer_t transfer,
  39. int timeout, grub_size_t *actual)
  40. {
  41. grub_usb_err_t err;
  42. grub_uint64_t endtime;
  43. err = dev->controller.dev->setup_transfer (&dev->controller, transfer);
  44. if (err)
  45. return err;
  46. /* endtime moved behind setup transfer to prevent false timeouts
  47. * while debugging... */
  48. endtime = grub_get_time_ms () + timeout;
  49. while (1)
  50. {
  51. err = dev->controller.dev->check_transfer (&dev->controller, transfer,
  52. actual);
  53. if (!err)
  54. return GRUB_USB_ERR_NONE;
  55. if (err != GRUB_USB_ERR_WAIT)
  56. return err;
  57. if (grub_get_time_ms () > endtime)
  58. {
  59. err = dev->controller.dev->cancel_transfer (&dev->controller,
  60. transfer);
  61. if (err)
  62. return err;
  63. return GRUB_USB_ERR_TIMEOUT;
  64. }
  65. grub_cpu_idle ();
  66. }
  67. }
  68. grub_usb_err_t
  69. grub_usb_control_msg (grub_usb_device_t dev,
  70. grub_uint8_t reqtype,
  71. grub_uint8_t request,
  72. grub_uint16_t value,
  73. grub_uint16_t index,
  74. grub_size_t size0, char *data_in)
  75. {
  76. int i;
  77. grub_usb_transfer_t transfer;
  78. int datablocks;
  79. volatile struct grub_usb_packet_setup *setupdata;
  80. grub_uint32_t setupdata_addr;
  81. grub_usb_err_t err;
  82. unsigned int max;
  83. struct grub_pci_dma_chunk *data_chunk, *setupdata_chunk;
  84. volatile char *data;
  85. grub_uint32_t data_addr;
  86. grub_size_t size = size0;
  87. grub_size_t actual;
  88. /* FIXME: avoid allocation any kind of buffer in a first place. */
  89. data_chunk = grub_memalign_dma32 (128, size ? : 16);
  90. if (!data_chunk)
  91. return GRUB_USB_ERR_INTERNAL;
  92. data = grub_dma_get_virt (data_chunk);
  93. data_addr = grub_dma_get_phys (data_chunk);
  94. grub_memcpy ((char *) data, data_in, size);
  95. grub_arch_sync_dma_caches (data, size);
  96. grub_dprintf ("usb",
  97. "control: reqtype=0x%02x req=0x%02x val=0x%02x idx=0x%02x size=%lu\n",
  98. reqtype, request, value, index, (unsigned long)size);
  99. /* Create a transfer. */
  100. transfer = grub_malloc (sizeof (*transfer));
  101. if (! transfer)
  102. {
  103. grub_dma_free (data_chunk);
  104. return GRUB_USB_ERR_INTERNAL;
  105. }
  106. setupdata_chunk = grub_memalign_dma32 (32, sizeof (*setupdata));
  107. if (! setupdata_chunk)
  108. {
  109. grub_free (transfer);
  110. grub_dma_free (data_chunk);
  111. return GRUB_USB_ERR_INTERNAL;
  112. }
  113. setupdata = grub_dma_get_virt (setupdata_chunk);
  114. setupdata_addr = grub_dma_get_phys (setupdata_chunk);
  115. /* Determine the maximum packet size. */
  116. if (dev->descdev.maxsize0)
  117. max = dev->descdev.maxsize0;
  118. else
  119. max = 64;
  120. grub_dprintf ("usb", "control: transfer = %p, dev = %p\n", transfer, dev);
  121. datablocks = (size + max - 1) / max;
  122. /* XXX: Discriminate between different types of control
  123. messages. */
  124. transfer->transcnt = datablocks + 2;
  125. transfer->size = size; /* XXX ? */
  126. transfer->endpoint = 0;
  127. transfer->devaddr = dev->addr;
  128. transfer->type = GRUB_USB_TRANSACTION_TYPE_CONTROL;
  129. transfer->max = max;
  130. transfer->dev = dev;
  131. /* Allocate an array of transfer data structures. */
  132. transfer->transactions = grub_malloc (transfer->transcnt
  133. * sizeof (struct grub_usb_transfer));
  134. if (! transfer->transactions)
  135. {
  136. grub_free (transfer);
  137. grub_dma_free (setupdata_chunk);
  138. grub_dma_free (data_chunk);
  139. return GRUB_USB_ERR_INTERNAL;
  140. }
  141. /* Build a Setup packet. XXX: Endianness. */
  142. setupdata->reqtype = reqtype;
  143. setupdata->request = request;
  144. setupdata->value = value;
  145. setupdata->index = index;
  146. setupdata->length = size;
  147. grub_arch_sync_dma_caches (setupdata, sizeof (*setupdata));
  148. transfer->transactions[0].size = sizeof (*setupdata);
  149. transfer->transactions[0].pid = GRUB_USB_TRANSFER_TYPE_SETUP;
  150. transfer->transactions[0].data = setupdata_addr;
  151. transfer->transactions[0].toggle = 0;
  152. /* Now the data... XXX: Is this the right way to transfer control
  153. transfers? */
  154. for (i = 0; i < datablocks; i++)
  155. {
  156. grub_usb_transaction_t tr = &transfer->transactions[i + 1];
  157. tr->size = (size > max) ? max : size;
  158. /* Use the right most bit as the data toggle. Simple and
  159. effective. */
  160. tr->toggle = !(i & 1);
  161. if (reqtype & 128)
  162. tr->pid = GRUB_USB_TRANSFER_TYPE_IN;
  163. else
  164. tr->pid = GRUB_USB_TRANSFER_TYPE_OUT;
  165. tr->data = data_addr + i * max;
  166. tr->preceding = i * max;
  167. size -= max;
  168. }
  169. /* End with an empty OUT transaction. */
  170. transfer->transactions[datablocks + 1].size = 0;
  171. transfer->transactions[datablocks + 1].data = 0;
  172. if ((reqtype & 128) && datablocks)
  173. transfer->transactions[datablocks + 1].pid = GRUB_USB_TRANSFER_TYPE_OUT;
  174. else
  175. transfer->transactions[datablocks + 1].pid = GRUB_USB_TRANSFER_TYPE_IN;
  176. transfer->transactions[datablocks + 1].toggle = 1;
  177. err = grub_usb_execute_and_wait_transfer (dev, transfer, 1000, &actual);
  178. grub_dprintf ("usb", "control: err=%d\n", err);
  179. grub_free (transfer->transactions);
  180. grub_free (transfer);
  181. grub_dma_free (setupdata_chunk);
  182. grub_arch_sync_dma_caches (data, size0);
  183. grub_memcpy (data_in, (char *) data, size0);
  184. grub_dma_free (data_chunk);
  185. return err;
  186. }
  187. static grub_usb_transfer_t
  188. grub_usb_bulk_setup_readwrite (grub_usb_device_t dev,
  189. struct grub_usb_desc_endp *endpoint,
  190. grub_size_t size0, char *data_in,
  191. grub_transfer_type_t type)
  192. {
  193. int i;
  194. grub_usb_transfer_t transfer;
  195. int datablocks;
  196. unsigned int max;
  197. volatile char *data;
  198. grub_uint32_t data_addr;
  199. struct grub_pci_dma_chunk *data_chunk;
  200. grub_size_t size = size0;
  201. int toggle = dev->toggle[endpoint->endp_addr];
  202. grub_dprintf ("usb", "bulk: size=0x%02lx type=%d\n", (unsigned long) size,
  203. type);
  204. /* FIXME: avoid allocation any kind of buffer in a first place. */
  205. data_chunk = grub_memalign_dma32 (128, size);
  206. if (!data_chunk)
  207. return NULL;
  208. data = grub_dma_get_virt (data_chunk);
  209. data_addr = grub_dma_get_phys (data_chunk);
  210. if (type == GRUB_USB_TRANSFER_TYPE_OUT)
  211. {
  212. grub_memcpy ((char *) data, data_in, size);
  213. grub_arch_sync_dma_caches (data, size);
  214. }
  215. /* Create a transfer. */
  216. transfer = grub_malloc (sizeof (struct grub_usb_transfer));
  217. if (! transfer)
  218. {
  219. grub_dma_free (data_chunk);
  220. return NULL;
  221. }
  222. max = grub_usb_bulk_maxpacket (dev, endpoint);
  223. datablocks = ((size + max - 1) / max);
  224. transfer->transcnt = datablocks;
  225. transfer->size = size - 1;
  226. transfer->endpoint = endpoint->endp_addr;
  227. transfer->devaddr = dev->addr;
  228. transfer->type = GRUB_USB_TRANSACTION_TYPE_BULK;
  229. transfer->dir = type;
  230. transfer->max = max;
  231. transfer->dev = dev;
  232. transfer->last_trans = -1; /* Reset index of last processed transaction (TD) */
  233. transfer->data_chunk = data_chunk;
  234. transfer->data = data_in;
  235. /* Allocate an array of transfer data structures. */
  236. transfer->transactions = grub_malloc (transfer->transcnt
  237. * sizeof (struct grub_usb_transfer));
  238. if (! transfer->transactions)
  239. {
  240. grub_free (transfer);
  241. grub_dma_free (data_chunk);
  242. return NULL;
  243. }
  244. /* Set up all transfers. */
  245. for (i = 0; i < datablocks; i++)
  246. {
  247. grub_usb_transaction_t tr = &transfer->transactions[i];
  248. tr->size = (size > max) ? max : size;
  249. /* XXX: Use the right most bit as the data toggle. Simple and
  250. effective. */
  251. tr->toggle = toggle;
  252. toggle = toggle ? 0 : 1;
  253. tr->pid = type;
  254. tr->data = data_addr + i * max;
  255. tr->preceding = i * max;
  256. size -= tr->size;
  257. }
  258. return transfer;
  259. }
  260. static void
  261. grub_usb_bulk_finish_readwrite (grub_usb_transfer_t transfer)
  262. {
  263. grub_usb_device_t dev = transfer->dev;
  264. int toggle = dev->toggle[transfer->endpoint];
  265. /* We must remember proper toggle value even if some transactions
  266. * were not processed - correct value should be inversion of last
  267. * processed transaction (TD). */
  268. if (transfer->last_trans >= 0)
  269. toggle = transfer->transactions[transfer->last_trans].toggle ? 0 : 1;
  270. else
  271. toggle = dev->toggle[transfer->endpoint]; /* Nothing done, take original */
  272. grub_dprintf ("usb", "bulk: toggle=%d\n", toggle);
  273. dev->toggle[transfer->endpoint] = toggle;
  274. if (transfer->dir == GRUB_USB_TRANSFER_TYPE_IN)
  275. {
  276. grub_arch_sync_dma_caches (grub_dma_get_virt (transfer->data_chunk),
  277. transfer->size + 1);
  278. grub_memcpy (transfer->data, (void *)
  279. grub_dma_get_virt (transfer->data_chunk),
  280. transfer->size + 1);
  281. }
  282. grub_free (transfer->transactions);
  283. grub_dma_free (transfer->data_chunk);
  284. grub_free (transfer);
  285. }
  286. static grub_usb_err_t
  287. grub_usb_bulk_readwrite (grub_usb_device_t dev,
  288. struct grub_usb_desc_endp *endpoint,
  289. grub_size_t size0, char *data_in,
  290. grub_transfer_type_t type, int timeout,
  291. grub_size_t *actual)
  292. {
  293. grub_usb_err_t err;
  294. grub_usb_transfer_t transfer;
  295. transfer = grub_usb_bulk_setup_readwrite (dev, endpoint, size0,
  296. data_in, type);
  297. if (!transfer)
  298. return GRUB_USB_ERR_INTERNAL;
  299. err = grub_usb_execute_and_wait_transfer (dev, transfer, timeout, actual);
  300. grub_usb_bulk_finish_readwrite (transfer);
  301. return err;
  302. }
  303. static grub_usb_err_t
  304. grub_usb_bulk_readwrite_packetize (grub_usb_device_t dev,
  305. struct grub_usb_desc_endp *endpoint,
  306. grub_transfer_type_t type,
  307. grub_size_t size, char *data)
  308. {
  309. grub_size_t actual, transferred;
  310. grub_usb_err_t err = GRUB_USB_ERR_NONE;
  311. grub_size_t current_size, position;
  312. grub_size_t max_bulk_transfer_len = MAX_USB_TRANSFER_LEN;
  313. grub_size_t max;
  314. if (dev->controller.dev->max_bulk_tds)
  315. {
  316. max = grub_usb_bulk_maxpacket (dev, endpoint);
  317. /* Calculate max. possible length of bulk transfer */
  318. max_bulk_transfer_len = dev->controller.dev->max_bulk_tds * max;
  319. }
  320. for (position = 0, transferred = 0;
  321. position < size; position += max_bulk_transfer_len)
  322. {
  323. current_size = size - position;
  324. if (current_size >= max_bulk_transfer_len)
  325. current_size = max_bulk_transfer_len;
  326. err = grub_usb_bulk_readwrite (dev, endpoint, current_size,
  327. &data[position], type, 1000, &actual);
  328. transferred += actual;
  329. if (err || (current_size != actual)) break;
  330. }
  331. if (!err && transferred != size)
  332. err = GRUB_USB_ERR_DATA;
  333. return err;
  334. }
  335. grub_usb_err_t
  336. grub_usb_bulk_write (grub_usb_device_t dev,
  337. struct grub_usb_desc_endp *endpoint,
  338. grub_size_t size, char *data)
  339. {
  340. return grub_usb_bulk_readwrite_packetize (dev, endpoint,
  341. GRUB_USB_TRANSFER_TYPE_OUT,
  342. size, data);
  343. }
  344. grub_usb_err_t
  345. grub_usb_bulk_read (grub_usb_device_t dev,
  346. struct grub_usb_desc_endp *endpoint,
  347. grub_size_t size, char *data)
  348. {
  349. return grub_usb_bulk_readwrite_packetize (dev, endpoint,
  350. GRUB_USB_TRANSFER_TYPE_IN,
  351. size, data);
  352. }
  353. grub_usb_err_t
  354. grub_usb_check_transfer (grub_usb_transfer_t transfer, grub_size_t *actual)
  355. {
  356. grub_usb_err_t err;
  357. grub_usb_device_t dev = transfer->dev;
  358. err = dev->controller.dev->check_transfer (&dev->controller, transfer,
  359. actual);
  360. if (err == GRUB_USB_ERR_WAIT)
  361. return err;
  362. grub_usb_bulk_finish_readwrite (transfer);
  363. return err;
  364. }
  365. grub_usb_transfer_t
  366. grub_usb_bulk_read_background (grub_usb_device_t dev,
  367. struct grub_usb_desc_endp *endpoint,
  368. grub_size_t size, void *data)
  369. {
  370. grub_usb_err_t err;
  371. grub_usb_transfer_t transfer;
  372. transfer = grub_usb_bulk_setup_readwrite (dev, endpoint, size,
  373. data, GRUB_USB_TRANSFER_TYPE_IN);
  374. if (!transfer)
  375. return NULL;
  376. err = dev->controller.dev->setup_transfer (&dev->controller, transfer);
  377. if (err)
  378. return NULL;
  379. return transfer;
  380. }
  381. void
  382. grub_usb_cancel_transfer (grub_usb_transfer_t transfer)
  383. {
  384. grub_usb_device_t dev = transfer->dev;
  385. dev->controller.dev->cancel_transfer (&dev->controller, transfer);
  386. grub_errno = GRUB_ERR_NONE;
  387. }
  388. grub_usb_err_t
  389. grub_usb_bulk_read_extended (grub_usb_device_t dev,
  390. struct grub_usb_desc_endp *endpoint,
  391. grub_size_t size, char *data,
  392. int timeout, grub_size_t *actual)
  393. {
  394. return grub_usb_bulk_readwrite (dev, endpoint, size, data,
  395. GRUB_USB_TRANSFER_TYPE_IN, timeout, actual);
  396. }