Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / disk / usbms.c
blob380ca4c4cbf2e7f78a2c491bf1f8f8ecb263b3a8
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 } GRUB_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 } GRUB_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_usb_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_usb_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_usb_err_t
112 grub_usbms_bo_reset (grub_usb_device_t dev, int interface)
114 return grub_usb_control_msg (dev, 0x21, 255, 0, interface, 0, 0);
117 static grub_usb_err_t
118 grub_usbms_reset (grub_usbms_dev_t dev)
120 if (dev->protocol == GRUB_USBMS_PROTOCOL_BULK)
121 return grub_usbms_bo_reset (dev->dev, dev->interface);
122 else
123 return grub_usbms_cbi_reset (dev->dev, dev->interface);
126 static void
127 grub_usbms_detach (grub_usb_device_t usbdev, int config, int interface)
129 unsigned i;
130 for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
131 if (grub_usbms_devices[i] && grub_usbms_devices[i]->dev == usbdev
132 && grub_usbms_devices[i]->interface == interface
133 && grub_usbms_devices[i]->config == config)
135 grub_free (grub_usbms_devices[i]);
136 grub_usbms_devices[i] = 0;
140 static int
141 grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)
143 struct grub_usb_desc_if *interf
144 = usbdev->config[configno].interf[interfno].descif;
145 int j;
146 grub_uint8_t luns = 0;
147 unsigned curnum;
148 grub_usb_err_t err = GRUB_USB_ERR_NONE;
150 grub_boot_time ("Attaching USB mass storage");
152 if (first_available_slot == ARRAY_SIZE (grub_usbms_devices))
153 return 0;
155 curnum = first_available_slot;
156 first_available_slot++;
158 interf = usbdev->config[configno].interf[interfno].descif;
160 if ((interf->subclass != GRUB_USBMS_SUBCLASS_BULK
161 /* Experimental support of RBC, MMC-2, UFI, SFF-8070i devices */
162 && interf->subclass != GRUB_USBMS_SUBCLASS_RBC
163 && interf->subclass != GRUB_USBMS_SUBCLASS_MMC2
164 && interf->subclass != GRUB_USBMS_SUBCLASS_UFI
165 && interf->subclass != GRUB_USBMS_SUBCLASS_SFF8070 )
166 || (interf->protocol != GRUB_USBMS_PROTOCOL_BULK
167 && interf->protocol != GRUB_USBMS_PROTOCOL_CBI
168 && interf->protocol != GRUB_USBMS_PROTOCOL_CB))
169 return 0;
171 grub_usbms_devices[curnum] = grub_zalloc (sizeof (struct grub_usbms_dev));
172 if (! grub_usbms_devices[curnum])
173 return 0;
175 grub_usbms_devices[curnum]->dev = usbdev;
176 grub_usbms_devices[curnum]->interface = interfno;
177 grub_usbms_devices[curnum]->subclass = interf->subclass;
178 grub_usbms_devices[curnum]->protocol = interf->protocol;
180 grub_dprintf ("usbms", "alive\n");
182 /* Iterate over all endpoints of this interface, at least a
183 IN and OUT bulk endpoint are required. */
184 for (j = 0; j < interf->endpointcnt; j++)
186 struct grub_usb_desc_endp *endp;
187 endp = &usbdev->config[0].interf[interfno].descendp[j];
189 if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2)
190 /* Bulk IN endpoint. */
191 grub_usbms_devices[curnum]->in = endp;
192 else if (!(endp->endp_addr & 128) && (endp->attrib & 3) == 2)
193 /* Bulk OUT endpoint. */
194 grub_usbms_devices[curnum]->out = endp;
195 else if ((endp->endp_addr & 128) && (endp->attrib & 3) == 3)
196 /* Interrupt (IN) endpoint. */
197 grub_usbms_devices[curnum]->intrpt = endp;
200 if (!grub_usbms_devices[curnum]->in || !grub_usbms_devices[curnum]->out
201 || ((grub_usbms_devices[curnum]->protocol == GRUB_USBMS_PROTOCOL_CBI)
202 && !grub_usbms_devices[curnum]->intrpt))
204 grub_free (grub_usbms_devices[curnum]);
205 grub_usbms_devices[curnum] = 0;
206 return 0;
209 grub_dprintf ("usbms", "alive\n");
211 /* XXX: Activate the first configuration. */
212 grub_usb_set_configuration (usbdev, 1);
214 /* Query the amount of LUNs. */
215 if (grub_usbms_devices[curnum]->protocol == GRUB_USBMS_PROTOCOL_BULK)
216 { /* Only Bulk only devices support Get Max LUN command */
217 err = grub_usb_control_msg (usbdev, 0xA1, 254, 0, interfno, 1, (char *) &luns);
219 if (err)
221 /* In case of a stall, clear the stall. */
222 if (err == GRUB_USB_ERR_STALL)
224 grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->in->endp_addr);
225 grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->out->endp_addr);
227 /* Just set the amount of LUNs to one. */
228 grub_errno = GRUB_ERR_NONE;
229 grub_usbms_devices[curnum]->luns = 1;
231 else
232 /* luns = 0 means one LUN with ID 0 present ! */
233 /* We get from device not number of LUNs but highest
234 * LUN number. LUNs are numbered from 0,
235 * i.e. number of LUNs is luns+1 ! */
236 grub_usbms_devices[curnum]->luns = luns + 1;
238 else
239 /* XXX: Does CBI devices support multiple LUNs ?
240 * I.e., should we detect number of device's LUNs ? (How?) */
241 grub_usbms_devices[curnum]->luns = 1;
243 grub_dprintf ("usbms", "alive\n");
245 usbdev->config[configno].interf[interfno].detach_hook = grub_usbms_detach;
247 grub_boot_time ("Attached USB mass storage");
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 (grub_scsi_dev_iterate_hook_t hook, void *hook_data,
269 grub_disk_pull_t pull)
271 unsigned i;
273 if (pull != GRUB_DISK_PULL_NONE)
274 return 0;
276 grub_usb_poll_devices (1);
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 hook_data))
283 return 1;
286 return 0;
289 static grub_err_t
290 grub_usbms_transfer_bo (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
291 grub_size_t size, char *buf, int read_write)
293 struct grub_usbms_cbw cbw;
294 grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
295 struct grub_usbms_csw status;
296 static grub_uint32_t tag = 0;
297 grub_usb_err_t err = GRUB_USB_ERR_NONE;
298 grub_usb_err_t errCSW = GRUB_USB_ERR_NONE;
299 int retrycnt = 3 + 1;
301 tag++;
303 retry:
304 retrycnt--;
305 if (retrycnt == 0)
306 return grub_error (GRUB_ERR_IO, "USB Mass Storage stalled");
308 /* Setup the request. */
309 grub_memset (&cbw, 0, sizeof (cbw));
310 cbw.signature = grub_cpu_to_le32_compile_time (0x43425355);
311 cbw.tag = tag;
312 cbw.transfer_length = grub_cpu_to_le32 (size);
313 cbw.flags = (!read_write) << GRUB_USBMS_DIRECTION_BIT;
314 cbw.lun = scsi->lun; /* In USB MS CBW are LUN bits on another place than in SCSI CDB, both should be set correctly. */
315 cbw.length = cmdsize;
316 grub_memcpy (cbw.cbwcb, cmd, cmdsize);
318 /* Debug print of CBW content. */
319 grub_dprintf ("usb", "CBW: sign=0x%08x tag=0x%08x len=0x%08x\n",
320 cbw.signature, cbw.tag, cbw.transfer_length);
321 grub_dprintf ("usb", "CBW: flags=0x%02x lun=0x%02x CB_len=0x%02x\n",
322 cbw.flags, cbw.lun, cbw.length);
323 grub_dprintf ("usb", "CBW: cmd:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
324 cbw.cbwcb[ 0], cbw.cbwcb[ 1], cbw.cbwcb[ 2], cbw.cbwcb[ 3],
325 cbw.cbwcb[ 4], cbw.cbwcb[ 5], cbw.cbwcb[ 6], cbw.cbwcb[ 7],
326 cbw.cbwcb[ 8], cbw.cbwcb[ 9], cbw.cbwcb[10], cbw.cbwcb[11],
327 cbw.cbwcb[12], cbw.cbwcb[13], cbw.cbwcb[14], cbw.cbwcb[15]);
329 /* Write the request.
330 * XXX: Error recovery is maybe still not fully correct. */
331 err = grub_usb_bulk_write (dev->dev, dev->out,
332 sizeof (cbw), (char *) &cbw);
333 if (err)
335 if (err == GRUB_USB_ERR_STALL)
337 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
338 goto CheckCSW;
340 goto retry;
343 /* Read/write the data, (maybe) according to specification. */
344 if (size && (read_write == 0))
346 err = grub_usb_bulk_read (dev->dev, dev->in, size, buf);
347 grub_dprintf ("usb", "read: %d %d\n", err, GRUB_USB_ERR_STALL);
348 if (err)
350 if (err == GRUB_USB_ERR_STALL)
351 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
352 goto CheckCSW;
354 /* Debug print of received data. */
355 grub_dprintf ("usb", "buf:\n");
356 if (size <= 64)
358 unsigned i;
359 for (i = 0; i < size; i++)
360 grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
362 else
363 grub_dprintf ("usb", "Too much data for debug print...\n");
365 else if (size)
367 err = grub_usb_bulk_write (dev->dev, dev->out, size, buf);
368 grub_dprintf ("usb", "write: %d %d\n", err, GRUB_USB_ERR_STALL);
369 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",
370 buf[ 0], buf[ 1], buf[ 2], buf[ 3],
371 buf[ 4], buf[ 5], buf[ 6], buf[ 7],
372 buf[ 8], buf[ 9], buf[10], buf[11],
373 buf[12], buf[13], buf[14], buf[15]);
374 if (err)
376 if (err == GRUB_USB_ERR_STALL)
377 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
378 goto CheckCSW;
380 /* Debug print of sent data. */
381 if (size <= 256)
383 unsigned i;
384 for (i=0; i<size; i++)
385 grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
387 else
388 grub_dprintf ("usb", "Too much data for debug print...\n");
391 /* Read the status - (maybe) according to specification. */
392 CheckCSW:
393 errCSW = grub_usb_bulk_read (dev->dev, dev->in,
394 sizeof (status), (char *) &status);
395 if (errCSW)
397 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
398 errCSW = grub_usb_bulk_read (dev->dev, dev->in,
399 sizeof (status), (char *) &status);
400 if (errCSW)
401 { /* Bulk-only reset device. */
402 grub_dprintf ("usb", "Bulk-only reset device - errCSW\n");
403 grub_usbms_reset (dev);
404 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
405 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
406 goto retry;
410 /* Debug print of CSW content. */
411 grub_dprintf ("usb", "CSW: sign=0x%08x tag=0x%08x resid=0x%08x\n",
412 status.signature, status.tag, status.residue);
413 grub_dprintf ("usb", "CSW: status=0x%02x\n", status.status);
415 /* If phase error or not valid signature, do bulk-only reset device. */
416 if ((status.status == 2) ||
417 (status.signature != grub_cpu_to_le32_compile_time(0x53425355)))
418 { /* Bulk-only reset device. */
419 grub_dprintf ("usb", "Bulk-only reset device - bad status\n");
420 grub_usbms_reset (dev);
421 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
422 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
424 goto retry;
427 /* If "command failed" status or data transfer failed -> error */
428 if ((status.status || err) && !read_write)
429 return grub_error (GRUB_ERR_READ_ERROR,
430 "error communication with USB Mass Storage device");
431 else if ((status.status || err) && read_write)
432 return grub_error (GRUB_ERR_WRITE_ERROR,
433 "error communication with USB Mass Storage device");
435 return GRUB_ERR_NONE;
438 static grub_err_t
439 grub_usbms_transfer_cbi (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
440 grub_size_t size, char *buf, int read_write)
442 grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
443 int retrycnt = 3 + 1;
444 grub_usb_err_t err = GRUB_USB_ERR_NONE;
445 grub_uint8_t cbicb[GRUB_USBMS_CBI_CMD_SIZE];
446 grub_uint16_t status;
448 retry:
449 retrycnt--;
450 if (retrycnt == 0)
451 return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI failed");
453 /* Setup the request. */
454 grub_memset (cbicb, 0, sizeof (cbicb));
455 grub_memcpy (cbicb, cmd,
456 cmdsize >= GRUB_USBMS_CBI_CMD_SIZE
457 ? GRUB_USBMS_CBI_CMD_SIZE
458 : cmdsize);
460 /* Debug print of CBIcb content. */
461 grub_dprintf ("usb", "cbicb:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
462 cbicb[ 0], cbicb[ 1], cbicb[ 2], cbicb[ 3],
463 cbicb[ 4], cbicb[ 5], cbicb[ 6], cbicb[ 7],
464 cbicb[ 8], cbicb[ 9], cbicb[10], cbicb[11]);
466 /* Write the request.
467 * XXX: Error recovery is maybe not correct. */
468 err = grub_usbms_cbi_cmd (dev->dev, dev->interface, cbicb);
469 if (err)
471 grub_dprintf ("usb", "CBI cmdcb setup err=%d\n", err);
472 if (err == GRUB_USB_ERR_STALL)
474 /* Stall in this place probably means bad or unsupported
475 * command, so we will not try it again. */
476 return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI request failed");
478 else if (dev->protocol == GRUB_USBMS_PROTOCOL_CBI)
480 /* Try to get status from interrupt pipe */
481 err = grub_usb_bulk_read (dev->dev, dev->intrpt,
482 2, (char*)&status);
483 grub_dprintf ("usb", "CBI cmdcb setup status: err=%d, status=0x%x\n", err, status);
485 /* Any other error could be transport problem, try it again */
486 goto retry;
489 /* Read/write the data, (maybe) according to specification. */
490 if (size && (read_write == 0))
492 err = grub_usb_bulk_read (dev->dev, dev->in, size, buf);
493 grub_dprintf ("usb", "read: %d\n", err);
494 if (err)
496 if (err == GRUB_USB_ERR_STALL)
497 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
498 goto retry;
501 else if (size)
503 err = grub_usb_bulk_write (dev->dev, dev->out, size, buf);
504 grub_dprintf ("usb", "write: %d\n", err);
505 if (err)
507 if (err == GRUB_USB_ERR_STALL)
508 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
509 goto retry;
513 /* XXX: It is not clear to me yet, how to check status of CBI
514 * data transfer on devices without interrupt pipe.
515 * AFAIK there is probably no status phase to indicate possibly
516 * bad transported data.
517 * Maybe we should do check on higher level, i.e. issue RequestSense
518 * command (we do it already in scsi.c) and check returned values
519 * (we do not it yet) - ? */
520 if (dev->protocol == GRUB_USBMS_PROTOCOL_CBI)
521 { /* Check status in interrupt pipe */
522 err = grub_usb_bulk_read (dev->dev, dev->intrpt,
523 2, (char*)&status);
524 grub_dprintf ("usb", "read status: %d\n", err);
525 if (err)
527 /* Try to reset device, because it is probably not standard
528 * situation */
529 grub_usbms_reset (dev);
530 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
531 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
532 grub_usb_clear_halt (dev->dev, dev->intrpt->endp_addr);
533 goto retry;
535 if (dev->subclass == GRUB_USBMS_SUBCLASS_UFI)
537 /* These devices should return bASC and bASCQ */
538 if (status != 0)
539 /* Some error, currently we don't care what it is... */
540 goto retry;
542 else if (dev->subclass == GRUB_USBMS_SUBCLASS_RBC)
544 /* XXX: I don't understand what returns RBC subclass devices,
545 * so I don't check it - maybe somebody helps ? */
547 else
549 /* Any other device should return bType = 0 and some bValue */
550 if (status & 0xff)
551 return grub_error (GRUB_ERR_IO, "USB Mass Storage CBI status type != 0");
552 status = (status & 0x0300) >> 8;
553 switch (status)
555 case 0 : /* OK */
556 break;
557 case 1 : /* Fail */
558 goto retry;
559 break;
560 case 2 : /* Phase error */
561 case 3 : /* Persistent Failure */
562 grub_dprintf ("usb", "CBI reset device - phase error or persistent failure\n");
563 grub_usbms_reset (dev);
564 grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
565 grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
566 grub_usb_clear_halt (dev->dev, dev->intrpt->endp_addr);
567 goto retry;
568 break;
573 if (err)
574 return grub_error (GRUB_ERR_IO, "USB error %d", err);
576 return GRUB_ERR_NONE;
580 static grub_err_t
581 grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
582 grub_size_t size, char *buf, int read_write)
584 grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
586 if (dev->protocol == GRUB_USBMS_PROTOCOL_BULK)
587 return grub_usbms_transfer_bo (scsi, cmdsize, cmd, size, buf,
588 read_write);
589 else
590 return grub_usbms_transfer_cbi (scsi, cmdsize, cmd, size, buf,
591 read_write);
594 static grub_err_t
595 grub_usbms_read (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
596 grub_size_t size, char *buf)
598 return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 0);
601 static grub_err_t
602 grub_usbms_write (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
603 grub_size_t size, const char *buf)
605 return grub_usbms_transfer (scsi, cmdsize, cmd, size, (char *) buf, 1);
608 static grub_err_t
609 grub_usbms_open (int id, int devnum, struct grub_scsi *scsi)
611 if (id != GRUB_SCSI_SUBSYSTEM_USBMS)
612 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
613 "not USB Mass Storage device");
615 if (!grub_usbms_devices[devnum])
616 grub_usb_poll_devices (1);
618 if (!grub_usbms_devices[devnum])
619 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
620 "unknown USB Mass Storage device");
622 scsi->data = grub_usbms_devices[devnum];
623 scsi->luns = grub_usbms_devices[devnum]->luns;
625 return GRUB_ERR_NONE;
628 static struct grub_scsi_dev grub_usbms_dev =
630 .iterate = grub_usbms_iterate,
631 .open = grub_usbms_open,
632 .read = grub_usbms_read,
633 .write = grub_usbms_write
636 static struct grub_usb_attach_desc attach_hook =
638 .class = GRUB_USB_CLASS_MASS_STORAGE,
639 .hook = grub_usbms_attach
642 GRUB_MOD_INIT(usbms)
644 grub_usb_register_attach_hook_class (&attach_hook);
645 grub_scsi_dev_register (&grub_usbms_dev);
648 GRUB_MOD_FINI(usbms)
650 unsigned i;
651 for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
653 grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
654 .interf[grub_usbms_devices[i]->interface].detach_hook = 0;
655 grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
656 .interf[grub_usbms_devices[i]->interface].attached = 0;
658 grub_usb_unregister_attach_hook_class (&attach_hook);
659 grub_scsi_dev_unregister (&grub_usbms_dev);