Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / disk / usbms.c
blob52cc33e933179b26901f6aebbd2f15be10bb85ec
1 /* usbms.c - USB Mass Storage Support. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/dl.h>
21 #include <grub/mm.h>
22 #include <grub/usb.h>
23 #include <grub/scsi.h>
24 #include <grub/scsicmd.h>
25 #include <grub/misc.h>
27 GRUB_MOD_LICENSE ("GPLv3+");
29 #define GRUB_USBMS_DIRECTION_BIT 7
31 /* Length of CBI command should be always 12 bytes */
32 #define GRUB_USBMS_CBI_CMD_SIZE 12
33 /* CBI class-specific USB request ADSC - it sends CBI (scsi) command to
34 * device in DATA stage */
35 #define GRUB_USBMS_CBI_ADSC_REQ 0x00
37 /* The USB Mass Storage Command Block Wrapper. */
38 struct grub_usbms_cbw
40 grub_uint32_t signature;
41 grub_uint32_t tag;
42 grub_uint32_t transfer_length;
43 grub_uint8_t flags;
44 grub_uint8_t lun;
45 grub_uint8_t length;
46 grub_uint8_t cbwcb[16];
47 } __attribute__ ((packed));
49 struct grub_usbms_csw
51 grub_uint32_t signature;
52 grub_uint32_t tag;
53 grub_uint32_t residue;
54 grub_uint8_t status;
55 } __attribute__ ((packed));
57 struct grub_usbms_dev
59 struct grub_usb_device *dev;
61 int luns;
63 int config;
64 int interface;
65 struct grub_usb_desc_endp *in;
66 struct grub_usb_desc_endp *out;
68 int subclass;
69 int protocol;
70 struct grub_usb_desc_endp *intrpt;
72 typedef struct grub_usbms_dev *grub_usbms_dev_t;
74 /* FIXME: remove limit. */
75 #define MAX_USBMS_DEVICES 128
76 static grub_usbms_dev_t grub_usbms_devices[MAX_USBMS_DEVICES];
77 static int first_available_slot = 0;
79 static grub_err_t
80 grub_usbms_cbi_cmd (grub_usb_device_t dev, int interface,
81 grub_uint8_t *cbicb)
83 return grub_usb_control_msg (dev,
84 GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
85 GRUB_USBMS_CBI_ADSC_REQ, 0, interface,
86 GRUB_USBMS_CBI_CMD_SIZE, (char*)cbicb);
89 static grub_err_t
90 grub_usbms_cbi_reset (grub_usb_device_t dev, int interface)
92 /* Prepare array with Command Block Reset (=CBR) */
93 /* CBI specific communication reset command should be send to device
94 * via CBI USB class specific request ADCS */
95 struct grub_cbi_reset
97 grub_uint8_t opcode; /* 0x1d = SEND DIAGNOSTIC */
98 grub_uint8_t lun; /* 7-5 LUN, 4-0 flags - for CBR always = 0x04 */
99 grub_uint8_t pad[10];
100 /* XXX: There is collision between CBI and UFI specifications:
101 * CBI says 0xff, UFI says 0x00 ... probably it does
102 * not matter ... (?) */
103 } cbicb = { 0x1d, 0x04,
104 { 0xff, 0xff, 0xff, 0xff, 0xff,
105 0xff, 0xff, 0xff, 0xff, 0xff }
108 return grub_usbms_cbi_cmd (dev, interface, (grub_uint8_t *)&cbicb);
111 static grub_err_t
112 grub_usbms_bo_reset (grub_usb_device_t dev, int interface)
114 grub_usb_err_t u;
115 u = grub_usb_control_msg (dev, 0x21, 255, 0, interface, 0, 0);
116 if (u)
117 return grub_error (GRUB_ERR_IO, "USB error %d", u);
118 return GRUB_ERR_NONE;
121 static grub_err_t
122 grub_usbms_reset (grub_usbms_dev_t dev)
124 if (dev->protocol == GRUB_USBMS_PROTOCOL_BULK)
125 return grub_usbms_bo_reset (dev->dev, dev->interface);
126 else
127 return grub_usbms_cbi_reset (dev->dev, dev->interface);
130 static void
131 grub_usbms_detach (grub_usb_device_t usbdev, int config, int interface)
133 unsigned i;
134 for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
135 if (grub_usbms_devices[i] && grub_usbms_devices[i]->dev == usbdev
136 && grub_usbms_devices[i]->interface == interface
137 && grub_usbms_devices[i]->config == config)
139 grub_free (grub_usbms_devices[i]);
140 grub_usbms_devices[i] = 0;
144 static int
145 grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)
147 struct grub_usb_desc_if *interf
148 = usbdev->config[configno].interf[interfno].descif;
149 int j;
150 grub_uint8_t luns = 0;
151 unsigned curnum;
152 grub_usb_err_t err = GRUB_ERR_NONE;
154 if (first_available_slot == ARRAY_SIZE (grub_usbms_devices))
155 return 0;
157 curnum = first_available_slot;
158 first_available_slot++;
160 interf = usbdev->config[configno].interf[interfno].descif;
162 if ((interf->subclass != GRUB_USBMS_SUBCLASS_BULK
163 /* Experimental support of RBC, MMC-2, UFI, SFF-8070i devices */
164 && interf->subclass != GRUB_USBMS_SUBCLASS_RBC
165 && interf->subclass != GRUB_USBMS_SUBCLASS_MMC2
166 && interf->subclass != GRUB_USBMS_SUBCLASS_UFI
167 && interf->subclass != GRUB_USBMS_SUBCLASS_SFF8070 )
168 || (interf->protocol != GRUB_USBMS_PROTOCOL_BULK
169 && interf->protocol != GRUB_USBMS_PROTOCOL_CBI
170 && interf->protocol != GRUB_USBMS_PROTOCOL_CB))
171 return 0;
173 grub_usbms_devices[curnum] = grub_zalloc (sizeof (struct grub_usbms_dev));
174 if (! grub_usbms_devices[curnum])
175 return 0;
177 grub_usbms_devices[curnum]->dev = usbdev;
178 grub_usbms_devices[curnum]->interface = interfno;
179 grub_usbms_devices[curnum]->subclass = interf->subclass;
180 grub_usbms_devices[curnum]->protocol = interf->protocol;
182 grub_dprintf ("usbms", "alive\n");
184 /* Iterate over all endpoints of this interface, at least a
185 IN and OUT bulk endpoint are required. */
186 for (j = 0; j < interf->endpointcnt; j++)
188 struct grub_usb_desc_endp *endp;
189 endp = &usbdev->config[0].interf[interfno].descendp[j];
191 if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2)
192 /* Bulk IN endpoint. */
193 grub_usbms_devices[curnum]->in = endp;
194 else if (!(endp->endp_addr & 128) && (endp->attrib & 3) == 2)
195 /* Bulk OUT endpoint. */
196 grub_usbms_devices[curnum]->out = endp;
197 else if ((endp->endp_addr & 128) && (endp->attrib & 3) == 3)
198 /* Interrupt (IN) endpoint. */
199 grub_usbms_devices[curnum]->intrpt = endp;
202 if (!grub_usbms_devices[curnum]->in || !grub_usbms_devices[curnum]->out
203 || ((grub_usbms_devices[curnum]->protocol == GRUB_USBMS_PROTOCOL_CBI)
204 && !grub_usbms_devices[curnum]->intrpt))
206 grub_free (grub_usbms_devices[curnum]);
207 grub_usbms_devices[curnum] = 0;
208 return 0;
211 grub_dprintf ("usbms", "alive\n");
213 /* XXX: Activate the first configuration. */
214 grub_usb_set_configuration (usbdev, 1);
216 /* Query the amount of LUNs. */
217 if (grub_usbms_devices[curnum]->protocol == GRUB_USBMS_PROTOCOL_BULK)
218 { /* Only Bulk only devices support Get Max LUN command */
219 err = grub_usb_control_msg (usbdev, 0xA1, 254, 0, interfno, 1, (char *) &luns);
221 if (err)
223 /* In case of a stall, clear the stall. */
224 if (err == GRUB_USB_ERR_STALL)
226 grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->in->endp_addr);
227 grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->out->endp_addr);
229 /* Just set the amount of LUNs to one. */
230 grub_errno = GRUB_ERR_NONE;
231 grub_usbms_devices[curnum]->luns = 1;
233 else
234 /* luns = 0 means one LUN with ID 0 present ! */
235 /* We get from device not number of LUNs but highest
236 * LUN number. LUNs are numbered from 0,
237 * i.e. number of LUNs is luns+1 ! */
238 grub_usbms_devices[curnum]->luns = luns + 1;
240 else
241 /* XXX: Does CBI devices support multiple LUNs ?
242 * I.e., should we detect number of device's LUNs ? (How?) */
243 grub_usbms_devices[curnum]->luns = 1;
245 grub_dprintf ("usbms", "alive\n");
247 usbdev->config[configno].interf[interfno].detach_hook = grub_usbms_detach;
249 #if 0 /* All this part should be probably deleted.
250 * This make trouble on some devices if they are not in
251 * Phase Error state - and there they should be not in such state...
252 * Bulk only mass storage reset procedure should be used only
253 * on place and in time when it is really necessary. */
254 /* Reset recovery procedure */
255 /* Bulk-Only Mass Storage Reset, after the reset commands
256 will be accepted. */
257 grub_usbms_reset (usbdev, i);
258 grub_usb_clear_halt (usbdev, usbms->in->endp_addr);
259 grub_usb_clear_halt (usbdev, usbms->out->endp_addr);
260 #endif
262 return 1;
267 static int
268 grub_usbms_iterate (int NESTED_FUNC_ATTR (*hook) (int id, int bus, int luns),
269 grub_disk_pull_t pull)
271 unsigned i;
273 if (pull != GRUB_DISK_PULL_NONE)
274 return 0;
276 grub_usb_poll_devices ();
278 for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
279 if (grub_usbms_devices[i])
281 if (hook (GRUB_SCSI_SUBSYSTEM_USBMS, i, grub_usbms_devices[i]->luns))
282 return 1;
285 return 0;
288 static grub_err_t
289 grub_usbms_transfer_bo (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
290 grub_size_t size, char *buf, int read_write)
292 struct grub_usbms_cbw cbw;
293 grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
294 struct grub_usbms_csw status;
295 static grub_uint32_t tag = 0;
296 grub_usb_err_t err = GRUB_USB_ERR_NONE;
297 grub_usb_err_t errCSW = GRUB_USB_ERR_NONE;
298 int retrycnt = 3 + 1;
300 retry:
301 retrycnt--;
302 if (retrycnt == 0)
303 return grub_error (GRUB_ERR_IO, "USB Mass Storage stalled");
305 /* Setup the request. */
306 grub_memset (&cbw, 0, sizeof (cbw));
307 cbw.signature = grub_cpu_to_le32 (0x43425355);
308 cbw.tag = tag++;
309 cbw.transfer_length = grub_cpu_to_le32 (size);
310 cbw.flags = (!read_write) << GRUB_USBMS_DIRECTION_BIT;
311 cbw.lun = scsi->lun; /* In USB MS CBW are LUN bits on another place than in SCSI CDB, both should be set correctly. */
312 cbw.length = cmdsize;
313 grub_memcpy (cbw.cbwcb, cmd, cmdsize);
315 /* Debug print of CBW content. */
316 grub_dprintf ("usb", "CBW: sign=0x%08x tag=0x%08x len=0x%08x\n",
317 cbw.signature, cbw.tag, cbw.transfer_length);
318 grub_dprintf ("usb", "CBW: flags=0x%02x lun=0x%02x CB_len=0x%02x\n",
319 cbw.flags, cbw.lun, cbw.length);
320 grub_dprintf ("usb", "CBW: cmd:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
321 cbw.cbwcb[ 0], cbw.cbwcb[ 1], cbw.cbwcb[ 2], cbw.cbwcb[ 3],
322 cbw.cbwcb[ 4], cbw.cbwcb[ 5], cbw.cbwcb[ 6], cbw.cbwcb[ 7],
323 cbw.cbwcb[ 8], cbw.cbwcb[ 9], cbw.cbwcb[10], cbw.cbwcb[11],
324 cbw.cbwcb[12], cbw.cbwcb[13], cbw.cbwcb[14], cbw.cbwcb[15]);
326 /* Write the request.
327 * XXX: Error recovery is maybe still not fully correct. */
328 err = grub_usb_bulk_write (dev->dev, dev->out->endp_addr,
329 sizeof (cbw), (char *) &cbw);
330 if (err)
332 if (err == GRUB_USB_ERR_STALL)
334 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
335 goto CheckCSW;
337 return grub_error (GRUB_ERR_IO, "USB Mass Storage request failed");
340 /* Read/write the data, (maybe) according to specification. */
341 if (size && (read_write == 0))
343 err = grub_usb_bulk_read (dev->dev, dev->in->endp_addr, size, buf);
344 grub_dprintf ("usb", "read: %d %d\n", err, GRUB_USB_ERR_STALL);
345 if (err)
347 if (err == GRUB_USB_ERR_STALL)
348 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
349 goto CheckCSW;
351 /* Debug print of received data. */
352 grub_dprintf ("usb", "buf:\n");
353 if (size <= 64)
355 unsigned i;
356 for (i = 0; i < size; i++)
357 grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
359 else
360 grub_dprintf ("usb", "Too much data for debug print...\n");
362 else if (size)
364 err = grub_usb_bulk_write (dev->dev, dev->out->endp_addr, size, buf);
365 grub_dprintf ("usb", "write: %d %d\n", err, GRUB_USB_ERR_STALL);
366 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",
367 buf[ 0], buf[ 1], buf[ 2], buf[ 3],
368 buf[ 4], buf[ 5], buf[ 6], buf[ 7],
369 buf[ 8], buf[ 9], buf[10], buf[11],
370 buf[12], buf[13], buf[14], buf[15]);
371 if (err)
373 if (err == GRUB_USB_ERR_STALL)
374 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
375 goto CheckCSW;
377 /* Debug print of sent data. */
378 if (size <= 256)
380 unsigned i;
381 for (i=0; i<size; i++)
382 grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
384 else
385 grub_dprintf ("usb", "Too much data for debug print...\n");
388 /* Read the status - (maybe) according to specification. */
389 CheckCSW:
390 errCSW = grub_usb_bulk_read (dev->dev, dev->in->endp_addr,
391 sizeof (status), (char *) &status);
392 if (errCSW)
394 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
395 errCSW = grub_usb_bulk_read (dev->dev, dev->in->endp_addr,
396 sizeof (status), (char *) &status);
397 if (errCSW)
398 { /* Bulk-only reset device. */
399 grub_dprintf ("usb", "Bulk-only reset device - errCSW\n");
400 grub_usbms_reset (dev);
401 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
402 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
403 goto retry;
407 /* Debug print of CSW content. */
408 grub_dprintf ("usb", "CSW: sign=0x%08x tag=0x%08x resid=0x%08x\n",
409 status.signature, status.tag, status.residue);
410 grub_dprintf ("usb", "CSW: status=0x%02x\n", status.status);
412 /* If phase error or not valid signature, do bulk-only reset device. */
413 if ((status.status == 2) ||
414 (status.signature != grub_cpu_to_le32(0x53425355)))
415 { /* Bulk-only reset device. */
416 grub_dprintf ("usb", "Bulk-only reset device - bad status\n");
417 grub_usbms_reset (dev);
418 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
419 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
421 goto retry;
424 /* If "command failed" status or data transfer failed -> error */
425 if ((status.status || err) && !read_write)
426 return grub_error (GRUB_ERR_READ_ERROR,
427 "error communication with USB Mass Storage device");
428 else if ((status.status || err) && read_write)
429 return grub_error (GRUB_ERR_WRITE_ERROR,
430 "error communication with USB Mass Storage device");
432 return GRUB_ERR_NONE;
435 static grub_err_t
436 grub_usbms_transfer_cbi (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
437 grub_size_t size, char *buf, int read_write)
439 grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
440 int retrycnt = 3 + 1;
441 grub_usb_err_t err = GRUB_USB_ERR_NONE;
442 grub_uint8_t cbicb[GRUB_USBMS_CBI_CMD_SIZE];
443 grub_uint16_t status;
445 retry:
446 retrycnt--;
447 if (retrycnt == 0)
448 return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI failed");
450 /* Setup the request. */
451 grub_memset (cbicb, 0, sizeof (cbicb));
452 grub_memcpy (cbicb, cmd,
453 cmdsize >= GRUB_USBMS_CBI_CMD_SIZE
454 ? GRUB_USBMS_CBI_CMD_SIZE
455 : cmdsize);
457 /* Debug print of CBIcb content. */
458 grub_dprintf ("usb", "cbicb:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
459 cbicb[ 0], cbicb[ 1], cbicb[ 2], cbicb[ 3],
460 cbicb[ 4], cbicb[ 5], cbicb[ 6], cbicb[ 7],
461 cbicb[ 8], cbicb[ 9], cbicb[10], cbicb[11]);
463 /* Write the request.
464 * XXX: Error recovery is maybe not correct. */
465 err = grub_usbms_cbi_cmd (dev->dev, dev->interface, cbicb);
466 if (err)
468 grub_dprintf ("usb", "CBI cmdcb setup err=%d\n", err);
469 if (err == GRUB_USB_ERR_STALL)
471 /* Stall in this place probably means bad or unsupported
472 * command, so we will not try it again. */
473 return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI request failed");
475 else if (dev->protocol == GRUB_USBMS_PROTOCOL_CBI)
477 /* Try to get status from interrupt pipe */
478 err = grub_usb_bulk_read (dev->dev, dev->intrpt->endp_addr,
479 2, (char*)&status);
480 grub_dprintf ("usb", "CBI cmdcb setup status: err=%d, status=0x%x\n", err, status);
482 /* Any other error could be transport problem, try it again */
483 goto retry;
486 /* Read/write the data, (maybe) according to specification. */
487 if (size && (read_write == 0))
489 err = grub_usb_bulk_read (dev->dev, dev->in->endp_addr, size, buf);
490 grub_dprintf ("usb", "read: %d\n", err);
491 if (err)
493 if (err == GRUB_USB_ERR_STALL)
494 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
495 goto retry;
498 else if (size)
500 err = grub_usb_bulk_write (dev->dev, dev->out->endp_addr, size, buf);
501 grub_dprintf ("usb", "write: %d\n", err);
502 if (err)
504 if (err == GRUB_USB_ERR_STALL)
505 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
506 goto retry;
510 /* XXX: It is not clear to me yet, how to check status of CBI
511 * data transfer on devices without interrupt pipe.
512 * AFAIK there is probably no status phase to indicate possibly
513 * bad transported data.
514 * Maybe we should do check on higher level, i.e. issue RequestSense
515 * command (we do it already in scsi.c) and check returned values
516 * (we do not it yet) - ? */
517 if (dev->protocol == GRUB_USBMS_PROTOCOL_CBI)
518 { /* Check status in interrupt pipe */
519 err = grub_usb_bulk_read (dev->dev, dev->intrpt->endp_addr,
520 2, (char*)&status);
521 grub_dprintf ("usb", "read status: %d\n", err);
522 if (err)
524 /* Try to reset device, because it is probably not standard
525 * situation */
526 grub_usbms_reset (dev);
527 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
528 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
529 grub_usb_clear_halt (dev->dev, dev->intrpt->endp_addr);
530 goto retry;
532 if (dev->subclass == GRUB_USBMS_SUBCLASS_UFI)
534 /* These devices should return bASC and bASCQ */
535 if (status != 0)
536 /* Some error, currently we don't care what it is... */
537 goto retry;
539 else if (dev->subclass == GRUB_USBMS_SUBCLASS_RBC)
541 /* XXX: I don't understand what returns RBC subclass devices,
542 * so I don't check it - maybe somebody helps ? */
544 else
546 /* Any other device should return bType = 0 and some bValue */
547 if (status & 0xff)
548 return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI status type != 0");
549 status = (status & 0x0300) >> 8;
550 switch (status)
552 case 0 : /* OK */
553 break;
554 case 1 : /* Fail */
555 goto retry;
556 break;
557 case 2 : /* Phase error */
558 case 3 : /* Persistent Failure */
559 grub_dprintf ("usb", "CBI reset device - phase error or persistent failure\n");
560 grub_usbms_reset (dev);
561 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
562 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
563 grub_usb_clear_halt (dev->dev, dev->intrpt->endp_addr);
564 goto retry;
565 break;
570 return err;
574 static grub_err_t
575 grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
576 grub_size_t size, char *buf, int read_write)
578 grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
580 if (dev->protocol == GRUB_USBMS_PROTOCOL_BULK)
581 return grub_usbms_transfer_bo (scsi, cmdsize, cmd, size, buf,
582 read_write);
583 else
584 return grub_usbms_transfer_cbi (scsi, cmdsize, cmd, size, buf,
585 read_write);
588 static grub_err_t
589 grub_usbms_read (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
590 grub_size_t size, char *buf)
592 return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 0);
595 static grub_err_t
596 grub_usbms_write (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
597 grub_size_t size, const char *buf)
599 return grub_usbms_transfer (scsi, cmdsize, cmd, size, (char *) buf, 1);
602 static grub_err_t
603 grub_usbms_open (int id, int devnum, struct grub_scsi *scsi)
605 if (id != GRUB_SCSI_SUBSYSTEM_USBMS)
606 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
607 "not USB Mass Storage device");
609 grub_usb_poll_devices ();
611 if (!grub_usbms_devices[devnum])
612 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
613 "unknown USB Mass Storage device");
615 scsi->data = grub_usbms_devices[devnum];
616 scsi->luns = grub_usbms_devices[devnum]->luns;
618 return GRUB_ERR_NONE;
621 static struct grub_scsi_dev grub_usbms_dev =
623 .iterate = grub_usbms_iterate,
624 .open = grub_usbms_open,
625 .read = grub_usbms_read,
626 .write = grub_usbms_write
629 static struct grub_usb_attach_desc attach_hook =
631 .class = GRUB_USB_CLASS_MASS_STORAGE,
632 .hook = grub_usbms_attach
635 GRUB_MOD_INIT(usbms)
637 grub_usb_register_attach_hook_class (&attach_hook);
638 grub_scsi_dev_register (&grub_usbms_dev);
641 GRUB_MOD_FINI(usbms)
643 unsigned i;
644 for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
646 grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
647 .interf[grub_usbms_devices[i]->interface].detach_hook = 0;
648 grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
649 .interf[grub_usbms_devices[i]->interface].attached = 0;
651 grub_usb_unregister_attach_hook_class (&attach_hook);
652 grub_scsi_dev_unregister (&grub_usbms_dev);