usbms.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /* usbms.c - USB Mass Storage Support. */
  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/mm.h>
  21. #include <grub/usb.h>
  22. #include <grub/scsi.h>
  23. #include <grub/scsicmd.h>
  24. #include <grub/misc.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #define GRUB_USBMS_DIRECTION_BIT 7
  27. /* Length of CBI command should be always 12 bytes */
  28. #define GRUB_USBMS_CBI_CMD_SIZE 12
  29. /* CBI class-specific USB request ADSC - it sends CBI (scsi) command to
  30. * device in DATA stage */
  31. #define GRUB_USBMS_CBI_ADSC_REQ 0x00
  32. /* The USB Mass Storage Command Block Wrapper. */
  33. struct grub_usbms_cbw
  34. {
  35. grub_uint32_t signature;
  36. grub_uint32_t tag;
  37. grub_uint32_t transfer_length;
  38. grub_uint8_t flags;
  39. grub_uint8_t lun;
  40. grub_uint8_t length;
  41. grub_uint8_t cbwcb[16];
  42. } GRUB_PACKED;
  43. struct grub_usbms_csw
  44. {
  45. grub_uint32_t signature;
  46. grub_uint32_t tag;
  47. grub_uint32_t residue;
  48. grub_uint8_t status;
  49. } GRUB_PACKED;
  50. struct grub_usbms_dev
  51. {
  52. struct grub_usb_device *dev;
  53. int luns;
  54. int config;
  55. int interface;
  56. struct grub_usb_desc_endp *in;
  57. struct grub_usb_desc_endp *out;
  58. int subclass;
  59. int protocol;
  60. struct grub_usb_desc_endp *intrpt;
  61. };
  62. typedef struct grub_usbms_dev *grub_usbms_dev_t;
  63. /* FIXME: remove limit. */
  64. #define MAX_USBMS_DEVICES 128
  65. static grub_usbms_dev_t grub_usbms_devices[MAX_USBMS_DEVICES];
  66. static int first_available_slot = 0;
  67. static grub_usb_err_t
  68. grub_usbms_cbi_cmd (grub_usb_device_t dev, int interface,
  69. grub_uint8_t *cbicb)
  70. {
  71. return grub_usb_control_msg (dev,
  72. GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
  73. GRUB_USBMS_CBI_ADSC_REQ, 0, interface,
  74. GRUB_USBMS_CBI_CMD_SIZE, (char*)cbicb);
  75. }
  76. static grub_usb_err_t
  77. grub_usbms_cbi_reset (grub_usb_device_t dev, int interface)
  78. {
  79. /* Prepare array with Command Block Reset (=CBR) */
  80. /* CBI specific communication reset command should be send to device
  81. * via CBI USB class specific request ADCS */
  82. struct grub_cbi_reset
  83. {
  84. grub_uint8_t opcode; /* 0x1d = SEND DIAGNOSTIC */
  85. grub_uint8_t lun; /* 7-5 LUN, 4-0 flags - for CBR always = 0x04 */
  86. grub_uint8_t pad[10];
  87. /* XXX: There is collision between CBI and UFI specifications:
  88. * CBI says 0xff, UFI says 0x00 ... probably it does
  89. * not matter ... (?) */
  90. } cbicb = { 0x1d, 0x04,
  91. { 0xff, 0xff, 0xff, 0xff, 0xff,
  92. 0xff, 0xff, 0xff, 0xff, 0xff }
  93. };
  94. return grub_usbms_cbi_cmd (dev, interface, (grub_uint8_t *)&cbicb);
  95. }
  96. static grub_usb_err_t
  97. grub_usbms_bo_reset (grub_usb_device_t dev, int interface)
  98. {
  99. return grub_usb_control_msg (dev, 0x21, 255, 0, interface, 0, 0);
  100. }
  101. static grub_usb_err_t
  102. grub_usbms_reset (grub_usbms_dev_t dev)
  103. {
  104. if (dev->protocol == GRUB_USBMS_PROTOCOL_BULK)
  105. return grub_usbms_bo_reset (dev->dev, dev->interface);
  106. else
  107. return grub_usbms_cbi_reset (dev->dev, dev->interface);
  108. }
  109. static void
  110. grub_usbms_detach (grub_usb_device_t usbdev, int config, int interface)
  111. {
  112. unsigned i;
  113. for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
  114. if (grub_usbms_devices[i] && grub_usbms_devices[i]->dev == usbdev
  115. && grub_usbms_devices[i]->interface == interface
  116. && grub_usbms_devices[i]->config == config)
  117. {
  118. grub_free (grub_usbms_devices[i]);
  119. grub_usbms_devices[i] = 0;
  120. }
  121. }
  122. static int
  123. grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)
  124. {
  125. struct grub_usb_desc_if *interf
  126. = usbdev->config[configno].interf[interfno].descif;
  127. int j;
  128. grub_uint8_t luns = 0;
  129. unsigned curnum;
  130. grub_usb_err_t err = GRUB_USB_ERR_NONE;
  131. grub_boot_time ("Attaching USB mass storage");
  132. if (first_available_slot == ARRAY_SIZE (grub_usbms_devices))
  133. return 0;
  134. curnum = first_available_slot;
  135. first_available_slot++;
  136. interf = usbdev->config[configno].interf[interfno].descif;
  137. if ((interf->subclass != GRUB_USBMS_SUBCLASS_BULK
  138. /* Experimental support of RBC, MMC-2, UFI, SFF-8070i devices */
  139. && interf->subclass != GRUB_USBMS_SUBCLASS_RBC
  140. && interf->subclass != GRUB_USBMS_SUBCLASS_MMC2
  141. && interf->subclass != GRUB_USBMS_SUBCLASS_UFI
  142. && interf->subclass != GRUB_USBMS_SUBCLASS_SFF8070 )
  143. || (interf->protocol != GRUB_USBMS_PROTOCOL_BULK
  144. && interf->protocol != GRUB_USBMS_PROTOCOL_CBI
  145. && interf->protocol != GRUB_USBMS_PROTOCOL_CB))
  146. return 0;
  147. grub_usbms_devices[curnum] = grub_zalloc (sizeof (struct grub_usbms_dev));
  148. if (! grub_usbms_devices[curnum])
  149. return 0;
  150. grub_usbms_devices[curnum]->dev = usbdev;
  151. grub_usbms_devices[curnum]->interface = interfno;
  152. grub_usbms_devices[curnum]->subclass = interf->subclass;
  153. grub_usbms_devices[curnum]->protocol = interf->protocol;
  154. grub_dprintf ("usbms", "alive\n");
  155. /* Iterate over all endpoints of this interface, at least a
  156. IN and OUT bulk endpoint are required. */
  157. for (j = 0; j < interf->endpointcnt; j++)
  158. {
  159. struct grub_usb_desc_endp *endp;
  160. endp = &usbdev->config[0].interf[interfno].descendp[j];
  161. if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2)
  162. /* Bulk IN endpoint. */
  163. grub_usbms_devices[curnum]->in = endp;
  164. else if (!(endp->endp_addr & 128) && (endp->attrib & 3) == 2)
  165. /* Bulk OUT endpoint. */
  166. grub_usbms_devices[curnum]->out = endp;
  167. else if ((endp->endp_addr & 128) && (endp->attrib & 3) == 3)
  168. /* Interrupt (IN) endpoint. */
  169. grub_usbms_devices[curnum]->intrpt = endp;
  170. }
  171. if (!grub_usbms_devices[curnum]->in || !grub_usbms_devices[curnum]->out
  172. || ((grub_usbms_devices[curnum]->protocol == GRUB_USBMS_PROTOCOL_CBI)
  173. && !grub_usbms_devices[curnum]->intrpt))
  174. {
  175. grub_free (grub_usbms_devices[curnum]);
  176. grub_usbms_devices[curnum] = 0;
  177. return 0;
  178. }
  179. grub_dprintf ("usbms", "alive\n");
  180. /* XXX: Activate the first configuration. */
  181. grub_usb_set_configuration (usbdev, 1);
  182. /* Query the amount of LUNs. */
  183. if (grub_usbms_devices[curnum]->protocol == GRUB_USBMS_PROTOCOL_BULK)
  184. { /* Only Bulk only devices support Get Max LUN command */
  185. err = grub_usb_control_msg (usbdev, 0xA1, 254, 0, interfno, 1, (char *) &luns);
  186. if (err)
  187. {
  188. /* In case of a stall, clear the stall. */
  189. if (err == GRUB_USB_ERR_STALL)
  190. {
  191. grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->in->endp_addr);
  192. grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->out->endp_addr);
  193. }
  194. /* Just set the amount of LUNs to one. */
  195. grub_errno = GRUB_ERR_NONE;
  196. grub_usbms_devices[curnum]->luns = 1;
  197. }
  198. else
  199. /* luns = 0 means one LUN with ID 0 present ! */
  200. /* We get from device not number of LUNs but highest
  201. * LUN number. LUNs are numbered from 0,
  202. * i.e. number of LUNs is luns+1 ! */
  203. grub_usbms_devices[curnum]->luns = luns + 1;
  204. }
  205. else
  206. /* XXX: Does CBI devices support multiple LUNs ?
  207. * I.e., should we detect number of device's LUNs ? (How?) */
  208. grub_usbms_devices[curnum]->luns = 1;
  209. grub_dprintf ("usbms", "alive\n");
  210. usbdev->config[configno].interf[interfno].detach_hook = grub_usbms_detach;
  211. grub_boot_time ("Attached USB mass storage");
  212. #if 0 /* All this part should be probably deleted.
  213. * This make trouble on some devices if they are not in
  214. * Phase Error state - and there they should be not in such state...
  215. * Bulk only mass storage reset procedure should be used only
  216. * on place and in time when it is really necessary. */
  217. /* Reset recovery procedure */
  218. /* Bulk-Only Mass Storage Reset, after the reset commands
  219. will be accepted. */
  220. grub_usbms_reset (usbdev, i);
  221. grub_usb_clear_halt (usbdev, usbms->in->endp_addr);
  222. grub_usb_clear_halt (usbdev, usbms->out->endp_addr);
  223. #endif
  224. return 1;
  225. }
  226. static int
  227. grub_usbms_iterate (grub_scsi_dev_iterate_hook_t hook, void *hook_data,
  228. grub_disk_pull_t pull)
  229. {
  230. unsigned i;
  231. if (pull != GRUB_DISK_PULL_NONE)
  232. return 0;
  233. grub_usb_poll_devices (1);
  234. for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
  235. if (grub_usbms_devices[i])
  236. {
  237. if (hook (GRUB_SCSI_SUBSYSTEM_USBMS, i, grub_usbms_devices[i]->luns,
  238. hook_data))
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. static grub_err_t
  244. grub_usbms_transfer_bo (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  245. grub_size_t size, char *buf, int read_write)
  246. {
  247. struct grub_usbms_cbw cbw;
  248. grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
  249. struct grub_usbms_csw status;
  250. static grub_uint32_t tag = 0;
  251. grub_usb_err_t err = GRUB_USB_ERR_NONE;
  252. grub_usb_err_t errCSW = GRUB_USB_ERR_NONE;
  253. int retrycnt = 3 + 1;
  254. tag++;
  255. retry:
  256. retrycnt--;
  257. if (retrycnt == 0)
  258. return grub_error (GRUB_ERR_IO, "USB Mass Storage stalled");
  259. /* Setup the request. */
  260. grub_memset (&cbw, 0, sizeof (cbw));
  261. cbw.signature = grub_cpu_to_le32_compile_time (0x43425355);
  262. cbw.tag = tag;
  263. cbw.transfer_length = grub_cpu_to_le32 (size);
  264. cbw.flags = (!read_write) << GRUB_USBMS_DIRECTION_BIT;
  265. cbw.lun = scsi->lun; /* In USB MS CBW are LUN bits on another place than in SCSI CDB, both should be set correctly. */
  266. cbw.length = cmdsize;
  267. grub_memcpy (cbw.cbwcb, cmd, cmdsize);
  268. /* Debug print of CBW content. */
  269. grub_dprintf ("usb", "CBW: sign=0x%08x tag=0x%08x len=0x%08x\n",
  270. cbw.signature, cbw.tag, cbw.transfer_length);
  271. grub_dprintf ("usb", "CBW: flags=0x%02x lun=0x%02x CB_len=0x%02x\n",
  272. cbw.flags, cbw.lun, cbw.length);
  273. grub_dprintf ("usb", "CBW: cmd:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  274. cbw.cbwcb[ 0], cbw.cbwcb[ 1], cbw.cbwcb[ 2], cbw.cbwcb[ 3],
  275. cbw.cbwcb[ 4], cbw.cbwcb[ 5], cbw.cbwcb[ 6], cbw.cbwcb[ 7],
  276. cbw.cbwcb[ 8], cbw.cbwcb[ 9], cbw.cbwcb[10], cbw.cbwcb[11],
  277. cbw.cbwcb[12], cbw.cbwcb[13], cbw.cbwcb[14], cbw.cbwcb[15]);
  278. /* Write the request.
  279. * XXX: Error recovery is maybe still not fully correct. */
  280. err = grub_usb_bulk_write (dev->dev, dev->out,
  281. sizeof (cbw), (char *) &cbw);
  282. if (err)
  283. {
  284. if (err == GRUB_USB_ERR_STALL)
  285. {
  286. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  287. goto CheckCSW;
  288. }
  289. goto retry;
  290. }
  291. /* Read/write the data, (maybe) according to specification. */
  292. if (size && (read_write == 0))
  293. {
  294. err = grub_usb_bulk_read (dev->dev, dev->in, size, buf);
  295. grub_dprintf ("usb", "read: %d %d\n", err, GRUB_USB_ERR_STALL);
  296. if (err)
  297. {
  298. if (err == GRUB_USB_ERR_STALL)
  299. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  300. goto CheckCSW;
  301. }
  302. /* Debug print of received data. */
  303. grub_dprintf ("usb", "buf:\n");
  304. if (size <= 64)
  305. {
  306. unsigned i;
  307. for (i = 0; i < size; i++)
  308. grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
  309. }
  310. else
  311. grub_dprintf ("usb", "Too much data for debug print...\n");
  312. }
  313. else if (size)
  314. {
  315. err = grub_usb_bulk_write (dev->dev, dev->out, size, buf);
  316. grub_dprintf ("usb", "write: %d %d\n", err, GRUB_USB_ERR_STALL);
  317. grub_dprintf ("usb", "First 16 bytes of sent data:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  318. buf[ 0], buf[ 1], buf[ 2], buf[ 3],
  319. buf[ 4], buf[ 5], buf[ 6], buf[ 7],
  320. buf[ 8], buf[ 9], buf[10], buf[11],
  321. buf[12], buf[13], buf[14], buf[15]);
  322. if (err)
  323. {
  324. if (err == GRUB_USB_ERR_STALL)
  325. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  326. goto CheckCSW;
  327. }
  328. /* Debug print of sent data. */
  329. if (size <= 256)
  330. {
  331. unsigned i;
  332. for (i=0; i<size; i++)
  333. grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
  334. }
  335. else
  336. grub_dprintf ("usb", "Too much data for debug print...\n");
  337. }
  338. /* Read the status - (maybe) according to specification. */
  339. CheckCSW:
  340. errCSW = grub_usb_bulk_read (dev->dev, dev->in,
  341. sizeof (status), (char *) &status);
  342. if (errCSW)
  343. {
  344. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  345. errCSW = grub_usb_bulk_read (dev->dev, dev->in,
  346. sizeof (status), (char *) &status);
  347. if (errCSW)
  348. { /* Bulk-only reset device. */
  349. grub_dprintf ("usb", "Bulk-only reset device - errCSW\n");
  350. grub_usbms_reset (dev);
  351. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  352. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  353. goto retry;
  354. }
  355. }
  356. /* Debug print of CSW content. */
  357. grub_dprintf ("usb", "CSW: sign=0x%08x tag=0x%08x resid=0x%08x\n",
  358. status.signature, status.tag, status.residue);
  359. grub_dprintf ("usb", "CSW: status=0x%02x\n", status.status);
  360. /* If phase error or not valid signature, do bulk-only reset device. */
  361. if ((status.status == 2) ||
  362. (status.signature != grub_cpu_to_le32_compile_time(0x53425355)))
  363. { /* Bulk-only reset device. */
  364. grub_dprintf ("usb", "Bulk-only reset device - bad status\n");
  365. grub_usbms_reset (dev);
  366. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  367. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  368. goto retry;
  369. }
  370. /* If "command failed" status or data transfer failed -> error */
  371. if ((status.status || err) && !read_write)
  372. return grub_error (GRUB_ERR_READ_ERROR,
  373. "error communication with USB Mass Storage device");
  374. else if ((status.status || err) && read_write)
  375. return grub_error (GRUB_ERR_WRITE_ERROR,
  376. "error communication with USB Mass Storage device");
  377. return GRUB_ERR_NONE;
  378. }
  379. static grub_err_t
  380. grub_usbms_transfer_cbi (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  381. grub_size_t size, char *buf, int read_write)
  382. {
  383. grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
  384. int retrycnt = 3 + 1;
  385. grub_usb_err_t err = GRUB_USB_ERR_NONE;
  386. grub_uint8_t cbicb[GRUB_USBMS_CBI_CMD_SIZE];
  387. grub_uint16_t status;
  388. retry:
  389. retrycnt--;
  390. if (retrycnt == 0)
  391. return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI failed");
  392. /* Setup the request. */
  393. grub_memset (cbicb, 0, sizeof (cbicb));
  394. grub_memcpy (cbicb, cmd,
  395. cmdsize >= GRUB_USBMS_CBI_CMD_SIZE
  396. ? GRUB_USBMS_CBI_CMD_SIZE
  397. : cmdsize);
  398. /* Debug print of CBIcb content. */
  399. grub_dprintf ("usb", "cbicb:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  400. cbicb[ 0], cbicb[ 1], cbicb[ 2], cbicb[ 3],
  401. cbicb[ 4], cbicb[ 5], cbicb[ 6], cbicb[ 7],
  402. cbicb[ 8], cbicb[ 9], cbicb[10], cbicb[11]);
  403. /* Write the request.
  404. * XXX: Error recovery is maybe not correct. */
  405. err = grub_usbms_cbi_cmd (dev->dev, dev->interface, cbicb);
  406. if (err)
  407. {
  408. grub_dprintf ("usb", "CBI cmdcb setup err=%d\n", err);
  409. if (err == GRUB_USB_ERR_STALL)
  410. {
  411. /* Stall in this place probably means bad or unsupported
  412. * command, so we will not try it again. */
  413. return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI request failed");
  414. }
  415. else if (dev->protocol == GRUB_USBMS_PROTOCOL_CBI)
  416. {
  417. /* Try to get status from interrupt pipe */
  418. err = grub_usb_bulk_read (dev->dev, dev->intrpt,
  419. 2, (char*)&status);
  420. grub_dprintf ("usb", "CBI cmdcb setup status: err=%d, status=0x%x\n", err, status);
  421. }
  422. /* Any other error could be transport problem, try it again */
  423. goto retry;
  424. }
  425. /* Read/write the data, (maybe) according to specification. */
  426. if (size && (read_write == 0))
  427. {
  428. err = grub_usb_bulk_read (dev->dev, dev->in, size, buf);
  429. grub_dprintf ("usb", "read: %d\n", err);
  430. if (err)
  431. {
  432. if (err == GRUB_USB_ERR_STALL)
  433. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  434. goto retry;
  435. }
  436. }
  437. else if (size)
  438. {
  439. err = grub_usb_bulk_write (dev->dev, dev->out, size, buf);
  440. grub_dprintf ("usb", "write: %d\n", err);
  441. if (err)
  442. {
  443. if (err == GRUB_USB_ERR_STALL)
  444. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  445. goto retry;
  446. }
  447. }
  448. /* XXX: It is not clear to me yet, how to check status of CBI
  449. * data transfer on devices without interrupt pipe.
  450. * AFAIK there is probably no status phase to indicate possibly
  451. * bad transported data.
  452. * Maybe we should do check on higher level, i.e. issue RequestSense
  453. * command (we do it already in scsi.c) and check returned values
  454. * (we do not it yet) - ? */
  455. if (dev->protocol == GRUB_USBMS_PROTOCOL_CBI)
  456. { /* Check status in interrupt pipe */
  457. err = grub_usb_bulk_read (dev->dev, dev->intrpt,
  458. 2, (char*)&status);
  459. grub_dprintf ("usb", "read status: %d\n", err);
  460. if (err)
  461. {
  462. /* Try to reset device, because it is probably not standard
  463. * situation */
  464. grub_usbms_reset (dev);
  465. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  466. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  467. grub_usb_clear_halt (dev->dev, dev->intrpt->endp_addr);
  468. goto retry;
  469. }
  470. if (dev->subclass == GRUB_USBMS_SUBCLASS_UFI)
  471. {
  472. /* These devices should return bASC and bASCQ */
  473. if (status != 0)
  474. /* Some error, currently we don't care what it is... */
  475. goto retry;
  476. }
  477. else if (dev->subclass == GRUB_USBMS_SUBCLASS_RBC)
  478. {
  479. /* XXX: I don't understand what returns RBC subclass devices,
  480. * so I don't check it - maybe somebody helps ? */
  481. }
  482. else
  483. {
  484. /* Any other device should return bType = 0 and some bValue */
  485. if (status & 0xff)
  486. return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI status type != 0");
  487. status = (status & 0x0300) >> 8;
  488. switch (status)
  489. {
  490. case 0 : /* OK */
  491. break;
  492. case 1 : /* Fail */
  493. goto retry;
  494. break;
  495. case 2 : /* Phase error */
  496. case 3 : /* Persistent Failure */
  497. grub_dprintf ("usb", "CBI reset device - phase error or persistent failure\n");
  498. grub_usbms_reset (dev);
  499. grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
  500. grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
  501. grub_usb_clear_halt (dev->dev, dev->intrpt->endp_addr);
  502. goto retry;
  503. break;
  504. }
  505. }
  506. }
  507. if (err)
  508. return grub_error (GRUB_ERR_IO, "USB error %d", err);
  509. return GRUB_ERR_NONE;
  510. }
  511. static grub_err_t
  512. grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  513. grub_size_t size, char *buf, int read_write)
  514. {
  515. grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
  516. if (dev->protocol == GRUB_USBMS_PROTOCOL_BULK)
  517. return grub_usbms_transfer_bo (scsi, cmdsize, cmd, size, buf,
  518. read_write);
  519. else
  520. return grub_usbms_transfer_cbi (scsi, cmdsize, cmd, size, buf,
  521. read_write);
  522. }
  523. static grub_err_t
  524. grub_usbms_read (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  525. grub_size_t size, char *buf)
  526. {
  527. return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 0);
  528. }
  529. static grub_err_t
  530. grub_usbms_write (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
  531. grub_size_t size, const char *buf)
  532. {
  533. return grub_usbms_transfer (scsi, cmdsize, cmd, size, (char *) buf, 1);
  534. }
  535. static grub_err_t
  536. grub_usbms_open (int id, int devnum, struct grub_scsi *scsi)
  537. {
  538. if (id != GRUB_SCSI_SUBSYSTEM_USBMS)
  539. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  540. "not USB Mass Storage device");
  541. if (!grub_usbms_devices[devnum])
  542. grub_usb_poll_devices (1);
  543. if (!grub_usbms_devices[devnum])
  544. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  545. "unknown USB Mass Storage device");
  546. scsi->data = grub_usbms_devices[devnum];
  547. scsi->luns = grub_usbms_devices[devnum]->luns;
  548. return GRUB_ERR_NONE;
  549. }
  550. static struct grub_scsi_dev grub_usbms_dev =
  551. {
  552. .iterate = grub_usbms_iterate,
  553. .open = grub_usbms_open,
  554. .read = grub_usbms_read,
  555. .write = grub_usbms_write
  556. };
  557. static struct grub_usb_attach_desc attach_hook =
  558. {
  559. .class = GRUB_USB_CLASS_MASS_STORAGE,
  560. .hook = grub_usbms_attach
  561. };
  562. GRUB_MOD_INIT(usbms)
  563. {
  564. grub_usb_register_attach_hook_class (&attach_hook);
  565. grub_scsi_dev_register (&grub_usbms_dev);
  566. }
  567. GRUB_MOD_FINI(usbms)
  568. {
  569. unsigned i;
  570. for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
  571. {
  572. grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
  573. .interf[grub_usbms_devices[i]->interface].detach_hook = 0;
  574. grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
  575. .interf[grub_usbms_devices[i]->interface].attached = 0;
  576. }
  577. grub_usb_unregister_attach_hook_class (&attach_hook);
  578. grub_scsi_dev_unregister (&grub_usbms_dev);
  579. }